Baptiste Lepilleur | 32927b0 | 2008-01-21 08:37:06 +0000 | [diff] [blame] | 1 | """ |
| 2 | Build system can be clean-up by sticking to a few core production factory, with automatic dependencies resolution. |
| 3 | 4 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 72 | import os |
| 73 | import os.path |
| 74 | import sys |
| 75 | |
| 76 | JSONCPP_VERSION = '0.1' |
| 77 | DIST_DIR = '#dist' |
| 78 | |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 79 | options = Variables() |
| 80 | options.Add( EnumVariable('platform', |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 81 | '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 | |
| 86 | try: |
| 87 | platform = ARGUMENTS['platform'] |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 88 | if platform == 'linux-gcc': |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 89 | 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 Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 98 | except KeyError: |
| 99 | print 'You must specify a "platform"' |
| 100 | sys.exit(2) |
| 101 | |
| 102 | print "Building using PLATFORM =", platform |
| 103 | |
| 104 | rootbuild_dir = Dir('#buildscons') |
| 105 | build_dir = os.path.join( '#buildscons', platform ) |
| 106 | bin_dir = os.path.join( '#bin', platform ) |
| 107 | lib_dir = os.path.join( '#libs', platform ) |
| 108 | sconsign_dir_path = Dir(build_dir).abspath |
| 109 | sconsign_path = os.path.join( sconsign_dir_path, '.sconsign.dbm' ) |
| 110 | |
| 111 | # Ensure build directory exist (SConsignFile fail otherwise!) |
| 112 | if not os.path.exists( sconsign_dir_path ): |
| 113 | os.makedirs( sconsign_dir_path ) |
| 114 | |
| 115 | # Store all dependencies signature in a database |
| 116 | SConsignFile( sconsign_path ) |
| 117 | |
Baptiste Lepilleur | 4e19f18 | 2009-11-19 12:07:58 +0000 | [diff] [blame] | 118 | def 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 | |
| 129 | env = Environment( ENV = make_environ_vars(), |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 130 | toolpath = ['scons-tools'], |
| 131 | tools=[] ) #, tools=['default'] ) |
| 132 | |
| 133 | if platform == 'suncc': |
| 134 | env.Tool( 'sunc++' ) |
| 135 | env.Tool( 'sunlink' ) |
| 136 | env.Tool( 'sunar' ) |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame^] | 137 | env.Append( CCFLAGS = ['-mt'] ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 138 | elif 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 |
| 146 | elif 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' |
| 151 | elif 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' |
| 156 | elif 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' |
| 161 | elif 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' |
| 166 | elif platform == 'mingw': |
| 167 | env.Tool( 'mingw' ) |
| 168 | env.Append( CPPDEFINES=[ "WIN32", "NDEBUG", "_MT" ] ) |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 169 | elif platform.startswith('linux-gcc'): |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 170 | env.Tool( 'default' ) |
| 171 | env.Append( LIBS = ['pthread'], CCFLAGS = "-Wall" ) |
Baptiste Lepilleur | bf95d0f | 2009-11-19 12:19:07 +0000 | [diff] [blame] | 172 | env['SHARED_LIB_ENABLED'] = True |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 173 | else: |
| 174 | print "UNSUPPORTED PLATFORM." |
| 175 | env.Exit(1) |
| 176 | |
| 177 | env.Tool('doxygen') |
| 178 | env.Tool('substinfile') |
| 179 | env.Tool('targz') |
| 180 | env.Tool('srcdist') |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 181 | env.Tool('globtool') |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 182 | |
| 183 | env.Append( CPPPATH = ['#include'], |
| 184 | LIBPATH = lib_dir ) |
| 185 | short_platform = platform |
| 186 | if short_platform.startswith('msvc'): |
| 187 | short_platform = short_platform[2:] |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 188 | # 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 Lepilleur | bf95d0f | 2009-11-19 12:19:07 +0000 | [diff] [blame] | 190 | # This also fails on AIX because both dynamic and static library ends with |
| 191 | # extension .a. |
| 192 | env['SHARED_LIB_ENABLED'] = env.get('SHARED_LIB_ENABLED', False) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 193 | env['LIB_PLATFORM'] = short_platform |
| 194 | env['LIB_LINK_TYPE'] = 'lib' # static |
| 195 | env['LIB_CRUNTIME'] = 'mt' |
| 196 | env['LIB_NAME_SUFFIX'] = '${LIB_PLATFORM}_${LIB_LINK_TYPE}${LIB_CRUNTIME}' # must match autolink naming convention |
| 197 | env['JSONCPP_VERSION'] = JSONCPP_VERSION |
| 198 | env['BUILD_DIR'] = env.Dir(build_dir) |
| 199 | env['ROOTBUILD_DIR'] = env.Dir(rootbuild_dir) |
| 200 | env['DIST_DIR'] = DIST_DIR |
Baptiste Lepilleur | 86ccb76 | 2009-11-19 13:05:54 +0000 | [diff] [blame^] | 201 | if '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 |
| 208 | else: # If tarfile module is missing |
| 209 | class SrcDistAdder: |
| 210 | def __init__( self, env ): |
| 211 | pass |
| 212 | def __call__( self, *args, **kw ): |
| 213 | pass |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 214 | env['SRCDIST_ADD'] = SrcDistAdder( env ) |
| 215 | 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] | 216 | |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 217 | env_testing = env.Clone( ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 218 | env_testing.Append( LIBS = ['json_${LIB_NAME_SUFFIX}'] ) |
| 219 | |
| 220 | def buildJSONExample( env, target_sources, target_name ): |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 221 | env = env.Clone() |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 222 | 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 | |
| 229 | def 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 | |
| 234 | def buildLibrary( env, target_sources, target_name ): |
| 235 | static_lib = env.StaticLibrary( target=target_name + '_${LIB_NAME_SUFFIX}', |
| 236 | source=target_sources ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 237 | global lib_dir |
| 238 | env.Install( lib_dir, static_lib ) |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 239 | if env['SHARED_LIB_ENABLED']: |
| 240 | shared_lib = env.SharedLibrary( target=target_name + '_${LIB_NAME_SUFFIX}', |
| 241 | source=target_sources ) |
| 242 | env.Install( lib_dir, shared_lib ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 243 | env['SRCDIST_ADD']( source=[target_sources] ) |
| 244 | |
| 245 | Export( 'env env_testing buildJSONExample buildLibrary buildJSONTests' ) |
| 246 | |
| 247 | def buildProjectInDirectory( target_directory ): |
| 248 | global build_dir |
| 249 | target_build_dir = os.path.join( build_dir, target_directory ) |
| 250 | target = os.path.join( target_directory, 'sconscript' ) |
| 251 | SConscript( target, build_dir=target_build_dir, duplicate=0 ) |
| 252 | env['SRCDIST_ADD']( source=[target] ) |
| 253 | |
| 254 | |
| 255 | def runJSONTests_action( target, source = None, env = None ): |
| 256 | # Add test scripts to python path |
| 257 | jsontest_path = Dir( '#test' ).abspath |
| 258 | sys.path.insert( 0, jsontest_path ) |
| 259 | import runjsontests |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 260 | return runjsontests.runAllTests( os.path.abspath(source[0].path), jsontest_path ) |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 261 | |
| 262 | def runJSONTests_string( target, source = None, env = None ): |
Baptiste Lepilleur | 64e07e5 | 2009-11-18 21:27:06 +0000 | [diff] [blame] | 263 | return 'RunJSONTests("%s")' % source[0] |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 264 | |
Christopher Dunn | e0d7224 | 2007-06-14 17:58:59 +0000 | [diff] [blame] | 265 | import SCons.Action |
| 266 | ActionFactory = SCons.Action.ActionFactory |
| 267 | RunJSONTests = ActionFactory(runJSONTests_action, runJSONTests_string ) |
| 268 | |
| 269 | env.Alias( 'check' ) |
| 270 | |
| 271 | srcdist_cmd = env['SRCDIST_ADD']( source = """ |
| 272 | AUTHORS README.txt SConstruct |
| 273 | """.split() ) |
| 274 | env.Alias( 'src-dist', srcdist_cmd ) |
| 275 | |
| 276 | buildProjectInDirectory( 'src/jsontestrunner' ) |
| 277 | buildProjectInDirectory( 'src/lib_json' ) |
Christopher Dunn | 060c45a | 2009-05-24 22:22:08 +0000 | [diff] [blame] | 278 | buildProjectInDirectory( 'doc' ) |
Baptiste Lepilleur | f66d370 | 2008-01-20 16:49:53 +0000 | [diff] [blame] | 279 | #print env.Dump() |
Christopher Dunn | f1a4946 | 2007-06-14 20:59:51 +0000 | [diff] [blame] | 280 | |