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 =head1 Separating source and build trees
30 It's often desirable to keep any derived files from the build completely
31 separate from the source files. This makes it much easier to keep track of
32 just what is a source file, and also makes it simpler to handle B<variant>
33 builds, especially if you want the variant builds to co-exist.
35 =head2 Separating build and source directories using the Link command
37 Cons provides a simple mechanism that handles all of these requirements. The
38 C<Link> command is invoked as in this example:
40 Link 'build' => 'src';
42 The specified directories are ``linked'' to the specified source
43 directory. Let's suppose that you setup a source directory, F<src>, with the
44 sub-directories F<world> and F<hello> below it, as in the previous
45 example. You could then substitute for the original build lines the
53 Notice that you treat the F<Conscript> file as if it existed in the build
54 directory. Now if you type the same command as before, you will get the
58 Install build/world/world.h as export/include/world.h
59 cc -Iexport/include -c build/hello/hello.c -o build/hello/hello.o
60 cc -Iexport/include -c build/world/world.c -o build/world/world.o
61 ar r build/world/libworld.a build/world/world.o
62 ar: creating build/world/libworld.a
63 ranlib build/world/libworld.a
64 Install build/world/libworld.a as export/lib/libworld.a
65 cc -o build/hello/hello build/hello/hello.o -Lexport/lib -lworld
66 Install build/hello/hello as export/bin/hello
68 Again, Cons has taken care of the details for you. In particular, you will
69 notice that all the builds are done using source files and object files from
70 the build directory. For example, F<build/world/world.o> is compiled from
71 F<build/world/world.c>, and F<export/include/world.h> is installed from
72 F<build/world/world.h>. This is accomplished on most systems by the simple
73 expedient of ``hard'' linking the required files from each source directory
74 into the appropriate build directory.
76 The links are maintained correctly by Cons, no matter what you do to the
77 source directory. If you modify a source file, your editor may do this ``in
78 place'' or it may rename it first and create a new file. In the latter case,
79 any hard link will be lost. Cons will detect this condition the next time
80 the source file is needed, and will relink it appropriately.
82 You'll also notice, by the way, that B<no> changes were required to the
83 underlying F<Conscript> files. And we can go further, as we shall see in the
86 =head2 Explicit references to the source directory
88 When using the C<Link> command on some operating systems or with some
89 tool chains, it's sometimes useful to have a command actually use
90 the path name to the source directory, not the build directory. For
91 example, on systems that must copy, not "hard link," the F<src/> and
92 F<build/> copies of C<Linked> files, using the F<src/> path of a file
93 name might make an editor aware that a syntax error must be fixed in the
94 source directory, not the build directory.
96 You can tell Cons that you want to use the "source path" for a file by
97 preceding the file name with a ``!'' (exclamation point). For example,
98 if we add a ``!'' to the beginning of a source file:
100 Program $env "foo", "!foo.c"; # Notice initial ! on foo.c
102 Cons will compile the target as follows:
104 cc -c src/foo.c -o build/foo.o
105 cc -o build/foo build/foo.o
107 Notice that Cons has compiled the program from the the F<src/foo.c>
108 source file. Without the initial ``!'', Cons would have compiled the
109 program using the F<build/foo.c> path name.
115 It's often useful to keep any built files completely
116 separate from the source files.
117 In &SCons;, this is usually done by creating one or more separate
118 <emphasis>variant directory trees</emphasis>
119 that are used to hold the built objects files, libraries,
120 and executable programs, etc.
121 for a specific flavor, or variant, of build.
122 &SCons; provides two ways to do this,
123 one through the &SConscript; function that we've already seen,
124 and the second through a more flexible &VariantDir; function.
130 One historical note: the &VariantDir; function
131 used to be called &BuildDir;.
132 That name is still supported
133 but has been deprecated
134 because the &SCons; functionality
135 differs from the model of a "build directory"
136 implemented by other build systems like the GNU Autotools.
141 <title>Specifying a Variant Directory Tree as Part of an &SConscript; Call</title>
145 The most straightforward way to establish a variant directory tree
146 uses the fact that the usual way to
147 set up a build hierarchy is to have an
148 &SConscript; file in the source subdirectory.
149 If you then pass a &variant_dir; argument to the
150 &SConscript; function call:
155 SConscript('src/SConscript', variant_dir='build')
160 &SCons; will then build all of the files in
161 the &build; subdirectory:
166 % <userinput>ls src</userinput>
168 % <userinput>scons -Q</userinput>
169 cc -o build/hello.o -c build/hello.c
170 cc -o build/hello build/hello.o
171 % <userinput>ls build</userinput>
172 SConscript hello hello.c hello.o
177 But wait a minute--what's going on here?
178 &SCons; created the object file
179 <filename>build/hello.o</filename>
180 in the &build; subdirectory,
182 But even though our &hello_c; file lives in the &src; subdirectory,
183 &SCons; has actually compiled a
184 <filename>build/hello.c</filename> file
185 to create the object file.
191 What's happened is that &SCons; has <emphasis>duplicated</emphasis>
192 the &hello_c; file from the &src; subdirectory
193 to the &build; subdirectory,
194 and built the program from there.
195 The next section explains why &SCons; does this.
202 <title>Why &SCons; Duplicates Source Files in a Variant Directory Tree</title>
206 &SCons; duplicates source files in variant directory trees
207 because it's the most straightforward way to guarantee a correct build
208 <emphasis>regardless of include-file directory paths,
209 relative references between files,
210 or tool support for putting files in different locations</emphasis>,
211 and the &SCons; philosophy is to, by default,
212 guarantee a correct build in all cases.
218 The most direct reason to duplicate source files
219 in variant directories
220 is simply that some tools (mostly older versions)
221 are written to only build their output files
222 in the same directory as the source files.
223 In this case, the choices are either
224 to build the output file in the source directory
225 and move it to the variant directory,
226 or to duplicate the source files in the variant directory.
233 relative references between files
234 can cause problems if we don't
235 just duplicate the hierarchy of source files
236 in the variant directory.
237 You can see this at work in
238 use of the C preprocessor <literal>#include</literal>
239 mechanism with double quotes, not angle brackets:
249 The <emphasis>de facto</emphasis> standard behavior
250 for most C compilers in this case
251 is to first look in the same directory
252 as the source file that contains the <literal>#include</literal> line,
253 then to look in the directories in the preprocessor search path.
254 Add to this that the &SCons; implementation of
255 support for code repositories
257 means not all of the files
258 will be found in the same directory hierarchy,
259 and the simplest way to make sure
260 that the right include file is found
261 is to duplicate the source files into the variant directory,
262 which provides a correct build
263 regardless of the original location(s) of the source files.
269 Although source-file duplication guarantees a correct build
270 even in these end-cases,
271 it <emphasis>can</emphasis> usually be safely disabled.
272 The next section describes
273 how you can disable the duplication of source files
274 in the variant directory.
281 <title>Telling &SCons; to Not Duplicate Source Files in the Variant Directory Tree</title>
285 In most cases and with most tool sets,
286 &SCons; can place its target files in a build subdirectory
287 <emphasis>without</emphasis>
288 duplicating the source files
289 and everything will work just fine.
290 You can disable the default &SCons; behavior
291 by specifying <literal>duplicate=0</literal>
292 when you call the &SConscript; function:
297 SConscript('src/SConscript', variant_dir='build', duplicate=0)
302 When this flag is specified,
303 &SCons; uses the variant directory
304 like most people expect--that is,
305 the output files are placed in the variant directory
306 while the source files stay in the source directory:
311 % <userinput>ls src</userinput>
314 % <userinput>scons -Q</userinput>
315 cc -c src/hello.c -o build/hello.o
316 cc -o build/hello build/hello.o
317 % <userinput>ls build</userinput>
325 <title>The &VariantDir; Function</title>
329 Use the &VariantDir; function to establish that target
330 files should be built in a separate directory
331 from the source files:
336 VariantDir('build', 'src')
338 env.Program('build/hello.c')
343 Note that when you're not using
344 an &SConscript; file in the &src; subdirectory,
345 you must actually specify that
346 the program must be built from
347 the <filename>build/hello.c</filename>
348 file that &SCons; will duplicate in the
349 &build; subdirectory.
355 When using the &VariantDir; function directly,
356 &SCons; still duplicates the source files
357 in the variant directory by default:
362 % <userinput>ls src</userinput>
364 % <userinput>scons -Q</userinput>
365 cc -o build/hello.o -c build/hello.c
366 cc -o build/hello build/hello.o
367 % <userinput>ls build</userinput>
368 hello hello.c hello.o
373 You can specify the same <literal>duplicate=0</literal> argument
374 that you can specify for an &SConscript; call:
379 VariantDir('build', 'src', duplicate=0)
381 env.Program('build/hello.c')
386 In which case &SCons;
387 will disable duplication of the source files:
392 % <userinput>ls src</userinput>
394 % <userinput>scons -Q</userinput>
395 cc -o build/hello.o -c src/hello.c
396 cc -o build/hello build/hello.o
397 % <userinput>ls build</userinput>
404 <title>Using &VariantDir; With an &SConscript; File</title>
408 Even when using the &VariantDir; function,
409 it's much more natural to use it with
410 a subsidiary &SConscript; file.
412 <filename>src/SConscript</filename>
419 env.Program('hello.c')
424 Then our &SConstruct; file could look like:
430 VariantDir('build', 'src')
431 SConscript('build/SConscript')
436 Yielding the following output:
441 % <userinput>ls src</userinput>
443 % <userinput>scons -Q</userinput>
444 cc -o build/hello.o -c build/hello.c
445 cc -o build/hello build/hello.o
446 % <userinput>ls build</userinput>
447 SConscript hello hello.c hello.o
452 Notice that this is completely equivalent
453 to the use of &SConscript; that we
454 learned about in the previous section.
461 <title>Using &Glob; with &VariantDir;</title>
465 The &Glob; file name pattern matching function
466 works just as usual when using &VariantDir;.
468 <filename>src/SConscript</filename>
475 env.Program('hello', Glob('*.c'))
480 Then with the same &SConstruct; file as in the previous section,
481 and source files <filename>f1.c</filename>
482 and <filename>f2.c</filename> in src,
483 we would see the following output:
488 % <userinput>ls src</userinput>
489 SConscript f1.c f2.c f2.h
490 % <userinput>scons -Q</userinput>
491 cc -o build/f1.o -c build/f1.c
492 cc -o build/f2.o -c build/f2.c
493 cc -o build/hello build/f1.o build/f2.o
494 % <userinput>ls build</userinput>
495 SConscript f1.c f1.o f2.c f2.h f2.o hello
500 The &Glob; function returns Nodes in the
501 <filename>build/</filename> tree, as you'd expect.
510 <title>Why You'd Want to Call &VariantDir; Instead of &SConscript;</title>
514 XXX why call VariantDir() instead of SConscript(variant_dir=)