gcc/ChangeLog
[official-gcc.git] / gcc / gcc-ar.c
blobbb6ed9fb78e8f2ca0d16a0b958296d60301f165e
1 /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
2 Copyright (C) 2011-2013 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
10 version.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "libiberty.h"
24 #include "file-find.h"
26 #ifndef PERSONALITY
27 #error "Please set personality"
28 #endif
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 };
79 static void
80 setup_prefixes (const char *exec_path)
82 const char *self;
84 self = getenv ("GCC_EXEC_PREFIX");
85 if (!self)
86 self = exec_path;
87 else
88 self = concat (self, "gcc-" PERSONALITY, NULL);
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;
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;
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);
125 int
126 main(int ac, char **av)
128 const char *exe_name;
129 char *plugin;
130 int k, status, err;
131 const char *err_msg;
132 const char **nargv;
133 bool is_ar = !strcmp (PERSONALITY, "ar");
134 int exit_code = FATAL_EXIT_CODE;
136 setup_prefixes (av[0]);
138 /* Find the GCC LTO plugin */
139 plugin = find_a_file (&target_path, LTOPLUGINSONAME);
140 if (!plugin)
142 fprintf (stderr, "%s: Cannot find plugin '%s'\n", av[0], LTOPLUGINSONAME);
143 exit (1);
146 /* Find the wrapped binutils program. */
147 exe_name = find_a_file (&target_path, PERSONALITY);
148 if (!exe_name)
150 #ifdef CROSS_DIRECTORY_STRUCTURE
151 const char *cross_exe_name;
153 cross_exe_name = concat (target_machine, "-", PERSONALITY, NULL);
154 exe_name = find_a_file (&path, cross_exe_name);
155 if (!exe_name)
157 fprintf (stderr, "%s: Cannot find binary '%s'\n", av[0],
158 cross_exe_name);
159 exit (1);
161 #else
162 fprintf (stderr, "%s: Cannot find binary '%s'\n", av[0], PERSONALITY);
163 exit (1);
164 #endif
167 /* Create new command line with plugin */
168 nargv = XCNEWVEC (const char *, ac + 4);
169 nargv[0] = exe_name;
170 nargv[1] = "--plugin";
171 nargv[2] = plugin;
172 if (is_ar && av[1] && av[1][0] != '-')
173 av[1] = concat("-", av[1], NULL);
174 for (k = 1; k < ac; k++)
175 nargv[2 + k] = av[k];
176 nargv[2 + k] = NULL;
178 /* Run utility */
179 /* ??? the const is misplaced in pex_one's argv? */
180 err_msg = pex_one (PEX_LAST|PEX_SEARCH,
181 exe_name,
182 CONST_CAST2 (char * const *, const char **, nargv),
183 concat("gcc-", exe_name, NULL),
184 NULL,NULL, &status, &err);
185 if (err_msg)
186 fprintf(stderr, "Error running %s: %s\n", exe_name, err_msg);
187 else if (status)
189 if (WIFSIGNALED (status))
191 int sig = WTERMSIG (status);
192 fprintf (stderr, "%s terminated with signal %d [%s]%s\n",
193 exe_name, sig, strsignal(sig),
194 WCOREDUMP(status) ? ", core dumped" : "");
196 else if (WIFEXITED (status))
197 exit_code = WEXITSTATUS (status);
199 else
200 exit_code = SUCCESS_EXIT_CODE;
202 return exit_code;