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.
29 you will see several examples of
30 very simple build configurations using &SCons;,
31 which will demonstrate how easy
32 it is to use &SCons; to
33 build programs from several different programming languages
34 on different types of systems.
39 <title>Specifying the Name of the Target (Output) File</title>
43 You've seen that when you call the &b-link-Program; builder method,
44 it builds the resulting program with the same
45 base name as the source file.
46 That is, the following call to build an
47 executable program from the &hello_c; source file
48 will build an executable program named &hello; on POSIX systems,
49 and an executable program named &hello_exe; on Windows systems:
59 If you want to build a program with
60 a different name than the base of the source file name,
61 you simply put the target file name
62 to the left of the source file name:
66 <scons_example name="target">
67 <file name="SConstruct" printme="1">
68 Program('new_hello', 'hello.c')
71 int main() { printf("Hello, world!\n"); }
77 (&SCons; requires the target file name first,
78 followed by the source file name,
79 so that the order mimics that of an
80 assignment statement in most programming languages,
82 <literal>"program = source files"</literal>.)
88 Now &SCons; will build an executable program
89 named &new_hello; when run on a POSIX system:
93 <scons_output example="target" os="posix">
94 <scons_output_command>scons -Q</scons_output_command>
99 And &SCons; will build an executable program
100 named &new_hello_exe; when run on a Windows system:
104 <scons_output example="target" os="win32">
105 <scons_output_command>scons -Q</scons_output_command>
111 <title>Compiling Multiple Source Files</title>
115 You've just seen how to configure &SCons;
116 to compile a program from a single source file.
117 It's more common, of course,
118 that you'll need to build a program from
119 many input source files, not just one.
120 To do this, you need to put the
121 source files in a Python list
122 (enclosed in square brackets),
127 <scons_example name="ex2">
128 <file name="SConstruct" printme="1">
129 Program(['prog.c', 'file1.c', 'file2.c'])
132 int main() { printf("prog.c\n"); }
134 <file name="file1.c">
135 void file1() { printf("file1.c\n"); }
137 <file name="file2.c">
138 void file2() { printf("file2.c\n"); }
144 A build of the above example would look like:
148 <scons_output example="ex2">
149 <scons_output_command>scons -Q</scons_output_command>
155 deduces the output program name
156 from the first source file specified
157 in the list--that is,
158 because the first source file was &prog_c;,
159 &SCons; will name the resulting program &prog;
160 (or &prog_exe; on a Windows system).
161 If you want to specify a different program name,
162 then (as we've seen in the previous section)
163 you slide the list of source files
165 to make room for the output program file name.
166 (&SCons; puts the output file name to the left
167 of the source file names
168 so that the order mimics that of an
169 assignment statement: "program = source files".)
170 This makes our example:
174 <scons_example name="ex3">
175 <file name="SConstruct" printme="1">
176 Program('program', ['prog.c', 'file1.c', 'file2.c'])
179 int main() { printf("prog.c\n"); }
181 <file name="file1.c">
182 void file1() { printf("file1.c\n"); }
184 <file name="file2.c">
185 void file2() { printf("file2.c\n"); }
191 On Linux, a build of this example would look like:
195 <scons_output example="ex3" os="posix">
196 <scons_output_command>scons -Q</scons_output_command>
205 <scons_output example="ex3" os="win32">
206 <scons_output_command>scons -Q</scons_output_command>
212 <title>Making a list of files with Glob()</title>
216 You can also use the Glob() function to find all files matching a
217 certain template, using standard the shell pattern matching
218 characters *, ?, and [abc] to match any of a, b, or c. [!abc] is
219 also supported, to match any character <emphasis>except</emphasis>
220 a, b, or c. This makes many multi-source-file builds quite easy:
225 Program('program', Glob('*.c'))
230 The SCons man page has more details on using Glob() with Variant
231 dirs and Repositories and returning strings rather than Nodes.
238 <title>Specifying Single Files Vs. Lists of Files</title>
242 We've now shown you two ways to specify
243 the source for a program,
244 one with a list of files:
249 Program('hello', ['file1.c', 'file2.c'])
254 And one with a single file:
259 Program('hello', 'hello.c')
264 You could actually put a single file name in a list, too,
265 which you might prefer just for the sake of consistency:
270 Program('hello', ['hello.c'])
275 &SCons; functions will accept a single file name in either form.
276 In fact, internally, &SCons; treats all input as lists of files,
277 but allows you to omit the square brackets
278 to cut down a little on the typing
279 when there's only a single file name.
287 Although &SCons; functions
288 are forgiving about whether or not you
289 use a string vs. a list for a single file name,
290 Python itself is more strict about
291 treating lists and strings differently.
292 So where &SCons; allows either
298 # The following two calls both work correctly:
299 Program('program1', 'program1.c')
300 Program('program2', ['program2.c'])
305 Trying to do "Python things" that mix strings and
306 lists will cause errors or lead to incorrect results:
311 common_sources = ['file1.c', 'file2.c']
313 # THE FOLLOWING IS INCORRECT AND GENERATES A PYTHON ERROR
314 # BECAUSE IT TRIES TO ADD A STRING TO A LIST:
315 Program('program1', common_sources + 'program1.c')
317 # The following works correctly, because it's adding two
318 # lists together to make another list.
319 Program('program2', common_sources + ['program2.c'])
327 <title>Making Lists of Files Easier to Read</title>
331 One drawback to the use of a Python list
332 for source files is that
333 each file name must be enclosed in quotes
334 (either single quotes or double quotes).
335 This can get cumbersome and difficult to read
336 when the list of file names is long.
337 Fortunately, &SCons; and Python provide a number of ways
339 the &SConstruct; file stays easy to read.
345 To make long lists of file names
346 easier to deal with, &SCons; provides a
348 that takes a quoted list of file names,
349 with the names separated by spaces or other white-space characters,
350 and turns it into a list of separate file names.
351 Using the &Split; function turns the
352 previous example into:
357 Program('program', Split('main.c file1.c file2.c'))
362 (If you're already familiar with Python,
363 you'll have realized that this is similar to the
364 <function>split()</function> method
365 in the Python standard <function>string</function> module.
366 Unlike the <function>string.split()</function> method,
367 however, the &Split; function
368 does not require a string as input
369 and will wrap up a single non-string object in a list,
370 or return its argument untouched if it's already a list.
371 This comes in handy as a way to make sure
372 arbitrary values can be passed to &SCons; functions
373 without having to check the type of the variable by hand.)
379 Putting the call to the &Split; function
380 inside the &b-Program; call
381 can also be a little unwieldy.
382 A more readable alternative is to
383 assign the output from the &Split; call
385 and then use the variable when calling the
386 &b-Program; function:
391 list = Split('main.c file1.c file2.c')
392 Program('program', list)
397 Lastly, the &Split; function
398 doesn't care how much white space separates
399 the file names in the quoted string.
400 This allows you to create lists of file
401 names that span multiple lines,
402 which often makes for easier editing:
407 list = Split("""main.c
410 Program('program', list)
415 (Note in this example that we used
416 the Python "triple-quote" syntax,
417 which allows a string to contain
419 The three quotes can be either
420 single or double quotes.)
427 <title>Keyword Arguments</title>
431 &SCons; also allows you to identify
432 the output file and input source files
433 using Python keyword arguments.
434 The output file is known as the
435 <emphasis>target</emphasis>,
436 and the source file(s) are known (logically enough) as the
437 <emphasis>source</emphasis>.
438 The Python syntax for this is:
443 list = Split('main.c file1.c file2.c')
444 Program(target = 'program', source = list)
449 Because the keywords explicitly identify
450 what each argument is,
451 you can actually reverse the order if you prefer:
456 list = Split('main.c file1.c file2.c')
457 Program(source = list, target = 'program')
462 Whether or not you choose to use keyword arguments
463 to identify the target and source files,
464 and the order in which you specify them
466 are purely personal choices;
467 &SCons; functions the same regardless.
474 <title>Compiling Multiple Programs</title>
478 In order to compile multiple programs
479 within the same &SConstruct; file,
480 simply call the &Program; method
482 once for each program you need to build:
486 <scons_example name="ex4">
487 <file name="SConstruct" printme="1">
489 Program('bar', ['bar1.c', 'bar2.c'])
492 int main() { printf("foo.c\n"); }
495 int main() { printf("bar1.c\n"); }
498 void bar2() { printf("bar2.c\n"); }
504 &SCons; would then build the programs as follows:
508 <scons_output example="ex4">
509 <scons_output_command>scons -Q</scons_output_command>
514 Notice that &SCons; does not necessarily build the
515 programs in the same order in which you specify
516 them in the &SConstruct; file.
517 &SCons; does, however, recognize that
518 the individual object files must be built
519 before the resulting program can be built.
520 We'll discuss this in greater detail in
521 the "Dependencies" section, below.
528 <title>Sharing Source Files Between Multiple Programs</title>
532 It's common to re-use code by sharing source files
533 between multiple programs.
534 One way to do this is to create a library
535 from the common source files,
536 which can then be linked into resulting programs.
537 (Creating libraries is discussed in
538 <xref linkend="chap-libraries"></xref>, below.)
544 A more straightforward, but perhaps less convenient,
545 way to share source files between multiple programs
546 is simply to include the common files
547 in the lists of source files for each program:
551 <scons_example name="ex5">
552 <file name="SConstruct" printme="1">
553 Program(Split('foo.c common1.c common2.c'))
554 Program('bar', Split('bar1.c bar2.c common1.c common2.c'))
557 int main() { printf("foo.c\n"); }
560 int main() { printf("bar1.c\n"); }
563 int bar2() { printf("bar2.c\n"); }
565 <file name="common1.c">
566 void common1() { printf("common1.c\n"); }
568 <file name="common2.c">
569 void common22() { printf("common2.c\n"); }
575 &SCons; recognizes that the object files for
576 the &common1_c; and &common2_c; source files
577 each only need to be built once,
578 even though the resulting object files are
579 each linked in to both of the resulting executable programs:
583 <scons_output example="ex5">
584 <scons_output_command>scons -Q</scons_output_command>
589 If two or more programs
590 share a lot of common source files,
591 repeating the common files in the list for each program
592 can be a maintenance problem when you need to change the
593 list of common files.
594 You can simplify this by creating a separate Python list
595 to hold the common file names,
596 and concatenating it with other lists
597 using the Python + operator:
602 common = ['common1.c', 'common2.c']
603 foo_files = ['foo.c'] + common
604 bar_files = ['bar1.c', 'bar2.c'] + common
605 Program('foo', foo_files)
606 Program('bar', bar_files)
611 This is functionally equivalent to the previous example.