1 /* d-spec.c -- Specific flags and argument handling of the D front end.
2 Copyright (C) 2006-2020 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)
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/>. */
20 #include "coretypes.h"
21 #include "opt-suggestions.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)
34 #define LIBSTDCXX "stdc++"
36 #ifndef LIBSTDCXX_PROFILE
37 #define LIBSTDCXX_PROFILE LIBSTDCXX
41 #define LIBPHOBOS "gphobos"
43 #ifndef LIBPHOBOS_PROFILE
44 #define LIBPHOBOS_PROFILE LIBPHOBOS
47 /* What do with libgphobos. */
50 /* libgphobos should not be linked in. */
52 /* libgphobos should be linked in if it is needed. */
54 /* libgphobos is needed and should be linked in. */
56 /* libgphobos is needed and should be linked statically. */
58 /* libgphobos is needed and should be linked dynamically. */
62 static phobos_action phobos_library = PHOBOS_DEFAULT;
64 /* If true, use the standard D runtime library when linking with
65 standard libraries. */
66 static bool need_phobos = true;
68 /* If true, do load libgphobos.spec even if not needed otherwise. */
69 static bool need_spec = false;
72 lang_specific_driver (cl_decoded_option **in_decoded_options,
73 unsigned int *in_decoded_options_count,
74 int *in_added_libraries)
78 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
79 int saw_profile_flag = 0;
81 /* If true, the user gave `-g'. Used by -debuglib. */
82 bool saw_debug_flag = false;
84 /* The new argument list will be contained in this. */
85 cl_decoded_option *new_decoded_options;
87 /* "-lstdc++" if it appears on the command line. */
88 const cl_decoded_option *saw_libcxx = 0;
90 /* Whether we need the C++ STD library. */
91 bool need_stdcxx = false;
93 /* True if we saw -static. */
94 bool static_link = false;
96 /* True if we should add -shared-libgcc to the command-line. */
97 bool shared_libgcc = true;
99 /* What default library to use instead of phobos. */
100 const char *defaultlib = NULL;
102 /* What debug library to use instead of phobos. */
103 const char *debuglib = NULL;
105 /* The total number of arguments with the new stuff. */
106 unsigned int num_args = 1;
108 /* "-fonly" if it appears on the command line. */
109 const char *only_source_option = 0;
111 /* Whether the -o option was used. */
112 bool saw_opt_o = false;
114 /* Whether the -c option was used. Also used for -E, -fsyntax-only,
115 in general anything which implies only compilation and not linking. */
116 bool saw_opt_c = false;
118 /* Whether the -S option was used. */
119 bool saw_opt_S = false;
121 /* The first input file with an extension of .d. */
122 const char *first_d_file = NULL;
124 /* The total number of arguments with the new stuff. */
125 unsigned int argc = *in_decoded_options_count;
127 /* The argument list. */
128 cl_decoded_option *decoded_options = *in_decoded_options;
130 /* The number of libraries added in. */
131 int added_libraries = *in_added_libraries;
133 /* An array used to flag each argument that needs a bit set for
134 DSOURCE, MATHLIB, WITHTHREAD, WITHLIBC or WITHLIBCXX. */
135 int *args = XCNEWVEC (int, argc);
137 for (i = 1; i < argc; i++)
139 const char *arg = decoded_options[i].arg;
140 const int value = decoded_options[i].value;
142 switch (decoded_options[i].opt_index)
144 case OPT_dstartfiles:
149 case OPT_nodefaultlibs:
150 phobos_library = PHOBOS_NOLINK;
153 case OPT_nophoboslib:
163 case OPT_defaultlib_:
164 if (defaultlib != NULL)
165 free (CONST_CAST (char *, defaultlib));
170 defaultlib = XNEWVEC (char, strlen (arg));
171 strcpy (CONST_CAST (char *, defaultlib), arg);
176 if (debuglib != NULL)
177 free (CONST_CAST (char *, debuglib));
182 debuglib = XNEWVEC (char, strlen (arg));
183 strcpy (CONST_CAST (char *, debuglib), arg);
188 if ((strcmp (arg, LIBSTDCXX) == 0)
189 || (strcmp (arg, LIBSTDCXX_PROFILE) == 0))
191 args[i] |= WITHLIBCXX;
194 /* Unrecognized libraries (e.g. -ltango) may require libphobos. */
195 else if (phobos_library == PHOBOS_DEFAULT)
196 phobos_library = PHOBOS_LINK;
205 saw_debug_flag = true;
209 /* If they only gave us `-v', don't try to link in libphobos. */
211 phobos_library = PHOBOS_NOLINK;
215 if (phobos_library == PHOBOS_DEFAULT && (strcmp (arg, "d") == 0))
216 phobos_library = PHOBOS_LINK;
221 /* Arguments that go directly to the linker might be .o files
222 or something, and so might cause libphobos to be needed. */
223 if (phobos_library == PHOBOS_DEFAULT)
224 phobos_library = PHOBOS_LINK;
231 case OPT_fsyntax_only:
232 /* Don't specify libaries if we won't link, since that would
235 phobos_library = PHOBOS_NOLINK;
240 phobos_library = PHOBOS_NOLINK;
251 case OPT_static_libgcc:
252 shared_libgcc = false;
255 case OPT_static_libphobos:
256 if (phobos_library != PHOBOS_NOLINK)
257 phobos_library = PHOBOS_STATIC;
261 case OPT_shared_libphobos:
262 if (phobos_library != PHOBOS_NOLINK)
263 phobos_library = PHOBOS_DYNAMIC;
269 only_source_option = decoded_options[i].orig_option_with_args_text;
273 const char *suffix = strrchr (only_source_option, '.');
274 if (suffix == NULL || strcmp (suffix, ".d") != 0)
275 only_source_option = concat (only_source_option, ".d", NULL);
279 case OPT_SPECIAL_input_file:
281 if (arg[0] == '\0' || arg[1] == '\0')
284 if (phobos_library == PHOBOS_DEFAULT)
285 phobos_library = PHOBOS_LINK;
287 /* Record that this is a D source file. */
288 const char *suffix = strrchr (arg, '.');
289 if (suffix != NULL && strcmp (suffix, ".d") == 0)
291 if (first_d_file == NULL)
297 /* If this is a C++ source file, we'll need to link
298 against libstdc++ library. */
300 && (strcmp (suffix, ".cc") == 0
301 || (strcmp (suffix, ".cpp") == 0)
302 || (strcmp (suffix, ".c++") == 0)))
310 /* There's no point adding -shared-libgcc if we don't have a shared
312 #ifndef ENABLE_SHARED_LIBGCC
313 shared_libgcc = false;
316 /* Make sure to have room for the trailing NULL argument.
317 - needstdcxx might add `-lstdcxx'
318 - libphobos adds `-Bstatic -lphobos -Bdynamic'
319 - only_source adds 1 more arg, also maybe add `-o'. */
320 num_args = argc + need_stdcxx + shared_libgcc + need_phobos * 4 + 2;
321 new_decoded_options = XNEWVEC (cl_decoded_option, num_args);
326 /* Copy the 0th argument, i.e., the name of the program itself. */
327 new_decoded_options[j++] = decoded_options[i++];
329 /* NOTE: We start at 1 now, not 0. */
332 if (args[i] & SKIPOPT)
338 new_decoded_options[j] = decoded_options[i];
340 if (!saw_libcxx && (args[i] & WITHLIBCXX))
343 saw_libcxx = &decoded_options[i];
346 if (args[i] & DSOURCE)
348 if (only_source_option)
356 if (only_source_option)
358 const char *only_source_arg = only_source_option + 7;
359 generate_option (OPT_fonly_, only_source_arg, 1, CL_DRIVER,
360 &new_decoded_options[j]);
363 generate_option_input_file (only_source_arg,
364 &new_decoded_options[j++]);
367 /* If no reason to link against libphobos library, then don't add it. */
368 if (phobos_library == PHOBOS_DEFAULT)
369 phobos_library = PHOBOS_NOLINK;
371 /* If we didn't see a -o option, add one. This is because we need the
372 driver to pass all .d files to the D compiler. Without a -o option
373 the driver will invoke the compiler separately for each input file. */
374 if (first_d_file != NULL && !saw_opt_o)
376 if (saw_opt_c || saw_opt_S)
378 const char *base = lbasename (first_d_file);
379 int baselen = strlen (base) - 2;
380 char *out = XNEWVEC (char, baselen + 3);
382 memcpy (out, base, baselen);
383 /* The driver will convert .o to some other suffix if appropriate. */
386 out[baselen + 1] = 's';
388 out[baselen + 1] = 'o';
389 out[baselen + 2] = '\0';
390 generate_option (OPT_o, out, 1, CL_DRIVER,
391 &new_decoded_options[j]);
395 /* Wouldn't be necessary if the driver converted .out also. */
396 const char *out = NULL;
398 #ifdef TARGET_EXECUTABLE_SUFFIX
399 if (TARGET_EXECUTABLE_SUFFIX[0] != 0)
400 out = "a" TARGET_EXECUTABLE_SUFFIX;
405 generate_option (OPT_o, out, 1, CL_DRIVER,
406 &new_decoded_options[j]);
411 /* Add `-lgphobos' if we haven't already done so. */
412 if (phobos_library != PHOBOS_NOLINK && need_phobos)
414 /* Default to static linking. */
415 if (phobos_library != PHOBOS_DYNAMIC)
416 phobos_library = PHOBOS_STATIC;
418 #ifdef HAVE_LD_STATIC_DYNAMIC
419 if (phobos_library == PHOBOS_DYNAMIC && static_link)
421 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
422 &new_decoded_options[j]);
425 else if (phobos_library == PHOBOS_STATIC && !static_link)
427 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
428 &new_decoded_options[j]);
433 generate_option (OPT_l,
434 saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
435 CL_DRIVER, &new_decoded_options[j]);
439 #ifdef HAVE_LD_STATIC_DYNAMIC
440 if (phobos_library == PHOBOS_DYNAMIC && static_link)
442 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
443 &new_decoded_options[j]);
446 else if (phobos_library == PHOBOS_STATIC && !static_link)
448 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
449 &new_decoded_options[j]);
454 else if (saw_debug_flag && debuglib)
456 generate_option (OPT_l, debuglib, 1, CL_DRIVER,
457 &new_decoded_options[j++]);
462 generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
463 &new_decoded_options[j++]);
468 new_decoded_options[j++] = *saw_libcxx;
469 else if (need_stdcxx)
471 generate_option (OPT_l,
475 1, CL_DRIVER, &new_decoded_options[j++]);
479 if (shared_libgcc && !static_link)
481 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
482 &new_decoded_options[j++]);
485 *in_decoded_options_count = j;
486 *in_decoded_options = new_decoded_options;
487 *in_added_libraries = added_libraries;
490 /* Called before linking. Returns 0 on success and -1 on failure. */
493 lang_specific_pre_link (void)
495 if ((phobos_library != PHOBOS_NOLINK && need_phobos) || need_spec)
496 do_spec ("%:include(libgphobos.spec)");
501 /* Number of extra output files that lang_specific_pre_link may generate. */
503 int lang_specific_extra_outfiles = 0; /* Not used for D. */