re PR bootstrap/80838 (PGO/LTO bootstrapped compiler 5% slower than pure PGO bootstra...
[platform/upstream/gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto.  Used by collect2 and the linker plugin.
2    Copyright (C) 2009-2017 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 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
51
52 /* Environment variable, used for passing the names of offload targets from GCC
53    driver to lto-wrapper.  */
54 #define OFFLOAD_TARGET_NAMES_ENV        "OFFLOAD_TARGET_NAMES"
55
56 enum lto_mode_d {
57   LTO_MODE_NONE,                        /* Not doing LTO.  */
58   LTO_MODE_LTO,                         /* Normal LTO.  */
59   LTO_MODE_WHOPR                        /* WHOPR.  */
60 };
61
62 /* Current LTO mode.  */
63 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
64
65 static char *ltrans_output_file;
66 static char *flto_out;
67 static unsigned int nr;
68 static char **input_names;
69 static char **output_names;
70 static char **offload_names;
71 static char *offload_objects_file_name;
72 static char *makefile;
73
74 const char tool_name[] = "lto-wrapper";
75
76 /* Delete tempfiles.  Called from utils_cleanup.  */
77
78 void
79 tool_cleanup (bool)
80 {
81   unsigned int i;
82
83   if (ltrans_output_file)
84     maybe_unlink (ltrans_output_file);
85   if (flto_out)
86     maybe_unlink (flto_out);
87   if (offload_objects_file_name)
88     maybe_unlink (offload_objects_file_name);
89   if (makefile)
90     maybe_unlink (makefile);
91   for (i = 0; i < nr; ++i)
92     {
93       maybe_unlink (input_names[i]);
94       if (output_names[i])
95         maybe_unlink (output_names[i]);
96     }
97 }
98
99 static void
100 lto_wrapper_cleanup (void)
101 {
102   utils_cleanup (false);
103 }
104
105 /* Unlink a temporary LTRANS file unless requested otherwise.  */
106
107 void
108 maybe_unlink (const char *file)
109 {
110   if (!save_temps)
111     {
112       if (unlink_if_ordinary (file)
113           && errno != ENOENT)
114         fatal_error (input_location, "deleting LTRANS file %s: %m", file);
115     }
116   else if (verbose)
117     fprintf (stderr, "[Leaving LTRANS %s]\n", file);
118 }
119
120 /* Template of LTRANS dumpbase suffix.  */
121 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
122
123 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
124    environment according to LANG_MASK.  */
125
126 static void
127 get_options_from_collect_gcc_options (const char *collect_gcc,
128                                       const char *collect_gcc_options,
129                                       unsigned int lang_mask,
130                                       struct cl_decoded_option **decoded_options,
131                                       unsigned int *decoded_options_count)
132 {
133   struct obstack argv_obstack;
134   char *argv_storage;
135   const char **argv;
136   int j, k, argc;
137
138   argv_storage = xstrdup (collect_gcc_options);
139   obstack_init (&argv_obstack);
140   obstack_ptr_grow (&argv_obstack, collect_gcc);
141
142   for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
143     {
144       if (argv_storage[j] == '\'')
145         {
146           obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
147           ++j;
148           do
149             {
150               if (argv_storage[j] == '\0')
151                 fatal_error (input_location, "malformed COLLECT_GCC_OPTIONS");
152               else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
153                 {
154                   argv_storage[k++] = '\'';
155                   j += 4;
156                 }
157               else if (argv_storage[j] == '\'')
158                 break;
159               else
160                 argv_storage[k++] = argv_storage[j++];
161             }
162           while (1);
163           argv_storage[k++] = '\0';
164         }
165     }
166
167   obstack_ptr_grow (&argv_obstack, NULL);
168   argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
169   argv = XOBFINISH (&argv_obstack, const char **);
170
171   decode_cmdline_options_to_array (argc, (const char **)argv,
172                                    lang_mask,
173                                    decoded_options, decoded_options_count);
174   obstack_free (&argv_obstack, NULL);
175 }
176
177 /* Append OPTION to the options array DECODED_OPTIONS with size
178    DECODED_OPTIONS_COUNT.  */
179
180 static void
181 append_option (struct cl_decoded_option **decoded_options,
182                unsigned int *decoded_options_count,
183                struct cl_decoded_option *option)
184 {
185   ++*decoded_options_count;
186   *decoded_options
187     = (struct cl_decoded_option *)
188         xrealloc (*decoded_options,
189                   (*decoded_options_count
190                    * sizeof (struct cl_decoded_option)));
191   memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
192           sizeof (struct cl_decoded_option));
193 }
194
195 /* Remove option number INDEX from DECODED_OPTIONS, update
196    DECODED_OPTIONS_COUNT.  */
197
198 static void
199 remove_option (struct cl_decoded_option **decoded_options,
200                int index, unsigned int *decoded_options_count)
201 {
202   --*decoded_options_count;
203   memmove (&(*decoded_options)[index + 1],
204            &(*decoded_options)[index],
205            sizeof (struct cl_decoded_option)
206            * (*decoded_options_count - index));
207 }
208
209 /* Try to merge and complain about options FDECODED_OPTIONS when applied
210    ontop of DECODED_OPTIONS.  */
211
212 static void
213 merge_and_complain (struct cl_decoded_option **decoded_options,
214                     unsigned int *decoded_options_count,
215                     struct cl_decoded_option *fdecoded_options,
216                     unsigned int fdecoded_options_count)
217 {
218   unsigned int i, j;
219   struct cl_decoded_option *pic_option = NULL;
220   struct cl_decoded_option *pie_option = NULL;
221
222   /* ???  Merge options from files.  Most cases can be
223      handled by either unioning or intersecting
224      (for example -fwrapv is a case for unioning,
225      -ffast-math is for intersection).  Most complaints
226      about real conflicts between different options can
227      be deferred to the compiler proper.  Options that
228      we can neither safely handle by intersection nor
229      unioning would need to be complained about here.
230      Ideally we'd have a flag in the opt files that
231      tells whether to union or intersect or reject.
232      In absence of that it's unclear what a good default is.
233      It's also difficult to get positional handling correct.  */
234
235   /* The following does what the old LTO option code did,
236      union all target and a selected set of common options.  */
237   for (i = 0; i < fdecoded_options_count; ++i)
238     {
239       struct cl_decoded_option *foption = &fdecoded_options[i];
240       switch (foption->opt_index)
241         {
242         case OPT_SPECIAL_unknown:
243         case OPT_SPECIAL_ignore:
244         case OPT_SPECIAL_program_name:
245         case OPT_SPECIAL_input_file:
246           break;
247
248         default:
249           if (!(cl_options[foption->opt_index].flags & CL_TARGET))
250             break;
251
252           /* Fallthru.  */
253         case OPT_fdiagnostics_show_caret:
254         case OPT_fdiagnostics_show_option:
255         case OPT_fdiagnostics_show_location_:
256         case OPT_fshow_column:
257         case OPT_fcommon:
258         case OPT_fgnu_tm:
259           /* Do what the old LTO code did - collect exactly one option
260              setting per OPT code, we pick the first we encounter.
261              ???  This doesn't make too much sense, but when it doesn't
262              then we should complain.  */
263           for (j = 0; j < *decoded_options_count; ++j)
264             if ((*decoded_options)[j].opt_index == foption->opt_index)
265               break;
266           if (j == *decoded_options_count)
267             append_option (decoded_options, decoded_options_count, foption);
268           break;
269
270         /* Figure out what PIC/PIE level wins and merge the results.  */
271         case OPT_fPIC:
272         case OPT_fpic:
273           pic_option = foption;
274           break;
275         case OPT_fPIE:
276         case OPT_fpie:
277           pie_option = foption;
278           break;
279
280         case OPT_fopenmp:
281         case OPT_fopenacc:
282         case OPT_fcilkplus:
283         case OPT_fcheck_pointer_bounds:
284           /* For selected options we can merge conservatively.  */
285           for (j = 0; j < *decoded_options_count; ++j)
286             if ((*decoded_options)[j].opt_index == foption->opt_index)
287               break;
288           if (j == *decoded_options_count)
289             append_option (decoded_options, decoded_options_count, foption);
290           /* -fopenmp > -fno-openmp,
291              -fopenacc > -fno-openacc,
292              -fcilkplus > -fno-cilkplus,
293              -fcheck_pointer_bounds > -fcheck_pointer_bounds  */
294           else if (foption->value > (*decoded_options)[j].value)
295             (*decoded_options)[j] = *foption;
296           break;
297
298         case OPT_fopenacc_dim_:
299           /* Append or check identical.  */
300           for (j = 0; j < *decoded_options_count; ++j)
301             if ((*decoded_options)[j].opt_index == foption->opt_index)
302               break;
303           if (j == *decoded_options_count)
304             append_option (decoded_options, decoded_options_count, foption);
305           else if (strcmp ((*decoded_options)[j].arg, foption->arg))
306             fatal_error (input_location,
307                          "Option %s with different values",
308                          foption->orig_option_with_args_text);
309           break;
310
311         case OPT_O:
312         case OPT_Ofast:
313         case OPT_Og:
314         case OPT_Os:
315           for (j = 0; j < *decoded_options_count; ++j)
316             if ((*decoded_options)[j].opt_index == OPT_O
317                 || (*decoded_options)[j].opt_index == OPT_Ofast
318                 || (*decoded_options)[j].opt_index == OPT_Og
319                 || (*decoded_options)[j].opt_index == OPT_Os)
320               break;
321           if (j == *decoded_options_count)
322             append_option (decoded_options, decoded_options_count, foption);
323           else if ((*decoded_options)[j].opt_index == foption->opt_index
324                    && foption->opt_index != OPT_O)
325             /* Exact same options get merged.  */
326             ;
327           else
328             {
329               /* For mismatched option kinds preserve the optimization
330                  level only, thus merge it as -On.  This also handles
331                  merging of same optimization level -On.  */
332               int level = 0;
333               switch (foption->opt_index)
334                 {
335                 case OPT_O:
336                   if (foption->arg[0] == '\0')
337                     level = MAX (level, 1);
338                   else
339                     level = MAX (level, atoi (foption->arg));
340                   break;
341                 case OPT_Ofast:
342                   level = MAX (level, 3);
343                   break;
344                 case OPT_Og:
345                   level = MAX (level, 1);
346                   break;
347                 case OPT_Os:
348                   level = MAX (level, 2);
349                   break;
350                 default:
351                   gcc_unreachable ();
352                 }
353               switch ((*decoded_options)[j].opt_index)
354                 {
355                 case OPT_O:
356                   if ((*decoded_options)[j].arg[0] == '\0')
357                     level = MAX (level, 1);
358                   else
359                     level = MAX (level, atoi ((*decoded_options)[j].arg));
360                   break;
361                 case OPT_Ofast:
362                   level = MAX (level, 3);
363                   break;
364                 case OPT_Og:
365                   level = MAX (level, 1);
366                   break;
367                 case OPT_Os:
368                   level = MAX (level, 2);
369                   break;
370                 default:
371                   gcc_unreachable ();
372                 }
373               (*decoded_options)[j].opt_index = OPT_O;
374               char *tem;
375               tem = xasprintf ("-O%d", level);
376               (*decoded_options)[j].arg = &tem[2];
377               (*decoded_options)[j].canonical_option[0] = tem;
378               (*decoded_options)[j].value = 1;
379             }
380           break;
381  
382
383         case OPT_foffload_abi_:
384           for (j = 0; j < *decoded_options_count; ++j)
385             if ((*decoded_options)[j].opt_index == foption->opt_index)
386               break;
387           if (j == *decoded_options_count)
388             append_option (decoded_options, decoded_options_count, foption);
389           else if (foption->value != (*decoded_options)[j].value)
390             fatal_error (input_location,
391                          "Option %s not used consistently in all LTO input"
392                          " files", foption->orig_option_with_args_text);
393           break;
394
395
396         case OPT_foffload_:
397           append_option (decoded_options, decoded_options_count, foption);
398           break;
399         }
400     }
401
402   /* Merge PIC options:
403       -fPIC + -fpic = -fpic
404       -fPIC + -fno-pic = -fno-pic
405       -fpic/-fPIC + nothin = nothing.  
406      It is a common mistake to mix few -fPIC compiled objects into otherwise
407      non-PIC code.  We do not want to build everything with PIC then.
408
409      It would be good to warn on mismatches, but it is bit hard to do as
410      we do not know what nothing translates to.  */
411     
412   for (unsigned int j = 0; j < *decoded_options_count;)
413     if ((*decoded_options)[j].opt_index == OPT_fPIC
414         || (*decoded_options)[j].opt_index == OPT_fpic)
415       {
416         if (!pic_option
417             || (pic_option->value > 0) != ((*decoded_options)[j].value > 0))
418           remove_option (decoded_options, j, decoded_options_count);
419         else if (pic_option->opt_index == OPT_fPIC
420                  && (*decoded_options)[j].opt_index == OPT_fpic)
421           {
422             (*decoded_options)[j] = *pic_option;
423             j++;
424           }
425         else
426           j++;
427       }
428    else if ((*decoded_options)[j].opt_index == OPT_fPIE
429             || (*decoded_options)[j].opt_index == OPT_fpie)
430       {
431         if (!pie_option
432             || pie_option->value != (*decoded_options)[j].value)
433           remove_option (decoded_options, j, decoded_options_count);
434         else if (pie_option->opt_index == OPT_fPIE
435                  && (*decoded_options)[j].opt_index == OPT_fpie)
436           {
437             (*decoded_options)[j] = *pie_option;
438             j++;
439           }
440         else
441           j++;
442       }
443    else
444      j++;
445 }
446
447 /* Auxiliary function that frees elements of PTR and PTR itself.
448    N is number of elements to be freed.  If PTR is NULL, nothing is freed.
449    If an element is NULL, subsequent elements are not freed.  */
450
451 static void **
452 free_array_of_ptrs (void **ptr, unsigned n)
453 {
454   if (!ptr)
455     return NULL;
456   for (unsigned i = 0; i < n; i++)
457     {
458       if (!ptr[i])
459         break;
460       free (ptr[i]);
461     }
462   free (ptr);
463   return NULL;
464 }
465
466 /* Parse STR, saving found tokens into PVALUES and return their number.
467    Tokens are assumed to be delimited by ':'.  If APPEND is non-null,
468    append it to every token we find.  */
469
470 static unsigned
471 parse_env_var (const char *str, char ***pvalues, const char *append)
472 {
473   const char *curval, *nextval;
474   char **values;
475   unsigned num = 1, i;
476
477   curval = strchr (str, ':');
478   while (curval)
479     {
480       num++;
481       curval = strchr (curval + 1, ':');
482     }
483
484   values = (char**) xmalloc (num * sizeof (char*));
485   curval = str;
486   nextval = strchr (curval, ':');
487   if (nextval == NULL)
488     nextval = strchr (curval, '\0');
489
490   int append_len = append ? strlen (append) : 0;
491   for (i = 0; i < num; i++)
492     {
493       int l = nextval - curval;
494       values[i] = (char*) xmalloc (l + 1 + append_len);
495       memcpy (values[i], curval, l);
496       values[i][l] = 0;
497       if (append)
498         strcat (values[i], append);
499       curval = nextval + 1;
500       nextval = strchr (curval, ':');
501       if (nextval == NULL)
502         nextval = strchr (curval, '\0');
503     }
504   *pvalues = values;
505   return num;
506 }
507
508 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK.  */
509
510 static void
511 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
512                          unsigned int count)
513 {
514   /* Append compiler driver arguments as far as they were merged.  */
515   for (unsigned int j = 1; j < count; ++j)
516     {
517       struct cl_decoded_option *option = &opts[j];
518
519       /* File options have been properly filtered by lto-opts.c.  */
520       switch (option->opt_index)
521         {
522         /* Drop arguments that we want to take from the link line.  */
523         case OPT_flto_:
524         case OPT_flto:
525         case OPT_flto_partition_:
526           continue;
527
528         default:
529           break;
530         }
531
532       /* For now do what the original LTO option code was doing - pass
533          on any CL_TARGET flag and a few selected others.  */
534       switch (option->opt_index)
535         {
536         case OPT_fdiagnostics_show_caret:
537         case OPT_fdiagnostics_show_option:
538         case OPT_fdiagnostics_show_location_:
539         case OPT_fshow_column:
540         case OPT_fPIC:
541         case OPT_fpic:
542         case OPT_fPIE:
543         case OPT_fpie:
544         case OPT_fcommon:
545         case OPT_fgnu_tm:
546         case OPT_fopenmp:
547         case OPT_fopenacc:
548         case OPT_fopenacc_dim_:
549         case OPT_fcilkplus:
550         case OPT_foffload_abi_:
551         case OPT_O:
552         case OPT_Ofast:
553         case OPT_Og:
554         case OPT_Os:
555         case OPT_fcheck_pointer_bounds:
556           break;
557
558         default:
559           if (!(cl_options[option->opt_index].flags & CL_TARGET))
560             continue;
561         }
562
563       /* Pass the option on.  */
564       for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
565         obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
566     }
567 }
568
569 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK.  */
570
571 static void
572 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
573                      unsigned int count)
574 {
575   /* Append compiler driver arguments as far as they were merged.  */
576   for (unsigned int j = 1; j < count; ++j)
577     {
578       struct cl_decoded_option *option = &opts[j];
579
580       switch (option->opt_index)
581         {
582         case OPT_fdiagnostics_color_:
583         case OPT_fdiagnostics_show_caret:
584         case OPT_fdiagnostics_show_option:
585         case OPT_fdiagnostics_show_location_:
586         case OPT_fshow_column:
587           break;
588         default:
589           continue;
590         }
591
592       /* Pass the option on.  */
593       for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
594         obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
595     }
596 }
597
598
599 /* Append linker options OPTS to ARGV_OBSTACK.  */
600
601 static void
602 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
603                        unsigned int count)
604 {
605   /* Append linker driver arguments.  Compiler options from the linker
606      driver arguments will override / merge with those from the compiler.  */
607   for (unsigned int j = 1; j < count; ++j)
608     {
609       struct cl_decoded_option *option = &opts[j];
610
611       /* Do not pass on frontend specific flags not suitable for lto.  */
612       if (!(cl_options[option->opt_index].flags
613             & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
614         continue;
615
616       switch (option->opt_index)
617         {
618         case OPT_o:
619         case OPT_flto_:
620         case OPT_flto:
621           /* We've handled these LTO options, do not pass them on.  */
622           continue;
623
624         case OPT_fopenmp:
625         case OPT_fopenacc:
626         case OPT_fcilkplus:
627           /* Ignore -fno-XXX form of these options, as otherwise
628              corresponding builtins will not be enabled.  */
629           if (option->value == 0)
630             continue;
631           break;
632
633         default:
634           break;
635         }
636
637       /* Pass the option on.  */
638       for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
639         obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
640     }
641 }
642
643 /* Extract options for TARGET offload compiler from OPTIONS and append
644    them to ARGV_OBSTACK.  */
645
646 static void
647 append_offload_options (obstack *argv_obstack, const char *target,
648                         struct cl_decoded_option *options,
649                         unsigned int options_count)
650 {
651   for (unsigned i = 0; i < options_count; i++)
652     {
653       const char *cur, *next, *opts;
654       char **argv;
655       unsigned argc;
656       struct cl_decoded_option *option = &options[i];
657
658       if (option->opt_index != OPT_foffload_)
659         continue;
660
661       /* If option argument starts with '-' then no target is specified.  That
662          means offload options are specified for all targets, so we need to
663          append them.  */
664       if (option->arg[0] == '-')
665         opts = option->arg;
666       else
667         {
668           opts = strchr (option->arg, '=');
669           /* If there are offload targets specified, but no actual options,
670              there is nothing to do here.  */
671           if (!opts)
672             continue;
673
674           cur = option->arg;
675
676           while (cur < opts)
677             {
678               next = strchr (cur, ',');
679               if (next == NULL)
680                 next = opts;
681               next = (next > opts) ? opts : next;
682
683               /* Are we looking for this offload target?  */
684               if (strlen (target) == (size_t) (next - cur)
685                   && strncmp (target, cur, next - cur) == 0)
686                 break;
687
688               /* Skip the comma or equal sign.  */
689               cur = next + 1;
690             }
691
692           if (cur >= opts)
693             continue;
694
695           opts++;
696         }
697
698       argv = buildargv (opts);
699       for (argc = 0; argv[argc]; argc++)
700         obstack_ptr_grow (argv_obstack, argv[argc]);
701     }
702 }
703
704 /* Check whether NAME can be accessed in MODE.  This is like access,
705    except that it never considers directories to be executable.  */
706
707 static int
708 access_check (const char *name, int mode)
709 {
710   if (mode == X_OK)
711     {
712       struct stat st;
713
714       if (stat (name, &st) < 0
715           || S_ISDIR (st.st_mode))
716         return -1;
717     }
718
719   return access (name, mode);
720 }
721
722 /* Prepare a target image for offload TARGET, using mkoffload tool from
723    COMPILER_PATH.  Return the name of the resultant object file.  */
724
725 static char *
726 compile_offload_image (const char *target, const char *compiler_path,
727                        unsigned in_argc, char *in_argv[],
728                        struct cl_decoded_option *compiler_opts,
729                        unsigned int compiler_opt_count,
730                        struct cl_decoded_option *linker_opts,
731                        unsigned int linker_opt_count)
732 {
733   char *filename = NULL;
734   char **argv;
735   char *suffix
736     = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
737   strcpy (suffix, "/accel/");
738   strcat (suffix, target);
739   strcat (suffix, "/mkoffload");
740
741   char **paths = NULL;
742   unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
743
744   const char *compiler = NULL;
745   for (unsigned i = 0; i < n_paths; i++)
746     if (access_check (paths[i], X_OK) == 0)
747       {
748         compiler = paths[i];
749         break;
750       }
751
752   if (compiler)
753     {
754       /* Generate temporary output file name.  */
755       filename = make_temp_file (".target.o");
756
757       struct obstack argv_obstack;
758       obstack_init (&argv_obstack);
759       obstack_ptr_grow (&argv_obstack, compiler);
760       if (save_temps)
761         obstack_ptr_grow (&argv_obstack, "-save-temps");
762       if (verbose)
763         obstack_ptr_grow (&argv_obstack, "-v");
764       obstack_ptr_grow (&argv_obstack, "-o");
765       obstack_ptr_grow (&argv_obstack, filename);
766
767       /* Append names of input object files.  */
768       for (unsigned i = 0; i < in_argc; i++)
769         obstack_ptr_grow (&argv_obstack, in_argv[i]);
770
771       /* Append options from offload_lto sections.  */
772       append_compiler_options (&argv_obstack, compiler_opts,
773                                compiler_opt_count);
774       append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
775
776       /* Append options specified by -foffload last.  In case of conflicting
777          options we expect offload compiler to choose the latest.  */
778       append_offload_options (&argv_obstack, target, compiler_opts,
779                               compiler_opt_count);
780       append_offload_options (&argv_obstack, target, linker_opts,
781                               linker_opt_count);
782
783       obstack_ptr_grow (&argv_obstack, NULL);
784       argv = XOBFINISH (&argv_obstack, char **);
785       fork_execute (argv[0], argv, true);
786       obstack_free (&argv_obstack, NULL);
787     }
788
789   free_array_of_ptrs ((void **) paths, n_paths);
790   return filename;
791 }
792
793
794 /* The main routine dealing with offloading.
795    The routine builds a target image for each offload target.  IN_ARGC and
796    IN_ARGV specify options and input object files.  As all of them could contain
797    target sections, we pass them all to target compilers.  */
798
799 static void
800 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
801                                     struct cl_decoded_option *compiler_opts,
802                                     unsigned int compiler_opt_count,
803                                     struct cl_decoded_option *linker_opts,
804                                     unsigned int linker_opt_count)
805 {
806   char **names = NULL;
807   const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
808   if (!target_names)
809     return;
810   unsigned num_targets = parse_env_var (target_names, &names, NULL);
811
812   int next_name_entry = 0;
813   const char *compiler_path = getenv ("COMPILER_PATH");
814   if (!compiler_path)
815     goto out;
816
817   /* Prepare an image for each target and save the name of the resultant object
818      file to the OFFLOAD_NAMES array.  It is terminated by a NULL entry.  */
819   offload_names = XCNEWVEC (char *, num_targets + 1);
820   for (unsigned i = 0; i < num_targets; i++)
821     {
822       /* HSA does not use LTO-like streaming and a different compiler, skip
823          it. */
824       if (strcmp (names[i], "hsa") == 0)
825         continue;
826
827       offload_names[next_name_entry]
828         = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
829                                  compiler_opts, compiler_opt_count,
830                                  linker_opts, linker_opt_count);
831       if (!offload_names[next_name_entry])
832         fatal_error (input_location,
833                      "problem with building target image for %s\n", names[i]);
834       next_name_entry++;
835     }
836
837  out:
838   free_array_of_ptrs ((void **) names, num_targets);
839 }
840
841 /* Copy a file from SRC to DEST.  */
842
843 static void
844 copy_file (const char *dest, const char *src)
845 {
846   FILE *d = fopen (dest, "wb");
847   FILE *s = fopen (src, "rb");
848   char buffer[512];
849   while (!feof (s))
850     {
851       size_t len = fread (buffer, 1, 512, s);
852       if (ferror (s) != 0)
853         fatal_error (input_location, "reading input file");
854       if (len > 0)
855         {
856           fwrite (buffer, 1, len, d);
857           if (ferror (d) != 0)
858             fatal_error (input_location, "writing output file");
859         }
860     }
861   fclose (d);
862   fclose (s);
863 }
864
865 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
866    the copy to the linker.  */
867
868 static void
869 find_crtoffloadtable (void)
870 {
871   char **paths = NULL;
872   const char *library_path = getenv ("LIBRARY_PATH");
873   if (!library_path)
874     return;
875   unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
876
877   unsigned i;
878   for (i = 0; i < n_paths; i++)
879     if (access_check (paths[i], R_OK) == 0)
880       {
881         /* The linker will delete the filename we give it, so make a copy.  */
882         char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
883         copy_file (crtoffloadtable, paths[i]);
884         printf ("%s\n", crtoffloadtable);
885         XDELETEVEC (crtoffloadtable);
886         break;
887       }
888   if (i == n_paths)
889     fatal_error (input_location,
890                  "installation error, can't find crtoffloadtable.o");
891
892   free_array_of_ptrs ((void **) paths, n_paths);
893 }
894
895 /* A subroutine of run_gcc.  Examine the open file FD for lto sections with
896    name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
897    and OPT_COUNT.  Return true if we found a matchingn section, false
898    otherwise.  COLLECT_GCC holds the value of the environment variable with
899    the same name.  */
900
901 static bool
902 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
903                         struct cl_decoded_option **opts,
904                         unsigned int *opt_count, const char *collect_gcc)
905 {
906   off_t offset, length;
907   char *data;
908   char *fopts;
909   const char *errmsg;
910   int err;
911   struct cl_decoded_option *fdecoded_options = *opts;
912   unsigned int fdecoded_options_count = *opt_count;
913
914   simple_object_read *sobj;
915   sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
916                                    &errmsg, &err);
917   if (!sobj)
918     return false;
919
920   char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
921   strcpy (secname, prefix);
922   strcat (secname, ".opts");
923   if (!simple_object_find_section (sobj, secname, &offset, &length,
924                                    &errmsg, &err))
925     {
926       simple_object_release_read (sobj);
927       return false;
928     }
929
930   lseek (fd, file_offset + offset, SEEK_SET);
931   data = (char *)xmalloc (length);
932   read (fd, data, length);
933   fopts = data;
934   do
935     {
936       struct cl_decoded_option *f2decoded_options;
937       unsigned int f2decoded_options_count;
938       get_options_from_collect_gcc_options (collect_gcc,
939                                             fopts, CL_LANG_ALL,
940                                             &f2decoded_options,
941                                             &f2decoded_options_count);
942       if (!fdecoded_options)
943        {
944          fdecoded_options = f2decoded_options;
945          fdecoded_options_count = f2decoded_options_count;
946        }
947       else
948         merge_and_complain (&fdecoded_options,
949                             &fdecoded_options_count,
950                             f2decoded_options, f2decoded_options_count);
951
952       fopts += strlen (fopts) + 1;
953     }
954   while (fopts - data < length);
955
956   free (data);
957   simple_object_release_read (sobj);
958   *opts = fdecoded_options;
959   *opt_count = fdecoded_options_count;
960   return true;
961 }
962
963 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
964
965 static void
966 run_gcc (unsigned argc, char *argv[])
967 {
968   unsigned i, j;
969   const char **new_argv;
970   const char **argv_ptr;
971   char *list_option_full = NULL;
972   const char *linker_output = NULL;
973   const char *collect_gcc, *collect_gcc_options;
974   int parallel = 0;
975   int jobserver = 0;
976   bool no_partition = false;
977   struct cl_decoded_option *fdecoded_options = NULL;
978   struct cl_decoded_option *offload_fdecoded_options = NULL;
979   unsigned int fdecoded_options_count = 0;
980   unsigned int offload_fdecoded_options_count = 0;
981   struct cl_decoded_option *decoded_options;
982   unsigned int decoded_options_count;
983   struct obstack argv_obstack;
984   int new_head_argc;
985   bool have_lto = false;
986   bool have_offload = false;
987   unsigned lto_argc = 0;
988   char **lto_argv;
989
990   /* Get the driver and options.  */
991   collect_gcc = getenv ("COLLECT_GCC");
992   if (!collect_gcc)
993     fatal_error (input_location,
994                  "environment variable COLLECT_GCC must be set");
995   collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
996   if (!collect_gcc_options)
997     fatal_error (input_location,
998                  "environment variable COLLECT_GCC_OPTIONS must be set");
999   get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
1000                                         CL_LANG_ALL,
1001                                         &decoded_options,
1002                                         &decoded_options_count);
1003
1004   /* Allocate array for input object files with LTO IL,
1005      and for possible preceding arguments.  */
1006   lto_argv = XNEWVEC (char *, argc);
1007
1008   /* Look at saved options in the IL files.  */
1009   for (i = 1; i < argc; ++i)
1010     {
1011       char *p;
1012       int fd;
1013       off_t file_offset = 0;
1014       long loffset;
1015       int consumed;
1016       char *filename = argv[i];
1017
1018       if (strncmp (argv[i], "-foffload-objects=",
1019                    sizeof ("-foffload-objects=") - 1) == 0)
1020         {
1021           have_offload = true;
1022           offload_objects_file_name
1023             = argv[i] + sizeof ("-foffload-objects=") - 1;
1024           continue;
1025         }
1026
1027       if ((p = strrchr (argv[i], '@'))
1028           && p != argv[i] 
1029           && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1030           && strlen (p) == (unsigned int) consumed)
1031         {
1032           filename = XNEWVEC (char, p - argv[i] + 1);
1033           memcpy (filename, argv[i], p - argv[i]);
1034           filename[p - argv[i]] = '\0';
1035           file_offset = (off_t) loffset;
1036         }
1037       fd = open (filename, O_RDONLY | O_BINARY);
1038       if (fd == -1)
1039         {
1040           lto_argv[lto_argc++] = argv[i];
1041           continue;
1042         }
1043
1044       if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
1045                                   &fdecoded_options, &fdecoded_options_count,
1046                                   collect_gcc))
1047         {
1048           have_lto = true;
1049           lto_argv[lto_argc++] = argv[i];
1050         }
1051       close (fd);
1052     }
1053
1054   /* Initalize the common arguments for the driver.  */
1055   obstack_init (&argv_obstack);
1056   obstack_ptr_grow (&argv_obstack, collect_gcc);
1057   obstack_ptr_grow (&argv_obstack, "-xlto");
1058   obstack_ptr_grow (&argv_obstack, "-c");
1059
1060   append_compiler_options (&argv_obstack, fdecoded_options,
1061                            fdecoded_options_count);
1062   append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
1063
1064   /* Scan linker driver arguments for things that are of relevance to us.  */
1065   for (j = 1; j < decoded_options_count; ++j)
1066     {
1067       struct cl_decoded_option *option = &decoded_options[j];
1068       switch (option->opt_index)
1069         {
1070         case OPT_o:
1071           linker_output = option->arg;
1072           break;
1073
1074         case OPT_save_temps:
1075           save_temps = 1;
1076           break;
1077
1078         case OPT_v:
1079           verbose = 1;
1080           break;
1081
1082         case OPT_flto_partition_:
1083           if (strcmp (option->arg, "none") == 0)
1084             no_partition = true;
1085           break;
1086
1087         case OPT_flto_:
1088           if (strcmp (option->arg, "jobserver") == 0)
1089             {
1090               jobserver = 1;
1091               parallel = 1;
1092             }
1093           else
1094             {
1095               parallel = atoi (option->arg);
1096               if (parallel <= 1)
1097                 parallel = 0;
1098             }
1099           /* Fallthru.  */
1100
1101         case OPT_flto:
1102           lto_mode = LTO_MODE_WHOPR;
1103           break;
1104
1105         default:
1106           break;
1107         }
1108     }
1109
1110   if (no_partition)
1111     {
1112       lto_mode = LTO_MODE_LTO;
1113       jobserver = 0;
1114       parallel = 0;
1115     }
1116
1117   if (linker_output)
1118     {
1119       char *output_dir, *base, *name;
1120       bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1121
1122       output_dir = xstrdup (linker_output);
1123       base = output_dir;
1124       for (name = base; *name; name++)
1125         if (IS_DIR_SEPARATOR (*name))
1126           base = name + 1;
1127       *base = '\0';
1128
1129       linker_output = &linker_output[base - output_dir];
1130       if (*output_dir == '\0')
1131         {
1132           static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1133           output_dir = current_dir;
1134         }
1135       if (!bit_bucket)
1136         {
1137           obstack_ptr_grow (&argv_obstack, "-dumpdir");
1138           obstack_ptr_grow (&argv_obstack, output_dir);
1139         }
1140
1141       obstack_ptr_grow (&argv_obstack, "-dumpbase");
1142     }
1143
1144   /* Remember at which point we can scrub args to re-use the commons.  */
1145   new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1146
1147   if (have_offload)
1148     {
1149       unsigned i, num_offload_files;
1150       char **offload_argv;
1151       FILE *f;
1152
1153       f = fopen (offload_objects_file_name, "r");
1154       if (f == NULL)
1155         fatal_error (input_location, "cannot open %s: %m",
1156                      offload_objects_file_name);
1157       if (fscanf (f, "%u ", &num_offload_files) != 1)
1158         fatal_error (input_location, "cannot read %s: %m",
1159                      offload_objects_file_name);
1160       offload_argv = XCNEWVEC (char *, num_offload_files);
1161
1162       /* Read names of object files with offload.  */
1163       for (i = 0; i < num_offload_files; i++)
1164         {
1165           const unsigned piece = 32;
1166           char *buf, *filename = XNEWVEC (char, piece);
1167           size_t len;
1168
1169           buf = filename;
1170 cont1:
1171           if (!fgets (buf, piece, f))
1172             break;
1173           len = strlen (filename);
1174           if (filename[len - 1] != '\n')
1175             {
1176               filename = XRESIZEVEC (char, filename, len + piece);
1177               buf = filename + len;
1178               goto cont1;
1179             }
1180           filename[len - 1] = '\0';
1181           offload_argv[i] = filename;
1182         }
1183       fclose (f);
1184       if (offload_argv[num_offload_files - 1] == NULL)
1185         fatal_error (input_location, "invalid format of %s",
1186                      offload_objects_file_name);
1187       maybe_unlink (offload_objects_file_name);
1188       offload_objects_file_name = NULL;
1189
1190       /* Look at saved offload options in files.  */
1191       for (i = 0; i < num_offload_files; i++)
1192         {
1193           char *p;
1194           long loffset;
1195           int fd, consumed;
1196           off_t file_offset = 0;
1197           char *filename = offload_argv[i];
1198
1199           if ((p = strrchr (offload_argv[i], '@'))
1200               && p != offload_argv[i]
1201               && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1202               && strlen (p) == (unsigned int) consumed)
1203             {
1204               filename = XNEWVEC (char, p - offload_argv[i] + 1);
1205               memcpy (filename, offload_argv[i], p - offload_argv[i]);
1206               filename[p - offload_argv[i]] = '\0';
1207               file_offset = (off_t) loffset;
1208             }
1209           fd = open (filename, O_RDONLY | O_BINARY);
1210           if (fd == -1)
1211             fatal_error (input_location, "cannot open %s: %m", filename);
1212           if (!find_and_merge_options (fd, file_offset,
1213                                        OFFLOAD_SECTION_NAME_PREFIX,
1214                                        &offload_fdecoded_options,
1215                                        &offload_fdecoded_options_count,
1216                                        collect_gcc))
1217             fatal_error (input_location, "cannot read %s: %m", filename);
1218           close (fd);
1219           if (filename != offload_argv[i])
1220             XDELETEVEC (filename);
1221         }
1222
1223       compile_images_for_offload_targets (num_offload_files, offload_argv,
1224                                           offload_fdecoded_options,
1225                                           offload_fdecoded_options_count,
1226                                           decoded_options,
1227                                           decoded_options_count);
1228
1229       free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1230
1231       if (offload_names)
1232         {
1233           find_crtoffloadtable ();
1234           for (i = 0; offload_names[i]; i++)
1235             printf ("%s\n", offload_names[i]);
1236           free_array_of_ptrs ((void **) offload_names, i);
1237         }
1238     }
1239
1240   /* If object files contain offload sections, but do not contain LTO sections,
1241      then there is no need to perform a link-time recompilation, i.e.
1242      lto-wrapper is used only for a compilation of offload images.  */
1243   if (have_offload && !have_lto)
1244     goto finish;
1245
1246   if (lto_mode == LTO_MODE_LTO)
1247     {
1248       flto_out = make_temp_file (".lto.o");
1249       if (linker_output)
1250         obstack_ptr_grow (&argv_obstack, linker_output);
1251       obstack_ptr_grow (&argv_obstack, "-o");
1252       obstack_ptr_grow (&argv_obstack, flto_out);
1253     }
1254   else 
1255     {
1256       const char *list_option = "-fltrans-output-list=";
1257       size_t list_option_len = strlen (list_option);
1258       char *tmp;
1259
1260       if (linker_output)
1261         {
1262           char *dumpbase = (char *) xmalloc (strlen (linker_output)
1263                                              + sizeof (".wpa") + 1);
1264           strcpy (dumpbase, linker_output);
1265           strcat (dumpbase, ".wpa");
1266           obstack_ptr_grow (&argv_obstack, dumpbase);
1267         }
1268
1269       if (linker_output && save_temps)
1270         {
1271           ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1272                                                  + sizeof (".ltrans.out") + 1);
1273           strcpy (ltrans_output_file, linker_output);
1274           strcat (ltrans_output_file, ".ltrans.out");
1275         }
1276       else
1277         ltrans_output_file = make_temp_file (".ltrans.out");
1278       list_option_full = (char *) xmalloc (sizeof (char) *
1279                          (strlen (ltrans_output_file) + list_option_len + 1));
1280       tmp = list_option_full;
1281
1282       obstack_ptr_grow (&argv_obstack, tmp);
1283       strcpy (tmp, list_option);
1284       tmp += list_option_len;
1285       strcpy (tmp, ltrans_output_file);
1286
1287       if (jobserver)
1288         obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1289       else if (parallel > 1)
1290         {
1291           char buf[256];
1292           sprintf (buf, "-fwpa=%i", parallel);
1293           obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1294         }
1295       else
1296         obstack_ptr_grow (&argv_obstack, "-fwpa");
1297     }
1298
1299   /* Append the input objects and possible preceding arguments.  */
1300   for (i = 0; i < lto_argc; ++i)
1301     obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1302   obstack_ptr_grow (&argv_obstack, NULL);
1303
1304   new_argv = XOBFINISH (&argv_obstack, const char **);
1305   argv_ptr = &new_argv[new_head_argc];
1306   fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1307
1308   if (lto_mode == LTO_MODE_LTO)
1309     {
1310       printf ("%s\n", flto_out);
1311       free (flto_out);
1312       flto_out = NULL;
1313     }
1314   else
1315     {
1316       FILE *stream = fopen (ltrans_output_file, "r");
1317       FILE *mstream = NULL;
1318       struct obstack env_obstack;
1319
1320       if (!stream)
1321         fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1322
1323       /* Parse the list of LTRANS inputs from the WPA stage.  */
1324       obstack_init (&env_obstack);
1325       nr = 0;
1326       for (;;)
1327         {
1328           const unsigned piece = 32;
1329           char *output_name = NULL;
1330           char *buf, *input_name = (char *)xmalloc (piece);
1331           size_t len;
1332
1333           buf = input_name;
1334 cont:
1335           if (!fgets (buf, piece, stream))
1336             break;
1337           len = strlen (input_name);
1338           if (input_name[len - 1] != '\n')
1339             {
1340               input_name = (char *)xrealloc (input_name, len + piece);
1341               buf = input_name + len;
1342               goto cont;
1343             }
1344           input_name[len - 1] = '\0';
1345
1346           if (input_name[0] == '*')
1347             output_name = &input_name[1];
1348
1349           nr++;
1350           input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1351           output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1352           input_names[nr-1] = input_name;
1353           output_names[nr-1] = output_name;
1354         }
1355       fclose (stream);
1356       maybe_unlink (ltrans_output_file);
1357       ltrans_output_file = NULL;
1358
1359       if (parallel)
1360         {
1361           makefile = make_temp_file (".mk");
1362           mstream = fopen (makefile, "w");
1363         }
1364
1365       /* Execute the LTRANS stage for each input file (or prepare a
1366          makefile to invoke this in parallel).  */
1367       for (i = 0; i < nr; ++i)
1368         {
1369           char *output_name;
1370           char *input_name = input_names[i];
1371           /* If it's a pass-through file do nothing.  */
1372           if (output_names[i])
1373             continue;
1374
1375           /* Replace the .o suffix with a .ltrans.o suffix and write
1376              the resulting name to the LTRANS output list.  */
1377           obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1378           obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1379           output_name = XOBFINISH (&env_obstack, char *);
1380
1381           /* Adjust the dumpbase if the linker output file was seen.  */
1382           if (linker_output)
1383             {
1384               char *dumpbase
1385                   = (char *) xmalloc (strlen (linker_output)
1386                                       + sizeof (DUMPBASE_SUFFIX) + 1);
1387               snprintf (dumpbase,
1388                         strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1389                         "%s.ltrans%u", linker_output, i);
1390               argv_ptr[0] = dumpbase;
1391             }
1392
1393           argv_ptr[1] = "-fltrans";
1394           argv_ptr[2] = "-o";
1395           argv_ptr[3] = output_name;
1396           argv_ptr[4] = input_name;
1397           argv_ptr[5] = NULL;
1398           if (parallel)
1399             {
1400               fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1401               for (j = 1; new_argv[j] != NULL; ++j)
1402                 fprintf (mstream, " '%s'", new_argv[j]);
1403               fprintf (mstream, "\n");
1404               /* If we are not preserving the ltrans input files then
1405                  truncate them as soon as we have processed it.  This
1406                  reduces temporary disk-space usage.  */
1407               if (! save_temps)
1408                 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1409                          "&& mv %s.tem %s\n",
1410                          input_name, input_name, input_name, input_name); 
1411             }
1412           else
1413             {
1414               fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1415                             true);
1416               maybe_unlink (input_name);
1417             }
1418
1419           output_names[i] = output_name;
1420         }
1421       if (parallel)
1422         {
1423           struct pex_obj *pex;
1424           char jobs[32];
1425
1426           fprintf (mstream, "all:");
1427           for (i = 0; i < nr; ++i)
1428             fprintf (mstream, " \\\n\t%s", output_names[i]);
1429           fprintf (mstream, "\n");
1430           fclose (mstream);
1431           if (!jobserver)
1432             {
1433               /* Avoid passing --jobserver-fd= and similar flags 
1434                  unless jobserver mode is explicitly enabled.  */
1435               putenv (xstrdup ("MAKEFLAGS="));
1436               putenv (xstrdup ("MFLAGS="));
1437             }
1438           new_argv[0] = getenv ("MAKE");
1439           if (!new_argv[0])
1440             new_argv[0] = "make";
1441           new_argv[1] = "-f";
1442           new_argv[2] = makefile;
1443           i = 3;
1444           if (!jobserver)
1445             {
1446               snprintf (jobs, 31, "-j%d", parallel);
1447               new_argv[i++] = jobs;
1448             }
1449           new_argv[i++] = "all";
1450           new_argv[i++] = NULL;
1451           pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1452                                  NULL, NULL, PEX_SEARCH, false);
1453           do_wait (new_argv[0], pex);
1454           maybe_unlink (makefile);
1455           makefile = NULL;
1456           for (i = 0; i < nr; ++i)
1457             maybe_unlink (input_names[i]);
1458         }
1459       for (i = 0; i < nr; ++i)
1460         {
1461           fputs (output_names[i], stdout);
1462           putc ('\n', stdout);
1463           free (input_names[i]);
1464         }
1465       nr = 0;
1466       free (output_names);
1467       free (input_names);
1468       free (list_option_full);
1469       obstack_free (&env_obstack, NULL);
1470     }
1471
1472  finish:
1473   XDELETE (lto_argv);
1474   obstack_free (&argv_obstack, NULL);
1475 }
1476
1477
1478 /* Entry point.  */
1479
1480 int
1481 main (int argc, char *argv[])
1482 {
1483   const char *p;
1484
1485   init_opts_obstack ();
1486
1487   p = argv[0] + strlen (argv[0]);
1488   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1489     --p;
1490   progname = p;
1491
1492   xmalloc_set_program_name (progname);
1493
1494   gcc_init_libintl ();
1495
1496   diagnostic_initialize (global_dc, 0);
1497
1498   if (atexit (lto_wrapper_cleanup) != 0)
1499     fatal_error (input_location, "atexit failed");
1500
1501   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1502     signal (SIGINT, fatal_signal);
1503 #ifdef SIGHUP
1504   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1505     signal (SIGHUP, fatal_signal);
1506 #endif
1507   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1508     signal (SIGTERM, fatal_signal);
1509 #ifdef SIGPIPE
1510   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1511     signal (SIGPIPE, fatal_signal);
1512 #endif
1513 #ifdef SIGCHLD
1514   /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1515      receive the signal.  A different setting is inheritable */
1516   signal (SIGCHLD, SIG_DFL);
1517 #endif
1518
1519   /* We may be called with all the arguments stored in some file and
1520      passed with @file.  Expand them into argv before processing.  */
1521   expandargv (&argc, &argv);
1522
1523   run_gcc (argc, argv);
1524
1525   return 0;
1526 }