blob: 0499db9c33f8f3e9210c3b317e1b98fba60d4c42 [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
60 vars = {}
61 for name in ('PATH', 'TEMP', 'TMP'):
62 if name in os.environ:
63 vars[name] = os.environ[name]
64 return vars
65
66
67env = Environment( ENV = make_environ_vars(),
Christopher Dunne0d72242007-06-14 17:58:59 +000068 toolpath = ['scons-tools'],
69 tools=[] ) #, tools=['default'] )
70
71if platform == 'suncc':
72 env.Tool( 'sunc++' )
73 env.Tool( 'sunlink' )
74 env.Tool( 'sunar' )
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +000075 env.Append( CCFLAGS = ['-mt'] )
Christopher Dunne0d72242007-06-14 17:58:59 +000076elif platform == 'vacpp':
77 env.Tool( 'default' )
78 env.Tool( 'aixcc' )
79 env['CXX'] = 'xlC_r' #scons does not pick-up the correct one !
80 # using xlC_r ensure multi-threading is enabled:
81 # http://publib.boulder.ibm.com/infocenter/pseries/index.jsp?topic=/com.ibm.vacpp7a.doc/compiler/ref/cuselect.htm
82 env.Append( CCFLAGS = '-qrtti=all',
83 LINKFLAGS='-bh:5' ) # -bh:5 remove duplicate symbol warning
84elif platform == 'msvc6':
85 env['MSVS_VERSION']='6.0'
86 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
87 env.Tool( tool )
88 env['CXXFLAGS']='-GR -GX /nologo /MT'
89elif platform == 'msvc70':
90 env['MSVS_VERSION']='7.0'
91 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
92 env.Tool( tool )
93 env['CXXFLAGS']='-GR -GX /nologo /MT'
94elif platform == 'msvc71':
95 env['MSVS_VERSION']='7.1'
96 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
97 env.Tool( tool )
98 env['CXXFLAGS']='-GR -GX /nologo /MT'
99elif platform == 'msvc80':
100 env['MSVS_VERSION']='8.0'
101 for tool in ['msvc', 'msvs', 'mslink', 'masm', 'mslib']:
102 env.Tool( tool )
103 env['CXXFLAGS']='-GR -EHsc /nologo /MT'
104elif platform == 'mingw':
105 env.Tool( 'mingw' )
106 env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] )
Christopher Dunnf1a49462007-06-14 20:59:51 +0000107elif platform.startswith('linux-gcc'):
Christopher Dunne0d72242007-06-14 17:58:59 +0000108 env.Tool( 'default' )
109 env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" )
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000110 env['SHARED_LIB_ENABLED'] = True
Christopher Dunne0d72242007-06-14 17:58:59 +0000111else:
112 print "UNSUPPORTED PLATFORM."
113 env.Exit(1)
114
Christopher Dunne0d72242007-06-14 17:58:59 +0000115env.Tool('targz')
116env.Tool('srcdist')
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000117env.Tool('globtool')
Christopher Dunne0d72242007-06-14 17:58:59 +0000118
119env.Append( CPPPATH = ['#include'],
120 LIBPATH = lib_dir )
121short_platform = platform
122if short_platform.startswith('msvc'):
123 short_platform = short_platform[2:]
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000124# Notes: on Windows you need to rebuild the source for each variant
125# Build script does not support that yet so we only build static libraries.
Baptiste Lepilleurbf95d0f2009-11-19 12:19:07 +0000126# This also fails on AIX because both dynamic and static library ends with
127# extension .a.
128env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', False)
Christopher Dunne0d72242007-06-14 17:58:59 +0000129env['LIB_PLATFORM'] = short_platform
130env['LIB_LINK_TYPE'] = 'lib' # static
131env['LIB_CRUNTIME'] = 'mt'
132env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention
133env['JSONCPP_VERSION'] = JSONCPP_VERSION
134env['BUILD_DIR'] = env.Dir(build_dir)
135env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir)
136env['DIST_DIR'] = DIST_DIR
Baptiste Lepilleur86ccb762009-11-19 13:05:54 +0000137if 'TarGz' in env['BUILDERS']:
138 class SrcDistAdder:
139 def __init__( self, env ):
140 self.env = env
141 def __call__( self, *args, **kw ):
142 apply( self.env.SrcDist, (self.env['SRCDIST_TARGET'],) + args, kw )
143 env['SRCDIST_BUILDER'] = env.TarGz
144else: # If tarfile module is missing
145 class SrcDistAdder:
146 def __init__( self, env ):
147 pass
148 def __call__( self, *args, **kw ):
149 pass
Christopher Dunne0d72242007-06-14 17:58:59 +0000150env['SRCDIST_ADD'] = SrcDistAdder( env )
151env['SRCDIST_TARGET'] = os.path.join( DIST_DIR, 'jsoncpp-src-%s.tar.gz' % env['JSONCPP_VERSION'] )
Christopher Dunne0d72242007-06-14 17:58:59 +0000152
Christopher Dunn060c45a2009-05-24 22:22:08 +0000153env_testing = env.Clone( )
Christopher Dunne0d72242007-06-14 17:58:59 +0000154env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] )
155
156def buildJSONExample( env, target_sources, target_name ):
Christopher Dunn060c45a2009-05-24 22:22:08 +0000157 env = env.Clone()
Christopher Dunne0d72242007-06-14 17:58:59 +0000158 env.Append( CPPPATH = ['#'] )
159 exe = env.Program( target=target_name,
160 source=target_sources )
161 env['SRCDIST_ADD']( source=[target_sources] )
162 global bin_dir
163 return env.Install( bin_dir, exe )
164
165def buildJSONTests( env, target_sources, target_name ):
166 jsontests_node = buildJSONExample( env, target_sources, target_name )
167 check_alias_target = env.Alias( 'check', jsontests_node, RunJSONTests( jsontests_node, jsontests_node ) )
168 env.AlwaysBuild( check_alias_target )
169
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000170def buildUnitTests( env, target_sources, target_name ):
171 jsontests_node = buildJSONExample( env, target_sources, target_name )
172 check_alias_target = env.Alias( 'check', jsontests_node,
173 RunUnitTests( jsontests_node, jsontests_node ) )
174 env.AlwaysBuild( check_alias_target )
175
Christopher Dunne0d72242007-06-14 17:58:59 +0000176def buildLibrary( env, target_sources, target_name ):
177 static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
178 source=target_sources )
Christopher Dunne0d72242007-06-14 17:58:59 +0000179 global lib_dir
180 env.Install( lib_dir, static_lib )
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000181 if env['SHARED_LIB_ENABLED']:
182 shared_lib = env.SharedLibrary( target=target_name + '_${LIB_NAME_SUFFIX}',
183 source=target_sources )
184 env.Install( lib_dir, shared_lib )
Christopher Dunne0d72242007-06-14 17:58:59 +0000185 env['SRCDIST_ADD']( source=[target_sources] )
186
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000187Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests buildUnitTests' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000188
189def buildProjectInDirectory( target_directory ):
190 global build_dir
191 target_build_dir = os.path.join( build_dir, target_directory )
192 target = os.path.join( target_directory, 'sconscript' )
193 SConscript( target, build_dir=target_build_dir, duplicate=0 )
194 env['SRCDIST_ADD']( source=[target] )
195
196
197def runJSONTests_action( target, source = None, env = None ):
198 # Add test scripts to python path
199 jsontest_path = Dir( '#test' ).abspath
200 sys.path.insert( 0, jsontest_path )
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000201 data_path = os.path.join( jsontest_path, 'data' )
Christopher Dunne0d72242007-06-14 17:58:59 +0000202 import runjsontests
Baptiste Lepilleur7dec64f2009-11-21 18:20:25 +0000203 return runjsontests.runAllTests( os.path.abspath(source[0].path), data_path )
Christopher Dunne0d72242007-06-14 17:58:59 +0000204
205def runJSONTests_string( target, source = None, env = None ):
Baptiste Lepilleur64e07e52009-11-18 21:27:06 +0000206 return 'RunJSONTests("%s")' % source[0]
Christopher Dunne0d72242007-06-14 17:58:59 +0000207
Christopher Dunne0d72242007-06-14 17:58:59 +0000208import SCons.Action
209ActionFactory = SCons.Action.ActionFactory
210RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string )
211
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000212def runUnitTests_action( target, source = None, env = None ):
213 # Add test scripts to python path
214 jsontest_path = Dir( '#test' ).abspath
215 sys.path.insert( 0, jsontest_path )
216 import rununittests
217 return rununittests.runAllTests( os.path.abspath(source[0].path) )
218
219def runUnitTests_string( target, source = None, env = None ):
220 return 'RunUnitTests("%s")' % source[0]
221
222RunUnitTests = ActionFactory(runUnitTests_action, runUnitTests_string )
223
Christopher Dunne0d72242007-06-14 17:58:59 +0000224env.Alias( 'check' )
225
226srcdist_cmd = env['SRCDIST_ADD']( source = """
227 AUTHORS README.txt SConstruct
228 """.split() )
229env.Alias( 'src-dist', srcdist_cmd )
230
231buildProjectInDirectory( 'src/jsontestrunner' )
232buildProjectInDirectory( 'src/lib_json' )
Baptiste Lepilleur45c499d2009-11-21 18:07:09 +0000233buildProjectInDirectory( 'src/test_lib_json' )
Baptiste Lepilleurf66d3702008-01-20 16:49:53 +0000234#print env.Dump()
Christopher Dunnf1a49462007-06-14 20:59:51 +0000235