1import os
2import sys
3import rtconfig
4import subprocess
5
6IS_EXPORTED = False
7
8# setup RT-Thread Root Path
9if os.getenv('RTT_ROOT'):
10    RTT_ROOT = os.getenv('RTT_ROOT')
11else:
12    RTT_ROOT = os.path.join(os.getcwd(),'..','..','..')
13
14sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
15try:
16    from building import *
17except Exception as e:
18    print('Cannot found RT-Thread root directory, please check RTT_ROOT')
19    exit(-1)
20
21if RTT_ROOT == 'rt-thread':
22    IS_EXPORTED = True # if kenrel and bsp has been exported by export_project.py
23
24# setup Phytium BSP Root Path
25if IS_EXPORTED:
26    BSP_ROOT = '.'
27else:
28    BSP_ROOT = os.path.join(RTT_ROOT ,'bsp','phytium')
29
30TARGET = 'rtthread_a64.' + rtconfig.TARGET_EXT
31
32DefaultEnvironment(tools=[])
33env = Environment(tools = ['mingw'],
34    AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
35    CC = rtconfig.CC, CFLAGS = rtconfig.CFLAGS,
36    CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
37    AR = rtconfig.AR, ARFLAGS = '-rc',
38    LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
39env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
40env['ASCOM'] = env['ASPPCOM']
41
42os.environ["BSP_ROOT"] = BSP_ROOT
43os.environ["SDK_DIR"] = os.path.join(BSP_ROOT,'libraries','phytium_standalone_sdk')
44
45
46Export('RTT_ROOT')
47Export('BSP_ROOT')
48Export('rtconfig')
49
50def is_phytium_sdk_installed():
51    py_target_folder =  os.path.join(BSP_ROOT,"libraries","phytium_standalone_sdk")
52    return os.path.exists(py_target_folder)
53
54def install_phytium_sdk():
55    if is_phytium_sdk_installed():
56        return 0
57
58    install_script_path = os.path.join(BSP_ROOT,"libraries","phytium_standalone_sdk_install.py")
59
60    if os.path.exists(install_script_path):
61        try:
62            subprocess.call(["python", install_script_path])
63        except:
64            subprocess.call(["python3", install_script_path])
65
66        if not is_phytium_sdk_installed():
67            print("Error: phytium_standalone_sdk install failed")
68            exit(0)
69    else:
70        print("Error: phytium_standalone_sdk_install.py is not exists, exit compilation")
71        exit(0)
72
73install_phytium_sdk()
74
75# prepare building environment
76objs = PrepareBuilding(env, RTT_ROOT, has_libcpu = False)
77
78if not IS_EXPORTED: # if project is not exported, libraries and board need to manually add
79    # include libraries
80    objs.extend(SConscript(os.path.join(BSP_ROOT,'libraries','SConscript')))
81
82    # include board
83    objs.extend(SConscript(os.path.join(BSP_ROOT,'board','SConscript')))
84
85# make a building
86DoBuilding(TARGET, objs)
87
88