RISC-V: Fix snafu in SI mode splitters patch
[official-gcc.git] / gcc / lto-wrapper.cc
blob6bfc96590a5aa47cb4a1695218bc04fd6409ac81
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2024 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 #include "config.h"
42 #include "system.h"
43 #include "coretypes.h"
44 #include "intl.h"
45 #include "diagnostic.h"
46 #include "obstack.h"
47 #include "opts.h"
48 #include "options.h"
49 #include "simple-object.h"
50 #include "lto-section-names.h"
51 #include "collect-utils.h"
52 #include "opts-diagnostic.h"
53 #include "opt-suggestions.h"
54 #include "opts-jobserver.h"
56 /* Environment variable, used for passing the names of offload targets from GCC
57 driver to lto-wrapper. */
58 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
59 #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT"
61 /* By default there is no special suffix for target executables. */
62 #ifdef TARGET_EXECUTABLE_SUFFIX
63 #define HAVE_TARGET_EXECUTABLE_SUFFIX
64 #else
65 #define TARGET_EXECUTABLE_SUFFIX ""
66 #endif
68 enum lto_mode_d {
69 LTO_MODE_NONE, /* Not doing LTO. */
70 LTO_MODE_LTO, /* Normal LTO. */
71 LTO_MODE_WHOPR /* WHOPR. */
74 /* Current LTO mode. */
75 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
77 static char *ltrans_output_file;
78 static char *flto_out;
79 static unsigned int nr;
80 static int *ltrans_priorities;
81 static char **input_names;
82 static char **output_names;
83 static char **offload_names;
84 static char *offload_objects_file_name;
85 static char *makefile;
86 static unsigned int num_deb_objs;
87 static const char **early_debug_object_names;
88 static bool xassembler_options_error = false;
90 const char tool_name[] = "lto-wrapper";
92 /* Auxiliary function that frees elements of PTR and PTR itself.
93 N is number of elements to be freed. If PTR is NULL, nothing is freed.
94 If an element is NULL, subsequent elements are not freed. */
96 static void **
97 free_array_of_ptrs (void **ptr, unsigned n)
99 if (!ptr)
100 return NULL;
101 for (unsigned i = 0; i < n; i++)
103 if (!ptr[i])
104 break;
105 free (ptr[i]);
107 free (ptr);
108 return NULL;
111 /* Delete tempfiles. Called from utils_cleanup. */
113 void
114 tool_cleanup (bool)
116 unsigned int i;
118 if (ltrans_output_file)
119 maybe_unlink (ltrans_output_file);
120 if (flto_out)
121 maybe_unlink (flto_out);
122 if (offload_objects_file_name)
123 maybe_unlink (offload_objects_file_name);
124 if (makefile)
125 maybe_unlink (makefile);
126 if (early_debug_object_names)
127 for (i = 0; i < num_deb_objs; ++i)
128 if (early_debug_object_names[i])
129 maybe_unlink (early_debug_object_names[i]);
130 for (i = 0; i < nr; ++i)
132 maybe_unlink (input_names[i]);
133 if (output_names[i])
134 maybe_unlink (output_names[i]);
136 if (offload_names)
138 for (i = 0; offload_names[i]; i++)
139 maybe_unlink (offload_names[i]);
140 free_array_of_ptrs ((void **) offload_names, i);
144 static void
145 lto_wrapper_cleanup (void)
147 utils_cleanup (false);
150 /* Unlink a temporary LTRANS file unless requested otherwise. */
152 void
153 maybe_unlink (const char *file)
155 if (!save_temps)
157 if (unlink_if_ordinary (file)
158 && errno != ENOENT)
159 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
161 else if (verbose)
162 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
165 /* Template of LTRANS dumpbase suffix. */
166 #define DUMPBASE_SUFFIX "ltrans18446744073709551615"
168 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
169 environment. */
171 static vec<cl_decoded_option>
172 get_options_from_collect_gcc_options (const char *collect_gcc,
173 const char *collect_gcc_options)
175 cl_decoded_option *decoded_options;
176 unsigned int decoded_options_count;
177 struct obstack argv_obstack;
178 const char **argv;
179 int argc;
181 obstack_init (&argv_obstack);
182 obstack_ptr_grow (&argv_obstack, collect_gcc);
184 parse_options_from_collect_gcc_options (collect_gcc_options,
185 &argv_obstack, &argc);
186 argv = XOBFINISH (&argv_obstack, const char **);
188 decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER,
189 &decoded_options, &decoded_options_count);
190 vec<cl_decoded_option> decoded;
191 decoded.create (decoded_options_count);
192 for (unsigned i = 0; i < decoded_options_count; ++i)
193 decoded.quick_push (decoded_options[i]);
194 free (decoded_options);
196 obstack_free (&argv_obstack, NULL);
198 return decoded;
201 /* Find option in OPTIONS based on OPT_INDEX, starting at START. -1
202 value is returned if the option is not present. */
204 static int
205 find_option (vec<cl_decoded_option> &options, size_t opt_index,
206 unsigned start = 0)
208 for (unsigned i = start; i < options.length (); ++i)
209 if (options[i].opt_index == opt_index)
210 return i;
212 return -1;
215 static int
216 find_option (vec<cl_decoded_option> &options, cl_decoded_option *option)
218 return find_option (options, option->opt_index);
221 /* Merge -flto FOPTION into vector of DECODED_OPTIONS. If FORCE is true
222 then FOPTION overrides previous settings. */
224 static void
225 merge_flto_options (vec<cl_decoded_option> &decoded_options,
226 cl_decoded_option *foption, bool force)
228 int existing_opt = find_option (decoded_options, foption);
229 if (existing_opt == -1)
230 decoded_options.safe_push (*foption);
231 else if (force)
232 decoded_options[existing_opt].arg = foption->arg;
233 else
235 if (strcmp (foption->arg, decoded_options[existing_opt].arg) != 0)
237 /* -flto=auto is preferred. */
238 if (strcmp (decoded_options[existing_opt].arg, "auto") == 0)
240 else if (strcmp (foption->arg, "auto") == 0
241 || strcmp (foption->arg, "jobserver") == 0)
242 decoded_options[existing_opt].arg = foption->arg;
243 else if (strcmp (decoded_options[existing_opt].arg,
244 "jobserver") != 0)
246 int n = atoi (foption->arg);
247 int original_n = atoi (decoded_options[existing_opt].arg);
248 if (n > original_n)
249 decoded_options[existing_opt].arg = foption->arg;
255 /* Try to merge and complain about options FDECODED_OPTIONS when applied
256 ontop of DECODED_OPTIONS. */
258 static void
259 merge_and_complain (vec<cl_decoded_option> &decoded_options,
260 vec<cl_decoded_option> fdecoded_options,
261 vec<cl_decoded_option> decoded_cl_options)
263 unsigned int i, j;
264 cl_decoded_option *pic_option = NULL;
265 cl_decoded_option *pie_option = NULL;
266 cl_decoded_option *cf_protection_option = NULL;
268 /* ??? Merge options from files. Most cases can be
269 handled by either unioning or intersecting
270 (for example -fwrapv is a case for unioning,
271 -ffast-math is for intersection). Most complaints
272 about real conflicts between different options can
273 be deferred to the compiler proper. Options that
274 we can neither safely handle by intersection nor
275 unioning would need to be complained about here.
276 Ideally we'd have a flag in the opt files that
277 tells whether to union or intersect or reject.
278 In absence of that it's unclear what a good default is.
279 It's also difficult to get positional handling correct. */
281 /* Look for a -fcf-protection option in the link-time options
282 which overrides any -fcf-protection from the lto sections. */
283 for (i = 0; i < decoded_cl_options.length (); ++i)
285 cl_decoded_option *foption = &decoded_cl_options[i];
286 if (foption->opt_index == OPT_fcf_protection_)
288 cf_protection_option = foption;
292 /* The following does what the old LTO option code did,
293 union all target and a selected set of common options. */
294 for (i = 0; i < fdecoded_options.length (); ++i)
296 cl_decoded_option *foption = &fdecoded_options[i];
297 int existing_opt = find_option (decoded_options, foption);
298 switch (foption->opt_index)
300 case OPT_SPECIAL_unknown:
301 case OPT_SPECIAL_ignore:
302 case OPT_SPECIAL_warn_removed:
303 case OPT_SPECIAL_program_name:
304 case OPT_SPECIAL_input_file:
305 break;
307 default:
308 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
309 break;
311 /* Fallthru. */
312 case OPT_fdiagnostics_show_caret:
313 case OPT_fdiagnostics_show_event_links:
314 case OPT_fdiagnostics_show_highlight_colors:
315 case OPT_fdiagnostics_show_labels:
316 case OPT_fdiagnostics_show_line_numbers:
317 case OPT_fdiagnostics_show_option:
318 case OPT_fdiagnostics_show_location_:
319 case OPT_fshow_column:
320 case OPT_fcommon:
321 case OPT_fgnu_tm:
322 case OPT_g:
323 /* Do what the old LTO code did - collect exactly one option
324 setting per OPT code, we pick the first we encounter.
325 ??? This doesn't make too much sense, but when it doesn't
326 then we should complain. */
327 if (existing_opt == -1)
328 decoded_options.safe_push (*foption);
329 break;
331 /* Figure out what PIC/PIE level wins and merge the results. */
332 case OPT_fPIC:
333 case OPT_fpic:
334 pic_option = foption;
335 break;
336 case OPT_fPIE:
337 case OPT_fpie:
338 pie_option = foption;
339 break;
341 case OPT_fopenmp:
342 case OPT_fopenacc:
343 case OPT_fasynchronous_unwind_tables:
344 case OPT_funwind_tables:
345 /* For selected options we can merge conservatively. */
346 if (existing_opt == -1)
347 decoded_options.safe_push (*foption);
348 /* -fopenmp > -fno-openmp,
349 -fopenacc > -fno-openacc */
350 else if (foption->value > decoded_options[existing_opt].value)
351 decoded_options[existing_opt] = *foption;
352 break;
354 case OPT_fopenacc_dim_:
355 /* Append or check identical. */
356 if (existing_opt == -1)
357 decoded_options.safe_push (*foption);
358 else if (strcmp (decoded_options[existing_opt].arg, foption->arg))
359 fatal_error (input_location,
360 "option %s with different values",
361 foption->orig_option_with_args_text);
362 break;
364 case OPT_fcf_protection_:
365 /* Default to link-time option, else append or check identical. */
366 if (!cf_protection_option
367 || cf_protection_option->value == CF_CHECK)
369 if (existing_opt == -1)
370 decoded_options.safe_push (*foption);
371 else if (decoded_options[existing_opt].value != foption->value)
373 if (cf_protection_option
374 && cf_protection_option->value == CF_CHECK)
375 fatal_error (input_location,
376 "option %qs with mismatching values"
377 " (%s, %s)",
378 "-fcf-protection",
379 decoded_options[existing_opt].arg,
380 foption->arg);
381 else
383 /* Merge and update the -fcf-protection option. */
384 decoded_options[existing_opt].value
385 &= (foption->value & CF_FULL);
386 switch (decoded_options[existing_opt].value)
388 case CF_NONE:
389 decoded_options[existing_opt].arg = "none";
390 break;
391 case CF_BRANCH:
392 decoded_options[existing_opt].arg = "branch";
393 break;
394 case CF_RETURN:
395 decoded_options[existing_opt].arg = "return";
396 break;
397 default:
398 gcc_unreachable ();
403 break;
405 case OPT_O:
406 case OPT_Ofast:
407 case OPT_Og:
408 case OPT_Os:
409 case OPT_Oz:
410 existing_opt = -1;
411 for (j = 0; j < decoded_options.length (); ++j)
412 if (decoded_options[j].opt_index == OPT_O
413 || decoded_options[j].opt_index == OPT_Ofast
414 || decoded_options[j].opt_index == OPT_Og
415 || decoded_options[j].opt_index == OPT_Os
416 || decoded_options[j].opt_index == OPT_Oz)
418 existing_opt = j;
419 break;
421 if (existing_opt == -1)
422 decoded_options.safe_push (*foption);
423 else if (decoded_options[existing_opt].opt_index == foption->opt_index
424 && foption->opt_index != OPT_O)
425 /* Exact same options get merged. */
427 else
429 /* For mismatched option kinds preserve the optimization
430 level only, thus merge it as -On. This also handles
431 merging of same optimization level -On. */
432 int level = 0;
433 switch (foption->opt_index)
435 case OPT_O:
436 if (foption->arg[0] == '\0')
437 level = MAX (level, 1);
438 else
439 level = MAX (level, atoi (foption->arg));
440 break;
441 case OPT_Ofast:
442 level = MAX (level, 3);
443 break;
444 case OPT_Og:
445 level = MAX (level, 1);
446 break;
447 case OPT_Os:
448 case OPT_Oz:
449 level = MAX (level, 2);
450 break;
451 default:
452 gcc_unreachable ();
454 switch (decoded_options[existing_opt].opt_index)
456 case OPT_O:
457 if (decoded_options[existing_opt].arg[0] == '\0')
458 level = MAX (level, 1);
459 else
460 level = MAX (level,
461 atoi (decoded_options[existing_opt].arg));
462 break;
463 case OPT_Ofast:
464 level = MAX (level, 3);
465 break;
466 case OPT_Og:
467 level = MAX (level, 1);
468 break;
469 case OPT_Os:
470 case OPT_Oz:
471 level = MAX (level, 2);
472 break;
473 default:
474 gcc_unreachable ();
476 decoded_options[existing_opt].opt_index = OPT_O;
477 char *tem;
478 tem = xasprintf ("-O%d", level);
479 decoded_options[existing_opt].arg = &tem[2];
480 decoded_options[existing_opt].canonical_option[0] = tem;
481 decoded_options[existing_opt].value = 1;
483 break;
486 case OPT_foffload_abi_:
487 if (existing_opt == -1)
488 decoded_options.safe_push (*foption);
489 else if (foption->value != decoded_options[existing_opt].value)
490 fatal_error (input_location,
491 "option %s not used consistently in all LTO input"
492 " files", foption->orig_option_with_args_text);
493 break;
496 case OPT_foffload_options_:
497 decoded_options.safe_push (*foption);
498 break;
500 case OPT_flto_:
501 merge_flto_options (decoded_options, foption, false);
502 break;
506 /* Merge PIC options:
507 -fPIC + -fpic = -fpic
508 -fPIC + -fno-pic = -fno-pic
509 -fpic/-fPIC + nothing = nothing.
510 It is a common mistake to mix few -fPIC compiled objects into otherwise
511 non-PIC code. We do not want to build everything with PIC then.
513 Similarly we merge PIE options, however in addition we keep
514 -fPIC + -fPIE = -fPIE
515 -fpic + -fPIE = -fpie
516 -fPIC/-fpic + -fpie = -fpie
518 It would be good to warn on mismatches, but it is bit hard to do as
519 we do not know what nothing translates to. */
521 for (unsigned int j = 0; j < decoded_options.length ();)
522 if (decoded_options[j].opt_index == OPT_fPIC
523 || decoded_options[j].opt_index == OPT_fpic)
525 /* -fno-pic in one unit implies -fno-pic everywhere. */
526 if (decoded_options[j].value == 0)
527 j++;
528 /* If we have no pic option or merge in -fno-pic, we still may turn
529 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
530 else if ((pic_option && pic_option->value == 0)
531 || !pic_option)
533 if (pie_option)
535 bool big = decoded_options[j].opt_index == OPT_fPIC
536 && pie_option->opt_index == OPT_fPIE;
537 decoded_options[j].opt_index = big ? OPT_fPIE : OPT_fpie;
538 if (pie_option->value)
539 decoded_options[j].canonical_option[0]
540 = big ? "-fPIE" : "-fpie";
541 else
542 decoded_options[j].canonical_option[0] = "-fno-pie";
543 decoded_options[j].value = pie_option->value;
544 j++;
546 else if (pic_option)
548 decoded_options[j] = *pic_option;
549 j++;
551 /* We do not know if target defaults to pic or not, so just remove
552 option if it is missing in one unit but enabled in other. */
553 else
554 decoded_options.ordered_remove (j);
556 else if (pic_option->opt_index == OPT_fpic
557 && decoded_options[j].opt_index == OPT_fPIC)
559 decoded_options[j] = *pic_option;
560 j++;
562 else
563 j++;
565 else if (decoded_options[j].opt_index == OPT_fPIE
566 || decoded_options[j].opt_index == OPT_fpie)
568 /* -fno-pie in one unit implies -fno-pie everywhere. */
569 if (decoded_options[j].value == 0)
570 j++;
571 /* If we have no pie option or merge in -fno-pie, we still preserve
572 PIE/pie if pic/PIC is present. */
573 else if ((pie_option && pie_option->value == 0)
574 || !pie_option)
576 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
577 if (pic_option)
579 if (pic_option->opt_index == OPT_fpic
580 && decoded_options[j].opt_index == OPT_fPIE)
582 decoded_options[j].opt_index = OPT_fpie;
583 decoded_options[j].canonical_option[0]
584 = pic_option->value ? "-fpie" : "-fno-pie";
586 else if (!pic_option->value)
587 decoded_options[j].canonical_option[0] = "-fno-pie";
588 decoded_options[j].value = pic_option->value;
589 j++;
591 else if (pie_option)
593 decoded_options[j] = *pie_option;
594 j++;
596 /* Because we always append pic/PIE options this code path should
597 not happen unless the LTO object was built by old lto1 which
598 did not contain that logic yet. */
599 else
600 decoded_options.ordered_remove (j);
602 else if (pie_option->opt_index == OPT_fpie
603 && decoded_options[j].opt_index == OPT_fPIE)
605 decoded_options[j] = *pie_option;
606 j++;
608 else
609 j++;
611 else
612 j++;
614 int existing_opt_index, existing_opt2_index;
615 if (!xassembler_options_error)
616 for (existing_opt_index = existing_opt2_index = 0; ;
617 existing_opt_index++, existing_opt2_index++)
619 existing_opt_index
620 = find_option (decoded_options, OPT_Xassembler, existing_opt_index);
621 existing_opt2_index
622 = find_option (fdecoded_options, OPT_Xassembler,
623 existing_opt2_index);
625 cl_decoded_option *existing_opt = NULL;
626 cl_decoded_option *existing_opt2 = NULL;
627 if (existing_opt_index != -1)
628 existing_opt = &decoded_options[existing_opt_index];
629 if (existing_opt2_index != -1)
630 existing_opt2 = &fdecoded_options[existing_opt2_index];
632 if (existing_opt == NULL && existing_opt2 == NULL)
633 break;
634 else if (existing_opt != NULL && existing_opt2 == NULL)
636 warning (0, "Extra option to %<-Xassembler%>: %s,"
637 " dropping all %<-Xassembler%> and %<-Wa%> options.",
638 existing_opt->arg);
639 xassembler_options_error = true;
640 break;
642 else if (existing_opt == NULL && existing_opt2 != NULL)
644 warning (0, "Extra option to %<-Xassembler%>: %s,"
645 " dropping all %<-Xassembler%> and %<-Wa%> options.",
646 existing_opt2->arg);
647 xassembler_options_error = true;
648 break;
650 else if (strcmp (existing_opt->arg, existing_opt2->arg) != 0)
652 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
653 " dropping all %<-Xassembler%> and %<-Wa%> options.",
654 existing_opt->arg, existing_opt2->arg);
655 xassembler_options_error = true;
656 break;
661 /* Parse STR, saving found tokens into PVALUES and return their number.
662 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
663 append it to every token we find. */
665 static unsigned
666 parse_env_var (const char *str, char ***pvalues, const char *append)
668 const char *curval, *nextval;
669 char **values;
670 unsigned num = 1, i;
672 curval = strchr (str, ':');
673 while (curval)
675 num++;
676 curval = strchr (curval + 1, ':');
679 values = (char**) xmalloc (num * sizeof (char*));
680 curval = str;
681 nextval = strchr (curval, ':');
682 if (nextval == NULL)
683 nextval = strchr (curval, '\0');
685 int append_len = append ? strlen (append) : 0;
686 for (i = 0; i < num; i++)
688 int l = nextval - curval;
689 values[i] = (char*) xmalloc (l + 1 + append_len);
690 memcpy (values[i], curval, l);
691 values[i][l] = 0;
692 if (append)
693 strcat (values[i], append);
694 curval = nextval + 1;
695 nextval = strchr (curval, ':');
696 if (nextval == NULL)
697 nextval = strchr (curval, '\0');
699 *pvalues = values;
700 return num;
703 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
705 static void
706 append_compiler_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
708 /* Append compiler driver arguments as far as they were merged. */
709 for (unsigned int j = 1; j < opts.length (); ++j)
711 cl_decoded_option *option = &opts[j];
713 /* File options have been properly filtered by lto-opts.cc. */
714 switch (option->opt_index)
716 /* Drop arguments that we want to take from the link line. */
717 case OPT_flto_:
718 case OPT_flto:
719 case OPT_flto_partition_:
720 continue;
722 default:
723 break;
726 /* For now do what the original LTO option code was doing - pass
727 on any CL_TARGET flag and a few selected others. */
728 switch (option->opt_index)
730 case OPT_fdiagnostics_show_caret:
731 case OPT_fdiagnostics_show_event_links:
732 case OPT_fdiagnostics_show_highlight_colors:
733 case OPT_fdiagnostics_show_labels:
734 case OPT_fdiagnostics_show_line_numbers:
735 case OPT_fdiagnostics_show_option:
736 case OPT_fdiagnostics_show_location_:
737 case OPT_fshow_column:
738 case OPT_fPIC:
739 case OPT_fpic:
740 case OPT_fPIE:
741 case OPT_fpie:
742 case OPT_fcommon:
743 case OPT_fgnu_tm:
744 case OPT_fopenmp:
745 case OPT_fopenacc:
746 case OPT_fopenacc_dim_:
747 case OPT_foffload_abi_:
748 case OPT_fcf_protection_:
749 case OPT_fasynchronous_unwind_tables:
750 case OPT_funwind_tables:
751 case OPT_g:
752 case OPT_O:
753 case OPT_Ofast:
754 case OPT_Og:
755 case OPT_Os:
756 case OPT_Oz:
757 break;
759 case OPT_Xassembler:
760 /* When we detected a mismatch in assembler options between
761 the input TU's fall back to previous behavior of ignoring them. */
762 if (xassembler_options_error)
763 continue;
764 break;
766 default:
767 if (!(cl_options[option->opt_index].flags & CL_TARGET))
768 continue;
771 /* Pass the option on. */
772 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
773 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
777 /* Append diag options in OPTS to ARGV_OBSTACK. */
779 static void
780 append_diag_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
782 /* Append compiler driver arguments as far as they were merged. */
783 for (unsigned int j = 1; j < opts.length (); ++j)
785 cl_decoded_option *option = &opts[j];
787 switch (option->opt_index)
789 case OPT_fdiagnostics_color_:
790 case OPT_fdiagnostics_format_:
791 case OPT_fdiagnostics_show_caret:
792 case OPT_fdiagnostics_show_event_links:
793 case OPT_fdiagnostics_show_highlight_colors:
794 case OPT_fdiagnostics_show_labels:
795 case OPT_fdiagnostics_show_line_numbers:
796 case OPT_fdiagnostics_show_option:
797 case OPT_fdiagnostics_show_location_:
798 case OPT_fshow_column:
799 break;
800 default:
801 continue;
804 /* Pass the option on. */
805 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
806 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
811 /* Append linker options OPTS to ARGV_OBSTACK. */
813 static void
814 append_linker_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
816 /* Append linker driver arguments. Compiler options from the linker
817 driver arguments will override / merge with those from the compiler. */
818 for (unsigned int j = 1; j < opts.length (); ++j)
820 cl_decoded_option *option = &opts[j];
822 /* Do not pass on frontend specific flags not suitable for lto. */
823 if (!(cl_options[option->opt_index].flags
824 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
825 continue;
827 switch (option->opt_index)
829 case OPT_o:
830 case OPT_flto_:
831 case OPT_flto:
832 /* We've handled these LTO options, do not pass them on. */
833 continue;
835 case OPT_fopenmp:
836 case OPT_fopenacc:
837 /* Ignore -fno-XXX form of these options, as otherwise
838 corresponding builtins will not be enabled. */
839 if (option->value == 0)
840 continue;
841 break;
843 default:
844 break;
847 /* Pass the option on. */
848 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
849 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
853 /* Extract options for TARGET offload compiler from OPTIONS and append
854 them to ARGV_OBSTACK. */
856 static void
857 append_offload_options (obstack *argv_obstack, const char *target,
858 vec<cl_decoded_option> options)
860 for (unsigned i = 0; i < options.length (); i++)
862 const char *cur, *next, *opts;
863 char **argv;
864 unsigned argc;
865 cl_decoded_option *option = &options[i];
867 if (option->opt_index != OPT_foffload_options_)
868 continue;
870 /* If option argument starts with '-' then no target is specified. That
871 means offload options are specified for all targets, so we need to
872 append them. */
873 if (option->arg[0] == '-')
874 opts = option->arg;
875 else
877 opts = strchr (option->arg, '=');
878 gcc_assert (opts);
879 cur = option->arg;
881 while (cur < opts)
883 next = strchr (cur, ',');
884 if (next == NULL)
885 next = opts;
886 next = (next > opts) ? opts : next;
888 /* Are we looking for this offload target? */
889 if (strlen (target) == (size_t) (next - cur)
890 && strncmp (target, cur, next - cur) == 0)
891 break;
893 /* Skip the comma or equal sign. */
894 cur = next + 1;
897 if (cur >= opts)
898 continue;
900 opts++;
903 argv = buildargv (opts);
904 for (argc = 0; argv[argc]; argc++)
905 obstack_ptr_grow (argv_obstack, argv[argc]);
909 /* Check whether NAME can be accessed in MODE. This is like access,
910 except that it never considers directories to be executable. */
912 static int
913 access_check (const char *name, int mode)
915 if (mode == X_OK)
917 struct stat st;
919 if (stat (name, &st) < 0
920 || S_ISDIR (st.st_mode))
921 return -1;
924 return access (name, mode);
927 /* Prepare a target image for offload TARGET, using mkoffload tool from
928 COMPILER_PATH. Return the name of the resultant object file. */
930 static const char *
931 compile_offload_image (const char *target, const char *compiler_path,
932 unsigned in_argc, char *in_argv[],
933 vec<cl_decoded_option> compiler_opts,
934 vec<cl_decoded_option> linker_opts,
935 char **filename)
937 char *dumpbase;
938 char **argv;
939 char *suffix
940 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
941 strcpy (suffix, "/accel/");
942 strcat (suffix, target);
943 strcat (suffix, "/mkoffload");
944 *filename = NULL;
946 char **paths = NULL;
947 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
949 const char *compiler = NULL;
950 for (unsigned i = 0; i < n_paths; i++)
951 if (access_check (paths[i], X_OK) == 0)
953 compiler = paths[i];
954 break;
956 #if OFFLOAD_DEFAULTED
957 if (!compiler && getenv (OFFLOAD_TARGET_DEFAULT_ENV))
959 free_array_of_ptrs ((void **) paths, n_paths);
960 return NULL;
962 #endif
964 if (!compiler)
965 fatal_error (input_location,
966 "could not find %s in %s (consider using %<-B%>)",
967 suffix + 1, compiler_path);
969 dumpbase = concat (dumppfx, "x", target, NULL);
971 /* Generate temporary output file name. */
972 if (save_temps)
973 *filename = concat (dumpbase, ".o", NULL);
974 else
975 *filename = make_temp_file (".target.o");
977 struct obstack argv_obstack;
978 obstack_init (&argv_obstack);
979 obstack_ptr_grow (&argv_obstack, compiler);
980 if (save_temps)
981 obstack_ptr_grow (&argv_obstack, "-save-temps");
982 if (verbose)
983 obstack_ptr_grow (&argv_obstack, "-v");
984 obstack_ptr_grow (&argv_obstack, "-o");
985 obstack_ptr_grow (&argv_obstack, *filename);
987 /* Append names of input object files. */
988 for (unsigned i = 0; i < in_argc; i++)
989 obstack_ptr_grow (&argv_obstack, in_argv[i]);
991 /* Append options from offload_lto sections. */
992 append_compiler_options (&argv_obstack, compiler_opts);
993 append_diag_options (&argv_obstack, linker_opts);
995 obstack_ptr_grow (&argv_obstack, "-dumpbase");
996 obstack_ptr_grow (&argv_obstack, dumpbase);
998 /* Append options specified by -foffload last. In case of conflicting
999 options we expect offload compiler to choose the latest. */
1000 append_offload_options (&argv_obstack, target, compiler_opts);
1001 append_offload_options (&argv_obstack, target, linker_opts);
1003 obstack_ptr_grow (&argv_obstack, NULL);
1004 argv = XOBFINISH (&argv_obstack, char **);
1005 suffix = concat (target, ".offload_args", NULL);
1006 fork_execute (argv[0], argv, true, suffix);
1007 obstack_free (&argv_obstack, NULL);
1009 free_array_of_ptrs ((void **) paths, n_paths);
1010 return *filename;
1014 /* The main routine dealing with offloading.
1015 The routine builds a target image for each offload target. IN_ARGC and
1016 IN_ARGV specify options and input object files. As all of them could contain
1017 target sections, we pass them all to target compilers. */
1019 static void
1020 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
1021 vec<cl_decoded_option> compiler_opts,
1022 vec<cl_decoded_option> linker_opts)
1024 char **names = NULL;
1025 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
1026 if (!target_names)
1027 return;
1028 unsigned num_targets = parse_env_var (target_names, &names, NULL);
1029 int next_name_entry = 0;
1031 const char *compiler_path = getenv ("COMPILER_PATH");
1032 if (!compiler_path)
1033 goto out;
1035 /* Prepare an image for each target and save the name of the resultant object
1036 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
1037 offload_names = XCNEWVEC (char *, num_targets + 1);
1038 for (unsigned i = 0; i < num_targets; i++)
1040 if (!compile_offload_image (names[i], compiler_path, in_argc, in_argv,
1041 compiler_opts, linker_opts,
1042 &offload_names[next_name_entry]))
1043 #if OFFLOAD_DEFAULTED
1044 continue;
1045 #else
1046 fatal_error (input_location,
1047 "problem with building target image for %s", names[i]);
1048 #endif
1049 next_name_entry++;
1052 #if OFFLOAD_DEFAULTED
1053 if (next_name_entry == 0)
1055 free (offload_names);
1056 offload_names = NULL;
1058 #endif
1060 out:
1061 free_array_of_ptrs ((void **) names, num_targets);
1064 /* Copy a file from SRC to DEST. */
1066 static void
1067 copy_file (const char *dest, const char *src)
1069 FILE *d = fopen (dest, "wb");
1070 FILE *s = fopen (src, "rb");
1071 char buffer[512];
1072 while (!feof (s))
1074 size_t len = fread (buffer, 1, 512, s);
1075 if (ferror (s) != 0)
1076 fatal_error (input_location, "reading input file");
1077 if (len > 0)
1079 fwrite (buffer, 1, len, d);
1080 if (ferror (d) != 0)
1081 fatal_error (input_location, "writing output file");
1084 fclose (d);
1085 fclose (s);
1088 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1089 the copy to the linker. */
1091 static void
1092 find_crtoffloadtable (int save_temps, const char *dumppfx)
1094 char **paths = NULL;
1095 const char *library_path = getenv ("LIBRARY_PATH");
1096 if (!library_path)
1097 return;
1098 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
1100 unsigned i;
1101 for (i = 0; i < n_paths; i++)
1102 if (access_check (paths[i], R_OK) == 0)
1104 /* The linker will delete the filename we give it, so make a copy. */
1105 char *crtoffloadtable;
1106 if (!save_temps)
1107 crtoffloadtable = make_temp_file (".crtoffloadtable.o");
1108 else
1109 crtoffloadtable = concat (dumppfx, "crtoffloadtable.o", NULL);
1110 copy_file (crtoffloadtable, paths[i]);
1111 printf ("%s\n", crtoffloadtable);
1112 XDELETEVEC (crtoffloadtable);
1113 break;
1115 if (i == n_paths)
1116 fatal_error (input_location,
1117 "installation error, cannot find %<crtoffloadtable.o%>");
1119 free_array_of_ptrs ((void **) paths, n_paths);
1122 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1123 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
1124 Return true if we found a matching section, false
1125 otherwise. COLLECT_GCC holds the value of the environment variable with
1126 the same name. */
1128 static bool
1129 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
1130 vec<cl_decoded_option> decoded_cl_options, bool first,
1131 vec<cl_decoded_option> *opts, const char *collect_gcc)
1133 off_t offset, length;
1134 char *data;
1135 char *fopts;
1136 const char *errmsg;
1137 int err;
1138 vec<cl_decoded_option> fdecoded_options;
1140 if (!first)
1141 fdecoded_options = *opts;
1143 simple_object_read *sobj;
1144 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
1145 &errmsg, &err);
1146 if (!sobj)
1147 return false;
1149 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
1150 strcpy (secname, prefix);
1151 strcat (secname, ".opts");
1152 if (!simple_object_find_section (sobj, secname, &offset, &length,
1153 &errmsg, &err))
1155 simple_object_release_read (sobj);
1156 return false;
1159 lseek (fd, file_offset + offset, SEEK_SET);
1160 data = (char *)xmalloc (length);
1161 read (fd, data, length);
1162 fopts = data;
1165 vec<cl_decoded_option> f2decoded_options
1166 = get_options_from_collect_gcc_options (collect_gcc, fopts);
1167 if (first)
1169 fdecoded_options = f2decoded_options;
1170 first = false;
1172 else
1173 merge_and_complain (fdecoded_options, f2decoded_options,
1174 decoded_cl_options);
1176 fopts += strlen (fopts) + 1;
1178 while (fopts - data < length);
1180 free (data);
1181 simple_object_release_read (sobj);
1182 *opts = fdecoded_options;
1183 return true;
1186 /* Copy early debug info sections from INFILE to a new file whose name
1187 is returned. Return NULL on error. */
1189 const char *
1190 debug_objcopy (const char *infile, bool rename)
1192 char *outfile;
1193 const char *errmsg;
1194 int err;
1196 const char *p;
1197 const char *orig_infile = infile;
1198 off_t inoff = 0;
1199 long loffset;
1200 int consumed;
1201 if ((p = strrchr (infile, '@'))
1202 && p != infile
1203 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1204 && strlen (p) == (unsigned int) consumed)
1206 char *fname = xstrdup (infile);
1207 fname[p - infile] = '\0';
1208 infile = fname;
1209 inoff = (off_t) loffset;
1211 int infd = open (infile, O_RDONLY | O_BINARY);
1212 if (infd == -1)
1213 return NULL;
1214 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1215 "__GNU_LTO",
1216 &errmsg, &err);
1217 if (!inobj)
1218 return NULL;
1220 off_t off, len;
1221 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1222 &off, &len, &errmsg, &err) != 1)
1224 if (errmsg)
1225 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1227 simple_object_release_read (inobj);
1228 close (infd);
1229 return NULL;
1232 if (save_temps)
1233 outfile = concat (orig_infile, ".debug.temp.o", NULL);
1234 else
1235 outfile = make_temp_file (".debug.temp.o");
1236 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1237 if (errmsg)
1239 unlink_if_ordinary (outfile);
1240 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1243 simple_object_release_read (inobj);
1244 close (infd);
1246 return outfile;
1249 /* Helper for qsort: compare priorities for parallel compilation. */
1252 cmp_priority (const void *a, const void *b)
1254 return *((const int *)b)-*((const int *)a);
1257 /* Number of CPUs that can be used for parallel LTRANS phase. */
1259 static unsigned long nthreads_var = 0;
1261 #ifdef HAVE_PTHREAD_AFFINITY_NP
1262 unsigned long cpuset_size;
1263 static unsigned long get_cpuset_size;
1264 cpu_set_t *cpusetp;
1266 unsigned long
1267 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1269 #ifdef CPU_COUNT_S
1270 /* glibc 2.7 and above provide a macro for this. */
1271 return CPU_COUNT_S (cpusetsize, cpusetp);
1272 #else
1273 #ifdef CPU_COUNT
1274 if (cpusetsize == sizeof (cpu_set_t))
1275 /* glibc 2.6 and above provide a macro for this. */
1276 return CPU_COUNT (cpusetp);
1277 #endif
1278 size_t i;
1279 unsigned long ret = 0;
1280 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1281 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1283 unsigned long int mask = cpusetp->__bits[i];
1284 if (mask == 0)
1285 continue;
1286 ret += __builtin_popcountl (mask);
1288 return ret;
1289 #endif
1291 #endif
1293 /* At startup, determine the default number of threads. It would seem
1294 this should be related to the number of cpus online. */
1296 static void
1297 init_num_threads (void)
1299 #ifdef HAVE_PTHREAD_AFFINITY_NP
1300 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1301 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1302 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1303 #else
1304 cpuset_size = sizeof (cpu_set_t);
1305 #endif
1307 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1310 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1311 cpusetp);
1312 if (ret == 0)
1314 /* Count only the CPUs this process can use. */
1315 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1316 if (nthreads_var == 0)
1317 break;
1318 get_cpuset_size = cpuset_size;
1319 #ifdef CPU_ALLOC_SIZE
1320 unsigned long i;
1321 for (i = cpuset_size * 8; i; i--)
1322 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1323 break;
1324 cpuset_size = CPU_ALLOC_SIZE (i);
1325 #endif
1326 return;
1328 if (ret != EINVAL)
1329 break;
1330 #ifdef CPU_ALLOC_SIZE
1331 if (cpuset_size < sizeof (cpu_set_t))
1332 cpuset_size = sizeof (cpu_set_t);
1333 else
1334 cpuset_size = cpuset_size * 2;
1335 if (cpuset_size < 8 * sizeof (cpu_set_t))
1336 cpusetp
1337 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1338 else
1340 /* Avoid fatal if too large memory allocation would be
1341 requested, e.g. kernel returning EINVAL all the time. */
1342 void *p = realloc (cpusetp, cpuset_size);
1343 if (p == NULL)
1344 break;
1345 cpusetp = (cpu_set_t *) p;
1347 #else
1348 break;
1349 #endif
1351 while (1);
1352 cpuset_size = 0;
1353 nthreads_var = 1;
1354 free (cpusetp);
1355 cpusetp = NULL;
1356 #endif
1357 #ifdef _SC_NPROCESSORS_ONLN
1358 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1359 #endif
1362 /* Print link to -flto documentation with a hint message. */
1364 void
1365 print_lto_docs_link ()
1367 bool print_url = global_dc->printer->supports_urls_p ();
1368 const char *url = global_dc->make_option_url (OPT_flto);
1370 pretty_printer pp;
1371 pp.set_url_format (URL_FORMAT_DEFAULT);
1372 pp_string (&pp, "see the ");
1373 if (print_url)
1374 pp_begin_url (&pp, url);
1375 pp_string (&pp, "%<-flto%> option documentation");
1376 if (print_url)
1377 pp_end_url (&pp);
1378 pp_string (&pp, " for more information");
1379 inform (UNKNOWN_LOCATION, pp_formatted_text (&pp));
1382 /* Test that a make command is present and working, return true if so. */
1384 static bool
1385 make_exists (void)
1387 const char *make = "make";
1388 char **make_argv = buildargv (getenv ("MAKE"));
1389 if (make_argv)
1390 make = make_argv[0];
1391 const char *make_args[] = {make, "--version", NULL};
1393 int exit_status = 0;
1394 int err = 0;
1395 const char *errmsg
1396 = pex_one (PEX_SEARCH, make_args[0], CONST_CAST (char **, make_args),
1397 "make", NULL, NULL, &exit_status, &err);
1398 freeargv (make_argv);
1399 return errmsg == NULL && exit_status == 0 && err == 0;
1402 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1404 static void
1405 run_gcc (unsigned argc, char *argv[])
1407 unsigned i, j;
1408 const char **new_argv;
1409 const char **argv_ptr;
1410 char *list_option_full = NULL;
1411 const char *linker_output = NULL;
1412 const char *collect_gcc;
1413 char *collect_gcc_options;
1414 int parallel = 0;
1415 int jobserver = 0;
1416 bool jobserver_requested = false;
1417 int auto_parallel = 0;
1418 bool no_partition = false;
1419 bool fdecoded_options_first = true;
1420 vec<cl_decoded_option> fdecoded_options;
1421 fdecoded_options.create (16);
1422 bool offload_fdecoded_options_first = true;
1423 vec<cl_decoded_option> offload_fdecoded_options = vNULL;
1424 struct obstack argv_obstack;
1425 int new_head_argc;
1426 bool have_lto = false;
1427 bool have_offload = false;
1428 unsigned lto_argc = 0, ltoobj_argc = 0;
1429 char **lto_argv, **ltoobj_argv;
1430 bool linker_output_rel = false;
1431 bool skip_debug = false;
1432 const char *incoming_dumppfx = dumppfx = NULL;
1433 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1435 /* Get the driver and options. */
1436 collect_gcc = getenv ("COLLECT_GCC");
1437 if (!collect_gcc)
1438 fatal_error (input_location,
1439 "environment variable %<COLLECT_GCC%> must be set");
1440 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1441 if (!collect_gcc_options)
1442 fatal_error (input_location,
1443 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1445 char *collect_as_options = getenv ("COLLECT_AS_OPTIONS");
1447 /* Prepend -Xassembler to each option, and append the string
1448 to collect_gcc_options. */
1449 if (collect_as_options)
1451 obstack temporary_obstack;
1452 obstack_init (&temporary_obstack);
1454 prepend_xassembler_to_collect_as_options (collect_as_options,
1455 &temporary_obstack);
1456 obstack_1grow (&temporary_obstack, '\0');
1458 char *xassembler_opts_string
1459 = XOBFINISH (&temporary_obstack, char *);
1460 collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string,
1461 NULL);
1464 vec<cl_decoded_option> decoded_options
1465 = get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options);
1467 /* Allocate array for input object files with LTO IL,
1468 and for possible preceding arguments. */
1469 lto_argv = XNEWVEC (char *, argc);
1470 ltoobj_argv = XNEWVEC (char *, argc);
1472 /* Look at saved options in the IL files. */
1473 for (i = 1; i < argc; ++i)
1475 char *p;
1476 int fd;
1477 off_t file_offset = 0;
1478 long loffset;
1479 int consumed;
1480 char *filename = argv[i];
1482 if (startswith (argv[i], "-foffload-objects="))
1484 have_offload = true;
1485 offload_objects_file_name
1486 = argv[i] + sizeof ("-foffload-objects=") - 1;
1487 continue;
1490 if ((p = strrchr (argv[i], '@'))
1491 && p != argv[i]
1492 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1493 && strlen (p) == (unsigned int) consumed)
1495 filename = XNEWVEC (char, p - argv[i] + 1);
1496 memcpy (filename, argv[i], p - argv[i]);
1497 filename[p - argv[i]] = '\0';
1498 file_offset = (off_t) loffset;
1500 fd = open (filename, O_RDONLY | O_BINARY);
1501 /* Linker plugin passes -fresolution and -flinker-output options.
1502 -flinker-output is passed only when user did not specify one and thus
1503 we do not need to worry about duplicities with the option handling
1504 below. */
1505 if (fd == -1)
1507 lto_argv[lto_argc++] = argv[i];
1508 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1509 linker_output_rel = true;
1510 continue;
1513 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1514 decoded_options, fdecoded_options_first,
1515 &fdecoded_options,
1516 collect_gcc))
1518 have_lto = true;
1519 ltoobj_argv[ltoobj_argc++] = argv[i];
1520 fdecoded_options_first = false;
1522 close (fd);
1525 /* Initalize the common arguments for the driver. */
1526 obstack_init (&argv_obstack);
1527 obstack_ptr_grow (&argv_obstack, collect_gcc);
1528 obstack_ptr_grow (&argv_obstack, "-xlto");
1529 obstack_ptr_grow (&argv_obstack, "-c");
1531 append_compiler_options (&argv_obstack, fdecoded_options);
1532 append_linker_options (&argv_obstack, decoded_options);
1534 /* Scan linker driver arguments for things that are of relevance to us. */
1535 for (j = 1; j < decoded_options.length (); ++j)
1537 cl_decoded_option *option = &decoded_options[j];
1538 switch (option->opt_index)
1540 case OPT_o:
1541 linker_output = option->arg;
1542 break;
1544 /* We don't have to distinguish between -save-temps=* and
1545 -save-temps, -dumpdir already carries that
1546 information. */
1547 case OPT_save_temps_:
1548 case OPT_save_temps:
1549 save_temps = 1;
1550 break;
1552 case OPT_v:
1553 verbose = 1;
1554 break;
1556 case OPT_flto_partition_:
1557 if (strcmp (option->arg, "none") == 0)
1558 no_partition = true;
1559 break;
1561 case OPT_flto_:
1562 /* Override IL file settings with a linker -flto= option. */
1563 merge_flto_options (fdecoded_options, option, true);
1564 if (strcmp (option->arg, "jobserver") == 0)
1565 jobserver_requested = true;
1566 break;
1568 case OPT_flinker_output_:
1569 linker_output_rel = !strcmp (option->arg, "rel");
1570 break;
1572 case OPT_g:
1573 /* Recognize -g0. */
1574 skip_debug = option->arg && !strcmp (option->arg, "0");
1575 break;
1577 case OPT_gbtf:
1578 case OPT_gctf:
1579 case OPT_gdwarf:
1580 case OPT_gdwarf_:
1581 case OPT_ggdb:
1582 case OPT_gvms:
1583 /* Negative forms, if allowed, enable debug info as well. */
1584 skip_debug = false;
1585 break;
1587 case OPT_dumpdir:
1588 incoming_dumppfx = dumppfx = option->arg;
1589 break;
1591 case OPT_fdiagnostics_urls_:
1592 diagnostic_urls_init (global_dc, option->value);
1593 break;
1595 case OPT_fdiagnostics_color_:
1596 diagnostic_color_init (global_dc, option->value);
1597 break;
1599 case OPT_fdiagnostics_show_highlight_colors:
1600 global_dc->set_show_highlight_colors (option->value);
1601 break;
1603 default:
1604 break;
1608 /* Process LTO-related options on merged options. */
1609 for (j = 1; j < fdecoded_options.length (); ++j)
1611 cl_decoded_option *option = &fdecoded_options[j];
1612 switch (option->opt_index)
1614 case OPT_flto_:
1615 if (strcmp (option->arg, "jobserver") == 0)
1617 parallel = 1;
1618 jobserver = 1;
1620 else if (strcmp (option->arg, "auto") == 0)
1622 parallel = 1;
1623 auto_parallel = 1;
1625 else
1627 parallel = atoi (option->arg);
1628 if (parallel <= 1)
1629 parallel = 0;
1631 /* Fallthru. */
1633 case OPT_flto:
1634 lto_mode = LTO_MODE_WHOPR;
1635 break;
1639 /* Output lto-wrapper invocation command. */
1640 if (verbose)
1642 for (i = 0; i < argc; ++i)
1644 fputs (argv[i], stderr);
1645 fputc (' ', stderr);
1647 fputc ('\n', stderr);
1650 if (linker_output_rel)
1651 no_partition = true;
1653 if (no_partition)
1655 lto_mode = LTO_MODE_LTO;
1656 jobserver = 0;
1657 jobserver_requested = false;
1658 auto_parallel = 0;
1659 parallel = 0;
1661 else
1663 jobserver_info jinfo;
1664 if (jobserver && !jinfo.is_active)
1666 /* Fall back to auto parallelism. */
1667 jobserver = 0;
1668 auto_parallel = 1;
1670 else if (!jobserver && jinfo.is_active)
1672 parallel = 1;
1673 jobserver = 1;
1677 /* We need make working for a parallel execution. */
1678 if (parallel && !make_exists ())
1679 parallel = 0;
1681 if (!dumppfx)
1683 if (!linker_output
1684 || strcmp (linker_output, HOST_BIT_BUCKET) == 0)
1685 dumppfx = "a.";
1686 else
1688 const char *obase = lbasename (linker_output), *temp;
1690 /* Strip the executable extension. */
1691 size_t blen = strlen (obase), xlen;
1692 if ((temp = strrchr (obase + 1, '.'))
1693 && (xlen = strlen (temp))
1694 && (strcmp (temp, ".exe") == 0
1695 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1696 || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
1697 #endif
1698 || strcmp (obase, "a.out") == 0))
1699 dumppfx = xstrndup (linker_output,
1700 obase - linker_output + blen - xlen + 1);
1701 else
1702 dumppfx = concat (linker_output, ".", NULL);
1706 /* If there's no directory component in the dumppfx, add one, so
1707 that, when it is used as -dumpbase, it overrides any occurrence
1708 of -dumpdir that might have been passed in. */
1709 if (!dumppfx || lbasename (dumppfx) == dumppfx)
1710 dumppfx = concat (current_dir, dumppfx, NULL);
1712 /* Make sure some -dumpdir is passed, so as to get predictable
1713 -dumpbase overriding semantics. If we got an incoming -dumpdir
1714 argument, we'll pass it on, so don't bother with another one
1715 then. */
1716 if (!incoming_dumppfx)
1718 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1719 obstack_ptr_grow (&argv_obstack, "");
1721 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1723 /* Remember at which point we can scrub args to re-use the commons. */
1724 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1726 if (have_offload)
1728 unsigned i, num_offload_files;
1729 char **offload_argv;
1730 FILE *f;
1732 f = fopen (offload_objects_file_name, "r");
1733 if (f == NULL)
1734 fatal_error (input_location, "cannot open %s: %m",
1735 offload_objects_file_name);
1736 if (fscanf (f, "%u ", &num_offload_files) != 1)
1737 fatal_error (input_location, "cannot read %s: %m",
1738 offload_objects_file_name);
1739 offload_argv = XCNEWVEC (char *, num_offload_files);
1741 /* Read names of object files with offload. */
1742 for (i = 0; i < num_offload_files; i++)
1744 const unsigned piece = 32;
1745 char *buf, *filename = XNEWVEC (char, piece);
1746 size_t len;
1748 buf = filename;
1749 cont1:
1750 if (!fgets (buf, piece, f))
1751 break;
1752 len = strlen (filename);
1753 if (filename[len - 1] != '\n')
1755 filename = XRESIZEVEC (char, filename, len + piece);
1756 buf = filename + len;
1757 goto cont1;
1759 filename[len - 1] = '\0';
1760 offload_argv[i] = filename;
1762 fclose (f);
1763 if (offload_argv[num_offload_files - 1] == NULL)
1764 fatal_error (input_location, "invalid format of %s",
1765 offload_objects_file_name);
1766 maybe_unlink (offload_objects_file_name);
1767 offload_objects_file_name = NULL;
1769 /* Look at saved offload options in files. */
1770 for (i = 0; i < num_offload_files; i++)
1772 char *p;
1773 long loffset;
1774 int fd, consumed;
1775 off_t file_offset = 0;
1776 char *filename = offload_argv[i];
1778 if ((p = strrchr (offload_argv[i], '@'))
1779 && p != offload_argv[i]
1780 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1781 && strlen (p) == (unsigned int) consumed)
1783 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1784 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1785 filename[p - offload_argv[i]] = '\0';
1786 file_offset = (off_t) loffset;
1788 fd = open (filename, O_RDONLY | O_BINARY);
1789 if (fd == -1)
1790 fatal_error (input_location, "cannot open %s: %m", filename);
1791 if (!find_and_merge_options (fd, file_offset,
1792 OFFLOAD_SECTION_NAME_PREFIX,
1793 decoded_options,
1794 offload_fdecoded_options_first,
1795 &offload_fdecoded_options,
1796 collect_gcc))
1797 fatal_error (input_location, "cannot read %s: %m", filename);
1798 offload_fdecoded_options_first = false;
1799 close (fd);
1800 if (filename != offload_argv[i])
1801 XDELETEVEC (filename);
1804 compile_images_for_offload_targets (num_offload_files, offload_argv,
1805 offload_fdecoded_options, decoded_options);
1807 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1809 if (offload_names)
1811 find_crtoffloadtable (save_temps, dumppfx);
1812 for (i = 0; offload_names[i]; i++)
1813 printf ("%s\n", offload_names[i]);
1814 free_array_of_ptrs ((void **) offload_names, i);
1815 offload_names = NULL;
1819 /* If object files contain offload sections, but do not contain LTO sections,
1820 then there is no need to perform a link-time recompilation, i.e.
1821 lto-wrapper is used only for a compilation of offload images. */
1822 if (have_offload && !have_lto)
1823 goto finish;
1825 if (lto_mode == LTO_MODE_LTO)
1827 /* -dumpbase argument for LTO. */
1828 flto_out = concat (dumppfx, "lto.o", NULL);
1829 obstack_ptr_grow (&argv_obstack, flto_out);
1831 if (!save_temps)
1832 flto_out = make_temp_file (".lto.o");
1833 obstack_ptr_grow (&argv_obstack, "-o");
1834 obstack_ptr_grow (&argv_obstack, flto_out);
1836 else
1838 const char *list_option = "-fltrans-output-list=";
1840 /* -dumpbase argument for WPA. */
1841 char *dumpbase = concat (dumppfx, "wpa", NULL);
1842 obstack_ptr_grow (&argv_obstack, dumpbase);
1844 if (save_temps)
1845 ltrans_output_file = concat (dumppfx, "ltrans.out", NULL);
1846 else
1847 ltrans_output_file = make_temp_file (".ltrans.out");
1848 list_option_full = concat (list_option, ltrans_output_file, NULL);
1849 obstack_ptr_grow (&argv_obstack, list_option_full);
1851 if (jobserver)
1853 if (verbose)
1854 fprintf (stderr, "Using make jobserver\n");
1855 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1857 else if (auto_parallel)
1859 char buf[256];
1860 init_num_threads ();
1861 if (nthreads_var == 0)
1862 nthreads_var = 1;
1863 if (verbose)
1864 fprintf (stderr, "LTO parallelism level set to %ld\n",
1865 nthreads_var);
1866 sprintf (buf, "-fwpa=%ld", nthreads_var);
1867 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1869 else if (parallel > 1)
1871 char buf[256];
1872 sprintf (buf, "-fwpa=%i", parallel);
1873 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1875 else
1876 obstack_ptr_grow (&argv_obstack, "-fwpa");
1879 /* Append input arguments. */
1880 for (i = 0; i < lto_argc; ++i)
1881 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1882 /* Append the input objects. */
1883 for (i = 0; i < ltoobj_argc; ++i)
1884 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1885 obstack_ptr_grow (&argv_obstack, NULL);
1887 new_argv = XOBFINISH (&argv_obstack, const char **);
1888 argv_ptr = &new_argv[new_head_argc];
1889 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
1890 "ltrans_args");
1892 /* Copy the early generated debug info from the objects to temporary
1893 files and append those to the partial link commandline. */
1894 early_debug_object_names = NULL;
1895 if (! skip_debug)
1897 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1898 num_deb_objs = ltoobj_argc;
1899 for (i = 0; i < ltoobj_argc; ++i)
1901 const char *tem;
1902 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1903 early_debug_object_names[i] = tem;
1907 if (lto_mode == LTO_MODE_LTO)
1909 printf ("%s\n", flto_out);
1910 if (!skip_debug)
1912 for (i = 0; i < ltoobj_argc; ++i)
1913 if (early_debug_object_names[i] != NULL)
1914 printf ("%s\n", early_debug_object_names[i]);
1916 /* These now belong to collect2. */
1917 free (flto_out);
1918 flto_out = NULL;
1919 free (early_debug_object_names);
1920 early_debug_object_names = NULL;
1922 else
1924 FILE *stream = fopen (ltrans_output_file, "r");
1925 FILE *mstream = NULL;
1926 struct obstack env_obstack;
1927 int priority;
1929 if (!stream)
1930 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1932 /* Parse the list of LTRANS inputs from the WPA stage. */
1933 obstack_init (&env_obstack);
1934 nr = 0;
1935 for (;;)
1937 const unsigned piece = 32;
1938 char *output_name = NULL;
1939 char *buf, *input_name = (char *)xmalloc (piece);
1940 size_t len;
1942 buf = input_name;
1943 if (fscanf (stream, "%i\n", &priority) != 1)
1945 if (!feof (stream))
1946 fatal_error (input_location,
1947 "corrupted ltrans output file %s",
1948 ltrans_output_file);
1949 break;
1951 cont:
1952 if (!fgets (buf, piece, stream))
1953 break;
1954 len = strlen (input_name);
1955 if (input_name[len - 1] != '\n')
1957 input_name = (char *)xrealloc (input_name, len + piece);
1958 buf = input_name + len;
1959 goto cont;
1961 input_name[len - 1] = '\0';
1963 if (input_name[0] == '*')
1964 output_name = &input_name[1];
1966 nr++;
1967 ltrans_priorities
1968 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1969 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1970 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1971 ltrans_priorities[(nr-1)*2] = priority;
1972 ltrans_priorities[(nr-1)*2+1] = nr-1;
1973 input_names[nr-1] = input_name;
1974 output_names[nr-1] = output_name;
1976 fclose (stream);
1977 maybe_unlink (ltrans_output_file);
1978 ltrans_output_file = NULL;
1980 if (nr > 1)
1982 jobserver_info jinfo;
1983 if (jobserver_requested && !jinfo.is_active)
1985 warning (0, jinfo.error_msg.c_str ());
1986 print_lto_docs_link ();
1988 else if (parallel == 0)
1990 warning (0, "using serial compilation of %d LTRANS jobs", nr);
1991 print_lto_docs_link ();
1995 if (parallel)
1997 makefile = make_temp_file (".mk");
1998 mstream = fopen (makefile, "w");
1999 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
2002 /* Execute the LTRANS stage for each input file (or prepare a
2003 makefile to invoke this in parallel). */
2004 for (i = 0; i < nr; ++i)
2006 char *output_name;
2007 char *input_name = input_names[i];
2008 /* If it's a pass-through file do nothing. */
2009 if (output_names[i])
2010 continue;
2012 /* Replace the .o suffix with a .ltrans.o suffix and write
2013 the resulting name to the LTRANS output list. */
2014 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
2015 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
2016 output_name = XOBFINISH (&env_obstack, char *);
2018 /* Adjust the dumpbase if the linker output file was seen. */
2019 int dumpbase_len = (strlen (dumppfx)
2020 + sizeof (DUMPBASE_SUFFIX)
2021 + sizeof (".ltrans"));
2022 char *dumpbase = (char *) xmalloc (dumpbase_len + 1);
2023 snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i);
2024 argv_ptr[0] = dumpbase;
2026 argv_ptr[1] = "-fltrans";
2027 argv_ptr[2] = "-o";
2028 argv_ptr[3] = output_name;
2029 argv_ptr[4] = input_name;
2030 argv_ptr[5] = NULL;
2031 if (parallel)
2033 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
2034 for (j = 1; new_argv[j] != NULL; ++j)
2035 fprintf (mstream, " '%s'", new_argv[j]);
2036 /* If we are not preserving the ltrans input files then
2037 truncate them as soon as we have processed it. This
2038 reduces temporary disk-space usage. */
2039 if (! save_temps)
2040 fprintf (mstream, " -truncate '%s'", input_name);
2041 fprintf (mstream, "\n");
2043 else
2045 char argsuffix[sizeof (DUMPBASE_SUFFIX)
2046 + sizeof (".ltrans_args") + 1];
2047 if (save_temps)
2048 snprintf (argsuffix,
2049 sizeof (DUMPBASE_SUFFIX) + sizeof (".ltrans_args"),
2050 "ltrans%u.ltrans_args", i);
2051 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
2052 true, save_temps ? argsuffix : NULL);
2053 maybe_unlink (input_name);
2056 output_names[i] = output_name;
2058 if (parallel)
2060 struct pex_obj *pex;
2061 char jobs[32];
2063 fprintf (mstream,
2064 ".PHONY: all\n"
2065 "all:");
2066 for (i = 0; i < nr; ++i)
2068 int j = ltrans_priorities[i*2 + 1];
2069 fprintf (mstream, " \\\n\t%s", output_names[j]);
2071 fprintf (mstream, "\n");
2072 fclose (mstream);
2073 if (!jobserver)
2075 /* Avoid passing --jobserver-fd= and similar flags
2076 unless jobserver mode is explicitly enabled. */
2077 putenv (xstrdup ("MAKEFLAGS="));
2078 putenv (xstrdup ("MFLAGS="));
2081 char **make_argv = buildargv (getenv ("MAKE"));
2082 if (make_argv)
2084 for (unsigned argc = 0; make_argv[argc]; argc++)
2085 obstack_ptr_grow (&argv_obstack, make_argv[argc]);
2087 else
2088 obstack_ptr_grow (&argv_obstack, "make");
2090 obstack_ptr_grow (&argv_obstack, "-f");
2091 obstack_ptr_grow (&argv_obstack, makefile);
2092 if (!jobserver)
2094 snprintf (jobs, 31, "-j%ld",
2095 auto_parallel ? nthreads_var : parallel);
2096 obstack_ptr_grow (&argv_obstack, jobs);
2098 obstack_ptr_grow (&argv_obstack, "all");
2099 obstack_ptr_grow (&argv_obstack, NULL);
2100 new_argv = XOBFINISH (&argv_obstack, const char **);
2102 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
2103 NULL, NULL, PEX_SEARCH, false, NULL);
2104 do_wait (new_argv[0], pex);
2105 freeargv (make_argv);
2106 maybe_unlink (makefile);
2107 makefile = NULL;
2108 for (i = 0; i < nr; ++i)
2109 maybe_unlink (input_names[i]);
2111 for (i = 0; i < nr; ++i)
2113 fputs (output_names[i], stdout);
2114 putc ('\n', stdout);
2115 free (input_names[i]);
2117 if (!skip_debug)
2119 for (i = 0; i < ltoobj_argc; ++i)
2120 if (early_debug_object_names[i] != NULL)
2121 printf ("%s\n", early_debug_object_names[i]);
2123 nr = 0;
2124 free (ltrans_priorities);
2125 free (output_names);
2126 output_names = NULL;
2127 free (early_debug_object_names);
2128 early_debug_object_names = NULL;
2129 free (input_names);
2130 free (list_option_full);
2131 obstack_free (&env_obstack, NULL);
2134 finish:
2135 XDELETE (lto_argv);
2136 obstack_free (&argv_obstack, NULL);
2140 /* Entry point. */
2143 main (int argc, char *argv[])
2145 const char *p;
2147 init_opts_obstack ();
2149 p = argv[0] + strlen (argv[0]);
2150 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
2151 --p;
2152 progname = p;
2154 xmalloc_set_program_name (progname);
2156 gcc_init_libintl ();
2158 diagnostic_initialize (global_dc, 0);
2159 diagnostic_color_init (global_dc);
2160 diagnostic_urls_init (global_dc);
2161 global_dc->set_option_hooks (nullptr,
2162 nullptr,
2163 nullptr,
2164 get_option_url,
2167 if (atexit (lto_wrapper_cleanup) != 0)
2168 fatal_error (input_location, "%<atexit%> failed");
2170 setup_signals ();
2172 /* We may be called with all the arguments stored in some file and
2173 passed with @file. Expand them into argv before processing. */
2174 expandargv (&argc, &argv);
2176 run_gcc (argc, argv);
2178 return 0;