Initialize the new branch.
[scons.git] / doc / user / variants.sgml
1 <!--
2
3   Copyright (c) 2001, 2002, 2003 Steven Knight
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   <programlisting>
64     platform = ARGUMENTS.get('OS', Platform())
65
66     include = "#export/$PLATFORM/include"
67     lib = "#export/$PLATFORM/lib"
68     bin = "#export/$PLATFORM/bin"
69
70     env = Environment(PLATFORM = platform,
71                       BINDIR = bin,
72                       INCDIR = include,
73                       LIBDIR = lib,
74                       CPPPATH = [include],
75                       LIBPATH = [lib],
76                       LIBS = 'world')
77
78     Export('env')
79
80     env.SConscript('src/SConscript', build_dir='build/$PLATFORM')
81
82     #
83     #BuildDir("#build/$PLATFORM", 'src')
84     #SConscript("build/$PLATFORM/hello/SConscript")
85     #SConscript("build/$PLATFORM/world/SConscript")
86   </programlisting>
87
88   <para>
89
90   This SConstruct file,
91   when run on a Linux system, yields:
92
93   </para>
94
95   <literallayout>
96     % <userinput>scons -Q OS=linux</userinput>
97     Install file: "build/linux/world/world.h" as "export/linux/include/world.h"
98     cc -Iexport/linux/include -c -o build/linux/hello/hello.o build/linux/hello/hello.c
99     cc -Iexport/linux/include -c -o build/linux/world/world.o build/linux/world/world.c
100     ar r build/linux/world/libworld.a build/linux/world/world.o
101     ranlib build/linux/world/libworld.a
102     Install file: "build/linux/world/libworld.a" as "export/linux/lib/libworld.a"
103     cc -o build/linux/hello/hello build/linux/hello/hello.o -Lexport/linux/lib -lworld
104     Install file: "build/linux/hello/hello" as "export/linux/bin/hello"
105   </literallayout>
106
107   <para>
108
109   The same SConstruct file on Windows would build:
110
111   </para>
112
113   <literallayout>
114     C:\><userinput>scons -Q OS=windows</userinput>
115     Install file: "build/windows/world/world.h" as "export/windows/include/world.h"
116     cl /nologo /Iexport\windows\include /c build\windows\hello\hello.c /Fobuild\windows\hello\hello.obj
117     cl /nologo /Iexport\windows\include /c build\windows\world\world.c /Fobuild\windows\world\world.obj
118     lib /nologo /OUT:build\windows\world\world.lib build\windows\world\world.obj
119     Install file: "build/windows/world/world.lib" as "export/windows/lib/world.lib"
120     link /nologo /OUT:build\windows\hello\hello.exe /LIBPATH:export\windows\lib world.lib build\windows\hello\hello.obj
121     Install file: "build/windows/hello/hello.exe" as "export/windows/bin/hello.exe"
122   </literallayout>
123
124   <!--
125
126   <scons_example name="ex_var2">
127     <file name="SConstruct" printme="1">
128     env = Environment(OS = ARGUMENTS.get('OS'))
129     for os in ['newell', 'post']:
130         SConscript('src/SConscript', build_dir='build/' + os)
131     </file>
132   </scons_example>
133
134   <scons_output example="ex_var2">
135     <command>scons -Q</command>
136   </scons_output>
137
138   -->