OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto.  Used by collect2 and the linker plugin.
2    Copyright (C) 2009, 2010 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
50 /* From lto-streamer.h which we cannot include with -fkeep-inline-functions.
51    ???  Split out a lto-streamer-core.h.  */
52
53 #define LTO_SECTION_NAME_PREFIX         ".gnu.lto_"
54
55 /* End of lto-streamer.h copy.  */
56
57 int debug;                              /* true if -save-temps.  */
58 int verbose;                            /* true if -v.  */
59
60 enum lto_mode_d {
61   LTO_MODE_NONE,                        /* Not doing LTO.  */
62   LTO_MODE_LTO,                         /* Normal LTO.  */
63   LTO_MODE_WHOPR                        /* WHOPR.  */
64 };
65
66 /* Current LTO mode.  */
67 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
68
69 static char *ltrans_output_file;
70 static char *flto_out;
71 static char *args_name;
72 static unsigned int nr;
73 static char **input_names;
74 static char **output_names;
75 static char *makefile;
76
77 static void maybe_unlink_file (const char *);
78
79  /* Delete tempfiles.  */
80
81 static void
82 lto_wrapper_cleanup (void)
83 {
84   static bool cleanup_done = false;
85   unsigned int i;
86
87   if (cleanup_done)
88     return;
89
90   /* Setting cleanup_done prevents an infinite loop if one of the
91      calls to maybe_unlink_file fails. */
92   cleanup_done = true;
93
94   if (ltrans_output_file)
95     maybe_unlink_file (ltrans_output_file);
96   if (flto_out)
97     maybe_unlink_file (flto_out);
98   if (args_name)
99     maybe_unlink_file (args_name);
100   if (makefile)
101     maybe_unlink_file (makefile);
102   for (i = 0; i < nr; ++i)
103     {
104       maybe_unlink_file (input_names[i]);
105       if (output_names[i])
106         maybe_unlink_file (output_names[i]);
107     }
108 }
109
110 static void
111 fatal_signal (int signum)
112 {
113   signal (signum, SIG_DFL);
114   lto_wrapper_cleanup ();
115   /* Get the same signal again, this time not handled,
116      so its normal effect occurs.  */
117   kill (getpid (), signum);
118 }
119
120 /* Just die. CMSGID is the error message. */
121
122 static void __attribute__ ((format (printf, 1, 2)))
123 fatal (const char * cmsgid, ...)
124 {
125   va_list ap;
126
127   va_start (ap, cmsgid);
128   fprintf (stderr, "lto-wrapper: ");
129   vfprintf (stderr, _(cmsgid), ap);
130   fprintf (stderr, "\n");
131   va_end (ap);
132
133   lto_wrapper_cleanup ();
134   exit (FATAL_EXIT_CODE);
135 }
136
137
138 /* Die when sys call fails. CMSGID is the error message.  */
139
140 static void __attribute__ ((format (printf, 1, 2)))
141 fatal_perror (const char *cmsgid, ...)
142 {
143   int e = errno;
144   va_list ap;
145
146   va_start (ap, cmsgid);
147   fprintf (stderr, "lto-wrapper: ");
148   vfprintf (stderr, _(cmsgid), ap);
149   fprintf (stderr, ": %s\n", xstrerror (e));
150   va_end (ap);
151
152   lto_wrapper_cleanup ();
153   exit (FATAL_EXIT_CODE);
154 }
155
156
157 /* Execute a program, and wait for the reply. ARGV are the arguments. The
158    last one must be NULL. */
159
160 static struct pex_obj *
161 collect_execute (char **argv)
162 {
163   struct pex_obj *pex;
164   const char *errmsg;
165   int err;
166
167   if (verbose)
168     {
169       char **p_argv;
170       const char *str;
171
172       for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
173         fprintf (stderr, " %s", str);
174
175       fprintf (stderr, "\n");
176     }
177
178   fflush (stdout);
179   fflush (stderr);
180
181   pex = pex_init (0, "lto-wrapper", NULL);
182   if (pex == NULL)
183     fatal_perror ("pex_init failed");
184
185   /* Do not use PEX_LAST here, we use our stdout for communicating with
186      collect2 or the linker-plugin.  Any output from the sub-process
187      will confuse that.  */
188   errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
189                     NULL, &err);
190   if (errmsg != NULL)
191     {
192       if (err != 0)
193         {
194           errno = err;
195           fatal_perror (errmsg);
196         }
197       else
198         fatal (errmsg);
199     }
200
201   return pex;
202 }
203
204
205 /* Wait for a process to finish, and exit if a nonzero status is found.
206    PROG is the program name. PEX is the process we should wait for. */
207
208 static int
209 collect_wait (const char *prog, struct pex_obj *pex)
210 {
211   int status;
212
213   if (!pex_get_status (pex, 1, &status))
214     fatal_perror ("can't get program status");
215   pex_free (pex);
216
217   if (status)
218     {
219       if (WIFSIGNALED (status))
220         {
221           int sig = WTERMSIG (status);
222           if (WCOREDUMP (status))
223             fatal ("%s terminated with signal %d [%s], core dumped",
224                    prog, sig, strsignal (sig));
225           else
226             fatal ("%s terminated with signal %d [%s]",
227                    prog, sig, strsignal (sig));
228         }
229
230       if (WIFEXITED (status))
231         fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
232     }
233
234   return 0;
235 }
236
237
238 /* Unlink a temporary LTRANS file unless requested otherwise.  */
239
240 static void
241 maybe_unlink_file (const char *file)
242 {
243   if (! debug)
244     {
245       if (unlink_if_ordinary (file)
246           && errno != ENOENT)
247         fatal_perror ("deleting LTRANS file %s", file);
248     }
249   else
250     fprintf (stderr, "[Leaving LTRANS %s]\n", file);
251 }
252
253
254 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish.  */
255
256 static void
257 fork_execute (char **argv)
258 {
259   struct pex_obj *pex;
260   char *new_argv[3];
261   char *at_args;
262   FILE *args;
263   int status;
264
265   args_name = make_temp_file (".args");
266   at_args = concat ("@", args_name, NULL);
267   args = fopen (args_name, "w");
268   if (args == NULL)
269     fatal ("failed to open %s", args_name);
270
271   status = writeargv (&argv[1], args);
272
273   if (status)
274     fatal ("could not write to temporary file %s",  args_name);
275
276   fclose (args);
277
278   new_argv[0] = argv[0];
279   new_argv[1] = at_args;
280   new_argv[2] = NULL;
281
282   pex = collect_execute (new_argv);
283   collect_wait (new_argv[0], pex);
284
285   maybe_unlink_file (args_name);
286   args_name = NULL;
287   free (at_args);
288 }
289
290 /* Template of LTRANS dumpbase suffix.  */
291 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
292
293 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
294    environment according to LANG_MASK.  */
295
296 static void
297 get_options_from_collect_gcc_options (const char *collect_gcc,
298                                       const char *collect_gcc_options,
299                                       unsigned int lang_mask,
300                                       struct cl_decoded_option **decoded_options,
301                                       unsigned int *decoded_options_count)
302 {
303   struct obstack argv_obstack;
304   char *argv_storage;
305   const char **argv;
306   int j, k, argc;
307
308   argv_storage = xstrdup (collect_gcc_options);
309   obstack_init (&argv_obstack);
310   obstack_ptr_grow (&argv_obstack, collect_gcc);
311
312   for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
313     {
314       if (argv_storage[j] == '\'')
315         {
316           obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
317           ++j;
318           do
319             {
320               if (argv_storage[j] == '\0')
321                 fatal ("malformed COLLECT_GCC_OPTIONS");
322               else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
323                 {
324                   argv_storage[k++] = '\'';
325                   j += 4;
326                 }
327               else if (argv_storage[j] == '\'')
328                 break;
329               else
330                 argv_storage[k++] = argv_storage[j++];
331             }
332           while (1);
333           argv_storage[k++] = '\0';
334         }
335     }
336
337   obstack_ptr_grow (&argv_obstack, NULL);
338   argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
339   argv = XOBFINISH (&argv_obstack, const char **);
340
341   decode_cmdline_options_to_array (argc, (const char **)argv,
342                                    lang_mask,
343                                    decoded_options, decoded_options_count);
344   obstack_free (&argv_obstack, NULL);
345 }
346
347 /* Append OPTION to the options array DECODED_OPTIONS with size
348    DECODED_OPTIONS_COUNT.  */
349
350 static void
351 append_option (struct cl_decoded_option **decoded_options,
352                unsigned int *decoded_options_count,
353                struct cl_decoded_option *option)
354 {
355   ++*decoded_options_count;
356   *decoded_options
357     = (struct cl_decoded_option *)
358         xrealloc (*decoded_options,
359                   (*decoded_options_count
360                    * sizeof (struct cl_decoded_option)));
361   memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
362           sizeof (struct cl_decoded_option));
363 }
364
365 /* Try to merge and complain about options FDECODED_OPTIONS when applied
366    ontop of DECODED_OPTIONS.  */
367
368 static void
369 merge_and_complain (struct cl_decoded_option **decoded_options,
370                     unsigned int *decoded_options_count,
371                     struct cl_decoded_option *fdecoded_options,
372                     unsigned int fdecoded_options_count)
373 {
374   unsigned int i, j;
375
376   /* ???  Merge options from files.  Most cases can be
377      handled by either unioning or intersecting
378      (for example -fwrapv is a case for unioning,
379      -ffast-math is for intersection).  Most complaints
380      about real conflicts between different options can
381      be deferred to the compiler proper.  Options that
382      we can neither safely handle by intersection nor
383      unioning would need to be complained about here.
384      Ideally we'd have a flag in the opt files that
385      tells whether to union or intersect or reject.
386      In absence of that it's unclear what a good default is.
387      It's also difficult to get positional handling correct.  */
388
389   /* The following does what the old LTO option code did,
390      union all target and a selected set of common options.  */
391   for (i = 0; i < fdecoded_options_count; ++i)
392     {
393       struct cl_decoded_option *foption = &fdecoded_options[i];
394       switch (foption->opt_index)
395         {
396         case OPT_SPECIAL_unknown:
397         case OPT_SPECIAL_ignore:
398         case OPT_SPECIAL_program_name:
399         case OPT_SPECIAL_input_file:
400           break;
401
402         default:
403           if (!(cl_options[foption->opt_index].flags & CL_TARGET))
404             break;
405
406           /* Fallthru.  */
407         case OPT_fPIC:
408         case OPT_fpic:
409         case OPT_fpie:
410         case OPT_fcommon:
411         case OPT_fexceptions:
412         case OPT_fgnu_tm:
413           /* Do what the old LTO code did - collect exactly one option
414              setting per OPT code, we pick the first we encounter.
415              ???  This doesn't make too much sense, but when it doesn't
416              then we should complain.  */
417           for (j = 0; j < *decoded_options_count; ++j)
418             if ((*decoded_options)[j].opt_index == foption->opt_index)
419               break;
420           if (j == *decoded_options_count)
421             append_option (decoded_options, decoded_options_count, foption);
422           break;
423         }
424     }
425 }
426
427 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
428
429 static void
430 run_gcc (unsigned argc, char *argv[])
431 {
432   unsigned i, j;
433   const char **new_argv;
434   const char **argv_ptr;
435   char *list_option_full = NULL;
436   const char *linker_output = NULL;
437   const char *collect_gcc, *collect_gcc_options;
438   int parallel = 0;
439   int jobserver = 0;
440   bool no_partition = false;
441   struct cl_decoded_option *fdecoded_options = NULL;
442   unsigned int fdecoded_options_count = 0;
443   struct cl_decoded_option *decoded_options;
444   unsigned int decoded_options_count;
445   struct obstack argv_obstack;
446   int new_head_argc;
447
448   /* Get the driver and options.  */
449   collect_gcc = getenv ("COLLECT_GCC");
450   if (!collect_gcc)
451     fatal ("environment variable COLLECT_GCC must be set");
452   collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
453   if (!collect_gcc_options)
454     fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
455   get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
456                                         CL_LANG_ALL,
457                                         &decoded_options,
458                                         &decoded_options_count);
459
460   /* Look at saved options in the IL files.  */
461   for (i = 1; i < argc; ++i)
462     {
463       char *data, *p;
464       char *fopts;
465       int fd;
466       const char *errmsg;
467       int err;
468       off_t file_offset = 0, offset, length;
469       long loffset;
470       simple_object_read *sobj;
471       int consumed;
472       struct cl_decoded_option *f2decoded_options;
473       unsigned int f2decoded_options_count;
474       char *filename = argv[i];
475       if ((p = strrchr (argv[i], '@'))
476           && p != argv[i] 
477           && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
478           && strlen (p) == (unsigned int) consumed)
479         {
480           filename = XNEWVEC (char, p - argv[i] + 1);
481           memcpy (filename, argv[i], p - argv[i]);
482           filename[p - argv[i]] = '\0';
483           file_offset = (off_t) loffset;
484         }
485       fd = open (argv[i], O_RDONLY);
486       if (fd == -1)
487         continue;
488       sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO", 
489                                        &errmsg, &err);
490       if (!sobj)
491         {
492           close (fd);
493           continue;
494         }
495       if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
496                                        &offset, &length, &errmsg, &err))
497         {
498           simple_object_release_read (sobj);
499           close (fd);
500           continue;
501         }
502       lseek (fd, file_offset + offset, SEEK_SET);
503       data = (char *)xmalloc (length);
504       read (fd, data, length);
505       fopts = data;
506       do
507         {
508           get_options_from_collect_gcc_options (collect_gcc,
509                                                 fopts, CL_LANG_ALL,
510                                                 &f2decoded_options,
511                                                 &f2decoded_options_count);
512           if (!fdecoded_options)
513             {
514               fdecoded_options = f2decoded_options;
515               fdecoded_options_count = f2decoded_options_count;
516             }
517           else
518             merge_and_complain (&fdecoded_options,
519                                 &fdecoded_options_count,
520                                 f2decoded_options, f2decoded_options_count);
521
522           fopts += strlen (fopts) + 1;
523         }
524       while (fopts - data < length);
525
526       free (data);
527       simple_object_release_read (sobj);
528       close (fd);
529     }
530
531   /* Initalize the common arguments for the driver.  */
532   obstack_init (&argv_obstack);
533   obstack_ptr_grow (&argv_obstack, collect_gcc);
534   obstack_ptr_grow (&argv_obstack, "-xlto");
535   obstack_ptr_grow (&argv_obstack, "-c");
536
537   /* Append compiler driver arguments as far as they were merged.  */
538   for (j = 1; j < fdecoded_options_count; ++j)
539     {
540       struct cl_decoded_option *option = &fdecoded_options[j];
541
542       /* File options have been properly filtered by lto-opts.c.  */
543       switch (option->opt_index)
544         {
545           /* Drop arguments that we want to take from the link line.  */
546           case OPT_flto_:
547           case OPT_flto:
548           case OPT_flto_partition_none:
549           case OPT_flto_partition_1to1:
550           case OPT_flto_partition_balanced:
551               continue;
552
553           default:
554               break;
555         }
556
557       /* For now do what the original LTO option code was doing - pass
558          on any CL_TARGET flag and a few selected others.  */
559       switch (option->opt_index)
560         {
561         case OPT_fPIC:
562         case OPT_fpic:
563         case OPT_fpie:
564         case OPT_fcommon:
565         case OPT_fexceptions:
566         case OPT_fgnu_tm:
567           break;
568
569         default:
570           if (!(cl_options[option->opt_index].flags & CL_TARGET))
571             continue;
572         }
573
574       /* Pass the option on.  */
575       for (i = 0; i < option->canonical_option_num_elements; ++i)
576         obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
577     }
578
579   /* Append linker driver arguments.  Compiler options from the linker
580      driver arguments will override / merge with those from the compiler.  */
581   for (j = 1; j < decoded_options_count; ++j)
582     {
583       struct cl_decoded_option *option = &decoded_options[j];
584
585       /* Do not pass on frontend specific flags not suitable for lto.  */
586       if (!(cl_options[option->opt_index].flags
587             & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
588         continue;
589
590       switch (option->opt_index)
591         {
592         case OPT_o:
593           linker_output = option->arg;
594           /* We generate new intermediate output, drop this arg.  */
595           continue;
596
597         case OPT_save_temps:
598           debug = 1;
599           break;
600
601         case OPT_v:
602           verbose = 1;
603           break;
604
605         case OPT_flto_partition_none:
606           no_partition = true;
607           break;
608
609         case OPT_flto_:
610           if (strcmp (option->arg, "jobserver") == 0)
611             {
612               jobserver = 1;
613               parallel = 1;
614             }
615           else
616             {
617               parallel = atoi (option->arg);
618               if (parallel <= 1)
619                 parallel = 0;
620             }
621           /* Fallthru.  */
622
623         case OPT_flto:
624           lto_mode = LTO_MODE_WHOPR;
625           /* We've handled these LTO options, do not pass them on.  */
626           continue;
627
628         default:
629           break;
630         }
631
632       /* Pass the option on.  */
633       for (i = 0; i < option->canonical_option_num_elements; ++i)
634         obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
635     }
636
637   if (no_partition)
638     {
639       lto_mode = LTO_MODE_LTO;
640       jobserver = 0;
641       parallel = 0;
642     }
643
644   if (linker_output)
645     {
646       char *output_dir, *base, *name;
647       bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
648
649       output_dir = xstrdup (linker_output);
650       base = output_dir;
651       for (name = base; *name; name++)
652         if (IS_DIR_SEPARATOR (*name))
653           base = name + 1;
654       *base = '\0';
655
656       linker_output = &linker_output[base - output_dir];
657       if (*output_dir == '\0')
658         {
659           static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
660           output_dir = current_dir;
661         }
662       if (!bit_bucket)
663         {
664           obstack_ptr_grow (&argv_obstack, "-dumpdir");
665           obstack_ptr_grow (&argv_obstack, output_dir);
666         }
667
668       obstack_ptr_grow (&argv_obstack, "-dumpbase");
669     }
670
671   /* Remember at which point we can scrub args to re-use the commons.  */
672   new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
673
674   if (lto_mode == LTO_MODE_LTO)
675     {
676       flto_out = make_temp_file (".lto.o");
677       if (linker_output)
678         obstack_ptr_grow (&argv_obstack, linker_output);
679       obstack_ptr_grow (&argv_obstack, "-o");
680       obstack_ptr_grow (&argv_obstack, flto_out);
681     }
682   else 
683     {
684       const char *list_option = "-fltrans-output-list=";
685       size_t list_option_len = strlen (list_option);
686       char *tmp;
687
688       if (linker_output)
689         {
690           char *dumpbase = (char *) xmalloc (strlen (linker_output)
691                                              + sizeof (".wpa") + 1);
692           strcpy (dumpbase, linker_output);
693           strcat (dumpbase, ".wpa");
694           obstack_ptr_grow (&argv_obstack, dumpbase);
695         }
696
697       if (linker_output && debug)
698         {
699           ltrans_output_file = (char *) xmalloc (strlen (linker_output)
700                                                  + sizeof (".ltrans.out") + 1);
701           strcpy (ltrans_output_file, linker_output);
702           strcat (ltrans_output_file, ".ltrans.out");
703         }
704       else
705         ltrans_output_file = make_temp_file (".ltrans.out");
706       list_option_full = (char *) xmalloc (sizeof (char) *
707                          (strlen (ltrans_output_file) + list_option_len + 1));
708       tmp = list_option_full;
709
710       obstack_ptr_grow (&argv_obstack, tmp);
711       strcpy (tmp, list_option);
712       tmp += list_option_len;
713       strcpy (tmp, ltrans_output_file);
714
715       obstack_ptr_grow (&argv_obstack, "-fwpa");
716     }
717
718   /* Append the input objects and possible preceeding arguments.  */
719   for (i = 1; i < argc; ++i)
720     obstack_ptr_grow (&argv_obstack, argv[i]);
721   obstack_ptr_grow (&argv_obstack, NULL);
722
723   new_argv = XOBFINISH (&argv_obstack, const char **);
724   argv_ptr = &new_argv[new_head_argc];
725   fork_execute (CONST_CAST (char **, new_argv));
726
727   if (lto_mode == LTO_MODE_LTO)
728     {
729       printf("%s\n", flto_out);
730       free (flto_out);
731       flto_out = NULL;
732     }
733   else
734     {
735       FILE *stream = fopen (ltrans_output_file, "r");
736       FILE *mstream = NULL;
737       struct obstack env_obstack;
738
739       if (!stream)
740         fatal_perror ("fopen: %s", ltrans_output_file);
741
742       /* Parse the list of LTRANS inputs from the WPA stage.  */
743       obstack_init (&env_obstack);
744       nr = 0;
745       for (;;)
746         {
747           const unsigned piece = 32;
748           char *output_name = NULL;
749           char *buf, *input_name = (char *)xmalloc (piece);
750           size_t len;
751
752           buf = input_name;
753 cont:
754           if (!fgets (buf, piece, stream))
755             break;
756           len = strlen (input_name);
757           if (input_name[len - 1] != '\n')
758             {
759               input_name = (char *)xrealloc (input_name, len + piece);
760               buf = input_name + len;
761               goto cont;
762             }
763           input_name[len - 1] = '\0';
764
765           if (input_name[0] == '*')
766             output_name = &input_name[1];
767
768           nr++;
769           input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
770           output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
771           input_names[nr-1] = input_name;
772           output_names[nr-1] = output_name;
773         }
774       fclose (stream);
775       maybe_unlink_file (ltrans_output_file);
776       ltrans_output_file = NULL;
777
778       if (parallel)
779         {
780           makefile = make_temp_file (".mk");
781           mstream = fopen (makefile, "w");
782         }
783
784       /* Execute the LTRANS stage for each input file (or prepare a
785          makefile to invoke this in parallel).  */
786       for (i = 0; i < nr; ++i)
787         {
788           char *output_name;
789           char *input_name = input_names[i];
790           /* If it's a pass-through file do nothing.  */
791           if (output_names[i])
792             continue;
793
794           /* Replace the .o suffix with a .ltrans.o suffix and write
795              the resulting name to the LTRANS output list.  */
796           obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
797           obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
798           output_name = XOBFINISH (&env_obstack, char *);
799
800           /* Adjust the dumpbase if the linker output file was seen.  */
801           if (linker_output)
802             {
803               char *dumpbase
804                   = (char *) xmalloc (strlen (linker_output)
805                                       + sizeof(DUMPBASE_SUFFIX) + 1);
806               snprintf (dumpbase,
807                         strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
808                         "%s.ltrans%u", linker_output, i);
809               argv_ptr[0] = dumpbase;
810             }
811
812           argv_ptr[1] = "-fltrans";
813           argv_ptr[2] = "-o";
814           argv_ptr[3] = output_name;
815           argv_ptr[4] = input_name;
816           argv_ptr[5] = NULL;
817           if (parallel)
818             {
819               fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
820               for (j = 1; new_argv[j] != NULL; ++j)
821                 fprintf (mstream, " '%s'", new_argv[j]);
822               fprintf (mstream, "\n");
823               /* If we are not preserving the ltrans input files then
824                  truncate them as soon as we have processed it.  This
825                  reduces temporary disk-space usage.  */
826               if (! debug)
827                 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
828                          "&& mv %s.tem %s\n",
829                          input_name, input_name, input_name, input_name); 
830             }
831           else
832             {
833               fork_execute (CONST_CAST (char **, new_argv));
834               maybe_unlink_file (input_name);
835             }
836
837           output_names[i] = output_name;
838         }
839       if (parallel)
840         {
841           struct pex_obj *pex;
842           char jobs[32];
843
844           fprintf (mstream, "all:");
845           for (i = 0; i < nr; ++i)
846             fprintf (mstream, " \\\n\t%s", output_names[i]);
847           fprintf (mstream, "\n");
848           fclose (mstream);
849           if (!jobserver)
850             {
851               /* Avoid passing --jobserver-fd= and similar flags 
852                  unless jobserver mode is explicitly enabled.  */
853               putenv (xstrdup ("MAKEFLAGS="));
854               putenv (xstrdup ("MFLAGS="));
855             }
856           new_argv[0] = getenv ("MAKE");
857           if (!new_argv[0])
858             new_argv[0] = "make";
859           new_argv[1] = "-f";
860           new_argv[2] = makefile;
861           i = 3;
862           if (!jobserver)
863             {
864               snprintf (jobs, 31, "-j%d", parallel);
865               new_argv[i++] = jobs;
866             }
867           new_argv[i++] = "all";
868           new_argv[i++] = NULL;
869           pex = collect_execute (CONST_CAST (char **, new_argv));
870           collect_wait (new_argv[0], pex);
871           maybe_unlink_file (makefile);
872           makefile = NULL;
873           for (i = 0; i < nr; ++i)
874             maybe_unlink_file (input_names[i]);
875         }
876       for (i = 0; i < nr; ++i)
877         {
878           fputs (output_names[i], stdout);
879           putc ('\n', stdout);
880           free (input_names[i]);
881         }
882       nr = 0;
883       free (output_names);
884       free (input_names);
885       free (list_option_full);
886       obstack_free (&env_obstack, NULL);
887     }
888
889   obstack_free (&argv_obstack, NULL);
890 }
891
892
893 /* Entry point.  */
894
895 int
896 main (int argc, char *argv[])
897 {
898   const char *p;
899
900   p = argv[0] + strlen (argv[0]);
901   while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
902     --p;
903   progname = p;
904
905   xmalloc_set_program_name (progname);
906
907   gcc_init_libintl ();
908
909   diagnostic_initialize (global_dc, 0);
910
911   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
912     signal (SIGINT, fatal_signal);
913 #ifdef SIGHUP
914   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
915     signal (SIGHUP, fatal_signal);
916 #endif
917   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
918     signal (SIGTERM, fatal_signal);
919 #ifdef SIGPIPE
920   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
921     signal (SIGPIPE, fatal_signal);
922 #endif
923 #ifdef SIGCHLD
924   /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
925      receive the signal.  A different setting is inheritable */
926   signal (SIGCHLD, SIG_DFL);
927 #endif
928
929   /* We may be called with all the arguments stored in some file and
930      passed with @file.  Expand them into argv before processing.  */
931   expandargv (&argc, &argv);
932
933   run_gcc (argc, argv);
934
935   return 0;
936 }