re PR bootstrap/41395 (Revision 151800 failed bootstrap)
[gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>. */
21
22
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.
27
28 Example:
29
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
31
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
34
35 If -fwhopr is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
38 */
39
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "tm.h"
44 #include "intl.h"
45 #include "libiberty.h"
46
47 int debug; /* true if -debug */
48
49 enum lto_mode_d {
50 LTO_MODE_NONE, /* Not doing LTO. */
51 LTO_MODE_LTO, /* Normal LTO. */
52 LTO_MODE_WHOPR /* WHOPR. */
53 };
54
55 /* Current LTO mode. */
56 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
57
58 /* Just die. CMSGID is the error message. */
59
60 static void __attribute__ ((format (printf, 1, 2)))
61 fatal (const char * cmsgid, ...)
62 {
63 va_list ap;
64
65 va_start (ap, cmsgid);
66 fprintf (stderr, "lto-wrapper: ");
67 vfprintf (stderr, _(cmsgid), ap);
68 fprintf (stderr, "\n");
69 va_end (ap);
70
71 exit (FATAL_EXIT_CODE);
72 }
73
74
75 /* Die when sys call fails. CMSGID is the error message. */
76
77 static void __attribute__ ((format (printf, 1, 2)))
78 fatal_perror (const char *cmsgid, ...)
79 {
80 int e = errno;
81 va_list ap;
82
83 va_start (ap, cmsgid);
84 fprintf (stderr, "lto-wrapper: ");
85 vfprintf (stderr, _(cmsgid), ap);
86 fprintf (stderr, ": %s\n", xstrerror (e));
87 va_end (ap);
88
89 exit (FATAL_EXIT_CODE);
90 }
91
92
93 /* Execute a program, and wait for the reply. ARGV are the arguments. The
94 last one must be NULL. */
95
96 static struct pex_obj *
97 collect_execute (char **argv)
98 {
99 struct pex_obj *pex;
100 const char *errmsg;
101 int err;
102
103 if (debug)
104 {
105 char **p_argv;
106 const char *str;
107
108 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
109 fprintf (stderr, " %s", str);
110
111 fprintf (stderr, "\n");
112 }
113
114 fflush (stdout);
115 fflush (stderr);
116
117 pex = pex_init (0, "lto-wrapper", NULL);
118 if (pex == NULL)
119 fatal_perror ("pex_init failed");
120
121 errmsg = pex_run (pex, PEX_LAST | PEX_SEARCH, argv[0], argv, NULL,
122 NULL, &err);
123 if (errmsg != NULL)
124 {
125 if (err != 0)
126 {
127 errno = err;
128 fatal_perror (errmsg);
129 }
130 else
131 fatal (errmsg);
132 }
133
134 return pex;
135 }
136
137
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. */
140
141 static int
142 collect_wait (const char *prog, struct pex_obj *pex)
143 {
144 int status;
145
146 if (!pex_get_status (pex, 1, &status))
147 fatal_perror ("can't get program status");
148 pex_free (pex);
149
150 if (status)
151 {
152 if (WIFSIGNALED (status))
153 {
154 int sig = WTERMSIG (status);
155 if (WCOREDUMP (status))
156 fatal ("%s terminated with signal %d [%s], core dumped",
157 prog, sig, strsignal (sig));
158 else
159 fatal ("%s terminated with signal %d [%s]",
160 prog, sig, strsignal (sig));
161 }
162
163 if (WIFEXITED (status))
164 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
165 }
166
167 return 0;
168 }
169
170
171 /* Unlink a temporary LTRANS file unless requested otherwise. */
172
173 static void
174 maybe_unlink_file (const char *file)
175 {
176 if (! debug)
177 {
178 if (unlink_if_ordinary (file))
179 fatal_perror ("deleting LTRANS file %s", file);
180 }
181 else
182 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
183 }
184
185
186 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
187
188 static void
189 fork_execute (char **argv)
190 {
191 struct pex_obj *pex;
192 char *new_argv[3];
193 char *args_name = make_temp_file (".args");
194 char *at_args = concat ("@", args_name, NULL);
195 FILE *args = fopen (args_name, "w");
196 int status;
197
198 if (args == NULL)
199 fatal ("failed to open %s", args_name);
200
201 status = writeargv (&argv[1], args);
202
203 if (status)
204 fatal ("could not write to temporary file %s", args_name);
205
206 fclose (args);
207
208 new_argv[0] = argv[0];
209 new_argv[1] = at_args;
210 new_argv[2] = NULL;
211
212 pex = collect_execute (new_argv);
213 collect_wait (new_argv[0], pex);
214
215 maybe_unlink_file (args_name);
216 free (args_name);
217 free (at_args);
218 }
219
220
221 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
222
223 static void
224 run_gcc (unsigned argc, char *argv[])
225 {
226 unsigned i;
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;
233
234 new_argc += 8;
235 new_argv = (const char **) xcalloc (sizeof (char *), new_argc);
236
237 argv_ptr = new_argv;
238
239 *argv_ptr++ = argv[0];
240 *argv_ptr++ = "-combine";
241 *argv_ptr++ = "-x";
242 *argv_ptr++ = "lto";
243 *argv_ptr++ = "-c";
244 if (lto_mode == LTO_MODE_LTO)
245 {
246 flto_out = make_temp_file (".lto.o");
247 *argv_ptr++ = "-o";
248 *argv_ptr++ = flto_out;
249 }
250 else if (lto_mode == LTO_MODE_WHOPR)
251 {
252 const char *list_option = "-fltrans-output-list=";
253 size_t list_option_len = strlen (list_option);
254 char *tmp;
255
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;
260
261 *argv_ptr++ = tmp;
262 strcpy (tmp, list_option);
263 tmp += list_option_len;
264 strcpy (tmp, ltrans_output_file);
265
266 *argv_ptr++ = "-fwpa";
267 }
268 else
269 fatal ("invalid LTO mode");
270
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++)
280 {
281 const char *s = argv[i];
282
283 if (strcmp (s, "-flto") == 0 || strcmp (s, "-fwhopr") == 0)
284 /* We've handled this LTO option, don't pass it on. */
285 ;
286 else if (*s == '-' && s[1] == 'o')
287 {
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. */
291 if (s[2] == '\0')
292 i++;
293 }
294 else
295 /* Pass the option or argument to LTO. */
296 *argv_ptr++ = s;
297 }
298
299 *argv_ptr = NULL;
300
301 fork_execute (CONST_CAST (char **, new_argv));
302 free (new_argv);
303 new_argv = NULL;
304
305 if (lto_mode == LTO_MODE_LTO)
306 {
307 printf("%s\n", flto_out);
308 free (flto_out);
309 flto_out = NULL;
310 }
311 else if (lto_mode == LTO_MODE_WHOPR)
312 {
313 FILE *stream = fopen (ltrans_output_file, "r");
314 int c;
315
316 if (!stream)
317 fatal_perror ("fopen: %s", ltrans_output_file);
318
319 while ((c = getc (stream)) != EOF)
320 putc (c, stdout);
321 fclose (stream);
322 maybe_unlink_file (ltrans_output_file);
323 free (ltrans_output_file);
324 free (list_option_full);
325 }
326 else
327 fatal ("invalid LTO mode");
328 }
329
330
331 /* Parse the command line. Copy any unused argument to GCC_ARGV. ARGC is the
332 number of arguments. ARGV contains the arguments. */
333
334 static int
335 process_args (int argc, char *argv[], char *gcc_argv[])
336 {
337 int i;
338 int j = 0;
339
340 for (i = 1; i < argc; i ++)
341 {
342 if (! strcmp (argv[i], "-debug"))
343 debug = 1;
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;
348 else
349 {
350 gcc_argv[j] = argv[i];
351 j++;
352 }
353 }
354
355 return j;
356 }
357
358
359 /* Entry point. */
360
361 int
362 main (int argc, char *argv[])
363 {
364 char **gcc_argv;
365 int gcc_argc;
366
367 gcc_init_libintl ();
368
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);
375 free (gcc_argv);
376
377 return 0;
378 }