blob: d652887e365b2b62817da53039f1feccf593ee87 [file] [log] [blame]
Baptiste Lepilleur32927b02008-01-21 08:37:06 +00001"""
Baptiste Lepilleur57ee0e32010-02-22 04:16:10 +00002Notes:
3- shared library support is buggy: it assumes that a static and dynamic library can be build from the same object files. This is not true on many platforms. For this reason it is only enabled on linux-gcc at the current time.
Baptiste Lepilleur32927b02008-01-21 08:37:06 +00004
Baptiste Lepilleur57ee0e32010-02-22 04:16:10 +00005To add a platform:
6- add its name in options allowed_values below
7- add tool initialization for this platform. Search for "if platform == 'suncc'" as an example.
Baptiste Lepilleur32927b02008-01-21 08:37:06 +00008"""
9
Christopher Dunne0d72242007-06-14 17:58:59 +000010import os
11import os.path
12import sys
13
Baptiste Lepilleur57ee0e32010-02-22 04:16:10 +000014JSONCPP_VERSION = open(File('#version').abspath,'rt').read().strip()
Christopher Dunne0d72242007-06-14 17:58:59 +000015DIST_DIR = '#dist'
16
Christopher Dunn060c45a2009-05-24 22:22:08 +000017options = Variables()
18options.Add( EnumVariable('platform',
Christopher Dunne0d72242007-06-14 17:58:59 +000019 'Platform (compiler/stl) used to build the project',
20 'msvc71',
21 allowed_values='suncc vacpp mingw msvc6 msvc7 msvc71 msvc80 linux-gcc'.split(),
22 ignorecase=2) )
23
24try:
25 platform = ARGUMENTS['platform']
Christopher Dunnf1a49462007-06-14 20:59:51 +000026 if platform == 'linux-gcc':
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +000027 CXX = 'g++' # not quite right, but env is not yet available.
28 import commands
29 version = commands.getoutput('%s -dumpversion' %CXX)
30 platform = 'linux-gcc-%s' %version
31 print "Using platform '%s'" %platform
32 LD_LIBRARY_PATH = os.environ.get('LD_LIBRARY_PATH', '')
33 LD_LIBRARY_PATH = "%s:libs/%s" %(LD_LIBRARY_PATH, platform)
34 os.environ['LD_LIBRARY_PATH'] = LD_LIBRARY_PATH
35 print "LD_LIBRARY_PATH =", LD_LIBRARY_PATH
Christopher Dunne0d72242007-06-14 17:58:59 +000036except KeyError:
37 print 'You must specify a "platform"'
38 sys.exit(2)
39
40print "Building using PLATFORM =", platform
41
42rootbuild_dir = Dir('#buildscons')
43build_dir = os.path.join( '#buildscons', platform )
44bin_dir = os.path.join( '#bin', platform )
45lib_dir = os.path.join( '#libs', platform )
46sconsign_dir_path = Dir(build_dir).abspath
47sconsign_path = os.path.join( sconsign_dir_path, '.sconsign.dbm' )
48
49# Ensure build directory exist (SConsignFile fail otherwise!)
50if not os.path.exists( sconsign_dir_path ):
51 os.makedirs( sconsign_dir_path )
52
53# Store all dependencies signature in a database
54SConsignFile( sconsign_path )
55
Baptiste Lepilleur4e19f182009-11-19 12:07:58 +000056def make_environ_vars():
57 """Returns a dictionnary with environment variable to use when compiling."""
58 # PATH is required to find the compiler
59 # TEMP is required for at least mingw
Baptiste Lepilleur0a899582010-03-13 07:55:46 +000060 # LD_LIBRARY_PATH & co is required on some system for the compiler
Baptiste Lepilleur4e19f182009-11-19 12:07:58 +000061 vars = {}
Baptiste Lepilleur0a899582010-03-13 07:55:46 +000062 for name in ('PATH', 'TEMP', 'TMP', 'LD_LIBRARY_PATH', 'LIBRARY_PATH'):
Baptiste Lepilleur4e19f182009-11-19 12:07:58 +000063 if name in os.environ:
64 vars[name] = os.environ[name]
65 return vars
66
67
68env = Environment( ENV = make_environ_vars(),
Christopher Dunne0d72242007-06-14 17:58:59 +000069 toolpath = ['scons-tools'],
70 tools=[] ) #, tools=['default'] )
71
72if platform == 'suncc':
73 env.Tool( 'sunc++' )
74 env.Tool( 'sunlink' )
75 env.Tool( 'sunar' )
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000076 env.Append( CCFLAGS = ['-mt'] )
Christopher Dunne0d72242007-06-14 17:58:59 +000077elif platform == 'vacpp':
78 env.Tool( 'default' )
79 env.Tool( 'aixcc' )
80 env['CXX'] = 'xlC_r' #scons does not pick-up the correct one !
81 # using xlC_r ensure multi-threading is enabled:
82 # http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.vacpp7a.doc/compiler/ref/cuselect.htm
83 env.Append( CCFLAGS = '-qrtti=all',
84 LINKFLAGS='-bh:5' ) # -bh:5 remove duplicate symbol warning
85elif platform == 'msvc6':
86 env['MSVS_VERSION']='6.0'
87 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
88 env.Tool( tool )
89 env['CXXFLAGS']='-GR -GX /nologo /MT'
90elif platform == 'msvc70':
91 env['MSVS_VERSION']='7.0'
92 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
93 env.Tool( tool )
94 env['CXXFLAGS']='-GR -GX /nologo /MT'
95elif platform == 'msvc71':
96 env['MSVS_VERSION']='7.1'
97 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
98 env.Tool( tool )
99 env['CXXFLAGS']='-GR -GX /nologo /MT'
100elif platform == 'msvc80':
101 env['MSVS_VERSION']='8.0'
102 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
103 env.Tool( tool )
104 env['CXXFLAGS']='-GR -EHsc /nologo /MT'
105elif platform == 'mingw':
106 env.Tool( 'mingw' )
107 env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] )
Christopher Dunnf1a49462007-06-14 20:59:51 +0000108elif platform.startswith('linux-gcc'):
Christopher Dunne0d72242007-06-14 17:58:59 +0000109 env.Tool( 'default' )
110 env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" )
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000111 env['SHARED_LIB_ENABLED'] = True
Christopher Dunne0d72242007-06-14 17:58:59 +0000112else:
113 print "UNSUPPORTED PLATFORM."
114 env.Exit(1)
115
Christopher Dunne0d72242007-06-14 17:58:59 +0000116env.Tool('targz')
117env.Tool('srcdist')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000118env.Tool('globtool')
Christopher Dunne0d72242007-06-14 17:58:59 +0000119
120env.Append( CPPPATH = ['#include'],
121 LIBPATH = lib_dir )
122short_platform = platform
123if short_platform.startswith('msvc'):
124 short_platform = short_platform[2:]
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000125# Notes: on Windows you need to rebuild the source for each variant
126# Build script does not support that yet so we only build static libraries.
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000127# This also fails on AIX because both dynamic and static library ends with
128# extension .a.
129env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', False)
Christopher Dunne0d72242007-06-14 17:58:59 +0000130env['LIB_PLATFORM'] = short_platform
131env['LIB_LINK_TYPE'] = 'lib' # static
132env['LIB_CRUNTIME'] = 'mt'
133env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention
134env['JSONCPP_VERSION'] = JSONCPP_VERSION
135env['BUILD_DIR'] = env.Dir(build_dir)
136env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir)
137env['DIST_DIR'] = DIST_DIR
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +0000138if 'TarGz' in env['BUILDERS']:
139 class SrcDistAdder:
140 def __init__( self, env ):
141 self.env = env
142 def __call__( self, *args, **kw ):
143 apply( self.env.SrcDist, (self.env['SRCDIST_TARGET'],) + args, kw )
144 env['SRCDIST_BUILDER'] = env.TarGz
145else: # If tarfile module is missing
146 class SrcDistAdder:
147 def __init__( self, env ):
148 pass
149 def __call__( self, *args, **kw ):
150 pass
Christopher Dunne0d72242007-06-14 17:58:59 +0000151env['SRCDIST_ADD'] = SrcDistAdder( env )
152env['SRCDIST_TARGET'] = os.path.join( DIST_DIR, 'jsoncpp-src-%s.tar.gz' % env['JSONCPP_VERSION'] )
Christopher Dunne0d72242007-06-14 17:58:59 +0000153
Christopher Dunn060c45a2009-05-24 22:22:08 +0000154env_testing = env.Clone( )
Christopher Dunne0d72242007-06-14 17:58:59 +0000155env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] )
156
157def buildJSONExample( env, target_sources, target_name ):
Christopher Dunn060c45a2009-05-24 22:22:08 +0000158 env = env.Clone()
Christopher Dunne0d72242007-06-14 17:58:59 +0000159 env.Append( CPPPATH = ['#'] )
160 exe = env.Program( target=target_name,
161 source=target_sources )
162 env['SRCDIST_ADD']( source=[target_sources] )
163 global bin_dir
164 return env.Install( bin_dir, exe )
165
166def buildJSONTests( env, target_sources, target_name ):
167 jsontests_node = buildJSONExample( env, target_sources, target_name )
168 check_alias_target = env.Alias( 'check', jsontests_node, RunJSONTests( jsontests_node, jsontests_node ) )
169 env.AlwaysBuild( check_alias_target )
170
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000171def buildUnitTests( env, target_sources, target_name ):
172 jsontests_node = buildJSONExample( env, target_sources, target_name )
173 check_alias_target = env.Alias( 'check', jsontests_node,
174 RunUnitTests( jsontests_node, jsontests_node ) )
175 env.AlwaysBuild( check_alias_target )
176
Christopher Dunne0d72242007-06-14 17:58:59 +0000177def buildLibrary( env, target_sources, target_name ):
178 static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
179 source=target_sources )
Christopher Dunne0d72242007-06-14 17:58:59 +0000180 global lib_dir
181 env.Install( lib_dir, static_lib )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000182 if env['SHARED_LIB_ENABLED']:
183 shared_lib = env.SharedLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
184 source=target_sources )
185 env.Install( lib_dir, shared_lib )
Christopher Dunne0d72242007-06-14 17:58:59 +0000186 env['SRCDIST_ADD']( source=[target_sources] )
187
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000188Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests buildUnitTests' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000189
190def buildProjectInDirectory( target_directory ):
191 global build_dir
192 target_build_dir = os.path.join( build_dir, target_directory )
193 target = os.path.join( target_directory, 'sconscript' )
194 SConscript( target, build_dir=target_build_dir, duplicate=0 )
195 env['SRCDIST_ADD']( source=[target] )
196
197
198def runJSONTests_action( target, source = None, env = None ):
199 # Add test scripts to python path
200 jsontest_path = Dir( '#test' ).abspath
201 sys.path.insert( 0, jsontest_path )
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000202 data_path = os.path.join( jsontest_path, 'data' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000203 import runjsontests
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000204 return runjsontests.runAllTests( os.path.abspath(source[0].path), data_path )
Christopher Dunne0d72242007-06-14 17:58:59 +0000205
206def runJSONTests_string( target, source = None, env = None ):
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000207 return 'RunJSONTests("%s")' % source[0]
Christopher Dunne0d72242007-06-14 17:58:59 +0000208
Christopher Dunne0d72242007-06-14 17:58:59 +0000209import SCons.Action
210ActionFactory = SCons.Action.ActionFactory
211RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string )
212
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000213def runUnitTests_action( target, source = None, env = None ):
214 # Add test scripts to python path
215 jsontest_path = Dir( '#test' ).abspath
216 sys.path.insert( 0, jsontest_path )
217 import rununittests
218 return rununittests.runAllTests( os.path.abspath(source[0].path) )
219
220def runUnitTests_string( target, source = None, env = None ):
221 return 'RunUnitTests("%s")' % source[0]
222
223RunUnitTests = ActionFactory(runUnitTests_action, runUnitTests_string )
224
Christopher Dunne0d72242007-06-14 17:58:59 +0000225env.Alias( 'check' )
226
227srcdist_cmd = env['SRCDIST_ADD']( source = """
228 AUTHORS README.txt SConstruct
229 """.split() )
230env.Alias( 'src-dist', srcdist_cmd )
231
232buildProjectInDirectory( 'src/jsontestrunner' )
233buildProjectInDirectory( 'src/lib_json' )
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000234buildProjectInDirectory( 'src/test_lib_json' )
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +0000235#print env.Dump()
Christopher Dunnf1a49462007-06-14 20:59:51 +0000236