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, 2010 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)
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/>. */
23 #include "coretypes.h"
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)
34 /* Skip this option. */
35 #define SKIPOPT (1<<4)
38 #define MATH_LIBRARY "m"
40 #ifndef MATH_LIBRARY_PROFILE
41 #define MATH_LIBRARY_PROFILE MATH_LIBRARY
45 #define LIBSTDCXX "stdc++"
47 #ifndef LIBSTDCXX_PROFILE
48 #define LIBSTDCXX_PROFILE LIBSTDCXX
50 #ifndef LIBSTDCXX_STATIC
51 #define LIBSTDCXX_STATIC NULL
55 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
56 unsigned int *in_decoded_options_count
,
57 int *in_added_libraries
)
61 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
62 int saw_profile_flag
= 0;
64 /* What do with libstdc++:
65 -1 means we should not link in libstdc++
66 0 means we should link in libstdc++ if it is needed
67 1 means libstdc++ is needed and should be linked in.
68 2 means libstdc++ is needed and should be linked statically. */
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
76 /* The new argument list will be contained in this. */
77 struct cl_decoded_option
*new_decoded_options
;
79 /* Nonzero if we saw a `-xfoo' language specification on the
80 command line. Used to avoid adding our own -xc++ if the user
81 already gave a language for the file. */
84 /* "-lm" or "-lmath" if it appears on the command line. */
85 const struct cl_decoded_option
*saw_math
= NULL
;
87 /* "-lc" if it appears on the command line. */
88 const struct cl_decoded_option
*saw_libc
= NULL
;
90 /* An array used to flag each argument that needs a bit set for
91 LANGSPEC, MATHLIB, or WITHLIBC. */
94 /* By default, we throw on the math library if we have one. */
95 int need_math
= (MATH_LIBRARY
[0] != '\0');
97 /* True if we saw -static. */
100 /* True if we should add -shared-libgcc to the command-line. */
101 int shared_libgcc
= 1;
103 /* The total number of arguments with the new stuff. */
106 /* The argument list. */
107 struct cl_decoded_option
*decoded_options
;
109 /* The number of libraries added in. */
112 /* The total number of arguments with the new stuff. */
113 unsigned int num_args
= 1;
115 argc
= *in_decoded_options_count
;
116 decoded_options
= *in_decoded_options
;
117 added_libraries
= *in_added_libraries
;
119 args
= XCNEWVEC (int, argc
);
121 for (i
= 1; i
< argc
; i
++)
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. */
127 switch (decoded_options
[i
].opt_index
)
130 case OPT_nodefaultlibs
:
135 if (strcmp (arg
, MATH_LIBRARY
) == 0)
140 else if (strcmp (arg
, "c") == 0)
143 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
144 library
= (library
== 0) ? 1 : library
;
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))
165 /* Arguments that go directly to the linker might be .o files,
166 or something, and so might cause libstdc++ to be needed. */
176 case OPT_fsyntax_only
:
177 /* Don't specify libraries if we won't link, since that would
186 case OPT_static_libgcc
:
190 case OPT_static_libstdc__
:
191 library
= library
>= 0 ? 2 : library
;
195 case OPT_SPECIAL_input_file
:
199 /* We don't do this anymore, since we don't get them with minus
201 if (arg
[0] == '\0' || arg
[1] == '\0')
210 /* If the filename ends in .[chi], put options around it.
211 But not if a specified -x option is currently active. */
214 && (arg
[len
- 1] == 'c'
215 || arg
[len
- 1] == 'i'
216 || arg
[len
- 1] == 'h')
217 && arg
[len
- 2] == '.')
223 /* If we don't know that this is a header file, we might
224 need to be linking in the libraries. */
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))
243 /* There's no point adding -shared-libgcc if we don't have a shared
245 #ifndef ENABLE_SHARED_LIBGCC
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
);
256 /* Copy the 0th argument, i.e., the name of the program itself. */
257 new_decoded_options
[j
++] = decoded_options
[i
++];
259 /* NOTE: We start at 1 now, not 0. */
262 new_decoded_options
[j
] = decoded_options
[i
];
264 /* Make sure -lstdc++ is before the math library, since libstdc++
265 itself uses those math routines. */
266 if (!saw_math
&& (args
[i
] & MATHLIB
) && library
> 0)
269 saw_math
= &decoded_options
[i
];
272 if (!saw_libc
&& (args
[i
] & WITHLIBC
) && library
> 0)
275 saw_libc
= &decoded_options
[i
];
278 /* Wrap foo.[chi] files in a language specification to
279 force the gcc compiler driver to run cc1plus on them. */
280 if (args
[i
] & LANGSPEC
)
282 const char *arg
= decoded_options
[i
].arg
;
283 int len
= strlen (arg
);
284 switch (arg
[len
- 1])
287 generate_option (OPT_x
, "c++", 1, CL_DRIVER
,
288 &new_decoded_options
[j
++]);
291 generate_option (OPT_x
, "c++-cpp-output", 1, CL_DRIVER
,
292 &new_decoded_options
[j
++]);
295 generate_option (OPT_x
, "c++-header", 1, CL_DRIVER
,
296 &new_decoded_options
[j
++]);
301 new_decoded_options
[j
++] = decoded_options
[i
];
302 generate_option (OPT_x
, "none", 1, CL_DRIVER
,
303 &new_decoded_options
[j
]);
306 if ((args
[i
] & SKIPOPT
) != 0)
313 /* Add `-lstdc++' if we haven't already done so. */
316 #ifdef HAVE_LD_STATIC_DYNAMIC
317 if (library
> 1 && !static_link
)
319 generate_option (OPT_Wl_
, "-Bstatic", 1, CL_DRIVER
,
320 &new_decoded_options
[j
]);
324 generate_option (OPT_l
,
325 saw_profile_flag
? LIBSTDCXX_PROFILE
: LIBSTDCXX
, 1,
326 CL_DRIVER
, &new_decoded_options
[j
]);
329 /* Add target-dependent static library, if necessary. */
330 if ((static_link
|| library
> 1) && LIBSTDCXX_STATIC
!= NULL
)
332 generate_option (OPT_l
, LIBSTDCXX_STATIC
, 1,
333 CL_DRIVER
, &new_decoded_options
[j
]);
337 #ifdef HAVE_LD_STATIC_DYNAMIC
338 if (library
> 1 && !static_link
)
340 generate_option (OPT_Wl_
, "-Bdynamic", 1, CL_DRIVER
,
341 &new_decoded_options
[j
]);
347 new_decoded_options
[j
++] = *saw_math
;
348 else if (library
> 0 && need_math
)
350 generate_option (OPT_l
,
351 saw_profile_flag
? MATH_LIBRARY_PROFILE
: MATH_LIBRARY
,
352 1, CL_DRIVER
, &new_decoded_options
[j
]);
357 new_decoded_options
[j
++] = *saw_libc
;
358 if (shared_libgcc
&& !static_link
)
359 generate_option (OPT_shared_libgcc
, NULL
, 1, CL_DRIVER
,
360 &new_decoded_options
[j
++]);
362 *in_decoded_options_count
= j
;
363 *in_decoded_options
= new_decoded_options
;
364 *in_added_libraries
= added_libraries
;
367 /* Called before linking. Returns 0 on success and -1 on failure. */
368 int lang_specific_pre_link (void) /* Not used for C++. */
373 /* Number of extra output files that lang_specific_pre_link may generate. */
374 int lang_specific_extra_outfiles
= 0; /* Not used for C++. */