1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
| >>>> 如果你想使用者调用一些除了scons提供的指令,你可以这样做
ARGUMENTS 这个东西将会帮到你. 但是有个缺陷是不能存储两个值,只能 key-value 的形式. ARGUMENTS.get() 获取 debug 这个指令 没有用后面的默认值
env = Environment() debug = ARGUMENTS.get('debug', 0) if int(debug): env.Append(CCFLAGS = '-g') env.Program('prog.c')
ej: scons -Q debug=0
>>>> 如果你想 一对多 你可以使用这个东西 ARGLIST
cppdefines = [] for key, value in ARGLIST: if key == 'define': cppdefines.append(value) env = Environment(CPPDEFINES = cppdefines) env.Object('prog.c')
ej: scons -Q define=FOO define=BAR
>>>> 从文件中读取用户构建配置 >>>> 会读取 custom.py 文件的配置表 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
>>>> 可以结合 两种做法 读取用户 在命令行输入或则是配置文件 >>>> 其中 本地配置文件会首先 覆盖 命令行 的 配置 vars = Variables('custom.py',ARGUMENTS)
>>>> 定义构造变量的函数 >>>> Bool 可以指定的值有 true => t on all 1 true False => f 0 no false
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')
ej: scons -Q RELEASE=yes foo.o
>>>> Enum 有个 allowed_values 的字段 定义枚举变量值 >>>> map 字段备用映射名 map={'navy':'blue'}, >>>> ignorecase=1 忽略 指定指令的大小写 如 COLOR=NaVy
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')
ej: scons -Q COLOR=red
>>>> ListVariable 允许一个字段指定多个值 >>>> 允许使用 all 或则是 none
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')
ej: scons -Q COLORS=red,blue foo.o => cc -o foo.o -c -DCOLORS="red blue" foo.c ej: scons -Q COLORS=all foo.o => cc -o foo.o -c -DCOLORS="red green blue" foo.c ej: scons -Q COLORS=none foo.o => cc -o foo.o -c -DCOLORS="" foo.c
>>>> PathVariable 路径变量 >>>> 提供输入的路径是文件 PathVariable.PathIsFile >>>> 是目录 PathVariable.PathIsDir >>>> 目录不存在,并创建 PathVariable.PathIsDirCreate >>>> 不关心上面的三点 PathVariable.PathAccept
ej: vars = Variables('custom.py') vars.Add(PathVariable('CONFIG', 'Path to configuration file', '/etc/my_config', PathVariable.PathIsFile)) env = Environment(variables = vars, CPPDEFINES={'CONFIG_FILE' : '"$CONFIG"'}) env.Program('foo.c')
>>>> PackageVariable >>>> 启用或禁止 路径名字 没有太懂 vars = Variables('custom.py') vars.Add(PackageVariable('PACKAGE', 'Location package', '/opt/location')) env = Environment(variables = vars, CPPDEFINES={'PACKAGE' : '"$PACKAGE"'}) env.Program('foo.c')
ej: % scons -Q foo.o cc -o foo.o -c -DPACKAGE="/opt/location" foo.c % scons -Q PACKAGE=/usr/local/location foo.o cc -o foo.o -c -DPACKAGE="/usr/local/location" foo.c % scons -Q PACKAGE=yes foo.o cc -o foo.o -c -DPACKAGE="True" foo.c % scons -Q PACKAGE=no foo.o cc -o foo.o -c -DPACKAGE="False" foo.c
>>>> AddVariables 一次性添加多个变量功能
vars = Variables() vars.AddVariables( ('RELEASE', 'Set to 1 to build for release', 0), ('CONFIG', 'Configuration file', '/etc/my_config'), BoolVariable('warnings', 'compilation with -Wall and similiar', 1), EnumVariable('debug', 'debug output and symbols', 'no', allowed_values=('yes', 'no', 'full'), map={}, ignorecase=0), ListVariable('shared', 'libraries to build as shared libraries', 'all', names = list_of_libs), PackageVariable('x11', 'use X11 installed here (yes = search some places)', 'yes'), PathVariable('qtdir', 'where the root of Qt is installed', qtdir), )
>>>> UnknownVariables 在用户使用了未定义的 指令时 你可以警告调用者 拼写错误 并退出程序
vars = Variables(None) vars.Add('RELEASE', 'Set to 1 to build for release', 0) env = Environment(variables = vars, CPPDEFINES={'RELEASE_BUILD' : '${RELEASE}'}) unknown = vars.UnknownVariables() if unknown: print("Unknown variables: %s"%unknown.keys()) Exit(1) env.Program('foo.c')
>>>> 你可以在用户使用某个命令的时候 提醒用户一些操作 if 'bar' in COMMAND_LINE_TARGETS: print("Don't forget to copy `bar' to the archive!") Default(Program('foo.c')) Program('bar.c')
ej: % scons -Q bar Don't forget to copy `bar' to the archive!
>>>> Default() 用户没有指定构建是 可以使用 这个函数 默认 构建一个程序 env = Environment() hello = env.Program('hello.c') env.Program('goodbye.c') Default(hello)
ej: % scons -Q cc -o hello.o -c hello.c cc -o hello hello.o % scons -Q scons: `hello' is up to date. % scons -Q goodbye cc -o goodbye.o -c goodbye.c cc -o goodbye goodbye.o Default 跟过用法 [传送门](https://scons.org/doc/production/HTML/scons-user/ch10s03.html)
>>>> DEFAULT_TARGETS 主要是迎合上面 Default 函数, 看一下例子应该很容易理解 prog1 = Program('prog1.c') Default(prog1) print("DEFAULT_TARGETS is %s"%map(str, DEFAULT_TARGETS))
ej: % scons scons: Reading SConscript files ... DEFAULT_TARGETS is ['prog1'] scons: done reading SConscript files. scons: Building targets ... cc -o prog1.o -c prog1.c cc -o prog1 prog1.o scons: done building targets.
>>>> BUILD_TARGETS prog1 = Program('prog1.c') Program('prog2.c') Default(prog1) print ("BUILD_TARGETS is %s"%map(str, BUILD_TARGETS))
ej: % scons -Q BUILD_TARGETS is ['prog1'] cc -o prog1.o -c prog1.c cc -o prog1 prog1.o % scons -Q prog2 BUILD_TARGETS is ['prog2'] cc -o prog2.o -c prog2.c cc -o prog2 prog2.o % scons -Q -c . BUILD_TARGETS is ['.'] Removed prog1.o Removed prog1 Removed prog2.o Removed prog2
>>>> 产生帮助文档(help) vars = Variables(None, ARGUMENTS) vars.Add('RELEASE', 'Set to 1 to build for release', 0) env = Environment(variables = vars) Help(vars.GenerateHelpText(env))
|