Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Scons to compile same objects in different environments with Glob?

Tags:

glob

scons

I have a C++ project builds with Scons. At first I have only the optimized version to compile, it works fine. Then I also need a debug version, then I add another environment for it. Here is the Scons code:

env = Environment()

opt = env.Clone(CCFLAGS=['-pthread', '-O3', '-Wall'])
opt_objs = opt.Glob('src/*.cpp')
prog = opt.Program('prog', opt_objs)

dbg = env.Clone(CCFLAGS=['-pthread', '-Wall', '-g', '-O0'])
dbg_objs = dbg.Glob('src/*.cpp')
dbg_prog = dbg.Program('dbg_prog', dbg_objs)

With this code, I ran into error:

scons: *** Two environments with different actions were specified for the same target: 
src/CometReadService.o

As you can see, those .o files targets created by opt.Glob('src/.cpp') and dbg.Glob('src/.cpp') exactly same name. By reading the document Multiple Construction Environments I know I can rename the object like "opt.Object('xxx-opt', 'xxx.c')", but however, it is Glob not Object. How can I solve this problem?

like image 389
Fang-Pen Lin Avatar asked Oct 26 '10 13:10

Fang-Pen Lin


People also ask

How does SCons build work?

SCons is a computer software build tool that automatically analyzes source code file dependencies and operating system adaptation requirements from a software project description and generates final binary executables for installation on the target operating system platform.

How do I run a SCons script?

Click the start button, (on XP click Run), then type cmd and from the command line type python -V. It should say something like Python 2.7. 2. Any version 2.4 or higher is ok for SCons.

How can I speed up my SCons?

The command 'scons --max-drift=1 --implicit-deps-unchanged' will execute your build as fast as possible.

What is SCons command?

DESCRIPTION. The scons utility builds software (or other files) by determining which component pieces must be rebuilt and executing the necessary commands to rebuild them.


1 Answers

The scons manual describes how to use the VariantDir function (or argument when adding SConscripts) to set up different build directories. At its simplest, VariantDir separates the build output from the source files, but it can also be used to separate the build output of different environments.

env = Environment()

opt = env.Clone(CCFLAGS=['-pthread', '-O3', '-Wall'])
opt.VariantDir('gen-opt', 'src', duplicate=0)
opt_objs = opt.Glob('gen-opt/*.cpp')
prog = opt.Program('prog', opt_objs)

dbg = env.Clone(CCFLAGS=['-pthread', '-Wall', '-g', '-O0'])
dbg.VariantDir('gen-dbg', 'src', duplicate=0)
dbg_objs = dbg.Glob('gen-dbg/*.cpp')
dbg_prog = dbg.Program('dbg_prog', dbg_objs)

Using VariantDir can take some experimentation. For instance, note that the Glob argument has changed -- without the duplicate=0 parameter, the default behavior is for VariantDir to duplicate the source files in the build directory.

like image 132
Dave Bacher Avatar answered Sep 24 '22 22:09

Dave Bacher