1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009 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 -fwhopr 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 #include "libiberty.h"
47 int debug
; /* true if -debug */
50 LTO_MODE_NONE
, /* Not doing LTO. */
51 LTO_MODE_LTO
, /* Normal LTO. */
52 LTO_MODE_WHOPR
/* WHOPR. */
55 /* Current LTO mode. */
56 static enum lto_mode_d lto_mode
= LTO_MODE_NONE
;
58 /* Just die. CMSGID is the error message. */
60 static void __attribute__ ((format (printf
, 1, 2)))
61 fatal (const char * cmsgid
, ...)
65 va_start (ap
, cmsgid
);
66 fprintf (stderr
, "lto-wrapper: ");
67 vfprintf (stderr
, _(cmsgid
), ap
);
68 fprintf (stderr
, "\n");
71 exit (FATAL_EXIT_CODE
);
75 /* Die when sys call fails. CMSGID is the error message. */
77 static void __attribute__ ((format (printf
, 1, 2)))
78 fatal_perror (const char *cmsgid
, ...)
83 va_start (ap
, cmsgid
);
84 fprintf (stderr
, "lto-wrapper: ");
85 vfprintf (stderr
, _(cmsgid
), ap
);
86 fprintf (stderr
, ": %s\n", xstrerror (e
));
89 exit (FATAL_EXIT_CODE
);
93 /* Execute a program, and wait for the reply. ARGV are the arguments. The
94 last one must be NULL. */
96 static struct pex_obj
*
97 collect_execute (char **argv
)
108 for (p_argv
= argv
; (str
= *p_argv
) != (char *) 0; p_argv
++)
109 fprintf (stderr
, " %s", str
);
111 fprintf (stderr
, "\n");
117 pex
= pex_init (0, "lto-wrapper", NULL
);
119 fatal_perror ("pex_init failed");
121 errmsg
= pex_run (pex
, PEX_LAST
| PEX_SEARCH
, argv
[0], argv
, NULL
,
128 fatal_perror (errmsg
);
138 /* Wait for a process to finish, and exit if a nonzero status is found.
139 PROG is the program name. PEX is the process we should wait for. */
142 collect_wait (const char *prog
, struct pex_obj
*pex
)
146 if (!pex_get_status (pex
, 1, &status
))
147 fatal_perror ("can't get program status");
152 if (WIFSIGNALED (status
))
154 int sig
= WTERMSIG (status
);
155 if (WCOREDUMP (status
))
156 fatal ("%s terminated with signal %d [%s], core dumped",
157 prog
, sig
, strsignal (sig
));
159 fatal ("%s terminated with signal %d [%s]",
160 prog
, sig
, strsignal (sig
));
163 if (WIFEXITED (status
))
164 fatal ("%s returned %d exit status", prog
, WEXITSTATUS (status
));
171 /* Unlink a temporary LTRANS file unless requested otherwise. */
174 maybe_unlink_file (const char *file
)
178 if (unlink_if_ordinary (file
))
179 fatal_perror ("deleting LTRANS file %s", file
);
182 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
186 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
189 fork_execute (char **argv
)
193 char *args_name
= make_temp_file (".args");
194 char *at_args
= concat ("@", args_name
, NULL
);
195 FILE *args
= fopen (args_name
, "w");
199 fatal ("failed to open %s", args_name
);
201 status
= writeargv (&argv
[1], args
);
204 fatal ("could not write to temporary file %s", args_name
);
208 new_argv
[0] = argv
[0];
209 new_argv
[1] = at_args
;
212 pex
= collect_execute (new_argv
);
213 collect_wait (new_argv
[0], pex
);
215 maybe_unlink_file (args_name
);
221 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
224 run_gcc (unsigned argc
, char *argv
[])
227 unsigned new_argc
= argc
;
228 const char **new_argv
;
229 const char **argv_ptr
;
230 char *ltrans_output_file
= NULL
;
231 char *flto_out
= NULL
;
232 char *list_option_full
= NULL
;
235 new_argv
= (const char **) xcalloc (sizeof (char *), new_argc
);
239 *argv_ptr
++ = argv
[0];
240 *argv_ptr
++ = "-combine";
244 if (lto_mode
== LTO_MODE_LTO
)
246 flto_out
= make_temp_file (".lto.o");
248 *argv_ptr
++ = flto_out
;
250 else if (lto_mode
== LTO_MODE_WHOPR
)
252 const char *list_option
= "-fltrans-output-list=";
253 size_t list_option_len
= strlen (list_option
);
256 ltrans_output_file
= make_temp_file (".ltrans.out");
257 list_option_full
= (char *) xmalloc (sizeof (char) *
258 (strlen (ltrans_output_file
) + list_option_len
+ 1));
259 tmp
= list_option_full
;
262 strcpy (tmp
, list_option
);
263 tmp
+= list_option_len
;
264 strcpy (tmp
, ltrans_output_file
);
266 *argv_ptr
++ = "-fwpa";
269 fatal ("invalid LTO mode");
271 /* Add inherited GCC options to the LTO back end command line.
272 Filter out some obviously inappropriate options that will
273 conflict with the options that we force above. We pass
274 all of the remaining options on to LTO, and let it complain
275 about any it doesn't like. Note that we invoke LTO via the
276 `gcc' driver, so the usual option processing takes place.
277 Except for `-flto' and `-fwhopr', we should only filter options that
278 are meaningful to `ld', lest an option go silently unclaimed. */
279 for (i
= 1; i
< argc
; i
++)
281 const char *s
= argv
[i
];
283 if (strcmp (s
, "-flto") == 0 || strcmp (s
, "-fwhopr") == 0)
284 /* We've handled this LTO option, don't pass it on. */
286 else if (*s
== '-' && s
[1] == 'o')
288 /* Drop `-o' and its filename argument. We will use a
289 temporary file for the LTO output. The `-o' option
290 will be interpreted by the linker. */
295 /* Pass the option or argument to LTO. */
301 fork_execute (CONST_CAST (char **, new_argv
));
305 if (lto_mode
== LTO_MODE_LTO
)
307 printf("%s\n", flto_out
);
311 else if (lto_mode
== LTO_MODE_WHOPR
)
313 FILE *stream
= fopen (ltrans_output_file
, "r");
317 fatal_perror ("fopen: %s", ltrans_output_file
);
319 while ((c
= getc (stream
)) != EOF
)
322 maybe_unlink_file (ltrans_output_file
);
323 free (ltrans_output_file
);
324 free (list_option_full
);
327 fatal ("invalid LTO mode");
331 /* Parse the command line. Copy any unused argument to GCC_ARGV. ARGC is the
332 number of arguments. ARGV contains the arguments. */
335 process_args (int argc
, char *argv
[], char *gcc_argv
[])
340 for (i
= 1; i
< argc
; i
++)
342 if (! strcmp (argv
[i
], "-debug"))
344 else if (! strcmp (argv
[i
], "-flto"))
345 lto_mode
= LTO_MODE_LTO
;
346 else if (! strcmp (argv
[i
], "-fwhopr"))
347 lto_mode
= LTO_MODE_WHOPR
;
350 gcc_argv
[j
] = argv
[i
];
362 main (int argc
, char *argv
[])
369 /* We may be called with all the arguments stored in some file and
370 passed with @file. Expand them into argv before processing. */
371 expandargv (&argc
, &argv
);
372 gcc_argv
= (char **) xcalloc (sizeof (char *), argc
);
373 gcc_argc
= process_args (argc
, argv
, gcc_argv
);
374 run_gcc (gcc_argc
, gcc_argv
);