Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,7 +37,6 @@
from mkdist import do_copy_file
from options import AddOptions


BuildOptions = {}
Projects = []
Rtt_Root = ''
Expand Down Expand Up @@ -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
Expand Down
55 changes: 55 additions & 0 deletions tools/env.py
Original file line number Diff line number Diff line change
@@ -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
14 changes: 13 additions & 1 deletion tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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])