SCons配置文件和默认值

2024-01-18

我有一个使用 SCons 构建的项目(以及 MinGW/gcc,具体取决于平台)。这个项目依赖于其他几个库(我们称它们为libfoo and libbar)可以为不同的用户安装在不同的地方。

目前,我的SConstruct文件嵌入了这些库的硬编码路径(例如:C:\libfoo).

现在,我想添加一个配置选项到我的SConstruct文件,以便安装的用户libfoo在另一个位置(比如C:\custom_path\libfoo)可以做类似的事情:

> scons --configure --libfoo-prefix=C:\custom_path\libfoo

Or:

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

Does scons提供这样的机制吗?我将如何实现这种行为?我并没有完全掌握Python,所以即使是显而易见的(但完整的)解决方案也是受欢迎的。

Thanks.


SCons 有一个功能称为“变量 http://www.scons.org/doc/production/HTML/scons-user/x2429.html#AEN2488“。您可以对其进行设置,以便它很容易地从命令行参数变量中读取。因此,在您的情况下,您可以从命令行执行类似的操作:

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功能,但比较复杂。

这个问题 https://stackoverflow.com/questions/456100/scons-problem-dont-understand-variables-class讨论从变量对象中获取值而不通过环境传递它们所涉及的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SCons配置文件和默认值 的相关文章

随机推荐