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