Daily bump.
[official-gcc.git] / gcc / collect-utils.c
blobd4fa2c3d345c905a67b93f21d50349e566fd6426
1 /* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "intl.h"
24 #include "diagnostic.h"
25 #include "obstack.h"
26 #include "opts.h"
27 #include "options.h"
28 #include "simple-object.h"
29 #include "lto-section-names.h"
30 #include "collect-utils.h"
32 static char *response_file;
34 bool debug;
35 bool verbose;
36 bool save_temps;
37 const char *dumppfx;
40 /* Notify user of a non-error. */
41 void
42 notice (const char *cmsgid, ...)
44 va_list ap;
46 va_start (ap, cmsgid);
47 vfprintf (stderr, _(cmsgid), ap);
48 va_end (ap);
51 void
52 fatal_signal (int signum)
54 signal (signum, SIG_DFL);
55 utils_cleanup (true);
56 /* Get the same signal again, this time not handled,
57 so its normal effect occurs. */
58 kill (getpid (), signum);
61 /* Wait for a process to finish, and exit if a nonzero status is found. */
63 int
64 collect_wait (const char *prog, struct pex_obj *pex)
66 int status;
68 if (!pex_get_status (pex, 1, &status))
69 fatal_error (input_location, "cannot get program status: %m");
70 pex_free (pex);
72 if (response_file && !save_temps)
74 unlink (response_file);
75 response_file = NULL;
78 if (status)
80 if (WIFSIGNALED (status))
82 int sig = WTERMSIG (status);
83 fatal_error (input_location, "%s terminated with signal %d [%s]%s",
84 prog, sig, strsignal (sig),
85 WCOREDUMP (status) ? ", core dumped" : "");
88 if (WIFEXITED (status))
89 return WEXITSTATUS (status);
91 return 0;
94 void
95 do_wait (const char *prog, struct pex_obj *pex)
97 int ret = collect_wait (prog, pex);
98 if (ret != 0)
99 fatal_error (input_location, "%s returned %d exit status", prog, ret);
103 /* Execute a program, and wait for the reply. */
105 struct pex_obj *
106 collect_execute (const char *prog, char **argv, const char *outname,
107 const char *errname, int flags, bool use_atfile)
109 struct pex_obj *pex;
110 const char *errmsg;
111 int err;
112 char *response_arg = NULL;
113 char *response_argv[3];
115 if (use_atfile && argv[0] != NULL)
117 /* If using @file arguments, create a temporary file and put the
118 contents of argv into it. Then change argv to an array corresponding
119 to a single argument @FILE, where FILE is the temporary filename. */
121 char **current_argv = argv + 1;
122 char *argv0 = argv[0];
123 int status;
124 FILE *f;
126 /* Note: we assume argv contains at least one element; this is
127 checked above. */
129 response_file = make_temp_file ("");
131 f = fopen (response_file, "w");
133 if (f == NULL)
134 fatal_error (input_location, "could not open response file %s",
135 response_file);
137 status = writeargv (current_argv, f);
139 if (status)
140 fatal_error (input_location, "could not write to response file %s",
141 response_file);
143 status = fclose (f);
145 if (EOF == status)
146 fatal_error (input_location, "could not close response file %s",
147 response_file);
149 response_arg = concat ("@", response_file, NULL);
150 response_argv[0] = argv0;
151 response_argv[1] = response_arg;
152 response_argv[2] = NULL;
154 argv = response_argv;
157 if (verbose || debug)
159 char **p_argv;
160 const char *str;
162 if (argv[0])
163 fprintf (stderr, "%s", argv[0]);
164 else
165 notice ("[cannot find %s]", prog);
167 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
168 fprintf (stderr, " %s", str);
170 fprintf (stderr, "\n");
173 fflush (stdout);
174 fflush (stderr);
176 /* If we cannot find a program we need, complain error. Do this here
177 since we might not end up needing something that we could not find. */
179 if (argv[0] == 0)
180 fatal_error (input_location, "cannot find %qs", prog);
182 pex = pex_init (0, "collect2", NULL);
183 if (pex == NULL)
184 fatal_error (input_location, "%<pex_init%> failed: %m");
186 errmsg = pex_run (pex, flags, argv[0], argv, outname,
187 errname, &err);
188 if (errmsg != NULL)
190 if (err != 0)
192 errno = err;
193 fatal_error (input_location, "%s: %m", _(errmsg));
195 else
196 fatal_error (input_location, errmsg);
199 free (response_arg);
201 return pex;
204 void
205 fork_execute (const char *prog, char **argv, bool use_atfile)
207 struct pex_obj *pex;
209 pex = collect_execute (prog, argv, NULL, NULL,
210 PEX_LAST | PEX_SEARCH, use_atfile);
211 do_wait (prog, pex);
214 /* Delete tempfiles. */
216 void
217 utils_cleanup (bool from_signal)
219 static bool cleanup_done = false;
221 if (cleanup_done)
222 return;
224 /* Setting cleanup_done prevents an infinite loop if one of the
225 calls to maybe_unlink fails. */
226 cleanup_done = true;
228 tool_cleanup (from_signal);