1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2018 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
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
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.
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
42 #include "coretypes.h"
44 #include "diagnostic.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"
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 int *ltrans_priorities
;
69 static char **input_names
;
70 static char **output_names
;
71 static char **offload_names
;
72 static char *offload_objects_file_name
;
73 static char *makefile
;
74 static char *debug_obj
;
76 const char tool_name
[] = "lto-wrapper";
78 /* Delete tempfiles. Called from utils_cleanup. */
85 if (ltrans_output_file
)
86 maybe_unlink (ltrans_output_file
);
88 maybe_unlink (flto_out
);
89 if (offload_objects_file_name
)
90 maybe_unlink (offload_objects_file_name
);
92 maybe_unlink (makefile
);
94 maybe_unlink (debug_obj
);
95 for (i
= 0; i
< nr
; ++i
)
97 maybe_unlink (input_names
[i
]);
99 maybe_unlink (output_names
[i
]);
104 lto_wrapper_cleanup (void)
106 utils_cleanup (false);
109 /* Unlink a temporary LTRANS file unless requested otherwise. */
112 maybe_unlink (const char *file
)
116 if (unlink_if_ordinary (file
)
118 fatal_error (input_location
, "deleting LTRANS file %s: %m", file
);
121 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
124 /* Template of LTRANS dumpbase suffix. */
125 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
127 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
128 environment according to LANG_MASK. */
131 get_options_from_collect_gcc_options (const char *collect_gcc
,
132 const char *collect_gcc_options
,
133 unsigned int lang_mask
,
134 struct cl_decoded_option
**decoded_options
,
135 unsigned int *decoded_options_count
)
137 struct obstack argv_obstack
;
142 argv_storage
= xstrdup (collect_gcc_options
);
143 obstack_init (&argv_obstack
);
144 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
146 for (j
= 0, k
= 0; argv_storage
[j
] != '\0'; ++j
)
148 if (argv_storage
[j
] == '\'')
150 obstack_ptr_grow (&argv_obstack
, &argv_storage
[k
]);
154 if (argv_storage
[j
] == '\0')
155 fatal_error (input_location
, "malformed COLLECT_GCC_OPTIONS");
156 else if (strncmp (&argv_storage
[j
], "'\\''", 4) == 0)
158 argv_storage
[k
++] = '\'';
161 else if (argv_storage
[j
] == '\'')
164 argv_storage
[k
++] = argv_storage
[j
++];
167 argv_storage
[k
++] = '\0';
171 obstack_ptr_grow (&argv_obstack
, NULL
);
172 argc
= obstack_object_size (&argv_obstack
) / sizeof (void *) - 1;
173 argv
= XOBFINISH (&argv_obstack
, const char **);
175 decode_cmdline_options_to_array (argc
, (const char **)argv
,
177 decoded_options
, decoded_options_count
);
178 obstack_free (&argv_obstack
, NULL
);
181 /* Append OPTION to the options array DECODED_OPTIONS with size
182 DECODED_OPTIONS_COUNT. */
185 append_option (struct cl_decoded_option
**decoded_options
,
186 unsigned int *decoded_options_count
,
187 struct cl_decoded_option
*option
)
189 ++*decoded_options_count
;
191 = (struct cl_decoded_option
*)
192 xrealloc (*decoded_options
,
193 (*decoded_options_count
194 * sizeof (struct cl_decoded_option
)));
195 memcpy (&(*decoded_options
)[*decoded_options_count
- 1], option
,
196 sizeof (struct cl_decoded_option
));
199 /* Remove option number INDEX from DECODED_OPTIONS, update
200 DECODED_OPTIONS_COUNT. */
203 remove_option (struct cl_decoded_option
**decoded_options
,
204 int index
, unsigned int *decoded_options_count
)
206 --*decoded_options_count
;
207 memmove (&(*decoded_options
)[index
+ 1],
208 &(*decoded_options
)[index
],
209 sizeof (struct cl_decoded_option
)
210 * (*decoded_options_count
- index
));
213 /* Try to merge and complain about options FDECODED_OPTIONS when applied
214 ontop of DECODED_OPTIONS. */
217 merge_and_complain (struct cl_decoded_option
**decoded_options
,
218 unsigned int *decoded_options_count
,
219 struct cl_decoded_option
*fdecoded_options
,
220 unsigned int fdecoded_options_count
)
223 struct cl_decoded_option
*pic_option
= NULL
;
224 struct cl_decoded_option
*pie_option
= NULL
;
226 /* ??? Merge options from files. Most cases can be
227 handled by either unioning or intersecting
228 (for example -fwrapv is a case for unioning,
229 -ffast-math is for intersection). Most complaints
230 about real conflicts between different options can
231 be deferred to the compiler proper. Options that
232 we can neither safely handle by intersection nor
233 unioning would need to be complained about here.
234 Ideally we'd have a flag in the opt files that
235 tells whether to union or intersect or reject.
236 In absence of that it's unclear what a good default is.
237 It's also difficult to get positional handling correct. */
239 /* The following does what the old LTO option code did,
240 union all target and a selected set of common options. */
241 for (i
= 0; i
< fdecoded_options_count
; ++i
)
243 struct cl_decoded_option
*foption
= &fdecoded_options
[i
];
244 switch (foption
->opt_index
)
246 case OPT_SPECIAL_unknown
:
247 case OPT_SPECIAL_ignore
:
248 case OPT_SPECIAL_deprecated
:
249 case OPT_SPECIAL_program_name
:
250 case OPT_SPECIAL_input_file
:
254 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
258 case OPT_fdiagnostics_show_caret
:
259 case OPT_fdiagnostics_show_labels
:
260 case OPT_fdiagnostics_show_line_numbers
:
261 case OPT_fdiagnostics_show_option
:
262 case OPT_fdiagnostics_show_location_
:
263 case OPT_fshow_column
:
266 /* Do what the old LTO code did - collect exactly one option
267 setting per OPT code, we pick the first we encounter.
268 ??? This doesn't make too much sense, but when it doesn't
269 then we should complain. */
270 for (j
= 0; j
< *decoded_options_count
; ++j
)
271 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
273 if (j
== *decoded_options_count
)
274 append_option (decoded_options
, decoded_options_count
, foption
);
277 /* Figure out what PIC/PIE level wins and merge the results. */
280 pic_option
= foption
;
284 pie_option
= foption
;
289 /* For selected options we can merge conservatively. */
290 for (j
= 0; j
< *decoded_options_count
; ++j
)
291 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
293 if (j
== *decoded_options_count
)
294 append_option (decoded_options
, decoded_options_count
, foption
);
295 /* -fopenmp > -fno-openmp,
296 -fopenacc > -fno-openacc */
297 else if (foption
->value
> (*decoded_options
)[j
].value
)
298 (*decoded_options
)[j
] = *foption
;
301 case OPT_fopenacc_dim_
:
302 /* Append or check identical. */
303 for (j
= 0; j
< *decoded_options_count
; ++j
)
304 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
306 if (j
== *decoded_options_count
)
307 append_option (decoded_options
, decoded_options_count
, foption
);
308 else if (strcmp ((*decoded_options
)[j
].arg
, foption
->arg
))
309 fatal_error (input_location
,
310 "Option %s with different values",
311 foption
->orig_option_with_args_text
);
318 for (j
= 0; j
< *decoded_options_count
; ++j
)
319 if ((*decoded_options
)[j
].opt_index
== OPT_O
320 || (*decoded_options
)[j
].opt_index
== OPT_Ofast
321 || (*decoded_options
)[j
].opt_index
== OPT_Og
322 || (*decoded_options
)[j
].opt_index
== OPT_Os
)
324 if (j
== *decoded_options_count
)
325 append_option (decoded_options
, decoded_options_count
, foption
);
326 else if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
327 && foption
->opt_index
!= OPT_O
)
328 /* Exact same options get merged. */
332 /* For mismatched option kinds preserve the optimization
333 level only, thus merge it as -On. This also handles
334 merging of same optimization level -On. */
336 switch (foption
->opt_index
)
339 if (foption
->arg
[0] == '\0')
340 level
= MAX (level
, 1);
342 level
= MAX (level
, atoi (foption
->arg
));
345 level
= MAX (level
, 3);
348 level
= MAX (level
, 1);
351 level
= MAX (level
, 2);
356 switch ((*decoded_options
)[j
].opt_index
)
359 if ((*decoded_options
)[j
].arg
[0] == '\0')
360 level
= MAX (level
, 1);
362 level
= MAX (level
, atoi ((*decoded_options
)[j
].arg
));
365 level
= MAX (level
, 3);
368 level
= MAX (level
, 1);
371 level
= MAX (level
, 2);
376 (*decoded_options
)[j
].opt_index
= OPT_O
;
378 tem
= xasprintf ("-O%d", level
);
379 (*decoded_options
)[j
].arg
= &tem
[2];
380 (*decoded_options
)[j
].canonical_option
[0] = tem
;
381 (*decoded_options
)[j
].value
= 1;
386 case OPT_foffload_abi_
:
387 for (j
= 0; j
< *decoded_options_count
; ++j
)
388 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
390 if (j
== *decoded_options_count
)
391 append_option (decoded_options
, decoded_options_count
, foption
);
392 else if (foption
->value
!= (*decoded_options
)[j
].value
)
393 fatal_error (input_location
,
394 "Option %s not used consistently in all LTO input"
395 " files", foption
->orig_option_with_args_text
);
400 append_option (decoded_options
, decoded_options_count
, foption
);
405 /* Merge PIC options:
406 -fPIC + -fpic = -fpic
407 -fPIC + -fno-pic = -fno-pic
408 -fpic/-fPIC + nothin = nothing.
409 It is a common mistake to mix few -fPIC compiled objects into otherwise
410 non-PIC code. We do not want to build everything with PIC then.
412 Similarly we merge PIE options, however in addition we keep
413 -fPIC + -fPIE = -fPIE
414 -fpic + -fPIE = -fpie
415 -fPIC/-fpic + -fpie = -fpie
417 It would be good to warn on mismatches, but it is bit hard to do as
418 we do not know what nothing translates to. */
420 for (unsigned int j
= 0; j
< *decoded_options_count
;)
421 if ((*decoded_options
)[j
].opt_index
== OPT_fPIC
422 || (*decoded_options
)[j
].opt_index
== OPT_fpic
)
424 /* -fno-pic in one unit implies -fno-pic everywhere. */
425 if ((*decoded_options
)[j
].value
== 0)
427 /* If we have no pic option or merge in -fno-pic, we still may turn
428 existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */
429 else if ((pic_option
&& pic_option
->value
== 0)
434 bool big
= (*decoded_options
)[j
].opt_index
== OPT_fPIC
435 && pie_option
->opt_index
== OPT_fPIE
;
436 (*decoded_options
)[j
].opt_index
= big
? OPT_fPIE
: OPT_fpie
;
437 if (pie_option
->value
)
438 (*decoded_options
)[j
].canonical_option
[0] = big
? "-fPIE" : "-fpie";
440 (*decoded_options
)[j
].canonical_option
[0] = big
? "-fno-pie" : "-fno-pie";
441 (*decoded_options
)[j
].value
= pie_option
->value
;
446 (*decoded_options
)[j
] = *pic_option
;
449 /* We do not know if target defaults to pic or not, so just remove
450 option if it is missing in one unit but enabled in other. */
452 remove_option (decoded_options
, j
, decoded_options_count
);
454 else if (pic_option
->opt_index
== OPT_fpic
455 && (*decoded_options
)[j
].opt_index
== OPT_fPIC
)
457 (*decoded_options
)[j
] = *pic_option
;
463 else if ((*decoded_options
)[j
].opt_index
== OPT_fPIE
464 || (*decoded_options
)[j
].opt_index
== OPT_fpie
)
466 /* -fno-pie in one unit implies -fno-pie everywhere. */
467 if ((*decoded_options
)[j
].value
== 0)
469 /* If we have no pie option or merge in -fno-pie, we still preserve
470 PIE/pie if pic/PIC is present. */
471 else if ((pie_option
&& pie_option
->value
== 0)
474 /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */
477 if (pic_option
->opt_index
== OPT_fpic
478 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
480 (*decoded_options
)[j
].opt_index
= OPT_fpie
;
481 (*decoded_options
)[j
].canonical_option
[0]
482 = pic_option
->value
? "-fpie" : "-fno-pie";
484 else if (!pic_option
->value
)
485 (*decoded_options
)[j
].canonical_option
[0] = "-fno-pie";
486 (*decoded_options
)[j
].value
= pic_option
->value
;
491 (*decoded_options
)[j
] = *pie_option
;
494 /* Because we always append pic/PIE options this code path should
495 not happen unless the LTO object was built by old lto1 which
496 did not contain that logic yet. */
498 remove_option (decoded_options
, j
, decoded_options_count
);
500 else if (pie_option
->opt_index
== OPT_fpie
501 && (*decoded_options
)[j
].opt_index
== OPT_fPIE
)
503 (*decoded_options
)[j
] = *pie_option
;
513 /* Auxiliary function that frees elements of PTR and PTR itself.
514 N is number of elements to be freed. If PTR is NULL, nothing is freed.
515 If an element is NULL, subsequent elements are not freed. */
518 free_array_of_ptrs (void **ptr
, unsigned n
)
522 for (unsigned i
= 0; i
< n
; i
++)
532 /* Parse STR, saving found tokens into PVALUES and return their number.
533 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
534 append it to every token we find. */
537 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
539 const char *curval
, *nextval
;
543 curval
= strchr (str
, ':');
547 curval
= strchr (curval
+ 1, ':');
550 values
= (char**) xmalloc (num
* sizeof (char*));
552 nextval
= strchr (curval
, ':');
554 nextval
= strchr (curval
, '\0');
556 int append_len
= append
? strlen (append
) : 0;
557 for (i
= 0; i
< num
; i
++)
559 int l
= nextval
- curval
;
560 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
561 memcpy (values
[i
], curval
, l
);
564 strcat (values
[i
], append
);
565 curval
= nextval
+ 1;
566 nextval
= strchr (curval
, ':');
568 nextval
= strchr (curval
, '\0');
574 /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */
577 append_compiler_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
580 /* Append compiler driver arguments as far as they were merged. */
581 for (unsigned int j
= 1; j
< count
; ++j
)
583 struct cl_decoded_option
*option
= &opts
[j
];
585 /* File options have been properly filtered by lto-opts.c. */
586 switch (option
->opt_index
)
588 /* Drop arguments that we want to take from the link line. */
591 case OPT_flto_partition_
:
598 /* For now do what the original LTO option code was doing - pass
599 on any CL_TARGET flag and a few selected others. */
600 switch (option
->opt_index
)
602 case OPT_fdiagnostics_show_caret
:
603 case OPT_fdiagnostics_show_labels
:
604 case OPT_fdiagnostics_show_line_numbers
:
605 case OPT_fdiagnostics_show_option
:
606 case OPT_fdiagnostics_show_location_
:
607 case OPT_fshow_column
:
616 case OPT_fopenacc_dim_
:
617 case OPT_foffload_abi_
:
625 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
629 /* Pass the option on. */
630 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
631 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
635 /* Append diag options in OPTS with length COUNT to ARGV_OBSTACK. */
638 append_diag_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
641 /* Append compiler driver arguments as far as they were merged. */
642 for (unsigned int j
= 1; j
< count
; ++j
)
644 struct cl_decoded_option
*option
= &opts
[j
];
646 switch (option
->opt_index
)
648 case OPT_fdiagnostics_color_
:
649 case OPT_fdiagnostics_format_
:
650 case OPT_fdiagnostics_show_caret
:
651 case OPT_fdiagnostics_show_labels
:
652 case OPT_fdiagnostics_show_line_numbers
:
653 case OPT_fdiagnostics_show_option
:
654 case OPT_fdiagnostics_show_location_
:
655 case OPT_fshow_column
:
661 /* Pass the option on. */
662 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
663 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
668 /* Append linker options OPTS to ARGV_OBSTACK. */
671 append_linker_options (obstack
*argv_obstack
, struct cl_decoded_option
*opts
,
674 /* Append linker driver arguments. Compiler options from the linker
675 driver arguments will override / merge with those from the compiler. */
676 for (unsigned int j
= 1; j
< count
; ++j
)
678 struct cl_decoded_option
*option
= &opts
[j
];
680 /* Do not pass on frontend specific flags not suitable for lto. */
681 if (!(cl_options
[option
->opt_index
].flags
682 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
685 switch (option
->opt_index
)
690 /* We've handled these LTO options, do not pass them on. */
695 /* Ignore -fno-XXX form of these options, as otherwise
696 corresponding builtins will not be enabled. */
697 if (option
->value
== 0)
705 /* Pass the option on. */
706 for (unsigned int i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
707 obstack_ptr_grow (argv_obstack
, option
->canonical_option
[i
]);
711 /* Extract options for TARGET offload compiler from OPTIONS and append
712 them to ARGV_OBSTACK. */
715 append_offload_options (obstack
*argv_obstack
, const char *target
,
716 struct cl_decoded_option
*options
,
717 unsigned int options_count
)
719 for (unsigned i
= 0; i
< options_count
; i
++)
721 const char *cur
, *next
, *opts
;
724 struct cl_decoded_option
*option
= &options
[i
];
726 if (option
->opt_index
!= OPT_foffload_
)
729 /* If option argument starts with '-' then no target is specified. That
730 means offload options are specified for all targets, so we need to
732 if (option
->arg
[0] == '-')
736 opts
= strchr (option
->arg
, '=');
737 /* If there are offload targets specified, but no actual options,
738 there is nothing to do here. */
746 next
= strchr (cur
, ',');
749 next
= (next
> opts
) ? opts
: next
;
751 /* Are we looking for this offload target? */
752 if (strlen (target
) == (size_t) (next
- cur
)
753 && strncmp (target
, cur
, next
- cur
) == 0)
756 /* Skip the comma or equal sign. */
766 argv
= buildargv (opts
);
767 for (argc
= 0; argv
[argc
]; argc
++)
768 obstack_ptr_grow (argv_obstack
, argv
[argc
]);
772 /* Check whether NAME can be accessed in MODE. This is like access,
773 except that it never considers directories to be executable. */
776 access_check (const char *name
, int mode
)
782 if (stat (name
, &st
) < 0
783 || S_ISDIR (st
.st_mode
))
787 return access (name
, mode
);
790 /* Prepare a target image for offload TARGET, using mkoffload tool from
791 COMPILER_PATH. Return the name of the resultant object file. */
794 compile_offload_image (const char *target
, const char *compiler_path
,
795 unsigned in_argc
, char *in_argv
[],
796 struct cl_decoded_option
*compiler_opts
,
797 unsigned int compiler_opt_count
,
798 struct cl_decoded_option
*linker_opts
,
799 unsigned int linker_opt_count
)
801 char *filename
= NULL
;
804 = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target
));
805 strcpy (suffix
, "/accel/");
806 strcat (suffix
, target
);
807 strcat (suffix
, "/mkoffload");
810 unsigned n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
812 const char *compiler
= NULL
;
813 for (unsigned i
= 0; i
< n_paths
; i
++)
814 if (access_check (paths
[i
], X_OK
) == 0)
821 fatal_error (input_location
,
822 "could not find %s in %s (consider using '-B')\n", suffix
+ 1,
825 /* Generate temporary output file name. */
826 filename
= make_temp_file (".target.o");
828 struct obstack argv_obstack
;
829 obstack_init (&argv_obstack
);
830 obstack_ptr_grow (&argv_obstack
, compiler
);
832 obstack_ptr_grow (&argv_obstack
, "-save-temps");
834 obstack_ptr_grow (&argv_obstack
, "-v");
835 obstack_ptr_grow (&argv_obstack
, "-o");
836 obstack_ptr_grow (&argv_obstack
, filename
);
838 /* Append names of input object files. */
839 for (unsigned i
= 0; i
< in_argc
; i
++)
840 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
842 /* Append options from offload_lto sections. */
843 append_compiler_options (&argv_obstack
, compiler_opts
,
845 append_diag_options (&argv_obstack
, linker_opts
, linker_opt_count
);
847 /* Append options specified by -foffload last. In case of conflicting
848 options we expect offload compiler to choose the latest. */
849 append_offload_options (&argv_obstack
, target
, compiler_opts
,
851 append_offload_options (&argv_obstack
, target
, linker_opts
,
854 obstack_ptr_grow (&argv_obstack
, NULL
);
855 argv
= XOBFINISH (&argv_obstack
, char **);
856 fork_execute (argv
[0], argv
, true);
857 obstack_free (&argv_obstack
, NULL
);
859 free_array_of_ptrs ((void **) paths
, n_paths
);
864 /* The main routine dealing with offloading.
865 The routine builds a target image for each offload target. IN_ARGC and
866 IN_ARGV specify options and input object files. As all of them could contain
867 target sections, we pass them all to target compilers. */
870 compile_images_for_offload_targets (unsigned in_argc
, char *in_argv
[],
871 struct cl_decoded_option
*compiler_opts
,
872 unsigned int compiler_opt_count
,
873 struct cl_decoded_option
*linker_opts
,
874 unsigned int linker_opt_count
)
877 const char *target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
880 unsigned num_targets
= parse_env_var (target_names
, &names
, NULL
);
882 int next_name_entry
= 0;
883 const char *compiler_path
= getenv ("COMPILER_PATH");
887 /* Prepare an image for each target and save the name of the resultant object
888 file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */
889 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
890 for (unsigned i
= 0; i
< num_targets
; i
++)
892 /* HSA does not use LTO-like streaming and a different compiler, skip
894 if (strcmp (names
[i
], "hsa") == 0)
897 offload_names
[next_name_entry
]
898 = compile_offload_image (names
[i
], compiler_path
, in_argc
, in_argv
,
899 compiler_opts
, compiler_opt_count
,
900 linker_opts
, linker_opt_count
);
901 if (!offload_names
[next_name_entry
])
902 fatal_error (input_location
,
903 "problem with building target image for %s\n", names
[i
]);
908 free_array_of_ptrs ((void **) names
, num_targets
);
911 /* Copy a file from SRC to DEST. */
914 copy_file (const char *dest
, const char *src
)
916 FILE *d
= fopen (dest
, "wb");
917 FILE *s
= fopen (src
, "rb");
921 size_t len
= fread (buffer
, 1, 512, s
);
923 fatal_error (input_location
, "reading input file");
926 fwrite (buffer
, 1, len
, d
);
928 fatal_error (input_location
, "writing output file");
935 /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of
936 the copy to the linker. */
939 find_crtoffloadtable (void)
942 const char *library_path
= getenv ("LIBRARY_PATH");
945 unsigned n_paths
= parse_env_var (library_path
, &paths
, "/crtoffloadtable.o");
948 for (i
= 0; i
< n_paths
; i
++)
949 if (access_check (paths
[i
], R_OK
) == 0)
951 /* The linker will delete the filename we give it, so make a copy. */
952 char *crtoffloadtable
= make_temp_file (".crtoffloadtable.o");
953 copy_file (crtoffloadtable
, paths
[i
]);
954 printf ("%s\n", crtoffloadtable
);
955 XDELETEVEC (crtoffloadtable
);
959 fatal_error (input_location
,
960 "installation error, can't find crtoffloadtable.o");
962 free_array_of_ptrs ((void **) paths
, n_paths
);
965 /* A subroutine of run_gcc. Examine the open file FD for lto sections with
966 name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS
967 and OPT_COUNT. Return true if we found a matchingn section, false
968 otherwise. COLLECT_GCC holds the value of the environment variable with
972 find_and_merge_options (int fd
, off_t file_offset
, const char *prefix
,
973 struct cl_decoded_option
**opts
,
974 unsigned int *opt_count
, const char *collect_gcc
)
976 off_t offset
, length
;
981 struct cl_decoded_option
*fdecoded_options
= *opts
;
982 unsigned int fdecoded_options_count
= *opt_count
;
984 simple_object_read
*sobj
;
985 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
990 char *secname
= XALLOCAVEC (char, strlen (prefix
) + sizeof (".opts"));
991 strcpy (secname
, prefix
);
992 strcat (secname
, ".opts");
993 if (!simple_object_find_section (sobj
, secname
, &offset
, &length
,
996 simple_object_release_read (sobj
);
1000 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
1001 data
= (char *)xmalloc (length
);
1002 read (fd
, data
, length
);
1006 struct cl_decoded_option
*f2decoded_options
;
1007 unsigned int f2decoded_options_count
;
1008 get_options_from_collect_gcc_options (collect_gcc
,
1011 &f2decoded_options_count
);
1012 if (!fdecoded_options
)
1014 fdecoded_options
= f2decoded_options
;
1015 fdecoded_options_count
= f2decoded_options_count
;
1018 merge_and_complain (&fdecoded_options
,
1019 &fdecoded_options_count
,
1020 f2decoded_options
, f2decoded_options_count
);
1022 fopts
+= strlen (fopts
) + 1;
1024 while (fopts
- data
< length
);
1027 simple_object_release_read (sobj
);
1028 *opts
= fdecoded_options
;
1029 *opt_count
= fdecoded_options_count
;
1033 /* Copy early debug info sections from INFILE to a new file whose name
1034 is returned. Return NULL on error. */
1037 debug_objcopy (const char *infile
, bool rename
)
1039 const char *outfile
;
1047 if ((p
= strrchr (infile
, '@'))
1049 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1050 && strlen (p
) == (unsigned int) consumed
)
1052 char *fname
= xstrdup (infile
);
1053 fname
[p
- infile
] = '\0';
1055 inoff
= (off_t
) loffset
;
1057 int infd
= open (infile
, O_RDONLY
| O_BINARY
);
1060 simple_object_read
*inobj
= simple_object_start_read (infd
, inoff
,
1067 if (simple_object_find_section (inobj
, ".gnu.debuglto_.debug_info",
1068 &off
, &len
, &errmsg
, &err
) != 1)
1071 fatal_error (0, "%s: %s\n", errmsg
, xstrerror (err
));
1073 simple_object_release_read (inobj
);
1078 outfile
= make_temp_file ("debugobjtem");
1079 errmsg
= simple_object_copy_lto_debug_sections (inobj
, outfile
, &err
, rename
);
1082 unlink_if_ordinary (outfile
);
1083 fatal_error (0, "%s: %s\n", errmsg
, xstrerror (err
));
1086 simple_object_release_read (inobj
);
1092 /* Helper for qsort: compare priorities for parallel compilation. */
1095 cmp_priority (const void *a
, const void *b
)
1097 return *((const int *)b
)-*((const int *)a
);
1101 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
1104 run_gcc (unsigned argc
, char *argv
[])
1107 const char **new_argv
;
1108 const char **argv_ptr
;
1109 char *list_option_full
= NULL
;
1110 const char *linker_output
= NULL
;
1111 const char *collect_gcc
, *collect_gcc_options
;
1114 bool no_partition
= false;
1115 struct cl_decoded_option
*fdecoded_options
= NULL
;
1116 struct cl_decoded_option
*offload_fdecoded_options
= NULL
;
1117 unsigned int fdecoded_options_count
= 0;
1118 unsigned int offload_fdecoded_options_count
= 0;
1119 struct cl_decoded_option
*decoded_options
;
1120 unsigned int decoded_options_count
;
1121 struct obstack argv_obstack
;
1123 bool have_lto
= false;
1124 bool have_offload
= false;
1125 unsigned lto_argc
= 0, ltoobj_argc
= 0;
1126 char **lto_argv
, **ltoobj_argv
;
1127 bool linker_output_rel
= false;
1128 bool skip_debug
= false;
1129 unsigned n_debugobj
;
1131 /* Get the driver and options. */
1132 collect_gcc
= getenv ("COLLECT_GCC");
1134 fatal_error (input_location
,
1135 "environment variable COLLECT_GCC must be set");
1136 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
1137 if (!collect_gcc_options
)
1138 fatal_error (input_location
,
1139 "environment variable COLLECT_GCC_OPTIONS must be set");
1140 get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
,
1143 &decoded_options_count
);
1145 /* Allocate array for input object files with LTO IL,
1146 and for possible preceding arguments. */
1147 lto_argv
= XNEWVEC (char *, argc
);
1148 ltoobj_argv
= XNEWVEC (char *, argc
);
1150 /* Look at saved options in the IL files. */
1151 for (i
= 1; i
< argc
; ++i
)
1155 off_t file_offset
= 0;
1158 char *filename
= argv
[i
];
1160 if (strncmp (argv
[i
], "-foffload-objects=",
1161 sizeof ("-foffload-objects=") - 1) == 0)
1163 have_offload
= true;
1164 offload_objects_file_name
1165 = argv
[i
] + sizeof ("-foffload-objects=") - 1;
1169 if ((p
= strrchr (argv
[i
], '@'))
1171 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1172 && strlen (p
) == (unsigned int) consumed
)
1174 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
1175 memcpy (filename
, argv
[i
], p
- argv
[i
]);
1176 filename
[p
- argv
[i
]] = '\0';
1177 file_offset
= (off_t
) loffset
;
1179 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1180 /* Linker plugin passes -fresolution and -flinker-output options.
1181 -flinker-output is passed only when user did not specify one and thus
1182 we do not need to worry about duplicities with the option handling
1186 lto_argv
[lto_argc
++] = argv
[i
];
1187 if (strcmp (argv
[i
], "-flinker-output=rel") == 0)
1188 linker_output_rel
= true;
1192 if (find_and_merge_options (fd
, file_offset
, LTO_SECTION_NAME_PREFIX
,
1193 &fdecoded_options
, &fdecoded_options_count
,
1197 ltoobj_argv
[ltoobj_argc
++] = argv
[i
];
1202 /* Initalize the common arguments for the driver. */
1203 obstack_init (&argv_obstack
);
1204 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
1205 obstack_ptr_grow (&argv_obstack
, "-xlto");
1206 obstack_ptr_grow (&argv_obstack
, "-c");
1208 append_compiler_options (&argv_obstack
, fdecoded_options
,
1209 fdecoded_options_count
);
1210 append_linker_options (&argv_obstack
, decoded_options
, decoded_options_count
);
1212 /* Scan linker driver arguments for things that are of relevance to us. */
1213 for (j
= 1; j
< decoded_options_count
; ++j
)
1215 struct cl_decoded_option
*option
= &decoded_options
[j
];
1216 switch (option
->opt_index
)
1219 linker_output
= option
->arg
;
1222 case OPT_save_temps
:
1230 case OPT_flto_partition_
:
1231 if (strcmp (option
->arg
, "none") == 0)
1232 no_partition
= true;
1236 if (strcmp (option
->arg
, "jobserver") == 0)
1243 parallel
= atoi (option
->arg
);
1250 lto_mode
= LTO_MODE_WHOPR
;
1253 case OPT_flinker_output_
:
1254 linker_output_rel
= !strcmp (option
->arg
, "rel");
1263 /* Output lto-wrapper invocation command. */
1266 for (i
= 0; i
< argc
; ++i
)
1268 fputs (argv
[i
], stderr
);
1269 fputc (' ', stderr
);
1271 fputc ('\n', stderr
);
1274 if (linker_output_rel
)
1275 no_partition
= true;
1279 lto_mode
= LTO_MODE_LTO
;
1286 char *output_dir
, *base
, *name
;
1287 bool bit_bucket
= strcmp (linker_output
, HOST_BIT_BUCKET
) == 0;
1289 output_dir
= xstrdup (linker_output
);
1291 for (name
= base
; *name
; name
++)
1292 if (IS_DIR_SEPARATOR (*name
))
1296 linker_output
= &linker_output
[base
- output_dir
];
1297 if (*output_dir
== '\0')
1299 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
1300 output_dir
= current_dir
;
1304 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
1305 obstack_ptr_grow (&argv_obstack
, output_dir
);
1308 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
1311 /* Remember at which point we can scrub args to re-use the commons. */
1312 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
1316 unsigned i
, num_offload_files
;
1317 char **offload_argv
;
1320 f
= fopen (offload_objects_file_name
, "r");
1322 fatal_error (input_location
, "cannot open %s: %m",
1323 offload_objects_file_name
);
1324 if (fscanf (f
, "%u ", &num_offload_files
) != 1)
1325 fatal_error (input_location
, "cannot read %s: %m",
1326 offload_objects_file_name
);
1327 offload_argv
= XCNEWVEC (char *, num_offload_files
);
1329 /* Read names of object files with offload. */
1330 for (i
= 0; i
< num_offload_files
; i
++)
1332 const unsigned piece
= 32;
1333 char *buf
, *filename
= XNEWVEC (char, piece
);
1338 if (!fgets (buf
, piece
, f
))
1340 len
= strlen (filename
);
1341 if (filename
[len
- 1] != '\n')
1343 filename
= XRESIZEVEC (char, filename
, len
+ piece
);
1344 buf
= filename
+ len
;
1347 filename
[len
- 1] = '\0';
1348 offload_argv
[i
] = filename
;
1351 if (offload_argv
[num_offload_files
- 1] == NULL
)
1352 fatal_error (input_location
, "invalid format of %s",
1353 offload_objects_file_name
);
1354 maybe_unlink (offload_objects_file_name
);
1355 offload_objects_file_name
= NULL
;
1357 /* Look at saved offload options in files. */
1358 for (i
= 0; i
< num_offload_files
; i
++)
1363 off_t file_offset
= 0;
1364 char *filename
= offload_argv
[i
];
1366 if ((p
= strrchr (offload_argv
[i
], '@'))
1367 && p
!= offload_argv
[i
]
1368 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
1369 && strlen (p
) == (unsigned int) consumed
)
1371 filename
= XNEWVEC (char, p
- offload_argv
[i
] + 1);
1372 memcpy (filename
, offload_argv
[i
], p
- offload_argv
[i
]);
1373 filename
[p
- offload_argv
[i
]] = '\0';
1374 file_offset
= (off_t
) loffset
;
1376 fd
= open (filename
, O_RDONLY
| O_BINARY
);
1378 fatal_error (input_location
, "cannot open %s: %m", filename
);
1379 if (!find_and_merge_options (fd
, file_offset
,
1380 OFFLOAD_SECTION_NAME_PREFIX
,
1381 &offload_fdecoded_options
,
1382 &offload_fdecoded_options_count
,
1384 fatal_error (input_location
, "cannot read %s: %m", filename
);
1386 if (filename
!= offload_argv
[i
])
1387 XDELETEVEC (filename
);
1390 compile_images_for_offload_targets (num_offload_files
, offload_argv
,
1391 offload_fdecoded_options
,
1392 offload_fdecoded_options_count
,
1394 decoded_options_count
);
1396 free_array_of_ptrs ((void **) offload_argv
, num_offload_files
);
1400 find_crtoffloadtable ();
1401 for (i
= 0; offload_names
[i
]; i
++)
1402 printf ("%s\n", offload_names
[i
]);
1403 free_array_of_ptrs ((void **) offload_names
, i
);
1407 /* If object files contain offload sections, but do not contain LTO sections,
1408 then there is no need to perform a link-time recompilation, i.e.
1409 lto-wrapper is used only for a compilation of offload images. */
1410 if (have_offload
&& !have_lto
)
1413 if (lto_mode
== LTO_MODE_LTO
)
1415 flto_out
= make_temp_file (".lto.o");
1417 obstack_ptr_grow (&argv_obstack
, linker_output
);
1418 obstack_ptr_grow (&argv_obstack
, "-o");
1419 obstack_ptr_grow (&argv_obstack
, flto_out
);
1423 const char *list_option
= "-fltrans-output-list=";
1424 size_t list_option_len
= strlen (list_option
);
1429 char *dumpbase
= (char *) xmalloc (strlen (linker_output
)
1430 + sizeof (".wpa") + 1);
1431 strcpy (dumpbase
, linker_output
);
1432 strcat (dumpbase
, ".wpa");
1433 obstack_ptr_grow (&argv_obstack
, dumpbase
);
1436 if (linker_output
&& save_temps
)
1438 ltrans_output_file
= (char *) xmalloc (strlen (linker_output
)
1439 + sizeof (".ltrans.out") + 1);
1440 strcpy (ltrans_output_file
, linker_output
);
1441 strcat (ltrans_output_file
, ".ltrans.out");
1445 char *prefix
= NULL
;
1448 prefix
= (char *) xmalloc (strlen (linker_output
) + 2);
1449 strcpy (prefix
, linker_output
);
1450 strcat (prefix
, ".");
1453 ltrans_output_file
= make_temp_file_with_prefix (prefix
,
1457 list_option_full
= (char *) xmalloc (sizeof (char) *
1458 (strlen (ltrans_output_file
) + list_option_len
+ 1));
1459 tmp
= list_option_full
;
1461 obstack_ptr_grow (&argv_obstack
, tmp
);
1462 strcpy (tmp
, list_option
);
1463 tmp
+= list_option_len
;
1464 strcpy (tmp
, ltrans_output_file
);
1467 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
1468 else if (parallel
> 1)
1471 sprintf (buf
, "-fwpa=%i", parallel
);
1472 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
1475 obstack_ptr_grow (&argv_obstack
, "-fwpa");
1478 /* Append input arguments. */
1479 for (i
= 0; i
< lto_argc
; ++i
)
1480 obstack_ptr_grow (&argv_obstack
, lto_argv
[i
]);
1481 /* Append the input objects. */
1482 for (i
= 0; i
< ltoobj_argc
; ++i
)
1483 obstack_ptr_grow (&argv_obstack
, ltoobj_argv
[i
]);
1484 obstack_ptr_grow (&argv_obstack
, NULL
);
1486 new_argv
= XOBFINISH (&argv_obstack
, const char **);
1487 argv_ptr
= &new_argv
[new_head_argc
];
1488 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true);
1490 /* Handle early generated debug information. At compile-time
1491 we output early DWARF debug info into .gnu.debuglto_ prefixed
1492 sections. LTRANS object DWARF debug info refers to that.
1493 So we need to transfer the .gnu.debuglto_ sections to the final
1494 link. Ideally the linker plugin interface would allow us to
1495 not claim those sections and instruct the linker to keep
1496 them, renaming them in the process. For now we extract and
1497 rename those sections via a simple-object interface to produce
1498 regular objects containing only the early debug info. We
1499 then partially link those to a single early debug info object
1500 and pass that as additional output back to the linker plugin. */
1502 /* Prepare the partial link to gather the compile-time generated
1503 debug-info into a single input for the final link. */
1504 debug_obj
= make_temp_file ("debugobj");
1505 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
1506 for (i
= 1; i
< decoded_options_count
; ++i
)
1508 /* Retain linker choice and -B. */
1509 if (decoded_options
[i
].opt_index
== OPT_B
1510 || decoded_options
[i
].opt_index
== OPT_fuse_ld_bfd
1511 || decoded_options
[i
].opt_index
== OPT_fuse_ld_gold
)
1512 append_linker_options (&argv_obstack
, &decoded_options
[i
-1], 2);
1513 /* Retain all target options, this preserves -m32 for example. */
1514 if (cl_options
[decoded_options
[i
].opt_index
].flags
& CL_TARGET
)
1515 append_linker_options (&argv_obstack
, &decoded_options
[i
-1], 2);
1516 /* Recognize -g0. */
1517 if (decoded_options
[i
].opt_index
== OPT_g
1518 && strcmp (decoded_options
[i
].arg
, "0") == 0)
1521 obstack_ptr_grow (&argv_obstack
, "-r");
1522 obstack_ptr_grow (&argv_obstack
, "-nostdlib");
1523 obstack_ptr_grow (&argv_obstack
, "-o");
1524 obstack_ptr_grow (&argv_obstack
, debug_obj
);
1526 /* Copy the early generated debug info from the objects to temporary
1527 files and append those to the partial link commandline. */
1530 for (i
= 0; i
< ltoobj_argc
; ++i
)
1533 if ((tem
= debug_objcopy (ltoobj_argv
[i
], !linker_output_rel
)))
1535 obstack_ptr_grow (&argv_obstack
, tem
);
1540 /* Link them all into a single object. Ideally this would reduce
1541 disk space usage mainly due to .debug_str merging but unfortunately
1542 GNU ld doesn't perform this with -r. */
1545 obstack_ptr_grow (&argv_obstack
, NULL
);
1546 const char **debug_link_argv
= XOBFINISH (&argv_obstack
, const char **);
1547 fork_execute (debug_link_argv
[0],
1548 CONST_CAST (char **, debug_link_argv
), false);
1550 /* And dispose the temporaries. */
1551 for (i
= 0; debug_link_argv
[i
]; ++i
)
1553 for (--i
; i
> 0; --i
)
1555 if (strcmp (debug_link_argv
[i
], debug_obj
) == 0)
1557 maybe_unlink (debug_link_argv
[i
]);
1562 unlink_if_ordinary (debug_obj
);
1568 if (lto_mode
== LTO_MODE_LTO
)
1570 printf ("%s\n", flto_out
);
1573 printf ("%s\n", debug_obj
);
1582 FILE *stream
= fopen (ltrans_output_file
, "r");
1583 FILE *mstream
= NULL
;
1584 struct obstack env_obstack
;
1588 fatal_error (input_location
, "fopen: %s: %m", ltrans_output_file
);
1590 /* Parse the list of LTRANS inputs from the WPA stage. */
1591 obstack_init (&env_obstack
);
1595 const unsigned piece
= 32;
1596 char *output_name
= NULL
;
1597 char *buf
, *input_name
= (char *)xmalloc (piece
);
1601 if (fscanf (stream
, "%i\n", &priority
) != 1)
1604 fatal_error (input_location
,
1605 "Corrupted ltrans output file %s",
1606 ltrans_output_file
);
1610 if (!fgets (buf
, piece
, stream
))
1612 len
= strlen (input_name
);
1613 if (input_name
[len
- 1] != '\n')
1615 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
1616 buf
= input_name
+ len
;
1619 input_name
[len
- 1] = '\0';
1621 if (input_name
[0] == '*')
1622 output_name
= &input_name
[1];
1626 = (int *)xrealloc (ltrans_priorities
, nr
* sizeof (int) * 2);
1627 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
1628 output_names
= (char **)xrealloc (output_names
, nr
* sizeof (char *));
1629 ltrans_priorities
[(nr
-1)*2] = priority
;
1630 ltrans_priorities
[(nr
-1)*2+1] = nr
-1;
1631 input_names
[nr
-1] = input_name
;
1632 output_names
[nr
-1] = output_name
;
1635 maybe_unlink (ltrans_output_file
);
1636 ltrans_output_file
= NULL
;
1640 makefile
= make_temp_file (".mk");
1641 mstream
= fopen (makefile
, "w");
1642 qsort (ltrans_priorities
, nr
, sizeof (int) * 2, cmp_priority
);
1645 /* Execute the LTRANS stage for each input file (or prepare a
1646 makefile to invoke this in parallel). */
1647 for (i
= 0; i
< nr
; ++i
)
1650 char *input_name
= input_names
[i
];
1651 /* If it's a pass-through file do nothing. */
1652 if (output_names
[i
])
1655 /* Replace the .o suffix with a .ltrans.o suffix and write
1656 the resulting name to the LTRANS output list. */
1657 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
1658 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1659 output_name
= XOBFINISH (&env_obstack
, char *);
1661 /* Adjust the dumpbase if the linker output file was seen. */
1665 = (char *) xmalloc (strlen (linker_output
)
1666 + sizeof (DUMPBASE_SUFFIX
) + 1);
1668 strlen (linker_output
) + sizeof (DUMPBASE_SUFFIX
),
1669 "%s.ltrans%u", linker_output
, i
);
1670 argv_ptr
[0] = dumpbase
;
1673 argv_ptr
[1] = "-fltrans";
1675 argv_ptr
[3] = output_name
;
1676 argv_ptr
[4] = input_name
;
1680 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
1681 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
1682 fprintf (mstream
, " '%s'", new_argv
[j
]);
1683 fprintf (mstream
, "\n");
1684 /* If we are not preserving the ltrans input files then
1685 truncate them as soon as we have processed it. This
1686 reduces temporary disk-space usage. */
1688 fprintf (mstream
, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1689 "&& mv %s.tem %s\n",
1690 input_name
, input_name
, input_name
, input_name
);
1694 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1696 maybe_unlink (input_name
);
1699 output_names
[i
] = output_name
;
1703 struct pex_obj
*pex
;
1706 fprintf (mstream
, "all:");
1707 for (i
= 0; i
< nr
; ++i
)
1709 int j
= ltrans_priorities
[i
*2 + 1];
1710 fprintf (mstream
, " \\\n\t%s", output_names
[j
]);
1712 fprintf (mstream
, "\n");
1716 /* Avoid passing --jobserver-fd= and similar flags
1717 unless jobserver mode is explicitly enabled. */
1718 putenv (xstrdup ("MAKEFLAGS="));
1719 putenv (xstrdup ("MFLAGS="));
1721 new_argv
[0] = getenv ("MAKE");
1723 new_argv
[0] = "make";
1725 new_argv
[2] = makefile
;
1729 snprintf (jobs
, 31, "-j%d", parallel
);
1730 new_argv
[i
++] = jobs
;
1732 new_argv
[i
++] = "all";
1733 new_argv
[i
++] = NULL
;
1734 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1735 NULL
, NULL
, PEX_SEARCH
, false);
1736 do_wait (new_argv
[0], pex
);
1737 maybe_unlink (makefile
);
1739 for (i
= 0; i
< nr
; ++i
)
1740 maybe_unlink (input_names
[i
]);
1744 printf ("%s\n", debug_obj
);
1748 for (i
= 0; i
< nr
; ++i
)
1750 fputs (output_names
[i
], stdout
);
1751 putc ('\n', stdout
);
1752 free (input_names
[i
]);
1755 free (ltrans_priorities
);
1756 free (output_names
);
1758 free (list_option_full
);
1759 obstack_free (&env_obstack
, NULL
);
1764 obstack_free (&argv_obstack
, NULL
);
1771 main (int argc
, char *argv
[])
1775 init_opts_obstack ();
1777 p
= argv
[0] + strlen (argv
[0]);
1778 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
1782 xmalloc_set_program_name (progname
);
1784 gcc_init_libintl ();
1786 diagnostic_initialize (global_dc
, 0);
1788 if (atexit (lto_wrapper_cleanup
) != 0)
1789 fatal_error (input_location
, "atexit failed");
1791 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
1792 signal (SIGINT
, fatal_signal
);
1794 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
1795 signal (SIGHUP
, fatal_signal
);
1797 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
1798 signal (SIGTERM
, fatal_signal
);
1800 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
1801 signal (SIGPIPE
, fatal_signal
);
1804 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1805 receive the signal. A different setting is inheritable */
1806 signal (SIGCHLD
, SIG_DFL
);
1809 /* We may be called with all the arguments stored in some file and
1810 passed with @file. Expand them into argv before processing. */
1811 expandargv (&argc
, &argv
);
1813 run_gcc (argc
, argv
);