PR target/60039
[official-gcc.git] / gcc / lto-wrapper.c
blob8e9a566713c9ec50183d24c369b914f669866adf
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #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"
50 /* From lto-streamer.h which we cannot include with -fkeep-inline-functions.
51 ??? Split out a lto-streamer-core.h. */
53 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
55 /* End of lto-streamer.h copy. */
57 int debug; /* true if -save-temps. */
58 int verbose; /* true if -v. */
60 enum lto_mode_d {
61 LTO_MODE_NONE, /* Not doing LTO. */
62 LTO_MODE_LTO, /* Normal LTO. */
63 LTO_MODE_WHOPR /* WHOPR. */
66 /* Current LTO mode. */
67 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
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;
77 static void maybe_unlink_file (const char *);
79 /* Delete tempfiles. */
81 static void
82 lto_wrapper_cleanup (void)
84 static bool cleanup_done = false;
85 unsigned int i;
87 if (cleanup_done)
88 return;
90 /* Setting cleanup_done prevents an infinite loop if one of the
91 calls to maybe_unlink_file fails. */
92 cleanup_done = true;
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)
104 maybe_unlink_file (input_names[i]);
105 if (output_names[i])
106 maybe_unlink_file (output_names[i]);
110 static void
111 fatal_signal (int signum)
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);
120 /* Just die. CMSGID is the error message. */
122 static void __attribute__ ((format (printf, 1, 2)))
123 fatal (const char * cmsgid, ...)
125 va_list ap;
127 va_start (ap, cmsgid);
128 fprintf (stderr, "lto-wrapper: ");
129 vfprintf (stderr, _(cmsgid), ap);
130 fprintf (stderr, "\n");
131 va_end (ap);
133 lto_wrapper_cleanup ();
134 exit (FATAL_EXIT_CODE);
138 /* Die when sys call fails. CMSGID is the error message. */
140 static void __attribute__ ((format (printf, 1, 2)))
141 fatal_perror (const char *cmsgid, ...)
143 int e = errno;
144 va_list ap;
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);
152 lto_wrapper_cleanup ();
153 exit (FATAL_EXIT_CODE);
157 /* Execute a program, and wait for the reply. ARGV are the arguments. The
158 last one must be NULL. */
160 static struct pex_obj *
161 collect_execute (char **argv)
163 struct pex_obj *pex;
164 const char *errmsg;
165 int err;
167 if (verbose)
169 char **p_argv;
170 const char *str;
172 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
173 fprintf (stderr, " %s", str);
175 fprintf (stderr, "\n");
178 fflush (stdout);
179 fflush (stderr);
181 pex = pex_init (0, "lto-wrapper", NULL);
182 if (pex == NULL)
183 fatal_perror ("pex_init failed");
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)
192 if (err != 0)
194 errno = err;
195 fatal_perror (errmsg);
197 else
198 fatal (errmsg);
201 return pex;
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. */
208 static int
209 collect_wait (const char *prog, struct pex_obj *pex)
211 int status;
213 if (!pex_get_status (pex, 1, &status))
214 fatal_perror ("can't get program status");
215 pex_free (pex);
217 if (status)
219 if (WIFSIGNALED (status))
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));
230 if (WIFEXITED (status))
231 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
234 return 0;
238 /* Unlink a temporary LTRANS file unless requested otherwise. */
240 static void
241 maybe_unlink_file (const char *file)
243 if (! debug)
245 if (unlink_if_ordinary (file)
246 && errno != ENOENT)
247 fatal_perror ("deleting LTRANS file %s", file);
249 else if (verbose)
250 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
254 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
256 static void
257 fork_execute (char **argv)
259 struct pex_obj *pex;
260 char *new_argv[3];
261 char *at_args;
262 FILE *args;
263 int status;
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);
271 status = writeargv (&argv[1], args);
273 if (status)
274 fatal ("could not write to temporary file %s", args_name);
276 fclose (args);
278 new_argv[0] = argv[0];
279 new_argv[1] = at_args;
280 new_argv[2] = NULL;
282 pex = collect_execute (new_argv);
283 collect_wait (new_argv[0], pex);
285 maybe_unlink_file (args_name);
286 args_name = NULL;
287 free (at_args);
290 /* Template of LTRANS dumpbase suffix. */
291 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
293 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
294 environment according to LANG_MASK. */
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)
303 struct obstack argv_obstack;
304 char *argv_storage;
305 const char **argv;
306 int j, k, argc;
308 argv_storage = xstrdup (collect_gcc_options);
309 obstack_init (&argv_obstack);
310 obstack_ptr_grow (&argv_obstack, collect_gcc);
312 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
314 if (argv_storage[j] == '\'')
316 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
317 ++j;
320 if (argv_storage[j] == '\0')
321 fatal ("malformed COLLECT_GCC_OPTIONS");
322 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
324 argv_storage[k++] = '\'';
325 j += 4;
327 else if (argv_storage[j] == '\'')
328 break;
329 else
330 argv_storage[k++] = argv_storage[j++];
332 while (1);
333 argv_storage[k++] = '\0';
337 obstack_ptr_grow (&argv_obstack, NULL);
338 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
339 argv = XOBFINISH (&argv_obstack, const char **);
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);
347 /* Append OPTION to the options array DECODED_OPTIONS with size
348 DECODED_OPTIONS_COUNT. */
350 static void
351 append_option (struct cl_decoded_option **decoded_options,
352 unsigned int *decoded_options_count,
353 struct cl_decoded_option *option)
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));
365 /* Try to merge and complain about options FDECODED_OPTIONS when applied
366 ontop of DECODED_OPTIONS. */
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)
374 unsigned int i, j;
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. */
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)
393 struct cl_decoded_option *foption = &fdecoded_options[i];
394 switch (foption->opt_index)
396 case OPT_SPECIAL_unknown:
397 case OPT_SPECIAL_ignore:
398 case OPT_SPECIAL_program_name:
399 case OPT_SPECIAL_input_file:
400 break;
402 default:
403 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
404 break;
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_fnon_call_exceptions:
413 case OPT_fgnu_tm:
414 /* Do what the old LTO code did - collect exactly one option
415 setting per OPT code, we pick the first we encounter.
416 ??? This doesn't make too much sense, but when it doesn't
417 then we should complain. */
418 for (j = 0; j < *decoded_options_count; ++j)
419 if ((*decoded_options)[j].opt_index == foption->opt_index)
420 break;
421 if (j == *decoded_options_count)
422 append_option (decoded_options, decoded_options_count, foption);
423 break;
425 case OPT_ftrapv:
426 case OPT_fstrict_overflow:
427 case OPT_ffp_contract_:
428 /* For selected options we can merge conservatively. */
429 for (j = 0; j < *decoded_options_count; ++j)
430 if ((*decoded_options)[j].opt_index == foption->opt_index)
431 break;
432 if (j == *decoded_options_count)
433 append_option (decoded_options, decoded_options_count, foption);
434 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
435 -fno-trapv < -ftrapv,
436 -fno-strict-overflow < -fstrict-overflow */
437 else if (foption->value < (*decoded_options)[j].value)
438 (*decoded_options)[j] = *foption;
439 break;
441 case OPT_fwrapv:
442 /* For selected options we can merge conservatively. */
443 for (j = 0; j < *decoded_options_count; ++j)
444 if ((*decoded_options)[j].opt_index == foption->opt_index)
445 break;
446 if (j == *decoded_options_count)
447 append_option (decoded_options, decoded_options_count, foption);
448 /* -fwrapv > -fno-wrapv. */
449 else if (foption->value > (*decoded_options)[j].value)
450 (*decoded_options)[j] = *foption;
451 break;
453 case OPT_freg_struct_return:
454 case OPT_fpcc_struct_return:
455 case OPT_fshort_double:
456 for (j = 0; j < *decoded_options_count; ++j)
457 if ((*decoded_options)[j].opt_index == foption->opt_index)
458 break;
459 if (j == *decoded_options_count)
460 fatal ("Option %s not used consistently in all LTO input files",
461 foption->orig_option_with_args_text);
462 break;
464 case OPT_O:
465 case OPT_Ofast:
466 case OPT_Og:
467 case OPT_Os:
468 for (j = 0; j < *decoded_options_count; ++j)
469 if ((*decoded_options)[j].opt_index == OPT_O
470 || (*decoded_options)[j].opt_index == OPT_Ofast
471 || (*decoded_options)[j].opt_index == OPT_Og
472 || (*decoded_options)[j].opt_index == OPT_Os)
473 break;
474 if (j == *decoded_options_count)
475 append_option (decoded_options, decoded_options_count, foption);
476 else if ((*decoded_options)[j].opt_index == foption->opt_index
477 && foption->opt_index != OPT_O)
478 /* Exact same options get merged. */
480 else
482 /* For mismatched option kinds preserve the optimization
483 level only, thus merge it as -On. This also handles
484 merging of same optimization level -On. */
485 int level = 0;
486 switch (foption->opt_index)
488 case OPT_O:
489 if (foption->arg[0] == '\0')
490 level = MAX (level, 1);
491 else
492 level = MAX (level, atoi (foption->arg));
493 break;
494 case OPT_Ofast:
495 level = MAX (level, 3);
496 break;
497 case OPT_Og:
498 level = MAX (level, 1);
499 break;
500 case OPT_Os:
501 level = MAX (level, 2);
502 break;
503 default:
504 gcc_unreachable ();
506 switch ((*decoded_options)[j].opt_index)
508 case OPT_O:
509 if ((*decoded_options)[j].arg[0] == '\0')
510 level = MAX (level, 1);
511 else
512 level = MAX (level, atoi ((*decoded_options)[j].arg));
513 break;
514 case OPT_Ofast:
515 level = MAX (level, 3);
516 break;
517 case OPT_Og:
518 level = MAX (level, 1);
519 break;
520 case OPT_Os:
521 level = MAX (level, 2);
522 break;
523 default:
524 gcc_unreachable ();
526 (*decoded_options)[j].opt_index = OPT_O;
527 char *tem;
528 asprintf (&tem, "-O%d", level);
529 (*decoded_options)[j].arg = &tem[2];
530 (*decoded_options)[j].canonical_option[0] = tem;
531 (*decoded_options)[j].value = 1;
533 break;
538 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
540 static void
541 run_gcc (unsigned argc, char *argv[])
543 unsigned i, j;
544 const char **new_argv;
545 const char **argv_ptr;
546 char *list_option_full = NULL;
547 const char *linker_output = NULL;
548 const char *collect_gcc, *collect_gcc_options;
549 int parallel = 0;
550 int jobserver = 0;
551 bool no_partition = false;
552 struct cl_decoded_option *fdecoded_options = NULL;
553 unsigned int fdecoded_options_count = 0;
554 struct cl_decoded_option *decoded_options;
555 unsigned int decoded_options_count;
556 struct obstack argv_obstack;
557 int new_head_argc;
559 /* Get the driver and options. */
560 collect_gcc = getenv ("COLLECT_GCC");
561 if (!collect_gcc)
562 fatal ("environment variable COLLECT_GCC must be set");
563 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
564 if (!collect_gcc_options)
565 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
566 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
567 CL_LANG_ALL,
568 &decoded_options,
569 &decoded_options_count);
571 /* Look at saved options in the IL files. */
572 for (i = 1; i < argc; ++i)
574 char *data, *p;
575 char *fopts;
576 int fd;
577 const char *errmsg;
578 int err;
579 off_t file_offset = 0, offset, length;
580 long loffset;
581 simple_object_read *sobj;
582 int consumed;
583 struct cl_decoded_option *f2decoded_options;
584 unsigned int f2decoded_options_count;
585 char *filename = argv[i];
586 if ((p = strrchr (argv[i], '@'))
587 && p != argv[i]
588 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
589 && strlen (p) == (unsigned int) consumed)
591 filename = XNEWVEC (char, p - argv[i] + 1);
592 memcpy (filename, argv[i], p - argv[i]);
593 filename[p - argv[i]] = '\0';
594 file_offset = (off_t) loffset;
596 fd = open (argv[i], O_RDONLY);
597 if (fd == -1)
598 continue;
599 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
600 &errmsg, &err);
601 if (!sobj)
603 close (fd);
604 continue;
606 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
607 &offset, &length, &errmsg, &err))
609 simple_object_release_read (sobj);
610 close (fd);
611 continue;
613 lseek (fd, file_offset + offset, SEEK_SET);
614 data = (char *)xmalloc (length);
615 read (fd, data, length);
616 fopts = data;
619 get_options_from_collect_gcc_options (collect_gcc,
620 fopts, CL_LANG_ALL,
621 &f2decoded_options,
622 &f2decoded_options_count);
623 if (!fdecoded_options)
625 fdecoded_options = f2decoded_options;
626 fdecoded_options_count = f2decoded_options_count;
628 else
629 merge_and_complain (&fdecoded_options,
630 &fdecoded_options_count,
631 f2decoded_options, f2decoded_options_count);
633 fopts += strlen (fopts) + 1;
635 while (fopts - data < length);
637 free (data);
638 simple_object_release_read (sobj);
639 close (fd);
642 /* Initalize the common arguments for the driver. */
643 obstack_init (&argv_obstack);
644 obstack_ptr_grow (&argv_obstack, collect_gcc);
645 obstack_ptr_grow (&argv_obstack, "-xlto");
646 obstack_ptr_grow (&argv_obstack, "-c");
648 /* Append compiler driver arguments as far as they were merged. */
649 for (j = 1; j < fdecoded_options_count; ++j)
651 struct cl_decoded_option *option = &fdecoded_options[j];
653 /* File options have been properly filtered by lto-opts.c. */
654 switch (option->opt_index)
656 /* Drop arguments that we want to take from the link line. */
657 case OPT_flto_:
658 case OPT_flto:
659 case OPT_flto_partition_none:
660 case OPT_flto_partition_1to1:
661 case OPT_flto_partition_balanced:
662 continue;
664 default:
665 break;
668 /* For now do what the original LTO option code was doing - pass
669 on any CL_TARGET flag and a few selected others. */
670 switch (option->opt_index)
672 case OPT_fPIC:
673 case OPT_fpic:
674 case OPT_fpie:
675 case OPT_fcommon:
676 case OPT_fexceptions:
677 case OPT_fnon_call_exceptions:
678 case OPT_fgnu_tm:
679 case OPT_freg_struct_return:
680 case OPT_fpcc_struct_return:
681 case OPT_fshort_double:
682 case OPT_ffp_contract_:
683 case OPT_fwrapv:
684 case OPT_ftrapv:
685 case OPT_fstrict_overflow:
686 case OPT_O:
687 case OPT_Ofast:
688 case OPT_Og:
689 case OPT_Os:
690 break;
692 default:
693 if (!(cl_options[option->opt_index].flags & CL_TARGET))
694 continue;
697 /* Pass the option on. */
698 for (i = 0; i < option->canonical_option_num_elements; ++i)
699 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
702 /* Append linker driver arguments. Compiler options from the linker
703 driver arguments will override / merge with those from the compiler. */
704 for (j = 1; j < decoded_options_count; ++j)
706 struct cl_decoded_option *option = &decoded_options[j];
708 /* Do not pass on frontend specific flags not suitable for lto. */
709 if (!(cl_options[option->opt_index].flags
710 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
711 continue;
713 switch (option->opt_index)
715 case OPT_o:
716 linker_output = option->arg;
717 /* We generate new intermediate output, drop this arg. */
718 continue;
720 case OPT_save_temps:
721 debug = 1;
722 break;
724 case OPT_v:
725 verbose = 1;
726 break;
728 case OPT_flto_partition_none:
729 no_partition = true;
730 break;
732 case OPT_flto_:
733 if (strcmp (option->arg, "jobserver") == 0)
735 jobserver = 1;
736 parallel = 1;
738 else
740 parallel = atoi (option->arg);
741 if (parallel <= 1)
742 parallel = 0;
744 /* Fallthru. */
746 case OPT_flto:
747 lto_mode = LTO_MODE_WHOPR;
748 /* We've handled these LTO options, do not pass them on. */
749 continue;
751 case OPT_freg_struct_return:
752 case OPT_fpcc_struct_return:
753 case OPT_fshort_double:
754 /* Ignore these, they are determined by the input files.
755 ??? We fail to diagnose a possible mismatch here. */
756 continue;
758 default:
759 break;
762 /* Pass the option on. */
763 for (i = 0; i < option->canonical_option_num_elements; ++i)
764 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
767 if (no_partition)
769 lto_mode = LTO_MODE_LTO;
770 jobserver = 0;
771 parallel = 0;
774 if (linker_output)
776 char *output_dir, *base, *name;
777 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
779 output_dir = xstrdup (linker_output);
780 base = output_dir;
781 for (name = base; *name; name++)
782 if (IS_DIR_SEPARATOR (*name))
783 base = name + 1;
784 *base = '\0';
786 linker_output = &linker_output[base - output_dir];
787 if (*output_dir == '\0')
789 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
790 output_dir = current_dir;
792 if (!bit_bucket)
794 obstack_ptr_grow (&argv_obstack, "-dumpdir");
795 obstack_ptr_grow (&argv_obstack, output_dir);
798 obstack_ptr_grow (&argv_obstack, "-dumpbase");
801 /* Remember at which point we can scrub args to re-use the commons. */
802 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
804 if (lto_mode == LTO_MODE_LTO)
806 flto_out = make_temp_file (".lto.o");
807 if (linker_output)
808 obstack_ptr_grow (&argv_obstack, linker_output);
809 obstack_ptr_grow (&argv_obstack, "-o");
810 obstack_ptr_grow (&argv_obstack, flto_out);
812 else
814 const char *list_option = "-fltrans-output-list=";
815 size_t list_option_len = strlen (list_option);
816 char *tmp;
818 if (linker_output)
820 char *dumpbase = (char *) xmalloc (strlen (linker_output)
821 + sizeof (".wpa") + 1);
822 strcpy (dumpbase, linker_output);
823 strcat (dumpbase, ".wpa");
824 obstack_ptr_grow (&argv_obstack, dumpbase);
827 if (linker_output && debug)
829 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
830 + sizeof (".ltrans.out") + 1);
831 strcpy (ltrans_output_file, linker_output);
832 strcat (ltrans_output_file, ".ltrans.out");
834 else
835 ltrans_output_file = make_temp_file (".ltrans.out");
836 list_option_full = (char *) xmalloc (sizeof (char) *
837 (strlen (ltrans_output_file) + list_option_len + 1));
838 tmp = list_option_full;
840 obstack_ptr_grow (&argv_obstack, tmp);
841 strcpy (tmp, list_option);
842 tmp += list_option_len;
843 strcpy (tmp, ltrans_output_file);
845 if (jobserver)
846 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
847 else if (parallel > 1)
849 char buf[256];
850 sprintf (buf, "-fwpa=%i", parallel);
851 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
853 else
854 obstack_ptr_grow (&argv_obstack, "-fwpa");
857 /* Append the input objects and possible preceding arguments. */
858 for (i = 1; i < argc; ++i)
859 obstack_ptr_grow (&argv_obstack, argv[i]);
860 obstack_ptr_grow (&argv_obstack, NULL);
862 new_argv = XOBFINISH (&argv_obstack, const char **);
863 argv_ptr = &new_argv[new_head_argc];
864 fork_execute (CONST_CAST (char **, new_argv));
866 if (lto_mode == LTO_MODE_LTO)
868 printf ("%s\n", flto_out);
869 free (flto_out);
870 flto_out = NULL;
872 else
874 FILE *stream = fopen (ltrans_output_file, "r");
875 FILE *mstream = NULL;
876 struct obstack env_obstack;
878 if (!stream)
879 fatal_perror ("fopen: %s", ltrans_output_file);
881 /* Parse the list of LTRANS inputs from the WPA stage. */
882 obstack_init (&env_obstack);
883 nr = 0;
884 for (;;)
886 const unsigned piece = 32;
887 char *output_name = NULL;
888 char *buf, *input_name = (char *)xmalloc (piece);
889 size_t len;
891 buf = input_name;
892 cont:
893 if (!fgets (buf, piece, stream))
894 break;
895 len = strlen (input_name);
896 if (input_name[len - 1] != '\n')
898 input_name = (char *)xrealloc (input_name, len + piece);
899 buf = input_name + len;
900 goto cont;
902 input_name[len - 1] = '\0';
904 if (input_name[0] == '*')
905 output_name = &input_name[1];
907 nr++;
908 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
909 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
910 input_names[nr-1] = input_name;
911 output_names[nr-1] = output_name;
913 fclose (stream);
914 maybe_unlink_file (ltrans_output_file);
915 ltrans_output_file = NULL;
917 if (parallel)
919 makefile = make_temp_file (".mk");
920 mstream = fopen (makefile, "w");
923 /* Execute the LTRANS stage for each input file (or prepare a
924 makefile to invoke this in parallel). */
925 for (i = 0; i < nr; ++i)
927 char *output_name;
928 char *input_name = input_names[i];
929 /* If it's a pass-through file do nothing. */
930 if (output_names[i])
931 continue;
933 /* Replace the .o suffix with a .ltrans.o suffix and write
934 the resulting name to the LTRANS output list. */
935 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
936 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
937 output_name = XOBFINISH (&env_obstack, char *);
939 /* Adjust the dumpbase if the linker output file was seen. */
940 if (linker_output)
942 char *dumpbase
943 = (char *) xmalloc (strlen (linker_output)
944 + sizeof (DUMPBASE_SUFFIX) + 1);
945 snprintf (dumpbase,
946 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
947 "%s.ltrans%u", linker_output, i);
948 argv_ptr[0] = dumpbase;
951 argv_ptr[1] = "-fltrans";
952 argv_ptr[2] = "-o";
953 argv_ptr[3] = output_name;
954 argv_ptr[4] = input_name;
955 argv_ptr[5] = NULL;
956 if (parallel)
958 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
959 for (j = 1; new_argv[j] != NULL; ++j)
960 fprintf (mstream, " '%s'", new_argv[j]);
961 fprintf (mstream, "\n");
962 /* If we are not preserving the ltrans input files then
963 truncate them as soon as we have processed it. This
964 reduces temporary disk-space usage. */
965 if (! debug)
966 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
967 "&& mv %s.tem %s\n",
968 input_name, input_name, input_name, input_name);
970 else
972 fork_execute (CONST_CAST (char **, new_argv));
973 maybe_unlink_file (input_name);
976 output_names[i] = output_name;
978 if (parallel)
980 struct pex_obj *pex;
981 char jobs[32];
983 fprintf (mstream, "all:");
984 for (i = 0; i < nr; ++i)
985 fprintf (mstream, " \\\n\t%s", output_names[i]);
986 fprintf (mstream, "\n");
987 fclose (mstream);
988 if (!jobserver)
990 /* Avoid passing --jobserver-fd= and similar flags
991 unless jobserver mode is explicitly enabled. */
992 putenv (xstrdup ("MAKEFLAGS="));
993 putenv (xstrdup ("MFLAGS="));
995 new_argv[0] = getenv ("MAKE");
996 if (!new_argv[0])
997 new_argv[0] = "make";
998 new_argv[1] = "-f";
999 new_argv[2] = makefile;
1000 i = 3;
1001 if (!jobserver)
1003 snprintf (jobs, 31, "-j%d", parallel);
1004 new_argv[i++] = jobs;
1006 new_argv[i++] = "all";
1007 new_argv[i++] = NULL;
1008 pex = collect_execute (CONST_CAST (char **, new_argv));
1009 collect_wait (new_argv[0], pex);
1010 maybe_unlink_file (makefile);
1011 makefile = NULL;
1012 for (i = 0; i < nr; ++i)
1013 maybe_unlink_file (input_names[i]);
1015 for (i = 0; i < nr; ++i)
1017 fputs (output_names[i], stdout);
1018 putc ('\n', stdout);
1019 free (input_names[i]);
1021 nr = 0;
1022 free (output_names);
1023 free (input_names);
1024 free (list_option_full);
1025 obstack_free (&env_obstack, NULL);
1028 obstack_free (&argv_obstack, NULL);
1032 /* Entry point. */
1035 main (int argc, char *argv[])
1037 const char *p;
1039 gcc_obstack_init (&opts_obstack);
1041 p = argv[0] + strlen (argv[0]);
1042 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1043 --p;
1044 progname = p;
1046 xmalloc_set_program_name (progname);
1048 gcc_init_libintl ();
1050 diagnostic_initialize (global_dc, 0);
1052 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1053 signal (SIGINT, fatal_signal);
1054 #ifdef SIGHUP
1055 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1056 signal (SIGHUP, fatal_signal);
1057 #endif
1058 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1059 signal (SIGTERM, fatal_signal);
1060 #ifdef SIGPIPE
1061 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1062 signal (SIGPIPE, fatal_signal);
1063 #endif
1064 #ifdef SIGCHLD
1065 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1066 receive the signal. A different setting is inheritable */
1067 signal (SIGCHLD, SIG_DFL);
1068 #endif
1070 /* We may be called with all the arguments stored in some file and
1071 passed with @file. Expand them into argv before processing. */
1072 expandargv (&argc, &argv);
1074 run_gcc (argc, argv);
1076 return 0;