Write _templates/SConscript sample file
[senf.git] / SConstruct
1 # -*- python -*-
2
3 import sys, glob, os.path, fnmatch
4 import SENFSCons, senfutil
5
6 ###########################################################################
7 # Load utilities and setup libraries and configure build
8
9 env = Environment()
10
11 # Load all the local SCons tools
12 env.Tool('Doxygen')
13 env.Tool('Dia2Png')
14 env.Tool('PkgDraw')
15 env.Tool('InstallSubdir')
16 env.Tool('CopyToDir')
17 env.Tool('Boost')
18 env.Tool('CombinedObject')
19 env.Tool('PhonyTarget')
20
21 env.Help("""
22 Additional top-level build targets:
23
24 prepare      Create all target files not part of the repository
25 default      Build all default targets (like calling scons with no arguments)
26 examples     Build all examples
27 all_tests    Build and run unit tests for all modules
28 all_docs     Build documentation for all modules
29 all          Build everything
30 install_all  Install SENF into $PREFIX
31 deb          Build debian source and binary package
32 debsrc       Build debian source package
33 debbin       Build debian binary package
34 linklint     Check links of doxygen documentation with 'linklint'
35 fixlinks     Fix broken links in doxygen documentation
36 valgrind     Run all tests under valgrind/memcheck
37 """)
38
39 class BuildTypeOptions:
40     def __init__(self, var):
41         self._var = var
42
43     def __call__(self, target, source, env, for_signature):
44         type = env['final'] and "final" or env['debug'] and "debug" or "normal"
45         return env[self._var + "_" + type]
46
47 env.Replace(
48    PKGDRAW                = 'doclib/pkgdraw',
49 )
50
51 env.Append(
52    ENV                    = { 'PATH' : os.environ.get('PATH') },
53    CLEAN_PATTERNS         = [ '*~', '#*#', '*.pyc', 'semantic.cache', '.sconsign*', '.sconsign' ],
54
55    CPPPATH                = [ '#' ],
56    LOCALLIBDIR            = '#',
57    LIBPATH                = [ '$LOCALLIBDIR' ],
58    LIBS                   = [ '$LIBSENF$LIBADDSUFFIX', 'rt', '$BOOSTREGEXLIB', 
59                               '$BOOSTIOSTREAMSLIB', '$BOOSTSIGNALSLIB', '$BOOSTFSLIB' ], 
60    TEST_EXTRA_LIBS        = [  ],
61
62    PREFIX                 = '#/dist',
63    LIBINSTALLDIR          = '$PREFIX${syslayout and "/lib" or ""}',
64    BININSTALLDIR          = '$PREFIX${syslayout and "/bin" or ""',
65    INCLUDEINSTALLDIR      = '$PREFIX${syslayout and "/include" or ""}',
66    OBJINSTALLDIR          = '$LIBINSTALLDIR${syslayout and "/$LIBINSTALLDIR/senf" or ""',
67    DOCINSTALLDIR          = '$PREFIX/docs',
68    CPP_INCLUDE_EXTENSIONS = [ '.h', '.hh', '.ih', '.mpp', '.cci', '.ct', '.cti' ],
69    CPP_EXCLUDE_EXTENSIONS = [ '.test.hh' ],
70
71    # These options are insane. Only useful for inline debugging. Need at least 1G free RAM
72    INLINE_OPTS_DEBUG      = [ '-finline-limit=20000', '-fvisibility-inlines-hidden', 
73                               '-fno-inline-functions', '-Winline' 
74                               '--param','large-function-growth=10000',
75                               '--param', 'large-function-insns=10000', 
76                               '--param','inline-unit-growth=10000' ],
77    INLINE_OPTS_NORMAL     = [ '-finline-limit=5000' ],
78    INLINE_OPTS            = [ '$INLINE_OPTS_NORMAL' ],
79    CXXFLAGS               = [ '-Wall', '-Woverloaded-virtual', '-Wno-long-long', '$INLINE_OPTS',
80                               '$CXXFLAGS_' ],
81    CXXFLAGS_              = BuildTypeOptions('CXXFLAGS'),
82    CXXFLAGS_final         = [ '-O3' ],
83    CXXFLAGS_normal        = [ '-O0', '-g' ],
84    CXXFLAGS_debug         = [ '$CXXFLAGS_normal' ],
85
86    CPPDEFINES             = [ '$expandLogOption', '$CPPDEFINES_' ],
87    expandLogOption        = senfutil.expandLogOption,
88    CPPDEFINES_            = BuildTypeOptions('CPPDEFINES'),
89    CPPDEFINES_final       = [ ],
90    CPPDEFINES_normal      = [ 'SENF_DEBUG' ],
91    CPPDEFINES_debug       = [ '$CPPDEFINES_normal' ],
92
93    LINKFLAGS              = [ '-rdynamic', '$LINKFLAGS_' ],
94    LINKFLAGS_             = BuildTypeOptions('LINKFLAGS'),
95    LINKFLAGS_final        = [ ],
96    LINKFLAGS_normal       = [ '-Wl,-S' ],
97    LINKFLAGS_debug        = [ ],
98 )
99
100 env.SetDefault(
101     LIBSENF   = "senf",
102     final     = 0,
103     debug     = 0,
104     syslayout = 0
105 )
106
107 # Set variables from command line
108 env.Replace(**ARGUMENTS)
109
110 Export('env')
111
112 # Create Doxyfile.local otherwise doxygen will barf on this non-existent file
113 # Create it even when cleaning, to silence the doxygen builder warnings
114 if not os.path.exists("Doxyfile.local"):
115     Execute(Touch("Doxyfile.local"))
116
117 if not env.GetOption('clean') and not os.path.exists(".prepare-stamp") \
118    and not os.environ.get("SCONS") and COMMAND_LINE_TARGETS != [ 'prepare' ]:
119     env.Execute([ "scons prepare" ])
120
121 # Load SConscripts
122
123 SConscriptChdir(0)
124 SConscript("debian/SConscript")
125 SConscriptChdir(1)
126 if os.path.exists('SConscript.local') : SConscript('SConscript.local')
127 SConscript("senf/SConscript")
128 SConscript("Examples/SConscript")
129 SConscript("HowTos/SConscript")
130 SConscript("doclib/SConscript")
131
132 ###########################################################################
133 # Define build targets
134
135 #### doc
136 env.Depends(SENFSCons.Doxygen(env), env.Value(env['ENV']['REVISION']))
137
138 #### libsenf.a
139 libsenf = env.Library("$LOCALLIBDIR/${LIBSENF}${LIBADDSUFFIX}", env['ALLOBJECTS'])
140 env.Default(libsenf)
141
142 env.Install('$LIBINSTALLDIR', libsenf)
143
144 #### install_all, default, all_tests, all
145 env.Alias('install_all', env.FindInstalledFiles())
146 env.Alias('default', DEFAULT_TARGETS)
147 env.Alias('all_tests', env.FindAllBoostUnitTests())
148 env.Alias('all', [ 'default', 'all_tests', 'examples', 'all_docs' ])
149
150 #### prepare
151 env.PhonyTarget('prepare', [], [])
152
153 #### valgrind
154 env.PhonyTarget('valgrind', [ 'all_tests' ], [ """
155     find -name .test.bin 
156         | while read test; do
157             echo;
158             echo "Running $$test";
159             echo;
160             valgrind --tool=memcheck --error-exitcode=99 --suppressions=valgrind.sup 
161                 $$test $BOOSTTESTARGS;
162             [ $$? -ne 99 ] || exit 1;
163         done
164 """.replace("\n"," ") ])
165
166 #### clean
167 env.Clean('all', '.prepare-stamp')
168 env.Clean('all', libsenf)
169 env.Clean('all', env.Dir('linklint')) # env.Dir to disambiguate from linklint PhonyTarget
170
171 if env.GetOption('clean'):
172     env.Clean('all', [ os.path.join(path,f)
173                        for path, subdirs, files in os.walk('.')
174                        for pattern in env['CLEAN_PATTERNS']
175                        for f in fnmatch.filter(files,pattern) ])
176
177 if not env.GetOption('clean') and not os.path.exists(".prepare-stamp"):
178     Execute(Touch(".prepare-stamp"))