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