views:

283

answers:

2

The SCons User Guide tells about the usage of Multiple Construction Environments to build build multiple versions of a single program and gives the following example:

opt = Environment(CCFLAGS = '-O2')
dbg = Environment(CCFLAGS = '-g')

o = opt.Object('foo-opt', 'foo.c')
opt.Program(o)

d = dbg.Object('foo-dbg', 'foo.c')
dbg.Program(d)

Instead of manually assigning different names to the objects compiled with different environments, VariantDir() / variant_dir sounds like a better solution...

But if I place the Program() builder inside the SConscript:

Import('env')
env.Program('foo.c')

How can I export different environments to the same SConscript file?

opt = Environment(CCFLAGS = '-O2')
dbg = Environment(CCFLAGS = '-g')

SConscript('SConscript', 'opt', variant_dir='release') #'opt' --> 'env'???
SConscript('SConscript', 'dbg', variant_dir='debug')   #'dbg' --> 'env'???

Unfortunately the discussion in the SCons Wiki does not bring more insight to this topic.

Thanks for your input!

+1  A: 

SConscript is a method defined on the environment itself:

for dir, env in (('release', opt), ('debug', dbg)):
    env.SConscript('SConscript', 'env', variant_dir=dir)

And then from the SConscript you can:

Import('env')
BennyG
Thank you BennyG: This looks very interesting and pythonic :-). I started to try your suggestion but until now I am stucked with other changes in my build scripts... I will get back with more feedback later!
OK
I'm still struggling with some of the aspects variant_dir brings into my SConscript, but your solution is perfectly right for my question. Thanks again!
OK
+1  A: 

Alternately, you can pass a dictionary as the exports arg to SConscript. The keys are the name the SConscript will use to import it, and the values are the objects in the SConstruct. So:

SConscript('SConscript', exports={'env': dbg}, variant_dir='debug')
SConscript('SConscript', exports={'env': opt}, variant_dir='release')

then in the SConscript Import('env') will get dbg the first time and opt the second time. This also works for exporting/importing anything else you like, not just env.

See Export() and SConscript() in the man page for more info.

garyo
Thanks garyo for pointing out this alternative. The man page is sparse on examples for the dictionary case, but in the end this approach looks cleaner to less pythonic people like me :-).
OK