blob: ae7b7e76c1a3b1e68430dae20e19217a5c1f430d [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
76JSONCPP_VERSION = '0.1'
77DIST_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' )
137 env.Append( LIBS = ['pthreads'] )
138elif 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'
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000151 env['SHARED_LIB_ENABLED'] = False
Christopher Dunne0d72242007-06-14 17:58:59 +0000152elif platform == 'msvc70':
153 env['MSVS_VERSION']='7.0'
154 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
155 env.Tool( tool )
156 env['CXXFLAGS']='-GR -GX /nologo /MT'
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000157 env['SHARED_LIB_ENABLED'] = False
Christopher Dunne0d72242007-06-14 17:58:59 +0000158elif platform == 'msvc71':
159 env['MSVS_VERSION']='7.1'
160 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
161 env.Tool( tool )
162 env['CXXFLAGS']='-GR -GX /nologo /MT'
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000163 env['SHARED_LIB_ENABLED'] = False
Christopher Dunne0d72242007-06-14 17:58:59 +0000164elif platform == 'msvc80':
165 env['MSVS_VERSION']='8.0'
166 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
167 env.Tool( tool )
168 env['CXXFLAGS']='-GR -EHsc /nologo /MT'
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000169 env['SHARED_LIB_ENABLED'] = False
Christopher Dunne0d72242007-06-14 17:58:59 +0000170elif platform == 'mingw':
171 env.Tool( 'mingw' )
172 env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000173 env['SHARED_LIB_ENABLED'] = False
Christopher Dunnf1a49462007-06-14 20:59:51 +0000174elif platform.startswith('linux-gcc'):
Christopher Dunne0d72242007-06-14 17:58:59 +0000175 env.Tool( 'default' )
176 env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" )
177else:
178 print "UNSUPPORTED PLATFORM."
179 env.Exit(1)
180
181env.Tool('doxygen')
182env.Tool('substinfile')
183env.Tool('targz')
184env.Tool('srcdist')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000185env.Tool('globtool')
Christopher Dunne0d72242007-06-14 17:58:59 +0000186
187env.Append( CPPPATH = ['#include'],
188 LIBPATH = lib_dir )
189short_platform = platform
190if short_platform.startswith('msvc'):
191 short_platform = short_platform[2:]
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000192# Notes: on Windows you need to rebuild the source for each variant
193# Build script does not support that yet so we only build static libraries.
194env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', True)
Christopher Dunne0d72242007-06-14 17:58:59 +0000195env['LIB_PLATFORM'] = short_platform
196env['LIB_LINK_TYPE'] = 'lib' # static
197env['LIB_CRUNTIME'] = 'mt'
198env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention
199env['JSONCPP_VERSION'] = JSONCPP_VERSION
200env['BUILD_DIR'] = env.Dir(build_dir)
201env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir)
202env['DIST_DIR'] = DIST_DIR
203class SrcDistAdder:
204 def __init__( self, env ):
205 self.env = env
206 def __call__( self, *args, **kw ):
207 apply( self.env.SrcDist, (self.env['SRCDIST_TARGET'],) + args, kw )
208env['SRCDIST_ADD'] = SrcDistAdder( env )
209env['SRCDIST_TARGET'] = os.path.join( DIST_DIR, 'jsoncpp-src-%s.tar.gz' % env['JSONCPP_VERSION'] )
210env['SRCDIST_BUILDER'] = env.TarGz
211
Christopher Dunn060c45a2009-05-24 22:22:08 +0000212env_testing = env.Clone( )
Christopher Dunne0d72242007-06-14 17:58:59 +0000213env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] )
214
215def buildJSONExample( env, target_sources, target_name ):
Christopher Dunn060c45a2009-05-24 22:22:08 +0000216 env = env.Clone()
Christopher Dunne0d72242007-06-14 17:58:59 +0000217 env.Append( CPPPATH = ['#'] )
218 exe = env.Program( target=target_name,
219 source=target_sources )
220 env['SRCDIST_ADD']( source=[target_sources] )
221 global bin_dir
222 return env.Install( bin_dir, exe )
223
224def buildJSONTests( env, target_sources, target_name ):
225 jsontests_node = buildJSONExample( env, target_sources, target_name )
226 check_alias_target = env.Alias( 'check', jsontests_node, RunJSONTests( jsontests_node, jsontests_node ) )
227 env.AlwaysBuild( check_alias_target )
228
229def buildLibrary( env, target_sources, target_name ):
230 static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
231 source=target_sources )
Christopher Dunne0d72242007-06-14 17:58:59 +0000232 global lib_dir
233 env.Install( lib_dir, static_lib )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000234 if env['SHARED_LIB_ENABLED']:
235 shared_lib = env.SharedLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
236 source=target_sources )
237 env.Install( lib_dir, shared_lib )
Christopher Dunne0d72242007-06-14 17:58:59 +0000238 env['SRCDIST_ADD']( source=[target_sources] )
239
240Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests' )
241
242def buildProjectInDirectory( target_directory ):
243 global build_dir
244 target_build_dir = os.path.join( build_dir, target_directory )
245 target = os.path.join( target_directory, 'sconscript' )
246 SConscript( target, build_dir=target_build_dir, duplicate=0 )
247 env['SRCDIST_ADD']( source=[target] )
248
249
250def runJSONTests_action( target, source = None, env = None ):
251 # Add test scripts to python path
252 jsontest_path = Dir( '#test' ).abspath
253 sys.path.insert( 0, jsontest_path )
254 import runjsontests
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000255 return runjsontests.runAllTests( os.path.abspath(source[0].path), jsontest_path )
Christopher Dunne0d72242007-06-14 17:58:59 +0000256
257def runJSONTests_string( target, source = None, env = None ):
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000258 return 'RunJSONTests("%s")' % source[0]
Christopher Dunne0d72242007-06-14 17:58:59 +0000259
Christopher Dunne0d72242007-06-14 17:58:59 +0000260import SCons.Action
261ActionFactory = SCons.Action.ActionFactory
262RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string )
263
264env.Alias( 'check' )
265
266srcdist_cmd = env['SRCDIST_ADD']( source = """
267 AUTHORS README.txt SConstruct
268 """.split() )
269env.Alias( 'src-dist', srcdist_cmd )
270
271buildProjectInDirectory( 'src/jsontestrunner' )
272buildProjectInDirectory( 'src/lib_json' )
Christopher Dunn060c45a2009-05-24 22:22:08 +0000273buildProjectInDirectory( 'doc' )
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +0000274#print env.Dump()
Christopher Dunnf1a49462007-06-14 20:59:51 +0000275