user1184162
user1184162

Reputation: 51

Pass an environment variable to a Custom Builder in Scons

I have a custom builder as follows

my_builder = Builder(action = ['mytool' + env['TESTFLAG'] + ' $SOURCE -o $TARGET')],
                     suffix = '',
                     src_suffix = '.cpp',
                     single_source = True)
env.Append(BUILDERS = {'TestBuilder': my_builder})

I would like to pass a different value for TESTFLAG each time TestBuilder is invoked. Setting the variable each time before a call to TestBuilder doesn't work.

Any ideas would be appreciated.

Upvotes: 5

Views: 1683

Answers (1)

Andrew Walker
Andrew Walker

Reputation: 42440

The trick when working with SCons, and in particular when adding new functionality is to remember that it is declarative - you say what you want to happen, and it will work out how (and in what order) it needs to execute actions to make that happen. When you write a Builder, you're specifying actions that will be executed when the tool needs to be run.

The problem comes about because the value of TESTFLAGS isn't known when you create the builder. In effect, you need a mechanism for delaying when a variable will be evaluated. SCons uses variable substitution to achieve this.

In the TestBuilder below, $TESTFLAG, $SOURCE and $TARGET will all be replaced with values when the tool is run.

my_builder = Builder(action = 'mytool $TESTFLAG $SOURCE -o $TARGET',
                     suffix = '',
                     src_suffix = '.cpp',
                     single_source = True)
env['TESTFLAG'] = 'default-value'
env.Append(BUILDERS = {'TestBuilder': my_builder})
env.TestBuilder( 'foo.cpp' )
env.TestBuilder( 'bar.cpp', TESTFLAG = 'overridden-value' )

Upvotes: 3

Related Questions