Supporting the SCons Help Command and Quiet Flag

This is the tenth post in my SCons series. The topic of this post is adding support for the silent / quiet flag, and the help command.

In previous episodes I’ve added various enhancements. Some of them print progress information (like processing modules, and two-pass processing). This makes my enhancements quite chatty, which isn’t a problem by itself. The problem is that these messages are printed also in SCons “silent mode” (scons -s), and this episode fixes that.

On the way, I also add a custom help message. Just because it’s fun.

The final result is available on my GitHub scons-series repository.

Making my SCons enhancements silent-mode aware!

As a reminder, the (seemingly silly) C++ project is a simple address book program. Refer to a previous post if you need more details.

Running scons with and without silent mode

Here’s a couple of executions of scons, without silent mode, and with several variants of silent mode:

itamar@legolas sconseries (episodes/09-quietmode) $ scons
scons: Reading SConscript files ...
scons: + Processing flavor debug ...
scons: |- First pass: Reading module AddressBook ...
scons: |- First pass: Reading module Writer ...
scons: |- Second pass: Reading module AddressBook ...
scons: |- Second pass: Reading module Writer ...
scons: + Processing flavor release ...
scons: |- First pass: Reading module AddressBook ...
scons: |- First pass: Reading module Writer ...
scons: |- Second pass: Reading module AddressBook ...
scons: |- Second pass: Reading module Writer ...
scons: done reading SConscript files.
scons: Building targets ...
scons: `.' is up to date.
scons: done building targets.
itamar@legolas sconseries (episodes/09-quietmode) $ scons -s
itamar@legolas sconseries (episodes/09-quietmode) $ scons --silent
itamar@legolas sconseries (episodes/09-quietmode) $ scons --quiet
itamar@legolas sconseries (episodes/09-quietmode) $

Before implementing silent-mode awareness in the episode, the message originating from my enhancements appear also in the silent-mode runs.

Passing all prints through a silent-mode-aware printing function

I wrote the sprint() function, in site_scons/site_utils.py, to disable printing in silent-mode in one place:

def sprint(message, *args):
    """Silent-mode-aware SCons message printer."""
    if not GetOption('silent'):
        if args:
            print 'scons:', message % (args)
        else:
            print 'scons:', message

As you can see, I take advantage of this function to add a “scons:” prefix, and defer string formatting to the actual printing.

I use the GetOption() function to check whether the silent flag was specified.

The site_utils module needs to work also outside of the SCons context (because of flavor terminal integration). Since GetOption is part of SCons, I needed to do the following import-dance in site_utils.py to make it work in both contexts:

try:
    from SCons.Script import GetOption
except ImportError:
    def GetOption(dummy):
        """Stub GetOption if not running in SCons context"""
        return True

Customizing the SCons help

It is sometimes useful to customize the SCons help. One reason is to give help that is specific to this environment. Another reason is to skip processing of all SConscripts when only help is requested.

Here’s my modified SConstruct file:

OSTRICH_SCONS_HELP = """usage: scons [OPTION] [TARGET or FLAVOR_NAME] ...

SCons Options:
  -c, --clean, --remove       Remove specified targets and dependencies.
  -h, --help                  Print this one message and exit.
  -H, --help-options          Print SCons standard help message.
  -j N, --jobs=N              Allow N jobs at once (I recommend 8).
  -n, --no-exec, --just-print, --dry-run, --recon
                              Don't build; just print commands.
  -s, --silent, --quiet       Don't print commands.
  -u, --up, --search-up       Search up directory tree for SConstruct,
                                build targets at or below current directory.
"""

if GetOption('help'):
    # Skip it all if user just wants help
    Help(OSTRICH_SCONS_HELP)
else:
    # Get the base construction environment
    _BASE_ENV = get_base_env()
    # Build every selected flavor
    for flavor in _BASE_ENV.flavors:
        sprint('+ Processing flavor %s ...', flavor)
        flav_bldr = FlavorBuilder(_BASE_ENV, flavor)
        flav_bldr.build()

I didn’t change the default help string much. I made the “FLAVOR_NAME” as a target name explicit, and included info on the most useful command-line flags.

When running scons -h, my custom help message is displayed. The original scons help message is still available by running scons -H.

No Comments Yet.

Leave a Reply