forked from BenoitHiller/gini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SconsBuilder.py
1274 lines (1104 loc) · 42.5 KB
/
SconsBuilder.py
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- python -*-
#/******************************************************************************
# * Copyright (c) 2004, 2009 Lothar Werzinger.
# * All rights reserved. This program and the accompanying materials
# * are made available under the terms of the Eclipse Public License v1.0
# * which accompanies this distribution, and is available at
# * http://www.eclipse.org/legal/epl-v10.html
# *****************************************************************************/
DEFAULT_BIN_DIR = 'bin'
DEFAULT_LIB_DIR = 'lib'
DEFAULT_DOC_DIR = 'doc'
DEFAULT_SKIP_DIRS = ['.*', 'CVS', 'bin', 'lib', 'doc']
DEFAULT_CXX_GLOB = ['*.c', '*.cc', '*.cpp', '*.cxx']
DEFAULT_BASE_VARIANT_DIR = '.build'
DEFAULT_DOXYGEN_CONFIGFILE = 'doxygen.cfg'
DEFAULT_UNITTEST_DIR_NAME = 'test'
DEFAULT_UNITTEST_TEST_PREFIX = 'test-'
DEFAULT_UNITTEST_LIB_PREFIX = 'test-'
DEFAULT_UNITTEST_LIB_DIR = ''
DEFAULT_UNITTEST_LIB = ''
DEFAULT_UNITTEST_INCLUDE_DIR = ''
DEFAULT_UNITTEST_TESTRUNNER = ''
DEFAULT_SCONSBUILDER_CONFIG_FILE_NAME = '.scb'
import fnmatch
import glob
import os
import re
import string
import SCons.Script
# SconsBuilder may work with earlier version,
# but it was build and tested against SCons 1.0.0
#SCons.Script.EnsureSConsVersion(1,0,0)
# SconsBuilder may work with earlier version,
# but it was build and tested against Python 2.4
SCons.Script.EnsurePythonVersion(2,4)
try:
import SconsBuilderConfig
except:
print 'Generating default SconsBuilderConfig.py'
configfile = open('SconsBuilderConfig.py', 'w')
configfile.write('# -*- python -*-\n')
configfile.write('# This file was automatically generated\n')
configfile.write('# Do NOT edit this file when using the Eclipse SconsBuilder plugin!\n')
configfile.write('# The Eclipse SconsBuilder plugin WILL overwrite this file!\n')
configfile.write('\n')
configfile.write('BASE_VARIANT_DIR = ' + repr(DEFAULT_BASE_VARIANT_DIR) + '\n')
configfile.write('\n')
configfile.write('BUILD_CONFIGURATIONS = []\n')
configfile.write('BUILD_CONFIGURATION = None\n')
configfile.write('\n')
configfile.write('BUILD_TARGETS = []\n')
configfile.write('BUILD_TARGET = None\n')
configfile.write('\n')
configfile.write('BIN_DIR = ' + repr(DEFAULT_BIN_DIR) + '\n')
configfile.write('LIB_DIR = ' + repr(DEFAULT_LIB_DIR) + '\n')
configfile.write('DOC_DIR = ' + repr(DEFAULT_DOC_DIR) + '\n')
configfile.write('\n')
configfile.write('DOXYGEN_CONFIGFILE = ' + repr(DEFAULT_DOXYGEN_CONFIGFILE) + '\n')
configfile.write('\n')
configfile.write('UNITTEST_ENABLED = False\n')
configfile.write('UNITTEST_DIR_NAME = ' + repr(DEFAULT_UNITTEST_DIR_NAME) + ' \n')
configfile.write('UNITTEST_TEST_PREFIX = ' + repr(DEFAULT_UNITTEST_TEST_PREFIX) + ' \n')
configfile.write('UNITTEST_LIB_PREFIX = ' + repr(DEFAULT_UNITTEST_LIB_PREFIX) + ' \n')
configfile.write('UNITTEST_INCLUDE_DIR = ' + repr(DEFAULT_UNITTEST_INCLUDE_DIR) + '\n')
configfile.write('UNITTEST_LIB_DIR = ' + repr(DEFAULT_UNITTEST_LIB_DIR) + '\n')
configfile.write('UNITTEST_LIB = ' + repr(DEFAULT_UNITTEST_LIB) + '\n')
configfile.write('UNITTEST_TESTRUNNER = ' + repr(DEFAULT_UNITTEST_TESTRUNNER) + '\n')
configfile.write('\n')
configfile.write('SKIP_DIRS = ' + repr(DEFAULT_SKIP_DIRS) + '\n')
configfile.write('\n')
configfile.write('CXX_GLOB = ' + repr(DEFAULT_CXX_GLOB) + '\n')
configfile.write('\n')
configfile.write('SCONSBUILDER_CONFIG_FILE_NAME = ' + repr(DEFAULT_SCONSBUILDER_CONFIG_FILE_NAME) + '\n')
configfile.write('\n')
configfile.close()
import SconsBuilderConfig
SCB_LAUNCH_DIR = SCons.Script.GetLaunchDir()
SCB_SCRIPT_DIR = os.getcwd()
allEnvironments = {}
def addCommandLineOptions():
SCons.Script.AddOption(
'--buildconfiguration',
dest='buildconfiguration',
nargs=1,
type='string',
action='store',
metavar='CONFIGURATION',
default=SconsBuilderConfig.BUILD_CONFIGURATION,
help='select a build configuration (e.g. debug, release, ...)'
)
SCons.Script.AddOption(
'--buildtarget',
dest='buildtarget',
nargs=1,
type='string',
action='store',
metavar='TARGET',
default=SconsBuilderConfig.BUILD_TARGET,
help='select a build target (e.g. linux, winXP, ...)'
)
SCons.Script.AddOption(
'--verbosity',
dest='verbosity',
nargs=1,
type='int',
action='store',
metavar='INTEGER',
default=1,
help='select the verbosity level (0..n)'
)
SCons.Script.AddOption(
'--showcommands',
dest='showcommands',
action='store_true',
default=False,
help='show commands executed'
)
SCons.Script.AddOption(
'--dist',
dest='dist',
action='store_true',
default=False,
help='make the GINI distribution file (tar gzipped)'
)
SCons.Script.AddOption(
'--install',
dest='install',
action='store_true',
default=False,
help='install the binary files in the proper location... '
)
SCons.Script.AddOption(
'--documentation',
dest='documentation',
action='store_true',
default=False,
help='build doxygen documentation'
)
SCons.Script.AddOption(
'--doxygenfile',
dest='doxygenfile',
nargs=1,
type='string',
action='store',
metavar='DOXYFILE',
default=SconsBuilderConfig.DOXYGEN_CONFIGFILE,
help='specify the doxygen config file'
)
SCons.Script.AddOption(
'--forcemodified',
dest='forcemodified',
nargs=1,
type='string',
action='append',
metavar='SOURCEFILE',
default=[],
help='pretend that SOURCEFILE was modified'
)
SCons.Script.AddOption(
'--nice',
dest='nice',
nargs=1,
type='int',
action='store',
metavar='INTEGER',
default=10,
help='select the nice level (process priority adjustment) (0..n)'
)
def printBuild(env):
target = tryGetEnvironment(env, 'SCB_BUILD_TARGET')
config = tryGetEnvironment(env, 'SCB_BUILD_CONFIGURATION')
if config and target:
space = ' '
else:
space = ''
print # new line
print (
'Building %s (%s%s%s)' %
(
relativePath(SCB_SCRIPT_DIR),
tryGetEnvironment(env, 'SCB_BUILD_TARGET', default=''),
space,
tryGetEnvironment(env, 'SCB_BUILD_CONFIGURATION', default='')
)
)
def printCmdLine(s, target, src, env):
if not SCons.Script.GetOption('showcommands'):
s = '\n'.join([ \
entry.builder.get_name(env) + "('" + \
str(entry) + "')" for entry in target \
])
log(1, [s])
def log(level, messages):
if verbosity() >= level:
for message in messages:
print(message)
def tryGetEnvironment(env, key, default=None, emptyOk=True):
value = default
try:
value = env[key]
try:
length = len(value)
if length == 0 and emptyOk:
value = value
else:
pass
except:
pass
except:
pass
log(12, ['tryGetEnvironment: env[%s]=%s' % (key, value)])
return value
def relativePath(path, base=SCB_SCRIPT_DIR, prefix=True):
if prefix:
base = os.path.dirname(base)
newpath = string.replace(
path,
os.path.commonprefix([base, path]),
''
)
if len(newpath) >=1 and newpath[0] == '/':
return newpath[1:]
else:
return newpath
def listDirectories(path, skip=[]):
dirs = []
if path == '':
path = '.'
dirlist = sorted(
[item for item in os.listdir(path)
if os.path.isdir(os.path.join(path,item))]
)
log(20, ['dirlist=%s' % (dirlist)])
for npath in dirlist:
dirs.append(npath)
name = os.path.basename(npath)
for check in skip:
match = fnmatch.fnmatch(name, check)
if match:
try:
dirs.remove(npath)
except:
pass
return dirs
def mkdir(newdir):
"""works the way a good mkdir should :)
- already exists, silently complete
- regular file in the way, raise an exception
- parent directory(ies) does not exist, make them as well
"""
if os.path.isdir(newdir):
pass
elif os.path.isfile(newdir):
raise OSError("a file with the same name as the desired " \
"dir, '%s', already exists." % newdir)
else:
head, tail = os.path.split(newdir)
if head and not os.path.isdir(head):
mkdir(head)
if tail:
os.mkdir(newdir)
def verbosity():
try:
return SCons.Script.GetOption('verbosity')
except:
return 1
def getEnvironment(path):
try:
return allEnvironments[os.path.abspath(path)]
except:
return None
def rootEnvironment():
return getEnvironment(SCB_SCRIPT_DIR)
def printEnv(verbosity, env):
if verbosity >= 15:
print
print env.Dump()
elif verbosity >= 14:
print
print 'env[\'SCB_LAUNCH_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_LAUNCH_DIR'))
print 'env[\'SCB_SCRIPT_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_SCRIPT_DIR'))
print 'env[\'SCB_BASE_VARIANT_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_BASE_VARIANT_DIR'))
print 'env[\'SCB_VARIANT_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_VARIANT_DIR'))
print 'env[\'SCB_BIN_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_BIN_DIR'))
print 'env[\'SCB_LIB_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_LIB_DIR'))
print 'env[\'SCB_DOC_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_DOC_DIR'))
print 'env[\'SCB_ABSOLUTE_PATH\']=%s' % (tryGetEnvironment(env, 'SCB_ABSOLUTE_PATH'))
print 'env[\'SCB_RELATIVE_PATH\']=%s' % (tryGetEnvironment(env, 'SCB_RELATIVE_PATH'))
print 'env[\'SCB_UNITTEST_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_MODIFY'))
print 'env[\'SCB_UNITTESTRUNNER_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTESTRUNNER_MODIFY'))
print 'env[\'SCB_FILE_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_FILE_MODIFY'))
print 'env[\'SCB_BUILD_CONFIGURATION\']=%s' % (tryGetEnvironment(env, 'SCB_BUILD_CONFIGURATION'))
print 'env[\'SCB_BUILD_TARGET\']=%s' % (tryGetEnvironment(env, 'SCB_BUILD_TARGET'))
print 'env[\'SCB_PREPARE_OBJECT_TARGETS\']=%s' % (tryGetEnvironment(env, 'SCB_PREPARE_OBJECT_TARGETS'))
print 'env[\'SCB_PREPARE_LIBRARY_TARGETS\']=%s' % (tryGetEnvironment(env, 'SCB_PREPARE_LIBRARY_TARGETS'))
print 'env[\'SCB_PREPARE_EXECUTABLE_TARGETS\']=%s' % (tryGetEnvironment(env, 'SCB_PREPARE_EXECUTABLE_TARGETS'))
print 'env[\'SCB_SCONS_TARGETS\']=%s' % (tryGetEnvironment(env, 'SCB_SCONS_TARGETS'))
print 'env[\'SCB_SKIP_DIRS\']=%s' % (tryGetEnvironment(env, 'SCB_SKIP_DIRS'))
print 'env[\'SCB_CXX_GLOB\']=%s' % (tryGetEnvironment(env, 'SCB_CXX_GLOB'))
print 'env[\'SCB_CXX_SKIP\']=%s' % (tryGetEnvironment(env, 'SCB_CXX_SKIP'))
print 'env[\'SCB_UNITTEST_ENABLED\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_ENABLED'))
print 'env[\'SCB_UNITTEST_DIR_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_DIR_NAME'))
print 'env[\'SCB_UNITTEST_TEST_PREFIX\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_TEST_PREFIX'))
print 'env[\'SCB_UNITTEST_LIB_PREFIX\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_LIB_PREFIX'))
print 'env[\'SCB_UNITTEST_LIB_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_LIB_DIR'))
print 'env[\'SCB_UNITTEST_LIB\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_LIB'))
print 'env[\'SCB_UNITTEST_INCLUDE_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_INCLUDE_DIR'))
print 'env[\'SCB_UNITTEST_TESTRUNNER\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_TESTRUNNER'))
print 'env[\'SCB_UNITTEST_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_MODIFY'))
print 'env[\'SCB_PLATFORM\']=%s' % (tryGetEnvironment(env, 'SCB_PLATFORM'))
print 'env[\'SCB_SHARED_LIB_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_SHARED_LIB_NAME'))
print 'env[\'SCB_STATIC_LIB_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_STATIC_LIB_NAME'))
print 'env[\'SCB_EXECUTABLE_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_EXECUTABLE_NAME'))
elif verbosity >= 13:
print
print 'env[\'SCB_BASE_VARIANT_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_BASE_VARIANT_DIR'))
print 'env[\'SCB_VARIANT_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_VARIANT_DIR'))
print 'env[\'SCB_BIN_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_BIN_DIR'))
print 'env[\'SCB_LIB_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_LIB_DIR'))
print 'env[\'SCB_DOC_DIR\']=%s' % (tryGetEnvironment(env, 'SCB_DOC_DIR'))
print 'env[\'SCB_ABSOLUTE_PATH\']=%s' % (tryGetEnvironment(env, 'SCB_ABSOLUTE_PATH'))
print 'env[\'SCB_RELATIVE_PATH\']=%s' % (tryGetEnvironment(env, 'SCB_RELATIVE_PATH'))
print 'env[\'SCB_UNITTEST_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_UNITTEST_MODIFY'))
print 'env[\'SCB_FILE_MODIFY\']=%s' % (tryGetEnvironment(env, 'SCB_FILE_MODIFY'))
print 'env[\'SCB_SHARED_LIB_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_SHARED_LIB_NAME'))
print 'env[\'SCB_STATIC_LIB_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_STATIC_LIB_NAME'))
print 'env[\'SCB_EXECUTABLE_NAME\']=%s' % (tryGetEnvironment(env, 'SCB_EXECUTABLE_NAME'))
# allow to (re)build files by specifying the source(s)
def forcedDecider(dependency, target, prev_ni):
log(12, ['Checking dependency %s' % dependency])
if str(dependency) in SCons.Script.GetOption('forcemodified'):
log(1, ['Dependency %s is forced modified' % dependency])
return True
elif prev_ni and dependency.get_csig() != prev_ni.csig:
log(11, ['Dependency %s is modified' % dependency])
return True
return False
def prepareCallback(env, targets, useCallback=True):
for target in targets:
name = target.get_builder().get_name(env)
slot = None
try:
slot = env['SCB_SCONS_TARGETS'][name]
except:
log(12, [
'creating env[\'SCB_SCONS_TARGETS\'][\'%s\']' % (name)
])
env['SCB_SCONS_TARGETS'][name] = []
slot = env['SCB_SCONS_TARGETS'][name]
slot.append(target)
log(12, [
'env[\'SCB_SCONS_TARGETS\'][\'%s\']=%s' % (name, [str(x) for x in env['SCB_SCONS_TARGETS'][name]])
])
if useCallback and env['SCB_PREPARE_CALLBACK']:
env['SCB_PREPARE_CALLBACK'](env, targets)
def download(url, target = None):
import urllib
webFile = urllib.urlopen(url)
if target is None:
target = url.split('/')[-1]
localFile = open(target, 'w')
localFile.write(webFile.read())
webFile.close()
localFile.close()
def buildBaseEnvironment(
buildconfiguration=None,
buildtarget=None
):
env = None
try:
env = allEnvironments[None]
log(10, ['Returning base environment'])
except:
log(10, ['Creating base environment'])
# build a primitive base environment that allows us to select
# the platform based on buildconfiguration and buildtarget
if not buildconfiguration:
buildconfiguration = SCons.Script.GetOption('buildconfiguration'),
if not buildtarget:
buildtarget = SCons.Script.GetOption('buildtarget')
baseenv = SCons.Script.Environment(
SCB_LAUNCH_DIR = SCB_LAUNCH_DIR,
SCB_SCRIPT_DIR = SCB_SCRIPT_DIR,
SCB_BUILD_CONFIGURATION = buildconfiguration,
SCB_BUILD_TARGET = buildtarget,
SCB_VERBOSE=0,
SCB_PLATFORM = SCons.Script.Platform()
)
updateEnvironment(baseenv, SCB_SCRIPT_DIR, ['SCB_PLATFORM'])
url = 'http://www.scons.org/wiki/DoxygenBuilder?action=AttachFile&do=get&target=doxygen.py'
doxygenbuilderfilename = 'SconsBuilderDoxygen.py'
print('checking for ' + doxygenbuilderfilename)
if not os.path.exists(doxygenbuilderfilename):
print('downloading ' + url)
download(
url,
doxygenbuilderfilename
)
print('checking for ' + doxygenbuilderfilename + ' done')
env = SCons.Script.Environment(
tools = ['default', 'SconsBuilderDoxygen'],
toolpath = '.',
SCB_LAUNCH_DIR = SCB_LAUNCH_DIR,
SCB_SCRIPT_DIR = SCB_SCRIPT_DIR,
SCB_BASE_VARIANT_DIR = SconsBuilderConfig.BASE_VARIANT_DIR,
SCB_VARIANT_DIR = '',
SCB_BIN_DIR = None,
SCB_LIB_DIR = None,
SCB_DOC_DIR = SconsBuilderConfig.DOC_DIR,
SCB_ABSOLUTE_PATH = SCB_SCRIPT_DIR,
SCB_RELATIVE_PATH = '',
SCB_BUILD_CONFIGURATION = SCons.Script.GetOption('buildconfiguration'),
SCB_BUILD_TARGET = SCons.Script.GetOption('buildtarget'),
SCB_PREPARE_OBJECT_TARGETS = [prepareObjectTargets],
SCB_PREPARE_LIBRARY_TARGETS = [prepareLibraryTargets],
SCB_PREPARE_EXECUTABLE_TARGETS = [prepareExecutableTargets, prepareUnittestTargets],
SCB_SCONS_TARGETS = {},
SCB_SKIP_DIRS = SconsBuilderConfig.SKIP_DIRS,
SCB_CXX_GLOB = SconsBuilderConfig.CXX_GLOB,
SCB_CXX_SKIP = SCons.Util.CLVar(),
SCB_UNITTEST_ENABLED = SconsBuilderConfig.UNITTEST_ENABLED,
SCB_UNITTEST_DIR_NAME = SconsBuilderConfig.UNITTEST_DIR_NAME,
SCB_UNITTEST_TEST_PREFIX = SconsBuilderConfig.UNITTEST_TEST_PREFIX,
SCB_UNITTEST_LIB_PREFIX = SconsBuilderConfig.UNITTEST_LIB_PREFIX,
SCB_UNITTEST_LIB_DIR = SconsBuilderConfig.UNITTEST_LIB_DIR,
SCB_UNITTEST_LIB = SconsBuilderConfig.UNITTEST_LIB,
SCB_UNITTEST_INCLUDE_DIR = SconsBuilderConfig.UNITTEST_INCLUDE_DIR,
SCB_UNITTEST_TESTRUNNER = SconsBuilderConfig.UNITTEST_TESTRUNNER,
SCB_UNITTEST_MODIFY = [],
SCB_UNITTESTRUNNER_MODIFY = [],
SCB_PLATFORM = baseenv['SCB_PLATFORM'],
SCB_PRINT_BUILD = printBuild,
SCB_PREPARE_CALLBACK = None,
PRINT_CMD_LINE_FUNC = printCmdLine,
platform = baseenv['SCB_PLATFORM']
)
# register our special builders
env['BUILDERS']['SconsBuilderHardLink'] = SCons.Script.Builder(
action=SCons.Script.Action(
HardLinkAction,
'Hard Link ${SOURCE} -> ${TARGETS}'
)
)
env['BUILDERS']['SconsBuilderSymLink'] = SCons.Script.Builder(
action=SCons.Script.Action(
SymLinkAction,
'Symbolic Link ${SOURCE} -> ${TARGETS}'
)
)
allEnvironments[None] = env
# allow to (re)build files by specifying the source(s)
if SCons.Script.GetOption('forcemodified'):
SCons.Script.Decider(forcedDecider)
return env
def prepareEnvironment(path = SCB_SCRIPT_DIR):
relpath = relativePath(path)
path = os.path.abspath(path)
parentenv = getEnvironment(os.path.dirname(path))
log(4, ['']) # a newline as logging get's more detailed
log(3, ['Preparing environment for %s' % (relpath)])
if parentenv:
allEnvironments[path] = parentenv.Clone()
else:
# clone the base environment
allEnvironments[path] = buildBaseEnvironment().Clone()
parentenv = allEnvironments[path]
env = allEnvironments[path]
rootenv = rootEnvironment()
env['SCB_ABSOLUTE_PATH'] = path
env['SCB_RELATIVE_PATH'] = relativePath(path, prefix=False)
# always start with an empty list (can not be inherited)
env['SCB_CXX_SKIP'] = SCons.Util.CLVar()
fullConfiguration = ''
if parentenv['SCB_BUILD_TARGET'] and parentenv['SCB_BUILD_TARGET'] != '':
fullConfiguration = os.path.join(fullConfiguration, parentenv['SCB_BUILD_TARGET'])
if parentenv['SCB_BUILD_CONFIGURATION'] and parentenv['SCB_BUILD_CONFIGURATION'] != '':
fullConfiguration = os.path.join(fullConfiguration, parentenv['SCB_BUILD_CONFIGURATION'])
# calculate the effective variant dir
variantdir = parentenv['SCB_BASE_VARIANT_DIR']
if fullConfiguration != '':
variantdir = os.path.join(variantdir, fullConfiguration)
env['SCB_VARIANT_DIR'] = os.path.join(variantdir, env['SCB_RELATIVE_PATH'])
if env['SCB_BIN_DIR'] is None:
if fullConfiguration != '':
env['SCB_BIN_DIR'] = os.path.join(
SconsBuilderConfig.BIN_DIR,
fullConfiguration
)
else:
env['SCB_BIN_DIR'] = SconsBuilderConfig.BIN_DIR
if env['SCB_LIB_DIR'] is None:
if fullConfiguration != '':
env['SCB_LIB_DIR'] = os.path.join(
SconsBuilderConfig.LIB_DIR,
fullConfiguration
)
else:
env['SCB_LIB_DIR'] = SconsBuilderConfig.LIB_DIR
if env.has_key('LIBPATH'):
env.AppendUnique(LIBPATH=env['SCB_LIB_DIR'])
else:
env.Replace(LIBPATH=SCons.Util.CLVar())
env.AppendUnique(LIBPATH=env['SCB_LIB_DIR'])
log(12, [
'env[\'LIBPATH\']=' + tryGetEnvironment(env, 'LIBPATH'),
'env[\'SCB_VARIANT_DIR\']=' + tryGetEnvironment(env, 'SCB_VARIANT_DIR'),
'env[\'SCB_RELATIVE_PATH\']=' + tryGetEnvironment(env, 'SCB_RELATIVE_PATH')
])
dirname = os.path.basename(path)
libname = dirname
# handle unit tests
if tryGetEnvironment(env, 'SCB_UNITTEST_ENABLED') and \
os.path.basename(path) == env['SCB_UNITTEST_DIR_NAME']:
# unit tests can have no executable name
env['SCB_EXECUTABLE_NAME'] = None
parentdirname = os.path.abspath(os.path.join(path, '..'))
parentenv = allEnvironments[parentdirname]
parentlib = tryGetEnvironment(parentenv, 'SCB_SHARED_LIB_NAME') or \
tryGetEnvironment(parentenv, 'SCB_STATIC_LIB_NAME') or \
os.path.basename(parentdirname)
log(12, ['parentlib=%s' % (parentlib)])
libname = env['SCB_UNITTEST_LIB_PREFIX'] + parentlib
env['SCB_UNITTEST_PARENTLIB_NAME'] = parentlib
env['SCB_UNITTEST_TEST_NAME'] = env['SCB_UNITTEST_TEST_PREFIX'] + parentlib
log(12, [
'SCB_UNITTEST_PARENTLIB_NAME=%s' % (env['SCB_UNITTEST_PARENTLIB_NAME']),
'SCB_UNITTEST_TEST_NAME=%s' % (env['SCB_UNITTEST_TEST_NAME'])
])
# reset some non inheritable settings
env['SCB_EXECUTABLE_NAME'] = None
env['SCB_STATIC_LIB_NAME'] = None
env['SCB_SHARED_LIB_NAME'] = None
# default is to build (shared) libraries
env['SCB_SHARED_LIB_NAME'] = libname
log(13, ['SCB_SHARED_LIB_NAME before: %s' % (env['SCB_SHARED_LIB_NAME'])])
updateEnvironment(env, path)
log(13, ['SCB_SHARED_LIB_NAME after=%s' % (env['SCB_SHARED_LIB_NAME'])])
if tryGetEnvironment(env, 'SCB_UNITTEST_ENABLED') and \
os.path.basename(path) == env['SCB_UNITTEST_DIR_NAME']:
log(12, [
'SCB_UNITTEST_PARENTLIB_NAME after=%s' % (env['SCB_UNITTEST_PARENTLIB_NAME']),
'SCB_UNITTEST_TEST_NAME after=%s' % (env['SCB_UNITTEST_TEST_NAME'])
])
log(12, [
'relpath = %s' % (relpath),
'SCB_EXECUTABLE_NAME: %s' % (tryGetEnvironment(env, 'SCB_EXECUTABLE_NAME')),
'SCB_STATIC_LIB_NAME: %s' % (tryGetEnvironment(env, 'SCB_STATIC_LIB_NAME')),
'SCB_SHARED_LIB_NAME: %s' % (tryGetEnvironment(env, 'SCB_SHARED_LIB_NAME')),
])
printEnv(verbosity(), env)
env.VariantDir(env['SCB_VARIANT_DIR'], path, duplicate=0)
dirsToScan = listDirectories(path, env['SCB_SKIP_DIRS'])
for entry in dirsToScan:
dirToScan = os.path.join(path, entry)
prepareEnvironment(dirToScan)
return env
def updateEnvironment(env, path, restrictList = []):
relpath = relativePath(path)
log(4, ['Updating environment for %s ' % (relpath)])
filename = os.path.join(path, SconsBuilderConfig.SCONSBUILDER_CONFIG_FILE_NAME)
try:
configfile = file(filename)
except:
return
dict = env.Dictionary()
config = {}
afterActionItems = {}
beforeActionItems = {}
fileActionItems = {}
unittestActionItems = []
unittestrunnerActionItems = []
actionItems = []
sconsfiles = []
lines = []
lineno = 0
for line in configfile:
lineno += 1
errorprefix = 'error in ' + filename + ' line ' + str(lineno)
line = string.strip(line)
if line == '':
continue
if line[0] == '#':
continue
items = line.split()
match = True
again = True
afterItems = None
beforeItems = None
fileItems = None
unittestItems = None
unittestrunnerItems = None
while again:
again = False;
if items[0] == 'platform':
if items[1] == str(env['SCB_PLATFORM']):
log(9, ['Detected %s match %s for line %s' % ('platform', str(env['SCB_PLATFORM']), line)])
again = True
items = items[2:]
else:
log(9, ['Detected %s mismatch %s for line %s' % ('platform', str(env['SCB_PLATFORM']), line)])
match = False
elif items[0] == 'configuration':
if items[1] == env['SCB_BUILD_CONFIGURATION']:
log(9, ['Detected %s match %s for line %s' % ('configuration', str(env['SCB_BUILD_CONFIGURATION']), line)])
again = True
items = items[2:]
else:
log(9, ['Detected %s mismatch %s for line %s' % ('configuration', str(env['SCB_BUILD_CONFIGURATION']), line)])
match = False
elif items[0] == 'target':
if items[1] == env['SCB_BUILD_TARGET']:
log(9, ['Detected %s match %s for line %s' % ('target', str(env['SCB_BUILD_TARGET']), line)])
again = True
items = items[2:]
else:
log(9, ['Detected %s mismatch %s for line %s' % ('target', str(env['SCB_BUILD_TARGET']), line)])
match = False
elif items[0] == 'after':
filename = items[1]
if os.path.exists(filename):
try:
afterItems = afterActionItems[filename]
except:
afterActionItems[filename] = []
afterItems = afterActionItems[filename]
log(9, ['Detected %s for file %s for line %s' % ('after', filename, line)])
again = True
items = items[2:]
else:
log(1, ['Detected %s ignored for nonexistent file %s for line %s' % ('after', filename, line)])
match = False
elif items[0] == 'before':
filename = items[1]
if os.path.exists(filename):
try:
beforeItems = beforeActionItems[filename]
except:
beforeActionItems[filename] = []
beforeItems = beforeActionItems[filename]
log(9, ['Detected %s for file %s for line %s' % ('before', filename, line)])
again = True
items = items[2:]
else:
log(1, ['Detected %s ignored for nonexistent file %s for line %s' % ('before', filename, line)])
match = False
elif items[0] == 'file':
filename = items[1]
filepath = os.path.join(path, filename)
if os.path.exists(filepath):
try:
fileItems = fileActionItems[filename]
except:
fileActionItems[filename] = []
fileItems = fileActionItems[filename]
log(9, ['Detected %s for file %s for line %s' % ('file', filename, line)])
again = True
items = items[2:]
else:
log(1, ['Detected %s ignored for nonexistent file %s for line %s' % ('file', filename, line)])
match = False
elif items[0] == 'unittest':
unittestItems = unittestActionItems
log(9, ['Detected %s for line %s' % ('unittest', line)])
again = True
items = items[1:]
elif items[0] == 'unittestrunner':
unittestrunnerItems = unittestrunnerActionItems
log(9, ['Detected %s for line %s' % ('unittestrunner', line)])
again = True
items = items[1:]
elif items[0] == 'sconsfile':
filename = items[1]
filepath = os.path.join(path, filename)
if os.path.exists(filepath):
log(9, ['Detected %s for file %s for line %s' % ('sconsfile', filename, line)])
sconsfiles.append(filename)
match = False
else:
log(1, ['Detected %s ignored for nonexistent file %s for line %s' % ('sconsfile', filename, line)])
match = False
if not match:
log(6, ['Ignoring unmatched line %s' % (line)])
else:
name = items[0]
action = items[1]
value = line[line.find(action) + len(action):].strip()
actionItem = (name, action, value)
log(13, ['actionItem=%s' % (repr(actionItem))])
if afterItems is not None:
afterItems.append(actionItem)
elif beforeItems is not None:
beforeItems.append(actionItem)
elif fileItems is not None:
fileItems.append(actionItem)
elif unittestItems is not None:
unittestItems.append(actionItem)
elif unittestrunnerItems is not None:
unittestrunnerItems.append(actionItem)
else:
actionItems.append(actionItem)
log(10, [
'afterItems=%s' % (afterItems),
'beforeItems=%s' % (beforeItems),
'fileItems=%s' % (fileItems),
'unittestItems=%s' % (unittestItems),
'unittestrunnerItems=%s' % (unittestrunnerItems)
])
log(10, [
'actionItems=%s' % (actionItems),
'afterActionItems=%s' % (afterActionItems),
'beforeActionItems=%s' % (beforeActionItems),
'fileActionItems=%s' % (fileActionItems),
'unittestActionItems=%s' % (unittestActionItems),
'unittestrunnerActionItems=%s' % (unittestrunnerActionItems)
])
for item in unittestActionItems:
env.AppendUnique(SCB_UNITTEST_MODIFY=item)
for item in unittestrunnerActionItems:
env.AppendUnique(SCB_UNITTESTRUNNER_MODIFY=item)
env.Replace(SCB_FILE_MODIFY=fileActionItems)
log(10, [
'env[\'SCB_UNITTEST_MODIFY\']=%s' % (repr(tryGetEnvironment(env, 'SCB_UNITTEST_MODIFY'))),
'env[\'SCB_UNITTESTRUNNER_MODIFY\']=%s' % (repr(tryGetEnvironment(env, 'SCB_UNITTESTRUNNER_MODIFY'))),
'env[\'SCB_FILE_MODIFY\']=%s' % (repr(tryGetEnvironment(env, 'SCB_FILE_MODIFY')))
])
if len(restrictList) > 0:
restrictedItems = []
for key, action, value in actionItems:
if key in restrictList:
restrictedItems.append((key, action, value))
try:
modifyEnvironment(env, restrictedItems)
except Exception, ex:
msg = 'ModifyEnvironment (restricted) for %s failed: %s' % (relpath, str(ex))
raise Exception(msg)
else:
try:
if actionItems:
log(4, ['Modifying environment for %s' % (relpath)])
modifyEnvironment(env, actionItems)
except Exception, ex:
msg = 'ModifyEnvironment for %s failed: %s' % (relpath, str(ex))
raise Exception(msg)
# if the environment belongs to a unit test add the unit test actions
unittestActionItems = tryGetEnvironment(env, 'SCB_UNITTEST_MODIFY')
unittestDirName = tryGetEnvironment(env, 'SCB_UNITTEST_DIR_NAME')
if unittestActionItems and \
unittestDirName and \
os.path.basename(path) == unittestDirName:
log(4, ['Modifying environment for %s (unit test modifications)' % (relpath)])
modifyEnvironment(env, unittestActionItems)
# execute the user SConscript files
for filename in sconsfiles:
path = os.path.join(env['SCB_RELATIVE_PATH'], filename)
actionItems = None
try:
actionItems = beforeActionItems[filename]
except:
pass
try:
if actionItems:
log(4, ['Modifying environment for %s (before %s)' % (relpath, filename)])
modifyEnvironment(env, actionItems)
except Exception, ex:
msg = 'ModifyEnvironment before User SConscript %s failed: %s' % (path, str(ex))
raise Exception(msg)
try:
log(2, ['Executing User SConscript %s' % (path)])
env.SConscript(
path,
variant_dir=env['SCB_VARIANT_DIR'],
duplicate=0,
exports=['env', 'path']
)
except Exception, ex:
msg = 'User SConscript %s failed: %s' % (path, str(ex))
raise Exception(msg)
# if the script changed the directory, change it back to SCB_SCRIPT_DIR
if os.getcwd() != SCB_SCRIPT_DIR:
os.chdir(SCB_SCRIPT_DIR)
try:
actionItems = afterActionItems[filename]
except:
pass
try:
if actionItems:
log(4, ['Modifying environment for %s (after %s)' % (relpath, filename)])
modifyEnvironment(env, actionItems)
except Exception, ex:
msg = 'ModifyEnvironment after User SConscript %s failed: %s' % (path, str(ex))
raise Exception(msg)
def modifyEnvironment(env, actions):
if actions == None:
return
for key, action, value in actions:
log(12, [
'key=%s' % (key),
'action=%s' % (action),
'value=%s' % (value)
])
try:
value = eval(value.strip())
except:
value = value.strip()
cmd = None
if not env.has_key(key):
env[key] = SCons.Util.CLVar()
try:
if action == 'append' or action == 'add':
cmd = 'env.Append(' + str(key) + '=' + repr(value) + ')'
elif action == 'appendunique':
cmd = 'env.AppendUnique(' + str(key) + '=' + repr(value) + ')'
elif action == 'prepend':
cmd = 'env.Prepend(' + str(key) + '=' + repr(value) + ')'
elif action == 'prependunique':
cmd = 'env.PrependUnique(' + str(key) + '=' + repr(value) + ')'
elif action == 'default':
cmd = 'env.SetDefault(' + str(key) + '=' + repr(value) + ')'
elif action == 'replace' or action == 'reset':
cmd = 'env.Replace(' + str(key) + '=' + repr(value) + ')'
elif action == 'remove':
try:
log(11, ['Removing %s from %s=\'%s\'' % (value, key, env[key])])
env[key].remove(value)
log(5, ['Removed %s from %s' % (value, key)])
except Exception, e:
log(0, ['Can not remove %s from %s=\'%s\'' % (value, key, env[key])])
else:
raise Exception('Unknown keyword: %s' % (action))
except Exception, ex:
msg = '%s failed for %s %s %s: %s' % (action, key, action, value, str(ex))
raise Exception(msg)
log(11, ['cmd=%s' % (cmd)])
if cmd:
eval(cmd)
def handleModifications(env, key=None, file=None, clone=False):
log(8, ['handleModifications: key=%s, file=%s' % (key, file)])
modifications = []
try:
if file:
modifications = env['SCB_FILE_MODIFY'][file]
elif key:
modifications = env[key]
except:
pass
if len(modifications) > 0:
if key:
log(8, ['Modifications key=%s: %s' % (key, modifications)])
if file:
log(4, ['Modifications file=%s: %s' % (file, modifications)])
if clone:
env = env.Clone()
modifyEnvironment(env, modifications)
return env
def SymLinkAction(target, source, env):
for trg in target:
# symlink the target from the source
fullsrc = str(source[0])
src = os.path.basename(str(source[0]))
dst = str(trg)
log(11, ['SymLinkAction %s -> %s' % (src, dst)])
try:
os.remove(dst)
except:
pass
try:
log(12, ['symlink(%s, %s)' % (src, dst)])
os.symlink(src, dst)
except:
try:
log(12, ['hardlink(%s, %s)' % (fullsrc, dst)])
os.link(fullsrc, dst)
except:
try:
log(12, ['copy(%s, %s)' % (fullsrc, dst)])
os.copy(fullsrc, dst)
except Exception, ex:
log(0, ['SymLinkAction failed: %s' % (str(ex))])
raise ex