http://scons.tigris.org/issues/show_bug.cgi?id=2329
[scons.git] / doc / user / parseflags.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  <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  <scons_example name="ParseFlags1">
61    <file name="SConstruct" printme="1">
62     env = Environment()
63     d = env.ParseFlags("-I/opt/include -L/opt/lib -lfoo")
64     for k,v in sorted(d.items()):
65         if v:
66             print k, v
67     env.MergeFlags(d)
68     env.Program('f1.c')
69    </file>
70    <file name="f1.c">
71      int main() { return 0; }
72    </file>
73  </scons_example>
74
75  <scons_output example="ParseFlags1" os="posix">
76     <scons_output_command>scons -Q</scons_output_command>
77  </scons_output>
78
79  <para>
80
81  Note that if the options are limited to generic types
82  like those above,
83  they will be correctly translated for other platform types:
84
85  </para>
86
87  <scons_output example="ParseFlags1" os="win32">
88     <scons_output_command>scons -Q</scons_output_command>
89  </scons_output>
90
91  <para>
92
93  Since the assumption is that the flags are used for the GCC toolchain,
94  unrecognized flags are placed in &cv-link-CCFLAGS;
95  so they will be used for both C and C++ compiles:
96
97  </para>
98
99  <scons_example name="ParseFlags2">
100    <file name="SConstruct" printme="1">
101     env = Environment()
102     d = env.ParseFlags("-whatever")
103     for k,v in sorted(d.items()):
104         if v:
105             print k, v
106     env.MergeFlags(d)
107     env.Program('f1.c')
108    </file>
109    <file name="f1.c">
110      int main() { return 0; }
111    </file>
112  </scons_example>
113
114  <scons_output example="ParseFlags2">
115     <scons_output_command>scons -Q</scons_output_command>
116  </scons_output>
117
118  <para>
119
120  &ParseFlags; will also accept a (recursive) list of strings as input;
121  the list is flattened before the strings are processed:
122
123  </para>
124
125  <scons_example name="ParseFlags3">
126    <file name="SConstruct" printme="1">
127     env = Environment()
128     d = env.ParseFlags(["-I/opt/include", ["-L/opt/lib", "-lfoo"]])
129     for k,v in sorted(d.items()):
130         if v:
131             print k, v
132     env.MergeFlags(d)
133     env.Program('f1.c')
134    </file>
135    <file name="f1.c">
136      int main() { return 0; }
137    </file>
138  </scons_example>
139
140  <scons_output example="ParseFlags3">
141     <scons_output_command>scons -Q</scons_output_command>
142  </scons_output>
143
144  <para>
145
146  If a string begins with a "!" (an exclamation mark, often called a bang),
147  the string is passed to the shell for execution.
148  The output of the command is then parsed:
149
150  </para>
151
152  <scons_example name="ParseFlags4">
153    <file name="SConstruct" printme="1">
154     env = Environment()
155     d = env.ParseFlags(["!echo -I/opt/include", "!echo -L/opt/lib", "-lfoo"])
156     for k,v in sorted(d.items()):
157         if v:
158             print k, v
159     env.MergeFlags(d)
160     env.Program('f1.c')
161    </file>
162    <file name="f1.c">
163      int main() { return 0; }
164    </file>
165  </scons_example>
166
167  <scons_output example="ParseFlags4">
168     <scons_output_command>scons -Q</scons_output_command>
169  </scons_output>
170
171  <para>
172
173  &ParseFlags; is regularly updated for new options;
174  consult the man page for details about those currently recognized.
175
176  </para>