Chapter 12. Controlling a Build From the Command Line

SCons provides a number of ways
for the writer of the SConscript files
to give the users who will run SCons
a great deal of control over the build execution.
The arguments that the user can specify on
the command line are broken down into three types:

Options

Command-line options always begin with
one or two - (hyphen) characters.
SCons provides ways for you to examind
and set options values from within your SConscript files,
as well as the ability to define your own
custom options.
See Section 12.1, below.

Variables

Any command-line argument containing an =
(equal sign) is considered a variable setting with the form
variable=valueSCons provides direct access to
all of the command-line variable settings,
the ability to apply command-line variable settings
to construction environments,
and functions for configuring
specific types of variables
(Boolean values, path names, etc.)
with automatic validation of the user's specified values.
See Section 12.2, below.

Targets

Any command-line argument that is not an option
or a variable setting
(does not begin with a hyphen
and does not contain an equal sign)
is considered a target that the user
(presumably) wants SCons to build.
A list of Node objects representing
the target or targets to build.
SCons provides access to the list of specified targets,
as well as ways to set the default list of targets
from within the SConscript files.
See Section 12.3, below.

Users may find themselves supplying
the same command-line options every time
they run SCons.
For example, you might find it saves time
to specify a value of -j 2
to have SCons run up to two build commands in parallel.
To avoid having to type -j 2 by hand
every time,
you can set the external environment variable
SCONSFLAGS to a string containing
command-line options that you want SCons to use.

If, for example,
you're using a POSIX shell that's
compatible with the Bourne shell,
and you always want SCons to use the
-Q option,
you can set the SCONSFLAGS
environment as follows:

SCons provides the GetOption function
to get the values set by the various command-line options.
One common use of this is to check whether or not
the -h or --help option
has been specified.
Normally, SCons does not print its help text
until after it has read all of the SConscript files,
because it's possible that help text has been added
by some subsidiary SConscript file deep in the
source tree hierarchy.
Of course, reading all of the SConscript files
takes extra time.

If you know that your configuration does not define
any additional help text in subsidiary SConscript files,
you can speed up the command-line help available to users
by using the GetOption function to load the
subsidiary SConscript files only if the
the user has not specified
the -h or --help option,
like so:

In general, the string that you pass to the
GetOption function to fetch the value of a command-line
option setting is the same as the "most common" long option name
(beginning with two hyphen characters),
although there are some exceptions.
The list of SCons command-line options
and the GetOption strings for fetching them,
are available in the
Section 12.1.4 section,
below.

You can also set the values of SCons
command-line options from within the SConscript files
by using the SetOption function.
The strings that you use to set the values of SCons
command-line options are available in the
Section 12.1.4 section,
below.

One use of the SetOption function is to
specify a value for the -j
or --jobs option,
so that users get the improved performance
of a parallel build without having to specify the option by hand.
A complicating factor is that a good value
for the -j option is
somewhat system-dependent.
One rough guideline is that the more processors
your system has,
the higher you want to set the
-j value,
in order to take advantage of the number of CPUs.

For example, suppose the administrators
of your development systems
have standardized on setting a
NUM_CPU environment variable
to the number of processors on each system.
A little bit of Python code
to access the environment variable
and the SetOption function
provide the right level of flexibility:

The above snippet of code
sets the value of the --jobs option
to the value specified in the
$NUM_CPU environment variable.
(This is one of the exception cases
where the string is spelled differently from
the from command-line option.
The string for fetching or setting the --jobs
value is num_jobs
for historical reasons.)
The code in this example prints the num_jobs
value for illustrative purposes.
It uses a default value of 2
to provide some minimal parallelism even on
single-processor systems:

% scons -Q
running with -j 2
scons: `.' is up to date.

But if the $NUM_CPU
environment variable is set,
then we use that for the default number of jobs:

The strings that you can pass to the GetOption
and SetOption functions usually correspond to the
first long-form option name
(beginning with two hyphen characters: --),
after replacing any remaining hyphen characters
with underscores.

The full list of strings and the variables they
correspond to is as follows:

SCons also allows you to define your own
command-line options with the AddOption function.
The AddOption function takes the same arguments
as the optparse.add_option function
from the standard Python library.
[1]
Once you have added a custom command-line option
with the AddOption function,
the value of the option (if any) is immediately available
using the standard GetOption function.
(The value can also be set using SetOption,
although that's not very useful in practice
because a default value can be specified in
directly in the AddOption call.)

One useful example of using this functionality
is to provide a --prefix for users:

The above code uses the GetOption function
to set the $PREFIX
construction variable to any
value that the user specifies with a command-line
option of --prefix.
Because $PREFIX
will expand to a null string if it's not initialized,
running SCons without the
option of --prefix
will install the file in the
/usr/bin/ directory:

% scons -Q -n
Install file: "foo.in" as "/usr/bin/foo.in"

But specifying --prefix=/tmp/install
on the command line causes the file to be installed in the
/tmp/install/usr/bin/ directory: