[PATCH] RISC-V: Move UNSPEC_SSP_SET and UNSPEC_SSP_TEST to correct enum
[gcc.git] / gcc / lto-wrapper.cc
bloba980b208783a18f503644be7f187fe0c9fe4305c
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2025 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #define INCLUDE_STRING
41 #define INCLUDE_ARRAY
42 #define INCLUDE_MAP
43 #define INCLUDE_VECTOR
44 #include "config.h"
45 #include "system.h"
46 #include "coretypes.h"
47 #include "intl.h"
48 #include "diagnostic.h"
49 #include "obstack.h"
50 #include "opts.h"
51 #include "options.h"
52 #include "simple-object.h"
53 #include "lto-section-names.h"
54 #include "collect-utils.h"
55 #include "opts-diagnostic.h"
56 #include "opt-suggestions.h"
57 #include "opts-jobserver.h"
58 #include "make-unique.h"
59 #include "lto-ltrans-cache.h"
61 /* Environment variable, used for passing the names of offload targets from GCC
62 driver to lto-wrapper. */
63 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
64 #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT"
66 /* By default there is no special suffix for target executables. */
67 #ifdef TARGET_EXECUTABLE_SUFFIX
68 #define HAVE_TARGET_EXECUTABLE_SUFFIX
69 #else
70 #define TARGET_EXECUTABLE_SUFFIX ""
71 #endif
73 enum lto_mode_d {
74 LTO_MODE_NONE, /* Not doing LTO. */
75 LTO_MODE_LTO, /* Normal LTO. */
76 LTO_MODE_WHOPR /* WHOPR. */
79 /* Current LTO mode. */
80 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
82 static char *ltrans_output_file;
83 static char *flto_out;
84 static unsigned int nr;
85 static int *ltrans_priorities;
86 static char **input_names;
87 static char const**output_names;
88 static char **offload_names;
89 static char *offload_objects_file_name;
90 static char *makefile;
91 static unsigned int num_deb_objs;
92 static const char **early_debug_object_names;
93 static bool xassembler_options_error = false;
95 const char tool_name[] = "lto-wrapper";
97 /* Auxiliary function that frees elements of PTR and PTR itself.
98 N is number of elements to be freed. If PTR is NULL, nothing is freed.
99 If an element is NULL, subsequent elements are not freed. */
101 static void **
102 free_array_of_ptrs (void **ptr, unsigned n)
104 if (!ptr)
105 return NULL;
106 for (unsigned i = 0; i < n; i++)
108 if (!ptr[i])
109 break;
110 free (ptr[i]);
112 free (ptr);
113 return NULL;
116 /* Delete tempfiles. Called from utils_cleanup. */
118 void
119 tool_cleanup (bool)
121 unsigned int i;
123 if (ltrans_output_file)
124 maybe_unlink (ltrans_output_file);
125 if (flto_out)
126 maybe_unlink (flto_out);
127 if (offload_objects_file_name)
128 maybe_unlink (offload_objects_file_name);
129 if (makefile)
130 maybe_unlink (makefile);
131 if (early_debug_object_names)
132 for (i = 0; i < num_deb_objs; ++i)
133 if (early_debug_object_names[i])
134 maybe_unlink (early_debug_object_names[i]);
135 for (i = 0; i < nr; ++i)
137 maybe_unlink (input_names[i]);
138 if (output_names[i])
139 maybe_unlink (output_names[i]);
141 if (offload_names)
143 for (i = 0; offload_names[i]; i++)
144 maybe_unlink (offload_names[i]);
145 free_array_of_ptrs ((void **) offload_names, i);
149 static void
150 lto_wrapper_cleanup (void)
152 utils_cleanup (false);
155 /* Unlink a temporary LTRANS file unless requested otherwise. */
157 void
158 maybe_unlink (const char *file)
160 if (!save_temps)
162 if (unlink_if_ordinary (file)
163 && errno != ENOENT)
164 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
166 else if (verbose)
167 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
170 /* Template of LTRANS dumpbase suffix. */
171 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
173 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
174 environment. */
176 static vec<cl_decoded_option>
177 get_options_from_collect_gcc_options (const char *collect_gcc,
178 const char *collect_gcc_options)
180 cl_decoded_option *decoded_options;
181 unsigned int decoded_options_count;
182 struct obstack argv_obstack;
183 const char **argv;
184 int argc;
186 obstack_init (&argv_obstack);
187 obstack_ptr_grow (&argv_obstack, collect_gcc);
189 parse_options_from_collect_gcc_options (collect_gcc_options,
190 &argv_obstack, &argc);
191 argv = XOBFINISH (&argv_obstack, const char **);
193 decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER,
194 &decoded_options, &decoded_options_count);
195 vec<cl_decoded_option> decoded;
196 decoded.create (decoded_options_count);
197 for (unsigned i = 0; i < decoded_options_count; ++i)
198 decoded.quick_push (decoded_options[i]);
199 free (decoded_options);
201 obstack_free (&argv_obstack, NULL);
203 return decoded;
206 /* Find option in OPTIONS based on OPT_INDEX, starting at START. -1
207 value is returned if the option is not present. */
209 static int
210 find_option (vec<cl_decoded_option> &options, size_t opt_index,
211 unsigned start = 0)
213 for (unsigned i = start; i < options.length (); ++i)
214 if (options[i].opt_index == opt_index)
215 return i;
217 return -1;
220 static int
221 find_option (vec<cl_decoded_option> &options, cl_decoded_option *option)
223 return find_option (options, option->opt_index);
226 /* Merge -flto FOPTION into vector of DECODED_OPTIONS. If FORCE is true
227 then FOPTION overrides previous settings. */
229 static void
230 merge_flto_options (vec<cl_decoded_option> &decoded_options,
231 cl_decoded_option *foption, bool force)
233 int existing_opt = find_option (decoded_options, foption);
234 if (existing_opt == -1)
235 decoded_options.safe_push (*foption);
236 else if (force)
237 decoded_options[existing_opt].arg = foption->arg;
238 else
240 if (strcmp (foption->arg, decoded_options[existing_opt].arg) != 0)
242 /* -flto=auto is preferred. */
243 if (strcmp (decoded_options[existing_opt].arg, "auto") == 0)
245 else if (strcmp (foption->arg, "auto") == 0
246 || strcmp (foption->arg, "jobserver") == 0)
247 decoded_options[existing_opt].arg = foption->arg;
248 else if (strcmp (decoded_options[existing_opt].arg,
249 "jobserver") != 0)
251 int n = atoi (foption->arg);
252 int original_n = atoi (decoded_options[existing_opt].arg);
253 if (n > original_n)
254 decoded_options[existing_opt].arg = foption->arg;
260 /* Try to merge and complain about options FDECODED_OPTIONS when applied
261 ontop of DECODED_OPTIONS. */
263 static void
264 merge_and_complain (vec<cl_decoded_option> &decoded_options,
265 vec<cl_decoded_option> fdecoded_options,
266 vec<cl_decoded_option> decoded_cl_options)
268 unsigned int i, j;
269 cl_decoded_option *pic_option = NULL;
270 cl_decoded_option *pie_option = NULL;
271 cl_decoded_option *cf_protection_option = NULL;
273 /* ??? Merge options from files. Most cases can be
274 handled by either unioning or intersecting
275 (for example -fwrapv is a case for unioning,
276 -ffast-math is for intersection). Most complaints
277 about real conflicts between different options can
278 be deferred to the compiler proper. Options that
279 we can neither safely handle by intersection nor
280 unioning would need to be complained about here.
281 Ideally we'd have a flag in the opt files that
282 tells whether to union or intersect or reject.
283 In absence of that it's unclear what a good default is.
284 It's also difficult to get positional handling correct. */
286 /* Look for a -fcf-protection option in the link-time options
287 which overrides any -fcf-protection from the lto sections. */
288 for (i = 0; i < decoded_cl_options.length (); ++i)
290 cl_decoded_option *foption = &decoded_cl_options[i];
291 if (foption->opt_index == OPT_fcf_protection_)
293 cf_protection_option = foption;
297 /* The following does what the old LTO option code did,
298 union all target and a selected set of common options. */
299 for (i = 0; i < fdecoded_options.length (); ++i)
301 cl_decoded_option *foption = &fdecoded_options[i];
302 int existing_opt = find_option (decoded_options, foption);
303 switch (foption->opt_index)
305 case OPT_SPECIAL_unknown:
306 case OPT_SPECIAL_ignore:
307 case OPT_SPECIAL_warn_removed:
308 case OPT_SPECIAL_program_name:
309 case OPT_SPECIAL_input_file:
310 break;
312 default:
313 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
314 break;
316 /* Fallthru. */
317 case OPT_fdiagnostics_show_caret:
318 case OPT_fdiagnostics_show_event_links:
319 case OPT_fdiagnostics_show_highlight_colors:
320 case OPT_fdiagnostics_show_labels:
321 case OPT_fdiagnostics_show_line_numbers:
322 case OPT_fdiagnostics_show_option:
323 case OPT_fdiagnostics_show_location_:
324 case OPT_fshow_column:
325 case OPT_fcommon:
326 case OPT_fgnu_tm:
327 case OPT_g:
328 /* Do what the old LTO code did - collect exactly one option
329 setting per OPT code, we pick the first we encounter.
330 ??? This doesn't make too much sense, but when it doesn't
331 then we should complain. */
332 if (existing_opt == -1)
333 decoded_options.safe_push (*foption);
334 break;
336 /* Figure out what PIC/PIE level wins and merge the results. */
337 case OPT_fPIC:
338 case OPT_fpic:
339 pic_option = foption;
340 break;
341 case OPT_fPIE:
342 case OPT_fpie:
343 pie_option = foption;
344 break;
346 case OPT_fopenmp:
347 case OPT_fopenacc:
348 case OPT_fasynchronous_unwind_tables:
349 case OPT_funwind_tables:
350 /* For selected options we can merge conservatively. */
351 if (existing_opt == -1)
352 decoded_options.safe_push (*foption);
353 /* -fopenmp > -fno-openmp,
354 -fopenacc > -fno-openacc */
355 else if (foption->value > decoded_options[existing_opt].value)
356 decoded_options[existing_opt] = *foption;
357 break;
359 case OPT_fopenacc_dim_:
360 /* Append or check identical. */
361 if (existing_opt == -1)
362 decoded_options.safe_push (*foption);
363 else if (strcmp (decoded_options[existing_opt].arg, foption->arg))
364 fatal_error (input_location,
365 "option %s with different values",
366 foption->orig_option_with_args_text);
367 break;
369 case OPT_fcf_protection_:
370 /* Default to link-time option, else append or check identical. */
371 if (!cf_protection_option
372 || cf_protection_option->value == CF_CHECK)
374 if (existing_opt == -1)
375 decoded_options.safe_push (*foption);
376 else if (decoded_options[existing_opt].value != foption->value)
378 if (cf_protection_option
379 && cf_protection_option->value == CF_CHECK)
380 fatal_error (input_location,
381 "option %qs with mismatching values"
382 " (%s, %s)",
383 "-fcf-protection",
384 decoded_options[existing_opt].arg,
385 foption->arg);
386 else
388 /* Merge and update the -fcf-protection option. */
389 decoded_options[existing_opt].value
390 &= (foption->value & CF_FULL);
391 switch (decoded_options[existing_opt].value)
393 case CF_NONE:
394 decoded_options[existing_opt].arg = "none";
395 break;
396 case CF_BRANCH:
397 decoded_options[existing_opt].arg = "branch";
398 break;
399 case CF_RETURN:
400 decoded_options[existing_opt].arg = "return";
401 break;
402 default:
403 gcc_unreachable ();
408 break;
410 case OPT_O:
411 case OPT_Ofast:
412 case OPT_Og:
413 case OPT_Os:
414 case OPT_Oz:
415 existing_opt = -1;
416 for (j = 0; j < decoded_options.length (); ++j)
417 if (decoded_options[j].opt_index == OPT_O
418 || decoded_options[j].opt_index == OPT_Ofast
419 || decoded_options[j].opt_index == OPT_Og
420 || decoded_options[j].opt_index == OPT_Os
421 || decoded_options[j].opt_index == OPT_Oz)
423 existing_opt = j;
424 break;
426 if (existing_opt == -1)
427 decoded_options.safe_push (*foption);
428 else if (decoded_options[existing_opt].opt_index == foption->opt_index
429 && foption->opt_index != OPT_O)
430 /* Exact same options get merged. */
432 else
434 /* For mismatched option kinds preserve the optimization
435 level only, thus merge it as -On. This also handles
436 merging of same optimization level -On. */
437 int level = 0;
438 switch (foption->opt_index)
440 case OPT_O:
441 if (foption->arg[0] == '\0')
442 level = MAX (level, 1);
443 else
444 level = MAX (level, atoi (foption->arg));
445 break;
446 case OPT_Ofast:
447 level = MAX (level, 3);
448 break;
449 case OPT_Og:
450 level = MAX (level, 1);
451 break;
452 case OPT_Os:
453 case OPT_Oz:
454 level = MAX (level, 2);
455 break;
456 default:
457 gcc_unreachable ();
459 switch (decoded_options[existing_opt].opt_index)
461 case OPT_O:
462 if (decoded_options[existing_opt].arg[0] == '\0')
463 level = MAX (level, 1);
464 else
465 level = MAX (level,
466 atoi (decoded_options[existing_opt].arg));
467 break;
468 case OPT_Ofast:
469 level = MAX (level, 3);
470 break;
471 case OPT_Og:
472 level = MAX (level, 1);
473 break;
474 case OPT_Os:
475 case OPT_Oz:
476 level = MAX (level, 2);
477 break;
478 default:
479 gcc_unreachable ();
481 decoded_options[existing_opt].opt_index = OPT_O;
482 char *tem;
483 tem = xasprintf ("-O%d", level);
484 decoded_options[existing_opt].arg = &tem[2];
485 decoded_options[existing_opt].canonical_option[0] = tem;
486 decoded_options[existing_opt].value = 1;
488 break;
491 case OPT_foffload_abi_:
492 case OPT_foffload_abi_host_opts_:
493 if (existing_opt == -1)
494 decoded_options.safe_push (*foption);
495 else if (foption->value != decoded_options[existing_opt].value)
496 fatal_error (input_location,
497 "option %s not used consistently in all LTO input"
498 " files", foption->orig_option_with_args_text);
499 break;
502 case OPT_foffload_options_:
503 decoded_options.safe_push (*foption);
504 break;
506 case OPT_flto_:
507 merge_flto_options (decoded_options, foption, false);
508 break;
512 /* Merge PIC options:
513 -fPIC + -fpic = -fpic
514 -fPIC + -fno-pic = -fno-pic
515 -fpic/-fPIC + nothing = nothing.
516 It is a common mistake to mix few -fPIC compiled objects into otherwise
517 non-PIC code. We do not want to build everything with PIC then.
519 Similarly we merge PIE options, however in addition we keep
520 -fPIC + -fPIE = -fPIE
521 -fpic + -fPIE = -fpie
522 -fPIC/-fpic + -fpie = -fpie
524 It would be good to warn on mismatches, but it is bit hard to do as
525 we do not know what nothing translates to. */
527 for (unsigned int j = 0; j < decoded_options.length ();)
528 if (decoded_options[j].opt_index == OPT_fPIC
529 || decoded_options[j].opt_index == OPT_fpic)
531 /* -fno-pic in one unit implies -fno-pic everywhere. */
532 if (decoded_options[j].value == 0)
533 j++;
534 /* If we have no pic option or merge in -fno-pic, we still may turn
535 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
536 else if ((pic_option && pic_option->value == 0)
537 || !pic_option)
539 if (pie_option)
541 bool big = decoded_options[j].opt_index == OPT_fPIC
542 && pie_option->opt_index == OPT_fPIE;
543 decoded_options[j].opt_index = big ? OPT_fPIE : OPT_fpie;
544 if (pie_option->value)
545 decoded_options[j].canonical_option[0]
546 = big ? "-fPIE" : "-fpie";
547 else
548 decoded_options[j].canonical_option[0] = "-fno-pie";
549 decoded_options[j].value = pie_option->value;
550 j++;
552 else if (pic_option)
554 decoded_options[j] = *pic_option;
555 j++;
557 /* We do not know if target defaults to pic or not, so just remove
558 option if it is missing in one unit but enabled in other. */
559 else
560 decoded_options.ordered_remove (j);
562 else if (pic_option->opt_index == OPT_fpic
563 && decoded_options[j].opt_index == OPT_fPIC)
565 decoded_options[j] = *pic_option;
566 j++;
568 else
569 j++;
571 else if (decoded_options[j].opt_index == OPT_fPIE
572 || decoded_options[j].opt_index == OPT_fpie)
574 /* -fno-pie in one unit implies -fno-pie everywhere. */
575 if (decoded_options[j].value == 0)
576 j++;
577 /* If we have no pie option or merge in -fno-pie, we still preserve
578 PIE/pie if pic/PIC is present. */
579 else if ((pie_option && pie_option->value == 0)
580 || !pie_option)
582 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
583 if (pic_option)
585 if (pic_option->opt_index == OPT_fpic
586 && decoded_options[j].opt_index == OPT_fPIE)
588 decoded_options[j].opt_index = OPT_fpie;
589 decoded_options[j].canonical_option[0]
590 = pic_option->value ? "-fpie" : "-fno-pie";
592 else if (!pic_option->value)
593 decoded_options[j].canonical_option[0] = "-fno-pie";
594 decoded_options[j].value = pic_option->value;
595 j++;
597 else if (pie_option)
599 decoded_options[j] = *pie_option;
600 j++;
602 /* Because we always append pic/PIE options this code path should
603 not happen unless the LTO object was built by old lto1 which
604 did not contain that logic yet. */
605 else
606 decoded_options.ordered_remove (j);
608 else if (pie_option->opt_index == OPT_fpie
609 && decoded_options[j].opt_index == OPT_fPIE)
611 decoded_options[j] = *pie_option;
612 j++;
614 else
615 j++;
617 else
618 j++;
620 int existing_opt_index, existing_opt2_index;
621 if (!xassembler_options_error)
622 for (existing_opt_index = existing_opt2_index = 0; ;
623 existing_opt_index++, existing_opt2_index++)
625 existing_opt_index
626 = find_option (decoded_options, OPT_Xassembler, existing_opt_index);
627 existing_opt2_index
628 = find_option (fdecoded_options, OPT_Xassembler,
629 existing_opt2_index);
631 cl_decoded_option *existing_opt = NULL;
632 cl_decoded_option *existing_opt2 = NULL;
633 if (existing_opt_index != -1)
634 existing_opt = &decoded_options[existing_opt_index];
635 if (existing_opt2_index != -1)
636 existing_opt2 = &fdecoded_options[existing_opt2_index];
638 if (existing_opt == NULL && existing_opt2 == NULL)
639 break;
640 else if (existing_opt != NULL && existing_opt2 == NULL)
642 warning (0, "Extra option to %<-Xassembler%>: %s,"
643 " dropping all %<-Xassembler%> and %<-Wa%> options.",
644 existing_opt->arg);
645 xassembler_options_error = true;
646 break;
648 else if (existing_opt == NULL && existing_opt2 != NULL)
650 warning (0, "Extra option to %<-Xassembler%>: %s,"
651 " dropping all %<-Xassembler%> and %<-Wa%> options.",
652 existing_opt2->arg);
653 xassembler_options_error = true;
654 break;
656 else if (strcmp (existing_opt->arg, existing_opt2->arg) != 0)
658 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
659 " dropping all %<-Xassembler%> and %<-Wa%> options.",
660 existing_opt->arg, existing_opt2->arg);
661 xassembler_options_error = true;
662 break;
667 /* Parse STR, saving found tokens into PVALUES and return their number.
668 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
669 append it to every token we find. */
671 static unsigned
672 parse_env_var (const char *str, char ***pvalues, const char *append)
674 const char *curval, *nextval;
675 char **values;
676 unsigned num = 1, i;
678 curval = strchr (str, ':');
679 while (curval)
681 num++;
682 curval = strchr (curval + 1, ':');
685 values = (char**) xmalloc (num * sizeof (char*));
686 curval = str;
687 nextval = strchr (curval, ':');
688 if (nextval == NULL)
689 nextval = strchr (curval, '\0');
691 int append_len = append ? strlen (append) : 0;
692 for (i = 0; i < num; i++)
694 int l = nextval - curval;
695 values[i] = (char*) xmalloc (l + 1 + append_len);
696 memcpy (values[i], curval, l);
697 values[i][l] = 0;
698 if (append)
699 strcat (values[i], append);
700 curval = nextval + 1;
701 nextval = strchr (curval, ':');
702 if (nextval == NULL)
703 nextval = strchr (curval, '\0');
705 *pvalues = values;
706 return num;
709 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
711 static void
712 append_compiler_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
714 /* Append compiler driver arguments as far as they were merged. */
715 for (unsigned int j = 1; j < opts.length (); ++j)
717 cl_decoded_option *option = &opts[j];
719 /* File options have been properly filtered by lto-opts.cc. */
720 switch (option->opt_index)
722 /* Drop arguments that we want to take from the link line. */
723 case OPT_flto_:
724 case OPT_flto:
725 case OPT_flto_partition_:
726 continue;
728 default:
729 break;
732 /* For now do what the original LTO option code was doing - pass
733 on any CL_TARGET flag and a few selected others. */
734 switch (option->opt_index)
736 case OPT_fdiagnostics_show_caret:
737 case OPT_fdiagnostics_show_event_links:
738 case OPT_fdiagnostics_show_highlight_colors:
739 case OPT_fdiagnostics_show_labels:
740 case OPT_fdiagnostics_show_line_numbers:
741 case OPT_fdiagnostics_show_option:
742 case OPT_fdiagnostics_show_location_:
743 case OPT_fshow_column:
744 case OPT_fPIC:
745 case OPT_fpic:
746 case OPT_fPIE:
747 case OPT_fpie:
748 case OPT_fcommon:
749 case OPT_fgnu_tm:
750 case OPT_fopenmp:
751 case OPT_fopenacc:
752 case OPT_fopenacc_dim_:
753 case OPT_foffload_abi_:
754 case OPT_foffload_abi_host_opts_:
755 case OPT_fcf_protection_:
756 case OPT_fasynchronous_unwind_tables:
757 case OPT_funwind_tables:
758 case OPT_g:
759 case OPT_O:
760 case OPT_Ofast:
761 case OPT_Og:
762 case OPT_Os:
763 case OPT_Oz:
764 break;
766 case OPT_Xassembler:
767 /* When we detected a mismatch in assembler options between
768 the input TU's fall back to previous behavior of ignoring them. */
769 if (xassembler_options_error)
770 continue;
771 break;
773 default:
774 if (!(cl_options[option->opt_index].flags & CL_TARGET))
775 continue;
778 /* Pass the option on. */
779 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
780 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
784 /* Append diag options in OPTS to ARGV_OBSTACK. */
786 static void
787 append_diag_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
789 /* Append compiler driver arguments as far as they were merged. */
790 for (unsigned int j = 1; j < opts.length (); ++j)
792 cl_decoded_option *option = &opts[j];
794 switch (option->opt_index)
796 case OPT_fdiagnostics_color_:
797 case OPT_fdiagnostics_format_:
798 case OPT_fdiagnostics_show_caret:
799 case OPT_fdiagnostics_show_event_links:
800 case OPT_fdiagnostics_show_highlight_colors:
801 case OPT_fdiagnostics_show_labels:
802 case OPT_fdiagnostics_show_line_numbers:
803 case OPT_fdiagnostics_show_option:
804 case OPT_fdiagnostics_show_location_:
805 case OPT_fshow_column:
806 break;
807 default:
808 continue;
811 /* Pass the option on. */
812 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
813 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
818 /* Append linker options OPTS to ARGV_OBSTACK. */
820 static void
821 append_linker_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
823 /* Append linker driver arguments. Compiler options from the linker
824 driver arguments will override / merge with those from the compiler. */
825 for (unsigned int j = 1; j < opts.length (); ++j)
827 cl_decoded_option *option = &opts[j];
829 /* Do not pass on frontend specific flags not suitable for lto. */
830 if (!(cl_options[option->opt_index].flags
831 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
832 continue;
834 switch (option->opt_index)
836 case OPT_o:
837 case OPT_flto_:
838 case OPT_flto:
839 /* We've handled these LTO options, do not pass them on. */
840 continue;
842 case OPT_fopenmp:
843 case OPT_fopenacc:
844 /* Ignore -fno-XXX form of these options, as otherwise
845 corresponding builtins will not be enabled. */
846 if (option->value == 0)
847 continue;
848 break;
850 default:
851 break;
854 /* Pass the option on. */
855 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
856 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
860 /* Extract options for TARGET offload compiler from OPTIONS and append
861 them to ARGV_OBSTACK. */
863 static void
864 append_offload_options (obstack *argv_obstack, const char *target,
865 vec<cl_decoded_option> options)
867 for (unsigned i = 0; i < options.length (); i++)
869 const char *cur, *next, *opts;
870 char **argv;
871 unsigned argc;
872 cl_decoded_option *option = &options[i];
874 if (option->opt_index != OPT_foffload_options_)
875 continue;
877 /* If option argument starts with '-' then no target is specified. That
878 means offload options are specified for all targets, so we need to
879 append them. */
880 if (option->arg[0] == '-')
881 opts = option->arg;
882 else
884 opts = strchr (option->arg, '=');
885 gcc_assert (opts);
886 cur = option->arg;
888 while (cur < opts)
890 next = strchr (cur, ',');
891 if (next == NULL)
892 next = opts;
893 next = (next > opts) ? opts : next;
895 /* Are we looking for this offload target? */
896 if (strlen (target) == (size_t) (next - cur)
897 && strncmp (target, cur, next - cur) == 0)
898 break;
900 /* Skip the comma or equal sign. */
901 cur = next + 1;
904 if (cur >= opts)
905 continue;
907 opts++;
910 argv = buildargv (opts);
911 for (argc = 0; argv[argc]; argc++)
912 obstack_ptr_grow (argv_obstack, argv[argc]);
916 /* Check whether NAME can be accessed in MODE. This is like access,
917 except that it never considers directories to be executable. */
919 static int
920 access_check (const char *name, int mode)
922 if (mode == X_OK)
924 struct stat st;
926 if (stat (name, &st) < 0
927 || S_ISDIR (st.st_mode))
928 return -1;
931 return access (name, mode);
934 /* Prepare a target image for offload TARGET, using mkoffload tool from
935 COMPILER_PATH. Return the name of the resultant object file. */
937 static const char *
938 compile_offload_image (const char *target, const char *compiler_path,
939 unsigned in_argc, char *in_argv[],
940 vec<cl_decoded_option> compiler_opts,
941 vec<cl_decoded_option> linker_opts,
942 char **filename)
944 char *dumpbase;
945 char **argv;
946 char *suffix
947 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
948 strcpy (suffix, "/accel/");
949 strcat (suffix, target);
950 strcat (suffix, "/mkoffload");
951 *filename = NULL;
953 char **paths = NULL;
954 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
956 const char *compiler = NULL;
957 for (unsigned i = 0; i < n_paths; i++)
958 if (access_check (paths[i], X_OK) == 0)
960 compiler = paths[i];
961 break;
963 #if OFFLOAD_DEFAULTED
964 if (!compiler && getenv (OFFLOAD_TARGET_DEFAULT_ENV))
966 free_array_of_ptrs ((void **) paths, n_paths);
967 return NULL;
969 #endif
971 if (!compiler)
972 fatal_error (input_location,
973 "could not find %s in %s (consider using %<-B%>)",
974 suffix + 1, compiler_path);
976 dumpbase = concat (dumppfx, "x", target, NULL);
978 /* Generate temporary output file name. */
979 if (save_temps)
980 *filename = concat (dumpbase, ".o", NULL);
981 else
982 *filename = make_temp_file (".target.o");
984 struct obstack argv_obstack;
985 obstack_init (&argv_obstack);
986 obstack_ptr_grow (&argv_obstack, compiler);
987 if (save_temps)
988 obstack_ptr_grow (&argv_obstack, "-save-temps");
989 if (verbose)
990 obstack_ptr_grow (&argv_obstack, "-v");
991 obstack_ptr_grow (&argv_obstack, "-o");
992 obstack_ptr_grow (&argv_obstack, *filename);
994 /* Append names of input object files. */
995 for (unsigned i = 0; i < in_argc; i++)
996 obstack_ptr_grow (&argv_obstack, in_argv[i]);
998 /* Append options from offload_lto sections. */
999 append_compiler_options (&argv_obstack, compiler_opts);
1000 append_diag_options (&argv_obstack, linker_opts);
1002 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1003 obstack_ptr_grow (&argv_obstack, dumpbase);
1005 /* Append options specified by -foffload last. In case of conflicting
1006 options we expect offload compiler to choose the latest. */
1007 append_offload_options (&argv_obstack, target, compiler_opts);
1008 append_offload_options (&argv_obstack, target, linker_opts);
1010 obstack_ptr_grow (&argv_obstack, NULL);
1011 argv = XOBFINISH (&argv_obstack, char **);
1012 suffix = concat (target, ".offload_args", NULL);
1013 fork_execute (argv[0], argv, true, suffix);
1014 obstack_free (&argv_obstack, NULL);
1016 free_array_of_ptrs ((void **) paths, n_paths);
1017 return *filename;
1021 /* The main routine dealing with offloading.
1022 The routine builds a target image for each offload target. IN_ARGC and
1023 IN_ARGV specify options and input object files. As all of them could contain
1024 target sections, we pass them all to target compilers. */
1026 static void
1027 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
1028 vec<cl_decoded_option> compiler_opts,
1029 vec<cl_decoded_option> linker_opts)
1031 char **names = NULL;
1032 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
1033 if (!target_names)
1034 return;
1035 unsigned num_targets = parse_env_var (target_names, &names, NULL);
1036 int next_name_entry = 0;
1038 const char *compiler_path = getenv ("COMPILER_PATH");
1039 if (!compiler_path)
1040 goto out;
1042 /* Prepare an image for each target and save the name of the resultant object
1043 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
1044 offload_names = XCNEWVEC (char *, num_targets + 1);
1045 for (unsigned i = 0; i < num_targets; i++)
1047 if (!compile_offload_image (names[i], compiler_path, in_argc, in_argv,
1048 compiler_opts, linker_opts,
1049 &offload_names[next_name_entry]))
1050 #if OFFLOAD_DEFAULTED
1051 continue;
1052 #else
1053 fatal_error (input_location,
1054 "problem with building target image for %s", names[i]);
1055 #endif
1056 next_name_entry++;
1059 #if OFFLOAD_DEFAULTED
1060 if (next_name_entry == 0)
1062 free (offload_names);
1063 offload_names = NULL;
1065 #endif
1067 out:
1068 free_array_of_ptrs ((void **) names, num_targets);
1071 /* Copy a file from SRC to DEST. */
1073 static void
1074 copy_file (const char *dest, const char *src)
1076 FILE *d = fopen (dest, "wb");
1077 FILE *s = fopen (src, "rb");
1078 char buffer[512];
1079 while (!feof (s))
1081 size_t len = fread (buffer, 1, 512, s);
1082 if (ferror (s) != 0)
1083 fatal_error (input_location, "reading input file");
1084 if (len > 0)
1086 fwrite (buffer, 1, len, d);
1087 if (ferror (d) != 0)
1088 fatal_error (input_location, "writing output file");
1091 fclose (d);
1092 fclose (s);
1095 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1096 the copy to the linker. */
1098 static void
1099 find_crtoffloadtable (int save_temps, bool pie_or_shared, const char *dumppfx)
1101 char **paths = NULL;
1102 const char *library_path = getenv ("LIBRARY_PATH");
1103 if (!library_path)
1104 return;
1105 unsigned n_paths = parse_env_var (library_path, &paths,
1106 pie_or_shared
1107 ? "/crtoffloadtableS.o"
1108 : "/crtoffloadtable.o");
1110 unsigned i;
1111 for (i = 0; i < n_paths; i++)
1112 if (access_check (paths[i], R_OK) == 0)
1114 /* The linker will delete the filename we give it, so make a copy. */
1115 char *crtoffloadtable;
1116 if (!save_temps)
1117 crtoffloadtable = make_temp_file (".crtoffloadtable.o");
1118 else
1119 crtoffloadtable = concat (dumppfx, "crtoffloadtable.o", NULL);
1120 copy_file (crtoffloadtable, paths[i]);
1121 printf ("%s\n", crtoffloadtable);
1122 XDELETEVEC (crtoffloadtable);
1123 break;
1125 if (i == n_paths)
1126 fatal_error (input_location,
1127 "installation error, cannot find %<crtoffloadtable%s.o%>",
1128 pie_or_shared ? "S" : "");
1130 free_array_of_ptrs ((void **) paths, n_paths);
1133 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1134 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
1135 Return true if we found a matching section, false
1136 otherwise. COLLECT_GCC holds the value of the environment variable with
1137 the same name. */
1139 static bool
1140 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
1141 vec<cl_decoded_option> decoded_cl_options, bool first,
1142 vec<cl_decoded_option> *opts, const char *collect_gcc)
1144 off_t offset, length;
1145 char *data;
1146 char *fopts;
1147 const char *errmsg;
1148 int err;
1149 vec<cl_decoded_option> fdecoded_options;
1151 if (!first)
1152 fdecoded_options = *opts;
1154 simple_object_read *sobj;
1155 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
1156 &errmsg, &err);
1157 if (!sobj)
1158 return false;
1160 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
1161 strcpy (secname, prefix);
1162 strcat (secname, ".opts");
1163 if (!simple_object_find_section (sobj, secname, &offset, &length,
1164 &errmsg, &err))
1166 simple_object_release_read (sobj);
1167 return false;
1170 lseek (fd, file_offset + offset, SEEK_SET);
1171 data = (char *)xmalloc (length);
1172 read (fd, data, length);
1173 fopts = data;
1176 vec<cl_decoded_option> f2decoded_options
1177 = get_options_from_collect_gcc_options (collect_gcc, fopts);
1178 if (first)
1180 fdecoded_options = f2decoded_options;
1181 first = false;
1183 else
1184 merge_and_complain (fdecoded_options, f2decoded_options,
1185 decoded_cl_options);
1187 fopts += strlen (fopts) + 1;
1189 while (fopts - data < length);
1191 free (data);
1192 simple_object_release_read (sobj);
1193 *opts = fdecoded_options;
1194 return true;
1197 /* Copy early debug info sections from INFILE to a new file whose name
1198 is returned. Return NULL on error. */
1200 const char *
1201 debug_objcopy (const char *infile, bool rename)
1203 char *outfile;
1204 const char *errmsg;
1205 int err;
1207 const char *p;
1208 const char *orig_infile = infile;
1209 off_t inoff = 0;
1210 long loffset;
1211 int consumed;
1212 if ((p = strrchr (infile, '@'))
1213 && p != infile
1214 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1215 && strlen (p) == (unsigned int) consumed)
1217 char *fname = xstrdup (infile);
1218 fname[p - infile] = '\0';
1219 infile = fname;
1220 inoff = (off_t) loffset;
1222 int infd = open (infile, O_RDONLY | O_BINARY);
1223 if (infd == -1)
1224 return NULL;
1225 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1226 "__GNU_LTO",
1227 &errmsg, &err);
1228 if (!inobj)
1229 return NULL;
1231 off_t off, len;
1232 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1233 &off, &len, &errmsg, &err) != 1)
1235 if (errmsg)
1236 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1238 simple_object_release_read (inobj);
1239 close (infd);
1240 return NULL;
1243 if (save_temps)
1244 outfile = concat (orig_infile, ".debug.temp.o", NULL);
1245 else
1246 outfile = make_temp_file (".debug.temp.o");
1247 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1248 if (errmsg)
1250 unlink_if_ordinary (outfile);
1251 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1254 simple_object_release_read (inobj);
1255 close (infd);
1257 return outfile;
1260 /* Helper for qsort: compare priorities for parallel compilation. */
1263 cmp_priority (const void *a, const void *b)
1265 return *((const int *)b)-*((const int *)a);
1268 /* Number of CPUs that can be used for parallel LTRANS phase. */
1270 static unsigned long nthreads_var = 0;
1272 #ifdef HAVE_PTHREAD_AFFINITY_NP
1273 unsigned long cpuset_size;
1274 static unsigned long get_cpuset_size;
1275 cpu_set_t *cpusetp;
1277 unsigned long
1278 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1280 #ifdef CPU_COUNT_S
1281 /* glibc 2.7 and above provide a macro for this. */
1282 return CPU_COUNT_S (cpusetsize, cpusetp);
1283 #else
1284 #ifdef CPU_COUNT
1285 if (cpusetsize == sizeof (cpu_set_t))
1286 /* glibc 2.6 and above provide a macro for this. */
1287 return CPU_COUNT (cpusetp);
1288 #endif
1289 size_t i;
1290 unsigned long ret = 0;
1291 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1292 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1294 unsigned long int mask = cpusetp->__bits[i];
1295 if (mask == 0)
1296 continue;
1297 ret += __builtin_popcountl (mask);
1299 return ret;
1300 #endif
1302 #endif
1304 /* At startup, determine the default number of threads. It would seem
1305 this should be related to the number of cpus online. */
1307 static void
1308 init_num_threads (void)
1310 #ifdef HAVE_PTHREAD_AFFINITY_NP
1311 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1312 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1313 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1314 #else
1315 cpuset_size = sizeof (cpu_set_t);
1316 #endif
1318 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1321 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1322 cpusetp);
1323 if (ret == 0)
1325 /* Count only the CPUs this process can use. */
1326 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1327 if (nthreads_var == 0)
1328 break;
1329 get_cpuset_size = cpuset_size;
1330 #ifdef CPU_ALLOC_SIZE
1331 unsigned long i;
1332 for (i = cpuset_size * 8; i; i--)
1333 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1334 break;
1335 cpuset_size = CPU_ALLOC_SIZE (i);
1336 #endif
1337 return;
1339 if (ret != EINVAL)
1340 break;
1341 #ifdef CPU_ALLOC_SIZE
1342 if (cpuset_size < sizeof (cpu_set_t))
1343 cpuset_size = sizeof (cpu_set_t);
1344 else
1345 cpuset_size = cpuset_size * 2;
1346 if (cpuset_size < 8 * sizeof (cpu_set_t))
1347 cpusetp
1348 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1349 else
1351 /* Avoid fatal if too large memory allocation would be
1352 requested, e.g. kernel returning EINVAL all the time. */
1353 void *p = realloc (cpusetp, cpuset_size);
1354 if (p == NULL)
1355 break;
1356 cpusetp = (cpu_set_t *) p;
1358 #else
1359 break;
1360 #endif
1362 while (1);
1363 cpuset_size = 0;
1364 nthreads_var = 1;
1365 free (cpusetp);
1366 cpusetp = NULL;
1367 #endif
1368 #ifdef _SC_NPROCESSORS_ONLN
1369 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1370 #endif
1373 /* Print link to -flto documentation with a hint message. */
1375 void
1376 print_lto_docs_link ()
1378 label_text url = label_text::take (global_dc->make_option_url (OPT_flto));
1379 inform (UNKNOWN_LOCATION,
1380 "see the %{%<-flto%> option documentation%} for more information",
1381 url.get ());
1384 /* Test that a make command is present and working, return true if so. */
1386 static bool
1387 make_exists (void)
1389 const char *make = "make";
1390 char **make_argv = buildargv (getenv ("MAKE"));
1391 if (make_argv)
1392 make = make_argv[0];
1393 const char *make_args[] = {make, "--version", NULL};
1395 int exit_status = 0;
1396 int err = 0;
1397 const char *errmsg
1398 = pex_one (PEX_SEARCH, make_args[0], CONST_CAST (char **, make_args),
1399 "make", NULL, NULL, &exit_status, &err);
1400 freeargv (make_argv);
1401 return errmsg == NULL && exit_status == 0 && err == 0;
1404 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1406 static void
1407 run_gcc (unsigned argc, char *argv[])
1409 unsigned i, j;
1410 const char **new_argv;
1411 const char **argv_ptr;
1412 char *list_option_full = NULL;
1413 const char *linker_output = NULL;
1414 const char *collect_gcc;
1415 char *collect_gcc_options;
1416 int parallel = 0;
1417 int jobserver = 0;
1418 bool jobserver_requested = false;
1419 int auto_parallel = 0;
1420 bool no_partition = false;
1421 bool fdecoded_options_first = true;
1422 vec<cl_decoded_option> fdecoded_options;
1423 fdecoded_options.create (16);
1424 bool offload_fdecoded_options_first = true;
1425 vec<cl_decoded_option> offload_fdecoded_options = vNULL;
1426 struct obstack argv_obstack;
1427 int new_head_argc;
1428 bool have_lto = false;
1429 bool have_offload = false;
1430 unsigned lto_argc = 0, ltoobj_argc = 0;
1431 char **lto_argv, **ltoobj_argv;
1432 bool linker_output_rel = false;
1433 bool skip_debug = false;
1434 #ifdef ENABLE_DEFAULT_PIE
1435 bool pie_or_shared = true;
1436 #else
1437 bool pie_or_shared = false;
1438 #endif
1439 const char *incoming_dumppfx = dumppfx = NULL;
1440 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1441 const char *ltrans_cache_dir = NULL;
1442 size_t ltrans_cache_size = 4096;
1444 /* Get the driver and options. */
1445 collect_gcc = getenv ("COLLECT_GCC");
1446 if (!collect_gcc)
1447 fatal_error (input_location,
1448 "environment variable %<COLLECT_GCC%> must be set");
1449 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1450 if (!collect_gcc_options)
1451 fatal_error (input_location,
1452 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1454 char *collect_as_options = getenv ("COLLECT_AS_OPTIONS");
1456 /* Prepend -Xassembler to each option, and append the string
1457 to collect_gcc_options. */
1458 if (collect_as_options)
1460 obstack temporary_obstack;
1461 obstack_init (&temporary_obstack);
1463 prepend_xassembler_to_collect_as_options (collect_as_options,
1464 &temporary_obstack);
1465 obstack_1grow (&temporary_obstack, '\0');
1467 char *xassembler_opts_string
1468 = XOBFINISH (&temporary_obstack, char *);
1469 collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string,
1470 NULL);
1473 vec<cl_decoded_option> decoded_options
1474 = get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options);
1476 /* Allocate array for input object files with LTO IL,
1477 and for possible preceding arguments. */
1478 lto_argv = XNEWVEC (char *, argc);
1479 ltoobj_argv = XNEWVEC (char *, argc);
1481 /* Look at saved options in the IL files. */
1482 for (i = 1; i < argc; ++i)
1484 char *p;
1485 int fd;
1486 off_t file_offset = 0;
1487 long loffset;
1488 int consumed;
1489 char *filename = argv[i];
1491 if (startswith (argv[i], "-foffload-objects="))
1493 have_offload = true;
1494 offload_objects_file_name
1495 = argv[i] + sizeof ("-foffload-objects=") - 1;
1496 continue;
1499 if ((p = strrchr (argv[i], '@'))
1500 && p != argv[i]
1501 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1502 && strlen (p) == (unsigned int) consumed)
1504 filename = XNEWVEC (char, p - argv[i] + 1);
1505 memcpy (filename, argv[i], p - argv[i]);
1506 filename[p - argv[i]] = '\0';
1507 file_offset = (off_t) loffset;
1509 fd = open (filename, O_RDONLY | O_BINARY);
1510 /* Linker plugin passes -fresolution and -flinker-output options.
1511 -flinker-output is passed only when user did not specify one and thus
1512 we do not need to worry about duplicities with the option handling
1513 below. */
1514 if (fd == -1)
1516 lto_argv[lto_argc++] = argv[i];
1517 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1518 linker_output_rel = true;
1519 continue;
1522 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1523 decoded_options, fdecoded_options_first,
1524 &fdecoded_options,
1525 collect_gcc))
1527 have_lto = true;
1528 ltoobj_argv[ltoobj_argc++] = argv[i];
1529 fdecoded_options_first = false;
1531 close (fd);
1534 /* Initalize the common arguments for the driver. */
1535 obstack_init (&argv_obstack);
1536 obstack_ptr_grow (&argv_obstack, collect_gcc);
1537 obstack_ptr_grow (&argv_obstack, "-xlto");
1538 obstack_ptr_grow (&argv_obstack, "-c");
1540 append_compiler_options (&argv_obstack, fdecoded_options);
1541 append_linker_options (&argv_obstack, decoded_options);
1543 /* Scan linker driver arguments for things that are of relevance to us. */
1544 for (j = 1; j < decoded_options.length (); ++j)
1546 cl_decoded_option *option = &decoded_options[j];
1547 switch (option->opt_index)
1549 case OPT_o:
1550 linker_output = option->arg;
1551 break;
1553 /* We don't have to distinguish between -save-temps=* and
1554 -save-temps, -dumpdir already carries that
1555 information. */
1556 case OPT_save_temps_:
1557 case OPT_save_temps:
1558 save_temps = 1;
1559 break;
1561 case OPT_v:
1562 verbose = 1;
1563 break;
1565 case OPT_flto_partition_:
1566 if (strcmp (option->arg, "none") == 0)
1567 no_partition = true;
1568 break;
1570 case OPT_flto_incremental_:
1571 /* Exists. */
1572 if (access (option->arg, W_OK) == 0)
1573 ltrans_cache_dir = option->arg;
1574 else
1575 fatal_error (input_location, "missing directory: %s", option->arg);
1576 break;
1578 case OPT_flto_incremental_cache_size_:
1579 ltrans_cache_size = atoi (option->arg);
1580 break;
1582 case OPT_flto_:
1583 /* Override IL file settings with a linker -flto= option. */
1584 merge_flto_options (fdecoded_options, option, true);
1585 if (strcmp (option->arg, "jobserver") == 0)
1586 jobserver_requested = true;
1587 break;
1589 case OPT_flinker_output_:
1590 linker_output_rel = !strcmp (option->arg, "rel");
1591 break;
1593 case OPT_g:
1594 /* Recognize -g0. */
1595 skip_debug = option->arg && !strcmp (option->arg, "0");
1596 break;
1598 case OPT_gbtf:
1599 case OPT_gctf:
1600 case OPT_gdwarf:
1601 case OPT_gdwarf_:
1602 case OPT_ggdb:
1603 case OPT_gvms:
1604 /* Negative forms, if allowed, enable debug info as well. */
1605 skip_debug = false;
1606 break;
1608 case OPT_dumpdir:
1609 incoming_dumppfx = dumppfx = option->arg;
1610 break;
1612 case OPT_fdiagnostics_urls_:
1613 diagnostic_urls_init (global_dc, option->value);
1614 break;
1616 case OPT_fdiagnostics_color_:
1617 diagnostic_color_init (global_dc, option->value);
1618 break;
1620 case OPT_fdiagnostics_show_highlight_colors:
1621 global_dc->set_show_highlight_colors (option->value);
1622 break;
1624 case OPT_pie:
1625 case OPT_shared:
1626 case OPT_static_pie:
1627 pie_or_shared = true;
1628 break;
1630 case OPT_no_pie:
1631 pie_or_shared = false;
1632 break;
1634 default:
1635 break;
1639 /* Process LTO-related options on merged options. */
1640 for (j = 1; j < fdecoded_options.length (); ++j)
1642 cl_decoded_option *option = &fdecoded_options[j];
1643 switch (option->opt_index)
1645 case OPT_flto_:
1646 if (strcmp (option->arg, "jobserver") == 0)
1648 parallel = 1;
1649 jobserver = 1;
1651 else if (strcmp (option->arg, "auto") == 0)
1653 parallel = 1;
1654 auto_parallel = 1;
1656 else
1658 parallel = atoi (option->arg);
1659 if (parallel <= 1)
1660 parallel = 0;
1662 /* Fallthru. */
1664 case OPT_flto:
1665 lto_mode = LTO_MODE_WHOPR;
1666 break;
1670 /* Output lto-wrapper invocation command. */
1671 if (verbose)
1673 for (i = 0; i < argc; ++i)
1675 fputs (argv[i], stderr);
1676 fputc (' ', stderr);
1678 fputc ('\n', stderr);
1681 if (linker_output_rel)
1682 no_partition = true;
1684 if (no_partition)
1686 lto_mode = LTO_MODE_LTO;
1687 jobserver = 0;
1688 jobserver_requested = false;
1689 auto_parallel = 0;
1690 parallel = 0;
1692 else
1694 jobserver_info jinfo;
1695 if (jobserver && !jinfo.is_active)
1697 /* Fall back to auto parallelism. */
1698 jobserver = 0;
1699 auto_parallel = 1;
1701 else if (!jobserver && jinfo.is_active)
1703 parallel = 1;
1704 jobserver = 1;
1708 /* We need make working for a parallel execution. */
1709 if (parallel && !make_exists ())
1710 parallel = 0;
1712 if (!dumppfx)
1714 if (!linker_output
1715 || strcmp (linker_output, HOST_BIT_BUCKET) == 0)
1716 dumppfx = "a.";
1717 else
1719 const char *obase = lbasename (linker_output), *temp;
1721 /* Strip the executable extension. */
1722 size_t blen = strlen (obase), xlen;
1723 if ((temp = strrchr (obase + 1, '.'))
1724 && (xlen = strlen (temp))
1725 && (strcmp (temp, ".exe") == 0
1726 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1727 || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
1728 #endif
1729 || strcmp (obase, "a.out") == 0))
1730 dumppfx = xstrndup (linker_output,
1731 obase - linker_output + blen - xlen + 1);
1732 else
1733 dumppfx = concat (linker_output, ".", NULL);
1737 /* If there's no directory component in the dumppfx, add one, so
1738 that, when it is used as -dumpbase, it overrides any occurrence
1739 of -dumpdir that might have been passed in. */
1740 if (!dumppfx || lbasename (dumppfx) == dumppfx)
1741 dumppfx = concat (current_dir, dumppfx, NULL);
1743 /* Make sure some -dumpdir is passed, so as to get predictable
1744 -dumpbase overriding semantics. If we got an incoming -dumpdir
1745 argument, we'll pass it on, so don't bother with another one
1746 then. */
1747 if (!incoming_dumppfx)
1749 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1750 obstack_ptr_grow (&argv_obstack, "");
1752 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1754 /* Remember at which point we can scrub args to re-use the commons. */
1755 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1757 if (have_offload)
1759 unsigned i, num_offload_files;
1760 char **offload_argv;
1761 FILE *f;
1763 f = fopen (offload_objects_file_name, "r");
1764 if (f == NULL)
1765 fatal_error (input_location, "cannot open %s: %m",
1766 offload_objects_file_name);
1767 if (fscanf (f, "%u ", &num_offload_files) != 1)
1768 fatal_error (input_location, "cannot read %s: %m",
1769 offload_objects_file_name);
1770 offload_argv = XCNEWVEC (char *, num_offload_files);
1772 /* Read names of object files with offload. */
1773 for (i = 0; i < num_offload_files; i++)
1775 const unsigned piece = 32;
1776 char *buf, *filename = XNEWVEC (char, piece);
1777 size_t len;
1779 buf = filename;
1780 cont1:
1781 if (!fgets (buf, piece, f))
1782 break;
1783 len = strlen (filename);
1784 if (filename[len - 1] != '\n')
1786 filename = XRESIZEVEC (char, filename, len + piece);
1787 buf = filename + len;
1788 goto cont1;
1790 filename[len - 1] = '\0';
1791 offload_argv[i] = filename;
1793 fclose (f);
1794 if (offload_argv[num_offload_files - 1] == NULL)
1795 fatal_error (input_location, "invalid format of %s",
1796 offload_objects_file_name);
1797 maybe_unlink (offload_objects_file_name);
1798 offload_objects_file_name = NULL;
1800 /* Look at saved offload options in files. */
1801 for (i = 0; i < num_offload_files; i++)
1803 char *p;
1804 long loffset;
1805 int fd, consumed;
1806 off_t file_offset = 0;
1807 char *filename = offload_argv[i];
1809 if ((p = strrchr (offload_argv[i], '@'))
1810 && p != offload_argv[i]
1811 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1812 && strlen (p) == (unsigned int) consumed)
1814 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1815 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1816 filename[p - offload_argv[i]] = '\0';
1817 file_offset = (off_t) loffset;
1819 fd = open (filename, O_RDONLY | O_BINARY);
1820 if (fd == -1)
1821 fatal_error (input_location, "cannot open %s: %m", filename);
1822 if (!find_and_merge_options (fd, file_offset,
1823 OFFLOAD_SECTION_NAME_PREFIX,
1824 decoded_options,
1825 offload_fdecoded_options_first,
1826 &offload_fdecoded_options,
1827 collect_gcc))
1828 fatal_error (input_location, "cannot read %s: %m", filename);
1829 offload_fdecoded_options_first = false;
1830 close (fd);
1831 if (filename != offload_argv[i])
1832 XDELETEVEC (filename);
1835 compile_images_for_offload_targets (num_offload_files, offload_argv,
1836 offload_fdecoded_options, decoded_options);
1838 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1840 if (offload_names)
1842 find_crtoffloadtable (save_temps, pie_or_shared, dumppfx);
1843 for (i = 0; offload_names[i]; i++)
1844 printf ("%s\n", offload_names[i]);
1845 free_array_of_ptrs ((void **) offload_names, i);
1846 offload_names = NULL;
1850 /* If object files contain offload sections, but do not contain LTO sections,
1851 then there is no need to perform a link-time recompilation, i.e.
1852 lto-wrapper is used only for a compilation of offload images. */
1853 if (have_offload && !have_lto)
1854 goto finish;
1856 if (lto_mode == LTO_MODE_LTO)
1858 /* -dumpbase argument for LTO. */
1859 flto_out = concat (dumppfx, "lto.o", NULL);
1860 obstack_ptr_grow (&argv_obstack, flto_out);
1862 if (!save_temps)
1863 flto_out = make_temp_file (".lto.o");
1864 obstack_ptr_grow (&argv_obstack, "-o");
1865 obstack_ptr_grow (&argv_obstack, flto_out);
1867 else
1869 const char *list_option = "-fltrans-output-list=";
1871 /* -dumpbase argument for WPA. */
1872 char *dumpbase = concat (dumppfx, "wpa", NULL);
1873 obstack_ptr_grow (&argv_obstack, dumpbase);
1875 if (ltrans_cache_dir)
1877 /* Results of wpa phase must be on the same disk partition as
1878 cache. */
1879 char* file = concat (ltrans_cache_dir, "/ccXXXXXX.ltrans.out", NULL);
1880 int fd = mkstemps (file, strlen (".ltrans.out"));
1881 gcc_assert (fd != -1 && !close (fd));
1883 ltrans_output_file = file;
1885 else if (save_temps)
1886 ltrans_output_file = concat (dumppfx, "ltrans.out", NULL);
1887 else
1888 ltrans_output_file = make_temp_file (".ltrans.out");
1889 list_option_full = concat (list_option, ltrans_output_file, NULL);
1890 obstack_ptr_grow (&argv_obstack, list_option_full);
1892 if (jobserver)
1894 if (verbose)
1895 fprintf (stderr, "Using make jobserver\n");
1896 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1898 else if (auto_parallel)
1900 char buf[256];
1901 init_num_threads ();
1902 if (nthreads_var == 0)
1903 nthreads_var = 1;
1904 if (verbose)
1905 fprintf (stderr, "LTO parallelism level set to %ld\n",
1906 nthreads_var);
1907 sprintf (buf, "-fwpa=%ld", nthreads_var);
1908 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1910 else if (parallel > 1)
1912 char buf[256];
1913 sprintf (buf, "-fwpa=%i", parallel);
1914 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1916 else
1917 obstack_ptr_grow (&argv_obstack, "-fwpa");
1920 /* Append input arguments. */
1921 for (i = 0; i < lto_argc; ++i)
1922 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1923 /* Append the input objects. */
1924 for (i = 0; i < ltoobj_argc; ++i)
1925 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1926 obstack_ptr_grow (&argv_obstack, NULL);
1928 new_argv = XOBFINISH (&argv_obstack, const char **);
1929 argv_ptr = &new_argv[new_head_argc];
1930 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
1931 "ltrans_args");
1933 /* Copy the early generated debug info from the objects to temporary
1934 files and append those to the partial link commandline. */
1935 early_debug_object_names = NULL;
1936 if (! skip_debug)
1938 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1939 num_deb_objs = ltoobj_argc;
1940 for (i = 0; i < ltoobj_argc; ++i)
1942 const char *tem;
1943 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1944 early_debug_object_names[i] = tem;
1948 if (lto_mode == LTO_MODE_LTO)
1950 printf ("%s\n", flto_out);
1951 if (!skip_debug)
1953 for (i = 0; i < ltoobj_argc; ++i)
1954 if (early_debug_object_names[i] != NULL)
1955 printf ("%s\n", early_debug_object_names[i]);
1957 /* These now belong to collect2. */
1958 free (flto_out);
1959 flto_out = NULL;
1960 free (early_debug_object_names);
1961 early_debug_object_names = NULL;
1963 else
1965 FILE *stream = fopen (ltrans_output_file, "r");
1966 FILE *mstream = NULL;
1967 struct obstack env_obstack;
1968 int priority;
1970 if (!stream)
1971 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1973 /* Parse the list of LTRANS inputs from the WPA stage. */
1974 obstack_init (&env_obstack);
1975 nr = 0;
1976 for (;;)
1978 const unsigned piece = 32;
1979 char *output_name = NULL;
1980 char *buf, *input_name = (char *)xmalloc (piece);
1981 size_t len;
1983 buf = input_name;
1984 if (fscanf (stream, "%i\n", &priority) != 1)
1986 if (!feof (stream))
1987 fatal_error (input_location,
1988 "corrupted ltrans output file %s",
1989 ltrans_output_file);
1990 break;
1992 cont:
1993 if (!fgets (buf, piece, stream))
1994 break;
1995 len = strlen (input_name);
1996 if (input_name[len - 1] != '\n')
1998 input_name = (char *)xrealloc (input_name, len + piece);
1999 buf = input_name + len;
2000 goto cont;
2002 input_name[len - 1] = '\0';
2004 if (input_name[0] == '*')
2005 output_name = &input_name[1];
2007 nr++;
2008 ltrans_priorities
2009 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
2010 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
2011 output_names = (char const**)
2012 xrealloc (output_names, nr * sizeof (char const*));
2013 ltrans_priorities[(nr-1)*2] = priority;
2014 ltrans_priorities[(nr-1)*2+1] = nr-1;
2015 input_names[nr-1] = input_name;
2016 output_names[nr-1] = output_name;
2018 fclose (stream);
2019 maybe_unlink (ltrans_output_file);
2020 ltrans_output_file = NULL;
2022 if (nr > 1)
2024 jobserver_info jinfo;
2025 if (jobserver_requested && !jinfo.is_active)
2027 warning (0, jinfo.error_msg.c_str ());
2028 print_lto_docs_link ();
2030 else if (parallel == 0)
2032 warning (0, "using serial compilation of %d LTRANS jobs", nr);
2033 print_lto_docs_link ();
2037 if (parallel)
2039 if (save_temps)
2040 makefile = concat (dumppfx, "ltrans.mk", NULL);
2041 else
2042 makefile = make_temp_file (".mk");
2043 mstream = fopen (makefile, "w");
2044 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
2047 ltrans_file_cache ltrans_cache (ltrans_cache_dir, "ltrans", ".o",
2048 ltrans_cache_size);
2050 if (ltrans_cache)
2052 if (!lockfile::lockfile_supported ())
2054 warning (0, "using ltrans cache without file locking support,"
2055 " do not use in parallel");
2057 ltrans_cache.deletion_lock.lock_read ();
2058 ltrans_cache.creation_lock.lock_write ();
2060 ltrans_cache.load_cache ();
2062 int recompiling = 0;
2064 for (i = 0; i < nr; ++i)
2066 /* If it's a pass-through file do nothing. */
2067 if (output_names[i])
2068 continue;
2070 ltrans_file_cache::item* item;
2071 bool existed = ltrans_cache.add_to_cache (input_names[i], item);
2072 free (input_names[i]);
2073 input_names[i] = xstrdup (item->input.c_str ());
2075 if (existed)
2077 /* Fill the output_name to skip compilation. */
2078 output_names[i] = item->output.c_str ();
2080 else
2082 /* Lock so no other process can access until the file is
2083 compiled. */
2084 item->lock.lock_write ();
2085 recompiling++;
2088 if (verbose)
2089 fprintf (stderr, "LTRANS: recompiling %d/%d\n", recompiling, nr);
2091 ltrans_cache.save_cache ();
2092 ltrans_cache.creation_lock.unlock ();
2095 /* Execute the LTRANS stage for each input file (or prepare a
2096 makefile to invoke this in parallel). */
2097 for (i = 0; i < nr; ++i)
2099 char const* output_name;
2100 char *input_name = input_names[i];
2101 /* If it's a pass-through or cached file do nothing. */
2102 if (output_names[i])
2103 continue;
2105 if (ltrans_cache)
2107 ltrans_file_cache::item* item;
2108 item = ltrans_cache.get_item (input_name);
2109 gcc_assert (item);
2111 output_name = item->output.c_str ();
2113 else
2115 /* Replace the .o suffix with a .ltrans.o suffix and write
2116 the resulting name to the LTRANS output list. */
2117 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
2118 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
2119 output_name = XOBFINISH (&env_obstack, char const*);
2122 /* Adjust the dumpbase if the linker output file was seen. */
2123 int dumpbase_len = (strlen (dumppfx)
2124 + sizeof (DUMPBASE_SUFFIX)
2125 + sizeof (".ltrans"));
2126 char *dumpbase = (char *) xmalloc (dumpbase_len + 1);
2127 snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i);
2128 argv_ptr[0] = dumpbase;
2130 argv_ptr[1] = "-fltrans";
2131 argv_ptr[2] = "-o";
2132 argv_ptr[3] = output_name;
2133 argv_ptr[4] = input_name;
2134 argv_ptr[5] = NULL;
2135 if (parallel)
2137 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
2138 for (j = 1; new_argv[j] != NULL; ++j)
2139 fprintf (mstream, " '%s'", new_argv[j]);
2140 /* If we are not preserving the ltrans input files then
2141 truncate them as soon as we have processed it. This
2142 reduces temporary disk-space usage. */
2143 if (!ltrans_cache && !save_temps)
2144 fprintf (mstream, " -truncate '%s'", input_name);
2145 fprintf (mstream, "\n");
2147 else
2149 char argsuffix[sizeof (DUMPBASE_SUFFIX)
2150 + sizeof (".ltrans_args") + 1];
2151 if (save_temps)
2152 snprintf (argsuffix,
2153 sizeof (DUMPBASE_SUFFIX) + sizeof (".ltrans_args"),
2154 "ltrans%u.ltrans_args", i);
2155 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
2156 true, save_temps ? argsuffix : NULL);
2157 if (!ltrans_cache)
2158 maybe_unlink (input_names[i]);
2161 output_names[i] = output_name;
2163 if (parallel)
2165 struct pex_obj *pex;
2166 char jobs[32];
2168 fprintf (mstream,
2169 ".PHONY: all\n"
2170 "all:");
2171 for (i = 0; i < nr; ++i)
2173 int j = ltrans_priorities[i*2 + 1];
2174 fprintf (mstream, " \\\n\t%s", output_names[j]);
2176 fprintf (mstream, "\n");
2177 fclose (mstream);
2178 if (!jobserver)
2180 /* Avoid passing --jobserver-fd= and similar flags
2181 unless jobserver mode is explicitly enabled. */
2182 putenv (xstrdup ("MAKEFLAGS="));
2183 putenv (xstrdup ("MFLAGS="));
2186 char **make_argv = buildargv (getenv ("MAKE"));
2187 if (make_argv)
2189 for (unsigned argc = 0; make_argv[argc]; argc++)
2190 obstack_ptr_grow (&argv_obstack, make_argv[argc]);
2192 else
2193 obstack_ptr_grow (&argv_obstack, "make");
2195 obstack_ptr_grow (&argv_obstack, "-f");
2196 obstack_ptr_grow (&argv_obstack, makefile);
2197 if (!jobserver)
2199 snprintf (jobs, 31, "-j%ld",
2200 auto_parallel ? nthreads_var : parallel);
2201 obstack_ptr_grow (&argv_obstack, jobs);
2203 obstack_ptr_grow (&argv_obstack, "all");
2204 obstack_ptr_grow (&argv_obstack, NULL);
2205 new_argv = XOBFINISH (&argv_obstack, const char **);
2207 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
2208 NULL, NULL, PEX_SEARCH, false, NULL);
2209 do_wait (new_argv[0], pex);
2210 freeargv (make_argv);
2211 maybe_unlink (makefile);
2212 makefile = NULL;
2214 if (!ltrans_cache)
2215 for (i = 0; i < nr; ++i)
2216 maybe_unlink (input_names[i]);
2219 if (ltrans_cache)
2221 for (i = 0; i < nr; ++i)
2223 ltrans_file_cache::item* item;
2224 item = ltrans_cache.get_item (input_names[i]);
2226 if (item)
2228 /* Ensure LTRANS for this item finished. */
2229 item->lock.lock_read ();
2230 item->lock.unlock ();
2234 ltrans_cache.deletion_lock.unlock ();
2237 for (i = 0; i < nr; ++i)
2239 fputs (output_names[i], stdout);
2240 putc ('\n', stdout);
2241 free (input_names[i]);
2244 if (ltrans_cache && !save_temps)
2245 ltrans_cache.try_prune ();
2247 if (!skip_debug)
2249 for (i = 0; i < ltoobj_argc; ++i)
2250 if (early_debug_object_names[i] != NULL)
2251 printf ("%s\n", early_debug_object_names[i]);
2253 nr = 0;
2254 free (ltrans_priorities);
2255 free (output_names);
2256 output_names = NULL;
2257 free (early_debug_object_names);
2258 early_debug_object_names = NULL;
2259 free (input_names);
2260 free (list_option_full);
2261 obstack_free (&env_obstack, NULL);
2264 finish:
2265 XDELETE (lto_argv);
2266 obstack_free (&argv_obstack, NULL);
2269 /* Concrete implementation of diagnostic_option_manager for LTO. */
2271 class lto_diagnostic_option_manager : public gcc_diagnostic_option_manager
2273 public:
2274 lto_diagnostic_option_manager ()
2275 : gcc_diagnostic_option_manager (0 /* lang_mask */)
2278 int option_enabled_p (diagnostic_option_id) const final override
2280 return true;
2282 char *make_option_name (diagnostic_option_id,
2283 diagnostic_t,
2284 diagnostic_t) const final override
2286 return nullptr;
2290 /* Entry point. */
2293 main (int argc, char *argv[])
2295 const char *p;
2297 init_opts_obstack ();
2299 p = argv[0] + strlen (argv[0]);
2300 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
2301 --p;
2302 progname = p;
2304 xmalloc_set_program_name (progname);
2306 gcc_init_libintl ();
2308 diagnostic_initialize (global_dc, 0);
2309 diagnostic_color_init (global_dc);
2310 diagnostic_urls_init (global_dc);
2311 global_dc->set_option_manager
2312 (::make_unique<lto_diagnostic_option_manager> (), 0);
2314 if (atexit (lto_wrapper_cleanup) != 0)
2315 fatal_error (input_location, "%<atexit%> failed");
2317 setup_signals ();
2319 /* We may be called with all the arguments stored in some file and
2320 passed with @file. Expand them into argv before processing. */
2321 expandargv (&argc, &argv);
2323 run_gcc (argc, argv);
2325 return 0;