632077e5f141e4febd9621a23291557219b88c44
[scons.git] / doc / user / parseflags.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  <para>
27
28  &SCons; has a bewildering array of construction variables
29  for different types of options when building programs.
30  Sometimes you may not know exactly which variable
31  should be used for a particular option.
32
33  </para>
34
35  <para>
36
37  &SCons; construction environments have a &ParseFlags; method
38  that takes a set of typical command-line options
39  and distrbutes them into the appropriate construction variables.
40  Historically, it was created to support the &ParseConfig; method,
41  so it focuses on options used by the GNU Compiler Collection (GCC)
42  for the C and C++ toolchains.
43
44  </para>
45
46  <para>
47
48  &ParseFlags; returns a dictionary containing the options
49  distributed into their respective construction variables.
50  Normally, this dictionary would be passed to &MergeFlags;
51  to merge the options into a &consenv;,
52  but the dictionary can be edited if desired to provide
53  additional functionality.
54  (Note that if the flags are not going to be edited,
55  calling &MergeFlags; with the options directly
56  will avoid an additional step.)
57
58  </para>
59
60  <programlisting>
61     env = Environment()
62     d = env.ParseFlags("-I/opt/include -L/opt/lib -lfoo")
63     l = d.items()
64     l.sort()
65     for k,v in l:
66         if v:
67             print k, v
68     env.MergeFlags(d)
69     env.Program('f1.c')
70  </programlisting>
71
72  <screen>
73     % <userinput>scons -Q</userinput>
74     CPPPATH ['/opt/include']
75     LIBPATH ['/opt/lib']
76     LIBS ['foo']
77     cc -o f1.o -c -I/opt/include f1.c
78     cc -o f1 f1.o -L/opt/lib -lfoo
79  </screen>
80
81  <para>
82
83  Note that if the options are limited to generic types
84  like those above,
85  they will be correctly translated for other platform types:
86
87  </para>
88
89  <screen>
90     C:\><userinput>scons -Q</userinput>
91     CPPPATH ['/opt/include']
92     LIBPATH ['/opt/lib']
93     LIBS ['foo']
94     cl /Fof1.obj /c f1.c /nologo /I\opt\include
95     link /nologo /OUT:f1.exe /LIBPATH:\opt\lib foo.lib f1.obj
96  </screen>
97
98  <para>
99
100  Since the assumption is that the flags are used for the GCC toolchain,
101  unrecognized flags are placed in &cv-link-CCFLAGS;
102  so they will be used for both C and C++ compiles:
103
104  </para>
105
106  <programlisting>
107     env = Environment()
108     d = env.ParseFlags("-whatever")
109     l = d.items()
110     l.sort()
111     for k,v in l:
112         if v:
113             print k, v
114     env.MergeFlags(d)
115     env.Program('f1.c')
116  </programlisting>
117
118  <screen>
119     % <userinput>scons -Q</userinput>
120     CCFLAGS -whatever
121     cc -o f1.o -c -whatever f1.c
122     cc -o f1 f1.o
123  </screen>
124
125  <para>
126
127  &ParseFlags; will also accept a (recursive) list of strings as input;
128  the list is flattened before the strings are processed:
129
130  </para>
131
132  <programlisting>
133     env = Environment()
134     d = env.ParseFlags(["-I/opt/include", ["-L/opt/lib", "-lfoo"]])
135     l = d.items()
136     l.sort()
137     for k,v in l:
138         if v:
139             print k, v
140     env.MergeFlags(d)
141     env.Program('f1.c')
142  </programlisting>
143
144  <screen>
145     % <userinput>scons -Q</userinput>
146     CPPPATH ['/opt/include']
147     LIBPATH ['/opt/lib']
148     LIBS ['foo']
149     cc -o f1.o -c -I/opt/include f1.c
150     cc -o f1 f1.o -L/opt/lib -lfoo
151  </screen>
152
153  <para>
154
155  If a string begins with a "!" (an exclamation mark, often called a bang),
156  the string is passed to the shell for execution.
157  The output of the command is then parsed:
158
159  </para>
160
161  <programlisting>
162     env = Environment()
163     d = env.ParseFlags(["!echo -I/opt/include", "!echo -L/opt/lib", "-lfoo"])
164     l = d.items()
165     l.sort()
166     for k,v in l:
167         if v:
168             print k, v
169     env.MergeFlags(d)
170     env.Program('f1.c')
171  </programlisting>
172
173  <screen>
174     % <userinput>scons -Q</userinput>
175     CPPPATH ['/opt/include']
176     LIBPATH ['/opt/lib']
177     LIBS ['foo']
178     cc -o f1.o -c -I/opt/include f1.c
179     cc -o f1 f1.o -L/opt/lib -lfoo
180  </screen>
181
182  <para>
183
184  &ParseFlags; is regularly updated for new options;
185  consult the man page for details about those currently recognized.
186
187  </para>