svn merge -r215707:216846 svn+ssh://gcc.gnu.org/svn/gcc/trunk
[official-gcc.git] / gcc / lto-wrapper.c
blob601351433ddd77bb391956cb932423d907a7f4ca
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2014 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
11 version.
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.
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.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
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"
52 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
54 enum lto_mode_d {
55 LTO_MODE_NONE, /* Not doing LTO. */
56 LTO_MODE_LTO, /* Normal LTO. */
57 LTO_MODE_WHOPR /* WHOPR. */
60 /* Current LTO mode. */
61 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
63 static char *ltrans_output_file;
64 static char *flto_out;
65 static unsigned int nr;
66 static char **input_names;
67 static char **output_names;
68 static char **offload_names;
69 static const char *ompbegin, *ompend;
70 static char *makefile;
72 const char tool_name[] = "lto-wrapper";
74 /* Delete tempfiles. Called from utils_cleanup. */
76 void
77 tool_cleanup (bool)
79 unsigned int i;
81 if (ltrans_output_file)
82 maybe_unlink (ltrans_output_file);
83 if (flto_out)
84 maybe_unlink (flto_out);
85 if (makefile)
86 maybe_unlink (makefile);
87 for (i = 0; i < nr; ++i)
89 maybe_unlink (input_names[i]);
90 if (output_names[i])
91 maybe_unlink (output_names[i]);
95 static void
96 lto_wrapper_cleanup (void)
98 utils_cleanup (false);
101 /* Unlink a temporary LTRANS file unless requested otherwise. */
103 void
104 maybe_unlink (const char *file)
106 if (!save_temps)
108 if (unlink_if_ordinary (file)
109 && errno != ENOENT)
110 fatal_error ("deleting LTRANS file %s: %m", file);
112 else if (verbose)
113 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
116 /* Template of LTRANS dumpbase suffix. */
117 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
119 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
120 environment according to LANG_MASK. */
122 static void
123 get_options_from_collect_gcc_options (const char *collect_gcc,
124 const char *collect_gcc_options,
125 unsigned int lang_mask,
126 struct cl_decoded_option **decoded_options,
127 unsigned int *decoded_options_count)
129 struct obstack argv_obstack;
130 char *argv_storage;
131 const char **argv;
132 int j, k, argc;
134 argv_storage = xstrdup (collect_gcc_options);
135 obstack_init (&argv_obstack);
136 obstack_ptr_grow (&argv_obstack, collect_gcc);
138 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
140 if (argv_storage[j] == '\'')
142 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
143 ++j;
146 if (argv_storage[j] == '\0')
147 fatal_error ("malformed COLLECT_GCC_OPTIONS");
148 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
150 argv_storage[k++] = '\'';
151 j += 4;
153 else if (argv_storage[j] == '\'')
154 break;
155 else
156 argv_storage[k++] = argv_storage[j++];
158 while (1);
159 argv_storage[k++] = '\0';
163 obstack_ptr_grow (&argv_obstack, NULL);
164 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
165 argv = XOBFINISH (&argv_obstack, const char **);
167 decode_cmdline_options_to_array (argc, (const char **)argv,
168 lang_mask,
169 decoded_options, decoded_options_count);
170 obstack_free (&argv_obstack, NULL);
173 /* Append OPTION to the options array DECODED_OPTIONS with size
174 DECODED_OPTIONS_COUNT. */
176 static void
177 append_option (struct cl_decoded_option **decoded_options,
178 unsigned int *decoded_options_count,
179 struct cl_decoded_option *option)
181 ++*decoded_options_count;
182 *decoded_options
183 = (struct cl_decoded_option *)
184 xrealloc (*decoded_options,
185 (*decoded_options_count
186 * sizeof (struct cl_decoded_option)));
187 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
188 sizeof (struct cl_decoded_option));
191 /* Try to merge and complain about options FDECODED_OPTIONS when applied
192 ontop of DECODED_OPTIONS. */
194 static void
195 merge_and_complain (struct cl_decoded_option **decoded_options,
196 unsigned int *decoded_options_count,
197 struct cl_decoded_option *fdecoded_options,
198 unsigned int fdecoded_options_count)
200 unsigned int i, j;
202 /* ??? Merge options from files. Most cases can be
203 handled by either unioning or intersecting
204 (for example -fwrapv is a case for unioning,
205 -ffast-math is for intersection). Most complaints
206 about real conflicts between different options can
207 be deferred to the compiler proper. Options that
208 we can neither safely handle by intersection nor
209 unioning would need to be complained about here.
210 Ideally we'd have a flag in the opt files that
211 tells whether to union or intersect or reject.
212 In absence of that it's unclear what a good default is.
213 It's also difficult to get positional handling correct. */
215 /* The following does what the old LTO option code did,
216 union all target and a selected set of common options. */
217 for (i = 0; i < fdecoded_options_count; ++i)
219 struct cl_decoded_option *foption = &fdecoded_options[i];
220 switch (foption->opt_index)
222 case OPT_SPECIAL_unknown:
223 case OPT_SPECIAL_ignore:
224 case OPT_SPECIAL_program_name:
225 case OPT_SPECIAL_input_file:
226 break;
228 default:
229 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
230 break;
232 /* Fallthru. */
233 case OPT_fPIC:
234 case OPT_fpic:
235 case OPT_fPIE:
236 case OPT_fpie:
237 case OPT_fcommon:
238 case OPT_fexceptions:
239 case OPT_fnon_call_exceptions:
240 case OPT_fgnu_tm:
241 /* Do what the old LTO code did - collect exactly one option
242 setting per OPT code, we pick the first we encounter.
243 ??? This doesn't make too much sense, but when it doesn't
244 then we should complain. */
245 for (j = 0; j < *decoded_options_count; ++j)
246 if ((*decoded_options)[j].opt_index == foption->opt_index)
247 break;
248 if (j == *decoded_options_count)
249 append_option (decoded_options, decoded_options_count, foption);
250 break;
252 case OPT_ftrapv:
253 case OPT_fstrict_overflow:
254 case OPT_ffp_contract_:
255 /* For selected options we can merge conservatively. */
256 for (j = 0; j < *decoded_options_count; ++j)
257 if ((*decoded_options)[j].opt_index == foption->opt_index)
258 break;
259 if (j == *decoded_options_count)
260 append_option (decoded_options, decoded_options_count, foption);
261 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
262 -fno-trapv < -ftrapv,
263 -fno-strict-overflow < -fstrict-overflow */
264 else if (foption->value < (*decoded_options)[j].value)
265 (*decoded_options)[j] = *foption;
266 break;
268 case OPT_fmath_errno:
269 case OPT_fsigned_zeros:
270 case OPT_ftrapping_math:
271 case OPT_fwrapv:
272 /* For selected options we can merge conservatively. */
273 for (j = 0; j < *decoded_options_count; ++j)
274 if ((*decoded_options)[j].opt_index == foption->opt_index)
275 break;
276 if (j == *decoded_options_count)
277 append_option (decoded_options, decoded_options_count, foption);
278 /* -fmath-errno > -fno-math-errno,
279 -fsigned-zeros > -fno-signed-zeros,
280 -ftrapping-math -> -fno-trapping-math,
281 -fwrapv > -fno-wrapv. */
282 else if (foption->value > (*decoded_options)[j].value)
283 (*decoded_options)[j] = *foption;
284 break;
286 case OPT_freg_struct_return:
287 case OPT_fpcc_struct_return:
288 case OPT_fshort_double:
289 for (j = 0; j < *decoded_options_count; ++j)
290 if ((*decoded_options)[j].opt_index == foption->opt_index)
291 break;
292 if (j == *decoded_options_count)
293 fatal_error ("Option %s not used consistently in all LTO input"
294 " files", foption->orig_option_with_args_text);
295 break;
297 case OPT_O:
298 case OPT_Ofast:
299 case OPT_Og:
300 case OPT_Os:
301 for (j = 0; j < *decoded_options_count; ++j)
302 if ((*decoded_options)[j].opt_index == OPT_O
303 || (*decoded_options)[j].opt_index == OPT_Ofast
304 || (*decoded_options)[j].opt_index == OPT_Og
305 || (*decoded_options)[j].opt_index == OPT_Os)
306 break;
307 if (j == *decoded_options_count)
308 append_option (decoded_options, decoded_options_count, foption);
309 else if ((*decoded_options)[j].opt_index == foption->opt_index
310 && foption->opt_index != OPT_O)
311 /* Exact same options get merged. */
313 else
315 /* For mismatched option kinds preserve the optimization
316 level only, thus merge it as -On. This also handles
317 merging of same optimization level -On. */
318 int level = 0;
319 switch (foption->opt_index)
321 case OPT_O:
322 if (foption->arg[0] == '\0')
323 level = MAX (level, 1);
324 else
325 level = MAX (level, atoi (foption->arg));
326 break;
327 case OPT_Ofast:
328 level = MAX (level, 3);
329 break;
330 case OPT_Og:
331 level = MAX (level, 1);
332 break;
333 case OPT_Os:
334 level = MAX (level, 2);
335 break;
336 default:
337 gcc_unreachable ();
339 switch ((*decoded_options)[j].opt_index)
341 case OPT_O:
342 if ((*decoded_options)[j].arg[0] == '\0')
343 level = MAX (level, 1);
344 else
345 level = MAX (level, atoi ((*decoded_options)[j].arg));
346 break;
347 case OPT_Ofast:
348 level = MAX (level, 3);
349 break;
350 case OPT_Og:
351 level = MAX (level, 1);
352 break;
353 case OPT_Os:
354 level = MAX (level, 2);
355 break;
356 default:
357 gcc_unreachable ();
359 (*decoded_options)[j].opt_index = OPT_O;
360 char *tem;
361 asprintf (&tem, "-O%d", level);
362 (*decoded_options)[j].arg = &tem[2];
363 (*decoded_options)[j].canonical_option[0] = tem;
364 (*decoded_options)[j].value = 1;
366 break;
371 /* Auxiliary function that frees elements of PTR and PTR itself.
372 N is number of elements to be freed.
373 If PTR is NULL, nothing is freed. If an element is NULL, subsequent elements
374 are not freed. */
375 static void**
376 free_array_of_ptrs (void **ptr, unsigned n)
378 unsigned i;
379 if (!ptr)
380 return NULL;
381 for (i = 0; i < n; i++)
383 if (!ptr[i])
384 break;
385 free (ptr[i]);
387 free (ptr);
388 return NULL;
391 /* Parse STR, saving found tokens into PVALUES and return their number.
392 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
393 append it to every token we find. */
395 static unsigned
396 parse_env_var (const char *str, char ***pvalues, const char *append)
398 const char *curval, *nextval;
399 char **values;
400 unsigned num = 1, i;
402 curval = strchr (str, ':');
403 while (curval)
405 num++;
406 curval = strchr (curval + 1, ':');
409 values = (char**) xmalloc (num * sizeof (char*));
410 curval = str;
411 nextval = strchrnul (curval, ':');
413 int append_len = append ? strlen (append) : 0;
414 for (i = 0; i < num; i++)
416 int l = nextval - curval;
417 values[i] = (char*) xmalloc (l + 1 + append_len);
418 memcpy (values[i], curval, l);
419 values[i][l] = 0;
420 if (append)
421 strcat (values[i], append);
422 curval = nextval + 1;
423 nextval = strchrnul (curval, ':');
425 *pvalues = values;
426 return num;
429 /* Check whether NAME can be accessed in MODE. This is like access,
430 except that it never considers directories to be executable. */
432 static int
433 access_check (const char *name, int mode)
435 if (mode == X_OK)
437 struct stat st;
439 if (stat (name, &st) < 0
440 || S_ISDIR (st.st_mode))
441 return -1;
444 return access (name, mode);
447 /* Prepare target image for target NAME.
448 Firstly, we execute COMPILER, passing all input files to it to produce DSO.
449 When target DSO is ready, we pass it to objcopy to place its image into a
450 special data section. After that we rename target image's symbols to values,
451 expected by the host side, and return the name of the resultant file. */
453 static char*
454 prepare_target_image (const char *target, const char *compiler_path,
455 unsigned in_argc, char *in_argv[])
457 const char **argv;
458 struct obstack argv_obstack;
459 unsigned i;
460 char *filename = NULL;
461 char *suffix = XALLOCAVEC (char, strlen ("/accel//mkoffload") + 1 + strlen (target));
462 const char *compiler = NULL;
464 strcpy (suffix, "/accel/");
465 strcat (suffix, target);
466 strcat (suffix, "/mkoffload");
468 char **paths;
469 int n_paths = parse_env_var (compiler_path, &paths, suffix);
471 for (int i = 0; i < n_paths; i++)
472 if (access_check (paths[i], X_OK) == 0)
474 compiler = paths[i];
475 break;
478 if (compiler == NULL)
479 goto out;
481 /* Generate temp file name. */
482 filename = make_temp_file (".target.o");
484 /* -------------------------------------- */
485 /* Run gcc for target. */
486 obstack_init (&argv_obstack);
487 obstack_ptr_grow (&argv_obstack, compiler);
488 obstack_ptr_grow (&argv_obstack, "-o");
489 obstack_ptr_grow (&argv_obstack, filename);
491 for (i = 1; i < in_argc; ++i)
492 if (strncmp (in_argv[i], "-fresolution=", sizeof ("-fresolution=") - 1))
493 obstack_ptr_grow (&argv_obstack, in_argv[i]);
494 obstack_ptr_grow (&argv_obstack, NULL);
496 argv = XOBFINISH (&argv_obstack, const char **);
497 fork_execute (argv[0], CONST_CAST (char **, argv), true);
498 obstack_free (&argv_obstack, NULL);
500 out:
501 free_array_of_ptrs ((void**) paths, n_paths);
502 return filename;
506 /* The main routine dealing with openmp offloading.
507 The routine builds a target image for each offloading target.
508 IN_ARGC and IN_ARGV specify input files. As all of them could contain
509 omp-sections, we pass them all to target compilers.
510 Env-variable OFFLOAD_TARGET_NAMES_ENV describes for which targets we should
511 build images.
512 This function stores the names of the object files in the OFFLOAD_NAMES
513 array. */
515 static void
516 compile_images_for_openmp_targets (unsigned in_argc, char *in_argv[])
518 char *target_names;
519 char **names;
520 unsigned num_targets;
522 /* Obtain names of offload targets and corresponding compilers. */
523 target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
524 if (!target_names)
525 return;
527 num_targets = parse_env_var (target_names, &names, NULL);
529 const char *compiler_path = getenv ("COMPILER_PATH");
530 if (compiler_path == NULL)
531 goto out;
533 /* Prepare an image for each target. The array is terminated by a NULL
534 entry. */
535 offload_names = XCNEWVEC (char *, num_targets + 1);
536 for (unsigned i = 0; i < num_targets; i++)
538 offload_names[i] = prepare_target_image (names[i], compiler_path,
539 in_argc, in_argv);
540 if (!offload_names[i])
541 fatal_error ("problem with building target image for %s: %m",
542 names[i]);
545 out:
546 free_array_of_ptrs ((void**) names, num_targets);
549 /* Copy a file from SRC to DEST. */
550 static void
551 copy_file (const char *dest, const char *src)
553 FILE *d = fopen (dest, "wb");
554 FILE *s = fopen (src, "rb");
555 char buffer[512];
556 while (!feof (s))
558 size_t len = fread (buffer, 1, 512, s);
559 if (ferror (s) != 0)
560 fatal_error ("reading input file");
561 if (len > 0)
563 fwrite (buffer, 1, len, d);
564 if (ferror (d) != 0)
565 fatal_error ("writing output file");
570 /* Find the omp_begin.o and omp_end.o files in LIBRARY_PATH, make copies
571 and store the names of the copies in ompbegin and ompend. */
573 static void
574 find_ompbeginend (void)
576 char **paths;
577 const char *library_path = getenv ("LIBRARY_PATH");
578 if (library_path == NULL)
579 return;
580 int n_paths = parse_env_var (library_path, &paths, "/crtompbegin.o");
582 int i;
583 for (i = 0; i < n_paths; i++)
584 if (access_check (paths[i], R_OK) == 0)
586 size_t len = strlen (paths[i]);
587 char *tmp = xstrdup (paths[i]);
588 strcpy (paths[i] + len - 7, "end.o");
589 if (access_check (paths[i], R_OK) != 0)
590 fatal_error ("installation error, can't find crtompend.o");
591 /* The linker will delete the filenames we give it, so make
592 copies. */
593 const char *omptmp1 = make_temp_file (".o");
594 const char *omptmp2 = make_temp_file (".o");
595 copy_file (omptmp1, tmp);
596 ompbegin = omptmp1;
597 copy_file (omptmp2, paths[i]);
598 ompend = omptmp2;
599 free (tmp);
600 break;
602 if (i == n_paths)
603 fatal_error ("installation error, can't find crtompbegin.o");
605 free_array_of_ptrs ((void**) paths, n_paths);
608 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
610 static void
611 run_gcc (unsigned argc, char *argv[])
613 unsigned i, j;
614 const char **new_argv;
615 const char **argv_ptr;
616 char *list_option_full = NULL;
617 const char *linker_output = NULL;
618 const char *collect_gcc, *collect_gcc_options;
619 int parallel = 0;
620 int jobserver = 0;
621 bool no_partition = false;
622 struct cl_decoded_option *fdecoded_options = NULL;
623 unsigned int fdecoded_options_count = 0;
624 struct cl_decoded_option *decoded_options;
625 unsigned int decoded_options_count;
626 struct obstack argv_obstack;
627 int new_head_argc;
628 bool have_offload = false;
630 /* Get the driver and options. */
631 collect_gcc = getenv ("COLLECT_GCC");
632 if (!collect_gcc)
633 fatal_error ("environment variable COLLECT_GCC must be set");
634 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
635 if (!collect_gcc_options)
636 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
637 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
638 CL_LANG_ALL,
639 &decoded_options,
640 &decoded_options_count);
642 /* Look at saved options in the IL files. */
643 for (i = 1; i < argc; ++i)
645 char *data, *p;
646 char *fopts;
647 int fd;
648 const char *errmsg;
649 int err;
650 off_t file_offset = 0, offset, length;
651 long loffset;
652 simple_object_read *sobj;
653 int consumed;
654 struct cl_decoded_option *f2decoded_options;
655 unsigned int f2decoded_options_count;
656 char *filename = argv[i];
657 if ((p = strrchr (argv[i], '@'))
658 && p != argv[i]
659 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
660 && strlen (p) == (unsigned int) consumed)
662 filename = XNEWVEC (char, p - argv[i] + 1);
663 memcpy (filename, argv[i], p - argv[i]);
664 filename[p - argv[i]] = '\0';
665 file_offset = (off_t) loffset;
667 fd = open (argv[i], O_RDONLY);
668 if (fd == -1)
669 continue;
670 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
671 &errmsg, &err);
672 if (!sobj)
674 close (fd);
675 continue;
677 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
678 &offset, &length, &errmsg, &err))
680 simple_object_release_read (sobj);
681 close (fd);
682 continue;
684 /* We may choose not to write out this .opts section in the future. In
685 that case we'll have to use something else to look for. */
686 if (simple_object_find_section (sobj, OMP_SECTION_NAME_PREFIX "." "opts",
687 &offset, &length, &errmsg, &err))
688 have_offload = true;
689 lseek (fd, file_offset + offset, SEEK_SET);
690 data = (char *)xmalloc (length);
691 read (fd, data, length);
692 fopts = data;
695 get_options_from_collect_gcc_options (collect_gcc,
696 fopts, CL_LANG_ALL,
697 &f2decoded_options,
698 &f2decoded_options_count);
699 if (!fdecoded_options)
701 fdecoded_options = f2decoded_options;
702 fdecoded_options_count = f2decoded_options_count;
704 else
705 merge_and_complain (&fdecoded_options,
706 &fdecoded_options_count,
707 f2decoded_options, f2decoded_options_count);
709 fopts += strlen (fopts) + 1;
711 while (fopts - data < length);
713 free (data);
714 simple_object_release_read (sobj);
715 close (fd);
718 /* Initalize the common arguments for the driver. */
719 obstack_init (&argv_obstack);
720 obstack_ptr_grow (&argv_obstack, collect_gcc);
721 obstack_ptr_grow (&argv_obstack, "-xlto");
722 obstack_ptr_grow (&argv_obstack, "-c");
724 /* Append compiler driver arguments as far as they were merged. */
725 for (j = 1; j < fdecoded_options_count; ++j)
727 struct cl_decoded_option *option = &fdecoded_options[j];
729 /* File options have been properly filtered by lto-opts.c. */
730 switch (option->opt_index)
732 /* Drop arguments that we want to take from the link line. */
733 case OPT_flto_:
734 case OPT_flto:
735 case OPT_flto_partition_:
736 continue;
738 default:
739 break;
742 /* For now do what the original LTO option code was doing - pass
743 on any CL_TARGET flag and a few selected others. */
744 switch (option->opt_index)
746 case OPT_fPIC:
747 case OPT_fpic:
748 case OPT_fPIE:
749 case OPT_fpie:
750 case OPT_fcommon:
751 case OPT_fexceptions:
752 case OPT_fnon_call_exceptions:
753 case OPT_fgnu_tm:
754 case OPT_freg_struct_return:
755 case OPT_fpcc_struct_return:
756 case OPT_fshort_double:
757 case OPT_ffp_contract_:
758 case OPT_fmath_errno:
759 case OPT_fsigned_zeros:
760 case OPT_ftrapping_math:
761 case OPT_fwrapv:
762 case OPT_ftrapv:
763 case OPT_fstrict_overflow:
764 case OPT_O:
765 case OPT_Ofast:
766 case OPT_Og:
767 case OPT_Os:
768 break;
770 default:
771 if (!(cl_options[option->opt_index].flags & CL_TARGET))
772 continue;
775 /* Pass the option on. */
776 for (i = 0; i < option->canonical_option_num_elements; ++i)
777 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
780 /* Append linker driver arguments. Compiler options from the linker
781 driver arguments will override / merge with those from the compiler. */
782 for (j = 1; j < decoded_options_count; ++j)
784 struct cl_decoded_option *option = &decoded_options[j];
786 /* Do not pass on frontend specific flags not suitable for lto. */
787 if (!(cl_options[option->opt_index].flags
788 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
789 continue;
791 switch (option->opt_index)
793 case OPT_o:
794 linker_output = option->arg;
795 /* We generate new intermediate output, drop this arg. */
796 continue;
798 case OPT_save_temps:
799 save_temps = 1;
800 break;
802 case OPT_v:
803 verbose = 1;
804 break;
806 case OPT_flto_partition_:
807 if (strcmp (option->arg, "none") == 0)
808 no_partition = true;
809 break;
811 case OPT_flto_:
812 if (strcmp (option->arg, "jobserver") == 0)
814 jobserver = 1;
815 parallel = 1;
817 else
819 parallel = atoi (option->arg);
820 if (parallel <= 1)
821 parallel = 0;
823 /* Fallthru. */
825 case OPT_flto:
826 lto_mode = LTO_MODE_WHOPR;
827 /* We've handled these LTO options, do not pass them on. */
828 continue;
830 case OPT_freg_struct_return:
831 case OPT_fpcc_struct_return:
832 case OPT_fshort_double:
833 /* Ignore these, they are determined by the input files.
834 ??? We fail to diagnose a possible mismatch here. */
835 continue;
837 default:
838 break;
841 /* Pass the option on. */
842 for (i = 0; i < option->canonical_option_num_elements; ++i)
843 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
846 if (no_partition)
848 lto_mode = LTO_MODE_LTO;
849 jobserver = 0;
850 parallel = 0;
853 if (linker_output)
855 char *output_dir, *base, *name;
856 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
858 output_dir = xstrdup (linker_output);
859 base = output_dir;
860 for (name = base; *name; name++)
861 if (IS_DIR_SEPARATOR (*name))
862 base = name + 1;
863 *base = '\0';
865 linker_output = &linker_output[base - output_dir];
866 if (*output_dir == '\0')
868 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
869 output_dir = current_dir;
871 if (!bit_bucket)
873 obstack_ptr_grow (&argv_obstack, "-dumpdir");
874 obstack_ptr_grow (&argv_obstack, output_dir);
877 obstack_ptr_grow (&argv_obstack, "-dumpbase");
880 /* Remember at which point we can scrub args to re-use the commons. */
881 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
883 if (lto_mode == LTO_MODE_LTO)
885 flto_out = make_temp_file (".lto.o");
886 if (linker_output)
887 obstack_ptr_grow (&argv_obstack, linker_output);
888 obstack_ptr_grow (&argv_obstack, "-o");
889 obstack_ptr_grow (&argv_obstack, flto_out);
891 else
893 const char *list_option = "-fltrans-output-list=";
894 size_t list_option_len = strlen (list_option);
895 char *tmp;
897 if (linker_output)
899 char *dumpbase = (char *) xmalloc (strlen (linker_output)
900 + sizeof (".wpa") + 1);
901 strcpy (dumpbase, linker_output);
902 strcat (dumpbase, ".wpa");
903 obstack_ptr_grow (&argv_obstack, dumpbase);
906 if (linker_output && save_temps)
908 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
909 + sizeof (".ltrans.out") + 1);
910 strcpy (ltrans_output_file, linker_output);
911 strcat (ltrans_output_file, ".ltrans.out");
913 else
914 ltrans_output_file = make_temp_file (".ltrans.out");
915 list_option_full = (char *) xmalloc (sizeof (char) *
916 (strlen (ltrans_output_file) + list_option_len + 1));
917 tmp = list_option_full;
919 obstack_ptr_grow (&argv_obstack, tmp);
920 strcpy (tmp, list_option);
921 tmp += list_option_len;
922 strcpy (tmp, ltrans_output_file);
924 if (jobserver)
925 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
926 else if (parallel > 1)
928 char buf[256];
929 sprintf (buf, "-fwpa=%i", parallel);
930 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
932 else
933 obstack_ptr_grow (&argv_obstack, "-fwpa");
936 /* Append the input objects and possible preceding arguments. */
937 for (i = 1; i < argc; ++i)
938 obstack_ptr_grow (&argv_obstack, argv[i]);
939 obstack_ptr_grow (&argv_obstack, NULL);
941 new_argv = XOBFINISH (&argv_obstack, const char **);
942 argv_ptr = &new_argv[new_head_argc];
943 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
945 if (lto_mode == LTO_MODE_LTO)
947 printf ("%s\n", flto_out);
948 free (flto_out);
949 flto_out = NULL;
951 else
953 FILE *stream = fopen (ltrans_output_file, "r");
954 FILE *mstream = NULL;
955 struct obstack env_obstack;
957 if (!stream)
958 fatal_error ("fopen: %s: %m", ltrans_output_file);
960 /* Parse the list of LTRANS inputs from the WPA stage. */
961 obstack_init (&env_obstack);
962 nr = 0;
963 for (;;)
965 const unsigned piece = 32;
966 char *output_name = NULL;
967 char *buf, *input_name = (char *)xmalloc (piece);
968 size_t len;
970 buf = input_name;
971 cont:
972 if (!fgets (buf, piece, stream))
973 break;
974 len = strlen (input_name);
975 if (input_name[len - 1] != '\n')
977 input_name = (char *)xrealloc (input_name, len + piece);
978 buf = input_name + len;
979 goto cont;
981 input_name[len - 1] = '\0';
983 if (input_name[0] == '*')
984 output_name = &input_name[1];
986 nr++;
987 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
988 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
989 input_names[nr-1] = input_name;
990 output_names[nr-1] = output_name;
992 fclose (stream);
993 maybe_unlink (ltrans_output_file);
994 ltrans_output_file = NULL;
996 if (parallel)
998 makefile = make_temp_file (".mk");
999 mstream = fopen (makefile, "w");
1002 /* Execute the LTRANS stage for each input file (or prepare a
1003 makefile to invoke this in parallel). */
1004 for (i = 0; i < nr; ++i)
1006 char *output_name;
1007 char *input_name = input_names[i];
1008 /* If it's a pass-through file do nothing. */
1009 if (output_names[i])
1010 continue;
1012 /* Replace the .o suffix with a .ltrans.o suffix and write
1013 the resulting name to the LTRANS output list. */
1014 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1015 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1016 output_name = XOBFINISH (&env_obstack, char *);
1018 /* Adjust the dumpbase if the linker output file was seen. */
1019 if (linker_output)
1021 char *dumpbase
1022 = (char *) xmalloc (strlen (linker_output)
1023 + sizeof (DUMPBASE_SUFFIX) + 1);
1024 snprintf (dumpbase,
1025 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1026 "%s.ltrans%u", linker_output, i);
1027 argv_ptr[0] = dumpbase;
1030 argv_ptr[1] = "-fltrans";
1031 argv_ptr[2] = "-o";
1032 argv_ptr[3] = output_name;
1033 argv_ptr[4] = input_name;
1034 argv_ptr[5] = NULL;
1035 if (parallel)
1037 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1038 for (j = 1; new_argv[j] != NULL; ++j)
1039 fprintf (mstream, " '%s'", new_argv[j]);
1040 fprintf (mstream, "\n");
1041 /* If we are not preserving the ltrans input files then
1042 truncate them as soon as we have processed it. This
1043 reduces temporary disk-space usage. */
1044 if (! save_temps)
1045 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1046 "&& mv %s.tem %s\n",
1047 input_name, input_name, input_name, input_name);
1049 else
1051 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1052 true);
1053 maybe_unlink (input_name);
1056 output_names[i] = output_name;
1058 if (parallel)
1060 struct pex_obj *pex;
1061 char jobs[32];
1063 fprintf (mstream, "all:");
1064 for (i = 0; i < nr; ++i)
1065 fprintf (mstream, " \\\n\t%s", output_names[i]);
1066 fprintf (mstream, "\n");
1067 fclose (mstream);
1068 if (!jobserver)
1070 /* Avoid passing --jobserver-fd= and similar flags
1071 unless jobserver mode is explicitly enabled. */
1072 putenv (xstrdup ("MAKEFLAGS="));
1073 putenv (xstrdup ("MFLAGS="));
1075 new_argv[0] = getenv ("MAKE");
1076 if (!new_argv[0])
1077 new_argv[0] = "make";
1078 new_argv[1] = "-f";
1079 new_argv[2] = makefile;
1080 i = 3;
1081 if (!jobserver)
1083 snprintf (jobs, 31, "-j%d", parallel);
1084 new_argv[i++] = jobs;
1086 new_argv[i++] = "all";
1087 new_argv[i++] = NULL;
1088 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1089 NULL, NULL, PEX_SEARCH, false);
1090 do_wait (new_argv[0], pex);
1091 maybe_unlink (makefile);
1092 makefile = NULL;
1093 for (i = 0; i < nr; ++i)
1094 maybe_unlink (input_names[i]);
1096 if (have_offload)
1098 compile_images_for_openmp_targets (argc, argv);
1099 if (offload_names)
1101 find_ompbeginend ();
1102 for (i = 0; offload_names[i]; i++)
1104 fputs (offload_names[i], stdout);
1105 putc ('\n', stdout);
1107 free_array_of_ptrs ((void **)offload_names, i);
1110 if (ompbegin)
1112 fputs (ompbegin, stdout);
1113 putc ('\n', stdout);
1116 for (i = 0; i < nr; ++i)
1118 fputs (output_names[i], stdout);
1119 putc ('\n', stdout);
1120 free (input_names[i]);
1122 if (ompend)
1124 fputs (ompend, stdout);
1125 putc ('\n', stdout);
1127 nr = 0;
1128 free (output_names);
1129 free (input_names);
1130 free (list_option_full);
1131 obstack_free (&env_obstack, NULL);
1134 obstack_free (&argv_obstack, NULL);
1138 /* Entry point. */
1141 main (int argc, char *argv[])
1143 const char *p;
1145 gcc_obstack_init (&opts_obstack);
1147 p = argv[0] + strlen (argv[0]);
1148 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1149 --p;
1150 progname = p;
1152 xmalloc_set_program_name (progname);
1154 if (atexit (lto_wrapper_cleanup) != 0)
1155 fatal_error ("atexit failed");
1157 gcc_init_libintl ();
1159 diagnostic_initialize (global_dc, 0);
1161 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1162 signal (SIGINT, fatal_signal);
1163 #ifdef SIGHUP
1164 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1165 signal (SIGHUP, fatal_signal);
1166 #endif
1167 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1168 signal (SIGTERM, fatal_signal);
1169 #ifdef SIGPIPE
1170 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1171 signal (SIGPIPE, fatal_signal);
1172 #endif
1173 #ifdef SIGCHLD
1174 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1175 receive the signal. A different setting is inheritable */
1176 signal (SIGCHLD, SIG_DFL);
1177 #endif
1179 /* We may be called with all the arguments stored in some file and
1180 passed with @file. Expand them into argv before processing. */
1181 expandargv (&argc, &argv);
1183 run_gcc (argc, argv);
1185 return 0;