1e4b17023SJohn Marino /* Wrapper for ar/ranlib/nm to pass the LTO plugin.
2e4b17023SJohn Marino Copyright (C) 2011, 2012 Free Software Foundation, Inc.
3e4b17023SJohn Marino Contributed by Andi Kleen.
4e4b17023SJohn Marino
5e4b17023SJohn Marino This file is part of GCC.
6e4b17023SJohn Marino
7e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10e4b17023SJohn Marino version.
11e4b17023SJohn Marino
12e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15e4b17023SJohn Marino for more details.
16e4b17023SJohn Marino
17e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20e4b17023SJohn Marino
21e4b17023SJohn Marino #include "config.h"
22e4b17023SJohn Marino #include "system.h"
23e4b17023SJohn Marino #include "libiberty.h"
24e4b17023SJohn Marino
25e4b17023SJohn Marino #ifndef PERSONALITY
26e4b17023SJohn Marino #error "Please set personality"
27e4b17023SJohn Marino #endif
28e4b17023SJohn Marino
29e4b17023SJohn Marino static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;
30e4b17023SJohn Marino static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;
31e4b17023SJohn Marino static const char *const target_machine = TARGET_MACHINE;
32e4b17023SJohn Marino
33e4b17023SJohn Marino static const char dir_separator[] = { DIR_SEPARATOR, 0 };
34e4b17023SJohn Marino
35e4b17023SJohn Marino int
main(int ac,char ** av)36e4b17023SJohn Marino main(int ac, char **av)
37e4b17023SJohn Marino {
38e4b17023SJohn Marino const char *nprefix;
39e4b17023SJohn Marino const char *exe_name;
40e4b17023SJohn Marino char *plugin;
41e4b17023SJohn Marino int k, status, err;
42e4b17023SJohn Marino const char *err_msg;
43e4b17023SJohn Marino const char **nargv;
44e4b17023SJohn Marino bool is_ar = !strcmp (PERSONALITY, "ar");
45*5ce9237cSJohn Marino int exit_code = FATAL_EXIT_CODE;
46e4b17023SJohn Marino
47e4b17023SJohn Marino exe_name = PERSONALITY;
48e4b17023SJohn Marino #ifdef CROSS_DIRECTORY_STRUCTURE
49e4b17023SJohn Marino exe_name = concat (target_machine, "-", exe_name, NULL);
50e4b17023SJohn Marino #endif
51e4b17023SJohn Marino
52e4b17023SJohn Marino /* Find plugin */
53e4b17023SJohn Marino /* XXX implement more magic from gcc.c? */
54e4b17023SJohn Marino nprefix = getenv ("GCC_EXEC_PREFIX");
55e4b17023SJohn Marino if (!nprefix)
56e4b17023SJohn Marino nprefix = av[0];
57e4b17023SJohn Marino else
58e4b17023SJohn Marino nprefix = concat (nprefix, "gcc-" PERSONALITY, NULL);
59e4b17023SJohn Marino
60e4b17023SJohn Marino nprefix = make_relative_prefix (nprefix,
61e4b17023SJohn Marino standard_bin_prefix,
62e4b17023SJohn Marino standard_libexec_prefix);
63e4b17023SJohn Marino if (nprefix == NULL)
64e4b17023SJohn Marino nprefix = standard_libexec_prefix;
65e4b17023SJohn Marino
66e4b17023SJohn Marino plugin = concat (nprefix,
67e4b17023SJohn Marino dir_separator,
68e4b17023SJohn Marino DEFAULT_TARGET_MACHINE,
69e4b17023SJohn Marino dir_separator,
70e4b17023SJohn Marino DEFAULT_TARGET_VERSION,
71e4b17023SJohn Marino dir_separator,
72e4b17023SJohn Marino LTOPLUGINSONAME,
73e4b17023SJohn Marino NULL);
74e4b17023SJohn Marino if (access (plugin, R_OK))
75e4b17023SJohn Marino {
76e4b17023SJohn Marino fprintf (stderr, "%s: Cannot find plugin %s\n", av[0], plugin);
77e4b17023SJohn Marino exit (1);
78e4b17023SJohn Marino }
79e4b17023SJohn Marino
80e4b17023SJohn Marino /* Create new command line with plugin */
81e4b17023SJohn Marino nargv = XCNEWVEC (const char *, ac + 4);
82e4b17023SJohn Marino nargv[0] = exe_name;
83e4b17023SJohn Marino nargv[1] = "--plugin";
84e4b17023SJohn Marino nargv[2] = plugin;
85e4b17023SJohn Marino if (is_ar && av[1] && av[1][0] != '-')
86e4b17023SJohn Marino av[1] = concat("-", av[1], NULL);
87e4b17023SJohn Marino for (k = 1; k < ac; k++)
88e4b17023SJohn Marino nargv[2 + k] = av[k];
89e4b17023SJohn Marino nargv[2 + k] = NULL;
90e4b17023SJohn Marino
91e4b17023SJohn Marino /* Run utility */
92e4b17023SJohn Marino /* ??? the const is misplaced in pex_one's argv? */
93e4b17023SJohn Marino err_msg = pex_one (PEX_LAST|PEX_SEARCH,
94e4b17023SJohn Marino exe_name,
95e4b17023SJohn Marino CONST_CAST2 (char * const *, const char **, nargv),
96e4b17023SJohn Marino concat("gcc-", exe_name, NULL),
97e4b17023SJohn Marino NULL,NULL, &status, &err);
98e4b17023SJohn Marino if (err_msg)
99e4b17023SJohn Marino fprintf(stderr, "Error running %s: %s\n", exe_name, err_msg);
100*5ce9237cSJohn Marino else if (status)
101*5ce9237cSJohn Marino {
102*5ce9237cSJohn Marino if (WIFSIGNALED (status))
103*5ce9237cSJohn Marino {
104*5ce9237cSJohn Marino int sig = WTERMSIG (status);
105*5ce9237cSJohn Marino fprintf (stderr, "%s terminated with signal %d [%s]%s\n",
106*5ce9237cSJohn Marino exe_name, sig, strsignal(sig),
107*5ce9237cSJohn Marino WCOREDUMP(status) ? ", core dumped" : "");
108*5ce9237cSJohn Marino }
109*5ce9237cSJohn Marino else if (WIFEXITED (status))
110*5ce9237cSJohn Marino exit_code = WEXITSTATUS (status);
111*5ce9237cSJohn Marino }
112*5ce9237cSJohn Marino else
113*5ce9237cSJohn Marino exit_code = SUCCESS_EXIT_CODE;
114e4b17023SJohn Marino
115*5ce9237cSJohn Marino return exit_code;
116e4b17023SJohn Marino }
117