aboutsummaryrefslogtreecommitdiff
path: root/gcc/gcc-ar.cc
blob: 479939fb0bf1f8289510f2ea335eb06187038672 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Wrapper for ar/ranlib/nm to pass the LTO plugin.
   Copyright (C) 2011-2024 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 "config.h"
#include "system.h"
#include "libiberty.h"
#include "file-find.h"

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

/* The exec prefix as derived at compile-time from --prefix.  */

static const char standard_exec_prefix[] = STANDARD_EXEC_PREFIX;

/* The libexec prefix as derived at compile-time from --prefix.  */

static const char standard_libexec_prefix[] = STANDARD_LIBEXEC_PREFIX;

/* The bindir prefix as derived at compile-time from --prefix.  */

static const char standard_bin_prefix[] = STANDARD_BINDIR_PREFIX;

/* A relative path to be used in finding the location of tools
   relative to this program.  */

static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;

/* The exec prefix as relocated from the location of this program.  */

static const char *self_exec_prefix;

/* The libexec prefix as relocated from the location of this program.  */

static const char *self_libexec_prefix;

/* The tools prefix as relocated from the location of this program.  */

static const char *self_tooldir_prefix;

/* The name of the machine that is being targeted.  */

static const char *const target_machine = DEFAULT_TARGET_MACHINE;

/* The target version.  */

static const char *const target_version = DEFAULT_TARGET_VERSION;

/* The collection of target specific path prefixes.  */

static struct path_prefix target_path;

/* The collection path prefixes.  */

static struct path_prefix path;

/* The directory separator.  */

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

static void
setup_prefixes (const char *exec_path)
{
  const char *self;

  self = getenv ("GCC_EXEC_PREFIX");
  if (!self)
    self = exec_path;
  else
    self = concat (self, "gcc-" PERSONALITY, NULL);

  /* Relocate the exec prefix.  */
  self_exec_prefix = make_relative_prefix (self,
					   standard_bin_prefix,
					   standard_exec_prefix);
  if (self_exec_prefix == NULL)
    self_exec_prefix = standard_exec_prefix;

  /* Relocate libexec prefix.  */
  self_libexec_prefix = make_relative_prefix (self,
					      standard_bin_prefix,
					      standard_libexec_prefix);
  if (self_libexec_prefix == NULL)
    self_libexec_prefix = standard_libexec_prefix;


  /* Build the relative path to the target-specific tool directory.  */
  self_tooldir_prefix = concat (tooldir_base_prefix, target_machine,
				dir_separator, NULL);
  self_tooldir_prefix = concat (self_exec_prefix, target_machine, 
				dir_separator, target_version, dir_separator,
				self_tooldir_prefix, NULL);

  /* Add the target-specific tool bin prefix.  */
  prefix_from_string (concat (self_tooldir_prefix, "bin", NULL), &target_path);

  /* Add the target-specific libexec prefix.  */
  self_libexec_prefix = concat (self_libexec_prefix, target_machine, 
				dir_separator, target_version,
				dir_separator, NULL);
  prefix_from_string (self_libexec_prefix, &target_path);

  /* Add path as a last resort.  */
  prefix_from_env ("PATH", &path);
}

