1 /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
3 Contributed by Andi Kleen.
5 This file is part of GCC.
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
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
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/>. */
23 #include "libiberty.h"
24 #include "file-find.h"
27 #error "Please set personality"
30 /* The exec prefix as derived at compile-time from --prefix. */
32 static const char standard_exec_prefix
[] = STANDARD_EXEC_PREFIX
;
34 /* The libexec prefix as derived at compile-time from --prefix. */
36 static const char standard_libexec_prefix
[] = STANDARD_LIBEXEC_PREFIX
;
38 /* The bindir prefix as derived at compile-time from --prefix. */
40 static const char standard_bin_prefix
[] = STANDARD_BINDIR_PREFIX
;
42 /* A relative path to be used in finding the location of tools
43 relative to this program. */
45 static const char *const tooldir_base_prefix
= TOOLDIR_BASE_PREFIX
;
47 /* The exec prefix as relocated from the location of this program. */
49 static const char *self_exec_prefix
;
51 /* The libexec prefix as relocated from the location of this program. */
53 static const char *self_libexec_prefix
;
55 /* The tools prefix as relocated from the location of this program. */
57 static const char *self_tooldir_prefix
;
59 /* The name of the machine that is being targeted. */
61 static const char *const target_machine
= DEFAULT_TARGET_MACHINE
;
63 /* The target version. */
65 static const char *const target_version
= DEFAULT_TARGET_VERSION
;
67 /* The collection of target specific path prefixes. */
69 static struct path_prefix target_path
;
71 /* The collection path prefixes. */
73 static struct path_prefix path
;
75 /* The directory separator. */
77 static const char dir_separator
[] = { DIR_SEPARATOR
, 0 };
80 setup_prefixes (const char *exec_path
)
84 self
= getenv ("GCC_EXEC_PREFIX");
88 self
= concat (self
, "gcc-" PERSONALITY
, NULL
);
90 /* Relocate the exec prefix. */
91 self_exec_prefix
= make_relative_prefix (self
,
93 standard_exec_prefix
);
94 if (self_exec_prefix
== NULL
)
95 self_exec_prefix
= standard_exec_prefix
;
97 /* Relocate libexec prefix. */
98 self_libexec_prefix
= make_relative_prefix (self
,
100 standard_libexec_prefix
);
101 if (self_libexec_prefix
== NULL
)
102 self_libexec_prefix
= standard_libexec_prefix
;
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
);
112 /* Add the target-specific tool bin prefix. */
113 prefix_from_string (concat (self_tooldir_prefix
, "bin", NULL
), &target_path
);
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
);
121 /* Add path as a last resort. */
122 prefix_from_env ("PATH", &path
);
126 main (int ac
, char **av
)
128 const char *exe_name
;
129 #if HAVE_LTO_PLUGIN > 0
131 const int j
= 2; /* Two extra args, --plugin <plugin> */
133 const int j
= 0; /* No extra args. */
139 const char *rsp_file
= NULL
;
140 const char *rsp_arg
= NULL
;
141 const char *rsp_argv
[3];
142 bool is_ar
= !strcmp (PERSONALITY
, "ar");
143 int exit_code
= FATAL_EXIT_CODE
;
146 setup_prefixes (av
[0]);
148 /* Not using getopt for now. */
149 for (i
= 0; i
< ac
; i
++)
150 if (startswith (av
[i
], "-B"))
152 const char *arg
= av
[i
] + 2;
156 memmove (av
+ i
, av
+ i
+ 1, sizeof (char *) * ((ac
+ 1) - i
));
163 fprintf (stderr
, "Usage: gcc-ar [-B prefix] ar arguments ...\n");
166 memmove (av
+ i
, av
+ i
+ 1, sizeof (char *) * ((ac
+ 1) - i
));
170 /* else it's a joined argument */
177 /* Always add a dir separator for the prefix list. */
178 if (end
> arg
&& !IS_DIR_SEPARATOR (*end
))
180 static const char dir_separator_str
[] = { DIR_SEPARATOR
, 0 };
181 arg
= concat (arg
, dir_separator_str
, NULL
);
184 add_prefix_begin (&path
, arg
);
185 add_prefix_begin (&target_path
, arg
);
189 #if HAVE_LTO_PLUGIN > 0
190 /* Find the GCC LTO plugin */
191 plugin
= find_a_file (&target_path
, LTOPLUGINSONAME
, R_OK
);
194 fprintf (stderr
, "%s: Cannot find plugin '%s'\n", av
[0], LTOPLUGINSONAME
);
199 /* Find the wrapped binutils program. */
200 exe_name
= find_a_file (&target_path
, PERSONALITY
, X_OK
);
203 const char *real_exe_name
= PERSONALITY
;
204 #ifdef CROSS_DIRECTORY_STRUCTURE
205 real_exe_name
= concat (target_machine
, "-", PERSONALITY
, NULL
);
207 exe_name
= find_a_file (&path
, real_exe_name
, X_OK
);
210 fprintf (stderr
, "%s: Cannot find binary '%s'\n", av
[0],
216 /* Expand any @files before modifying the command line
217 and use a temporary response file if there were any. */
219 expandargv (&ac
, &av
);
221 rsp_file
= make_temp_file ("");
223 /* Prepend - if necessary. */
224 if (is_ar
&& av
[1] && av
[1][0] != '-')
225 av
[1] = concat ("-", av
[1], NULL
);
227 /* Create new command line with plugin - if we have one, otherwise just
228 copy the command through. */
229 nargv
= XCNEWVEC (const char *, ac
+ j
+ 1); /* +j plugin args +1 for NULL. */
231 #if HAVE_LTO_PLUGIN > 0
232 nargv
[1] = "--plugin";
235 for (k
= 1; k
< ac
; k
++)
236 nargv
[j
+ k
] = av
[k
];
239 /* If @file was passed, put nargv into the temporary response
240 file and then change it to a single @FILE argument, where
241 FILE is the temporary filename. */
246 f
= fopen (rsp_file
, "w");
249 fprintf (stderr
, "Cannot open temporary file %s\n", rsp_file
);
253 CONST_CAST2 (char * const *, const char **, nargv
) + 1, f
);
256 fprintf (stderr
, "Cannot write to temporary file %s\n", rsp_file
);
262 fprintf (stderr
, "Cannot close temporary file %s\n", rsp_file
);
265 rsp_arg
= concat ("@", rsp_file
, NULL
);
266 rsp_argv
[0] = nargv
[0];
267 rsp_argv
[1] = rsp_arg
;
273 /* ??? the const is misplaced in pex_one's argv? */
274 err_msg
= pex_one (PEX_LAST
|PEX_SEARCH
,
276 CONST_CAST2 (char * const *, const char **, nargv
),
277 concat ("gcc-", exe_name
, NULL
),
278 NULL
,NULL
, &status
, &err
);
280 fprintf (stderr
, "Error running %s: %s\n", exe_name
, err_msg
);
283 if (WIFSIGNALED (status
))
285 int sig
= WTERMSIG (status
);
286 fprintf (stderr
, "%s terminated with signal %d [%s]%s\n",
287 exe_name
, sig
, strsignal (sig
),
288 WCOREDUMP (status
) ? ", core dumped" : "");
290 else if (WIFEXITED (status
))
291 exit_code
= WEXITSTATUS (status
);
294 exit_code
= SUCCESS_EXIT_CODE
;