Fix dot dump bug
[official-gcc.git] / gcc / lto-wrapper.c
blobf42b14efd5dd491ec74adfb4649745052f192605
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 /* Execute a program, and wait for the reply. ARGV are the arguments. The
115 last one must be NULL. */
117 static struct pex_obj *
118 collect_execute (char **argv)
120 struct pex_obj *pex;
121 const char *errmsg;
122 int err;
124 if (verbose)
126 char **p_argv;
127 const char *str;
129 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
130 fprintf (stderr, " %s", str);
132 fprintf (stderr, "\n");
135 fflush (stdout);
136 fflush (stderr);
138 pex = pex_init (0, "lto-wrapper", NULL);
139 if (pex == NULL)
140 fatal_error ("pex_init failed: %m");
142 /* Do not use PEX_LAST here, we use our stdout for communicating with
143 collect2 or the linker-plugin. Any output from the sub-process
144 will confuse that. */
145 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
146 NULL, &err);
147 if (errmsg != NULL)
149 if (err != 0)
151 errno = err;
152 fatal_error ("%s: %m", _(errmsg));
154 else
155 fatal_error (errmsg);
158 return pex;
162 /* Wait for a process to finish, and exit if a nonzero status is found.
163 PROG is the program name. PEX is the process we should wait for. */
165 static int
166 collect_wait (const char *prog, struct pex_obj *pex)
168 int status;
170 if (!pex_get_status (pex, 1, &status))
171 fatal_error ("can't get program status: %m");
172 pex_free (pex);
174 if (status)
176 if (WIFSIGNALED (status))
178 int sig = WTERMSIG (status);
179 if (WCOREDUMP (status))
180 fatal_error ("%s terminated with signal %d [%s], core dumped",
181 prog, sig, strsignal (sig));
182 else
183 fatal_error ("%s terminated with signal %d [%s]",
184 prog, sig, strsignal (sig));
187 if (WIFEXITED (status))
188 fatal_error ("%s returned %d exit status", prog, WEXITSTATUS (status));
191 return 0;
195 /* Unlink a temporary LTRANS file unless requested otherwise. */
197 static void
198 maybe_unlink_file (const char *file)
200 if (! debug)
202 if (unlink_if_ordinary (file)
203 && errno != ENOENT)
204 fatal_error ("deleting LTRANS file %s: %m", file);
206 else if (verbose)
207 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
211 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
213 static void
214 fork_execute (char **argv)
216 struct pex_obj *pex;
217 char *new_argv[3];
218 char *at_args;
219 FILE *args;
220 int status;
222 args_name = make_temp_file (".args");
223 at_args = concat ("@", args_name, NULL);
224 args = fopen (args_name, "w");
225 if (args == NULL)
226 fatal_error ("failed to open %s", args_name);
228 status = writeargv (&argv[1], args);
230 if (status)
231 fatal_error ("could not write to temporary file %s", args_name);
233 fclose (args);
235 new_argv[0] = argv[0];
236 new_argv[1] = at_args;
237 new_argv[2] = NULL;
239 pex = collect_execute (new_argv);
240 collect_wait (new_argv[0], pex);
242 maybe_unlink_file (args_name);
243 args_name = NULL;
244 free (at_args);
247 /* Template of LTRANS dumpbase suffix. */
248 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
250 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
251 environment according to LANG_MASK. */
253 static void
254 get_options_from_collect_gcc_options (const char *collect_gcc,
255 const char *collect_gcc_options,
256 unsigned int lang_mask,
257 struct cl_decoded_option **decoded_options,
258 unsigned int *decoded_options_count)
260 struct obstack argv_obstack;
261 char *argv_storage;
262 const char **argv;
263 int j, k, argc;
265 argv_storage = xstrdup (collect_gcc_options);
266 obstack_init (&argv_obstack);
267 obstack_ptr_grow (&argv_obstack, collect_gcc);
269 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
271 if (argv_storage[j] == '\'')
273 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
274 ++j;
277 if (argv_storage[j] == '\0')
278 fatal_error ("malformed COLLECT_GCC_OPTIONS");
279 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
281 argv_storage[k++] = '\'';
282 j += 4;
284 else if (argv_storage[j] == '\'')
285 break;
286 else
287 argv_storage[k++] = argv_storage[j++];
289 while (1);
290 argv_storage[k++] = '\0';
294 obstack_ptr_grow (&argv_obstack, NULL);
295 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
296 argv = XOBFINISH (&argv_obstack, const char **);
298 decode_cmdline_options_to_array (argc, (const char **)argv,
299 lang_mask,
300 decoded_options, decoded_options_count);
301 obstack_free (&argv_obstack, NULL);
304 /* Append OPTION to the options array DECODED_OPTIONS with size
305 DECODED_OPTIONS_COUNT. */
307 static void
308 append_option (struct cl_decoded_option **decoded_options,
309 unsigned int *decoded_options_count,
310 struct cl_decoded_option *option)
312 ++*decoded_options_count;
313 *decoded_options
314 = (struct cl_decoded_option *)
315 xrealloc (*decoded_options,
316 (*decoded_options_count
317 * sizeof (struct cl_decoded_option)));
318 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
319 sizeof (struct cl_decoded_option));
322 /* Try to merge and complain about options FDECODED_OPTIONS when applied
323 ontop of DECODED_OPTIONS. */
325 static void
326 merge_and_complain (struct cl_decoded_option **decoded_options,
327 unsigned int *decoded_options_count,
328 struct cl_decoded_option *fdecoded_options,
329 unsigned int fdecoded_options_count)
331 unsigned int i, j;
333 /* ??? Merge options from files. Most cases can be
334 handled by either unioning or intersecting
335 (for example -fwrapv is a case for unioning,
336 -ffast-math is for intersection). Most complaints
337 about real conflicts between different options can
338 be deferred to the compiler proper. Options that
339 we can neither safely handle by intersection nor
340 unioning would need to be complained about here.
341 Ideally we'd have a flag in the opt files that
342 tells whether to union or intersect or reject.
343 In absence of that it's unclear what a good default is.
344 It's also difficult to get positional handling correct. */
346 /* The following does what the old LTO option code did,
347 union all target and a selected set of common options. */
348 for (i = 0; i < fdecoded_options_count; ++i)
350 struct cl_decoded_option *foption = &fdecoded_options[i];
351 switch (foption->opt_index)
353 case OPT_SPECIAL_unknown:
354 case OPT_SPECIAL_ignore:
355 case OPT_SPECIAL_program_name:
356 case OPT_SPECIAL_input_file:
357 break;
359 default:
360 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
361 break;
363 /* Fallthru. */
364 case OPT_fPIC:
365 case OPT_fpic:
366 case OPT_fPIE:
367 case OPT_fpie:
368 case OPT_fcommon:
369 case OPT_fexceptions:
370 case OPT_fnon_call_exceptions:
371 case OPT_fgnu_tm:
372 /* Do what the old LTO code did - collect exactly one option
373 setting per OPT code, we pick the first we encounter.
374 ??? This doesn't make too much sense, but when it doesn't
375 then we should complain. */
376 for (j = 0; j < *decoded_options_count; ++j)
377 if ((*decoded_options)[j].opt_index == foption->opt_index)
378 break;
379 if (j == *decoded_options_count)
380 append_option (decoded_options, decoded_options_count, foption);
381 break;
383 case OPT_ftrapv:
384 case OPT_fstrict_overflow:
385 case OPT_ffp_contract_:
386 /* For selected options we can merge conservatively. */
387 for (j = 0; j < *decoded_options_count; ++j)
388 if ((*decoded_options)[j].opt_index == foption->opt_index)
389 break;
390 if (j == *decoded_options_count)
391 append_option (decoded_options, decoded_options_count, foption);
392 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
393 -fno-trapv < -ftrapv,
394 -fno-strict-overflow < -fstrict-overflow */
395 else if (foption->value < (*decoded_options)[j].value)
396 (*decoded_options)[j] = *foption;
397 break;
399 case OPT_fwrapv:
400 /* For selected options we can merge conservatively. */
401 for (j = 0; j < *decoded_options_count; ++j)
402 if ((*decoded_options)[j].opt_index == foption->opt_index)
403 break;
404 if (j == *decoded_options_count)
405 append_option (decoded_options, decoded_options_count, foption);
406 /* -fwrapv > -fno-wrapv. */
407 else if (foption->value > (*decoded_options)[j].value)
408 (*decoded_options)[j] = *foption;
409 break;
411 case OPT_freg_struct_return:
412 case OPT_fpcc_struct_return:
413 case OPT_fshort_double:
414 for (j = 0; j < *decoded_options_count; ++j)
415 if ((*decoded_options)[j].opt_index == foption->opt_index)
416 break;
417 if (j == *decoded_options_count)
418 fatal_error ("Option %s not used consistently in all LTO input"
419 " files", foption->orig_option_with_args_text);
420 break;
422 case OPT_O:
423 case OPT_Ofast:
424 case OPT_Og:
425 case OPT_Os:
426 for (j = 0; j < *decoded_options_count; ++j)
427 if ((*decoded_options)[j].opt_index == OPT_O
428 || (*decoded_options)[j].opt_index == OPT_Ofast
429 || (*decoded_options)[j].opt_index == OPT_Og
430 || (*decoded_options)[j].opt_index == OPT_Os)
431 break;
432 if (j == *decoded_options_count)
433 append_option (decoded_options, decoded_options_count, foption);
434 else if ((*decoded_options)[j].opt_index == foption->opt_index
435 && foption->opt_index != OPT_O)
436 /* Exact same options get merged. */
438 else
440 /* For mismatched option kinds preserve the optimization
441 level only, thus merge it as -On. This also handles
442 merging of same optimization level -On. */
443 int level = 0;
444 switch (foption->opt_index)
446 case OPT_O:
447 if (foption->arg[0] == '\0')
448 level = MAX (level, 1);
449 else
450 level = MAX (level, atoi (foption->arg));
451 break;
452 case OPT_Ofast:
453 level = MAX (level, 3);
454 break;
455 case OPT_Og:
456 level = MAX (level, 1);
457 break;
458 case OPT_Os:
459 level = MAX (level, 2);
460 break;
461 default:
462 gcc_unreachable ();
464 switch ((*decoded_options)[j].opt_index)
466 case OPT_O:
467 if ((*decoded_options)[j].arg[0] == '\0')
468 level = MAX (level, 1);
469 else
470 level = MAX (level, atoi ((*decoded_options)[j].arg));
471 break;
472 case OPT_Ofast:
473 level = MAX (level, 3);
474 break;
475 case OPT_Og:
476 level = MAX (level, 1);
477 break;
478 case OPT_Os:
479 level = MAX (level, 2);
480 break;
481 default:
482 gcc_unreachable ();
484 (*decoded_options)[j].opt_index = OPT_O;
485 char *tem;
486 asprintf (&tem, "-O%d", level);
487 (*decoded_options)[j].arg = &tem[2];
488 (*decoded_options)[j].canonical_option[0] = tem;
489 (*decoded_options)[j].value = 1;
491 break;
496 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
498 static void
499 run_gcc (unsigned argc, char *argv[])
501 unsigned i, j;
502 const char **new_argv;
503 const char **argv_ptr;
504 char *list_option_full = NULL;
505 const char *linker_output = NULL;
506 const char *collect_gcc, *collect_gcc_options;
507 int parallel = 0;
508 int jobserver = 0;
509 bool no_partition = false;
510 struct cl_decoded_option *fdecoded_options = NULL;
511 unsigned int fdecoded_options_count = 0;
512 struct cl_decoded_option *decoded_options;
513 unsigned int decoded_options_count;
514 struct obstack argv_obstack;
515 int new_head_argc;
517 /* Get the driver and options. */
518 collect_gcc = getenv ("COLLECT_GCC");
519 if (!collect_gcc)
520 fatal_error ("environment variable COLLECT_GCC must be set");
521 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
522 if (!collect_gcc_options)
523 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
524 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
525 CL_LANG_ALL,
526 &decoded_options,
527 &decoded_options_count);
529 /* Look at saved options in the IL files. */
530 for (i = 1; i < argc; ++i)
532 char *data, *p;
533 char *fopts;
534 int fd;
535 const char *errmsg;
536 int err;
537 off_t file_offset = 0, offset, length;
538 long loffset;
539 simple_object_read *sobj;
540 int consumed;
541 struct cl_decoded_option *f2decoded_options;
542 unsigned int f2decoded_options_count;
543 char *filename = argv[i];
544 if ((p = strrchr (argv[i], '@'))
545 && p != argv[i]
546 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
547 && strlen (p) == (unsigned int) consumed)
549 filename = XNEWVEC (char, p - argv[i] + 1);
550 memcpy (filename, argv[i], p - argv[i]);
551 filename[p - argv[i]] = '\0';
552 file_offset = (off_t) loffset;
554 fd = open (argv[i], O_RDONLY);
555 if (fd == -1)
556 continue;
557 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
558 &errmsg, &err);
559 if (!sobj)
561 close (fd);
562 continue;
564 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
565 &offset, &length, &errmsg, &err))
567 simple_object_release_read (sobj);
568 close (fd);
569 continue;
571 lseek (fd, file_offset + offset, SEEK_SET);
572 data = (char *)xmalloc (length);
573 read (fd, data, length);
574 fopts = data;
577 get_options_from_collect_gcc_options (collect_gcc,
578 fopts, CL_LANG_ALL,
579 &f2decoded_options,
580 &f2decoded_options_count);
581 if (!fdecoded_options)
583 fdecoded_options = f2decoded_options;
584 fdecoded_options_count = f2decoded_options_count;
586 else
587 merge_and_complain (&fdecoded_options,
588 &fdecoded_options_count,
589 f2decoded_options, f2decoded_options_count);
591 fopts += strlen (fopts) + 1;
593 while (fopts - data < length);
595 free (data);
596 simple_object_release_read (sobj);
597 close (fd);
600 /* Initalize the common arguments for the driver. */
601 obstack_init (&argv_obstack);
602 obstack_ptr_grow (&argv_obstack, collect_gcc);
603 obstack_ptr_grow (&argv_obstack, "-xlto");
604 obstack_ptr_grow (&argv_obstack, "-c");
606 /* Append compiler driver arguments as far as they were merged. */
607 for (j = 1; j < fdecoded_options_count; ++j)
609 struct cl_decoded_option *option = &fdecoded_options[j];
611 /* File options have been properly filtered by lto-opts.c. */
612 switch (option->opt_index)
614 /* Drop arguments that we want to take from the link line. */
615 case OPT_flto_:
616 case OPT_flto:
617 case OPT_flto_partition_:
618 continue;
620 default:
621 break;
624 /* For now do what the original LTO option code was doing - pass
625 on any CL_TARGET flag and a few selected others. */
626 switch (option->opt_index)
628 case OPT_fPIC:
629 case OPT_fpic:
630 case OPT_fPIE:
631 case OPT_fpie:
632 case OPT_fcommon:
633 case OPT_fexceptions:
634 case OPT_fnon_call_exceptions:
635 case OPT_fgnu_tm:
636 case OPT_freg_struct_return:
637 case OPT_fpcc_struct_return:
638 case OPT_fshort_double:
639 case OPT_ffp_contract_:
640 case OPT_fwrapv:
641 case OPT_ftrapv:
642 case OPT_fstrict_overflow:
643 case OPT_O:
644 case OPT_Ofast:
645 case OPT_Og:
646 case OPT_Os:
647 break;
649 default:
650 if (!(cl_options[option->opt_index].flags & CL_TARGET))
651 continue;
654 /* Pass the option on. */
655 for (i = 0; i < option->canonical_option_num_elements; ++i)
656 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
659 /* Append linker driver arguments. Compiler options from the linker
660 driver arguments will override / merge with those from the compiler. */
661 for (j = 1; j < decoded_options_count; ++j)
663 struct cl_decoded_option *option = &decoded_options[j];
665 /* Do not pass on frontend specific flags not suitable for lto. */
666 if (!(cl_options[option->opt_index].flags
667 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
668 continue;
670 switch (option->opt_index)
672 case OPT_o:
673 linker_output = option->arg;
674 /* We generate new intermediate output, drop this arg. */
675 continue;
677 case OPT_save_temps:
678 debug = 1;
679 break;
681 case OPT_v:
682 verbose = 1;
683 break;
685 case OPT_flto_partition_:
686 if (strcmp (option->arg, "none") == 0)
687 no_partition = true;
688 break;
690 case OPT_flto_:
691 if (strcmp (option->arg, "jobserver") == 0)
693 jobserver = 1;
694 parallel = 1;
696 else
698 parallel = atoi (option->arg);
699 if (parallel <= 1)
700 parallel = 0;
702 /* Fallthru. */
704 case OPT_flto:
705 lto_mode = LTO_MODE_WHOPR;
706 /* We've handled these LTO options, do not pass them on. */
707 continue;
709 case OPT_freg_struct_return:
710 case OPT_fpcc_struct_return:
711 case OPT_fshort_double:
712 /* Ignore these, they are determined by the input files.
713 ??? We fail to diagnose a possible mismatch here. */
714 continue;
716 default:
717 break;
720 /* Pass the option on. */
721 for (i = 0; i < option->canonical_option_num_elements; ++i)
722 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
725 if (no_partition)
727 lto_mode = LTO_MODE_LTO;
728 jobserver = 0;
729 parallel = 0;
732 if (linker_output)
734 char *output_dir, *base, *name;
735 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
737 output_dir = xstrdup (linker_output);
738 base = output_dir;
739 for (name = base; *name; name++)
740 if (IS_DIR_SEPARATOR (*name))
741 base = name + 1;
742 *base = '\0';
744 linker_output = &linker_output[base - output_dir];
745 if (*output_dir == '\0')
747 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
748 output_dir = current_dir;
750 if (!bit_bucket)
752 obstack_ptr_grow (&argv_obstack, "-dumpdir");
753 obstack_ptr_grow (&argv_obstack, output_dir);
756 obstack_ptr_grow (&argv_obstack, "-dumpbase");
759 /* Remember at which point we can scrub args to re-use the commons. */
760 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
762 if (lto_mode == LTO_MODE_LTO)
764 flto_out = make_temp_file (".lto.o");
765 if (linker_output)
766 obstack_ptr_grow (&argv_obstack, linker_output);
767 obstack_ptr_grow (&argv_obstack, "-o");
768 obstack_ptr_grow (&argv_obstack, flto_out);
770 else
772 const char *list_option = "-fltrans-output-list=";
773 size_t list_option_len = strlen (list_option);
774 char *tmp;
776 if (linker_output)
778 char *dumpbase = (char *) xmalloc (strlen (linker_output)
779 + sizeof (".wpa") + 1);
780 strcpy (dumpbase, linker_output);
781 strcat (dumpbase, ".wpa");
782 obstack_ptr_grow (&argv_obstack, dumpbase);
785 if (linker_output && debug)
787 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
788 + sizeof (".ltrans.out") + 1);
789 strcpy (ltrans_output_file, linker_output);
790 strcat (ltrans_output_file, ".ltrans.out");
792 else
793 ltrans_output_file = make_temp_file (".ltrans.out");
794 list_option_full = (char *) xmalloc (sizeof (char) *
795 (strlen (ltrans_output_file) + list_option_len + 1));
796 tmp = list_option_full;
798 obstack_ptr_grow (&argv_obstack, tmp);
799 strcpy (tmp, list_option);
800 tmp += list_option_len;
801 strcpy (tmp, ltrans_output_file);
803 if (jobserver)
804 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
805 else if (parallel > 1)
807 char buf[256];
808 sprintf (buf, "-fwpa=%i", parallel);
809 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
811 else
812 obstack_ptr_grow (&argv_obstack, "-fwpa");
815 /* Append the input objects and possible preceding arguments. */
816 for (i = 1; i < argc; ++i)
817 obstack_ptr_grow (&argv_obstack, argv[i]);
818 obstack_ptr_grow (&argv_obstack, NULL);
820 new_argv = XOBFINISH (&argv_obstack, const char **);
821 argv_ptr = &new_argv[new_head_argc];
822 fork_execute (CONST_CAST (char **, new_argv));
824 if (lto_mode == LTO_MODE_LTO)
826 printf ("%s\n", flto_out);
827 free (flto_out);
828 flto_out = NULL;
830 else
832 FILE *stream = fopen (ltrans_output_file, "r");
833 FILE *mstream = NULL;
834 struct obstack env_obstack;
836 if (!stream)
837 fatal_error ("fopen: %s: %m", ltrans_output_file);
839 /* Parse the list of LTRANS inputs from the WPA stage. */
840 obstack_init (&env_obstack);
841 nr = 0;
842 for (;;)
844 const unsigned piece = 32;
845 char *output_name = NULL;
846 char *buf, *input_name = (char *)xmalloc (piece);
847 size_t len;
849 buf = input_name;
850 cont:
851 if (!fgets (buf, piece, stream))
852 break;
853 len = strlen (input_name);
854 if (input_name[len - 1] != '\n')
856 input_name = (char *)xrealloc (input_name, len + piece);
857 buf = input_name + len;
858 goto cont;
860 input_name[len - 1] = '\0';
862 if (input_name[0] == '*')
863 output_name = &input_name[1];
865 nr++;
866 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
867 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
868 input_names[nr-1] = input_name;
869 output_names[nr-1] = output_name;
871 fclose (stream);
872 maybe_unlink_file (ltrans_output_file);
873 ltrans_output_file = NULL;
875 if (parallel)
877 makefile = make_temp_file (".mk");
878 mstream = fopen (makefile, "w");
881 /* Execute the LTRANS stage for each input file (or prepare a
882 makefile to invoke this in parallel). */
883 for (i = 0; i < nr; ++i)
885 char *output_name;
886 char *input_name = input_names[i];
887 /* If it's a pass-through file do nothing. */
888 if (output_names[i])
889 continue;
891 /* Replace the .o suffix with a .ltrans.o suffix and write
892 the resulting name to the LTRANS output list. */
893 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
894 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
895 output_name = XOBFINISH (&env_obstack, char *);
897 /* Adjust the dumpbase if the linker output file was seen. */
898 if (linker_output)
900 char *dumpbase
901 = (char *) xmalloc (strlen (linker_output)
902 + sizeof (DUMPBASE_SUFFIX) + 1);
903 snprintf (dumpbase,
904 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
905 "%s.ltrans%u", linker_output, i);
906 argv_ptr[0] = dumpbase;
909 argv_ptr[1] = "-fltrans";
910 argv_ptr[2] = "-o";
911 argv_ptr[3] = output_name;
912 argv_ptr[4] = input_name;
913 argv_ptr[5] = NULL;
914 if (parallel)
916 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
917 for (j = 1; new_argv[j] != NULL; ++j)
918 fprintf (mstream, " '%s'", new_argv[j]);
919 fprintf (mstream, "\n");
920 /* If we are not preserving the ltrans input files then
921 truncate them as soon as we have processed it. This
922 reduces temporary disk-space usage. */
923 if (! debug)
924 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
925 "&& mv %s.tem %s\n",
926 input_name, input_name, input_name, input_name);
928 else
930 fork_execute (CONST_CAST (char **, new_argv));
931 maybe_unlink_file (input_name);
934 output_names[i] = output_name;
936 if (parallel)
938 struct pex_obj *pex;
939 char jobs[32];
941 fprintf (mstream, "all:");
942 for (i = 0; i < nr; ++i)
943 fprintf (mstream, " \\\n\t%s", output_names[i]);
944 fprintf (mstream, "\n");
945 fclose (mstream);
946 if (!jobserver)
948 /* Avoid passing --jobserver-fd= and similar flags
949 unless jobserver mode is explicitly enabled. */
950 putenv (xstrdup ("MAKEFLAGS="));
951 putenv (xstrdup ("MFLAGS="));
953 new_argv[0] = getenv ("MAKE");
954 if (!new_argv[0])
955 new_argv[0] = "make";
956 new_argv[1] = "-f";
957 new_argv[2] = makefile;
958 i = 3;
959 if (!jobserver)
961 snprintf (jobs, 31, "-j%d", parallel);
962 new_argv[i++] = jobs;
964 new_argv[i++] = "all";
965 new_argv[i++] = NULL;
966 pex = collect_execute (CONST_CAST (char **, new_argv));
967 collect_wait (new_argv[0], pex);
968 maybe_unlink_file (makefile);
969 makefile = NULL;
970 for (i = 0; i < nr; ++i)
971 maybe_unlink_file (input_names[i]);
973 for (i = 0; i < nr; ++i)
975 fputs (output_names[i], stdout);
976 putc ('\n', stdout);
977 free (input_names[i]);
979 nr = 0;
980 free (output_names);
981 free (input_names);
982 free (list_option_full);
983 obstack_free (&env_obstack, NULL);
986 obstack_free (&argv_obstack, NULL);
990 /* Entry point. */
993 main (int argc, char *argv[])
995 const char *p;
997 gcc_obstack_init (&opts_obstack);
999 p = argv[0] + strlen (argv[0]);
1000 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1001 --p;
1002 progname = p;
1004 xmalloc_set_program_name (progname);
1006 if (atexit (lto_wrapper_cleanup) != 0)
1007 fatal_error ("atexit failed");
1009 gcc_init_libintl ();
1011 diagnostic_initialize (global_dc, 0);
1013 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1014 signal (SIGINT, fatal_signal);
1015 #ifdef SIGHUP
1016 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1017 signal (SIGHUP, fatal_signal);
1018 #endif
1019 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1020 signal (SIGTERM, fatal_signal);
1021 #ifdef SIGPIPE
1022 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1023 signal (SIGPIPE, fatal_signal);
1024 #endif
1025 #ifdef SIGCHLD
1026 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1027 receive the signal. A different setting is inheritable */
1028 signal (SIGCHLD, SIG_DFL);
1029 #endif
1031 /* We may be called with all the arguments stored in some file and
1032 passed with @file. Expand them into argv before processing. */
1033 expandargv (&argc, &argv);
1035 run_gcc (argc, argv);
1037 return 0;