According to the scons documentation, the subst
method will recursively interpolate construction variables. However, it does not seem to be recursive:
e = Environment(CPPDEFINES = ["FOOBAR=${foobar}"])
e["foo"] = 1
e["bar"] = "${foo + 1}"
e["foobar"] = "$${foo + ${bar}}"
# I want this to print "foobar: 3"
print "foobar:", e.subst("${foobar}")
e.Program("test.c")
Prints:
scons: Reading SConscript files ...
foobar: ${foo + 2}
scons: done reading SConscript files.
scons: Building targets ...
gcc -o test.o -c -DFOOBAR=3 test.c
gcc -o test test.o
scons: done building targets.
foobar
is correctly evaluated during compilation as a part of CPPDEFINES, but not in the print statement. How can I get subst
to fully evaluate foobar?
Using the expression
, as suggested by Kenny Ostrom, doesn't help either. It yields a syntax error because the
subst
method doesn't really handle nested braces too well.The actual question is: Why do we see different outputs when using
subst
in theSConstruct
directly, and when it gets used within a build command?If we add
to the
SConstruct
, we see the same output${foo + 2}
forFOOBAR
. The difference at build time is, that the internal variable$_CPPDEFFLAGS
is declared in terms of the_defines
method:(from a
print e.Dump()
). This_defines
method runs all variables throughsubst_path
a second time, such that one can use variables in include paths, for example.So the
subst
method is doing the right thing, you just have to evaluate again:to get the same output.