1 /* Utility functions used by tools like collect2 and lto-wrapper.
2 Copyright (C) 2009-2014 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
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
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/>. */
22 #include "coretypes.h"
24 #include "diagnostic.h"
28 #include "simple-object.h"
29 #include "lto-section-names.h"
30 #include "collect-utils.h"
32 static char *response_file
;
39 /* Notify user of a non-error. */
41 notice (const char *cmsgid
, ...)
45 va_start (ap
, cmsgid
);
46 vfprintf (stderr
, _(cmsgid
), ap
);
51 fatal_signal (int signum
)
53 signal (signum
, SIG_DFL
);
55 /* Get the same signal again, this time not handled,
56 so its normal effect occurs. */
57 kill (getpid (), signum
);
60 /* Wait for a process to finish, and exit if a nonzero status is found. */
63 collect_wait (const char *prog
, struct pex_obj
*pex
)
67 if (!pex_get_status (pex
, 1, &status
))
68 fatal_error ("can't get program status: %m");
73 if (WIFSIGNALED (status
))
75 int sig
= WTERMSIG (status
);
76 fatal_error ("%s terminated with signal %d [%s]%s",
77 prog
, sig
, strsignal (sig
),
78 WCOREDUMP (status
) ? ", core dumped" : "");
81 if (WIFEXITED (status
))
82 return WEXITSTATUS (status
);
88 do_wait (const char *prog
, struct pex_obj
*pex
)
90 int ret
= collect_wait (prog
, pex
);
92 fatal_error ("%s returned %d exit status", prog
, ret
);
94 if (response_file
&& !save_temps
)
96 unlink (response_file
);
102 /* Execute a program, and wait for the reply. */
105 collect_execute (const char *prog
, char **argv
, const char *outname
,
106 const char *errname
, int flags
, bool use_atfile
)
111 char *response_arg
= NULL
;
112 char *response_argv
[3];
114 if (use_atfile
&& argv
[0] != NULL
)
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. */
120 char **current_argv
= argv
+ 1;
121 char *argv0
= argv
[0];
125 /* Note: we assume argv contains at least one element; this is
128 response_file
= make_temp_file ("");
130 f
= fopen (response_file
, "w");
133 fatal_error ("could not open response file %s", response_file
);
135 status
= writeargv (current_argv
, f
);
138 fatal_error ("could not write to response file %s", response_file
);
143 fatal_error ("could not close response file %s", response_file
);
145 response_arg
= concat ("@", response_file
, NULL
);
146 response_argv
[0] = argv0
;
147 response_argv
[1] = response_arg
;
148 response_argv
[2] = NULL
;
150 argv
= response_argv
;
153 if (verbose
|| debug
)
159 fprintf (stderr
, "%s", argv
[0]);
161 notice ("[cannot find %s]", prog
);
163 for (p_argv
= &argv
[1]; (str
= *p_argv
) != (char *) 0; p_argv
++)
164 fprintf (stderr
, " %s", str
);
166 fprintf (stderr
, "\n");
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. */
176 fatal_error ("cannot find '%s'", prog
);
178 pex
= pex_init (0, "collect2", NULL
);
180 fatal_error ("pex_init failed: %m");
182 errmsg
= pex_run (pex
, flags
, argv
[0], argv
, outname
,
189 fatal_error ("%s: %m", _(errmsg
));
192 fatal_error (errmsg
);
201 fork_execute (const char *prog
, char **argv
, bool use_atfile
)
205 pex
= collect_execute (prog
, argv
, NULL
, NULL
,
206 PEX_LAST
| PEX_SEARCH
, use_atfile
);
210 /* Delete tempfiles. */
213 utils_cleanup (bool from_signal
)
215 static bool cleanup_done
= false;
220 /* Setting cleanup_done prevents an infinite loop if one of the
221 calls to maybe_unlink fails. */
225 maybe_unlink (response_file
);
226 tool_cleanup (from_signal
);