OSDN Git Service

Fix cross compilation build in gcc-ar
[pf3gnuchains/gcc-fork.git] / gcc / gcc-ar.c
1 /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
2    Copyright (C) 2011 Free Software Foundation, Inc.
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
21 #include <stdio.h>
22 #include "config.h"
23 #include "system.h"
24 #include "libiberty.h"
25
26 #ifndef PERSONALITY
27 #error "Please set personality"
28 #endif
29
30 static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;
31 static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;
32 static const char *const target_machine = TARGET_MACHINE;
33
34 static const char dir_separator[] = { DIR_SEPARATOR, 0 };
35
36 int 
37 main(int ac, char **av)
38 {
39   const char *nprefix;
40   const char *exe_name;
41   char *plugin;
42   int k, status, err;
43   const char *err_msg;
44   const char **nargv;
45   bool is_ar = !strcmp (PERSONALITY, "ar");
46
47   exe_name = PERSONALITY;
48 #ifdef CROSS_DIRECTORY_STRUCTURE
49   exe_name = concat (target_machine, "-", exe_name, NULL);
50 #endif
51
52   /* Find plugin */
53   /* XXX implement more magic from gcc.c? */
54   nprefix = getenv ("GCC_EXEC_PREFIX");
55   if (!nprefix)
56     nprefix = standard_libexec_prefix;
57
58   nprefix = make_relative_prefix (av[0], 
59                                   standard_bin_prefix, 
60                                   nprefix);
61   plugin = concat (nprefix,
62                    dir_separator,
63                    DEFAULT_TARGET_MACHINE, 
64                    dir_separator,
65                    DEFAULT_TARGET_VERSION,
66                    dir_separator,
67                    LTOPLUGINSONAME,
68                    NULL);
69   if (access (plugin, X_OK))
70     {
71       fprintf (stderr, "%s: Cannot find plugin %s\n", av[0], plugin);
72       exit (1);
73     }
74
75   /* Create new command line with plugin */
76   nargv = XCNEWVEC (const char *, ac + 4);
77   nargv[0] = exe_name;
78   nargv[1] = "--plugin";
79   nargv[2] = plugin;
80   if (is_ar && av[1] && av[1][0] != '-')
81     av[1] = concat("-", av[1], NULL);
82   for (k = 1; k < ac; k++)
83     nargv[2 + k] = av[k];
84   nargv[2 + k] = NULL;
85
86   /* Run utility */
87   /* ??? the const is misplaced in pex_one's argv? */
88   err_msg = pex_one (PEX_LAST|PEX_SEARCH, 
89                      exe_name, 
90                      CONST_CAST2 (char * const *, const char **, nargv),
91                      concat("gcc-", exe_name, NULL), 
92                      NULL,NULL,  &status, &err);
93   if (err_msg) 
94     fprintf(stderr, "Error running %s: %s\n", exe_name, err_msg);
95
96   return err;
97 }