d613f42450e6bfee4ba58ac5dec0e62cb25f7122
[scons.git] / doc / user / separate.in
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   This is usually done by creating one or more separate
118   <emphasis>build directories</emphasis>
119   that are used to hold the built objects files, libraries,
120   and executable programs, etc.
121   for a specific flavor 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 &BuildDir; function.
125
126   </para>
127
128   <section>
129   <title>Specifying a Build Directory as Part of an &SConscript; Call</title>
130
131     <para>
132
133     The most straightforward way to establish a build directory
134     uses the fact that the usual way to
135     set up a build hierarchy is to have an
136     &SConscript; file in the source subdirectory.
137     If you then pass a &build_dir; argument to the
138     &SConscript; function call:
139
140     </para>
141
142     <scons_example name="ex1">
143       <file name="SConstruct" printme="1">
144       SConscript('src/SConscript', build_dir='build')
145       </file>
146       <file name="src/SConscript">
147       env = Environment()
148       env.Program('hello.c')
149       </file>
150       <file name="src/hello.c">
151       int main() { printf("Hello, world!\n"); }
152       </file>
153     </scons_example>
154
155     <para>
156
157     &SCons; will then build all of the files in
158     the &build; subdirectory:
159
160     </para>
161
162     <scons_output example="ex1">
163       <command>ls src</command>
164       <command>scons -Q</command>
165       <command>ls build</command>
166     </scons_output>
167
168     <para>
169
170     But wait a minute--what's going on here?
171     &SCons; created the object file
172     <filename>build/hello.o</filename>
173     in the &build; subdirectory,
174     as expected.
175     But even though our &hello_c; file lives in the &src; subdirectory,
176     &SCons; has actually compiled a
177     <filename>build/hello.c</filename> file
178     to create the object file.
179
180     </para>
181
182     <para>
183
184     What's happened is that &SCons; has <emphasis>duplicated</emphasis>
185     the &hello_c; file from the &src; subdirectory
186     to the &build; subdirectory,
187     and built the program from there.
188     The next section explains why &SCons; does this.
189
190     </para>
191
192   </section>
193
194   <section>
195   <title>Why &SCons; Duplicates Source Files in a Build Directory</title>
196
197     <para>
198
199     &SCons; duplicates source files in build directories
200     because it's the most straightforward way to guarantee a correct build
201     <emphasis>regardless of include-file directory paths,
202     relative references between files,
203     or tool support for putting files in different locations</emphasis>,
204     and the &SCons; philosophy is to, by default,
205     guarantee a correct build in all cases.
206
207     </para>
208
209     <para>
210
211     The most direct reason to duplicate source files
212     in build directories
213     is simply that some tools (mostly older vesions)
214     are written to only build their output files
215     in the same directory as the source files.
216     In this case, the choices are either
217     to build the output file in the source directory
218     and move it to the build directory,
219     or to duplicate the source files in the build directory.
220
221     </para>
222
223     <para>
224
225     Additionally,
226     relative references between files
227     can cause problems if we don't
228     just duplicate the hierarchy of source files
229     in the build directory.
230     You can see this at work in
231     use of the C preprocessor <literal>#include</literal>
232     mechanism with double quotes, not angle brackets:
233
234     </para>
235
236     <sconstruct>
237       #include "file.h"
238     </sconstruct>
239
240     <para>
241
242     The <emphasis>de facto</emphasis> standard behavior
243     for most C compilers in this case
244     is to first look in the same directory
245     as the source file that contains the <literal>#include</literal> line,
246     then to look in the directories in the preprocessor search path.
247     Add to this that the &SCons; implementation of
248     support for code repositories
249     (described below)
250     means not all of the files
251     will be found in the same directory hierarchy,
252     and the simplest way to make sure
253     that the right include file is found
254     is to duplicate the source files into the build directory,
255     which provides a correct build
256     regardless of the original location(s) of the source files.
257
258     </para>
259
260     <para>
261
262     Although source-file duplication guarantees a correct build
263     even in these end-cases,
264     it <emphasis>can</emphasis> usually be safely disabled.
265     The next section describes
266     how you can disable the duplication of source files
267     in the build directory.
268
269     </para>
270
271   </section> 
272
273   <section>
274   <title>Telling &SCons; to Not Duplicate Source Files in the Build Directory</title>
275
276     <para>
277
278     In most cases and with most tool sets,
279     &SCons; can place its target files in a build subdirectory
280     <emphasis>without</emphasis>
281     duplicating the source files
282     and everything will work just fine.
283     You can disable the default &SCons; behavior
284     by specifying <literal>duplicate=0</literal>
285     when you call the &SConscript; function:
286
287     </para>
288
289     <sconstruct>
290       SConscript('src/SConscript', build_dir='build', duplicate=0)
291     </sconstruct>
292
293     <para>
294
295     When this flag is specified,
296     &SCons; uses the build directory
297     like most people expect--that is,
298     the output files are placed in the build directory
299     while the source files stay in the source directory:
300
301     </para>
302
303     <screen>
304       % <userinput>ls src</userinput>
305       SConscript
306       hello.c
307       % <userinput>scons -Q</userinput>
308       cc -c src/hello.c -o build/hello.o
309       cc -o build/hello build/hello.o
310       % <userinput>ls build</userinput>
311       hello
312       hello.o
313     </screen>
314
315   </section>
316
317   <section>
318   <title>The &BuildDir; Function</title>
319
320     <para>
321
322     Use the &BuildDir; function to establish that target
323     files should be built in a separate directory
324     from the source files:
325
326     </para>
327
328     <scons_example name="ex_builddir">
329       <file name="SConstruct" printme="1">
330       BuildDir('build', 'src')
331       env = Environment()
332       env.Program('build/hello.c')
333       </file>
334       <file name="src/hello.c">
335       int main() { printf("Hello, world!\n"); }
336       </file>
337     </scons_example>
338
339     <para>
340
341     Note that when you're not using
342     an &SConscript; file in the &src; subdirectory,
343     you must actually specify that
344     the program must be built from
345     the <filename>build/hello.c</filename>
346     file that &SCons; will duplicate in the
347     &build; subdirectory.
348
349     </para>
350
351     <para>
352
353     When using the &BuildDir; function directly,
354     &SCons; still duplicates the source files
355     in the build directory by default:
356
357     </para>
358
359     <scons_output example="ex_builddir">
360       <command>ls src</command>
361       <command>scons -Q</command>
362       <command>ls build</command>
363     </scons_output>
364
365     <para>
366
367     You can specify the same <literal>duplicate=0</literal> argument
368     that you can specify for an &SConscript; call:
369
370     </para>
371
372     <scons_example name="ex_duplicate_0">
373       <file name="SConstruct" printme="1">
374       BuildDir('build', 'src', duplicate=0)
375       env = Environment()
376       env.Program('build/hello.c')
377       </file>
378       <file name="src/hello.c">
379       int main() { printf("Hello, world!\n"); }
380       </file>
381     </scons_example>
382
383     <para>
384
385     In which case &SCons;
386     will disable duplication of the source files:
387
388     </para>
389
390     <scons_output example="ex_duplicate_0">
391       <command>ls src</command>
392       <command>scons -Q</command>
393       <command>ls build</command>
394     </scons_output>
395
396   </section>
397
398   <section>
399   <title>Using &BuildDir; With an &SConscript; File</title>
400
401     <para>
402
403     Even when using the &BuildDir; function,
404     it's much more natural to use it with
405     a subsidiary &SConscript; file.
406     For example, if the
407     <filename>src/SConscript</filename>
408     looks like this:
409
410     </para>
411
412     <scons_example name="example_builddir_sconscript">
413       <file name="SConstruct">
414       BuildDir('build', 'src')
415       SConscript('build/SConscript')
416       </file>
417       <file name="src/SConscript" printme="1">
418       env = Environment()
419       env.Program('hello.c')
420       </file>
421       <file name="src/hello.c">
422       int main() { printf("Hello, world!\n"); }
423       </file>
424     </scons_example>
425
426     <para>
427
428     Then our &SConstruct; file could look like:
429
430     </para>
431
432     <scons_example_file example="example_builddir_sconscript" name="SConstruct">
433     </scons_example_file>
434
435     <para>
436
437     Yielding the following output:
438
439     </para>
440
441     <scons_output example="example_builddir_sconscript">
442       <command>ls src</command>
443       <command>scons -Q</command>
444       <command>ls build</command>
445     </scons_output>
446
447     <para>
448
449     Notice that this is completely equivalent
450     to the use of &SConscript; that we
451     learned about in the previous section.
452
453     </para>
454
455   </section>
456
457   <!--
458
459   <section>
460   <title>Why You'd Want to Call &BuildDir; Instead of &SConscript;</title>
461
462     <para>
463     
464     XXX
465
466     </para>
467
468   </section>
469
470   -->