Getting at the List of Build Targets, Regardless of Origin

We've already been introduced to the COMMAND_LINE_TARGETS variable, which contains a list of targets specified on the command line, and the DEFAULT_TARGETS variable, which contains a list of targets specified via calls to the Default method or function. Sometimes, however, you want a list of whatever targets SCons will try to build, regardless of whether the targets came from the command line or a Default call. You could code this up by hand, as follows:

      if COMMAND_LINE_TARGETS:
          targets = COMMAND_LINE_TARGETS
      else:
          targets = DEFAULT_TARGETS
    

SCons, however, provides a convenient BUILD_TARGETS variable that eliminates the need for this by-hand manipulation. Essentially, the BUILD_TARGETS variable contains a list of the command-line targets, if any were specified, and if no command-line targets were specified, it contains a list of the targets specified via the Default method or function.

Because BUILD_TARGETS may contain a list of SCons nodes, you must convert the list elements to strings if you want to print them or look for a specific target name, just like the DEFAULT_TARGETS list:

      prog1 = Program('prog1.c')
      Program('prog2.c')
      Default(prog1)
      print "BUILD_TARGETS is", map(str, BUILD_TARGETS)
    

Notice how the value of BUILD_TARGETS changes depending on whether a target is specified on the command line:

      % scons -Q
      BUILD_TARGETS is ['prog1']
      cc -o prog1.o -c prog1.c
      cc -o prog1 prog1.o
      % scons -Q prog2
      BUILD_TARGETS is ['prog2']
      cc -o prog2.o -c prog2.c
      cc -o prog2 prog2.o
      % scons -Q -c .
      BUILD_TARGETS is ['.']
      Removed prog1.o
      Removed prog1
      Removed prog2.o
      Removed prog2