Debian Codegraph @

gcc/lto-wrapper.cc

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