I've recently converted from make to SCons. One thing I'd usually do in make is have a recipe to generate preprocessed source from a source file, with all the compiler options that would be applied to a normal build. This is useful for figuring out how headers are being included.
What is the best way of doing the same thing in SCons? I can't find a built-in builder to do it, so am I stuck writing my own builder?
I would write a pseudo-builder that invokes env.Object
with special parameters, like so:
env = Environment()
# Create pseudo-builder and add to environment
def pre_process(env, source):
env = env.Clone()
env.Replace(OBJSUFFIX = '.E')
env.AppendUnique(CCFLAGS = '-E')
return env.Object(source)
env.AddMethod(pre_process, 'PreProcess')
# Regular build
source = ['a.c', 'b.c']
env.AppendUnique(CPPDEFINES = 'DO_COMPUTE_PI') # for example
main = env.Program('main', source)
env.Alias('build', 'main')
env.Default('build')
# Preprocessor build
env.Alias('preprocess', env.PreProcess(source))
Sample output. Notice how the -DDO_COMPUTE_PI
appears in both the regular compile and the -E
compile:
$ scons -Q
gcc -o a.o -c -DDO_COMPUTE_PI a.c
gcc -o b.o -c -DDO_COMPUTE_PI b.c
gcc -o main a.o b.o
$ scons -Q preprocess
gcc -o a.E -c -E -DDO_COMPUTE_PI a.c
gcc -o b.E -c -E -DDO_COMPUTE_PI b.c
$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With