williamblevins edited this page Jun 18, 2015 · 2 revisions

Here is a fragment that illustrates how to make Phony targets that run commands. The AlwaysBuild line makes sure the command runs even if there happens to be a file named the same as the target.

def PhonyTarget(target, action):
        phony = Environment(ENV = os.environ,
                            BUILDERS = { 'phony' : Builder(action = action) })
        AlwaysBuild(phony.phony(target = target, source = 'SConstruct'))

PhonyTarget('TAGS', 'tools/ -e')

Here's a better implementation that handles multiple targets and doesn't require generating an Environment every time.

def PhonyTargets(env = None, **kw):
    if not env: env = DefaultEnvironment()
    for target,action in kw.items():
        env.AlwaysBuild(env.Alias(target, [], action))

PhonyTargets(TAGS = 'tools/ -e')

env = Environment(parse_flags = '-std=c89 -DFOO -lm')
PhonyTargets(env, CFLAGS  = '@echo $CFLAGS',
                  DEFINES = '@echo $CPPDEFINES',
                  LIBS    = '@echo $LIBS')

The output looks like this:

$ scons TAGS
tools/ -e
$ scons CFLAGS
$ scons DEFINES
$ scons LIBS
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.