]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/g++spec.c
configure.ac (gcc_cv_ld_static_option): Define.
[thirdparty/gcc.git] / gcc / cp / g++spec.c
CommitLineData
3b426391 1/* Specific flags and argument handling of the C++ front end.
e77f031d 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
c6092243 3 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5b76d03b 4
f5adbb8d 5This file is part of GCC.
5b76d03b 6
f5adbb8d 7GCC is free software; you can redistribute it and/or modify
5b76d03b 8it under the terms of the GNU General Public License as published by
e77f031d 9the Free Software Foundation; either version 3, or (at your option)
5b76d03b
BK
10any later version.
11
f5adbb8d 12GCC is distributed in the hope that it will be useful,
5b76d03b
BK
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
e77f031d
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
5b76d03b 20
da20811c 21#include "config.h"
8d052bc7 22#include "system.h"
4977bab6
ZW
23#include "coretypes.h"
24#include "tm.h"
9257393c 25#include "gcc.h"
d9d16a19 26#include "opts.h"
2ba25f50 27
f442f723
MS
28/* This bit is set if we saw a `-xfoo' language specification. */
29#define LANGSPEC (1<<1)
30/* This bit is set if they did `-lm' or `-lmath'. */
31#define MATHLIB (1<<2)
32/* This bit is set if they did `-lc'. */
33#define WITHLIBC (1<<3)
96be7a11
ILT
34/* Skip this option. */
35#define SKIPOPT (1<<4)
f442f723
MS
36
37#ifndef MATH_LIBRARY
d9d16a19 38#define MATH_LIBRARY "m"
f442f723 39#endif
271f61fb 40#ifndef MATH_LIBRARY_PROFILE
61fec9ff 41#define MATH_LIBRARY_PROFILE MATH_LIBRARY
271f61fb 42#endif
f442f723 43
18cd4ded 44#ifndef LIBSTDCXX
d9d16a19 45#define LIBSTDCXX "stdc++"
18cd4ded 46#endif
271f61fb 47#ifndef LIBSTDCXX_PROFILE
4afd7f6c 48#define LIBSTDCXX_PROFILE LIBSTDCXX
271f61fb 49#endif
ce75e1dd 50#ifndef LIBSTDCXX_STATIC
a322799d 51#define LIBSTDCXX_STATIC NULL
ce75e1dd 52#endif
18cd4ded 53
f442f723 54void
d9d16a19
JM
55lang_specific_driver (struct cl_decoded_option **in_decoded_options,
56 unsigned int *in_decoded_options_count,
5671bf27 57 int *in_added_libraries)
f442f723 58{
d9d16a19 59 unsigned int i, j;
f442f723 60
838dfd8a 61 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
271f61fb
DB
62 int saw_profile_flag = 0;
63
96be7a11 64 /* What do with libstdc++:
17211ab5
GK
65 -1 means we should not link in libstdc++
66 0 means we should link in libstdc++ if it is needed
96be7a11
ILT
67 1 means libstdc++ is needed and should be linked in.
68 2 means libstdc++ is needed and should be linked statically. */
17211ab5 69 int library = 0;
f442f723
MS
70
71 /* The number of arguments being added to what's in argv, other than
72 libraries. We use this to track the number of times we've inserted
73 -xc++/-xnone. */
17211ab5 74 int added = 0;
f442f723 75
f442f723 76 /* The new argument list will be contained in this. */
d9d16a19 77 struct cl_decoded_option *new_decoded_options;
f442f723 78
838dfd8a 79 /* Nonzero if we saw a `-xfoo' language specification on the
f442f723
MS
80 command line. Used to avoid adding our own -xc++ if the user
81 already gave a language for the file. */
82 int saw_speclang = 0;
83
84 /* "-lm" or "-lmath" if it appears on the command line. */
d9d16a19 85 const struct cl_decoded_option *saw_math = NULL;
f442f723
MS
86
87 /* "-lc" if it appears on the command line. */
d9d16a19 88 const struct cl_decoded_option *saw_libc = NULL;
f442f723
MS
89
90 /* An array used to flag each argument that needs a bit set for
91 LANGSPEC, MATHLIB, or WITHLIBC. */
92 int *args;
93
b54ccf71
JM
94 /* By default, we throw on the math library if we have one. */
95 int need_math = (MATH_LIBRARY[0] != '\0');
f442f723 96
96be7a11
ILT
97 /* True if we saw -static. */
98 int static_link = 0;
99
049f6ec9
MM
100 /* True if we should add -shared-libgcc to the command-line. */
101 int shared_libgcc = 1;
102
f442f723 103 /* The total number of arguments with the new stuff. */
d9d16a19 104 unsigned int argc;
f442f723
MS
105
106 /* The argument list. */
d9d16a19 107 struct cl_decoded_option *decoded_options;
f442f723 108
84914634
JW
109 /* The number of libraries added in. */
110 int added_libraries;
111
f442f723 112 /* The total number of arguments with the new stuff. */
d9d16a19 113 unsigned int num_args = 1;
f442f723 114
d9d16a19
JM
115 argc = *in_decoded_options_count;
116 decoded_options = *in_decoded_options;
84914634 117 added_libraries = *in_added_libraries;
f442f723 118
67f5655f 119 args = XCNEWVEC (int, argc);
f442f723 120
1bf2b2d2 121 for (i = 1; i < argc; i++)
f442f723 122 {
d9d16a19
JM
123 const char *arg = decoded_options[i].arg;
124 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
125 continue; /* Avoid examining arguments of options missing them. */
f442f723 126
d9d16a19 127 switch (decoded_options[i].opt_index)
f442f723 128 {
d9d16a19
JM
129 case OPT_nostdlib:
130 case OPT_nodefaultlibs:
131 library = -1;
132 break;
133
134 case OPT_l:
135 if (strcmp (arg, MATH_LIBRARY) == 0)
f442f723
MS
136 {
137 args[i] |= MATHLIB;
138 need_math = 0;
139 }
d9d16a19 140 else if (strcmp (arg, "c") == 0)
f442f723 141 args[i] |= WITHLIBC;
d9d16a19
JM
142 else
143 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
144 library = (library == 0) ? 1 : library;
145 break;
146
147 case OPT_pg:
148 case OPT_p:
149 saw_profile_flag++;
150 break;
151
152 case OPT_x:
153 if (library == 0
154 && (strcmp (arg, "c++") == 0
155 || strcmp (arg, "c++-cpp-output") == 0
156 || strcmp (arg, "objective-c++") == 0
157 || strcmp (arg, "objective-c++-cpp-output") == 0))
158 library = 1;
a073516d 159
d9d16a19
JM
160 saw_speclang = 1;
161 break;
162
163 case OPT_Xlinker:
164 case OPT_Wl_:
01e05466
GK
165 /* Arguments that go directly to the linker might be .o files,
166 or something, and so might cause libstdc++ to be needed. */
d9d16a19
JM
167 if (library == 0)
168 library = 1;
169 break;
170
171 case OPT_c:
172 case OPT_S:
173 case OPT_E:
174 case OPT_M:
175 case OPT_MM:
176 case OPT_fsyntax_only:
177 /* Don't specify libraries if we won't link, since that would
178 cause a warning. */
179 library = -1;
180 break;
181
182 case OPT_static:
183 static_link = 1;
184 break;
185
186 case OPT_static_libgcc:
187 shared_libgcc = 0;
188 break;
189
190 case OPT_static_libstdc__:
191 library = library >= 0 ? 2 : library;
192 args[i] |= SKIPOPT;
193 break;
194
195 case OPT_SPECIAL_input_file:
196 {
197 int len;
198
199 /* We don't do this anymore, since we don't get them with minus
200 signs on them. */
201 if (arg[0] == '\0' || arg[1] == '\0')
f442f723 202 continue;
f442f723 203
d9d16a19
JM
204 if (saw_speclang)
205 {
206 saw_speclang = 0;
207 continue;
208 }
209
210 /* If the filename ends in .[chi], put options around it.
211 But not if a specified -x option is currently active. */
212 len = strlen (arg);
213 if (len > 2
214 && (arg[len - 1] == 'c'
215 || arg[len - 1] == 'i'
216 || arg[len - 1] == 'h')
217 && arg[len - 2] == '.')
218 {
219 args[i] |= LANGSPEC;
220 added += 2;
221 }
222
223 /* If we don't know that this is a header file, we might
224 need to be linking in the libraries. */
225 if (library == 0)
226 {
227 if ((len <= 2 || strcmp (arg + (len - 2), ".H") != 0)
228 && (len <= 2 || strcmp (arg + (len - 2), ".h") != 0)
229 && (len <= 4 || strcmp (arg + (len - 4), ".hpp") != 0)
230 && (len <= 3 || strcmp (arg + (len - 3), ".hp") != 0)
231 && (len <= 4 || strcmp (arg + (len - 4), ".hxx") != 0)
232 && (len <= 4 || strcmp (arg + (len - 4), ".h++") != 0)
233 && (len <= 4 || strcmp (arg + (len - 4), ".HPP") != 0)
234 && (len <= 4 || strcmp (arg + (len - 4), ".tcc") != 0)
235 && (len <= 3 || strcmp (arg + (len - 3), ".hh") != 0))
236 library = 1;
237 }
238 }
239 break;
f442f723
MS
240 }
241 }
242
049f6ec9
MM
243 /* There's no point adding -shared-libgcc if we don't have a shared
244 libgcc. */
245#ifndef ENABLE_SHARED_LIBGCC
246 shared_libgcc = 0;
247#endif
248
d9d16a19
JM
249 /* Add one for shared_libgcc or extra static library. */
250 num_args = argc + added + need_math + (library > 0) * 4 + 1;
251 new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
f442f723 252
834c6dff
MM
253 i = 0;
254 j = 0;
c8094d83 255
834c6dff 256 /* Copy the 0th argument, i.e., the name of the program itself. */
d9d16a19 257 new_decoded_options[j++] = decoded_options[i++];
834c6dff 258
f442f723 259 /* NOTE: We start at 1 now, not 0. */
834c6dff 260 while (i < argc)
f442f723 261 {
d9d16a19 262 new_decoded_options[j] = decoded_options[i];
f442f723
MS
263
264 /* Make sure -lstdc++ is before the math library, since libstdc++
265 itself uses those math routines. */
17211ab5 266 if (!saw_math && (args[i] & MATHLIB) && library > 0)
f442f723
MS
267 {
268 --j;
d9d16a19 269 saw_math = &decoded_options[i];
f442f723
MS
270 }
271
17211ab5 272 if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
f442f723
MS
273 {
274 --j;
d9d16a19 275 saw_libc = &decoded_options[i];
f442f723
MS
276 }
277
17211ab5 278 /* Wrap foo.[chi] files in a language specification to
f442f723
MS
279 force the gcc compiler driver to run cc1plus on them. */
280 if (args[i] & LANGSPEC)
281 {
d9d16a19
JM
282 const char *arg = decoded_options[i].arg;
283 int len = strlen (arg);
284 switch (arg[len - 1])
17211ab5
GK
285 {
286 case 'c':
d9d16a19
JM
287 generate_option (OPT_x, "c++", 1, CL_DRIVER,
288 &new_decoded_options[j++]);
17211ab5
GK
289 break;
290 case 'i':
d9d16a19
JM
291 generate_option (OPT_x, "c++-cpp-output", 1, CL_DRIVER,
292 &new_decoded_options[j++]);
17211ab5
GK
293 break;
294 case 'h':
d9d16a19
JM
295 generate_option (OPT_x, "c++-header", 1, CL_DRIVER,
296 &new_decoded_options[j++]);
17211ab5
GK
297 break;
298 default:
8dc2b103 299 gcc_unreachable ();
17211ab5 300 }
d9d16a19
JM
301 new_decoded_options[j++] = decoded_options[i];
302 generate_option (OPT_x, "none", 1, CL_DRIVER,
303 &new_decoded_options[j]);
f442f723 304 }
f442f723 305
96be7a11
ILT
306 if ((args[i] & SKIPOPT) != 0)
307 --j;
308
834c6dff
MM
309 i++;
310 j++;
311 }
eeb109f2 312
f442f723 313 /* Add `-lstdc++' if we haven't already done so. */
17211ab5 314 if (library > 0)
84914634 315 {
96be7a11
ILT
316#ifdef HAVE_LD_STATIC_DYNAMIC
317 if (library > 1 && !static_link)
318 {
c6092243 319 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
d9d16a19 320 &new_decoded_options[j]);
96be7a11
ILT
321 j++;
322 }
323#endif
d9d16a19
JM
324 generate_option (OPT_l,
325 saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX, 1,
326 CL_DRIVER, &new_decoded_options[j]);
327 added_libraries++;
a322799d 328 j++;
96be7a11
ILT
329 /* Add target-dependent static library, if necessary. */
330 if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
331 {
d9d16a19
JM
332 generate_option (OPT_l, LIBSTDCXX_STATIC, 1,
333 CL_DRIVER, &new_decoded_options[j]);
334 added_libraries++;
96be7a11
ILT
335 j++;
336 }
337#ifdef HAVE_LD_STATIC_DYNAMIC
338 if (library > 1 && !static_link)
339 {
c6092243 340 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
d9d16a19 341 &new_decoded_options[j]);
96be7a11
ILT
342 j++;
343 }
344#endif
84914634 345 }
f442f723 346 if (saw_math)
d9d16a19 347 new_decoded_options[j++] = *saw_math;
17211ab5 348 else if (library > 0 && need_math)
84914634 349 {
d9d16a19
JM
350 generate_option (OPT_l,
351 saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
352 1, CL_DRIVER, &new_decoded_options[j]);
353 added_libraries++;
4afd7f6c 354 j++;
84914634 355 }
f442f723 356 if (saw_libc)
d9d16a19 357 new_decoded_options[j++] = *saw_libc;
96be7a11 358 if (shared_libgcc && !static_link)
d9d16a19
JM
359 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
360 &new_decoded_options[j++]);
f442f723 361
d9d16a19
JM
362 *in_decoded_options_count = j;
363 *in_decoded_options = new_decoded_options;
84914634 364 *in_added_libraries = added_libraries;
f442f723 365}
240661ba 366
c6002625 367/* Called before linking. Returns 0 on success and -1 on failure. */
bf9d3c27 368int lang_specific_pre_link (void) /* Not used for C++. */
240661ba
PB
369{
370 return 0;
371}
372
c6002625
KH
373/* Number of extra output files that lang_specific_pre_link may generate. */
374int lang_specific_extra_outfiles = 0; /* Not used for C++. */