Suppress -fstack-protector warning on hppa.
[official-gcc.git] / gcc / lto-wrapper.cc
blobb12bcc1ad273249f7f67dda122389cfea5d1e8df
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2022 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. */
223 static void
224 merge_flto_options (vec<cl_decoded_option> &decoded_options,
225 cl_decoded_option *foption)
227 int existing_opt = find_option (decoded_options, foption);
228 if (existing_opt == -1)
229 decoded_options.safe_push (*foption);
230 else
232 if (strcmp (foption->arg, decoded_options[existing_opt].arg) != 0)
234 /* -flto=auto is preferred. */
235 if (strcmp (decoded_options[existing_opt].arg, "auto") == 0)
237 else if (strcmp (foption->arg, "auto") == 0
238 || strcmp (foption->arg, "jobserver") == 0)
239 decoded_options[existing_opt].arg = foption->arg;
240 else if (strcmp (decoded_options[existing_opt].arg,
241 "jobserver") != 0)
243 int n = atoi (foption->arg);
244 int original_n = atoi (decoded_options[existing_opt].arg);
245 if (n > original_n)
246 decoded_options[existing_opt].arg = foption->arg;
252 /* Try to merge and complain about options FDECODED_OPTIONS when applied
253 ontop of DECODED_OPTIONS. */
255 static void
256 merge_and_complain (vec<cl_decoded_option> &decoded_options,
257 vec<cl_decoded_option> fdecoded_options,
258 vec<cl_decoded_option> decoded_cl_options)
260 unsigned int i, j;
261 cl_decoded_option *pic_option = NULL;
262 cl_decoded_option *pie_option = NULL;
263 cl_decoded_option *cf_protection_option = NULL;
265 /* ??? Merge options from files. Most cases can be
266 handled by either unioning or intersecting
267 (for example -fwrapv is a case for unioning,
268 -ffast-math is for intersection). Most complaints
269 about real conflicts between different options can
270 be deferred to the compiler proper. Options that
271 we can neither safely handle by intersection nor
272 unioning would need to be complained about here.
273 Ideally we'd have a flag in the opt files that
274 tells whether to union or intersect or reject.
275 In absence of that it's unclear what a good default is.
276 It's also difficult to get positional handling correct. */
278 /* Look for a -fcf-protection option in the link-time options
279 which overrides any -fcf-protection from the lto sections. */
280 for (i = 0; i < decoded_cl_options.length (); ++i)
282 cl_decoded_option *foption = &decoded_cl_options[i];
283 if (foption->opt_index == OPT_fcf_protection_)
285 cf_protection_option = foption;
289 /* The following does what the old LTO option code did,
290 union all target and a selected set of common options. */
291 for (i = 0; i < fdecoded_options.length (); ++i)
293 cl_decoded_option *foption = &fdecoded_options[i];
294 int existing_opt = find_option (decoded_options, foption);
295 switch (foption->opt_index)
297 case OPT_SPECIAL_unknown:
298 case OPT_SPECIAL_ignore:
299 case OPT_SPECIAL_warn_removed:
300 case OPT_SPECIAL_program_name:
301 case OPT_SPECIAL_input_file:
302 break;
304 default:
305 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
306 break;
308 /* Fallthru. */
309 case OPT_fdiagnostics_show_caret:
310 case OPT_fdiagnostics_show_labels:
311 case OPT_fdiagnostics_show_line_numbers:
312 case OPT_fdiagnostics_show_option:
313 case OPT_fdiagnostics_show_location_:
314 case OPT_fshow_column:
315 case OPT_fcommon:
316 case OPT_fgnu_tm:
317 case OPT_g:
318 /* Do what the old LTO code did - collect exactly one option
319 setting per OPT code, we pick the first we encounter.
320 ??? This doesn't make too much sense, but when it doesn't
321 then we should complain. */
322 if (existing_opt == -1)
323 decoded_options.safe_push (*foption);
324 break;
326 /* Figure out what PIC/PIE level wins and merge the results. */
327 case OPT_fPIC:
328 case OPT_fpic:
329 pic_option = foption;
330 break;
331 case OPT_fPIE:
332 case OPT_fpie:
333 pie_option = foption;
334 break;
336 case OPT_fopenmp:
337 case OPT_fopenacc:
338 /* For selected options we can merge conservatively. */
339 if (existing_opt == -1)
340 decoded_options.safe_push (*foption);
341 /* -fopenmp > -fno-openmp,
342 -fopenacc > -fno-openacc */
343 else if (foption->value > decoded_options[existing_opt].value)
344 decoded_options[existing_opt] = *foption;
345 break;
347 case OPT_fopenacc_dim_:
348 /* Append or check identical. */
349 if (existing_opt == -1)
350 decoded_options.safe_push (*foption);
351 else if (strcmp (decoded_options[existing_opt].arg, foption->arg))
352 fatal_error (input_location,
353 "option %s with different values",
354 foption->orig_option_with_args_text);
355 break;
357 case OPT_fcf_protection_:
358 /* Default to link-time option, else append or check identical. */
359 if (!cf_protection_option
360 || cf_protection_option->value == CF_CHECK)
362 if (existing_opt == -1)
363 decoded_options.safe_push (*foption);
364 else if (decoded_options[existing_opt].value != foption->value)
366 if (cf_protection_option
367 && cf_protection_option->value == CF_CHECK)
368 fatal_error (input_location,
369 "option %qs with mismatching values"
370 " (%s, %s)",
371 "-fcf-protection",
372 decoded_options[existing_opt].arg,
373 foption->arg);
374 else
376 /* Merge and update the -fcf-protection option. */
377 decoded_options[existing_opt].value
378 &= (foption->value & CF_FULL);
379 switch (decoded_options[existing_opt].value)
381 case CF_NONE:
382 decoded_options[existing_opt].arg = "none";
383 break;
384 case CF_BRANCH:
385 decoded_options[existing_opt].arg = "branch";
386 break;
387 case CF_RETURN:
388 decoded_options[existing_opt].arg = "return";
389 break;
390 default:
391 gcc_unreachable ();
396 break;
398 case OPT_O:
399 case OPT_Ofast:
400 case OPT_Og:
401 case OPT_Os:
402 case OPT_Oz:
403 existing_opt = -1;
404 for (j = 0; j < decoded_options.length (); ++j)
405 if (decoded_options[j].opt_index == OPT_O
406 || decoded_options[j].opt_index == OPT_Ofast
407 || decoded_options[j].opt_index == OPT_Og
408 || decoded_options[j].opt_index == OPT_Os
409 || decoded_options[j].opt_index == OPT_Oz)
411 existing_opt = j;
412 break;
414 if (existing_opt == -1)
415 decoded_options.safe_push (*foption);
416 else if (decoded_options[existing_opt].opt_index == foption->opt_index
417 && foption->opt_index != OPT_O)
418 /* Exact same options get merged. */
420 else
422 /* For mismatched option kinds preserve the optimization
423 level only, thus merge it as -On. This also handles
424 merging of same optimization level -On. */
425 int level = 0;
426 switch (foption->opt_index)
428 case OPT_O:
429 if (foption->arg[0] == '\0')
430 level = MAX (level, 1);
431 else
432 level = MAX (level, atoi (foption->arg));
433 break;
434 case OPT_Ofast:
435 level = MAX (level, 3);
436 break;
437 case OPT_Og:
438 level = MAX (level, 1);
439 break;
440 case OPT_Os:
441 case OPT_Oz:
442 level = MAX (level, 2);
443 break;
444 default:
445 gcc_unreachable ();
447 switch (decoded_options[existing_opt].opt_index)
449 case OPT_O:
450 if (decoded_options[existing_opt].arg[0] == '\0')
451 level = MAX (level, 1);
452 else
453 level = MAX (level,
454 atoi (decoded_options[existing_opt].arg));
455 break;
456 case OPT_Ofast:
457 level = MAX (level, 3);
458 break;
459 case OPT_Og:
460 level = MAX (level, 1);
461 break;
462 case OPT_Os:
463 case OPT_Oz:
464 level = MAX (level, 2);
465 break;
466 default:
467 gcc_unreachable ();
469 decoded_options[existing_opt].opt_index = OPT_O;
470 char *tem;
471 tem = xasprintf ("-O%d", level);
472 decoded_options[existing_opt].arg = &tem[2];
473 decoded_options[existing_opt].canonical_option[0] = tem;
474 decoded_options[existing_opt].value = 1;
476 break;
479 case OPT_foffload_abi_:
480 if (existing_opt == -1)
481 decoded_options.safe_push (*foption);
482 else if (foption->value != decoded_options[existing_opt].value)
483 fatal_error (input_location,
484 "option %s not used consistently in all LTO input"
485 " files", foption->orig_option_with_args_text);
486 break;
489 case OPT_foffload_options_:
490 decoded_options.safe_push (*foption);
491 break;
493 case OPT_flto_:
494 merge_flto_options (decoded_options, foption);
495 break;
499 /* Merge PIC options:
500 -fPIC + -fpic = -fpic
501 -fPIC + -fno-pic = -fno-pic
502 -fpic/-fPIC + nothing = nothing.
503 It is a common mistake to mix few -fPIC compiled objects into otherwise
504 non-PIC code. We do not want to build everything with PIC then.
506 Similarly we merge PIE options, however in addition we keep
507 -fPIC + -fPIE = -fPIE
508 -fpic + -fPIE = -fpie
509 -fPIC/-fpic + -fpie = -fpie
511 It would be good to warn on mismatches, but it is bit hard to do as
512 we do not know what nothing translates to. */
514 for (unsigned int j = 0; j < decoded_options.length ();)
515 if (decoded_options[j].opt_index == OPT_fPIC
516 || decoded_options[j].opt_index == OPT_fpic)
518 /* -fno-pic in one unit implies -fno-pic everywhere. */
519 if (decoded_options[j].value == 0)
520 j++;
521 /* If we have no pic option or merge in -fno-pic, we still may turn
522 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
523 else if ((pic_option && pic_option->value == 0)
524 || !pic_option)
526 if (pie_option)
528 bool big = decoded_options[j].opt_index == OPT_fPIC
529 && pie_option->opt_index == OPT_fPIE;
530 decoded_options[j].opt_index = big ? OPT_fPIE : OPT_fpie;
531 if (pie_option->value)
532 decoded_options[j].canonical_option[0]
533 = big ? "-fPIE" : "-fpie";
534 else
535 decoded_options[j].canonical_option[0] = "-fno-pie";
536 decoded_options[j].value = pie_option->value;
537 j++;
539 else if (pic_option)
541 decoded_options[j] = *pic_option;
542 j++;
544 /* We do not know if target defaults to pic or not, so just remove
545 option if it is missing in one unit but enabled in other. */
546 else
547 decoded_options.ordered_remove (j);
549 else if (pic_option->opt_index == OPT_fpic
550 && decoded_options[j].opt_index == OPT_fPIC)
552 decoded_options[j] = *pic_option;
553 j++;
555 else
556 j++;
558 else if (decoded_options[j].opt_index == OPT_fPIE
559 || decoded_options[j].opt_index == OPT_fpie)
561 /* -fno-pie in one unit implies -fno-pie everywhere. */
562 if (decoded_options[j].value == 0)
563 j++;
564 /* If we have no pie option or merge in -fno-pie, we still preserve
565 PIE/pie if pic/PIC is present. */
566 else if ((pie_option && pie_option->value == 0)
567 || !pie_option)
569 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
570 if (pic_option)
572 if (pic_option->opt_index == OPT_fpic
573 && decoded_options[j].opt_index == OPT_fPIE)
575 decoded_options[j].opt_index = OPT_fpie;
576 decoded_options[j].canonical_option[0]
577 = pic_option->value ? "-fpie" : "-fno-pie";
579 else if (!pic_option->value)
580 decoded_options[j].canonical_option[0] = "-fno-pie";
581 decoded_options[j].value = pic_option->value;
582 j++;
584 else if (pie_option)
586 decoded_options[j] = *pie_option;
587 j++;
589 /* Because we always append pic/PIE options this code path should
590 not happen unless the LTO object was built by old lto1 which
591 did not contain that logic yet. */
592 else
593 decoded_options.ordered_remove (j);
595 else if (pie_option->opt_index == OPT_fpie
596 && decoded_options[j].opt_index == OPT_fPIE)
598 decoded_options[j] = *pie_option;
599 j++;
601 else
602 j++;
604 else
605 j++;
607 int existing_opt_index, existing_opt2_index;
608 if (!xassembler_options_error)
609 for (existing_opt_index = existing_opt2_index = 0; ;
610 existing_opt_index++, existing_opt2_index++)
612 existing_opt_index
613 = find_option (decoded_options, OPT_Xassembler, existing_opt_index);
614 existing_opt2_index
615 = find_option (fdecoded_options, OPT_Xassembler,
616 existing_opt2_index);
618 cl_decoded_option *existing_opt = NULL;
619 cl_decoded_option *existing_opt2 = NULL;
620 if (existing_opt_index != -1)
621 existing_opt = &decoded_options[existing_opt_index];
622 if (existing_opt2_index != -1)
623 existing_opt2 = &fdecoded_options[existing_opt2_index];
625 if (existing_opt == NULL && existing_opt2 == NULL)
626 break;
627 else if (existing_opt != NULL && existing_opt2 == NULL)
629 warning (0, "Extra option to %<-Xassembler%>: %s,"
630 " dropping all %<-Xassembler%> and %<-Wa%> options.",
631 existing_opt->arg);
632 xassembler_options_error = true;
633 break;
635 else if (existing_opt == NULL && existing_opt2 != NULL)
637 warning (0, "Extra option to %<-Xassembler%>: %s,"
638 " dropping all %<-Xassembler%> and %<-Wa%> options.",
639 existing_opt2->arg);
640 xassembler_options_error = true;
641 break;
643 else if (strcmp (existing_opt->arg, existing_opt2->arg) != 0)
645 warning (0, "Options to %<-Xassembler%> do not match: %s, %s,"
646 " dropping all %<-Xassembler%> and %<-Wa%> options.",
647 existing_opt->arg, existing_opt2->arg);
648 xassembler_options_error = true;
649 break;
654 /* Parse STR, saving found tokens into PVALUES and return their number.
655 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
656 append it to every token we find. */
658 static unsigned
659 parse_env_var (const char *str, char ***pvalues, const char *append)
661 const char *curval, *nextval;
662 char **values;
663 unsigned num = 1, i;
665 curval = strchr (str, ':');
666 while (curval)
668 num++;
669 curval = strchr (curval + 1, ':');
672 values = (char**) xmalloc (num * sizeof (char*));
673 curval = str;
674 nextval = strchr (curval, ':');
675 if (nextval == NULL)
676 nextval = strchr (curval, '\0');
678 int append_len = append ? strlen (append) : 0;
679 for (i = 0; i < num; i++)
681 int l = nextval - curval;
682 values[i] = (char*) xmalloc (l + 1 + append_len);
683 memcpy (values[i], curval, l);
684 values[i][l] = 0;
685 if (append)
686 strcat (values[i], append);
687 curval = nextval + 1;
688 nextval = strchr (curval, ':');
689 if (nextval == NULL)
690 nextval = strchr (curval, '\0');
692 *pvalues = values;
693 return num;
696 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
698 static void
699 append_compiler_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
701 /* Append compiler driver arguments as far as they were merged. */
702 for (unsigned int j = 1; j < opts.length (); ++j)
704 cl_decoded_option *option = &opts[j];
706 /* File options have been properly filtered by lto-opts.cc. */
707 switch (option->opt_index)
709 /* Drop arguments that we want to take from the link line. */
710 case OPT_flto_:
711 case OPT_flto:
712 case OPT_flto_partition_:
713 continue;
715 default:
716 break;
719 /* For now do what the original LTO option code was doing - pass
720 on any CL_TARGET flag and a few selected others. */
721 switch (option->opt_index)
723 case OPT_fdiagnostics_show_caret:
724 case OPT_fdiagnostics_show_labels:
725 case OPT_fdiagnostics_show_line_numbers:
726 case OPT_fdiagnostics_show_option:
727 case OPT_fdiagnostics_show_location_:
728 case OPT_fshow_column:
729 case OPT_fPIC:
730 case OPT_fpic:
731 case OPT_fPIE:
732 case OPT_fpie:
733 case OPT_fcommon:
734 case OPT_fgnu_tm:
735 case OPT_fopenmp:
736 case OPT_fopenacc:
737 case OPT_fopenacc_dim_:
738 case OPT_foffload_abi_:
739 case OPT_fcf_protection_:
740 case OPT_g:
741 case OPT_O:
742 case OPT_Ofast:
743 case OPT_Og:
744 case OPT_Os:
745 case OPT_Oz:
746 break;
748 case OPT_Xassembler:
749 /* When we detected a mismatch in assembler options between
750 the input TU's fall back to previous behavior of ignoring them. */
751 if (xassembler_options_error)
752 continue;
753 break;
755 default:
756 if (!(cl_options[option->opt_index].flags & CL_TARGET))
757 continue;
760 /* Pass the option on. */
761 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
762 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
766 /* Append diag options in OPTS to ARGV_OBSTACK. */
768 static void
769 append_diag_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
771 /* Append compiler driver arguments as far as they were merged. */
772 for (unsigned int j = 1; j < opts.length (); ++j)
774 cl_decoded_option *option = &opts[j];
776 switch (option->opt_index)
778 case OPT_fdiagnostics_color_:
779 case OPT_fdiagnostics_format_:
780 case OPT_fdiagnostics_show_caret:
781 case OPT_fdiagnostics_show_labels:
782 case OPT_fdiagnostics_show_line_numbers:
783 case OPT_fdiagnostics_show_option:
784 case OPT_fdiagnostics_show_location_:
785 case OPT_fshow_column:
786 break;
787 default:
788 continue;
791 /* Pass the option on. */
792 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
793 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
798 /* Append linker options OPTS to ARGV_OBSTACK. */
800 static void
801 append_linker_options (obstack *argv_obstack, vec<cl_decoded_option> opts)
803 /* Append linker driver arguments. Compiler options from the linker
804 driver arguments will override / merge with those from the compiler. */
805 for (unsigned int j = 1; j < opts.length (); ++j)
807 cl_decoded_option *option = &opts[j];
809 /* Do not pass on frontend specific flags not suitable for lto. */
810 if (!(cl_options[option->opt_index].flags
811 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
812 continue;
814 switch (option->opt_index)
816 case OPT_o:
817 case OPT_flto_:
818 case OPT_flto:
819 /* We've handled these LTO options, do not pass them on. */
820 continue;
822 case OPT_fopenmp:
823 case OPT_fopenacc:
824 /* Ignore -fno-XXX form of these options, as otherwise
825 corresponding builtins will not be enabled. */
826 if (option->value == 0)
827 continue;
828 break;
830 default:
831 break;
834 /* Pass the option on. */
835 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
836 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
840 /* Extract options for TARGET offload compiler from OPTIONS and append
841 them to ARGV_OBSTACK. */
843 static void
844 append_offload_options (obstack *argv_obstack, const char *target,
845 vec<cl_decoded_option> options)
847 for (unsigned i = 0; i < options.length (); i++)
849 const char *cur, *next, *opts;
850 char **argv;
851 unsigned argc;
852 cl_decoded_option *option = &options[i];
854 if (option->opt_index != OPT_foffload_options_)
855 continue;
857 /* If option argument starts with '-' then no target is specified. That
858 means offload options are specified for all targets, so we need to
859 append them. */
860 if (option->arg[0] == '-')
861 opts = option->arg;
862 else
864 opts = strchr (option->arg, '=');
865 gcc_assert (opts);
866 cur = option->arg;
868 while (cur < opts)
870 next = strchr (cur, ',');
871 if (next == NULL)
872 next = opts;
873 next = (next > opts) ? opts : next;
875 /* Are we looking for this offload target? */
876 if (strlen (target) == (size_t) (next - cur)
877 && strncmp (target, cur, next - cur) == 0)
878 break;
880 /* Skip the comma or equal sign. */
881 cur = next + 1;
884 if (cur >= opts)
885 continue;
887 opts++;
890 argv = buildargv (opts);
891 for (argc = 0; argv[argc]; argc++)
892 obstack_ptr_grow (argv_obstack, argv[argc]);
896 /* Check whether NAME can be accessed in MODE. This is like access,
897 except that it never considers directories to be executable. */
899 static int
900 access_check (const char *name, int mode)
902 if (mode == X_OK)
904 struct stat st;
906 if (stat (name, &st) < 0
907 || S_ISDIR (st.st_mode))
908 return -1;
911 return access (name, mode);
914 /* Prepare a target image for offload TARGET, using mkoffload tool from
915 COMPILER_PATH. Return the name of the resultant object file. */
917 static const char *
918 compile_offload_image (const char *target, const char *compiler_path,
919 unsigned in_argc, char *in_argv[],
920 vec<cl_decoded_option> compiler_opts,
921 vec<cl_decoded_option> linker_opts,
922 char **filename)
924 char *dumpbase;
925 char **argv;
926 char *suffix
927 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
928 strcpy (suffix, "/accel/");
929 strcat (suffix, target);
930 strcat (suffix, "/mkoffload");
931 *filename = NULL;
933 char **paths = NULL;
934 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
936 const char *compiler = NULL;
937 for (unsigned i = 0; i < n_paths; i++)
938 if (access_check (paths[i], X_OK) == 0)
940 compiler = paths[i];
941 break;
943 #if OFFLOAD_DEFAULTED
944 if (!compiler && getenv (OFFLOAD_TARGET_DEFAULT_ENV))
946 free_array_of_ptrs ((void **) paths, n_paths);
947 return NULL;
949 #endif
951 if (!compiler)
952 fatal_error (input_location,
953 "could not find %s in %s (consider using %<-B%>)",
954 suffix + 1, compiler_path);
956 dumpbase = concat (dumppfx, "x", target, NULL);
958 /* Generate temporary output file name. */
959 if (save_temps)
960 *filename = concat (dumpbase, ".o", NULL);
961 else
962 *filename = make_temp_file (".target.o");
964 struct obstack argv_obstack;
965 obstack_init (&argv_obstack);
966 obstack_ptr_grow (&argv_obstack, compiler);
967 if (save_temps)
968 obstack_ptr_grow (&argv_obstack, "-save-temps");
969 if (verbose)
970 obstack_ptr_grow (&argv_obstack, "-v");
971 obstack_ptr_grow (&argv_obstack, "-o");
972 obstack_ptr_grow (&argv_obstack, *filename);
974 /* Append names of input object files. */
975 for (unsigned i = 0; i < in_argc; i++)
976 obstack_ptr_grow (&argv_obstack, in_argv[i]);
978 /* Append options from offload_lto sections. */
979 append_compiler_options (&argv_obstack, compiler_opts);
980 append_diag_options (&argv_obstack, linker_opts);
982 obstack_ptr_grow (&argv_obstack, "-dumpbase");
983 obstack_ptr_grow (&argv_obstack, dumpbase);
985 /* Append options specified by -foffload last. In case of conflicting
986 options we expect offload compiler to choose the latest. */
987 append_offload_options (&argv_obstack, target, compiler_opts);
988 append_offload_options (&argv_obstack, target, linker_opts);
990 obstack_ptr_grow (&argv_obstack, NULL);
991 argv = XOBFINISH (&argv_obstack, char **);
992 fork_execute (argv[0], argv, true, "offload_args");
993 obstack_free (&argv_obstack, NULL);
995 free_array_of_ptrs ((void **) paths, n_paths);
996 return *filename;
1000 /* The main routine dealing with offloading.
1001 The routine builds a target image for each offload target. IN_ARGC and
1002 IN_ARGV specify options and input object files. As all of them could contain
1003 target sections, we pass them all to target compilers. */
1005 static void
1006 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
1007 vec<cl_decoded_option> compiler_opts,
1008 vec<cl_decoded_option> linker_opts)
1010 char **names = NULL;
1011 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
1012 if (!target_names)
1013 return;
1014 unsigned num_targets = parse_env_var (target_names, &names, NULL);
1015 int next_name_entry = 0;
1017 const char *compiler_path = getenv ("COMPILER_PATH");
1018 if (!compiler_path)
1019 goto out;
1021 /* Prepare an image for each target and save the name of the resultant object
1022 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
1023 offload_names = XCNEWVEC (char *, num_targets + 1);
1024 for (unsigned i = 0; i < num_targets; i++)
1026 if (!compile_offload_image (names[i], compiler_path, in_argc, in_argv,
1027 compiler_opts, linker_opts,
1028 &offload_names[next_name_entry]))
1029 #if OFFLOAD_DEFAULTED
1030 continue;
1031 #else
1032 fatal_error (input_location,
1033 "problem with building target image for %s", names[i]);
1034 #endif
1035 next_name_entry++;
1038 #if OFFLOAD_DEFAULTED
1039 if (next_name_entry == 0)
1041 free (offload_names);
1042 offload_names = NULL;
1044 #endif
1046 out:
1047 free_array_of_ptrs ((void **) names, num_targets);
1050 /* Copy a file from SRC to DEST. */
1052 static void
1053 copy_file (const char *dest, const char *src)
1055 FILE *d = fopen (dest, "wb");
1056 FILE *s = fopen (src, "rb");
1057 char buffer[512];
1058 while (!feof (s))
1060 size_t len = fread (buffer, 1, 512, s);
1061 if (ferror (s) != 0)
1062 fatal_error (input_location, "reading input file");
1063 if (len > 0)
1065 fwrite (buffer, 1, len, d);
1066 if (ferror (d) != 0)
1067 fatal_error (input_location, "writing output file");
1070 fclose (d);
1071 fclose (s);
1074 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
1075 the copy to the linker. */
1077 static void
1078 find_crtoffloadtable (int save_temps, const char *dumppfx)
1080 char **paths = NULL;
1081 const char *library_path = getenv ("LIBRARY_PATH");
1082 if (!library_path)
1083 return;
1084 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
1086 unsigned i;
1087 for (i = 0; i < n_paths; i++)
1088 if (access_check (paths[i], R_OK) == 0)
1090 /* The linker will delete the filename we give it, so make a copy. */
1091 char *crtoffloadtable;
1092 if (!save_temps)
1093 crtoffloadtable = make_temp_file (".crtoffloadtable.o");
1094 else
1095 crtoffloadtable = concat (dumppfx, "crtoffloadtable.o", NULL);
1096 copy_file (crtoffloadtable, paths[i]);
1097 printf ("%s\n", crtoffloadtable);
1098 XDELETEVEC (crtoffloadtable);
1099 break;
1101 if (i == n_paths)
1102 fatal_error (input_location,
1103 "installation error, cannot find %<crtoffloadtable.o%>");
1105 free_array_of_ptrs ((void **) paths, n_paths);
1108 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
1109 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS.
1110 Return true if we found a matching section, false
1111 otherwise. COLLECT_GCC holds the value of the environment variable with
1112 the same name. */
1114 static bool
1115 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
1116 vec<cl_decoded_option> decoded_cl_options, bool first,
1117 vec<cl_decoded_option> *opts, const char *collect_gcc)
1119 off_t offset, length;
1120 char *data;
1121 char *fopts;
1122 const char *errmsg;
1123 int err;
1124 vec<cl_decoded_option> fdecoded_options;
1126 if (!first)
1127 fdecoded_options = *opts;
1129 simple_object_read *sobj;
1130 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
1131 &errmsg, &err);
1132 if (!sobj)
1133 return false;
1135 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
1136 strcpy (secname, prefix);
1137 strcat (secname, ".opts");
1138 if (!simple_object_find_section (sobj, secname, &offset, &length,
1139 &errmsg, &err))
1141 simple_object_release_read (sobj);
1142 return false;
1145 lseek (fd, file_offset + offset, SEEK_SET);
1146 data = (char *)xmalloc (length);
1147 read (fd, data, length);
1148 fopts = data;
1151 vec<cl_decoded_option> f2decoded_options
1152 = get_options_from_collect_gcc_options (collect_gcc, fopts);
1153 if (first)
1155 fdecoded_options = f2decoded_options;
1156 first = false;
1158 else
1159 merge_and_complain (fdecoded_options, f2decoded_options,
1160 decoded_cl_options);
1162 fopts += strlen (fopts) + 1;
1164 while (fopts - data < length);
1166 free (data);
1167 simple_object_release_read (sobj);
1168 *opts = fdecoded_options;
1169 return true;
1172 /* Copy early debug info sections from INFILE to a new file whose name
1173 is returned. Return NULL on error. */
1175 const char *
1176 debug_objcopy (const char *infile, bool rename)
1178 char *outfile;
1179 const char *errmsg;
1180 int err;
1182 const char *p;
1183 const char *orig_infile = infile;
1184 off_t inoff = 0;
1185 long loffset;
1186 int consumed;
1187 if ((p = strrchr (infile, '@'))
1188 && p != infile
1189 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1190 && strlen (p) == (unsigned int) consumed)
1192 char *fname = xstrdup (infile);
1193 fname[p - infile] = '\0';
1194 infile = fname;
1195 inoff = (off_t) loffset;
1197 int infd = open (infile, O_RDONLY | O_BINARY);
1198 if (infd == -1)
1199 return NULL;
1200 simple_object_read *inobj = simple_object_start_read (infd, inoff,
1201 "__GNU_LTO",
1202 &errmsg, &err);
1203 if (!inobj)
1204 return NULL;
1206 off_t off, len;
1207 if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info",
1208 &off, &len, &errmsg, &err) != 1)
1210 if (errmsg)
1211 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1213 simple_object_release_read (inobj);
1214 close (infd);
1215 return NULL;
1218 if (save_temps)
1219 outfile = concat (orig_infile, ".debug.temp.o", NULL);
1220 else
1221 outfile = make_temp_file (".debug.temp.o");
1222 errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename);
1223 if (errmsg)
1225 unlink_if_ordinary (outfile);
1226 fatal_error (0, "%s: %s", errmsg, xstrerror (err));
1229 simple_object_release_read (inobj);
1230 close (infd);
1232 return outfile;
1235 /* Helper for qsort: compare priorities for parallel compilation. */
1238 cmp_priority (const void *a, const void *b)
1240 return *((const int *)b)-*((const int *)a);
1243 /* Number of CPUs that can be used for parallel LTRANS phase. */
1245 static unsigned long nthreads_var = 0;
1247 #ifdef HAVE_PTHREAD_AFFINITY_NP
1248 unsigned long cpuset_size;
1249 static unsigned long get_cpuset_size;
1250 cpu_set_t *cpusetp;
1252 unsigned long
1253 static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp)
1255 #ifdef CPU_COUNT_S
1256 /* glibc 2.7 and above provide a macro for this. */
1257 return CPU_COUNT_S (cpusetsize, cpusetp);
1258 #else
1259 #ifdef CPU_COUNT
1260 if (cpusetsize == sizeof (cpu_set_t))
1261 /* glibc 2.6 and above provide a macro for this. */
1262 return CPU_COUNT (cpusetp);
1263 #endif
1264 size_t i;
1265 unsigned long ret = 0;
1266 STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int));
1267 for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++)
1269 unsigned long int mask = cpusetp->__bits[i];
1270 if (mask == 0)
1271 continue;
1272 ret += __builtin_popcountl (mask);
1274 return ret;
1275 #endif
1277 #endif
1279 /* At startup, determine the default number of threads. It would seem
1280 this should be related to the number of cpus online. */
1282 static void
1283 init_num_threads (void)
1285 #ifdef HAVE_PTHREAD_AFFINITY_NP
1286 #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE)
1287 cpuset_size = sysconf (_SC_NPROCESSORS_CONF);
1288 cpuset_size = CPU_ALLOC_SIZE (cpuset_size);
1289 #else
1290 cpuset_size = sizeof (cpu_set_t);
1291 #endif
1293 cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size);
1296 int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size,
1297 cpusetp);
1298 if (ret == 0)
1300 /* Count only the CPUs this process can use. */
1301 nthreads_var = cpuset_popcount (cpuset_size, cpusetp);
1302 if (nthreads_var == 0)
1303 break;
1304 get_cpuset_size = cpuset_size;
1305 #ifdef CPU_ALLOC_SIZE
1306 unsigned long i;
1307 for (i = cpuset_size * 8; i; i--)
1308 if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp))
1309 break;
1310 cpuset_size = CPU_ALLOC_SIZE (i);
1311 #endif
1312 return;
1314 if (ret != EINVAL)
1315 break;
1316 #ifdef CPU_ALLOC_SIZE
1317 if (cpuset_size < sizeof (cpu_set_t))
1318 cpuset_size = sizeof (cpu_set_t);
1319 else
1320 cpuset_size = cpuset_size * 2;
1321 if (cpuset_size < 8 * sizeof (cpu_set_t))
1322 cpusetp
1323 = (cpu_set_t *) realloc (cpusetp, cpuset_size);
1324 else
1326 /* Avoid fatal if too large memory allocation would be
1327 requested, e.g. kernel returning EINVAL all the time. */
1328 void *p = realloc (cpusetp, cpuset_size);
1329 if (p == NULL)
1330 break;
1331 cpusetp = (cpu_set_t *) p;
1333 #else
1334 break;
1335 #endif
1337 while (1);
1338 cpuset_size = 0;
1339 nthreads_var = 1;
1340 free (cpusetp);
1341 cpusetp = NULL;
1342 #endif
1343 #ifdef _SC_NPROCESSORS_ONLN
1344 nthreads_var = sysconf (_SC_NPROCESSORS_ONLN);
1345 #endif
1348 /* Print link to -flto documentation with a hint message. */
1350 void
1351 print_lto_docs_link ()
1353 bool print_url = global_dc->printer->url_format != URL_FORMAT_NONE;
1354 const char *url = global_dc->get_option_url (global_dc, OPT_flto);
1356 pretty_printer pp;
1357 pp.url_format = URL_FORMAT_DEFAULT;
1358 pp_string (&pp, "see the ");
1359 if (print_url)
1360 pp_begin_url (&pp, url);
1361 pp_string (&pp, "%<-flto%> option documentation");
1362 if (print_url)
1363 pp_end_url (&pp);
1364 pp_string (&pp, " for more information");
1365 inform (UNKNOWN_LOCATION, pp_formatted_text (&pp));
1368 /* Test that a make command is present and working, return true if so. */
1370 static bool
1371 make_exists (void)
1373 const char *make = "make";
1374 char **make_argv = buildargv (getenv ("MAKE"));
1375 if (make_argv)
1376 make = make_argv[0];
1377 const char *make_args[] = {make, "--version", NULL};
1379 int exit_status = 0;
1380 int err = 0;
1381 const char *errmsg
1382 = pex_one (PEX_SEARCH, make_args[0], CONST_CAST (char **, make_args),
1383 "make", NULL, NULL, &exit_status, &err);
1384 freeargv (make_argv);
1385 return errmsg == NULL && exit_status == 0 && err == 0;
1388 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1390 static void
1391 run_gcc (unsigned argc, char *argv[])
1393 unsigned i, j;
1394 const char **new_argv;
1395 const char **argv_ptr;
1396 char *list_option_full = NULL;
1397 const char *linker_output = NULL;
1398 const char *collect_gcc;
1399 char *collect_gcc_options;
1400 int parallel = 0;
1401 int jobserver = 0;
1402 bool jobserver_requested = false;
1403 int auto_parallel = 0;
1404 bool no_partition = false;
1405 bool fdecoded_options_first = true;
1406 vec<cl_decoded_option> fdecoded_options;
1407 fdecoded_options.create (16);
1408 bool offload_fdecoded_options_first = true;
1409 vec<cl_decoded_option> offload_fdecoded_options = vNULL;
1410 struct obstack argv_obstack;
1411 int new_head_argc;
1412 bool have_lto = false;
1413 bool have_offload = false;
1414 unsigned lto_argc = 0, ltoobj_argc = 0;
1415 char **lto_argv, **ltoobj_argv;
1416 bool linker_output_rel = false;
1417 bool skip_debug = false;
1418 const char *incoming_dumppfx = dumppfx = NULL;
1419 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1421 /* Get the driver and options. */
1422 collect_gcc = getenv ("COLLECT_GCC");
1423 if (!collect_gcc)
1424 fatal_error (input_location,
1425 "environment variable %<COLLECT_GCC%> must be set");
1426 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
1427 if (!collect_gcc_options)
1428 fatal_error (input_location,
1429 "environment variable %<COLLECT_GCC_OPTIONS%> must be set");
1431 char *collect_as_options = getenv ("COLLECT_AS_OPTIONS");
1433 /* Prepend -Xassembler to each option, and append the string
1434 to collect_gcc_options. */
1435 if (collect_as_options)
1437 obstack temporary_obstack;
1438 obstack_init (&temporary_obstack);
1440 prepend_xassembler_to_collect_as_options (collect_as_options,
1441 &temporary_obstack);
1442 obstack_1grow (&temporary_obstack, '\0');
1444 char *xassembler_opts_string
1445 = XOBFINISH (&temporary_obstack, char *);
1446 collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string,
1447 NULL);
1450 vec<cl_decoded_option> decoded_options
1451 = get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options);
1453 /* Allocate array for input object files with LTO IL,
1454 and for possible preceding arguments. */
1455 lto_argv = XNEWVEC (char *, argc);
1456 ltoobj_argv = XNEWVEC (char *, argc);
1458 /* Look at saved options in the IL files. */
1459 for (i = 1; i < argc; ++i)
1461 char *p;
1462 int fd;
1463 off_t file_offset = 0;
1464 long loffset;
1465 int consumed;
1466 char *filename = argv[i];
1468 if (startswith (argv[i], "-foffload-objects="))
1470 have_offload = true;
1471 offload_objects_file_name
1472 = argv[i] + sizeof ("-foffload-objects=") - 1;
1473 continue;
1476 if ((p = strrchr (argv[i], '@'))
1477 && p != argv[i]
1478 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1479 && strlen (p) == (unsigned int) consumed)
1481 filename = XNEWVEC (char, p - argv[i] + 1);
1482 memcpy (filename, argv[i], p - argv[i]);
1483 filename[p - argv[i]] = '\0';
1484 file_offset = (off_t) loffset;
1486 fd = open (filename, O_RDONLY | O_BINARY);
1487 /* Linker plugin passes -fresolution and -flinker-output options.
1488 -flinker-output is passed only when user did not specify one and thus
1489 we do not need to worry about duplicities with the option handling
1490 below. */
1491 if (fd == -1)
1493 lto_argv[lto_argc++] = argv[i];
1494 if (strcmp (argv[i], "-flinker-output=rel") == 0)
1495 linker_output_rel = true;
1496 continue;
1499 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1500 decoded_options, fdecoded_options_first,
1501 &fdecoded_options,
1502 collect_gcc))
1504 have_lto = true;
1505 ltoobj_argv[ltoobj_argc++] = argv[i];
1506 fdecoded_options_first = false;
1508 close (fd);
1511 /* Initalize the common arguments for the driver. */
1512 obstack_init (&argv_obstack);
1513 obstack_ptr_grow (&argv_obstack, collect_gcc);
1514 obstack_ptr_grow (&argv_obstack, "-xlto");
1515 obstack_ptr_grow (&argv_obstack, "-c");
1517 append_compiler_options (&argv_obstack, fdecoded_options);
1518 append_linker_options (&argv_obstack, decoded_options);
1520 /* Scan linker driver arguments for things that are of relevance to us. */
1521 for (j = 1; j < decoded_options.length (); ++j)
1523 cl_decoded_option *option = &decoded_options[j];
1524 switch (option->opt_index)
1526 case OPT_o:
1527 linker_output = option->arg;
1528 break;
1530 /* We don't have to distinguish between -save-temps=* and
1531 -save-temps, -dumpdir already carries that
1532 information. */
1533 case OPT_save_temps_:
1534 case OPT_save_temps:
1535 save_temps = 1;
1536 break;
1538 case OPT_v:
1539 verbose = 1;
1540 break;
1542 case OPT_flto_partition_:
1543 if (strcmp (option->arg, "none") == 0)
1544 no_partition = true;
1545 break;
1547 case OPT_flto_:
1548 /* Merge linker -flto= option with what we have in IL files. */
1549 merge_flto_options (fdecoded_options, option);
1550 if (strcmp (option->arg, "jobserver") == 0)
1551 jobserver_requested = true;
1552 break;
1554 case OPT_flinker_output_:
1555 linker_output_rel = !strcmp (option->arg, "rel");
1556 break;
1558 case OPT_g:
1559 /* Recognize -g0. */
1560 skip_debug = option->arg && !strcmp (option->arg, "0");
1561 break;
1563 case OPT_dumpdir:
1564 incoming_dumppfx = dumppfx = option->arg;
1565 break;
1567 case OPT_fdiagnostics_urls_:
1568 diagnostic_urls_init (global_dc, option->value);
1569 break;
1571 case OPT_fdiagnostics_color_:
1572 diagnostic_color_init (global_dc, option->value);
1573 break;
1575 default:
1576 break;
1580 /* Process LTO-related options on merged options. */
1581 for (j = 1; j < fdecoded_options.length (); ++j)
1583 cl_decoded_option *option = &fdecoded_options[j];
1584 switch (option->opt_index)
1586 case OPT_flto_:
1587 if (strcmp (option->arg, "jobserver") == 0)
1589 parallel = 1;
1590 jobserver = 1;
1592 else if (strcmp (option->arg, "auto") == 0)
1594 parallel = 1;
1595 auto_parallel = 1;
1597 else
1599 parallel = atoi (option->arg);
1600 if (parallel <= 1)
1601 parallel = 0;
1603 /* Fallthru. */
1605 case OPT_flto:
1606 lto_mode = LTO_MODE_WHOPR;
1607 break;
1611 /* Output lto-wrapper invocation command. */
1612 if (verbose)
1614 for (i = 0; i < argc; ++i)
1616 fputs (argv[i], stderr);
1617 fputc (' ', stderr);
1619 fputc ('\n', stderr);
1622 if (linker_output_rel)
1623 no_partition = true;
1625 if (no_partition)
1627 lto_mode = LTO_MODE_LTO;
1628 jobserver = 0;
1629 jobserver_requested = false;
1630 auto_parallel = 0;
1631 parallel = 0;
1633 else
1635 jobserver_info jinfo;
1636 if (jobserver && !jinfo.is_active)
1638 /* Fall back to auto parallelism. */
1639 jobserver = 0;
1640 auto_parallel = 1;
1642 else if (!jobserver && jinfo.is_active)
1644 parallel = 1;
1645 jobserver = 1;
1649 /* We need make working for a parallel execution. */
1650 if (parallel && !make_exists ())
1651 parallel = 0;
1653 if (!dumppfx)
1655 if (!linker_output
1656 || strcmp (linker_output, HOST_BIT_BUCKET) == 0)
1657 dumppfx = "a.";
1658 else
1660 const char *obase = lbasename (linker_output), *temp;
1662 /* Strip the executable extension. */
1663 size_t blen = strlen (obase), xlen;
1664 if ((temp = strrchr (obase + 1, '.'))
1665 && (xlen = strlen (temp))
1666 && (strcmp (temp, ".exe") == 0
1667 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1668 || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0
1669 #endif
1670 || strcmp (obase, "a.out") == 0))
1671 dumppfx = xstrndup (linker_output,
1672 obase - linker_output + blen - xlen + 1);
1673 else
1674 dumppfx = concat (linker_output, ".", NULL);
1678 /* If there's no directory component in the dumppfx, add one, so
1679 that, when it is used as -dumpbase, it overrides any occurrence
1680 of -dumpdir that might have been passed in. */
1681 if (!dumppfx || lbasename (dumppfx) == dumppfx)
1682 dumppfx = concat (current_dir, dumppfx, NULL);
1684 /* Make sure some -dumpdir is passed, so as to get predictable
1685 -dumpbase overriding semantics. If we got an incoming -dumpdir
1686 argument, we'll pass it on, so don't bother with another one
1687 then. */
1688 if (!incoming_dumppfx)
1690 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1691 obstack_ptr_grow (&argv_obstack, "");
1693 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1695 /* Remember at which point we can scrub args to re-use the commons. */
1696 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1698 if (have_offload)
1700 unsigned i, num_offload_files;
1701 char **offload_argv;
1702 FILE *f;
1704 f = fopen (offload_objects_file_name, "r");
1705 if (f == NULL)
1706 fatal_error (input_location, "cannot open %s: %m",
1707 offload_objects_file_name);
1708 if (fscanf (f, "%u ", &num_offload_files) != 1)
1709 fatal_error (input_location, "cannot read %s: %m",
1710 offload_objects_file_name);
1711 offload_argv = XCNEWVEC (char *, num_offload_files);
1713 /* Read names of object files with offload. */
1714 for (i = 0; i < num_offload_files; i++)
1716 const unsigned piece = 32;
1717 char *buf, *filename = XNEWVEC (char, piece);
1718 size_t len;
1720 buf = filename;
1721 cont1:
1722 if (!fgets (buf, piece, f))
1723 break;
1724 len = strlen (filename);
1725 if (filename[len - 1] != '\n')
1727 filename = XRESIZEVEC (char, filename, len + piece);
1728 buf = filename + len;
1729 goto cont1;
1731 filename[len - 1] = '\0';
1732 offload_argv[i] = filename;
1734 fclose (f);
1735 if (offload_argv[num_offload_files - 1] == NULL)
1736 fatal_error (input_location, "invalid format of %s",
1737 offload_objects_file_name);
1738 maybe_unlink (offload_objects_file_name);
1739 offload_objects_file_name = NULL;
1741 /* Look at saved offload options in files. */
1742 for (i = 0; i < num_offload_files; i++)
1744 char *p;
1745 long loffset;
1746 int fd, consumed;
1747 off_t file_offset = 0;
1748 char *filename = offload_argv[i];
1750 if ((p = strrchr (offload_argv[i], '@'))
1751 && p != offload_argv[i]
1752 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1753 && strlen (p) == (unsigned int) consumed)
1755 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1756 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1757 filename[p - offload_argv[i]] = '\0';
1758 file_offset = (off_t) loffset;
1760 fd = open (filename, O_RDONLY | O_BINARY);
1761 if (fd == -1)
1762 fatal_error (input_location, "cannot open %s: %m", filename);
1763 if (!find_and_merge_options (fd, file_offset,
1764 OFFLOAD_SECTION_NAME_PREFIX,
1765 decoded_options,
1766 offload_fdecoded_options_first,
1767 &offload_fdecoded_options,
1768 collect_gcc))
1769 fatal_error (input_location, "cannot read %s: %m", filename);
1770 offload_fdecoded_options_first = false;
1771 close (fd);
1772 if (filename != offload_argv[i])
1773 XDELETEVEC (filename);
1776 compile_images_for_offload_targets (num_offload_files, offload_argv,
1777 offload_fdecoded_options, decoded_options);
1779 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1781 if (offload_names)
1783 find_crtoffloadtable (save_temps, dumppfx);
1784 for (i = 0; offload_names[i]; i++)
1785 printf ("%s\n", offload_names[i]);
1786 free_array_of_ptrs ((void **) offload_names, i);
1787 offload_names = NULL;
1791 /* If object files contain offload sections, but do not contain LTO sections,
1792 then there is no need to perform a link-time recompilation, i.e.
1793 lto-wrapper is used only for a compilation of offload images. */
1794 if (have_offload && !have_lto)
1795 goto finish;
1797 if (lto_mode == LTO_MODE_LTO)
1799 /* -dumpbase argument for LTO. */
1800 flto_out = concat (dumppfx, "lto.o", NULL);
1801 obstack_ptr_grow (&argv_obstack, flto_out);
1803 if (!save_temps)
1804 flto_out = make_temp_file (".lto.o");
1805 obstack_ptr_grow (&argv_obstack, "-o");
1806 obstack_ptr_grow (&argv_obstack, flto_out);
1808 else
1810 const char *list_option = "-fltrans-output-list=";
1812 /* -dumpbase argument for WPA. */
1813 char *dumpbase = concat (dumppfx, "wpa", NULL);
1814 obstack_ptr_grow (&argv_obstack, dumpbase);
1816 if (save_temps)
1817 ltrans_output_file = concat (dumppfx, "ltrans.out", NULL);
1818 else
1819 ltrans_output_file = make_temp_file (".ltrans.out");
1820 list_option_full = concat (list_option, ltrans_output_file, NULL);
1821 obstack_ptr_grow (&argv_obstack, list_option_full);
1823 if (jobserver)
1825 if (verbose)
1826 fprintf (stderr, "Using make jobserver\n");
1827 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1829 else if (auto_parallel)
1831 char buf[256];
1832 init_num_threads ();
1833 if (nthreads_var == 0)
1834 nthreads_var = 1;
1835 if (verbose)
1836 fprintf (stderr, "LTO parallelism level set to %ld\n",
1837 nthreads_var);
1838 sprintf (buf, "-fwpa=%ld", nthreads_var);
1839 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1841 else if (parallel > 1)
1843 char buf[256];
1844 sprintf (buf, "-fwpa=%i", parallel);
1845 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1847 else
1848 obstack_ptr_grow (&argv_obstack, "-fwpa");
1851 /* Append input arguments. */
1852 for (i = 0; i < lto_argc; ++i)
1853 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1854 /* Append the input objects. */
1855 for (i = 0; i < ltoobj_argc; ++i)
1856 obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]);
1857 obstack_ptr_grow (&argv_obstack, NULL);
1859 new_argv = XOBFINISH (&argv_obstack, const char **);
1860 argv_ptr = &new_argv[new_head_argc];
1861 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true,
1862 "ltrans_args");
1864 /* Copy the early generated debug info from the objects to temporary
1865 files and append those to the partial link commandline. */
1866 early_debug_object_names = NULL;
1867 if (! skip_debug)
1869 early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1);
1870 num_deb_objs = ltoobj_argc;
1871 for (i = 0; i < ltoobj_argc; ++i)
1873 const char *tem;
1874 if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel)))
1875 early_debug_object_names[i] = tem;
1879 if (lto_mode == LTO_MODE_LTO)
1881 printf ("%s\n", flto_out);
1882 if (!skip_debug)
1884 for (i = 0; i < ltoobj_argc; ++i)
1885 if (early_debug_object_names[i] != NULL)
1886 printf ("%s\n", early_debug_object_names[i]);
1888 /* These now belong to collect2. */
1889 free (flto_out);
1890 flto_out = NULL;
1891 free (early_debug_object_names);
1892 early_debug_object_names = NULL;
1894 else
1896 FILE *stream = fopen (ltrans_output_file, "r");
1897 FILE *mstream = NULL;
1898 struct obstack env_obstack;
1899 int priority;
1901 if (!stream)
1902 fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file);
1904 /* Parse the list of LTRANS inputs from the WPA stage. */
1905 obstack_init (&env_obstack);
1906 nr = 0;
1907 for (;;)
1909 const unsigned piece = 32;
1910 char *output_name = NULL;
1911 char *buf, *input_name = (char *)xmalloc (piece);
1912 size_t len;
1914 buf = input_name;
1915 if (fscanf (stream, "%i\n", &priority) != 1)
1917 if (!feof (stream))
1918 fatal_error (input_location,
1919 "corrupted ltrans output file %s",
1920 ltrans_output_file);
1921 break;
1923 cont:
1924 if (!fgets (buf, piece, stream))
1925 break;
1926 len = strlen (input_name);
1927 if (input_name[len - 1] != '\n')
1929 input_name = (char *)xrealloc (input_name, len + piece);
1930 buf = input_name + len;
1931 goto cont;
1933 input_name[len - 1] = '\0';
1935 if (input_name[0] == '*')
1936 output_name = &input_name[1];
1938 nr++;
1939 ltrans_priorities
1940 = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2);
1941 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1942 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1943 ltrans_priorities[(nr-1)*2] = priority;
1944 ltrans_priorities[(nr-1)*2+1] = nr-1;
1945 input_names[nr-1] = input_name;
1946 output_names[nr-1] = output_name;
1948 fclose (stream);
1949 maybe_unlink (ltrans_output_file);
1950 ltrans_output_file = NULL;
1952 if (nr > 1)
1954 jobserver_info jinfo;
1955 if (jobserver_requested && !jinfo.is_active)
1957 warning (0, jinfo.error_msg.c_str ());
1958 print_lto_docs_link ();
1960 else if (parallel == 0)
1962 warning (0, "using serial compilation of %d LTRANS jobs", nr);
1963 print_lto_docs_link ();
1967 if (parallel)
1969 makefile = make_temp_file (".mk");
1970 mstream = fopen (makefile, "w");
1971 qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority);
1974 /* Execute the LTRANS stage for each input file (or prepare a
1975 makefile to invoke this in parallel). */
1976 for (i = 0; i < nr; ++i)
1978 char *output_name;
1979 char *input_name = input_names[i];
1980 /* If it's a pass-through file do nothing. */
1981 if (output_names[i])
1982 continue;
1984 /* Replace the .o suffix with a .ltrans.o suffix and write
1985 the resulting name to the LTRANS output list. */
1986 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1987 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1988 output_name = XOBFINISH (&env_obstack, char *);
1990 /* Adjust the dumpbase if the linker output file was seen. */
1991 int dumpbase_len = (strlen (dumppfx)
1992 + sizeof (DUMPBASE_SUFFIX)
1993 + sizeof (".ltrans"));
1994 char *dumpbase = (char *) xmalloc (dumpbase_len + 1);
1995 snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i);
1996 argv_ptr[0] = dumpbase;
1998 argv_ptr[1] = "-fltrans";
1999 argv_ptr[2] = "-o";
2000 argv_ptr[3] = output_name;
2001 argv_ptr[4] = input_name;
2002 argv_ptr[5] = NULL;
2003 if (parallel)
2005 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
2006 for (j = 1; new_argv[j] != NULL; ++j)
2007 fprintf (mstream, " '%s'", new_argv[j]);
2008 fprintf (mstream, "\n");
2009 /* If we are not preserving the ltrans input files then
2010 truncate them as soon as we have processed it. This
2011 reduces temporary disk-space usage. */
2012 if (! save_temps)
2013 fprintf (mstream, "\t@-touch -r \"%s\" \"%s.tem\" > /dev/null "
2014 "2>&1 && mv \"%s.tem\" \"%s\"\n",
2015 input_name, input_name, input_name, input_name);
2017 else
2019 char argsuffix[sizeof (DUMPBASE_SUFFIX)
2020 + sizeof (".ltrans_args") + 1];
2021 if (save_temps)
2022 snprintf (argsuffix,
2023 sizeof (DUMPBASE_SUFFIX) + sizeof (".ltrans_args"),
2024 "ltrans%u.ltrans_args", i);
2025 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
2026 true, save_temps ? argsuffix : NULL);
2027 maybe_unlink (input_name);
2030 output_names[i] = output_name;
2032 if (parallel)
2034 struct pex_obj *pex;
2035 char jobs[32];
2037 fprintf (mstream,
2038 ".PHONY: all\n"
2039 "all:");
2040 for (i = 0; i < nr; ++i)
2042 int j = ltrans_priorities[i*2 + 1];
2043 fprintf (mstream, " \\\n\t%s", output_names[j]);
2045 fprintf (mstream, "\n");
2046 fclose (mstream);
2047 if (!jobserver)
2049 /* Avoid passing --jobserver-fd= and similar flags
2050 unless jobserver mode is explicitly enabled. */
2051 putenv (xstrdup ("MAKEFLAGS="));
2052 putenv (xstrdup ("MFLAGS="));
2055 char **make_argv = buildargv (getenv ("MAKE"));
2056 if (make_argv)
2058 for (unsigned argc = 0; make_argv[argc]; argc++)
2059 obstack_ptr_grow (&argv_obstack, make_argv[argc]);
2061 else
2062 obstack_ptr_grow (&argv_obstack, "make");
2064 obstack_ptr_grow (&argv_obstack, "-f");
2065 obstack_ptr_grow (&argv_obstack, makefile);
2066 if (!jobserver)
2068 snprintf (jobs, 31, "-j%ld",
2069 auto_parallel ? nthreads_var : parallel);
2070 obstack_ptr_grow (&argv_obstack, jobs);
2072 obstack_ptr_grow (&argv_obstack, "all");
2073 obstack_ptr_grow (&argv_obstack, NULL);
2074 new_argv = XOBFINISH (&argv_obstack, const char **);
2076 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
2077 NULL, NULL, PEX_SEARCH, false, NULL);
2078 do_wait (new_argv[0], pex);
2079 freeargv (make_argv);
2080 maybe_unlink (makefile);
2081 makefile = NULL;
2082 for (i = 0; i < nr; ++i)
2083 maybe_unlink (input_names[i]);
2085 for (i = 0; i < nr; ++i)
2087 fputs (output_names[i], stdout);
2088 putc ('\n', stdout);
2089 free (input_names[i]);
2091 if (!skip_debug)
2093 for (i = 0; i < ltoobj_argc; ++i)
2094 if (early_debug_object_names[i] != NULL)
2095 printf ("%s\n", early_debug_object_names[i]);
2097 nr = 0;
2098 free (ltrans_priorities);
2099 free (output_names);
2100 output_names = NULL;
2101 free (early_debug_object_names);
2102 early_debug_object_names = NULL;
2103 free (input_names);
2104 free (list_option_full);
2105 obstack_free (&env_obstack, NULL);
2108 finish:
2109 XDELETE (lto_argv);
2110 obstack_free (&argv_obstack, NULL);
2114 /* Entry point. */
2117 main (int argc, char *argv[])
2119 const char *p;
2121 init_opts_obstack ();
2123 p = argv[0] + strlen (argv[0]);
2124 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
2125 --p;
2126 progname = p;
2128 xmalloc_set_program_name (progname);
2130 gcc_init_libintl ();
2132 diagnostic_initialize (global_dc, 0);
2133 diagnostic_color_init (global_dc);
2134 diagnostic_urls_init (global_dc);
2135 global_dc->get_option_url = get_option_url;
2137 if (atexit (lto_wrapper_cleanup) != 0)
2138 fatal_error (input_location, "%<atexit%> failed");
2140 setup_signals ();
2142 /* We may be called with all the arguments stored in some file and
2143 passed with @file. Expand them into argv before processing. */
2144 expandargv (&argc, &argv);
2146 run_gcc (argc, argv);
2148 return 0;