4

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?

3
  • Would you be able to elaborate a bit, and give a small example of what you're trying to accomplish? I really don't understand why (for what) you need infos about how headers are included. Just let SCons figure out the implicit dependencies itself...it's really good at that. Aug 8, 2014 at 7:09
  • SCons is good at figuring out dependencies, but that's not quite what I want to know. Sometimes code has a mess of hundreds of header files which include each other multiple times in layers ten deep or so. The meaning of those header files depends on the context where they are included and they only take effect the first time they are included (through #pragma once or #ifndef guards). So I want to know which path the preprocessor is following to first include a particular header file. I could painfully follow every #include path, or just look at the preprocessed output.
    – Tom
    Aug 11, 2014 at 1:20
  • It's also useful where the preprocessor makes an unexpected mess of your source - classically, where a header #defines min or max, then you #include <algorithm>. This usually produces an incomprehensible mash of error messages and the easiest way to figure it out is to look at the preprocessed source.
    – Tom
    Aug 11, 2014 at 1:22

3 Answers 3

4

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
$ 
0

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.

2
  • 1
    In practice I suspect it's easier to just copy the commandline that SCons produces and manually s/.o/.i/ s/-c/-E/.
    – Tom
    Aug 18, 2014 at 10:44
  • 1
    This answer doesn't give any concrete answer, just points to references where the answer can be obtained. Posts should be self-contained: meta.stackexchange.com/questions/18669 Oct 25, 2016 at 23:05
0

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

1
  • Here speaks an angry man, from experience!
    – Tom
    Apr 26, 2023 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.