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
;
38 /* Delete tempfiles. */
43 static bool cleanup_done
= false;
48 /* Setting cleanup_done prevents an infinite loop if one of the
49 calls to maybe_unlink fails. */
53 maybe_unlink (response_file
);
57 /* Notify user of a non-error. */
59 notice (const char *cmsgid
, ...)
63 va_start (ap
, cmsgid
);
64 vfprintf (stderr
, _(cmsgid
), ap
);
69 fatal_signal (int signum
)
71 signal (signum
, SIG_DFL
);
73 /* Get the same signal again, this time not handled,
74 so its normal effect occurs. */
75 kill (getpid (), signum
);
78 /* Execute a program, and wait for the reply. ARGV are the arguments. The
79 last one must be NULL. */
82 collect_execute (char **argv
)
93 for (p_argv
= argv
; (str
= *p_argv
) != (char *) 0; p_argv
++)
94 fprintf (stderr
, " %s", str
);
96 fprintf (stderr
, "\n");
102 pex
= pex_init (0, tool_name
, NULL
);
104 fatal_error ("pex_init failed: %m");
106 /* Do not use PEX_LAST here, we use our stdout for communicating with
107 collect2 or the linker-plugin. Any output from the sub-process
108 will confuse that. */
109 errmsg
= pex_run (pex
, PEX_SEARCH
, argv
[0], argv
, NULL
,
116 fatal_error ("%s: %m", _(errmsg
));
119 fatal_error (errmsg
);
126 /* Wait for a process to finish, and exit if a nonzero status is found.
127 PROG is the program name. PEX is the process we should wait for. */
130 collect_wait (const char *prog
, struct pex_obj
*pex
)
134 if (!pex_get_status (pex
, 1, &status
))
135 fatal_error ("can't get program status: %m");
140 if (WIFSIGNALED (status
))
142 int sig
= WTERMSIG (status
);
143 if (WCOREDUMP (status
))
144 fatal_error ("%s terminated with signal %d [%s], core dumped",
145 prog
, sig
, strsignal (sig
));
147 fatal_error ("%s terminated with signal %d [%s]",
148 prog
, sig
, strsignal (sig
));
151 if (WIFEXITED (status
))
152 fatal_error ("%s returned %d exit status", prog
, WEXITSTATUS (status
));
159 do_wait (const char *prog
, struct pex_obj
*pex
)
161 int ret
= collect_wait (prog
, pex
);
163 fatal_error ("%s returned %d exit status", prog
, ret
);
165 if (response_file
&& !save_temps
)
167 unlink (response_file
);
168 response_file
= NULL
;
172 /* Unlink a temporary LTRANS file unless requested otherwise. */
175 maybe_unlink_file (const char *file
)
179 if (unlink_if_ordinary (file
)
181 fatal_error ("deleting file %s: %m", file
);
184 fprintf (stderr
, "[Leaving %s]\n", file
);
188 /* Execute program ARGV[0] with arguments ARGV. Wait for it to finish. */
191 fork_execute (char **argv
)
199 response_file
= make_temp_file (".args");
200 at_args
= concat ("@", response_file
, NULL
);
201 args
= fopen (response_file
, "w");
203 fatal_error ("failed to open %s", response_file
);
205 status
= writeargv (&argv
[1], args
);
208 fatal_error ("could not write to temporary file %s", response_file
);
212 new_argv
[0] = argv
[0];
213 new_argv
[1] = at_args
;
216 pex
= collect_execute (new_argv
);
217 do_wait (new_argv
[0], pex
);