http://scons.tigris.org/issues/show_bug.cgi?id=2345
[scons.git] / doc / user / separate.xml
1 <!--
2
3   __COPYRIGHT__
4
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:
12
13   The above copyright notice and this permission notice shall be included
14   in all copies or substantial portions of the Software.
15
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.
23
24 -->
25
26 <!--
27
28 =head1 Separating source and build trees
29
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.
34
35 =head2 Separating build and source directories using the Link command
36
37 Cons provides a simple mechanism that handles all of these requirements. The
38 C<Link> command is invoked as in this example:
39
40   Link 'build' => 'src';
41
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
46 following:
47
48   Build qw(
49         build/world/Conscript
50         build/hello/Conscript
51   );
52
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
55 following results:
56
57   % cons export
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
67
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.
75
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.
81
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
84 next section.
85
86 =head2 Explicit references to the source directory
87
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.
95
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:
99
100   Program $env "foo", "!foo.c"; # Notice initial ! on foo.c
101
102 Cons will compile the target as follows:
103
104   cc -c src/foo.c -o build/foo.o
105   cc -o build/foo build/foo.o
106
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.
110
111 -->
112
113   <para>
114
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.
125
126   </para>
127
128   <para>
129
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.
137
138   </para>
139
140   <section>
141   <title>Specifying a Variant Directory Tree as Part of an &SConscript; Call</title>
142
143     <para>
144
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:
151
152     </para>
153
154     <programlisting>
155       SConscript('src/SConscript', variant_dir='build')
156     </programlisting>
157
158     <para>
159
160     &SCons; will then build all of the files in
161     the &build; subdirectory:
162
163     </para>
164
165     <screen>
166       % <userinput>ls src</userinput>
167       SConscript  hello.c
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
173     </screen>
174
175     <para>
176
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,
181     as expected.
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.
186
187     </para>
188
189     <para>
190
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.
196
197     </para>
198
199   </section>
200
201   <section>
202   <title>Why &SCons; Duplicates Source Files in a Variant Directory Tree</title>
203
204     <para>
205
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.
213
214     </para>
215
216     <para>
217
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.
227
228     </para>
229
230     <para>
231
232     Additionally,
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:
240
241     </para>
242
243     <programlisting>
244       #include "file.h"
245     </programlisting>
246
247     <para>
248
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
256     (described below)
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.
264
265     </para>
266
267     <para>
268
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.
275
276     </para>
277
278   </section>
279
280   <section>
281   <title>Telling &SCons; to Not Duplicate Source Files in the Variant Directory Tree</title>
282
283     <para>
284
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:
293
294     </para>
295
296     <programlisting>
297       SConscript('src/SConscript', variant_dir='build', duplicate=0)
298     </programlisting>
299
300     <para>
301
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:
307
308     </para>
309
310     <screen>
311       % <userinput>ls src</userinput>
312       SConscript
313       hello.c
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>
318       hello
319       hello.o
320     </screen>
321
322   </section>
323
324   <section>
325   <title>The &VariantDir; Function</title>
326
327     <para>
328
329     Use the &VariantDir; function to establish that target
330     files should be built in a separate directory
331     from the source files:
332
333     </para>
334
335     <programlisting>
336       VariantDir('build', 'src')
337       env = Environment()
338       env.Program('build/hello.c')
339     </programlisting>
340
341     <para>
342
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.
350
351     </para>
352
353     <para>
354
355     When using the &VariantDir; function directly,
356     &SCons; still duplicates the source files
357     in the variant directory by default:
358
359     </para>
360
361     <screen>
362       % <userinput>ls src</userinput>
363       hello.c
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
369     </screen>
370
371     <para>
372
373     You can specify the same <literal>duplicate=0</literal> argument
374     that you can specify for an &SConscript; call:
375
376     </para>
377
378     <programlisting>
379       VariantDir('build', 'src', duplicate=0)
380       env = Environment()
381       env.Program('build/hello.c')
382     </programlisting>
383
384     <para>
385
386     In which case &SCons;
387     will disable duplication of the source files:
388
389     </para>
390
391     <screen>
392       % <userinput>ls src</userinput>
393       hello.c
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>
398       hello  hello.o
399     </screen>
400
401   </section>
402
403   <section>
404   <title>Using &VariantDir; With an &SConscript; File</title>
405
406     <para>
407
408     Even when using the &VariantDir; function,
409     it's much more natural to use it with
410     a subsidiary &SConscript; file.
411     For example, if the
412     <filename>src/SConscript</filename>
413     looks like this:
414
415     </para>
416
417     <programlisting>
418       env = Environment()
419       env.Program('hello.c')
420     </programlisting>
421
422     <para>
423
424     Then our &SConstruct; file could look like:
425
426     </para>
427
428     
429     <programlisting>
430       VariantDir('build', 'src')
431       SConscript('build/SConscript')
432       </programlisting>
433
434     <para>
435
436     Yielding the following output:
437
438     </para>
439
440     <screen>
441       % <userinput>ls src</userinput>
442       SConscript  hello.c
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
448     </screen>
449
450     <para>
451
452     Notice that this is completely equivalent
453     to the use of &SConscript; that we
454     learned about in the previous section.
455
456     </para>
457
458   </section>
459
460   <section>
461   <title>Using &Glob; with &VariantDir;</title>
462
463     <para>
464
465     The &Glob; file name pattern matching function
466     works just as usual when using &VariantDir;.
467     For example, if the
468     <filename>src/SConscript</filename>
469     looks like this:
470
471     </para>
472
473     <programlisting>
474       env = Environment()
475       env.Program('hello', Glob('*.c'))
476     </programlisting>
477
478     <para>
479
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:
484
485     </para>
486
487     <screen>
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
496     </screen>
497
498     <para>
499
500     The &Glob; function returns Nodes in the
501     <filename>build/</filename> tree, as you'd expect.
502
503     </para>
504
505   </section>
506
507   <!--
508
509   <section>
510   <title>Why You'd Want to Call &VariantDir; Instead of &SConscript;</title>
511
512     <para>
513
514     XXX why call VariantDir() instead of SConscript(variant_dir=)
515
516     </para>
517
518   </section>
519
520   -->