09533d24cf16371262ae735c0ea4fc65a795c51c
[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     for k,v in sorted(d.items()):
64         if v:
65             print k, v
66     env.MergeFlags(d)
67     env.Program('f1.c')
68  </programlisting>
69
70  <screen>
71     % <userinput>scons -Q</userinput>
72     CPPPATH ['/opt/include']
73     LIBPATH ['/opt/lib']
74     LIBS ['foo']
75     cc -o f1.o -c -I/opt/include f1.c
76     cc -o f1 f1.o -L/opt/lib -lfoo
77  </screen>
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  <screen>
88     C:\><userinput>scons -Q</userinput>
89     CPPPATH ['/opt/include']
90     LIBPATH ['/opt/lib']
91     LIBS ['foo']
92     cl /Fof1.obj /c f1.c /nologo /I\opt\include
93     link /nologo /OUT:f1.exe /LIBPATH:\opt\lib foo.lib f1.obj
94  </screen>
95
96  <para>
97
98  Since the assumption is that the flags are used for the GCC toolchain,
99  unrecognized flags are placed in &cv-link-CCFLAGS;
100  so they will be used for both C and C++ compiles:
101
102  </para>
103
104  <programlisting>
105     env = Environment()
106     d = env.ParseFlags("-whatever")
107     for k,v in sorted(d.items()):
108         if v:
109             print k, v
110     env.MergeFlags(d)
111     env.Program('f1.c')
112  </programlisting>
113
114  <screen>
115     % <userinput>scons -Q</userinput>
116     CCFLAGS -whatever
117     cc -o f1.o -c -whatever f1.c
118     cc -o f1 f1.o
119  </screen>
120
121  <para>
122
123  &ParseFlags; will also accept a (recursive) list of strings as input;
124  the list is flattened before the strings are processed:
125
126  </para>
127
128  <programlisting>
129     env = Environment()
130     d = env.ParseFlags(["-I/opt/include", ["-L/opt/lib", "-lfoo"]])
131     for k,v in sorted(d.items()):
132         if v:
133             print k, v
134     env.MergeFlags(d)
135     env.Program('f1.c')
136  </programlisting>
137
138  <screen>
139     % <userinput>scons -Q</userinput>
140     CPPPATH ['/opt/include']
141     LIBPATH ['/opt/lib']
142     LIBS ['foo']
143     cc -o f1.o -c -I/opt/include f1.c
144     cc -o f1 f1.o -L/opt/lib -lfoo
145  </screen>
146
147  <para>
148
149  If a string begins with a "!" (an exclamation mark, often called a bang),
150  the string is passed to the shell for execution.
151  The output of the command is then parsed:
152
153  </para>
154
155  <programlisting>
156     env = Environment()
157     d = env.ParseFlags(["!echo -I/opt/include", "!echo -L/opt/lib", "-lfoo"])
158     for k,v in l:
159         if v:
160             print k, v
161     env.MergeFlags(d)
162     env.Program('f1.c')
163  </programlisting>
164
165  <screen>
166     % <userinput>scons -Q</userinput>
167     CPPPATH ['/opt/include']
168     LIBPATH ['/opt/lib']
169     LIBS ['foo']
170     cc -o f1.o -c -I/opt/include f1.c
171     cc -o f1 f1.o -L/opt/lib -lfoo
172  </screen>
173
174  <para>
175
176  &ParseFlags; is regularly updated for new options;
177  consult the man page for details about those currently recognized.
178
179  </para>