Tom
Tom

Reputation: 7921

Generate preprocessed source with scons?

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?

Upvotes: 4

Views: 1450

Answers (3)

Robᵩ
Robᵩ

Reputation: 168596

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
$ 

Upvotes: 4

user3696153
user3696153

Reputation: 752

Recognizing that I am 8years late to the OPs original question, and this is not not an answer, but in total agreement with the OP... I have suffered this many many times I too would want this.

This is one of my pet peeves with various IDEs, they can right-click compile a file but cannot right-click pre-process the file.

As others have said - this is super important when unwinding a series of nested macros that have shit all over the code and made the compiler vomit incomprehensible bullshit for error messages. Often the only way is to debug at the pre-processor output level.

I too am comming from a "make world" - I always have encoded a generic wildcard like target, like: "make foo.i" - or "make foo.i" that (A) preprocesses the code and dumps the output in the specified file, ie: foo.c (or foo.cpp) to 'foo.i'

Yea, copy/cut/paste the command line works, but it is not wonderful. I done that enough times that I prefer the 'make foo.i' method. Another thing is sometimes the make script (or in this case, the SCons script) might setup other environment variables that effect things that make it not work the same

Upvotes: 0

dirkbaechle
dirkbaechle

Reputation: 4052

Thanks for the additional info about what you're trying to do. I got that you basically want to call the compiler with the "-E"/"/E" option and store its output in a separate file, correct? In that case the simplest thing to do would indeed be to write a new Builder for this single purpose. Don't despair, it's not that hard...SCons has been designed with great flexibility in mind. Have a look at http://scons.org/wiki/ToolsForFools , and if you get stuck write to the User mailing list [email protected] ... we'll be glad to help you and give further support over there.

Upvotes: 0

Related Questions