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