所有的檔案與目錄在SCons中都被視為"Nodes",善用"Node"可以使您的SConscript文件可移植且易於閱讀。
5.1. Builder Methods Return Lists of Target Nodes
所有builder methods都回傳一個Node list,用於辨識要構建的目標文件。這Node list可作為參數傳遞給其他builder methods。brook@vista:~/scons/04.7$ cat SConstruct a_list = Object('a.c', CCFLAGS='-DHELLO') b_list = Object('b.c', CCFLAGS='-DWORLD') Program(a_list + b_list) brook@vista:~/scons/04.7$ scons -Q gcc -o a.o -c -DHELLO a.c gcc -o b.o -c -DWORLD b.c gcc -o a a.o b.o
5.2. Explicitly Creating File and Directory Nodes
SCons的File()與Dir()可回傳file或directory Node,而Entry()可回傳file或directory Node。hello_c = File('hello.c') Program(hello_c) classes = Dir('classes') Java(classes, 'src') xyzzy = Entry('xyzzy')
5.3. Printing Node File Names
您可以對Node執行print,打Node所代表的檔名。brook@vista:~/scons/05.3$ cat SConstruct object_list = Object(['a.c', 'b.c']) program_list = Program(object_list) print("The object file is: %s"%object_list) print("The program file is: %s"%program_list[0]) brook@vista:~/scons/05.3$ scons -Q The object file is: ['a.o', 'b.o'] The program file is: a scons: `.' is up to date.
5.4. Using a Node's File Name as a String
您可以使用Python的str函數將Node轉成字串加以處理。brook@vista:~/scons/05.5$ cat SConstruct import os object_list = Object(['a.c', 'b.c']) obj_str = str(object_list[0]) print("The object file is: %s"% obj_str) if not os.path.exists(obj_str): print("%s does not exist!"% obj_str) else: print("%s exist!"% obj_str) brook@vista:~/scons/05.5$ scons -Q The object file is: a.o a.o exist! scons: `.' is up to date.
5.5. GetBuildPath: Getting the Path From a Node or String
env.GetBuildPath(file_or_list)可以用來取得file/Node的路徑。brook@vista:~/scons/05.6$ cat SConstruct object_list = Object(['a.c', 'b.c']) env=Environment(VAR="value") n=File("foo.c") print(env.GetBuildPath([n, "sub/dir/$VAR", '/tmp'] + object_list)) brook@vista:~/scons/05.6$ scons -Q ['foo.c', 'sub/dir/value', '/tmp', 'a.o', 'b.o'] scons: `.' is up to date.
-
參考資料:
- SCons 3.0.3 User Guide