gcc/
[official-gcc.git] / gcc / lto-wrapper.c
blobf59d74e4fa52c127c1712d76259b7e3f1d63b821
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"
49 #include "lto-section-names.h"
51 int debug; /* true if -save-temps. */
52 int verbose; /* true if -v. */
54 enum lto_mode_d {
55 LTO_MODE_NONE, /* Not doing LTO. */
56 LTO_MODE_LTO, /* Normal LTO. */
57 LTO_MODE_WHOPR /* WHOPR. */
60 /* Current LTO mode. */
61 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
63 static char *ltrans_output_file;
64 static char *flto_out;
65 static char *args_name;
66 static unsigned int nr;
67 static char **input_names;
68 static char **output_names;
69 static char *makefile;
71 static void maybe_unlink_file (const char *);
73 /* Delete tempfiles. */
75 static void
76 lto_wrapper_cleanup (void)
78 static bool cleanup_done = false;
79 unsigned int i;
81 if (cleanup_done)
82 return;
84 /* Setting cleanup_done prevents an infinite loop if one of the
85 calls to maybe_unlink_file fails. */
86 cleanup_done = true;
88 if (ltrans_output_file)
89 maybe_unlink_file (ltrans_output_file);
90 if (flto_out)
91 maybe_unlink_file (flto_out);
92 if (args_name)
93 maybe_unlink_file (args_name);
94 if (makefile)
95 maybe_unlink_file (makefile);
96 for (i = 0; i < nr; ++i)
98 maybe_unlink_file (input_names[i]);
99 if (output_names[i])
100 maybe_unlink_file (output_names[i]);
104 static void
105 fatal_signal (int signum)
107 signal (signum, SIG_DFL);
108 lto_wrapper_cleanup ();
109 /* Get the same signal again, this time not handled,
110 so its normal effect occurs. */
111 kill (getpid (), signum);
114 /* Just die. CMSGID is the error message. */
116 static void __attribute__ ((format (printf, 1, 2)))
117 fatal (const char * cmsgid, ...)
119 va_list ap;
121 va_start (ap, cmsgid);
122 fprintf (stderr, "lto-wrapper: ");
123 vfprintf (stderr, _(cmsgid), ap);
124 fprintf (stderr, "\n");
125 va_end (ap);
127 lto_wrapper_cleanup ();
128 exit (FATAL_EXIT_CODE);
132 /* Die when sys call fails. CMSGID is the error message. */
134 static void __attribute__ ((format (printf, 1, 2)))
135 fatal_perror (const char *cmsgid, ...)
137 int e = errno;
138 va_list ap;
140 va_start (ap, cmsgid);
141 fprintf (stderr, "lto-wrapper: ");
142 vfprintf (stderr, _(cmsgid), ap);
143 fprintf (stderr, ": %s\n", xstrerror (e));
144 va_end (ap);
146 lto_wrapper_cleanup ();
147 exit (FATAL_EXIT_CODE);
151 /* Execute a program, and wait for the reply. ARGV are the arguments. The
152 last one must be NULL. */
154 static struct pex_obj *
155 collect_execute (char **argv)
157 struct pex_obj *pex;
158 const char *errmsg;
159 int err;
161 if (verbose)
163 char **p_argv;
164 const char *str;
166 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
167 fprintf (stderr, " %s", str);
169 fprintf (stderr, "\n");
172 fflush (stdout);
173 fflush (stderr);
175 pex = pex_init (0, "lto-wrapper", NULL);
176 if (pex == NULL)
177 fatal_perror ("pex_init failed");
179 /* Do not use PEX_LAST here, we use our stdout for communicating with
180 collect2 or the linker-plugin. Any output from the sub-process
181 will confuse that. */
182 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
183 NULL, &err);
184 if (errmsg != NULL)
186 if (err != 0)
188 errno = err;
189 fatal_perror (errmsg);
191 else
192 fatal (errmsg);
195 return pex;
199 /* Wait for a process to finish, and exit if a nonzero status is found.
200 PROG is the program name. PEX is the process we should wait for. */
202 static int
203 collect_wait (const char *prog, struct pex_obj *pex)
205 int status;
207 if (!pex_get_status (pex, 1, &status))
208 fatal_perror ("can't get program status");
209 pex_free (pex);
211 if (status)
213 if (WIFSIGNALED (status))
215 int sig = WTERMSIG (status);
216 if (WCOREDUMP (status))
217 fatal ("%s terminated with signal %d [%s], core dumped",
218 prog, sig, strsignal (sig));
219 else
220 fatal ("%s terminated with signal %d [%s]",
221 prog, sig, strsignal (sig));
224 if (WIFEXITED (status))
225 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
228 return 0;
232 /* Unlink a temporary LTRANS file unless requested otherwise. */
234 static void
235 maybe_unlink_file (const char *file)
237 if (! debug)
239 if (unlink_if_ordinary (file)
240 && errno != ENOENT)
241 fatal_perror ("deleting LTRANS file %s", file);
243 else if (verbose)
244 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
248 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
250 static void
251 fork_execute (char **argv)
253 struct pex_obj *pex;
254 char *new_argv[3];
255 char *at_args;
256 FILE *args;
257 int status;
259 args_name = make_temp_file (".args");
260 at_args = concat ("@", args_name, NULL);
261 args = fopen (args_name, "w");
262 if (args == NULL)
263 fatal ("failed to open %s", args_name);
265 status = writeargv (&argv[1], args);
267 if (status)
268 fatal ("could not write to temporary file %s", args_name);
270 fclose (args);
272 new_argv[0] = argv[0];
273 new_argv[1] = at_args;
274 new_argv[2] = NULL;
276 pex = collect_execute (new_argv);
277 collect_wait (new_argv[0], pex);
279 maybe_unlink_file (args_name);
280 args_name = NULL;
281 free (at_args);
284 /* Template of LTRANS dumpbase suffix. */
285 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
287 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
288 environment according to LANG_MASK. */
290 static void
291 get_options_from_collect_gcc_options (const char *collect_gcc,
292 const char *collect_gcc_options,
293 unsigned int lang_mask,
294 struct cl_decoded_option **decoded_options,
295 unsigned int *decoded_options_count)
297 struct obstack argv_obstack;
298 char *argv_storage;
299 const char **argv;
300 int j, k, argc;
302 argv_storage = xstrdup (collect_gcc_options);
303 obstack_init (&argv_obstack);
304 obstack_ptr_grow (&argv_obstack, collect_gcc);
306 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
308 if (argv_storage[j] == '\'')
310 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
311 ++j;
314 if (argv_storage[j] == '\0')
315 fatal ("malformed COLLECT_GCC_OPTIONS");
316 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
318 argv_storage[k++] = '\'';
319 j += 4;
321 else if (argv_storage[j] == '\'')
322 break;
323 else
324 argv_storage[k++] = argv_storage[j++];
326 while (1);
327 argv_storage[k++] = '\0';
331 obstack_ptr_grow (&argv_obstack, NULL);
332 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
333 argv = XOBFINISH (&argv_obstack, const char **);
335 decode_cmdline_options_to_array (argc, (const char **)argv,
336 lang_mask,
337 decoded_options, decoded_options_count);
338 obstack_free (&argv_obstack, NULL);
341 /* Append OPTION to the options array DECODED_OPTIONS with size
342 DECODED_OPTIONS_COUNT. */
344 static void
345 append_option (struct cl_decoded_option **decoded_options,
346 unsigned int *decoded_options_count,
347 struct cl_decoded_option *option)
349 ++*decoded_options_count;
350 *decoded_options
351 = (struct cl_decoded_option *)
352 xrealloc (*decoded_options,
353 (*decoded_options_count
354 * sizeof (struct cl_decoded_option)));
355 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
356 sizeof (struct cl_decoded_option));
359 /* Try to merge and complain about options FDECODED_OPTIONS when applied
360 ontop of DECODED_OPTIONS. */
362 static void
363 merge_and_complain (struct cl_decoded_option **decoded_options,
364 unsigned int *decoded_options_count,
365 struct cl_decoded_option *fdecoded_options,
366 unsigned int fdecoded_options_count)
368 unsigned int i, j;
370 /* ??? Merge options from files. Most cases can be
371 handled by either unioning or intersecting
372 (for example -fwrapv is a case for unioning,
373 -ffast-math is for intersection). Most complaints
374 about real conflicts between different options can
375 be deferred to the compiler proper. Options that
376 we can neither safely handle by intersection nor
377 unioning would need to be complained about here.
378 Ideally we'd have a flag in the opt files that
379 tells whether to union or intersect or reject.
380 In absence of that it's unclear what a good default is.
381 It's also difficult to get positional handling correct. */
383 /* The following does what the old LTO option code did,
384 union all target and a selected set of common options. */
385 for (i = 0; i < fdecoded_options_count; ++i)
387 struct cl_decoded_option *foption = &fdecoded_options[i];
388 switch (foption->opt_index)
390 case OPT_SPECIAL_unknown:
391 case OPT_SPECIAL_ignore:
392 case OPT_SPECIAL_program_name:
393 case OPT_SPECIAL_input_file:
394 break;
396 default:
397 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
398 break;
400 /* Fallthru. */
401 case OPT_fPIC:
402 case OPT_fpic:
403 case OPT_fPIE:
404 case OPT_fpie:
405 case OPT_fcommon:
406 case OPT_fexceptions:
407 case OPT_fnon_call_exceptions:
408 case OPT_fgnu_tm:
409 /* Do what the old LTO code did - collect exactly one option
410 setting per OPT code, we pick the first we encounter.
411 ??? This doesn't make too much sense, but when it doesn't
412 then we should complain. */
413 for (j = 0; j < *decoded_options_count; ++j)
414 if ((*decoded_options)[j].opt_index == foption->opt_index)
415 break;
416 if (j == *decoded_options_count)
417 append_option (decoded_options, decoded_options_count, foption);
418 break;
420 case OPT_ftrapv:
421 case OPT_fstrict_overflow:
422 case OPT_ffp_contract_:
423 /* For selected options we can merge conservatively. */
424 for (j = 0; j < *decoded_options_count; ++j)
425 if ((*decoded_options)[j].opt_index == foption->opt_index)
426 break;
427 if (j == *decoded_options_count)
428 append_option (decoded_options, decoded_options_count, foption);
429 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
430 -fno-trapv < -ftrapv,
431 -fno-strict-overflow < -fstrict-overflow */
432 else if (foption->value < (*decoded_options)[j].value)
433 (*decoded_options)[j] = *foption;
434 break;
436 case OPT_fwrapv:
437 /* For selected options we can merge conservatively. */
438 for (j = 0; j < *decoded_options_count; ++j)
439 if ((*decoded_options)[j].opt_index == foption->opt_index)
440 break;
441 if (j == *decoded_options_count)
442 append_option (decoded_options, decoded_options_count, foption);
443 /* -fwrapv > -fno-wrapv. */
444 else if (foption->value > (*decoded_options)[j].value)
445 (*decoded_options)[j] = *foption;
446 break;
448 case OPT_freg_struct_return:
449 case OPT_fpcc_struct_return:
450 case OPT_fshort_double:
451 for (j = 0; j < *decoded_options_count; ++j)
452 if ((*decoded_options)[j].opt_index == foption->opt_index)
453 break;
454 if (j == *decoded_options_count)
455 fatal ("Option %s not used consistently in all LTO input files",
456 foption->orig_option_with_args_text);
457 break;
459 case OPT_O:
460 case OPT_Ofast:
461 case OPT_Og:
462 case OPT_Os:
463 for (j = 0; j < *decoded_options_count; ++j)
464 if ((*decoded_options)[j].opt_index == OPT_O
465 || (*decoded_options)[j].opt_index == OPT_Ofast
466 || (*decoded_options)[j].opt_index == OPT_Og
467 || (*decoded_options)[j].opt_index == OPT_Os)
468 break;
469 if (j == *decoded_options_count)
470 append_option (decoded_options, decoded_options_count, foption);
471 else if ((*decoded_options)[j].opt_index == foption->opt_index
472 && foption->opt_index != OPT_O)
473 /* Exact same options get merged. */
475 else
477 /* For mismatched option kinds preserve the optimization
478 level only, thus merge it as -On. This also handles
479 merging of same optimization level -On. */
480 int level = 0;
481 switch (foption->opt_index)
483 case OPT_O:
484 if (foption->arg[0] == '\0')
485 level = MAX (level, 1);
486 else
487 level = MAX (level, atoi (foption->arg));
488 break;
489 case OPT_Ofast:
490 level = MAX (level, 3);
491 break;
492 case OPT_Og:
493 level = MAX (level, 1);
494 break;
495 case OPT_Os:
496 level = MAX (level, 2);
497 break;
498 default:
499 gcc_unreachable ();
501 switch ((*decoded_options)[j].opt_index)
503 case OPT_O:
504 if ((*decoded_options)[j].arg[0] == '\0')
505 level = MAX (level, 1);
506 else
507 level = MAX (level, atoi ((*decoded_options)[j].arg));
508 break;
509 case OPT_Ofast:
510 level = MAX (level, 3);
511 break;
512 case OPT_Og:
513 level = MAX (level, 1);
514 break;
515 case OPT_Os:
516 level = MAX (level, 2);
517 break;
518 default:
519 gcc_unreachable ();
521 (*decoded_options)[j].opt_index = OPT_O;
522 char *tem;
523 asprintf (&tem, "-O%d", level);
524 (*decoded_options)[j].arg = &tem[2];
525 (*decoded_options)[j].canonical_option[0] = tem;
526 (*decoded_options)[j].value = 1;
528 break;
533 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
535 static void
536 run_gcc (unsigned argc, char *argv[])
538 unsigned i, j;
539 const char **new_argv;
540 const char **argv_ptr;
541 char *list_option_full = NULL;
542 const char *linker_output = NULL;
543 const char *collect_gcc, *collect_gcc_options;
544 int parallel = 0;
545 int jobserver = 0;
546 bool no_partition = false;
547 struct cl_decoded_option *fdecoded_options = NULL;
548 unsigned int fdecoded_options_count = 0;
549 struct cl_decoded_option *decoded_options;
550 unsigned int decoded_options_count;
551 struct obstack argv_obstack;
552 int new_head_argc;
554 /* Get the driver and options. */
555 collect_gcc = getenv ("COLLECT_GCC");
556 if (!collect_gcc)
557 fatal ("environment variable COLLECT_GCC must be set");
558 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
559 if (!collect_gcc_options)
560 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
561 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
562 CL_LANG_ALL,
563 &decoded_options,
564 &decoded_options_count);
566 /* Look at saved options in the IL files. */
567 for (i = 1; i < argc; ++i)
569 char *data, *p;
570 char *fopts;
571 int fd;
572 const char *errmsg;
573 int err;
574 off_t file_offset = 0, offset, length;
575 long loffset;
576 simple_object_read *sobj;
577 int consumed;
578 struct cl_decoded_option *f2decoded_options;
579 unsigned int f2decoded_options_count;
580 char *filename = argv[i];
581 if ((p = strrchr (argv[i], '@'))
582 && p != argv[i]
583 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
584 && strlen (p) == (unsigned int) consumed)
586 filename = XNEWVEC (char, p - argv[i] + 1);
587 memcpy (filename, argv[i], p - argv[i]);
588 filename[p - argv[i]] = '\0';
589 file_offset = (off_t) loffset;
591 fd = open (argv[i], O_RDONLY);
592 if (fd == -1)
593 continue;
594 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
595 &errmsg, &err);
596 if (!sobj)
598 close (fd);
599 continue;
601 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
602 &offset, &length, &errmsg, &err))
604 simple_object_release_read (sobj);
605 close (fd);
606 continue;
608 lseek (fd, file_offset + offset, SEEK_SET);
609 data = (char *)xmalloc (length);
610 read (fd, data, length);
611 fopts = data;
614 get_options_from_collect_gcc_options (collect_gcc,
615 fopts, CL_LANG_ALL,
616 &f2decoded_options,
617 &f2decoded_options_count);
618 if (!fdecoded_options)
620 fdecoded_options = f2decoded_options;
621 fdecoded_options_count = f2decoded_options_count;
623 else
624 merge_and_complain (&fdecoded_options,
625 &fdecoded_options_count,
626 f2decoded_options, f2decoded_options_count);
628 fopts += strlen (fopts) + 1;
630 while (fopts - data < length);
632 free (data);
633 simple_object_release_read (sobj);
634 close (fd);
637 /* Initalize the common arguments for the driver. */
638 obstack_init (&argv_obstack);
639 obstack_ptr_grow (&argv_obstack, collect_gcc);
640 obstack_ptr_grow (&argv_obstack, "-xlto");
641 obstack_ptr_grow (&argv_obstack, "-c");
643 /* Append compiler driver arguments as far as they were merged. */
644 for (j = 1; j < fdecoded_options_count; ++j)
646 struct cl_decoded_option *option = &fdecoded_options[j];
648 /* File options have been properly filtered by lto-opts.c. */
649 switch (option->opt_index)
651 /* Drop arguments that we want to take from the link line. */
652 case OPT_flto_:
653 case OPT_flto:
654 case OPT_flto_partition_:
655 continue;
657 default:
658 break;
661 /* For now do what the original LTO option code was doing - pass
662 on any CL_TARGET flag and a few selected others. */
663 switch (option->opt_index)
665 case OPT_fPIC:
666 case OPT_fpic:
667 case OPT_fPIE:
668 case OPT_fpie:
669 case OPT_fcommon:
670 case OPT_fexceptions:
671 case OPT_fnon_call_exceptions:
672 case OPT_fgnu_tm:
673 case OPT_freg_struct_return:
674 case OPT_fpcc_struct_return:
675 case OPT_fshort_double:
676 case OPT_ffp_contract_:
677 case OPT_fwrapv:
678 case OPT_ftrapv:
679 case OPT_fstrict_overflow:
680 case OPT_O:
681 case OPT_Ofast:
682 case OPT_Og:
683 case OPT_Os:
684 break;
686 default:
687 if (!(cl_options[option->opt_index].flags & CL_TARGET))
688 continue;
691 /* Pass the option on. */
692 for (i = 0; i < option->canonical_option_num_elements; ++i)
693 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
696 /* Append linker driver arguments. Compiler options from the linker
697 driver arguments will override / merge with those from the compiler. */
698 for (j = 1; j < decoded_options_count; ++j)
700 struct cl_decoded_option *option = &decoded_options[j];
702 /* Do not pass on frontend specific flags not suitable for lto. */
703 if (!(cl_options[option->opt_index].flags
704 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
705 continue;
707 switch (option->opt_index)
709 case OPT_o:
710 linker_output = option->arg;
711 /* We generate new intermediate output, drop this arg. */
712 continue;
714 case OPT_save_temps:
715 debug = 1;
716 break;
718 case OPT_v:
719 verbose = 1;
720 break;
722 case OPT_flto_partition_:
723 if (strcmp (option->arg, "none") == 0)
724 no_partition = true;
725 break;
727 case OPT_flto_:
728 if (strcmp (option->arg, "jobserver") == 0)
730 jobserver = 1;
731 parallel = 1;
733 else
735 parallel = atoi (option->arg);
736 if (parallel <= 1)
737 parallel = 0;
739 /* Fallthru. */
741 case OPT_flto:
742 lto_mode = LTO_MODE_WHOPR;
743 /* We've handled these LTO options, do not pass them on. */
744 continue;
746 case OPT_freg_struct_return:
747 case OPT_fpcc_struct_return:
748 case OPT_fshort_double:
749 /* Ignore these, they are determined by the input files.
750 ??? We fail to diagnose a possible mismatch here. */
751 continue;
753 default:
754 break;
757 /* Pass the option on. */
758 for (i = 0; i < option->canonical_option_num_elements; ++i)
759 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
762 if (no_partition)
764 lto_mode = LTO_MODE_LTO;
765 jobserver = 0;
766 parallel = 0;
769 if (linker_output)
771 char *output_dir, *base, *name;
772 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
774 output_dir = xstrdup (linker_output);
775 base = output_dir;
776 for (name = base; *name; name++)
777 if (IS_DIR_SEPARATOR (*name))
778 base = name + 1;
779 *base = '\0';
781 linker_output = &linker_output[base - output_dir];
782 if (*output_dir == '\0')
784 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
785 output_dir = current_dir;
787 if (!bit_bucket)
789 obstack_ptr_grow (&argv_obstack, "-dumpdir");
790 obstack_ptr_grow (&argv_obstack, output_dir);
793 obstack_ptr_grow (&argv_obstack, "-dumpbase");
796 /* Remember at which point we can scrub args to re-use the commons. */
797 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
799 if (lto_mode == LTO_MODE_LTO)
801 flto_out = make_temp_file (".lto.o");
802 if (linker_output)
803 obstack_ptr_grow (&argv_obstack, linker_output);
804 obstack_ptr_grow (&argv_obstack, "-o");
805 obstack_ptr_grow (&argv_obstack, flto_out);
807 else
809 const char *list_option = "-fltrans-output-list=";
810 size_t list_option_len = strlen (list_option);
811 char *tmp;
813 if (linker_output)
815 char *dumpbase = (char *) xmalloc (strlen (linker_output)
816 + sizeof (".wpa") + 1);
817 strcpy (dumpbase, linker_output);
818 strcat (dumpbase, ".wpa");
819 obstack_ptr_grow (&argv_obstack, dumpbase);
822 if (linker_output && debug)
824 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
825 + sizeof (".ltrans.out") + 1);
826 strcpy (ltrans_output_file, linker_output);
827 strcat (ltrans_output_file, ".ltrans.out");
829 else
830 ltrans_output_file = make_temp_file (".ltrans.out");
831 list_option_full = (char *) xmalloc (sizeof (char) *
832 (strlen (ltrans_output_file) + list_option_len + 1));
833 tmp = list_option_full;
835 obstack_ptr_grow (&argv_obstack, tmp);
836 strcpy (tmp, list_option);
837 tmp += list_option_len;
838 strcpy (tmp, ltrans_output_file);
840 if (jobserver)
841 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
842 else if (parallel > 1)
844 char buf[256];
845 sprintf (buf, "-fwpa=%i", parallel);
846 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
848 else
849 obstack_ptr_grow (&argv_obstack, "-fwpa");
852 /* Append the input objects and possible preceding arguments. */
853 for (i = 1; i < argc; ++i)
854 obstack_ptr_grow (&argv_obstack, argv[i]);
855 obstack_ptr_grow (&argv_obstack, NULL);
857 new_argv = XOBFINISH (&argv_obstack, const char **);
858 argv_ptr = &new_argv[new_head_argc];
859 fork_execute (CONST_CAST (char **, new_argv));
861 if (lto_mode == LTO_MODE_LTO)
863 printf ("%s\n", flto_out);
864 free (flto_out);
865 flto_out = NULL;
867 else
869 FILE *stream = fopen (ltrans_output_file, "r");
870 FILE *mstream = NULL;
871 struct obstack env_obstack;
873 if (!stream)
874 fatal_perror ("fopen: %s", ltrans_output_file);
876 /* Parse the list of LTRANS inputs from the WPA stage. */
877 obstack_init (&env_obstack);
878 nr = 0;
879 for (;;)
881 const unsigned piece = 32;
882 char *output_name = NULL;
883 char *buf, *input_name = (char *)xmalloc (piece);
884 size_t len;
886 buf = input_name;
887 cont:
888 if (!fgets (buf, piece, stream))
889 break;
890 len = strlen (input_name);
891 if (input_name[len - 1] != '\n')
893 input_name = (char *)xrealloc (input_name, len + piece);
894 buf = input_name + len;
895 goto cont;
897 input_name[len - 1] = '\0';
899 if (input_name[0] == '*')
900 output_name = &input_name[1];
902 nr++;
903 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
904 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
905 input_names[nr-1] = input_name;
906 output_names[nr-1] = output_name;
908 fclose (stream);
909 maybe_unlink_file (ltrans_output_file);
910 ltrans_output_file = NULL;
912 if (parallel)
914 makefile = make_temp_file (".mk");
915 mstream = fopen (makefile, "w");
918 /* Execute the LTRANS stage for each input file (or prepare a
919 makefile to invoke this in parallel). */
920 for (i = 0; i < nr; ++i)
922 char *output_name;
923 char *input_name = input_names[i];
924 /* If it's a pass-through file do nothing. */
925 if (output_names[i])
926 continue;
928 /* Replace the .o suffix with a .ltrans.o suffix and write
929 the resulting name to the LTRANS output list. */
930 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
931 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
932 output_name = XOBFINISH (&env_obstack, char *);
934 /* Adjust the dumpbase if the linker output file was seen. */
935 if (linker_output)
937 char *dumpbase
938 = (char *) xmalloc (strlen (linker_output)
939 + sizeof (DUMPBASE_SUFFIX) + 1);
940 snprintf (dumpbase,
941 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
942 "%s.ltrans%u", linker_output, i);
943 argv_ptr[0] = dumpbase;
946 argv_ptr[1] = "-fltrans";
947 argv_ptr[2] = "-o";
948 argv_ptr[3] = output_name;
949 argv_ptr[4] = input_name;
950 argv_ptr[5] = NULL;
951 if (parallel)
953 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
954 for (j = 1; new_argv[j] != NULL; ++j)
955 fprintf (mstream, " '%s'", new_argv[j]);
956 fprintf (mstream, "\n");
957 /* If we are not preserving the ltrans input files then
958 truncate them as soon as we have processed it. This
959 reduces temporary disk-space usage. */
960 if (! debug)
961 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
962 "&& mv %s.tem %s\n",
963 input_name, input_name, input_name, input_name);
965 else
967 fork_execute (CONST_CAST (char **, new_argv));
968 maybe_unlink_file (input_name);
971 output_names[i] = output_name;
973 if (parallel)
975 struct pex_obj *pex;
976 char jobs[32];
978 fprintf (mstream, "all:");
979 for (i = 0; i < nr; ++i)
980 fprintf (mstream, " \\\n\t%s", output_names[i]);
981 fprintf (mstream, "\n");
982 fclose (mstream);
983 if (!jobserver)
985 /* Avoid passing --jobserver-fd= and similar flags
986 unless jobserver mode is explicitly enabled. */
987 putenv (xstrdup ("MAKEFLAGS="));
988 putenv (xstrdup ("MFLAGS="));
990 new_argv[0] = getenv ("MAKE");
991 if (!new_argv[0])
992 new_argv[0] = "make";
993 new_argv[1] = "-f";
994 new_argv[2] = makefile;
995 i = 3;
996 if (!jobserver)
998 snprintf (jobs, 31, "-j%d", parallel);
999 new_argv[i++] = jobs;
1001 new_argv[i++] = "all";
1002 new_argv[i++] = NULL;
1003 pex = collect_execute (CONST_CAST (char **, new_argv));
1004 collect_wait (new_argv[0], pex);
1005 maybe_unlink_file (makefile);
1006 makefile = NULL;
1007 for (i = 0; i < nr; ++i)
1008 maybe_unlink_file (input_names[i]);
1010 for (i = 0; i < nr; ++i)
1012 fputs (output_names[i], stdout);
1013 putc ('\n', stdout);
1014 free (input_names[i]);
1016 nr = 0;
1017 free (output_names);
1018 free (input_names);
1019 free (list_option_full);
1020 obstack_free (&env_obstack, NULL);
1023 obstack_free (&argv_obstack, NULL);
1027 /* Entry point. */
1030 main (int argc, char *argv[])
1032 const char *p;
1034 gcc_obstack_init (&opts_obstack);
1036 p = argv[0] + strlen (argv[0]);
1037 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1038 --p;
1039 progname = p;
1041 xmalloc_set_program_name (progname);
1043 gcc_init_libintl ();
1045 diagnostic_initialize (global_dc, 0);
1047 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1048 signal (SIGINT, fatal_signal);
1049 #ifdef SIGHUP
1050 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1051 signal (SIGHUP, fatal_signal);
1052 #endif
1053 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1054 signal (SIGTERM, fatal_signal);
1055 #ifdef SIGPIPE
1056 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1057 signal (SIGPIPE, fatal_signal);
1058 #endif
1059 #ifdef SIGCHLD
1060 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1061 receive the signal. A different setting is inheritable */
1062 signal (SIGCHLD, SIG_DFL);
1063 #endif
1065 /* We may be called with all the arguments stored in some file and
1066 passed with @file. Expand them into argv before processing. */
1067 expandargv (&argc, &argv);
1069 run_gcc (argc, argv);
1071 return 0;