# File : SConstruct.py # Building Script for User Applications # This file is part of RT-Thread RTOS # COPYRIGHT (C) 2006 - 2015, RT-Thread Development Team # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # # Change Logs: # Date Author Notes # 2015-02-07 Bernard The firstly version # 2018-04-15 Bernard Add the build option for library # import os import sys import platform import shutil # 0. path variable cwd = os.getcwd() tools_path = os.path.join(cwd, 'tools') sdk_path = os.path.join(tools_path, 'sdk') # luban-Lite root directory AIC_ROOT = os.path.join(cwd, '../../../') AIC_ROOT = os.path.normpath(AIC_ROOT) config_path = os.path.join(AIC_ROOT, '.config') toolchain_path = os.path.join(cwd, 'toolchain') os.environ["M_EXEC_PATH"] = os.path.join(toolchain_path, 'bin') if os.path.exists(config_path): # luban-lite custom scripts aic_script_path = os.path.join(AIC_ROOT, 'tools/scripts/') sys.path.append(aic_script_path) from aic_build import * PRJ_CHIP,PRJ_BOARD,PRJ_KERNEL,PRJ_APP,PRJ_DEFCONFIG_NAME,PRJ_CUSTOM_LDS,MKIMAGE_POST_ACTION = get_prj_config(AIC_ROOT) PRJ_NAME = PRJ_DEFCONFIG_NAME.replace('_defconfig','') PRJ_OUT_DIR = 'output/' + PRJ_NAME + '/images/' # 1. luban-lite paths bsp_root = AIC_ROOT rtt_root = os.path.join(AIC_ROOT, 'kernel/rt-thread/') rtt_root = os.path.normpath(rtt_root) chip_dir = os.path.join(AIC_ROOT, 'bsp/artinchip/sys/' + PRJ_CHIP) chip_dir = os.path.normpath(chip_dir) prj_out_dir = os.path.join(AIC_ROOT, PRJ_OUT_DIR) prj_out_dir = os.path.normpath(prj_out_dir) # 2. make local sdk AddOption('--target', dest = 'target', type = 'string', help = 'Generate target project: sdk') if GetOption('target') == 'sdk': if not os.path.exists(sdk_path): os.mkdir(sdk_path) else: shutil.rmtree(sdk_path) os.mkdir(sdk_path) # copy rtconfig.h print('Copy rtconfig.h...') src_f = os.path.join(AIC_ROOT, 'rtconfig.h') des_f = os.path.join(sdk_path, 'rtconfig.h') shutil.copy(src_f, des_f) # copy rtconfig.py print('Copy rtconfig.py...') src_f = os.path.join(chip_dir, 'rtconfig.py') des_f = os.path.join(sdk_path, 'rtconfig.py') shutil.copy(src_f, des_f) # copy rtua.py print('Copy rtua.py...') src_f = os.path.join(prj_out_dir, 'rtua.py') des_f = os.path.join(sdk_path, 'rtua.py') shutil.copy(src_f, des_f) # copy rt-thread/tools/ print('Copy rt-thread/tools/...') src_d = os.path.join(rtt_root, 'tools') des_d = os.path.join(sdk_path, 'tools') src_d = os.path.normpath(src_d); des_d = os.path.normpath(des_d); if not os.path.exists(des_d): if platform.system() == 'Linux': shutil.copytree(src_d, des_d) elif platform.system() == 'Windows': shutil.copytree('\\\\?\\' + src_d, '\\\\?\\' + des_d) # copy tools/env/ print('Copy tools/env/...') src_d = os.path.join(AIC_ROOT, 'tools/env') des_d = os.path.join(tools_path, 'env') src_d = os.path.normpath(src_d); des_d = os.path.normpath(des_d); if os.path.exists(des_d): shutil.rmtree(des_d) if not os.path.exists(des_d): if platform.system() == 'Linux': shutil.copytree(src_d, des_d) elif platform.system() == 'Windows': shutil.copytree('\\\\?\\' + src_d, '\\\\?\\' + des_d) # copy tools/scripts/ print('Copy tools/scripts/...') src_d = os.path.join(AIC_ROOT, 'tools/scripts') des_d = os.path.join(tools_path, 'scripts') src_d = os.path.normpath(src_d); des_d = os.path.normpath(des_d); if os.path.exists(des_d): shutil.rmtree(des_d) if not os.path.exists(des_d): if platform.system() == 'Linux': shutil.copytree(src_d, des_d) elif platform.system() == 'Windows': shutil.copytree('\\\\?\\' + src_d, '\\\\?\\' + des_d) # copy onestep.sh print('Copy onestep.sh...') src_f = os.path.join(AIC_ROOT, 'tools/onestep.sh') des_f = os.path.join(tools_path, 'onestep.sh') if os.path.exists(des_f): os.remove(des_f) shutil.copy(src_f, des_f) # copy win_env.bat print('Copy win_env.bat...') src_f = os.path.join(AIC_ROOT, 'win_env.bat') des_f = os.path.join(cwd, 'win_env.bat') if os.path.exists(des_f): os.remove(des_f) shutil.copy(src_f, des_f) # copy win_cmd.bat print('Copy win_cmd.bat...') src_f = os.path.join(AIC_ROOT, 'win_cmd.bat') des_f = os.path.join(cwd, 'win_cmd.bat') if os.path.exists(des_f): os.remove(des_f) shutil.copy(src_f, des_f) print('Build local sdk succeed!') exit(0) # check local sdk if os.path.exists(sdk_path): # local dm-app sdk # 3. local sdk paths bsp_root = sdk_path rtt_root = sdk_path chip_dir = sdk_path prj_out_dir = os.path.join(tools_path, 'scripts') if not os.path.exists(config_path) and not os.path.exists(sdk_path): print('Error: no luban-lite sdk!') exit(-1) else: # 4. check & prepare toolchain toolchain_prefix = 'xpack-riscv-none-embed-gcc' if platform.system() == 'Linux': toolchain_suffix = 'linux-x64.tar.gz' elif platform.system() == 'Windows': toolchain_suffix = 'win32-x64.tar.gz' toolchain_ppath = os.path.join(cwd, 'tools/toolchain/') toolchain_ppath = os.path.normpath(toolchain_ppath) ready_f = os.path.join(toolchain_path, '.ready') if os.path.exists(ready_f): with open(ready_f, 'r') as f: toolchain_os = f.read() if platform.system() != toolchain_os: shutil.rmtree(toolchain_path) if not os.path.exists(ready_f): for root, dirs, files in os.walk(toolchain_ppath): if root != toolchain_ppath: break for f in files: if f.startswith(toolchain_prefix) and f.endswith(toolchain_suffix): abs_f = os.path.join(toolchain_ppath, f) print('Extract toolchain ' + toolchain_prefix + ' ...') if not os.path.exists(toolchain_path): os.mkdir(toolchain_path) os.system('tar -xzf ' + abs_f \ + ' --strip-components 1 -C ' + toolchain_path) with open(ready_f, 'w') as f: f.write(platform.system()) break # 5. compile dynamic module app from tools.ua import BuildApplication from tools.ua import BuildLibrary AddOption('--app', dest='app', nargs=1, type='string', action='store', metavar='DIR', help='application to be built') AddOption('--lib', dest='lib', nargs=1, type='string', action='store', metavar='DIR', help='library to be built') app = GetOption('app') lib = GetOption('lib') if app == None and lib == None: print("none application or library, please use ") print(" scons --app=your_app") print(" scons --lib=your_lib") exit(0) else: if app: BuildApplication(app + '/' + app + '.mo', app +'/SConscript', bsp_root, rtt_root, chip_dir, prj_out_dir) if lib: BuildLibrary(lib + '/' + lib + '.so', lib +'/SConscript', bsp_root, rtt_root, chip_dir, prj_out_dir)