diff --git a/tools/building.py b/tools/building.py index 797ffc0076..3b85bc9930 100644 --- a/tools/building.py +++ b/tools/building.py @@ -22,7 +22,7 @@ # 2015-01-20 Bernard Add copyright information # 2015-07-25 Bernard Add LOCAL_CCFLAGS/LOCAL_CPPPATH/LOCAL_CPPDEFINES for # group definition. -# +# 2024-04-21 Bernard Add toolchain detection in sdk packages import os import sys @@ -37,7 +37,6 @@ from utils import _make_path_relative from mkdist import do_copy_file from options import AddOptions - BuildOptions = {} Projects = [] Rtt_Root = '' @@ -201,6 +200,22 @@ def PrepareBuilding(env, root_directory, has_libcpu=False, remove_components = [ # del the 'RTT_EXEC_PATH' and using the 'EXEC_PATH' setting on rtconfig.py del os.environ['RTT_EXEC_PATH'] + try: + # try to detect toolchains in env + from utils import ImportModule + envm = ImportModule('env') + # from env import GetSDKPath + exec_path = envm.GetSDKPath(rtconfig.CC) + if 'gcc' in rtconfig.CC: + exec_path = os.path.join(exec_path, 'bin') + if os.path.exists(exec_path): + print('set CC to ' + exec_path) + rtconfig.EXEC_PATH = exec_path + os.environ['RTT_EXEC_PATH'] = exec_path + except Exception as e: + # detect failed, ignore + pass + exec_path = GetOption('exec-path') if exec_path: os.environ['RTT_EXEC_PATH'] = exec_path diff --git a/tools/env.py b/tools/env.py new file mode 100644 index 0000000000..995cfd135b --- /dev/null +++ b/tools/env.py @@ -0,0 +1,55 @@ +#! /usr/bin/env python +#coding=utf-8 +# +# Copyright (c) 2024, RT-Thread Development Team +# +# SPDX-License-Identifier: GPL-2.0 +# +# Change Logs: +# Date Author Notes +# 2024-04-20 Bernard the first version + +import os +import json +import platform + +def GetEnvPath(): + env = os.environ.get('ENV_ROOT') + + if env is None: + if platform.system() == 'Windows': + return os.path.join(os.environ['USERPROFILE'], '.env') + else: + return os.path.join(os.environ['HOME'], '.env') + + return env + +def GetSDKPackage(): + env = GetEnvPath() + + if env: + return os.path.join(env, 'tools', 'packages') + + return None + +def GetSDKPath(name): + env = GetEnvPath() + + if env: + #read packages.json under env/tools/packages + with open(os.path.join(env, 'tools', 'packages', 'pkgs.json'), 'r', encoding='utf-8') as f: + # packages_json = f.read() + packages = json.load(f) + + for item in packages: + package_path = os.path.join(GetEnvPath(), 'packages', item['path'], 'package.json') + # read package['path']/package.json under env/packages + with open(package_path, 'r', encoding='utf-8') as f: + # package_json = f.read() + package = json.load(f) + + if package['name'] == name: + return os.path.join(GetSDKPackage(), package['name'] + '-' + item['ver']) + + # not found named package + return None diff --git a/tools/utils.py b/tools/utils.py index 2d88176417..7be58b9db9 100644 --- a/tools/utils.py +++ b/tools/utils.py @@ -20,7 +20,7 @@ # Change Logs: # Date Author Notes # 2015-01-20 Bernard Add copyright information -# +# 2024-04-21 Bernard Add ImportModule to import local module import sys import os @@ -291,3 +291,15 @@ def ReloadModule(module): importlib.reload(module) else: reload(module) + +def ImportModule(module): + import sys + if sys.version_info.major >= 3: + import importlib.util + path = os.path.dirname(__file__) + spec = importlib.util.spec_from_file_location(module, os.path.join(path, module+".py")) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + else: + return __import__(module, fromlist=[module])