Line data Source code
1 : /* Specific flags and argument handling of the C++ front end.
2 : Copyright (C) 1996-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
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)
9 : any later version.
10 :
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.
15 :
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/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "tm.h"
24 : #include "opts.h"
25 :
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)
36 :
37 : #ifndef MATH_LIBRARY
38 : #define MATH_LIBRARY "m"
39 : #endif
40 : #ifndef MATH_LIBRARY_PROFILE
41 : #define MATH_LIBRARY_PROFILE MATH_LIBRARY
42 : #endif
43 :
44 : #ifndef LIBSTDCXX
45 : #define LIBSTDCXX "stdc++"
46 : #endif
47 : #ifndef LIBSTDCXX_PROFILE
48 : #define LIBSTDCXX_PROFILE LIBSTDCXX
49 : #endif
50 : #ifndef LIBSTDCXX_STATIC
51 : #define LIBSTDCXX_STATIC NULL
52 : #endif
53 :
54 : #ifndef LIBCXX
55 : #define LIBCXX "c++"
56 : #endif
57 : #ifndef LIBCXX_PROFILE
58 : #define LIBCXX_PROFILE LIBCXX
59 : #endif
60 : #ifndef LIBCXX_STATIC
61 : #define LIBCXX_STATIC NULL
62 : #endif
63 :
64 : #ifndef LIBCXXABI
65 : #define LIBCXXABI "c++abi"
66 : #endif
67 : #ifndef LIBCXXABI_PROFILE
68 : #define LIBCXXABI_PROFILE LIBCXXABI
69 : #endif
70 : #ifndef LIBCXXABI_STATIC
71 : #define LIBCXXABI_STATIC NULL
72 : #endif
73 :
74 : /* The values used here must match those of the stdlib_kind enumeration
75 : in c.opt. */
76 : enum stdcxxlib_kind
77 : {
78 : USE_LIBSTDCXX = 1,
79 : USE_LIBCXX = 2
80 : };
81 :
82 : void
83 106175 : lang_specific_driver (struct cl_decoded_option **in_decoded_options,
84 : unsigned int *in_decoded_options_count,
85 : int *in_added_libraries)
86 : {
87 106175 : unsigned int i, j;
88 :
89 : /* If nonzero, the user gave us the `-p' or `-pg' flag. */
90 106175 : int saw_profile_flag = 0;
91 :
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. */
97 106175 : int library = 0;
98 :
99 : /* Which c++ runtime library to link. */
100 106175 : stdcxxlib_kind which_library = USE_LIBSTDCXX;
101 :
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
104 : -xc++/-xnone. */
105 106175 : int added = 0;
106 :
107 : /* The new argument list will be contained in this. */
108 106175 : struct cl_decoded_option *new_decoded_options;
109 :
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 106175 : int saw_speclang = 0;
114 :
115 : /* "-lm" or "-lmath" if it appears on the command line. */
116 106175 : const struct cl_decoded_option *saw_math = NULL;
117 :
118 : /* "-lrt" or equivalent if it appears on the command line. */
119 106175 : const struct cl_decoded_option *saw_time = NULL;
120 :
121 : /* "-lc" if it appears on the command line. */
122 106175 : const struct cl_decoded_option *saw_libc = NULL;
123 :
124 : /* An array used to flag each argument that needs a bit set for
125 : LANGSPEC, MATHLIB, or WITHLIBC. */
126 106175 : int *args;
127 :
128 : /* By default, we throw on the math library if we have one. */
129 106175 : int need_math = (MATH_LIBRARY[0] != '\0');
130 :
131 : /* By default, we don't add -lstdc++exp. */
132 106175 : bool need_experimental = false;
133 :
134 : /* Whether to also compile module std. */
135 106175 : bool std_module = false;
136 :
137 : /* True if we saw -static. */
138 106175 : int static_link = 0;
139 :
140 : /* True if we should add -shared-libgcc to the command-line. */
141 106175 : int shared_libgcc = 1;
142 :
143 : /* The total number of arguments with the new stuff. */
144 106175 : unsigned int argc;
145 :
146 : /* The argument list. */
147 106175 : struct cl_decoded_option *decoded_options;
148 :
149 : /* The number of libraries added in. */
150 106175 : int added_libraries;
151 :
152 : /* The total number of arguments with the new stuff. */
153 106175 : unsigned int num_args = 1;
154 :
155 106175 : argc = *in_decoded_options_count;
156 106175 : decoded_options = *in_decoded_options;
157 106175 : added_libraries = *in_added_libraries;
158 :
159 106175 : args = XCNEWVEC (int, argc);
160 :
161 2888288 : for (i = 1; i < argc; i++)
162 : {
163 2675938 : const char *arg = decoded_options[i].arg;
164 2675938 : if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
165 0 : continue; /* Avoid examining arguments of options missing them. */
166 :
167 2675938 : switch (decoded_options[i].opt_index)
168 : {
169 : /* Contracts support requires -lstdc++exp, and c_common_post_options
170 : enables it by default in C++26 and above. */
171 : case OPT_fcontracts:
172 : case OPT_std_c__26:
173 : case OPT_std_c__29:
174 : case OPT_std_gnu__26:
175 : case OPT_std_gnu__29:
176 2675938 : need_experimental = true;
177 : break;
178 :
179 3 : case OPT_nostdlib__:
180 3 : args[i] |= SKIPOPT;
181 : /* FALLTHRU */
182 : case OPT_nostdlib:
183 : case OPT_nodefaultlibs:
184 : library = -1;
185 : break;
186 :
187 21733 : case OPT_l:
188 21733 : if (strcmp (arg, MATH_LIBRARY) == 0)
189 : {
190 21521 : args[i] |= MATHLIB;
191 21521 : need_math = 0;
192 : }
193 212 : else if (strcmp (arg, "c") == 0)
194 0 : args[i] |= WITHLIBC;
195 : else
196 : /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
197 212 : library = (library == 0) ? 1 : library;
198 : break;
199 :
200 87 : case OPT_pg:
201 87 : case OPT_p:
202 87 : saw_profile_flag++;
203 87 : break;
204 :
205 3004 : case OPT_x:
206 3004 : if (library == 0
207 2940 : && (strcmp (arg, "c++") == 0
208 2940 : || strcmp (arg, "c++-cpp-output") == 0
209 2940 : || strcmp (arg, "objective-c++") == 0
210 2940 : || strcmp (arg, "objective-c++-cpp-output") == 0))
211 0 : library = 1;
212 :
213 : saw_speclang = 1;
214 : break;
215 :
216 6643 : case OPT_Xlinker:
217 6643 : case OPT_Wl_:
218 : /* Arguments that go directly to the linker might be .o files,
219 : or something, and so might cause libstdc++ to be needed. */
220 6643 : if (library == 0)
221 2675938 : library = 1;
222 : break;
223 :
224 81171 : case OPT_c:
225 81171 : case OPT_r:
226 81171 : case OPT_S:
227 81171 : case OPT_E:
228 81171 : case OPT_M:
229 81171 : case OPT_MM:
230 81171 : case OPT_fsyntax_only:
231 : /* Don't specify libraries if we won't link, since that would
232 : cause a warning. */
233 81171 : library = -1;
234 81171 : break;
235 :
236 30 : case OPT_static:
237 30 : static_link = 1;
238 30 : break;
239 :
240 1 : case OPT_static_libgcc:
241 1 : shared_libgcc = 0;
242 1 : break;
243 :
244 9 : case OPT_static_libstdc__:
245 9 : library = library >= 0 ? 2 : library;
246 : #ifdef HAVE_LD_STATIC_DYNAMIC
247 : /* Remove -static-libstdc++ from the command only if target supports
248 : LD_STATIC_DYNAMIC. When not supported, it is left in so that a
249 : back-end target can use outfile substitution. */
250 9 : args[i] |= SKIPOPT;
251 : #endif
252 9 : break;
253 :
254 0 : case OPT_stdlib_:
255 0 : which_library = (stdcxxlib_kind) decoded_options[i].value;
256 0 : break;
257 :
258 1 : case OPT__compile_std_module:
259 1 : std_module = true;
260 1 : break;
261 :
262 110670 : case OPT_SPECIAL_input_file:
263 110670 : {
264 110670 : int len;
265 :
266 : /* We don't do this anymore, since we don't get them with minus
267 : signs on them. */
268 110670 : if (arg[0] == '\0' || arg[1] == '\0')
269 0 : continue;
270 :
271 110670 : if (saw_speclang)
272 : {
273 2991 : saw_speclang = 0;
274 2991 : continue;
275 : }
276 :
277 : /* If the filename ends in .[chi], put options around it.
278 : But not if a specified -x option is currently active. */
279 107679 : len = strlen (arg);
280 107679 : if (len > 2
281 107679 : && (arg[len - 1] == 'c'
282 81124 : || arg[len - 1] == 'i'
283 81124 : || arg[len - 1] == 'h')
284 26555 : && arg[len - 2] == '.')
285 : {
286 13803 : args[i] |= LANGSPEC;
287 13803 : added += 2;
288 : }
289 :
290 : /* If we don't know that this is a header file, we might
291 : need to be linking in the libraries. */
292 107679 : if (library == 0)
293 : {
294 89671 : if ((len <= 2 || strcmp (arg + (len - 2), ".H") != 0)
295 89671 : && (len <= 2 || strcmp (arg + (len - 2), ".h") != 0)
296 88669 : && (len <= 4 || strcmp (arg + (len - 4), ".hpp") != 0)
297 88669 : && (len <= 3 || strcmp (arg + (len - 3), ".hp") != 0)
298 88669 : && (len <= 4 || strcmp (arg + (len - 4), ".hxx") != 0)
299 88669 : && (len <= 4 || strcmp (arg + (len - 4), ".h++") != 0)
300 88669 : && (len <= 4 || strcmp (arg + (len - 4), ".HPP") != 0)
301 88669 : && (len <= 4 || strcmp (arg + (len - 4), ".tcc") != 0)
302 88669 : && (len <= 3 || strcmp (arg + (len - 3), ".hh") != 0))
303 2675938 : library = 1;
304 : }
305 : }
306 : break;
307 : }
308 : }
309 :
310 : /* There's no point adding -shared-libgcc if we don't have a shared
311 : libgcc. */
312 : #ifndef ENABLE_SHARED_LIBGCC
313 : shared_libgcc = 0;
314 : #endif
315 :
316 : /* Add one for shared_libgcc or extra static library. */
317 212350 : num_args = (argc + added + need_math + need_experimental
318 106175 : + (std_module * 5)
319 106175 : + (library > 0) * 4 + 1);
320 : /* For libc++, on most platforms, the ABI library (usually called libc++abi)
321 : is provided as a separate DSO, which we must also append.
322 : However, a platform might have the ability to forward the ABI library
323 : from libc++, or combine it in some other way; in that case, LIBCXXABI
324 : should be set to NULL to signal that it need not be appended. */
325 106175 : if (which_library == USE_LIBCXX && LIBCXXABI != NULL)
326 0 : num_args += 4;
327 106175 : new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
328 :
329 106175 : i = 0;
330 106175 : j = 0;
331 :
332 : /* Copy the 0th argument, i.e., the name of the program itself. */
333 106175 : new_decoded_options[j++] = decoded_options[i++];
334 :
335 : /* NOTE: We start at 1 now, not 0. */
336 2782113 : while (i < argc)
337 : {
338 2675938 : new_decoded_options[j] = decoded_options[i];
339 :
340 : /* Make sure -lstdc++ is before the math library, since libstdc++
341 : itself uses those math routines. */
342 2675938 : if (!saw_math && (args[i] & MATHLIB) && library > 0)
343 : {
344 21513 : --j;
345 21513 : saw_math = &decoded_options[i];
346 : }
347 :
348 2675938 : if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
349 : {
350 0 : --j;
351 0 : saw_libc = &decoded_options[i];
352 : }
353 :
354 : /* Insert --compile-std-module options before any -x or source files. */
355 2675938 : size_t opt = decoded_options[i].opt_index;
356 2675938 : if (std_module
357 4 : && (opt == OPT__compile_std_module
358 4 : || opt == OPT_SPECIAL_input_file
359 3 : || opt == OPT_x))
360 : {
361 1 : generate_option (OPT_x, "c++-system-header", 1, CL_DRIVER,
362 1 : &new_decoded_options[j++]);
363 1 : generate_option_input_file ("bits/stdc++.h",
364 1 : &new_decoded_options[j]);
365 : /* Tell process_command that this file was added by the driver. */
366 1 : new_decoded_options[j++].mask = CL_DRIVER;
367 1 : generate_option (OPT_x, "c++-system-module", 1, CL_DRIVER,
368 1 : &new_decoded_options[j++]);
369 1 : generate_option_input_file ("bits/std.cc",
370 1 : &new_decoded_options[j]);
371 1 : new_decoded_options[j++].mask = CL_DRIVER;
372 1 : generate_option_input_file ("bits/std.compat.cc",
373 1 : &new_decoded_options[j]);
374 1 : new_decoded_options[j++].mask = CL_DRIVER;
375 1 : generate_option (OPT_x, "none", 1, CL_DRIVER,
376 1 : &new_decoded_options[j++]);
377 1 : new_decoded_options[j] = decoded_options[i];
378 1 : std_module = false;
379 : }
380 2675938 : if (opt == OPT__compile_std_module)
381 1 : --j;
382 :
383 : /* Wrap foo.[chi] files in a language specification to
384 : force the gcc compiler driver to run cc1plus on them. */
385 2675938 : if (args[i] & LANGSPEC)
386 : {
387 13803 : const char *arg = decoded_options[i].arg;
388 13803 : int len = strlen (arg);
389 13803 : switch (arg[len - 1])
390 : {
391 13803 : case 'c':
392 13803 : generate_option (OPT_x, "c++", 1, CL_DRIVER,
393 13803 : &new_decoded_options[j++]);
394 13803 : break;
395 0 : case 'i':
396 0 : generate_option (OPT_x, "c++-cpp-output", 1, CL_DRIVER,
397 0 : &new_decoded_options[j++]);
398 0 : break;
399 0 : case 'h':
400 0 : generate_option (OPT_x, "c++-header", 1, CL_DRIVER,
401 0 : &new_decoded_options[j++]);
402 0 : break;
403 0 : default:
404 0 : gcc_unreachable ();
405 : }
406 13803 : new_decoded_options[j++] = decoded_options[i];
407 13803 : generate_option (OPT_x, "none", 1, CL_DRIVER,
408 13803 : &new_decoded_options[j]);
409 : }
410 :
411 2675938 : if ((args[i] & SKIPOPT) != 0)
412 12 : --j;
413 :
414 2675938 : i++;
415 2675938 : j++;
416 : }
417 :
418 : /* Add `-lstdc++' if we haven't already done so. */
419 106175 : if (library > 0)
420 : {
421 21952 : if (need_experimental && which_library == USE_LIBSTDCXX)
422 : {
423 3574 : generate_option (OPT_l, "stdc++exp", 1, CL_DRIVER,
424 3574 : &new_decoded_options[j++]);
425 3574 : ++added_libraries;
426 : }
427 : #ifdef HAVE_LD_STATIC_DYNAMIC
428 21952 : if (library > 1 && !static_link)
429 : {
430 9 : generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
431 9 : &new_decoded_options[j]);
432 9 : j++;
433 : }
434 : #endif
435 21952 : if (which_library == USE_LIBCXX)
436 : {
437 0 : generate_option (OPT_l,
438 : saw_profile_flag ? LIBCXX_PROFILE : LIBCXX, 1,
439 0 : CL_DRIVER, &new_decoded_options[j]);
440 0 : if (LIBCXXABI != NULL)
441 : {
442 0 : j++;
443 0 : added_libraries++;
444 0 : generate_option (OPT_l,
445 : saw_profile_flag ? LIBCXXABI_PROFILE
446 : : LIBCXXABI, 1,
447 0 : CL_DRIVER, &new_decoded_options[j]);
448 : }
449 : }
450 : else
451 21952 : generate_option (OPT_l,
452 : saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX, 1,
453 21952 : CL_DRIVER, &new_decoded_options[j]);
454 21952 : added_libraries++;
455 21952 : j++;
456 : /* Add target-dependent static library, if necessary. */
457 21952 : if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
458 : {
459 : generate_option (OPT_l, LIBSTDCXX_STATIC, 1,
460 : CL_DRIVER, &new_decoded_options[j]);
461 : added_libraries++;
462 : j++;
463 : }
464 : #ifdef HAVE_LD_STATIC_DYNAMIC
465 21952 : if (library > 1 && !static_link)
466 : {
467 9 : generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
468 9 : &new_decoded_options[j]);
469 9 : j++;
470 : }
471 : #endif
472 : }
473 106175 : if (saw_math)
474 21513 : new_decoded_options[j++] = *saw_math;
475 84662 : else if (library > 0 && need_math)
476 : {
477 439 : generate_option (OPT_l,
478 : saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
479 439 : 1, CL_DRIVER, &new_decoded_options[j]);
480 439 : added_libraries++;
481 439 : j++;
482 : }
483 106175 : if (saw_time)
484 : new_decoded_options[j++] = *saw_time;
485 106175 : if (saw_libc)
486 0 : new_decoded_options[j++] = *saw_libc;
487 106175 : if (shared_libgcc && !static_link)
488 106144 : generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
489 106144 : &new_decoded_options[j++]);
490 :
491 106175 : *in_decoded_options_count = j;
492 106175 : *in_decoded_options = new_decoded_options;
493 106175 : *in_added_libraries = added_libraries;
494 106175 : }
495 :
496 : /* Called before linking. Returns 0 on success and -1 on failure. */
497 82208 : int lang_specific_pre_link (void) /* Not used for C++. */
498 : {
499 82208 : return 0;
500 : }
501 :
502 : /* Number of extra output files that lang_specific_pre_link may generate. */
503 : int lang_specific_extra_outfiles = 0; /* Not used for C++. */
|