SCons 配置文件及默认值
我有一个项目,是用 SCons(根据平台使用 MinGW/gcc)来构建的。这个项目依赖于几个其他的库(我们称它们为 libfoo
和 libbar
),这些库可以安装在不同的位置,供不同的用户使用。
目前,我的 SConstruct
文件里写死了这些库的路径(比如:C:\libfoo
)。
现在,我想在我的 SConstruct
文件中添加一个配置选项,这样如果用户把 libfoo
安装在其他地方(比如 C:\custom_path\libfoo
),就可以像这样操作:
> scons --configure --libfoo-prefix=C:\custom_path\libfoo
或者:
> scons --configure
scons: Reading SConscript files ...
scons: done reading SConscript files.
### Environment configuration ###
Please enter location of 'libfoo' ("C:\libfoo"): C:\custom_path\libfoo
Please enter location of 'libbar' ("C:\libfoo"): C:\custom_path\libbar
### Configuration over ###
一旦选择了这些配置选项,它们应该被写入某个文件中,并且每次运行 scons
时都能自动读取。
请问 scons
有这样的机制吗?我该如何实现这个功能?我对 Python 不是很精通,所以即使是明显的(但完整的)解决方案也非常欢迎。
谢谢。
1 个回答
6
SCons 有一个叫做 "变量" 的功能。你可以很简单地设置它,让它从命令行的参数中读取变量。所以在你的情况下,你可以在命令行中这样做:
scons LIBFOO=C:\custom_path\libfoo
... 这样变量在多次运行之间会被记住。所以下次你只需要运行 scons
,它就会使用之前的 LIBFOO 的值。
在代码中,你可以这样使用它:
# read variables from the cache, a user's custom.py file or command line
# arguments
var = Variables(['variables.cache', 'custom.py'], ARGUMENTS)
# add a path variable
var.AddVariables(PathVariable('LIBFOO',
'where the foo library is installed',
r'C:\default\libfoo', PathVariable.PathIsDir))
env = Environment(variables=var)
env.Program('test', 'main.c', LIBPATH='$LIBFOO')
# save variables to a file
var.Save('variables.cache', env)
如果你真的想使用 "--" 这种风格的选项,你可以把上面的内容和 AddOption
函数结合起来,但这样会复杂一些。
这个 SO 问题 讨论了如何从变量对象中获取值,而不通过环境传递它们时所遇到的问题。