testsuite: Update pr79251 ilp32 store counts.
[official-gcc.git] / gcc / collect-utils.c
blob6b5d61d51622ef69ad000b4cbe812104bd0b5ae1
1 /* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2021 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,
108 const char *atsuffix)
110 struct pex_obj *pex;
111 const char *errmsg;
112 int err;
113 char *response_arg = NULL;
114 char *response_argv[3];
116 if (use_atfile && argv[0] != NULL)
118 /* If using @file arguments, create a temporary file and put the
119 contents of argv into it. Then change argv to an array corresponding
120 to a single argument @FILE, where FILE is the temporary filename. */
122 char **current_argv = argv + 1;
123 char *argv0 = argv[0];
124 int status;
125 FILE *f;
127 /* Note: we assume argv contains at least one element; this is
128 checked above. */
130 if (!save_temps || !atsuffix || !dumppfx)
131 response_file = make_temp_file ("");
132 else
133 response_file = concat (dumppfx, atsuffix, NULL);
135 f = fopen (response_file, "w");
137 if (f == NULL)
138 fatal_error (input_location, "could not open response file %s",
139 response_file);
141 status = writeargv (current_argv, f);
143 if (status)
144 fatal_error (input_location, "could not write to response file %s",
145 response_file);
147 status = fclose (f);
149 if (EOF == status)
150 fatal_error (input_location, "could not close response file %s",
151 response_file);
153 response_arg = concat ("@", response_file, NULL);
154 response_argv[0] = argv0;
155 response_argv[1] = response_arg;
156 response_argv[2] = NULL;
158 argv = response_argv;
161 if (verbose || debug)
163 char **p_argv;
164 const char *str;
166 if (argv[0])
167 fprintf (stderr, "%s", argv[0]);
168 else
169 notice ("[cannot find %s]", prog);
171 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
172 fprintf (stderr, " %s", str);
174 fprintf (stderr, "\n");
177 fflush (stdout);
178 fflush (stderr);
180 /* If we cannot find a program we need, complain error. Do this here
181 since we might not end up needing something that we could not find. */
183 if (argv[0] == 0)
184 fatal_error (input_location, "cannot find %qs", prog);
186 pex = pex_init (0, "collect2", NULL);
187 if (pex == NULL)
188 fatal_error (input_location, "%<pex_init%> failed: %m");
190 errmsg = pex_run (pex, flags, argv[0], argv, outname,
191 errname, &err);
192 if (errmsg != NULL)
194 if (err != 0)
196 errno = err;
197 fatal_error (input_location, "%s: %m", _(errmsg));
199 else
200 fatal_error (input_location, errmsg);
203 free (response_arg);
205 return pex;
208 void
209 fork_execute (const char *prog, char **argv, bool use_atfile,
210 const char *atsuffix)
212 struct pex_obj *pex;
214 pex = collect_execute (prog, argv, NULL, NULL,
215 PEX_LAST | PEX_SEARCH, use_atfile, atsuffix);
216 do_wait (prog, pex);
219 /* Delete tempfiles. */
221 void
222 utils_cleanup (bool from_signal)
224 static bool cleanup_done = false;
226 if (cleanup_done)
227 return;
229 /* Setting cleanup_done prevents an infinite loop if one of the
230 calls to maybe_unlink fails. */
231 cleanup_done = true;
233 tool_cleanup (from_signal);