3 <!ENTITY % scons SYSTEM "../scons.mod">
7 <chapter id="chap-engine"
8 xmlns="http://www.scons.org/dbxsd/v1.0"
9 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10 xsi:schemaLocation="http://www.scons.org/dbxsd/v1.0 http://www.scons.org/dbxsd/v1.0/scons.xsd">
11 <title>Build Engine API</title>
17 Permission is hereby granted, free of charge, to any person obtaining
18 a copy of this software and associated documentation files (the
19 "Software"), to deal in the Software without restriction, including
20 without limitation the rights to use, copy, modify, merge, publish,
21 distribute, sublicense, and/or sell copies of the Software, and to
22 permit persons to whom the Software is furnished to do so, subject to
23 the following conditions:
25 The above copyright notice and this permission notice shall be included
26 in all copies or substantial portions of the Software.
28 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
29 KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
30 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 <section id="sect-principles">
39 <title>General Principles</title>
42 <title>Keyword arguments</title>
46 All methods and functions in this API will support the use of keyword
47 arguments in calls, for the sake of explicitness and readability.
48 For brevity in the hands of experts, most methods and functions
49 will also support positional arguments for their most-commonly-used
50 arguments. As an explicit example, the following two lines will each
51 arrange for an executable program named <filename>foo</filename> (or
52 <filename>foo.exe</filename> on a Win32 system) to be compiled from
53 the <filename>foo.c</filename> source file:
58 env.Program(target = 'foo', source = 'foo.c')
60 env.Program('foo', 'foo.c')
66 <title>Internal object representation</title>
70 All methods and functions use internal (Python) objects that
71 represent the external objects (files, for example) for which they
72 perform dependency analysis.
78 All methods and functions in this API that accept an external object
79 as an argument will accept <emphasis>either</emphasis> a string
80 description or an object reference. For example, the two following
81 two-line examples are equivalent:
86 env.Object(target = 'foo.o', source = 'foo.c')
87 env.Program(target = 'foo', 'foo.o') # builds foo from foo.o
89 foo_obj = env.Object(target = 'foo.o', source = 'foo.c')
90 env.Program(target = 'foo', foo_obj) # builds foo from foo.o
99 <section id="sect-envs">
100 <title>&ConsEnvs;</title>
104 A &consenv; is the basic means by which a software system interacts
105 with the &SCons; Python API to control a build process.
111 A &consenv; is an object with associated methods for generating target
112 files of various types (&Builder; objects), other associated object
113 methods for automatically determining dependencies from the contents
114 of various types of source files (&Scanner; objects), and a dictionary
115 of values used by these methods.
121 Passing no arguments to the &Environment; instantiation creates a
122 &consenv; with default values for the current platform:
131 <title>&Consvars;</title>
135 A &consenv; has an associated dictionary of &consvars; that control how
136 the build is performed. By default, the &Environment; method creates
137 a &consenv; with values that make most software build "out of the box"
138 on the host system. These default values will be generated at the
139 time &SCons; is installed using functionality similar to that provided
143 It would be nice if we could avoid re-inventing the wheel here by
144 using some other Python-based tool &Autoconf; replacement--like what
145 was supposed to come out of the Software Carpentry configuration
146 tool contest. It will probably be most efficient to roll our own
147 logic initially and convert if something better does come along.
150 At a minimum, there will be pre-configured sets of default values
151 that will provide reasonable defaults for UNIX and Windows NT.
157 The default &consenv; values may be overridden when a new &consenv; is
158 created by specifying keyword arguments:
163 env = Environment(CC = 'gcc',
165 CPPPATH = ['.', 'src', '/usr/include'],
166 LIBPATH = ['/usr/lib', '.'])
172 <title>Fetching &consvars;</title>
176 A copy of the dictionary of &consvars; can be returned using
177 the &Dictionary; method:
183 dict = env.Dictionary()
188 If any arguments are supplied, then just the corresponding value(s)
194 ccflags = env.Dictionary('CCFLAGS')
195 cc, ld = env.Dictionary('CC', 'LD')
201 <title>Copying a &consenv;</title>
205 A method exists to return a copy of an existing environment, with
206 any overridden values specified as keyword arguments to the method:
212 debug = env.Copy(CCFLAGS = '-g')
218 <title>Multiple &consenvs;</title>
222 Different external objects often require different build
223 characteristics. Multiple &consenvs; may be defined, each with
229 env = Environment(CCFLAGS = '')
230 debug = Environment(CCFLAGS = '-g')
231 env.Make(target = 'hello', source = 'hello.c')
232 debug.Make(target = 'hello-debug', source = 'hello.c')
237 Dictionaries of values from multiple &consenvs; may be passed to the
238 &Environment; instantiation or the &Copy; method, in which case the
239 last-specified dictionary value wins:
244 env1 = Environment(CCFLAGS = '-O', LDFLAGS = '-d')
245 env2 = Environment(CCFLAGS = '-g')
246 new = Environment(env1.Dictionary(), env2.Dictionary())
251 The <varname>new</varname> environment in the above example retains
252 <literal>LDFLAGS = '-d'</literal> from the <varname>env1</varname>
253 environment, and <literal>CCFLAGS = '-g'</literal> from the
254 <varname>env2</varname> environment.
262 OS environment variables
263 compilers and options,
264 aliases for commands,
267 environment overrides a la Cons
271 cross compilation via selection of tool+options
273 paths for header files (specify alternate path)
275 accomodate smart compilers that can tell you
276 "I know how to turn .c or .ccp into .o",
277 "I know how to turn .f into .o"
284 <title>Variable substitution</title>
288 Within a construction command, any variable from the &consenv; may
289 be interpolated by prefixing the name of the construction with
295 MyBuilder = Builder(command = "$XX $XXFLAGS -c $_INPUTS -o $target")
297 env.Command(targets = 'bar.out', sources = 'bar.in',
298 command = "sed '1d' < $source > $target")
303 Variable substitution is recursive: the command line is expanded
304 until no more substitutions can be made.
310 Variable names following the <symbol>$</symbol> may be enclosed in
311 braces. This can be used to concatenate an interpolated value with an
312 alphanumeric character:
317 VerboseBuilder = Builder(command = "$XX -${XXFLAGS}v > $target")
322 The variable within braces may contain a pair of parentheses
323 after a Python function name to be evaluated (for example,
324 <literal>${map()}</literal>). &SCons; will interpolate the return
325 value from the function (presumably a string):
330 env = Environment(FUNC = myfunc)
331 env.Command(target = 'foo.out', source = 'foo.in',
332 command = "${FUNC($<)}")
337 If a referenced variable is not defined in the &consenv;,
338 the null string is interpolated.
344 The following special variables can also be used:
351 <term><literal>$targets</literal></term>
355 All target file names. If multiple targets are specified in an
356 array, <literal>$targets</literal> expands to the entire list of
357 targets, separated by a single space.
363 Individual targets from a list may be extracted by enclosing
364 the <literal>targets</literal> keyword in braces and using the
365 appropriate Python array index or slice:
370 ${targets[0]} # expands to the first target
372 ${targets[1:]} # expands to all but the first target
374 ${targets[1:-1]} # expands to all but the first and last targets
381 <term><literal>$target</literal></term>
385 A synonym for <literal>${targets[0]}</literal>, the first target
393 <term><literal>$sources</literal></term>
397 All input file names. Any input file names that
398 are used anywhere else on the current command
399 line (via <literal>${sources[0]}</literal>,
400 <literal>${sources{[1]}</literal>, etc.) are removed from the
411 Any of the above special variables may be enclosed in braces and
412 followed immediately by one of the following attributes to select just
413 a portion of the expanded path name:
420 <term><literal>.base</literal></term>
424 Basename: the directory plus the file name, minus any file suffix.
431 <term><literal>.dir</literal></term>
435 The directory in which the file lives. This is a relative path,
443 <term><literal>.file</literal></term>
447 The file name, minus any directory portion.
454 <term><literal>.suffix</literal></term>
458 The file name suffix (that is, the right-most dot in the file name,
459 and all characters to the right of that).
466 <term><literal>.filebase</literal></term>
470 The file name (no directory portion), minus any file suffix.
477 <term><literal>.abspath</literal></term>
481 The absolute path to the file.
488 <term><literal>.relpath</literal></term>
492 The path to the file relative to the root SConstruct file's directory.
506 <section id="sect-builders">
507 <title>&Builder; Objects</title>
511 By default, &SCons; supplies (and uses) a number of pre-defined
521 <entry>&Object;</entry>
522 <entry>compile or assemble an object file</entry>
526 <entry>&Library;</entry>
527 <entry>archive files into a library</entry>
531 <entry>&SharedLibrary;</entry>
532 <entry>archive files into a shared library</entry>
536 <entry>&Program;</entry>
537 <entry>link objects and/or libraries into an executable</entry>
541 <entry>&MakeBuilder;</entry>
542 <entry>build according to file suffixes; see below</entry>
550 &Library; and &SharedLibrary; have nearly identical
551 semantics, just different
552 tools and &consenvs (paths, etc.) that they use.
553 In other words, you can construct a shared library
554 using just the &Library; &Builder; object
555 with a different environment.
556 I think that's a better way to do it.
562 A &consenv; can be explicitly initialized with associated &Builder;
563 objects that will be bound to the &consenv; object:
568 env = Environment(BUILDERS = ['Object', 'Program'])
573 &Builder; objects bound to a &consenv; can be called directly as
574 methods. When invoked, a &Builder; object returns a (list of) objects
580 obj = env.Object(target ='hello.o', source = 'hello.c')
581 lib = env.Library(target ='libfoo.a',
582 source = ['aaa.c', 'bbb.c'])
583 slib = env.SharedLibrary(target ='libbar.so',
584 source = ['xxx.c', 'yyy.c'])
585 prog = env.Program(target ='hello',
586 source = ['hello.o', 'libfoo.a', 'libbar.so'])
590 <title>Specifying multiple inputs</title>
594 Multiple input files that go into creating a target file may be passed
595 in as a single string, with the individual file names separated by
601 env.Library(target = 'foo.a', source = 'aaa.c bbb.c ccc.c')
602 env.Object(target = 'yyy.o', source = 'yyy.c')
603 env.Program(target = 'bar', source = 'xxx.c yyy.o foo.a')
608 Alternatively, multiple input files that go into creating a target
609 file may be passed in as an array. This allows input files to be
610 specified using their object representation:
615 env.Library(target = 'foo.a', source = ['aaa.c', 'bbb.c', 'ccc.c'])
616 yyy_obj = env.Object(target = 'yyy.o', source = 'yyy.c')
617 env.Program(target = 'bar', source = ['xxx.c', yyy_obj, 'foo.a'])
622 Individual string elements within an array of input files are
623 <emphasis>not</emphasis> further split into white-space separated
624 file names. This allows file names that contain white space to
625 be specified by putting the value into an array:
628 env.Program(target = 'foo', source = ['an input file.c'])
636 <title>Specifying multiple targets</title>
640 Conversely, the generated target may be a string listing multiple
641 files separated by white space:
646 env.Object(target = 'grammar.o y.tab.h', source = 'grammar.y')
651 An array of multiple target files can be used to mix string and object
652 representations, or to accomodate file names that contain white space:
657 env.Program(target = ['my program'], source = 'input.c')
663 <title>File prefixes and suffixes</title>
667 For portability, if the target file name does not already have an
668 appropriate file prefix or suffix, the &Builder; objects will
669 append one appropriate for the file type on the current system:
674 # builds 'hello.o' on UNIX, 'hello.obj' on Windows NT:
675 obj = env.Object(target ='hello', source = 'hello.c')
677 # builds 'libfoo.a' on UNIX, 'foo.lib' on Windows NT:
678 lib = env.Library(target ='foo', source = ['aaa.c', 'bbb.c'])
680 # builds 'libbar.so' on UNIX, 'bar.dll' on Windows NT:
681 slib = env.SharedLibrary(target ='bar', source = ['xxx.c', 'yyy.c'])
683 # builds 'hello' on UNIX, 'hello.exe' on Windows NT:
684 prog = env.Program(target ='hello',
685 source = ['hello.o', 'libfoo.a', 'libbar.so'])
691 <title>&Builder; object exceptions</title>
695 &Builder; objects raise the following exceptions on error:
698 LIST THESE ONCE WE FIGURE OUT WHAT THEY ARE FROM CODING THEM.
705 <title>User-defined &Builder; objects</title>
709 Users can define additional &Builder; objects for specific external
710 object types unknown to &SCons;. A &Builder; object may build its
711 target by executing an external command:
716 WebPage = Builder(command = 'htmlgen $HTMLGENFLAGS $sources > $target',
723 Alternatively, a &Builder; object may also build its target by
724 executing a Python function:
730 # [code to update the object]
733 OtherBuilder1 = Builder(function = update,
734 src_suffix = ['.in', '.input'])
739 An optional argument to pass to the function may be specified:
744 def update_arg(dest, arg):
745 # [code to update the object]
748 OtherBuilder2 = Builder(function = update_arg,
749 function_arg = 'xyzzy',
750 src_suffix = ['.in', '.input'])
755 Both an external command and an internal function may be specified,
756 in which case the function will be called to build the object first,
757 followed by the command line.
762 NEED AN EXAMPLE HERE.
767 User-defined &Builder; objects can be used like the default &Builder;
768 objects to initialize &consenvs;.
773 WebPage = Builder(command = 'htmlgen $HTMLGENFLAGS $sources > $target',
776 env = Environment(BUILDERS = ['WebPage'])
777 env.WebPage(target = 'foo.html', source = 'foo.in')
778 # Builds 'bar.html' on UNIX, 'bar.htm' on Windows NT:
779 env.WebPage(target = 'bar', source = 'bar.in')
784 The command-line specification can interpolate variables from the
785 &consenv;; see "Variable substitution," above.
791 A &Builder; object may optionally be initialized with a list of:
799 the prefix of the target file (e.g., 'lib' for libraries)
807 the suffix of the target file (e.g., '.a' for libraries)
815 the expected suffixes of the input files
816 (e.g., '.o' for object files)
824 These arguments are used in automatic
825 dependency analysis and to generate output file names that don't
826 have suffixes supplied explicitly.
832 <title>Copying &Builder; Objects</title>
836 A &Copy; method exists to return a copy of an existing &Builder;
837 object, with any overridden values specified as keyword arguments to
843 build = Builder(function = my_build)
844 build_out = build.Copy(suffix = '.out')
849 Typically, &Builder; objects will be supplied by a tool-master or
850 administrator through a shared &consenv;.
856 <title>Special-purpose build rules</title>
860 A pre-defined &Command; builder exists to associate a target file with
861 a specific command or list of commands for building the file:
866 env.Command(target = 'foo.out', source =
867 command = 'foo.in', "foo.process $sources > $target")
869 commands = [ "bar.process -o .tmpfile $sources",
870 "mv .tmpfile $target" ]
871 env.Command(target = 'bar.out', source = 'bar.in', command = commands)
875 This is useful when it's too cumbersome to create a &Builder;
876 object just to build a single file in a special way.
882 <title>The &MakeBuilder; &Builder;</title>
886 A pre-defined &Builder; object named &MakeBuilder; exists to make
887 simple builds as easy as possible for users, at the expense of
888 sacrificing some build portability.
894 The following minimal example builds the 'hello' program from the
895 'hello.c' source file:
900 Environment().Make('hello', 'hello.c')
905 Users of the &MakeBuilder; &Builder; object are not required to
906 understand intermediate steps involved in generating a file--for
907 example, the distinction between compiling source code into an object
908 file, and then linking object files into an executable. The details
909 of intermediate steps are handled by the invoked method. Users that
910 need to, however, can specify intermediate steps explicitly:
916 env.Make(target = 'hello.o', source = 'hello.c')
917 env.Make(target = 'hello', source = 'hello.o')
922 The &MakeBuilder; method understands the file suffixes specified and
923 "does the right thing" to generate the target object and program
924 files, respectively. It does this by examining the specified output
925 suffixes for the &Builder; objects bound to the environment.
931 Because file name suffixes in the target and source file names
932 must be specified, the &MakeBuilder; method can't be used
933 portably across operating systems. In other words, for the
934 example above, the &MakeBuilder; builder will not generate
935 <filename>hello.exe</filename> on Windows NT.
942 <title>&Builder; maps</title>
945 Do we even need this anymore?
946 Now that the individual builders
947 have specified <literal>suffix</literal>
948 and <literal>src_suffix</literal> values,
949 all of the information we need to support
950 the &MakeBuilder; builder is right there in the environment.
951 I think this is a holdover from before I
952 added the <literal>suffix</literal> arguments.
953 If you want &MakeBuilder; to do something different,
954 you set it up with another environment...
959 The <function>env.Make</function> method "does the right thing" to
960 build different file types because it uses a dictionary from the
961 &consenv; that maps file suffixes to the appropriate &Builder; object.
962 This &BUILDERMAP; can be initialized at instantiation:
967 env = Environment(BUILDERMAP = {
977 With the &BUILDERMAP; properly initialized, the
978 <function>env.Make</function> method can be used to build additional
984 env.Make(target = 'index.html', source = 'index.input')
989 &Builder; objects referenced in the &BUILDERMAP; do not need to be
990 listed separately in the <literal>BUILDERS</literal> variable. The &consenv; will
991 bind the union of the &Builder; objects listed in both variables.
997 YYY support scanners which detect files which haven't been generated yet
1007 <section id="sect-deps">
1008 <title>Dependencies</title>
1011 <title>Automatic dependencies</title>
1015 By default, &SCons; assumes that a target file has <literal>automatic
1016 dependencies</literal> on the:
1023 <member>tool used to build the target file</member>
1025 <member>contents of the input files</member>
1027 <member>command line used to build the target file</member>
1034 If any of these changes, the target file will be rebuilt.
1040 <title>Implicit dependencies</title>
1044 Additionally, &SCons; can scan the contents of files for
1045 <literal>implicit dependencies</literal> on other files. For
1046 example, &SCons; will scan the contents of a <filename>.c</filename>
1047 file and determine that any object created from it is
1048 dependent on any <filename>.h</filename> files specified via
1049 <literal>#include</literal>. &SCons;, therefore, "does the right
1050 thing" without needing to have these dependencies listed explicitly:
1057 env.Program('hello', 'hello.c')
1059 #include "hello_string.h"
1062 printf("%s\n", STRING);
1064 % cat > hello_string.h
1065 #define STRING "Hello, world!\n"
1067 gcc -c hello.c -o hello.o
1068 gcc -o hello hello.c
1071 % cat > hello_string.h
1072 #define STRING "Hello, world, hello!\n"
1074 gcc -c hello.c -o hello.o
1075 gcc -o hello hello.c
1077 Hello, world, hello!
1084 <title>Ignoring dependencies</title>
1088 Undesirable <literal>automatic dependencies</literal> or
1089 <literal>implicit dependencies</literal> may be ignored:
1094 env.Program(target = 'bar', source = 'bar.c')
1095 env.Ignore('bar', '/usr/bin/gcc', 'version.h')
1100 In the above example, the <filename>bar</filename> program will not
1101 be rebuilt if the <filename>/usr/bin/gcc</filename> compiler or the
1102 <filename>version.h</filename> file change.
1108 <title>Explicit dependencies</title>
1112 Dependencies that are unknown to &SCons; may be specified explicitly
1113 in an &SCons; configuration file:
1118 env.Depends(target = 'output1', dependency = 'input_1 input_2')
1119 env.Depends(target = 'output2', dependency = ['input_1', 'input_2'])
1120 env.Depends(target = 'output3', dependency = ['white space input'])
1122 env.Depends(target = 'output_a output_b', dependency = 'input_3')
1123 env.Depends(target = ['output_c', 'output_d'], dependency = 'input_4')
1124 env.Depends(target = ['white space output'], dependency = 'input_5')
1129 Just like the <literal>target</literal> keyword argument, the
1130 <literal>dependency</literal> keyword argument may be specified as a
1131 string of white-space separated file names, or as an array.
1137 A dependency on an &SCons; configuration file itself may be specified
1138 explicitly to force a rebuild whenever the configuration file changes:
1143 env.Depends(target = 'archive.tar.gz', dependency = 'SConstruct')
1152 <section id="sect-scanners">
1153 <title>&Scanner; Objects</title>
1157 Analagous to the previously-described &Builder; objects, &SCons;
1158 supplies (and uses) &Scanner; objects to search the contents of
1159 a file for implicit dependency files:
1168 <entry>CScan</entry>
1169 <entry>scan .{c,C,cc,cxx,cpp} files for #include dependencies</entry>
1178 A &consenv; can be explicitly initialized with
1179 associated &Scanner; objects:
1184 env = Environment(SCANNERS = ['CScan', 'M4Scan'])
1189 &Scanner; objects bound to a &consenv; can be
1190 associated directly with specified files:
1195 env.CScan('foo.c', 'bar.c')
1196 env.M4Scan('input.m4')
1200 <title>User-defined &Scanner; objects</title>
1204 A user may define a &Scanner; object to scan a type of file for
1205 implicit dependencies:
1210 def scanner1(file_contents):
1211 # search for dependencies
1212 return dependency_list
1214 FirstScan = Scanner(function = scanner1)
1219 The scanner function must return a list of dependencies that its finds
1220 based on analyzing the file contents it is passed as an argument.
1226 The scanner function, when invoked, will be passed the calling
1227 environment. The scanner function can use &consenvs; from the passed
1228 environment to affect how it performs its dependency scan--the
1229 canonical example being to use some sort of search-path construction
1230 variable to look for dependency files in other directories:
1235 def scanner2(file_contents, env):
1236 path = env.{'SCANNERPATH'} # XXX
1237 # search for dependencies using 'path'
1238 return dependency_list
1240 SecondScan = Scanner(function = scanner2)
1245 The user may specify an additional argument when the &Scanner; object
1246 is created. When the scanner is invoked, the additional argument
1247 will be passed to the scanner funciton, which can be used in any way
1248 the scanner function sees fit:
1253 def scanner3(file_contents, env, arg):
1254 # skip 'arg' lines, then search for dependencies
1255 return dependency_list
1257 Skip_3_Lines_Scan = Scanner(function = scanner2, argument = 3)
1258 Skip_6_Lines_Scan = Scanner(function = scanner2, argument = 6)
1264 <title>Copying &Scanner; Objects</title>
1268 A method exists to return a copy of an existing &Scanner; object,
1269 with any overridden values specified as keyword arguments to the
1275 scan = Scanner(function = my_scan)
1276 scan_path = scan.Copy(path = '%SCANNERPATH')
1281 Typically, &Scanner; objects will be supplied by a tool-master or
1282 administrator through a shared &consenv;.
1288 <title>&Scanner; maps</title>
1291 If the &BUILDERMAP; proves unnecessary,
1292 we could/should get rid of this one, too,
1293 by adding a parallel <literal>src_suffix</literal>
1294 argument to the &Scanner; factory...
1300 Each &consenv; has a &SCANNERMAP;, a dictionary that associates
1301 different file suffixes with a scanner object that can be used to
1302 generate a list of dependencies from the contents of that file. This
1303 &SCANNERMAP; can be initialized at instantiation:
1308 env = Environment(SCANNERMAP = {
1317 &Scanner; objects referenced in the &SCANNERMAP; do not need to
1318 be listed separately in the <literal>SCANNERS</literal> variable. The &consenv;
1319 will bind the union of the &Scanner; objects listed
1330 <section id="sect-targets">
1331 <title>Targets</title>
1335 The methods in the build engine API described so far merely
1336 establish associations that describe file dependencies, how a
1337 file should be scanned, etc. Since the real point is to actually
1338 <emphasis>build</emphasis> files, &SCons; also has methods that
1339 actually direct the build engine to build, or otherwise manipulate,
1345 <title>Building targets</title>
1348 One or more targets may be built as follows:
1353 env.Build(target = ['foo', 'bar'])
1358 Note that specifying a directory (or other collective object) will
1359 cause all subsidiary/dependent objects to be built as well:
1364 env.Build(target = '.')
1366 env.Build(target = 'builddir')
1371 By default, &SCons; explicitly removes a target file before
1372 invoking the underlying function or command(s) to build it.
1378 <title>Removing targets</title>
1382 A "cleanup" operation of removing generated (target) files is
1383 performed as follows:
1388 env.Clean(target = ['foo', 'bar'])
1393 Like the &Build; method, the &Clean; method may be passed a
1394 directory or other collective object, in which case the subsidiary
1395 target objects under the directory will be removed:
1400 env.Clean(target = '.')
1402 env.Clean(target = 'builddir')
1407 (The directories themselves are not removed.)
1413 <title>Suppressing cleanup removal of build-targets</title>
1417 By default, &SCons; explicitly removes all build-targets
1418 when invoked to perform "cleanup". Files that should not be
1419 removed during "cleanup" can be specified via the
1425 env.Library(target = 'libfoo.a', source = ['aaa.c', 'bbb.c', 'ccc.c'])
1426 env.NoClean('libfoo.a')
1431 The NoClean operation has precedence over the Clean operation.
1432 A target that is specified as both Clean and NoClean, will not
1433 be removed during a clean.
1435 In the following example, target 'foo' will not be removed
1439 env.Clean(target = 'foo')
1449 <title>Suppressing build-target removal</title>
1453 As mentioned, by default, &SCons; explicitly removes a target
1454 file before invoking the underlying function or command(s) to build
1455 it. Files that should not be removed before rebuilding can be
1456 specified via the &Precious; method:
1461 env.Library(target = 'libfoo.a', source = ['aaa.c', 'bbb.c', 'ccc.c'])
1462 env.Precious('libfoo.a')
1468 <title>Default targets</title>
1472 The user may specify default targets that will be built if there are no
1473 targets supplied on the command line:
1478 env.Default('install', 'src')
1483 Multiple calls to the &Default; method (typically one per &SConscript;
1484 file) append their arguments to the list of default targets.
1490 <title>File installation</title>
1494 Files may be installed in a destination directory:
1499 env.Install('/usr/bin', 'program1', 'program2')
1504 Files may be renamed on installation:
1509 env.InstallAs('/usr/bin/xyzzy', 'xyzzy.in')
1514 Multiple files may be renamed on installation by specifying
1515 equal-length lists of target and source files:
1520 env.InstallAs(['/usr/bin/foo', '/usr/bin/bar'],
1521 ['foo.in', 'bar.in'])
1527 <title>Target aliases</title>
1531 In order to provide convenient "shortcut" target names that expand to
1532 a specified list of targets, aliases may be established:
1537 env.Alias(alias = 'install',
1538 targets = ['/sbin', '/usr/lib', '/usr/share/man'])
1543 In this example, specifying a target of <literal>install</literal>
1544 will cause all the files in the associated directories to be built
1545 (that is, installed).
1551 An &Alias; may include one or more other &Aliases; in its list:
1556 env.Alias(alias = 'libraries', targets = ['lib'])
1557 env.Alias(alias = 'programs', targets = ['libraries', 'src'])
1566 <section id="sect-custom">
1567 <title>Customizing output</title>
1570 Take this whole section with a grain of salt.
1571 I whipped it up without a great deal of thought
1572 to try to add a "competitive advantage"
1573 for the second round of the Software Carpentry contest.
1574 In particular, hard-coding the
1575 analysis points and the keywords that specify them
1577 but I can't think of another way it would be
1579 I dunno, maybe this is fine as it is...
1584 The &SCons; API supports the ability to customize, redirect, or
1585 suppress its printed output through user-defined functions.
1586 &SCons; has several pre-defined points in its build process at
1587 which it calls a function to (potentially) print output. User-defined
1588 functions can be specified for these call-back points when &Build;
1589 or &Clean;is invoked:
1594 env.Build(target = '.',
1595 on_analysis = dump_dependency,
1596 pre_update = my_print_command,
1597 post_update = my_error_handler)
1598 on_error = my_error_handler)
1603 The specific call-back points are:
1610 <term><literal>on_analysis</literal></term>
1614 Called for every object, immediately after the object has been
1615 analyzed to see if it's out-of-date. Typically used to print a
1616 trace of considered objects for debugging of unexpected dependencies.
1623 <term><literal>pre_update</literal></term>
1627 Called for every object that has been determined to be out-of-date
1628 before its update function or command is executed. Typically used
1629 to print the command being called to update a target.
1636 <term><literal>post_update</literal></term>
1640 Called for every object after its update function or command has
1641 been executed. Typically used to report that a top-level specified
1642 target is up-to-date or was not remade.
1649 <term><literal>on_error</literal></term>
1653 Called for every error returned by an update function or command.
1654 Typically used to report errors with some string that will be
1655 identifiable to build-analysis tools.
1665 Functions for each of these call-back points all take the same
1671 my_dump_dependency(target, level, status, update, dependencies)
1676 where the arguments are:
1683 <term><literal>target</literal></term>
1687 The target object being considered.
1694 <term><literal>level</literal></term>
1698 Specifies how many levels the dependency analysis has
1699 recursed in order to consider the <literal>target</literal>.
1700 A value of <literal>0</literal> specifies a top-level
1701 <literal>target</literal> (that is, one passed to the
1702 &Build; or &Clean; method). Objects which a top-level
1703 <literal>target</literal> is directly dependent upon have a
1704 <literal>level</literal> of <1>, their direct dependencies have a
1705 <literal>level</literal> of <2>, etc. Typically used to indent
1706 output to reflect the recursive levels.
1713 <term><literal>status</literal></term>
1717 A string specifying the current status of the target
1718 (<literal>"unknown"</literal>, <literal>"built"</literal>,
1719 <literal>"error"</literal>, <literal>"analyzed"</literal>, etc.). A
1720 complete list will be enumerated and described during implementation.
1727 <term><literal>update</literal></term>
1731 The command line or function name that will be (or has been) executed
1732 to update the <literal>target</literal>.
1739 <term><literal>dependencies</literal></term>
1743 A list of direct dependencies of the target.
1755 <section id="separate">
1756 <title>Separate source and build trees</title>
1759 I've never liked Cons' use of the name <literal>Link</literal>
1760 for this functionality,
1761 mainly because the term is overloaded
1762 with linking object files into an executable.
1763 Yet I've never come up with anything better.
1768 Also, I made this an &Environment; method because
1769 it logically belongs in the API reference
1770 (the build engine needs to know about it),
1771 and I thought it was clean to have
1772 everything in the build-engine API
1773 be called through an &Environment; object.
1774 But <literal>&Link</literal> isn't really
1775 associated with a specific environment
1776 (the &Cons; classic implementation just
1777 leaves it as a bare function call),
1778 so maybe we should just follow that example
1779 and not call it through an environment...
1784 &SCons; allows target files to be built completely separately from
1785 the source files by "linking" a build directory to an underlying
1791 env.Link('build', 'src')
1793 SConscript('build/SConscript')
1798 &SCons; will copy (or hard link) necessary files (including the
1799 &SConscript; file) into the build directory hierarchy. This allows the
1800 source directory to remain uncluttered by derived files.
1808 <section id="sect-variant">
1809 <title>Variant builds</title>
1813 The &Link; method may be used in conjunction with multiple
1814 &consenvs; to support variant builds. The following
1815 &SConstruct; and &SConscript; files would build separate debug and
1816 production versions of the same program side-by-side:
1823 env.Link('build/debug', 'src')
1824 env.Link('build/production', 'src')
1826 SConscript('build/debug/SConscript', Export(env))
1828 SConscript('build/production/SConscript', Export(env))
1829 % cat src/SConscript
1830 env = Environment(CCFLAGS = flags)
1831 env.Program('hello', 'hello.c')
1836 The following example would build the appropriate program for the current
1837 compilation platform, without having to clean any directories of object
1838 or executable files for other architectures:
1844 build_platform = os.path.join('build', sys.platform)
1845 Link(build_platform, 'src')
1846 SConscript(os.path.join(build_platform, 'SConscript'))
1847 % cat src/SConscript
1849 env.Program('hello', 'hello.c')
1856 <section id="sect-repositories">
1857 <title>Code repositories</title>
1860 Like &Link;, &Repository; and &Local; are part of the
1861 API reference, but not really tied to any specific environment.
1862 Is it better to be consistent about calling
1863 everything in the API through an environment,
1864 or to leave these independent so as
1865 not to complicate their calling interface?
1870 &SCons; may use files from one or more shared code repositories in order
1871 to build local copies of changed target files. A repository would
1872 typically be a central directory tree, maintained by an integrator,
1873 with known good libraries and executables.
1878 Repository('/home/source/1.1', '/home/source/1.0')
1883 Specified repositories will be searched in-order for any file
1884 (configuration file, input file, target file) that does not exist
1885 in the local directory tree. When building a local target file,
1886 &SCons; will rewrite path names in the build command to use the
1887 necessary repository files. This includes modifying lists of
1888 <option>-I</option> or <option>-L</option> flags to specify an
1889 appropriate set of include paths for dependency analysis.
1894 &SCons; will modify the Python <varname>sys.path</varname> variable to
1895 reflect the addition of repositories to the search path, so that any
1896 imported modules or packages necessary for the build can be found in a
1897 repository, as well.
1902 If an up-to-date target file is found in a code repository, the file
1903 will not be rebuilt or copied locally. Files that must exist locally
1904 (for example, to run tests) may be specified:
1909 Local('program', 'libfoo.a')
1914 in which case &SCons; will copy or link an up-to-date copy of the
1915 file from the appropriate repository.
1923 <section id="sect-caching">
1924 <title>Derived-file caching</title>
1927 There should be extensions to this part of the API for
1928 auxiliary functions like cleaning the cache.
1933 &SCons; can maintain a cache directory of target files which may be
1934 shared among multiple builds. This reduces build times by allowing
1935 developers working on a project together to share common target
1941 Cache('/var/tmp/build.cache/i386')
1946 When a target file is generated, a copy is added to the cache.
1947 When generating a target file, if &SCons; determines that a file
1948 that has been built with the exact same dependencies already exists
1949 in the specified cache, &SCons; will copy the cached file rather
1950 than re-building the target.
1955 Command-line options exist to modify the &SCons; caching behavior
1956 for a specific build, including disabling caching, building
1957 dependencies in random order, and displaying commands as if cached
1966 <section id="sect-jobs">
1967 <title>Job management</title>
1970 This has been completely superseded by
1971 the more sophisticated &Task; manager
1972 that Anthony Roach has contributed.
1973 I need to write that up...
1978 A simple API exists to inform the Build Engine how many jobs may
1979 be run simultaneously: