从 external.py 文件读取 scons 构建变量 [英] Read scons build variables from a external.py file

查看:38
本文介绍了从 external.py 文件读取 scons 构建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 external.py 文件中定义 scons 构建变量,如

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

mode=debug
toolchain=x86

我想读回 SConstruct 文件中的这些变量,该文件位于同一目录中.我要根据变量值做一些操作!

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!

推荐答案

Soumyajit 的回答很好,但我想补充一点,如果您希望能够使用命令行覆盖文件中的值并限制变量的允许值您可以执行以下操作:

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...

您可以像这样调用您的构建脚本 scons,但也可以通过执行 scons mode=release

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

如果你为变量指定了一个错误的值,你会从 Scons 得到一个错误,比如:

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')

这篇关于从 external.py 文件读取 scons 构建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