Chinaunix首页 | 论坛 | 博客
  • 博客访问: 783367
  • 博文数量: 231
  • 博客积分: 3217
  • 博客等级: 中校
  • 技术积分: 2053
  • 用 户 组: 普通用户
  • 注册时间: 2011-07-04 12:01
文章分类

全部博文(231)

文章存档

2015年(1)

2013年(10)

2012年(92)

2011年(128)

分类: Python/Ruby

2012-08-27 15:43:08

scons提供GetOpt函数得到命令行选项变量的值
SCons provides theGetOptionfunction to get the values set by the various command-line options. One common use of this is to check whether or not the-hor--helpoption has been specified. Normally, SCons does not print its help text until after it has read all of theSConscriptfiles, because it’s possible that help text has been added by some subsidiary SConscriptfile deep in the source tree hierarchy. Of course, reading all of theSConscriptfiles takes extra time.
 
use_colors = GetOption('use_colors')
 
scons提供SetOption函数设置SConscript文件的命令行值。
You can also set the values of SCons command-line options from within theSConscriptfiles by using theSetOptionfunction. The strings that you use to set the values of SCons command-line options are available in theSection 12.1.4section, below.
import os
num_cpu = int(os.environ.get(’NUM_CPU’, 2))
SetOption(’num_jobs’, num_cpu)
print "running with -j", GetOption(’num_jobs’)
 
scons通过AddOption函数定义命令选项。它使用参数像标准python库中optparse.add_option函数一样。使用GetOpt函数获取命令,使用SetOpt函数来设置命令。
下面提供一个“--prefix”命令选项:
AddOption(’--prefix’,
dest=’prefix’,
type=’string’,
nargs=1,
action=’store’,
metavar=’DIR’,
help=’installation prefix’)
env = Environment(PREFIX = GetOption(’prefix’))
installed_foo = env.Install(’$PREFIX/usr/bin’, ’foo.in’)
Default(installed_foo)
 
Command-Linevariable=valueBuild Variables
在命令行设置变量的参数如:% scons -Q debug=1 
设置debug的参数为1.
也可以使用python中ARGUMENTS.get()函数来实现此功能。
env = Environment()
debug = ARGUMENTS.get(’debug’, 0)
if int(debug):
env.Append(CCFLAGS = ’-g’)
env.Program(’prog.c’)
 
 Providing Help for Command-Line Build Variables
提供Help函数来支持GenerateHelpText
vars = Variables(’custom.py’)
vars.Add(’RELEASE’, ’Set to 1 to build for release’, 0)
env = Environment(variables = vars)
Help(vars.GenerateHelpText(env))
 
Reading Build Variables From a File从文件读取变量:
创造一个Variable对象让用户从本地文件中读入编译变量:
vars = Variables(’custom.py’)
vars.Add(’RELEASE’, ’Set to 1 to build for release’, 0)
env = Environment(variables = vars,
CPPDEFINES={’RELEASE_BUILD’ : ’${RELEASE}’})
env.Program([’foo.c’, ’bar.c’])
Help(vars.GenerateHelpText(env))
你也可以改变custom.py中RELEASE=1
% scons -Q
cc -o bar.o -c -DRELEASE_BUILD=1 bar.c
cc -o foo.o -c -DRELEASE_BUILD=1 foo.c
cc -o foo foo.o bar.o
当RELEASE=0:
% scons -Q
cc -o bar.o -c -DRELEASE_BUILD=0 bar.c
cc -o foo.o -c -DRELEASE_BUILD=0 foo.c
cc -o foo foo.o bar.o
 
预定义Variable函数:
True/False Values: theBoolVariableBuild Variable Function
vars = Variables(’custom.py’)
vars.Add(BoolVariable(’RELEASE’, ’Set to build for release’, 0))
env = Environment(variables = vars,
CPPDEFINES={’RELEASE_BUILD’ : ’${RELEASE}’})
env.Program(’foo.c’)
 
Single Value From a List: theEnumVariableBuild Variable Function
从一个列表选定一个值设置为参数的值:
vars = Variables(’custom.py’)
vars.Add(EnumVariable(’COLOR’, ’Set background color’, ’red’,allowed_values=(’red’, ’green’, ’blue’)))
env = Environment(variables = vars,
CPPDEFINES={’COLOR’ : ’"${COLOR}"’})
env.Program(’foo.c’)
 
Multiple Values From a List: theListVariableBuild Variable :设置参数是一个或多个变量值
vars = Variables(’custom.py’)
vars.Add(ListVariable(’COLORS’, ’List of colors’, 0,
[’red’, ’green’, ’blue’]))
env = Environment(variables = vars,
CPPDEFINES={’COLORS’ : ’"${COLORS}"’})
env.Program(’foo.c’)
 
Path Names: thePathVariableBuild Variable Function:设置编译文件所在的的路径
vars = Variables(’custom.py’)
vars.Add(PathVariable(’CONFIG’,
’Path to configuration file’,
’/etc/my_config’))
env = Environment(variables = vars,
CPPDEFINES={’CONFIG_FILE’ : ’"$CONFIG"’})
env.Program(’foo.c’)
 
Enabled/Disabled Path Names: thePackageVariableBuild Variable Function:
Sometimes you want to give users even more control over a path name variable, allowing them to
explicitly enable or disable the path name by usingyesornokeywords, in addition to allow them to supply an explicit path name. SCons supports thePackageVariablefunction to support this:
vars = Variables(’custom.py’)
vars.Add(PackageVariable(’PACKAGE’,
’Location package’,
’/opt/location’))
env = Environment(variables = vars,
CPPDEFINES={’PACKAGE’ : ’"$PACKAGE"’})
env.Program(’foo.c’)
 
 
 
阅读(2199) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~