Daily bump.
[official-gcc.git] / gcc / d / d-spec.cc
blobb12d28f10478b6ab32f1d0f42ff502d777eb197f
1 /* d-spec.c -- Specific flags and argument handling of the D front end.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
4 GCC is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
9 GCC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GCC; see the file COPYING3. If not see
16 <http://www.gnu.org/licenses/>. */
18 #include "config.h"
19 #include "system.h"
20 #include "coretypes.h"
21 #include "opt-suggestions.h"
22 #include "gcc.h"
23 #include "tm.h"
24 #include "opts.h"
26 /* This bit is set if the arguments is a D source file. */
27 #define DSOURCE (1<<1)
28 /* This bit is set if they did `-lstdc++'. */
29 #define WITHLIBCXX (1<<2)
30 /* Skip this option. */
31 #define SKIPOPT (1<<3)
33 #ifndef LIBSTDCXX
34 #define LIBSTDCXX "stdc++"
35 #endif
36 #ifndef LIBSTDCXX_PROFILE
37 #define LIBSTDCXX_PROFILE LIBSTDCXX
38 #endif
40 #ifndef LIBPHOBOS
41 #define LIBPHOBOS "gphobos"
42 #endif
43 #ifndef LIBPHOBOS_PROFILE
44 #define LIBPHOBOS_PROFILE LIBPHOBOS
45 #endif
47 /* What do with libgphobos. */
48 enum phobos_action
50 /* libgphobos should not be linked in. */
51 PHOBOS_NOLINK = -1,
52 /* libgphobos should be linked in if it is needed. */
53 PHOBOS_DEFAULT = 0,
54 /* libgphobos is needed and should be linked in. */
55 PHOBOS_LINK,
56 /* libgphobos is needed and should be linked statically. */
57 PHOBOS_STATIC,
58 /* libgphobos is needed and should be linked dynamically. */
59 PHOBOS_DYNAMIC
62 static phobos_action phobos_library = PHOBOS_DEFAULT;
64 /* If true, do load libgphobos.spec even if not needed otherwise. */
65 static bool need_spec = false;
67 void
68 lang_specific_driver (cl_decoded_option **in_decoded_options,
69 unsigned int *in_decoded_options_count,
70 int *in_added_libraries)
72 unsigned int i, j;
74 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
75 int saw_profile_flag = 0;
77 /* If true, the user gave `-g'. Used by -debuglib. */
78 bool saw_debug_flag = false;
80 /* The new argument list will be contained in this. */
81 cl_decoded_option *new_decoded_options;
83 /* "-lstdc++" if it appears on the command line. */
84 const cl_decoded_option *saw_libcxx = 0;
86 /* True if we saw `-static-libstdc++'. */
87 bool saw_static_libcxx = false;
89 /* Whether we need the C++ STD library. */
90 bool need_stdcxx = false;
92 /* True if we saw -static. */
93 bool static_link = false;
95 /* True if we should add -shared-libgcc to the command-line. */
96 bool shared_libgcc = true;
98 /* What default library to use instead of phobos. */
99 const char *defaultlib = NULL;
101 /* What debug library to use instead of phobos. */
102 const char *debuglib = NULL;
104 /* The total number of arguments with the new stuff. */
105 unsigned int num_args = 1;
107 /* "-fonly" if it appears on the command line. */
108 const char *only_source_option = 0;
110 /* Whether the -o option was used. */
111 bool saw_opt_o = false;
113 /* Whether the -c option was used. Also used for -E, -fsyntax-only,
114 in general anything which implies only compilation and not linking. */
115 bool saw_opt_c = false;
117 /* Whether the -S option was used. */
118 bool saw_opt_S = false;
120 /* The first input file with an extension of .d. */
121 const char *first_d_file = NULL;
123 /* The total number of arguments with the new stuff. */
124 unsigned int argc = *in_decoded_options_count;
126 /* The argument list. */
127 cl_decoded_option *decoded_options = *in_decoded_options;
129 /* The number of libraries added in. */
130 int added_libraries = *in_added_libraries;
132 /* An array used to flag each argument that needs a bit set for
133 DSOURCE, MATHLIB, WITHTHREAD, WITHLIBC or WITHLIBCXX. */
134 int *args = XCNEWVEC (int, argc);
136 for (i = 1; i < argc; i++)
138 const char *arg = decoded_options[i].arg;
139 const int value = decoded_options[i].value;
141 switch (decoded_options[i].opt_index)
143 case OPT_dstartfiles:
144 need_spec = true;
145 break;
147 case OPT_nostdlib:
148 case OPT_nodefaultlibs:
149 phobos_library = PHOBOS_NOLINK;
150 break;
152 case OPT_nophoboslib:
153 phobos_library = PHOBOS_NOLINK;
154 args[i] |= SKIPOPT;
155 break;
157 case OPT_fdruntime:
158 if (!value)
159 phobos_library = PHOBOS_NOLINK;
160 else
161 phobos_library = PHOBOS_LINK;
162 break;
164 case OPT_defaultlib_:
165 if (defaultlib != NULL)
166 free (CONST_CAST (char *, defaultlib));
167 if (arg != NULL)
169 args[i] |= SKIPOPT;
170 defaultlib = XNEWVEC (char, strlen (arg));
171 strcpy (CONST_CAST (char *, defaultlib), arg);
173 break;
175 case OPT_debuglib_:
176 if (debuglib != NULL)
177 free (CONST_CAST (char *, debuglib));
178 if (arg != NULL)
180 args[i] |= SKIPOPT;
181 debuglib = XNEWVEC (char, strlen (arg));
182 strcpy (CONST_CAST (char *, debuglib), arg);
184 break;
186 case OPT_l:
187 if ((strcmp (arg, LIBSTDCXX) == 0)
188 || (strcmp (arg, LIBSTDCXX_PROFILE) == 0))
190 args[i] |= WITHLIBCXX;
191 need_stdcxx = false;
193 /* Unrecognized libraries (e.g. -ltango) may require libphobos. */
194 else if (phobos_library == PHOBOS_DEFAULT)
195 phobos_library = PHOBOS_LINK;
196 break;
198 case OPT_pg:
199 case OPT_p:
200 saw_profile_flag++;
201 break;
203 case OPT_g:
204 saw_debug_flag = true;
205 break;
207 case OPT_v:
208 /* If they only gave us `-v', don't try to link in libphobos. */
209 if (argc == 2)
210 phobos_library = PHOBOS_NOLINK;
211 break;
213 case OPT_x:
214 if (phobos_library == PHOBOS_DEFAULT && (strcmp (arg, "d") == 0))
215 phobos_library = PHOBOS_LINK;
216 break;
218 case OPT_Xlinker:
219 case OPT_Wl_:
220 /* Arguments that go directly to the linker might be .o files
221 or something, and so might cause libphobos to be needed. */
222 if (phobos_library == PHOBOS_DEFAULT)
223 phobos_library = PHOBOS_LINK;
224 break;
226 case OPT_c:
227 case OPT_E:
228 case OPT_M:
229 case OPT_MM:
230 case OPT_fsyntax_only:
231 /* Don't specify libaries if we won't link, since that would
232 cause a warning. */
233 saw_opt_c = true;
234 phobos_library = PHOBOS_NOLINK;
235 break;
237 case OPT_S:
238 saw_opt_S = true;
239 phobos_library = PHOBOS_NOLINK;
240 break;
242 case OPT_o:
243 saw_opt_o = true;
244 break;
246 case OPT_static:
247 static_link = true;
248 break;
250 case OPT_static_libgcc:
251 shared_libgcc = false;
252 break;
254 case OPT_static_libstdc__:
255 saw_static_libcxx = true;
256 args[i] |= SKIPOPT;
257 break;
259 case OPT_static_libphobos:
260 if (phobos_library != PHOBOS_NOLINK)
261 phobos_library = PHOBOS_STATIC;
262 args[i] |= SKIPOPT;
263 break;
265 case OPT_shared_libphobos:
266 if (phobos_library != PHOBOS_NOLINK)
267 phobos_library = PHOBOS_DYNAMIC;
268 args[i] |= SKIPOPT;
269 break;
271 case OPT_fonly_:
272 args[i] |= SKIPOPT;
273 only_source_option = decoded_options[i].orig_option_with_args_text;
275 if (arg != NULL)
277 const char *suffix = strrchr (only_source_option, '.');
278 if (suffix == NULL || strcmp (suffix, ".d") != 0)
279 only_source_option = concat (only_source_option, ".d", NULL);
281 break;
283 case OPT_SPECIAL_input_file:
285 if (arg[0] == '\0' || arg[1] == '\0')
286 continue;
288 if (phobos_library == PHOBOS_DEFAULT)
289 phobos_library = PHOBOS_LINK;
291 /* Record that this is a D source file. */
292 const char *suffix = strrchr (arg, '.');
293 if (suffix != NULL && strcmp (suffix, ".d") == 0)
295 if (first_d_file == NULL)
296 first_d_file = arg;
298 args[i] |= DSOURCE;
301 /* If this is a C++ source file, we'll need to link
302 against libstdc++ library. */
303 if (suffix != NULL
304 && (strcmp (suffix, ".cc") == 0
305 || (strcmp (suffix, ".cpp") == 0)
306 || (strcmp (suffix, ".c++") == 0)))
307 need_stdcxx = true;
309 break;
314 /* There's no point adding -shared-libgcc if we don't have a shared
315 libgcc. */
316 #ifndef ENABLE_SHARED_LIBGCC
317 shared_libgcc = false;
318 #endif
320 /* Make sure to have room for the trailing NULL argument.
321 - need_stdcxx might add `-lstdcxx'
322 - libphobos adds `-Bstatic -lphobos -Bdynamic'
323 - only_source adds 1 more arg, also maybe add `-o'. */
324 num_args = argc + need_stdcxx + shared_libgcc
325 + (phobos_library != PHOBOS_NOLINK) * 4 + 2;
326 new_decoded_options = XNEWVEC (cl_decoded_option, num_args);
328 i = 0;
329 j = 0;
331 /* Copy the 0th argument, i.e., the name of the program itself. */
332 new_decoded_options[j++] = decoded_options[i++];
334 /* NOTE: We start at 1 now, not 0. */
335 while (i < argc)
337 if (args[i] & SKIPOPT)
339 ++i;
340 continue;
343 new_decoded_options[j] = decoded_options[i];
345 if (!saw_libcxx && (args[i] & WITHLIBCXX))
347 --j;
348 saw_libcxx = &decoded_options[i];
351 if (args[i] & DSOURCE)
353 if (only_source_option)
354 --j;
357 i++;
358 j++;
361 if (only_source_option)
363 const char *only_source_arg = only_source_option + 7;
364 generate_option (OPT_fonly_, only_source_arg, 1, CL_DRIVER,
365 &new_decoded_options[j]);
366 j++;
368 generate_option_input_file (only_source_arg,
369 &new_decoded_options[j++]);
372 /* If no reason to link against libphobos library, then don't add it. */
373 if (phobos_library == PHOBOS_DEFAULT)
374 phobos_library = PHOBOS_NOLINK;
376 /* If we didn't see a -o option, add one. This is because we need the
377 driver to pass all .d files to the D compiler. Without a -o option
378 the driver will invoke the compiler separately for each input file. */
379 if (first_d_file != NULL && !saw_opt_o)
381 if (saw_opt_c || saw_opt_S)
383 const char *base = lbasename (first_d_file);
384 int baselen = strlen (base) - 2;
385 char *out = XNEWVEC (char, baselen + 3);
387 memcpy (out, base, baselen);
388 /* The driver will convert .o to some other suffix if appropriate. */
389 out[baselen] = '.';
390 if (saw_opt_S)
391 out[baselen + 1] = 's';
392 else
393 out[baselen + 1] = 'o';
394 out[baselen + 2] = '\0';
395 generate_option (OPT_o, out, 1, CL_DRIVER,
396 &new_decoded_options[j]);
398 else
400 /* Wouldn't be necessary if the driver converted .out also. */
401 const char *out = NULL;
403 #ifdef TARGET_EXECUTABLE_SUFFIX
404 if (TARGET_EXECUTABLE_SUFFIX[0] != 0)
405 out = "a" TARGET_EXECUTABLE_SUFFIX;
406 #endif
407 if (out == NULL)
408 out = "a.out";
410 generate_option (OPT_o, out, 1, CL_DRIVER,
411 &new_decoded_options[j]);
413 j++;
416 /* Add `-lgphobos' if we haven't already done so. */
417 if (phobos_library != PHOBOS_NOLINK)
419 /* Default to static linking. */
420 if (phobos_library != PHOBOS_DYNAMIC)
421 phobos_library = PHOBOS_STATIC;
423 #ifdef HAVE_LD_STATIC_DYNAMIC
424 if (phobos_library == PHOBOS_STATIC && !static_link)
426 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
427 &new_decoded_options[j++]);
429 #endif
430 /* Order of precedence in determining what library to link against is:
431 - `-l<lib>' from `-debuglib=<lib>' if `-g' was also seen.
432 - `-l<lib>' from `-defaultlib=<lib>'.
433 - `-lgphobos' unless `-nophoboslib' or `-fno-druntime' was seen. */
434 if (debuglib && saw_debug_flag)
436 generate_option (OPT_l, debuglib, 1, CL_DRIVER,
437 &new_decoded_options[j++]);
438 added_libraries++;
440 else if (defaultlib)
442 generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
443 &new_decoded_options[j++]);
444 added_libraries++;
446 else
448 generate_option (OPT_l,
449 saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
450 CL_DRIVER, &new_decoded_options[j++]);
451 added_libraries++;
454 #ifdef HAVE_LD_STATIC_DYNAMIC
455 if (phobos_library == PHOBOS_STATIC && !static_link)
457 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
458 &new_decoded_options[j++]);
460 #endif
463 if (saw_libcxx || need_stdcxx)
465 #ifdef HAVE_LD_STATIC_DYNAMIC
466 if (saw_static_libcxx && !static_link)
468 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
469 &new_decoded_options[j++]);
471 #else
472 /* Push the -static-libstdc++ option back onto the command so that
473 a target without LD_STATIC_DYNAMIC can use outfile substitution. */
474 if (saw_static_libcxx && !static_link)
475 generate_option (OPT_static_libstdc__, NULL, 1, CL_DRIVER,
476 &new_decoded_options[j++]);
477 #endif
478 if (saw_libcxx)
479 new_decoded_options[j++] = *saw_libcxx;
480 else if (need_stdcxx)
482 generate_option (OPT_l,
483 (saw_profile_flag
484 ? LIBSTDCXX_PROFILE
485 : LIBSTDCXX),
486 1, CL_DRIVER, &new_decoded_options[j++]);
487 added_libraries++;
489 #ifdef HAVE_LD_STATIC_DYNAMIC
490 if (saw_static_libcxx && !static_link)
492 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
493 &new_decoded_options[j++]);
495 #endif
498 if (shared_libgcc && !static_link)
500 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
501 &new_decoded_options[j++]);
504 *in_decoded_options_count = j;
505 *in_decoded_options = new_decoded_options;
506 *in_added_libraries = added_libraries;
509 /* Called before linking. Returns 0 on success and -1 on failure. */
512 lang_specific_pre_link (void)
514 if ((phobos_library != PHOBOS_NOLINK) || need_spec)
515 do_spec ("%:include(libgphobos.spec)");
517 return 0;
520 /* Number of extra output files that lang_specific_pre_link may generate. */
522 int lang_specific_extra_outfiles = 0; /* Not used for D. */