int 
main (int ac, char **av)
{
  const char *exe_name;
#if HAVE_LTO_PLUGIN > 0
  char *plugin;
  const int j = 2; /* Two extra args, --plugin <plugin>  */
#else
  const int j = 0; /* No extra args.  */
#endif
  int k, status, err;
  const char *err_msg;
  const char **nargv;
  char **old_argv;
  const char *rsp_file = NULL;
  const char *rsp_arg = NULL;
  const char *rsp_argv[3];
  bool is_ar = !strcmp (PERSONALITY, "ar");
  int exit_code = FATAL_EXIT_CODE;
  int i;

  setup_prefixes (av[0]);

  /* Not using getopt for now.  */
  for (i = 0; i < ac; i++)
      if (startswith (av[i], "-B"))
	{
	  const char *arg = av[i] + 2;
	  const char *end;
	  size_t len;

	  memmove (av + i, av + i + 1, sizeof (char *) * ((ac + 1) - i));
	  ac--;
	  if (*arg == 0)
	    {
	      arg = av[i];
	      if (!arg)
		{
		  fprintf (stderr, "Usage: gcc-ar [-B prefix] ar arguments ...\n");
		  exit (EXIT_FAILURE);
		}
	      memmove (av + i, av + i + 1, sizeof (char *) * ((ac + 1) - i));
	      ac--;
	      i++;
	    }
	  /* else it's a joined argument  */

	  len = strlen (arg);
	  if (len > 0)
	    len--;
	  end = arg + len;

	  /* Always add a dir separator for the prefix list.  */
	  if (end > arg && !IS_DIR_SEPARATOR (*end))
	    {
	      static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
	      arg = concat (arg, dir_separator_str, NULL);
	    }

	  add_prefix_begin (&path, arg);
	  add_prefix_begin (&target_path, arg);
	  break;
	}

#if HAVE_LTO_PLUGIN > 0
  /* Find the GCC LTO plugin */
  plugin = find_a_file (&target_path, LTOPLUGINSONAME, R_OK);
  if (!plugin)
    {
      fprintf (stderr, "%s: Cannot find plugin '%s'\n", av[0], LTOPLUGINSONAME);
      exit (1);
    }
#endif

  /* Find the wrapped binutils program.  */
  exe_name = find_a_file (&target_path, PERSONALITY, X_OK);
  if (!exe_name)
    {
      const char *real_exe_name = PERSONALITY;
#ifdef CROSS_DIRECTORY_STRUCTURE
      real_exe_name = concat (target_machine, "-", PERSONALITY, NULL);
#endif
      exe_name = find_a_file (&path, real_exe_name, X_OK);
      if (!exe_name)
	{
	  fprintf (stderr, "%s: Cannot find binary '%s'\n", av[0],
		   real_exe_name);
	  exit (1);
	}
    }

  /* Expand any @files before modifying the command line
     and use a temporary response file if there were any.  */
  old_argv = av;
  expandargv (&ac, &av);
  if (av != old_argv)
    rsp_file = make_temp_file ("");

  /* Prepend - if necessary.  */
  if (is_ar && av[1] && av[1][0] != '-')
    av[1] = concat ("-", av[1], NULL);
  
  /* Create new command line with plugin - if we have one, otherwise just
     copy the command through.  */
  nargv = XCNEWVEC (const char *, ac + j + 1); /* +j plugin args +1 for NULL.  */
  nargv[0] = exe_name;
#if HAVE_LTO_PLUGIN > 0
  nargv[1] = "--plugin";
  nargv[2] = plugin;
#endif
  for (k = 1; k < ac; k++)
    nargv[j + k] = av[k];
  nargv[j + k] = NULL;

  /* If @file was passed, put nargv into the temporary response
     file and then change it to a single @FILE argument, where
     FILE is the temporary filename.  */
  if (rsp_file)
    {
      FILE *f;
      int status;
      f = fopen (rsp_file, "w");
      if (f == NULL)
	{
	  fprintf (stderr, "Cannot open temporary file %s\n", rsp_file);
	  exit (1);
	}
      status = writeargv (
	  CONST_CAST2 (char * const *, const char **, nargv) + 1, f);
      if (status)
	{
	  fprintf (stderr, "Cannot write to temporary file %s\n", rsp_file);
	  exit (1);
	}
      status = fclose (f);
      if (EOF == status)
	{
	  fprintf (stderr, "Cannot close temporary file %s\n", rsp_file);
	  exit (1);
	}
      rsp_arg = concat ("@", rsp_file, NULL);
      rsp_argv[0] = nargv[0];
      rsp_argv[1] = rsp_arg;
      rsp_argv[2] = NULL;
      nargv = rsp_argv;
    }

  /* 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);
  else if (status)
    {
      if (WIFSIGNALED (status))
	{
	  int sig = WTERMSIG (status);
	  fprintf (stderr, "%s terminated with signal %d [%s]%s\n",
		   exe_name, sig, strsignal (sig),
		   WCOREDUMP (status) ? ", core dumped" : "");
	}
      else if (WIFEXITED (status))
	exit_code = WEXITSTATUS (status);
    }
  else
    exit_code = SUCCESS_EXIT_CODE;

  if (rsp_file)
    unlink (rsp_file);

  return exit_code;
}