Merge trunk version 190524 into gupc branch.
[official-gcc.git] / gcc / gcc-ar.c
blobcaae1670bf6012dd0c3231b45a05eac9903050ce
1 /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
2 Copyright (C) 2011, 2012 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"
25 #ifndef PERSONALITY
26 #error "Please set personality"
27 #endif
29 static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;
30 static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;
31 static const char *const target_machine = TARGET_MACHINE;
33 static const char dir_separator[] = { DIR_SEPARATOR, 0 };
35 int
36 main(int ac, char **av)
38 const char *nprefix;
39 const char *exe_name;
40 char *plugin;
41 int k, status, err;
42 const char *err_msg;
43 const char **nargv;
44 bool is_ar = !strcmp (PERSONALITY, "ar");
46 exe_name = PERSONALITY;
47 #ifdef CROSS_DIRECTORY_STRUCTURE
48 exe_name = concat (target_machine, "-", exe_name, NULL);
49 #endif
51 /* Find plugin */
52 /* XXX implement more magic from gcc.c? */
53 nprefix = getenv ("GCC_EXEC_PREFIX");
54 if (!nprefix)
55 nprefix = av[0];
56 else
57 nprefix = concat (nprefix, "gcc-" PERSONALITY, NULL);
59 nprefix = make_relative_prefix (nprefix,
60 standard_bin_prefix,
61 standard_libexec_prefix);
62 if (nprefix == NULL)
63 nprefix = standard_libexec_prefix;
65 plugin = concat (nprefix,
66 dir_separator,
67 DEFAULT_TARGET_MACHINE,
68 dir_separator,
69 DEFAULT_TARGET_VERSION,
70 dir_separator,
71 LTOPLUGINSONAME,
72 NULL);
73 if (access (plugin, R_OK))
75 fprintf (stderr, "%s: Cannot find plugin %s\n", av[0], plugin);
76 exit (1);
79 /* Create new command line with plugin */
80 nargv = XCNEWVEC (const char *, ac + 4);
81 nargv[0] = exe_name;
82 nargv[1] = "--plugin";
83 nargv[2] = plugin;
84 if (is_ar && av[1] && av[1][0] != '-')
85 av[1] = concat("-", av[1], NULL);
86 for (k = 1; k < ac; k++)
87 nargv[2 + k] = av[k];
88 nargv[2 + k] = NULL;
90 /* Run utility */
91 /* ??? the const is misplaced in pex_one's argv? */
92 err_msg = pex_one (PEX_LAST|PEX_SEARCH,
93 exe_name,
94 CONST_CAST2 (char * const *, const char **, nargv),
95 concat("gcc-", exe_name, NULL),
96 NULL,NULL, &status, &err);
97 if (err_msg)
98 fprintf(stderr, "Error running %s: %s\n", exe_name, err_msg);
100 return err;