1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
42 #include "coretypes.h"
44 #include "diagnostic.h"
48 #include "simple-object.h"
49 #include "lto-section-names.h"
50 #include "collect-utils.h"
52 #define OFFLOAD_TARGET_NAMES_ENV "OFFLOAD_TARGET_NAMES"
55 LTO_MODE_NONE
, /* Not doing LTO. */
56 LTO_MODE_LTO
, /* Normal LTO. */
57 LTO_MODE_WHOPR
/* WHOPR. */
60 /* Current LTO mode. */
61 static enum lto_mode_d lto_mode
= LTO_MODE_NONE
;
63 static char *ltrans_output_file
;
64 static char *flto_out
;
65 static unsigned int nr
;
66 static char **input_names
;
67 static char **output_names
;
68 static char **offload_names
;
69 static const char *ompbegin
, *ompend
;
70 static char *makefile
;
72 const char tool_name
[] = "lto-wrapper";
74 /* Delete tempfiles. Called from utils_cleanup. */
81 if (ltrans_output_file
)
82 maybe_unlink (ltrans_output_file
);
84 maybe_unlink (flto_out
);
86 maybe_unlink (makefile
);
87 for (i
= 0; i
< nr
; ++i
)
89 maybe_unlink (input_names
[i
]);
91 maybe_unlink (output_names
[i
]);
96 lto_wrapper_cleanup (void)
98 utils_cleanup (false);
101 /* Unlink a temporary LTRANS file unless requested otherwise. */
104 maybe_unlink (const char *file
)
108 if (unlink_if_ordinary (file
)
110 fatal_error ("deleting LTRANS file %s: %m", file
);
113 fprintf (stderr
, "[Leaving LTRANS %s]\n", file
);
116 /* Template of LTRANS dumpbase suffix. */
117 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
119 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
120 environment according to LANG_MASK. */
123 get_options_from_collect_gcc_options (const char *collect_gcc
,
124 const char *collect_gcc_options
,
125 unsigned int lang_mask
,
126 struct cl_decoded_option
**decoded_options
,
127 unsigned int *decoded_options_count
)
129 struct obstack argv_obstack
;
134 argv_storage
= xstrdup (collect_gcc_options
);
135 obstack_init (&argv_obstack
);
136 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
138 for (j
= 0, k
= 0; argv_storage
[j
] != '\0'; ++j
)
140 if (argv_storage
[j
] == '\'')
142 obstack_ptr_grow (&argv_obstack
, &argv_storage
[k
]);
146 if (argv_storage
[j
] == '\0')
147 fatal_error ("malformed COLLECT_GCC_OPTIONS");
148 else if (strncmp (&argv_storage
[j
], "'\\''", 4) == 0)
150 argv_storage
[k
++] = '\'';
153 else if (argv_storage
[j
] == '\'')
156 argv_storage
[k
++] = argv_storage
[j
++];
159 argv_storage
[k
++] = '\0';
163 obstack_ptr_grow (&argv_obstack
, NULL
);
164 argc
= obstack_object_size (&argv_obstack
) / sizeof (void *) - 1;
165 argv
= XOBFINISH (&argv_obstack
, const char **);
167 decode_cmdline_options_to_array (argc
, (const char **)argv
,
169 decoded_options
, decoded_options_count
);
170 obstack_free (&argv_obstack
, NULL
);
173 /* Append OPTION to the options array DECODED_OPTIONS with size
174 DECODED_OPTIONS_COUNT. */
177 append_option (struct cl_decoded_option
**decoded_options
,
178 unsigned int *decoded_options_count
,
179 struct cl_decoded_option
*option
)
181 ++*decoded_options_count
;
183 = (struct cl_decoded_option
*)
184 xrealloc (*decoded_options
,
185 (*decoded_options_count
186 * sizeof (struct cl_decoded_option
)));
187 memcpy (&(*decoded_options
)[*decoded_options_count
- 1], option
,
188 sizeof (struct cl_decoded_option
));
191 /* Try to merge and complain about options FDECODED_OPTIONS when applied
192 ontop of DECODED_OPTIONS. */
195 merge_and_complain (struct cl_decoded_option
**decoded_options
,
196 unsigned int *decoded_options_count
,
197 struct cl_decoded_option
*fdecoded_options
,
198 unsigned int fdecoded_options_count
)
202 /* ??? Merge options from files. Most cases can be
203 handled by either unioning or intersecting
204 (for example -fwrapv is a case for unioning,
205 -ffast-math is for intersection). Most complaints
206 about real conflicts between different options can
207 be deferred to the compiler proper. Options that
208 we can neither safely handle by intersection nor
209 unioning would need to be complained about here.
210 Ideally we'd have a flag in the opt files that
211 tells whether to union or intersect or reject.
212 In absence of that it's unclear what a good default is.
213 It's also difficult to get positional handling correct. */
215 /* The following does what the old LTO option code did,
216 union all target and a selected set of common options. */
217 for (i
= 0; i
< fdecoded_options_count
; ++i
)
219 struct cl_decoded_option
*foption
= &fdecoded_options
[i
];
220 switch (foption
->opt_index
)
222 case OPT_SPECIAL_unknown
:
223 case OPT_SPECIAL_ignore
:
224 case OPT_SPECIAL_program_name
:
225 case OPT_SPECIAL_input_file
:
229 if (!(cl_options
[foption
->opt_index
].flags
& CL_TARGET
))
238 case OPT_fexceptions
:
239 case OPT_fnon_call_exceptions
:
241 /* Do what the old LTO code did - collect exactly one option
242 setting per OPT code, we pick the first we encounter.
243 ??? This doesn't make too much sense, but when it doesn't
244 then we should complain. */
245 for (j
= 0; j
< *decoded_options_count
; ++j
)
246 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
248 if (j
== *decoded_options_count
)
249 append_option (decoded_options
, decoded_options_count
, foption
);
253 case OPT_fstrict_overflow
:
254 case OPT_ffp_contract_
:
255 /* For selected options we can merge conservatively. */
256 for (j
= 0; j
< *decoded_options_count
; ++j
)
257 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
259 if (j
== *decoded_options_count
)
260 append_option (decoded_options
, decoded_options_count
, foption
);
261 /* FP_CONTRACT_OFF < FP_CONTRACT_ON < FP_CONTRACT_FAST,
262 -fno-trapv < -ftrapv,
263 -fno-strict-overflow < -fstrict-overflow */
264 else if (foption
->value
< (*decoded_options
)[j
].value
)
265 (*decoded_options
)[j
] = *foption
;
268 case OPT_fmath_errno
:
269 case OPT_fsigned_zeros
:
270 case OPT_ftrapping_math
:
272 /* For selected options we can merge conservatively. */
273 for (j
= 0; j
< *decoded_options_count
; ++j
)
274 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
276 if (j
== *decoded_options_count
)
277 append_option (decoded_options
, decoded_options_count
, foption
);
278 /* -fmath-errno > -fno-math-errno,
279 -fsigned-zeros > -fno-signed-zeros,
280 -ftrapping-math -> -fno-trapping-math,
281 -fwrapv > -fno-wrapv. */
282 else if (foption
->value
> (*decoded_options
)[j
].value
)
283 (*decoded_options
)[j
] = *foption
;
286 case OPT_freg_struct_return
:
287 case OPT_fpcc_struct_return
:
288 case OPT_fshort_double
:
289 for (j
= 0; j
< *decoded_options_count
; ++j
)
290 if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
)
292 if (j
== *decoded_options_count
)
293 fatal_error ("Option %s not used consistently in all LTO input"
294 " files", foption
->orig_option_with_args_text
);
301 for (j
= 0; j
< *decoded_options_count
; ++j
)
302 if ((*decoded_options
)[j
].opt_index
== OPT_O
303 || (*decoded_options
)[j
].opt_index
== OPT_Ofast
304 || (*decoded_options
)[j
].opt_index
== OPT_Og
305 || (*decoded_options
)[j
].opt_index
== OPT_Os
)
307 if (j
== *decoded_options_count
)
308 append_option (decoded_options
, decoded_options_count
, foption
);
309 else if ((*decoded_options
)[j
].opt_index
== foption
->opt_index
310 && foption
->opt_index
!= OPT_O
)
311 /* Exact same options get merged. */
315 /* For mismatched option kinds preserve the optimization
316 level only, thus merge it as -On. This also handles
317 merging of same optimization level -On. */
319 switch (foption
->opt_index
)
322 if (foption
->arg
[0] == '\0')
323 level
= MAX (level
, 1);
325 level
= MAX (level
, atoi (foption
->arg
));
328 level
= MAX (level
, 3);
331 level
= MAX (level
, 1);
334 level
= MAX (level
, 2);
339 switch ((*decoded_options
)[j
].opt_index
)
342 if ((*decoded_options
)[j
].arg
[0] == '\0')
343 level
= MAX (level
, 1);
345 level
= MAX (level
, atoi ((*decoded_options
)[j
].arg
));
348 level
= MAX (level
, 3);
351 level
= MAX (level
, 1);
354 level
= MAX (level
, 2);
359 (*decoded_options
)[j
].opt_index
= OPT_O
;
361 asprintf (&tem
, "-O%d", level
);
362 (*decoded_options
)[j
].arg
= &tem
[2];
363 (*decoded_options
)[j
].canonical_option
[0] = tem
;
364 (*decoded_options
)[j
].value
= 1;
371 /* Auxiliary function that frees elements of PTR and PTR itself.
372 N is number of elements to be freed.
373 If PTR is NULL, nothing is freed. If an element is NULL, subsequent elements
376 free_array_of_ptrs (void **ptr
, unsigned n
)
381 for (i
= 0; i
< n
; i
++)
391 /* Parse STR, saving found tokens into PVALUES and return their number.
392 Tokens are assumed to be delimited by ':'. If APPEND is non-null,
393 append it to every token we find. */
396 parse_env_var (const char *str
, char ***pvalues
, const char *append
)
398 const char *curval
, *nextval
;
402 curval
= strchr (str
, ':');
406 curval
= strchr (curval
+ 1, ':');
409 values
= (char**) xmalloc (num
* sizeof (char*));
411 nextval
= strchrnul (curval
, ':');
413 int append_len
= append
? strlen (append
) : 0;
414 for (i
= 0; i
< num
; i
++)
416 int l
= nextval
- curval
;
417 values
[i
] = (char*) xmalloc (l
+ 1 + append_len
);
418 memcpy (values
[i
], curval
, l
);
421 strcat (values
[i
], append
);
422 curval
= nextval
+ 1;
423 nextval
= strchrnul (curval
, ':');
429 /* Check whether NAME can be accessed in MODE. This is like access,
430 except that it never considers directories to be executable. */
433 access_check (const char *name
, int mode
)
439 if (stat (name
, &st
) < 0
440 || S_ISDIR (st
.st_mode
))
444 return access (name
, mode
);
447 /* Prepare target image for target NAME.
448 Firstly, we execute COMPILER, passing all input files to it to produce DSO.
449 When target DSO is ready, we pass it to objcopy to place its image into a
450 special data section. After that we rename target image's symbols to values,
451 expected by the host side, and return the name of the resultant file. */
454 prepare_target_image (const char *target
, const char *compiler_path
,
455 unsigned in_argc
, char *in_argv
[])
458 struct obstack argv_obstack
;
460 char *filename
= NULL
;
461 char *suffix
= XALLOCAVEC (char, strlen ("/accel//mkoffload") + 1 + strlen (target
));
462 const char *compiler
= NULL
;
464 strcpy (suffix
, "/accel/");
465 strcat (suffix
, target
);
466 strcat (suffix
, "/mkoffload");
469 int n_paths
= parse_env_var (compiler_path
, &paths
, suffix
);
471 for (int i
= 0; i
< n_paths
; i
++)
472 if (access_check (paths
[i
], X_OK
) == 0)
478 if (compiler
== NULL
)
481 /* Generate temp file name. */
482 filename
= make_temp_file (".target.o");
484 /* -------------------------------------- */
485 /* Run gcc for target. */
486 obstack_init (&argv_obstack
);
487 obstack_ptr_grow (&argv_obstack
, compiler
);
488 obstack_ptr_grow (&argv_obstack
, "-o");
489 obstack_ptr_grow (&argv_obstack
, filename
);
491 for (i
= 1; i
< in_argc
; ++i
)
492 if (strncmp (in_argv
[i
], "-fresolution=", sizeof ("-fresolution=") - 1))
493 obstack_ptr_grow (&argv_obstack
, in_argv
[i
]);
494 obstack_ptr_grow (&argv_obstack
, NULL
);
496 argv
= XOBFINISH (&argv_obstack
, const char **);
497 fork_execute (argv
[0], CONST_CAST (char **, argv
), true);
498 obstack_free (&argv_obstack
, NULL
);
501 free_array_of_ptrs ((void**) paths
, n_paths
);
506 /* The main routine dealing with openmp offloading.
507 The routine builds a target image for each offloading target.
508 IN_ARGC and IN_ARGV specify input files. As all of them could contain
509 omp-sections, we pass them all to target compilers.
510 Env-variable OFFLOAD_TARGET_NAMES_ENV describes for which targets we should
512 This function stores the names of the object files in the OFFLOAD_NAMES
516 compile_images_for_openmp_targets (unsigned in_argc
, char *in_argv
[])
520 unsigned num_targets
;
522 /* Obtain names of offload targets and corresponding compilers. */
523 target_names
= getenv (OFFLOAD_TARGET_NAMES_ENV
);
527 num_targets
= parse_env_var (target_names
, &names
, NULL
);
529 const char *compiler_path
= getenv ("COMPILER_PATH");
530 if (compiler_path
== NULL
)
533 /* Prepare an image for each target. The array is terminated by a NULL
535 offload_names
= XCNEWVEC (char *, num_targets
+ 1);
536 for (unsigned i
= 0; i
< num_targets
; i
++)
538 offload_names
[i
] = prepare_target_image (names
[i
], compiler_path
,
540 if (!offload_names
[i
])
541 fatal_error ("problem with building target image for %s: %m",
546 free_array_of_ptrs ((void**) names
, num_targets
);
549 /* Copy a file from SRC to DEST. */
551 copy_file (const char *dest
, const char *src
)
553 FILE *d
= fopen (dest
, "wb");
554 FILE *s
= fopen (src
, "rb");
558 size_t len
= fread (buffer
, 1, 512, s
);
560 fatal_error ("reading input file");
563 fwrite (buffer
, 1, len
, d
);
565 fatal_error ("writing output file");
570 /* Find the omp_begin.o and omp_end.o files in LIBRARY_PATH, make copies
571 and store the names of the copies in ompbegin and ompend. */
574 find_ompbeginend (void)
577 const char *library_path
= getenv ("LIBRARY_PATH");
578 if (library_path
== NULL
)
580 int n_paths
= parse_env_var (library_path
, &paths
, "/crtompbegin.o");
583 for (i
= 0; i
< n_paths
; i
++)
584 if (access_check (paths
[i
], R_OK
) == 0)
586 size_t len
= strlen (paths
[i
]);
587 char *tmp
= xstrdup (paths
[i
]);
588 strcpy (paths
[i
] + len
- 7, "end.o");
589 if (access_check (paths
[i
], R_OK
) != 0)
590 fatal_error ("installation error, can't find crtompend.o");
591 /* The linker will delete the filenames we give it, so make
593 const char *omptmp1
= make_temp_file (".o");
594 const char *omptmp2
= make_temp_file (".o");
595 copy_file (omptmp1
, tmp
);
597 copy_file (omptmp2
, paths
[i
]);
603 fatal_error ("installation error, can't find crtompbegin.o");
605 free_array_of_ptrs ((void**) paths
, n_paths
);
608 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
611 run_gcc (unsigned argc
, char *argv
[])
614 const char **new_argv
;
615 const char **argv_ptr
;
616 char *list_option_full
= NULL
;
617 const char *linker_output
= NULL
;
618 const char *collect_gcc
, *collect_gcc_options
;
621 bool no_partition
= false;
622 struct cl_decoded_option
*fdecoded_options
= NULL
;
623 unsigned int fdecoded_options_count
= 0;
624 struct cl_decoded_option
*decoded_options
;
625 unsigned int decoded_options_count
;
626 struct obstack argv_obstack
;
628 bool have_offload
= false;
630 /* Get the driver and options. */
631 collect_gcc
= getenv ("COLLECT_GCC");
633 fatal_error ("environment variable COLLECT_GCC must be set");
634 collect_gcc_options
= getenv ("COLLECT_GCC_OPTIONS");
635 if (!collect_gcc_options
)
636 fatal_error ("environment variable COLLECT_GCC_OPTIONS must be set");
637 get_options_from_collect_gcc_options (collect_gcc
, collect_gcc_options
,
640 &decoded_options_count
);
642 /* Look at saved options in the IL files. */
643 for (i
= 1; i
< argc
; ++i
)
650 off_t file_offset
= 0, offset
, length
;
652 simple_object_read
*sobj
;
654 struct cl_decoded_option
*f2decoded_options
;
655 unsigned int f2decoded_options_count
;
656 char *filename
= argv
[i
];
657 if ((p
= strrchr (argv
[i
], '@'))
659 && sscanf (p
, "@%li%n", &loffset
, &consumed
) >= 1
660 && strlen (p
) == (unsigned int) consumed
)
662 filename
= XNEWVEC (char, p
- argv
[i
] + 1);
663 memcpy (filename
, argv
[i
], p
- argv
[i
]);
664 filename
[p
- argv
[i
]] = '\0';
665 file_offset
= (off_t
) loffset
;
667 fd
= open (argv
[i
], O_RDONLY
);
670 sobj
= simple_object_start_read (fd
, file_offset
, "__GNU_LTO",
677 if (!simple_object_find_section (sobj
, LTO_SECTION_NAME_PREFIX
"." "opts",
678 &offset
, &length
, &errmsg
, &err
))
680 simple_object_release_read (sobj
);
684 /* We may choose not to write out this .opts section in the future. In
685 that case we'll have to use something else to look for. */
686 if (simple_object_find_section (sobj
, OMP_SECTION_NAME_PREFIX
"." "opts",
687 &offset
, &length
, &errmsg
, &err
))
689 lseek (fd
, file_offset
+ offset
, SEEK_SET
);
690 data
= (char *)xmalloc (length
);
691 read (fd
, data
, length
);
695 get_options_from_collect_gcc_options (collect_gcc
,
698 &f2decoded_options_count
);
699 if (!fdecoded_options
)
701 fdecoded_options
= f2decoded_options
;
702 fdecoded_options_count
= f2decoded_options_count
;
705 merge_and_complain (&fdecoded_options
,
706 &fdecoded_options_count
,
707 f2decoded_options
, f2decoded_options_count
);
709 fopts
+= strlen (fopts
) + 1;
711 while (fopts
- data
< length
);
714 simple_object_release_read (sobj
);
718 /* Initalize the common arguments for the driver. */
719 obstack_init (&argv_obstack
);
720 obstack_ptr_grow (&argv_obstack
, collect_gcc
);
721 obstack_ptr_grow (&argv_obstack
, "-xlto");
722 obstack_ptr_grow (&argv_obstack
, "-c");
724 /* Append compiler driver arguments as far as they were merged. */
725 for (j
= 1; j
< fdecoded_options_count
; ++j
)
727 struct cl_decoded_option
*option
= &fdecoded_options
[j
];
729 /* File options have been properly filtered by lto-opts.c. */
730 switch (option
->opt_index
)
732 /* Drop arguments that we want to take from the link line. */
735 case OPT_flto_partition_
:
742 /* For now do what the original LTO option code was doing - pass
743 on any CL_TARGET flag and a few selected others. */
744 switch (option
->opt_index
)
751 case OPT_fexceptions
:
752 case OPT_fnon_call_exceptions
:
754 case OPT_freg_struct_return
:
755 case OPT_fpcc_struct_return
:
756 case OPT_fshort_double
:
757 case OPT_ffp_contract_
:
758 case OPT_fmath_errno
:
759 case OPT_fsigned_zeros
:
760 case OPT_ftrapping_math
:
763 case OPT_fstrict_overflow
:
771 if (!(cl_options
[option
->opt_index
].flags
& CL_TARGET
))
775 /* Pass the option on. */
776 for (i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
777 obstack_ptr_grow (&argv_obstack
, option
->canonical_option
[i
]);
780 /* Append linker driver arguments. Compiler options from the linker
781 driver arguments will override / merge with those from the compiler. */
782 for (j
= 1; j
< decoded_options_count
; ++j
)
784 struct cl_decoded_option
*option
= &decoded_options
[j
];
786 /* Do not pass on frontend specific flags not suitable for lto. */
787 if (!(cl_options
[option
->opt_index
].flags
788 & (CL_COMMON
|CL_TARGET
|CL_DRIVER
|CL_LTO
)))
791 switch (option
->opt_index
)
794 linker_output
= option
->arg
;
795 /* We generate new intermediate output, drop this arg. */
806 case OPT_flto_partition_
:
807 if (strcmp (option
->arg
, "none") == 0)
812 if (strcmp (option
->arg
, "jobserver") == 0)
819 parallel
= atoi (option
->arg
);
826 lto_mode
= LTO_MODE_WHOPR
;
827 /* We've handled these LTO options, do not pass them on. */
830 case OPT_freg_struct_return
:
831 case OPT_fpcc_struct_return
:
832 case OPT_fshort_double
:
833 /* Ignore these, they are determined by the input files.
834 ??? We fail to diagnose a possible mismatch here. */
841 /* Pass the option on. */
842 for (i
= 0; i
< option
->canonical_option_num_elements
; ++i
)
843 obstack_ptr_grow (&argv_obstack
, option
->canonical_option
[i
]);
848 lto_mode
= LTO_MODE_LTO
;
855 char *output_dir
, *base
, *name
;
856 bool bit_bucket
= strcmp (linker_output
, HOST_BIT_BUCKET
) == 0;
858 output_dir
= xstrdup (linker_output
);
860 for (name
= base
; *name
; name
++)
861 if (IS_DIR_SEPARATOR (*name
))
865 linker_output
= &linker_output
[base
- output_dir
];
866 if (*output_dir
== '\0')
868 static char current_dir
[] = { '.', DIR_SEPARATOR
, '\0' };
869 output_dir
= current_dir
;
873 obstack_ptr_grow (&argv_obstack
, "-dumpdir");
874 obstack_ptr_grow (&argv_obstack
, output_dir
);
877 obstack_ptr_grow (&argv_obstack
, "-dumpbase");
880 /* Remember at which point we can scrub args to re-use the commons. */
881 new_head_argc
= obstack_object_size (&argv_obstack
) / sizeof (void *);
883 if (lto_mode
== LTO_MODE_LTO
)
885 flto_out
= make_temp_file (".lto.o");
887 obstack_ptr_grow (&argv_obstack
, linker_output
);
888 obstack_ptr_grow (&argv_obstack
, "-o");
889 obstack_ptr_grow (&argv_obstack
, flto_out
);
893 const char *list_option
= "-fltrans-output-list=";
894 size_t list_option_len
= strlen (list_option
);
899 char *dumpbase
= (char *) xmalloc (strlen (linker_output
)
900 + sizeof (".wpa") + 1);
901 strcpy (dumpbase
, linker_output
);
902 strcat (dumpbase
, ".wpa");
903 obstack_ptr_grow (&argv_obstack
, dumpbase
);
906 if (linker_output
&& save_temps
)
908 ltrans_output_file
= (char *) xmalloc (strlen (linker_output
)
909 + sizeof (".ltrans.out") + 1);
910 strcpy (ltrans_output_file
, linker_output
);
911 strcat (ltrans_output_file
, ".ltrans.out");
914 ltrans_output_file
= make_temp_file (".ltrans.out");
915 list_option_full
= (char *) xmalloc (sizeof (char) *
916 (strlen (ltrans_output_file
) + list_option_len
+ 1));
917 tmp
= list_option_full
;
919 obstack_ptr_grow (&argv_obstack
, tmp
);
920 strcpy (tmp
, list_option
);
921 tmp
+= list_option_len
;
922 strcpy (tmp
, ltrans_output_file
);
925 obstack_ptr_grow (&argv_obstack
, xstrdup ("-fwpa=jobserver"));
926 else if (parallel
> 1)
929 sprintf (buf
, "-fwpa=%i", parallel
);
930 obstack_ptr_grow (&argv_obstack
, xstrdup (buf
));
933 obstack_ptr_grow (&argv_obstack
, "-fwpa");
936 /* Append the input objects and possible preceding arguments. */
937 for (i
= 1; i
< argc
; ++i
)
938 obstack_ptr_grow (&argv_obstack
, argv
[i
]);
939 obstack_ptr_grow (&argv_obstack
, NULL
);
941 new_argv
= XOBFINISH (&argv_obstack
, const char **);
942 argv_ptr
= &new_argv
[new_head_argc
];
943 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
), true);
945 if (lto_mode
== LTO_MODE_LTO
)
947 printf ("%s\n", flto_out
);
953 FILE *stream
= fopen (ltrans_output_file
, "r");
954 FILE *mstream
= NULL
;
955 struct obstack env_obstack
;
958 fatal_error ("fopen: %s: %m", ltrans_output_file
);
960 /* Parse the list of LTRANS inputs from the WPA stage. */
961 obstack_init (&env_obstack
);
965 const unsigned piece
= 32;
966 char *output_name
= NULL
;
967 char *buf
, *input_name
= (char *)xmalloc (piece
);
972 if (!fgets (buf
, piece
, stream
))
974 len
= strlen (input_name
);
975 if (input_name
[len
- 1] != '\n')
977 input_name
= (char *)xrealloc (input_name
, len
+ piece
);
978 buf
= input_name
+ len
;
981 input_name
[len
- 1] = '\0';
983 if (input_name
[0] == '*')
984 output_name
= &input_name
[1];
987 input_names
= (char **)xrealloc (input_names
, nr
* sizeof (char *));
988 output_names
= (char **)xrealloc (output_names
, nr
* sizeof (char *));
989 input_names
[nr
-1] = input_name
;
990 output_names
[nr
-1] = output_name
;
993 maybe_unlink (ltrans_output_file
);
994 ltrans_output_file
= NULL
;
998 makefile
= make_temp_file (".mk");
999 mstream
= fopen (makefile
, "w");
1002 /* Execute the LTRANS stage for each input file (or prepare a
1003 makefile to invoke this in parallel). */
1004 for (i
= 0; i
< nr
; ++i
)
1007 char *input_name
= input_names
[i
];
1008 /* If it's a pass-through file do nothing. */
1009 if (output_names
[i
])
1012 /* Replace the .o suffix with a .ltrans.o suffix and write
1013 the resulting name to the LTRANS output list. */
1014 obstack_grow (&env_obstack
, input_name
, strlen (input_name
) - 2);
1015 obstack_grow (&env_obstack
, ".ltrans.o", sizeof (".ltrans.o"));
1016 output_name
= XOBFINISH (&env_obstack
, char *);
1018 /* Adjust the dumpbase if the linker output file was seen. */
1022 = (char *) xmalloc (strlen (linker_output
)
1023 + sizeof (DUMPBASE_SUFFIX
) + 1);
1025 strlen (linker_output
) + sizeof (DUMPBASE_SUFFIX
),
1026 "%s.ltrans%u", linker_output
, i
);
1027 argv_ptr
[0] = dumpbase
;
1030 argv_ptr
[1] = "-fltrans";
1032 argv_ptr
[3] = output_name
;
1033 argv_ptr
[4] = input_name
;
1037 fprintf (mstream
, "%s:\n\t@%s ", output_name
, new_argv
[0]);
1038 for (j
= 1; new_argv
[j
] != NULL
; ++j
)
1039 fprintf (mstream
, " '%s'", new_argv
[j
]);
1040 fprintf (mstream
, "\n");
1041 /* If we are not preserving the ltrans input files then
1042 truncate them as soon as we have processed it. This
1043 reduces temporary disk-space usage. */
1045 fprintf (mstream
, "\t@-touch -r %s %s.tem > /dev/null 2>&1 "
1046 "&& mv %s.tem %s\n",
1047 input_name
, input_name
, input_name
, input_name
);
1051 fork_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1053 maybe_unlink (input_name
);
1056 output_names
[i
] = output_name
;
1060 struct pex_obj
*pex
;
1063 fprintf (mstream
, "all:");
1064 for (i
= 0; i
< nr
; ++i
)
1065 fprintf (mstream
, " \\\n\t%s", output_names
[i
]);
1066 fprintf (mstream
, "\n");
1070 /* Avoid passing --jobserver-fd= and similar flags
1071 unless jobserver mode is explicitly enabled. */
1072 putenv (xstrdup ("MAKEFLAGS="));
1073 putenv (xstrdup ("MFLAGS="));
1075 new_argv
[0] = getenv ("MAKE");
1077 new_argv
[0] = "make";
1079 new_argv
[2] = makefile
;
1083 snprintf (jobs
, 31, "-j%d", parallel
);
1084 new_argv
[i
++] = jobs
;
1086 new_argv
[i
++] = "all";
1087 new_argv
[i
++] = NULL
;
1088 pex
= collect_execute (new_argv
[0], CONST_CAST (char **, new_argv
),
1089 NULL
, NULL
, PEX_SEARCH
, false);
1090 do_wait (new_argv
[0], pex
);
1091 maybe_unlink (makefile
);
1093 for (i
= 0; i
< nr
; ++i
)
1094 maybe_unlink (input_names
[i
]);
1098 compile_images_for_openmp_targets (argc
, argv
);
1101 find_ompbeginend ();
1102 for (i
= 0; offload_names
[i
]; i
++)
1104 fputs (offload_names
[i
], stdout
);
1105 putc ('\n', stdout
);
1107 free_array_of_ptrs ((void **)offload_names
, i
);
1112 fputs (ompbegin
, stdout
);
1113 putc ('\n', stdout
);
1116 for (i
= 0; i
< nr
; ++i
)
1118 fputs (output_names
[i
], stdout
);
1119 putc ('\n', stdout
);
1120 free (input_names
[i
]);
1124 fputs (ompend
, stdout
);
1125 putc ('\n', stdout
);
1128 free (output_names
);
1130 free (list_option_full
);
1131 obstack_free (&env_obstack
, NULL
);
1134 obstack_free (&argv_obstack
, NULL
);
1141 main (int argc
, char *argv
[])
1145 gcc_obstack_init (&opts_obstack
);
1147 p
= argv
[0] + strlen (argv
[0]);
1148 while (p
!= argv
[0] && !IS_DIR_SEPARATOR (p
[-1]))
1152 xmalloc_set_program_name (progname
);
1154 if (atexit (lto_wrapper_cleanup
) != 0)
1155 fatal_error ("atexit failed");
1157 gcc_init_libintl ();
1159 diagnostic_initialize (global_dc
, 0);
1161 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
1162 signal (SIGINT
, fatal_signal
);
1164 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
1165 signal (SIGHUP
, fatal_signal
);
1167 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
1168 signal (SIGTERM
, fatal_signal
);
1170 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
1171 signal (SIGPIPE
, fatal_signal
);
1174 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
1175 receive the signal. A different setting is inheritable */
1176 signal (SIGCHLD
, SIG_DFL
);
1179 /* We may be called with all the arguments stored in some file and
1180 passed with @file. Expand them into argv before processing. */
1181 expandargv (&argc
, &argv
);
1183 run_gcc (argc
, argv
);