ere*_*eOn 5 python configuration scons
我有一个项目,我使用SCons(和MinGW/gcc,取决于平台)构建.该项目依赖于其他几个库(让我们称之为libfoo和libbar),它可以在不同的地方为不同的用户进行安装.
目前,我的SConstruct文件将硬编码路径嵌入到这些库中(例如,类似:) C:\libfoo.
现在,我想在我的SConstruct文件中添加一个配置选项,以便安装libfoo在其他位置的用户(比如说C:\custom_path\libfoo)可以执行以下操作:
> scons --configure --libfoo-prefix=C:\custom_path\libfoo
Run Code Online (Sandbox Code Playgroud)
要么:
> 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 ###
Run Code Online (Sandbox Code Playgroud)
选择后,应将这些配置选项写入某个文件,并在每次scons运行时自动重新读取.
是否scons提供这样的机制?我该如何实现这种行为?我并不完全掌握Python,所以即使是明显(但完整)的解决方案也是受欢迎的.
谢谢.
SCons有一个名为" 变量 " 的功能.您可以对其进行设置,以便它可以非常轻松地从命令行参数变量中读取.因此,在您的情况下,您可以从命令行执行以下操作:
scons LIBFOO=C:\custom_path\libfoo
Run Code Online (Sandbox Code Playgroud)
......并且在运行之间会记住变量.所以下次你运行时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)
Run Code Online (Sandbox Code Playgroud)
如果你真的想使用" - "样式选项,那么你可以将上面的内容与AddOption函数结合起来,但它更复杂.
这个SO问题讨论了从Variables对象中获取值而不通过环境传递它们所涉及的问题.