]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/g++spec.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / cp / g++spec.c
CommitLineData
3b426391 1/* Specific flags and argument handling of the C++ front end.
99dee823 2 Copyright (C) 1996-2021 Free Software Foundation, Inc.
5b76d03b 3
f5adbb8d 4This file is part of GCC.
5b76d03b 5
f5adbb8d 6GCC is free software; you can redistribute it and/or modify
5b76d03b 7it under the terms of the GNU General Public License as published by
e77f031d 8the Free Software Foundation; either version 3, or (at your option)
5b76d03b
BK
9any later version.
10
f5adbb8d 11GCC is distributed in the hope that it will be useful,
5b76d03b
BK
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
e77f031d
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
5b76d03b 19
da20811c 20#include "config.h"
8d052bc7 21#include "system.h"
4977bab6
ZW
22#include "coretypes.h"
23#include "tm.h"
d9d16a19 24#include "opts.h"
2ba25f50 25
f442f723
MS
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'. */
55ebb0d6 31#define WITHLIBC (1<<3)
96be7a11 32/* Skip this option. */
55ebb0d6 33#define SKIPOPT (1<<4)
f442f723
MS
34
35#ifndef MATH_LIBRARY
d9d16a19 36#define MATH_LIBRARY "m"
f442f723 37#endif
271f61fb 38#ifndef MATH_LIBRARY_PROFILE
61fec9ff 39#define MATH_LIBRARY_PROFILE MATH_LIBRARY
271f61fb 40#endif
f442f723 41
18cd4ded 42#ifndef LIBSTDCXX
d9d16a19 43#define LIBSTDCXX "stdc++"
18cd4ded 44#endif
271f61fb 45#ifndef LIBSTDCXX_PROFILE
4afd7f6c 46#define LIBSTDCXX_PROFILE LIBSTDCXX
271f61fb 47#endif
ce75e1dd 48#ifndef LIBSTDCXX_STATIC
a322799d 49#define LIBSTDCXX_STATIC NULL
ce75e1dd 50#endif
18cd4ded 51
662b9c55
IS
52#ifndef LIBCXX
53#define LIBCXX "c++"
54#endif
55#ifndef LIBCXX_PROFILE
56#define LIBCXX_PROFILE LIBCXX
57#endif
58#ifndef LIBCXX_STATIC
59#define LIBCXX_STATIC NULL
60#endif
61
62#ifndef LIBCXXABI
63#define LIBCXXABI "c++abi"
64#endif
65#ifndef LIBCXXABI_PROFILE
66#define LIBCXXABI_PROFILE LIBCXXABI
67#endif
68#ifndef LIBCXXABI_STATIC
69#define LIBCXXABI_STATIC NULL
70#endif
71
72/* The values used here must match those of the stdlib_kind enumeration
73 in c.opt. */
74enum stdcxxlib_kind
75{
76 USE_LIBSTDCXX = 1,
77 USE_LIBCXX = 2
78};
79
f442f723 80void
d9d16a19
JM
81lang_specific_driver (struct cl_decoded_option **in_decoded_options,
82 unsigned int *in_decoded_options_count,
5671bf27 83 int *in_added_libraries)
f442f723 84{
d9d16a19 85 unsigned int i, j;
f442f723 86
838dfd8a 87 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
271f61fb
DB
88 int saw_profile_flag = 0;
89
662b9c55
IS
90 /* What action to take for the c++ runtime library:
91 -1 means we should not link it in.
92 0 means we should link it if it is needed.
93 1 means it is needed and should be linked in.
94 2 means it is needed but should be linked statically. */
17211ab5 95 int library = 0;
f442f723 96
662b9c55
IS
97 /* Which c++ runtime library to link. */
98 stdcxxlib_kind which_library = USE_LIBSTDCXX;
99
f442f723
MS
100 /* The number of arguments being added to what's in argv, other than
101 libraries. We use this to track the number of times we've inserted
102 -xc++/-xnone. */
17211ab5 103 int added = 0;
f442f723 104
f442f723 105 /* The new argument list will be contained in this. */
d9d16a19 106 struct cl_decoded_option *new_decoded_options;
f442f723 107
838dfd8a 108 /* Nonzero if we saw a `-xfoo' language specification on the
f442f723
MS
109 command line. Used to avoid adding our own -xc++ if the user
110 already gave a language for the file. */
111 int saw_speclang = 0;
112
113 /* "-lm" or "-lmath" if it appears on the command line. */
d9d16a19 114 const struct cl_decoded_option *saw_math = NULL;
f442f723 115
ff082cab
RO
116 /* "-lrt" or eqivalent if it appears on the command line. */
117 const struct cl_decoded_option *saw_time = NULL;
118
f442f723 119 /* "-lc" if it appears on the command line. */
d9d16a19 120 const struct cl_decoded_option *saw_libc = NULL;
f442f723
MS
121
122 /* An array used to flag each argument that needs a bit set for
55ebb0d6 123 LANGSPEC, MATHLIB, or WITHLIBC. */
f442f723
MS
124 int *args;
125
b54ccf71
JM
126 /* By default, we throw on the math library if we have one. */
127 int need_math = (MATH_LIBRARY[0] != '\0');
f442f723 128
96be7a11
ILT
129 /* True if we saw -static. */
130 int static_link = 0;
131
049f6ec9
MM
132 /* True if we should add -shared-libgcc to the command-line. */
133 int shared_libgcc = 1;
134
f442f723 135 /* The total number of arguments with the new stuff. */
d9d16a19 136 unsigned int argc;
f442f723
MS
137
138 /* The argument list. */
d9d16a19 139 struct cl_decoded_option *decoded_options;
f442f723 140
84914634
JW
141 /* The number of libraries added in. */
142 int added_libraries;
143
f442f723 144 /* The total number of arguments with the new stuff. */
d9d16a19 145 unsigned int num_args = 1;
f442f723 146
d9d16a19
JM
147 argc = *in_decoded_options_count;
148 decoded_options = *in_decoded_options;
84914634 149 added_libraries = *in_added_libraries;
f442f723 150
67f5655f 151 args = XCNEWVEC (int, argc);
f442f723 152
1bf2b2d2 153 for (i = 1; i < argc; i++)
f442f723 154 {
d9d16a19
JM
155 const char *arg = decoded_options[i].arg;
156 if (decoded_options[i].errors & CL_ERR_MISSING_ARG)
157 continue; /* Avoid examining arguments of options missing them. */
f442f723 158
d9d16a19 159 switch (decoded_options[i].opt_index)
f442f723 160 {
d9d16a19
JM
161 case OPT_nostdlib:
162 case OPT_nodefaultlibs:
163 library = -1;
164 break;
165
166 case OPT_l:
167 if (strcmp (arg, MATH_LIBRARY) == 0)
f442f723
MS
168 {
169 args[i] |= MATHLIB;
170 need_math = 0;
171 }
d9d16a19 172 else if (strcmp (arg, "c") == 0)
f442f723 173 args[i] |= WITHLIBC;
d9d16a19
JM
174 else
175 /* Unrecognized libraries (e.g. -lfoo) may require libstdc++. */
176 library = (library == 0) ? 1 : library;
177 break;
178
179 case OPT_pg:
180 case OPT_p:
181 saw_profile_flag++;
182 break;
183
184 case OPT_x:
185 if (library == 0
186 && (strcmp (arg, "c++") == 0
187 || strcmp (arg, "c++-cpp-output") == 0
188 || strcmp (arg, "objective-c++") == 0
189 || strcmp (arg, "objective-c++-cpp-output") == 0))
190 library = 1;
a073516d 191
d9d16a19
JM
192 saw_speclang = 1;
193 break;
194
195 case OPT_Xlinker:
196 case OPT_Wl_:
01e05466
GK
197 /* Arguments that go directly to the linker might be .o files,
198 or something, and so might cause libstdc++ to be needed. */
d9d16a19
JM
199 if (library == 0)
200 library = 1;
201 break;
202
203 case OPT_c:
0b7fb27b 204 case OPT_r:
d9d16a19
JM
205 case OPT_S:
206 case OPT_E:
207 case OPT_M:
208 case OPT_MM:
209 case OPT_fsyntax_only:
210 /* Don't specify libraries if we won't link, since that would
211 cause a warning. */
212 library = -1;
213 break;
214
215 case OPT_static:
216 static_link = 1;
217 break;
218
219 case OPT_static_libgcc:
220 shared_libgcc = 0;
221 break;
222
223 case OPT_static_libstdc__:
224 library = library >= 0 ? 2 : library;
225 args[i] |= SKIPOPT;
226 break;
227
662b9c55
IS
228 case OPT_stdlib_:
229 which_library = (stdcxxlib_kind) decoded_options[i].value;
230 break;
231
d9d16a19
JM
232 case OPT_SPECIAL_input_file:
233 {
234 int len;
235
236 /* We don't do this anymore, since we don't get them with minus
237 signs on them. */
238 if (arg[0] == '\0' || arg[1] == '\0')
f442f723 239 continue;
f442f723 240
d9d16a19
JM
241 if (saw_speclang)
242 {
243 saw_speclang = 0;
244 continue;
245 }
246
247 /* If the filename ends in .[chi], put options around it.
248 But not if a specified -x option is currently active. */
249 len = strlen (arg);
250 if (len > 2
251 && (arg[len - 1] == 'c'
252 || arg[len - 1] == 'i'
253 || arg[len - 1] == 'h')
254 && arg[len - 2] == '.')
255 {
256 args[i] |= LANGSPEC;
257 added += 2;
258 }
259
260 /* If we don't know that this is a header file, we might
261 need to be linking in the libraries. */
262 if (library == 0)
263 {
264 if ((len <= 2 || strcmp (arg + (len - 2), ".H") != 0)
265 && (len <= 2 || strcmp (arg + (len - 2), ".h") != 0)
266 && (len <= 4 || strcmp (arg + (len - 4), ".hpp") != 0)
267 && (len <= 3 || strcmp (arg + (len - 3), ".hp") != 0)
268 && (len <= 4 || strcmp (arg + (len - 4), ".hxx") != 0)
269 && (len <= 4 || strcmp (arg + (len - 4), ".h++") != 0)
270 && (len <= 4 || strcmp (arg + (len - 4), ".HPP") != 0)
271 && (len <= 4 || strcmp (arg + (len - 4), ".tcc") != 0)
272 && (len <= 3 || strcmp (arg + (len - 3), ".hh") != 0))
273 library = 1;
274 }
275 }
276 break;
f442f723
MS
277 }
278 }
279
049f6ec9
MM
280 /* There's no point adding -shared-libgcc if we don't have a shared
281 libgcc. */
282#ifndef ENABLE_SHARED_LIBGCC
283 shared_libgcc = 0;
284#endif
285
d9d16a19
JM
286 /* Add one for shared_libgcc or extra static library. */
287 num_args = argc + added + need_math + (library > 0) * 4 + 1;
662b9c55
IS
288 /* For libc++, on most platforms, the ABI library (usually called libc++abi)
289 is provided as a separate DSO, which we must also append.
290 However, a platform might have the ability to forward the ABI library
291 from libc++, or combine it in some other way; in that case, LIBCXXABI
292 should be set to NULL to signal that it need not be appended. */
293 if (which_library == USE_LIBCXX && LIBCXXABI != NULL)
294 num_args += 4;
d9d16a19 295 new_decoded_options = XNEWVEC (struct cl_decoded_option, num_args);
f442f723 296
834c6dff
MM
297 i = 0;
298 j = 0;
c8094d83 299
834c6dff 300 /* Copy the 0th argument, i.e., the name of the program itself. */
d9d16a19 301 new_decoded_options[j++] = decoded_options[i++];
834c6dff 302
f442f723 303 /* NOTE: We start at 1 now, not 0. */
834c6dff 304 while (i < argc)
f442f723 305 {
d9d16a19 306 new_decoded_options[j] = decoded_options[i];
f442f723
MS
307
308 /* Make sure -lstdc++ is before the math library, since libstdc++
309 itself uses those math routines. */
17211ab5 310 if (!saw_math && (args[i] & MATHLIB) && library > 0)
f442f723
MS
311 {
312 --j;
d9d16a19 313 saw_math = &decoded_options[i];
f442f723
MS
314 }
315
17211ab5 316 if (!saw_libc && (args[i] & WITHLIBC) && library > 0)
f442f723
MS
317 {
318 --j;
d9d16a19 319 saw_libc = &decoded_options[i];
f442f723
MS
320 }
321
17211ab5 322 /* Wrap foo.[chi] files in a language specification to
f442f723
MS
323 force the gcc compiler driver to run cc1plus on them. */
324 if (args[i] & LANGSPEC)
325 {
d9d16a19
JM
326 const char *arg = decoded_options[i].arg;
327 int len = strlen (arg);
328 switch (arg[len - 1])
17211ab5
GK
329 {
330 case 'c':
d9d16a19
JM
331 generate_option (OPT_x, "c++", 1, CL_DRIVER,
332 &new_decoded_options[j++]);
17211ab5
GK
333 break;
334 case 'i':
d9d16a19
JM
335 generate_option (OPT_x, "c++-cpp-output", 1, CL_DRIVER,
336 &new_decoded_options[j++]);
17211ab5
GK
337 break;
338 case 'h':
d9d16a19
JM
339 generate_option (OPT_x, "c++-header", 1, CL_DRIVER,
340 &new_decoded_options[j++]);
17211ab5
GK
341 break;
342 default:
8dc2b103 343 gcc_unreachable ();
17211ab5 344 }
d9d16a19
JM
345 new_decoded_options[j++] = decoded_options[i];
346 generate_option (OPT_x, "none", 1, CL_DRIVER,
347 &new_decoded_options[j]);
f442f723 348 }
f442f723 349
96be7a11
ILT
350 if ((args[i] & SKIPOPT) != 0)
351 --j;
352
834c6dff
MM
353 i++;
354 j++;
355 }
eeb109f2 356
f442f723 357 /* Add `-lstdc++' if we haven't already done so. */
17211ab5 358 if (library > 0)
84914634 359 {
96be7a11
ILT
360#ifdef HAVE_LD_STATIC_DYNAMIC
361 if (library > 1 && !static_link)
362 {
c6092243 363 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
d9d16a19 364 &new_decoded_options[j]);
96be7a11
ILT
365 j++;
366 }
367#endif
662b9c55
IS
368 if (which_library == USE_LIBCXX)
369 {
370 generate_option (OPT_l,
371 saw_profile_flag ? LIBCXX_PROFILE : LIBCXX, 1,
372 CL_DRIVER, &new_decoded_options[j]);
373 if (LIBCXXABI != NULL)
374 {
375 j++;
376 added_libraries++;
377 generate_option (OPT_l,
378 saw_profile_flag ? LIBCXXABI_PROFILE
379 : LIBCXXABI, 1,
380 CL_DRIVER, &new_decoded_options[j]);
381 }
382 }
383 else
384 generate_option (OPT_l,
385 saw_profile_flag ? LIBSTDCXX_PROFILE : LIBSTDCXX, 1,
386 CL_DRIVER, &new_decoded_options[j]);
d9d16a19 387 added_libraries++;
a322799d 388 j++;
96be7a11
ILT
389 /* Add target-dependent static library, if necessary. */
390 if ((static_link || library > 1) && LIBSTDCXX_STATIC != NULL)
391 {
d9d16a19
JM
392 generate_option (OPT_l, LIBSTDCXX_STATIC, 1,
393 CL_DRIVER, &new_decoded_options[j]);
394 added_libraries++;
96be7a11
ILT
395 j++;
396 }
397#ifdef HAVE_LD_STATIC_DYNAMIC
398 if (library > 1 && !static_link)
399 {
c6092243 400 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
d9d16a19 401 &new_decoded_options[j]);
96be7a11
ILT
402 j++;
403 }
404#endif
84914634 405 }
f442f723 406 if (saw_math)
d9d16a19 407 new_decoded_options[j++] = *saw_math;
17211ab5 408 else if (library > 0 && need_math)
84914634 409 {
d9d16a19
JM
410 generate_option (OPT_l,
411 saw_profile_flag ? MATH_LIBRARY_PROFILE : MATH_LIBRARY,
412 1, CL_DRIVER, &new_decoded_options[j]);
413 added_libraries++;
4afd7f6c 414 j++;
84914634 415 }
ff082cab
RO
416 if (saw_time)
417 new_decoded_options[j++] = *saw_time;
f442f723 418 if (saw_libc)
d9d16a19 419 new_decoded_options[j++] = *saw_libc;
96be7a11 420 if (shared_libgcc && !static_link)
d9d16a19
JM
421 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
422 &new_decoded_options[j++]);
f442f723 423
d9d16a19
JM
424 *in_decoded_options_count = j;
425 *in_decoded_options = new_decoded_options;
84914634 426 *in_added_libraries = added_libraries;
f442f723 427}
240661ba 428
c6002625 429/* Called before linking. Returns 0 on success and -1 on failure. */
bf9d3c27 430int lang_specific_pre_link (void) /* Not used for C++. */
240661ba
PB
431{
432 return 0;
433}
434
c6002625
KH
435/* Number of extra output files that lang_specific_pre_link may generate. */
436int lang_specific_extra_outfiles = 0; /* Not used for C++. */