1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009, 2010 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
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
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.
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
42 #include "coretypes.h"
44 #include "diagnostic.h"
49 int debug; /* true if -save-temps. */
50 int verbose; /* true if -v. */
53 LTO_MODE_NONE, /* Not doing LTO. */
54 LTO_MODE_LTO, /* Normal LTO. */
55 LTO_MODE_WHOPR /* WHOPR. */
58 /* Current LTO mode. */
59 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
61 static char *ltrans_output_file;
62 static char *flto_out;
63 static char *args_name;
64 static unsigned int nr;
65 static char **input_names;
66 static char **output_names;
67 static char *makefile;
69 static void maybe_unlink_file (const char *);
71 /* Delete tempfiles. */
74 lto_wrapper_cleanup (void)
76 static bool cleanup_done = false;
82 /* Setting cleanup_done prevents an infinite loop if one of the
83 calls to maybe_unlink_file fails. */
86 if (ltrans_output_file)
87 maybe_unlink_file (ltrans_output_file);
89 maybe_unlink_file (flto_out);
91 maybe_unlink_file (args_name);
93 maybe_unlink_file (makefile);
94 for (i = 0; i < nr; ++i)
96 maybe_unlink_file (input_names[i]);
98 maybe_unlink_file (output_names[i]);
103 fatal_signal (int signum)
105 signal (signum, SIG_DFL);
106 lto_wrapper_cleanup ();
107 /* Get the same signal again, this time not handled,
108 so its normal effect occurs. */
109 kill (getpid (), signum);
112 /* Just die. CMSGID is the error message. */
114 static void __attribute__ ((format (printf, 1, 2)))
115 fatal (const char * cmsgid, ...)
119 va_start (ap, cmsgid);
120 fprintf (stderr, "lto-wrapper: ");
121 vfprintf (stderr, _(cmsgid), ap);
122 fprintf (stderr, "\n");
125 lto_wrapper_cleanup ();
126 exit (FATAL_EXIT_CODE);
130 /* Die when sys call fails. CMSGID is the error message. */
132 static void __attribute__ ((format (printf, 1, 2)))
133 fatal_perror (const char *cmsgid, ...)
138 va_start (ap, cmsgid);
139 fprintf (stderr, "lto-wrapper: ");
140 vfprintf (stderr, _(cmsgid), ap);
141 fprintf (stderr, ": %s\n", xstrerror (e));
144 lto_wrapper_cleanup ();
145 exit (FATAL_EXIT_CODE);
149 /* Execute a program, and wait for the reply. ARGV are the arguments. The
150 last one must be NULL. */
152 static struct pex_obj *
153 collect_execute (char **argv)
164 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
165 fprintf (stderr, " %s", str);
167 fprintf (stderr, "\n");
173 pex = pex_init (0, "lto-wrapper", NULL);
175 fatal_perror ("pex_init failed");
177 /* Do not use PEX_LAST here, we use our stdout for communicating with
178 collect2 or the linker-plugin. Any output from the sub-process
179 will confuse that. */
180 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
187 fatal_perror (errmsg);
197 /* Wait for a process to finish, and exit if a nonzero status is found.
198 PROG is the program name. PEX is the process we should wait for. */
201 collect_wait (const char *prog, struct pex_obj *pex)
205 if (!pex_get_status (pex, 1, &status))
206 fatal_perror ("can't get program status");
211 if (WIFSIGNALED (status))
213 int sig = WTERMSIG (status);
214 if (WCOREDUMP (status))
215 fatal ("%s terminated with signal %d [%s], core dumped",
216 prog, sig, strsignal (sig));
218 fatal ("%s terminated with signal %d [%s]",
219 prog, sig, strsignal (sig));
222 if (WIFEXITED (status))
223 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
230 /* Unlink a temporary LTRANS file unless requested otherwise. */
233 maybe_unlink_file (const char *file)
237 if (unlink_if_ordinary (file)
239 fatal_perror ("deleting LTRANS file %s", file);
242 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
246 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
249 fork_execute (char **argv)
257 args_name = make_temp_file (".args");
258 at_args = concat ("@", args_name, NULL);
259 args = fopen (args_name, "w");
261 fatal ("failed to open %s", args_name);
263 status = writeargv (&argv[1], args);
266 fatal ("could not write to temporary file %s", args_name);
270 new_argv[0] = argv[0];
271 new_argv[1] = at_args;
274 pex = collect_execute (new_argv);
275 collect_wait (new_argv[0], pex);
277 maybe_unlink_file (args_name);
282 /* Template of LTRANS dumpbase suffix. */
283 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
285 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
286 environment according to LANG_MASK. */
289 get_options_from_collect_gcc_options (const char *collect_gcc,
290 const char *collect_gcc_options,
291 unsigned int lang_mask,
292 struct cl_decoded_option **decoded_options,
293 unsigned int *decoded_options_count)
299 /* Count arguments. */
301 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
302 if (collect_gcc_options[j] == '\'')
305 fatal ("malformed COLLECT_GCC_OPTIONS");
307 /* Copy the options to a argv-like array. */
309 argv = (const char **) xmalloc ((argc + 2) * sizeof (char *));
310 argv[0] = collect_gcc;
311 argv_storage = xstrdup (collect_gcc_options);
312 for (i = 1, j = 0; argv_storage[j] != '\0'; ++j)
314 if (argv_storage[j] == '\'')
316 argv[i++] = &argv_storage[++j];
317 while (argv_storage[j] != '\'')
319 argv_storage[j] = '\0';
324 decode_cmdline_options_to_array (argc, (const char **)argv,
326 decoded_options, decoded_options_count);
331 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
334 run_gcc (unsigned argc, char *argv[])
337 const char **new_argv;
338 const char **argv_ptr;
339 char *list_option_full = NULL;
340 const char *linker_output = NULL;
341 const char *collect_gcc, *collect_gcc_options;
342 struct obstack env_obstack;
345 bool no_partition = false;
346 struct cl_decoded_option *decoded_options;
347 unsigned int decoded_options_count;
349 /* Get the driver and options. */
350 collect_gcc = getenv ("COLLECT_GCC");
352 fatal ("environment variable COLLECT_GCC must be set");
353 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
354 if (!collect_gcc_options)
355 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
356 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
359 &decoded_options_count);
361 /* Initalize the common arguments for the driver. */
362 new_argv = (const char **) xmalloc ((15 + decoded_options_count + argc)
365 *argv_ptr++ = collect_gcc;
366 *argv_ptr++ = "-xlto";
368 for (j = 1; j < decoded_options_count; ++j)
370 struct cl_decoded_option *option = &decoded_options[j];
372 /* Do not pass on frontend specific flags. */
373 if (!(cl_options[option->opt_index].flags
374 & (CL_COMMON|CL_TARGET|CL_DRIVER)))
377 switch (option->opt_index)
380 linker_output = option->arg;
381 /* We generate new intermediate output, drop this arg. */
392 case OPT_flto_partition_none:
397 if (strcmp (option->arg, "jobserver") == 0)
404 parallel = atoi (option->arg);
411 lto_mode = LTO_MODE_WHOPR;
412 /* We've handled these LTO options, do not pass them on. */
419 /* Pass the option on. */
420 *argv_ptr++ = option->orig_option_with_args_text;
425 lto_mode = LTO_MODE_LTO;
432 char *output_dir, *base, *name;
433 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
435 output_dir = xstrdup (linker_output);
437 for (name = base; *name; name++)
438 if (IS_DIR_SEPARATOR (*name))
442 linker_output = &linker_output[base - output_dir];
443 if (*output_dir == '\0')
445 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
446 output_dir = current_dir;
450 *argv_ptr++ = "-dumpdir";
451 *argv_ptr++ = output_dir;
454 *argv_ptr++ = "-dumpbase";
459 if (lto_mode == LTO_MODE_LTO)
461 flto_out = make_temp_file (".lto.o");
463 argv_ptr[0] = linker_output;
465 argv_ptr[2] = flto_out;
469 const char *list_option = "-fltrans-output-list=";
470 size_t list_option_len = strlen (list_option);
475 char *dumpbase = (char *) xmalloc (strlen (linker_output)
476 + sizeof (".wpa") + 1);
477 strcpy (dumpbase, linker_output);
478 strcat (dumpbase, ".wpa");
479 argv_ptr[0] = dumpbase;
482 if (linker_output && debug)
484 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
485 + sizeof (".ltrans.out") + 1);
486 strcpy (ltrans_output_file, linker_output);
487 strcat (ltrans_output_file, ".ltrans.out");
490 ltrans_output_file = make_temp_file (".ltrans.out");
491 list_option_full = (char *) xmalloc (sizeof (char) *
492 (strlen (ltrans_output_file) + list_option_len + 1));
493 tmp = list_option_full;
496 strcpy (tmp, list_option);
497 tmp += list_option_len;
498 strcpy (tmp, ltrans_output_file);
500 argv_ptr[2] = "-fwpa";
503 /* Append the input objects and possible preceeding arguments. */
504 for (i = 1; i < argc; ++i)
505 argv_ptr[2 + i] = argv[i];
506 argv_ptr[2 + i] = NULL;
508 fork_execute (CONST_CAST (char **, new_argv));
510 if (lto_mode == LTO_MODE_LTO)
512 printf("%s\n", flto_out);
518 FILE *stream = fopen (ltrans_output_file, "r");
519 FILE *mstream = NULL;
522 fatal_perror ("fopen: %s", ltrans_output_file);
524 /* Parse the list of LTRANS inputs from the WPA stage. */
528 const unsigned piece = 32;
529 char *output_name = NULL;
530 char *buf, *input_name = (char *)xmalloc (piece);
535 if (!fgets (buf, piece, stream))
537 len = strlen (input_name);
538 if (input_name[len - 1] != '\n')
540 input_name = (char *)xrealloc (input_name, len + piece);
541 buf = input_name + len;
544 input_name[len - 1] = '\0';
546 if (input_name[0] == '*')
547 output_name = &input_name[1];
550 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
551 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
552 input_names[nr-1] = input_name;
553 output_names[nr-1] = output_name;
556 maybe_unlink_file (ltrans_output_file);
557 ltrans_output_file = NULL;
561 makefile = make_temp_file (".mk");
562 mstream = fopen (makefile, "w");
565 /* Execute the LTRANS stage for each input file (or prepare a
566 makefile to invoke this in parallel). */
567 for (i = 0; i < nr; ++i)
570 char *input_name = input_names[i];
571 /* If it's a pass-through file do nothing. */
575 /* Replace the .o suffix with a .ltrans.o suffix and write
576 the resulting name to the LTRANS output list. */
577 obstack_init (&env_obstack);
578 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
579 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
580 output_name = XOBFINISH (&env_obstack, char *);
582 /* Adjust the dumpbase if the linker output file was seen. */
586 = (char *) xmalloc (strlen (linker_output)
587 + sizeof(DUMPBASE_SUFFIX) + 1);
589 strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
590 "%s.ltrans%u", linker_output, i);
591 argv_ptr[0] = dumpbase;
594 argv_ptr[1] = "-fltrans";
596 argv_ptr[3] = output_name;
597 argv_ptr[4] = input_name;
601 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
602 for (j = 1; new_argv[j] != NULL; ++j)
603 fprintf (mstream, " '%s'", new_argv[j]);
604 fprintf (mstream, "\n");
607 fork_execute (CONST_CAST (char **, new_argv));
609 output_names[i] = output_name;
616 fprintf (mstream, "all:");
617 for (i = 0; i < nr; ++i)
618 fprintf (mstream, " \\\n\t%s", output_names[i]);
619 fprintf (mstream, "\n");
623 /* Avoid passing --jobserver-fd= and similar flags
624 unless jobserver mode is explicitly enabled. */
625 putenv (xstrdup ("MAKEFLAGS="));
626 putenv (xstrdup ("MFLAGS="));
628 new_argv[0] = getenv ("MAKE");
630 new_argv[0] = "make";
632 new_argv[2] = makefile;
636 snprintf (jobs, 31, "-j%d", parallel);
637 new_argv[i++] = jobs;
639 new_argv[i++] = "all";
640 new_argv[i++] = NULL;
641 pex = collect_execute (CONST_CAST (char **, new_argv));
642 collect_wait (new_argv[0], pex);
643 maybe_unlink_file (makefile);
646 for (i = 0; i < nr; ++i)
648 fputs (output_names[i], stdout);
650 maybe_unlink_file (input_names[i]);
651 free (input_names[i]);
656 free (list_option_full);
659 obstack_free (&env_obstack, NULL);
666 main (int argc, char *argv[])
670 p = argv[0] + strlen (argv[0]);
671 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
675 xmalloc_set_program_name (progname);
679 diagnostic_initialize (global_dc, 0);
681 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
682 signal (SIGINT, fatal_signal);
684 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
685 signal (SIGHUP, fatal_signal);
687 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
688 signal (SIGTERM, fatal_signal);
690 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
691 signal (SIGPIPE, fatal_signal);
694 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
695 receive the signal. A different setting is inheritable */
696 signal (SIGCHLD, SIG_DFL);
699 /* We may be called with all the arguments stored in some file and
700 passed with @file. Expand them into argv before processing. */
701 expandargv (&argc, &argv);
703 run_gcc (argc, argv);