1 /* Specific flags and argument handling of the C++ front end.
2 Copyright (C) 1996-2022 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"
26 /* This bit is set if we saw a `-xfoo' language specification. */
27 #define LANGSPEC (1<<1)
28 /* This bit is set if they did `-lm' or `-lmath'. */
29 #define MATHLIB (1<<2)
30 /* This bit is set if they did `-lc'. */
31 #define WITHLIBC (1<<3)
32 /* Skip this option. */
33 #define SKIPOPT (1<<4)
34 /* Add -lstdc++exp for experimental features that need library support. */
35 #define EXPERIMENTAL (1<<5)
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
57 #ifndef LIBCXX_PROFILE
58 #define LIBCXX_PROFILE LIBCXX
61 #define LIBCXX_STATIC NULL
65 #define LIBCXXABI "c++abi"
67 #ifndef LIBCXXABI_PROFILE
68 #define LIBCXXABI_PROFILE LIBCXXABI
70 #ifndef LIBCXXABI_STATIC
71 #define LIBCXXABI_STATIC NULL
74 /* The values used here must match those of the stdlib_kind enumeration
83 lang_specific_driver (struct cl_decoded_option
**in_decoded_options
,
84 unsigned int *in_decoded_options_count
,
85 int *in_added_libraries
)
89 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
90 int saw_profile_flag
= 0;
92 /* What action to take for the c++ runtime library:
93 -1 means we should not link it in.
94 0 means we should link it if it is needed.
95 1 means it is needed and should be linked in.
96 2 means it is needed but should be linked statically. */
99 /* Which c++ runtime library to link. */
100 stdcxxlib_kind which_library
= USE_LIBSTDCXX
;
102 /* The number of arguments being added to what's in argv, other than
103 libraries. We use this to track the number of times we've inserted
107 /* The new argument list will be contained in this. */
108 struct cl_decoded_option
*new_decoded_options
;
110 /* Nonzero if we saw a `-xfoo' language specification on the
111 command line. Used to avoid adding our own -xc++ if the user
112 already gave a language for the file. */
113 int saw_speclang
= 0;
115 /* "-lm" or "-lmath" if it appears on the command line. */
116 const struct cl_decoded_option
*saw_math
= NULL
;
118 /* "-lrt" or eqivalent if it appears on the command line. */
119 const struct cl_decoded_option
*saw_time
= NULL
;
121 /* "-lc" if it appears on the command line. */
122 const struct cl_decoded_option
*saw_libc
= NULL
;
124 /* An array used to flag each argument that needs a bit set for
125 LANGSPEC, MATHLIB, or WITHLIBC. */
128 /* By default, we throw on the math library if we have one. */
129 int need_math
= (MATH_LIBRARY
[0] != '\0');
131 /* By default, we don't add -lstdc++exp. */
132 bool need_experimental
= false;
134 /* True if we saw -static. */
137 /* True if we should add -shared-libgcc to the command-line. */
138 int shared_libgcc
= 1;
140 /* The total number of arguments with the new stuff. */
143 /* The argument list. */
144 struct cl_decoded_option
*decoded_options
;
146 /* The number of libraries added in. */
149 /* The total number of arguments with the new stuff. */
150 unsigned int num_args
= 1;
152 argc
= *in_decoded_options_count
;
153 decoded_options
= *in_decoded_options
;
154 added_libraries
= *in_added_libraries
;
156 args
= XCNEWVEC (int, argc
);
158 for (i
= 1; i
< argc
; i
++)
160 const char *arg
= decoded_options
[i
].arg
;
161 if (decoded_options
[i
].errors
& CL_ERR_MISSING_ARG
)
162 continue; /* Avoid examining arguments of options missing them. */
164 switch (decoded_options
[i
].opt_index
)
167 need_experimental
= true;
174 case OPT_nodefaultlibs
:
179 if (strcmp (arg
, MATH_LIBRARY
) == 0)
184 else if (strcmp (arg
, "c") == 0)
187 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
188 library
= (library
== 0) ? 1 : library
;
198 && (strcmp (arg
, "c++") == 0
199 || strcmp (arg
, "c++-cpp-output") == 0
200 || strcmp (arg
, "objective-c++") == 0
201 || strcmp (arg
, "objective-c++-cpp-output") == 0))
209 /* Arguments that go directly to the linker might be .o files,
210 or something, and so might cause libstdc++ to be needed. */
221 case OPT_fsyntax_only
:
222 /* Don't specify libraries if we won't link, since that would
231 case OPT_static_libgcc
:
235 case OPT_static_libstdc__
:
236 library
= library
>= 0 ? 2 : library
;
241 which_library
= (stdcxxlib_kind
) decoded_options
[i
].value
;
244 case OPT_SPECIAL_input_file
:
248 /* We don't do this anymore, since we don't get them with minus
250 if (arg
[0] == '\0' || arg
[1] == '\0')
259 /* If the filename ends in .[chi], put options around it.
260 But not if a specified -x option is currently active. */
263 && (arg
[len
- 1] == 'c'
264 || arg
[len
- 1] == 'i'
265 || arg
[len
- 1] == 'h')
266 && arg
[len
- 2] == '.')
272 /* If we don't know that this is a header file, we might
273 need to be linking in the libraries. */
276 if ((len
<= 2 || strcmp (arg
+ (len
- 2), ".H") != 0)
277 && (len
<= 2 || strcmp (arg
+ (len
- 2), ".h") != 0)
278 && (len
<= 4 || strcmp (arg
+ (len
- 4), ".hpp") != 0)
279 && (len
<= 3 || strcmp (arg
+ (len
- 3), ".hp") != 0)
280 && (len
<= 4 || strcmp (arg
+ (len
- 4), ".hxx") != 0)
281 && (len
<= 4 || strcmp (arg
+ (len
- 4), ".h++") != 0)
282 && (len
<= 4 || strcmp (arg
+ (len
- 4), ".HPP") != 0)
283 && (len
<= 4 || strcmp (arg
+ (len
- 4), ".tcc") != 0)
284 && (len
<= 3 || strcmp (arg
+ (len
- 3), ".hh") != 0))
292 /* There's no point adding -shared-libgcc if we don't have a shared
294 #ifndef ENABLE_SHARED_LIBGCC
298 /* Add one for shared_libgcc or extra static library. */
299 num_args
= (argc
+ added
+ need_math
+ need_experimental
300 + (library
> 0) * 4 + 1);
301 /* For libc++, on most platforms, the ABI library (usually called libc++abi)
302 is provided as a separate DSO, which we must also append.
303 However, a platform might have the ability to forward the ABI library
304 from libc++, or combine it in some other way; in that case, LIBCXXABI
305 should be set to NULL to signal that it need not be appended. */
306 if (which_library
== USE_LIBCXX
&& LIBCXXABI
!= NULL
)
308 new_decoded_options
= XNEWVEC (struct cl_decoded_option
, num_args
);
313 /* Copy the 0th argument, i.e., the name of the program itself. */
314 new_decoded_options
[j
++] = decoded_options
[i
++];
316 /* NOTE: We start at 1 now, not 0. */
319 new_decoded_options
[j
] = decoded_options
[i
];
321 /* Make sure -lstdc++ is before the math library, since libstdc++
322 itself uses those math routines. */
323 if (!saw_math
&& (args
[i
] & MATHLIB
) && library
> 0)
326 saw_math
= &decoded_options
[i
];
329 if (!saw_libc
&& (args
[i
] & WITHLIBC
) && library
> 0)
332 saw_libc
= &decoded_options
[i
];
335 /* Wrap foo.[chi] files in a language specification to
336 force the gcc compiler driver to run cc1plus on them. */
337 if (args
[i
] & LANGSPEC
)
339 const char *arg
= decoded_options
[i
].arg
;
340 int len
= strlen (arg
);
341 switch (arg
[len
- 1])
344 generate_option (OPT_x
, "c++", 1, CL_DRIVER
,
345 &new_decoded_options
[j
++]);
348 generate_option (OPT_x
, "c++-cpp-output", 1, CL_DRIVER
,
349 &new_decoded_options
[j
++]);
352 generate_option (OPT_x
, "c++-header", 1, CL_DRIVER
,
353 &new_decoded_options
[j
++]);
358 new_decoded_options
[j
++] = decoded_options
[i
];
359 generate_option (OPT_x
, "none", 1, CL_DRIVER
,
360 &new_decoded_options
[j
]);
363 if ((args
[i
] & SKIPOPT
) != 0)
370 /* Add `-lstdc++' if we haven't already done so. */
373 if (need_experimental
&& which_library
== USE_LIBSTDCXX
)
375 generate_option (OPT_l
, "stdc++exp", 1, CL_DRIVER
,
376 &new_decoded_options
[j
++]);
379 #ifdef HAVE_LD_STATIC_DYNAMIC
380 if (library
> 1 && !static_link
)
382 generate_option (OPT_Wl_
, LD_STATIC_OPTION
, 1, CL_DRIVER
,
383 &new_decoded_options
[j
]);
387 if (which_library
== USE_LIBCXX
)
389 generate_option (OPT_l
,
390 saw_profile_flag
? LIBCXX_PROFILE
: LIBCXX
, 1,
391 CL_DRIVER
, &new_decoded_options
[j
]);
392 if (LIBCXXABI
!= NULL
)
396 generate_option (OPT_l
,
397 saw_profile_flag
? LIBCXXABI_PROFILE
399 CL_DRIVER
, &new_decoded_options
[j
]);
403 generate_option (OPT_l
,
404 saw_profile_flag
? LIBSTDCXX_PROFILE
: LIBSTDCXX
, 1,
405 CL_DRIVER
, &new_decoded_options
[j
]);
408 /* Add target-dependent static library, if necessary. */
409 if ((static_link
|| library
> 1) && LIBSTDCXX_STATIC
!= NULL
)
411 generate_option (OPT_l
, LIBSTDCXX_STATIC
, 1,
412 CL_DRIVER
, &new_decoded_options
[j
]);
416 #ifdef HAVE_LD_STATIC_DYNAMIC
417 if (library
> 1 && !static_link
)
419 generate_option (OPT_Wl_
, LD_DYNAMIC_OPTION
, 1, CL_DRIVER
,
420 &new_decoded_options
[j
]);
426 new_decoded_options
[j
++] = *saw_math
;
427 else if (library
> 0 && need_math
)
429 generate_option (OPT_l
,
430 saw_profile_flag
? MATH_LIBRARY_PROFILE
: MATH_LIBRARY
,
431 1, CL_DRIVER
, &new_decoded_options
[j
]);
436 new_decoded_options
[j
++] = *saw_time
;
438 new_decoded_options
[j
++] = *saw_libc
;
439 if (shared_libgcc
&& !static_link
)
440 generate_option (OPT_shared_libgcc
, NULL
, 1, CL_DRIVER
,
441 &new_decoded_options
[j
++]);
443 *in_decoded_options_count
= j
;
444 *in_decoded_options
= new_decoded_options
;
445 *in_added_libraries
= added_libraries
;
448 /* Called before linking. Returns 0 on success and -1 on failure. */
449 int lang_specific_pre_link (void) /* Not used for C++. */
454 /* Number of extra output files that lang_specific_pre_link may generate. */
455 int lang_specific_extra_outfiles
= 0; /* Not used for C++. */