Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate preprocessed source with scons?

Tags:

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?

like image 755
Tom Avatar asked Sep 02 '25 04:09

Tom


1 Answers

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
$ 
like image 188
Robᵩ Avatar answered Sep 05 '25 00:09

Robᵩ