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"
45 #if ! defined( SIGCHLD ) && defined( SIGCLD )
46 # define SIGCHLD SIGCLD
49 #include "libiberty.h"
53 #define kill(p,s) raise(s)
56 int debug; /* true if -save-temps. */
57 int verbose; /* true if -v. */
60 LTO_MODE_NONE, /* Not doing LTO. */
61 LTO_MODE_LTO, /* Normal LTO. */
62 LTO_MODE_WHOPR /* WHOPR. */
65 /* Current LTO mode. */
66 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
68 static char *ltrans_output_file;
69 static char *flto_out;
70 static char *args_name;
71 static unsigned int nr;
72 static char **input_names;
73 static char **output_names;
74 static char *makefile;
76 static void maybe_unlink_file (const char *);
78 /* Delete tempfiles. */
81 lto_wrapper_cleanup (void)
83 static bool cleanup_done = false;
89 /* Setting cleanup_done prevents an infinite loop if one of the
90 calls to maybe_unlink_file fails. */
93 if (ltrans_output_file)
94 maybe_unlink_file (ltrans_output_file);
96 maybe_unlink_file (flto_out);
98 maybe_unlink_file (args_name);
100 maybe_unlink_file (makefile);
101 for (i = 0; i < nr; ++i)
103 maybe_unlink_file (input_names[i]);
105 maybe_unlink_file (output_names[i]);
110 fatal_signal (int signum)
112 signal (signum, SIG_DFL);
113 lto_wrapper_cleanup ();
114 /* Get the same signal again, this time not handled,
115 so its normal effect occurs. */
116 kill (getpid (), signum);
119 /* Just die. CMSGID is the error message. */
121 static void __attribute__ ((format (printf, 1, 2)))
122 fatal (const char * cmsgid, ...)
126 va_start (ap, cmsgid);
127 fprintf (stderr, "lto-wrapper: ");
128 vfprintf (stderr, _(cmsgid), ap);
129 fprintf (stderr, "\n");
132 lto_wrapper_cleanup ();
133 exit (FATAL_EXIT_CODE);
137 /* Die when sys call fails. CMSGID is the error message. */
139 static void __attribute__ ((format (printf, 1, 2)))
140 fatal_perror (const char *cmsgid, ...)
145 va_start (ap, cmsgid);
146 fprintf (stderr, "lto-wrapper: ");
147 vfprintf (stderr, _(cmsgid), ap);
148 fprintf (stderr, ": %s\n", xstrerror (e));
151 lto_wrapper_cleanup ();
152 exit (FATAL_EXIT_CODE);
156 /* Execute a program, and wait for the reply. ARGV are the arguments. The
157 last one must be NULL. */
159 static struct pex_obj *
160 collect_execute (char **argv)
171 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
172 fprintf (stderr, " %s", str);
174 fprintf (stderr, "\n");
180 pex = pex_init (0, "lto-wrapper", NULL);
182 fatal_perror ("pex_init failed");
184 /* Do not use PEX_LAST here, we use our stdout for communicating with
185 collect2 or the linker-plugin. Any output from the sub-process
186 will confuse that. */
187 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
194 fatal_perror (errmsg);
204 /* Wait for a process to finish, and exit if a nonzero status is found.
205 PROG is the program name. PEX is the process we should wait for. */
208 collect_wait (const char *prog, struct pex_obj *pex)
212 if (!pex_get_status (pex, 1, &status))
213 fatal_perror ("can't get program status");
218 if (WIFSIGNALED (status))
220 int sig = WTERMSIG (status);
221 if (WCOREDUMP (status))
222 fatal ("%s terminated with signal %d [%s], core dumped",
223 prog, sig, strsignal (sig));
225 fatal ("%s terminated with signal %d [%s]",
226 prog, sig, strsignal (sig));
229 if (WIFEXITED (status))
230 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
237 /* Unlink a temporary LTRANS file unless requested otherwise. */
240 maybe_unlink_file (const char *file)
244 if (unlink_if_ordinary (file)
246 fatal_perror ("deleting LTRANS file %s", file);
249 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
253 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
256 fork_execute (char **argv)
264 args_name = make_temp_file (".args");
265 at_args = concat ("@", args_name, NULL);
266 args = fopen (args_name, "w");
268 fatal ("failed to open %s", args_name);
270 status = writeargv (&argv[1], args);
273 fatal ("could not write to temporary file %s", args_name);
277 new_argv[0] = argv[0];
278 new_argv[1] = at_args;
281 pex = collect_execute (new_argv);
282 collect_wait (new_argv[0], pex);
284 maybe_unlink_file (args_name);
289 /* Template of LTRANS dumpbase suffix. */
290 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
292 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
295 run_gcc (unsigned argc, char *argv[])
298 const char **new_argv;
299 const char **argv_ptr;
300 char *list_option_full = NULL;
301 const char *linker_output = NULL;
302 const char *collect_gcc_options, *collect_gcc;
303 struct obstack env_obstack;
307 bool no_partition = false;
309 /* Get the driver and options. */
310 collect_gcc = getenv ("COLLECT_GCC");
312 fatal ("environment variable COLLECT_GCC must be set");
314 /* Set the CFLAGS environment variable. */
315 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
316 if (!collect_gcc_options)
317 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
319 /* Count arguments. */
321 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
322 if (collect_gcc_options[j] == '\'')
326 fatal ("malformed COLLECT_GCC_OPTIONS");
328 /* Initalize the common arguments for the driver. */
329 new_argv = (const char **) xmalloc ((15 + i / 2 + argc) * sizeof (char *));
331 *argv_ptr++ = collect_gcc;
332 *argv_ptr++ = "-xlto";
334 for (j = 0; collect_gcc_options[j] != '\0'; ++j)
335 if (collect_gcc_options[j] == '\'')
341 while (collect_gcc_options[j] != '\'')
344 obstack_init (&env_obstack);
345 obstack_grow (&env_obstack, &collect_gcc_options[i], j - i);
346 obstack_1grow (&env_obstack, 0);
347 option = XOBFINISH (&env_obstack, char *);
350 linker_output = option;
355 /* If we see -o, skip it and skip and record its argument. */
356 if (option[0] == '-' && option[1] == 'o')
358 if (option[2] == '\0')
361 linker_output = &option[2];
365 if (strcmp (option, "-save-temps") == 0)
367 if (strcmp (option, "-v") == 0)
370 if (strcmp (option, "-flto-partition=none") == 0)
372 /* We've handled these LTO options, do not pass them on. */
373 if (strncmp (option, "-flto=", 6) == 0
374 || !strcmp (option, "-flto"))
376 lto_mode = LTO_MODE_WHOPR;
377 if (option[5] == '=')
379 if (!strcmp (option + 6, "jobserver"))
386 parallel = atoi (option+8);
393 *argv_ptr++ = option;
397 lto_mode = LTO_MODE_LTO;
404 char *output_dir, *base, *name;
406 output_dir = xstrdup (linker_output);
408 for (name = base; *name; name++)
409 if (IS_DIR_SEPARATOR (*name))
413 linker_output = &linker_output[base - output_dir];
414 if (*output_dir == '\0')
416 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
417 output_dir = current_dir;
419 *argv_ptr++ = "-dumpdir";
420 *argv_ptr++ = output_dir;
422 *argv_ptr++ = "-dumpbase";
427 if (lto_mode == LTO_MODE_LTO)
429 flto_out = make_temp_file (".lto.o");
431 argv_ptr[0] = linker_output;
433 argv_ptr[2] = flto_out;
435 else if (lto_mode == LTO_MODE_WHOPR)
437 const char *list_option = "-fltrans-output-list=";
438 size_t list_option_len = strlen (list_option);
443 char *dumpbase = (char *) xmalloc (strlen (linker_output)
444 + sizeof (".wpa") + 1);
445 strcpy (dumpbase, linker_output);
446 strcat (dumpbase, ".wpa");
447 argv_ptr[0] = dumpbase;
450 if (linker_output && debug)
452 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
453 + sizeof (".ltrans.out") + 1);
454 strcpy (ltrans_output_file, linker_output);
455 strcat (ltrans_output_file, ".ltrans.out");
458 ltrans_output_file = make_temp_file (".ltrans.out");
459 list_option_full = (char *) xmalloc (sizeof (char) *
460 (strlen (ltrans_output_file) + list_option_len + 1));
461 tmp = list_option_full;
464 strcpy (tmp, list_option);
465 tmp += list_option_len;
466 strcpy (tmp, ltrans_output_file);
468 argv_ptr[2] = "-fwpa";
471 fatal ("invalid LTO mode");
473 /* Append the input objects and possible preceeding arguments. */
474 for (i = 1; i < argc; ++i)
475 argv_ptr[2 + i] = argv[i];
476 argv_ptr[2 + i] = NULL;
478 fork_execute (CONST_CAST (char **, new_argv));
480 if (lto_mode == LTO_MODE_LTO)
482 printf("%s\n", flto_out);
486 else if (lto_mode == LTO_MODE_WHOPR)
488 FILE *stream = fopen (ltrans_output_file, "r");
489 FILE *mstream = NULL;
492 fatal_perror ("fopen: %s", ltrans_output_file);
494 /* Parse the list of LTRANS inputs from the WPA stage. */
498 const unsigned piece = 32;
499 char *output_name = NULL;
500 char *buf, *input_name = (char *)xmalloc (piece);
505 if (!fgets (buf, piece, stream))
507 len = strlen (input_name);
508 if (input_name[len - 1] != '\n')
510 input_name = (char *)xrealloc (input_name, len + piece);
511 buf = input_name + len;
514 input_name[len - 1] = '\0';
516 if (input_name[0] == '*')
517 output_name = &input_name[1];
520 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
521 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
522 input_names[nr-1] = input_name;
523 output_names[nr-1] = output_name;
526 maybe_unlink_file (ltrans_output_file);
527 ltrans_output_file = NULL;
531 makefile = make_temp_file (".mk");
532 mstream = fopen (makefile, "w");
535 /* Execute the LTRANS stage for each input file (or prepare a
536 makefile to invoke this in parallel). */
537 for (i = 0; i < nr; ++i)
540 char *input_name = input_names[i];
541 /* If it's a pass-through file do nothing. */
545 /* Replace the .o suffix with a .ltrans.o suffix and write
546 the resulting name to the LTRANS output list. */
547 obstack_init (&env_obstack);
548 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
549 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
550 output_name = XOBFINISH (&env_obstack, char *);
552 /* Adjust the dumpbase if the linker output file was seen. */
556 = (char *) xmalloc (strlen (linker_output)
557 + sizeof(DUMPBASE_SUFFIX) + 1);
559 strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
560 "%s.ltrans%u", linker_output, i);
561 argv_ptr[0] = dumpbase;
564 argv_ptr[1] = "-fltrans";
566 argv_ptr[3] = output_name;
567 argv_ptr[4] = input_name;
571 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
572 for (j = 1; new_argv[j] != NULL; ++j)
573 fprintf (mstream, " '%s'", new_argv[j]);
574 fprintf (mstream, "\n");
577 fork_execute (CONST_CAST (char **, new_argv));
579 output_names[i] = output_name;
586 fprintf (mstream, "all:");
587 for (i = 0; i < nr; ++i)
588 fprintf (mstream, " \\\n\t%s", output_names[i]);
589 fprintf (mstream, "\n");
593 /* Avoid passing --jobserver-fd= and similar flags
594 unless jobserver mode is explicitly enabled. */
595 putenv (xstrdup ("MAKEFLAGS="));
596 putenv (xstrdup ("MFLAGS="));
598 new_argv[0] = getenv ("MAKE");
600 new_argv[0] = "make";
602 new_argv[2] = makefile;
606 snprintf (jobs, 31, "-j%d", parallel);
607 new_argv[i++] = jobs;
609 new_argv[i++] = "all";
610 new_argv[i++] = NULL;
611 pex = collect_execute (CONST_CAST (char **, new_argv));
612 collect_wait (new_argv[0], pex);
613 maybe_unlink_file (makefile);
616 for (i = 0; i < nr; ++i)
618 fputs (output_names[i], stdout);
620 maybe_unlink_file (input_names[i]);
621 free (input_names[i]);
626 free (list_option_full);
629 fatal ("invalid LTO mode");
631 obstack_free (&env_obstack, NULL);
638 main (int argc, char *argv[])
642 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
643 signal (SIGINT, fatal_signal);
645 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
646 signal (SIGHUP, fatal_signal);
648 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
649 signal (SIGTERM, fatal_signal);
651 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
652 signal (SIGPIPE, fatal_signal);
655 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
656 receive the signal. A different setting is inheritable */
657 signal (SIGCHLD, SIG_DFL);
660 /* We may be called with all the arguments stored in some file and
661 passed with @file. Expand them into argv before processing. */
662 expandargv (&argc, &argv);
663 run_gcc (argc, argv);