Force a dependence distance of 1 in gnat.dg/vect17.adb
[official-gcc.git] / gcc / lto-wrapper.c
blob2545a317fb61accee713ce7521933b239d2d89ad
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2017 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 /* Environment variable, used for passing the names of offload targets from GCC
53 driver to lto-wrapper. */
54 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
56 enum lto_mode_d {
57 LTO_MODE_NONE, /* Not doing LTO. */
58 LTO_MODE_LTO, /* Normal LTO. */
59 LTO_MODE_WHOPR /* WHOPR. */
62 /* Current LTO mode. */
63 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
65 static char *ltrans_output_file;
66 static char *flto_out;
67 static unsigned int nr;
68 static char **input_names;
69 static char **output_names;
70 static char **offload_names;
71 static char *offload_objects_file_name;
72 static char *makefile;
74 const char tool_name[] = "lto-wrapper";
76 /* Delete tempfiles. Called from utils_cleanup. */
78 void
79 tool_cleanup (bool)
81 unsigned int i;
83 if (ltrans_output_file)
84 maybe_unlink (ltrans_output_file);
85 if (flto_out)
86 maybe_unlink (flto_out);
87 if (offload_objects_file_name)
88 maybe_unlink (offload_objects_file_name);
89 if (makefile)
90 maybe_unlink (makefile);
91 for (i = 0; i < nr; ++i)
93 maybe_unlink (input_names[i]);
94 if (output_names[i])
95 maybe_unlink (output_names[i]);
99 static void
100 lto_wrapper_cleanup (void)
102 utils_cleanup (false);
105 /* Unlink a temporary LTRANS file unless requested otherwise. */
107 void
108 maybe_unlink (const char *file)
110 if (!save_temps)
112 if (unlink_if_ordinary (file)
113 && errno != ENOENT)
114 fatal_error (input_location, "deleting LTRANS file %s: %m", file);
116 else if (verbose)
117 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
120 /* Template of LTRANS dumpbase suffix. */
121 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
123 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
124 environment according to LANG_MASK. */
126 static void
127 get_options_from_collect_gcc_options (const char *collect_gcc,
128 const char *collect_gcc_options,
129 unsigned int lang_mask,
130 struct cl_decoded_option **decoded_options,
131 unsigned int *decoded_options_count)
133 struct obstack argv_obstack;
134 char *argv_storage;
135 const char **argv;
136 int j, k, argc;
138 argv_storage = xstrdup (collect_gcc_options);
139 obstack_init (&argv_obstack);
140 obstack_ptr_grow (&argv_obstack, collect_gcc);
142 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
144 if (argv_storage[j] == '\'')
146 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
147 ++j;
150 if (argv_storage[j] == '\0')
151 fatal_error (input_location, "malformed COLLECT_GCC_OPTIONS");
152 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
154 argv_storage[k++] = '\'';
155 j += 4;
157 else if (argv_storage[j] == '\'')
158 break;
159 else
160 argv_storage[k++] = argv_storage[j++];
162 while (1);
163 argv_storage[k++] = '\0';
167 obstack_ptr_grow (&argv_obstack, NULL);
168 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
169 argv = XOBFINISH (&argv_obstack, const char **);
171 decode_cmdline_options_to_array (argc, (const char **)argv,
172 lang_mask,
173 decoded_options, decoded_options_count);
174 obstack_free (&argv_obstack, NULL);
177 /* Append OPTION to the options array DECODED_OPTIONS with size
178 DECODED_OPTIONS_COUNT. */
180 static void
181 append_option (struct cl_decoded_option **decoded_options,
182 unsigned int *decoded_options_count,
183 struct cl_decoded_option *option)
185 ++*decoded_options_count;
186 *decoded_options
187 = (struct cl_decoded_option *)
188 xrealloc (*decoded_options,
189 (*decoded_options_count
190 * sizeof (struct cl_decoded_option)));
191 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
192 sizeof (struct cl_decoded_option));
195 /* Try to merge and complain about options FDECODED_OPTIONS when applied
196 ontop of DECODED_OPTIONS. */
198 static void
199 merge_and_complain (struct cl_decoded_option **decoded_options,
200 unsigned int *decoded_options_count,
201 struct cl_decoded_option *fdecoded_options,
202 unsigned int fdecoded_options_count)
204 unsigned int i, j;
206 /* ??? Merge options from files. Most cases can be
207 handled by either unioning or intersecting
208 (for example -fwrapv is a case for unioning,
209 -ffast-math is for intersection). Most complaints
210 about real conflicts between different options can
211 be deferred to the compiler proper. Options that
212 we can neither safely handle by intersection nor
213 unioning would need to be complained about here.
214 Ideally we'd have a flag in the opt files that
215 tells whether to union or intersect or reject.
216 In absence of that it's unclear what a good default is.
217 It's also difficult to get positional handling correct. */
219 /* The following does what the old LTO option code did,
220 union all target and a selected set of common options. */
221 for (i = 0; i < fdecoded_options_count; ++i)
223 struct cl_decoded_option *foption = &fdecoded_options[i];
224 switch (foption->opt_index)
226 case OPT_SPECIAL_unknown:
227 case OPT_SPECIAL_ignore:
228 case OPT_SPECIAL_program_name:
229 case OPT_SPECIAL_input_file:
230 break;
232 default:
233 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
234 break;
236 /* Fallthru. */
237 case OPT_fdiagnostics_show_caret:
238 case OPT_fdiagnostics_show_option:
239 case OPT_fdiagnostics_show_location_:
240 case OPT_fshow_column:
241 case OPT_fPIC:
242 case OPT_fpic:
243 case OPT_fPIE:
244 case OPT_fpie:
245 case OPT_fcommon:
246 case OPT_fgnu_tm:
247 /* Do what the old LTO code did - collect exactly one option
248 setting per OPT code, we pick the first we encounter.
249 ??? This doesn't make too much sense, but when it doesn't
250 then we should complain. */
251 for (j = 0; j < *decoded_options_count; ++j)
252 if ((*decoded_options)[j].opt_index == foption->opt_index)
253 break;
254 if (j == *decoded_options_count)
255 append_option (decoded_options, decoded_options_count, foption);
256 break;
258 case OPT_fopenmp:
259 case OPT_fopenacc:
260 case OPT_fcilkplus:
261 case OPT_fcheck_pointer_bounds:
262 /* For selected options we can merge conservatively. */
263 for (j = 0; j < *decoded_options_count; ++j)
264 if ((*decoded_options)[j].opt_index == foption->opt_index)
265 break;
266 if (j == *decoded_options_count)
267 append_option (decoded_options, decoded_options_count, foption);
268 /* -fopenmp > -fno-openmp,
269 -fopenacc > -fno-openacc,
270 -fcilkplus > -fno-cilkplus,
271 -fcheck_pointer_bounds > -fcheck_pointer_bounds */
272 else if (foption->value > (*decoded_options)[j].value)
273 (*decoded_options)[j] = *foption;
274 break;
276 case OPT_fopenacc_dim_:
277 /* Append or check identical. */
278 for (j = 0; j < *decoded_options_count; ++j)
279 if ((*decoded_options)[j].opt_index == foption->opt_index)
280 break;
281 if (j == *decoded_options_count)
282 append_option (decoded_options, decoded_options_count, foption);
283 else if (strcmp ((*decoded_options)[j].arg, foption->arg))
284 fatal_error (input_location,
285 "Option %s with different values",
286 foption->orig_option_with_args_text);
287 break;
289 case OPT_foffload_abi_:
290 for (j = 0; j < *decoded_options_count; ++j)
291 if ((*decoded_options)[j].opt_index == foption->opt_index)
292 break;
293 if (j == *decoded_options_count)
294 append_option (decoded_options, decoded_options_count, foption);
295 else if (foption->value != (*decoded_options)[j].value)
296 fatal_error (input_location,
297 "Option %s not used consistently in all LTO input"
298 " files", foption->orig_option_with_args_text);
299 break;
301 case OPT_O:
302 case OPT_Ofast:
303 case OPT_Og:
304 case OPT_Os:
305 for (j = 0; j < *decoded_options_count; ++j)
306 if ((*decoded_options)[j].opt_index == OPT_O
307 || (*decoded_options)[j].opt_index == OPT_Ofast
308 || (*decoded_options)[j].opt_index == OPT_Og
309 || (*decoded_options)[j].opt_index == OPT_Os)
310 break;
311 if (j == *decoded_options_count)
312 append_option (decoded_options, decoded_options_count, foption);
313 else if ((*decoded_options)[j].opt_index == foption->opt_index
314 && foption->opt_index != OPT_O)
315 /* Exact same options get merged. */
317 else
319 /* For mismatched option kinds preserve the optimization
320 level only, thus merge it as -On. This also handles
321 merging of same optimization level -On. */
322 int level = 0;
323 switch (foption->opt_index)
325 case OPT_O:
326 if (foption->arg[0] == '\0')
327 level = MAX (level, 1);
328 else
329 level = MAX (level, atoi (foption->arg));
330 break;
331 case OPT_Ofast:
332 level = MAX (level, 3);
333 break;
334 case OPT_Og:
335 level = MAX (level, 1);
336 break;
337 case OPT_Os:
338 level = MAX (level, 2);
339 break;
340 default:
341 gcc_unreachable ();
343 switch ((*decoded_options)[j].opt_index)
345 case OPT_O:
346 if ((*decoded_options)[j].arg[0] == '\0')
347 level = MAX (level, 1);
348 else
349 level = MAX (level, atoi ((*decoded_options)[j].arg));
350 break;
351 case OPT_Ofast:
352 level = MAX (level, 3);
353 break;
354 case OPT_Og:
355 level = MAX (level, 1);
356 break;
357 case OPT_Os:
358 level = MAX (level, 2);
359 break;
360 default:
361 gcc_unreachable ();
363 (*decoded_options)[j].opt_index = OPT_O;
364 char *tem;
365 tem = xasprintf ("-O%d", level);
366 (*decoded_options)[j].arg = &tem[2];
367 (*decoded_options)[j].canonical_option[0] = tem;
368 (*decoded_options)[j].value = 1;
370 break;
372 case OPT_foffload_:
373 append_option (decoded_options, decoded_options_count, foption);
374 break;
379 /* Auxiliary function that frees elements of PTR and PTR itself.
380 N is number of elements to be freed. If PTR is NULL, nothing is freed.
381 If an element is NULL, subsequent elements are not freed. */
383 static void **
384 free_array_of_ptrs (void **ptr, unsigned n)
386 if (!ptr)
387 return NULL;
388 for (unsigned i = 0; i < n; i++)
390 if (!ptr[i])
391 break;
392 free (ptr[i]);
394 free (ptr);
395 return NULL;
398 /* Parse STR, saving found tokens into PVALUES and return their number.
399 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
400 append it to every token we find. */
402 static unsigned
403 parse_env_var (const char *str, char ***pvalues, const char *append)
405 const char *curval, *nextval;
406 char **values;
407 unsigned num = 1, i;
409 curval = strchr (str, ':');
410 while (curval)
412 num++;
413 curval = strchr (curval + 1, ':');
416 values = (char**) xmalloc (num * sizeof (char*));
417 curval = str;
418 nextval = strchr (curval, ':');
419 if (nextval == NULL)
420 nextval = strchr (curval, '\0');
422 int append_len = append ? strlen (append) : 0;
423 for (i = 0; i < num; i++)
425 int l = nextval - curval;
426 values[i] = (char*) xmalloc (l + 1 + append_len);
427 memcpy (values[i], curval, l);
428 values[i][l] = 0;
429 if (append)
430 strcat (values[i], append);
431 curval = nextval + 1;
432 nextval = strchr (curval, ':');
433 if (nextval == NULL)
434 nextval = strchr (curval, '\0');
436 *pvalues = values;
437 return num;
440 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
442 static void
443 append_compiler_options (obstack *argv_obstack, struct cl_decoded_option *opts,
444 unsigned int count)
446 /* Append compiler driver arguments as far as they were merged. */
447 for (unsigned int j = 1; j < count; ++j)
449 struct cl_decoded_option *option = &opts[j];
451 /* File options have been properly filtered by lto-opts.c. */
452 switch (option->opt_index)
454 /* Drop arguments that we want to take from the link line. */
455 case OPT_flto_:
456 case OPT_flto:
457 case OPT_flto_partition_:
458 continue;
460 default:
461 break;
464 /* For now do what the original LTO option code was doing - pass
465 on any CL_TARGET flag and a few selected others. */
466 switch (option->opt_index)
468 case OPT_fdiagnostics_show_caret:
469 case OPT_fdiagnostics_show_option:
470 case OPT_fdiagnostics_show_location_:
471 case OPT_fshow_column:
472 case OPT_fPIC:
473 case OPT_fpic:
474 case OPT_fPIE:
475 case OPT_fpie:
476 case OPT_fcommon:
477 case OPT_fgnu_tm:
478 case OPT_fopenmp:
479 case OPT_fopenacc:
480 case OPT_fopenacc_dim_:
481 case OPT_fcilkplus:
482 case OPT_foffload_abi_:
483 case OPT_O:
484 case OPT_Ofast:
485 case OPT_Og:
486 case OPT_Os:
487 case OPT_fcheck_pointer_bounds:
488 break;
490 default:
491 if (!(cl_options[option->opt_index].flags & CL_TARGET))
492 continue;
495 /* Pass the option on. */
496 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
497 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
501 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
503 static void
504 append_diag_options (obstack *argv_obstack, struct cl_decoded_option *opts,
505 unsigned int count)
507 /* Append compiler driver arguments as far as they were merged. */
508 for (unsigned int j = 1; j < count; ++j)
510 struct cl_decoded_option *option = &opts[j];
512 switch (option->opt_index)
514 case OPT_fdiagnostics_color_:
515 case OPT_fdiagnostics_show_caret:
516 case OPT_fdiagnostics_show_option:
517 case OPT_fdiagnostics_show_location_:
518 case OPT_fshow_column:
519 break;
520 default:
521 continue;
524 /* Pass the option on. */
525 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
526 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
531 /* Append linker options OPTS to ARGV_OBSTACK. */
533 static void
534 append_linker_options (obstack *argv_obstack, struct cl_decoded_option *opts,
535 unsigned int count)
537 /* Append linker driver arguments. Compiler options from the linker
538 driver arguments will override / merge with those from the compiler. */
539 for (unsigned int j = 1; j < count; ++j)
541 struct cl_decoded_option *option = &opts[j];
543 /* Do not pass on frontend specific flags not suitable for lto. */
544 if (!(cl_options[option->opt_index].flags
545 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
546 continue;
548 switch (option->opt_index)
550 case OPT_o:
551 case OPT_flto_:
552 case OPT_flto:
553 /* We've handled these LTO options, do not pass them on. */
554 continue;
556 case OPT_fopenmp:
557 case OPT_fopenacc:
558 case OPT_fcilkplus:
559 /* Ignore -fno-XXX form of these options, as otherwise
560 corresponding builtins will not be enabled. */
561 if (option->value == 0)
562 continue;
563 break;
565 default:
566 break;
569 /* Pass the option on. */
570 for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i)
571 obstack_ptr_grow (argv_obstack, option->canonical_option[i]);
575 /* Extract options for TARGET offload compiler from OPTIONS and append
576 them to ARGV_OBSTACK. */
578 static void
579 append_offload_options (obstack *argv_obstack, const char *target,
580 struct cl_decoded_option *options,
581 unsigned int options_count)
583 for (unsigned i = 0; i < options_count; i++)
585 const char *cur, *next, *opts;
586 char **argv;
587 unsigned argc;
588 struct cl_decoded_option *option = &options[i];
590 if (option->opt_index != OPT_foffload_)
591 continue;
593 /* If option argument starts with '-' then no target is specified. That
594 means offload options are specified for all targets, so we need to
595 append them. */
596 if (option->arg[0] == '-')
597 opts = option->arg;
598 else
600 opts = strchr (option->arg, '=');
601 /* If there are offload targets specified, but no actual options,
602 there is nothing to do here. */
603 if (!opts)
604 continue;
606 cur = option->arg;
608 while (cur < opts)
610 next = strchr (cur, ',');
611 if (next == NULL)
612 next = opts;
613 next = (next > opts) ? opts : next;
615 /* Are we looking for this offload target? */
616 if (strlen (target) == (size_t) (next - cur)
617 && strncmp (target, cur, next - cur) == 0)
618 break;
620 /* Skip the comma or equal sign. */
621 cur = next + 1;
624 if (cur >= opts)
625 continue;
627 opts++;
630 argv = buildargv (opts);
631 for (argc = 0; argv[argc]; argc++)
632 obstack_ptr_grow (argv_obstack, argv[argc]);
636 /* Check whether NAME can be accessed in MODE. This is like access,
637 except that it never considers directories to be executable. */
639 static int
640 access_check (const char *name, int mode)
642 if (mode == X_OK)
644 struct stat st;
646 if (stat (name, &st) < 0
647 || S_ISDIR (st.st_mode))
648 return -1;
651 return access (name, mode);
654 /* Prepare a target image for offload TARGET, using mkoffload tool from
655 COMPILER_PATH. Return the name of the resultant object file. */
657 static char *
658 compile_offload_image (const char *target, const char *compiler_path,
659 unsigned in_argc, char *in_argv[],
660 struct cl_decoded_option *compiler_opts,
661 unsigned int compiler_opt_count,
662 struct cl_decoded_option *linker_opts,
663 unsigned int linker_opt_count)
665 char *filename = NULL;
666 char **argv;
667 char *suffix
668 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target));
669 strcpy (suffix, "/accel/");
670 strcat (suffix, target);
671 strcat (suffix, "/mkoffload");
673 char **paths = NULL;
674 unsigned n_paths = parse_env_var (compiler_path, &paths, suffix);
676 const char *compiler = NULL;
677 for (unsigned i = 0; i < n_paths; i++)
678 if (access_check (paths[i], X_OK) == 0)
680 compiler = paths[i];
681 break;
684 if (compiler)
686 /* Generate temporary output file name. */
687 filename = make_temp_file (".target.o");
689 struct obstack argv_obstack;
690 obstack_init (&argv_obstack);
691 obstack_ptr_grow (&argv_obstack, compiler);
692 if (save_temps)
693 obstack_ptr_grow (&argv_obstack, "-save-temps");
694 if (verbose)
695 obstack_ptr_grow (&argv_obstack, "-v");
696 obstack_ptr_grow (&argv_obstack, "-o");
697 obstack_ptr_grow (&argv_obstack, filename);
699 /* Append names of input object files. */
700 for (unsigned i = 0; i < in_argc; i++)
701 obstack_ptr_grow (&argv_obstack, in_argv[i]);
703 /* Append options from offload_lto sections. */
704 append_compiler_options (&argv_obstack, compiler_opts,
705 compiler_opt_count);
706 append_diag_options (&argv_obstack, linker_opts, linker_opt_count);
708 /* Append options specified by -foffload last. In case of conflicting
709 options we expect offload compiler to choose the latest. */
710 append_offload_options (&argv_obstack, target, compiler_opts,
711 compiler_opt_count);
712 append_offload_options (&argv_obstack, target, linker_opts,
713 linker_opt_count);
715 obstack_ptr_grow (&argv_obstack, NULL);
716 argv = XOBFINISH (&argv_obstack, char **);
717 fork_execute (argv[0], argv, true);
718 obstack_free (&argv_obstack, NULL);
721 free_array_of_ptrs ((void **) paths, n_paths);
722 return filename;
726 /* The main routine dealing with offloading.
727 The routine builds a target image for each offload target. IN_ARGC and
728 IN_ARGV specify options and input object files. As all of them could contain
729 target sections, we pass them all to target compilers. */
731 static void
732 compile_images_for_offload_targets (unsigned in_argc, char *in_argv[],
733 struct cl_decoded_option *compiler_opts,
734 unsigned int compiler_opt_count,
735 struct cl_decoded_option *linker_opts,
736 unsigned int linker_opt_count)
738 char **names = NULL;
739 const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV);
740 if (!target_names)
741 return;
742 unsigned num_targets = parse_env_var (target_names, &names, NULL);
744 int next_name_entry = 0;
745 const char *compiler_path = getenv ("COMPILER_PATH");
746 if (!compiler_path)
747 goto out;
749 /* Prepare an image for each target and save the name of the resultant object
750 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
751 offload_names = XCNEWVEC (char *, num_targets + 1);
752 for (unsigned i = 0; i < num_targets; i++)
754 /* HSA does not use LTO-like streaming and a different compiler, skip
755 it. */
756 if (strcmp (names[i], "hsa") == 0)
757 continue;
759 offload_names[next_name_entry]
760 = compile_offload_image (names[i], compiler_path, in_argc, in_argv,
761 compiler_opts, compiler_opt_count,
762 linker_opts, linker_opt_count);
763 if (!offload_names[next_name_entry])
764 fatal_error (input_location,
765 "problem with building target image for %s\n", names[i]);
766 next_name_entry++;
769 out:
770 free_array_of_ptrs ((void **) names, num_targets);
773 /* Copy a file from SRC to DEST. */
775 static void
776 copy_file (const char *dest, const char *src)
778 FILE *d = fopen (dest, "wb");
779 FILE *s = fopen (src, "rb");
780 char buffer[512];
781 while (!feof (s))
783 size_t len = fread (buffer, 1, 512, s);
784 if (ferror (s) != 0)
785 fatal_error (input_location, "reading input file");
786 if (len > 0)
788 fwrite (buffer, 1, len, d);
789 if (ferror (d) != 0)
790 fatal_error (input_location, "writing output file");
793 fclose (d);
794 fclose (s);
797 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
798 the copy to the linker. */
800 static void
801 find_crtoffloadtable (void)
803 char **paths = NULL;
804 const char *library_path = getenv ("LIBRARY_PATH");
805 if (!library_path)
806 return;
807 unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o");
809 unsigned i;
810 for (i = 0; i < n_paths; i++)
811 if (access_check (paths[i], R_OK) == 0)
813 /* The linker will delete the filename we give it, so make a copy. */
814 char *crtoffloadtable = make_temp_file (".crtoffloadtable.o");
815 copy_file (crtoffloadtable, paths[i]);
816 printf ("%s\n", crtoffloadtable);
817 XDELETEVEC (crtoffloadtable);
818 break;
820 if (i == n_paths)
821 fatal_error (input_location,
822 "installation error, can't find crtoffloadtable.o");
824 free_array_of_ptrs ((void **) paths, n_paths);
827 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
828 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
829 and OPT_COUNT. Return true if we found a matchingn section, false
830 otherwise. COLLECT_GCC holds the value of the environment variable with
831 the same name. */
833 static bool
834 find_and_merge_options (int fd, off_t file_offset, const char *prefix,
835 struct cl_decoded_option **opts,
836 unsigned int *opt_count, const char *collect_gcc)
838 off_t offset, length;
839 char *data;
840 char *fopts;
841 const char *errmsg;
842 int err;
843 struct cl_decoded_option *fdecoded_options = *opts;
844 unsigned int fdecoded_options_count = *opt_count;
846 simple_object_read *sobj;
847 sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO",
848 &errmsg, &err);
849 if (!sobj)
850 return false;
852 char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts"));
853 strcpy (secname, prefix);
854 strcat (secname, ".opts");
855 if (!simple_object_find_section (sobj, secname, &offset, &length,
856 &errmsg, &err))
858 simple_object_release_read (sobj);
859 return false;
862 lseek (fd, file_offset + offset, SEEK_SET);
863 data = (char *)xmalloc (length);
864 read (fd, data, length);
865 fopts = data;
868 struct cl_decoded_option *f2decoded_options;
869 unsigned int f2decoded_options_count;
870 get_options_from_collect_gcc_options (collect_gcc,
871 fopts, CL_LANG_ALL,
872 &f2decoded_options,
873 &f2decoded_options_count);
874 if (!fdecoded_options)
876 fdecoded_options = f2decoded_options;
877 fdecoded_options_count = f2decoded_options_count;
879 else
880 merge_and_complain (&fdecoded_options,
881 &fdecoded_options_count,
882 f2decoded_options, f2decoded_options_count);
884 fopts += strlen (fopts) + 1;
886 while (fopts - data < length);
888 free (data);
889 simple_object_release_read (sobj);
890 *opts = fdecoded_options;
891 *opt_count = fdecoded_options_count;
892 return true;
895 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
897 static void
898 run_gcc (unsigned argc, char *argv[])
900 unsigned i, j;
901 const char **new_argv;
902 const char **argv_ptr;
903 char *list_option_full = NULL;
904 const char *linker_output = NULL;
905 const char *collect_gcc, *collect_gcc_options;
906 int parallel = 0;
907 int jobserver = 0;
908 bool no_partition = false;
909 struct cl_decoded_option *fdecoded_options = NULL;
910 struct cl_decoded_option *offload_fdecoded_options = NULL;
911 unsigned int fdecoded_options_count = 0;
912 unsigned int offload_fdecoded_options_count = 0;
913 struct cl_decoded_option *decoded_options;
914 unsigned int decoded_options_count;
915 struct obstack argv_obstack;
916 int new_head_argc;
917 bool have_lto = false;
918 bool have_offload = false;
919 unsigned lto_argc = 0;
920 char **lto_argv;
922 /* Get the driver and options. */
923 collect_gcc = getenv ("COLLECT_GCC");
924 if (!collect_gcc)
925 fatal_error (input_location,
926 "environment variable COLLECT_GCC must be set");
927 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
928 if (!collect_gcc_options)
929 fatal_error (input_location,
930 "environment variable COLLECT_GCC_OPTIONS must be set");
931 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
932 CL_LANG_ALL,
933 &decoded_options,
934 &decoded_options_count);
936 /* Allocate array for input object files with LTO IL,
937 and for possible preceding arguments. */
938 lto_argv = XNEWVEC (char *, argc);
940 /* Look at saved options in the IL files. */
941 for (i = 1; i < argc; ++i)
943 char *p;
944 int fd;
945 off_t file_offset = 0;
946 long loffset;
947 int consumed;
948 char *filename = argv[i];
950 if (strncmp (argv[i], "-foffload-objects=",
951 sizeof ("-foffload-objects=") - 1) == 0)
953 have_offload = true;
954 offload_objects_file_name
955 = argv[i] + sizeof ("-foffload-objects=") - 1;
956 continue;
959 if ((p = strrchr (argv[i], '@'))
960 && p != argv[i]
961 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
962 && strlen (p) == (unsigned int) consumed)
964 filename = XNEWVEC (char, p - argv[i] + 1);
965 memcpy (filename, argv[i], p - argv[i]);
966 filename[p - argv[i]] = '\0';
967 file_offset = (off_t) loffset;
969 fd = open (filename, O_RDONLY | O_BINARY);
970 if (fd == -1)
972 lto_argv[lto_argc++] = argv[i];
973 continue;
976 if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX,
977 &fdecoded_options, &fdecoded_options_count,
978 collect_gcc))
980 have_lto = true;
981 lto_argv[lto_argc++] = argv[i];
983 close (fd);
986 /* Initalize the common arguments for the driver. */
987 obstack_init (&argv_obstack);
988 obstack_ptr_grow (&argv_obstack, collect_gcc);
989 obstack_ptr_grow (&argv_obstack, "-xlto");
990 obstack_ptr_grow (&argv_obstack, "-c");
992 append_compiler_options (&argv_obstack, fdecoded_options,
993 fdecoded_options_count);
994 append_linker_options (&argv_obstack, decoded_options, decoded_options_count);
996 /* Scan linker driver arguments for things that are of relevance to us. */
997 for (j = 1; j < decoded_options_count; ++j)
999 struct cl_decoded_option *option = &decoded_options[j];
1000 switch (option->opt_index)
1002 case OPT_o:
1003 linker_output = option->arg;
1004 break;
1006 case OPT_save_temps:
1007 save_temps = 1;
1008 break;
1010 case OPT_v:
1011 verbose = 1;
1012 break;
1014 case OPT_flto_partition_:
1015 if (strcmp (option->arg, "none") == 0)
1016 no_partition = true;
1017 break;
1019 case OPT_flto_:
1020 if (strcmp (option->arg, "jobserver") == 0)
1022 jobserver = 1;
1023 parallel = 1;
1025 else
1027 parallel = atoi (option->arg);
1028 if (parallel <= 1)
1029 parallel = 0;
1031 /* Fallthru. */
1033 case OPT_flto:
1034 lto_mode = LTO_MODE_WHOPR;
1035 break;
1037 default:
1038 break;
1042 if (no_partition)
1044 lto_mode = LTO_MODE_LTO;
1045 jobserver = 0;
1046 parallel = 0;
1049 if (linker_output)
1051 char *output_dir, *base, *name;
1052 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
1054 output_dir = xstrdup (linker_output);
1055 base = output_dir;
1056 for (name = base; *name; name++)
1057 if (IS_DIR_SEPARATOR (*name))
1058 base = name + 1;
1059 *base = '\0';
1061 linker_output = &linker_output[base - output_dir];
1062 if (*output_dir == '\0')
1064 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
1065 output_dir = current_dir;
1067 if (!bit_bucket)
1069 obstack_ptr_grow (&argv_obstack, "-dumpdir");
1070 obstack_ptr_grow (&argv_obstack, output_dir);
1073 obstack_ptr_grow (&argv_obstack, "-dumpbase");
1076 /* Remember at which point we can scrub args to re-use the commons. */
1077 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
1079 if (have_offload)
1081 unsigned i, num_offload_files;
1082 char **offload_argv;
1083 FILE *f;
1085 f = fopen (offload_objects_file_name, "r");
1086 if (f == NULL)
1087 fatal_error (input_location, "cannot open %s: %m",
1088 offload_objects_file_name);
1089 if (fscanf (f, "%u ", &num_offload_files) != 1)
1090 fatal_error (input_location, "cannot read %s: %m",
1091 offload_objects_file_name);
1092 offload_argv = XCNEWVEC (char *, num_offload_files);
1094 /* Read names of object files with offload. */
1095 for (i = 0; i < num_offload_files; i++)
1097 const unsigned piece = 32;
1098 char *buf, *filename = XNEWVEC (char, piece);
1099 size_t len;
1101 buf = filename;
1102 cont1:
1103 if (!fgets (buf, piece, f))
1104 break;
1105 len = strlen (filename);
1106 if (filename[len - 1] != '\n')
1108 filename = XRESIZEVEC (char, filename, len + piece);
1109 buf = filename + len;
1110 goto cont1;
1112 filename[len - 1] = '\0';
1113 offload_argv[i] = filename;
1115 fclose (f);
1116 if (offload_argv[num_offload_files - 1] == NULL)
1117 fatal_error (input_location, "invalid format of %s",
1118 offload_objects_file_name);
1119 maybe_unlink (offload_objects_file_name);
1120 offload_objects_file_name = NULL;
1122 /* Look at saved offload options in files. */
1123 for (i = 0; i < num_offload_files; i++)
1125 char *p;
1126 long loffset;
1127 int fd, consumed;
1128 off_t file_offset = 0;
1129 char *filename = offload_argv[i];
1131 if ((p = strrchr (offload_argv[i], '@'))
1132 && p != offload_argv[i]
1133 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
1134 && strlen (p) == (unsigned int) consumed)
1136 filename = XNEWVEC (char, p - offload_argv[i] + 1);
1137 memcpy (filename, offload_argv[i], p - offload_argv[i]);
1138 filename[p - offload_argv[i]] = '\0';
1139 file_offset = (off_t) loffset;
1141 fd = open (filename, O_RDONLY | O_BINARY);
1142 if (fd == -1)
1143 fatal_error (input_location, "cannot open %s: %m", filename);
1144 if (!find_and_merge_options (fd, file_offset,
1145 OFFLOAD_SECTION_NAME_PREFIX,
1146 &offload_fdecoded_options,
1147 &offload_fdecoded_options_count,
1148 collect_gcc))
1149 fatal_error (input_location, "cannot read %s: %m", filename);
1150 close (fd);
1151 if (filename != offload_argv[i])
1152 XDELETEVEC (filename);
1155 compile_images_for_offload_targets (num_offload_files, offload_argv,
1156 offload_fdecoded_options,
1157 offload_fdecoded_options_count,
1158 decoded_options,
1159 decoded_options_count);
1161 free_array_of_ptrs ((void **) offload_argv, num_offload_files);
1163 if (offload_names)
1165 find_crtoffloadtable ();
1166 for (i = 0; offload_names[i]; i++)
1167 printf ("%s\n", offload_names[i]);
1168 free_array_of_ptrs ((void **) offload_names, i);
1172 /* If object files contain offload sections, but do not contain LTO sections,
1173 then there is no need to perform a link-time recompilation, i.e.
1174 lto-wrapper is used only for a compilation of offload images. */
1175 if (have_offload && !have_lto)
1176 goto finish;
1178 if (lto_mode == LTO_MODE_LTO)
1180 flto_out = make_temp_file (".lto.o");
1181 if (linker_output)
1182 obstack_ptr_grow (&argv_obstack, linker_output);
1183 obstack_ptr_grow (&argv_obstack, "-o");
1184 obstack_ptr_grow (&argv_obstack, flto_out);
1186 else
1188 const char *list_option = "-fltrans-output-list=";
1189 size_t list_option_len = strlen (list_option);
1190 char *tmp;
1192 if (linker_output)
1194 char *dumpbase = (char *) xmalloc (strlen (linker_output)
1195 + sizeof (".wpa") + 1);
1196 strcpy (dumpbase, linker_output);
1197 strcat (dumpbase, ".wpa");
1198 obstack_ptr_grow (&argv_obstack, dumpbase);
1201 if (linker_output && save_temps)
1203 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
1204 + sizeof (".ltrans.out") + 1);
1205 strcpy (ltrans_output_file, linker_output);
1206 strcat (ltrans_output_file, ".ltrans.out");
1208 else
1209 ltrans_output_file = make_temp_file (".ltrans.out");
1210 list_option_full = (char *) xmalloc (sizeof (char) *
1211 (strlen (ltrans_output_file) + list_option_len + 1));
1212 tmp = list_option_full;
1214 obstack_ptr_grow (&argv_obstack, tmp);
1215 strcpy (tmp, list_option);
1216 tmp += list_option_len;
1217 strcpy (tmp, ltrans_output_file);
1219 if (jobserver)
1220 obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver"));
1221 else if (parallel > 1)
1223 char buf[256];
1224 sprintf (buf, "-fwpa=%i", parallel);
1225 obstack_ptr_grow (&argv_obstack, xstrdup (buf));
1227 else
1228 obstack_ptr_grow (&argv_obstack, "-fwpa");
1231 /* Append the input objects and possible preceding arguments. */
1232 for (i = 0; i < lto_argc; ++i)
1233 obstack_ptr_grow (&argv_obstack, lto_argv[i]);
1234 obstack_ptr_grow (&argv_obstack, NULL);
1236 new_argv = XOBFINISH (&argv_obstack, const char **);
1237 argv_ptr = &new_argv[new_head_argc];
1238 fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true);
1240 if (lto_mode == LTO_MODE_LTO)
1242 printf ("%s\n", flto_out);
1243 free (flto_out);
1244 flto_out = NULL;
1246 else
1248 FILE *stream = fopen (ltrans_output_file, "r");
1249 FILE *mstream = NULL;
1250 struct obstack env_obstack;
1252 if (!stream)
1253 fatal_error (input_location, "fopen: %s: %m", ltrans_output_file);
1255 /* Parse the list of LTRANS inputs from the WPA stage. */
1256 obstack_init (&env_obstack);
1257 nr = 0;
1258 for (;;)
1260 const unsigned piece = 32;
1261 char *output_name = NULL;
1262 char *buf, *input_name = (char *)xmalloc (piece);
1263 size_t len;
1265 buf = input_name;
1266 cont:
1267 if (!fgets (buf, piece, stream))
1268 break;
1269 len = strlen (input_name);
1270 if (input_name[len - 1] != '\n')
1272 input_name = (char *)xrealloc (input_name, len + piece);
1273 buf = input_name + len;
1274 goto cont;
1276 input_name[len - 1] = '\0';
1278 if (input_name[0] == '*')
1279 output_name = &input_name[1];
1281 nr++;
1282 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
1283 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
1284 input_names[nr-1] = input_name;
1285 output_names[nr-1] = output_name;
1287 fclose (stream);
1288 maybe_unlink (ltrans_output_file);
1289 ltrans_output_file = NULL;
1291 if (parallel)
1293 makefile = make_temp_file (".mk");
1294 mstream = fopen (makefile, "w");
1297 /* Execute the LTRANS stage for each input file (or prepare a
1298 makefile to invoke this in parallel). */
1299 for (i = 0; i < nr; ++i)
1301 char *output_name;
1302 char *input_name = input_names[i];
1303 /* If it's a pass-through file do nothing. */
1304 if (output_names[i])
1305 continue;
1307 /* Replace the .o suffix with a .ltrans.o suffix and write
1308 the resulting name to the LTRANS output list. */
1309 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
1310 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
1311 output_name = XOBFINISH (&env_obstack, char *);
1313 /* Adjust the dumpbase if the linker output file was seen. */
1314 if (linker_output)
1316 char *dumpbase
1317 = (char *) xmalloc (strlen (linker_output)
1318 + sizeof (DUMPBASE_SUFFIX) + 1);
1319 snprintf (dumpbase,
1320 strlen (linker_output) + sizeof (DUMPBASE_SUFFIX),
1321 "%s.ltrans%u", linker_output, i);
1322 argv_ptr[0] = dumpbase;
1325 argv_ptr[1] = "-fltrans";
1326 argv_ptr[2] = "-o";
1327 argv_ptr[3] = output_name;
1328 argv_ptr[4] = input_name;
1329 argv_ptr[5] = NULL;
1330 if (parallel)
1332 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
1333 for (j = 1; new_argv[j] != NULL; ++j)
1334 fprintf (mstream, " '%s'", new_argv[j]);
1335 fprintf (mstream, "\n");
1336 /* If we are not preserving the ltrans input files then
1337 truncate them as soon as we have processed it. This
1338 reduces temporary disk-space usage. */
1339 if (! save_temps)
1340 fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1341 "&& mv %s.tem %s\n",
1342 input_name, input_name, input_name, input_name);
1344 else
1346 fork_execute (new_argv[0], CONST_CAST (char **, new_argv),
1347 true);
1348 maybe_unlink (input_name);
1351 output_names[i] = output_name;
1353 if (parallel)
1355 struct pex_obj *pex;
1356 char jobs[32];
1358 fprintf (mstream, "all:");
1359 for (i = 0; i < nr; ++i)
1360 fprintf (mstream, " \\\n\t%s", output_names[i]);
1361 fprintf (mstream, "\n");
1362 fclose (mstream);
1363 if (!jobserver)
1365 /* Avoid passing --jobserver-fd= and similar flags
1366 unless jobserver mode is explicitly enabled. */
1367 putenv (xstrdup ("MAKEFLAGS="));
1368 putenv (xstrdup ("MFLAGS="));
1370 new_argv[0] = getenv ("MAKE");
1371 if (!new_argv[0])
1372 new_argv[0] = "make";
1373 new_argv[1] = "-f";
1374 new_argv[2] = makefile;
1375 i = 3;
1376 if (!jobserver)
1378 snprintf (jobs, 31, "-j%d", parallel);
1379 new_argv[i++] = jobs;
1381 new_argv[i++] = "all";
1382 new_argv[i++] = NULL;
1383 pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv),
1384 NULL, NULL, PEX_SEARCH, false);
1385 do_wait (new_argv[0], pex);
1386 maybe_unlink (makefile);
1387 makefile = NULL;
1388 for (i = 0; i < nr; ++i)
1389 maybe_unlink (input_names[i]);
1391 for (i = 0; i < nr; ++i)
1393 fputs (output_names[i], stdout);
1394 putc ('\n', stdout);
1395 free (input_names[i]);
1397 nr = 0;
1398 free (output_names);
1399 free (input_names);
1400 free (list_option_full);
1401 obstack_free (&env_obstack, NULL);
1404 finish:
1405 XDELETE (lto_argv);
1406 obstack_free (&argv_obstack, NULL);
1410 /* Entry point. */
1413 main (int argc, char *argv[])
1415 const char *p;
1417 init_opts_obstack ();
1419 p = argv[0] + strlen (argv[0]);
1420 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
1421 --p;
1422 progname = p;
1424 xmalloc_set_program_name (progname);
1426 gcc_init_libintl ();
1428 diagnostic_initialize (global_dc, 0);
1430 if (atexit (lto_wrapper_cleanup) != 0)
1431 fatal_error (input_location, "atexit failed");
1433 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
1434 signal (SIGINT, fatal_signal);
1435 #ifdef SIGHUP
1436 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
1437 signal (SIGHUP, fatal_signal);
1438 #endif
1439 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
1440 signal (SIGTERM, fatal_signal);
1441 #ifdef SIGPIPE
1442 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
1443 signal (SIGPIPE, fatal_signal);
1444 #endif
1445 #ifdef SIGCHLD
1446 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1447 receive the signal. A different setting is inheritable */
1448 signal (SIGCHLD, SIG_DFL);
1449 #endif
1451 /* We may be called with all the arguments stored in some file and
1452 passed with @file. Expand them into argv before processing. */
1453 expandargv (&argc, &argv);
1455 run_gcc (argc, argv);
1457 return 0;