1import os
2import sys
3import rtconfig
4import platform
5import subprocess
6
7from rtconfig import RTT_ROOT
8import sys
9
10def generate_ldscript(input, output):
11    if not os.path.exists(input):
12        print('Error: file', input, 'not found')
13        return
14
15    if os.path.exists(output):
16        os.remove(output)
17
18    if rtconfig.PLATFORM == 'gcc':
19
20        gcc_cmd = os.path.join(rtconfig.EXEC_PATH, rtconfig.CC)
21
22        # gcc -E -P -x c $input -o $output
23        if (platform.system() == 'Windows'):
24            child = subprocess.Popen([gcc_cmd, '-E', '-P', '-x', 'c', input, '-o', output], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
25        else:
26            child = subprocess.Popen(gcc_cmd + f' -E -P -x c {input} -o {output}', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
27
28        child.communicate()
29
30        print(output, 'is generated from', input)
31
32sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
33from building import *
34
35TARGET = 'rtthread.' + rtconfig.TARGET_EXT
36
37# apply soft-FPU config
38options = LocalOptions("rtconfig.h")
39soft_fpu = GetLocalDepend(options, 'BSP_RISCV_FPU_SOFT')
40if soft_fpu:
41    rtconfig.DEVICE = rtconfig.DEVICE.replace('-march=rv64imafdcv -mabi=lp64d', '-march=rv64imafdc -mabi=lp64')
42    rtconfig.CFLAGS = rtconfig.CFLAGS.replace('-march=rv64imafdcv -mabi=lp64d', '-march=rv64imafdc -mabi=lp64')
43    rtconfig.LFLAGS = rtconfig.LFLAGS.replace('-march=rv64imafdcv -mabi=lp64d', '-march=rv64imafdc -mabi=lp64')
44    rtconfig.AFLAGS = rtconfig.AFLAGS.replace('-march=rv64imafdcv -mabi=lp64d', '-march=rv64imafdc -mabi=lp64')
45
46DefaultEnvironment(tools=[])
47env = Environment(tools = ['mingw'],
48    AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
49    CC = rtconfig.CC, CCFLAGS = rtconfig.CFLAGS,
50    CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
51    AR = rtconfig.AR, ARFLAGS = '-rc',
52    LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
53env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
54env['ASCOM'] = env['ASPPCOM']
55
56Export('RTT_ROOT')
57Export('rtconfig')
58
59# prepare building environment
60objs = PrepareBuilding(env, RTT_ROOT, has_libcpu = False)
61
62generate_ldscript('link.lds', 'link.lds.generated')
63
64# make a building
65DoBuilding(TARGET, objs)
66
67Clean(TARGET, 'link.lds.generated')
68Clean(TARGET, 'build')
69