SCons配置文件和默认值 [英] SCons configuration file and default values

查看:263
本文介绍了SCons配置文件和默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目,我建立使用SCons(和MinGW / gcc取决于平台)。这个项目依赖于其他几个库(可以调用 libfoo libbar

I have a project which I build using SCons (and MinGW/gcc depending on the platform). This project depends on several other libraries (lets call them libfoo and libbar) which can be installed on different places for different users.

目前,我的 SConstruct 文件会将硬编码路径嵌入到这些库中(例如: code> C:\ libfoo )。

Currently, my SConstruct file embeds hard-coded path to those libraries (say, something like: C:\libfoo).

现在,我想添加一个配置选项到我的 SConstruct 文件,以便在另一个位置(例如 C:\custom_path\)安装 libfoo libfoo )可以这样做:

Now, I'd like to add a configuration option to my SConstruct file so that a user who installed libfoo at another location (say C:\custom_path\libfoo) can do something like:

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

/ p>

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 运行时都会自动重新读取。

Once chosen, those configuration options should be written to some file and reread automatically every time scons runs.

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

Does scons provide such a mechanism ? How would I achieve this behavior ? I don't exactly master Python so even obvious (but complete) solutions are welcome.

谢谢。

推荐答案

SCons有一个名为变量。你可以设置它,使它从命令行参数变量很容易读取。所以在你的情况下,你会从命令行做这样的事情:

SCons has a feature called "Variables". You can set it up so that it reads from command line argument variables pretty easily. So in your case you would do something like this from the command line:

scons LIBFOO=C:\custom_path\libfoo

...并且变量将在运行之间记住。所以下次你只需运行 scons ,它使用LIBFOO的前一个值。

... and the variable would be remembered between runs. So next time you just run scons and it uses the previous value of LIBFOO.

在代码中你使用它所以:

In code you use it like so:

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

如果你真的想使用 - 样式选项, code> AddOption 函数,但更复杂。

If you really wanted to use "--" style options then you could combine the above with the AddOption function, but it is more complicated.

这个SO问题谈到了从变量对象获取值而不通过环境的问题。

This SO question talks about the issues involved in getting values out of the Variables object without passing them through an Environment.

这篇关于SCons配置文件和默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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