SCons教程(6) 环境
環境
外部環境
用戶在執行 SCons 的外部環境變量設置是,可以使用 python 的 os.ennviron 模塊,可以將 import os 放在任意 SConstruct 中,并在該文件中使用用戶外部環境變量。
import osenv = os.environ print("current OS is", env["OS"])執行 socns 結果就是 ‘Windows_NT’,對此,認為可以使用用戶環境變量或者是系統環境變量。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q ('current OS is', 'Windows_NT') scons: `.' is up to date.特此說明,SCons 可以使用大部分 python 中的語法、環境、庫等信息,模塊調用也可以共享。
本地環境
在一個構建環境中,可以對環境內容進行設置,例如設置不同編譯器,優化等級,庫文件,頭文件搜索路徑或者其他參數,此時需要使用 Environment 相關函數。
創建一個環境
env = Environment(CC='gcc', CCFLAGS='-O2') env.Program('hello.c')執行 scons 之后的結果就是下面結果,可以看出和之前不同的是,編譯語句增加了 -O2 的優化等級,和設置的一致。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q gcc -o hello.o -c -O2 hello.c gcc -o hello.exe hello.o除了可以設置環境變量之外,也可以通過 get 函數獲取變量的值,并打印輸出。
env = Environment() print("CC is: %s" % env['CC']) print("LATEX is: %s" % env.get('LATEX', None))執行 scons 之后的結果就是下面結果,默認編譯器就是 gcc , LATEX 變量沒有值,使用了設置的默認值 None。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q CC is: gcc LATEX is: None scons: `.' is up to date.查看默認 SCons 的默認變量,可以使用下面語句:
env = Environment() for item in sorted(env.Dictionary().items()):print("construction variable = '%s', value = '%s'" % item)# 或者 env = Environment() print(env.Dump())命令行參數
增加 help 語句
在一個命令不知道怎么使用時,使用 --help 來獲取當前命令的選項幫助是最好的方法。SCons 也是提供了 help 選項的編寫,可以幫助用戶簡單的創建 --help 的語句。
Help(""" Type: 'scons program' to build the production program, 'scons debug' to build the debug version. """) admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -h scons: Reading SConscript files ... scons: done reading SConscript files.Type: 'scons program' to build the production program, 'scons debug' to build the debug version.Use scons -H for help about command-line options.使用 -H 可以看有關 SCons 的幫助文檔。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -H usage: scons [OPTION] [TARGET] ...SCons Options:-b, -d, -e, -m, -S, -t, -w, --environment-overrides, --no-keep-going,--no-print-directory, --print-directory, --stop, --touchIgnored for compatibility.-c, --clean, --remove Remove specified targets and dependencies.-C DIR, --directory=DIR Change to DIR before doing anything.--cache-debug=FILE Print CacheDir debug info to FILE.--cache-disable, --no-cacheDo not retrieve built targets from CacheDir.--cache-force, --cache-populateCopy already-built targets into the CacheDir.--cache-readonly Do not update CacheDir with built targets.--cache-show Print build actions for files from CacheDir.--config=MODE Controls Configure subsystem: auto, force,cache.-D Search up directory tree for SConstruct,build all Default() targets.--debug=TYPE Print various types of debugging information:count, duplicate, explain, findlibs, includes,memoizer, memory, objects, pdb, prepare,presub, stacktrace, time, action-timestamps.--diskcheck=TYPE Enable specific on-disk checks.--duplicate=DUPLICATE Set the preferred duplication methods. Must beone of hard-soft-copy, soft-hard-copy,hard-copy, soft-copy, copy--enable-virtualenv Import certain virtualenv variables to SCons-f FILE, --file=FILE, --makefile=FILE, --sconstruct=FILERead FILE as the top-level SConstruct file.-h, --help Print defined help message, or this one.-H, --help-options Print this message and exit.-i, --ignore-errors Ignore errors from build actions.-I DIR, --include-dir=DIR Search DIR for imported Python modules.--ignore-virtualenv Do not import virtualenv variables to SCons--implicit-cache Cache implicit dependencies--implicit-deps-changed Ignore cached implicit dependencies.--implicit-deps-unchanged Ignore changes in implicit dependencies.--interact, --interactive Run in interactive mode.-j N, --jobs=N Allow N jobs at once.-k, --keep-going Keep going when a target can't be made.--max-drift=N Set maximum system clock drift to N seconds.--md5-chunksize=N Set chunk-size for MD5 signature computation toN kilobytes.-n, --no-exec, --just-print, --dry-run, --reconDon't build; just print commands.--no-site-dir Don't search or use the usual site_scons dir.--profile=FILE Profile SCons and put results in FILE.-q, --question Don't build; exit status says if up to date.-Q Suppress "Reading/Building" progress messages.--random Build dependencies in random order.-s, --silent, --quiet Don't print commands.--site-dir=DIR Use DIR instead of the usual site_scons dir.--stack-size=N Set the stack size of the threads used to runjobs to N kilobytes.--taskmastertrace=FILE Trace Node evaluation to FILE.--tree=OPTIONS Print a dependency tree in various formats: all,derived, prune, status.-u, --up, --search-up Search up directory tree for SConstruct,build targets at or below current directory.-U Search up directory tree for SConstruct,build Default() targets from local SConscript.-v, --version Print the SCons version number and exit.--warn=WARNING-SPEC, --warning=WARNING-SPECEnable or disable warnings.-Y REPOSITORY, --repository=REPOSITORY, --srcdir=REPOSITORYSearch REPOSITORY for source and target files.獲取命令行輸入參數
可以使用 ARGUMENTS.get 來獲取命令行參數,在一些情況下,可以增加選項以增加多個項目的構建。
env = Environment() if ARGUMENTS.get('VERBOSE') != '1':print("VERBOSE") admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q VERBOSE scons: '.' is up to date.admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q VERBOSE=1 scons: '.' is up to date.或者使用 set PARAM=VALUE 方式顯式設置參數的值。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ set SCONSFLAGS=-Qadmin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons VERBOSE scons: `.' is up to date.獲取參數
- GetOption
修改 SCsonstruct 內容如下,在命令行使用 -h, --help 時,可以使用 GetOption 來獲取 help 參數,如果有 help 選項,則輸出 help text 文本,沒有 help 選項時,輸出 not help 。
可以看出預期結果進行輸出。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -h hellp help text Use scons -H for help about command-line options.admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons not hellp scons: `.' is up to date.設置選項
- SetOption
使用 SetOption 可以設置命令行選項,例如設置 GetOption('help') 就可以看作是執行了 scons -h 。
import osSetOption('help', "1") print("help : %s" % GetOption('help')) Program('hello.c')下面可以看到 SCson 的可選項有 -h, -d, -e, -m, -S, -t, -w 等,可以使用 SetOption 進行配置。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons scons: Reading SConscript files ... help : 1 scons: done reading SConscript files. usage: scons [OPTION] [TARGET] ...SCons Options:-b, -d, -e, -m, -S, -t, -w, --environment-overrides, --no-keep-going,--no-print-directory, --print-directory, --stop, --touch可以使用 GetOption 或者 SetOption 的選項如下:
| cache_debug | –cache-debug |
| cache_disable | –cache-disable |
| cache_force | –cache-force |
| cache_show | –cache-show |
| clean | -c, --clean, --remove |
| config | –config |
| directory | -C, --directory |
| diskcheck | –diskcheck |
| duplicate | –duplicate |
| file | -f, --file, --makefile , --sconstruct |
| help | -h, --help |
| ignore_errors | –ignore-errors |
| implicit_cache | –implicit-cache |
| implicit_deps_changed | –implicit-deps-changed |
| implicit_deps_unchanged | –implicit-deps-unchanged |
| interactive | –interact, --interactive |
| keep_going | -k, --keep-going |
| max_drift | –max-drift |
| no_exec | -n, --no-exec, --just-print, --dry-run, --recon |
| no_site_dir | –no-site-dir |
| num_jobs | -j, --jobs |
| profile_file | –profile |
| question | -q, --question |
| random | –random |
| repository | -Y, --repository, --srcdir |
| silent | -s, --silent, --quiet |
| site_dir | –site-dir |
| stack_size | –stack-size |
| taskmastertrace_file | –taskmastertrace |
| warn | –warn --warning |
增加選項
暫時不做解釋
變量
隨時可以在構建命令中,使用 ARGUMENTS 或者 增加選項來通過 GetOption 來獲取命令行中的選項變量,但是每次執行,都需要開發者對執行命令特別熟悉,以防止漏掉選項造成編譯失敗或者編譯與實際情況不符等現象。為了解決上述問題,可以使用 python 讀取一個文件,文件內存儲使用的選項變量。SCons 提供了 Variables 類來處理構建變量,來影響整個構建過程。
- Add
增加一個變量,使用 key-value 結構。
執行 scons -Q -HELLO_BUILD=1 可以得到以下輸出。
admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q HELLO_BUILD=1 gcc -o hello.o -c -DHELLO=1 hello.c gcc -o hello.exe hello.o- 讀取外部文件
創建外部文件,作為選項的存放文件,所有的選項都放在這個文件內,使用時通過 python 來讀取每個選項,并增加到當前環境中。
vars = Variables('var.py', ARGUMENTS) vars.Add('HELLO_BUILD') env = Environment(variables=vars, CPPDEFINES={'HELLO': '${HELLO_BUILD}'}) env.Program(['hello.c']) admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q gcc -o hello.o -c -DHELLO=2 hello.c gcc -o hello.exe hello.o同時可以使用 BoolVariable 類來創建一個變量,使用 Add 來添加到當前環境中。三個參數依次是: 參數名稱, 幫助信息, 默認值。bool變量可以是 True 和 False 兩個值。 yes,y,t,True 都認為是 True, no,n,f,False 都認為是 False。
vars = Variables('var1.py', ARGUMENTS) vars.Add(BoolVariable('HELLO_BUILD', help='Set to build for release', default=0)) env = Environment(variables=vars, CPPDEFINES={'HELLO': '${HELLO_BUILD}'}) env.Program(['hello.c']) admin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q HELLO_BUILD=t gcc -o hello.o -c -DHELLO=True hello.cadmin@DESKTOP-NQU1HUV C:\Users\admin\Desktop\scons\day6 $ scons -Q HELLO_BUILD=yes gcc -o hello.o -c -DHELLO=True hello.c gcc -o hello.exe hello.o初此之外,還有其他類來實現變量的創建,下面簡單介紹:
- EnumVariable
ignorecase 的使用方法:
當值為1的時候,忽略命令行參數值的大小寫,且將命令行參數直接添加到構建環境內。
ignorecase = 1 ==> scons -Q COLOR=Red foo.o cc -o foo.o -c -DCOLOR="Red" foo.c
當值為2的時候,忽略命令行參數值的大小寫,且將命令行參數轉為全小寫添加到構建環境內。
ignorecase = 2 ==> scons -Q COLOR=Red foo.o cc -o foo.o -c -DCOLOR="red" foo.c
- ListVariable
可以使用 ListVariable 一次增加多個選項。具體應用如下:
vars = Variables('custom.py') vars.Add(ListVariable('COLORS', help='List of colors', default=0, names=['red', 'green', 'blue']) ) env = Environment(variables=vars, CPPDEFINES={'COLORS': '"${COLORS}"'}) env.Program('foo.c')執行結果:
% scons -Q COLORS=red,blue foo.o cc -o foo.o -c -DCOLORS="red -Dblue" foo.c % scons -Q COLORS=blue,green,red foo.o cc -o foo.o -c -DCOLORS="blue -Dgreen -Dred" foo.c- PathVariable
可以使用 PathVariable 來方便的創建構建變量的文件。例如如果選喲創建一個配置文件,來包含需要的選項,則按照一下操作:
創建 config.cfg 文件,內容為選項值:
COLOR=red vars = Variables('custom.py') vars.Add(PathVariable('CONFIG',help='Path to configuration file',default='config.cfg',validator=PathVariable.PathIsFile,) ) env = Environment(variables=vars, CPPDEFINES={'CONFIG_FILE': '"$CONFIG"'}) env.Program('foo.c')執行過程:
% scons -Q foo.o cc -o foo.o -c -DCONFIG_FILE="config.cfg" foo.c % scons -Q CONFIG=./config.cfg foo.o scons: 'foo.o' is up to date.其中 validator 可以設置驗證函數,驗證功能函數有以下幾個:
如果想確保任何指定路徑實際上都需要是文件而不是目錄,請使用 PathVariable.PathIsFile 作為驗證函數。
如果是目錄,則需要使用 PathVariable.PathIsDir ,并修改下面傳入參數 env = Environment(variables=vars, CPPDEFINES={'DBDIR': '"$DBDIR"'}) 。
如果目錄不存在,且需要創建,則需要使用 PathVariable.PathIsDirCreate ,并修改下面傳入函數 env = Environment(variables=vars, CPPDEFINES={'DBDIR': '"$DBDIR"'}) 。
如果不關心輸入的是文件還是目錄是否存在,則需要使用 PathVariable.PathAccept
-
PackageVariable
后面詳細介紹 -
UnknownVariables
命令行目標
SCons 提供了一個 COMMAND_LINE_TARGETS 變量來保存命令行中的目標參數,如果需要的話,可以對目標進行檢查,做出對應的處理。
下面舉例說明: 對 hello 目標的顯示構建,在構建過程中顯示,提示開發者。
但是實際執行,一直在出錯,暫時不做測試,后期進行補充。
Default 默認構建目標
SCons 在默認狀態下,會對構建腳本中所有目標進行構建,如果需要默認構建一個目標,則需要使用 Default 來指定構建目標。
沒有測試出來,后面進行補充
總結
以上是生活随笔為你收集整理的SCons教程(6) 环境的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我的编程之路点滴记录(四)
- 下一篇: R pdf大小_免费、开源、多平台支持的