summaryrefslogtreecommitdiff
path: root/gcc/gcc-ar.c
blob: 1e86d20b88f526bf18fe26a2e0a7f8f84e75c883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* Wrapper for ar/ranlib/nm to pass the LTO plugin.
   Copyright (C) 2011 Free Software Foundation, Inc.
   Contributed by Andi Kleen.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include <stdio.h>
#include "config.h"
#include "system.h"
#include "libiberty.h"

#ifndef PERSONALITY
#error "Please set personality"
#endif

static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;
static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;
static const char *const target_machine = TARGET_MACHINE;

static const char dir_separator[] = { DIR_SEPARATOR, 0 };

int 
main(int ac, char **av)
{
  const char *nprefix;
  const char *exe_name;
  char *plugin;
  int k, status, err;
  const char *err_msg;
  const char **nargv;
  bool is_ar = !strcmp (PERSONALITY, "ar");

  exe_name = PERSONALITY;
#ifdef CROSS_DIRECTORY_STRUCTURE
  exe_name = concat (target_machine, "-", exe_name, NULL);
#endif

  /* Find plugin */
  /* XXX implement more magic from gcc.c? */
  nprefix = getenv ("GCC_EXEC_PREFIX");
  if (!nprefix)
    nprefix = standard_libexec_prefix;

  nprefix = make_relative_prefix (av[0], 
				  standard_bin_prefix, 
				  nprefix);
  plugin = concat (nprefix,
		   dir_separator,
                   DEFAULT_TARGET_MACHINE, 
		   dir_separator,
		   DEFAULT_TARGET_VERSION,
	           dir_separator,
		   LTOPLUGINSONAME,
		   NULL);
  if (access (plugin, X_OK))
    {
      fprintf (stderr, "%s: Cannot find plugin %s\n", av[0], plugin);
      exit (1);
    }

  /* Create new command line with plugin */
  nargv = XCNEWVEC (const char *, ac + 4);
  nargv[0] = exe_name;
  nargv[1] = "--plugin";
  nargv[2] = plugin;
  if (is_ar && av[1] && av[1][0] != '-')
    av[1] = concat("-", av[1], NULL);
  for (k = 1; k < ac; k++)
    nargv[2 + k] = av[k];
  nargv[2 + k] = NULL;

  /* Run utility */
  /* ??? the const is misplaced in pex_one's argv? */
  err_msg = pex_one (PEX_LAST|PEX_SEARCH, 
		     exe_name, 
		     CONST_CAST2 (char * const *, const char **, nargv),
		     concat("gcc-", exe_name, NULL), 
		     NULL,NULL,  &status, &err);
  if (err_msg) 
    fprintf(stderr, "Error running %s: %s\n", exe_name, err_msg);

  return err;
}