Reading Build Options From a File

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 to read build option values from a file. This allows you to control how the build options affect construction environments. The way you do this is by specifying a file name when you call Options, like custom.py in the following example:

         opts = Options('custom.py')
         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'])
         Help(opts.GenerateHelpText(env))
    

This then allows us to control the RELEASE variable by setting it in the custom.py file:

      RELEASE = 1
      

Note that this file is actually executed like a Python script. Now when we run SCons:

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

And if we change the contents of custom.py to:

      RELEASE = 0
    

The object files are rebuilt appropriately with the new option:

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