Read scons build variables from a external.py file

时间秒杀一切 提交于 2019-12-12 18:27:43

问题


I want to define the scons build variables in external.py file like

external.py

mode=debug
toolchain=x86

This I want to read back these variables in the SConstruct file which is there in the same directory. Depending on the variable values I want to do some operations!

vars = Variables('external.py')
vars.Add('mode', 'Set the mode for debug or release', 'debug')
if ${RELEASE}=="debug"
   #Do these!
elif ${RELEASE}=="release"
   #Do that!

回答1:


If external.py contains valid Python code then you can simply import it using the import keyword. You can then use the dir function to iterate over the names defined in the external module and add them to the SCons variables. You might also want to take a look at the getattr function.




回答2:


The thing you are missing is Scons Environment with your variables.

vars = Variables('external.py')
vars.Add('mode', 'Set the mode for debug or release', 'debug')

env = Environment(variables = vars)

if env['mode'] == 'debug':
    # do action1
elif env['mode'] == 'release':
    # do action2
else:
    # do action3


You can read more about using Scons here, and about your question here




回答3:


Soumyajit answer is great but I would add that if you want to be able to override values from your file with the command line and restrain the allowed values for your variables you can do as follow:

# Build variables are loaded in this order:
# Command Line (ARGUMENTS) >> Config File (external.py) >> Default Value
vars = Variables(files='external.py', args=ARGUMENTS)
vars.Add(EnumVariable('mode', 'Build mode.', 'debug', allowed_values=('debug', 'release')))

env = Environment(variables = vars)

if env['mode'] == 'debug':
    env.Append(CCFLAGS = [ '-g' ])
    # whatever...
else:
    env.Append(CCFLAGS = '-O2')
    # whatever...

You can invoke you build script like this scons but also override specific variables without editing your config file by doing scons mode=release

If you specify a bad value for your variable you will get an error from Scons like:

$> scons mode=foo
scons: Reading SConscript files ...
scons: *** Invalid value for option mode: foo.  Valid values are: ('debug', 'release')


来源:https://stackoverflow.com/questions/20122810/read-scons-build-variables-from-a-external-py-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!