5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 &SCons; supports a lot of additional functionality
29 that doesn't readily fit into the other chapters.
34 <title>Verifying the Python Version: the &EnsurePythonVersion; Function</title>
38 Although the &SCons; code itself will run
39 on any Python version 1.5.2 or later,
40 you are perfectly free to make use of
41 Python syntax and modules from more modern versions
42 (for example, Python 2.4 or 2.5)
43 when writing your &SConscript; files
44 or your own local modules.
45 If you do this, it's usually helpful to
46 configure &SCons; to exit gracefully with an error message
47 if it's being run with a version of Python
48 that simply won't work with your code.
49 This is especially true if you're going to use &SCons;
50 to build source code that you plan to distribute publicly,
51 where you can't be sure of the Python version
52 that an anonymous remote user might use
53 to try to build your software.
59 &SCons; provides an &EnsurePythonVersion; function for this.
60 You simply pass it the major and minor versions
61 numbers of the version of Python you require:
67 TODO: Figure out how to generate the error message
68 regardless of executing Python version by faking out
69 the infrastructure in some way.
71 <scons_example name="EnsurePythonVersion">
72 <file name="SConstruct" printme="1">
73 EnsurePythonVersion(2, 5)
80 EnsurePythonVersion(2, 5)
85 And then &SCons will exit with the following error
86 message when a user runs it with an unsupported
87 earlier version of Python:
93 TODO: Figure out how to generate the error message
94 regardless of executing Python version by faking out
95 the infrastructure in some way.
97 <scons_output example="EnsurePythonVersion">
98 <scons_output_command>scons -Q</scons_output_command>
104 % <userinput>scons -Q</userinput>
105 Python 2.5 or greater required, but you have Python 2.3.6
111 <title>Verifying the SCons Version: the &EnsureSConsVersion; Function</title>
115 You may, of course, write your &SConscript; files
116 to use features that were only added in
117 recent versions of &SCons;.
118 When you publicly distribute software that is built using &SCons;,
119 it's helpful to have &SCons;
120 verify the version being used and
121 exit gracefully with an error message
122 if the user's version of &SCons; won't work
123 with your &SConscript; files.
124 &SCons; provides an &EnsureSConsVersion; function
125 that verifies the version of &SCons;
127 the &EnsurePythonVersion; function
128 verifies the version of Python,
129 by passing in the major and minor versions
130 numbers of the version of SCons you require:
136 TODO: Figure out how to generate the error message
137 regardless of executing SCons version by faking out
138 the infrastructure in some way.
140 <scons_example name="EnsureSConsVersion">
141 <file name="SConstruct" printme="1">
142 EnsureSConsVersion(1, 0)
149 EnsureSConsVersion(1, 0)
154 And then &SCons will exit with the following error
155 message when a user runs it with an unsupported
156 earlier version of &SCons;:
162 TODO: Figure out how to generate the error message
163 regardless of executing SCons version by faking out
164 the infrastructure in some way.
166 <scons_output example="EnsureSConsVersion">
167 <scons_output_command>scons -Q</scons_output_command>
173 % <userinput>scons -Q</userinput>
174 SCons 1.0 or greater required, but you have SCons 0.98.5
180 <title>Explicitly Terminating &SCons; While Reading &SConscript; Files: the &Exit; Function</title>
184 &SCons; supports an &Exit; function
185 which can be used to terminate &SCons;
186 while reading the &SConscript; files,
187 usually because you've detected a condition
188 under which it doesn't make sense to proceed:
192 <scons_example name="Exit">
193 <file name="SConstruct" printme="1">
194 if ARGUMENTS.get('FUTURE'):
195 print "The FUTURE option is not supported yet!"
198 env.Program('hello.c')
200 <file name="hello.c">
205 <scons_output example="Exit">
206 <scons_output_command>scons -Q FUTURE=1</scons_output_command>
207 <scons_output_command>scons -Q</scons_output_command>
212 The &Exit; function takes as an argument
213 the (numeric) exit status that you want &SCons; to exit with.
214 If you don't specify a value,
215 the default is to exit with <literal>0</literal>,
216 which indicates successful execution.
222 Note that the &Exit; function
223 is equivalent to calling the Python
224 <function>sys.exit</function> function
225 (which the it actually calls),
226 but because &Exit; is a &SCons; function,
227 you don't have to import the Python
228 <literal>sys</literal> module to use it.
235 <title>Handling Nested Lists: the &Flatten; Function</title>
239 &SCons; supports a &Flatten; function
240 which takes an input Python sequence
242 and returns a flattened list
243 containing just the individual elements of
245 This can be handy when trying to examine
246 a list composed of the lists
247 returned by calls to various Builders.
248 For example, you might collect
249 object files built in different ways
250 into one call to the &Program; Builder
251 by just enclosing them in a list, as follows:
255 <scons_example name="Flatten1">
256 <file name="SConstruct" printme="1">
259 Object('prog2.c', CCFLAGS='-DFOO'),
263 <file name="prog1.c">
266 <file name="prog2.c">
273 Because the Builder calls in &SCons;
274 flatten their input lists,
275 this works just fine to build the program:
279 <scons_output example="Flatten1">
280 <scons_output_command>scons -Q</scons_output_command>
285 But if you were debugging your build
286 and wanted to print the absolute path
287 of each object file in the
288 <varname>objects</varname> list,
289 you might try the following simple approach,
290 trying to print each Node's
291 <literal>abspath</literal>
296 <scons_example name="Flatten2">
297 <file name="SConstruct" printme="1">
300 Object('prog2.c', CCFLAGS='-DFOO'),
304 for object_file in objects:
305 print object_file.abspath
307 <file name="prog1.c">
310 <file name="prog2.c">
317 This does not work as expected
318 because each call to <function>str</function>
319 is operating an embedded list returned by
321 not on the underlying Nodes within those lists:
325 <scons_output example="Flatten2">
326 <scons_output_command>scons -Q</scons_output_command>
331 The solution is to use the &Flatten; function
332 so that you can pass each Node to
333 the <function>str</function> separately:
337 <scons_example name="Flatten3">
338 <file name="SConstruct" printme="1">
341 Object('prog2.c', CCFLAGS='-DFOO'),
345 for object_file in Flatten(objects):
346 print object_file.abspath
348 <file name="prog1.c">
351 <file name="prog2.c">
358 TODO: can't use this now because it displays the temporary path name
360 <scons_output example="Flatten3">
361 <scons_output_command>scons -Q</scons_output_command>
367 % <userinput>scons -Q</userinput>
368 /home/me/project/prog1.o
369 /home/me/project/prog2.o
370 cc -o prog1.o -c prog1.c
371 cc -o prog2.o -c -DFOO prog2.c
372 cc -o prog1 prog1.o prog2.o
378 <title>Finding the Invocation Directory: the &GetLaunchDir; Function</title>
382 If you need to find the directory from
383 which the user invoked the &scons; command,
384 you can use the &GetLaunchDir; function:
390 LAUNCHDIR = GetLaunchDir(),
392 env.Command('directory_build_info',
393 '$LAUNCHDIR/build_info'
394 Copy('$TARGET', '$SOURCE'))
399 Because &SCons; is usually invoked from the top-level
400 directory in which the &SConstruct; file lives,
401 the Python <function>os.getcwd()</function>
404 <literal>-u</literal>,
405 <literal>-U</literal>
407 <literal>-D</literal>
408 command-line options,
409 when invoked from a subdirectory,
410 will cause &SCons; to change to the directory
411 in which the &SConstruct; file is found.
412 When those options are used,
413 &GetLaunchDir; will still return the path to the
414 user's invoking subdirectory,
415 allowing the &SConscript; configuration
416 to still get at configuration (or other) files
417 from the originating directory.