collect-utils.c 5.3 KB

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