2010-09-16 103 views
5

我有一个使用SCons(和MinGW/gcc取决于平台)构建的项目。这个项目取决于其他几个库(我们称它们为libfoolibbar),它们可以安装在不同的地方供不同的用户使用。SCons配置文件和默认值

目前,我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,所以即使是明显的(但完整的)解决方案也是受欢迎的。

谢谢。

回答

5

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

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功能,但它是更为复杂。

This SO question讨论了将值从Variables对象中取出而不通过环境传递的问题。

+0

谢谢,这似乎有窍门;)是否有另一种方法来获取变量的值?像'print var.getVariable('LIBFOO')''? – ereOn 2010-09-16 12:32:25

+0

@ereOn我已经搜遍了文档,但是*没有*似乎有任何方法可以做到这一点。相当不对称。您必须将变量放入环境中并将其读出。如果我知道,我会更新这篇文章。 – richq 2010-09-16 18:40:36