[Scons-users] How to build variants from generated source files

Petteri Hintsanen petterih at iki.fi
Thu Oct 6 09:32:35 EDT 2016


Hello all,

I'm stuck with an embarrassingly simple problem with variant builds.

Greatly simplified, I have a code generator tool in a sub-directory.  It
reads some input files and generates C++ source files from the input.
The generator uses hard-coded file names for its inputs, and it writes
files into its own sub-directory, again using hard-coded file names.  I
cannot easily change these.

>From the build's standpoint, the generator should be run whenever its
inputs are modified.  After that the generated sources should be
recompiled and the object files wrapped into a static library.  The
object files and the library should go to a separate variant build
directory.  Visually it should look like this:

./
| SConstruct
|- build/                  <-- variant dir
   |- generated.o
   |- libmylib.a
|- generator/
   |- SConscript
   |- generator.sh         <-- generator tool
   |- hello.txt            <-- generator input
   |- generated.h          <-- generator output
   |- generated.cpp        <-- generator output

The problem is that generated.o is compiled into the source directory,
that is, generator/generated.o instead of build/generated.o

How could I get the object file into the build directory?  This is
important because I have multiple variants, each on its own build
sub-tree.

My sconscripts look like this:

---------------------------

./SConstruct:

env = Environment()
Export("env")
env.SConscript("generator/SConscript", variant_dir = "build", duplicate = 0)

./generator/SConscript:

Import('*')

generator_input = Split("""
hello.txt
""")

generator_output = Split("""
#generator/generated.h
#generator/generated.cpp
""")

env.Command(target = generator_output,
            source = generator_input,
            action = "generator/generator.sh")
lib = env.Library("mylib", ["#generator/generated.cpp"])

----------------------------

The only workaround I have been able to figure out is to use

obj = env.StaticObject("generated.o", "#generator/generator.cpp")
lib = env.Library("mylib", obj)

but this gets cumbersome with multiple source files.

I'm afraid I'm lacking some fundamental understanding here.  Any advice
is greatly appreciated!

Thanks,
Petteri


More information about the Scons-users mailing list