82d938b96d52c2a1dc1a57399b3efbe29a7c6bc5
[scons.git] / doc / user / variants.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 Variant builds
29
30 =head2 Variations on a theme
31
32 Other variations of this model are possible. For example, you might decide
33 that you want to separate out your include files into platform dependent and
34 platform independent files. In this case, you'd have to define an
35 alternative to C<$INCLUDE> for platform-dependent files. Most F<Conscript>
36 files, generating purely platform-independent include files, would not have
37 to change.
38
39 You might also want to be able to compile your whole system with debugging
40 or profiling, for example, enabled. You could do this with appropriate
41 command line options, such as C<DEBUG=on>. This would then be translated
42 into the appropriate platform-specific requirements to enable debugging
43 (this might include turning off optimization, for example). You could
44 optionally vary the name space for these different types of systems, but, as
45 we'll see in the next section, it's not B<essential> to do this, since Cons
46 is pretty smart about rebuilding things when you change options.
47
48 -->
49
50   <para>
51
52   The &BuildDir; function now gives us everything
53   we need to show how easy it is to create
54   variant builds using &SCons;.
55   Suppose, for example, that we want to
56   build a program for both Windows and Linux platforms,
57   but that we want to build it in a shared directory
58   with separate side-by-side build directories
59   for the Windows and Linux versions of the program.
60
61   </para>
62
63   <scons_example name="ex_variants">
64     <file name="SConstruct" printme="1">
65     platform = ARGUMENTS.get('OS', Platform())
66
67     include = "#export/$PLATFORM/include"
68     lib = "#export/$PLATFORM/lib"
69     bin = "#export/$PLATFORM/bin"
70
71     env = Environment(PLATFORM = platform,
72                       BINDIR = bin,
73                       INCDIR = include,
74                       LIBDIR = lib,
75                       CPPPATH = [include],
76                       LIBPATH = [lib],
77                       LIBS = 'world')
78
79     Export('env')
80
81     env.SConscript('src/SConscript', build_dir='build/$PLATFORM')
82
83     #
84     #BuildDir("#build/$PLATFORM", 'src')
85     #SConscript("build/$PLATFORM/hello/SConscript")
86     #SConscript("build/$PLATFORM/world/SConscript")
87     </file>
88     <directory name="src"></directory>
89     <directory name="src/hello"></directory>
90     <directory name="src/world"></directory>
91     <file name="src/SConscript">
92     Import('env')
93     SConscript('hello/SConscript')
94     SConscript('world/SConscript')
95     </file>
96     <file name="src/hello/SConscript">
97     Import('env')
98     hello = env.Program('hello.c')
99     env.Install('$BINDIR', hello)
100     </file>
101     <file name="src/hello/hello.c">
102     #include "world.h"
103     int main(int argc, char *argv[]) { printf "hello.c\n"; world(); }
104     </file>
105     <file name="src/world/SConscript">
106     Import('env')
107     world = env.Library('world.c')
108     env.Install('$LIBDIR', world)
109     env.Install('$INCDIR', 'world.h')
110     </file>
111     <file name="src/world/world.h">
112     #define STRING "world.h"
113     extern int world();
114     </file>
115     <file name="src/world/world.c">
116     int world() { printf "world.c\n"; }
117     </file>
118   </scons_example>
119
120   <para>
121
122   This SConstruct file,
123   when run on a Linux system, yields:
124
125   </para>
126
127   <scons_output example="ex_variants" os="posix">
128     <command>scons -Q OS=linux</command>
129   </scons_output>
130
131   <para>
132
133   The same SConstruct file on Windows would build:
134
135   </para>
136
137   <scons_output example="ex_variants" os="win32">
138     <command>scons -Q OS=windows</command>
139   </scons_output>
140
141   <!--
142
143   <scons_example name="ex_var2">
144     <file name="SConstruct" printme="1">
145     env = Environment(OS = ARGUMENTS.get('OS'))
146     for os in ['newell', 'post']:
147         SConscript('src/SConscript', build_dir='build/' + os)
148     </file>
149   </scons_example>
150
151   <scons_output example="ex_var2">
152     <command>scons -Q</command>
153   </scons_output>
154
155   -->