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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
<!--
__COPYRIGHT__
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<para>
Internally, &SCons; represents all of the files
and directories it knows about as &Nodes;.
These internal objects
(not object <emphasis>files</emphasis>)
can be used in a variety of ways
to make your &SConscript;
files portable and easy to read.
</para>
<section>
<title>Builder Methods Return Lists of Target Nodes</title>
<para>
All builder methods return a list of
&Node; objects that identify the
target file or files that will be built.
These returned &Nodes; can be passed
as source files to other builder methods,
</para>
<para>
For example, suppose that we want to build
the two object files that make up a program with different options.
This would mean calling the &b-link-Object;
builder once for each object file,
specifying the desired options:
</para>
<programlisting>
Object('hello.c', CCFLAGS='-DHELLO')
Object('goodbye.c', CCFLAGS='-DGOODBYE')
</programlisting>
<para>
One way to combine these object files
into the resulting program
would be to call the &b-link-Program;
builder with the names of the object files
listed as sources:
</para>
<programlisting>
Object('hello.c', CCFLAGS='-DHELLO')
Object('goodbye.c', CCFLAGS='-DGOODBYE')
Program(['hello.o', 'goodbye.o'])
</programlisting>
<para>
The problem with listing the names as strings
is that our &SConstruct; file is no longer portable
across operating systems.
It won't, for example, work on Windows
because the object files there would be
named &hello_obj; and &goodbye_obj;,
not &hello_o; and &goodbye_o;.
</para>
<para>
A better solution is to assign the lists of targets
returned by the calls to the &b-Object; builder to variables,
which we can then concatenate in our
call to the &b-Program; builder:
</para>
<programlisting>
hello_list = Object('hello.c', CCFLAGS='-DHELLO')
goodbye_list = Object('goodbye.c', CCFLAGS='-DGOODBYE')
Program(hello_list + goodbye_list)
</programlisting>
<para>
This makes our &SConstruct; file portable again,
the build output on Linux looking like:
</para>
<screen>
% <userinput>scons -Q</userinput>
cc -DGOODBYE -c -o goodbye.o goodbye.c
cc -DHELLO -c -o hello.o hello.c
cc -o hello hello.o goodbye.o
</screen>
<para>
And on Windows:
</para>
<screen>
C:\><userinput>scons -Q</userinput>
cl -DGOODBYE /c goodbye.c /Fogoodbye.obj
cl -DHELLO /c hello.c /Fohello.obj
link /nologo /OUT:hello.exe hello.obj goodbye.obj
</screen>
<para>
We'll see examples of using the list of nodes
returned by builder methods throughout
the rest of this guide.
</para>
</section>
<section>
<title>Explicitly Creating File and Directory Nodes</title>
<para>
It's worth mentioning here that
&SCons; maintains a clear distinction
between Nodes that represent files
and Nodes that represent directories.
&SCons; supports &File; and &Dir;
functions that, repectively,
return a file or directory Node:
</para>
<programlisting>
hello_c = File('hello.c')
Program(hello_c)
classes = Dir('classes')
Java(classes, 'src')
</programlisting>
<para>
Normally, you don't need to call
&File; or &Dir; directly,
because calling a builder method automatically
treats strings as the names of files or directories,
and translates them into
the Node objects for you.
The &File; and &Dir; functions can come in handy
in situations where you need to explicitly
instruct &SCons; about the type of Node being
passed to a builder or other function,
or unambiguously refer to a specific
file in a directory tree.
<!--
(For an example of when you might
need to use &File; or &Dir; to
prevent ambiguous interpretation of a string
naming a file or directory, see
<xref linkend="chap-hierarchy">.)
-->
</para>
<para>
There are also times when you may need to
refer to an entry in a file system
without knowing in advance
whether it's a file or a directory.
For those situations,
&SCons; also supports an &Entry; function,
which returns a Node
that can represent either a file or a directory.
</para>
<programlisting>
xyzzy = Entry('xyzzy')
</programlisting>
<para>
The returned <literal>xyzzy</literal> Node
will be turned into a file or directory Node
the first time it is used by a builder method
or other function that
requires one vs. the other.
</para>
</section>
<section>
<title>Printing &Node; File Names</title>
<para>
One of the most common things you can do
with a Node is use it to print the
file name that the node represents.
For example, the following &SConstruct; file:
</para>
<programlisting>
hello_c = File('hello.c')
Program(hello_c)
classes = Dir('classes')
Java(classes, 'src')
object_list = Object('hello.c')
program_list = Program(object_list)
print "The object file is:", object_list[0]
print "The program file is:", program_list[0]
</programlisting>
<para>
Would print the following file names on a POSIX system:
</para>
<screen>
% <userinput>scons -Q</userinput>
The object file is: hello.o
The program file is: hello
cc -c -o hello.o hello.c
cc -o hello hello.o
</screen>
<para>
And the following file names on a Windows system:
</para>
<screen>
C:\><userinput>scons -Q</userinput>
The object file is: hello.obj
The program file is: hello.exe
cl /nologo /c hello.c /Fohello.obj
link /nologo /OUT:hello.exe hello.obj
</screen>
</section>
<section>
<title>Using a &Node;'s File Name as a String</title>
<para>
Printing a &Node;'s name
as described in the previous section
works because the string representation of a &Node;
is the name of the file.
If you want to do something other than
print the name of the file,
you can fetch it by using the builtin Python
&str; function.
For example, if you want to use the Python
<function>os.path.exists</function>
to figure out whether a file
exists while the &SConstruct; file
is being read and executed,
you can fetch the string as follows:
</para>
<programlisting>
import os.path
program_list = Program('hello.c')
program_name = str(program_list[0])
if not os.path.exists(program_name):
print program_name, "does not exist!"
</programlisting>
<para>
Which executes as follows on a POSIX system:
</para>
<screen>
% <userinput>scons -Q</userinput>
The object file is: hello.o
The program file is: hello
cc -c -o hello.o hello.c
cc -o hello hello.o
</screen>
</section>
<!--
<section>
<title>Fetching the Contents of a &Node;</title>
<para>
XXX Describe using read() and readlines()
when we add that as a public interface.
</para>
<scons_example name="exists">
<file name="SConstruct" printme="1">
hello_c = File('hello.c')
contents = hello_c.read()
print "contents are:"
print contents
</file>
<file name="hello.c">
int main() { printf("Hello, world!\n"); }
</file>
</scons_example>
<para>
Which executes as follows on a POSIX system:
</para>
<scons_output example="print" os="posix">
<scons_output_command>scons -Q</scons_output_command>
</scons_output>
</section>
-->
|