gcc/ChangeLog:
[official-gcc.git] / gcc / d / d-spec.cc
blobd0b784458360e23e7ffec04124e909273a4d0a28
1 /* d-spec.c -- Specific flags and argument handling of the D front end.
2 Copyright (C) 2006-2018 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 #ifndef LIBDRUNTIME
48 #define LIBDRUNTIME "gdruntime"
49 #endif
50 #ifndef LIBDRUNTIME_PROFILE
51 #define LIBDRUNTIME_PROFILE LIBDRUNTIME
52 #endif
54 /* What do with libgphobos. */
55 enum phobos_action
57 /* libgphobos should not be linked in. */
58 PHOBOS_NOLINK = -1,
59 /* libgphobos should be linked in if it is needed. */
60 PHOBOS_DEFAULT = 0,
61 /* libgphobos is needed and should be linked in. */
62 PHOBOS_LINK,
63 /* libgphobos is needed and should be linked statically. */
64 PHOBOS_STATIC,
65 /* libgphobos is needed and should be linked dynamically. */
66 PHOBOS_DYNAMIC,
69 static phobos_action phobos_library = PHOBOS_DEFAULT;
71 /* If true, use the standard D runtime library when linking with
72 standard libraries. */
73 static bool need_phobos = true;
75 void
76 lang_specific_driver (cl_decoded_option **in_decoded_options,
77 unsigned int *in_decoded_options_count,
78 int *in_added_libraries)
80 unsigned int i, j;
82 /* If nonzero, the user gave us the `-p' or `-pg' flag. */
83 int saw_profile_flag = 0;
85 /* If true, the user gave `-g'. Used by -debuglib. */
86 bool saw_debug_flag = false;
88 /* The new argument list will be contained in this. */
89 cl_decoded_option *new_decoded_options;
91 /* "-lstdc++" if it appears on the command line. */
92 const cl_decoded_option *saw_libcxx = 0;
94 /* Whether we need the C++ STD library. */
95 bool need_stdcxx = false;
97 /* True if we saw -static. */
98 bool static_link = false;
100 /* True if we should add -shared-libgcc to the command-line. */
101 bool shared_libgcc = true;
103 /* What default library to use instead of phobos. */
104 const char *defaultlib = NULL;
106 /* What debug library to use instead of phobos. */
107 const char *debuglib = NULL;
109 /* The total number of arguments with the new stuff. */
110 unsigned int num_args = 1;
112 /* "-fonly" if it appears on the command line. */
113 const char *only_source_option = 0;
115 /* Whether the -o option was used. */
116 bool saw_opt_o = false;
118 /* Whether the -c option was used. Also used for -E, -fsyntax-only,
119 in general anything which implies only compilation and not linking. */
120 bool saw_opt_c = false;
122 /* Whether the -S option was used. */
123 bool saw_opt_S = false;
125 /* The first input file with an extension of .d. */
126 const char *first_d_file = NULL;
128 /* The total number of arguments with the new stuff. */
129 unsigned int argc = *in_decoded_options_count;
131 /* The argument list. */
132 cl_decoded_option *decoded_options = *in_decoded_options;
134 /* The number of libraries added in. */
135 int added_libraries = *in_added_libraries;
137 /* An array used to flag each argument that needs a bit set for
138 DSOURCE, MATHLIB, WITHTHREAD, WITHLIBC or WITHLIBCXX. */
139 int *args = XCNEWVEC (int, argc);
141 for (i = 1; i < argc; i++)
143 const char *arg = decoded_options[i].arg;
145 switch (decoded_options[i].opt_index)
147 case OPT_nostdlib:
148 case OPT_nodefaultlibs:
149 phobos_library = PHOBOS_NOLINK;
150 break;
152 case OPT_nophoboslib:
153 need_phobos = false;
154 args[i] |= SKIPOPT;
155 break;
157 case OPT_defaultlib_:
158 if (defaultlib != NULL)
159 free (CONST_CAST (char *, defaultlib));
160 if (arg != NULL)
162 need_phobos = false;
163 args[i] |= SKIPOPT;
164 defaultlib = XNEWVEC (char, strlen (arg));
165 strcpy (CONST_CAST (char *, defaultlib), arg);
167 break;
169 case OPT_debuglib_:
170 if (debuglib != NULL)
171 free (CONST_CAST (char *, debuglib));
172 if (arg != NULL)
174 need_phobos = false;
175 args[i] |= SKIPOPT;
176 debuglib = XNEWVEC (char, strlen (arg));
177 strcpy (CONST_CAST (char *, debuglib), arg);
179 break;
181 case OPT_l:
182 if ((strcmp (arg, LIBSTDCXX) == 0)
183 || (strcmp (arg, LIBSTDCXX_PROFILE) == 0))
185 args[i] |= WITHLIBCXX;
186 need_stdcxx = false;
188 /* Unrecognized libraries (e.g. -ltango) may require libphobos. */
189 else if (phobos_library == PHOBOS_DEFAULT)
190 phobos_library = PHOBOS_LINK;
191 break;
193 case OPT_pg:
194 case OPT_p:
195 saw_profile_flag++;
196 break;
198 case OPT_g:
199 saw_debug_flag = true;
200 break;
202 case OPT_v:
203 /* If they only gave us `-v', don't try to link in libphobos. */
204 if (argc == 2)
205 phobos_library = PHOBOS_NOLINK;
206 break;
208 case OPT_x:
209 if (phobos_library == PHOBOS_DEFAULT && (strcmp (arg, "d") == 0))
210 phobos_library = PHOBOS_LINK;
211 break;
213 case OPT_Xlinker:
214 case OPT_Wl_:
215 /* Arguments that go directly to the linker might be .o files
216 or something, and so might cause libphobos to be needed. */
217 if (phobos_library == PHOBOS_DEFAULT)
218 phobos_library = PHOBOS_LINK;
219 break;
221 case OPT_c:
222 case OPT_E:
223 case OPT_M:
224 case OPT_MM:
225 case OPT_fsyntax_only:
226 /* Don't specify libaries if we won't link, since that would
227 cause a warning. */
228 saw_opt_c = true;
229 phobos_library = PHOBOS_NOLINK;
230 break;
232 case OPT_S:
233 saw_opt_S = true;
234 phobos_library = PHOBOS_NOLINK;
235 break;
237 case OPT_o:
238 saw_opt_o = true;
239 break;
241 case OPT_static:
242 static_link = true;
243 break;
245 case OPT_static_libgcc:
246 shared_libgcc = false;
247 break;
249 case OPT_static_libphobos:
250 if (phobos_library != PHOBOS_NOLINK)
251 phobos_library = PHOBOS_STATIC;
252 args[i] |= SKIPOPT;
253 break;
255 case OPT_shared_libphobos:
256 if (phobos_library != PHOBOS_NOLINK)
257 phobos_library = PHOBOS_DYNAMIC;
258 args[i] |= SKIPOPT;
259 break;
261 case OPT_fonly_:
262 args[i] |= SKIPOPT;
263 only_source_option = decoded_options[i].orig_option_with_args_text;
265 if (arg != NULL)
267 const char *suffix = strrchr (only_source_option, '.');
268 if (suffix == NULL || strcmp (suffix, ".d") != 0)
269 only_source_option = concat (only_source_option, ".d", NULL);
271 break;
273 case OPT_SPECIAL_input_file:
275 if (arg[0] == '\0' || arg[1] == '\0')
276 continue;
278 if (phobos_library == PHOBOS_DEFAULT)
279 phobos_library = PHOBOS_LINK;
281 /* Record that this is a D source file. */
282 const char *suffix = strrchr (arg, '.');
283 if (suffix != NULL && strcmp (suffix, ".d") == 0)
285 if (first_d_file == NULL)
286 first_d_file = arg;
288 args[i] |= DSOURCE;
291 /* If this is a C++ source file, we'll need to link
292 against libstdc++ library. */
293 if (suffix != NULL
294 && (strcmp (suffix, ".cc") == 0
295 || (strcmp (suffix, ".cpp") == 0)
296 || (strcmp (suffix, ".c++") == 0)))
297 need_stdcxx = true;
299 break;
304 /* There's no point adding -shared-libgcc if we don't have a shared
305 libgcc. */
306 #ifndef ENABLE_SHARED_LIBGCC
307 shared_libgcc = false;
308 #endif
310 /* Make sure to have room for the trailing NULL argument.
311 - needstdcxx might add `-lstdcxx'
312 - libphobos adds `-Bstatic -lphobos -ldruntime -Bdynamic'
313 - only_source adds 1 more arg, also maybe add `-o'. */
314 num_args = argc + need_stdcxx + shared_libgcc + need_phobos * 4 + 2;
315 new_decoded_options = XNEWVEC (cl_decoded_option, num_args);
317 i = 0;
318 j = 0;
320 /* Copy the 0th argument, i.e., the name of the program itself. */
321 new_decoded_options[j++] = decoded_options[i++];
323 /* NOTE: We start at 1 now, not 0. */
324 while (i < argc)
326 if (args[i] & SKIPOPT)
328 ++i;
329 continue;
332 new_decoded_options[j] = decoded_options[i];
334 if (!saw_libcxx && (args[i] & WITHLIBCXX))
336 --j;
337 saw_libcxx = &decoded_options[i];
340 if (args[i] & DSOURCE)
342 if (only_source_option)
343 --j;
346 i++;
347 j++;
350 if (only_source_option)
352 const char *only_source_arg = only_source_option + 7;
353 generate_option (OPT_fonly_, only_source_arg, 1, CL_DRIVER,
354 &new_decoded_options[j]);
355 j++;
357 generate_option_input_file (only_source_arg,
358 &new_decoded_options[j++]);
361 /* If no reason to link against libphobos library, then don't add it. */
362 if (phobos_library == PHOBOS_DEFAULT)
363 phobos_library = PHOBOS_NOLINK;
365 /* If we didn't see a -o option, add one. This is because we need the
366 driver to pass all .d files to the D compiler. Without a -o option
367 the driver will invoke the compiler separately for each input file. */
368 if (first_d_file != NULL && !saw_opt_o)
370 if (saw_opt_c || saw_opt_S)
372 const char *base = lbasename (first_d_file);
373 int baselen = strlen (base) - 2;
374 char *out = XNEWVEC (char, baselen + 3);
376 memcpy (out, base, baselen);
377 /* The driver will convert .o to some other suffix if appropriate. */
378 out[baselen] = '.';
379 if (saw_opt_S)
380 out[baselen + 1] = 's';
381 else
382 out[baselen + 1] = 'o';
383 out[baselen + 2] = '\0';
384 generate_option (OPT_o, out, 1, CL_DRIVER,
385 &new_decoded_options[j]);
387 else
389 /* Wouldn't be necessary if the driver converted .out also. */
390 const char *out = NULL;
392 #ifdef TARGET_EXECUTABLE_SUFFIX
393 if (TARGET_EXECUTABLE_SUFFIX[0] != 0)
394 out = "a" TARGET_EXECUTABLE_SUFFIX;
395 #endif
396 if (out == NULL)
397 out = "a.out";
399 generate_option (OPT_o, out, 1, CL_DRIVER,
400 &new_decoded_options[j]);
402 j++;
405 /* Add `-lgphobos' if we haven't already done so. */
406 if (phobos_library != PHOBOS_NOLINK && need_phobos)
408 /* Default to static linking. */
409 if (phobos_library != PHOBOS_DYNAMIC)
410 phobos_library = PHOBOS_STATIC;
412 #ifdef HAVE_LD_STATIC_DYNAMIC
413 if (phobos_library == PHOBOS_DYNAMIC && static_link)
415 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
416 &new_decoded_options[j]);
417 j++;
419 else if (phobos_library == PHOBOS_STATIC && !static_link)
421 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
422 &new_decoded_options[j]);
423 j++;
425 #endif
427 generate_option (OPT_l,
428 saw_profile_flag ? LIBPHOBOS_PROFILE : LIBPHOBOS, 1,
429 CL_DRIVER, &new_decoded_options[j]);
430 added_libraries++;
431 j++;
432 generate_option (OPT_l,
433 saw_profile_flag ? LIBDRUNTIME_PROFILE : LIBDRUNTIME, 1,
434 CL_DRIVER, &new_decoded_options[j]);
435 added_libraries++;
436 j++;
438 #ifdef HAVE_LD_STATIC_DYNAMIC
439 if (phobos_library == PHOBOS_DYNAMIC && static_link)
441 generate_option (OPT_Wl_, LD_STATIC_OPTION, 1, CL_DRIVER,
442 &new_decoded_options[j]);
443 j++;
445 else if (phobos_library == PHOBOS_STATIC && !static_link)
447 generate_option (OPT_Wl_, LD_DYNAMIC_OPTION, 1, CL_DRIVER,
448 &new_decoded_options[j]);
449 j++;
451 #endif
453 else if (saw_debug_flag && debuglib)
455 generate_option (OPT_l, debuglib, 1, CL_DRIVER,
456 &new_decoded_options[j++]);
457 added_libraries++;
459 else if (defaultlib)
461 generate_option (OPT_l, defaultlib, 1, CL_DRIVER,
462 &new_decoded_options[j++]);
463 added_libraries++;
466 if (saw_libcxx)
467 new_decoded_options[j++] = *saw_libcxx;
468 else if (need_stdcxx)
470 generate_option (OPT_l,
471 (saw_profile_flag
472 ? LIBSTDCXX_PROFILE
473 : LIBSTDCXX),
474 1, CL_DRIVER, &new_decoded_options[j++]);
475 added_libraries++;
478 if (shared_libgcc && !static_link)
480 generate_option (OPT_shared_libgcc, NULL, 1, CL_DRIVER,
481 &new_decoded_options[j++]);
484 *in_decoded_options_count = j;
485 *in_decoded_options = new_decoded_options;
486 *in_added_libraries = added_libraries;
489 /* Called before linking. Returns 0 on success and -1 on failure. */
492 lang_specific_pre_link (void)
494 if (phobos_library != PHOBOS_NOLINK && need_phobos)
495 do_spec ("%:include(libgphobos.spec)");
497 return 0;
500 /* Number of extra output files that lang_specific_pre_link may generate. */
502 int lang_specific_extra_outfiles = 0; /* Not used for D. */