2011-11-04 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / lto-wrapper.c
blob5fb37624810aa5b42235cb9689c2351b30820abf
1 /* Wrapper to call lto. Used by collect2 and the linker plugin.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 Factored out of collect2 by Rafael Espindola <espindola@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 /* This program is passed a gcc, a list of gcc arguments and a list of
24 object files containing IL. It scans the argument list to check if
25 we are in whopr mode or not modifies the arguments and needed and
26 prints a list of output files on stdout.
28 Example:
30 $ lto-wrapper gcc/xgcc -B gcc a.o b.o -o test -flto
32 The above will print something like
33 /tmp/ccwbQ8B2.lto.o
35 If WHOPR is used instead, more than one file might be produced
36 ./ccXj2DTk.lto.ltrans.o
37 ./ccCJuXGv.lto.ltrans.o
40 #include "config.h"
41 #include "system.h"
42 #include "coretypes.h"
43 #include "intl.h"
44 #include "diagnostic.h"
45 #include "obstack.h"
46 #include "opts.h"
47 #include "options.h"
48 #include "simple-object.h"
50 /* From lto-streamer.h which we cannot include with -fkeep-inline-functions.
51 ??? Split out a lto-streamer-core.h. */
53 #define LTO_SECTION_NAME_PREFIX ".gnu.lto_"
55 /* End of lto-streamer.h copy. */
57 int debug; /* true if -save-temps. */
58 int verbose; /* true if -v. */
60 enum lto_mode_d {
61 LTO_MODE_NONE, /* Not doing LTO. */
62 LTO_MODE_LTO, /* Normal LTO. */
63 LTO_MODE_WHOPR /* WHOPR. */
66 /* Current LTO mode. */
67 static enum lto_mode_d lto_mode = LTO_MODE_NONE;
69 static char *ltrans_output_file;
70 static char *flto_out;
71 static char *args_name;
72 static unsigned int nr;
73 static char **input_names;
74 static char **output_names;
75 static char *makefile;
77 static void maybe_unlink_file (const char *);
79 /* Delete tempfiles. */
81 static void
82 lto_wrapper_cleanup (void)
84 static bool cleanup_done = false;
85 unsigned int i;
87 if (cleanup_done)
88 return;
90 /* Setting cleanup_done prevents an infinite loop if one of the
91 calls to maybe_unlink_file fails. */
92 cleanup_done = true;
94 if (ltrans_output_file)
95 maybe_unlink_file (ltrans_output_file);
96 if (flto_out)
97 maybe_unlink_file (flto_out);
98 if (args_name)
99 maybe_unlink_file (args_name);
100 if (makefile)
101 maybe_unlink_file (makefile);
102 for (i = 0; i < nr; ++i)
104 maybe_unlink_file (input_names[i]);
105 if (output_names[i])
106 maybe_unlink_file (output_names[i]);
110 static void
111 fatal_signal (int signum)
113 signal (signum, SIG_DFL);
114 lto_wrapper_cleanup ();
115 /* Get the same signal again, this time not handled,
116 so its normal effect occurs. */
117 kill (getpid (), signum);
120 /* Just die. CMSGID is the error message. */
122 static void __attribute__ ((format (printf, 1, 2)))
123 fatal (const char * cmsgid, ...)
125 va_list ap;
127 va_start (ap, cmsgid);
128 fprintf (stderr, "lto-wrapper: ");
129 vfprintf (stderr, _(cmsgid), ap);
130 fprintf (stderr, "\n");
131 va_end (ap);
133 lto_wrapper_cleanup ();
134 exit (FATAL_EXIT_CODE);
138 /* Die when sys call fails. CMSGID is the error message. */
140 static void __attribute__ ((format (printf, 1, 2)))
141 fatal_perror (const char *cmsgid, ...)
143 int e = errno;
144 va_list ap;
146 va_start (ap, cmsgid);
147 fprintf (stderr, "lto-wrapper: ");
148 vfprintf (stderr, _(cmsgid), ap);
149 fprintf (stderr, ": %s\n", xstrerror (e));
150 va_end (ap);
152 lto_wrapper_cleanup ();
153 exit (FATAL_EXIT_CODE);
157 /* Execute a program, and wait for the reply. ARGV are the arguments. The
158 last one must be NULL. */
160 static struct pex_obj *
161 collect_execute (char **argv)
163 struct pex_obj *pex;
164 const char *errmsg;
165 int err;
167 if (verbose)
169 char **p_argv;
170 const char *str;
172 for (p_argv = argv; (str = *p_argv) != (char *) 0; p_argv++)
173 fprintf (stderr, " %s", str);
175 fprintf (stderr, "\n");
178 fflush (stdout);
179 fflush (stderr);
181 pex = pex_init (0, "lto-wrapper", NULL);
182 if (pex == NULL)
183 fatal_perror ("pex_init failed");
185 /* Do not use PEX_LAST here, we use our stdout for communicating with
186 collect2 or the linker-plugin. Any output from the sub-process
187 will confuse that. */
188 errmsg = pex_run (pex, PEX_SEARCH, argv[0], argv, NULL,
189 NULL, &err);
190 if (errmsg != NULL)
192 if (err != 0)
194 errno = err;
195 fatal_perror (errmsg);
197 else
198 fatal (errmsg);
201 return pex;
205 /* Wait for a process to finish, and exit if a nonzero status is found.
206 PROG is the program name. PEX is the process we should wait for. */
208 static int
209 collect_wait (const char *prog, struct pex_obj *pex)
211 int status;
213 if (!pex_get_status (pex, 1, &status))
214 fatal_perror ("can't get program status");
215 pex_free (pex);
217 if (status)
219 if (WIFSIGNALED (status))
221 int sig = WTERMSIG (status);
222 if (WCOREDUMP (status))
223 fatal ("%s terminated with signal %d [%s], core dumped",
224 prog, sig, strsignal (sig));
225 else
226 fatal ("%s terminated with signal %d [%s]",
227 prog, sig, strsignal (sig));
230 if (WIFEXITED (status))
231 fatal ("%s returned %d exit status", prog, WEXITSTATUS (status));
234 return 0;
238 /* Unlink a temporary LTRANS file unless requested otherwise. */
240 static void
241 maybe_unlink_file (const char *file)
243 if (! debug)
245 if (unlink_if_ordinary (file)
246 && errno != ENOENT)
247 fatal_perror ("deleting LTRANS file %s", file);
249 else
250 fprintf (stderr, "[Leaving LTRANS %s]\n", file);
254 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
256 static void
257 fork_execute (char **argv)
259 struct pex_obj *pex;
260 char *new_argv[3];
261 char *at_args;
262 FILE *args;
263 int status;
265 args_name = make_temp_file (".args");
266 at_args = concat ("@", args_name, NULL);
267 args = fopen (args_name, "w");
268 if (args == NULL)
269 fatal ("failed to open %s", args_name);
271 status = writeargv (&argv[1], args);
273 if (status)
274 fatal ("could not write to temporary file %s", args_name);
276 fclose (args);
278 new_argv[0] = argv[0];
279 new_argv[1] = at_args;
280 new_argv[2] = NULL;
282 pex = collect_execute (new_argv);
283 collect_wait (new_argv[0], pex);
285 maybe_unlink_file (args_name);
286 args_name = NULL;
287 free (at_args);
290 /* Template of LTRANS dumpbase suffix. */
291 #define DUMPBASE_SUFFIX ".ltrans18446744073709551615"
293 /* Create decoded options from the COLLECT_GCC and COLLECT_GCC_OPTIONS
294 environment according to LANG_MASK. */
296 static void
297 get_options_from_collect_gcc_options (const char *collect_gcc,
298 const char *collect_gcc_options,
299 unsigned int lang_mask,
300 struct cl_decoded_option **decoded_options,
301 unsigned int *decoded_options_count)
303 struct obstack argv_obstack;
304 char *argv_storage;
305 const char **argv;
306 int j, k, argc;
308 argv_storage = xstrdup (collect_gcc_options);
309 obstack_init (&argv_obstack);
310 obstack_ptr_grow (&argv_obstack, collect_gcc);
312 for (j = 0, k = 0; argv_storage[j] != '\0'; ++j)
314 if (argv_storage[j] == '\'')
316 obstack_ptr_grow (&argv_obstack, &argv_storage[k]);
317 ++j;
320 if (argv_storage[j] == '\0')
321 fatal ("malformed COLLECT_GCC_OPTIONS");
322 else if (strncmp (&argv_storage[j], "'\\''", 4) == 0)
324 argv_storage[k++] = '\'';
325 j += 4;
327 else if (argv_storage[j] == '\'')
328 break;
329 else
330 argv_storage[k++] = argv_storage[j++];
332 while (1);
333 argv_storage[k++] = '\0';
337 obstack_ptr_grow (&argv_obstack, NULL);
338 argc = obstack_object_size (&argv_obstack) / sizeof (void *) - 1;
339 argv = XOBFINISH (&argv_obstack, const char **);
341 decode_cmdline_options_to_array (argc, (const char **)argv,
342 lang_mask,
343 decoded_options, decoded_options_count);
344 obstack_free (&argv_obstack, NULL);
347 /* Append OPTION to the options array DECODED_OPTIONS with size
348 DECODED_OPTIONS_COUNT. */
350 static void
351 append_option (struct cl_decoded_option **decoded_options,
352 unsigned int *decoded_options_count,
353 struct cl_decoded_option *option)
355 ++*decoded_options_count;
356 *decoded_options
357 = (struct cl_decoded_option *)
358 xrealloc (*decoded_options,
359 (*decoded_options_count
360 * sizeof (struct cl_decoded_option)));
361 memcpy (&(*decoded_options)[*decoded_options_count - 1], option,
362 sizeof (struct cl_decoded_option));
365 /* Try to merge and complain about options FDECODED_OPTIONS when applied
366 ontop of DECODED_OPTIONS. */
368 static void
369 merge_and_complain (struct cl_decoded_option **decoded_options,
370 unsigned int *decoded_options_count,
371 struct cl_decoded_option *fdecoded_options,
372 unsigned int fdecoded_options_count)
374 unsigned int i, j;
376 /* ??? Merge options from files. Most cases can be
377 handled by either unioning or intersecting
378 (for example -fwrapv is a case for unioning,
379 -ffast-math is for intersection). Most complaints
380 about real conflicts between different options can
381 be deferred to the compiler proper. Options that
382 we can neither safely handle by intersection nor
383 unioning would need to be complained about here.
384 Ideally we'd have a flag in the opt files that
385 tells whether to union or intersect or reject.
386 In absence of that it's unclear what a good default is.
387 It's also difficult to get positional handling correct. */
389 /* The following does what the old LTO option code did,
390 union all target and a selected set of common options. */
391 for (i = 0; i < fdecoded_options_count; ++i)
393 struct cl_decoded_option *foption = &fdecoded_options[i];
394 switch (foption->opt_index)
396 default:
397 if (!(cl_options[foption->opt_index].flags & CL_TARGET))
398 break;
400 /* Fallthru. */
401 case OPT_fPIC:
402 case OPT_fpic:
403 case OPT_fpie:
404 case OPT_fcommon:
405 case OPT_fexceptions:
406 /* Do what the old LTO code did - collect exactly one option
407 setting per OPT code, we pick the first we encounter.
408 ??? This doesn't make too much sense, but when it doesn't
409 then we should complain. */
410 for (j = 0; j < *decoded_options_count; ++j)
411 if ((*decoded_options)[j].opt_index == foption->opt_index)
412 break;
413 if (j == *decoded_options_count)
414 append_option (decoded_options, decoded_options_count, foption);
415 break;
420 /* Execute gcc. ARGC is the number of arguments. ARGV contains the arguments. */
422 static void
423 run_gcc (unsigned argc, char *argv[])
425 unsigned i, j;
426 const char **new_argv;
427 const char **argv_ptr;
428 char *list_option_full = NULL;
429 const char *linker_output = NULL;
430 const char *collect_gcc, *collect_gcc_options;
431 int parallel = 0;
432 int jobserver = 0;
433 bool no_partition = false;
434 struct cl_decoded_option *fdecoded_options = NULL;
435 unsigned int fdecoded_options_count = 0;
436 struct cl_decoded_option *decoded_options;
437 unsigned int decoded_options_count;
438 struct obstack argv_obstack;
439 int new_head_argc;
441 /* Get the driver and options. */
442 collect_gcc = getenv ("COLLECT_GCC");
443 if (!collect_gcc)
444 fatal ("environment variable COLLECT_GCC must be set");
445 collect_gcc_options = getenv ("COLLECT_GCC_OPTIONS");
446 if (!collect_gcc_options)
447 fatal ("environment variable COLLECT_GCC_OPTIONS must be set");
448 get_options_from_collect_gcc_options (collect_gcc, collect_gcc_options,
449 CL_LANG_ALL,
450 &decoded_options,
451 &decoded_options_count);
453 /* Look at saved options in the IL files. */
454 for (i = 1; i < argc; ++i)
456 char *data, *p;
457 char *fopts;
458 int fd;
459 const char *errmsg;
460 int err;
461 off_t file_offset = 0, offset, length;
462 long loffset;
463 simple_object_read *sobj;
464 int consumed;
465 struct cl_decoded_option *f2decoded_options;
466 unsigned int f2decoded_options_count;
467 char *filename = argv[i];
468 if ((p = strrchr (argv[i], '@'))
469 && p != argv[i]
470 && sscanf (p, "@%li%n", &loffset, &consumed) >= 1
471 && strlen (p) == (unsigned int) consumed)
473 filename = XNEWVEC (char, p - argv[i] + 1);
474 memcpy (filename, argv[i], p - argv[i]);
475 filename[p - argv[i]] = '\0';
476 file_offset = (off_t) loffset;
478 fd = open (argv[i], O_RDONLY);
479 if (fd == -1)
480 continue;
481 sobj = simple_object_start_read (fd, file_offset, NULL, &errmsg, &err);
482 if (!sobj)
484 close (fd);
485 continue;
487 if (!simple_object_find_section (sobj, LTO_SECTION_NAME_PREFIX "." "opts",
488 &offset, &length, &errmsg, &err))
490 simple_object_release_read (sobj);
491 close (fd);
492 continue;
494 lseek (fd, file_offset + offset, SEEK_SET);
495 data = (char *)xmalloc (length);
496 read (fd, data, length);
497 fopts = data;
500 get_options_from_collect_gcc_options (collect_gcc,
501 fopts, CL_LANG_ALL,
502 &f2decoded_options,
503 &f2decoded_options_count);
504 if (!fdecoded_options)
506 fdecoded_options = f2decoded_options;
507 fdecoded_options_count = f2decoded_options_count;
509 else
510 merge_and_complain (&fdecoded_options,
511 &fdecoded_options_count,
512 f2decoded_options, f2decoded_options_count);
514 fopts += strlen (fopts) + 1;
516 while (fopts - data < length);
518 free (data);
519 simple_object_release_read (sobj);
520 close (fd);
523 /* Initalize the common arguments for the driver. */
524 obstack_init (&argv_obstack);
525 obstack_ptr_grow (&argv_obstack, collect_gcc);
526 obstack_ptr_grow (&argv_obstack, "-xlto");
527 obstack_ptr_grow (&argv_obstack, "-c");
529 /* Append compiler driver arguments as far as they were merged. */
530 for (j = 1; j < fdecoded_options_count; ++j)
532 struct cl_decoded_option *option = &fdecoded_options[j];
534 /* File options have been properly filtered by lto-opts.c. */
535 switch (option->opt_index)
537 /* Drop arguments that we want to take from the link line. */
538 case OPT_flto_:
539 case OPT_flto:
540 case OPT_flto_partition_none:
541 case OPT_flto_partition_1to1:
542 case OPT_flto_partition_balanced:
543 continue;
545 default:
546 break;
549 /* For now do what the original LTO option code was doing - pass
550 on any CL_TARGET flag and a few selected others. */
551 switch (option->opt_index)
553 case OPT_fPIC:
554 case OPT_fpic:
555 case OPT_fpie:
556 case OPT_fcommon:
557 case OPT_fexceptions:
558 break;
560 default:
561 if (!(cl_options[option->opt_index].flags & CL_TARGET))
562 continue;
565 /* Pass the option on. */
566 for (i = 0; i < option->canonical_option_num_elements; ++i)
567 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
570 /* Append linker driver arguments. Compiler options from the linker
571 driver arguments will override / merge with those from the compiler. */
572 for (j = 1; j < decoded_options_count; ++j)
574 struct cl_decoded_option *option = &decoded_options[j];
576 /* Do not pass on frontend specific flags not suitable for lto. */
577 if (!(cl_options[option->opt_index].flags
578 & (CL_COMMON|CL_TARGET|CL_DRIVER|CL_LTO)))
579 continue;
581 switch (option->opt_index)
583 case OPT_o:
584 linker_output = option->arg;
585 /* We generate new intermediate output, drop this arg. */
586 continue;
588 case OPT_save_temps:
589 debug = 1;
590 break;
592 case OPT_v:
593 verbose = 1;
594 break;
596 case OPT_flto_partition_none:
597 no_partition = true;
598 break;
600 case OPT_flto_:
601 if (strcmp (option->arg, "jobserver") == 0)
603 jobserver = 1;
604 parallel = 1;
606 else
608 parallel = atoi (option->arg);
609 if (parallel <= 1)
610 parallel = 0;
612 /* Fallthru. */
614 case OPT_flto:
615 lto_mode = LTO_MODE_WHOPR;
616 /* We've handled these LTO options, do not pass them on. */
617 continue;
619 default:
620 break;
623 /* Pass the option on. */
624 for (i = 0; i < option->canonical_option_num_elements; ++i)
625 obstack_ptr_grow (&argv_obstack, option->canonical_option[i]);
628 if (no_partition)
630 lto_mode = LTO_MODE_LTO;
631 jobserver = 0;
632 parallel = 0;
635 if (linker_output)
637 char *output_dir, *base, *name;
638 bool bit_bucket = strcmp (linker_output, HOST_BIT_BUCKET) == 0;
640 output_dir = xstrdup (linker_output);
641 base = output_dir;
642 for (name = base; *name; name++)
643 if (IS_DIR_SEPARATOR (*name))
644 base = name + 1;
645 *base = '\0';
647 linker_output = &linker_output[base - output_dir];
648 if (*output_dir == '\0')
650 static char current_dir[] = { '.', DIR_SEPARATOR, '\0' };
651 output_dir = current_dir;
653 if (!bit_bucket)
655 obstack_ptr_grow (&argv_obstack, "-dumpdir");
656 obstack_ptr_grow (&argv_obstack, output_dir);
659 obstack_ptr_grow (&argv_obstack, "-dumpbase");
662 /* Remember at which point we can scrub args to re-use the commons. */
663 new_head_argc = obstack_object_size (&argv_obstack) / sizeof (void *);
665 if (lto_mode == LTO_MODE_LTO)
667 flto_out = make_temp_file (".lto.o");
668 if (linker_output)
669 obstack_ptr_grow (&argv_obstack, linker_output);
670 obstack_ptr_grow (&argv_obstack, "-o");
671 obstack_ptr_grow (&argv_obstack, flto_out);
673 else
675 const char *list_option = "-fltrans-output-list=";
676 size_t list_option_len = strlen (list_option);
677 char *tmp;
679 if (linker_output)
681 char *dumpbase = (char *) xmalloc (strlen (linker_output)
682 + sizeof (".wpa") + 1);
683 strcpy (dumpbase, linker_output);
684 strcat (dumpbase, ".wpa");
685 obstack_ptr_grow (&argv_obstack, dumpbase);
688 if (linker_output && debug)
690 ltrans_output_file = (char *) xmalloc (strlen (linker_output)
691 + sizeof (".ltrans.out") + 1);
692 strcpy (ltrans_output_file, linker_output);
693 strcat (ltrans_output_file, ".ltrans.out");
695 else
696 ltrans_output_file = make_temp_file (".ltrans.out");
697 list_option_full = (char *) xmalloc (sizeof (char) *
698 (strlen (ltrans_output_file) + list_option_len + 1));
699 tmp = list_option_full;
701 obstack_ptr_grow (&argv_obstack, tmp);
702 strcpy (tmp, list_option);
703 tmp += list_option_len;
704 strcpy (tmp, ltrans_output_file);
706 obstack_ptr_grow (&argv_obstack, "-fwpa");
709 /* Append the input objects and possible preceeding arguments. */
710 for (i = 1; i < argc; ++i)
711 obstack_ptr_grow (&argv_obstack, argv[i]);
712 obstack_ptr_grow (&argv_obstack, NULL);
714 new_argv = XOBFINISH (&argv_obstack, const char **);
715 argv_ptr = &new_argv[new_head_argc];
716 fork_execute (CONST_CAST (char **, new_argv));
718 if (lto_mode == LTO_MODE_LTO)
720 printf("%s\n", flto_out);
721 free (flto_out);
722 flto_out = NULL;
724 else
726 FILE *stream = fopen (ltrans_output_file, "r");
727 FILE *mstream = NULL;
728 struct obstack env_obstack;
730 if (!stream)
731 fatal_perror ("fopen: %s", ltrans_output_file);
733 /* Parse the list of LTRANS inputs from the WPA stage. */
734 obstack_init (&env_obstack);
735 nr = 0;
736 for (;;)
738 const unsigned piece = 32;
739 char *output_name = NULL;
740 char *buf, *input_name = (char *)xmalloc (piece);
741 size_t len;
743 buf = input_name;
744 cont:
745 if (!fgets (buf, piece, stream))
746 break;
747 len = strlen (input_name);
748 if (input_name[len - 1] != '\n')
750 input_name = (char *)xrealloc (input_name, len + piece);
751 buf = input_name + len;
752 goto cont;
754 input_name[len - 1] = '\0';
756 if (input_name[0] == '*')
757 output_name = &input_name[1];
759 nr++;
760 input_names = (char **)xrealloc (input_names, nr * sizeof (char *));
761 output_names = (char **)xrealloc (output_names, nr * sizeof (char *));
762 input_names[nr-1] = input_name;
763 output_names[nr-1] = output_name;
765 fclose (stream);
766 maybe_unlink_file (ltrans_output_file);
767 ltrans_output_file = NULL;
769 if (parallel)
771 makefile = make_temp_file (".mk");
772 mstream = fopen (makefile, "w");
775 /* Execute the LTRANS stage for each input file (or prepare a
776 makefile to invoke this in parallel). */
777 for (i = 0; i < nr; ++i)
779 char *output_name;
780 char *input_name = input_names[i];
781 /* If it's a pass-through file do nothing. */
782 if (output_names[i])
783 continue;
785 /* Replace the .o suffix with a .ltrans.o suffix and write
786 the resulting name to the LTRANS output list. */
787 obstack_grow (&env_obstack, input_name, strlen (input_name) - 2);
788 obstack_grow (&env_obstack, ".ltrans.o", sizeof (".ltrans.o"));
789 output_name = XOBFINISH (&env_obstack, char *);
791 /* Adjust the dumpbase if the linker output file was seen. */
792 if (linker_output)
794 char *dumpbase
795 = (char *) xmalloc (strlen (linker_output)
796 + sizeof(DUMPBASE_SUFFIX) + 1);
797 snprintf (dumpbase,
798 strlen (linker_output) + sizeof(DUMPBASE_SUFFIX),
799 "%s.ltrans%u", linker_output, i);
800 argv_ptr[0] = dumpbase;
803 argv_ptr[1] = "-fltrans";
804 argv_ptr[2] = "-o";
805 argv_ptr[3] = output_name;
806 argv_ptr[4] = input_name;
807 argv_ptr[5] = NULL;
808 if (parallel)
810 fprintf (mstream, "%s:\n\t@%s ", output_name, new_argv[0]);
811 for (j = 1; new_argv[j] != NULL; ++j)
812 fprintf (mstream, " '%s'", new_argv[j]);
813 fprintf (mstream, "\n");
815 else
816 fork_execute (CONST_CAST (char **, new_argv));
818 output_names[i] = output_name;
820 if (parallel)
822 struct pex_obj *pex;
823 char jobs[32];
825 fprintf (mstream, "all:");
826 for (i = 0; i < nr; ++i)
827 fprintf (mstream, " \\\n\t%s", output_names[i]);
828 fprintf (mstream, "\n");
829 fclose (mstream);
830 if (!jobserver)
832 /* Avoid passing --jobserver-fd= and similar flags
833 unless jobserver mode is explicitly enabled. */
834 putenv (xstrdup ("MAKEFLAGS="));
835 putenv (xstrdup ("MFLAGS="));
837 new_argv[0] = getenv ("MAKE");
838 if (!new_argv[0])
839 new_argv[0] = "make";
840 new_argv[1] = "-f";
841 new_argv[2] = makefile;
842 i = 3;
843 if (!jobserver)
845 snprintf (jobs, 31, "-j%d", parallel);
846 new_argv[i++] = jobs;
848 new_argv[i++] = "all";
849 new_argv[i++] = NULL;
850 pex = collect_execute (CONST_CAST (char **, new_argv));
851 collect_wait (new_argv[0], pex);
852 maybe_unlink_file (makefile);
853 makefile = NULL;
855 for (i = 0; i < nr; ++i)
857 fputs (output_names[i], stdout);
858 putc ('\n', stdout);
859 maybe_unlink_file (input_names[i]);
860 free (input_names[i]);
862 nr = 0;
863 free (output_names);
864 free (input_names);
865 free (list_option_full);
866 obstack_free (&env_obstack, NULL);
869 obstack_free (&argv_obstack, NULL);
873 /* Entry point. */
876 main (int argc, char *argv[])
878 const char *p;
880 p = argv[0] + strlen (argv[0]);
881 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
882 --p;
883 progname = p;
885 xmalloc_set_program_name (progname);
887 gcc_init_libintl ();
889 diagnostic_initialize (global_dc, 0);
891 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
892 signal (SIGINT, fatal_signal);
893 #ifdef SIGHUP
894 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
895 signal (SIGHUP, fatal_signal);
896 #endif
897 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
898 signal (SIGTERM, fatal_signal);
899 #ifdef SIGPIPE
900 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
901 signal (SIGPIPE, fatal_signal);
902 #endif
903 #ifdef SIGCHLD
904 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
905 receive the signal. A different setting is inheritable */
906 signal (SIGCHLD, SIG_DFL);
907 #endif
909 /* We may be called with all the arguments stored in some file and
910 passed with @file. Expand them into argv before processing. */
911 expandargv (&argc, &argv);
913 run_gcc (argc, argv);
915 return 0;