forked from tuttleofx/sconsProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
executable file
·1655 lines (1423 loc) · 60.7 KB
/
__init__.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
"""SConsProject
The SConsProject module proposes a way to easily create the compilation system
of your project with the minimum of information. It's an helper around SCons.
"""
from SCons.Environment import *
from SCons.Script import *
import sys
from time import *
import atexit
import os
import socket
import string
import subprocess
import getpass
import autoconf
import compiler
from utils import *
from utils.colors import *
def join_if_basedir_not_empty( *dirs ):
'''
Join directories like standard 'os.path.join' function but with the particular case that if the first directory is empty, the function return an empty string.
For example if you join '' and 'include', the result is ''.
'''
if not dirs or not dirs[0]:
return ''
return os.path.join(*dirs)
class SConsProject:
'''
This is a base class helper for SCons build tool.
In your SConstruct simply do:
########################################
# Example 1
from sconsProject import SConsProject
project = SConsProject()
Export('project')
Export({'libs':project.libs})
project.begin()
project.SConscript()
project.end()
########################################
# Example 2
# If you have common creation things in your project, create a class for your project which inherite this class.
# So this function is accessible in all SConscript files.
# You can also overload some SConsProject function to cusomize it.
class MyProject( SConsProject ):
def createCustomPlugins( self, sources=[], libs=[] ):
"""
Create a particular type of plugins from a sources list and a libraries list.
"""
pluginName = self.getName()
env_local = self.createEnv( libs )
env_local.AppendUnique( CCFLAGS = self.CC['visibilityhidden'] )
plugin = env_local.SharedLibrary( target=pluginName, source=sources )
env_local.InstallAs( self.inOutputBin(), plugin )
project = MyProject(
Export('project')
Export({'libs':project.libs})
project.begin()
project.SConscript()
project.end()
########################################
'''
now = strftime("%Y-%m-%d_%Hh%Mm%S", localtime())
osname = os.name.lower()
sysplatform = sys.platform.lower()
hostname = socket.gethostname()
windows = osname == "nt" and sysplatform.startswith("win")
macos = sysplatform.startswith("darwin")
linux = not windows and not macos
unix = not windows
user = getpass.getuser()
modes = ('debug', 'release', 'production')
compil_mode = 'unknown_mode'
dir = os.getcwd()
dir_output_build = 'undefined' #
dir_output = 'undefined' #
dir_output_bin = 'undefined' # name generated depending on compilation type,
dir_output_lib = 'undefined' # we need to know if we are in debug mode, etc.
dir_output_plugin = 'undefined'
dir_output_header = 'undefined' # (options needs to be initilized)
dir_output_test = 'undefined' #
dir_sconsProject = os.path.abspath(os.path.dirname(__file__)) # directory containing this file
compiler = None
libs = autoconf
commonLibs = [libs.sconsProject]
libs_help = [] # temporary list of librairies already added to help
libs_error = [] # list of libraries with autoconf error
allLibsChecked = [] # temporary list of librairies already checked
removedFromDefaultTargets = {}
env = Environment( tools=[
'packaging',
'doxygen',
'unittest',
'scripttest',
] + (['msvs'] if windows else []),
toolpath=[os.path.join(dir_sconsProject,'tools')] )
allVisualProjects = []
def __init__(self):
'''
Initialisation of variables depending on computer.
'''
self.allTargets = {}
if self.windows:
self.packagetype = 'msi'
else:
self.packagetype = 'rpm'
if self.unix:
if (os.uname()[4][-3:] == '_64'):
self.bits = 64
else:
self.bits = 32
elif self.windows:
if 'PROGRAMFILES(X86)' not in os.environ:
self.bits = 32
else:
self.bits = 64
sconf = [
'display',
'default',
'local',
'host',
]
if self.unix:
sconf.append( 'unix' )
sconf.append( 'unix-'+str(self.bits) )
if self.linux:
sconf.append( 'linux' )
sconf.append( 'linux-'+str(self.bits) )
elif self.macos:
sconf.append( 'macos' )
sconf.append( 'macos-'+str(self.bits) )
elif self.windows:
sconf.append( 'windows' )
sconf.append( 'windows-'+str(self.bits) )
sconf.append( self.hostname )
sconf.append( 'user' )
sconf.append( self.user )
sconf.append( 'finalize' )
sconf_sconsProject = ['display', 'default']
self.sconf_files = [
os.path.join(self.dir_sconsProject, s)+'.sconf' for s in sconf_sconsProject
] + [
os.path.join(self.dir, s)+'.sconf' for s in sconf
]
self.sconf_files = [ f for f in self.sconf_files if os.path.exists(f) ]
#if self.windows:
self.env['ENV']['PATH'] = os.environ['PATH'] # access to the compiler (if not in '/usr/bin')
# scons optimizations...
# http://www.scons.org/wiki/GoFastButton
#
# Next line is important, it deactivates tools search for default variable, just note that now in SConscript you have
# to use env.Program(...) instead of simply Program().
SCons.Defaults.DefaultEnvironment(tools = [])
# Avoid RCS and SCCS scans by using env.SourceCode(".", None) - this is especially interesting if you are using lots of c or c++ headers in your program and that your file system is remote (nfs, samba).
self.env.SourceCode('.', None)
# as of SCons 0.98, you can set the Decider function on an environment. MD5-timestamp says if the timestamp matches, don't bother re-MD5ing the file. This can give huge speedups.
self.env.Decider('MD5-timestamp')
# This option tells SCons to intelligently cache implicit dependencies. It attempts to determine if the implicit dependencies have changed since the last build, and if so it will recalculate them. This is usually slower than using --implicit-deps-unchanged, but is also more accurate.
SetOption('implicit_cache', 1)
# By default SCons will calculate the MD5 checksum of every source file in your build each time it is run, and will only cache the checksum after the file is 2 days old. This default of 2 days is to protect from clock skew from NFS or revision control systems. You can tweak this delay using --max-drift=SECONDS where SECONDS is some number of seconds. Decreasing SECONDS can improve build speed by eliminating superfluous MD5 checksum calculations.
SetOption('max_drift', 60 * 15) # cache the checksum after max_drift seconds
# Normally you tell Scons about include directories by setting the CPPPATH construction variable, which causes SCons to search those directories when doing implicit dependency scans and also includes those directories in the compile command line. If you have header files that never or rarely change (e.g. system headers, or C run-time headers), then you can exclude them from CPPPATH and include them in the CCFLAGS construction variable instead, which causes SCons to ignore those include directories when scanning for implicit dependencies. Carefully tuning the include directories in this way can usually result in a dramatic speed increase with very little loss of accuracy.
# To achieve this we add a new variable 'EXTERNCPPPATH' which is the same as CPPPATH but without searching for implicit dependencies in those directories. So we always use EXTERNCPPPATH for external libraries.
self.env['_CPPINCFLAGS'] = '$( ${_concat(INCPREFIX, CPPPATH, INCSUFFIX, __env__, RDirs, TARGET, SOURCE)} ' \
'${_concat(INCPREFIX, EXTERNCPPPATH, INCSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'
self.env['_join_if_basedir_not_empty'] = join_if_basedir_not_empty
#------------------------------------ Utils -----------------------------------#
def printInfos(self):
'''
Print information at compilation's begining.
'''
sys.stdout.write(self.env['color_info'])
print ':' * 80
print '::' + ' '*32, self.env['mode'], 'mode'
print ':' * 80
print ':: dir = ' + self.dir
print ':: dir_output_build = ' + self.dir_output_build
print ':: dir_output_bin = ' + self.dir_output_bin
print ':: dir_output_plugin = ' + self.dir_output_plugin
print ':: dir_output_lib = ' + self.dir_output_lib
print ':: dir_output_test = ' + self.dir_output_test
print ':: dir_sconsProject = ' + self.dir_sconsProject
print ':: now = ' + self.now
print ':: osname = ' + self.osname
print ':: sysplatform = ' + self.sysplatform
print ':: hostname = ' + self.hostname
print ':: compiler c = %s (%s)' % (self.env['CC'], self.env['CCVERSION'])
print ':: compiler c++ = %s (%s)' % (self.env['CXX'], self.env['CXXVERSION'])
print ':: parallel jobs = %d' % (GetOption('num_jobs'))
if self.env['ccache']:
print ':: ccachedir = ' + self.env['ccachedir']
print(':' * 80)
sys.stdout.write(self.env['color_clear'])
def printEnv(self, env=None, keys=[]):
'''
Debug function to display all environement options.
'''
if not env:
print ':' * 20, ' env ', ':' * 20
env = self.env
if not keys:
sys.stdout.write(self.env['color_info'])
print env.Dump()
else:
print '*' * 50, 'keys: ', keys
dict = env.Dictionary()
for key in keys:
if key in dict:
sys.stdout.write(self.env['color_info'])
print ':' * 10, ' %s = %s' % (key, dict[key])
sys.stdout.write(self.env['color_clear'])
def getAllAbsoluteCwd(self, relativePath=None):
'''
Returns current directory (in original path and VariantDir path) or relativePath in current directory.
Paths are absolute.
Returns a list.
'''
if isinstance(relativePath, list):
alldirs = []
for rp in relativePath:
alldirs.extend( self.getAllAbsoluteCwd(rp) )
return alldirs
if relativePath:
if relativePath.startswith('#'):
return [os.path.join(self.dir, relativePath[1:]),
os.path.join(self.dir_output_build, relativePath[1:])]
elif os.path.isabs(relativePath):
if relativePath.startswith(self.dir_output_build):
return [relativePath,
os.path.join(self.dir, relativePath[len(self.dir_output_build)+1:])]
elif relativePath.startswith(self.dir):
return [relativePath,
os.path.join(self.dir_output_build, relativePath[len(self.dir)+1:])]
else:
return [relativePath]
else:
return [os.path.join(Dir('.').srcnode().abspath, relativePath),
os.path.join(Dir('.').abspath, relativePath)]
else:
return [Dir('.').srcnode().abspath,
Dir('.').abspath]
def getRealAbsoluteCwd(self, relativePath=None):
'''
Returns original current directory (not inside the VariantDir) or relativePath in original current directory.
Paths are absolute.
'''
if isinstance(relativePath, list):
return [self.getRealAbsoluteCwd(rp) for rp in relativePath]
cdir = Dir('.').srcnode().abspath
if relativePath:
if isinstance(relativePath, SCons.Node.FS.Dir):
return relativePath.srcnode().abspath
elif relativePath.startswith('#'):
return os.path.join(self.dir, relativePath[1:])
elif os.path.isabs(relativePath):
if relativePath.startswith(self.dir_output_build):
return os.path.join(self.dir, relativePath[len(self.dir_output_build)+1:])
return relativePath
return os.path.join(cdir, relativePath)
else:
return cdir
def getAbsoluteCwd(self, relativePath=None):
'''
Returns current directory or relativePath in current directory.
Paths are absolute.
'''
if isinstance(relativePath, list):
return [self.getAbsoluteCwd(rp) for rp in relativePath]
cdir = Dir('.').abspath
if relativePath:
if relativePath.startswith('#'):
return os.path.join(self.dir_output_build, relativePath[1:])
elif os.path.isabs(relativePath):
return relativePath
return os.path.join(cdir, relativePath)
else:
return cdir
def getCwdInProject(self):
cdir = Dir('.').srcnode().abspath
return os.path.relpath(cdir, self.dir)
def getSubDirsAbsolutePath(self, current_dir=None):
'''Returns sub-directories with absolute paths (in original file tree).'''
if current_dir == None:
current_dir = self.getRealAbsoluteCwd()
else:
current_dir = Dir('./' + current_dir).srcnode().abspath
files = (os.listdir(current_dir)) # relative paths (only directories names)
#files.append(current_dir)
nonhidden = (f for f in files if f[0] != '.' and f.find('@'))
absfiles = (os.path.join(current_dir, f) for f in nonhidden) # absolute paths
dirs = (f for f in absfiles if os.path.isdir(f))
return dirs
def getSubDirs(self, current_dir=None):
'''Returns sub-directories with relative paths (in original file tree).'''
return map(os.path.basename, self.getSubDirsAbsolutePath(current_dir)) # absolute path -> relative path (for variant_dir)
def getSubDirsWithSConscript(self):
'''Returns sub-directories containing a SConscript file with relative paths (in original file tree).'''
alldirs = self.getSubDirsAbsolutePath()
dirs = (f for f in alldirs if os.path.isfile(os.path.join(f, 'SConscript')))
ldirs = map(os.path.basename, dirs) # absolute path -> relative path (for variant_dir)
return ldirs
def inBuildDir(self, * dirs):
'''Returns "dirs" as subdirectories of temporary "buildDir".'''
if not dirs:
return string.replace(os.getcwd(), self.dir, self.dir_output_build, 1)
if len(dirs) == 1 and isinstance(dirs[0], str):
d = dirs[0]
if not d.startswith(self.dir_output_build):
return string.replace(d, self.dir, self.dir_output_build, 1)
else:
return d
l_dirs = SCons.Util.flatten(dirs)
return [ self.inBuildDir(d) for d in l_dirs ]
def inTopDir(self, * dirs):
'''Returns "dirs" as subdirectories of "topDir".'''
if not dirs:
return self.dir
if len(dirs) == 1:
if issubclass(dirs[0].__class__, SCons.Node.FS.Base):
return dirs[0]
elif isinstance(dirs[0], str):
if os.path.isabs( dirs[0] ):
return dirs[0]
return os.path.join(self.inTopDir(), dirs[0])
l_dirs = SCons.Util.flatten(dirs)
return [ self.inTopDir(d) for d in l_dirs ]
def inOutputDir(self, *dirs):
'''Returns "dirs" as subdirectories of "outputDir".'''
if not dirs:
return self.dir_output
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputDir(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputDir(d) for d in l_dirs ]
def inOutputLib(self, *dirs):
'''Returns "dirs" as subdirectories of "outputLib".'''
if not dirs:
return self.dir_output_lib
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputLib(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputLib(d) for d in l_dirs ]
def inOutputHeaders(self, *dirs):
'''Returns "dirs" as subdirectories of "outputHeaders".'''
if not dirs:
return self.dir_output_header
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputHeaders(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputHeaders(d) for d in l_dirs ]
def inOutputBin(self, *dirs):
'''Returns "dirs" as subdirectories of "outputBin".'''
if not dirs:
return self.dir_output_bin
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputBin(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputBin(d) for d in l_dirs ]
def inOutputPlugin(self, *dirs):
'''Returns "dirs" as subdirectories of "outputPlugin".'''
if not dirs:
return self.dir_output_plugin
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputPlugin(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputPlugin(d) for d in l_dirs ]
def inOutputTest(self, *dirs):
'''Returns "dirs" as subdirectories of "outputTest".'''
if not dirs:
return self.dir_output_test
if len(dirs) == 1 and isinstance(dirs[0], str):
return os.path.join( self.inOutputTest(), dirs[0] )
l_dirs = SCons.Util.flatten(dirs)
return [ self.inOutputTest(d) for d in l_dirs ]
def getName(self, n=1):
'''Create a name using the current directory. "n" is the number of parents to build the name.'''
v = self.getCwdInProject().split(os.sep)
if n == 0:
return '_'.join( v )
return '_'.join( v[-n:] )
def getDirs(self, n=1):
'''Create a list of upper directories. "n" is the number of parents.'''
alldirs = self.getCwdInProject().split(os.sep)
if isinstance( n, list ):
return [alldirs[i] for i in n]
else:
return alldirs[-n:]
def convertSconsPathToStr(self, *dirs):
'''Returns "dirs" as str.'''
if len(dirs) == 1:
if issubclass(dirs[0].__class__, SCons.Node.FS.Base):
return dirs[0].srcnode().abspath
elif isinstance(dirs[0], str):
return dirs[0]
l_dirs = SCons.Util.flatten(dirs)
return [ self.convertSconsPathToStr(d) for d in l_dirs ]
def needConfigure(self):
'''If the target builds nothing, we don't need to call the configure function.'''
return not GetOption('clean') and not GetOption('help')
def needCheck(self):
'''If we check all libraries before compiling.'''
return self.env['check_libs']
#------------------------- Compilation options ----------------------------#
def initOptions(self):
'''
Read options from configuration files and at last from the command line
(which has the last word)
'''
self.env.Tool('default')
# default values
if self.windows:
self.compiler = compiler.visual
else:
self.compiler = compiler.gcc
self.CC = self.compiler.CC
# options from command line or configuration file
self.opts = self.createOptions(self.sconf_files, ARGUMENTS)
self.defineHiddenOptions(self.opts)
self.opts_help = self.createOptions(self.sconf_files, ARGUMENTS)
self.opts.Update(self.env)
if 'icecc' in self.env['CC']:
self.env['CCVERSION'] = self.compiler.retrieveVersion(self.env['ICECC_CC'])
self.env['CXXVERSION'] = self.compiler.retrieveVersion(self.env['ICECC_CXX'])
self.env['ENV']['ICECC_CC'] = self.env['ICECC_CC']
self.env['ENV']['ICECC_CXX'] = self.env['ICECC_CXX']
self.compiler.setup(self.env['ICECC_CC'], self.env['ICECC_CXX'])
else:
self.env['CCVERSION'] = self.compiler.retrieveVersion(self.env['CC'])
self.env['CXXVERSION'] = self.compiler.retrieveVersion(self.env['CXX'])
self.compiler.setup(self.env['CC'], self.env['CXX'])
# select the environment from user options
compilerName = self.env['compiler']
self.compiler = eval( 'compiler.' + compilerName )
self.CC = self.compiler.CC
if self.windows:
if compilerName == 'visual':
self.env.Tool('default')
self.env.Tool('msvc')
elif compilerName == 'gcc':
self.env.Tool('mingw')
else:
self.env.Tool('default')
print 'Error: Unrecognized compiler value on this platform. ('+str(compilerName)+')'
def createOptions(self, filename, args):
'''
Define options.
'''
opts = Variables(filename, args)
def help_format(env, opt, help, default, actual, aliases):
alignment = ' '*(len(opt)+2)
multilineHelp = help.replace('\n', '\n'+alignment)
return '%s%s%s %s\n%s(default=%s, actual=%s)\n\n' % (self.env['color_title'], opt, self.env['color_clear'], multilineHelp, alignment, default, actual)
opts.FormatVariableHelpText = help_format
opts.Add(EnumVariable('mode', 'Compilation mode', 'production', allowed_values=self.modes))
opts.Add(BoolVariable('install', 'Install', False))
opts.Add(BoolVariable('profile', 'Build with profiling support', False))
opts.Add(BoolVariable('cover', 'Build with cover support', False))
opts.Add(BoolVariable('clean', 'Remove all the build directory', False))
opts.Add(BoolVariable('ignore_configure_errors', 'Ignore "configure" errors. The default target will only build the possible targets', False))
# opts.Add( BoolVariable( 'log', 'Enable output to a log file', False ) )
opts.Add(BoolVariable('ccache', 'Enable compiler cache system (ccache style)', False))
opts.Add(PathVariable('ccachedir', 'Cache directory', 'ccache', PathVariable.PathAccept))
opts.Add(BoolVariable('colors', 'Using colors of the terminal', True if not self.windows else False))
opts.Add('default', 'Default objects to build', 'all')
opts.Add('aliases', 'A list of custom aliases.', [])
opts.Add('jobs', 'Parallel jobs', '1')
opts.Add(BoolVariable('check_libs', 'Enable/Disable lib checking', True))
opts.Add('CC', 'Specify the C Compiler', self.compiler.ccBin)
opts.Add('CXX', 'Specify the C++ Compiler', self.compiler.cxxBin)
opts.Add('SCRIPTTESTXX', 'Specify the script test binary', "nosetests")
opts.Add('SCRIPTTESTFLAGS', 'Specify the script test flags', "")
opts.Add('ENVINC', 'Additional include path (at compilation)', [] if not self.windows else os.environ.get('INCLUDE', '').split(':'))
opts.Add('ENVPATH', 'Additional bin path (at compilation)', [])
opts.Add('ENVLIBPATH', 'Additional librairie path (at compilation)', [] if not self.windows else os.environ.get('LIB', '').split(':'))
if self.windows:
opts.Add(PathVariable('PROGRAMFILES', 'Program Files directory', os.environ.get('PROGRAMFILES', ''), PathVariable.PathAccept))
opts.Add('CPPPATH', 'Additional preprocessor paths', [])
opts.Add('CPPDEFINES', 'Additional preprocessor defines', [])
opts.Add('LIBPATH', 'Additional library paths', [])
opts.Add('LIBS', 'Additional libraries', [])
# Don't explicitly put include directory arguments in CCFLAGS or CXXFLAGS
# because the result will be non-portable and the directories will not
# be searched by the dependency scanner.
opts.Add('CCFLAGS', 'Additional C and C++ flags', [])
opts.Add('CFLAGS', 'Additional C flags', [])
opts.Add('CXXFLAGS', 'Additional C++ flags', [])
opts.Add('LINKFLAGS', 'Additional linker flags', [])
opts.Add('ICECC_CC', 'Compilator', self.compiler.ccBin)
opts.Add('ICECC_CXX', 'Compilator', self.compiler.cxxBin)
opts.Add('ICECC_VERSION', 'Compilator', '')
buildDirName = '.dist' # base dir name for all intermediate compilation objects
distDirName = 'dist' # base dir name for output build
opts.Add(PathVariable('BUILDPATH', 'Top directory of compilation tree', self.dir, PathVariable.PathIsDir))
opts.Add('BUILDDIRNAME', 'Top directory of compilation tree', buildDirName)
opts.Add(PathVariable('DISTPATH', 'Top directory to output compiled files', self.dir, PathVariable.PathIsDir))
opts.Add('DISTDIRNAME', 'Directory name to output compiled files', distDirName)
opts.Add(PathVariable('INSTALLPATH', 'Top directory to install compiled files', '${DISTPATH}/${DISTDIRNAME}', PathVariable.PathIsDirCreate))
return opts
def defineHiddenOptions(self, opts):
'''
Define basics options which don't need to be visible in the help.
'''
opts.Add(PathVariable('TOPDIR', 'Top directory', self.dir))
opts.Add('osname', 'OS name', 'windows' if self.windows else 'unix')
opts.Add('osbits', 'OS bits', self.bits)
opts.Add(BoolVariable('unix', 'operating system', self.unix))
opts.Add(BoolVariable('linux', 'operating system', self.linux))
opts.Add(BoolVariable('windows', 'operating system', self.windows))
opts.Add(BoolVariable('macos', 'operating system', self.macos))
opts.Add('compiler', 'Choose compiler mode. This defines all flag system to use.', 'visual' if self.windows else 'gcc')
opts.Add('EXTERNCPPPATH', 'Additional preprocessor paths (like CPPPATH but without dependencies check)', [])
# display options
opts.Add('SHCCCOMSTR', 'display option', '$SHCCCOM')
opts.Add('SHCXXCOMSTR', 'display option', '$SHCXXCOM')
opts.Add('SHLINKCOMSTR', 'display option', '$SHLINKCOM')
opts.Add('CCCOMSTR', 'display option', '$CCCOM')
opts.Add('CXXCOMSTR', 'display option', '$CXXCOM')
opts.Add('LINKCOMSTR', 'display option', '$LINKCOM')
opts.Add('ARCOMSTR', 'display option', '$ARCOM')
opts.Add('INSTALLSTR', 'display option', 'Install file: $SOURCE as $TARGET')
opts.Add('SWIG', 'swig binary', 'swig')
opts.Add('SWIGCOMSTR', 'display option', '$SWIGCOM')
opts.Add('QT_MOCFROMCXXCOMSTR', 'display option', '$QT_MOCFROMCXXCOM')
opts.Add('QT_MOCFROMHCOMSTR', 'display option', '$QT_MOCFROMHCOM')
opts.Add('QT_UICCOMSTR', 'display option', '$QT_UICCOM')
opts.Add('color_clear', 'color', colors['clear'])
opts.Add('color_red', 'color', colors['red'])
opts.Add('color_redB', 'color', colors['redB'])
opts.Add('color_green', 'color', colors['green'])
opts.Add('color_blue', 'color', colors['blue'])
opts.Add('color_blueB', 'color', colors['blueB'])
opts.Add('color_yellow', 'color', colors['yellow'])
opts.Add('color_brown', 'color', colors['brown'])
opts.Add('color_violet', 'color', colors['violet'])
opts.Add('color_autoconf', 'color', '')
opts.Add('color_header', 'color', '')
opts.Add('color_title', 'color', '')
opts.Add('color_compile', 'color', '')
opts.Add('color_link', 'color', '')
opts.Add('color_install', 'color', '')
opts.Add('color_info', 'color', '')
opts.Add('color_success', 'color', '')
opts.Add('color_warning', 'color', '')
opts.Add('color_fail', 'color', '')
opts.Add('color_error', 'color', colors['error'])
def applyOptionsOnProject(self):
'''
Some options are used to modify the project (common to the whole compilation).
'''
subpath = os.path.join(self.hostname, '-'.join([self.compiler.name, self.env['CCVERSION']]), self.env['mode'])
self.bits = int(self.env['osbits'])
self.dir_output_build = os.path.join(self.env['BUILDPATH'], self.env['BUILDDIRNAME'], subpath)
install_dir = os.path.join(self.env['DISTPATH'], self.env['DISTDIRNAME'], subpath)
if self.env['install']:
install_dir = self.env['INSTALLPATH']
self.dir_output = install_dir
self.dir_output_bin = os.path.join(install_dir, 'bin')
self.dir_output_lib = os.path.join(install_dir, 'lib')
self.dir_output_plugin = os.path.join(install_dir, 'plugin')
self.dir_output_header = os.path.join(install_dir, 'include')
self.dir_output_test = os.path.join(install_dir, 'test')
# temporary files of SCons inside the build directory
self.env['CONFIGUREDIR'] = os.path.join(self.dir_output_build, 'sconf_temp')
self.env['CONFIGURELOG'] = os.path.join(self.dir_output_build, 'config.log')
SConsignFile(os.path.join(self.dir_output_build, 'sconsign.dblite'))
if self.env['ccache']:
if os.path.isabs(self.env['ccachedir']):
CacheDir(self.env['ccachedir'])
else:
CacheDir(os.path.join(self.dir_output_build, self.env['ccachedir']))
try:
SetOption('num_jobs', int(self.env['jobs']))
except:
pass
self.applyOptionsOnEnv(self.env)
def applyOptionsOnEnv(self, env):
'''
Some options are used to modify others.
'''
env.PrependENVPath('INCLUDE', self.env['ENVINC'])
env.PrependENVPath('PATH', self.env['ENVPATH'])
env.PrependENVPath('LIB', self.env['ENVLIBPATH'])
if not env['colors']:
for c in ['color_clear', 'color_red', 'color_redB', 'color_green', 'color_blue', 'color_blueB', 'color_yellow', 'color_brown', 'color_violet', 'color_autoconf', 'color_header', 'color_title', 'color_compile', 'color_link', 'color_install', 'color_info', 'color_success', 'color_warning', 'color_fail', 'color_error']:
env[c] = ''
def SConscript(self, dirs=[], exports=[]):
'''
To include SConscript from SConstruct, this automatically defines variantdirs.
'''
if not dirs:
sconscriptFilename = self.inBuildDir(self.getAbsoluteCwd('SConscript'))
SConscript( sconscriptFilename, exports=exports )
else:
for d in dirs:
SConscript( dirs=self.inBuildDir(d), exports=exports )
def begin(self):
'''
The begining function the SConstruct need to call at first of all.
'''
self.initOptions()
self.applyOptionsOnProject()
if self.env['clean']:
Execute(Delete(self.dir_output_build))
Exit(1)
self.printInfos()
VariantDir(self.dir_output_build, self.dir, duplicate=0)
def end(self):
'''
The last function call at the end by the SConstruct.
'''
if self.windows:
visualSolution = self.env.MSVSSolution(
target = 'project' + self.env['MSVSSOLUTIONSUFFIX'],
projects = self.allVisualProjects,
variant = [m.capitalize() for m in self.modes], )
self.env.Depends( visualSolution, self.allVisualProjects )
self.env.Alias( 'visualSolution', visualSolution )
def printInstalledFiles(target, source, env):
# Whatever it takes to build
for t in FindInstalledFiles():
print '*', t.name, ':'
print ' '*5, t.abspath
return None
printInstalledFilesCmd = self.env.Command('always', '', printInstalledFiles)
self.env.Alias('targets', printInstalledFilesCmd)
if self.libs_error:
sys.stdout.write(self.env['color_error'])
for lib in self.libs_error:
print "Error in '" + lib.name + "' library :"
if lib.error:
print '\t', lib.error
sys.stdout.write(self.env['color_clear'])
if not self.env['ignore_configure_errors']:
print ''
print ''
print ''
print ' Errors during the configure. Some external libraries are missing.'
print ' See "config.log" file to check the errors.'
print ''
print ' You could ignore these errors and build the possible targets.'
print ' >>> scons ignore_configure_errors=1'
print ''
print ''
Exit(1)
sys.stdout.write(self.env['color_clear'])
Help(
'''
-- Usefull scons options --
scons -Q : making the SCons output less verbose
scons -j : parallel builds
scons -i : continue building after it encounters an error
scons --tree : display all or part of the SCons dependency graph
scons --debug=presub : pre-substitution string that SCons uses to generate the command lines it executes
scons --debug=findlibs : display what library names SCons is searching for, and in which directories it is searching
'''
)
Help(
'''
-- Build options --
scons : build all plugins and programs
scons plugins : build all plugins
scons pluginName : build the plugin named 'pluginName'
scons doc : build doxygen documentation
'''
)
Help(self.opts_help.GenerateHelpText(self.env))
# user can add some aliases
for v in self.env['aliases']:
self.env.Alias(v[0], v[1:])
# by default compiles the target 'all'
if isinstance(self.env['default'], str):
Default( self.env['default'].split() )
else:
Default( self.env['default'] )
# register function to display compilation status at the end
# to avoid going through if SCons raises an exception (error in a SConscript)
atexit.register(utils.display_build_status, self.removedFromDefaultTargets)
#-------------------------------- Autoconf ------------------------------------#
def createEnv(self, libs=[], name=''):
'''
Create an environment from the common one and apply libraries configuration to this environment.
@todo : add opts=[] ?
'''
new_env = self.env.Clone()
new_libs = list(libs)
for lib in self.commonLibs:
new_libs.insert(0, lib) # prepend (self.libs.sconsProject)
return self.appendLibsToEnv( new_env, new_libs, name )
def appendLibsToEnv(self, env, libs=[], name=''):
'''
Append libraries to an environment.
'''
if not libs:
return env
sys.stdout.write(self.env['color_autoconf']) # print without new line
if 'SconsProjectLibraries' in env:
env['SconsProjectLibraries'] += libs
else:
env['SconsProjectLibraries'] = libs
opts_current = self.createOptions(self.sconf_files, ARGUMENTS)
self.defineHiddenOptions(opts_current)
allLibs = []
for eachlib in libs:
libdeps = self.findLibsDependencies(eachlib)
allLibs.extend( libdeps )
allLibs.append( (eachlib,0) )
allLibs = self.uniqLibs(allLibs)
#print 'libs:', [a.name for a in libs]
#print 'allLibs:', [a.name for a in allLibs]
#print '-'*10
for lib, level in allLibs:
if not lib.initOptions(self, opts_current):
if lib not in self.libs_error:
self.libs_error.append(lib)
if lib not in self.libs_help:
lib.initOptions(self, self.opts_help)
self.libs_help.append(lib)
opts_current.Update(env)
self.applyOptionsOnEnv(env)
for lib, level in allLibs:
lib.initEnv(self, env)
if self.needConfigure():
libs_error = []
for lib, level in allLibs:
if not lib.enabled(env):
print 'Target "'+name+'" compiled without "'+lib.name+'" library.'
else:
checkStatus = True
if not self.checkLibrary( lib ):
checkStatus = False
if not lib.configure(self, env):
checkStatus = False
else:
conf = env.Configure()
if not lib.check(self, conf):
checkStatus = False
env = conf.Finish()
if not checkStatus:
libs_error.append(lib)
#print '-- name:', name
#print '-- libs_error:', libs_error
#print '-- allLibs:', [a[0].name for a in allLibs]
if 'SconsProject_missingDependencies' not in env:
env['SconsProject_missingDependencies'] = []
env['SconsProject_missingDependencies'].extend([l.name for l in libs_error])
#print '-- SconsProject_missingDependencies:', env['SconsProject_missingDependencies']
for lib in libs_error:
if lib not in self.libs_error:
self.libs_error.append(lib)
for lib, level in allLibs:
lib.postconfigure(self, env, level)
sys.stdout.write(self.env['color_clear'])
return env
def checkLibrary( self, lib=None ):
'''
Create a temporary environment, apply all library dependencies and do
a check on lib.
'''
if lib.checkDone:
return True
# if it's an internal library, no check
if lib.sconsNode:
return True
if lib.name in self.allLibsChecked:
#print 'Already checked ', lib.name
lib.checkDone = True
return True
#print '_'*20
#print 'checkLibrary: ', lib.name
if not self.needCheck():
lib.checkDone = True
self.allLibsChecked.append( lib.name )
return True
dependencies = self.uniqLibs( self.findLibsDependencies(lib) )
checkStatus = True
#print "_"*50
#print "lib.name:", lib.name
#print "dependencies:", [d.name for d in dependencies]
check_env = self.env.Clone()
check_opts = self.createOptions(self.sconf_files, ARGUMENTS)
self.defineHiddenOptions(check_opts)
for a, level in dependencies:
a.initOptions(self, check_opts)
if not lib.initOptions(self, check_opts):
if lib not in self.libs_error:
self.libs_error.append(lib)
check_opts.Update(check_env)
self.applyOptionsOnEnv(check_env)
for a, level in dependencies:
a.initEnv(self, check_env)
lib.initEnv(self, check_env)
for a, level in dependencies:
a.configure(self, check_env)
if not lib.configure(self, check_env):
checkStatus = False
check_conf = check_env.Configure()
for a, level in dependencies:
a.check(self, check_conf)
if not lib.check(self, check_conf):
checkStatus = False
check_env = check_conf.Finish()
lib.checkDone = True
self.allLibsChecked.append( lib.name )
return checkStatus
def uniqLibs(self, allLibs):
'''
Return the list of libraries contains in allLibs without any duplication.
'''
libs = []
names = []
for s, level in allLibs:
if (s.name, s.id) not in names:
names.append( (s.name, s.id) )
libs.append( (s,level) )
return libs
def findLibsDependencies(self, libs):
'''
return the list of all dependencies of lib (without the lib itself).
'''
def internFindLibDependencies(lib, level=0):
if not lib:
return []
ll = []
for l in lib.dependencies:
ll.extend( internFindLibDependencies(l, level+1) )
ll.append( (lib,level) )
return ll
if not isinstance(libs, list):
libs = [libs]
ll = []
for lib in libs:
for l in lib.dependencies:
ll.extend( internFindLibDependencies(l) )
return ll
# todo
# def Install(self):
#
# env.AddPostAction(obj , Chmod(str(obj),bin_mode) )
#-------------------------------- Autoconf ------------------------------------#
def appendDict( self, dst, src ):
'''
Append the src dict into dst.
If elements are not a list type, put it into a list to merge the values.
'''
for k, v in src.items():
if k in dst:
vlist = (v if isinstance(v, list) else [v])
if isinstance(dst[k], list):
dst[k].extend( vlist )
else:
dst[k] = [dst[k]] + vlist
else:
dst[k] = v
def prepareIncludes(self, dirs):
objDirs = [Dir(d) for d in self.getAllAbsoluteCwd(dirs)]
objDirs = self.unique(objDirs)
return objDirs
def MSVSProject(self, targetName, buildTarget,
sources=[], headers=[], localHeaders=[],