-
Notifications
You must be signed in to change notification settings - Fork 25
/
SConstruct
415 lines (372 loc) · 13.7 KB
/
SConstruct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import os, sys
try:
import ConfigParser
except:
import configparser
from io import StringIO
sys.path.insert(0, 'src/tools')
import install
target = os.environ.get('M3_TARGET')
if target == 't2' or target == 't3':
toolversion = 'RE-2014.5-linux' if target == 't3' else 'RD-2011.2-linux'
# config (prefix it with [root] to make it usable from bash and python)
ini_str = '[root]\n' + open('hw/th/config.ini', 'r').read()
config = configparser.RawConfigParser()
config.readfp(StringIO.StringIO(ini_str))
if target == 't3':
core = 'Pe_4MB_128k_4irq'
else:
core = 'oi_lx4_PE_6'
isa = 'xtensa'
cross = 'xtensa-buildroot-linux-uclibc-'
crossdir = Dir(config.get('root', 'buildroot')).abspath + '/host/usr/'
crossver = '5.3.0'
runtime = 'sim' if target == 't3' else 'min-rt'
configpath = Dir(config.get('root', 'cfgpath'))
xtroot = Dir(config.get('root', 'xtroot'))
tooldir = Dir(xtroot.abspath + '/XtDevTools/install/tools/' + toolversion + '/XtensaTools/bin')
crtdir = crossdir + '/lib/gcc/' + cross[:-1] + '/' + crossver
elif target == 'gem5':
isa = os.environ.get('M3_ISA')
if isa is None:
isa = 'x86_64'
if isa == 'arm':
rustabi = 'gnueabihf'
cross = 'arm-none-eabi-'
else:
rustabi = 'gnu'
cross = ''
configpath = Dir('.')
else:
# build for host by default
isa = os.popen("uname -m").read().strip()
if isa == 'armv7l':
isa = 'arm'
target = 'host'
cross = ''
rustabi = 'gnu'
configpath = Dir('.')
# build basic environment
baseenv = Environment(
CPPFLAGS = '-D__' + target + '__',
CXXFLAGS = ' -std=c++11 -Wall -Wextra -Wsign-conversion',
CFLAGS = ' -std=c99 -Wall -Wextra -Wsign-conversion',
CPPPATH = ['#src/include'],
ENV = {
'PATH' : os.environ['PATH'],
# required for colored outputs
'HOME' : os.environ['HOME'],
'TERM' : os.environ['TERM'],
}
)
def CheckCompilerParam(context, param):
context.Message('Checking for parameter "' + param + '"...')
result = context.TryAction(cross + 'gcc ' + param + ' -c -xc++ /dev/null -o /dev/null')[0]
context.Result(result)
return result
conf = Configure(baseenv, custom_tests={'CheckCompilerParam': CheckCompilerParam})
if conf.CheckCompilerParam('-fdiagnostics-color=always'):
baseenv.Append(CXXFLAGS = ' -fdiagnostics-color=always')
conf.Finish()
# print executed commands?
verbose = os.environ.get('M3_VERBOSE', 0)
if int(verbose) == 0:
baseenv['INSTALLSTR'] = "[INSTALL] $TARGET"
baseenv['ASPPCOMSTR'] = "[AS ] $TARGET"
baseenv['ASPPCOMSTR'] = "[ASPP ] $TARGET"
baseenv['CCCOMSTR'] = "[CC ] $TARGET"
baseenv['SHCCCOMSTR'] = "[SHCC ] $TARGET"
baseenv['CXXCOMSTR'] = "[CXX ] $TARGET"
baseenv['SHCXXCOMSTR'] = "[SHCXX ] $TARGET"
baseenv['LINKCOMSTR'] = "[LD ] $TARGET"
baseenv['SHLINKCOMSTR'] = "[SHLD ] $TARGET"
baseenv['ARCOMSTR'] = "[AR ] $TARGET"
baseenv['RANLIBCOMSTR'] = "[RANLIB ] $TARGET"
baseenv['F90COMSTR'] = "[FC ] $TARGET"
baseenv['MDUMPCOMSTR'] = "[MDUMP ] $TARGET"
baseenv['STRIPCOMSTR'] = "[STRIP ] $TARGET"
baseenv['DUMPCOMSTR'] = "[DUMP ] $TARGET"
baseenv['MKFSCOMSTR'] = "[MKFS ] $TARGET"
baseenv['CPPCOMSTR'] = "[CPP ] $TARGET"
baseenv['CRGCOMSTR'] = "[CARGO ] $TARGET"
# for host compilation
hostenv = baseenv.Clone()
hostenv.Append(
CPPFLAGS = ' -D__tools__'
)
# for target compilation
env = baseenv.Clone()
env.Append(
CXXFLAGS = ' -ffreestanding -fno-strict-aliasing -fno-exceptions -fno-rtti -gdwarf-2' \
' -fno-threadsafe-statics -fno-stack-protector',
CPPFLAGS = ' -U_FORTIFY_SOURCE',
CFLAGS = ' -gdwarf-2 -fno-stack-protector',
ASFLAGS = ' -Wl,-W -Wall -Wextra',
LINKFLAGS = ' -fno-exceptions -fno-rtti -Wl,--no-gc-sections -Wno-lto-type-mismatch' \
' -fno-stack-protector',
CRGFLAGS = ' --target ' + isa + '-unknown-' + target + '-' + rustabi,
)
env.Append(ENV = {
'RUST_TARGET_PATH' : Dir('src/toolchain/rust').abspath
})
if int(verbose) != 0:
env.Append(CRGFLAGS = ' -v')
# allow to add preprocessor flags via env variable
cppdefines = []
for flag in os.environ.get('M3_CFLAGS', '').split():
cppdefines.append(flag)
env.Append(CPPDEFINES = cppdefines)
# add target-dependent stuff to env
if target == 't2' or target == 't3':
env.Append(
# align it appropriately for the DTU
LINKFLAGS = ' -nostdlib -Wl,-z,max-page-size=8 -Wl,-z,common-page-size=8',
CPPPATH = [
Dir(configpath.abspath + '/' + core + '/xtensa-elf/arch/include'),
Dir(xtroot.abspath + '/XtDevTools/install/tools/' + toolversion + '/XtensaTools/xtensa-elf/include')
],
SUPDIR = Dir(configpath.abspath + '/' + core + '/xtensa-elf/arch/lib'),
CRTDIR = Dir(crtdir)
)
env.Replace(ENV = {'PATH' : crossdir + '/bin:' + tooldir.abspath + ':' + os.environ['PATH']})
else:
env.Append(CXXFLAGS = ' -fno-omit-frame-pointer')
if target == 'gem5':
if isa == 'x86_64':
# disable red-zone for all applications, because we use the application's stack in rctmux's
# IRQ handlers since applications run in privileged mode.
env.Append(CFLAGS = ' -mno-red-zone')
env.Append(CXXFLAGS = ' -mno-red-zone')
else:
env.Append(CFLAGS = ' -march=armv7-a')
env.Append(CXXFLAGS = ' -march=armv7-a')
env.Append(LINKFLAGS = ' -march=armv7-a')
env.Append(ASFLAGS = ' -march=armv7-a')
# no build-id because it confuses gem5
env.Append(LINKFLAGS = ' -static -Wl,--build-id=none -nostdlib')
# binaries get very large otherwise
env.Append(LINKFLAGS = ' -Wl,-z,max-page-size=4096 -Wl,-z,common-page-size=4096')
env.Replace(CXX = cross + 'g++')
env.Replace(AS = cross + 'gcc')
env.Replace(CC = cross + 'gcc')
env.Replace(LD = cross + 'ld')
env.Replace(AR = cross + 'gcc-ar')
env.Replace(RANLIB = cross + 'gcc-ranlib')
env.Replace(FORTRAN = cross + 'gfortran')
env.Replace(F90 = cross + 'gfortran')
# add build-dependent flags (debug/release)
btype = os.environ.get('M3_BUILD', 'release')
if btype == 'debug':
if target == 'host' or target == 'gem5':
env.Append(CXXFLAGS = ' -O0 -g')
env.Append(CFLAGS = ' -O0 -g')
else:
# use -Os here because otherwise the binaries tend to get larger than 32k
env.Append(CXXFLAGS = ' -Os -g')
env.Append(CFLAGS = ' -Os -g')
env.Append(ASFLAGS = ' -g')
hostenv.Append(CXXFLAGS = ' -O0 -g')
hostenv.Append(CFLAGS = ' -O0 -g')
else:
env.Append(CRGFLAGS = ' --release')
if target == 't2':
env.Append(CXXFLAGS = ' -Os -DNDEBUG -flto')
env.Append(CFLAGS = ' -Os -DNDEBUG -flto')
env.Append(LINKFLAGS = ' -Os -flto')
else:
env.Append(CXXFLAGS = ' -O2 -DNDEBUG -flto')
env.Append(CFLAGS = ' -O2 -DNDEBUG -flto')
env.Append(LINKFLAGS = ' -O2 -flto')
builddir = 'build/' + target + '-' + isa + '-' + btype
if target == 't2' or target == 't3':
archtype = 'th'
else:
archtype = target
# add some important paths
env.Append(
ARCH = target,
ISA = isa,
ARCHTYPE = archtype,
BUILD = btype,
CFGS = configpath,
BUILDDIR = Dir(builddir),
BINARYDIR = Dir(builddir + '/bin'),
LIBDIR = Dir(builddir + '/bin'),
MEMDIR = Dir(builddir + '/mem'),
FSDIR = Dir(builddir + '/fsdata'),
RUSTDIR = Dir('build/rust'),
)
hostenv.Append(
BINARYDIR = env['BINARYDIR']
)
def M3MemDump(env, target, source):
dump = env.Command(
target, source,
Action(
'xt-dumpelf --width=64 --offset=0 $SOURCE > $TARGET',
'$MDUMPCOMSTR'
)
)
env.Install('$MEMDIR', dump)
def M3FileDump(env, target, source, addr, args = ''):
dump = env.Command(
target, source,
Action(
'$BUILDDIR/src/tools/dumpfile/dumpfile $SOURCE 0x%x %s > $TARGET' % (addr, args),
'$DUMPCOMSTR'
)
)
env.Depends(dump, '$BUILDDIR/src/tools/dumpfile/dumpfile')
env.Install('$MEMDIR', dump)
def M3Mkfs(env, target, source, blocks, inodes, blks_per_ext):
fs = env.Command(
target, source,
Action(
'$BUILDDIR/src/tools/mkm3fs/mkm3fs $TARGET $SOURCE %d %d %d' % (blocks, inodes, blks_per_ext),
'$MKFSCOMSTR'
)
)
env.Depends(fs, '$BUILDDIR/src/tools/mkm3fs/mkm3fs')
env.Install('$BUILDDIR', fs)
def M3Strip(env, target, source):
return env.Command(
target, source,
Action(
cross + 'strip -o $TARGET $SOURCE',
'$STRIPCOMSTR'
)
)
def M3CPP(env, target, source):
env.Command(
target, source,
Action(
cross + 'cpp -P $CPPFLAGS $SOURCE $TARGET',
'$CPPCOMSTR'
)
)
def_ldscript = env.File('$BUILDDIR/ld-final.conf')
M3CPP(env, def_ldscript, '#src/toolchain/gem5/ld.conf')
link_addr = 0x200000
def M3Program(env, target, source, libs = [], libpaths = [], NoSup = False, tgtcore = None,
ldscript = None, varAddr = True):
myenv = env.Clone()
m3libs = ['base', 'thread'] if target == 'kernel' else ['base', 'm3', 'thread']
if myenv['ARCH'] == 't2' or myenv['ARCH'] == 't3':
# set variables, depending on core
if tgtcore is None:
tgtcore = core
runtimedir = configpath.abspath + '/' + tgtcore + '/xtensa-elf/lib/' + runtime
sources = []
if not NoSup:
sources += [
myenv['LIBDIR'].abspath + '/crti.o',
crtdir + '/crtbegin.o',
crtdir + '/crtend.o',
myenv['LIBDIR'].abspath + '/crtn.o',
myenv['LIBDIR'].abspath + '/Window.o'
]
libs = ['hal', 'handlers-sim', 'gcc', 'c'] + m3libs + libs
sources += source
if ldscript is None:
ldscript = File(runtimedir + '/ldscripts/elf32xtensa.x')
myenv.Append(LINKFLAGS = ' -Wl,-T,' + ldscript.abspath)
myenv.Append(CPPPATH = [
'#src/include',
Dir(configpath.abspath + '/' + tgtcore + '/xtensa-elf/arch/include'),
Dir(xtroot.abspath + '/XtDevTools/install/tools/' + toolversion + '/XtensaTools/xtensa-elf/include'),
])
prog = myenv.Program(
target,
sources,
LIBS = ['handler-reset'] + libs,
LIBPATH = [myenv['LIBDIR'], myenv['SUPDIR']] + libpaths,
SUPDIR = Dir(configpath.abspath + '/' + tgtcore + '/xtensa-elf/arch/lib')
)
myenv.M3MemDump(target + '.mem', prog)
myenv.Depends(prog, ldscript)
myenv.Depends(prog, File(runtimedir + '/specs'))
myenv.Depends(prog, myenv['LIBDIR'].abspath + '/libm3.a')
elif myenv['ARCH'] == 'gem5':
if not NoSup:
libs = ['gcc', 'c', 'heap'] + m3libs + libs
source = [myenv['LIBDIR'].abspath + '/crt0.o'] + [source]
if ldscript is None:
ldscript = def_ldscript
myenv.Append(LINKFLAGS = ' -Wl,-T,' + ldscript.abspath)
if varAddr:
global link_addr
myenv.Append(LINKFLAGS = ' -Wl,--section-start=.text=' + ("0x%x" % link_addr))
link_addr += 0x10000
prog = myenv.Program(
target, source,
LIBS = libs,
LIBPATH = [myenv['LIBDIR']] + libpaths
)
myenv.Depends(prog, ldscript)
else:
if not NoSup:
libs = m3libs + ['m3', 'heap', 'pthread'] + libs
prog = myenv.Program(
target, source,
LIBS = libs,
LIBPATH = [myenv['LIBDIR']] + libpaths
)
myenv.Install(myenv['BINARYDIR'], prog)
return prog
def Cargo(env, target, source):
return env.Command(
target, source,
Action(
'cd ${SOURCE.dir.dir} && xargo build $CRGFLAGS',
'$CRGCOMSTR'
)
)
def RustProgram(env, target, libs = []):
myenv = env.Clone()
myenv.Append(LINKFLAGS = ' -Wl,-z,muldefs')
stlib = myenv.Cargo(
target = '$RUSTDIR/$ISA-unknown-$ARCH-' + rustabi + '/$BUILD/lib' + target + '.a',
source = 'src/' + target + '.rs'
)
myenv.Install(myenv['LIBDIR'], stlib)
myenv.Depends(stlib, myenv.File('Cargo.toml'))
myenv.Depends(stlib, [myenv.File('#src/Cargo.toml'), myenv.File('#src/Xargo.toml')])
myenv.Depends(stlib, myenv.File('#src/toolchain/rust/$ISA-unknown-$ARCH-' + rustabi + '.json'))
myenv.Depends(stlib, [
myenv.Glob('#src/libs/rust*/src/*.rs'),
myenv.Glob('#src/libs/rust*/src/*/*.rs'),
myenv.Glob('#src/libs/rust*/src/*/*/*.rs'),
myenv.Glob('#src/libs/rust*/src/*/*/*/*.rs'),
])
myenv.Depends(stlib, [
myenv.Glob('src/*.rs'),
myenv.Glob('src/*/*.rs'),
myenv.Glob('src/*/*/*.rs'),
])
if myenv['ARCH'] == 'gem5':
sources = [myenv['LIBDIR'].abspath + '/crt0.o']
libs = ['c', 'heap', 'gcc', target] + libs
else:
sources = []
libs = ['c', 'heap', 'gcc', 'host', 'pthread', target] + libs
prog = myenv.M3Program(
myenv,
target = target,
source = sources,
libs = libs,
NoSup = True
)
return prog
env.AddMethod(Cargo)
env.AddMethod(M3MemDump)
env.AddMethod(M3FileDump)
env.AddMethod(M3Mkfs)
env.AddMethod(M3Strip)
env.AddMethod(M3CPP)
env.AddMethod(install.InstallFiles)
env.M3Program = M3Program
env.RustProgram = RustProgram
# always use grouping for static libraries, because they may depend on each other so that we want
# to cycle through them until all references are resolved.
env['_LIBFLAGS'] = '-Wl,--start-group ' + env['_LIBFLAGS'] + ' -Wl,--end-group'
env.SConscript('src/SConscript', exports = ['env', 'hostenv'], variant_dir = builddir, src_dir = '.', duplicate = 0)