annotate gcc/collect-utils.c @ 16:04ced10e8804

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