This shows how to have multiple sub-projects driven from a single sconstruct.

It assumes a directory structure:

...\Sconstruct                ; main sconstruct file
...\herprogram\sconscript     ; sconscript for sub-project
              \src files      ; the source files for the sub-project
...\hisprogram\sconscript
              \src files
...\myprogram\sconscript
             \src files

...\debug\herprogram          ; the debug build directory for sub-project
...\debug\hisprogram
...\debug\myprogram
...\release\herprogram        ; the release build directory for sub-project
...\release\hisprogram
...\release\myprogram

The command line looks like this:

scons myprogram                ; build project 'myprogram' in release mode
scons hisprogram               ; build project 'hisprogram' in release mode

scons herprogram               ; build project 'herprogram' in release mode
scons mode=debug herprogram    ; build project 'herprogram' in debug mode
scons mode=release herprogram  ; build project 'herprogram' in release mode

SConstruct:

   1 #get the mode flag from the command line
   2 #default to 'release' if the user didn't specify
   3 mymode = ARGUMENTS.get('mode', 'release')   #holds current mode
   4 
   5 #check if the user has been naughty: only 'debug' or 'release' allowed
   6 if not (mymode in ['debug', 'release']):
   7    print "Error: expected 'debug' or 'release', found: " + mymode
   8    Exit(1)
   9 
  10 #tell the user what we're doing
  11 print '**** Compiling in ' + mymode + ' mode...'
  12 
  13 debugcflags = ['-W1', '-GX', '-EHsc', '-D_DEBUG', '/MDd']   #extra compile flags for debug
  14 releasecflags = ['-O2', '-EHsc', '-DNDEBUG', '/MD']         #extra compile flags for release
  15 
  16 env = Environment()
  17 
  18 #make sure the sconscripts can get to the variables
  19 Export('env', 'mymode', 'debugcflags', 'releasecflags')
  20 
  21 #put all .sconsign files in one place
  22 env.SConsignFile()
  23 
  24 #specify the sconscript for myprogram
  25 project = 'myprogram'
  26 SConscript(project + '/sconscript', exports=['project'])
  27 
  28 #specify the sconscript for hisprogram
  29 project = 'hisprogram'
  30 SConscript(project + '/sconscript', exports=['project'])
  31 
  32 #specify the sconscript for herprogram
  33 project = 'herprogram'
  34 SConscript(project + '/sconscript', exports=['project'])

SConscript:

   1 import glob
   2 
   3 #get all the build variables we need
   4 Import('env', 'project', 'mymode', 'debugcflags', 'releasecflags')
   5 localenv = env.Copy()
   6 
   7 buildroot = '../' + mymode  #holds the root of the build directory tree
   8 builddir = buildroot + '/' + project   #holds the build directory for this project
   9 targetpath = builddir + '/' + project  #holds the path to the executable in the build directory
  10 
  11 #append the user's additional compile flags
  12 #assume debugcflags and releasecflags are defined
  13 if mymode == 'debug':
  14    localenv.Append(CCFLAGS=debugcflags)
  15 else:
  16    localenv.Append(CCFLAGS=releasecflags)
  17 
  18 #specify the build directory
  19 localenv.BuildDir(builddir, ".", duplicate=0)
  20 
  21 srclst = map(lambda x: builddir + '/' + x, glob.glob('*.cpp'))
  22 localenv.Program(targetpath, source=srclst)

SconstructMultiple (last edited 2008-03-12 02:47:01 by localhost)