Controlling Command-Line Build Options

Being able to use a command-line build option like debug=1 is handy, but it can be a chore to write specific Python code to recognize each such option and apply the values to a construction variable. To help with this, SCons supports a class to define such build options easily, and a mechanism to apply the build options to a construction environment. This allows you to control how the build options affect construction environments.

For example, suppose that you want users to set a RELEASE construction variable on the command line whenever the time comes to build a program for release, and that the value of this variable should be added to the command line with the appropriate -D option (or other command line option) to pass the value to the C compiler. Here's how you might do that by setting the appropriate value in a dictionary for the CPPDEFINES construction variable:

         opts = Options()
         opts.Add('RELEASE', 'Set to 1 to build for release', 0)
         env = Environment(options = opts,
                           CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'})
         env.Program(['foo.c', 'bar.c'])
    

This SConstruct file first creates an Options object (the opts = Options() call), and then uses the object's Add method to indicate that the RELEASE option can be set on the command line, and that it's default value will be 0 (the third argument to the Add method). The second argument is a line of help text; we'll learn how to use it in the next section.

We then pass the created Options object as an options keyword argument to the Environment call used to create the construction environment. This then allows a user to set the RELEASE build option on the command line and have the variable show up in the command line used to build each object from a C source file:

      % scons -Q RELEASE=1
      cc -DRELEASE_BUILD=1 -c -o bar.o bar.c
      cc -DRELEASE_BUILD=1 -c -o foo.o foo.c
      cc -o foo foo.o bar.o