Baptiste Lepilleur | 32927b0 | 2008-01-21 08:37:06 +0000 | [diff] [blame] | 1 | """ |
Baptiste Lepilleur | 57ee0e3 | 2010-02-22 04:16:10 +0000 | [diff] [blame] | 2 | Notes: |
| 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 Lepilleur | 32927b0 | 2008-01-21 08:37:06 +0000 | [diff] [blame] | 4 | |
Baptiste Lepilleur | 57ee0e3 | 2010-02-22 04:16:10 +0000 | [diff] [blame] | 5 | To 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 Lepilleur | 32927b0 | 2008-01-21 08:37:06 +0000 | [diff] [blame] | 8 | """ |
| 9 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 10 | import os |
| 11 | import os.path |
| 12 | import sys |
| 13 | |
Baptiste Lepilleur | 57ee0e3 | 2010-02-22 04:16:10 +0000 | [diff] [blame] | 14 | JSONCPP_VERSION = open(File('#version').abspath,'rt').read().strip() |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 15 | DIST_DIR = '#dist' |
| 16 | |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 17 | options = Variables() |
| 18 | options.Add( EnumVariable('platform', |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 19 | '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 | |
| 24 | try: |
| 25 | platform = ARGUMENTS['platform'] |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 26 | if platform == 'linux-gcc': |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 27 | 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 36 | except KeyError: |
| 37 | print 'You must specify a "platform"' |
| 38 | sys.exit(2) |
| 39 | |
| 40 | print "Building using PLATFORM =", platform |
| 41 | |
| 42 | rootbuild_dir = Dir('#buildscons') |
| 43 | build_dir = os.path.join( '#buildscons', platform ) |
| 44 | bin_dir = os.path.join( '#bin', platform ) |
| 45 | lib_dir = os.path.join( '#libs', platform ) |
| 46 | sconsign_dir_path = Dir(build_dir).abspath |
| 47 | sconsign_path = os.path.join( sconsign_dir_path, '.sconsign.dbm' ) |
| 48 | |
| 49 | # Ensure build directory exist (SConsignFile fail otherwise!) |
| 50 | if not os.path.exists( sconsign_dir_path ): |
| 51 | os.makedirs( sconsign_dir_path ) |
| 52 | |
| 53 | # Store all dependencies signature in a database |
| 54 | SConsignFile( sconsign_path ) |
| 55 | |
Baptiste Lepilleur | 4e19f18 | 2009-11-19 12:07:58 +0000 | [diff] [blame] | 56 | def 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 | |
| 67 | env = Environment( ENV = make_environ_vars(), |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 68 | toolpath = ['scons-tools'], |
| 69 | tools=[] ) #, tools=['default'] ) |
| 70 | |
| 71 | if platform == 'suncc': |
| 72 | env.Tool( 'sunc++' ) |
| 73 | env.Tool( 'sunlink' ) |
| 74 | env.Tool( 'sunar' ) |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame] | 75 | env.Append( CCFLAGS = ['-mt'] ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 76 | elif 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 |
| 84 | elif 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' |
| 89 | elif 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' |
| 94 | elif 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' |
| 99 | elif 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' |
| 104 | elif platform == 'mingw': |
| 105 | env.Tool( 'mingw' ) |
| 106 | env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] ) |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 107 | elif platform.startswith('linux-gcc'): |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 108 | env.Tool( 'default' ) |
| 109 | env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" ) |
Baptiste Lepilleur | bf95d0f | 2009-11-19 12:19:07 +0000 | [diff] [blame] | 110 | env['SHARED_LIB_ENABLED'] = True |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 111 | else: |
| 112 | print "UNSUPPORTED PLATFORM." |
| 113 | env.Exit(1) |
| 114 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 115 | env.Tool('targz') |
| 116 | env.Tool('srcdist') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 117 | env.Tool('globtool') |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 118 | |
| 119 | env.Append( CPPPATH = ['#include'], |
| 120 | LIBPATH = lib_dir ) |
| 121 | short_platform = platform |
| 122 | if short_platform.startswith('msvc'): |
| 123 | short_platform = short_platform[2:] |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 124 | # 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 Lepilleur | bf95d0f | 2009-11-19 12:19:07 +0000 | [diff] [blame] | 126 | # This also fails on AIX because both dynamic and static library ends with |
| 127 | # extension .a. |
| 128 | env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', False) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 129 | env['LIB_PLATFORM'] = short_platform |
| 130 | env['LIB_LINK_TYPE'] = 'lib' # static |
| 131 | env['LIB_CRUNTIME'] = 'mt' |
| 132 | env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention |
| 133 | env['JSONCPP_VERSION'] = JSONCPP_VERSION |
| 134 | env['BUILD_DIR'] = env.Dir(build_dir) |
| 135 | env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir) |
| 136 | env['DIST_DIR'] = DIST_DIR |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame] | 137 | if '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 |
| 144 | else: # If tarfile module is missing |
| 145 | class SrcDistAdder: |
| 146 | def __init__( self, env ): |
| 147 | pass |
| 148 | def __call__( self, *args, **kw ): |
| 149 | pass |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 150 | env['SRCDIST_ADD'] = SrcDistAdder( env ) |
| 151 | env['SRCDIST_TARGET'] = os.path.join( DIST_DIR, 'jsoncpp-src-%s.tar.gz' % env['JSONCPP_VERSION'] ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 152 | |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 153 | env_testing = env.Clone( ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 154 | env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] ) |
| 155 | |
| 156 | def buildJSONExample( env, target_sources, target_name ): |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 157 | env = env.Clone() |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 158 | 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 | |
| 165 | def 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 Lepilleur | 45c499d | 2009-11-21 18:07:09 +0000 | [diff] [blame] | 170 | def 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 176 | def buildLibrary( env, target_sources, target_name ): |
| 177 | static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}', |
| 178 | source=target_sources ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 179 | global lib_dir |
| 180 | env.Install( lib_dir, static_lib ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 181 | 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 185 | env['SRCDIST_ADD']( source=[target_sources] ) |
| 186 | |
Baptiste Lepilleur | 45c499d | 2009-11-21 18:07:09 +0000 | [diff] [blame] | 187 | Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests buildUnitTests' ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 188 | |
| 189 | def 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 | |
| 197 | def 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 Lepilleur | 7dec64f | 2009-11-21 18:20:25 +0000 | [diff] [blame] | 201 | data_path = os.path.join( jsontest_path, 'data' ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 202 | import runjsontests |
Baptiste Lepilleur | 7dec64f | 2009-11-21 18:20:25 +0000 | [diff] [blame] | 203 | return runjsontests.runAllTests( os.path.abspath(source[0].path), data_path ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 204 | |
| 205 | def runJSONTests_string( target, source = None, env = None ): |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 206 | return 'RunJSONTests("%s")' % source[0] |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 207 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 208 | import SCons.Action |
| 209 | ActionFactory = SCons.Action.ActionFactory |
| 210 | RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string ) |
| 211 | |
Baptiste Lepilleur | 45c499d | 2009-11-21 18:07:09 +0000 | [diff] [blame] | 212 | def 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 | |
| 219 | def runUnitTests_string( target, source = None, env = None ): |
| 220 | return 'RunUnitTests("%s")' % source[0] |
| 221 | |
| 222 | RunUnitTests = ActionFactory(runUnitTests_action, runUnitTests_string ) |
| 223 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 224 | env.Alias( 'check' ) |
| 225 | |
| 226 | srcdist_cmd = env['SRCDIST_ADD']( source = """ |
| 227 | AUTHORS README.txt SConstruct |
| 228 | """.split() ) |
| 229 | env.Alias( 'src-dist', srcdist_cmd ) |
| 230 | |
| 231 | buildProjectInDirectory( 'src/jsontestrunner' ) |
| 232 | buildProjectInDirectory( 'src/lib_json' ) |
Baptiste Lepilleur | 45c499d | 2009-11-21 18:07:09 +0000 | [diff] [blame] | 233 | buildProjectInDirectory( 'src/test_lib_json' ) |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 234 | #print env.Dump() |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 235 | |