Factor uses of build_pairwise_scheduling.
[official-gcc/Ramakrishna.git] / gcc / cp / g++spec.c
blob7ad268fabf98fc2f65994b10e0cfc8785b7e189c
1 /* Specific flags and argument handling of the C++ front end.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "gcc.h"
27 /* This bit is set if we saw a `-xfoo' language specification. */
28 #define LANGSPEC (1<<1)
29 /* This bit is set if they did `-lm' or `-lmath'. */
30 #define MATHLIB (1<<2)
31 /* This bit is set if they did `-lc'. */
32 #define WITHLIBC (1<<3)
33 /* Skip this option. */
34 #define SKIPOPT (1<<4)
36 #ifndef MATH_LIBRARY
37 #define MATH_LIBRARY "-lm"
38 #endif
39 #ifndef MATH_LIBRARY_PROFILE
40 #define MATH_LIBRARY_PROFILE MATH_LIBRARY
41 #endif
43 #ifndef LIBSTDCXX
44 #define LIBSTDCXX "-lstdc++"
45 #endif
46 #ifndef LIBSTDCXX_PROFILE
47 #define LIBSTDCXX_PROFILE LIBSTDCXX
48 #endif
49 #ifndef LIBSTDCXX_STATIC
50 #define LIBSTDCXX_STATIC NULL
51 #endif
53 void
54 lang_specific_driver (int *in_argc, const char *const **in_argv,
55 int *in_added_libraries)
57 int i, j;
59 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
60 int saw_profile_flag = 0;
62 /* If nonzero, the user gave us the `-v' flag. */
63 int saw_verbose_flag = 0;
65 /* What do with libstdc++:
66 -1 means we should not link in libstdc++
67 0 means we should link in libstdc++ if it is needed
68 1 means libstdc++ is needed and should be linked in.
69 2 means libstdc++ is needed and should be linked statically. */
70 int library = 0;
72 /* The number of arguments being added to what's in argv, other than
73 libraries. We use this to track the number of times we've inserted
74 -xc++/-xnone. */
75 int added = 0;
77 /* Used to track options that take arguments, so we don't go wrapping
78 those with -xc++/-xnone. */
79 const char *quote = NULL;
81 /* The new argument list will be contained in this. */
82 const char **arglist;
84 /* Nonzero if we saw a `-xfoo' language specification on the
85 command line. Used to avoid adding our own -xc++ if the user
86 already gave a language for the file. */
87 int saw_speclang = 0;
89 /* "-lm" or "-lmath" if it appears on the command line. */
90 const char *saw_math = 0;
92 /* "-lc" if it appears on the command line. */
93 const char *saw_libc = 0;
95 /* An array used to flag each argument that needs a bit set for
96 LANGSPEC, MATHLIB, or WITHLIBC. */
97 int *args;
99 /* By default, we throw on the math library if we have one. */
100 int need_math = (MATH_LIBRARY[0] != '\0');
102 /* True if we saw -static. */
103 int static_link = 0;
105 /* True if we should add -shared-libgcc to the command-line. */
106 int shared_libgcc = 1;
108 /* The total number of arguments with the new stuff. */
109 int argc;
111 /* The argument list. */
112 const char *const *argv;
114 /* The number of libraries added in. */
115 int added_libraries;
117 /* The total number of arguments with the new stuff. */
118 int num_args = 1;
120 argc = *in_argc;
121 argv = *in_argv;
122 added_libraries = *in_added_libraries;
124 args = XCNEWVEC (int, argc);
126 for (i = 1; i < argc; i++)
128 /* If the previous option took an argument, we swallow it here. */
129 if (quote)
131 quote = NULL;
132 continue;
135 /* We don't do this anymore, since we don't get them with minus
136 signs on them. */
137 if (argv[i][0] == '\0' || argv[i][1] == '\0')
138 continue;
140 if (argv[i][0] == '-')
142 if (strcmp (argv[i], "-nostdlib") == 0
143 || strcmp (argv[i], "-nodefaultlibs") == 0)
145 library = -1;
147 else if (strcmp (argv[i], MATH_LIBRARY) == 0)
149 args[i] |= MATHLIB;
150 need_math = 0;
152 else if (strcmp (argv[i], "-lc") == 0)
153 args[i] |= WITHLIBC;
154 else if (strcmp (argv[i], "-pg") == 0 || strcmp (argv[i], "-p") == 0)
155 saw_profile_flag++;
156 else if (strcmp (argv[i], "-v") == 0)
157 saw_verbose_flag = 1;
158 else if (strncmp (argv[i], "-x", 2) == 0)
160 const char * arg;
161 if (argv[i][2] != '\0')
162 arg = argv[i]+2;
163 else if ((argv[i+1]) != NULL)
164 /* We need to swallow arg on next loop. */
165 quote = arg = argv[i+1];
166 else /* Error condition, message will be printed later. */
167 arg = "";
168 if (library == 0
169 && (strcmp (arg, "c++") == 0
170 || strcmp (arg, "c++-cpp-output") == 0
171 || strcmp (arg, "objective-c++") == 0
172 || strcmp (arg, "objective-c++-cpp-output") == 0))
173 library = 1;
175 saw_speclang = 1;
177 else if (strcmp (argv[i], "-ObjC++") == 0)
179 if (library == 0)
180 library = 1;
181 saw_speclang = 1;
183 /* Arguments that go directly to the linker might be .o files,
184 or something, and so might cause libstdc++ to be needed. */
185 else if (strcmp (argv[i], "-Xlinker") == 0)
187 quote = argv[i];
188 if (library == 0)
189 library = 1;
191 else if (strncmp (argv[i], "-Wl,", 4) == 0)
192 library = (library == 0) ? 1 : library;
193 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
194 else if (strncmp (argv[i], "-l", 2) == 0)
195 library = (library == 0) ? 1 : library;
196 else if (((argv[i][2] == '\0'
197 && strchr ("bBVDUoeTuIYmLiA", argv[i][1]) != NULL)
198 || strcmp (argv[i], "-Tdata") == 0))
199 quote = argv[i];
200 else if ((argv[i][2] == '\0'
201 && strchr ("cSEM", argv[i][1]) != NULL)
202 || strcmp (argv[i], "-MM") == 0
203 || strcmp (argv[i], "-fsyntax-only") == 0)
205 /* Don't specify libraries if we won't link, since that would
206 cause a warning. */
207 library = -1;
209 else if (strcmp (argv[i], "-static") == 0)
210 static_link = 1;
211 else if (strcmp (argv[i], "-static-libgcc") == 0)
212 shared_libgcc = 0;
213 else if (strcmp (argv[i], "-static-libstdc++") == 0)
215 library = library >= 0 ? 2 : library;
216 args[i] |= SKIPOPT;
218 else if (DEFAULT_WORD_SWITCH_TAKES_ARG (&argv[i][1]))
219 i++;
220 else
221 /* Pass other options through. */
222 continue;
224 else
226 int len;
228 if (saw_speclang)
230 saw_speclang = 0;
231 continue;
234 /* If the filename ends in .[chi], put options around it.
235 But not if a specified -x option is currently active. */
236 len = strlen (argv[i]);
237 if (len > 2
238 && (argv[i][len - 1] == 'c'
239 || argv[i][len - 1] == 'i'
240 || argv[i][len - 1] == 'h')
241 && argv[i][len - 2] == '.')
243 args[i] |= LANGSPEC;
244 added += 2;
247 /* If we don't know that this is a header file, we might
248 need to be linking in the libraries. */
249 if (library == 0)
251 if ((len <= 2 || strcmp (argv[i] + (len - 2), ".H") != 0)
252 && (len <= 2 || strcmp (argv[i] + (len - 2), ".h") != 0)
253 && (len <= 4 || strcmp (argv[i] + (len - 4), ".hpp") != 0)
254 && (len <= 3 || strcmp (argv[i] + (len - 3), ".hp") != 0)
255 && (len <= 4 || strcmp (argv[i] + (len - 4), ".hxx") != 0)
256 && (len <= 4 || strcmp (argv[i] + (len - 4), ".h++") != 0)
257 && (len <= 4 || strcmp (argv[i] + (len - 4), ".HPP") != 0)
258 && (len <= 4 || strcmp (argv[i] + (len - 4), ".tcc") != 0)
259 && (len <= 3 || strcmp (argv[i] + (len - 3), ".hh") != 0))
260 library = 1;
265 if (quote)
266 fatal ("argument to '%s' missing\n", quote);
268 /* There's no point adding -shared-libgcc if we don't have a shared
269 libgcc. */
270 #ifndef ENABLE_SHARED_LIBGCC
271 shared_libgcc = 0;
272 #endif
274 /* Make sure to have room for the trailing NULL argument.
275 Add one for shared_libgcc or extra static library. */
276 num_args = argc + added + need_math + (library > 0) * 4 + 2;
277 arglist = XNEWVEC (const char *, num_args);
279 i = 0;
280 j = 0;
282 /* Copy the 0th argument, i.e., the name of the program itself. */
283 arglist[i++] = argv[j++];
285 /* NOTE: We start at 1 now, not 0. */
286 while (i < argc)
288 arglist[j] = argv[i];
290 /* Make sure -lstdc++ is before the math library, since libstdc++
291 itself uses those math routines. */
292 if (!saw_math && (args[i] & MATHLIB) && library > 0)
294 --j;
295 saw_math = argv[i];
298 if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
300 --j;
301 saw_libc = argv[i];
304 /* Wrap foo.[chi] files in a language specification to
305 force the gcc compiler driver to run cc1plus on them. */
306 if (args[i] & LANGSPEC)
308 int len = strlen (argv[i]);
309 switch (argv[i][len - 1])
311 case 'c':
312 arglist[j++] = "-xc++";
313 break;
314 case 'i':
315 arglist[j++] = "-xc++-cpp-output";
316 break;
317 case 'h':
318 arglist[j++] = "-xc++-header";
319 break;
320 default:
321 gcc_unreachable ();
323 arglist[j++] = argv[i];
324 arglist[j] = "-xnone";
327 if ((args[i] & SKIPOPT) != 0)
328 --j;
330 i++;
331 j++;
334 /* Add `-lstdc++' if we haven't already done so. */
335 if (library > 0)
337 #ifdef HAVE_LD_STATIC_DYNAMIC
338 if (library > 1 && !static_link)
340 arglist[j] = "-Wl,-Bstatic";
341 j++;
343 #endif
344 arglist[j] = saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX;
345 if (arglist[j][0] != '-' || arglist[j][1] == 'l')
346 added_libraries++;
347 j++;
348 /* Add target-dependent static library, if necessary. */
349 if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
351 arglist[j] = LIBSTDCXX_STATIC;
352 if (arglist[j][0] != '-' || arglist[j][1] == 'l')
353 added_libraries++;
354 j++;
356 #ifdef HAVE_LD_STATIC_DYNAMIC
357 if (library > 1 && !static_link)
359 arglist[j] = "-Wl,-Bdynamic";
360 j++;
362 #endif
364 if (saw_math)
365 arglist[j++] = saw_math;
366 else if (library > 0 && need_math)
368 arglist[j] = saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY;
369 if (arglist[j][0] != '-' || arglist[j][1] == 'l')
370 added_libraries++;
371 j++;
373 if (saw_libc)
374 arglist[j++] = saw_libc;
375 if (shared_libgcc && !static_link)
376 arglist[j++] = "-shared-libgcc";
378 arglist[j] = NULL;
380 *in_argc = j;
381 *in_argv = arglist;
382 *in_added_libraries = added_libraries;
385 /* Called before linking. Returns 0 on success and -1 on failure. */
386 int lang_specific_pre_link (void) /* Not used for C++. */
388 return 0;
391 /* Number of extra output files that lang_specific_pre_link may generate. */
392 int lang_specific_extra_outfiles = 0; /* Not used for C++. */