]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/collect-utils.c
Part 2 of the collect-utils library, now also used for collect2.
[thirdparty/gcc.git] / gcc / collect-utils.c
CommitLineData
a185856a
BS
1/* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
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"
31
32static char *response_file;
33
34bool debug;
35bool verbose;
608508a6 36bool save_temps;
a185856a 37
a185856a
BS
38
39/* Notify user of a non-error. */
40void
41notice (const char *cmsgid, ...)
42{
43 va_list ap;
44
45 va_start (ap, cmsgid);
46 vfprintf (stderr, _(cmsgid), ap);
47 va_end (ap);
48}
49
50void
51fatal_signal (int signum)
52{
53 signal (signum, SIG_DFL);
5f0ad6a5 54 utils_cleanup (true);
a185856a
BS
55 /* Get the same signal again, this time not handled,
56 so its normal effect occurs. */
57 kill (getpid (), signum);
58}
5f0ad6a5
BS
59\f
60/* Wait for a process to finish, and exit if a nonzero status is found. */
a185856a
BS
61
62int
63collect_wait (const char *prog, struct pex_obj *pex)
64{
65 int status;
66
67 if (!pex_get_status (pex, 1, &status))
68 fatal_error ("can't get program status: %m");
69 pex_free (pex);
70
71 if (status)
72 {
73 if (WIFSIGNALED (status))
74 {
75 int sig = WTERMSIG (status);
5f0ad6a5
BS
76 fatal_error ("%s terminated with signal %d [%s]%s",
77 prog, sig, strsignal (sig),
78 WCOREDUMP (status) ? ", core dumped" : "");
a185856a
BS
79 }
80
81 if (WIFEXITED (status))
5f0ad6a5 82 return WEXITSTATUS (status);
a185856a 83 }
a185856a
BS
84 return 0;
85}
86
87void
88do_wait (const char *prog, struct pex_obj *pex)
89{
90 int ret = collect_wait (prog, pex);
91 if (ret != 0)
608508a6 92 fatal_error ("%s returned %d exit status", prog, ret);
a185856a 93
608508a6 94 if (response_file && !save_temps)
a185856a
BS
95 {
96 unlink (response_file);
97 response_file = NULL;
98 }
99}
100
5f0ad6a5
BS
101\f
102/* Execute a program, and wait for the reply. */
a185856a 103
5f0ad6a5
BS
104struct pex_obj *
105collect_execute (const char *prog, char **argv, const char *outname,
106 const char *errname, int flags, bool use_atfile)
a185856a 107{
5f0ad6a5
BS
108 struct pex_obj *pex;
109 const char *errmsg;
110 int err;
111 char *response_arg = NULL;
112 char *response_argv[3];
113
114 if (use_atfile && argv[0] != NULL)
a185856a 115 {
5f0ad6a5
BS
116 /* If using @file arguments, create a temporary file and put the
117 contents of argv into it. Then change argv to an array corresponding
118 to a single argument @FILE, where FILE is the temporary filename. */
119
120 char **current_argv = argv + 1;
121 char *argv0 = argv[0];
122 int status;
123 FILE *f;
124
125 /* Note: we assume argv contains at least one element; this is
126 checked above. */
127
128 response_file = make_temp_file ("");
129
130 f = fopen (response_file, "w");
131
132 if (f == NULL)
133 fatal_error ("could not open response file %s", response_file);
134
135 status = writeargv (current_argv, f);
136
137 if (status)
138 fatal_error ("could not write to response file %s", response_file);
139
140 status = fclose (f);
141
142 if (EOF == status)
143 fatal_error ("could not close response file %s", response_file);
144
145 response_arg = concat ("@", response_file, NULL);
146 response_argv[0] = argv0;
147 response_argv[1] = response_arg;
148 response_argv[2] = NULL;
149
150 argv = response_argv;
a185856a 151 }
a185856a 152
5f0ad6a5
BS
153 if (verbose || debug)
154 {
155 char **p_argv;
156 const char *str;
157
158 if (argv[0])
159 fprintf (stderr, "%s", argv[0]);
160 else
161 notice ("[cannot find %s]", prog);
162
163 for (p_argv = &argv[1]; (str = *p_argv) != (char *) 0; p_argv++)
164 fprintf (stderr, " %s", str);
165
166 fprintf (stderr, "\n");
167 }
168
169 fflush (stdout);
170 fflush (stderr);
171
172 /* If we cannot find a program we need, complain error. Do this here
173 since we might not end up needing something that we could not find. */
174
175 if (argv[0] == 0)
176 fatal_error ("cannot find '%s'", prog);
177
178 pex = pex_init (0, "collect2", NULL);
179 if (pex == NULL)
180 fatal_error ("pex_init failed: %m");
181
182 errmsg = pex_run (pex, flags, argv[0], argv, outname,
183 errname, &err);
184 if (errmsg != NULL)
185 {
186 if (err != 0)
187 {
188 errno = err;
189 fatal_error ("%s: %m", _(errmsg));
190 }
191 else
192 fatal_error (errmsg);
193 }
a185856a 194
5f0ad6a5
BS
195 free (response_arg);
196
197 return pex;
198}
a185856a
BS
199
200void
5f0ad6a5 201fork_execute (const char *prog, char **argv, bool use_atfile)
a185856a
BS
202{
203 struct pex_obj *pex;
a185856a 204
5f0ad6a5
BS
205 pex = collect_execute (prog, argv, NULL, NULL,
206 PEX_LAST | PEX_SEARCH, use_atfile);
207 do_wait (prog, pex);
208}
a185856a 209
5f0ad6a5 210/* Delete tempfiles. */
a185856a 211
5f0ad6a5
BS
212void
213utils_cleanup (bool from_signal)
214{
215 static bool cleanup_done = false;
a185856a 216
5f0ad6a5
BS
217 if (cleanup_done)
218 return;
a185856a 219
5f0ad6a5
BS
220 /* Setting cleanup_done prevents an infinite loop if one of the
221 calls to maybe_unlink fails. */
222 cleanup_done = true;
a185856a 223
5f0ad6a5
BS
224 if (response_file)
225 maybe_unlink (response_file);
226 tool_cleanup (from_signal);
a185856a 227}