Command Line Options
Suppose, for example, we want to us the Test-Driven-Development (TDD) testing framework such as doctest
. For this, we would like to be able to use two different build configurations:
- Normal (Debug) build
- Test (doctest) build
Setting up CLI choices
There a couple of different approaches, depending on you specifics needs. The two major options are:
- Command Line Variables
- Command Line Flags
Command Line Variables
Suppose we require two build opions:
- A Debug build (default)
- A Test build
First, we to create a Variables
object to hold out options, e.g.
vars = Variables()
We Add
the options we require to the variables object and that object to our environment, e.g.
vars.Add(EnumVariable('BUILD_TYPE', 'type of build to use', 'debug', allowed_values=('debug', 'test')))
env = Environment(variables = vars)
Then, within the SConstruct
file we can test against the supplied CLI variable, e.g. (in this example, the default option has been set to debug
)
if env['BUILD_TYPE'] == 'debug':
print '*** Debug build'
if env['BUILD_TYPE'] == 'tdd':
print '*** TDD build'
env.Append( CPPPATH=["#/doctest/doctest"] )
env.Append( CPPDEFINES = ['TDD'] )
To test, run scons
, i.e.
$ scons
scons: Reading SConscript files ...
*** Debug build
scons: done reading SConscript files.
scons: Building targets ...
...
and to check the Test
option:
$ scons BUILD_TYPE=test
scons: Reading SConscript files ...
*** Test build
scons: done reading SConscript files.
scons: Building targets ...
...
There is also an option to automatically generates the scons
help capability for our project using Help(vars.GenerateHelpText(env))
, e.g.
vars.Add(EnumVariable('BUILD_TYPE', 'type of build to use', 'debug', allowed_values=('debug', 'test')))
env = Environment(variables = vars)
Help(vars.GenerateHelpText(env))
Running scons
with the -h
option invokes the help text:
$ scons -h
scons: Reading SConscript files ...
*** debug build
scons: done reading SConscript files.
BUILD_TYPE: type of build to use (debug|test)
default: debug
actual: debug
Option 2 - Simple flag
Another approach is to use Values of variables passed to the SConscript file(s):
scons debug=1 .
These variables are available in SConscript files through the ARGUMENTS dictionary, and can be used in the SConscript file(s) to modify the build in any way:
if ARGUMENTS.get('debug', 0):
env = Environment(CCFLAGS = '-g')
else:
env = Environment()