]>
Commit | Line | Data |
---|---|---|
d7f09764 | 1 | /* Wrapper to call lto. Used by collect2 and the linker plugin. |
99dee823 | 2 | Copyright (C) 2009-2021 Free Software Foundation, Inc. |
d7f09764 DN |
3 | |
4 | Factored out of collect2 by Rafael Espindola <espindola@google.com> | |
5 | ||
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
10 | Software Foundation; either version 3, or (at your option) any later | |
11 | version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | ||
23 | /* This program is passed a gcc, a list of gcc arguments and a list of | |
24 | object files containing IL. It scans the argument list to check if | |
25 | we are in whopr mode or not modifies the arguments and needed and | |
26 | prints a list of output files on stdout. | |
27 | ||
28 | Example: | |
29 | ||
30 | $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto | |
31 | ||
32 | The above will print something like | |
33 | /tmp/ccwbQ8B2.lto.o | |
34 | ||
014d92e1 | 35 | If WHOPR is used instead, more than one file might be produced |
d7f09764 DN |
36 | ./ccXj2DTk.lto.ltrans.o |
37 | ./ccCJuXGv.lto.ltrans.o | |
38 | */ | |
39 | ||
40 | #include "config.h" | |
41 | #include "system.h" | |
10692477 | 42 | #include "coretypes.h" |
d7f09764 | 43 | #include "intl.h" |
2691e6d7 | 44 | #include "diagnostic.h" |
48cf395b | 45 | #include "obstack.h" |
f31c0018 RG |
46 | #include "opts.h" |
47 | #include "options.h" | |
52a35ef7 | 48 | #include "simple-object.h" |
4000360e | 49 | #include "lto-section-names.h" |
a185856a | 50 | #include "collect-utils.h" |
7d7d925d | 51 | #include "opts-diagnostic.h" |
d7f09764 | 52 | |
fc8b3540 IV |
53 | /* Environment variable, used for passing the names of offload targets from GCC |
54 | driver to lto-wrapper. */ | |
55 | #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES" | |
fe5bfa67 | 56 | #define OFFLOAD_TARGET_DEFAULT_ENV "OFFLOAD_TARGET_DEFAULT" |
fc8b3540 | 57 | |
1dedc12d AO |
58 | /* By default there is no special suffix for target executables. */ |
59 | #ifdef TARGET_EXECUTABLE_SUFFIX | |
60 | #define HAVE_TARGET_EXECUTABLE_SUFFIX | |
61 | #else | |
62 | #define TARGET_EXECUTABLE_SUFFIX "" | |
63 | #endif | |
64 | ||
d7f09764 | 65 | enum lto_mode_d { |
cf96bae7 RG |
66 | LTO_MODE_NONE, /* Not doing LTO. */ |
67 | LTO_MODE_LTO, /* Normal LTO. */ | |
68 | LTO_MODE_WHOPR /* WHOPR. */ | |
d7f09764 DN |
69 | }; |
70 | ||
71 | /* Current LTO mode. */ | |
72 | static enum lto_mode_d lto_mode = LTO_MODE_NONE; | |
73 | ||
b1b07c92 RG |
74 | static char *ltrans_output_file; |
75 | static char *flto_out; | |
50ee30d5 | 76 | static unsigned int nr; |
b6e33d73 | 77 | static int *ltrans_priorities; |
50ee30d5 RG |
78 | static char **input_names; |
79 | static char **output_names; | |
fc8b3540 | 80 | static char **offload_names; |
e6861a99 | 81 | static char *offload_objects_file_name; |
50ee30d5 | 82 | static char *makefile; |
b5327e50 IS |
83 | static unsigned int num_deb_objs; |
84 | static const char **early_debug_object_names; | |
f1a681a1 | 85 | static bool xassembler_options_error = false; |
b1b07c92 | 86 | |
a185856a | 87 | const char tool_name[] = "lto-wrapper"; |
b1b07c92 | 88 | |
a185856a | 89 | /* Delete tempfiles. Called from utils_cleanup. */ |
b1b07c92 | 90 | |
a185856a | 91 | void |
5f0ad6a5 | 92 | tool_cleanup (bool) |
b1b07c92 | 93 | { |
396717c9 RG |
94 | unsigned int i; |
95 | ||
396717c9 | 96 | if (ltrans_output_file) |
a185856a | 97 | maybe_unlink (ltrans_output_file); |
396717c9 | 98 | if (flto_out) |
a185856a | 99 | maybe_unlink (flto_out); |
e6861a99 IV |
100 | if (offload_objects_file_name) |
101 | maybe_unlink (offload_objects_file_name); | |
396717c9 | 102 | if (makefile) |
a185856a | 103 | maybe_unlink (makefile); |
b5327e50 IS |
104 | if (early_debug_object_names) |
105 | for (i = 0; i < num_deb_objs; ++i) | |
106 | if (early_debug_object_names[i]) | |
107 | maybe_unlink (early_debug_object_names[i]); | |
396717c9 | 108 | for (i = 0; i < nr; ++i) |
8aea79e6 | 109 | { |
a185856a | 110 | maybe_unlink (input_names[i]); |
396717c9 | 111 | if (output_names[i]) |
a185856a | 112 | maybe_unlink (output_names[i]); |
8aea79e6 | 113 | } |
396717c9 RG |
114 | } |
115 | ||
116 | static void | |
a185856a | 117 | lto_wrapper_cleanup (void) |
d7f09764 | 118 | { |
5f0ad6a5 | 119 | utils_cleanup (false); |
d7f09764 DN |
120 | } |
121 | ||
d7f09764 DN |
122 | /* Unlink a temporary LTRANS file unless requested otherwise. */ |
123 | ||
a185856a BS |
124 | void |
125 | maybe_unlink (const char *file) | |
d7f09764 | 126 | { |
608508a6 | 127 | if (!save_temps) |
d7f09764 | 128 | { |
62116e60 RG |
129 | if (unlink_if_ordinary (file) |
130 | && errno != ENOENT) | |
40fecdd6 | 131 | fatal_error (input_location, "deleting LTRANS file %s: %m", file); |
d7f09764 | 132 | } |
eba14fca | 133 | else if (verbose) |
d7f09764 DN |
134 | fprintf (stderr, "[Leaving LTRANS %s]\n", file); |
135 | } | |
136 | ||
48cf395b | 137 | /* Template of LTRANS dumpbase suffix. */ |
1dedc12d | 138 | #define DUMPBASE_SUFFIX "ltrans18446744073709551615" |
d7f09764 | 139 | |
f31c0018 | 140 | /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS |
8508ae1d | 141 | environment. */ |
f31c0018 | 142 | |
227a2ecf | 143 | static vec<cl_decoded_option> |
f31c0018 | 144 | get_options_from_collect_gcc_options (const char *collect_gcc, |
227a2ecf | 145 | const char *collect_gcc_options) |
f31c0018 | 146 | { |
227a2ecf ML |
147 | cl_decoded_option *decoded_options; |
148 | unsigned int decoded_options_count; | |
969d578c | 149 | struct obstack argv_obstack; |
f31c0018 | 150 | const char **argv; |
f1a681a1 | 151 | int argc; |
969d578c | 152 | |
969d578c RG |
153 | obstack_init (&argv_obstack); |
154 | obstack_ptr_grow (&argv_obstack, collect_gcc); | |
155 | ||
f1a681a1 PK |
156 | parse_options_from_collect_gcc_options (collect_gcc_options, |
157 | &argv_obstack, &argc); | |
969d578c | 158 | argv = XOBFINISH (&argv_obstack, const char **); |
f31c0018 | 159 | |
8508ae1d | 160 | decode_cmdline_options_to_array (argc, (const char **)argv, CL_DRIVER, |
227a2ecf ML |
161 | &decoded_options, &decoded_options_count); |
162 | vec<cl_decoded_option> decoded; | |
163 | decoded.create (decoded_options_count); | |
164 | for (unsigned i = 0; i < decoded_options_count; ++i) | |
165 | decoded.quick_push (decoded_options[i]); | |
166 | free (decoded_options); | |
167 | ||
969d578c | 168 | obstack_free (&argv_obstack, NULL); |
227a2ecf ML |
169 | |
170 | return decoded; | |
f31c0018 RG |
171 | } |
172 | ||
227a2ecf ML |
173 | /* Find option in OPTIONS based on OPT_INDEX. -1 value is returned |
174 | if the option is not present. */ | |
52a35ef7 | 175 | |
227a2ecf ML |
176 | static int |
177 | find_option (vec<cl_decoded_option> &options, size_t opt_index) | |
52a35ef7 | 178 | { |
227a2ecf ML |
179 | for (unsigned i = 0; i < options.length (); ++i) |
180 | if (options[i].opt_index == opt_index) | |
181 | return i; | |
52a35ef7 | 182 | |
227a2ecf ML |
183 | return -1; |
184 | } | |
472a2536 | 185 | |
227a2ecf ML |
186 | static int |
187 | find_option (vec<cl_decoded_option> &options, cl_decoded_option *option) | |
472a2536 | 188 | { |
227a2ecf | 189 | return find_option (options, option->opt_index); |
472a2536 JH |
190 | } |
191 | ||
3cbcb5d0 ML |
192 | /* Merge -flto FOPTION into vector of DECODED_OPTIONS. */ |
193 | ||
194 | static void | |
195 | merge_flto_options (vec<cl_decoded_option> &decoded_options, | |
196 | cl_decoded_option *foption) | |
197 | { | |
198 | int existing_opt = find_option (decoded_options, foption); | |
199 | if (existing_opt == -1) | |
200 | decoded_options.safe_push (*foption); | |
201 | else | |
202 | { | |
203 | if (strcmp (foption->arg, decoded_options[existing_opt].arg) != 0) | |
204 | { | |
205 | /* -flto=auto is preferred. */ | |
206 | if (strcmp (decoded_options[existing_opt].arg, "auto") == 0) | |
207 | ; | |
208 | else if (strcmp (foption->arg, "auto") == 0 | |
209 | || strcmp (foption->arg, "jobserver") == 0) | |
210 | decoded_options[existing_opt].arg = foption->arg; | |
211 | else if (strcmp (decoded_options[existing_opt].arg, | |
212 | "jobserver") != 0) | |
213 | { | |
214 | int n = atoi (foption->arg); | |
215 | int original_n = atoi (decoded_options[existing_opt].arg); | |
216 | if (n > original_n) | |
217 | decoded_options[existing_opt].arg = foption->arg; | |
218 | } | |
219 | } | |
220 | } | |
221 | } | |
222 | ||
52a35ef7 RG |
223 | /* Try to merge and complain about options FDECODED_OPTIONS when applied |
224 | ontop of DECODED_OPTIONS. */ | |
225 | ||
226 | static void | |
227a2ecf ML |
227 | merge_and_complain (vec<cl_decoded_option> decoded_options, |
228 | vec<cl_decoded_option> fdecoded_options, | |
229 | vec<cl_decoded_option> decoded_cl_options) | |
52a35ef7 RG |
230 | { |
231 | unsigned int i, j; | |
227a2ecf ML |
232 | cl_decoded_option *pic_option = NULL; |
233 | cl_decoded_option *pie_option = NULL; | |
234 | cl_decoded_option *cf_protection_option = NULL; | |
52a35ef7 RG |
235 | |
236 | /* ??? Merge options from files. Most cases can be | |
237 | handled by either unioning or intersecting | |
238 | (for example -fwrapv is a case for unioning, | |
239 | -ffast-math is for intersection). Most complaints | |
240 | about real conflicts between different options can | |
241 | be deferred to the compiler proper. Options that | |
242 | we can neither safely handle by intersection nor | |
243 | unioning would need to be complained about here. | |
244 | Ideally we'd have a flag in the opt files that | |
245 | tells whether to union or intersect or reject. | |
246 | In absence of that it's unclear what a good default is. | |
247 | It's also difficult to get positional handling correct. */ | |
248 | ||
6a48d124 MK |
249 | /* Look for a -fcf-protection option in the link-time options |
250 | which overrides any -fcf-protection from the lto sections. */ | |
227a2ecf | 251 | for (i = 0; i < decoded_cl_options.length (); ++i) |
6a48d124 | 252 | { |
227a2ecf | 253 | cl_decoded_option *foption = &decoded_cl_options[i]; |
6a48d124 MK |
254 | if (foption->opt_index == OPT_fcf_protection_) |
255 | { | |
256 | cf_protection_option = foption; | |
257 | } | |
258 | } | |
259 | ||
52a35ef7 RG |
260 | /* The following does what the old LTO option code did, |
261 | union all target and a selected set of common options. */ | |
227a2ecf | 262 | for (i = 0; i < fdecoded_options.length (); ++i) |
52a35ef7 | 263 | { |
227a2ecf ML |
264 | cl_decoded_option *foption = &fdecoded_options[i]; |
265 | int existing_opt = find_option (decoded_options, foption); | |
52a35ef7 RG |
266 | switch (foption->opt_index) |
267 | { | |
169d8507 L |
268 | case OPT_SPECIAL_unknown: |
269 | case OPT_SPECIAL_ignore: | |
68a57628 | 270 | case OPT_SPECIAL_warn_removed: |
169d8507 L |
271 | case OPT_SPECIAL_program_name: |
272 | case OPT_SPECIAL_input_file: | |
273 | break; | |
274 | ||
52a35ef7 RG |
275 | default: |
276 | if (!(cl_options[foption->opt_index].flags & CL_TARGET)) | |
277 | break; | |
278 | ||
279 | /* Fallthru. */ | |
eb472171 | 280 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 281 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 282 | case OPT_fdiagnostics_show_line_numbers: |
eb472171 CLT |
283 | case OPT_fdiagnostics_show_option: |
284 | case OPT_fdiagnostics_show_location_: | |
285 | case OPT_fshow_column: | |
52a35ef7 | 286 | case OPT_fcommon: |
aad038ca | 287 | case OPT_fgnu_tm: |
5a307ee5 | 288 | case OPT_g: |
52a35ef7 RG |
289 | /* Do what the old LTO code did - collect exactly one option |
290 | setting per OPT code, we pick the first we encounter. | |
291 | ??? This doesn't make too much sense, but when it doesn't | |
292 | then we should complain. */ | |
227a2ecf ML |
293 | if (existing_opt == -1) |
294 | decoded_options.safe_push (*foption); | |
52a35ef7 | 295 | break; |
8bb50e5c | 296 | |
472a2536 JH |
297 | /* Figure out what PIC/PIE level wins and merge the results. */ |
298 | case OPT_fPIC: | |
299 | case OPT_fpic: | |
300 | pic_option = foption; | |
301 | break; | |
302 | case OPT_fPIE: | |
303 | case OPT_fpie: | |
304 | pie_option = foption; | |
305 | break; | |
306 | ||
1506ae0e | 307 | case OPT_fopenmp: |
a0c88d06 | 308 | case OPT_fopenacc: |
4094757e | 309 | /* For selected options we can merge conservatively. */ |
227a2ecf ML |
310 | if (existing_opt == -1) |
311 | decoded_options.safe_push (*foption); | |
2fff1c81 | 312 | /* -fopenmp > -fno-openmp, |
c0c12356 | 313 | -fopenacc > -fno-openacc */ |
227a2ecf ML |
314 | else if (foption->value > decoded_options[existing_opt].value) |
315 | decoded_options[existing_opt] = *foption; | |
4094757e RB |
316 | break; |
317 | ||
b6adbb9f NS |
318 | case OPT_fopenacc_dim_: |
319 | /* Append or check identical. */ | |
227a2ecf ML |
320 | if (existing_opt == -1) |
321 | decoded_options.safe_push (*foption); | |
322 | else if (strcmp (decoded_options[existing_opt].arg, foption->arg)) | |
b6adbb9f | 323 | fatal_error (input_location, |
a9c697b8 | 324 | "option %s with different values", |
b6adbb9f NS |
325 | foption->orig_option_with_args_text); |
326 | break; | |
327 | ||
6a48d124 MK |
328 | case OPT_fcf_protection_: |
329 | /* Default to link-time option, else append or check identical. */ | |
c4c22e83 L |
330 | if (!cf_protection_option |
331 | || cf_protection_option->value == CF_CHECK) | |
6a48d124 | 332 | { |
227a2ecf ML |
333 | if (existing_opt == -1) |
334 | decoded_options.safe_push (*foption); | |
335 | else if (decoded_options[existing_opt].value != foption->value) | |
c4c22e83 L |
336 | { |
337 | if (cf_protection_option | |
338 | && cf_protection_option->value == CF_CHECK) | |
339 | fatal_error (input_location, | |
ca23341b | 340 | "option %qs with mismatching values" |
c4c22e83 | 341 | " (%s, %s)", |
ca23341b | 342 | "-fcf-protection", |
227a2ecf ML |
343 | decoded_options[existing_opt].arg, |
344 | foption->arg); | |
c4c22e83 L |
345 | else |
346 | { | |
347 | /* Merge and update the -fcf-protection option. */ | |
227a2ecf ML |
348 | decoded_options[existing_opt].value |
349 | &= (foption->value & CF_FULL); | |
350 | switch (decoded_options[existing_opt].value) | |
c4c22e83 L |
351 | { |
352 | case CF_NONE: | |
227a2ecf | 353 | decoded_options[existing_opt].arg = "none"; |
c4c22e83 L |
354 | break; |
355 | case CF_BRANCH: | |
227a2ecf | 356 | decoded_options[existing_opt].arg = "branch"; |
c4c22e83 L |
357 | break; |
358 | case CF_RETURN: | |
227a2ecf | 359 | decoded_options[existing_opt].arg = "return"; |
c4c22e83 L |
360 | break; |
361 | default: | |
362 | gcc_unreachable (); | |
363 | } | |
364 | } | |
365 | } | |
6a48d124 | 366 | } |
c4c22e83 | 367 | break; |
6a48d124 | 368 | |
f3ba16d0 RB |
369 | case OPT_O: |
370 | case OPT_Ofast: | |
371 | case OPT_Og: | |
372 | case OPT_Os: | |
227a2ecf ML |
373 | existing_opt = -1; |
374 | for (j = 0; j < decoded_options.length (); ++j) | |
375 | if (decoded_options[j].opt_index == OPT_O | |
376 | || decoded_options[j].opt_index == OPT_Ofast | |
377 | || decoded_options[j].opt_index == OPT_Og | |
378 | || decoded_options[j].opt_index == OPT_Os) | |
379 | { | |
380 | existing_opt = j; | |
381 | break; | |
382 | } | |
383 | if (existing_opt == -1) | |
384 | decoded_options.safe_push (*foption); | |
385 | else if (decoded_options[existing_opt].opt_index == foption->opt_index | |
f3ba16d0 RB |
386 | && foption->opt_index != OPT_O) |
387 | /* Exact same options get merged. */ | |
388 | ; | |
389 | else | |
390 | { | |
391 | /* For mismatched option kinds preserve the optimization | |
392 | level only, thus merge it as -On. This also handles | |
393 | merging of same optimization level -On. */ | |
394 | int level = 0; | |
395 | switch (foption->opt_index) | |
396 | { | |
397 | case OPT_O: | |
398 | if (foption->arg[0] == '\0') | |
399 | level = MAX (level, 1); | |
400 | else | |
401 | level = MAX (level, atoi (foption->arg)); | |
402 | break; | |
403 | case OPT_Ofast: | |
404 | level = MAX (level, 3); | |
405 | break; | |
406 | case OPT_Og: | |
407 | level = MAX (level, 1); | |
408 | break; | |
409 | case OPT_Os: | |
410 | level = MAX (level, 2); | |
411 | break; | |
412 | default: | |
413 | gcc_unreachable (); | |
414 | } | |
227a2ecf | 415 | switch (decoded_options[existing_opt].opt_index) |
f3ba16d0 RB |
416 | { |
417 | case OPT_O: | |
227a2ecf | 418 | if (decoded_options[existing_opt].arg[0] == '\0') |
f3ba16d0 RB |
419 | level = MAX (level, 1); |
420 | else | |
227a2ecf ML |
421 | level = MAX (level, |
422 | atoi (decoded_options[existing_opt].arg)); | |
f3ba16d0 RB |
423 | break; |
424 | case OPT_Ofast: | |
425 | level = MAX (level, 3); | |
426 | break; | |
427 | case OPT_Og: | |
428 | level = MAX (level, 1); | |
429 | break; | |
430 | case OPT_Os: | |
431 | level = MAX (level, 2); | |
432 | break; | |
433 | default: | |
434 | gcc_unreachable (); | |
435 | } | |
227a2ecf | 436 | decoded_options[existing_opt].opt_index = OPT_O; |
f3ba16d0 | 437 | char *tem; |
582f770b | 438 | tem = xasprintf ("-O%d", level); |
227a2ecf ML |
439 | decoded_options[existing_opt].arg = &tem[2]; |
440 | decoded_options[existing_opt].canonical_option[0] = tem; | |
441 | decoded_options[existing_opt].value = 1; | |
f3ba16d0 RB |
442 | } |
443 | break; | |
472a2536 JH |
444 | |
445 | ||
446 | case OPT_foffload_abi_: | |
227a2ecf ML |
447 | if (existing_opt == -1) |
448 | decoded_options.safe_push (*foption); | |
449 | else if (foption->value != decoded_options[existing_opt].value) | |
472a2536 | 450 | fatal_error (input_location, |
a9c697b8 | 451 | "option %s not used consistently in all LTO input" |
472a2536 JH |
452 | " files", foption->orig_option_with_args_text); |
453 | break; | |
454 | ||
c713ddc0 | 455 | |
33c4e466 | 456 | case OPT_foffload_options_: |
227a2ecf | 457 | decoded_options.safe_push (*foption); |
c713ddc0 | 458 | break; |
3835aa0e ML |
459 | |
460 | case OPT_flto_: | |
3cbcb5d0 | 461 | merge_flto_options (decoded_options, foption); |
3835aa0e | 462 | break; |
52a35ef7 RG |
463 | } |
464 | } | |
472a2536 JH |
465 | |
466 | /* Merge PIC options: | |
467 | -fPIC + -fpic = -fpic | |
468 | -fPIC + -fno-pic = -fno-pic | |
5afa32b8 | 469 | -fpic/-fPIC + nothing = nothing. |
472a2536 JH |
470 | It is a common mistake to mix few -fPIC compiled objects into otherwise |
471 | non-PIC code. We do not want to build everything with PIC then. | |
472 | ||
4e6a9380 JH |
473 | Similarly we merge PIE options, however in addition we keep |
474 | -fPIC + -fPIE = -fPIE | |
475 | -fpic + -fPIE = -fpie | |
476 | -fPIC/-fpic + -fpie = -fpie | |
477 | ||
472a2536 JH |
478 | It would be good to warn on mismatches, but it is bit hard to do as |
479 | we do not know what nothing translates to. */ | |
480 | ||
227a2ecf ML |
481 | for (unsigned int j = 0; j < decoded_options.length ();) |
482 | if (decoded_options[j].opt_index == OPT_fPIC | |
483 | || decoded_options[j].opt_index == OPT_fpic) | |
472a2536 | 484 | { |
4e6a9380 | 485 | /* -fno-pic in one unit implies -fno-pic everywhere. */ |
227a2ecf | 486 | if (decoded_options[j].value == 0) |
4e6a9380 JH |
487 | j++; |
488 | /* If we have no pic option or merge in -fno-pic, we still may turn | |
489 | existing pic/PIC mode into pie/PIE if -fpie/-fPIE is present. */ | |
490 | else if ((pic_option && pic_option->value == 0) | |
491 | || !pic_option) | |
492 | { | |
493 | if (pie_option) | |
494 | { | |
227a2ecf | 495 | bool big = decoded_options[j].opt_index == OPT_fPIC |
4e6a9380 | 496 | && pie_option->opt_index == OPT_fPIE; |
227a2ecf | 497 | decoded_options[j].opt_index = big ? OPT_fPIE : OPT_fpie; |
4e6a9380 | 498 | if (pie_option->value) |
227a2ecf | 499 | decoded_options[j].canonical_option[0] |
5afa32b8 | 500 | = big ? "-fPIE" : "-fpie"; |
4e6a9380 | 501 | else |
227a2ecf ML |
502 | decoded_options[j].canonical_option[0] = "-fno-pie"; |
503 | decoded_options[j].value = pie_option->value; | |
504 | j++; | |
4e6a9380 JH |
505 | } |
506 | else if (pic_option) | |
507 | { | |
227a2ecf ML |
508 | decoded_options[j] = *pic_option; |
509 | j++; | |
4e6a9380 JH |
510 | } |
511 | /* We do not know if target defaults to pic or not, so just remove | |
512 | option if it is missing in one unit but enabled in other. */ | |
513 | else | |
227a2ecf | 514 | decoded_options.ordered_remove (j); |
4e6a9380 JH |
515 | } |
516 | else if (pic_option->opt_index == OPT_fpic | |
227a2ecf | 517 | && decoded_options[j].opt_index == OPT_fPIC) |
472a2536 | 518 | { |
227a2ecf | 519 | decoded_options[j] = *pic_option; |
472a2536 JH |
520 | j++; |
521 | } | |
522 | else | |
523 | j++; | |
524 | } | |
227a2ecf ML |
525 | else if (decoded_options[j].opt_index == OPT_fPIE |
526 | || decoded_options[j].opt_index == OPT_fpie) | |
472a2536 | 527 | { |
4e6a9380 | 528 | /* -fno-pie in one unit implies -fno-pie everywhere. */ |
227a2ecf | 529 | if (decoded_options[j].value == 0) |
4e6a9380 JH |
530 | j++; |
531 | /* If we have no pie option or merge in -fno-pie, we still preserve | |
532 | PIE/pie if pic/PIC is present. */ | |
533 | else if ((pie_option && pie_option->value == 0) | |
534 | || !pie_option) | |
535 | { | |
536 | /* If -fPIC/-fpic is given, merge it with -fPIE/-fpie. */ | |
537 | if (pic_option) | |
538 | { | |
539 | if (pic_option->opt_index == OPT_fpic | |
227a2ecf | 540 | && decoded_options[j].opt_index == OPT_fPIE) |
4e6a9380 | 541 | { |
227a2ecf ML |
542 | decoded_options[j].opt_index = OPT_fpie; |
543 | decoded_options[j].canonical_option[0] | |
5afa32b8 | 544 | = pic_option->value ? "-fpie" : "-fno-pie"; |
4e6a9380 JH |
545 | } |
546 | else if (!pic_option->value) | |
227a2ecf ML |
547 | decoded_options[j].canonical_option[0] = "-fno-pie"; |
548 | decoded_options[j].value = pic_option->value; | |
4e6a9380 JH |
549 | j++; |
550 | } | |
551 | else if (pie_option) | |
552 | { | |
227a2ecf | 553 | decoded_options[j] = *pie_option; |
4e6a9380 JH |
554 | j++; |
555 | } | |
556 | /* Because we always append pic/PIE options this code path should | |
557 | not happen unless the LTO object was built by old lto1 which | |
558 | did not contain that logic yet. */ | |
559 | else | |
227a2ecf | 560 | decoded_options.ordered_remove (j); |
4e6a9380 JH |
561 | } |
562 | else if (pie_option->opt_index == OPT_fpie | |
227a2ecf | 563 | && decoded_options[j].opt_index == OPT_fPIE) |
472a2536 | 564 | { |
227a2ecf | 565 | decoded_options[j] = *pie_option; |
472a2536 JH |
566 | j++; |
567 | } | |
568 | else | |
569 | j++; | |
570 | } | |
571 | else | |
572 | j++; | |
f1a681a1 PK |
573 | |
574 | if (!xassembler_options_error) | |
575 | for (i = j = 0; ; i++, j++) | |
576 | { | |
227a2ecf ML |
577 | int existing_opt_index |
578 | = find_option (decoded_options, OPT_Xassembler); | |
579 | int existing_opt2_index | |
580 | = find_option (fdecoded_options, OPT_Xassembler); | |
581 | ||
582 | cl_decoded_option *existing_opt = NULL; | |
583 | cl_decoded_option *existing_opt2 = NULL; | |
584 | if (existing_opt_index != -1) | |
585 | existing_opt = &decoded_options[existing_opt_index]; | |
586 | if (existing_opt2_index != -1) | |
587 | existing_opt2 = &fdecoded_options[existing_opt2_index]; | |
588 | ||
589 | if (existing_opt == NULL && existing_opt2 == NULL) | |
f1a681a1 | 590 | break; |
227a2ecf | 591 | else if (existing_opt != NULL && existing_opt2 == NULL) |
f1a681a1 | 592 | { |
47fe9634 ML |
593 | warning (0, "Extra option to %<-Xassembler%>: %s," |
594 | " dropping all %<-Xassembler%> and %<-Wa%> options.", | |
227a2ecf | 595 | existing_opt->arg); |
f1a681a1 PK |
596 | xassembler_options_error = true; |
597 | break; | |
598 | } | |
227a2ecf | 599 | else if (existing_opt == NULL && existing_opt2 != NULL) |
f1a681a1 | 600 | { |
47fe9634 ML |
601 | warning (0, "Extra option to %<-Xassembler%>: %s," |
602 | " dropping all %<-Xassembler%> and %<-Wa%> options.", | |
227a2ecf | 603 | existing_opt2->arg); |
f1a681a1 PK |
604 | xassembler_options_error = true; |
605 | break; | |
606 | } | |
227a2ecf | 607 | else if (strcmp (existing_opt->arg, existing_opt2->arg) != 0) |
f1a681a1 | 608 | { |
47fe9634 ML |
609 | warning (0, "Options to %<-Xassembler%> do not match: %s, %s," |
610 | " dropping all %<-Xassembler%> and %<-Wa%> options.", | |
227a2ecf | 611 | existing_opt->arg, existing_opt2->arg); |
f1a681a1 PK |
612 | xassembler_options_error = true; |
613 | break; | |
614 | } | |
615 | } | |
52a35ef7 | 616 | } |
f31c0018 | 617 | |
fc8b3540 IV |
618 | /* Auxiliary function that frees elements of PTR and PTR itself. |
619 | N is number of elements to be freed. If PTR is NULL, nothing is freed. | |
620 | If an element is NULL, subsequent elements are not freed. */ | |
621 | ||
622 | static void ** | |
623 | free_array_of_ptrs (void **ptr, unsigned n) | |
624 | { | |
625 | if (!ptr) | |
626 | return NULL; | |
627 | for (unsigned i = 0; i < n; i++) | |
628 | { | |
629 | if (!ptr[i]) | |
630 | break; | |
631 | free (ptr[i]); | |
632 | } | |
633 | free (ptr); | |
634 | return NULL; | |
635 | } | |
636 | ||
637 | /* Parse STR, saving found tokens into PVALUES and return their number. | |
638 | Tokens are assumed to be delimited by ':'. If APPEND is non-null, | |
639 | append it to every token we find. */ | |
640 | ||
641 | static unsigned | |
642 | parse_env_var (const char *str, char ***pvalues, const char *append) | |
643 | { | |
644 | const char *curval, *nextval; | |
645 | char **values; | |
646 | unsigned num = 1, i; | |
647 | ||
648 | curval = strchr (str, ':'); | |
649 | while (curval) | |
650 | { | |
651 | num++; | |
652 | curval = strchr (curval + 1, ':'); | |
653 | } | |
654 | ||
655 | values = (char**) xmalloc (num * sizeof (char*)); | |
656 | curval = str; | |
b08dec2f DH |
657 | nextval = strchr (curval, ':'); |
658 | if (nextval == NULL) | |
659 | nextval = strchr (curval, '\0'); | |
fc8b3540 IV |
660 | |
661 | int append_len = append ? strlen (append) : 0; | |
662 | for (i = 0; i < num; i++) | |
663 | { | |
664 | int l = nextval - curval; | |
665 | values[i] = (char*) xmalloc (l + 1 + append_len); | |
666 | memcpy (values[i], curval, l); | |
667 | values[i][l] = 0; | |
668 | if (append) | |
669 | strcat (values[i], append); | |
670 | curval = nextval + 1; | |
b08dec2f DH |
671 | nextval = strchr (curval, ':'); |
672 | if (nextval == NULL) | |
673 | nextval = strchr (curval, '\0'); | |
fc8b3540 IV |
674 | } |
675 | *pvalues = values; | |
676 | return num; | |
677 | } | |
678 | ||
c713ddc0 BS |
679 | /* Append options OPTS from lto or offload_lto sections to ARGV_OBSTACK. */ |
680 | ||
681 | static void | |
227a2ecf | 682 | append_compiler_options (obstack *argv_obstack, vec<cl_decoded_option> opts) |
c713ddc0 BS |
683 | { |
684 | /* Append compiler driver arguments as far as they were merged. */ | |
227a2ecf | 685 | for (unsigned int j = 1; j < opts.length (); ++j) |
c713ddc0 | 686 | { |
227a2ecf | 687 | cl_decoded_option *option = &opts[j]; |
c713ddc0 BS |
688 | |
689 | /* File options have been properly filtered by lto-opts.c. */ | |
690 | switch (option->opt_index) | |
691 | { | |
692 | /* Drop arguments that we want to take from the link line. */ | |
693 | case OPT_flto_: | |
694 | case OPT_flto: | |
695 | case OPT_flto_partition_: | |
696 | continue; | |
697 | ||
698 | default: | |
699 | break; | |
700 | } | |
701 | ||
702 | /* For now do what the original LTO option code was doing - pass | |
703 | on any CL_TARGET flag and a few selected others. */ | |
704 | switch (option->opt_index) | |
705 | { | |
eb472171 | 706 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 707 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 708 | case OPT_fdiagnostics_show_line_numbers: |
eb472171 CLT |
709 | case OPT_fdiagnostics_show_option: |
710 | case OPT_fdiagnostics_show_location_: | |
711 | case OPT_fshow_column: | |
c713ddc0 BS |
712 | case OPT_fPIC: |
713 | case OPT_fpic: | |
714 | case OPT_fPIE: | |
715 | case OPT_fpie: | |
716 | case OPT_fcommon: | |
c713ddc0 | 717 | case OPT_fgnu_tm: |
1506ae0e | 718 | case OPT_fopenmp: |
a0c88d06 | 719 | case OPT_fopenacc: |
b6adbb9f | 720 | case OPT_fopenacc_dim_: |
c713ddc0 | 721 | case OPT_foffload_abi_: |
6a48d124 | 722 | case OPT_fcf_protection_: |
5a307ee5 | 723 | case OPT_g: |
c713ddc0 BS |
724 | case OPT_O: |
725 | case OPT_Ofast: | |
726 | case OPT_Og: | |
727 | case OPT_Os: | |
728 | break; | |
729 | ||
f1a681a1 PK |
730 | case OPT_Xassembler: |
731 | /* When we detected a mismatch in assembler options between | |
732 | the input TU's fall back to previous behavior of ignoring them. */ | |
733 | if (xassembler_options_error) | |
734 | continue; | |
735 | break; | |
736 | ||
c713ddc0 BS |
737 | default: |
738 | if (!(cl_options[option->opt_index].flags & CL_TARGET)) | |
739 | continue; | |
740 | } | |
741 | ||
742 | /* Pass the option on. */ | |
743 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
744 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
745 | } | |
746 | } | |
747 | ||
227a2ecf | 748 | /* Append diag options in OPTS to ARGV_OBSTACK. */ |
34df756c TV |
749 | |
750 | static void | |
227a2ecf | 751 | append_diag_options (obstack *argv_obstack, vec<cl_decoded_option> opts) |
34df756c TV |
752 | { |
753 | /* Append compiler driver arguments as far as they were merged. */ | |
227a2ecf | 754 | for (unsigned int j = 1; j < opts.length (); ++j) |
34df756c | 755 | { |
227a2ecf | 756 | cl_decoded_option *option = &opts[j]; |
34df756c TV |
757 | |
758 | switch (option->opt_index) | |
759 | { | |
760 | case OPT_fdiagnostics_color_: | |
478dd60d | 761 | case OPT_fdiagnostics_format_: |
34df756c | 762 | case OPT_fdiagnostics_show_caret: |
96e6ae57 | 763 | case OPT_fdiagnostics_show_labels: |
56b61d7f | 764 | case OPT_fdiagnostics_show_line_numbers: |
34df756c TV |
765 | case OPT_fdiagnostics_show_option: |
766 | case OPT_fdiagnostics_show_location_: | |
767 | case OPT_fshow_column: | |
768 | break; | |
769 | default: | |
770 | continue; | |
771 | } | |
772 | ||
773 | /* Pass the option on. */ | |
774 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
775 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
776 | } | |
777 | } | |
778 | ||
779 | ||
c713ddc0 BS |
780 | /* Append linker options OPTS to ARGV_OBSTACK. */ |
781 | ||
782 | static void | |
227a2ecf | 783 | append_linker_options (obstack *argv_obstack, vec<cl_decoded_option> opts) |
c713ddc0 BS |
784 | { |
785 | /* Append linker driver arguments. Compiler options from the linker | |
786 | driver arguments will override / merge with those from the compiler. */ | |
227a2ecf | 787 | for (unsigned int j = 1; j < opts.length (); ++j) |
c713ddc0 | 788 | { |
227a2ecf | 789 | cl_decoded_option *option = &opts[j]; |
c713ddc0 BS |
790 | |
791 | /* Do not pass on frontend specific flags not suitable for lto. */ | |
792 | if (!(cl_options[option->opt_index].flags | |
793 | & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO))) | |
794 | continue; | |
795 | ||
796 | switch (option->opt_index) | |
797 | { | |
798 | case OPT_o: | |
799 | case OPT_flto_: | |
800 | case OPT_flto: | |
801 | /* We've handled these LTO options, do not pass them on. */ | |
802 | continue; | |
803 | ||
b0b35f86 JJ |
804 | case OPT_fopenmp: |
805 | case OPT_fopenacc: | |
b0b35f86 JJ |
806 | /* Ignore -fno-XXX form of these options, as otherwise |
807 | corresponding builtins will not be enabled. */ | |
808 | if (option->value == 0) | |
809 | continue; | |
810 | break; | |
811 | ||
c713ddc0 BS |
812 | default: |
813 | break; | |
814 | } | |
815 | ||
816 | /* Pass the option on. */ | |
817 | for (unsigned int i = 0; i < option->canonical_option_num_elements; ++i) | |
818 | obstack_ptr_grow (argv_obstack, option->canonical_option[i]); | |
819 | } | |
820 | } | |
821 | ||
822 | /* Extract options for TARGET offload compiler from OPTIONS and append | |
823 | them to ARGV_OBSTACK. */ | |
824 | ||
825 | static void | |
826 | append_offload_options (obstack *argv_obstack, const char *target, | |
227a2ecf | 827 | vec<cl_decoded_option> options) |
c713ddc0 | 828 | { |
227a2ecf | 829 | for (unsigned i = 0; i < options.length (); i++) |
c713ddc0 BS |
830 | { |
831 | const char *cur, *next, *opts; | |
832 | char **argv; | |
833 | unsigned argc; | |
227a2ecf | 834 | cl_decoded_option *option = &options[i]; |
c713ddc0 | 835 | |
33c4e466 | 836 | if (option->opt_index != OPT_foffload_options_) |
c713ddc0 BS |
837 | continue; |
838 | ||
839 | /* If option argument starts with '-' then no target is specified. That | |
840 | means offload options are specified for all targets, so we need to | |
841 | append them. */ | |
842 | if (option->arg[0] == '-') | |
843 | opts = option->arg; | |
844 | else | |
845 | { | |
846 | opts = strchr (option->arg, '='); | |
33c4e466 | 847 | gcc_assert (opts); |
c713ddc0 BS |
848 | cur = option->arg; |
849 | ||
850 | while (cur < opts) | |
851 | { | |
b08dec2f DH |
852 | next = strchr (cur, ','); |
853 | if (next == NULL) | |
191ec640 | 854 | next = opts; |
c713ddc0 BS |
855 | next = (next > opts) ? opts : next; |
856 | ||
64186aad | 857 | /* Are we looking for this offload target? */ |
c713ddc0 BS |
858 | if (strlen (target) == (size_t) (next - cur) |
859 | && strncmp (target, cur, next - cur) == 0) | |
860 | break; | |
861 | ||
64186aad | 862 | /* Skip the comma or equal sign. */ |
c713ddc0 BS |
863 | cur = next + 1; |
864 | } | |
865 | ||
866 | if (cur >= opts) | |
867 | continue; | |
868 | ||
869 | opts++; | |
870 | } | |
871 | ||
872 | argv = buildargv (opts); | |
873 | for (argc = 0; argv[argc]; argc++) | |
874 | obstack_ptr_grow (argv_obstack, argv[argc]); | |
875 | } | |
876 | } | |
877 | ||
fc8b3540 IV |
878 | /* Check whether NAME can be accessed in MODE. This is like access, |
879 | except that it never considers directories to be executable. */ | |
880 | ||
881 | static int | |
882 | access_check (const char *name, int mode) | |
883 | { | |
884 | if (mode == X_OK) | |
885 | { | |
886 | struct stat st; | |
887 | ||
888 | if (stat (name, &st) < 0 | |
889 | || S_ISDIR (st.st_mode)) | |
890 | return -1; | |
891 | } | |
892 | ||
893 | return access (name, mode); | |
894 | } | |
895 | ||
896 | /* Prepare a target image for offload TARGET, using mkoffload tool from | |
897 | COMPILER_PATH. Return the name of the resultant object file. */ | |
898 | ||
899 | static char * | |
900 | compile_offload_image (const char *target, const char *compiler_path, | |
c713ddc0 | 901 | unsigned in_argc, char *in_argv[], |
227a2ecf ML |
902 | vec<cl_decoded_option> compiler_opts, |
903 | vec<cl_decoded_option> linker_opts) | |
fc8b3540 IV |
904 | { |
905 | char *filename = NULL; | |
efc16503 | 906 | char *dumpbase; |
fc8b3540 IV |
907 | char **argv; |
908 | char *suffix | |
909 | = XALLOCAVEC (char, sizeof ("/accel//mkoffload") + strlen (target)); | |
910 | strcpy (suffix, "/accel/"); | |
911 | strcat (suffix, target); | |
912 | strcat (suffix, "/mkoffload"); | |
913 | ||
914 | char **paths = NULL; | |
915 | unsigned n_paths = parse_env_var (compiler_path, &paths, suffix); | |
916 | ||
917 | const char *compiler = NULL; | |
918 | for (unsigned i = 0; i < n_paths; i++) | |
919 | if (access_check (paths[i], X_OK) == 0) | |
920 | { | |
921 | compiler = paths[i]; | |
922 | break; | |
923 | } | |
fe5bfa67 TB |
924 | #if OFFLOAD_DEFAULTED |
925 | if (!compiler && getenv (OFFLOAD_TARGET_DEFAULT_ENV)) | |
926 | { | |
927 | free_array_of_ptrs ((void **) paths, n_paths); | |
928 | return NULL; | |
929 | } | |
930 | #endif | |
fc8b3540 | 931 | |
c2e1580c TV |
932 | if (!compiler) |
933 | fatal_error (input_location, | |
a9c697b8 | 934 | "could not find %s in %s (consider using %<-B%>)", |
904f3daa | 935 | suffix + 1, compiler_path); |
c2e1580c | 936 | |
efc16503 AO |
937 | dumpbase = concat (dumppfx, "x", target, NULL); |
938 | ||
c2e1580c | 939 | /* Generate temporary output file name. */ |
efc16503 AO |
940 | if (save_temps) |
941 | filename = concat (dumpbase, ".o", NULL); | |
942 | else | |
943 | filename = make_temp_file (".target.o"); | |
c2e1580c TV |
944 | |
945 | struct obstack argv_obstack; | |
946 | obstack_init (&argv_obstack); | |
947 | obstack_ptr_grow (&argv_obstack, compiler); | |
948 | if (save_temps) | |
949 | obstack_ptr_grow (&argv_obstack, "-save-temps"); | |
950 | if (verbose) | |
951 | obstack_ptr_grow (&argv_obstack, "-v"); | |
952 | obstack_ptr_grow (&argv_obstack, "-o"); | |
953 | obstack_ptr_grow (&argv_obstack, filename); | |
954 | ||
955 | /* Append names of input object files. */ | |
956 | for (unsigned i = 0; i < in_argc; i++) | |
957 | obstack_ptr_grow (&argv_obstack, in_argv[i]); | |
958 | ||
959 | /* Append options from offload_lto sections. */ | |
227a2ecf ML |
960 | append_compiler_options (&argv_obstack, compiler_opts); |
961 | append_diag_options (&argv_obstack, linker_opts); | |
c2e1580c | 962 | |
efc16503 AO |
963 | obstack_ptr_grow (&argv_obstack, "-dumpbase"); |
964 | obstack_ptr_grow (&argv_obstack, dumpbase); | |
965 | ||
c2e1580c TV |
966 | /* Append options specified by -foffload last. In case of conflicting |
967 | options we expect offload compiler to choose the latest. */ | |
227a2ecf ML |
968 | append_offload_options (&argv_obstack, target, compiler_opts); |
969 | append_offload_options (&argv_obstack, target, linker_opts); | |
c2e1580c TV |
970 | |
971 | obstack_ptr_grow (&argv_obstack, NULL); | |
972 | argv = XOBFINISH (&argv_obstack, char **); | |
b3032d1b | 973 | fork_execute (argv[0], argv, true, "offload_args"); |
c2e1580c | 974 | obstack_free (&argv_obstack, NULL); |
fc8b3540 IV |
975 | |
976 | free_array_of_ptrs ((void **) paths, n_paths); | |
977 | return filename; | |
978 | } | |
979 | ||
980 | ||
981 | /* The main routine dealing with offloading. | |
982 | The routine builds a target image for each offload target. IN_ARGC and | |
983 | IN_ARGV specify options and input object files. As all of them could contain | |
984 | target sections, we pass them all to target compilers. */ | |
985 | ||
986 | static void | |
c713ddc0 | 987 | compile_images_for_offload_targets (unsigned in_argc, char *in_argv[], |
227a2ecf ML |
988 | vec<cl_decoded_option> compiler_opts, |
989 | vec<cl_decoded_option> linker_opts) | |
fc8b3540 IV |
990 | { |
991 | char **names = NULL; | |
992 | const char *target_names = getenv (OFFLOAD_TARGET_NAMES_ENV); | |
993 | if (!target_names) | |
994 | return; | |
995 | unsigned num_targets = parse_env_var (target_names, &names, NULL); | |
fe5bfa67 | 996 | int next_name_entry = 0; |
fc8b3540 IV |
997 | |
998 | const char *compiler_path = getenv ("COMPILER_PATH"); | |
999 | if (!compiler_path) | |
1000 | goto out; | |
1001 | ||
1002 | /* Prepare an image for each target and save the name of the resultant object | |
1003 | file to the OFFLOAD_NAMES array. It is terminated by a NULL entry. */ | |
1004 | offload_names = XCNEWVEC (char *, num_targets + 1); | |
1005 | for (unsigned i = 0; i < num_targets; i++) | |
1006 | { | |
fe5bfa67 | 1007 | offload_names[next_name_entry] |
c713ddc0 | 1008 | = compile_offload_image (names[i], compiler_path, in_argc, in_argv, |
227a2ecf | 1009 | compiler_opts, linker_opts); |
fe5bfa67 TB |
1010 | if (!offload_names[next_name_entry]) |
1011 | #if OFFLOAD_DEFAULTED | |
1012 | continue; | |
1013 | #else | |
40fecdd6 | 1014 | fatal_error (input_location, |
a9c697b8 | 1015 | "problem with building target image for %s", names[i]); |
fe5bfa67 TB |
1016 | #endif |
1017 | next_name_entry++; | |
fc8b3540 IV |
1018 | } |
1019 | ||
fe5bfa67 TB |
1020 | #if OFFLOAD_DEFAULTED |
1021 | if (next_name_entry == 0) | |
1022 | { | |
1023 | free (offload_names); | |
1024 | offload_names = NULL; | |
1025 | } | |
1026 | #endif | |
1027 | ||
fc8b3540 IV |
1028 | out: |
1029 | free_array_of_ptrs ((void **) names, num_targets); | |
1030 | } | |
1031 | ||
1032 | /* Copy a file from SRC to DEST. */ | |
1033 | ||
1034 | static void | |
1035 | copy_file (const char *dest, const char *src) | |
1036 | { | |
1037 | FILE *d = fopen (dest, "wb"); | |
1038 | FILE *s = fopen (src, "rb"); | |
1039 | char buffer[512]; | |
1040 | while (!feof (s)) | |
1041 | { | |
1042 | size_t len = fread (buffer, 1, 512, s); | |
1043 | if (ferror (s) != 0) | |
40fecdd6 | 1044 | fatal_error (input_location, "reading input file"); |
fc8b3540 IV |
1045 | if (len > 0) |
1046 | { | |
1047 | fwrite (buffer, 1, len, d); | |
1048 | if (ferror (d) != 0) | |
40fecdd6 | 1049 | fatal_error (input_location, "writing output file"); |
fc8b3540 IV |
1050 | } |
1051 | } | |
367e91e1 SL |
1052 | fclose (d); |
1053 | fclose (s); | |
fc8b3540 IV |
1054 | } |
1055 | ||
e6861a99 IV |
1056 | /* Find the crtoffloadtable.o file in LIBRARY_PATH, make copy and pass name of |
1057 | the copy to the linker. */ | |
fc8b3540 IV |
1058 | |
1059 | static void | |
7287cf18 | 1060 | find_crtoffloadtable (int save_temps, const char *dumppfx) |
fc8b3540 IV |
1061 | { |
1062 | char **paths = NULL; | |
1063 | const char *library_path = getenv ("LIBRARY_PATH"); | |
1064 | if (!library_path) | |
1065 | return; | |
e6861a99 | 1066 | unsigned n_paths = parse_env_var (library_path, &paths, "/crtoffloadtable.o"); |
fc8b3540 IV |
1067 | |
1068 | unsigned i; | |
1069 | for (i = 0; i < n_paths; i++) | |
1070 | if (access_check (paths[i], R_OK) == 0) | |
1071 | { | |
e6861a99 | 1072 | /* The linker will delete the filename we give it, so make a copy. */ |
7287cf18 TB |
1073 | char *crtoffloadtable; |
1074 | if (!save_temps) | |
1075 | crtoffloadtable = make_temp_file (".crtoffloadtable.o"); | |
1076 | else | |
8311899e | 1077 | crtoffloadtable = concat (dumppfx, "crtoffloadtable.o", NULL); |
e6861a99 IV |
1078 | copy_file (crtoffloadtable, paths[i]); |
1079 | printf ("%s\n", crtoffloadtable); | |
1080 | XDELETEVEC (crtoffloadtable); | |
fc8b3540 IV |
1081 | break; |
1082 | } | |
1083 | if (i == n_paths) | |
40fecdd6 | 1084 | fatal_error (input_location, |
a9c697b8 | 1085 | "installation error, cannot find %<crtoffloadtable.o%>"); |
fc8b3540 IV |
1086 | |
1087 | free_array_of_ptrs ((void **) paths, n_paths); | |
1088 | } | |
1089 | ||
c713ddc0 | 1090 | /* A subroutine of run_gcc. Examine the open file FD for lto sections with |
227a2ecf ML |
1091 | name prefix PREFIX, at FILE_OFFSET, and store any options we find in OPTS. |
1092 | Return true if we found a matching section, false | |
c713ddc0 BS |
1093 | otherwise. COLLECT_GCC holds the value of the environment variable with |
1094 | the same name. */ | |
1095 | ||
1096 | static bool | |
1097 | find_and_merge_options (int fd, off_t file_offset, const char *prefix, | |
227a2ecf ML |
1098 | vec<cl_decoded_option> decoded_cl_options, |
1099 | vec<cl_decoded_option> *opts, const char *collect_gcc) | |
c713ddc0 BS |
1100 | { |
1101 | off_t offset, length; | |
1102 | char *data; | |
1103 | char *fopts; | |
1104 | const char *errmsg; | |
1105 | int err; | |
227a2ecf | 1106 | vec<cl_decoded_option> fdecoded_options; |
c713ddc0 BS |
1107 | |
1108 | simple_object_read *sobj; | |
1109 | sobj = simple_object_start_read (fd, file_offset, "__GNU_LTO", | |
1110 | &errmsg, &err); | |
1111 | if (!sobj) | |
1112 | return false; | |
1113 | ||
1114 | char *secname = XALLOCAVEC (char, strlen (prefix) + sizeof (".opts")); | |
1115 | strcpy (secname, prefix); | |
1116 | strcat (secname, ".opts"); | |
1117 | if (!simple_object_find_section (sobj, secname, &offset, &length, | |
1118 | &errmsg, &err)) | |
1119 | { | |
1120 | simple_object_release_read (sobj); | |
1121 | return false; | |
1122 | } | |
1123 | ||
1124 | lseek (fd, file_offset + offset, SEEK_SET); | |
1125 | data = (char *)xmalloc (length); | |
1126 | read (fd, data, length); | |
1127 | fopts = data; | |
227a2ecf | 1128 | bool first = true; |
c713ddc0 BS |
1129 | do |
1130 | { | |
227a2ecf ML |
1131 | vec<cl_decoded_option> f2decoded_options |
1132 | = get_options_from_collect_gcc_options (collect_gcc, fopts); | |
1133 | if (first) | |
1134 | { | |
1135 | fdecoded_options = f2decoded_options; | |
1136 | first = false; | |
1137 | } | |
c713ddc0 | 1138 | else |
227a2ecf ML |
1139 | merge_and_complain (fdecoded_options, f2decoded_options, |
1140 | decoded_cl_options); | |
c713ddc0 BS |
1141 | |
1142 | fopts += strlen (fopts) + 1; | |
1143 | } | |
1144 | while (fopts - data < length); | |
1145 | ||
1146 | free (data); | |
1147 | simple_object_release_read (sobj); | |
1148 | *opts = fdecoded_options; | |
c713ddc0 BS |
1149 | return true; |
1150 | } | |
1151 | ||
1ea85365 RB |
1152 | /* Copy early debug info sections from INFILE to a new file whose name |
1153 | is returned. Return NULL on error. */ | |
1154 | ||
1155 | const char * | |
0df8dc6e | 1156 | debug_objcopy (const char *infile, bool rename) |
1ea85365 | 1157 | { |
b5327e50 | 1158 | char *outfile; |
1ea85365 RB |
1159 | const char *errmsg; |
1160 | int err; | |
1161 | ||
1162 | const char *p; | |
fe267711 | 1163 | const char *orig_infile = infile; |
1ea85365 RB |
1164 | off_t inoff = 0; |
1165 | long loffset; | |
1166 | int consumed; | |
1167 | if ((p = strrchr (infile, '@')) | |
1168 | && p != infile | |
1169 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1170 | && strlen (p) == (unsigned int) consumed) | |
1171 | { | |
1172 | char *fname = xstrdup (infile); | |
1173 | fname[p - infile] = '\0'; | |
1174 | infile = fname; | |
1175 | inoff = (off_t) loffset; | |
1176 | } | |
dc0e0c6b | 1177 | int infd = open (infile, O_RDONLY | O_BINARY); |
1ea85365 RB |
1178 | if (infd == -1) |
1179 | return NULL; | |
1180 | simple_object_read *inobj = simple_object_start_read (infd, inoff, | |
1181 | "__GNU_LTO", | |
1182 | &errmsg, &err); | |
1183 | if (!inobj) | |
1184 | return NULL; | |
1185 | ||
1186 | off_t off, len; | |
1187 | if (simple_object_find_section (inobj, ".gnu.debuglto_.debug_info", | |
1188 | &off, &len, &errmsg, &err) != 1) | |
1189 | { | |
1190 | if (errmsg) | |
a9c697b8 | 1191 | fatal_error (0, "%s: %s", errmsg, xstrerror (err)); |
1ea85365 RB |
1192 | |
1193 | simple_object_release_read (inobj); | |
1194 | close (infd); | |
1195 | return NULL; | |
1196 | } | |
1197 | ||
b5327e50 | 1198 | if (save_temps) |
1dedc12d | 1199 | outfile = concat (orig_infile, ".debug.temp.o", NULL); |
b5327e50 IS |
1200 | else |
1201 | outfile = make_temp_file (".debug.temp.o"); | |
0df8dc6e | 1202 | errmsg = simple_object_copy_lto_debug_sections (inobj, outfile, &err, rename); |
1ea85365 RB |
1203 | if (errmsg) |
1204 | { | |
1205 | unlink_if_ordinary (outfile); | |
a9c697b8 | 1206 | fatal_error (0, "%s: %s", errmsg, xstrerror (err)); |
1ea85365 RB |
1207 | } |
1208 | ||
1209 | simple_object_release_read (inobj); | |
1210 | close (infd); | |
1211 | ||
1212 | return outfile; | |
1213 | } | |
1214 | ||
b6e33d73 JH |
1215 | /* Helper for qsort: compare priorities for parallel compilation. */ |
1216 | ||
1217 | int | |
1218 | cmp_priority (const void *a, const void *b) | |
1219 | { | |
1220 | return *((const int *)b)-*((const int *)a); | |
1221 | } | |
1ea85365 | 1222 | |
f2b4f212 ML |
1223 | /* Number of CPUs that can be used for parallel LTRANS phase. */ |
1224 | ||
1225 | static unsigned long nthreads_var = 0; | |
1226 | ||
1227 | #ifdef HAVE_PTHREAD_AFFINITY_NP | |
1228 | unsigned long cpuset_size; | |
1229 | static unsigned long get_cpuset_size; | |
1230 | cpu_set_t *cpusetp; | |
1231 | ||
1232 | unsigned long | |
1233 | static cpuset_popcount (unsigned long cpusetsize, cpu_set_t *cpusetp) | |
1234 | { | |
1235 | #ifdef CPU_COUNT_S | |
1236 | /* glibc 2.7 and above provide a macro for this. */ | |
1237 | return CPU_COUNT_S (cpusetsize, cpusetp); | |
1238 | #else | |
1239 | #ifdef CPU_COUNT | |
1240 | if (cpusetsize == sizeof (cpu_set_t)) | |
1241 | /* glibc 2.6 and above provide a macro for this. */ | |
1242 | return CPU_COUNT (cpusetp); | |
1243 | #endif | |
1244 | size_t i; | |
1245 | unsigned long ret = 0; | |
1246 | STATIC_ASSERT (sizeof (cpusetp->__bits[0]) == sizeof (unsigned long int)); | |
1247 | for (i = 0; i < cpusetsize / sizeof (cpusetp->__bits[0]); i++) | |
1248 | { | |
1249 | unsigned long int mask = cpusetp->__bits[i]; | |
1250 | if (mask == 0) | |
1251 | continue; | |
1252 | ret += __builtin_popcountl (mask); | |
1253 | } | |
1254 | return ret; | |
1255 | #endif | |
1256 | } | |
1257 | #endif | |
1258 | ||
1259 | /* At startup, determine the default number of threads. It would seem | |
1260 | this should be related to the number of cpus online. */ | |
1261 | ||
1262 | static void | |
1263 | init_num_threads (void) | |
1264 | { | |
1265 | #ifdef HAVE_PTHREAD_AFFINITY_NP | |
1266 | #if defined (_SC_NPROCESSORS_CONF) && defined (CPU_ALLOC_SIZE) | |
1267 | cpuset_size = sysconf (_SC_NPROCESSORS_CONF); | |
1268 | cpuset_size = CPU_ALLOC_SIZE (cpuset_size); | |
1269 | #else | |
1270 | cpuset_size = sizeof (cpu_set_t); | |
1271 | #endif | |
1272 | ||
1273 | cpusetp = (cpu_set_t *) xmalloc (gomp_cpuset_size); | |
1274 | do | |
1275 | { | |
1276 | int ret = pthread_getaffinity_np (pthread_self (), gomp_cpuset_size, | |
1277 | cpusetp); | |
1278 | if (ret == 0) | |
1279 | { | |
1280 | /* Count only the CPUs this process can use. */ | |
1281 | nthreads_var = cpuset_popcount (cpuset_size, cpusetp); | |
1282 | if (nthreads_var == 0) | |
1283 | break; | |
1284 | get_cpuset_size = cpuset_size; | |
1285 | #ifdef CPU_ALLOC_SIZE | |
1286 | unsigned long i; | |
1287 | for (i = cpuset_size * 8; i; i--) | |
1288 | if (CPU_ISSET_S (i - 1, cpuset_size, cpusetp)) | |
1289 | break; | |
1290 | cpuset_size = CPU_ALLOC_SIZE (i); | |
1291 | #endif | |
1292 | return; | |
1293 | } | |
1294 | if (ret != EINVAL) | |
1295 | break; | |
1296 | #ifdef CPU_ALLOC_SIZE | |
1297 | if (cpuset_size < sizeof (cpu_set_t)) | |
1298 | cpuset_size = sizeof (cpu_set_t); | |
1299 | else | |
1300 | cpuset_size = cpuset_size * 2; | |
1301 | if (cpuset_size < 8 * sizeof (cpu_set_t)) | |
1302 | cpusetp | |
1303 | = (cpu_set_t *) realloc (cpusetp, cpuset_size); | |
1304 | else | |
1305 | { | |
1306 | /* Avoid fatal if too large memory allocation would be | |
1307 | requested, e.g. kernel returning EINVAL all the time. */ | |
1308 | void *p = realloc (cpusetp, cpuset_size); | |
1309 | if (p == NULL) | |
1310 | break; | |
1311 | cpusetp = (cpu_set_t *) p; | |
1312 | } | |
1313 | #else | |
1314 | break; | |
1315 | #endif | |
1316 | } | |
1317 | while (1); | |
1318 | cpuset_size = 0; | |
1319 | nthreads_var = 1; | |
1320 | free (cpusetp); | |
1321 | cpusetp = NULL; | |
1322 | #endif | |
1323 | #ifdef _SC_NPROCESSORS_ONLN | |
1324 | nthreads_var = sysconf (_SC_NPROCESSORS_ONLN); | |
1325 | #endif | |
1326 | } | |
1327 | ||
0f62caf5 | 1328 | /* Test and return reason why a jobserver cannot be detected. */ |
200b0e7e | 1329 | |
0f62caf5 | 1330 | static const char * |
200b0e7e ML |
1331 | jobserver_active_p (void) |
1332 | { | |
0f62caf5 ML |
1333 | #define JS_PREFIX "jobserver is not available: " |
1334 | #define JS_NEEDLE "--jobserver-auth=" | |
1335 | ||
200b0e7e ML |
1336 | const char *makeflags = getenv ("MAKEFLAGS"); |
1337 | if (makeflags == NULL) | |
0f62caf5 | 1338 | return JS_PREFIX "%<MAKEFLAGS%> environment variable is unset"; |
200b0e7e | 1339 | |
0f62caf5 | 1340 | const char *n = strstr (makeflags, JS_NEEDLE); |
200b0e7e | 1341 | if (n == NULL) |
0f62caf5 | 1342 | return JS_PREFIX "%<" JS_NEEDLE "%> is not present in %<MAKEFLAGS%>"; |
200b0e7e ML |
1343 | |
1344 | int rfd = -1; | |
1345 | int wfd = -1; | |
1346 | ||
0f62caf5 ML |
1347 | if (sscanf (n + strlen (JS_NEEDLE), "%d,%d", &rfd, &wfd) == 2 |
1348 | && rfd > 0 | |
1349 | && wfd > 0 | |
1350 | && is_valid_fd (rfd) | |
1351 | && is_valid_fd (wfd)) | |
1352 | return NULL; | |
1353 | else | |
1354 | return JS_PREFIX "cannot access %<" JS_NEEDLE "%> file descriptors"; | |
200b0e7e | 1355 | } |
1ea85365 | 1356 | |
7d7d925d ML |
1357 | /* Print link to -flto documentation with a hint message. */ |
1358 | ||
1359 | void | |
1360 | print_lto_docs_link () | |
1361 | { | |
1362 | const char *url = get_option_url (NULL, OPT_flto); | |
1363 | ||
1364 | pretty_printer pp; | |
1365 | pp.url_format = URL_FORMAT_DEFAULT; | |
1366 | pp_string (&pp, "see the "); | |
1367 | pp_begin_url (&pp, url); | |
1368 | pp_string (&pp, "%<-flto%> option documentation"); | |
1369 | pp_end_url (&pp); | |
1370 | pp_string (&pp, " for more information"); | |
1371 | inform (UNKNOWN_LOCATION, pp_formatted_text (&pp)); | |
1372 | } | |
1373 | ||
6fade5a6 ML |
1374 | /* Test that a make command is present and working, return true if so. */ |
1375 | ||
1376 | static bool | |
1377 | make_exists (void) | |
1378 | { | |
1379 | const char *make = "make"; | |
1380 | char **make_argv = buildargv (getenv ("MAKE")); | |
1381 | if (make_argv) | |
1382 | make = make_argv[0]; | |
1383 | const char *make_args[] = {make, "--version", NULL}; | |
1384 | ||
1385 | int exit_status = 0; | |
1386 | int err = 0; | |
1387 | const char *errmsg | |
1388 | = pex_one (PEX_SEARCH, make_args[0], CONST_CAST (char **, make_args), | |
1389 | "make", NULL, NULL, &exit_status, &err); | |
1390 | freeargv (make_argv); | |
1391 | return errmsg == NULL && exit_status == 0 && err == 0; | |
1392 | } | |
1393 | ||
d7f09764 DN |
1394 | /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */ |
1395 | ||
1396 | static void | |
1397 | run_gcc (unsigned argc, char *argv[]) | |
1398 | { | |
cf96bae7 | 1399 | unsigned i, j; |
d7f09764 DN |
1400 | const char **new_argv; |
1401 | const char **argv_ptr; | |
d7f09764 | 1402 | char *list_option_full = NULL; |
cf96bae7 | 1403 | const char *linker_output = NULL; |
f1a681a1 PK |
1404 | const char *collect_gcc; |
1405 | char *collect_gcc_options; | |
279dc7a3 | 1406 | int parallel = 0; |
a478ffff | 1407 | int jobserver = 0; |
7d7d925d | 1408 | bool jobserver_requested = false; |
200b0e7e | 1409 | int auto_parallel = 0; |
014d92e1 | 1410 | bool no_partition = false; |
7d7d925d | 1411 | const char *jobserver_error = NULL; |
227a2ecf ML |
1412 | vec<cl_decoded_option> fdecoded_options; |
1413 | fdecoded_options.create (16); | |
1414 | vec<cl_decoded_option> offload_fdecoded_options = vNULL; | |
ef6f874e RG |
1415 | struct obstack argv_obstack; |
1416 | int new_head_argc; | |
fc8b3540 IV |
1417 | bool have_lto = false; |
1418 | bool have_offload = false; | |
1ea85365 RB |
1419 | unsigned lto_argc = 0, ltoobj_argc = 0; |
1420 | char **lto_argv, **ltoobj_argv; | |
0df8dc6e | 1421 | bool linker_output_rel = false; |
1ea85365 RB |
1422 | bool skip_debug = false; |
1423 | unsigned n_debugobj; | |
efc16503 | 1424 | const char *incoming_dumppfx = dumppfx = NULL; |
1dedc12d | 1425 | static char current_dir[] = { '.', DIR_SEPARATOR, '\0' }; |
cf96bae7 RG |
1426 | |
1427 | /* Get the driver and options. */ | |
1428 | collect_gcc = getenv ("COLLECT_GCC"); | |
1429 | if (!collect_gcc) | |
40fecdd6 | 1430 | fatal_error (input_location, |
a9c697b8 | 1431 | "environment variable %<COLLECT_GCC%> must be set"); |
cf96bae7 RG |
1432 | collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS"); |
1433 | if (!collect_gcc_options) | |
40fecdd6 | 1434 | fatal_error (input_location, |
a9c697b8 | 1435 | "environment variable %<COLLECT_GCC_OPTIONS%> must be set"); |
f1a681a1 PK |
1436 | |
1437 | char *collect_as_options = getenv ("COLLECT_AS_OPTIONS"); | |
1438 | ||
1439 | /* Prepend -Xassembler to each option, and append the string | |
1440 | to collect_gcc_options. */ | |
1441 | if (collect_as_options) | |
1442 | { | |
1443 | obstack temporary_obstack; | |
1444 | obstack_init (&temporary_obstack); | |
1445 | ||
1446 | prepend_xassembler_to_collect_as_options (collect_as_options, | |
1447 | &temporary_obstack); | |
1448 | obstack_1grow (&temporary_obstack, '\0'); | |
1449 | ||
1450 | char *xassembler_opts_string | |
1451 | = XOBFINISH (&temporary_obstack, char *); | |
98ff89d1 ML |
1452 | collect_gcc_options = concat (collect_gcc_options, xassembler_opts_string, |
1453 | NULL); | |
f1a681a1 PK |
1454 | } |
1455 | ||
227a2ecf ML |
1456 | vec<cl_decoded_option> decoded_options |
1457 | = get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options); | |
cf96bae7 | 1458 | |
e6861a99 | 1459 | /* Allocate array for input object files with LTO IL, |
443743fd IV |
1460 | and for possible preceding arguments. */ |
1461 | lto_argv = XNEWVEC (char *, argc); | |
1ea85365 | 1462 | ltoobj_argv = XNEWVEC (char *, argc); |
443743fd | 1463 | |
52a35ef7 RG |
1464 | /* Look at saved options in the IL files. */ |
1465 | for (i = 1; i < argc; ++i) | |
1466 | { | |
c713ddc0 | 1467 | char *p; |
52a35ef7 | 1468 | int fd; |
c713ddc0 | 1469 | off_t file_offset = 0; |
52a35ef7 | 1470 | long loffset; |
52a35ef7 | 1471 | int consumed; |
52a35ef7 | 1472 | char *filename = argv[i]; |
c713ddc0 | 1473 | |
6ba3079d | 1474 | if (startswith (argv[i], "-foffload-objects=")) |
e6861a99 IV |
1475 | { |
1476 | have_offload = true; | |
1477 | offload_objects_file_name | |
1478 | = argv[i] + sizeof ("-foffload-objects=") - 1; | |
1479 | continue; | |
1480 | } | |
1481 | ||
52a35ef7 RG |
1482 | if ((p = strrchr (argv[i], '@')) |
1483 | && p != argv[i] | |
1484 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1485 | && strlen (p) == (unsigned int) consumed) | |
1486 | { | |
1487 | filename = XNEWVEC (char, p - argv[i] + 1); | |
1488 | memcpy (filename, argv[i], p - argv[i]); | |
1489 | filename[p - argv[i]] = '\0'; | |
1490 | file_offset = (off_t) loffset; | |
1491 | } | |
1473ab9a | 1492 | fd = open (filename, O_RDONLY | O_BINARY); |
0df8dc6e JH |
1493 | /* Linker plugin passes -fresolution and -flinker-output options. |
1494 | -flinker-output is passed only when user did not specify one and thus | |
1495 | we do not need to worry about duplicities with the option handling | |
1496 | below. */ | |
52a35ef7 | 1497 | if (fd == -1) |
443743fd IV |
1498 | { |
1499 | lto_argv[lto_argc++] = argv[i]; | |
0df8dc6e JH |
1500 | if (strcmp (argv[i], "-flinker-output=rel") == 0) |
1501 | linker_output_rel = true; | |
443743fd IV |
1502 | continue; |
1503 | } | |
1504 | ||
1505 | if (find_and_merge_options (fd, file_offset, LTO_SECTION_NAME_PREFIX, | |
227a2ecf | 1506 | decoded_options, &fdecoded_options, |
443743fd IV |
1507 | collect_gcc)) |
1508 | { | |
1509 | have_lto = true; | |
1ea85365 | 1510 | ltoobj_argv[ltoobj_argc++] = argv[i]; |
443743fd | 1511 | } |
52a35ef7 RG |
1512 | close (fd); |
1513 | } | |
1514 | ||
cf96bae7 | 1515 | /* Initalize the common arguments for the driver. */ |
ef6f874e RG |
1516 | obstack_init (&argv_obstack); |
1517 | obstack_ptr_grow (&argv_obstack, collect_gcc); | |
1518 | obstack_ptr_grow (&argv_obstack, "-xlto"); | |
1519 | obstack_ptr_grow (&argv_obstack, "-c"); | |
52a35ef7 | 1520 | |
227a2ecf ML |
1521 | append_compiler_options (&argv_obstack, fdecoded_options); |
1522 | append_linker_options (&argv_obstack, decoded_options); | |
52a35ef7 | 1523 | |
c713ddc0 | 1524 | /* Scan linker driver arguments for things that are of relevance to us. */ |
227a2ecf | 1525 | for (j = 1; j < decoded_options.length (); ++j) |
f31c0018 | 1526 | { |
227a2ecf | 1527 | cl_decoded_option *option = &decoded_options[j]; |
f31c0018 RG |
1528 | switch (option->opt_index) |
1529 | { | |
1530 | case OPT_o: | |
1531 | linker_output = option->arg; | |
c713ddc0 | 1532 | break; |
f31c0018 | 1533 | |
1dedc12d AO |
1534 | /* We don't have to distinguish between -save-temps=* and |
1535 | -save-temps, -dumpdir already carries that | |
1536 | information. */ | |
1537 | case OPT_save_temps_: | |
f31c0018 | 1538 | case OPT_save_temps: |
608508a6 | 1539 | save_temps = 1; |
f31c0018 RG |
1540 | break; |
1541 | ||
1542 | case OPT_v: | |
cf96bae7 | 1543 | verbose = 1; |
f31c0018 | 1544 | break; |
cf96bae7 | 1545 | |
783dab6b RB |
1546 | case OPT_flto_partition_: |
1547 | if (strcmp (option->arg, "none") == 0) | |
1548 | no_partition = true; | |
f31c0018 RG |
1549 | break; |
1550 | ||
1551 | case OPT_flto_: | |
3cbcb5d0 ML |
1552 | /* Merge linker -flto= option with what we have in IL files. */ |
1553 | merge_flto_options (fdecoded_options, option); | |
f31c0018 | 1554 | if (strcmp (option->arg, "jobserver") == 0) |
3835aa0e | 1555 | jobserver_requested = true; |
c713ddc0 | 1556 | break; |
8bb50e5c | 1557 | |
0df8dc6e JH |
1558 | case OPT_flinker_output_: |
1559 | linker_output_rel = !strcmp (option->arg, "rel"); | |
1560 | break; | |
1561 | ||
5a307ee5 RB |
1562 | case OPT_g: |
1563 | /* Recognize -g0. */ | |
1564 | skip_debug = option->arg && !strcmp (option->arg, "0"); | |
1565 | break; | |
0df8dc6e | 1566 | |
1dedc12d AO |
1567 | case OPT_dumpdir: |
1568 | incoming_dumppfx = dumppfx = option->arg; | |
1569 | break; | |
1570 | ||
f31c0018 RG |
1571 | default: |
1572 | break; | |
1573 | } | |
f31c0018 RG |
1574 | } |
1575 | ||
3cbcb5d0 ML |
1576 | /* Process LTO-related options on merged options. */ |
1577 | for (j = 1; j < fdecoded_options.length (); ++j) | |
1578 | { | |
1579 | cl_decoded_option *option = &fdecoded_options[j]; | |
1580 | switch (option->opt_index) | |
1581 | { | |
1582 | case OPT_flto_: | |
1583 | if (strcmp (option->arg, "jobserver") == 0) | |
1584 | { | |
1585 | parallel = 1; | |
1586 | jobserver = 1; | |
1587 | } | |
1588 | else if (strcmp (option->arg, "auto") == 0) | |
1589 | { | |
1590 | parallel = 1; | |
1591 | auto_parallel = 1; | |
1592 | } | |
1593 | else | |
1594 | { | |
1595 | parallel = atoi (option->arg); | |
1596 | if (parallel <= 1) | |
1597 | parallel = 0; | |
1598 | } | |
1599 | /* Fallthru. */ | |
1600 | ||
1601 | case OPT_flto: | |
1602 | lto_mode = LTO_MODE_WHOPR; | |
1603 | break; | |
1604 | } | |
1605 | } | |
1606 | ||
1ea85365 RB |
1607 | /* Output lto-wrapper invocation command. */ |
1608 | if (verbose) | |
1609 | { | |
1610 | for (i = 0; i < argc; ++i) | |
1611 | { | |
1612 | fputs (argv[i], stderr); | |
1613 | fputc (' ', stderr); | |
1614 | } | |
1615 | fputc ('\n', stderr); | |
1616 | } | |
1617 | ||
0df8dc6e JH |
1618 | if (linker_output_rel) |
1619 | no_partition = true; | |
1620 | ||
014d92e1 JH |
1621 | if (no_partition) |
1622 | { | |
1623 | lto_mode = LTO_MODE_LTO; | |
1624 | jobserver = 0; | |
7d7d925d | 1625 | jobserver_requested = false; |
200b0e7e | 1626 | auto_parallel = 0; |
014d92e1 JH |
1627 | parallel = 0; |
1628 | } | |
0f62caf5 | 1629 | else |
19566bdd | 1630 | { |
7d7d925d | 1631 | jobserver_error = jobserver_active_p (); |
0f62caf5 | 1632 | if (jobserver && jobserver_error != NULL) |
11929537 | 1633 | { |
7f9b7ccf | 1634 | /* Fall back to auto parallelism. */ |
11929537 | 1635 | jobserver = 0; |
7f9b7ccf | 1636 | auto_parallel = 1; |
11929537 | 1637 | } |
0f62caf5 ML |
1638 | else if (!jobserver && jobserver_error == NULL) |
1639 | { | |
1640 | parallel = 1; | |
1641 | jobserver = 1; | |
1642 | } | |
19566bdd | 1643 | } |
cf96bae7 | 1644 | |
6fade5a6 ML |
1645 | /* We need make working for a parallel execution. */ |
1646 | if (parallel && !make_exists ()) | |
1647 | parallel = 0; | |
1648 | ||
1dedc12d | 1649 | if (!dumppfx) |
cf96bae7 | 1650 | { |
1dedc12d AO |
1651 | if (!linker_output |
1652 | || strcmp (linker_output, HOST_BIT_BUCKET) == 0) | |
1653 | dumppfx = "a."; | |
1654 | else | |
dd3b31fb | 1655 | { |
1dedc12d AO |
1656 | const char *obase = lbasename (linker_output), *temp; |
1657 | ||
1658 | /* Strip the executable extension. */ | |
1659 | size_t blen = strlen (obase), xlen; | |
1660 | if ((temp = strrchr (obase + 1, '.')) | |
1661 | && (xlen = strlen (temp)) | |
1662 | && (strcmp (temp, ".exe") == 0 | |
1663 | #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) | |
1664 | || strcmp (temp, TARGET_EXECUTABLE_SUFFIX) == 0 | |
1665 | #endif | |
1666 | || strcmp (obase, "a.out") == 0)) | |
1667 | dumppfx = xstrndup (linker_output, | |
1668 | obase - linker_output + blen - xlen + 1); | |
1669 | else | |
1670 | dumppfx = concat (linker_output, ".", NULL); | |
dd3b31fb | 1671 | } |
1dedc12d | 1672 | } |
cf96bae7 | 1673 | |
1dedc12d AO |
1674 | /* If there's no directory component in the dumppfx, add one, so |
1675 | that, when it is used as -dumpbase, it overrides any occurrence | |
1676 | of -dumpdir that might have been passed in. */ | |
1677 | if (!dumppfx || lbasename (dumppfx) == dumppfx) | |
1678 | dumppfx = concat (current_dir, dumppfx, NULL); | |
1679 | ||
1680 | /* Make sure some -dumpdir is passed, so as to get predictable | |
1681 | -dumpbase overriding semantics. If we got an incoming -dumpdir | |
1682 | argument, we'll pass it on, so don't bother with another one | |
1683 | then. */ | |
1684 | if (!incoming_dumppfx) | |
1685 | { | |
1686 | obstack_ptr_grow (&argv_obstack, "-dumpdir"); | |
1687 | obstack_ptr_grow (&argv_obstack, ""); | |
cf96bae7 | 1688 | } |
1dedc12d | 1689 | obstack_ptr_grow (&argv_obstack, "-dumpbase"); |
ef6f874e RG |
1690 | |
1691 | /* Remember at which point we can scrub args to re-use the commons. */ | |
1692 | new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *); | |
d7f09764 | 1693 | |
fc8b3540 IV |
1694 | if (have_offload) |
1695 | { | |
e6861a99 IV |
1696 | unsigned i, num_offload_files; |
1697 | char **offload_argv; | |
1698 | FILE *f; | |
1699 | ||
1700 | f = fopen (offload_objects_file_name, "r"); | |
1701 | if (f == NULL) | |
1702 | fatal_error (input_location, "cannot open %s: %m", | |
1703 | offload_objects_file_name); | |
1704 | if (fscanf (f, "%u ", &num_offload_files) != 1) | |
1705 | fatal_error (input_location, "cannot read %s: %m", | |
1706 | offload_objects_file_name); | |
1707 | offload_argv = XCNEWVEC (char *, num_offload_files); | |
1708 | ||
1709 | /* Read names of object files with offload. */ | |
1710 | for (i = 0; i < num_offload_files; i++) | |
1711 | { | |
1712 | const unsigned piece = 32; | |
1713 | char *buf, *filename = XNEWVEC (char, piece); | |
1714 | size_t len; | |
1715 | ||
1716 | buf = filename; | |
1717 | cont1: | |
1718 | if (!fgets (buf, piece, f)) | |
1719 | break; | |
1720 | len = strlen (filename); | |
1721 | if (filename[len - 1] != '\n') | |
1722 | { | |
1723 | filename = XRESIZEVEC (char, filename, len + piece); | |
1724 | buf = filename + len; | |
1725 | goto cont1; | |
1726 | } | |
1727 | filename[len - 1] = '\0'; | |
1728 | offload_argv[i] = filename; | |
1729 | } | |
1730 | fclose (f); | |
1731 | if (offload_argv[num_offload_files - 1] == NULL) | |
1732 | fatal_error (input_location, "invalid format of %s", | |
1733 | offload_objects_file_name); | |
1734 | maybe_unlink (offload_objects_file_name); | |
1735 | offload_objects_file_name = NULL; | |
1736 | ||
1737 | /* Look at saved offload options in files. */ | |
1738 | for (i = 0; i < num_offload_files; i++) | |
1739 | { | |
1740 | char *p; | |
1741 | long loffset; | |
1742 | int fd, consumed; | |
1743 | off_t file_offset = 0; | |
1744 | char *filename = offload_argv[i]; | |
1745 | ||
1746 | if ((p = strrchr (offload_argv[i], '@')) | |
1747 | && p != offload_argv[i] | |
1748 | && sscanf (p, "@%li%n", &loffset, &consumed) >= 1 | |
1749 | && strlen (p) == (unsigned int) consumed) | |
1750 | { | |
1751 | filename = XNEWVEC (char, p - offload_argv[i] + 1); | |
1752 | memcpy (filename, offload_argv[i], p - offload_argv[i]); | |
1753 | filename[p - offload_argv[i]] = '\0'; | |
1754 | file_offset = (off_t) loffset; | |
1755 | } | |
1756 | fd = open (filename, O_RDONLY | O_BINARY); | |
1757 | if (fd == -1) | |
1758 | fatal_error (input_location, "cannot open %s: %m", filename); | |
1759 | if (!find_and_merge_options (fd, file_offset, | |
1760 | OFFLOAD_SECTION_NAME_PREFIX, | |
227a2ecf | 1761 | decoded_options, &offload_fdecoded_options, |
e6861a99 IV |
1762 | collect_gcc)) |
1763 | fatal_error (input_location, "cannot read %s: %m", filename); | |
1764 | close (fd); | |
1765 | if (filename != offload_argv[i]) | |
1766 | XDELETEVEC (filename); | |
1767 | } | |
1768 | ||
1769 | compile_images_for_offload_targets (num_offload_files, offload_argv, | |
227a2ecf | 1770 | offload_fdecoded_options, decoded_options); |
e6861a99 IV |
1771 | |
1772 | free_array_of_ptrs ((void **) offload_argv, num_offload_files); | |
1773 | ||
fc8b3540 IV |
1774 | if (offload_names) |
1775 | { | |
7287cf18 | 1776 | find_crtoffloadtable (save_temps, dumppfx); |
fc8b3540 IV |
1777 | for (i = 0; offload_names[i]; i++) |
1778 | printf ("%s\n", offload_names[i]); | |
1779 | free_array_of_ptrs ((void **) offload_names, i); | |
1780 | } | |
1781 | } | |
1782 | ||
fc8b3540 IV |
1783 | /* If object files contain offload sections, but do not contain LTO sections, |
1784 | then there is no need to perform a link-time recompilation, i.e. | |
1785 | lto-wrapper is used only for a compilation of offload images. */ | |
1786 | if (have_offload && !have_lto) | |
e6861a99 | 1787 | goto finish; |
fc8b3540 | 1788 | |
d7f09764 DN |
1789 | if (lto_mode == LTO_MODE_LTO) |
1790 | { | |
1dedc12d AO |
1791 | /* -dumpbase argument for LTO. */ |
1792 | flto_out = concat (dumppfx, "lto.o", NULL); | |
1793 | obstack_ptr_grow (&argv_obstack, flto_out); | |
1794 | ||
1795 | if (!save_temps) | |
b5327e50 | 1796 | flto_out = make_temp_file (".lto.o"); |
ef6f874e RG |
1797 | obstack_ptr_grow (&argv_obstack, "-o"); |
1798 | obstack_ptr_grow (&argv_obstack, flto_out); | |
d7f09764 | 1799 | } |
be286227 | 1800 | else |
d7f09764 DN |
1801 | { |
1802 | const char *list_option = "-fltrans-output-list="; | |
d7f09764 | 1803 | |
1dedc12d AO |
1804 | /* -dumpbase argument for WPA. */ |
1805 | char *dumpbase = concat (dumppfx, "wpa", NULL); | |
1806 | obstack_ptr_grow (&argv_obstack, dumpbase); | |
cf96bae7 | 1807 | |
1dedc12d AO |
1808 | if (save_temps) |
1809 | ltrans_output_file = concat (dumppfx, "ltrans.out", NULL); | |
b5611987 | 1810 | else |
1dedc12d AO |
1811 | ltrans_output_file = make_temp_file (".ltrans.out"); |
1812 | list_option_full = concat (list_option, ltrans_output_file, NULL); | |
1813 | obstack_ptr_grow (&argv_obstack, list_option_full); | |
d7f09764 | 1814 | |
f300e7b8 | 1815 | if (jobserver) |
200b0e7e ML |
1816 | { |
1817 | if (verbose) | |
1818 | fprintf (stderr, "Using make jobserver\n"); | |
1819 | obstack_ptr_grow (&argv_obstack, xstrdup ("-fwpa=jobserver")); | |
1820 | } | |
1821 | else if (auto_parallel) | |
1822 | { | |
1823 | char buf[256]; | |
f2b4f212 | 1824 | init_num_threads (); |
cab1b0eb ML |
1825 | if (nthreads_var == 0) |
1826 | nthreads_var = 1; | |
200b0e7e ML |
1827 | if (verbose) |
1828 | fprintf (stderr, "LTO parallelism level set to %ld\n", | |
1829 | nthreads_var); | |
1830 | sprintf (buf, "-fwpa=%ld", nthreads_var); | |
1831 | obstack_ptr_grow (&argv_obstack, xstrdup (buf)); | |
1832 | } | |
f300e7b8 JH |
1833 | else if (parallel > 1) |
1834 | { | |
1835 | char buf[256]; | |
1836 | sprintf (buf, "-fwpa=%i", parallel); | |
1837 | obstack_ptr_grow (&argv_obstack, xstrdup (buf)); | |
1838 | } | |
1839 | else | |
1840 | obstack_ptr_grow (&argv_obstack, "-fwpa"); | |
d7f09764 | 1841 | } |
d7f09764 | 1842 | |
1ea85365 | 1843 | /* Append input arguments. */ |
443743fd IV |
1844 | for (i = 0; i < lto_argc; ++i) |
1845 | obstack_ptr_grow (&argv_obstack, lto_argv[i]); | |
1ea85365 RB |
1846 | /* Append the input objects. */ |
1847 | for (i = 0; i < ltoobj_argc; ++i) | |
1848 | obstack_ptr_grow (&argv_obstack, ltoobj_argv[i]); | |
ef6f874e | 1849 | obstack_ptr_grow (&argv_obstack, NULL); |
d7f09764 | 1850 | |
ef6f874e RG |
1851 | new_argv = XOBFINISH (&argv_obstack, const char **); |
1852 | argv_ptr = &new_argv[new_head_argc]; | |
b3032d1b TB |
1853 | fork_execute (new_argv[0], CONST_CAST (char **, new_argv), true, |
1854 | "ltrans_args"); | |
48cf395b | 1855 | |
1ea85365 RB |
1856 | /* Copy the early generated debug info from the objects to temporary |
1857 | files and append those to the partial link commandline. */ | |
1858 | n_debugobj = 0; | |
b5327e50 | 1859 | early_debug_object_names = NULL; |
1ea85365 | 1860 | if (! skip_debug) |
1ea85365 | 1861 | { |
b5327e50 IS |
1862 | early_debug_object_names = XCNEWVEC (const char *, ltoobj_argc+ 1); |
1863 | num_deb_objs = ltoobj_argc; | |
1864 | for (i = 0; i < ltoobj_argc; ++i) | |
1ea85365 | 1865 | { |
b5327e50 IS |
1866 | const char *tem; |
1867 | if ((tem = debug_objcopy (ltoobj_argv[i], !linker_output_rel))) | |
1868 | { | |
1869 | early_debug_object_names[i] = tem; | |
1870 | n_debugobj++; | |
1871 | } | |
1ea85365 RB |
1872 | } |
1873 | } | |
1ea85365 | 1874 | |
d7f09764 DN |
1875 | if (lto_mode == LTO_MODE_LTO) |
1876 | { | |
c3284718 | 1877 | printf ("%s\n", flto_out); |
1ea85365 RB |
1878 | if (!skip_debug) |
1879 | { | |
b5327e50 IS |
1880 | for (i = 0; i < ltoobj_argc; ++i) |
1881 | if (early_debug_object_names[i] != NULL) | |
1882 | printf ("%s\n", early_debug_object_names[i]); | |
1ea85365 | 1883 | } |
b5327e50 | 1884 | /* These now belong to collect2. */ |
d7f09764 DN |
1885 | free (flto_out); |
1886 | flto_out = NULL; | |
b5327e50 IS |
1887 | free (early_debug_object_names); |
1888 | early_debug_object_names = NULL; | |
d7f09764 | 1889 | } |
be286227 | 1890 | else |
d7f09764 DN |
1891 | { |
1892 | FILE *stream = fopen (ltrans_output_file, "r"); | |
c04b6b38 | 1893 | FILE *mstream = NULL; |
4559db79 | 1894 | struct obstack env_obstack; |
b6e33d73 | 1895 | int priority; |
d7f09764 DN |
1896 | |
1897 | if (!stream) | |
a9c697b8 | 1898 | fatal_error (input_location, "%<fopen%>: %s: %m", ltrans_output_file); |
d7f09764 | 1899 | |
50ee30d5 | 1900 | /* Parse the list of LTRANS inputs from the WPA stage. */ |
4559db79 | 1901 | obstack_init (&env_obstack); |
50ee30d5 | 1902 | nr = 0; |
48cf395b RB |
1903 | for (;;) |
1904 | { | |
1905 | const unsigned piece = 32; | |
50ee30d5 | 1906 | char *output_name = NULL; |
48cf395b RB |
1907 | char *buf, *input_name = (char *)xmalloc (piece); |
1908 | size_t len; | |
1909 | ||
1910 | buf = input_name; | |
b6e33d73 JH |
1911 | if (fscanf (stream, "%i\n", &priority) != 1) |
1912 | { | |
1913 | if (!feof (stream)) | |
1914 | fatal_error (input_location, | |
a9c697b8 | 1915 | "corrupted ltrans output file %s", |
b6e33d73 JH |
1916 | ltrans_output_file); |
1917 | break; | |
1918 | } | |
48cf395b RB |
1919 | cont: |
1920 | if (!fgets (buf, piece, stream)) | |
1921 | break; | |
1922 | len = strlen (input_name); | |
1923 | if (input_name[len - 1] != '\n') | |
1924 | { | |
1925 | input_name = (char *)xrealloc (input_name, len + piece); | |
1926 | buf = input_name + len; | |
1927 | goto cont; | |
1928 | } | |
1929 | input_name[len - 1] = '\0'; | |
1930 | ||
1931 | if (input_name[0] == '*') | |
c04b6b38 | 1932 | output_name = &input_name[1]; |
48cf395b | 1933 | |
c04b6b38 | 1934 | nr++; |
b6e33d73 JH |
1935 | ltrans_priorities |
1936 | = (int *)xrealloc (ltrans_priorities, nr * sizeof (int) * 2); | |
c04b6b38 RG |
1937 | input_names = (char **)xrealloc (input_names, nr * sizeof (char *)); |
1938 | output_names = (char **)xrealloc (output_names, nr * sizeof (char *)); | |
b6e33d73 JH |
1939 | ltrans_priorities[(nr-1)*2] = priority; |
1940 | ltrans_priorities[(nr-1)*2+1] = nr-1; | |
c04b6b38 RG |
1941 | input_names[nr-1] = input_name; |
1942 | output_names[nr-1] = output_name; | |
1943 | } | |
50ee30d5 | 1944 | fclose (stream); |
a185856a | 1945 | maybe_unlink (ltrans_output_file); |
50ee30d5 RG |
1946 | ltrans_output_file = NULL; |
1947 | ||
7d7d925d ML |
1948 | if (nr > 1) |
1949 | { | |
1950 | if (jobserver_requested && jobserver_error != NULL) | |
1951 | { | |
1952 | warning (0, jobserver_error); | |
1953 | print_lto_docs_link (); | |
1954 | } | |
1955 | else if (parallel == 0) | |
1956 | { | |
1957 | warning (0, "using serial compilation of %d LTRANS jobs", nr); | |
1958 | print_lto_docs_link (); | |
1959 | } | |
1960 | } | |
1961 | ||
50ee30d5 RG |
1962 | if (parallel) |
1963 | { | |
1964 | makefile = make_temp_file (".mk"); | |
1965 | mstream = fopen (makefile, "w"); | |
b6e33d73 | 1966 | qsort (ltrans_priorities, nr, sizeof (int) * 2, cmp_priority); |
50ee30d5 RG |
1967 | } |
1968 | ||
1969 | /* Execute the LTRANS stage for each input file (or prepare a | |
1970 | makefile to invoke this in parallel). */ | |
1971 | for (i = 0; i < nr; ++i) | |
1972 | { | |
1973 | char *output_name; | |
1974 | char *input_name = input_names[i]; | |
1975 | /* If it's a pass-through file do nothing. */ | |
1976 | if (output_names[i]) | |
1977 | continue; | |
1978 | ||
1979 | /* Replace the .o suffix with a .ltrans.o suffix and write | |
1980 | the resulting name to the LTRANS output list. */ | |
50ee30d5 RG |
1981 | obstack_grow (&env_obstack, input_name, strlen (input_name) - 2); |
1982 | obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o")); | |
1983 | output_name = XOBFINISH (&env_obstack, char *); | |
1984 | ||
1985 | /* Adjust the dumpbase if the linker output file was seen. */ | |
1dedc12d AO |
1986 | int dumpbase_len = (strlen (dumppfx) + sizeof (DUMPBASE_SUFFIX)); |
1987 | char *dumpbase = (char *) xmalloc (dumpbase_len + 1); | |
1988 | snprintf (dumpbase, dumpbase_len, "%sltrans%u.ltrans", dumppfx, i); | |
1989 | argv_ptr[0] = dumpbase; | |
50ee30d5 RG |
1990 | |
1991 | argv_ptr[1] = "-fltrans"; | |
1992 | argv_ptr[2] = "-o"; | |
1993 | argv_ptr[3] = output_name; | |
1994 | argv_ptr[4] = input_name; | |
1995 | argv_ptr[5] = NULL; | |
1996 | if (parallel) | |
1997 | { | |
1998 | fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]); | |
1999 | for (j = 1; new_argv[j] != NULL; ++j) | |
2000 | fprintf (mstream, " '%s'", new_argv[j]); | |
2001 | fprintf (mstream, "\n"); | |
9d69847d RG |
2002 | /* If we are not preserving the ltrans input files then |
2003 | truncate them as soon as we have processed it. This | |
2004 | reduces temporary disk-space usage. */ | |
608508a6 | 2005 | if (! save_temps) |
9d69847d RG |
2006 | fprintf (mstream, "\t@-touch -r %s %s.tem > /dev/null 2>&1 " |
2007 | "&& mv %s.tem %s\n", | |
2008 | input_name, input_name, input_name, input_name); | |
50ee30d5 RG |
2009 | } |
2010 | else | |
9d69847d | 2011 | { |
b3032d1b TB |
2012 | char argsuffix[sizeof (DUMPBASE_SUFFIX) + 1]; |
2013 | if (save_temps) | |
6267bb7a | 2014 | snprintf (argsuffix, sizeof (DUMPBASE_SUFFIX), |
b3032d1b | 2015 | "ltrans%u.ltrans_args", i); |
5f0ad6a5 | 2016 | fork_execute (new_argv[0], CONST_CAST (char **, new_argv), |
b3032d1b | 2017 | true, save_temps ? argsuffix : NULL); |
a185856a | 2018 | maybe_unlink (input_name); |
9d69847d | 2019 | } |
50ee30d5 RG |
2020 | |
2021 | output_names[i] = output_name; | |
2022 | } | |
c04b6b38 RG |
2023 | if (parallel) |
2024 | { | |
2025 | struct pex_obj *pex; | |
2026 | char jobs[32]; | |
a478ffff | 2027 | |
bb52a82a RB |
2028 | fprintf (mstream, |
2029 | ".PHONY: all\n" | |
2030 | "all:"); | |
c04b6b38 | 2031 | for (i = 0; i < nr; ++i) |
b6e33d73 JH |
2032 | { |
2033 | int j = ltrans_priorities[i*2 + 1]; | |
2034 | fprintf (mstream, " \\\n\t%s", output_names[j]); | |
2035 | } | |
c04b6b38 RG |
2036 | fprintf (mstream, "\n"); |
2037 | fclose (mstream); | |
a478ffff AK |
2038 | if (!jobserver) |
2039 | { | |
2040 | /* Avoid passing --jobserver-fd= and similar flags | |
2041 | unless jobserver mode is explicitly enabled. */ | |
2042 | putenv (xstrdup ("MAKEFLAGS=")); | |
2043 | putenv (xstrdup ("MFLAGS=")); | |
2044 | } | |
b24fc8a6 ML |
2045 | |
2046 | char **make_argv = buildargv (getenv ("MAKE")); | |
2047 | if (make_argv) | |
2048 | { | |
2049 | for (unsigned argc = 0; make_argv[argc]; argc++) | |
2050 | obstack_ptr_grow (&argv_obstack, make_argv[argc]); | |
2051 | } | |
2052 | else | |
2053 | obstack_ptr_grow (&argv_obstack, "make"); | |
2054 | ||
2055 | obstack_ptr_grow (&argv_obstack, "-f"); | |
2056 | obstack_ptr_grow (&argv_obstack, makefile); | |
a478ffff AK |
2057 | if (!jobserver) |
2058 | { | |
200b0e7e ML |
2059 | snprintf (jobs, 31, "-j%ld", |
2060 | auto_parallel ? nthreads_var : parallel); | |
b24fc8a6 | 2061 | obstack_ptr_grow (&argv_obstack, jobs); |
a478ffff | 2062 | } |
b24fc8a6 ML |
2063 | obstack_ptr_grow (&argv_obstack, "all"); |
2064 | obstack_ptr_grow (&argv_obstack, NULL); | |
2065 | new_argv = XOBFINISH (&argv_obstack, const char **); | |
2066 | ||
5f0ad6a5 | 2067 | pex = collect_execute (new_argv[0], CONST_CAST (char **, new_argv), |
b3032d1b | 2068 | NULL, NULL, PEX_SEARCH, false, NULL); |
5f0ad6a5 | 2069 | do_wait (new_argv[0], pex); |
b24fc8a6 | 2070 | freeargv (make_argv); |
a185856a | 2071 | maybe_unlink (makefile); |
50ee30d5 | 2072 | makefile = NULL; |
9d69847d | 2073 | for (i = 0; i < nr; ++i) |
a185856a | 2074 | maybe_unlink (input_names[i]); |
c04b6b38 RG |
2075 | } |
2076 | for (i = 0; i < nr; ++i) | |
2077 | { | |
2078 | fputs (output_names[i], stdout); | |
48cf395b | 2079 | putc ('\n', stdout); |
c04b6b38 | 2080 | free (input_names[i]); |
48cf395b | 2081 | } |
b5327e50 IS |
2082 | if (!skip_debug) |
2083 | { | |
2084 | for (i = 0; i < ltoobj_argc; ++i) | |
2085 | if (early_debug_object_names[i] != NULL) | |
2086 | printf ("%s\n", early_debug_object_names[i]); | |
2087 | } | |
50ee30d5 | 2088 | nr = 0; |
b6e33d73 | 2089 | free (ltrans_priorities); |
c04b6b38 | 2090 | free (output_names); |
b5327e50 IS |
2091 | output_names = NULL; |
2092 | free (early_debug_object_names); | |
2093 | early_debug_object_names = NULL; | |
c04b6b38 | 2094 | free (input_names); |
d7f09764 | 2095 | free (list_option_full); |
4559db79 | 2096 | obstack_free (&env_obstack, NULL); |
d7f09764 | 2097 | } |
ef6f874e | 2098 | |
fc8b3540 | 2099 | finish: |
443743fd | 2100 | XDELETE (lto_argv); |
ef6f874e | 2101 | obstack_free (&argv_obstack, NULL); |
d7f09764 DN |
2102 | } |
2103 | ||
2104 | ||
2105 | /* Entry point. */ | |
2106 | ||
2107 | int | |
2108 | main (int argc, char *argv[]) | |
2109 | { | |
2691e6d7 JM |
2110 | const char *p; |
2111 | ||
de5672fc | 2112 | init_opts_obstack (); |
dc357798 | 2113 | |
2691e6d7 JM |
2114 | p = argv[0] + strlen (argv[0]); |
2115 | while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1])) | |
2116 | --p; | |
2117 | progname = p; | |
2118 | ||
2119 | xmalloc_set_program_name (progname); | |
2120 | ||
d7f09764 DN |
2121 | gcc_init_libintl (); |
2122 | ||
2691e6d7 JM |
2123 | diagnostic_initialize (global_dc, 0); |
2124 | ||
877088b7 | 2125 | if (atexit (lto_wrapper_cleanup) != 0) |
a9c697b8 | 2126 | fatal_error (input_location, "%<atexit%> failed"); |
877088b7 | 2127 | |
2dc6782a | 2128 | setup_signals (); |
396717c9 | 2129 | |
d7f09764 DN |
2130 | /* We may be called with all the arguments stored in some file and |
2131 | passed with @file. Expand them into argv before processing. */ | |
2132 | expandargv (&argc, &argv); | |
f31c0018 | 2133 | |
cf96bae7 | 2134 | run_gcc (argc, argv); |
d7f09764 DN |
2135 | |
2136 | return 0; | |
2137 | } |