1 /* Specific flags and argument handling of the C++ front end.
2 Copyright (C) 1996-2013 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.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)
37 #define MATH_LIBRARY "m"
39 #ifndef MATH_LIBRARY_PROFILE
40 #define MATH_LIBRARY_PROFILE MATH_LIBRARY
44 #define LIBSTDCXX "stdc++"
46 #ifndef LIBSTDCXX_PROFILE
47 #define LIBSTDCXX_PROFILE LIBSTDCXX
49 #ifndef LIBSTDCXX_STATIC
50 #define LIBSTDCXX_STATIC NULL
54 lang_specific_driver (struct cl_decoded_option **in_decoded_options,
55 unsigned int *in_decoded_options_count,
56 int *in_added_libraries)
60 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
61 int saw_profile_flag = 0;
63 /* What do with libstdc++:
64 -1 means we should not link in libstdc++
65 0 means we should link in libstdc++ if it is needed
66 1 means libstdc++ is needed and should be linked in.
67 2 means libstdc++ is needed and should be linked statically. */
70 /* The number of arguments being added to what's in argv, other than
71 libraries. We use this to track the number of times we've inserted
75 /* The new argument list will be contained in this. */
76 struct cl_decoded_option *new_decoded_options;
78 /* Nonzero if we saw a `-xfoo' language specification on the
79 command line. Used to avoid adding our own -xc++ if the user
80 already gave a language for the file. */
83 /* "-lm" or "-lmath" if it appears on the command line. */
84 const struct cl_decoded_option *saw_math = NULL;
86 /* "-lc" if it appears on the command line. */
87 const struct cl_decoded_option *saw_libc = NULL;
89 /* An array used to flag each argument that needs a bit set for
90 LANGSPEC, MATHLIB, or WITHLIBC. */
93 /* By default, we throw on the math library if we have one. */
94 int need_math = (MATH_LIBRARY[0] != '\0');
96 /* True if we saw -static. */
99 /* True if we should add -shared-libgcc to the command-line. */
100 int shared_libgcc = 1;
102 /* The total number of arguments with the new stuff. */
105 /* The argument list. */
106 struct cl_decoded_option *decoded_options;
108 /* The number of libraries added in. */
111 /* The total number of arguments with the new stuff. */
112 unsigned int num_args = 1;
114 argc = *in_decoded_options_count;
115 decoded_options = *in_decoded_options;
116 added_libraries = *in_added_libraries;
118 args = XCNEWVEC (int, argc);
120 for (i = 1; i < argc; i++)
122 const char *arg = decoded_options[i].arg;
123 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
124 continue; /* Avoid examining arguments of options missing them. */
126 switch (decoded_options[i].opt_index)
129 case OPT_nodefaultlibs:
134 if (strcmp (arg, MATH_LIBRARY) == 0)
139 else if (strcmp (arg, "c") == 0)
142 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
143 library = (library == 0) ? 1 : library;
153 && (strcmp (arg, "c++") == 0
154 || strcmp (arg, "c++-cpp-output") == 0
155 || strcmp (arg, "objective-c++") == 0
156 || strcmp (arg, "objective-c++-cpp-output") == 0))
164 /* Arguments that go directly to the linker might be .o files,
165 or something, and so might cause libstdc++ to be needed. */
175 case OPT_fsyntax_only:
176 /* Don't specify libraries if we won't link, since that would
185 case OPT_static_libgcc:
189 case OPT_static_libstdc__:
190 library = library >= 0 ? 2 : library;
194 case OPT_SPECIAL_input_file:
198 /* We don't do this anymore, since we don't get them with minus
200 if (arg[0] == '\0' || arg[1] == '\0')
209 /* If the filename ends in .[chi], put options around it.
210 But not if a specified -x option is currently active. */
213 && (arg[len - 1] == 'c'
214 || arg[len - 1] == 'i'
215 || arg[len - 1] == 'h')
216 && arg[len - 2] == '.')
222 /* If we don't know that this is a header file, we might
223 need to be linking in the libraries. */
226 if ((len <= 2 || strcmp (arg + (len - 2), ".H") != 0)
227 && (len <= 2 || strcmp (arg + (len - 2), ".h") != 0)
228 && (len <= 4 || strcmp (arg + (len - 4), ".hpp") != 0)
229 && (len <= 3 || strcmp (arg + (len - 3), ".hp") != 0)
230 && (len <= 4 || strcmp (arg + (len - 4), ".hxx") != 0)
231 && (len <= 4 || strcmp (arg + (len - 4), ".h++") != 0)
232 && (len <= 4 || strcmp (arg + (len - 4), ".HPP") != 0)
233 && (len <= 4 || strcmp (arg + (len - 4), ".tcc") != 0)
234 && (len <= 3 || strcmp (arg + (len - 3), ".hh") != 0))
242 /* There's no point adding -shared-libgcc if we don't have a shared
244 #ifndef ENABLE_SHARED_LIBGCC
248 /* Add one for shared_libgcc or extra static library. */
249 num_args = argc + added + need_math + (library > 0) * 4 + 1;
250 new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
255 /* Copy the 0th argument, i.e., the name of the program itself. */
256 new_decoded_options[j++] = decoded_options[i++];
258 /* NOTE: We start at 1 now, not 0. */
261 new_decoded_options[j] = decoded_options[i];
263 /* Make sure -lstdc++ is before the math library, since libstdc++
264 itself uses those math routines. */
265 if (!saw_math && (args[i] & MATHLIB) && library > 0)
268 saw_math = &decoded_options[i];
271 if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
274 saw_libc = &decoded_options[i];
277 /* Wrap foo.[chi] files in a language specification to
278 force the gcc compiler driver to run cc1plus on them. */
279 if (args[i] & LANGSPEC)
281 const char *arg = decoded_options[i].arg;
282 int len = strlen (arg);
283 switch (arg[len - 1])
286 generate_option (OPT_x, "c++", 1, CL_DRIVER,
287 &new_decoded_options[j++]);
290 generate_option (OPT_x, "c++-cpp-output", 1, CL_DRIVER,
291 &new_decoded_options[j++]);
294 generate_option (OPT_x, "c++-header", 1, CL_DRIVER,
295 &new_decoded_options[j++]);
300 new_decoded_options[j++] = decoded_options[i];
301 generate_option (OPT_x, "none", 1, CL_DRIVER,
302 &new_decoded_options[j]);
305 if ((args[i] & SKIPOPT) != 0)
312 /* Add `-lstdc++' if we haven't already done so. */
315 #ifdef HAVE_LD_STATIC_DYNAMIC
316 if (library > 1 && !static_link)
318 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
319 &new_decoded_options[j]);
323 generate_option (OPT_l,
324 saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX, 1,
325 CL_DRIVER, &new_decoded_options[j]);
328 /* Add target-dependent static library, if necessary. */
329 if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
331 generate_option (OPT_l, LIBSTDCXX_STATIC, 1,
332 CL_DRIVER, &new_decoded_options[j]);
336 #ifdef HAVE_LD_STATIC_DYNAMIC
337 if (library > 1 && !static_link)
339 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
340 &new_decoded_options[j]);
346 new_decoded_options[j++] = *saw_math;
347 else if (library > 0 && need_math)
349 generate_option (OPT_l,
350 saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
351 1, CL_DRIVER, &new_decoded_options[j]);
356 new_decoded_options[j++] = *saw_libc;
357 if (shared_libgcc && !static_link)
358 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
359 &new_decoded_options[j++]);
361 *in_decoded_options_count = j;
362 *in_decoded_options = new_decoded_options;
363 *in_added_libraries = added_libraries;
366 /* Called before linking. Returns 0 on success and -1 on failure. */
367 int lang_specific_pre_link (void) /* Not used for C++. */
372 /* Number of extra output files that lang_specific_pre_link may generate. */
373 int lang_specific_extra_outfiles = 0; /* Not used for C++. */