ReplacingBuilder¶
Description¶
ReplacingBuilder
is a wrapper for SCons builders, used to call the
wrapped builders with certain construction variables replaced. A mapping of
original variable names into their replacements is provided via constructor
arguments.
obj = ReplacingBuilder(env['BUILDERS']['Object'], CFLAGS='MY_CFLAGS')
env['BUILDERS']['MyObject'] = obj
In the above example, a builder named MyObject
is created which passes the
value of MY_CFLAGS
instead of CFLAGS
to the Object
builder. When
used as follows with gcc
env.Replace(MY_CFLAGS=['-Wall', '-Wextra'])
env.MyObject('test.c')
it will invoke compiler with -Wall
and -Wextra
flags
gcc -c -o test.o -Wall -Wextra test.c
Replacements are applied also to variables passed via builder’s keyword arguments
env.MyObject('test.c', MY_CFLAGS=['-Wall', '-Wextra'])
The ReplacingBuilder
wrapper exposes
inject_replacements()
method which may be used to
set the replacement variables in environment to their default values.
obj = ReplacingBuilder(env['BUILDERS']['Object'], CFLAGS='MY_CFLAGS')
env['BUILDERS']['MyObject'] = obj
obj.inject_replacements(env)
assert env['MY_CFLAGS'] == '$CFLAGS'
Note, that replacements also alter source and target prefixes/suffixes. Let’s
redefine MyObject
builder as follows
obj = ReplacingBuilder(env['BUILDERS']['Object'], OBJSUFFIX='MY_OBJSUFFIX')
env['BUILDERS']['MyObject'] = obj
env['MY_OBJSUFFIX'] = '.my$OBJSUFFIX'
this builder will produce files with suffix '.my.o'
if the original
$OBJSUFFIX
is '.o'
. Note, that when you wrap your own builders,
they should use original variables, like $OBJSUFFIX
for suffixes, not
their replacements.
obj = SCons.Builder.Builder(action=SCons.Defaults.CXXAction,
emitter={},
prefix='$OBJPREFIX', # <- not $MY_OBJPREFIX
suffix='$OBJSUFFIX', # <- not $MY_OBJSUFFIX
src_builder=['MyCXXFile'],
src_suffix='$MY_CXXSUFFIX',
source_scanner=SCons.Tool.SourceFileScanner,
single_source=1)
obj = ReplacingBuilder(obj, OBJPREFIX='MY_OBJPREFIX',
OBJSUFFIX='MY_OBJSUFFIX')
env['BUILDERS']['MyObject'] = obj
# ...
env.SetDefault(MY_OBJSUFFIX='.my$OBJSUFFIX')
If we used suffix='$MY_OBJSUFFIX'
in the above example, variable
substitution would be performed twice, and the actual suffix woule be
'.my.my.o'
instead of '.my.o'
.