Generate preprocessed source with scons?
I recently converted from make to SCons. One thing I used to do in make is a recipe for creating a preprocessed source from a source file with all the compiler options that would apply to a normal build. This is useful for determining how headers are included.
What's the best way to do the same in SCons? I can't find a built-in constructor to do this, so I'm stuck writing my own builder?
source to share
I would write a pseudo builder that calls env.Object
with special parameters, for example:
env = Environment()
# Create pseudo-builder and add to enviroment
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. Note how it -DDO_COMPUTE_PI
appears in both normal compilation and compilation -E
:
$ 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
$
source to share
Thanks for the additional information on what you are trying to do. I figured you basically want to invoke the compiler with the "-E" / "/ E" option and save its output in a separate file, right? In this case, the simplest thing to do would be to write a new Builder for this sole purpose. Don't despair, it's not that hard ... SCons has been designed with a lot of flexibility in mind. Take a look at http://scons.org/wiki/ToolsForFools , and if you get stuck, write to the scons-users@scons.org user mailing list ... we'll be happy to help you and provide additional support there.
source to share