Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scons: overriding build options for one file

Tags:

scons

Easy question but I don't know the answer.

Let's say I have a scons build where my CCFLAGS includes -O1. I have one file needsOptimization.cpp where I would like to override the -O1 with -O2 instead. How could I do this in scons?


update: this is what I ended up doing based on bialix's answer:

in my SConscript file:

Import('env');

env2 = env.Clone();
env2.Append(CCFLAGS=Split('-O2 --asm_listing'));

sourceFiles = ['main.cpp','pwm3phase.cpp'];
sourceFiles2 = ['serialencoder.cpp','uartTestObject.cpp'];
objectFiles = [];
objectFiles.append(env.Object(sourceFiles));
objectFiles.append(env2.Object(sourceFiles2));
   ...

previously this file was:

Import('env');

sourceFiles = ['main.cpp','pwm3phase.cpp','serialencoder.cpp','uartTestObject.cpp'];
objectFiles = env.Object(sourceFiles);
   ...
like image 781
Jason S Avatar asked Jan 06 '10 14:01

Jason S


2 Answers

Use Object() builder for fine-grained control over compilation, and then pass these objects to Program() builder.

E.g. instead of:

env = Environment()
env.Program(target='foo', source=['foo.cpp', 'bar.cpp', 'needsOptimisation.cpp'])

You need to use following:

env = Environment()
env_o1 = env.Clone()
env_o1.Append(CCFLAGS = '-O1')

env_o2 = env.Clone()
env_o2.Append(CCFLAGS = '-O2')

# extend these lists if needed
SRC_O1 = ['foo.cpp', 'bar.cpp']
SRC_O2 = ['needsOptimisation.cpp']

obj_o1 = [env_o1.Object(i) for i in SRC_O1]
obj_o2 = [env_o2.Object(i) for i in SRC_O2]

env.Program(target='foo', source=obj_o1+obj_o2)

You can avoid creation of separate clone of env variable if you provide CCFLAGS='-O2' right in the Object() call:

obj_o2 = [env.Object(i, CCFLAGS=env['CCFLAGS'] + ['-O2']) for i in SRC_O2]
like image 186
bialix Avatar answered Sep 20 '22 12:09

bialix


Avoiding creating of a separate env variable requires (ref: bialix's answer) needs something like this.

 obj_o2 = env.Object(SRC_O2, CCFLAGS=env['CCFLAGS'] + ['-O2']);

If you just do this (or in the for loop like bialix does)

 obj_o2 = env.Object(SRC_O2, CCFLAGS='-O2');

then you lose all the builtin flags.

like image 6
Jason S Avatar answered Sep 21 '22 12:09

Jason S