9.2. Getting at Command-Line Targets

SCons supports a COMMAND_LINE_TARGETS variable that lets you get at the list of targets that the user specified on the command line. You can use the targets to manipulate the build in any way you wish. As a simple example, suppose that you want to print a reminder to the user whenever a specific program is built. You can do this by checking for the target in the COMMAND_LINE_TARGETS list:

      if 'bar' in COMMAND_LINE_TARGETS:
          print "Don't forget to copy `bar' to the archive!"

Then, running SCons with the default target works as it always does, but explicity specifying the bar target on the command line generates the warning message:

      % scons -Q
      cc -o foo.o -c foo.c
      cc -o foo foo.o
      % scons -Q bar
      Don't forget to copy `bar' to the archive!
      cc -o bar.o -c bar.c
      cc -o bar bar.o

Another practical use for the COMMAND_LINE_TARGETS variable might be to speed up a build by only reading certain subsidiary SConscript files if a specific target is requested.