Small cleanups before making collect2 use collect-utils.
[gcc.git] / gcc / lto-wrapper.c
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2014 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 WHOPR 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 "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
51
52 enum lto_mode_d {
53 LTO_MODE_NONE, /* Not doing LTO. */
54 LTO_MODE_LTO, /* Normal LTO. */
55 LTO_MODE_WHOPR /* WHOPR. */
56 };
57
58 /* Current LTO mode. */
59 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
60
61 static char *ltrans_output_file;
62 static char *flto_out;
63 static unsigned int nr;
64 static char **input_names;
65 static char **output_names;
66 static char *makefile;
67
68 const char tool_name[] = "lto-wrapper";
69
70 /* Delete tempfiles. Called from utils_cleanup. */
71
72 void
73 tool_cleanup (void)
74 {
75 unsigned int i;
76
77 if (ltrans_output_file)
78 maybe_unlink (ltrans_output_file);
79 if (flto_out)
80 maybe_unlink (flto_out);
81 if (makefile)
82 maybe_unlink (makefile);
83 for (i = 0; i < nr; ++i)
84 {
85 maybe_unlink (input_names[i]);
86 if (output_names[i])
87 maybe_unlink (output_names[i]);
88 }
89 }
90
91 static void
92 lto_wrapper_cleanup (void)
93 {
94 utils_cleanup ();
95 }
96
97 /* Unlink a temporary LTRANS file unless requested otherwise. */
98
99 void
100 maybe_unlink (const char *file)
101 {
102 if (!save_temps)
103 {
104 if (unlink_if_ordinary (file)
105 && errno != ENOENT)
106 fatal_error ("deleting LTRANS file %s: %m", file);
107 }
108 else if (verbose)
109 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
110 }
111
112 /* Template of LTRANS dumpbase suffix. */
113 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
114
115 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
116 environment according to LANG_MASK. */
117
118 static void
119 get_options_from_collect_gcc_options (const char *collect_gcc,
120 const char *collect_gcc_options,
121 unsigned int lang_mask,
122 struct cl_decoded_option **decoded_options,
123 unsigned int *decoded_options_count)
124 {
125 struct obstack argv_obstack;
126 char *argv_storage;
127 const char **argv;
128 int j, k, argc;
129
130 argv_storage = xstrdup (collect_gcc_options);
131 obstack_init (&argv_obstack);
132 obstack_ptr_grow (&argv_obstack, collect_gcc);
133
134 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
135 {
136 if (argv_storage[j] == '\'')
137 {
138 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
139 ++j;
140 do
141 {
142 if (argv_storage[j] == '\0')
143 fatal_error ("malformed COLLECT_GCC_OPTIONS");
144 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
145 {
146 argv_storage[k++] = '\'';
147 j += 4;
148 }
149 else if (argv_storage[j] == '\'')
150 break;
151 else
152 argv_storage[k++] = argv_storage[j++];
153 }
154 while (1);
155 argv_storage[k++] = '\0';
156 }
157 }
158
159 obstack_ptr_grow (&argv_obstack, NULL);
160 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
161 argv = XOBFINISH (&argv_obstack, const char **);
162
163 decode_cmdline_options_to_array (argc, (const char **)argv,
164 lang_mask,
165 decoded_options, decoded_options_count);
166 obstack_free (&argv_obstack, NULL);
167 }
168
169 /* Append OPTION to the options array DECODED_OPTIONS with size
170 DECODED_OPTIONS_COUNT. */
171
172 static void
173 append_option (struct cl_decoded_option **decoded_options,
174 unsigned int *decoded_options_count,
175 struct cl_decoded_option *option)
176 {
177 ++*decoded_options_count;
178 *decoded_options
179 = (struct cl_decoded_option *)
180 xrealloc (*decoded_options,
181 (*decoded_options_count
182 * sizeof (struct cl_decoded_option)));
183 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
184 sizeof (struct cl_decoded_option));
185 }
186
187 /* Try to merge and complain about options FDECODED_OPTIONS when applied
188 ontop of DECODED_OPTIONS. */
189
190 static void
191 merge_and_complain (struct cl_decoded_option **decoded_options,
192 unsigned int *decoded_options_count,
193 struct cl_decoded_option *fdecoded_options,
194 unsigned int fdecoded_options_count)
195 {
196 unsigned int i, j;
197
198 /* ??? Merge options from files. Most cases can be
199 handled by either unioning or intersecting
200 (for example -fwrapv is a case for unioning,
201 -ffast-math is for intersection). Most complaints
202 about real conflicts between different options can
203 be deferred to the compiler proper. Options that
204 we can neither safely handle by intersection nor
205 unioning would need to be complained about here.
206 Ideally we'd have a flag in the opt files that
207 tells whether to union or intersect or reject.
208 In absence of that it's unclear what a good default is.
209 It's also difficult to get positional handling correct. */
210
211 /* The following does what the old LTO option code did,
212 union all target and a selected set of common options. */
213 for (i = 0; i < fdecoded_options_count; ++i)
214 {
215 struct cl_decoded_option *foption = &fdecoded_options[i];
216 switch (foption->opt_index)
217 {
218 case OPT_SPECIAL_unknown:
219 case OPT_SPECIAL_ignore:
220 case OPT_SPECIAL_program_name:
221 case OPT_SPECIAL_input_file:
222 break;
223
224 default:
225 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
226 break;
227
228 /* Fallthru. */
229 case OPT_fPIC:
230 case OPT_fpic:
231 case OPT_fPIE:
232 case OPT_fpie:
233 case OPT_fcommon:
234 case OPT_fexceptions:
235 case OPT_fnon_call_exceptions:
236 case OPT_fgnu_tm:
237 /* Do what the old LTO code did - collect exactly one option
238 setting per OPT code, we pick the first we encounter.
239 ??? This doesn't make too much sense, but when it doesn't
240 then we should complain. */
241 for (j = 0; j < *decoded_options_count; ++j)
242 if ((*decoded_options)[j].opt_index == foption->opt_index)
243 break;
244 if (j == *decoded_options_count)
245 append_option (decoded_options, decoded_options_count, foption);
246 break;
247
248 case OPT_ftrapv:
249 case OPT_fstrict_overflow:
250 case OPT_ffp_contract_:
251 /* For selected options we can merge conservatively. */
252 for (j = 0; j < *decoded_options_count; ++j)
253 if ((*decoded_options)[j].opt_index == foption->opt_index)
254 break;
255 if (j == *decoded_options_count)
256 append_option (decoded_options, decoded_options_count, foption);
257 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
258 -fno-trapv < -ftrapv,
259 -fno-strict-overflow < -fstrict-overflow */
260 else if (foption->value < (*decoded_options)[j].value)
261 (*decoded_options)[j] = *foption;
262 break;
263
264 case OPT_fwrapv:
265 /* For selected options we can merge conservatively. */
266 for (j = 0; j < *decoded_options_count; ++j)
267 if ((*decoded_options)[j].opt_index == foption->opt_index)
268 break;
269 if (j == *decoded_options_count)
270 append_option (decoded_options, decoded_options_count, foption);
271 /* -fwrapv > -fno-wrapv. */
272 else if (foption->value > (*decoded_options)[j].value)
273 (*decoded_options)[j] = *foption;
274 break;
275
276 case OPT_freg_struct_return:
277 case OPT_fpcc_struct_return:
278 case OPT_fshort_double:
279 for (j = 0; j < *decoded_options_count; ++j)
280 if ((*decoded_options)[j].opt_index == foption->opt_index)
281 break;
282 if (j == *decoded_options_count)
283 fatal_error ("Option %s not used consistently in all LTO input"
284 " files", foption->orig_option_with_args_text);
285 break;
286
287 case OPT_O:
288 case OPT_Ofast:
289 case OPT_Og:
290 case OPT_Os:
291 for (j = 0; j < *decoded_options_count; ++j)
292 if ((*decoded_options)[j].opt_index == OPT_O
293 || (*decoded_options)[j].opt_index == OPT_Ofast
294 || (*decoded_options)[j].opt_index == OPT_Og
295 || (*decoded_options)[j].opt_index == OPT_Os)
296 break;
297 if (j == *decoded_options_count)
298 append_option (decoded_options, decoded_options_count, foption);
299 else if ((*decoded_options)[j].opt_index == foption->opt_index
300 && foption->opt_index != OPT_O)
301 /* Exact same options get merged. */
302 ;
303 else
304 {
305 /* For mismatched option kinds preserve the optimization
306 level only, thus merge it as -On. This also handles
307 merging of same optimization level -On. */
308 int level = 0;
309 switch (foption->opt_index)
310 {
311 case OPT_O:
312 if (foption->arg[0] == '\0')
313 level = MAX (level, 1);
314 else
315 level = MAX (level, atoi (foption->arg));
316 break;
317 case OPT_Ofast:
318 level = MAX (level, 3);
319 break;
320 case OPT_Og:
321 level = MAX (level, 1);
322 break;
323 case OPT_Os:
324 level = MAX (level, 2);
325 break;
326 default:
327 gcc_unreachable ();
328 }
329 switch ((*decoded_options)[j].opt_index)
330 {
331 case OPT_O:
332 if ((*decoded_options)[j].arg[0] == '\0')
333 level = MAX (level, 1);
334 else
335 level = MAX (level, atoi ((*decoded_options)[j].arg));
336 break;
337 case OPT_Ofast:
338 level = MAX (level, 3);
339 break;
340 case OPT_Og:
341 level = MAX (level, 1);
342 break;
343 case OPT_Os:
344 level = MAX (level, 2);
345 break;
346 default:
347 gcc_unreachable ();
348 }
349 (*decoded_options)[j].opt_index = OPT_O;
350 char *tem;
351 asprintf (&tem, "-O%d", level);
352 (*decoded_options)[j].arg = &tem[2];
353 (*decoded_options)[j].canonical_option[0] = tem;
354 (*decoded_options)[j].value = 1;
355 }
356 break;
357 }
358 }
359 }
360
361 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
362
363 static void
364 run_gcc (unsigned argc, char *argv[])
365 {
366 unsigned i, j;
367 const char **new_argv;
368 const char **argv_ptr;
369 char *list_option_full = NULL;
370 const char *linker_output = NULL;
371 const char *collect_gcc, *collect_gcc_options;
372 int parallel = 0;
373 int jobserver = 0;
374 bool no_partition = false;
375 struct cl_decoded_option *fdecoded_options = NULL;
376 unsigned int fdecoded_options_count = 0;
377 struct cl_decoded_option *decoded_options;
378 unsigned int decoded_options_count;
379 struct obstack argv_obstack;
380 int new_head_argc;
381
382 /* Get the driver and options. */
383 collect_gcc = getenv ("COLLECT_GCC");
384 if (!collect_gcc)
385 fatal_error ("environment variable COLLECT_GCC must be set");
386 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
387 if (!collect_gcc_options)
388 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
389 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
390 CL_LANG_ALL,
391 &decoded_options,
392 &decoded_options_count);
393
394 /* Look at saved options in the IL files. */
395 for (i = 1; i < argc; ++i)
396 {
397 char *data, *p;
398 char *fopts;
399 int fd;
400 const char *errmsg;
401 int err;
402 off_t file_offset = 0, offset, length;
403 long loffset;
404 simple_object_read *sobj;
405 int consumed;
406 struct cl_decoded_option *f2decoded_options;
407 unsigned int f2decoded_options_count;
408 char *filename = argv[i];
409 if ((p = strrchr (argv[i], '@'))
410 && p != argv[i]
411 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
412 && strlen (p) == (unsigned int) consumed)
413 {
414 filename = XNEWVEC (char, p - argv[i] + 1);
415 memcpy (filename, argv[i], p - argv[i]);
416 filename[p - argv[i]] = '\0';
417 file_offset = (off_t) loffset;
418 }
419 fd = open (argv[i], O_RDONLY);
420 if (fd == -1)
421 continue;
422 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
423 &errmsg, &err);
424 if (!sobj)
425 {
426 close (fd);
427 continue;
428 }
429 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
430 &offset, &length, &errmsg, &err))
431 {
432 simple_object_release_read (sobj);
433 close (fd);
434 continue;
435 }
436 lseek (fd, file_offset + offset, SEEK_SET);
437 data = (char *)xmalloc (length);
438 read (fd, data, length);
439 fopts = data;
440 do
441 {
442 get_options_from_collect_gcc_options (collect_gcc,
443 fopts, CL_LANG_ALL,
444 &f2decoded_options,
445 &f2decoded_options_count);
446 if (!fdecoded_options)
447 {
448 fdecoded_options = f2decoded_options;
449 fdecoded_options_count = f2decoded_options_count;
450 }
451 else
452 merge_and_complain (&fdecoded_options,
453 &fdecoded_options_count,
454 f2decoded_options, f2decoded_options_count);
455
456 fopts += strlen (fopts) + 1;
457 }
458 while (fopts - data < length);
459
460 free (data);
461 simple_object_release_read (sobj);
462 close (fd);
463 }
464
465 /* Initalize the common arguments for the driver. */
466 obstack_init (&argv_obstack);
467 obstack_ptr_grow (&argv_obstack, collect_gcc);
468 obstack_ptr_grow (&argv_obstack, "-xlto");
469 obstack_ptr_grow (&argv_obstack, "-c");
470
471 /* Append compiler driver arguments as far as they were merged. */
472 for (j = 1; j < fdecoded_options_count; ++j)
473 {
474 struct cl_decoded_option *option = &fdecoded_options[j];
475
476 /* File options have been properly filtered by lto-opts.c. */
477 switch (option->opt_index)
478 {
479 /* Drop arguments that we want to take from the link line. */
480 case OPT_flto_:
481 case OPT_flto:
482 case OPT_flto_partition_:
483 continue;
484
485 default:
486 break;
487 }
488
489 /* For now do what the original LTO option code was doing - pass
490 on any CL_TARGET flag and a few selected others. */
491 switch (option->opt_index)
492 {
493 case OPT_fPIC:
494 case OPT_fpic:
495 case OPT_fPIE:
496 case OPT_fpie:
497 case OPT_fcommon:
498 case OPT_fexceptions:
499 case OPT_fnon_call_exceptions:
500 case OPT_fgnu_tm:
501 case OPT_freg_struct_return:
502 case OPT_fpcc_struct_return:
503 case OPT_fshort_double:
504 case OPT_ffp_contract_:
505 case OPT_fwrapv:
506 case OPT_ftrapv:
507 case OPT_fstrict_overflow:
508 case OPT_O:
509 case OPT_Ofast:
510 case OPT_Og:
511 case OPT_Os:
512 break;
513
514 default:
515 if (!(cl_options[option->opt_index].flags & CL_TARGET))
516 continue;
517 }
518
519 /* Pass the option on. */
520 for (i = 0; i < option->canonical_option_num_elements; ++i)
521 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
522 }
523
524 /* Append linker driver arguments. Compiler options from the linker
525 driver arguments will override / merge with those from the compiler. */
526 for (j = 1; j < decoded_options_count; ++j)
527 {
528 struct cl_decoded_option *option = &decoded_options[j];
529
530 /* Do not pass on frontend specific flags not suitable for lto. */
531 if (!(cl_options[option->opt_index].flags
532 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
533 continue;
534
535 switch (option->opt_index)
536 {
537 case OPT_o:
538 linker_output = option->arg;
539 /* We generate new intermediate output, drop this arg. */
540 continue;
541
542 case OPT_save_temps:
543 save_temps = 1;
544 break;
545
546 case OPT_v:
547 verbose = 1;
548 break;
549
550 case OPT_flto_partition_:
551 if (strcmp (option->arg, "none") == 0)
552 no_partition = true;
553 break;
554
555 case OPT_flto_:
556 if (strcmp (option->arg, "jobserver") == 0)
557 {
558 jobserver = 1;
559 parallel = 1;
560 }
561 else
562 {
563 parallel = atoi (option->arg);
564 if (parallel <= 1)
565 parallel = 0;
566 }
567 /* Fallthru. */
568
569 case OPT_flto:
570 lto_mode = LTO_MODE_WHOPR;
571 /* We've handled these LTO options, do not pass them on. */
572 continue;
573
574 case OPT_freg_struct_return:
575 case OPT_fpcc_struct_return:
576 case OPT_fshort_double:
577 /* Ignore these, they are determined by the input files.
578 ??? We fail to diagnose a possible mismatch here. */
579 continue;
580
581 default:
582 break;
583 }
584
585 /* Pass the option on. */
586 for (i = 0; i < option->canonical_option_num_elements; ++i)
587 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
588 }
589
590 if (no_partition)
591 {
592 lto_mode = LTO_MODE_LTO;
593 jobserver = 0;
594 parallel = 0;
595 }
596
597 if (linker_output)
598 {
599 char *output_dir, *base, *name;
600 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
601
602 output_dir = xstrdup (linker_output);
603 base = output_dir;
604 for (name = base; *name; name++)
605 if (IS_DIR_SEPARATOR (*name))
606 base = name + 1;
607 *base = '\0';
608
609 linker_output = &linker_output[base - output_dir];
610 if (*output_dir == '\0')
611 {
612 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
613 output_dir = current_dir;
614 }
615 if (!bit_bucket)
616 {
617 obstack_ptr_grow (&argv_obstack, "-dumpdir");
618 obstack_ptr_grow (&argv_obstack, output_dir);
619 }
620
621 obstack_ptr_grow (&argv_obstack, "-dumpbase");
622 }
623
624 /* Remember at which point we can scrub args to re-use the commons. */
625 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
626
627 if (lto_mode == LTO_MODE_LTO)
628 {
629 flto_out = make_temp_file (".lto.o");
630 if (linker_output)
631 obstack_ptr_grow (&argv_obstack, linker_output);
632 obstack_ptr_grow (&argv_obstack, "-o");
633 obstack_ptr_grow (&argv_obstack, flto_out);
634 }
635 else
636 {
637 const char *list_option = "-fltrans-output-list=";
638 size_t list_option_len = strlen (list_option);
639 char *tmp;
640
641 if (linker_output)
642 {
643 char *dumpbase = (char *) xmalloc (strlen (linker_output)
644 + sizeof (".wpa") + 1);
645 strcpy (dumpbase, linker_output);
646 strcat (dumpbase, ".wpa");
647 obstack_ptr_grow (&argv_obstack, dumpbase);
648 }
649
650 if (linker_output && save_temps)
651 {
652 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
653 + sizeof (".ltrans.out") + 1);
654 strcpy (ltrans_output_file, linker_output);
655 strcat (ltrans_output_file, ".ltrans.out");
656 }
657 else
658 ltrans_output_file = make_temp_file (".ltrans.out");
659 list_option_full = (char *) xmalloc (sizeof (char) *
660 (strlen (ltrans_output_file) + list_option_len + 1));
661 tmp = list_option_full;
662
663 obstack_ptr_grow (&argv_obstack, tmp);
664 strcpy (tmp, list_option);
665 tmp += list_option_len;
666 strcpy (tmp, ltrans_output_file);
667
668 if (jobserver)
669 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
670 else if (parallel > 1)
671 {
672 char buf[256];
673 sprintf (buf, "-fwpa=%i", parallel);
674 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
675 }
676 else
677 obstack_ptr_grow (&argv_obstack, "-fwpa");
678 }
679
680 /* Append the input objects and possible preceding arguments. */
681 for (i = 1; i < argc; ++i)
682 obstack_ptr_grow (&argv_obstack, argv[i]);
683 obstack_ptr_grow (&argv_obstack, NULL);
684
685 new_argv = XOBFINISH (&argv_obstack, const char **);
686 argv_ptr = &new_argv[new_head_argc];
687 fork_execute (CONST_CAST (char **, new_argv));
688
689 if (lto_mode == LTO_MODE_LTO)
690 {
691 printf ("%s\n", flto_out);
692 free (flto_out);
693 flto_out = NULL;
694 }
695 else
696 {
697 FILE *stream = fopen (ltrans_output_file, "r");
698 FILE *mstream = NULL;
699 struct obstack env_obstack;
700
701 if (!stream)
702 fatal_error ("fopen: %s: %m", ltrans_output_file);
703
704 /* Parse the list of LTRANS inputs from the WPA stage. */
705 obstack_init (&env_obstack);
706 nr = 0;
707 for (;;)
708 {
709 const unsigned piece = 32;
710 char *output_name = NULL;
711 char *buf, *input_name = (char *)xmalloc (piece);
712 size_t len;
713
714 buf = input_name;
715 cont:
716 if (!fgets (buf, piece, stream))
717 break;
718 len = strlen (input_name);
719 if (input_name[len - 1] != '\n')
720 {
721 input_name = (char *)xrealloc (input_name, len + piece);
722 buf = input_name + len;
723 goto cont;
724 }
725 input_name[len - 1] = '\0';
726
727 if (input_name[0] == '*')
728 output_name = &input_name[1];
729
730 nr++;
731 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
732 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
733 input_names[nr-1] = input_name;
734 output_names[nr-1] = output_name;
735 }
736 fclose (stream);
737 maybe_unlink (ltrans_output_file);
738 ltrans_output_file = NULL;
739
740 if (parallel)
741 {
742 makefile = make_temp_file (".mk");
743 mstream = fopen (makefile, "w");
744 }
745
746 /* Execute the LTRANS stage for each input file (or prepare a
747 makefile to invoke this in parallel). */
748 for (i = 0; i < nr; ++i)
749 {
750 char *output_name;
751 char *input_name = input_names[i];
752 /* If it's a pass-through file do nothing. */
753 if (output_names[i])
754 continue;
755
756 /* Replace the .o suffix with a .ltrans.o suffix and write
757 the resulting name to the LTRANS output list. */
758 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
759 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
760 output_name = XOBFINISH (&env_obstack, char *);
761
762 /* Adjust the dumpbase if the linker output file was seen. */
763 if (linker_output)
764 {
765 char *dumpbase
766 = (char *) xmalloc (strlen (linker_output)
767 + sizeof (DUMPBASE_SUFFIX) + 1);
768 snprintf (dumpbase,
769 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
770 "%s.ltrans%u", linker_output, i);
771 argv_ptr[0] = dumpbase;
772 }
773
774 argv_ptr[1] = "-fltrans";
775 argv_ptr[2] = "-o";
776 argv_ptr[3] = output_name;
777 argv_ptr[4] = input_name;
778 argv_ptr[5] = NULL;
779 if (parallel)
780 {
781 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
782 for (j = 1; new_argv[j] != NULL; ++j)
783 fprintf (mstream, " '%s'", new_argv[j]);
784 fprintf (mstream, "\n");
785 /* If we are not preserving the ltrans input files then
786 truncate them as soon as we have processed it. This
787 reduces temporary disk-space usage. */
788 if (! save_temps)
789 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
790 "&& mv %s.tem %s\n",
791 input_name, input_name, input_name, input_name);
792 }
793 else
794 {
795 fork_execute (CONST_CAST (char **, new_argv));
796 maybe_unlink (input_name);
797 }
798
799 output_names[i] = output_name;
800 }
801 if (parallel)
802 {
803 struct pex_obj *pex;
804 char jobs[32];
805
806 fprintf (mstream, "all:");
807 for (i = 0; i < nr; ++i)
808 fprintf (mstream, " \\\n\t%s", output_names[i]);
809 fprintf (mstream, "\n");
810 fclose (mstream);
811 if (!jobserver)
812 {
813 /* Avoid passing --jobserver-fd= and similar flags
814 unless jobserver mode is explicitly enabled. */
815 putenv (xstrdup ("MAKEFLAGS="));
816 putenv (xstrdup ("MFLAGS="));
817 }
818 new_argv[0] = getenv ("MAKE");
819 if (!new_argv[0])
820 new_argv[0] = "make";
821 new_argv[1] = "-f";
822 new_argv[2] = makefile;
823 i = 3;
824 if (!jobserver)
825 {
826 snprintf (jobs, 31, "-j%d", parallel);
827 new_argv[i++] = jobs;
828 }
829 new_argv[i++] = "all";
830 new_argv[i++] = NULL;
831 pex = collect_execute (CONST_CAST (char **, new_argv));
832 collect_wait (new_argv[0], pex);
833 maybe_unlink (makefile);
834 makefile = NULL;
835 for (i = 0; i < nr; ++i)
836 maybe_unlink (input_names[i]);
837 }
838 for (i = 0; i < nr; ++i)
839 {
840 fputs (output_names[i], stdout);
841 putc ('\n', stdout);
842 free (input_names[i]);
843 }
844 nr = 0;
845 free (output_names);
846 free (input_names);
847 free (list_option_full);
848 obstack_free (&env_obstack, NULL);
849 }
850
851 obstack_free (&argv_obstack, NULL);
852 }
853
854
855 /* Entry point. */
856
857 int
858 main (int argc, char *argv[])
859 {
860 const char *p;
861
862 gcc_obstack_init (&opts_obstack);
863
864 p = argv[0] + strlen (argv[0]);
865 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
866 --p;
867 progname = p;
868
869 xmalloc_set_program_name (progname);
870
871 if (atexit (lto_wrapper_cleanup) != 0)
872 fatal_error ("atexit failed");
873
874 gcc_init_libintl ();
875
876 diagnostic_initialize (global_dc, 0);
877
878 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
879 signal (SIGINT, fatal_signal);
880 #ifdef SIGHUP
881 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
882 signal (SIGHUP, fatal_signal);
883 #endif
884 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
885 signal (SIGTERM, fatal_signal);
886 #ifdef SIGPIPE
887 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
888 signal (SIGPIPE, fatal_signal);
889 #endif
890 #ifdef SIGCHLD
891 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
892 receive the signal. A different setting is inheritable */
893 signal (SIGCHLD, SIG_DFL);
894 #endif
895
896 /* We may be called with all the arguments stored in some file and
897 passed with @file. Expand them into argv before processing. */
898 expandargv (&argc, &argv);
899
900 run_gcc (argc, argv);
901
902 return 0;
903 }