9.5. Command-Line variable=value Build Options

You may want to control various aspects of your build by allowing the user to specify variable=value values on the command line. For example, suppose you want users to be able to build a debug version of a program by running SCons as follows:


      % scons -Q debug=1
    

SCons provides an ARGUMENTS dictionary that stores all of the variable=value assignments from the command line. This allows you to modify aspects of your build in response to specifications on the command line. (Note that unless you want to require that users always specify an option, you probably want to use the Python ARGUMENTS.get() function, which allows you to specify a default value to be used if there is no specification on the command line.)

The following code sets the $CCFLAGS construction variable in response to the debug flag being set in the ARGUMENTS dictionary:


       env = Environment()
       debug = ARGUMENTS.get('debug', 0)
       if int(debug):
           env.Append(CCFLAGS = '-g')
       env.Program('prog.c')
    

This results in the -g compiler option being used when debug=1 is used on the command line:


       % scons -Q debug=0
       cc -o prog.o -c prog.c
       cc -o prog prog.o
       % scons -Q debug=0
       scons: `.' is up to date.
       % scons -Q debug=1
       cc -o prog.o -c -g prog.c
       cc -o prog prog.o
       % scons -Q debug=1
       scons: `.' is up to date.
    

Notice that SCons keeps track of the last values used to build the object files, and as a result correctly rebuilds the object and executable files only when the value of the debug argument has changed.