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