]> gcc.gnu.org Git - gcc.git/blob - gcc/d/d-spec.cc
Merge branch 'master' into devel/modula-2.
[gcc.git] / gcc / d / d-spec.cc
1 /* d-spec.c -- Specific flags and argument handling of the D front end.
2 Copyright (C) 2006-2021 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
17
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"
25
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)
32
33 #ifndef LIBSTDCXX
34 #define LIBSTDCXX "stdc++"
35 #endif
36 #ifndef LIBSTDCXX_PROFILE
37 #define LIBSTDCXX_PROFILE LIBSTDCXX
38 #endif
39
40 #ifndef LIBPHOBOS
41 #define LIBPHOBOS "gphobos"
42 #endif
43 #ifndef LIBPHOBOS_PROFILE
44 #define LIBPHOBOS_PROFILE LIBPHOBOS
45 #endif
46
47 /* What do with libgphobos. */
48 enum phobos_action
49 {
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
60 };
61
62 static phobos_action phobos_library = PHOBOS_DEFAULT;
63
64 /* If true, do load libgphobos.spec even if not needed otherwise. */
65 static bool need_spec = false;
66
67 void
68 lang_specific_driver (cl_decoded_option **in_decoded_options,
69 unsigned int *in_decoded_options_count,
70 int *in_added_libraries)
71 {
72 unsigned int i, j;
73
74 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
75 int saw_profile_flag = 0;
76
77 /* If true, the user gave `-g'. Used by -debuglib. */
78 bool saw_debug_flag = false;
79
80 /* The new argument list will be contained in this. */
81 cl_decoded_option *new_decoded_options;
82
83 /* "-lstdc++" if it appears on the command line. */
84 const cl_decoded_option *saw_libcxx = 0;
85
86 /* True if we saw `-static-libstdc++'. */
87 bool saw_static_libcxx = false;
88
89 /* Whether we need the C++ STD library. */
90 bool need_stdcxx = false;
91
92 /* True if we saw -static. */
93 bool static_link = false;
94
95 /* True if we should add -shared-libgcc to the command-line. */
96 bool shared_libgcc = true;
97
98 /* What default library to use instead of phobos. */
99 const char *defaultlib = NULL;
100
101 /* What debug library to use instead of phobos. */
102 const char *debuglib = NULL;
103
104 /* The total number of arguments with the new stuff. */
105 unsigned int num_args = 1;
106
107 /* "-fonly" if it appears on the command line. */
108 const char *only_source_option = 0;
109
110 /* Whether the -o option was used. */
111 bool saw_opt_o = false;
112
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;
116
117 /* Whether the -S option was used. */
118 bool saw_opt_S = false;
119
120 /* The first input file with an extension of .d. */
121 const char *first_d_file = NULL;
122
123 /* The total number of arguments with the new stuff. */
124 unsigned int argc = *in_decoded_options_count;
125
126 /* The argument list. */
127 cl_decoded_option *decoded_options = *in_decoded_options;
128
129 /* The number of libraries added in. */
130 int added_libraries = *in_added_libraries;
131
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);
135
136 for (i = 1; i < argc; i++)
137 {
138 const char *arg = decoded_options[i].arg;
139 const int value = decoded_options[i].value;
140
141 switch (decoded_options[i].opt_index)
142 {
143 case OPT_dstartfiles:
144 need_spec = true;
145 break;
146
147 case OPT_nostdlib:
148 case OPT_nodefaultlibs:
149 phobos_library = PHOBOS_NOLINK;
150 break;
151
152 case OPT_nophoboslib:
153 phobos_library = PHOBOS_NOLINK;
154 args[i] |= SKIPOPT;
155 break;
156
157 case OPT_fdruntime:
158 if (!value)
159 phobos_library = PHOBOS_NOLINK;
160 else
161 phobos_library = PHOBOS_LINK;
162 break;
163
164 case OPT_defaultlib_:
165 if (defaultlib != NULL)
166 free (CONST_CAST (char *, defaultlib));
167 if (arg != NULL)
168 {
169 args[i] |= SKIPOPT;
170 defaultlib = XNEWVEC (char, strlen (arg));
171 strcpy (CONST_CAST (char *, defaultlib), arg);
172 }
173 break;
174
175 case OPT_debuglib_:
176 if (debuglib != NULL)
177 free (CONST_CAST (char *, debuglib));
178 if (arg != NULL)
179 {
180 args[i] |= SKIPOPT;
181 debuglib = XNEWVEC (char, strlen (arg));
182 strcpy (CONST_CAST (char *, debuglib), arg);
183 }
184 break;
185
186 case OPT_l:
187 if ((strcmp (arg, LIBSTDCXX) == 0)
188 || (strcmp (arg, LIBSTDCXX_PROFILE) == 0))
189 {
190 args[i] |= WITHLIBCXX;
191 need_stdcxx = false;
192 }
193 /* Unrecognized libraries (e.g. -ltango) may require libphobos. */
194 else if (phobos_library == PHOBOS_DEFAULT)
195 phobos_library = PHOBOS_LINK;
196 break;
197
198 case OPT_pg:
199 case OPT_p:
200 saw_profile_flag++;
201 break;
202
203 case OPT_g:
204 saw_debug_flag = true;
205 break;
206
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;
212
213 case OPT_x:
214 if (phobos_library == PHOBOS_DEFAULT && (strcmp (arg, "d") == 0))
215 phobos_library = PHOBOS_LINK;
216 break;
217
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;
225
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;
236
237 case OPT_S:
238 saw_opt_S = true;
239 phobos_library = PHOBOS_NOLINK;
240 break;
241
242 case OPT_o:
243 saw_opt_o = true;
244 break;
245
246 case OPT_static:
247 static_link = true;
248 break;
249
250 case OPT_static_libgcc:
251 shared_libgcc = false;
252 break;
253
254 case OPT_static_libstdc__:
255 saw_static_libcxx = true;
256 #ifdef HAVE_LD_STATIC_DYNAMIC
257 /* Remove -static-libstdc++ from the command only if target supports
258 LD_STATIC_DYNAMIC. When not supported, it is left in so that a
259 back-end target can use outfile substitution. */
260 args[i] |= SKIPOPT;
261 #endif
262 break;
263
264 case OPT_static_libphobos:
265 if (phobos_library != PHOBOS_NOLINK)
266 phobos_library = PHOBOS_STATIC;
267 #ifdef HAVE_LD_STATIC_DYNAMIC
268 /* Remove -static-libphobos from the command only if target supports
269 LD_STATIC_DYNAMIC. When not supported, it is left in so that a
270 back-end target can use outfile substitution. */
271 args[i] |= SKIPOPT;
272 #endif
273 break;
274
275 case OPT_shared_libphobos:
276 if (phobos_library != PHOBOS_NOLINK)
277 phobos_library = PHOBOS_DYNAMIC;
278 args[i] |= SKIPOPT;
279 break;
280
281 case OPT_fonly_:
282 args[i] |= SKIPOPT;
283 only_source_option = decoded_options[i].orig_option_with_args_text;
284
285 if (arg != NULL)
286 {
287 const char *suffix = strrchr (only_source_option, '.');
288 if (suffix == NULL || strcmp (suffix, ".d") != 0)
289 only_source_option = concat (only_source_option, ".d", NULL);
290 }
291 break;
292
293 case OPT_SPECIAL_input_file:
294 {
295 if (arg[0] == '\0' || arg[1] == '\0')
296 continue;
297
298 if (phobos_library == PHOBOS_DEFAULT)
299 phobos_library = PHOBOS_LINK;
300
301 /* Record that this is a D source file. */
302 const char *suffix = strrchr (arg, '.');
303 if (suffix != NULL && strcmp (suffix, ".d") == 0)
304 {
305 if (first_d_file == NULL)
306 first_d_file = arg;
307
308 args[i] |= DSOURCE;
309 }
310
311 /* If this is a C++ source file, we'll need to link
312 against libstdc++ library. */
313 if (suffix != NULL
314 && (strcmp (suffix, ".cc") == 0
315 || (strcmp (suffix, ".cpp") == 0)
316 || (strcmp (suffix, ".c++") == 0)))
317 need_stdcxx = true;
318
319 break;
320 }
321 }
322 }
323
324 /* There's no point adding -shared-libgcc if we don't have a shared
325 libgcc. */
326 #ifndef ENABLE_SHARED_LIBGCC
327 shared_libgcc = false;
328 #endif
329
330 /* Make sure to have room for the trailing NULL argument.
331 - need_stdcxx might add `-lstdcxx'
332 - libphobos adds `-Bstatic -lphobos -Bdynamic'
333 - only_source adds 1 more arg, also maybe add `-o'. */
334 num_args = argc + need_stdcxx + shared_libgcc
335 + (phobos_library != PHOBOS_NOLINK) * 4 + 2;
336 new_decoded_options = XNEWVEC (cl_decoded_option, num_args);
337
338 i = 0;
339 j = 0;
340
341 /* Copy the 0th argument, i.e., the name of the program itself. */
342 new_decoded_options[j++] = decoded_options[i++];
343
344 /* NOTE: We start at 1 now, not 0. */
345 while (i < argc)
346 {
347 if (args[i] & SKIPOPT)
348 {
349 ++i;
350 continue;
351 }
352
353 new_decoded_options[j] = decoded_options[i];
354
355 if (!saw_libcxx && (args[i] & WITHLIBCXX))
356 {
357 --j;
358 saw_libcxx = &decoded_options[i];
359 }
360
361 if (args[i] & DSOURCE)
362 {
363 if (only_source_option)
364 --j;
365 }
366
367 i++;
368 j++;
369 }
370
371 if (only_source_option)
372 {
373 const char *only_source_arg = only_source_option + 7;
374 generate_option (OPT_fonly_, only_source_arg, 1, CL_DRIVER,
375 &new_decoded_options[j]);
376 j++;
377
378 generate_option_input_file (only_source_arg,
379 &new_decoded_options[j++]);
380 }
381
382 /* If no reason to link against libphobos library, then don't add it. */
383 if (phobos_library == PHOBOS_DEFAULT)
384 phobos_library = PHOBOS_NOLINK;
385
386 /* If we didn't see a -o option, add one. This is because we need the
387 driver to pass all .d files to the D compiler. Without a -o option
388 the driver will invoke the compiler separately for each input file. */
389 if (first_d_file != NULL && !saw_opt_o)
390 {
391 if (saw_opt_c || saw_opt_S)
392 {
393 const char *base = lbasename (first_d_file);
394 int baselen = strlen (base) - 2;
395 char *out = XNEWVEC (char, baselen + 3);
396
397 memcpy (out, base, baselen);
398 /* The driver will convert .o to some other suffix if appropriate. */
399 out[baselen] = '.';
400 if (saw_opt_S)
401 out[baselen + 1] = 's';
402 else
403 out[baselen + 1] = 'o';
404 out[baselen + 2] = '\0';
405 generate_option (OPT_o, out, 1, CL_DRIVER,
406 &new_decoded_options[j]);
407 }
408 else
409 {
410 /* Wouldn't be necessary if the driver converted .out also. */
411 const char *out = NULL;
412
413 #ifdef TARGET_EXECUTABLE_SUFFIX
414 if (TARGET_EXECUTABLE_SUFFIX[0] != 0)
415 out = "a" TARGET_EXECUTABLE_SUFFIX;
416 #endif
417 if (out == NULL)
418 out = "a.out";
419
420 generate_option (OPT_o, out, 1, CL_DRIVER,
421 &new_decoded_options[j]);
422 }
423 j++;
424 }
425
426 /* Add `-lgphobos' if we haven't already done so. */
427 if (phobos_library != PHOBOS_NOLINK)
428 {
429 /* Default to static linking. */
430 if (phobos_library != PHOBOS_DYNAMIC)
431 phobos_library = PHOBOS_STATIC;
432
433 #ifdef HAVE_LD_STATIC_DYNAMIC
434 if (phobos_library == PHOBOS_STATIC && !static_link)
435 {
436 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
437 &new_decoded_options[j++]);
438 }
439 #endif
440 /* Order of precedence in determining what library to link against is:
441 - `-l<lib>' from `-debuglib=<lib>' if `-g' was also seen.
442 - `-l<lib>' from `-defaultlib=<lib>'.
443 - `-lgphobos' unless `-nophoboslib' or `-fno-druntime' was seen. */
444 if (debuglib && saw_debug_flag)
445 {
446 generate_option (OPT_l, debuglib, 1, CL_DRIVER,
447 &new_decoded_options[j++]);
448 added_libraries++;
449 }
450 else if (defaultlib)
451 {
452 generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
453 &new_decoded_options[j++]);
454 added_libraries++;
455 }
456 else
457 {
458 generate_option (OPT_l,
459 saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
460 CL_DRIVER, &new_decoded_options[j++]);
461 added_libraries++;
462 }
463
464 #ifdef HAVE_LD_STATIC_DYNAMIC
465 if (phobos_library == PHOBOS_STATIC && !static_link)
466 {
467 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
468 &new_decoded_options[j++]);
469 }
470 #endif
471 }
472
473 if (saw_libcxx || saw_static_libcxx || need_stdcxx)
474 {
475 #ifdef HAVE_LD_STATIC_DYNAMIC
476 if (saw_static_libcxx && !static_link)
477 {
478 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
479 &new_decoded_options[j++]);
480 }
481 #endif
482 if (saw_libcxx)
483 new_decoded_options[j++] = *saw_libcxx;
484 else if (need_stdcxx)
485 {
486 generate_option (OPT_l,
487 (saw_profile_flag
488 ? LIBSTDCXX_PROFILE
489 : LIBSTDCXX),
490 1, CL_DRIVER, &new_decoded_options[j++]);
491 added_libraries++;
492 }
493 #ifdef HAVE_LD_STATIC_DYNAMIC
494 if (saw_static_libcxx && !static_link)
495 {
496 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
497 &new_decoded_options[j++]);
498 }
499 #endif
500 }
501
502 if (shared_libgcc && !static_link)
503 {
504 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
505 &new_decoded_options[j++]);
506 }
507
508 *in_decoded_options_count = j;
509 *in_decoded_options = new_decoded_options;
510 *in_added_libraries = added_libraries;
511 }
512
513 /* Called before linking. Returns 0 on success and -1 on failure. */
514
515 int
516 lang_specific_pre_link (void)
517 {
518 if ((phobos_library != PHOBOS_NOLINK) || need_spec)
519 do_spec ("%:include(libgphobos.spec)");
520
521 return 0;
522 }
523
524 /* Number of extra output files that lang_specific_pre_link may generate. */
525
526 int lang_specific_extra_outfiles = 0; /* Not used for D. */
527
528 /* lang_register_spec_functions register the D associated spec
529 functions. Not used for D. */
530
531 void
532 lang_register_spec_functions (void)
533 {
534 }
This page took 0.056115 seconds and 5 git commands to generate.