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)
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
48 #define LIBDRUNTIME "gdruntime"
50 #ifndef LIBDRUNTIME_PROFILE
51 #define LIBDRUNTIME_PROFILE LIBDRUNTIME
54 /* What do with libgphobos. */
57 /* libgphobos should not be linked in. */
59 /* libgphobos should be linked in if it is needed. */
61 /* libgphobos is needed and should be linked in. */
63 /* libgphobos is needed and should be linked statically. */
65 /* libgphobos is needed and should be linked dynamically. */
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;
76 lang_specific_driver (cl_decoded_option
**in_decoded_options
,
77 unsigned int *in_decoded_options_count
,
78 int *in_added_libraries
)
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
)
148 case OPT_nodefaultlibs
:
149 phobos_library
= PHOBOS_NOLINK
;
152 case OPT_nophoboslib
:
157 case OPT_defaultlib_
:
158 if (defaultlib
!= NULL
)
159 free (CONST_CAST (char *, defaultlib
));
164 defaultlib
= XNEWVEC (char, strlen (arg
));
165 strcpy (CONST_CAST (char *, defaultlib
), arg
);
170 if (debuglib
!= NULL
)
171 free (CONST_CAST (char *, debuglib
));
176 debuglib
= XNEWVEC (char, strlen (arg
));
177 strcpy (CONST_CAST (char *, debuglib
), arg
);
182 if ((strcmp (arg
, LIBSTDCXX
) == 0)
183 || (strcmp (arg
, LIBSTDCXX_PROFILE
) == 0))
185 args
[i
] |= WITHLIBCXX
;
188 /* Unrecognized libraries (e.g. -ltango) may require libphobos. */
189 else if (phobos_library
== PHOBOS_DEFAULT
)
190 phobos_library
= PHOBOS_LINK
;
199 saw_debug_flag
= true;
203 /* If they only gave us `-v', don't try to link in libphobos. */
205 phobos_library
= PHOBOS_NOLINK
;
209 if (phobos_library
== PHOBOS_DEFAULT
&& (strcmp (arg
, "d") == 0))
210 phobos_library
= PHOBOS_LINK
;
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
;
225 case OPT_fsyntax_only
:
226 /* Don't specify libaries if we won't link, since that would
229 phobos_library
= PHOBOS_NOLINK
;
234 phobos_library
= PHOBOS_NOLINK
;
245 case OPT_static_libgcc
:
246 shared_libgcc
= false;
249 case OPT_static_libphobos
:
250 if (phobos_library
!= PHOBOS_NOLINK
)
251 phobos_library
= PHOBOS_STATIC
;
255 case OPT_shared_libphobos
:
256 if (phobos_library
!= PHOBOS_NOLINK
)
257 phobos_library
= PHOBOS_DYNAMIC
;
263 only_source_option
= decoded_options
[i
].orig_option_with_args_text
;
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
);
273 case OPT_SPECIAL_input_file
:
275 if (arg
[0] == '\0' || arg
[1] == '\0')
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
)
291 /* If this is a C++ source file, we'll need to link
292 against libstdc++ library. */
294 && (strcmp (suffix
, ".cc") == 0
295 || (strcmp (suffix
, ".cpp") == 0)
296 || (strcmp (suffix
, ".c++") == 0)))
304 /* There's no point adding -shared-libgcc if we don't have a shared
306 #ifndef ENABLE_SHARED_LIBGCC
307 shared_libgcc
= false;
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
);
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. */
326 if (args
[i
] & SKIPOPT
)
332 new_decoded_options
[j
] = decoded_options
[i
];
334 if (!saw_libcxx
&& (args
[i
] & WITHLIBCXX
))
337 saw_libcxx
= &decoded_options
[i
];
340 if (args
[i
] & DSOURCE
)
342 if (only_source_option
)
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
]);
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. */
380 out
[baselen
+ 1] = 's';
382 out
[baselen
+ 1] = 'o';
383 out
[baselen
+ 2] = '\0';
384 generate_option (OPT_o
, out
, 1, CL_DRIVER
,
385 &new_decoded_options
[j
]);
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
;
399 generate_option (OPT_o
, out
, 1, CL_DRIVER
,
400 &new_decoded_options
[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
]);
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
]);
427 generate_option (OPT_l
,
428 saw_profile_flag
? LIBPHOBOS_PROFILE
: LIBPHOBOS
, 1,
429 CL_DRIVER
, &new_decoded_options
[j
]);
432 generate_option (OPT_l
,
433 saw_profile_flag
? LIBDRUNTIME_PROFILE
: LIBDRUNTIME
, 1,
434 CL_DRIVER
, &new_decoded_options
[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
]);
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
]);
453 else if (saw_debug_flag
&& debuglib
)
455 generate_option (OPT_l
, debuglib
, 1, CL_DRIVER
,
456 &new_decoded_options
[j
++]);
461 generate_option (OPT_l
, defaultlib
, 1, CL_DRIVER
,
462 &new_decoded_options
[j
++]);
467 new_decoded_options
[j
++] = *saw_libcxx
;
468 else if (need_stdcxx
)
470 generate_option (OPT_l
,
474 1, CL_DRIVER
, &new_decoded_options
[j
++]);
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)");
500 /* Number of extra output files that lang_specific_pre_link may generate. */
502 int lang_specific_extra_outfiles
= 0; /* Not used for D. */