]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/lto-wrapper.c
2014-03-06 Richard Biener <rguenther@suse.de>
[thirdparty/gcc.git] / gcc / lto-wrapper.c
CommitLineData
7bfefa9d 1/* Wrapper to call lto. Used by collect2 and the linker plugin.
3aea1f79 2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
7bfefa9d 3
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along 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
cbcf2791 35 If WHOPR is used instead, more than one file might be produced
7bfefa9d 36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
38*/
39
40#include "config.h"
41#include "system.h"
960ebfe7 42#include "coretypes.h"
7bfefa9d 43#include "intl.h"
0b4f4daf 44#include "diagnostic.h"
264cbb51 45#include "obstack.h"
00d389c8 46#include "opts.h"
47#include "options.h"
954825c9 48#include "simple-object.h"
49
50/* From lto-streamer.h which we cannot include with -fkeep-inline-functions.
51 ??? Split out a lto-streamer-core.h. */
52
53#define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
54
55/* End of lto-streamer.h copy. */
7bfefa9d 56
c8c65e07 57int debug; /* true if -save-temps. */
58int verbose; /* true if -v. */
7bfefa9d 59
60enum lto_mode_d {
c8c65e07 61 LTO_MODE_NONE, /* Not doing LTO. */
62 LTO_MODE_LTO, /* Normal LTO. */
63 LTO_MODE_WHOPR /* WHOPR. */
7bfefa9d 64};
65
66/* Current LTO mode. */
67static enum lto_mode_d lto_mode = LTO_MODE_NONE;
68
c515f146 69static char *ltrans_output_file;
70static char *flto_out;
71static char *args_name;
4062f557 72static unsigned int nr;
73static char **input_names;
74static char **output_names;
75static char *makefile;
c515f146 76
77static void maybe_unlink_file (const char *);
78
c04a3a63 79 /* Delete tempfiles. */
c515f146 80
81static void
c04a3a63 82lto_wrapper_cleanup (void)
c515f146 83{
3c1d5969 84 static bool cleanup_done = false;
c04a3a63 85 unsigned int i;
86
87 if (cleanup_done)
88 return;
89
90 /* Setting cleanup_done prevents an infinite loop if one of the
91 calls to maybe_unlink_file fails. */
92 cleanup_done = true;
93
94 if (ltrans_output_file)
95 maybe_unlink_file (ltrans_output_file);
96 if (flto_out)
97 maybe_unlink_file (flto_out);
98 if (args_name)
99 maybe_unlink_file (args_name);
100 if (makefile)
101 maybe_unlink_file (makefile);
102 for (i = 0; i < nr; ++i)
3c1d5969 103 {
c04a3a63 104 maybe_unlink_file (input_names[i]);
105 if (output_names[i])
106 maybe_unlink_file (output_names[i]);
3c1d5969 107 }
c04a3a63 108}
109
110static void
111fatal_signal (int signum)
112{
113 signal (signum, SIG_DFL);
114 lto_wrapper_cleanup ();
115 /* Get the same signal again, this time not handled,
116 so its normal effect occurs. */
117 kill (getpid (), signum);
c515f146 118}
119
7bfefa9d 120/* Just die. CMSGID is the error message. */
121
122static void __attribute__ ((format (printf, 1, 2)))
123fatal (const char * cmsgid, ...)
124{
125 va_list ap;
126
127 va_start (ap, cmsgid);
128 fprintf (stderr, "lto-wrapper: ");
129 vfprintf (stderr, _(cmsgid), ap);
130 fprintf (stderr, "\n");
131 va_end (ap);
132
c04a3a63 133 lto_wrapper_cleanup ();
134 exit (FATAL_EXIT_CODE);
7bfefa9d 135}
136
137
138/* Die when sys call fails. CMSGID is the error message. */
139
140static void __attribute__ ((format (printf, 1, 2)))
141fatal_perror (const char *cmsgid, ...)
142{
143 int e = errno;
144 va_list ap;
145
146 va_start (ap, cmsgid);
147 fprintf (stderr, "lto-wrapper: ");
148 vfprintf (stderr, _(cmsgid), ap);
149 fprintf (stderr, ": %s\n", xstrerror (e));
150 va_end (ap);
151
c04a3a63 152 lto_wrapper_cleanup ();
153 exit (FATAL_EXIT_CODE);
7bfefa9d 154}
155
156
157/* Execute a program, and wait for the reply. ARGV are the arguments. The
158 last one must be NULL. */
159
160static struct pex_obj *
161collect_execute (char **argv)
162{
163 struct pex_obj *pex;
164 const char *errmsg;
165 int err;
166
c8c65e07 167 if (verbose)
7bfefa9d 168 {
169 char **p_argv;
170 const char *str;
171
172 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
173 fprintf (stderr, " %s", str);
174
175 fprintf (stderr, "\n");
176 }
177
178 fflush (stdout);
179 fflush (stderr);
180
181 pex = pex_init (0, "lto-wrapper", NULL);
182 if (pex == NULL)
183 fatal_perror ("pex_init failed");
184
cba6710d 185 /* Do not use PEX_LAST here, we use our stdout for communicating with
186 collect2 or the linker-plugin. Any output from the sub-process
187 will confuse that. */
188 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
7bfefa9d 189 NULL, &err);
190 if (errmsg != NULL)
191 {
192 if (err != 0)
193 {
194 errno = err;
195 fatal_perror (errmsg);
196 }
197 else
198 fatal (errmsg);
199 }
200
201 return pex;
202}
203
204
205/* Wait for a process to finish, and exit if a nonzero status is found.
206 PROG is the program name. PEX is the process we should wait for. */
207
208static int
209collect_wait (const char *prog, struct pex_obj *pex)
210{
211 int status;
212
213 if (!pex_get_status (pex, 1, &status))
214 fatal_perror ("can't get program status");
215 pex_free (pex);
216
217 if (status)
218 {
219 if (WIFSIGNALED (status))
220 {
221 int sig = WTERMSIG (status);
222 if (WCOREDUMP (status))
223 fatal ("%s terminated with signal %d [%s], core dumped",
224 prog, sig, strsignal (sig));
225 else
226 fatal ("%s terminated with signal %d [%s]",
227 prog, sig, strsignal (sig));
228 }
229
230 if (WIFEXITED (status))
231 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
232 }
233
234 return 0;
235}
236
237
238/* Unlink a temporary LTRANS file unless requested otherwise. */
239
240static void
241maybe_unlink_file (const char *file)
242{
243 if (! debug)
244 {
411503bd 245 if (unlink_if_ordinary (file)
246 && errno != ENOENT)
7bfefa9d 247 fatal_perror ("deleting LTRANS file %s", file);
248 }
249 else
250 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
251}
252
253
254/* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
255
256static void
257fork_execute (char **argv)
258{
259 struct pex_obj *pex;
260 char *new_argv[3];
c515f146 261 char *at_args;
262 FILE *args;
7bfefa9d 263 int status;
264
c515f146 265 args_name = make_temp_file (".args");
266 at_args = concat ("@", args_name, NULL);
267 args = fopen (args_name, "w");
7bfefa9d 268 if (args == NULL)
269 fatal ("failed to open %s", args_name);
270
271 status = writeargv (&argv[1], args);
272
273 if (status)
274 fatal ("could not write to temporary file %s", args_name);
275
276 fclose (args);
277
278 new_argv[0] = argv[0];
279 new_argv[1] = at_args;
280 new_argv[2] = NULL;
281
282 pex = collect_execute (new_argv);
283 collect_wait (new_argv[0], pex);
284
285 maybe_unlink_file (args_name);
411503bd 286 args_name = NULL;
7bfefa9d 287 free (at_args);
288}
289
264cbb51 290/* Template of LTRANS dumpbase suffix. */
291#define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
7bfefa9d 292
00d389c8 293/* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
294 environment according to LANG_MASK. */
295
296static void
297get_options_from_collect_gcc_options (const char *collect_gcc,
298 const char *collect_gcc_options,
299 unsigned int lang_mask,
300 struct cl_decoded_option **decoded_options,
301 unsigned int *decoded_options_count)
302{
05f1abc2 303 struct obstack argv_obstack;
00d389c8 304 char *argv_storage;
305 const char **argv;
05f1abc2 306 int j, k, argc;
307
00d389c8 308 argv_storage = xstrdup (collect_gcc_options);
05f1abc2 309 obstack_init (&argv_obstack);
310 obstack_ptr_grow (&argv_obstack, collect_gcc);
311
312 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
00d389c8 313 {
314 if (argv_storage[j] == '\'')
315 {
05f1abc2 316 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
317 ++j;
318 do
319 {
320 if (argv_storage[j] == '\0')
321 fatal ("malformed COLLECT_GCC_OPTIONS");
322 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
323 {
324 argv_storage[k++] = '\'';
325 j += 4;
326 }
327 else if (argv_storage[j] == '\'')
328 break;
329 else
330 argv_storage[k++] = argv_storage[j++];
331 }
332 while (1);
333 argv_storage[k++] = '\0';
00d389c8 334 }
335 }
05f1abc2 336
337 obstack_ptr_grow (&argv_obstack, NULL);
338 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
339 argv = XOBFINISH (&argv_obstack, const char **);
00d389c8 340
341 decode_cmdline_options_to_array (argc, (const char **)argv,
342 lang_mask,
343 decoded_options, decoded_options_count);
05f1abc2 344 obstack_free (&argv_obstack, NULL);
00d389c8 345}
346
954825c9 347/* Append OPTION to the options array DECODED_OPTIONS with size
348 DECODED_OPTIONS_COUNT. */
349
350static void
351append_option (struct cl_decoded_option **decoded_options,
352 unsigned int *decoded_options_count,
353 struct cl_decoded_option *option)
354{
355 ++*decoded_options_count;
356 *decoded_options
357 = (struct cl_decoded_option *)
358 xrealloc (*decoded_options,
359 (*decoded_options_count
360 * sizeof (struct cl_decoded_option)));
361 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
362 sizeof (struct cl_decoded_option));
363}
364
365/* Try to merge and complain about options FDECODED_OPTIONS when applied
366 ontop of DECODED_OPTIONS. */
367
368static void
369merge_and_complain (struct cl_decoded_option **decoded_options,
370 unsigned int *decoded_options_count,
371 struct cl_decoded_option *fdecoded_options,
372 unsigned int fdecoded_options_count)
373{
374 unsigned int i, j;
375
376 /* ??? Merge options from files. Most cases can be
377 handled by either unioning or intersecting
378 (for example -fwrapv is a case for unioning,
379 -ffast-math is for intersection). Most complaints
380 about real conflicts between different options can
381 be deferred to the compiler proper. Options that
382 we can neither safely handle by intersection nor
383 unioning would need to be complained about here.
384 Ideally we'd have a flag in the opt files that
385 tells whether to union or intersect or reject.
386 In absence of that it's unclear what a good default is.
387 It's also difficult to get positional handling correct. */
388
389 /* The following does what the old LTO option code did,
390 union all target and a selected set of common options. */
391 for (i = 0; i < fdecoded_options_count; ++i)
392 {
393 struct cl_decoded_option *foption = &fdecoded_options[i];
394 switch (foption->opt_index)
395 {
645c2597 396 case OPT_SPECIAL_unknown:
397 case OPT_SPECIAL_ignore:
398 case OPT_SPECIAL_program_name:
399 case OPT_SPECIAL_input_file:
400 break;
401
954825c9 402 default:
403 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
404 break;
405
406 /* Fallthru. */
407 case OPT_fPIC:
408 case OPT_fpic:
409 case OPT_fpie:
410 case OPT_fcommon:
411 case OPT_fexceptions:
8eabe518 412 case OPT_fnon_call_exceptions:
50f418ca 413 case OPT_fgnu_tm:
954825c9 414 /* Do what the old LTO code did - collect exactly one option
415 setting per OPT code, we pick the first we encounter.
416 ??? This doesn't make too much sense, but when it doesn't
417 then we should complain. */
418 for (j = 0; j < *decoded_options_count; ++j)
419 if ((*decoded_options)[j].opt_index == foption->opt_index)
420 break;
421 if (j == *decoded_options_count)
422 append_option (decoded_options, decoded_options_count, foption);
423 break;
940a2ab8 424
e38bbd96 425 case OPT_ftrapv:
426 case OPT_fstrict_overflow:
5a91aea7 427 case OPT_ffp_contract_:
428 /* For selected options we can merge conservatively. */
429 for (j = 0; j < *decoded_options_count; ++j)
430 if ((*decoded_options)[j].opt_index == foption->opt_index)
431 break;
432 if (j == *decoded_options_count)
433 append_option (decoded_options, decoded_options_count, foption);
e38bbd96 434 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
435 -fno-trapv < -ftrapv,
436 -fno-strict-overflow < -fstrict-overflow */
5a91aea7 437 else if (foption->value < (*decoded_options)[j].value)
438 (*decoded_options)[j] = *foption;
439 break;
440
e38bbd96 441 case OPT_fwrapv:
442 /* For selected options we can merge conservatively. */
443 for (j = 0; j < *decoded_options_count; ++j)
444 if ((*decoded_options)[j].opt_index == foption->opt_index)
445 break;
446 if (j == *decoded_options_count)
447 append_option (decoded_options, decoded_options_count, foption);
448 /* -fwrapv > -fno-wrapv. */
449 else if (foption->value > (*decoded_options)[j].value)
450 (*decoded_options)[j] = *foption;
451 break;
452
940a2ab8 453 case OPT_freg_struct_return:
454 case OPT_fpcc_struct_return:
455 for (j = 0; j < *decoded_options_count; ++j)
456 if ((*decoded_options)[j].opt_index == foption->opt_index)
457 break;
458 if (j == *decoded_options_count)
459 fatal ("Option %s not used consistently in all LTO input files",
460 foption->orig_option_with_args_text);
461 break;
958d7687 462
463 case OPT_O:
464 case OPT_Ofast:
465 case OPT_Og:
466 case OPT_Os:
467 for (j = 0; j < *decoded_options_count; ++j)
468 if ((*decoded_options)[j].opt_index == OPT_O
469 || (*decoded_options)[j].opt_index == OPT_Ofast
470 || (*decoded_options)[j].opt_index == OPT_Og
471 || (*decoded_options)[j].opt_index == OPT_Os)
472 break;
473 if (j == *decoded_options_count)
474 append_option (decoded_options, decoded_options_count, foption);
475 else if ((*decoded_options)[j].opt_index == foption->opt_index
476 && foption->opt_index != OPT_O)
477 /* Exact same options get merged. */
478 ;
479 else
480 {
481 /* For mismatched option kinds preserve the optimization
482 level only, thus merge it as -On. This also handles
483 merging of same optimization level -On. */
484 int level = 0;
485 switch (foption->opt_index)
486 {
487 case OPT_O:
488 if (foption->arg[0] == '\0')
489 level = MAX (level, 1);
490 else
491 level = MAX (level, atoi (foption->arg));
492 break;
493 case OPT_Ofast:
494 level = MAX (level, 3);
495 break;
496 case OPT_Og:
497 level = MAX (level, 1);
498 break;
499 case OPT_Os:
500 level = MAX (level, 2);
501 break;
502 default:
503 gcc_unreachable ();
504 }
505 switch ((*decoded_options)[j].opt_index)
506 {
507 case OPT_O:
508 if ((*decoded_options)[j].arg[0] == '\0')
509 level = MAX (level, 1);
510 else
511 level = MAX (level, atoi ((*decoded_options)[j].arg));
512 break;
513 case OPT_Ofast:
514 level = MAX (level, 3);
515 break;
516 case OPT_Og:
517 level = MAX (level, 1);
518 break;
519 case OPT_Os:
520 level = MAX (level, 2);
521 break;
522 default:
523 gcc_unreachable ();
524 }
525 (*decoded_options)[j].opt_index = OPT_O;
526 char *tem;
527 asprintf (&tem, "-O%d", level);
528 (*decoded_options)[j].arg = &tem[2];
529 (*decoded_options)[j].canonical_option[0] = tem;
530 (*decoded_options)[j].value = 1;
531 }
532 break;
954825c9 533 }
534 }
535}
00d389c8 536
7bfefa9d 537/* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
538
539static void
540run_gcc (unsigned argc, char *argv[])
541{
c8c65e07 542 unsigned i, j;
7bfefa9d 543 const char **new_argv;
544 const char **argv_ptr;
7bfefa9d 545 char *list_option_full = NULL;
c8c65e07 546 const char *linker_output = NULL;
00d389c8 547 const char *collect_gcc, *collect_gcc_options;
cba6710d 548 int parallel = 0;
442bd81c 549 int jobserver = 0;
cbcf2791 550 bool no_partition = false;
954825c9 551 struct cl_decoded_option *fdecoded_options = NULL;
552 unsigned int fdecoded_options_count = 0;
00d389c8 553 struct cl_decoded_option *decoded_options;
554 unsigned int decoded_options_count;
b84346c6 555 struct obstack argv_obstack;
556 int new_head_argc;
c8c65e07 557
558 /* Get the driver and options. */
559 collect_gcc = getenv ("COLLECT_GCC");
560 if (!collect_gcc)
561 fatal ("environment variable COLLECT_GCC must be set");
c8c65e07 562 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
563 if (!collect_gcc_options)
564 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
00d389c8 565 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
566 CL_LANG_ALL,
567 &decoded_options,
568 &decoded_options_count);
c8c65e07 569
954825c9 570 /* Look at saved options in the IL files. */
571 for (i = 1; i < argc; ++i)
572 {
573 char *data, *p;
574 char *fopts;
575 int fd;
576 const char *errmsg;
577 int err;
578 off_t file_offset = 0, offset, length;
579 long loffset;
580 simple_object_read *sobj;
581 int consumed;
582 struct cl_decoded_option *f2decoded_options;
583 unsigned int f2decoded_options_count;
584 char *filename = argv[i];
585 if ((p = strrchr (argv[i], '@'))
586 && p != argv[i]
587 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
588 && strlen (p) == (unsigned int) consumed)
589 {
590 filename = XNEWVEC (char, p - argv[i] + 1);
591 memcpy (filename, argv[i], p - argv[i]);
592 filename[p - argv[i]] = '\0';
593 file_offset = (off_t) loffset;
594 }
595 fd = open (argv[i], O_RDONLY);
596 if (fd == -1)
597 continue;
38fe0c89 598 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
599 &errmsg, &err);
954825c9 600 if (!sobj)
601 {
602 close (fd);
603 continue;
604 }
605 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
606 &offset, &length, &errmsg, &err))
607 {
608 simple_object_release_read (sobj);
609 close (fd);
610 continue;
611 }
612 lseek (fd, file_offset + offset, SEEK_SET);
613 data = (char *)xmalloc (length);
614 read (fd, data, length);
615 fopts = data;
616 do
617 {
618 get_options_from_collect_gcc_options (collect_gcc,
619 fopts, CL_LANG_ALL,
620 &f2decoded_options,
621 &f2decoded_options_count);
622 if (!fdecoded_options)
623 {
624 fdecoded_options = f2decoded_options;
625 fdecoded_options_count = f2decoded_options_count;
626 }
627 else
628 merge_and_complain (&fdecoded_options,
629 &fdecoded_options_count,
630 f2decoded_options, f2decoded_options_count);
631
632 fopts += strlen (fopts) + 1;
633 }
634 while (fopts - data < length);
635
636 free (data);
637 simple_object_release_read (sobj);
638 close (fd);
639 }
640
c8c65e07 641 /* Initalize the common arguments for the driver. */
b84346c6 642 obstack_init (&argv_obstack);
643 obstack_ptr_grow (&argv_obstack, collect_gcc);
644 obstack_ptr_grow (&argv_obstack, "-xlto");
645 obstack_ptr_grow (&argv_obstack, "-c");
954825c9 646
647 /* Append compiler driver arguments as far as they were merged. */
648 for (j = 1; j < fdecoded_options_count; ++j)
649 {
650 struct cl_decoded_option *option = &fdecoded_options[j];
651
652 /* File options have been properly filtered by lto-opts.c. */
653 switch (option->opt_index)
654 {
655 /* Drop arguments that we want to take from the link line. */
656 case OPT_flto_:
657 case OPT_flto:
658 case OPT_flto_partition_none:
659 case OPT_flto_partition_1to1:
660 case OPT_flto_partition_balanced:
661 continue;
662
663 default:
664 break;
665 }
666
667 /* For now do what the original LTO option code was doing - pass
668 on any CL_TARGET flag and a few selected others. */
669 switch (option->opt_index)
670 {
671 case OPT_fPIC:
672 case OPT_fpic:
673 case OPT_fpie:
674 case OPT_fcommon:
675 case OPT_fexceptions:
8eabe518 676 case OPT_fnon_call_exceptions:
50f418ca 677 case OPT_fgnu_tm:
940a2ab8 678 case OPT_freg_struct_return:
679 case OPT_fpcc_struct_return:
5a91aea7 680 case OPT_ffp_contract_:
e38bbd96 681 case OPT_fwrapv:
682 case OPT_ftrapv:
683 case OPT_fstrict_overflow:
958d7687 684 case OPT_O:
685 case OPT_Ofast:
686 case OPT_Og:
687 case OPT_Os:
954825c9 688 break;
689
690 default:
691 if (!(cl_options[option->opt_index].flags & CL_TARGET))
692 continue;
693 }
694
695 /* Pass the option on. */
696 for (i = 0; i < option->canonical_option_num_elements; ++i)
697 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
698 }
699
700 /* Append linker driver arguments. Compiler options from the linker
701 driver arguments will override / merge with those from the compiler. */
00d389c8 702 for (j = 1; j < decoded_options_count; ++j)
703 {
704 struct cl_decoded_option *option = &decoded_options[j];
7bfefa9d 705
b84346c6 706 /* Do not pass on frontend specific flags not suitable for lto. */
00d389c8 707 if (!(cl_options[option->opt_index].flags
b84346c6 708 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
00d389c8 709 continue;
710
711 switch (option->opt_index)
712 {
713 case OPT_o:
714 linker_output = option->arg;
715 /* We generate new intermediate output, drop this arg. */
716 continue;
717
718 case OPT_save_temps:
c8c65e07 719 debug = 1;
00d389c8 720 break;
721
722 case OPT_v:
c8c65e07 723 verbose = 1;
00d389c8 724 break;
c8c65e07 725
00d389c8 726 case OPT_flto_partition_none:
cbcf2791 727 no_partition = true;
00d389c8 728 break;
729
730 case OPT_flto_:
731 if (strcmp (option->arg, "jobserver") == 0)
732 {
733 jobserver = 1;
734 parallel = 1;
735 }
736 else
737 {
738 parallel = atoi (option->arg);
739 if (parallel <= 1)
740 parallel = 0;
741 }
742 /* Fallthru. */
743
744 case OPT_flto:
745 lto_mode = LTO_MODE_WHOPR;
746 /* We've handled these LTO options, do not pass them on. */
747 continue;
748
940a2ab8 749 case OPT_freg_struct_return:
750 case OPT_fpcc_struct_return:
751 /* Ignore these, they are determined by the input files.
752 ??? We fail to diagnose a possible mismatch here. */
753 continue;
754
00d389c8 755 default:
756 break;
757 }
758
759 /* Pass the option on. */
b84346c6 760 for (i = 0; i < option->canonical_option_num_elements; ++i)
761 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
00d389c8 762 }
763
cbcf2791 764 if (no_partition)
765 {
766 lto_mode = LTO_MODE_LTO;
767 jobserver = 0;
768 parallel = 0;
769 }
c8c65e07 770
771 if (linker_output)
772 {
773 char *output_dir, *base, *name;
ad905e43 774 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
7bfefa9d 775
c8c65e07 776 output_dir = xstrdup (linker_output);
777 base = output_dir;
778 for (name = base; *name; name++)
779 if (IS_DIR_SEPARATOR (*name))
780 base = name + 1;
781 *base = '\0';
782
783 linker_output = &linker_output[base - output_dir];
784 if (*output_dir == '\0')
785 {
786 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
787 output_dir = current_dir;
788 }
ad905e43 789 if (!bit_bucket)
790 {
b84346c6 791 obstack_ptr_grow (&argv_obstack, "-dumpdir");
792 obstack_ptr_grow (&argv_obstack, output_dir);
ad905e43 793 }
c8c65e07 794
b84346c6 795 obstack_ptr_grow (&argv_obstack, "-dumpbase");
c8c65e07 796 }
b84346c6 797
798 /* Remember at which point we can scrub args to re-use the commons. */
799 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
7bfefa9d 800
7bfefa9d 801 if (lto_mode == LTO_MODE_LTO)
802 {
803 flto_out = make_temp_file (".lto.o");
c8c65e07 804 if (linker_output)
b84346c6 805 obstack_ptr_grow (&argv_obstack, linker_output);
806 obstack_ptr_grow (&argv_obstack, "-o");
807 obstack_ptr_grow (&argv_obstack, flto_out);
7bfefa9d 808 }
c7d28f13 809 else
7bfefa9d 810 {
811 const char *list_option = "-fltrans-output-list=";
812 size_t list_option_len = strlen (list_option);
813 char *tmp;
814
c8c65e07 815 if (linker_output)
816 {
817 char *dumpbase = (char *) xmalloc (strlen (linker_output)
2024fa7d 818 + sizeof (".wpa") + 1);
c8c65e07 819 strcpy (dumpbase, linker_output);
820 strcat (dumpbase, ".wpa");
b84346c6 821 obstack_ptr_grow (&argv_obstack, dumpbase);
c8c65e07 822 }
823
2024fa7d 824 if (linker_output && debug)
825 {
826 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
827 + sizeof (".ltrans.out") + 1);
828 strcpy (ltrans_output_file, linker_output);
829 strcat (ltrans_output_file, ".ltrans.out");
830 }
831 else
832 ltrans_output_file = make_temp_file (".ltrans.out");
7bfefa9d 833 list_option_full = (char *) xmalloc (sizeof (char) *
834 (strlen (ltrans_output_file) + list_option_len + 1));
835 tmp = list_option_full;
836
b84346c6 837 obstack_ptr_grow (&argv_obstack, tmp);
7bfefa9d 838 strcpy (tmp, list_option);
839 tmp += list_option_len;
840 strcpy (tmp, ltrans_output_file);
841
3e17769e 842 if (jobserver)
843 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
844 else if (parallel > 1)
845 {
846 char buf[256];
847 sprintf (buf, "-fwpa=%i", parallel);
848 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
849 }
850 else
851 obstack_ptr_grow (&argv_obstack, "-fwpa");
7bfefa9d 852 }
7bfefa9d 853
9d75589a 854 /* Append the input objects and possible preceding arguments. */
c8c65e07 855 for (i = 1; i < argc; ++i)
b84346c6 856 obstack_ptr_grow (&argv_obstack, argv[i]);
857 obstack_ptr_grow (&argv_obstack, NULL);
7bfefa9d 858
b84346c6 859 new_argv = XOBFINISH (&argv_obstack, const char **);
860 argv_ptr = &new_argv[new_head_argc];
7bfefa9d 861 fork_execute (CONST_CAST (char **, new_argv));
264cbb51 862
7bfefa9d 863 if (lto_mode == LTO_MODE_LTO)
864 {
9af5ce0c 865 printf ("%s\n", flto_out);
7bfefa9d 866 free (flto_out);
867 flto_out = NULL;
868 }
c7d28f13 869 else
7bfefa9d 870 {
871 FILE *stream = fopen (ltrans_output_file, "r");
cba6710d 872 FILE *mstream = NULL;
eefcf076 873 struct obstack env_obstack;
7bfefa9d 874
875 if (!stream)
876 fatal_perror ("fopen: %s", ltrans_output_file);
877
4062f557 878 /* Parse the list of LTRANS inputs from the WPA stage. */
eefcf076 879 obstack_init (&env_obstack);
4062f557 880 nr = 0;
264cbb51 881 for (;;)
882 {
883 const unsigned piece = 32;
4062f557 884 char *output_name = NULL;
264cbb51 885 char *buf, *input_name = (char *)xmalloc (piece);
886 size_t len;
887
888 buf = input_name;
889cont:
890 if (!fgets (buf, piece, stream))
891 break;
892 len = strlen (input_name);
893 if (input_name[len - 1] != '\n')
894 {
895 input_name = (char *)xrealloc (input_name, len + piece);
896 buf = input_name + len;
897 goto cont;
898 }
899 input_name[len - 1] = '\0';
900
901 if (input_name[0] == '*')
cba6710d 902 output_name = &input_name[1];
264cbb51 903
cba6710d 904 nr++;
905 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
906 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
907 input_names[nr-1] = input_name;
908 output_names[nr-1] = output_name;
909 }
4062f557 910 fclose (stream);
911 maybe_unlink_file (ltrans_output_file);
912 ltrans_output_file = NULL;
913
914 if (parallel)
915 {
916 makefile = make_temp_file (".mk");
917 mstream = fopen (makefile, "w");
918 }
919
920 /* Execute the LTRANS stage for each input file (or prepare a
921 makefile to invoke this in parallel). */
922 for (i = 0; i < nr; ++i)
923 {
924 char *output_name;
925 char *input_name = input_names[i];
926 /* If it's a pass-through file do nothing. */
927 if (output_names[i])
928 continue;
929
930 /* Replace the .o suffix with a .ltrans.o suffix and write
931 the resulting name to the LTRANS output list. */
4062f557 932 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
933 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
934 output_name = XOBFINISH (&env_obstack, char *);
935
936 /* Adjust the dumpbase if the linker output file was seen. */
937 if (linker_output)
938 {
939 char *dumpbase
940 = (char *) xmalloc (strlen (linker_output)
9af5ce0c 941 + sizeof (DUMPBASE_SUFFIX) + 1);
4062f557 942 snprintf (dumpbase,
9af5ce0c 943 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
2024fa7d 944 "%s.ltrans%u", linker_output, i);
4062f557 945 argv_ptr[0] = dumpbase;
946 }
947
948 argv_ptr[1] = "-fltrans";
949 argv_ptr[2] = "-o";
950 argv_ptr[3] = output_name;
951 argv_ptr[4] = input_name;
952 argv_ptr[5] = NULL;
953 if (parallel)
954 {
955 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
956 for (j = 1; new_argv[j] != NULL; ++j)
957 fprintf (mstream, " '%s'", new_argv[j]);
958 fprintf (mstream, "\n");
4fe86c66 959 /* If we are not preserving the ltrans input files then
960 truncate them as soon as we have processed it. This
961 reduces temporary disk-space usage. */
962 if (! debug)
963 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
964 "&& mv %s.tem %s\n",
965 input_name, input_name, input_name, input_name);
4062f557 966 }
967 else
4fe86c66 968 {
969 fork_execute (CONST_CAST (char **, new_argv));
970 maybe_unlink_file (input_name);
971 }
4062f557 972
973 output_names[i] = output_name;
974 }
cba6710d 975 if (parallel)
976 {
977 struct pex_obj *pex;
978 char jobs[32];
442bd81c 979
cba6710d 980 fprintf (mstream, "all:");
981 for (i = 0; i < nr; ++i)
982 fprintf (mstream, " \\\n\t%s", output_names[i]);
983 fprintf (mstream, "\n");
984 fclose (mstream);
442bd81c 985 if (!jobserver)
986 {
987 /* Avoid passing --jobserver-fd= and similar flags
988 unless jobserver mode is explicitly enabled. */
989 putenv (xstrdup ("MAKEFLAGS="));
990 putenv (xstrdup ("MFLAGS="));
991 }
c335b5fe 992 new_argv[0] = getenv ("MAKE");
993 if (!new_argv[0])
994 new_argv[0] = "make";
cba6710d 995 new_argv[1] = "-f";
996 new_argv[2] = makefile;
442bd81c 997 i = 3;
998 if (!jobserver)
999 {
1000 snprintf (jobs, 31, "-j%d", parallel);
1001 new_argv[i++] = jobs;
1002 }
1003 new_argv[i++] = "all";
1004 new_argv[i++] = NULL;
cba6710d 1005 pex = collect_execute (CONST_CAST (char **, new_argv));
1006 collect_wait (new_argv[0], pex);
1007 maybe_unlink_file (makefile);
4062f557 1008 makefile = NULL;
4fe86c66 1009 for (i = 0; i < nr; ++i)
1010 maybe_unlink_file (input_names[i]);
cba6710d 1011 }
1012 for (i = 0; i < nr; ++i)
1013 {
1014 fputs (output_names[i], stdout);
264cbb51 1015 putc ('\n', stdout);
cba6710d 1016 free (input_names[i]);
264cbb51 1017 }
4062f557 1018 nr = 0;
cba6710d 1019 free (output_names);
1020 free (input_names);
7bfefa9d 1021 free (list_option_full);
eefcf076 1022 obstack_free (&env_obstack, NULL);
7bfefa9d 1023 }
b84346c6 1024
1025 obstack_free (&argv_obstack, NULL);
7bfefa9d 1026}
1027
1028
1029/* Entry point. */
1030
1031int
1032main (int argc, char *argv[])
1033{
0b4f4daf 1034 const char *p;
1035
ba30d337 1036 gcc_obstack_init (&opts_obstack);
1037
0b4f4daf 1038 p = argv[0] + strlen (argv[0]);
1039 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1040 --p;
1041 progname = p;
1042
1043 xmalloc_set_program_name (progname);
1044
7bfefa9d 1045 gcc_init_libintl ();
1046
0b4f4daf 1047 diagnostic_initialize (global_dc, 0);
1048
c04a3a63 1049 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1050 signal (SIGINT, fatal_signal);
1051#ifdef SIGHUP
1052 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1053 signal (SIGHUP, fatal_signal);
1054#endif
1055 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1056 signal (SIGTERM, fatal_signal);
1057#ifdef SIGPIPE
1058 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1059 signal (SIGPIPE, fatal_signal);
1060#endif
1061#ifdef SIGCHLD
1062 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1063 receive the signal. A different setting is inheritable */
1064 signal (SIGCHLD, SIG_DFL);
1065#endif
1066
7bfefa9d 1067 /* We may be called with all the arguments stored in some file and
1068 passed with @file. Expand them into argv before processing. */
1069 expandargv (&argc, &argv);
00d389c8 1070
c8c65e07 1071 run_gcc (argc, argv);
7bfefa9d 1072
1073 return 0;
1074}