gcc-ar.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
  2. Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. Contributed by Andi Kleen.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "libiberty.h"
  19. #include "file-find.h"
  20. #ifndef PERSONALITY
  21. #error "Please set personality"
  22. #endif
  23. /* The exec prefix as derived at compile-time from --prefix. */
  24. static const char standard_exec_prefix[] = STANDARD_EXEC_PREFIX;
  25. /* The libexec prefix as derived at compile-time from --prefix. */
  26. static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;
  27. /* The bindir prefix as derived at compile-time from --prefix. */
  28. static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;
  29. /* A relative path to be used in finding the location of tools
  30. relative to this program. */
  31. static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
  32. /* The exec prefix as relocated from the location of this program. */
  33. static const char *self_exec_prefix;
  34. /* The libexec prefix as relocated from the location of this program. */
  35. static const char *self_libexec_prefix;
  36. /* The tools prefix as relocated from the location of this program. */
  37. static const char *self_tooldir_prefix;
  38. /* The name of the machine that is being targeted. */
  39. static const char *const target_machine = DEFAULT_TARGET_MACHINE;
  40. /* The target version. */
  41. static const char *const target_version = DEFAULT_TARGET_VERSION;
  42. /* The collection of target specific path prefixes. */
  43. static struct path_prefix target_path;
  44. /* The collection path prefixes. */
  45. static struct path_prefix path;
  46. /* The directory separator. */
  47. static const char dir_separator[] = { DIR_SEPARATOR, 0 };
  48. static void
  49. setup_prefixes (const char *exec_path)
  50. {
  51. const char *self;
  52. self = getenv ("GCC_EXEC_PREFIX");
  53. if (!self)
  54. self = exec_path;
  55. else
  56. self = concat (self, "gcc-" PERSONALITY, NULL);
  57. /* Relocate the exec prefix. */
  58. self_exec_prefix = make_relative_prefix (self,
  59. standard_bin_prefix,
  60. standard_exec_prefix);
  61. if (self_exec_prefix == NULL)
  62. self_exec_prefix = standard_exec_prefix;
  63. /* Relocate libexec prefix. */
  64. self_libexec_prefix = make_relative_prefix (self,
  65. standard_bin_prefix,
  66. standard_libexec_prefix);
  67. if (self_libexec_prefix == NULL)
  68. self_libexec_prefix = standard_libexec_prefix;
  69. /* Build the relative path to the target-specific tool directory. */
  70. self_tooldir_prefix = concat (tooldir_base_prefix, target_machine,
  71. dir_separator, NULL);
  72. self_tooldir_prefix = concat (self_exec_prefix, target_machine,
  73. dir_separator, target_version, dir_separator,
  74. self_tooldir_prefix, NULL);
  75. /* Add the target-specific tool bin prefix. */
  76. prefix_from_string (concat (self_tooldir_prefix, "bin", NULL), &target_path);
  77. /* Add the target-specific libexec prefix. */
  78. self_libexec_prefix = concat (self_libexec_prefix, target_machine,
  79. dir_separator, target_version,
  80. dir_separator, NULL);
  81. prefix_from_string (self_libexec_prefix, &target_path);
  82. /* Add path as a last resort. */
  83. prefix_from_env ("PATH", &path);
  84. }
  85. int
  86. main (int ac, char **av)
  87. {
  88. const char *exe_name;
  89. char *plugin;
  90. int k, status, err;
  91. const char *err_msg;
  92. const char **nargv;
  93. bool is_ar = !strcmp (PERSONALITY, "ar");
  94. int exit_code = FATAL_EXIT_CODE;
  95. int i;
  96. setup_prefixes (av[0]);
  97. /* Not using getopt for now. */
  98. for (i = 0; i < ac; i++)
  99. if (!strncmp (av[i], "-B", 2))
  100. {
  101. const char *arg = av[i] + 2;
  102. const char *end;
  103. size_t len;
  104. memmove (av + i, av + i + 1, sizeof (char *) * ((ac + 1) - i));
  105. ac--;
  106. if (*arg == 0)
  107. {
  108. arg = av[i];
  109. if (!arg)
  110. {
  111. fprintf (stderr, "Usage: gcc-ar [-B prefix] ar arguments ...\n");
  112. exit (EXIT_FAILURE);
  113. }
  114. memmove (av + i, av + i + 1, sizeof (char *) * ((ac + 1) - i));
  115. ac--;
  116. i++;
  117. }
  118. /* else it's a joined argument */
  119. len = strlen (arg);
  120. if (len > 0)
  121. len--;
  122. end = arg + len;
  123. /* Always add a dir separator for the prefix list. */
  124. if (end > arg && !IS_DIR_SEPARATOR (*end))
  125. {
  126. static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
  127. arg = concat (arg, dir_separator_str, NULL);
  128. }
  129. add_prefix_begin (&path, arg);
  130. add_prefix_begin (&target_path, arg);
  131. break;
  132. }
  133. /* Find the GCC LTO plugin */
  134. plugin = find_a_file (&target_path, LTOPLUGINSONAME, R_OK);
  135. if (!plugin)
  136. {
  137. fprintf (stderr, "%s: Cannot find plugin '%s'\n", av[0], LTOPLUGINSONAME);
  138. exit (1);
  139. }
  140. /* Find the wrapped binutils program. */
  141. exe_name = find_a_file (&target_path, PERSONALITY, X_OK);
  142. if (!exe_name)
  143. {
  144. const char *real_exe_name = PERSONALITY;
  145. #ifdef CROSS_DIRECTORY_STRUCTURE
  146. real_exe_name = concat (target_machine, "-", PERSONALITY, NULL);
  147. #endif
  148. exe_name = find_a_file (&path, real_exe_name, X_OK);
  149. if (!exe_name)
  150. {
  151. fprintf (stderr, "%s: Cannot find binary '%s'\n", av[0],
  152. real_exe_name);
  153. exit (1);
  154. }
  155. }
  156. /* Create new command line with plugin */
  157. nargv = XCNEWVEC (const char *, ac + 4);
  158. nargv[0] = exe_name;
  159. nargv[1] = "--plugin";
  160. nargv[2] = plugin;
  161. if (is_ar && av[1] && av[1][0] != '-')
  162. av[1] = concat ("-", av[1], NULL);
  163. for (k = 1; k < ac; k++)
  164. nargv[2 + k] = av[k];
  165. nargv[2 + k] = NULL;
  166. /* Run utility */
  167. /* ??? the const is misplaced in pex_one's argv? */
  168. err_msg = pex_one (PEX_LAST|PEX_SEARCH,
  169. exe_name,
  170. CONST_CAST2 (char * const *, const char **, nargv),
  171. concat ("gcc-", exe_name, NULL),
  172. NULL,NULL, &status, &err);
  173. if (err_msg)
  174. fprintf (stderr, "Error running %s: %s\n", exe_name, err_msg);
  175. else if (status)
  176. {
  177. if (WIFSIGNALED (status))
  178. {
  179. int sig = WTERMSIG (status);
  180. fprintf (stderr, "%s terminated with signal %d [%s]%s\n",
  181. exe_name, sig, strsignal (sig),
  182. WCOREDUMP (status) ? ", core dumped" : "");
  183. }
  184. else if (WIFEXITED (status))
  185. exit_code = WEXITSTATUS (status);
  186. }
  187. else
  188. exit_code = SUCCESS_EXIT_CODE;
  189. return exit_code;
  190. }