blob: 00d37411bd5804be178a9a9eab58434c8a948203 [file] [log] [blame]
Baptiste Lepilleur32927b02008-01-21 08:37:06 +00001"""
2Build system can be clean-up by sticking to a few core production factory, with automatic dependencies resolution.
34 basic project productions:
4- library
5- binary
6- documentation
7- tests
8
9* Library:
10 Input:
11 - dependencies (other libraries)
12 - headers: include path & files
13 - sources
14 - generated sources
15 - resources
16 - generated resources
17 Production:
18 - Static library
19 - Dynamic library
20 - Naming rule
21 Life-cycle:
22 - Library compilation
23 - Compilation as a dependencies
24 - Run-time
25 - Packaging
26 Identity:
27 - Name
28 - Version
29* Binary:
30 Input:
31 - dependencies (other libraries)
32 - headers: include path & files (usually empty)
33 - sources
34 - generated sources
35 - resources
36 - generated resources
37 - supported variant (optimized/debug, dll/static...)
38 Production:
39 - Binary executable
40 - Manifest [on some platforms]
41 - Debug symbol [on some platforms]
42 Life-cycle:
43 - Compilation
44 - Run-time
45 - Packaging
46 Identity:
47 - Name
48 - Version
49* Documentation:
50 Input:
51 - dependencies (libraries, binaries)
52 - additional sources
53 - generated sources
54 - resources
55 - generated resources
56 - supported variant (public/internal)
57 Production:
58 - HTML documentation
59 - PDF documentation
60 - CHM documentation
61 Life-cycle:
62 - Documentation
63 - Packaging
64 - Test
65 Identity:
66 - Name
67 - Version
68"""
69
70
71
Christopher Dunne0d72242007-06-14 17:58:59 +000072import os
73import os.path
74import sys
75
Baptiste Lepilleur43e25c32009-11-19 19:03:14 +000076JSONCPP_VERSION = '0.2'
Christopher Dunne0d72242007-06-14 17:58:59 +000077DIST_DIR = '#dist'
78
Christopher Dunn060c45a2009-05-24 22:22:08 +000079options = Variables()
80options.Add( EnumVariable('platform',
Christopher Dunne0d72242007-06-14 17:58:59 +000081 'Platform (compiler/stl) used to build the project',
82 'msvc71',
83 allowed_values='suncc vacpp mingw msvc6 msvc7 msvc71 msvc80 linux-gcc'.split(),
84 ignorecase=2) )
85
86try:
87 platform = ARGUMENTS['platform']
Christopher Dunnf1a49462007-06-14 20:59:51 +000088 if platform == 'linux-gcc':
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +000089 CXX = 'g++' # not quite right, but env is not yet available.
90 import commands
91 version = commands.getoutput('%s -dumpversion' %CXX)
92 platform = 'linux-gcc-%s' %version
93 print "Using platform '%s'" %platform
94 LD_LIBRARY_PATH = os.environ.get('LD_LIBRARY_PATH', '')
95 LD_LIBRARY_PATH = "%s:libs/%s" %(LD_LIBRARY_PATH, platform)
96 os.environ['LD_LIBRARY_PATH'] = LD_LIBRARY_PATH
97 print "LD_LIBRARY_PATH =", LD_LIBRARY_PATH
Christopher Dunne0d72242007-06-14 17:58:59 +000098except KeyError:
99 print 'You must specify a "platform"'
100 sys.exit(2)
101
102print "Building using PLATFORM =", platform
103
104rootbuild_dir = Dir('#buildscons')
105build_dir = os.path.join( '#buildscons', platform )
106bin_dir = os.path.join( '#bin', platform )
107lib_dir = os.path.join( '#libs', platform )
108sconsign_dir_path = Dir(build_dir).abspath
109sconsign_path = os.path.join( sconsign_dir_path, '.sconsign.dbm' )
110
111# Ensure build directory exist (SConsignFile fail otherwise!)
112if not os.path.exists( sconsign_dir_path ):
113 os.makedirs( sconsign_dir_path )
114
115# Store all dependencies signature in a database
116SConsignFile( sconsign_path )
117
Baptiste Lepilleur4e19f182009-11-19 12:07:58 +0000118def make_environ_vars():
119 """Returns a dictionnary with environment variable to use when compiling."""
120 # PATH is required to find the compiler
121 # TEMP is required for at least mingw
122 vars = {}
123 for name in ('PATH', 'TEMP', 'TMP'):
124 if name in os.environ:
125 vars[name] = os.environ[name]
126 return vars
127
128
129env = Environment( ENV = make_environ_vars(),
Christopher Dunne0d72242007-06-14 17:58:59 +0000130 toolpath = ['scons-tools'],
131 tools=[] ) #, tools=['default'] )
132
133if platform == 'suncc':
134 env.Tool( 'sunc++' )
135 env.Tool( 'sunlink' )
136 env.Tool( 'sunar' )
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +0000137 env.Append( CCFLAGS = ['-mt'] )
Christopher Dunne0d72242007-06-14 17:58:59 +0000138elif platform == 'vacpp':
139 env.Tool( 'default' )
140 env.Tool( 'aixcc' )
141 env['CXX'] = 'xlC_r' #scons does not pick-up the correct one !
142 # using xlC_r ensure multi-threading is enabled:
143 # http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.vacpp7a.doc/compiler/ref/cuselect.htm
144 env.Append( CCFLAGS = '-qrtti=all',
145 LINKFLAGS='-bh:5' ) # -bh:5 remove duplicate symbol warning
146elif platform == 'msvc6':
147 env['MSVS_VERSION']='6.0'
148 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
149 env.Tool( tool )
150 env['CXXFLAGS']='-GR -GX /nologo /MT'
151elif platform == 'msvc70':
152 env['MSVS_VERSION']='7.0'
153 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
154 env.Tool( tool )
155 env['CXXFLAGS']='-GR -GX /nologo /MT'
156elif platform == 'msvc71':
157 env['MSVS_VERSION']='7.1'
158 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
159 env.Tool( tool )
160 env['CXXFLAGS']='-GR -GX /nologo /MT'
161elif platform == 'msvc80':
162 env['MSVS_VERSION']='8.0'
163 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
164 env.Tool( tool )
165 env['CXXFLAGS']='-GR -EHsc /nologo /MT'
166elif platform == 'mingw':
167 env.Tool( 'mingw' )
168 env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] )
Christopher Dunnf1a49462007-06-14 20:59:51 +0000169elif platform.startswith('linux-gcc'):
Christopher Dunne0d72242007-06-14 17:58:59 +0000170 env.Tool( 'default' )
171 env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" )
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000172 env['SHARED_LIB_ENABLED'] = True
Christopher Dunne0d72242007-06-14 17:58:59 +0000173else:
174 print "UNSUPPORTED PLATFORM."
175 env.Exit(1)
176
177env.Tool('doxygen')
178env.Tool('substinfile')
179env.Tool('targz')
180env.Tool('srcdist')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000181env.Tool('globtool')
Christopher Dunne0d72242007-06-14 17:58:59 +0000182
183env.Append( CPPPATH = ['#include'],
184 LIBPATH = lib_dir )
185short_platform = platform
186if short_platform.startswith('msvc'):
187 short_platform = short_platform[2:]
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000188# Notes: on Windows you need to rebuild the source for each variant
189# Build script does not support that yet so we only build static libraries.
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000190# This also fails on AIX because both dynamic and static library ends with
191# extension .a.
192env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', False)
Christopher Dunne0d72242007-06-14 17:58:59 +0000193env['LIB_PLATFORM'] = short_platform
194env['LIB_LINK_TYPE'] = 'lib' # static
195env['LIB_CRUNTIME'] = 'mt'
196env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention
197env['JSONCPP_VERSION'] = JSONCPP_VERSION
198env['BUILD_DIR'] = env.Dir(build_dir)
199env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir)
200env['DIST_DIR'] = DIST_DIR
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +0000201if 'TarGz' in env['BUILDERS']:
202 class SrcDistAdder:
203 def __init__( self, env ):
204 self.env = env
205 def __call__( self, *args, **kw ):
206 apply( self.env.SrcDist, (self.env['SRCDIST_TARGET'],) + args, kw )
207 env['SRCDIST_BUILDER'] = env.TarGz
208else: # If tarfile module is missing
209 class SrcDistAdder:
210 def __init__( self, env ):
211 pass
212 def __call__( self, *args, **kw ):
213 pass
Christopher Dunne0d72242007-06-14 17:58:59 +0000214env['SRCDIST_ADD'] = SrcDistAdder( env )
215env['SRCDIST_TARGET'] = os.path.join( DIST_DIR, 'jsoncpp-src-%s.tar.gz' % env['JSONCPP_VERSION'] )
Christopher Dunne0d72242007-06-14 17:58:59 +0000216
Christopher Dunn060c45a2009-05-24 22:22:08 +0000217env_testing = env.Clone( )
Christopher Dunne0d72242007-06-14 17:58:59 +0000218env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] )
219
220def buildJSONExample( env, target_sources, target_name ):
Christopher Dunn060c45a2009-05-24 22:22:08 +0000221 env = env.Clone()
Christopher Dunne0d72242007-06-14 17:58:59 +0000222 env.Append( CPPPATH = ['#'] )
223 exe = env.Program( target=target_name,
224 source=target_sources )
225 env['SRCDIST_ADD']( source=[target_sources] )
226 global bin_dir
227 return env.Install( bin_dir, exe )
228
229def buildJSONTests( env, target_sources, target_name ):
230 jsontests_node = buildJSONExample( env, target_sources, target_name )
231 check_alias_target = env.Alias( 'check', jsontests_node, RunJSONTests( jsontests_node, jsontests_node ) )
232 env.AlwaysBuild( check_alias_target )
233
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000234def buildUnitTests( env, target_sources, target_name ):
235 jsontests_node = buildJSONExample( env, target_sources, target_name )
236 check_alias_target = env.Alias( 'check', jsontests_node,
237 RunUnitTests( jsontests_node, jsontests_node ) )
238 env.AlwaysBuild( check_alias_target )
239
Christopher Dunne0d72242007-06-14 17:58:59 +0000240def buildLibrary( env, target_sources, target_name ):
241 static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
242 source=target_sources )
Christopher Dunne0d72242007-06-14 17:58:59 +0000243 global lib_dir
244 env.Install( lib_dir, static_lib )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000245 if env['SHARED_LIB_ENABLED']:
246 shared_lib = env.SharedLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
247 source=target_sources )
248 env.Install( lib_dir, shared_lib )
Christopher Dunne0d72242007-06-14 17:58:59 +0000249 env['SRCDIST_ADD']( source=[target_sources] )
250
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000251Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests buildUnitTests' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000252
253def buildProjectInDirectory( target_directory ):
254 global build_dir
255 target_build_dir = os.path.join( build_dir, target_directory )
256 target = os.path.join( target_directory, 'sconscript' )
257 SConscript( target, build_dir=target_build_dir, duplicate=0 )
258 env['SRCDIST_ADD']( source=[target] )
259
260
261def runJSONTests_action( target, source = None, env = None ):
262 # Add test scripts to python path
263 jsontest_path = Dir( '#test' ).abspath
264 sys.path.insert( 0, jsontest_path )
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000265 data_path = os.path.join( jsontest_path, 'data' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000266 import runjsontests
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000267 return runjsontests.runAllTests( os.path.abspath(source[0].path), data_path )
Christopher Dunne0d72242007-06-14 17:58:59 +0000268
269def runJSONTests_string( target, source = None, env = None ):
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000270 return 'RunJSONTests("%s")' % source[0]
Christopher Dunne0d72242007-06-14 17:58:59 +0000271
Christopher Dunne0d72242007-06-14 17:58:59 +0000272import SCons.Action
273ActionFactory = SCons.Action.ActionFactory
274RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string )
275
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000276def runUnitTests_action( target, source = None, env = None ):
277 # Add test scripts to python path
278 jsontest_path = Dir( '#test' ).abspath
279 sys.path.insert( 0, jsontest_path )
280 import rununittests
281 return rununittests.runAllTests( os.path.abspath(source[0].path) )
282
283def runUnitTests_string( target, source = None, env = None ):
284 return 'RunUnitTests("%s")' % source[0]
285
286RunUnitTests = ActionFactory(runUnitTests_action, runUnitTests_string )
287
Christopher Dunne0d72242007-06-14 17:58:59 +0000288env.Alias( 'check' )
289
290srcdist_cmd = env['SRCDIST_ADD']( source = """
291 AUTHORS README.txt SConstruct
292 """.split() )
293env.Alias( 'src-dist', srcdist_cmd )
294
295buildProjectInDirectory( 'src/jsontestrunner' )
296buildProjectInDirectory( 'src/lib_json' )
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000297buildProjectInDirectory( 'src/test_lib_json' )
Christopher Dunn060c45a2009-05-24 22:22:08 +0000298buildProjectInDirectory( 'doc' )
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +0000299#print env.Dump()
Christopher Dunnf1a49462007-06-14 20:59:51 +0000300