forked from Razish/japp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SConstruct
567 lines (512 loc) · 13.6 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#
# JA++ SCons project file
# written by Raz0r
#
# options:
# debug generate debug information. value 2 also enables optimisations
# force32 force 32 bit target when on 64 bit machine
# no_sql don't include any SQL dependencies
# no_crashhandler don't include the crash logger (x86 only)
#
# example:
# scons -Q debug=1 force32=1
#
# envvars:
# MORE_WARNINGS enable additional warnings
# NO_SSE disable SSE floating point instructions, force x87 fpu
#
debug = int( ARGUMENTS.get( 'debug', 0 ) )
configuration = { 0: lambda x: 'release', 1: lambda x: 'debug', 2: lambda x: 'optimised-debug' }[debug](debug)
force32 = int( ARGUMENTS.get( 'force32', 0 ) )
no_sql = int( ARGUMENTS.get( 'no_sql', 0 ) )
no_crashhandler = int( ARGUMENTS.get( 'no_crashhandler', 0 ) )
toolStr = ARGUMENTS.get( 'tools', 'gcc,g++,ar,as,gnulink' )
tools = [x for x in toolStr.split( ',' )]
# compare semantic versions (1.0.2 < 1.0.10 < 1.2.0)
def cmp_version( v1, v2 ):
def normalise( v ):
import re
return [int(x) for x in re.sub( r'(\.0+)*$', '', v ).split( '.' )]
return cmp(
normalise( v1 ),
normalise( v2 )
)
import platform
plat = platform.system() # Windows or Linux
try:
bits = int( platform.architecture()[0][:2] ) # 32 or 64
except( ValueError, TypeError ):
bits = None
arch = None # platform-specific, set manually
# architecture settings, needed for binary names, also passed as a preprocessor definition
if force32:
bits = 32
if bits == 32:
if plat == 'Windows':
arch = 'x86'
elif plat == 'Linux':
if platform.machine()[:3] == 'arm':
arch = 'arm'
else:
arch = 'i386'
elif plat == 'Darwin':
arch = 'i386'
else:
raise Exception( 'unexpected platform: ' + plat )
elif bits == 64:
if plat == 'Windows':
arch = 'x64'
elif plat == 'Linux':
arch = 'x86_64'
elif plat == 'Darwin':
arch = 'x86_64'
else:
raise Exception( 'unexpected platform: ' + plat )
else:
raise Exception( 'could not determine architecture width: ' + str(bits) )
clangHack = plat == 'Darwin'
# create the build environment
#FIXME: also consider LD, AS, AR in the toolset
import os
env = Environment(
TARGET_ARCH = arch,
tools = tools,
ENV = { 'PATH' : os.environ['PATH'] }
)
# set the proper compiler name
realcc = ARGUMENTS.get( 'CC', None )
realcxx = ARGUMENTS.get( 'CXX', None )
if 'CC' in os.environ:
if 'ccc' in os.environ['CC']:
env['CC'] = os.environ['CC']
env['CXX'] = os.environ['CXX']
# running a scan-build
if realcc is None or realcxx is None:
raise Exception( 'please specify CC/CXX as such: scons CC=gcc CXX=g++' )
else:
# not a scan-build, do not allow inheriting CC/CXX from outside environment
raise Exception( 'please specify CC/CXX as such: scons CC=gcc CXX=g++' )
elif realcc is None and realcxx is None:
realcc = env['CC']
realcxx = env['CXX']
#FIXME: realcxx is "$CC" on Windows/MSVC
env['ENV'].update( x for x in os.environ.items() if x[0].startswith( 'CCC_' ) )
# prettify the compiler output
if 'TERM' in os.environ:
env['ENV']['TERM'] = os.environ['TERM']
import sys
colours = {}
enableColours = sys.stdout.isatty()
colours['white'] = '\033[1;97m' if enableColours else ''
colours['cyan'] = '\033[96m' if enableColours else ''
colours['orange'] = '\033[33m' if enableColours else ''
colours['green'] = '\033[92m' if enableColours else ''
colours['end'] = '\033[0m' if enableColours else ''
env['SHCCCOMSTR'] = env['SHCXXCOMSTR'] = env['CCCOMSTR'] = env['CXXCOMSTR'] = \
'%s compiling: %s$SOURCE%s' % (colours['cyan'], colours['white'], colours['end'])
env['ARCOMSTR'] = \
'%s archiving: %s$TARGET%s' % (colours['orange'], colours['white'], colours['end'])
env['RANLIBCOMSTR'] = \
'%s indexing: %s$TARGET%s' % (colours['orange'], colours['white'], colours['end'])
env['ASCOMSTR'] = \
'%sassembling: %s$TARGET%s' % (colours['orange'], colours['white'], colours['end'])
env['SHLINKCOMSTR'] = env['LINKCOMSTR'] = \
'%s linking: %s$TARGET%s' % (colours['green'], colours['white'], colours['end'])
# obtain the compiler version
import commands
if realcc == 'cl':
# msvc
ccversion = env['MSVC_VERSION']
elif realcc == 'gcc' or realcc == 'clang':
status, ccrawversion = commands.getstatusoutput( realcc + ' -dumpversion' )
ccversion = None if status else ccrawversion
# scons version
import SCons
sconsversion = SCons.__version__
# git revision
status, rawrevision = commands.getstatusoutput( 'git rev-parse --short HEAD' )
revision = None if status else rawrevision
if revision:
status, dummy = commands.getstatusoutput( 'git diff-index --quiet HEAD' )
if status:
revision += '*'
# set job/thread count
def GetNumCores():
if plat == 'Linux' or plat == 'Darwin':
# works on recent mac/linux
status, num_cores = commands.getstatusoutput( 'getconf _NPROCESSORS_ONLN' )
if status == 0:
return int(num_cores)
# only works on linux
status, num_cores = commands.getstatusoutput( 'cat /proc/cpuinfo | grep processor | wc -l' )
if status == 0:
return int(num_cores)
return 1;
elif plat == 'Windows':
# exists since at-least XP SP2
return int( os.environ['NUMBER_OF_PROCESSORS'] )
env.SetOption( 'num_jobs', GetNumCores() )
# notify the user of the build configuration
if not env.GetOption( 'clean' ):
# build tools
msg = 'Building '
if revision:
msg += revision + ' '
msg += 'for ' + plat + ' '
if force32:
msg += 'forced '
msg += str(bits) + ' bits using ' + str(env.GetOption( 'num_jobs' )) + ' threads\n'\
+ '\t' + realcc + '/' + realcxx + ': ' + ccversion + '\n'\
+ '\tpython: ' + platform.python_version() + '\n'\
+ '\tscons: ' + sconsversion + '\n'
# build options
msg += 'options:\n'\
+ '\tconfiguration: ' + configuration + '\n'\
+ '\tinstruction set: ' + arch\
+ ((' with x87 fpu' if 'NO_SSE' in os.environ else ' with SSE') if arch != 'arm' else '') + '\n'\
+ '\tsql support: ' + ('dis' if no_sql else 'en') + 'abled\n'\
+ '\tcrash logging: ' + ('dis' if no_crashhandler else 'en') + 'abled\n'
# build environment
if 'SCONS_DEBUG' in os.environ:
msg += realcc + ' located at ' + commands.getoutput( 'where ' + realcc ).split( '\n' )[0] + '\n'
if 'AR' in env:
msg += env['AR'] + ' located at ' + commands.getoutput( 'where ' + env['AR'] ).split( '\n' )[0] + '\n'
if 'AS' in env:
msg += env['AS'] + ' located at ' + commands.getoutput( 'where ' + env['AS'] ).split( '\n' )[0] + '\n'
msg += 'python located at ' + sys.executable + '\n'
msg += 'scons' + ' located at ' + commands.getoutput( 'where ' + 'scons' ).split( '\n' )[0] + '\n'
print( msg )
# clear default compiler/linker switches
def emptyEnv( env, e ):
if 'SCONS_DEBUG' in os.environ:
if e in env:
if env[e]:
print( 'discarding ' + e + ': ' + env[e] )
else:
print( 'env[' + e + '] is empty' )
else:
print( 'env[' + e + '] does not exist' )
env[e] = []
emptyEnv( env, 'CPPDEFINES' )
emptyEnv( env, 'CFLAGS' )
emptyEnv( env, 'CCFLAGS' )
emptyEnv( env, 'CXXFLAGS' )
emptyEnv( env, 'LINKFLAGS' )
emptyEnv( env, 'ARFLAGS' )
emptyEnv( env, 'LIBS' )
# compiler switches
if realcc == 'gcc' or realcc == 'clang':
env['CCFLAGS'] += [
#'-M', # show include hierarchy
]
# c warnings
env['CFLAGS'] += [
#'-Wdeclaration-after-statement',
'-Wnested-externs',
'-Wold-style-definition',
'-Wstrict-prototypes',
]
# c/cpp warnings
env['CCFLAGS'] += [
'-Wall',
'-Wextra',
'-Wno-missing-braces',
'-Wno-missing-field-initializers',
'-Wno-sign-compare',
'-Wno-unused-parameter',
'-Winit-self',
'-Winline',
'-Wmissing-include-dirs',
'-Woverlength-strings',
'-Wpointer-arith',
'-Wredundant-decls',
'-Wundef',
'-Wuninitialized',
'-Wwrite-strings',
]
# strict c/cpp warnings
if 'MORE_WARNINGS' in os.environ:
env['CCFLAGS'] += [
'-Waggregate-return',
'-Wbad-function-cast',
'-Wcast-qual',
'-Wfloat-equal',
'-Wlong-long',
'-Wshadow',
'-Wsign-conversion',
'-Wswitch-default',
'-Wunreachable-code',
]
if not clangHack:
env['CCFLAGS'] += [
'-Wdouble-promotion',
'-Wsuggest-attribute=const',
'-Wunsuffixed-float-constants',
]
# gcc-specific warnings
if realcc == 'gcc' and cmp_version( ccversion, '4.6' ) >= 0 and arch != 'arm':
env['CCFLAGS'] += [
'-Wlogical-op',
]
# requires gcc 4.7 or above
if cmp_version( ccversion, '4.7' ) >= 0:
env['CCFLAGS'] += [
'-Wstack-usage=32768',
]
# disable warnings
env['CCFLAGS'] += [
'-Wno-char-subscripts',
]
# c/cpp flags
if arch == 'arm':
env['CCFLAGS'] += [
'-fsigned-char',
]
else:
env['CCFLAGS'] += [
'-mstackrealign',
'-masm=intel',
]
env['ASFLAGS'] += [
'-msyntax=intel',
'-mmnemonic=intel',
]
if 'NO_SSE' in os.environ:
env['CCFLAGS'] += [
'-mfpmath=387',
'-mno-sse2',
'-ffloat-store',
]
if realcc == 'gcc':
env['CFLAGS'] += [
'-fexcess-precision=standard',
]
else:
env['CCFLAGS'] += [
'-mfpmath=sse',
'-msse2',
]
if arch == 'i386':
env['CCFLAGS'] += [
'-march=i686',
]
elif arch == 'x86_64':
env['CCFLAGS'] += [
'-mtune=generic',
]
if bits == 32:
env['CCFLAGS'] += [
'-m32',
]
env['LINKFLAGS'] += [
'-m32',
]
env['CCFLAGS'] += [
'-fvisibility=hidden',
]
# misc settings
#if realcc == 'gcc' and cmp_version( ccversion, '4.9' ) >= 0:
# env['CCFLAGS'] += [
# '-fdiagnostics-color',
# ]
# c flags
env['CFLAGS'] += [
'-std=gnu99',
]
# c++ flags
env['CXXFLAGS'] += [
'-fvisibility-inlines-hidden',
'-std=c++11',
]
# archive flags
env['ARFLAGS'] = 'rc'
elif realcc == 'cl':
# msvc
env['CCFLAGS'] += [
#'/showIncludes',
]
env['CFLAGS'] += [
'/TC', # compile as c
]
env['CXXFLAGS'] += [
'/TP', # compile as c++
]
env['CCFLAGS'] += [
'/EHsc', # exception handling
'/nologo', # remove watermark
]
env['LINKFLAGS'] += [
'/ERRORREPORT:none', # don't send error reports for internal linker errors
'/NOLOGO', # remove watermark
'/MACHINE:' + arch, # 32/64 bit linking
]
if bits == 64:
env['LINKFLAGS'] += [
'/SUBSYSTEM:WINDOWS', # graphical application
]
else:
env['LINKFLAGS'] += [
'/SUBSYSTEM:WINDOWS,5.1', # graphical application, XP support
]
env['CPPDEFINES'] += [
'_WIN32',
]
if bits == 64:
env['CPPDEFINES'] += [
'_WIN64',
]
# fpu control
if 'NO_SSE' in os.environ:
env['CCFLAGS'] += [
'/fp:precise', # precise FP
]
if bits == 32:
env['CCFLAGS'] += [
'/arch:IA32', # no sse, x87 fpu
]
else:
env['CCFLAGS'] += [
'/fp:strict', # strict FP
]
if bits == 32 and cmp_version( ccversion, '14.0' ) < 0:
env['CCFLAGS'] += [
'/arch:SSE2', # sse2
]
# strict c/cpp warnings
if 'LESS_WARNINGS' in os.environ:
env['CCFLAGS'] += [
'/W2',
]
else:
env['CCFLAGS'] += [
'/W4',
'/we 4013',
'/we 4024',
'/we 4026',
'/we 4028',
'/we 4029',
'/we 4033',
'/we 4047',
'/we 4053',
'/we 4087',
'/we 4098',
'/we 4245',
'/we 4305',
'/we 4700',
]
if 'MORE_WARNINGS' in os.environ:
env['CCFLAGS'] += [
'/Wall',
]
else:
env['CCFLAGS'] += [
'/wd 4100',
'/wd 4127',
'/wd 4244',
'/wd 4706',
'/wd 4131',
'/wd 4996',
]
env['LINKFLAGS'] += [
'/NODEFAULTLIB:LIBCMTD',
'/NODEFAULTLIB:MSVCRT',
]
if plat == 'Darwin':
env['CPPDEFINES'] += [ 'MACOS_X' ]
env['LINKFLAGS'] += [
'-framework', 'CoreFoundation',
'-framework', 'ApplicationServices'
]
# debug / release
if debug == 0 or debug == 2:
if realcc == 'gcc' or realcc == 'clang':
env['CCFLAGS'] += [
'-O2', # O3 may not be best, due to cache size not being big enough for the amount of inlining performed
'-fomit-frame-pointer',
]
if debug == 0 and realcc == 'gcc':
env['LINKFLAGS'] += [
'-s', # strip unused symbols
]
elif realcc == 'cl':
env['CCFLAGS'] += [
'/O2', # maximise speed
'/MD', # multi-threaded runtime for DLLs
]
env['LINKFLAGS'] += [
'/OPT:REF', # remove unreferenced functions/data
'/STACK:32768', # stack size
]
if debug == 0:
env['CPPDEFINES'] += [
'NDEBUG',
]
if debug:
if realcc == 'gcc' or realcc == 'clang':
env['CCFLAGS'] += [
'-g3',
#'-pg',
#'-finstrument-functions',
'-fno-omit-frame-pointer',
]
#env['LINKFLAGS'] += [
# '-pg',
#]
elif realcc == 'cl':
env['CCFLAGS'] += [
'/Od', # disable optimisations
'/Z7', # emit debug information
'/MDd', # multi-threaded debug runtime for DLLs
]
env['LINKFLAGS'] += [
'/DEBUG', # generate debug info
'/OPT:ICF', # enable COMDAT folding
'/INCREMENTAL:NO', # no incremental linking
]
env['CPPDEFINES'] += [
'_DEBUG',
]
if revision:
env['CPPDEFINES'] += [
'REVISION=\\"' + revision + '\\"'
]
if no_crashhandler:
env['CPPDEFINES'] += [
'NO_CRASHHANDLER',
]
if no_sql:
env['CPPDEFINES'] += [
'NO_SQL',
]
# build-time settings
env['CPPDEFINES'] += [
'JAPP_COMPILER=\\"' + realcc + ' ' + ccversion + '\\"',
'ARCH_STRING=\\"' + arch + '\\"',
]
env['CPPPATH'] = [
'#',
'..' + os.sep + 'game',
]
env['LIBPATH'] = [
'#' + os.sep + 'libs' + os.sep + plat + os.sep + realcc + os.sep + str(bits) + os.sep
]
# invoke the per-project scripts
projects = [
'game',
'cgame',
'ui',
]
for project in projects:
env.SConscript(
os.path.join( project, 'SConscript' ),
exports = [
'arch',
'bits',
'configuration',
'env',
'no_crashhandler',
'no_sql',
'plat',
'realcc',
]
)