]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcc.c
Makefile.in (bitmap.o, [...]): Update dependencies.
[thirdparty/gcc.git] / gcc / gcc.c
CommitLineData
ed1f651b 1/* Compiler driver program that can handle many languages.
9218435e 2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
d7fb0a6d
L
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010
c662432e 5 Free Software Foundation, Inc.
ed1f651b 6
1322177d 7This file is part of GCC.
ed1f651b 8
1322177d
LB
9GCC is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
9dcd6f09 11Software Foundation; either version 3, or (at your option) any later
1322177d 12version.
ed1f651b 13
1322177d
LB
14GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17for more details.
ed1f651b
RS
18
19You should have received a copy of the GNU General Public License
9dcd6f09 20along with GCC; see the file COPYING3. If not see
8a554c70 21<http://www.gnu.org/licenses/>. */
ed1f651b
RS
22
23/* This program is the user interface to the C compiler and possibly to
24other compilers. It is used because compilation is a complicated procedure
25which involves running several programs and passing temporary files between
26them, forwarding the users switches to those programs selectively,
27and deleting the temporary files at the end.
28
29CC recognizes how to compile each input file by suffixes in the file names.
30Once it knows which kind of compilation to perform, the procedure for
31compilation is specified by a string called a "spec". */
c5c76735 32
e9a25f70 33#include "config.h"
670ee920 34#include "system.h"
4977bab6 35#include "coretypes.h"
1713a69f 36#include "multilib.h" /* before tm.h */
4977bab6 37#include "tm.h"
670ee920 38#include <signal.h>
798bdf70
BK
39#if ! defined( SIGCHLD ) && defined( SIGCLD )
40# define SIGCHLD SIGCLD
41#endif
ed5b9f96 42#include "xregex.h"
17248a6b 43#include "obstack.h"
ab87f8c8 44#include "intl.h"
460ee112 45#include "prefix.h"
9257393c 46#include "gcc.h"
6afbc885 47#include "diagnostic.h"
8a63621f 48#include "flags.h"
14c7833c 49#include "opts.h"
5b634ee0 50#include "vec.h"
c10d53dd 51
2153915d
AO
52#ifdef HAVE_MMAP_FILE
53# include <sys/mman.h>
54# ifdef HAVE_MINCORE
55/* This is on Solaris. */
56# include <sys/types.h>
57# endif
58#endif
59
60#ifndef MAP_FAILED
61# define MAP_FAILED ((void *)-1)
62#endif
63
45936a85
DD
64/* By default there is no special suffix for target executables. */
65/* FIXME: when autoconf is fixed, remove the host check - dj */
66#if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX)
67#define HAVE_TARGET_EXECUTABLE_SUFFIX
ed1f651b 68#endif
f6ec7e54 69
45936a85
DD
70/* By default there is no special suffix for host executables. */
71#ifdef HOST_EXECUTABLE_SUFFIX
72#define HAVE_HOST_EXECUTABLE_SUFFIX
f70165f6 73#else
45936a85
DD
74#define HOST_EXECUTABLE_SUFFIX ""
75#endif
76
77/* By default, the suffix for target object files is ".o". */
78#ifdef TARGET_OBJECT_SUFFIX
79#define HAVE_TARGET_OBJECT_SUFFIX
80#else
81#define TARGET_OBJECT_SUFFIX ".o"
ed7dae04
RK
82#endif
83
8b60264b 84static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
48ff801b 85
512b62fb
JM
86/* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
87#ifndef LIBRARY_PATH_ENV
88#define LIBRARY_PATH_ENV "LIBRARY_PATH"
89#endif
90
956d6950
JL
91#ifndef HAVE_KILL
92#define kill(p,s) raise(s)
93#endif
94
ed1f651b
RS
95/* If a stage of compilation returns an exit status >= 1,
96 compilation of that file ceases. */
97
98#define MIN_FATAL_STATUS 1
99
69927b59
NB
100/* Flag set by cppspec.c to 1. */
101int is_cpp_driver;
102
110abdbc 103/* Flag set to nonzero if an @file argument has been supplied to gcc. */
2091ff66
NF
104static bool at_file_supplied;
105
f5fa9a5b
PE
106/* Definition of string containing the arguments given to configure. */
107#include "configargs.h"
108
b8468bc7
NC
109/* Flag saying to print the command line options understood by gcc and its
110 sub-processes. */
111
112static int print_help_list;
113
41fd0f9b
RAE
114/* Flag saying to print the version of gcc and its sub-processes. */
115
116static int print_version;
117
99f78cdd
IR
118/* Flag indicating whether we should ONLY print the command and
119 arguments (like verbose_flag) without executing the command.
120 Displayed arguments are quoted so that the generated command
121 line is suitable for execution. This is intended for use in
122 shell scripts to capture the driver-generated command line. */
123static int verbose_only_flag;
124
c662432e 125/* Flag indicating how to print command line options of sub-processes. */
91606ce2 126
c662432e 127static int print_subprocess_help;
91606ce2 128
bdde878c
AO
129/* Whether we should report subprocess execution times to a file. */
130
131FILE *report_times_to_file = NULL;
132
4977bab6
ZW
133/* Nonzero means place this string before uses of /, so that include
134 and library files can be found in an alternate location. */
135
db720d9a 136#ifdef TARGET_SYSTEM_ROOT
4977bab6 137static const char *target_system_root = TARGET_SYSTEM_ROOT;
db720d9a
DJ
138#else
139static const char *target_system_root = 0;
140#endif
4977bab6 141
047d636f
DJ
142/* Nonzero means pass the updated target_system_root to the compiler. */
143
144static int target_system_root_changed;
145
e7f13528
GP
146/* Nonzero means append this string to target_system_root. */
147
148static const char *target_sysroot_suffix = 0;
149
150/* Nonzero means append this string to target_system_root for headers. */
151
152static const char *target_sysroot_hdrs_suffix = 0;
153
ed1f651b
RS
154/* Nonzero means write "temp" files in source directory
155 and use the source file's name in them, and don't delete them. */
156
14fdc613
MM
157static enum save_temps {
158 SAVE_TEMPS_NONE, /* no -save-temps */
159 SAVE_TEMPS_CWD, /* -save-temps in current directory */
160 SAVE_TEMPS_OBJ /* -save-temps in object directory */
161} save_temps_flag;
162
163/* Output file to use to get the object directory for -save-temps=obj */
164static char *save_temps_prefix = 0;
165static size_t save_temps_length = 0;
ed1f651b 166
53117a2f 167/* The compiler version. */
ed1f651b 168
3b304f5b 169static const char *compiler_version;
53117a2f 170
1401cf37 171/* The target version. */
53117a2f 172
37a4aa31 173static const char *const spec_version = DEFAULT_TARGET_VERSION;
ed1f651b 174
1401cf37 175/* The target machine. */
ed1f651b 176
878f32c3 177static const char *spec_machine = DEFAULT_TARGET_MACHINE;
ed1f651b 178
004fd4d5
RS
179/* Nonzero if cross-compiling.
180 When -b is used, the value comes from the `specs' file. */
181
2989d30c 182#ifdef CROSS_DIRECTORY_STRUCTURE
3b304f5b 183static const char *cross_compile = "1";
004fd4d5 184#else
3b304f5b 185static const char *cross_compile = "0";
004fd4d5
RS
186#endif
187
14a774a9
RK
188/* Greatest exit code of sub-processes that has been encountered up to
189 now. */
190static int greatest_status = 1;
191
ed1f651b
RS
192/* This is the obstack which we use to allocate many strings. */
193
194static struct obstack obstack;
195
b3865ca9 196/* This is the obstack to build an environment variable to pass to
6dc42e49 197 collect2 that describes all of the relevant switches of what to
b3865ca9
RS
198 pass the compiler in building the list of pointers to constructors
199 and destructors. */
200
201static struct obstack collect_obstack;
202
99360286
DE
203/* Forward declaration for prototypes. */
204struct path_prefix;
76391e5a 205struct prefix_list;
99360286 206
1d088dee
AJ
207static void init_spec (void);
208static void store_arg (const char *, int, int);
fe7df9c4 209static void insert_wrapper (const char *);
1d088dee
AJ
210static char *load_specs (const char *);
211static void read_specs (const char *, int);
212static void set_spec (const char *, const char *);
213static struct compiler *lookup_compiler (const char *, size_t, const char *);
00dcee0c
AM
214static char *build_search_list (const struct path_prefix *, const char *,
215 bool, bool);
e9c15f6e 216static void xputenv (const char *);
00dcee0c
AM
217static void putenv_from_prefixes (const struct path_prefix *, const char *,
218 bool);
1d088dee 219static int access_check (const char *, int);
00dcee0c 220static char *find_a_file (const struct path_prefix *, const char *, int, bool);
1d088dee 221static void add_prefix (struct path_prefix *, const char *, const char *,
1a5d37a1 222 int, int, int);
1d088dee 223static void add_sysrooted_prefix (struct path_prefix *, const char *,
1a5d37a1 224 const char *, int, int, int);
1d088dee
AJ
225static char *skip_whitespace (char *);
226static void delete_if_ordinary (const char *);
227static void delete_temp_files (void);
228static void delete_failure_queue (void);
229static void clear_failure_queue (void);
230static int check_live_switch (int, int);
231static const char *handle_braces (const char *);
232static inline bool input_suffix_matches (const char *, const char *);
233static inline bool switch_matches (const char *, const char *, int);
234static inline void mark_matching_switches (const char *, const char *, int);
235static inline void process_marked_switches (void);
236static const char *process_brace_body (const char *, const char *, const char *, int, int);
237static const struct spec_function *lookup_spec_function (const char *);
238static const char *eval_spec_function (const char *, const char *);
239static const char *handle_spec_function (const char *);
240static char *save_string (const char *, int);
241static void set_collect_gcc_options (void);
242static int do_spec_1 (const char *, int, const char *);
243static int do_spec_2 (const char *);
244static void do_option_spec (const char *, const char *);
245static void do_self_spec (const char *);
246static const char *find_file (const char *);
00dcee0c 247static int is_directory (const char *, bool);
1d088dee
AJ
248static const char *validate_switches (const char *);
249static void validate_all_switches (void);
250static inline void validate_switches_from_spec (const char *);
251static void give_switch (int, int);
252static int used_arg (const char *, int);
253static int default_arg (const char *, int);
254static void set_multilib_dir (void);
255static void print_multilib_info (void);
256static void perror_with_name (const char *);
1d088dee
AJ
257static void display_help (void);
258static void add_preprocessor_option (const char *, int);
259static void add_assembler_option (const char *, int);
260static void add_linker_option (const char *, int);
60cf253a 261static void process_command (unsigned int, struct cl_decoded_option *);
1d088dee
AJ
262static int execute (void);
263static void alloc_args (void);
264static void clear_args (void);
2dec80c7 265static void fatal_signal (int);
328163dc 266#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1d088dee
AJ
267static void init_gcc_specs (struct obstack *, const char *, const char *,
268 const char *);
6894579f 269#endif
40cdfca6 270#if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
1d088dee 271static const char *convert_filename (const char *, int, int);
40cdfca6 272#endif
f3226a90 273
30d8946b 274static const char *getenv_spec_function (int, const char **);
1d088dee
AJ
275static const char *if_exists_spec_function (int, const char **);
276static const char *if_exists_else_spec_function (int, const char **);
3dd53121 277static const char *replace_outfile_spec_function (int, const char **);
2642f659 278static const char *remove_outfile_spec_function (int, const char **);
ed5b9f96 279static const char *version_compare_spec_function (int, const char **);
953ff289 280static const char *include_spec_function (int, const char **);
4adbd5dd 281static const char *find_file_spec_function (int, const char **);
c1ce46a5 282static const char *find_plugindir_spec_function (int, const char **);
a0f87454 283static const char *print_asm_header_spec_function (int, const char **);
2153915d
AO
284static const char *compare_debug_dump_opt_spec_function (int, const char **);
285static const char *compare_debug_self_opt_spec_function (int, const char **);
286static const char *compare_debug_auxbase_opt_spec_function (int, const char **);
ed1f651b 287\f
d991c721
BK
288/* The Specs Language
289
290Specs are strings containing lines, each of which (if not blank)
ed1f651b
RS
291is made up of a program name, and arguments separated by spaces.
292The program name must be exact and start from root, since no path
293is searched and it is unreliable to depend on the current working directory.
294Redirection of input or output is not supported; the subprograms must
295accept filenames saying what files to read and write.
296
297In addition, the specs can contain %-sequences to substitute variable text
298or for conditional text. Here is a table of all defined %-sequences.
299Note that spaces are not generated automatically around the results of
300expanding these sequences; therefore, you can concatenate them together
301or with constant text in a single argument.
302
303 %% substitute one % into the program name or argument.
304 %i substitute the name of the input file being processed.
305 %b substitute the basename of the input file being processed.
306 This is the substring up to (and not including) the last period
14fdc613 307 and not including the directory unless -save-temps was specified
3beb864c 308 to put temporaries in a different location.
ea414c97 309 %B same as %b, but include the file suffix (text after the last period).
dd75c292
CB
310 %gSUFFIX
311 substitute a file name that has suffix SUFFIX and is chosen
312 once per compilation, and mark the argument a la %d. To reduce
313 exposure to denial-of-service attacks, the file name is now
314 chosen in a way that is hard to predict even when previously
315 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
316 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
a92dd235 317 the regexp "[.0-9A-Za-z]*%O"; "%O" is treated exactly as if it
a1d9074c
TT
318 had been pre-processed. Previously, %g was simply substituted
319 with a file name chosen once per compilation, without regard
320 to any appended suffix (which was therefore treated just like
321 ordinary text), making such attacks more likely to succeed.
4977bab6
ZW
322 %|SUFFIX
323 like %g, but if -pipe is in effect, expands simply to "-".
324 %mSUFFIX
325 like %g, but if -pipe is in effect, expands to nothing. (We have both
326 %| and %m to accommodate differences between system assemblers; see
327 the AS_NEEDS_DASH_FOR_PIPED_INPUT target macro.)
dd75c292
CB
328 %uSUFFIX
329 like %g, but generates a new temporary file name even if %uSUFFIX
330 was already seen.
331 %USUFFIX
332 substitutes the last file name generated with %uSUFFIX, generating a
333 new one if there is no such last file name. In the absence of any
334 %uSUFFIX, this is just like %gSUFFIX, except they don't share
335 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
336 would involve the generation of two distinct file names, one
337 for each `%g.s' and another for each `%U.s'. Previously, %U was
338 simply substituted with a file name chosen for the previous %u,
339 without regard to any appended suffix.
49009afd
JL
340 %jSUFFIX
341 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
342 writable, and if save-temps is off; otherwise, substitute the name
343 of a temporary file, just like %u. This temporary file is not
344 meant for communication between processes, but rather as a junk
345 disposal mechanism.
11972f66
NS
346 %.SUFFIX
347 substitutes .SUFFIX for the suffixes of a matched switch's args when
348 it is subsequently output with %*. SUFFIX is terminated by the next
349 space or %.
ed1f651b 350 %d marks the argument containing or following the %d as a
ee618c17 351 temporary file name, so that that file will be deleted if GCC exits
ed1f651b
RS
352 successfully. Unlike %g, this contributes no text to the argument.
353 %w marks the argument containing or following the %w as the
354 "output file" of this compilation. This puts the argument
355 into the sequence of arguments that %o will substitute later.
17211ab5 356 %V indicates that this compilation produces no "output file".
ed1f651b
RS
357 %W{...}
358 like %{...} but mark last argument supplied within
359 as a file to be deleted on failure.
360 %o substitutes the names of all the output files, with spaces
361 automatically placed around them. You should write spaces
362 around the %o as well or the results are undefined.
363 %o is for use in the specs for running the linker.
364 Input files whose names have no recognized suffix are not compiled
365 at all, but they are included among the output files, so they will
366 be linked.
dd75c292 367 %O substitutes the suffix for object files. Note that this is
a1d9074c
TT
368 handled specially when it immediately follows %g, %u, or %U
369 (with or without a suffix argument) because of the need for
370 those to form complete file names. The handling is such that
371 %O is treated exactly as if it had already been substituted,
372 except that %g, %u, and %U do not currently support additional
373 SUFFIX characters following %O as they would following, for
374 example, `.o'.
047d636f 375 %I Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
2b6dd222
JM
376 (made from TARGET_SYSTEM_ROOT), -isystem (made from COMPILER_PATH
377 and -B options) and -imultilib as necessary.
ed1f651b
RS
378 %s current argument is the name of a library or startup file of some sort.
379 Search for that file in a standard list of directories
380 and substitute the full name found.
381 %eSTR Print STR as an error message. STR is terminated by a newline.
382 Use this when inconsistent options are detected.
09da1532 383 %nSTR Print STR as a notice. STR is terminated by a newline.
ed1f651b
RS
384 %x{OPTION} Accumulate an option for %X.
385 %X Output the accumulated linker options specified by compilations.
c9ebacb8 386 %Y Output the accumulated assembler options specified by compilations.
57cb9b60 387 %Z Output the accumulated preprocessor options specified by compilations.
ed1f651b
RS
388 %a process ASM_SPEC as a spec.
389 This allows config.h to specify part of the spec for running as.
390 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
391 used here. This can be used to run a post-processor after the
9ec36da5 392 assembler has done its job.
48ff801b 393 %D Dump out a -L option for each directory in startfile_prefixes.
60103a34 394 If multilib_dir is set, extra entries are generated with it affixed.
ed1f651b
RS
395 %l process LINK_SPEC as a spec.
396 %L process LIB_SPEC as a spec.
68d69835 397 %G process LIBGCC_SPEC as a spec.
380e5ca4
MM
398 %R Output the concatenation of target_system_root and
399 target_sysroot_suffix.
ed1f651b
RS
400 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
401 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
9db0819e 402 %C process CPP_SPEC as a spec.
ed1f651b
RS
403 %1 process CC1_SPEC as a spec.
404 %2 process CC1PLUS_SPEC as a spec.
405 %* substitute the variable part of a matched option. (See below.)
406 Note that each comma in the substituted string is replaced by
407 a single space.
4977bab6
ZW
408 %<S remove all occurrences of -S from the command line.
409 Note - this command is position dependent. % commands in the
410 spec string before this one will see -S, % commands in the
411 spec string after this one will not.
412 %<S* remove all occurrences of all switches beginning with -S from the
413 command line.
f3226a90
JT
414 %:function(args)
415 Call the named function FUNCTION, passing it ARGS. ARGS is
416 first processed as a nested spec string, then split into an
417 argument vector in the usual fashion. The function returns
418 a string which is processed as if it had appeared literally
419 as part of the current spec.
ee618c17 420 %{S} substitutes the -S switch, if that switch was given to GCC.
ed1f651b
RS
421 If that switch was not specified, this substitutes nothing.
422 Here S is a metasyntactic variable.
ee618c17 423 %{S*} substitutes all the switches specified to GCC whose names start
196a37f4 424 with -S. This is used for -o, -I, etc; switches that take
ee618c17 425 arguments. GCC considers `-o foo' as being one switch whose
ed1f651b
RS
426 name starts with `o'. %{o*} would substitute this text,
427 including the space; thus, two arguments would be generated.
196a37f4 428 %{S*&T*} likewise, but preserve order of S and T options (the order
1d088dee
AJ
429 of S and T in the spec is not significant). Can be any number
430 of ampersand-separated variables; for each the wild card is
431 optional. Useful for CPP as %{D*&U*&A*}.
4977bab6 432
ee618c17
BE
433 %{S:X} substitutes X, if the -S switch was given to GCC.
434 %{!S:X} substitutes X, if the -S switch was NOT given to GCC.
4977bab6 435 %{S*:X} substitutes X if one or more switches whose names start
ee618c17 436 with -S was given to GCC. Normally X is substituted only
4977bab6
ZW
437 once, no matter how many such switches appeared. However,
438 if %* appears somewhere in X, then X will be substituted
439 once for each matching switch, with the %* replaced by the
440 part of that switch that matched the '*'.
441 %{.S:X} substitutes X, if processing a file with suffix S.
442 %{!.S:X} substitutes X, if NOT processing a file with suffix S.
48137d59
GK
443 %{,S:X} substitutes X, if processing a file which will use spec S.
444 %{!,S:X} substitutes X, if NOT processing a file which will use spec S.
3beb864c 445
ee618c17 446 %{S|T:X} substitutes X if either -S or -T was given to GCC. This may be
48137d59
GK
447 combined with '!', '.', ',', and '*' as above binding stronger
448 than the OR.
4977bab6
ZW
449 If %* appears in X, all of the alternatives must be starred, and
450 only the first matching alternative is substituted.
ee618c17
BE
451 %{S:X; if S was given to GCC, substitutes X;
452 T:Y; else if T was given to GCC, substitutes Y;
4977bab6 453 :D} else substitutes D. There can be as many clauses as you need.
48137d59 454 This may be combined with '.', '!', ',', '|', and '*' as above.
4977bab6 455
b3865ca9 456 %(Spec) processes a specification defined in a specs file as *Spec:
4089dfab 457 %[Spec] as above, but put __ around -D arguments
ed1f651b 458
4977bab6 459The conditional text X in a %{S:X} or similar construct may contain
ed1f651b 460other nested % constructs or spaces, or even newlines. They are
4977bab6
ZW
461processed as usual, as described above. Trailing white space in X is
462ignored. White space may also appear anywhere on the left side of the
463colon in these constructs, except between . or * and the corresponding
464word.
ed1f651b 465
6c396fb5 466The -O, -f, -m, and -W switches are handled specifically in these
f5b0eb4e
RK
467constructs. If another value of -O or the negated form of a -f, -m, or
468-W switch is found later in the command line, the earlier switch
6c396fb5
RK
469value is ignored, except with {S*} where S is just one letter; this
470passes all matching options.
f5b0eb4e 471
9bf09437
RH
472The character | at the beginning of the predicate text is used to indicate
473that a command should be piped to the following command, but only if -pipe
474is specified.
ed1f651b 475
ee618c17 476Note that it is built into GCC which switches take arguments and which
ed1f651b
RS
477do not. You might think it would be useful to generalize this to
478allow each compiler's spec to say which switches take arguments. But
ee618c17 479this cannot be done in a consistent fashion. GCC cannot even decide
ed1f651b
RS
480which input files have been specified without knowing which switches
481take arguments, and it must know which input files to compile in order
482to tell which compilers to run.
483
ee618c17 484GCC also knows implicitly that arguments starting in `-l' are to be
ed1f651b
RS
485treated as compiler output files, and passed to the linker in their
486proper position among the other output files. */
487\f
0fef3fd0 488/* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
ed1f651b
RS
489
490/* config.h can define ASM_SPEC to provide extra args to the assembler
491 or extra switch-translations. */
492#ifndef ASM_SPEC
493#define ASM_SPEC ""
494#endif
495
496/* config.h can define ASM_FINAL_SPEC to run a post processor after
497 the assembler has run. */
498#ifndef ASM_FINAL_SPEC
499#define ASM_FINAL_SPEC ""
500#endif
501
502/* config.h can define CPP_SPEC to provide extra args to the C preprocessor
503 or extra switch-translations. */
504#ifndef CPP_SPEC
505#define CPP_SPEC ""
506#endif
507
508/* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
509 or extra switch-translations. */
510#ifndef CC1_SPEC
511#define CC1_SPEC ""
512#endif
513
514/* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
515 or extra switch-translations. */
516#ifndef CC1PLUS_SPEC
517#define CC1PLUS_SPEC ""
518#endif
519
520/* config.h can define LINK_SPEC to provide extra args to the linker
521 or extra switch-translations. */
522#ifndef LINK_SPEC
523#define LINK_SPEC ""
524#endif
525
526/* config.h can define LIB_SPEC to override the default libraries. */
527#ifndef LIB_SPEC
68d69835
JM
528#define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
529#endif
530
6de9cd9a
DN
531/* mudflap specs */
532#ifndef MFWRAP_SPEC
533/* XXX: valid only for GNU ld */
534/* XXX: should exactly match hooks provided by libmudflap.a */
535#define MFWRAP_SPEC " %{static: %{fmudflap|fmudflapth: \
536 --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc\
537 --wrap=mmap --wrap=munmap --wrap=alloca\
7544a87f 538} %{fmudflapth: --wrap=pthread_create\
6de9cd9a
DN
539}} %{fmudflap|fmudflapth: --wrap=main}"
540#endif
541#ifndef MFLIB_SPEC
7904f95f 542#define MFLIB_SPEC "%{fmudflap|fmudflapth: -export-dynamic}"
6de9cd9a
DN
543#endif
544
7458026b
ILT
545/* When using -fsplit-stack we need to wrap pthread_create, in order
546 to initialize the stack guard. We always use wrapping, rather than
547 shared library ordering, and we keep the wrapper function in
548 libgcc. This is not yet a real spec, though it could become one;
549 it is currently just stuffed into LINK_SPEC. FIXME: This wrapping
550 only works with GNU ld and gold. FIXME: This is incompatible with
551 -fmudflap when linking statically, which wants to do its own
552 wrapping. */
553#define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
554
68d69835
JM
555/* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
556 included. */
557#ifndef LIBGCC_SPEC
328163dc
MA
558#if defined(REAL_LIBGCC_SPEC)
559#define LIBGCC_SPEC REAL_LIBGCC_SPEC
57642751 560#elif defined(LINK_LIBGCC_SPECIAL_1)
68d69835 561/* Have gcc do the search for libgcc.a. */
ba2b7435 562#define LIBGCC_SPEC "libgcc.a%s"
68d69835 563#else
ba2b7435 564#define LIBGCC_SPEC "-lgcc"
68d69835 565#endif
ed1f651b
RS
566#endif
567
568/* config.h can define STARTFILE_SPEC to override the default crt0 files. */
569#ifndef STARTFILE_SPEC
570#define STARTFILE_SPEC \
adcb8d7d 571 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
ed1f651b
RS
572#endif
573
ed1f651b
RS
574/* config.h can define ENDFILE_SPEC to override the default crtn files. */
575#ifndef ENDFILE_SPEC
576#define ENDFILE_SPEC ""
577#endif
578
10da1131
BM
579#ifndef LINKER_NAME
580#define LINKER_NAME "collect2"
581#endif
582
c8aea42c
PB
583#ifdef HAVE_AS_DEBUG_PREFIX_MAP
584#define ASM_MAP " %{fdebug-prefix-map=*:--debug-prefix-map %*}"
585#else
586#define ASM_MAP ""
587#endif
588
5f0e9ea2
GK
589/* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
590 to the assembler. */
591#ifndef ASM_DEBUG_SPEC
b2ace8a4
JJ
592# if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
593 && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
c8aea42c
PB
594# define ASM_DEBUG_SPEC \
595 (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG \
c7b94907
MM
596 ? "%{!g0:%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}}" ASM_MAP \
597 : "%{!g0:%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}}" ASM_MAP)
b2ace8a4
JJ
598# else
599# if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
c7b94907 600# define ASM_DEBUG_SPEC "%{g*:%{!g0:--gstabs}}" ASM_MAP
b2ace8a4
JJ
601# endif
602# if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
c7b94907 603# define ASM_DEBUG_SPEC "%{g*:%{!g0:--gdwarf2}}" ASM_MAP
5f0e9ea2 604# endif
5f0e9ea2
GK
605# endif
606#endif
8a63621f
JJ
607#ifndef ASM_DEBUG_SPEC
608# define ASM_DEBUG_SPEC ""
609#endif
5f0e9ea2 610
ea414c97
ZW
611/* Here is the spec for running the linker, after compiling all files. */
612
bbd7687d
DM
613/* This is overridable by the target in case they need to specify the
614 -lgcc and -lc order specially, yet not require them to override all
615 of LINK_COMMAND_SPEC. */
616#ifndef LINK_GCC_C_SEQUENCE_SPEC
617#define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
618#endif
619
77008252
JJ
620#ifndef LINK_SSP_SPEC
621#ifdef TARGET_LIBC_PROVIDES_SSP
622#define LINK_SSP_SPEC "%{fstack-protector:}"
623#else
ec92bd4b 624#define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}"
77008252
JJ
625#endif
626#endif
627
24a4dd31
JJ
628#ifndef LINK_PIE_SPEC
629#ifdef HAVE_LD_PIE
630#define LINK_PIE_SPEC "%{pie:-pie} "
631#else
632#define LINK_PIE_SPEC "%{pie:} "
633#endif
634#endif
635
cd533689
PP
636#ifndef LINK_BUILDID_SPEC
637# if defined(HAVE_LD_BUILDID) && defined(ENABLE_LD_BUILDID)
638# define LINK_BUILDID_SPEC "%{!r:--build-id} "
639# endif
640#endif
641
642
ea414c97
ZW
643/* -u* was put back because both BSD and SysV seem to support it. */
644/* %{static:} simply prevents an error message if the target machine
645 doesn't handle -static. */
646/* We want %{T*} after %{L*} and %D so that it can be used to specify linker
647 scripts which exist in user specified directories, or in standard
648 directories. */
014d92e1 649/* We pass any -flto flags on to the linker, which is expected
d7f09764 650 to understand them. In practice, this means it had better be collect2. */
ea414c97
ZW
651#ifndef LINK_COMMAND_SPEC
652#define LINK_COMMAND_SPEC "\
653%{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
d7f09764 654 %(linker) \
da18ea94 655 %{fuse-linker-plugin: \
d7f09764
DN
656 -plugin %(linker_plugin_file) \
657 -plugin-opt=%(lto_wrapper) \
5cd0e96b 658 -plugin-opt=-fresolution=%u.res \
d7f09764 659 %{static|static-libgcc:-plugin-opt=-pass-through=%(lto_libgcc)} \
d520c7fb 660 %{static:-plugin-opt=-pass-through=-lc} \
d7f09764 661 } \
014d92e1
JH
662 %{flto*:%<fcompare-debug*} \
663 %{flto*} %l " LINK_PIE_SPEC \
d7f09764 664 "%X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r}\
6d721f67 665 %{s} %{t} %{u*} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
8beb0d9e 666 %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
7458026b
ILT
667 %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)}\
668 %(mflib) " STACK_SPLIT_SPEC "\
a123d175 669 %{fprofile-arcs|fprofile-generate*|coverage:-lgcov}\
9714c911 670 %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
ea414c97
ZW
671 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
672#endif
673
674#ifndef LINK_LIBGCC_SPEC
57642751
DJ
675/* Generate -L options for startfile prefix list. */
676# define LINK_LIBGCC_SPEC "%D"
ea414c97
ZW
677#endif
678
343f59d9
AM
679#ifndef STARTFILE_PREFIX_SPEC
680# define STARTFILE_PREFIX_SPEC ""
681#endif
682
380e5ca4
MM
683#ifndef SYSROOT_SPEC
684# define SYSROOT_SPEC "--sysroot=%R"
685#endif
686
e7f13528
GP
687#ifndef SYSROOT_SUFFIX_SPEC
688# define SYSROOT_SUFFIX_SPEC ""
689#endif
690
691#ifndef SYSROOT_HEADERS_SUFFIX_SPEC
692# define SYSROOT_HEADERS_SUFFIX_SPEC ""
693#endif
694
81bca2f5 695static const char *asm_debug;
3b304f5b 696static const char *cpp_spec = CPP_SPEC;
3b304f5b
ZW
697static const char *cc1_spec = CC1_SPEC;
698static const char *cc1plus_spec = CC1PLUS_SPEC;
bbd7687d 699static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
77008252 700static const char *link_ssp_spec = LINK_SSP_SPEC;
3b304f5b
ZW
701static const char *asm_spec = ASM_SPEC;
702static const char *asm_final_spec = ASM_FINAL_SPEC;
703static const char *link_spec = LINK_SPEC;
704static const char *lib_spec = LIB_SPEC;
6de9cd9a
DN
705static const char *mfwrap_spec = MFWRAP_SPEC;
706static const char *mflib_spec = MFLIB_SPEC;
953ff289 707static const char *link_gomp_spec = "";
3b304f5b
ZW
708static const char *libgcc_spec = LIBGCC_SPEC;
709static const char *endfile_spec = ENDFILE_SPEC;
710static const char *startfile_spec = STARTFILE_SPEC;
3b304f5b 711static const char *linker_name_spec = LINKER_NAME;
d7f09764
DN
712static const char *linker_plugin_file_spec = "";
713static const char *lto_wrapper_spec = "";
714static const char *lto_gcc_spec = "";
715static const char *lto_libgcc_spec = "";
ea414c97
ZW
716static const char *link_command_spec = LINK_COMMAND_SPEC;
717static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
343f59d9 718static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
380e5ca4 719static const char *sysroot_spec = SYSROOT_SPEC;
e7f13528
GP
720static const char *sysroot_suffix_spec = SYSROOT_SUFFIX_SPEC;
721static const char *sysroot_hdrs_suffix_spec = SYSROOT_HEADERS_SUFFIX_SPEC;
ea414c97
ZW
722
723/* Standard options to cpp, cc1, and as, to reduce duplication in specs.
724 There should be no need to override these in target dependent files,
725 but we need to copy them to the specs file so that newer versions
726 of the GCC driver can correctly drive older tool chains with the
727 appropriate -B options. */
728
e5f5feea
NB
729/* When cpplib handles traditional preprocessing, get rid of this, and
730 call cc1 (or cc1obj in objc/lang-specs.h) from the main specs so
731 that we default the front end language better. */
ea414c97 732static const char *trad_capable_cpp =
017acb41 733"cc1 -E %{traditional|ftraditional|traditional-cpp:-traditional-cpp}";
ea414c97 734
4ca1256f
NB
735/* We don't wrap .d files in %W{} since a missing .d file, and
736 therefore no dependency entry, confuses make into thinking a .o
737 file that happens to exist is up-to-date. */
ffdeea47 738static const char *cpp_unique_options =
9cc54940 739"%{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*&F*} %{P} %I\
c878765b
JM
740 %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
741 %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
4ca1256f 742 %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
3dfe046f 743 %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}}\
3a610de8 744 %{remap} %{g3|ggdb3|gstabs3|gcoff3|gxcoff3|gvms3:-dD}\
c1ce46a5 745 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
3a610de8 746 %{H} %C %{D*&U*&A*} %{i*} %Z %i\
6de9cd9a
DN
747 %{fmudflap:-D_MUDFLAP -include mf-runtime.h}\
748 %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h}\
56cd5b95 749 %{E|M|MM:%W{o*}}";
ffdeea47
JJ
750
751/* This contains cpp options which are common with cc1_options and are passed
059ba716
CD
752 only when preprocessing only to avoid duplication. We pass the cc1 spec
753 options to the preprocessor so that it the cc1 spec may manipulate
754 options used to set target flags. Those special target flags settings may
755 in turn cause preprocessor symbols to be defined specially. */
ffdeea47 756static const char *cpp_options =
740ca4b2 757"%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
09828811 758 %{f*} %{g*:%{!g0:%{g*} %{!fno-working-directory:-fworking-directory}}} %{O*}\
14fdc613 759 %{undef} %{save-temps*:-fpch-preprocess}";
ea414c97 760
8b968bd1
MW
761/* This contains cpp options which are not passed when the preprocessor
762 output will be used by another program. */
763static const char *cpp_debug_options = "%{d*}";
764
708bceb7 765/* NB: This is shared amongst all front-ends, except for Ada. */
ea414c97
ZW
766static const char *cc1_options =
767"%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
c1ce46a5 768 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
1890bccc 769 %1 %{!Q:-quiet} %{!dumpbase:-dumpbase %B} %{d*} %{m*} %{aux-info*}\
2153915d
AO
770 %{fcompare-debug-second:%:compare-debug-auxbase-opt(%b)} \
771 %{!fcompare-debug-second:%{c|S:%{o*:-auxbase-strip %*}%{!o*:-auxbase %b}}}%{!c:%{!S:-auxbase %b}} \
740ca4b2 772 %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs}\
3df89291 773 %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
e200444e
JM
774 %{Qn:-fno-ident} %{-help:--help}\
775 %{-target-help:--target-help}\
776 %{-version:--version}\
777 %{-help=*:--help=%*}\
49009afd 778 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
6de9cd9a 779 %{fsyntax-only:-o %j} %{-param*}\
ee4c708e
BE
780 %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants}\
781 %{coverage:-fprofile-arcs -ftest-coverage}";
ea414c97
ZW
782
783static const char *asm_options =
dc60b775
CD
784"%{--target-help:%:print-asm-header()} "
785#if HAVE_GNU_AS
786/* If GNU AS is used, then convert -w (no warnings), -I, and -v
787 to the assembler equivalents. */
788"%{v} %{w:-W} %{I*} "
789#endif
790"%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
ffd86336 791
5a8e2650 792static const char *invoke_as =
4977bab6 793#ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
d7f09764
DN
794"%{!fwpa:\
795 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
796 %{!S:-o %|.s |\n as %(asm_options) %|.s %A }\
797 }";
4977bab6 798#else
d7f09764
DN
799"%{!fwpa:\
800 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
801 %{!S:-o %|.s |\n as %(asm_options) %m.s %A }\
802 }";
4977bab6 803#endif
5a8e2650 804
ffd86336 805/* Some compilers have limits on line lengths, and the multilib_select
961b7009
MM
806 and/or multilib_matches strings can be very long, so we build them at
807 run time. */
ffd86336 808static struct obstack multilib_obstack;
3b304f5b
ZW
809static const char *multilib_select;
810static const char *multilib_matches;
811static const char *multilib_defaults;
812static const char *multilib_exclusions;
961b7009
MM
813
814/* Check whether a particular argument is a default argument. */
815
816#ifndef MULTILIB_DEFAULTS
817#define MULTILIB_DEFAULTS { "" }
818#endif
819
d25a45d4 820static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
ed1f651b 821
db36994b
RS
822#ifndef DRIVER_SELF_SPECS
823#define DRIVER_SELF_SPECS ""
824#endif
825
953ff289
DN
826/* Adding -fopenmp should imply pthreads. This is particularly important
827 for targets that use different start files and suchlike. */
828#ifndef GOMP_SELF_SPECS
daf49354 829#define GOMP_SELF_SPECS "%{fopenmp|ftree-parallelize-loops=*: -pthread}"
953ff289
DN
830#endif
831
832static const char *const driver_self_specs[] = {
b5b8b0ac 833 "%{fdump-final-insns:-fdump-final-insns=.} %<fdump-final-insns",
e11ddaf5 834 DRIVER_SELF_SPECS, CONFIGURE_SPECS, GOMP_SELF_SPECS
953ff289 835};
db36994b 836
7816bea0
DJ
837#ifndef OPTION_DEFAULT_SPECS
838#define OPTION_DEFAULT_SPECS { "", "" }
839#endif
840
841struct default_spec
842{
843 const char *name;
844 const char *spec;
845};
846
847static const struct default_spec
848 option_default_specs[] = { OPTION_DEFAULT_SPECS };
849
3ac63d94
NC
850struct user_specs
851{
d9ac3a07 852 struct user_specs *next;
878f32c3 853 const char *filename;
d9ac3a07
MM
854};
855
856static struct user_specs *user_specs_head, *user_specs_tail;
857
ed1f651b
RS
858\f
859/* Record the mapping from file suffixes for compilation specs. */
860
861struct compiler
862{
878f32c3 863 const char *suffix; /* Use this compiler for input files
ed1f651b 864 whose names end in this suffix. */
ec32609a 865
ea414c97 866 const char *spec; /* To use this compiler, run this spec. */
a9374841
MM
867
868 const char *cpp_spec; /* If non-NULL, substitute this spec
869 for `%C', rather than the usual
870 cpp_spec. */
1ea7e6ad 871 const int combinable; /* If nonzero, compiler can deal with
0855eab7 872 multiple source files at once (IMA). */
1ea7e6ad 873 const int needs_preprocessing; /* If nonzero, source files need to
0855eab7 874 be run through a preprocessor. */
ed1f651b
RS
875};
876
877/* Pointer to a vector of `struct compiler' that gives the spec for
878 compiling a file, based on its suffix.
879 A file that does not end in any of these suffixes will be passed
880 unchanged to the loader and nothing else will be done to it.
881
882 An entry containing two 0s is used to terminate the vector.
883
884 If multiple entries match a file, the last matching one is used. */
885
886static struct compiler *compilers;
887
888/* Number of entries in `compilers', not counting the null terminator. */
889
890static int n_compilers;
891
892/* The default list of file name suffixes and their compilation specs. */
893
5e65297b 894static const struct compiler default_compilers[] =
ed1f651b 895{
4689ad58 896 /* Add lists of suffixes of known languages here. If those languages
e9a25f70
JL
897 were not present when we built the driver, we will hit these copies
898 and be given a more meaningful error than "file not used since
4689ad58 899 linking is not done". */
0855eab7 900 {".m", "#Objective-C", 0, 0, 0}, {".mi", "#Objective-C", 0, 0, 0},
ad8c162b
ZL
901 {".mm", "#Objective-C++", 0, 0, 0}, {".M", "#Objective-C++", 0, 0, 0},
902 {".mii", "#Objective-C++", 0, 0, 0},
7904f95f
EC
903 {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0},
904 {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0},
0855eab7
CT
905 {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0},
906 {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0},
907 {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0},
3135ce84
FXC
908 {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0},
909 {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0},
910 {".ftn", "#Fortran", 0, 0, 0}, {".FTN", "#Fortran", 0, 0, 0},
911 {".fpp", "#Fortran", 0, 0, 0}, {".FPP", "#Fortran", 0, 0, 0},
912 {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0},
913 {".f95", "#Fortran", 0, 0, 0}, {".F95", "#Fortran", 0, 0, 0},
914 {".f03", "#Fortran", 0, 0, 0}, {".F03", "#Fortran", 0, 0, 0},
915 {".f08", "#Fortran", 0, 0, 0}, {".F08", "#Fortran", 0, 0, 0},
0855eab7
CT
916 {".r", "#Ratfor", 0, 0, 0},
917 {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0},
918 {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0},
919 {".zip", "#Java", 0, 0, 0}, {".jar", "#Java", 0, 0, 0},
4689ad58 920 /* Next come the entries for C. */
c3224d6f 921 {".c", "@c", 0, 0, 1},
ed1f651b 922 {"@c",
5a8e2650 923 /* cc1 has an integrated ISO C preprocessor. We should invoke the
f458d1d5 924 external preprocessor if -save-temps is given. */
f749a36b 925 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
5a8e2650 926 %{!E:%{!M:%{!MM:\
f458d1d5
ZW
927 %{traditional|ftraditional:\
928%eGNU C no longer supports -traditional without -E}\
c3224d6f
RG
929 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
930 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
931 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
932 %(cc1_options)}\
933 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
934 cc1 %(cpp_unique_options) %(cc1_options)}}}\
935 %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 1},
ed1f651b 936 {"-",
6ba40dd7 937 "%{!E:%e-E or -x required when input is from standard input}\
0855eab7
CT
938 %(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)", 0, 0, 0},
939 {".h", "@c-header", 0, 0, 0},
ed1f651b 940 {"@c-header",
17211ab5
GK
941 /* cc1 has an integrated ISO C preprocessor. We should invoke the
942 external preprocessor if -save-temps is given. */
943 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
944 %{!E:%{!M:%{!MM:\
14fdc613
MM
945 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
946 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
947 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
0de12fcc 948 %(cc1_options)\
9cc54940
AC
949 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
950 %W{o*:--output-pch=%*}}%V}\
14fdc613 951 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
17211ab5 952 cc1 %(cpp_unique_options) %(cc1_options)\
9cc54940
AC
953 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
954 %W{o*:--output-pch=%*}}%V}}}}}}", 0, 0, 0},
c3224d6f 955 {".i", "@cpp-output", 0, 0, 0},
ed1f651b 956 {"@cpp-output",
2a782c52 957 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0, 1, 0},
0855eab7 958 {".s", "@assembler", 0, 1, 0},
ed1f651b 959 {"@assembler",
0855eab7 960 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0, 1, 0},
55b8093e 961 {".sx", "@assembler-with-cpp", 0, 1, 0},
0855eab7 962 {".S", "@assembler-with-cpp", 0, 1, 0},
ed1f651b 963 {"@assembler-with-cpp",
4977bab6 964#ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
2710d6d7 965 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
8b968bd1 966 %{E|M|MM:%(cpp_debug_options)}\
4977bab6
ZW
967 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
968 as %(asm_debug) %(asm_options) %|.s %A }}}}"
969#else
2710d6d7 970 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
4977bab6
ZW
971 %{E|M|MM:%(cpp_debug_options)}\
972 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
973 as %(asm_debug) %(asm_options) %m.s %A }}}}"
974#endif
0855eab7 975 , 0, 1, 0},
1d088dee 976
1346ae41 977#include "specs.h"
f9da5064 978 /* Mark end of table. */
0855eab7 979 {0, 0, 0, 0, 0}
ed1f651b
RS
980};
981
982/* Number of elements in default_compilers, not counting the terminator. */
983
ca7558fc 984static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
ed1f651b 985
ed1f651b 986/* A vector of options to give to the linker.
368dfd3a 987 These options are accumulated by %x,
ed1f651b
RS
988 and substituted into the linker command with %X. */
989static int n_linker_options;
990static char **linker_options;
c9ebacb8
RS
991
992/* A vector of options to give to the assembler.
993 These options are accumulated by -Wa,
57cb9b60 994 and substituted into the assembler command with %Y. */
c9ebacb8
RS
995static int n_assembler_options;
996static char **assembler_options;
57cb9b60
JW
997
998/* A vector of options to give to the preprocessor.
999 These options are accumulated by -Wp,
1000 and substituted into the preprocessor command with %Z. */
1001static int n_preprocessor_options;
1002static char **preprocessor_options;
ed1f651b 1003\f
ed1f651b 1004static char *
1d088dee 1005skip_whitespace (char *p)
ed1f651b
RS
1006{
1007 while (1)
1008 {
1009 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1010 be considered whitespace. */
1011 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1012 return p + 1;
1013 else if (*p == '\n' || *p == ' ' || *p == '\t')
1014 p++;
1015 else if (*p == '#')
1016 {
d25a45d4
KH
1017 while (*p != '\n')
1018 p++;
ed1f651b
RS
1019 p++;
1020 }
1021 else
1022 break;
1023 }
1024
1025 return p;
1026}
2296d164
RK
1027/* Structures to keep track of prefixes to try when looking for files. */
1028
1029struct prefix_list
1030{
8060c8ee 1031 const char *prefix; /* String to prepend to the path. */
2296d164
RK
1032 struct prefix_list *next; /* Next in linked list. */
1033 int require_machine_suffix; /* Don't use without machine_suffix. */
1034 /* 2 means try both machine_suffix and just_machine_suffix. */
5bbcd587
JJ
1035 int priority; /* Sort key - priority within list. */
1036 int os_multilib; /* 1 if OS multilib scheme should be used,
1037 0 for GCC multilib scheme. */
2296d164
RK
1038};
1039
1040struct path_prefix
1041{
1042 struct prefix_list *plist; /* List of prefixes to try */
1043 int max_len; /* Max length of a prefix in PLIST */
1044 const char *name; /* Name of this list (used in config stuff) */
1045};
1046
1047/* List of prefixes to try when looking for executables. */
1048
1049static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1050
1051/* List of prefixes to try when looking for startup (crt0) files. */
1052
1053static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1054
1055/* List of prefixes to try when looking for include files. */
1056
1057static struct path_prefix include_prefixes = { 0, 0, "include" };
1058
1059/* Suffix to attach to directories searched for commands.
1060 This looks like `MACHINE/VERSION/'. */
1061
1062static const char *machine_suffix = 0;
1063
1064/* Suffix to attach to directories searched for commands.
1065 This is just `MACHINE/'. */
1066
1067static const char *just_machine_suffix = 0;
1068
1069/* Adjusted value of GCC_EXEC_PREFIX envvar. */
1070
1071static const char *gcc_exec_prefix;
1072
a8ee6e2d
GK
1073/* Adjusted value of standard_libexec_prefix. */
1074
1075static const char *gcc_libexec_prefix;
1076
2296d164
RK
1077/* Default prefixes to attach to command names. */
1078
656c7a3a
AL
1079#ifndef STANDARD_STARTFILE_PREFIX_1
1080#define STANDARD_STARTFILE_PREFIX_1 "/lib/"
1081#endif
1082#ifndef STANDARD_STARTFILE_PREFIX_2
1083#define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
1084#endif
7904f95f 1085
2989d30c 1086#ifdef CROSS_DIRECTORY_STRUCTURE /* Don't use these prefixes for a cross compiler. */
2296d164
RK
1087#undef MD_EXEC_PREFIX
1088#undef MD_STARTFILE_PREFIX
1089#undef MD_STARTFILE_PREFIX_1
1090#endif
1091
1092/* If no prefixes defined, use the null string, which will disable them. */
1093#ifndef MD_EXEC_PREFIX
1094#define MD_EXEC_PREFIX ""
1095#endif
1096#ifndef MD_STARTFILE_PREFIX
1097#define MD_STARTFILE_PREFIX ""
1098#endif
1099#ifndef MD_STARTFILE_PREFIX_1
1100#define MD_STARTFILE_PREFIX_1 ""
1101#endif
1102
f4c0a303
CD
1103/* These directories are locations set at configure-time based on the
1104 --prefix option provided to configure. Their initializers are
1105 defined in Makefile.in. These paths are not *directly* used when
1106 gcc_exec_prefix is set because, in that case, we know where the
1107 compiler has been installed, and use paths relative to that
1108 location instead. */
27c38fbe 1109static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
f4c0a303
CD
1110static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
1111static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1112static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1113
1114/* For native compilers, these are well-known paths containing
1115 components that may be provided by the system. For cross
1116 compilers, these paths are not used. */
2296d164 1117static const char *md_exec_prefix = MD_EXEC_PREFIX;
2296d164
RK
1118static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1119static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
c662432e 1120static const char *const standard_startfile_prefix_1
656c7a3a
AL
1121 = STANDARD_STARTFILE_PREFIX_1;
1122static const char *const standard_startfile_prefix_2
1123 = STANDARD_STARTFILE_PREFIX_2;
2296d164 1124
f4c0a303
CD
1125/* A relative path to be used in finding the location of tools
1126 relative to the driver. */
27c38fbe 1127static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
a8ee6e2d 1128
2296d164
RK
1129/* Subdirectory to use for locating libraries. Set by
1130 set_multilib_dir based on the compilation options. */
1131
1132static const char *multilib_dir;
5bbcd587
JJ
1133
1134/* Subdirectory to use for locating libraries in OS conventions. Set by
1135 set_multilib_dir based on the compilation options. */
1136
1137static const char *multilib_os_dir;
ed1f651b 1138\f
0f41302f 1139/* Structure to keep track of the specs that have been defined so far.
4089dfab
JL
1140 These are accessed using %(specname) or %[specname] in a compiler
1141 or link spec. */
ed1f651b
RS
1142
1143struct spec_list
1144{
79aff5ac
MM
1145 /* The following 2 fields must be first */
1146 /* to allow EXTRA_SPECS to be initialized */
3b304f5b
ZW
1147 const char *name; /* name of the spec. */
1148 const char *ptr; /* available ptr if no static pointer */
79aff5ac
MM
1149
1150 /* The following fields are not initialized */
1151 /* by EXTRA_SPECS */
3b304f5b 1152 const char **ptr_spec; /* pointer to the spec itself. */
79aff5ac
MM
1153 struct spec_list *next; /* Next spec in linked list. */
1154 int name_len; /* length of the name */
1155 int alloc_p; /* whether string was allocated */
ed1f651b
RS
1156};
1157
79aff5ac 1158#define INIT_STATIC_SPEC(NAME,PTR) \
6496a589 1159{ NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
79aff5ac 1160
3ac63d94
NC
1161/* List of statically defined specs. */
1162static struct spec_list static_specs[] =
1163{
79aff5ac 1164 INIT_STATIC_SPEC ("asm", &asm_spec),
5f0e9ea2 1165 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
79aff5ac 1166 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
ea414c97 1167 INIT_STATIC_SPEC ("asm_options", &asm_options),
5a8e2650 1168 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
79aff5ac 1169 INIT_STATIC_SPEC ("cpp", &cpp_spec),
ea414c97 1170 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
8b968bd1 1171 INIT_STATIC_SPEC ("cpp_debug_options", &cpp_debug_options),
ffdeea47 1172 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
ea414c97 1173 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
79aff5ac 1174 INIT_STATIC_SPEC ("cc1", &cc1_spec),
ea414c97 1175 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
79aff5ac 1176 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
bbd7687d 1177 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
77008252 1178 INIT_STATIC_SPEC ("link_ssp", &link_ssp_spec),
79aff5ac
MM
1179 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1180 INIT_STATIC_SPEC ("link", &link_spec),
1181 INIT_STATIC_SPEC ("lib", &lib_spec),
6de9cd9a
DN
1182 INIT_STATIC_SPEC ("mfwrap", &mfwrap_spec),
1183 INIT_STATIC_SPEC ("mflib", &mflib_spec),
953ff289 1184 INIT_STATIC_SPEC ("link_gomp", &link_gomp_spec),
79aff5ac
MM
1185 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1186 INIT_STATIC_SPEC ("startfile", &startfile_spec),
79aff5ac
MM
1187 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1188 INIT_STATIC_SPEC ("version", &compiler_version),
1189 INIT_STATIC_SPEC ("multilib", &multilib_select),
1190 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1191 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1192 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
0a8d6618 1193 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
5bbcd587 1194 INIT_STATIC_SPEC ("multilib_options", &multilib_options),
10da1131 1195 INIT_STATIC_SPEC ("linker", &linker_name_spec),
d7f09764
DN
1196 INIT_STATIC_SPEC ("linker_plugin_file", &linker_plugin_file_spec),
1197 INIT_STATIC_SPEC ("lto_wrapper", &lto_wrapper_spec),
1198 INIT_STATIC_SPEC ("lto_gcc", &lto_gcc_spec),
1199 INIT_STATIC_SPEC ("lto_libgcc", &lto_libgcc_spec),
ea414c97 1200 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
2296d164
RK
1201 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1202 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1203 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
343f59d9 1204 INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
380e5ca4 1205 INIT_STATIC_SPEC ("sysroot_spec", &sysroot_spec),
e7f13528
GP
1206 INIT_STATIC_SPEC ("sysroot_suffix_spec", &sysroot_suffix_spec),
1207 INIT_STATIC_SPEC ("sysroot_hdrs_suffix_spec", &sysroot_hdrs_suffix_spec),
79aff5ac
MM
1208};
1209
1210#ifdef EXTRA_SPECS /* additional specs needed */
829245be 1211/* Structure to keep track of just the first two args of a spec_list.
3ac63d94 1212 That is all that the EXTRA_SPECS macro gives us. */
829245be
KG
1213struct spec_list_1
1214{
8b60264b
KG
1215 const char *const name;
1216 const char *const ptr;
829245be
KG
1217};
1218
8b60264b 1219static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
d25a45d4 1220static struct spec_list *extra_specs = (struct spec_list *) 0;
79aff5ac
MM
1221#endif
1222
1223/* List of dynamically allocates specs that have been defined so far. */
1224
9218435e 1225static struct spec_list *specs = (struct spec_list *) 0;
79aff5ac 1226\f
f3226a90
JT
1227/* List of static spec functions. */
1228
1229static const struct spec_function static_spec_functions[] =
1230{
30d8946b 1231 { "getenv", getenv_spec_function },
f3226a90 1232 { "if-exists", if_exists_spec_function },
152a5a9c 1233 { "if-exists-else", if_exists_else_spec_function },
3dd53121 1234 { "replace-outfile", replace_outfile_spec_function },
2642f659 1235 { "remove-outfile", remove_outfile_spec_function },
ed5b9f96 1236 { "version-compare", version_compare_spec_function },
953ff289 1237 { "include", include_spec_function },
4adbd5dd 1238 { "find-file", find_file_spec_function },
c1ce46a5 1239 { "find-plugindir", find_plugindir_spec_function },
a0f87454 1240 { "print-asm-header", print_asm_header_spec_function },
2153915d
AO
1241 { "compare-debug-dump-opt", compare_debug_dump_opt_spec_function },
1242 { "compare-debug-self-opt", compare_debug_self_opt_spec_function },
1243 { "compare-debug-auxbase-opt", compare_debug_auxbase_opt_spec_function },
fa959ce4
MM
1244#ifdef EXTRA_SPEC_FUNCTIONS
1245 EXTRA_SPEC_FUNCTIONS
1246#endif
f3226a90
JT
1247 { 0, 0 }
1248};
1249
1250static int processing_spec_function;
1251\f
049f6ec9
MM
1252/* Add appropriate libgcc specs to OBSTACK, taking into account
1253 various permutations of -shared-libgcc, -shared, and such. */
1254
328163dc 1255#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
8efcd34f
AM
1256
1257#ifndef USE_LD_AS_NEEDED
1258#define USE_LD_AS_NEEDED 0
1259#endif
1260
049f6ec9 1261static void
1d088dee
AJ
1262init_gcc_specs (struct obstack *obstack, const char *shared_name,
1263 const char *static_name, const char *eh_name)
049f6ec9 1264{
5c181756
AO
1265 char *buf;
1266
5e4f1974
AM
1267 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1268 "%{!static:%{!static-libgcc:"
8efcd34f 1269#if USE_LD_AS_NEEDED
5e4f1974
AM
1270 "%{!shared-libgcc:",
1271 static_name, " --as-needed ", shared_name, " --no-as-needed"
1272 "}"
1273 "%{shared-libgcc:",
1274 shared_name, "%{!shared: ", static_name, "}"
1275 "}"
765f1bf9 1276#else
5e4f1974
AM
1277 "%{!shared:"
1278 "%{!shared-libgcc:", static_name, " ", eh_name, "}"
1279 "%{shared-libgcc:", shared_name, " ", static_name, "}"
1280 "}"
275b60d6 1281#ifdef LINK_EH_SPEC
5e4f1974
AM
1282 "%{shared:"
1283 "%{shared-libgcc:", shared_name, "}"
1284 "%{!shared-libgcc:", static_name, "}"
1285 "}"
275b60d6 1286#else
5e4f1974 1287 "%{shared:", shared_name, "}"
765f1bf9 1288#endif
275b60d6 1289#endif
5e4f1974 1290 "}}", NULL);
5c181756
AO
1291
1292 obstack_grow (obstack, buf, strlen (buf));
1293 free (buf);
049f6ec9 1294}
6894579f 1295#endif /* ENABLE_SHARED_LIBGCC */
049f6ec9 1296
79aff5ac
MM
1297/* Initialize the specs lookup routines. */
1298
1299static void
7e51717c 1300init_spec (void)
79aff5ac 1301{
9218435e
KH
1302 struct spec_list *next = (struct spec_list *) 0;
1303 struct spec_list *sl = (struct spec_list *) 0;
79aff5ac
MM
1304 int i;
1305
1306 if (specs)
3ac63d94 1307 return; /* Already initialized. */
79aff5ac 1308
20df0482 1309 if (verbose_flag)
2dec80c7 1310 fnotice (stderr, "Using built-in specs.\n");
20df0482 1311
79aff5ac 1312#ifdef EXTRA_SPECS
e1e4cdc4 1313 extra_specs = XCNEWVEC (struct spec_list, ARRAY_SIZE (extra_specs_1));
9218435e 1314
b6a1cbae 1315 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
03fc1620
JW
1316 {
1317 sl = &extra_specs[i];
829245be
KG
1318 sl->name = extra_specs_1[i].name;
1319 sl->ptr = extra_specs_1[i].ptr;
03fc1620
JW
1320 sl->next = next;
1321 sl->name_len = strlen (sl->name);
1322 sl->ptr_spec = &sl->ptr;
1323 next = sl;
1324 }
79aff5ac
MM
1325#endif
1326
b6a1cbae 1327 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
79aff5ac
MM
1328 {
1329 sl = &static_specs[i];
1330 sl->next = next;
1331 next = sl;
1332 }
1333
328163dc 1334#if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
9db0819e
RH
1335 /* ??? If neither -shared-libgcc nor --static-libgcc was
1336 seen, then we should be making an educated guess. Some proposed
1337 heuristics for ELF include:
1338
1339 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1340 program will be doing dynamic loading, which will likely
1341 need the shared libgcc.
1342
1343 (2) If "-ldl", then it's also a fair bet that we're doing
1344 dynamic loading.
1345
1346 (3) For each ET_DYN we're linking against (either through -lfoo
1347 or /some/path/foo.so), check to see whether it or one of
ff7cc307 1348 its dependencies depends on a shared libgcc.
9db0819e
RH
1349
1350 (4) If "-shared"
1351
1352 If the runtime is fixed to look for program headers instead
1353 of calling __register_frame_info at all, for each object,
1354 use the shared libgcc if any EH symbol referenced.
1355
1356 If crtstuff is fixed to not invoke __register_frame_info
1357 automatically, for each object, use the shared libgcc if
1358 any non-empty unwind section found.
1359
1360 Doing any of this probably requires invoking an external program to
1361 do the actual object file scanning. */
1362 {
1363 const char *p = libgcc_spec;
1364 int in_sep = 1;
589005ff 1365
16757495 1366 /* Transform the extant libgcc_spec into one that uses the shared libgcc
9db0819e
RH
1367 when given the proper command line arguments. */
1368 while (*p)
1369 {
589005ff 1370 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
9db0819e 1371 {
049f6ec9 1372 init_gcc_specs (&obstack,
049f6ec9 1373 "-lgcc_s"
443728bb
L
1374#ifdef USE_LIBUNWIND_EXCEPTIONS
1375 " -lunwind"
4977bab6 1376#endif
348d71c7
JW
1377 ,
1378 "-lgcc",
1379 "-lgcc_eh"
4977bab6 1380#ifdef USE_LIBUNWIND_EXCEPTIONS
7e9d8517
L
1381# ifdef HAVE_LD_STATIC_DYNAMIC
1382 " %{!static:-Bstatic} -lunwind %{!static:-Bdynamic}"
1383# else
4977bab6 1384 " -lunwind"
7e9d8517 1385# endif
9db0819e 1386#endif
348d71c7
JW
1387 );
1388
ba2b7435 1389 p += 5;
9db0819e
RH
1390 in_sep = 0;
1391 }
ba2b7435 1392 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
9db0819e
RH
1393 {
1394 /* Ug. We don't know shared library extensions. Hope that
1395 systems that use this form don't do shared libraries. */
049f6ec9 1396 init_gcc_specs (&obstack,
4e8d0554 1397 "-lgcc_s",
275b60d6 1398 "libgcc.a%s",
aedec8dd
JW
1399 "libgcc_eh.a%s"
1400#ifdef USE_LIBUNWIND_EXCEPTIONS
1401 " -lunwind"
1402#endif
1403 );
ba2b7435 1404 p += 10;
9db0819e
RH
1405 in_sep = 0;
1406 }
1407 else
1408 {
1409 obstack_1grow (&obstack, *p);
1410 in_sep = (*p == ' ');
1411 p += 1;
1412 }
1413 }
1414
1415 obstack_1grow (&obstack, '\0');
7973fd2a 1416 libgcc_spec = XOBFINISH (&obstack, const char *);
9db0819e
RH
1417 }
1418#endif
c64688ae
RH
1419#ifdef USE_AS_TRADITIONAL_FORMAT
1420 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1421 {
8b60264b 1422 static const char tf[] = "--traditional-format ";
c64688ae
RH
1423 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1424 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
7973fd2a 1425 asm_spec = XOBFINISH (&obstack, const char *);
c64688ae
RH
1426 }
1427#endif
cd533689
PP
1428
1429#if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC
1430# ifdef LINK_BUILDID_SPEC
1431 /* Prepend LINK_BUILDID_SPEC to whatever link_spec we had before. */
1432 obstack_grow (&obstack, LINK_BUILDID_SPEC, sizeof(LINK_BUILDID_SPEC) - 1);
1433# endif
1434# ifdef LINK_EH_SPEC
275b60d6
JJ
1435 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1436 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
cd533689 1437# endif
275b60d6 1438 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
7973fd2a 1439 link_spec = XOBFINISH (&obstack, const char *);
275b60d6 1440#endif
9db0819e 1441
79aff5ac
MM
1442 specs = sl;
1443}
ed1f651b
RS
1444\f
1445/* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1446 removed; If the spec starts with a + then SPEC is added to the end of the
0f41302f 1447 current spec. */
ed1f651b
RS
1448
1449static void
1d088dee 1450set_spec (const char *name, const char *spec)
ed1f651b
RS
1451{
1452 struct spec_list *sl;
3b304f5b 1453 const char *old_spec;
79aff5ac
MM
1454 int name_len = strlen (name);
1455 int i;
1456
3ac63d94 1457 /* If this is the first call, initialize the statically allocated specs. */
20df0482
MM
1458 if (!specs)
1459 {
9218435e 1460 struct spec_list *next = (struct spec_list *) 0;
b6a1cbae 1461 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
20df0482
MM
1462 {
1463 sl = &static_specs[i];
1464 sl->next = next;
1465 next = sl;
1466 }
1467 specs = sl;
1468 }
1469
3ac63d94 1470 /* See if the spec already exists. */
ed1f651b 1471 for (sl = specs; sl; sl = sl->next)
79aff5ac 1472 if (name_len == sl->name_len && !strcmp (sl->name, name))
ed1f651b
RS
1473 break;
1474
1475 if (!sl)
1476 {
3ac63d94 1477 /* Not found - make it. */
5ed6ace5 1478 sl = XNEW (struct spec_list);
ad85216e 1479 sl->name = xstrdup (name);
79aff5ac
MM
1480 sl->name_len = name_len;
1481 sl->ptr_spec = &sl->ptr;
1482 sl->alloc_p = 0;
1483 *(sl->ptr_spec) = "";
ed1f651b
RS
1484 sl->next = specs;
1485 specs = sl;
1486 }
1487
79aff5ac 1488 old_spec = *(sl->ptr_spec);
e51712db 1489 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
d4f2852f 1490 ? concat (old_spec, spec + 1, NULL)
ad85216e 1491 : xstrdup (spec));
841faeed 1492
20df0482
MM
1493#ifdef DEBUG_SPECS
1494 if (verbose_flag)
2dec80c7 1495 fnotice (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
20df0482
MM
1496#endif
1497
3ac63d94 1498 /* Free the old spec. */
79aff5ac 1499 if (old_spec && sl->alloc_p)
b1d5455a 1500 free (CONST_CAST(char *, old_spec));
79aff5ac
MM
1501
1502 sl->alloc_p = 1;
ed1f651b
RS
1503}
1504\f
1505/* Accumulate a command (program name and args), and run it. */
1506
5b634ee0
JM
1507typedef const char *const_char_p; /* For DEF_VEC_P. */
1508DEF_VEC_P(const_char_p);
1509DEF_VEC_ALLOC_P(const_char_p,heap);
ed1f651b 1510
5b634ee0 1511/* Vector of pointers to arguments in the current line of specifications. */
ed1f651b 1512
5b634ee0 1513static VEC(const_char_p,heap) *argbuf;
ed1f651b 1514
5b634ee0 1515/* Position in the argbuf vector containing the name of the output file
0855eab7
CT
1516 (the value associated with the "-o" flag). */
1517
1518static int have_o_argbuf_index = 0;
1519
1b5bc8e9 1520/* Were the options -c, -S or -E passed. */
af41c57d
AP
1521static int have_c = 0;
1522
1523/* Was the option -o passed. */
1524static int have_o = 0;
1525
49009afd
JL
1526/* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1527 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1528 it here. */
fb266030
TW
1529
1530static struct temp_name {
878f32c3 1531 const char *suffix; /* suffix associated with the code. */
fb266030
TW
1532 int length; /* strlen (suffix). */
1533 int unique; /* Indicates whether %g or %u/%U was used. */
878f32c3 1534 const char *filename; /* associated filename. */
fb266030
TW
1535 int filename_length; /* strlen (filename). */
1536 struct temp_name *next;
1537} *temp_names;
39d45901 1538
ed1f651b
RS
1539/* Number of commands executed so far. */
1540
1541static int execution_count;
1542
3b9b4d3f
RS
1543/* Number of commands that exited with a signal. */
1544
1545static int signal_count;
ed1f651b 1546\f
f3226a90
JT
1547/* Allocate the argument vector. */
1548
1549static void
7e51717c 1550alloc_args (void)
f3226a90 1551{
5b634ee0 1552 argbuf = VEC_alloc (const_char_p, heap, 10);
f3226a90
JT
1553}
1554
ed1f651b
RS
1555/* Clear out the vector of arguments (after a command is executed). */
1556
1557static void
1d088dee 1558clear_args (void)
ed1f651b 1559{
5b634ee0 1560 VEC_truncate (const_char_p, argbuf, 0);
ed1f651b
RS
1561}
1562
1563/* Add one argument to the vector at the end.
1564 This is done when a space is seen or at the end of the line.
1565 If DELETE_ALWAYS is nonzero, the arg is a filename
1566 and the file should be deleted eventually.
1567 If DELETE_FAILURE is nonzero, the arg is a filename
1568 and the file should be deleted if this compilation fails. */
1569
1570static void
1d088dee 1571store_arg (const char *arg, int delete_always, int delete_failure)
ed1f651b 1572{
5b634ee0 1573 VEC_safe_push (const_char_p, heap, argbuf, arg);
ed1f651b 1574
0855eab7 1575 if (strcmp (arg, "-o") == 0)
5b634ee0 1576 have_o_argbuf_index = VEC_length (const_char_p, argbuf);
ed1f651b 1577 if (delete_always || delete_failure)
d6205185
RG
1578 {
1579 const char *p;
1580 /* If the temporary file we should delete is specified as
1581 part of a joined argument extract the filename. */
1582 if (arg[0] == '-'
1583 && (p = strrchr (arg, '=')))
1584 arg = p + 1;
1585 record_temp_file (arg, delete_always, delete_failure);
1586 }
ed1f651b
RS
1587}
1588\f
ff7cc307 1589/* Load specs from a file name named FILENAME, replacing occurrences of
9218435e 1590 various different types of line-endings, \r\n, \n\r and just \r, with
b633b6c0 1591 a single \n. */
20df0482 1592
d25a45d4 1593static char *
1d088dee 1594load_specs (const char *filename)
20df0482
MM
1595{
1596 int desc;
1597 int readlen;
1598 struct stat statbuf;
1599 char *buffer;
b633b6c0
MK
1600 char *buffer_p;
1601 char *specs;
1602 char *specs_p;
20df0482
MM
1603
1604 if (verbose_flag)
2dec80c7 1605 fnotice (stderr, "Reading specs from %s\n", filename);
20df0482
MM
1606
1607 /* Open and stat the file. */
1608 desc = open (filename, O_RDONLY, 0);
1609 if (desc < 0)
1610 pfatal_with_name (filename);
1611 if (stat (filename, &statbuf) < 0)
1612 pfatal_with_name (filename);
1613
1614 /* Read contents of file into BUFFER. */
5ed6ace5 1615 buffer = XNEWVEC (char, statbuf.st_size + 1);
20df0482
MM
1616 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1617 if (readlen < 0)
1618 pfatal_with_name (filename);
1619 buffer[readlen] = 0;
1620 close (desc);
1621
5ed6ace5 1622 specs = XNEWVEC (char, readlen + 1);
b633b6c0
MK
1623 specs_p = specs;
1624 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1625 {
1626 int skip = 0;
1627 char c = *buffer_p;
1628 if (c == '\r')
d25a45d4
KH
1629 {
1630 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
b633b6c0 1631 skip = 1;
d25a45d4 1632 else if (*(buffer_p + 1) == '\n') /* \r\n */
b633b6c0
MK
1633 skip = 1;
1634 else /* \r */
1635 c = '\n';
1636 }
1637 if (! skip)
1638 *specs_p++ = c;
1639 }
1640 *specs_p = '\0';
1641
1642 free (buffer);
1643 return (specs);
1644}
1645
1646/* Read compilation specs from a file named FILENAME,
1647 replacing the default ones.
1648
1649 A suffix which starts with `*' is a definition for
1650 one of the machine-specific sub-specs. The "suffix" should be
0fef3fd0 1651 *asm, *cc1, *cpp, *link, *startfile, etc.
b633b6c0
MK
1652 The corresponding spec is stored in asm_spec, etc.,
1653 rather than in the `compilers' vector.
1654
1655 Anything invalid in the file is a fatal error. */
1656
1657static void
1d088dee 1658read_specs (const char *filename, int main_p)
b633b6c0
MK
1659{
1660 char *buffer;
b3694847 1661 char *p;
b633b6c0
MK
1662
1663 buffer = load_specs (filename);
1664
20df0482
MM
1665 /* Scan BUFFER for specs, putting them in the vector. */
1666 p = buffer;
1667 while (1)
1668 {
1669 char *suffix;
1670 char *spec;
1671 char *in, *out, *p1, *p2, *p3;
1672
1673 /* Advance P in BUFFER to the next nonblank nocomment line. */
1674 p = skip_whitespace (p);
1675 if (*p == 0)
1676 break;
1677
1678 /* Is this a special command that starts with '%'? */
1679 /* Don't allow this for the main specs file, since it would
1680 encourage people to overwrite it. */
1681 if (*p == '%' && !main_p)
1682 {
1683 p1 = p;
e9a25f70
JL
1684 while (*p && *p != '\n')
1685 p++;
1686
d25a45d4
KH
1687 /* Skip '\n'. */
1688 p++;
20df0482 1689
d25a45d4 1690 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
e9a25f70
JL
1691 && (p1[sizeof "%include" - 1] == ' '
1692 || p1[sizeof "%include" - 1] == '\t'))
20df0482
MM
1693 {
1694 char *new_filename;
1695
1696 p1 += sizeof ("%include");
e9a25f70
JL
1697 while (*p1 == ' ' || *p1 == '\t')
1698 p1++;
20df0482
MM
1699
1700 if (*p1++ != '<' || p[-2] != '>')
2dec80c7
JM
1701 fatal_error ("specs %%include syntax malformed after "
1702 "%ld characters",
1703 (long) (p1 - buffer + 1));
20df0482
MM
1704
1705 p[-2] = '\0';
00dcee0c 1706 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
20df0482
MM
1707 read_specs (new_filename ? new_filename : p1, FALSE);
1708 continue;
1709 }
e9a25f70
JL
1710 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1711 && (p1[sizeof "%include_noerr" - 1] == ' '
1712 || p1[sizeof "%include_noerr" - 1] == '\t'))
20df0482
MM
1713 {
1714 char *new_filename;
1715
e9a25f70 1716 p1 += sizeof "%include_noerr";
d25a45d4
KH
1717 while (*p1 == ' ' || *p1 == '\t')
1718 p1++;
20df0482
MM
1719
1720 if (*p1++ != '<' || p[-2] != '>')
2dec80c7
JM
1721 fatal_error ("specs %%include syntax malformed after "
1722 "%ld characters",
1723 (long) (p1 - buffer + 1));
20df0482
MM
1724
1725 p[-2] = '\0';
00dcee0c 1726 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
20df0482
MM
1727 if (new_filename)
1728 read_specs (new_filename, FALSE);
1729 else if (verbose_flag)
2dec80c7 1730 fnotice (stderr, "could not find specs file %s\n", p1);
20df0482
MM
1731 continue;
1732 }
e9a25f70
JL
1733 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1734 && (p1[sizeof "%rename" - 1] == ' '
1735 || p1[sizeof "%rename" - 1] == '\t'))
20df0482
MM
1736 {
1737 int name_len;
1738 struct spec_list *sl;
169ce44d 1739 struct spec_list *newsl;
20df0482 1740
169ce44d 1741 /* Get original name. */
e9a25f70
JL
1742 p1 += sizeof "%rename";
1743 while (*p1 == ' ' || *p1 == '\t')
1744 p1++;
1745
9218435e 1746 if (! ISALPHA ((unsigned char) *p1))
2dec80c7
JM
1747 fatal_error ("specs %%rename syntax malformed after "
1748 "%ld characters",
1749 (long) (p1 - buffer));
20df0482
MM
1750
1751 p2 = p1;
9218435e 1752 while (*p2 && !ISSPACE ((unsigned char) *p2))
e9a25f70
JL
1753 p2++;
1754
20df0482 1755 if (*p2 != ' ' && *p2 != '\t')
2dec80c7
JM
1756 fatal_error ("specs %%rename syntax malformed after "
1757 "%ld characters",
1758 (long) (p2 - buffer));
20df0482
MM
1759
1760 name_len = p2 - p1;
1761 *p2++ = '\0';
e9a25f70
JL
1762 while (*p2 == ' ' || *p2 == '\t')
1763 p2++;
1764
9218435e 1765 if (! ISALPHA ((unsigned char) *p2))
2dec80c7
JM
1766 fatal_error ("specs %%rename syntax malformed after "
1767 "%ld characters",
1768 (long) (p2 - buffer));
20df0482 1769
d25a45d4 1770 /* Get new spec name. */
20df0482 1771 p3 = p2;
9218435e 1772 while (*p3 && !ISSPACE ((unsigned char) *p3))
e9a25f70
JL
1773 p3++;
1774
d25a45d4 1775 if (p3 != p - 1)
2dec80c7
JM
1776 fatal_error ("specs %%rename syntax malformed after "
1777 "%ld characters",
1778 (long) (p3 - buffer));
20df0482
MM
1779 *p3 = '\0';
1780
1781 for (sl = specs; sl; sl = sl->next)
1782 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1783 break;
1784
1785 if (!sl)
2dec80c7 1786 fatal_error ("specs %s spec was not found to be renamed", p1);
20df0482 1787
e9a25f70 1788 if (strcmp (p1, p2) == 0)
20df0482
MM
1789 continue;
1790
169ce44d
NC
1791 for (newsl = specs; newsl; newsl = newsl->next)
1792 if (strcmp (newsl->name, p2) == 0)
bdc6b402
JM
1793 fatal_error ("%s: attempt to rename spec %qs to "
1794 "already defined spec %qs",
169ce44d
NC
1795 filename, p1, p2);
1796
20df0482
MM
1797 if (verbose_flag)
1798 {
2dec80c7 1799 fnotice (stderr, "rename spec %s to %s\n", p1, p2);
20df0482 1800#ifdef DEBUG_SPECS
2dec80c7 1801 fnotice (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
20df0482
MM
1802#endif
1803 }
1804
1805 set_spec (p2, *(sl->ptr_spec));
1806 if (sl->alloc_p)
b1d5455a 1807 free (CONST_CAST (char *, *(sl->ptr_spec)));
20df0482
MM
1808
1809 *(sl->ptr_spec) = "";
1810 sl->alloc_p = 0;
1811 continue;
1812 }
1813 else
2dec80c7
JM
1814 fatal_error ("specs unknown %% command after %ld characters",
1815 (long) (p1 - buffer));
20df0482
MM
1816 }
1817
1818 /* Find the colon that should end the suffix. */
1819 p1 = p;
e9a25f70
JL
1820 while (*p1 && *p1 != ':' && *p1 != '\n')
1821 p1++;
1822
20df0482
MM
1823 /* The colon shouldn't be missing. */
1824 if (*p1 != ':')
2dec80c7
JM
1825 fatal_error ("specs file malformed after %ld characters",
1826 (long) (p1 - buffer));
e9a25f70 1827
20df0482
MM
1828 /* Skip back over trailing whitespace. */
1829 p2 = p1;
e9a25f70
JL
1830 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1831 p2--;
1832
20df0482
MM
1833 /* Copy the suffix to a string. */
1834 suffix = save_string (p, p2 - p);
1835 /* Find the next line. */
1836 p = skip_whitespace (p1 + 1);
1837 if (p[1] == 0)
2dec80c7
JM
1838 fatal_error ("specs file malformed after %ld characters",
1839 (long) (p - buffer));
e9a25f70 1840
20df0482 1841 p1 = p;
bbeb7b65
JW
1842 /* Find next blank line or end of string. */
1843 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
e9a25f70
JL
1844 p1++;
1845
20df0482
MM
1846 /* Specs end at the blank line and do not include the newline. */
1847 spec = save_string (p, p1 - p);
1848 p = p1;
1849
1850 /* Delete backslash-newline sequences from the spec. */
1851 in = spec;
1852 out = spec;
1853 while (*in != 0)
1854 {
1855 if (in[0] == '\\' && in[1] == '\n')
1856 in += 2;
1857 else if (in[0] == '#')
e9a25f70
JL
1858 while (*in && *in != '\n')
1859 in++;
1860
20df0482
MM
1861 else
1862 *out++ = *in++;
1863 }
1864 *out = 0;
1865
1866 if (suffix[0] == '*')
1867 {
1868 if (! strcmp (suffix, "*link_command"))
1869 link_command_spec = spec;
1870 else
1871 set_spec (suffix + 1, spec);
1872 }
1873 else
1874 {
1875 /* Add this pair to the vector. */
1876 compilers
e1e4cdc4 1877 = XRESIZEVEC (struct compiler, compilers, n_compilers + 2);
e9a25f70 1878
20df0482 1879 compilers[n_compilers].suffix = suffix;
ea414c97 1880 compilers[n_compilers].spec = spec;
20df0482 1881 n_compilers++;
9257393c 1882 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
20df0482
MM
1883 }
1884
1885 if (*suffix == 0)
1886 link_command_spec = spec;
1887 }
1888
1889 if (link_command_spec == 0)
2dec80c7 1890 fatal_error ("spec file has no spec for linking");
20df0482
MM
1891}
1892\f
ed1f651b
RS
1893/* Record the names of temporary files we tell compilers to write,
1894 and delete them at the end of the run. */
1895
aa34f42c
RG
1896/* This is the common prefix we use to make temp file names.
1897 It is chosen once for each run of this program.
1898 It is substituted into a spec by %g or %j.
1899 Thus, all temp file names contain this prefix.
1900 In practice, all temp file names start with this prefix.
1901
1902 This prefix comes from the envvar TMPDIR if it is defined;
1903 otherwise, from the P_tmpdir macro if that is defined;
1904 otherwise, in /usr/tmp or /tmp;
1905 or finally the current directory if all else fails. */
1906
1907static const char *temp_filename;
1908
1909/* Length of the prefix. */
1910
1911static int temp_filename_length;
1912
ed1f651b
RS
1913/* Define the list of temporary files to delete. */
1914
1915struct temp_file
1916{
878f32c3 1917 const char *name;
ed1f651b
RS
1918 struct temp_file *next;
1919};
1920
1921/* Queue of files to delete on success or failure of compilation. */
1922static struct temp_file *always_delete_queue;
1923/* Queue of files to delete on failure of compilation. */
1924static struct temp_file *failure_delete_queue;
1925
1926/* Record FILENAME as a file to be deleted automatically.
1927 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1928 otherwise delete it in any case.
1929 FAIL_DELETE nonzero means delete it if a compilation step fails;
1930 otherwise delete it in any case. */
1931
d991c721 1932void
1d088dee 1933record_temp_file (const char *filename, int always_delete, int fail_delete)
ed1f651b 1934{
b3694847 1935 char *const name = xstrdup (filename);
ed1f651b
RS
1936
1937 if (always_delete)
1938 {
b3694847 1939 struct temp_file *temp;
ed1f651b
RS
1940 for (temp = always_delete_queue; temp; temp = temp->next)
1941 if (! strcmp (name, temp->name))
1942 goto already1;
e9a25f70 1943
5ed6ace5 1944 temp = XNEW (struct temp_file);
ed1f651b
RS
1945 temp->next = always_delete_queue;
1946 temp->name = name;
1947 always_delete_queue = temp;
e9a25f70 1948
ed1f651b
RS
1949 already1:;
1950 }
1951
1952 if (fail_delete)
1953 {
b3694847 1954 struct temp_file *temp;
ed1f651b
RS
1955 for (temp = failure_delete_queue; temp; temp = temp->next)
1956 if (! strcmp (name, temp->name))
1957 goto already2;
e9a25f70 1958
5ed6ace5 1959 temp = XNEW (struct temp_file);
ed1f651b
RS
1960 temp->next = failure_delete_queue;
1961 temp->name = name;
1962 failure_delete_queue = temp;
e9a25f70 1963
ed1f651b
RS
1964 already2:;
1965 }
1966}
1967
1968/* Delete all the temporary files whose names we previously recorded. */
1969
8fd58397
DR
1970#ifndef DELETE_IF_ORDINARY
1971#define DELETE_IF_ORDINARY(NAME,ST,VERBOSE_FLAG) \
1972do \
1973 { \
1974 if (stat (NAME, &ST) >= 0 && S_ISREG (ST.st_mode)) \
1975 if (unlink (NAME) < 0) \
1976 if (VERBOSE_FLAG) \
1977 perror_with_name (NAME); \
1978 } while (0)
1979#endif
1980
d5ea2ac4 1981static void
1d088dee 1982delete_if_ordinary (const char *name)
d5ea2ac4
RK
1983{
1984 struct stat st;
1985#ifdef DEBUG
1986 int i, c;
1987
1988 printf ("Delete %s? (y or n) ", name);
1989 fflush (stdout);
1990 i = getchar ();
1991 if (i != '\n')
e9a25f70
JL
1992 while ((c = getchar ()) != '\n' && c != EOF)
1993 ;
1994
d5ea2ac4
RK
1995 if (i == 'y' || i == 'Y')
1996#endif /* DEBUG */
8fd58397 1997 DELETE_IF_ORDINARY (name, st, verbose_flag);
d5ea2ac4
RK
1998}
1999
ed1f651b 2000static void
1d088dee 2001delete_temp_files (void)
ed1f651b 2002{
b3694847 2003 struct temp_file *temp;
ed1f651b
RS
2004
2005 for (temp = always_delete_queue; temp; temp = temp->next)
d5ea2ac4 2006 delete_if_ordinary (temp->name);
ed1f651b
RS
2007 always_delete_queue = 0;
2008}
2009
2010/* Delete all the files to be deleted on error. */
2011
2012static void
1d088dee 2013delete_failure_queue (void)
ed1f651b 2014{
b3694847 2015 struct temp_file *temp;
ed1f651b
RS
2016
2017 for (temp = failure_delete_queue; temp; temp = temp->next)
d5ea2ac4 2018 delete_if_ordinary (temp->name);
ed1f651b
RS
2019}
2020
2021static void
1d088dee 2022clear_failure_queue (void)
ed1f651b
RS
2023{
2024 failure_delete_queue = 0;
2025}
b3865ca9 2026\f
00dcee0c
AM
2027/* Call CALLBACK for each path in PATHS, breaking out early if CALLBACK
2028 returns non-NULL.
2029 If DO_MULTI is true iterate over the paths twice, first with multilib
2030 suffix then without, otherwise iterate over the paths once without
2031 adding a multilib suffix. When DO_MULTI is true, some attempt is made
2032 to avoid visiting the same path twice, but we could do better. For
2033 instance, /usr/lib/../lib is considered different from /usr/lib.
2034 At least EXTRA_SPACE chars past the end of the path passed to
2035 CALLBACK are available for use by the callback.
2036 CALLBACK_INFO allows extra parameters to be passed to CALLBACK.
2037
2038 Returns the value returned by CALLBACK. */
2039
2040static void *
2041for_each_path (const struct path_prefix *paths,
2042 bool do_multi,
2043 size_t extra_space,
2044 void *(*callback) (char *, void *),
2045 void *callback_info)
b3865ca9 2046{
00dcee0c
AM
2047 struct prefix_list *pl;
2048 const char *multi_dir = NULL;
2049 const char *multi_os_dir = NULL;
2050 const char *multi_suffix;
2051 const char *just_multi_suffix;
2052 char *path = NULL;
2053 void *ret = NULL;
2054 bool skip_multi_dir = false;
2055 bool skip_multi_os_dir = false;
2056
2057 multi_suffix = machine_suffix;
2058 just_multi_suffix = just_machine_suffix;
2059 if (do_multi && multilib_dir && strcmp (multilib_dir, ".") != 0)
b3865ca9 2060 {
00dcee0c
AM
2061 multi_dir = concat (multilib_dir, dir_separator_str, NULL);
2062 multi_suffix = concat (multi_suffix, multi_dir, NULL);
2063 just_multi_suffix = concat (just_multi_suffix, multi_dir, NULL);
2064 }
2065 if (do_multi && multilib_os_dir && strcmp (multilib_os_dir, ".") != 0)
2066 multi_os_dir = concat (multilib_os_dir, dir_separator_str, NULL);
b3865ca9 2067
00dcee0c
AM
2068 while (1)
2069 {
2070 size_t multi_dir_len = 0;
2071 size_t multi_os_dir_len = 0;
2072 size_t suffix_len;
2073 size_t just_suffix_len;
2074 size_t len;
2075
2076 if (multi_dir)
2077 multi_dir_len = strlen (multi_dir);
2078 if (multi_os_dir)
2079 multi_os_dir_len = strlen (multi_os_dir);
2080 suffix_len = strlen (multi_suffix);
2081 just_suffix_len = strlen (just_multi_suffix);
2082
2083 if (path == NULL)
b3865ca9 2084 {
00dcee0c
AM
2085 len = paths->max_len + extra_space + 1;
2086 if (suffix_len > multi_os_dir_len)
2087 len += suffix_len;
2088 else
2089 len += multi_os_dir_len;
5ed6ace5 2090 path = XNEWVEC (char, len);
b3865ca9
RS
2091 }
2092
00dcee0c 2093 for (pl = paths->plist; pl != 0; pl = pl->next)
ae04227b 2094 {
00dcee0c
AM
2095 len = strlen (pl->prefix);
2096 memcpy (path, pl->prefix, len);
9218435e 2097
00dcee0c
AM
2098 /* Look first in MACHINE/VERSION subdirectory. */
2099 if (!skip_multi_dir)
2100 {
2101 memcpy (path + len, multi_suffix, suffix_len + 1);
2102 ret = callback (path, callback_info);
2103 if (ret)
2104 break;
2105 }
2106
2107 /* Some paths are tried with just the machine (ie. target)
2108 subdir. This is used for finding as, ld, etc. */
2109 if (!skip_multi_dir
2110 && pl->require_machine_suffix == 2)
2111 {
2112 memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
2113 ret = callback (path, callback_info);
2114 if (ret)
2115 break;
2116 }
2117
2118 /* Now try the base path. */
2119 if (!pl->require_machine_suffix
2120 && !(pl->os_multilib ? skip_multi_os_dir : skip_multi_dir))
2121 {
2122 const char *this_multi;
2123 size_t this_multi_len;
2124
2125 if (pl->os_multilib)
2126 {
2127 this_multi = multi_os_dir;
2128 this_multi_len = multi_os_dir_len;
2129 }
2130 else
2131 {
2132 this_multi = multi_dir;
2133 this_multi_len = multi_dir_len;
2134 }
2135
2136 if (this_multi_len)
2137 memcpy (path + len, this_multi, this_multi_len + 1);
2138 else
2139 path[len] = '\0';
2140
2141 ret = callback (path, callback_info);
2142 if (ret)
2143 break;
2144 }
ae04227b 2145 }
00dcee0c
AM
2146 if (pl)
2147 break;
ae04227b 2148
00dcee0c
AM
2149 if (multi_dir == NULL && multi_os_dir == NULL)
2150 break;
b3865ca9 2151
00dcee0c
AM
2152 /* Run through the paths again, this time without multilibs.
2153 Don't repeat any we have already seen. */
2154 if (multi_dir)
2155 {
b1d5455a 2156 free (CONST_CAST (char *, multi_dir));
00dcee0c 2157 multi_dir = NULL;
b1d5455a 2158 free (CONST_CAST (char *, multi_suffix));
00dcee0c 2159 multi_suffix = machine_suffix;
b1d5455a 2160 free (CONST_CAST (char *, just_multi_suffix));
00dcee0c 2161 just_multi_suffix = just_machine_suffix;
b3865ca9 2162 }
00dcee0c
AM
2163 else
2164 skip_multi_dir = true;
2165 if (multi_os_dir)
2166 {
b1d5455a 2167 free (CONST_CAST (char *, multi_os_dir));
00dcee0c
AM
2168 multi_os_dir = NULL;
2169 }
2170 else
2171 skip_multi_os_dir = true;
2172 }
2173
2174 if (multi_dir)
2175 {
b1d5455a
KG
2176 free (CONST_CAST (char *, multi_dir));
2177 free (CONST_CAST (char *, multi_suffix));
2178 free (CONST_CAST (char *, just_multi_suffix));
b3865ca9 2179 }
00dcee0c 2180 if (multi_os_dir)
b1d5455a 2181 free (CONST_CAST (char *, multi_os_dir));
00dcee0c
AM
2182 if (ret != path)
2183 free (path);
2184 return ret;
2185}
2186
2187/* Callback for build_search_list. Adds path to obstack being built. */
2188
2189struct add_to_obstack_info {
2190 struct obstack *ob;
2191 bool check_dir;
2192 bool first_time;
2193};
2194
2195static void *
2196add_to_obstack (char *path, void *data)
2197{
e1e4cdc4 2198 struct add_to_obstack_info *info = (struct add_to_obstack_info *) data;
00dcee0c
AM
2199
2200 if (info->check_dir && !is_directory (path, false))
2201 return NULL;
2202
2203 if (!info->first_time)
2204 obstack_1grow (info->ob, PATH_SEPARATOR);
2205
2206 obstack_grow (info->ob, path, strlen (path));
2207
2208 info->first_time = false;
2209 return NULL;
2210}
2211
47d33318
FXC
2212/* Add or change the value of an environment variable, outputting the
2213 change to standard error if in verbose mode. */
2214static void
e9c15f6e 2215xputenv (const char *string)
47d33318
FXC
2216{
2217 if (verbose_flag)
2dec80c7 2218 fnotice (stderr, "%s\n", string);
e9c15f6e 2219 putenv (CONST_CAST (char *, string));
47d33318
FXC
2220}
2221
00dcee0c
AM
2222/* Build a list of search directories from PATHS.
2223 PREFIX is a string to prepend to the list.
2224 If CHECK_DIR_P is true we ensure the directory exists.
2225 If DO_MULTI is true, multilib paths are output first, then
2226 non-multilib paths.
2227 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2228 It is also used by the --print-search-dirs flag. */
2229
2230static char *
2231build_search_list (const struct path_prefix *paths, const char *prefix,
2232 bool check_dir, bool do_multi)
2233{
2234 struct add_to_obstack_info info;
2235
2236 info.ob = &collect_obstack;
2237 info.check_dir = check_dir;
2238 info.first_time = true;
2239
2240 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2241 obstack_1grow (&collect_obstack, '=');
2242
2243 for_each_path (paths, do_multi, 0, add_to_obstack, &info);
e9a25f70 2244
3ae7de4e 2245 obstack_1grow (&collect_obstack, '\0');
7973fd2a 2246 return XOBFINISH (&collect_obstack, char *);
b3865ca9
RS
2247}
2248
0f41302f
MS
2249/* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2250 for collect. */
2628b9d3
DE
2251
2252static void
00dcee0c
AM
2253putenv_from_prefixes (const struct path_prefix *paths, const char *env_var,
2254 bool do_multi)
2628b9d3 2255{
47d33318 2256 xputenv (build_search_list (paths, env_var, true, do_multi));
2628b9d3 2257}
ed1f651b 2258\f
ca606201
ILT
2259/* Check whether NAME can be accessed in MODE. This is like access,
2260 except that it never considers directories to be executable. */
2261
2262static int
1d088dee 2263access_check (const char *name, int mode)
ca606201
ILT
2264{
2265 if (mode == X_OK)
2266 {
2267 struct stat st;
2268
2269 if (stat (name, &st) < 0
2270 || S_ISDIR (st.st_mode))
2271 return -1;
2272 }
2273
2274 return access (name, mode);
2275}
2276
00dcee0c
AM
2277/* Callback for find_a_file. Appends the file name to the directory
2278 path. If the resulting file exists in the right mode, return the
2279 full pathname to the file. */
2280
2281struct file_at_path_info {
2282 const char *name;
2283 const char *suffix;
2284 int name_len;
2285 int suffix_len;
2286 int mode;
2287};
2288
2289static void *
2290file_at_path (char *path, void *data)
2291{
e1e4cdc4 2292 struct file_at_path_info *info = (struct file_at_path_info *) data;
00dcee0c
AM
2293 size_t len = strlen (path);
2294
2295 memcpy (path + len, info->name, info->name_len);
2296 len += info->name_len;
2297
2298 /* Some systems have a suffix for executable files.
2299 So try appending that first. */
2300 if (info->suffix_len)
2301 {
2302 memcpy (path + len, info->suffix, info->suffix_len + 1);
2303 if (access_check (path, info->mode) == 0)
2304 return path;
2305 }
2306
2307 path[len] = '\0';
2308 if (access_check (path, info->mode) == 0)
2309 return path;
2310
2311 return NULL;
2312}
2313
ed1f651b 2314/* Search for NAME using the prefix list PREFIXES. MODE is passed to
00dcee0c
AM
2315 access to check permissions. If DO_MULTI is true, search multilib
2316 paths then non-multilib paths, otherwise do not search multilib paths.
0f41302f 2317 Return 0 if not found, otherwise return its name, allocated with malloc. */
ed1f651b
RS
2318
2319static char *
00dcee0c
AM
2320find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
2321 bool do_multi)
ed1f651b 2322{
00dcee0c 2323 struct file_at_path_info info;
ed1f651b 2324
ab339d62 2325#ifdef DEFAULT_ASSEMBLER
c5c0b3d9 2326 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
ad85216e 2327 return xstrdup (DEFAULT_ASSEMBLER);
ab339d62
AO
2328#endif
2329
2330#ifdef DEFAULT_LINKER
ad85216e
KG
2331 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2332 return xstrdup (DEFAULT_LINKER);
ab339d62
AO
2333#endif
2334
ed1f651b
RS
2335 /* Determine the filename to execute (special case for absolute paths). */
2336
3dce1408 2337 if (IS_ABSOLUTE_PATH (name))
ed1f651b 2338 {
ab339d62 2339 if (access (name, mode) == 0)
00dcee0c 2340 return xstrdup (name);
460dcab4 2341
00dcee0c
AM
2342 return NULL;
2343 }
460dcab4 2344
00dcee0c
AM
2345 info.name = name;
2346 info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
2347 info.name_len = strlen (info.name);
2348 info.suffix_len = strlen (info.suffix);
2349 info.mode = mode;
ed1f651b 2350
e1e4cdc4
KG
2351 return (char*) for_each_path (pprefix, do_multi,
2352 info.name_len + info.suffix_len,
2353 file_at_path, &info);
ed1f651b
RS
2354}
2355
922a4beb 2356/* Ranking of prefixes in the sort list. -B prefixes are put before
9218435e 2357 all others. */
922a4beb
AC
2358
2359enum path_prefix_priority
2360{
2361 PREFIX_PRIORITY_B_OPT,
2362 PREFIX_PRIORITY_LAST
2363};
2364
fbe5a4a6 2365/* Add an entry for PREFIX in PLIST. The PLIST is kept in ascending
922a4beb
AC
2366 order according to PRIORITY. Within each PRIORITY, new entries are
2367 appended.
ed1f651b
RS
2368
2369 If WARN is nonzero, we will warn if no file is found
2370 through this prefix. WARN should point to an int
ae04227b
CH
2371 which will be set to 1 if this entry is used.
2372
e9a25f70
JL
2373 COMPONENT is the value to be passed to update_path.
2374
ae04227b
CH
2375 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2376 the complete value of machine_suffix.
2377 2 means try both machine_suffix and just_machine_suffix. */
ed1f651b
RS
2378
2379static void
1d088dee
AJ
2380add_prefix (struct path_prefix *pprefix, const char *prefix,
2381 const char *component, /* enum prefix_priority */ int priority,
1a5d37a1 2382 int require_machine_suffix, int os_multilib)
ed1f651b
RS
2383{
2384 struct prefix_list *pl, **prev;
2385 int len;
2386
922a4beb
AC
2387 for (prev = &pprefix->plist;
2388 (*prev) != NULL && (*prev)->priority <= priority;
2389 prev = &(*prev)->next)
2390 ;
ed1f651b 2391
f9da5064 2392 /* Keep track of the longest prefix. */
ed1f651b 2393
e9a25f70 2394 prefix = update_path (prefix, component);
ed1f651b
RS
2395 len = strlen (prefix);
2396 if (len > pprefix->max_len)
2397 pprefix->max_len = len;
2398
5ed6ace5 2399 pl = XNEW (struct prefix_list);
51c04256 2400 pl->prefix = prefix;
ed1f651b 2401 pl->require_machine_suffix = require_machine_suffix;
922a4beb 2402 pl->priority = priority;
5bbcd587 2403 pl->os_multilib = os_multilib;
ed1f651b 2404
f9da5064 2405 /* Insert after PREV. */
922a4beb
AC
2406 pl->next = (*prev);
2407 (*prev) = pl;
ed1f651b 2408}
4977bab6
ZW
2409
2410/* Same as add_prefix, but prepending target_system_root to prefix. */
f4c0a303 2411/* The target_system_root prefix has been relocated by gcc_exec_prefix. */
4977bab6 2412static void
1d088dee
AJ
2413add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
2414 const char *component,
2415 /* enum prefix_priority */ int priority,
1a5d37a1 2416 int require_machine_suffix, int os_multilib)
4977bab6 2417{
3dce1408 2418 if (!IS_ABSOLUTE_PATH (prefix))
bdc6b402 2419 fatal_error ("system path %qs is not absolute", prefix);
4977bab6
ZW
2420
2421 if (target_system_root)
2422 {
e7f13528
GP
2423 if (target_sysroot_suffix)
2424 prefix = concat (target_sysroot_suffix, prefix, NULL);
4977bab6 2425 prefix = concat (target_system_root, prefix, NULL);
e7f13528 2426
4977bab6
ZW
2427 /* We have to override this because GCC's notion of sysroot
2428 moves along with GCC. */
2429 component = "GCC";
2430 }
2431
2432 add_prefix (pprefix, prefix, component, priority,
1a5d37a1 2433 require_machine_suffix, os_multilib);
4977bab6 2434}
ed1f651b
RS
2435\f
2436/* Execute the command specified by the arguments on the current line of spec.
2437 When using pipes, this includes several piped-together commands
2438 with `|' between them.
2439
2440 Return 0 if successful, -1 if failed. */
2441
2442static int
1d088dee 2443execute (void)
ed1f651b
RS
2444{
2445 int i;
2446 int n_commands; /* # of command. */
2447 char *string;
054e88a8 2448 struct pex_obj *pex;
ed1f651b 2449 struct command
d25a45d4
KH
2450 {
2451 const char *prog; /* program name. */
2452 const char **argv; /* vector of args. */
d25a45d4 2453 };
5b634ee0 2454 const char *arg;
ed1f651b
RS
2455
2456 struct command *commands; /* each command buffer with above info. */
2457
3b5edfee 2458 gcc_assert (!processing_spec_function);
f3226a90 2459
fe7df9c4
SP
2460 if (wrapper_string)
2461 {
5b634ee0
JM
2462 string = find_a_file (&exec_prefixes,
2463 VEC_index (const_char_p, argbuf, 0), X_OK, false);
2464 if (string)
2465 VEC_replace (const_char_p, argbuf, 0, string);
fe7df9c4
SP
2466 insert_wrapper (wrapper_string);
2467 }
2468
ed1f651b 2469 /* Count # of piped commands. */
5b634ee0
JM
2470 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2471 if (strcmp (arg, "|") == 0)
ed1f651b
RS
2472 n_commands++;
2473
2474 /* Get storage for each command. */
e1e4cdc4 2475 commands = (struct command *) alloca (n_commands * sizeof (struct command));
ed1f651b
RS
2476
2477 /* Split argbuf into its separate piped processes,
2478 and record info about each one.
2479 Also search for the programs that are to be run. */
2480
5b634ee0
JM
2481 VEC_safe_push (const_char_p, heap, argbuf, 0);
2482
2483 commands[0].prog = VEC_index (const_char_p, argbuf, 0); /* first command. */
2484 commands[0].argv = VEC_address (const_char_p, argbuf);
b8698a0f 2485
fe7df9c4
SP
2486 if (!wrapper_string)
2487 {
2488 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK, false);
2489 commands[0].argv[0] = (string) ? string : commands[0].argv[0];
2490 }
ed1f651b 2491
5b634ee0
JM
2492 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2493 if (arg && strcmp (arg, "|") == 0)
ed1f651b 2494 { /* each command. */
6405c0ec 2495#if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2dec80c7 2496 fatal_error ("-pipe not supported");
ed1f651b 2497#endif
5b634ee0
JM
2498 VEC_replace (const_char_p, argbuf, i, 0); /* Termination of
2499 command args. */
2500 commands[n_commands].prog = VEC_index (const_char_p, argbuf, i + 1);
2501 commands[n_commands].argv
2502 = &(VEC_address (const_char_p, argbuf))[i + 1];
5bbcd587 2503 string = find_a_file (&exec_prefixes, commands[n_commands].prog,
00dcee0c 2504 X_OK, false);
ed1f651b
RS
2505 if (string)
2506 commands[n_commands].argv[0] = string;
2507 n_commands++;
2508 }
2509
ed1f651b
RS
2510 /* If -v, print what we are about to do, and maybe query. */
2511
b3865ca9 2512 if (verbose_flag)
ed1f651b 2513 {
b8468bc7
NC
2514 /* For help listings, put a blank line between sub-processes. */
2515 if (print_help_list)
2516 fputc ('\n', stderr);
9218435e 2517
ed1f651b 2518 /* Print each piped command as a separate line. */
d25a45d4 2519 for (i = 0; i < n_commands; i++)
ed1f651b 2520 {
37620334 2521 const char *const *j;
ed1f651b 2522
589005ff
KH
2523 if (verbose_only_flag)
2524 {
99f78cdd
IR
2525 for (j = commands[i].argv; *j; j++)
2526 {
88f92c0f 2527 const char *p;
99f78cdd 2528 for (p = *j; *p; ++p)
09b201fc
JJ
2529 if (!ISALNUM ((unsigned char) *p)
2530 && *p != '_' && *p != '/' && *p != '-' && *p != '.')
2531 break;
2532 if (*p || !*j)
99f78cdd 2533 {
09b201fc
JJ
2534 fprintf (stderr, " \"");
2535 for (p = *j; *p; ++p)
2536 {
2537 if (*p == '"' || *p == '\\' || *p == '$')
2538 fputc ('\\', stderr);
2539 fputc (*p, stderr);
2540 }
2541 fputc ('"', stderr);
99f78cdd 2542 }
09b201fc
JJ
2543 else
2544 fprintf (stderr, " %s", *j);
99f78cdd 2545 }
589005ff
KH
2546 }
2547 else
99f78cdd
IR
2548 for (j = commands[i].argv; *j; j++)
2549 fprintf (stderr, " %s", *j);
ed1f651b
RS
2550
2551 /* Print a pipe symbol after all but the last command. */
2552 if (i + 1 != n_commands)
2553 fprintf (stderr, " |");
2554 fprintf (stderr, "\n");
2555 }
2556 fflush (stderr);
99f78cdd 2557 if (verbose_only_flag != 0)
6a40fb21
AP
2558 {
2559 /* verbose_only_flag should act as if the spec was
2560 executed, so increment execution_count before
ba228239 2561 returning. This prevents spurious warnings about
6a40fb21
AP
2562 unused linker input files, etc. */
2563 execution_count++;
2564 return 0;
2565 }
ed1f651b 2566#ifdef DEBUG
2dec80c7 2567 fnotice (stderr, "\nGo ahead? (y or n) ");
ed1f651b
RS
2568 fflush (stderr);
2569 i = getchar ();
2570 if (i != '\n')
e9a25f70
JL
2571 while (getchar () != '\n')
2572 ;
2573
ed1f651b
RS
2574 if (i != 'y' && i != 'Y')
2575 return 0;
2576#endif /* DEBUG */
2577 }
2578
414d23ae 2579#ifdef ENABLE_VALGRIND_CHECKING
fbe5a4a6 2580 /* Run the each command through valgrind. To simplify prepending the
414d23ae
HPN
2581 path to valgrind and the option "-q" (for quiet operation unless
2582 something triggers), we allocate a separate argv array. */
2583
2584 for (i = 0; i < n_commands; i++)
2585 {
2586 const char **argv;
2587 int argc;
2588 int j;
2589
2590 for (argc = 0; commands[i].argv[argc] != NULL; argc++)
2591 ;
2592
63ab5b8c 2593 argv = XALLOCAVEC (const char *, argc + 3);
414d23ae
HPN
2594
2595 argv[0] = VALGRIND_PATH;
2596 argv[1] = "-q";
2597 for (j = 2; j < argc + 2; j++)
2598 argv[j] = commands[i].argv[j - 2];
2599 argv[j] = NULL;
2600
2601 commands[i].argv = argv;
2602 commands[i].prog = argv[0];
2603 }
2604#endif
2605
ed1f651b
RS
2606 /* Run each piped subprocess. */
2607
bdde878c
AO
2608 pex = pex_init (PEX_USE_PIPES | ((report_times || report_times_to_file)
2609 ? PEX_RECORD_TIMES : 0),
6afbc885 2610 progname, temp_filename);
054e88a8 2611 if (pex == NULL)
bdc6b402 2612 fatal_error ("pex_init failed: %m");
054e88a8 2613
ed1f651b
RS
2614 for (i = 0; i < n_commands; i++)
2615 {
054e88a8
ILT
2616 const char *errmsg;
2617 int err;
fbd40359 2618 const char *string = commands[i].argv[0];
ed1f651b 2619
054e88a8
ILT
2620 errmsg = pex_run (pex,
2621 ((i + 1 == n_commands ? PEX_LAST : 0)
2622 | (string == commands[i].prog ? PEX_SEARCH : 0)),
b1d5455a 2623 string, CONST_CAST (char **, commands[i].argv),
054e88a8
ILT
2624 NULL, NULL, &err);
2625 if (errmsg != NULL)
2626 {
2627 if (err == 0)
2dec80c7 2628 fatal_error (errmsg);
054e88a8
ILT
2629 else
2630 {
2631 errno = err;
2632 pfatal_with_name (errmsg);
2633 }
2634 }
ed1f651b
RS
2635
2636 if (string != commands[i].prog)
b1d5455a 2637 free (CONST_CAST (char *, string));
ed1f651b
RS
2638 }
2639
2640 execution_count++;
2641
054e88a8 2642 /* Wait for all the subprocesses to finish. */
ed1f651b
RS
2643
2644 {
054e88a8
ILT
2645 int *statuses;
2646 struct pex_time *times = NULL;
ed1f651b
RS
2647 int ret_code = 0;
2648
e1e4cdc4 2649 statuses = (int *) alloca (n_commands * sizeof (int));
054e88a8 2650 if (!pex_get_status (pex, n_commands, statuses))
bdc6b402 2651 fatal_error ("failed to get exit status: %m");
054e88a8 2652
bdde878c 2653 if (report_times || report_times_to_file)
ed1f651b 2654 {
e1e4cdc4 2655 times = (struct pex_time *) alloca (n_commands * sizeof (struct pex_time));
054e88a8 2656 if (!pex_get_times (pex, n_commands, times))
bdc6b402 2657 fatal_error ("failed to get process times: %m");
054e88a8 2658 }
ed1f651b 2659
054e88a8 2660 pex_free (pex);
ed1f651b 2661
054e88a8
ILT
2662 for (i = 0; i < n_commands; ++i)
2663 {
2664 int status = statuses[i];
03c41c05 2665
054e88a8
ILT
2666 if (WIFSIGNALED (status))
2667 {
c334349b 2668#ifdef SIGPIPE
054e88a8
ILT
2669 /* SIGPIPE is a special case. It happens in -pipe mode
2670 when the compiler dies before the preprocessor is done,
2671 or the assembler dies before the compiler is done.
2672 There's generally been an error already, and this is
2673 just fallout. So don't generate another error unless
2674 we would otherwise have succeeded. */
2675 if (WTERMSIG (status) == SIGPIPE
2676 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
eeac616e
VR
2677 {
2678 signal_count++;
2679 ret_code = -1;
2680 }
054e88a8
ILT
2681 else
2682#endif
6afbc885
JM
2683 internal_error ("%s (program %s)",
2684 strsignal (WTERMSIG (status)), commands[i].prog);
054e88a8
ILT
2685 }
2686 else if (WIFEXITED (status)
2687 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2688 {
2689 if (WEXITSTATUS (status) > greatest_status)
2690 greatest_status = WEXITSTATUS (status);
2691 ret_code = -1;
2692 }
2693
bdde878c 2694 if (report_times || report_times_to_file)
054e88a8
ILT
2695 {
2696 struct pex_time *pt = &times[i];
2697 double ut, st;
2698
2699 ut = ((double) pt->user_seconds
2700 + (double) pt->user_microseconds / 1.0e6);
2701 st = ((double) pt->system_seconds
2702 + (double) pt->system_microseconds / 1.0e6);
2703
2704 if (ut + st != 0)
bdde878c
AO
2705 {
2706 if (report_times)
2dec80c7
JM
2707 fnotice (stderr, "# %s %.2f %.2f\n",
2708 commands[i].prog, ut, st);
bdde878c
AO
2709
2710 if (report_times_to_file)
2711 {
2712 int c = 0;
2713 const char *const *j;
2714
2715 fprintf (report_times_to_file, "%g %g", ut, st);
2716
2717 for (j = &commands[i].prog; *j; j = &commands[i].argv[++c])
2718 {
2719 const char *p;
2720 for (p = *j; *p; ++p)
2721 if (*p == '"' || *p == '\\' || *p == '$'
2722 || ISSPACE (*p))
2723 break;
2724
2725 if (*p)
2726 {
2727 fprintf (report_times_to_file, " \"");
2728 for (p = *j; *p; ++p)
2729 {
2730 if (*p == '"' || *p == '\\' || *p == '$')
2731 fputc ('\\', report_times_to_file);
2732 fputc (*p, report_times_to_file);
2733 }
2734 fputc ('"', report_times_to_file);
2735 }
2736 else
2737 fprintf (report_times_to_file, " %s", *j);
2738 }
2739
2740 fputc ('\n', report_times_to_file);
2741 }
2742 }
054e88a8 2743 }
ed1f651b 2744 }
054e88a8 2745
ed1f651b
RS
2746 return ret_code;
2747 }
2748}
2749\f
2750/* Find all the switches given to us
2751 and make a vector describing them.
2752 The elements of the vector are strings, one per switch given.
2753 If a switch uses following arguments, then the `part1' field
2754 is the switch itself and the `args' field
2755 is a null-terminated vector containing the following arguments.
3371362c
L
2756 Bits in the `live_cond' field are:
2757 SWITCH_LIVE to indicate this switch is true in a conditional spec.
2758 SWITCH_FALSE to indicate this switch is overridden by a later switch.
2759 SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
94b90527
JJ
2760 SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored
2761 in all do_spec calls afterwards. Used for %<S from self specs.
ab87f8c8 2762 The `validated' field is nonzero if any spec has looked at this switch;
ed1f651b
RS
2763 if it remains zero at the end of the run, it must be meaningless. */
2764
94b90527
JJ
2765#define SWITCH_LIVE 0x1
2766#define SWITCH_FALSE 0x2
2767#define SWITCH_IGNORE 0x4
2768#define SWITCH_IGNORE_PERMANENTLY 0x8
8097c429 2769
ed1f651b
RS
2770struct switchstr
2771{
878f32c3 2772 const char *part1;
fbd40359 2773 const char **args;
3371362c 2774 unsigned int live_cond;
196a37f4
NB
2775 unsigned char validated;
2776 unsigned char ordering;
ed1f651b
RS
2777};
2778
2779static struct switchstr *switches;
2780
2781static int n_switches;
2782
922e1882
JM
2783static int n_switches_alloc;
2784
2153915d
AO
2785/* Set to zero if -fcompare-debug is disabled, positive if it's
2786 enabled and we're running the first compilation, negative if it's
2787 enabled and we're running the second compilation. For most of the
2788 time, it's in the range -1..1, but it can be temporarily set to 2
2789 or 3 to indicate that the -fcompare-debug flags didn't come from
2790 the command-line, but rather from the GCC_COMPARE_DEBUG environment
2791 variable, until a synthesized -fcompare-debug flag is added to the
2792 command line. */
2793int compare_debug;
2794
2795/* Set to nonzero if we've seen the -fcompare-debug-second flag. */
2796int compare_debug_second;
2797
2798/* Set to the flags that should be passed to the second compilation in
2799 a -fcompare-debug compilation. */
2800const char *compare_debug_opt;
2801
2802static struct switchstr *switches_debug_check[2];
2803
2804static int n_switches_debug_check[2];
2805
efe5e5a0
JM
2806static int n_switches_alloc_debug_check[2];
2807
2153915d
AO
2808static char *debug_check_temp_file[2];
2809
817a8255
EC
2810/* Language is one of three things:
2811
2812 1) The name of a real programming language.
2813 2) NULL, indicating that no one has figured out
2814 what it is yet.
2815 3) '*', indicating that the file should be passed
2816 to the linker. */
ed1f651b
RS
2817struct infile
2818{
878f32c3
KG
2819 const char *name;
2820 const char *language;
0855eab7
CT
2821 struct compiler *incompiler;
2822 bool compiled;
2823 bool preprocessed;
ed1f651b
RS
2824};
2825
2826/* Also a vector of input files specified. */
2827
2828static struct infile *infiles;
2829
3a5a9edc 2830int n_infiles;
ed1f651b 2831
922e1882
JM
2832static int n_infiles_alloc;
2833
d1bd0ded
GK
2834/* True if multiple input files are being compiled to a single
2835 assembly file. */
2836
2837static bool combine_inputs;
2838
08dc830e 2839/* This counts the number of libraries added by lang_specific_driver, so that
a2a05b0a
JW
2840 we can tell if there were any user supplied any files or libraries. */
2841
2842static int added_libraries;
2843
ed1f651b
RS
2844/* And a vector of corresponding output files is made up later. */
2845
3a5a9edc 2846const char **outfiles;
853e0b2d 2847\f
45936a85 2848#if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d
RK
2849
2850/* Convert NAME to a new name if it is the standard suffix. DO_EXE
a9657ce8
DR
2851 is true if we should look for an executable suffix. DO_OBJ
2852 is true if we should look for an object suffix. */
853e0b2d 2853
40cdfca6 2854static const char *
1d088dee
AJ
2855convert_filename (const char *name, int do_exe ATTRIBUTE_UNUSED,
2856 int do_obj ATTRIBUTE_UNUSED)
853e0b2d 2857{
40cdfca6 2858#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d 2859 int i;
40cdfca6 2860#endif
87e690e2
MK
2861 int len;
2862
2863 if (name == NULL)
2864 return NULL;
9218435e 2865
87e690e2 2866 len = strlen (name);
853e0b2d 2867
45936a85
DD
2868#ifdef HAVE_TARGET_OBJECT_SUFFIX
2869 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
a9657ce8 2870 if (do_obj && len > 2
853e0b2d
RK
2871 && name[len - 2] == '.'
2872 && name[len - 1] == 'o')
2873 {
bdc5ed93 2874 obstack_grow (&obstack, name, len - 2);
45936a85 2875 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
7973fd2a 2876 name = XOBFINISH (&obstack, const char *);
853e0b2d
RK
2877 }
2878#endif
2879
45936a85 2880#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
853e0b2d
RK
2881 /* If there is no filetype, make it the executable suffix (which includes
2882 the "."). But don't get confused if we have just "-o". */
45936a85 2883 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
853e0b2d
RK
2884 return name;
2885
e0040a8e 2886 for (i = len - 1; i >= 0; i--)
509781a4 2887 if (IS_DIR_SEPARATOR (name[i]))
e0040a8e
RK
2888 break;
2889
2890 for (i++; i < len; i++)
853e0b2d
RK
2891 if (name[i] == '.')
2892 return name;
2893
2894 obstack_grow (&obstack, name, len);
5d9669fd
RK
2895 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
2896 strlen (TARGET_EXECUTABLE_SUFFIX));
7973fd2a 2897 name = XOBFINISH (&obstack, const char *);
853e0b2d
RK
2898#endif
2899
2900 return name;
2901}
2902#endif
b8468bc7
NC
2903\f
2904/* Display the command line switches accepted by gcc. */
2905static void
1d088dee 2906display_help (void)
b8468bc7 2907{
6afbc885 2908 printf (_("Usage: %s [options] file...\n"), progname);
5e4adfba 2909 fputs (_("Options:\n"), stdout);
b8468bc7 2910
5e4adfba
PT
2911 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2912 fputs (_(" --help Display this information\n"), stdout);
91606ce2 2913 fputs (_(" --target-help Display target specific command line options\n"), stdout);
0631b69f 2914 fputs (_(" --help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]\n"), stdout);
c662432e 2915 fputs (_(" Display specific types of command line options\n"), stdout);
b8468bc7 2916 if (! verbose_flag)
5e4adfba 2917 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
1156c176 2918 fputs (_(" --version Display compiler version information\n"), stdout);
5e4adfba
PT
2919 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2920 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2921 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2922 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2923 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2924 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2925 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2926 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2927 fputs (_("\
2928 -print-multi-lib Display the mapping between command line options and\n\
2929 multiple library search directories\n"), stdout);
5bbcd587 2930 fputs (_(" -print-multi-os-directory Display the relative path to OS libraries\n"), stdout);
3def1397 2931 fputs (_(" -print-sysroot Display the target libraries directory\n"), stdout);
14da6073 2932 fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers\n"), stdout);
5e4adfba
PT
2933 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2934 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2935 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
4977bab6
ZW
2936 fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler\n"), stdout);
2937 fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor\n"), stdout);
5e4adfba
PT
2938 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2939 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
14fdc613 2940 fputs (_(" -save-temps=<arg> Do not delete intermediate files\n"), stdout);
ba0c638e
SB
2941 fputs (_("\
2942 -no-canonical-prefixes Do not canonicalize paths when building relative\n\
2943 prefixes to other gcc components\n"), stdout);
5e4adfba
PT
2944 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2945 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
b0287a90 2946 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
5e4adfba 2947 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
160633c6
MM
2948 fputs (_("\
2949 --sysroot=<directory> Use <directory> as the root directory for headers\n\
b3cccd58 2950 and libraries\n"), stdout);
5e4adfba 2951 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
5e4adfba 2952 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
99f78cdd 2953 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
5e4adfba
PT
2954 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2955 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2956 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2957 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2958 fputs (_("\
2959 -x <language> Specify the language of the following input files\n\
8e854b76 2960 Permissible languages include: c c++ assembler none\n\
1737c953 2961 'none' means revert to the default behavior of\n\
5e4adfba
PT
2962 guessing the language based on the file's extension\n\
2963"), stdout);
2964
0c386769 2965 printf (_("\
d991c721
BK
2966\nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
2967 passed on to the various sub-processes invoked by %s. In order to pass\n\
2968 other options on to these processes the -W<letter> options must be used.\n\
6afbc885 2969"), progname);
b8468bc7
NC
2970
2971 /* The rest of the options are displayed by invocations of the various
2972 sub-processes. */
2973}
2974
9218435e 2975static void
1d088dee 2976add_preprocessor_option (const char *option, int len)
9218435e 2977{
878f32c3 2978 n_preprocessor_options++;
9218435e 2979
878f32c3 2980 if (! preprocessor_options)
5ed6ace5 2981 preprocessor_options = XNEWVEC (char *, n_preprocessor_options);
878f32c3 2982 else
e1e4cdc4
KG
2983 preprocessor_options = XRESIZEVEC (char *, preprocessor_options,
2984 n_preprocessor_options);
9218435e 2985
878f32c3
KG
2986 preprocessor_options [n_preprocessor_options - 1] =
2987 save_string (option, len);
40f943dd 2988}
9218435e
KH
2989
2990static void
1d088dee 2991add_assembler_option (const char *option, int len)
878f32c3
KG
2992{
2993 n_assembler_options++;
2994
2995 if (! assembler_options)
5ed6ace5 2996 assembler_options = XNEWVEC (char *, n_assembler_options);
878f32c3 2997 else
e1e4cdc4
KG
2998 assembler_options = XRESIZEVEC (char *, assembler_options,
2999 n_assembler_options);
878f32c3
KG
3000
3001 assembler_options [n_assembler_options - 1] = save_string (option, len);
b8468bc7 3002}
9218435e
KH
3003
3004static void
1d088dee 3005add_linker_option (const char *option, int len)
878f32c3
KG
3006{
3007 n_linker_options++;
3008
3009 if (! linker_options)
5ed6ace5 3010 linker_options = XNEWVEC (char *, n_linker_options);
878f32c3 3011 else
3beb864c 3012 linker_options = XRESIZEVEC (char *, linker_options, n_linker_options);
878f32c3
KG
3013
3014 linker_options [n_linker_options - 1] = save_string (option, len);
40f943dd 3015}
853e0b2d 3016\f
922e1882
JM
3017/* Allocate space for an input file in infiles. */
3018
3019static void
3020alloc_infile (void)
3021{
3022 if (n_infiles_alloc == 0)
3023 {
3024 n_infiles_alloc = 16;
3025 infiles = XNEWVEC (struct infile, n_infiles_alloc);
3026 }
3027 else if (n_infiles_alloc == n_infiles)
3028 {
3029 n_infiles_alloc *= 2;
3030 infiles = XRESIZEVEC (struct infile, infiles, n_infiles_alloc);
3031 }
3032}
3033
3034/* Store an input file with the given NAME and LANGUAGE in
3035 infiles. */
3036
3037static void
3038add_infile (const char *name, const char *language)
3039{
3040 alloc_infile ();
3041 infiles[n_infiles].name = name;
3042 infiles[n_infiles++].language = language;
3043}
3044
3045/* Allocate space for a switch in switches. */
3046
3047static void
3048alloc_switch (void)
3049{
3050 if (n_switches_alloc == 0)
3051 {
3052 n_switches_alloc = 16;
3053 switches = XNEWVEC (struct switchstr, n_switches_alloc);
3054 }
3055 else if (n_switches_alloc == n_switches)
3056 {
3057 n_switches_alloc *= 2;
3058 switches = XRESIZEVEC (struct switchstr, switches, n_switches_alloc);
3059 }
3060}
3061
603349bf
JM
3062/* Save an option OPT with N_ARGS arguments in array ARGS, marking it
3063 as validated if VALIDATED. */
3064
3065static void
3066save_switch (const char *opt, size_t n_args, const char *const *args,
3067 bool validated)
3068{
3069 alloc_switch ();
3070 switches[n_switches].part1 = opt + 1;
3071 if (n_args == 0)
3072 switches[n_switches].args = 0;
3073 else
3074 {
3075 switches[n_switches].args = XNEWVEC (const char *, n_args + 1);
3076 memcpy (switches[n_switches].args, args, n_args * sizeof (const char *));
3077 switches[n_switches].args[n_args] = NULL;
3078 }
3079
3080 switches[n_switches].live_cond = 0;
3081 switches[n_switches].validated = validated;
3082 switches[n_switches].ordering = 0;
3083 n_switches++;
3084}
3085
3086/* Handle an option DECODED that is unknown to the option-processing
3087 machinery, but may be known to specs. */
3088
3089static bool
3090driver_unknown_option_callback (const struct cl_decoded_option *decoded)
3091{
3092 save_switch (decoded->canonical_option[0],
3093 decoded->canonical_option_num_elements - 1,
3094 &decoded->canonical_option[1], false);
3095
3096 return false;
3097}
3098
3099/* Handle an option DECODED that is not marked as CL_DRIVER.
3100 LANG_MASK will always be CL_DRIVER. */
3101
3102static void
3103driver_wrong_lang_callback (const struct cl_decoded_option *decoded,
3104 unsigned int lang_mask ATTRIBUTE_UNUSED)
3105{
3106 /* At this point, non-driver options are accepted (and expected to
3107 be passed down by specs) unless marked to be rejected by the
3108 driver. Options to be rejected by the driver but accepted by the
3109 compilers proper are treated just like completely unknown
3110 options. */
3111 const struct cl_option *option = &cl_options[decoded->opt_index];
3112
3113 if (option->flags & CL_REJECT_DRIVER)
3114 error ("unrecognized command line option %qs",
3115 decoded->orig_option_with_args_text);
3116 else
3117 driver_unknown_option_callback (decoded);
3118}
3119
3120/* Note that an option (index OPT_INDEX, argument ARG, value VALUE)
3121 has been successfully handled with a handler for mask MASK. */
3122
3123static void
3124driver_post_handling_callback (const struct cl_decoded_option *decoded ATTRIBUTE_UNUSED,
3125 unsigned int mask ATTRIBUTE_UNUSED)
3126{
3127 /* Nothing to do here. */
3128}
3129
3130static const char *spec_lang = 0;
3131static int last_language_n_infiles;
3132
3133/* Handle a driver option; arguments and return value as for
3134 handle_option. */
3135
3136static bool
46625112 3137driver_handle_option (struct gcc_options *opts,
d4d24ba4 3138 struct gcc_options *opts_set,
46625112 3139 const struct cl_decoded_option *decoded,
603349bf 3140 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
a4d8c676 3141 location_t loc,
d5478783
JM
3142 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
3143 diagnostic_context *dc)
603349bf
JM
3144{
3145 size_t opt_index = decoded->opt_index;
3146 const char *arg = decoded->arg;
3147 const char *compare_debug_replacement_opt;
3148 int value = decoded->value;
3149 bool validated = false;
3150 bool do_save = true;
3151
46625112 3152 gcc_assert (opts == &global_options);
d4d24ba4 3153 gcc_assert (opts_set == &global_options_set);
603349bf 3154 gcc_assert (kind == DK_UNSPECIFIED);
a4d8c676 3155 gcc_assert (loc == UNKNOWN_LOCATION);
d5478783 3156 gcc_assert (dc == global_dc);
603349bf
JM
3157
3158 switch (opt_index)
3159 {
3160 case OPT_dumpspecs:
3161 {
3162 struct spec_list *sl;
3163 init_spec ();
3164 for (sl = specs; sl; sl = sl->next)
3165 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3166 if (link_command_spec)
3167 printf ("*link_command:\n%s\n\n", link_command_spec);
3168 exit (0);
3169 }
3170
3171 case OPT_dumpversion:
3172 printf ("%s\n", spec_version);
3173 exit (0);
3174
3175 case OPT_dumpmachine:
3176 printf ("%s\n", spec_machine);
3177 exit (0);
3178
e200444e 3179 case OPT__version:
603349bf
JM
3180 print_version = 1;
3181
3182 /* CPP driver cannot obtain switch from cc1_options. */
3183 if (is_cpp_driver)
3184 add_preprocessor_option ("--version", strlen ("--version"));
3185 add_assembler_option ("--version", strlen ("--version"));
3186 add_linker_option ("--version", strlen ("--version"));
3187 break;
3188
e200444e 3189 case OPT__help:
603349bf
JM
3190 print_help_list = 1;
3191
3192 /* CPP driver cannot obtain switch from cc1_options. */
3193 if (is_cpp_driver)
3194 add_preprocessor_option ("--help", 6);
3195 add_assembler_option ("--help", 6);
3196 add_linker_option ("--help", 6);
3197 break;
3198
e200444e 3199 case OPT__help_:
603349bf
JM
3200 print_subprocess_help = 2;
3201 break;
3202
e200444e 3203 case OPT__target_help:
603349bf
JM
3204 print_subprocess_help = 1;
3205
3206 /* CPP driver cannot obtain switch from cc1_options. */
3207 if (is_cpp_driver)
3208 add_preprocessor_option ("--target-help", 13);
3209 add_assembler_option ("--target-help", 13);
3210 add_linker_option ("--target-help", 13);
3211 break;
3212
3213 case OPT_pass_exit_codes:
3214 case OPT_print_search_dirs:
3215 case OPT_print_file_name_:
3216 case OPT_print_prog_name_:
3217 case OPT_print_multi_lib:
3218 case OPT_print_multi_directory:
3219 case OPT_print_sysroot:
3220 case OPT_print_multi_os_directory:
3221 case OPT_print_sysroot_headers_suffix:
3222 case OPT_time:
3223 case OPT_wrapper:
3224 /* These options set the variables specified in common.opt
3225 automatically, and do not need to be saved for spec
3226 processing. */
3227 do_save = false;
3228 break;
3229
3230 case OPT_print_libgcc_file_name:
3231 print_file_name = "libgcc.a";
3232 do_save = false;
3233 break;
3234
3235 case OPT_fcompare_debug_second:
3236 compare_debug_second = 1;
3237 break;
3238
3239 case OPT_fcompare_debug:
3240 switch (value)
3241 {
3242 case 0:
3243 compare_debug_replacement_opt = "-fcompare-debug=";
3244 arg = "";
3245 goto compare_debug_with_arg;
3246
3247 case 1:
3248 compare_debug_replacement_opt = "-fcompare-debug=-gtoggle";
3249 arg = "-gtoggle";
3250 goto compare_debug_with_arg;
3251
3252 default:
3253 gcc_unreachable ();
3254 }
3255 break;
3256
3257 case OPT_fcompare_debug_:
3258 compare_debug_replacement_opt = decoded->canonical_option[0];
3259 compare_debug_with_arg:
3260 gcc_assert (decoded->canonical_option_num_elements == 1);
3261 gcc_assert (arg != NULL);
3262 if (arg)
3263 compare_debug = 1;
3264 else
3265 compare_debug = -1;
3266 if (compare_debug < 0)
3267 compare_debug_opt = NULL;
3268 else
3269 compare_debug_opt = arg;
3270 save_switch (compare_debug_replacement_opt, 0, NULL, validated);
3271 return true;
3272
3273 case OPT_Wa_:
3274 {
3275 int prev, j;
3276 /* Pass the rest of this option to the assembler. */
3277
3278 /* Split the argument at commas. */
3279 prev = 0;
3280 for (j = 0; arg[j]; j++)
3281 if (arg[j] == ',')
3282 {
3283 add_assembler_option (arg + prev, j - prev);
3284 prev = j + 1;
3285 }
3286
3287 /* Record the part after the last comma. */
3288 add_assembler_option (arg + prev, j - prev);
3289 }
3290 do_save = false;
3291 break;
3292
3293 case OPT_Wp_:
3294 {
3295 int prev, j;
3296 /* Pass the rest of this option to the preprocessor. */
3297
3298 /* Split the argument at commas. */
3299 prev = 0;
3300 for (j = 0; arg[j]; j++)
3301 if (arg[j] == ',')
3302 {
3303 add_preprocessor_option (arg + prev, j - prev);
3304 prev = j + 1;
3305 }
3306
3307 /* Record the part after the last comma. */
3308 add_preprocessor_option (arg + prev, j - prev);
3309 }
3310 do_save = false;
3311 break;
3312
3313 case OPT_Wl_:
3314 {
3315 int prev, j;
3316 /* Split the argument at commas. */
3317 prev = 0;
3318 for (j = 0; arg[j]; j++)
3319 if (arg[j] == ',')
3320 {
3321 add_infile (save_string (arg + prev, j - prev), "*");
3322 prev = j + 1;
3323 }
3324 /* Record the part after the last comma. */
3325 add_infile (arg + prev, "*");
3326 }
3327 do_save = false;
3328 break;
3329
3330 case OPT_Xlinker:
3331 add_infile (arg, "*");
3332 do_save = false;
3333 break;
3334
3335 case OPT_Xpreprocessor:
3336 add_preprocessor_option (arg, strlen (arg));
3337 do_save = false;
3338 break;
3339
3340 case OPT_Xassembler:
3341 add_assembler_option (arg, strlen (arg));
3342 do_save = false;
3343 break;
3344
3345 case OPT_l:
3346 /* POSIX allows separation of -l and the lib arg; canonicalize
3347 by concatenating -l with its arg */
3348 add_infile (concat ("-l", arg, NULL), "*");
3349 do_save = false;
3350 break;
3351
5de8299c
JM
3352 case OPT_L:
3353 /* Similarly, canonicalize -L for linkers that may not accept
3354 separate arguments. */
3355 save_switch (concat ("-L", arg, NULL), 0, NULL, validated);
3356 return true;
3357
603349bf
JM
3358 case OPT_save_temps:
3359 save_temps_flag = SAVE_TEMPS_CWD;
3360 validated = true;
3361 break;
3362
3363 case OPT_save_temps_:
3364 if (strcmp (arg, "cwd") == 0)
3365 save_temps_flag = SAVE_TEMPS_CWD;
3366 else if (strcmp (arg, "obj") == 0
3367 || strcmp (arg, "object") == 0)
3368 save_temps_flag = SAVE_TEMPS_OBJ;
3369 else
3370 fatal_error ("%qs is an unknown -save-temps option",
3371 decoded->orig_option_with_args_text);
3372 break;
3373
3374 case OPT_no_canonical_prefixes:
3375 /* Already handled as a special case, so ignored here. */
3376 do_save = false;
3377 break;
3378
3379 case OPT_pipe:
3380 validated = true;
603349bf
JM
3381 /* These options set the variables specified in common.opt
3382 automatically, but do need to be saved for spec
3383 processing. */
3384 break;
3385
603349bf
JM
3386 case OPT_specs_:
3387 {
3388 struct user_specs *user = XNEW (struct user_specs);
3389
3390 user->next = (struct user_specs *) 0;
3391 user->filename = arg;
3392 if (user_specs_tail)
3393 user_specs_tail->next = user;
3394 else
3395 user_specs_head = user;
3396 user_specs_tail = user;
3397 }
3398 do_save = false;
3399 break;
3400
3401 case OPT__sysroot_:
3402 target_system_root = arg;
3403 target_system_root_changed = 1;
3404 do_save = false;
3405 break;
3406
3407 case OPT_time_:
3408 if (report_times_to_file)
3409 fclose (report_times_to_file);
3410 report_times_to_file = fopen (arg, "a");
3411 do_save = false;
3412 break;
3413
3414 case OPT____:
3415 /* "-###"
3416 This is similar to -v except that there is no execution
3417 of the commands and the echoed arguments are quoted. It
3418 is intended for use in shell scripts to capture the
3419 driver-generated command line. */
3420 verbose_only_flag++;
d5478783 3421 verbose_flag = 1;
603349bf
JM
3422 do_save = false;
3423 break;
3424
3425 case OPT_B:
3426 {
3427 size_t len = strlen (arg);
3428
3429 /* Catch the case where the user has forgotten to append a
3430 directory separator to the path. Note, they may be using
3431 -B to add an executable name prefix, eg "i386-elf-", in
3432 order to distinguish between multiple installations of
3433 GCC in the same directory. Hence we must check to see
3434 if appending a directory separator actually makes a
3435 valid directory name. */
3436 if (!IS_DIR_SEPARATOR (arg[len - 1])
3437 && is_directory (arg, false))
3438 {
3439 char *tmp = XNEWVEC (char, len + 2);
3440 strcpy (tmp, arg);
3441 tmp[len] = DIR_SEPARATOR;
3442 tmp[++len] = 0;
3443 arg = tmp;
3444 }
3445
3446 add_prefix (&exec_prefixes, arg, NULL,
3447 PREFIX_PRIORITY_B_OPT, 0, 0);
3448 add_prefix (&startfile_prefixes, arg, NULL,
3449 PREFIX_PRIORITY_B_OPT, 0, 0);
3450 add_prefix (&include_prefixes, arg, NULL,
3451 PREFIX_PRIORITY_B_OPT, 0, 0);
3452 }
3453 validated = true;
3454 break;
3455
603349bf
JM
3456 case OPT_x:
3457 spec_lang = arg;
3458 if (!strcmp (spec_lang, "none"))
3459 /* Suppress the warning if -xnone comes after the last input
3460 file, because alternate command interfaces like g++ might
3461 find it useful to place -xnone after each input file. */
3462 spec_lang = 0;
3463 else
3464 last_language_n_infiles = n_infiles;
3465 do_save = false;
3466 break;
3467
603349bf
JM
3468 case OPT_o:
3469 have_o = 1;
3470#if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3471 arg = convert_filename (arg, ! have_c, 0);
3472#endif
3473 /* Save the output name in case -save-temps=obj was used. */
3474 save_temps_prefix = xstrdup (arg);
3475 /* On some systems, ld cannot handle "-o" without a space. So
3476 split the option from its argument. */
3477 save_switch ("-o", 1, &arg, validated);
3478 return true;
3479
3480 case OPT_static_libgcc:
3481 case OPT_shared_libgcc:
3482 case OPT_static_libgfortran:
3483 case OPT_static_libstdc__:
3484 /* These are always valid, since gcc.c itself understands the
3485 first two, gfortranspec.c understands -static-libgfortran and
3486 g++spec.c understands -static-libstdc++ */
3487 validated = true;
3488 break;
3489
3490 default:
d9d16a19
JM
3491 /* Various driver options need no special processing at this
3492 point, having been handled in a prescan above or being
3493 handled by specs. */
3494 break;
603349bf
JM
3495 }
3496
3497 if (do_save)
3498 save_switch (decoded->canonical_option[0],
3499 decoded->canonical_option_num_elements - 1,
3500 &decoded->canonical_option[1], validated);
3501 return true;
3502}
3503
efe5e5a0
JM
3504/* Put the driver's standard set of option handlers in *HANDLERS. */
3505
3506static void
3507set_option_handlers (struct cl_option_handlers *handlers)
3508{
3509 handlers->unknown_option_callback = driver_unknown_option_callback;
3510 handlers->wrong_lang_callback = driver_wrong_lang_callback;
3511 handlers->post_handling_callback = driver_post_handling_callback;
3512 handlers->num_handlers = 1;
3513 handlers->handlers[0].handler = driver_handle_option;
3514 handlers->handlers[0].mask = CL_DRIVER;
3515}
3516
ed1f651b
RS
3517/* Create the vector `switches' and its contents.
3518 Store its length in `n_switches'. */
3519
3520static void
60cf253a
JM
3521process_command (unsigned int decoded_options_count,
3522 struct cl_decoded_option *decoded_options)
ed1f651b 3523{
878f32c3
KG
3524 const char *temp;
3525 char *temp1;
f4c0a303 3526 const char *tooldir_prefix;
ba0c638e
SB
3527 char *(*get_relative_prefix) (const char *, const char *,
3528 const char *) = NULL;
603349bf 3529 struct cl_option_handlers handlers;
60cf253a 3530 unsigned int j;
ed1f651b 3531
2f8dd115 3532 GET_ENVIRONMENT (gcc_exec_prefix, "GCC_EXEC_PREFIX");
8eebb258 3533
ed1f651b
RS
3534 n_switches = 0;
3535 n_infiles = 0;
a2a05b0a 3536 added_libraries = 0;
2484b6d2 3537
53117a2f
RK
3538 /* Figure compiler version from version string. */
3539
9218435e 3540 compiler_version = temp1 = xstrdup (version_string);
ad85216e 3541
878f32c3 3542 for (; *temp1; ++temp1)
53117a2f 3543 {
878f32c3 3544 if (*temp1 == ' ')
53117a2f 3545 {
878f32c3 3546 *temp1 = '\0';
53117a2f
RK
3547 break;
3548 }
3549 }
ed1f651b 3550
ba0c638e
SB
3551 /* Handle any -no-canonical-prefixes flag early, to assign the function
3552 that builds relative prefixes. This function creates default search
3553 paths that are needed later in normal option handling. */
3554
e200444e 3555 for (j = 1; j < decoded_options_count; j++)
ba0c638e 3556 {
e200444e 3557 if (decoded_options[j].opt_index == OPT_no_canonical_prefixes)
ba0c638e
SB
3558 {
3559 get_relative_prefix = make_relative_prefix_ignore_links;
3560 break;
3561 }
3562 }
3563 if (! get_relative_prefix)
3564 get_relative_prefix = make_relative_prefix;
3565
0deb20df 3566 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
60cf253a
JM
3567 see if we can create it from the pathname specified in
3568 decoded_options[0].arg. */
0deb20df 3569
a8ee6e2d 3570 gcc_libexec_prefix = standard_libexec_prefix;
0deb20df
TT
3571#ifndef VMS
3572 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3573 if (!gcc_exec_prefix)
3574 {
60cf253a 3575 gcc_exec_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3576 standard_bindir_prefix,
3577 standard_exec_prefix);
60cf253a 3578 gcc_libexec_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3579 standard_bindir_prefix,
3580 standard_libexec_prefix);
0deb20df 3581 if (gcc_exec_prefix)
47d33318 3582 xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
0deb20df 3583 }
a8ee6e2d 3584 else
c7370b83
JM
3585 {
3586 /* make_relative_prefix requires a program name, but
3587 GCC_EXEC_PREFIX is typically a directory name with a trailing
3588 / (which is ignored by make_relative_prefix), so append a
3589 program name. */
3590 char *tmp_prefix = concat (gcc_exec_prefix, "gcc", NULL);
ba0c638e
SB
3591 gcc_libexec_prefix = get_relative_prefix (tmp_prefix,
3592 standard_exec_prefix,
3593 standard_libexec_prefix);
f4c0a303
CD
3594
3595 /* The path is unrelocated, so fallback to the original setting. */
3596 if (!gcc_libexec_prefix)
3597 gcc_libexec_prefix = standard_libexec_prefix;
3598
c7370b83
JM
3599 free (tmp_prefix);
3600 }
a8ee6e2d 3601#else
0deb20df 3602#endif
f4c0a303
CD
3603 /* From this point onward, gcc_exec_prefix is non-null if the toolchain
3604 is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
60cf253a
JM
3605 or an automatically created GCC_EXEC_PREFIX from
3606 decoded_options[0].arg. */
ed1f651b 3607
a288b143 3608 /* Do language-specific adjustment/addition of flags. */
d9d16a19 3609 lang_specific_driver (&decoded_options, &decoded_options_count,
a288b143
AH
3610 &added_libraries);
3611
8eebb258 3612 if (gcc_exec_prefix)
ed1f651b 3613 {
6ed4bb9a 3614 int len = strlen (gcc_exec_prefix);
c5c0b3d9 3615
a8ee6e2d 3616 if (len > (int) sizeof ("/lib/gcc/") - 1
509781a4 3617 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
6ed4bb9a 3618 {
a8ee6e2d 3619 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc/") + 1;
509781a4 3620 if (IS_DIR_SEPARATOR (*temp)
d25a45d4 3621 && strncmp (temp + 1, "lib", 3) == 0
509781a4 3622 && IS_DIR_SEPARATOR (temp[4])
c5ef564b 3623 && strncmp (temp + 5, "gcc", 3) == 0)
a8ee6e2d 3624 len -= sizeof ("/lib/gcc/") - 1;
6ed4bb9a
MM
3625 }
3626
3627 set_std_prefix (gcc_exec_prefix, len);
a8ee6e2d 3628 add_prefix (&exec_prefixes, gcc_libexec_prefix, "GCC",
1a5d37a1 3629 PREFIX_PRIORITY_LAST, 0, 0);
922a4beb 3630 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
1a5d37a1 3631 PREFIX_PRIORITY_LAST, 0, 0);
ed1f651b
RS
3632 }
3633
3634 /* COMPILER_PATH and LIBRARY_PATH have values
3635 that are lists of directory names with colons. */
3636
2f8dd115 3637 GET_ENVIRONMENT (temp, "COMPILER_PATH");
ed1f651b
RS
3638 if (temp)
3639 {
878f32c3 3640 const char *startp, *endp;
e1e4cdc4 3641 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3642
3643 startp = endp = temp;
3644 while (1)
3645 {
f6ec7e54 3646 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3647 {
d25a45d4 3648 strncpy (nstore, startp, endp - startp);
ed1f651b 3649 if (endp == startp)
d4f2852f 3650 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3651 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3652 {
d25a45d4
KH
3653 nstore[endp - startp] = DIR_SEPARATOR;
3654 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3655 }
3656 else
d25a45d4 3657 nstore[endp - startp] = 0;
922a4beb 3658 add_prefix (&exec_prefixes, nstore, 0,
1a5d37a1 3659 PREFIX_PRIORITY_LAST, 0, 0);
76391e5a 3660 add_prefix (&include_prefixes, nstore, 0,
1a5d37a1 3661 PREFIX_PRIORITY_LAST, 0, 0);
ed1f651b
RS
3662 if (*endp == 0)
3663 break;
3664 endp = startp = endp + 1;
3665 }
3666 else
3667 endp++;
3668 }
3669 }
3670
2f8dd115 3671 GET_ENVIRONMENT (temp, LIBRARY_PATH_ENV);
fcc9ad83 3672 if (temp && *cross_compile == '0')
ed1f651b 3673 {
878f32c3 3674 const char *startp, *endp;
e1e4cdc4 3675 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3676
3677 startp = endp = temp;
3678 while (1)
3679 {
f6ec7e54 3680 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3681 {
d25a45d4 3682 strncpy (nstore, startp, endp - startp);
ed1f651b 3683 if (endp == startp)
d4f2852f 3684 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3685 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3686 {
d25a45d4
KH
3687 nstore[endp - startp] = DIR_SEPARATOR;
3688 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3689 }
3690 else
d25a45d4 3691 nstore[endp - startp] = 0;
6496a589 3692 add_prefix (&startfile_prefixes, nstore, NULL,
1a5d37a1 3693 PREFIX_PRIORITY_LAST, 0, 1);
ed1f651b
RS
3694 if (*endp == 0)
3695 break;
3696 endp = startp = endp + 1;
3697 }
3698 else
3699 endp++;
3700 }
3701 }
3702
3703 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2f8dd115 3704 GET_ENVIRONMENT (temp, "LPATH");
fcc9ad83 3705 if (temp && *cross_compile == '0')
ed1f651b 3706 {
878f32c3 3707 const char *startp, *endp;
e1e4cdc4 3708 char *nstore = (char *) alloca (strlen (temp) + 3);
ed1f651b
RS
3709
3710 startp = endp = temp;
3711 while (1)
3712 {
f6ec7e54 3713 if (*endp == PATH_SEPARATOR || *endp == 0)
ed1f651b 3714 {
d25a45d4 3715 strncpy (nstore, startp, endp - startp);
ed1f651b 3716 if (endp == startp)
d4f2852f 3717 strcpy (nstore, concat (".", dir_separator_str, NULL));
509781a4 3718 else if (!IS_DIR_SEPARATOR (endp[-1]))
ed1f651b 3719 {
d25a45d4
KH
3720 nstore[endp - startp] = DIR_SEPARATOR;
3721 nstore[endp - startp + 1] = 0;
ed1f651b
RS
3722 }
3723 else
d25a45d4 3724 nstore[endp - startp] = 0;
6496a589 3725 add_prefix (&startfile_prefixes, nstore, NULL,
1a5d37a1 3726 PREFIX_PRIORITY_LAST, 0, 1);
ed1f651b
RS
3727 if (*endp == 0)
3728 break;
3729 endp = startp = endp + 1;
3730 }
3731 else
3732 endp++;
3733 }
3734 }
3735
922e1882
JM
3736 /* Process the options and store input files and switches in their
3737 vectors. */
3738
3739 last_language_n_infiles = -1;
ed1f651b 3740
efe5e5a0 3741 set_option_handlers (&handlers);
fe7df9c4 3742
603349bf
JM
3743 for (j = 1; j < decoded_options_count; j++)
3744 {
3745 switch (decoded_options[j].opt_index)
99f78cdd 3746 {
603349bf
JM
3747 case OPT_S:
3748 case OPT_c:
3749 case OPT_E:
3750 have_c = 1;
3751 break;
99f78cdd 3752 }
603349bf
JM
3753 if (have_c)
3754 break;
3755 }
ed1f651b 3756
603349bf
JM
3757 for (j = 1; j < decoded_options_count; j++)
3758 {
3759 if (decoded_options[j].opt_index == OPT_SPECIAL_input_file)
3a265431 3760 {
603349bf
JM
3761 const char *arg = decoded_options[j].arg;
3762 const char *p = strrchr (arg, '@');
922e1882
JM
3763 char *fname;
3764 long offset;
3765 int consumed;
3766#ifdef HAVE_TARGET_OBJECT_SUFFIX
603349bf 3767 arg = convert_filename (arg, 0, access (arg, F_OK));
922e1882
JM
3768#endif
3769 /* For LTO static archive support we handle input file
3770 specifications that are composed of a filename and
3771 an offset like FNAME@OFFSET. */
3772 if (p
603349bf 3773 && p != arg
922e1882
JM
3774 && sscanf (p, "@%li%n", &offset, &consumed) >= 1
3775 && strlen (p) == (unsigned int)consumed)
3776 {
603349bf
JM
3777 fname = (char *)xmalloc (p - arg + 1);
3778 memcpy (fname, arg, p - arg);
3779 fname[p - arg] = '\0';
922e1882
JM
3780 /* Only accept non-stdin and existing FNAME parts, otherwise
3781 try with the full name. */
3782 if (strcmp (fname, "-") == 0 || access (fname, F_OK) < 0)
3783 {
3784 free (fname);
603349bf 3785 fname = xstrdup (arg);
922e1882
JM
3786 }
3787 }
3788 else
603349bf 3789 fname = xstrdup (arg);
922e1882
JM
3790
3791 if (strcmp (fname, "-") != 0 && access (fname, F_OK) < 0)
3792 perror_with_name (fname);
3793 else
603349bf 3794 add_infile (arg, spec_lang);
922e1882
JM
3795
3796 free (fname);
603349bf 3797 continue;
3a265431 3798 }
603349bf 3799
d4d24ba4 3800 read_cmdline_option (&global_options, &global_options_set,
a4d8c676
JM
3801 decoded_options + j, UNKNOWN_LOCATION,
3802 CL_DRIVER, &handlers, global_dc);
ed1f651b
RS
3803 }
3804
14fdc613
MM
3805 /* If -save-temps=obj and -o name, create the prefix to use for %b.
3806 Otherwise just make -save-temps=obj the same as -save-temps=cwd. */
3807 if (save_temps_flag == SAVE_TEMPS_OBJ && save_temps_prefix != NULL)
3808 {
3809 save_temps_length = strlen (save_temps_prefix);
3810 temp = strrchr (lbasename (save_temps_prefix), '.');
3811 if (temp)
3812 {
3813 save_temps_length -= strlen (temp);
3814 save_temps_prefix[save_temps_length] = '\0';
3815 }
3816
3817 }
3818 else if (save_temps_prefix != NULL)
3819 {
3820 free (save_temps_prefix);
3821 save_temps_prefix = NULL;
3822 }
3823
054e88a8 3824 if (save_temps_flag && use_pipes)
4977bab6
ZW
3825 {
3826 /* -save-temps overrides -pipe, so that temp files are produced */
3827 if (save_temps_flag)
2dec80c7 3828 warning (0, "-pipe ignored because -save-temps specified");
4977bab6
ZW
3829 use_pipes = 0;
3830 }
1d088dee 3831
2153915d
AO
3832 if (!compare_debug)
3833 {
3834 const char *gcd = getenv ("GCC_COMPARE_DEBUG");
3835
3836 if (gcd && gcd[0] == '-')
3837 {
3838 compare_debug = 2;
3839 compare_debug_opt = gcd;
2153915d
AO
3840 }
3841 else if (gcd && *gcd && strcmp (gcd, "0"))
3842 {
3843 compare_debug = 3;
3844 compare_debug_opt = "-gtoggle";
2153915d
AO
3845 }
3846 }
3847 else if (compare_debug < 0)
3848 {
3849 compare_debug = 0;
3850 gcc_assert (!compare_debug_opt);
3851 }
3852
f4c0a303
CD
3853 /* Set up the search paths. We add directories that we expect to
3854 contain GNU Toolchain components before directories specified by
3855 the machine description so that we will find GNU components (like
b8698a0f 3856 the GNU assembler) before those of the host system. */
ed1f651b 3857
f4c0a303
CD
3858 /* If we don't know where the toolchain has been installed, use the
3859 configured-in locations. */
3860 if (!gcc_exec_prefix)
3861 {
48ff801b 3862#ifndef OS2
f4c0a303
CD
3863 add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
3864 PREFIX_PRIORITY_LAST, 1, 0);
3865 add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
3866 PREFIX_PRIORITY_LAST, 2, 0);
3867 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3868 PREFIX_PRIORITY_LAST, 2, 0);
48ff801b 3869#endif
f4c0a303
CD
3870 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3871 PREFIX_PRIORITY_LAST, 1, 0);
3872 }
ed1f651b 3873
f4c0a303 3874 gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
9218435e 3875 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
d4f2852f 3876 dir_separator_str, NULL);
c648ab8a 3877
f4c0a303
CD
3878 /* Look for tools relative to the location from which the driver is
3879 running, or, if that is not available, the configured prefix. */
3880 tooldir_prefix
3881 = concat (gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
3882 spec_machine, dir_separator_str,
3883 spec_version, dir_separator_str, tooldir_prefix, NULL);
c648ab8a 3884
9218435e 3885 add_prefix (&exec_prefixes,
d4f2852f 3886 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
1a5d37a1 3887 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 0);
48ff801b 3888 add_prefix (&startfile_prefixes,
d4f2852f 3889 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
1a5d37a1 3890 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);
f18fd956 3891
047d636f
DJ
3892#if defined(TARGET_SYSTEM_ROOT_RELOCATABLE) && !defined(VMS)
3893 /* If the normal TARGET_SYSTEM_ROOT is inside of $exec_prefix,
3894 then consider it to relocate with the rest of the GCC installation
3895 if GCC_EXEC_PREFIX is set.
3896 ``make_relative_prefix'' is not compiled for VMS, so don't call it. */
922e1882 3897 if (target_system_root && !target_system_root_changed && gcc_exec_prefix)
4977bab6 3898 {
60cf253a 3899 char *tmp_prefix = get_relative_prefix (decoded_options[0].arg,
ba0c638e
SB
3900 standard_bindir_prefix,
3901 target_system_root);
4977bab6 3902 if (tmp_prefix && access_check (tmp_prefix, F_OK) == 0)
047d636f
DJ
3903 {
3904 target_system_root = tmp_prefix;
3905 target_system_root_changed = 1;
3906 }
4977bab6 3907 }
047d636f 3908#endif
4977bab6 3909
004fd4d5
RS
3910 /* More prefixes are enabled in main, after we read the specs file
3911 and determine whether this is cross-compilation or not. */
ed1f651b 3912
fa0d5369 3913 if (n_infiles == last_language_n_infiles && spec_lang != 0)
bdc6b402 3914 warning (0, "%<-x %s%> after last input file has no effect", spec_lang);
ed1f651b 3915
2153915d
AO
3916 if (compare_debug == 2 || compare_debug == 3)
3917 {
922e1882 3918 alloc_switch ();
2153915d
AO
3919 switches[n_switches].part1 = concat ("fcompare-debug=",
3920 compare_debug_opt,
3921 NULL);
3922 switches[n_switches].args = 0;
3923 switches[n_switches].live_cond = 0;
3924 switches[n_switches].validated = 0;
3925 switches[n_switches].ordering = 0;
3926 n_switches++;
3927 compare_debug = 1;
3928 }
3929
69927b59 3930 /* Ensure we only invoke each subprocess once. */
41fd0f9b 3931 if (print_subprocess_help || print_help_list || print_version)
69927b59 3932 {
922e1882 3933 n_infiles = 0;
69927b59 3934
c662432e
NC
3935 /* Create a dummy input file, so that we can pass
3936 the help option on to the various sub-processes. */
922e1882 3937 add_infile ("help-dummy", "c");
69927b59
NB
3938 }
3939
922e1882 3940 alloc_switch ();
ed1f651b 3941 switches[n_switches].part1 = 0;
922e1882 3942 alloc_infile ();
ed1f651b
RS
3943 infiles[n_infiles].name = 0;
3944}
b856c15d 3945
4977bab6 3946/* Store switches not filtered out by %<S in spec in COLLECT_GCC_OPTIONS
b856c15d
RO
3947 and place that in the environment. */
3948
3949static void
1d088dee 3950set_collect_gcc_options (void)
b856c15d
RO
3951{
3952 int i;
3953 int first_time;
3954
3955 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3956 the compiler. */
3957 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3958 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
3959
3960 first_time = TRUE;
3961 for (i = 0; (int) i < n_switches; i++)
3962 {
3963 const char *const *args;
3964 const char *p, *q;
3965 if (!first_time)
3966 obstack_grow (&collect_obstack, " ", 1);
3967
3968 first_time = FALSE;
3969
3970 /* Ignore elided switches. */
3371362c 3971 if ((switches[i].live_cond & SWITCH_IGNORE) != 0)
b856c15d
RO
3972 continue;
3973
3974 obstack_grow (&collect_obstack, "'-", 2);
3975 q = switches[i].part1;
3976 while ((p = strchr (q, '\'')))
3977 {
3978 obstack_grow (&collect_obstack, q, p - q);
3979 obstack_grow (&collect_obstack, "'\\''", 4);
3980 q = ++p;
3981 }
3982 obstack_grow (&collect_obstack, q, strlen (q));
3983 obstack_grow (&collect_obstack, "'", 1);
3984
3985 for (args = switches[i].args; args && *args; args++)
3986 {
3987 obstack_grow (&collect_obstack, " '", 2);
3988 q = *args;
3989 while ((p = strchr (q, '\'')))
3990 {
3991 obstack_grow (&collect_obstack, q, p - q);
3992 obstack_grow (&collect_obstack, "'\\''", 4);
3993 q = ++p;
3994 }
3995 obstack_grow (&collect_obstack, q, strlen (q));
3996 obstack_grow (&collect_obstack, "'", 1);
3997 }
3998 }
3999 obstack_grow (&collect_obstack, "\0", 1);
47d33318 4000 xputenv (XOBFINISH (&collect_obstack, char *));
b856c15d 4001}
ed1f651b
RS
4002\f
4003/* Process a spec string, accumulating and running commands. */
4004
4005/* These variables describe the input file name.
4006 input_file_number is the index on outfiles of this file,
4007 so that the output file name can be stored for later use by %o.
4008 input_basename is the start of the part of the input file
4009 sans all directory names, and basename_length is the number
4010 of characters starting there excluding the suffix .c or whatever. */
4011
6afbc885 4012static const char *gcc_input_filename;
ed1f651b 4013static int input_file_number;
f271358e 4014size_t input_filename_length;
ed1f651b 4015static int basename_length;
ea414c97 4016static int suffixed_basename_length;
878f32c3
KG
4017static const char *input_basename;
4018static const char *input_suffix;
a9024779 4019#ifndef HOST_LACKS_INODE_NUMBERS
99f78cdd 4020static struct stat input_stat;
a9024779 4021#endif
99f78cdd 4022static int input_stat_set;
ed1f651b 4023
a9374841
MM
4024/* The compiler used to process the current input file. */
4025static struct compiler *input_file_compiler;
4026
ed1f651b
RS
4027/* These are variables used within do_spec and do_spec_1. */
4028
4029/* Nonzero if an arg has been started and not yet terminated
4030 (with space, tab or newline). */
4031static int arg_going;
4032
4033/* Nonzero means %d or %g has been seen; the next arg to be terminated
4034 is a temporary file name. */
4035static int delete_this_arg;
4036
4037/* Nonzero means %w has been seen; the next arg to be terminated
4038 is the output file name of this compilation. */
4039static int this_is_output_file;
4040
4041/* Nonzero means %s has been seen; the next arg to be terminated
4042 is the name of a library file and we should try the standard
4043 search dirs for it. */
4044static int this_is_library_file;
4045
3beb864c
NC
4046/* Nonzero means %T has been seen; the next arg to be terminated
4047 is the name of a linker script and we should try all of the
4048 standard search dirs for it. If it is found insert a --script
4049 command line switch and then substitute the full path in place,
4050 otherwise generate an error message. */
4051static int this_is_linker_script;
4052
a99bf70c
JW
4053/* Nonzero means that the input of this command is coming from a pipe. */
4054static int input_from_pipe;
4055
11972f66 4056/* Nonnull means substitute this for any suffix when outputting a switches
dc297297 4057 arguments. */
11972f66
NS
4058static const char *suffix_subst;
4059
13e7cedb
DH
4060/* If there is an argument being accumulated, terminate it and store it. */
4061
4062static void
4063end_going_arg (void)
4064{
4065 if (arg_going)
4066 {
4067 const char *string;
4068
4069 obstack_1grow (&obstack, 0);
4070 string = XOBFINISH (&obstack, const char *);
4071 if (this_is_library_file)
4072 string = find_file (string);
3beb864c
NC
4073 if (this_is_linker_script)
4074 {
4075 char * full_script_path = find_a_file (&startfile_prefixes, string, R_OK, true);
4076
4077 if (full_script_path == NULL)
4078 {
bdc6b402 4079 error ("unable to locate default linker script %qs in the library search paths", string);
3beb864c
NC
4080 /* Script was not found on search path. */
4081 return;
4082 }
4083 store_arg ("--script", false, false);
4084 string = full_script_path;
4085 }
13e7cedb
DH
4086 store_arg (string, delete_this_arg, this_is_output_file);
4087 if (this_is_output_file)
4088 outfiles[input_file_number] = string;
4089 arg_going = 0;
4090 }
4091}
4092
fe7df9c4
SP
4093
4094/* Parse the WRAPPER string which is a comma separated list of the command line
4095 and insert them into the beginning of argbuf. */
4096
4097static void
4098insert_wrapper (const char *wrapper)
4099{
4100 int n = 0;
4101 int i;
4102 char *buf = xstrdup (wrapper);
4103 char *p = buf;
5b634ee0 4104 unsigned int old_length = VEC_length (const_char_p, argbuf);
fe7df9c4
SP
4105
4106 do
4107 {
4108 n++;
4109 while (*p == ',')
4110 p++;
4111 }
4112 while ((p = strchr (p, ',')) != NULL);
4113
5b634ee0
JM
4114 VEC_safe_grow (const_char_p, heap, argbuf, old_length + n);
4115 memmove (VEC_address (const_char_p, argbuf) + n,
4116 VEC_address (const_char_p, argbuf),
4117 old_length * sizeof (const_char_p));
fe7df9c4
SP
4118
4119 i = 0;
4120 p = buf;
4121 do
4122 {
4123 while (*p == ',')
4124 {
4125 *p = 0;
4126 p++;
4127 }
5b634ee0
JM
4128 VEC_replace (const_char_p, argbuf, i, p);
4129 i++;
fe7df9c4
SP
4130 }
4131 while ((p = strchr (p, ',')) != NULL);
4132 gcc_assert (i == n);
fe7df9c4
SP
4133}
4134
ed1f651b
RS
4135/* Process the spec SPEC and run the commands specified therein.
4136 Returns 0 if the spec is successfully processed; -1 if failed. */
4137
f271358e 4138int
1d088dee 4139do_spec (const char *spec)
ed1f651b
RS
4140{
4141 int value;
4142
343f59d9 4143 value = do_spec_2 (spec);
ed1f651b
RS
4144
4145 /* Force out any unfinished command.
4146 If -pipe, this forces out the last command if it ended in `|'. */
4147 if (value == 0)
4148 {
5b634ee0
JM
4149 if (VEC_length (const_char_p, argbuf) > 0
4150 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
4151 VEC_pop (const_char_p, argbuf);
ed1f651b 4152
b856c15d
RO
4153 set_collect_gcc_options ();
4154
5b634ee0 4155 if (VEC_length (const_char_p, argbuf) > 0)
ed1f651b
RS
4156 value = execute ();
4157 }
4158
4159 return value;
4160}
4161
343f59d9 4162static int
1d088dee 4163do_spec_2 (const char *spec)
343f59d9 4164{
6544fbcb
RS
4165 int result;
4166
343f59d9
AM
4167 clear_args ();
4168 arg_going = 0;
4169 delete_this_arg = 0;
4170 this_is_output_file = 0;
4171 this_is_library_file = 0;
3beb864c 4172 this_is_linker_script = 0;
343f59d9
AM
4173 input_from_pipe = 0;
4174 suffix_subst = NULL;
4175
6544fbcb
RS
4176 result = do_spec_1 (spec, 0, NULL);
4177
13e7cedb 4178 end_going_arg ();
6544fbcb
RS
4179
4180 return result;
343f59d9
AM
4181}
4182
db36994b 4183
7816bea0
DJ
4184/* Process the given spec string and add any new options to the end
4185 of the switches/n_switches array. */
4186
4187static void
1d088dee 4188do_option_spec (const char *name, const char *spec)
7816bea0
DJ
4189{
4190 unsigned int i, value_count, value_len;
4191 const char *p, *q, *value;
4192 char *tmp_spec, *tmp_spec_p;
4193
4194 if (configure_default_options[0].name == NULL)
4195 return;
4196
4197 for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
4198 if (strcmp (configure_default_options[i].name, name) == 0)
4199 break;
4200 if (i == ARRAY_SIZE (configure_default_options))
4201 return;
4202
4203 value = configure_default_options[i].value;
4204 value_len = strlen (value);
4205
4206 /* Compute the size of the final spec. */
4207 value_count = 0;
4208 p = spec;
4209 while ((p = strstr (p, "%(VALUE)")) != NULL)
4210 {
4211 p ++;
4212 value_count ++;
4213 }
4214
4215 /* Replace each %(VALUE) by the specified value. */
e1e4cdc4 4216 tmp_spec = (char *) alloca (strlen (spec) + 1
7816bea0
DJ
4217 + value_count * (value_len - strlen ("%(VALUE)")));
4218 tmp_spec_p = tmp_spec;
4219 q = spec;
4220 while ((p = strstr (q, "%(VALUE)")) != NULL)
4221 {
4222 memcpy (tmp_spec_p, q, p - q);
4223 tmp_spec_p = tmp_spec_p + (p - q);
4224 memcpy (tmp_spec_p, value, value_len);
4225 tmp_spec_p += value_len;
4226 q = p + strlen ("%(VALUE)");
4227 }
4228 strcpy (tmp_spec_p, q);
4229
4230 do_self_spec (tmp_spec);
4231}
4232
db36994b
RS
4233/* Process the given spec string and add any new options to the end
4234 of the switches/n_switches array. */
4235
4236static void
1d088dee 4237do_self_spec (const char *spec)
db36994b 4238{
94b90527
JJ
4239 int i;
4240
db36994b
RS
4241 do_spec_2 (spec);
4242 do_spec_1 (" ", 0, NULL);
4243
94b90527
JJ
4244 /* Mark %<S switches processed by do_self_spec to be ignored permanently.
4245 do_self_specs adds the replacements to switches array, so it shouldn't
4246 be processed afterwards. */
4247 for (i = 0; i < n_switches; i++)
4248 if ((switches[i].live_cond & SWITCH_IGNORE))
4249 switches[i].live_cond |= SWITCH_IGNORE_PERMANENTLY;
4250
5b634ee0 4251 if (VEC_length (const_char_p, argbuf) > 0)
db36994b 4252 {
efe5e5a0
JM
4253 const char **argbuf_copy;
4254 struct cl_decoded_option *decoded_options;
4255 struct cl_option_handlers handlers;
4256 unsigned int decoded_options_count;
4257 unsigned int j;
db36994b 4258
efe5e5a0
JM
4259 /* Create a copy of argbuf with a dummy argv[0] entry for
4260 decode_cmdline_options_to_array. */
5b634ee0
JM
4261 argbuf_copy = XNEWVEC (const char *,
4262 VEC_length (const_char_p, argbuf) + 1);
efe5e5a0 4263 argbuf_copy[0] = "";
5b634ee0
JM
4264 memcpy (argbuf_copy + 1, VEC_address (const_char_p, argbuf),
4265 VEC_length (const_char_p, argbuf) * sizeof (const char *));
db36994b 4266
5b634ee0
JM
4267 decode_cmdline_options_to_array (VEC_length (const_char_p, argbuf) + 1,
4268 argbuf_copy,
efe5e5a0
JM
4269 CL_DRIVER, &decoded_options,
4270 &decoded_options_count);
2153915d 4271
efe5e5a0 4272 set_option_handlers (&handlers);
2153915d 4273
efe5e5a0
JM
4274 for (j = 1; j < decoded_options_count; j++)
4275 {
4276 switch (decoded_options[j].opt_index)
2153915d 4277 {
efe5e5a0
JM
4278 case OPT_SPECIAL_input_file:
4279 /* Specs should only generate options, not input
4280 files. */
4281 if (strcmp (decoded_options[j].arg, "-") != 0)
4282 fatal_error ("switch %qs does not start with %<-%>",
4283 decoded_options[j].arg);
4284 else
4285 fatal_error ("spec-generated switch is just %<-%>");
4286 break;
2153915d 4287
efe5e5a0
JM
4288 case OPT_fcompare_debug_second:
4289 case OPT_fcompare_debug:
4290 case OPT_fcompare_debug_:
4291 case OPT_o:
4292 /* Avoid duplicate processing of some options from
4293 compare-debug specs; just save them here. */
4294 save_switch (decoded_options[j].canonical_option[0],
4295 (decoded_options[j].canonical_option_num_elements
4296 - 1),
4297 &decoded_options[j].canonical_option[1], false);
4298 break;
4299
4300 default:
4301 read_cmdline_option (&global_options, &global_options_set,
a4d8c676
JM
4302 decoded_options + j, UNKNOWN_LOCATION,
4303 CL_DRIVER, &handlers, global_dc);
efe5e5a0 4304 break;
2153915d 4305 }
db36994b 4306 }
2153915d 4307
efe5e5a0 4308 alloc_switch ();
2153915d 4309 switches[n_switches].part1 = 0;
db36994b
RS
4310 }
4311}
4312
00dcee0c
AM
4313/* Callback for processing %D and %I specs. */
4314
4315struct spec_path_info {
4316 const char *option;
4317 const char *append;
4318 size_t append_len;
4319 bool omit_relative;
4320 bool separate_options;
4321};
4322
4323static void *
4324spec_path (char *path, void *data)
76391e5a 4325{
e1e4cdc4 4326 struct spec_path_info *info = (struct spec_path_info *) data;
00dcee0c
AM
4327 size_t len = 0;
4328 char save = 0;
76391e5a 4329
00dcee0c
AM
4330 if (info->omit_relative && !IS_ABSOLUTE_PATH (path))
4331 return NULL;
4332
4333 if (info->append_len != 0)
76391e5a 4334 {
00dcee0c
AM
4335 len = strlen (path);
4336 memcpy (path + len, info->append, info->append_len + 1);
76391e5a
PB
4337 }
4338
00dcee0c
AM
4339 if (!is_directory (path, true))
4340 return NULL;
76391e5a 4341
00dcee0c
AM
4342 do_spec_1 (info->option, 1, NULL);
4343 if (info->separate_options)
4344 do_spec_1 (" ", 0, NULL);
4345
4346 if (info->append_len == 0)
76391e5a 4347 {
00dcee0c
AM
4348 len = strlen (path);
4349 save = path[len - 1];
4350 if (IS_DIR_SEPARATOR (path[len - 1]))
4351 path[len - 1] = '\0';
76391e5a 4352 }
00dcee0c
AM
4353
4354 do_spec_1 (path, 1, NULL);
4355 do_spec_1 (" ", 0, NULL);
4356
4357 /* Must not damage the original path. */
4358 if (info->append_len == 0)
4359 path[len - 1] = save;
4360
4361 return NULL;
76391e5a
PB
4362}
4363
5a691e98
RAE
4364/* Create a temporary FILE with the contents of ARGV. Add @FILE to the
4365 argument list. */
4366
4367static void
4368create_at_file (char **argv)
4369{
4370 char *temp_file = make_temp_file ("");
4371 char *at_argument = concat ("@", temp_file, NULL);
4372 FILE *f = fopen (temp_file, "w");
4373 int status;
4374
4375 if (f == NULL)
2dec80c7
JM
4376 fatal_error ("could not open temporary response file %s",
4377 temp_file);
5a691e98
RAE
4378
4379 status = writeargv (argv, f);
4380
4381 if (status)
2dec80c7
JM
4382 fatal_error ("could not write to temporary response file %s",
4383 temp_file);
5a691e98
RAE
4384
4385 status = fclose (f);
4386
4387 if (EOF == status)
2dec80c7
JM
4388 fatal_error ("could not close temporary response file %s",
4389 temp_file);
5a691e98
RAE
4390
4391 store_arg (at_argument, 0, 0);
4392
4393 record_temp_file (temp_file, !save_temps_flag, !save_temps_flag);
4394}
4395
4396/* True if we should compile INFILE. */
4397
4398static bool
4399compile_input_file_p (struct infile *infile)
4400{
4401 if ((!infile->language) || (infile->language[0] != '*'))
4402 if (infile->incompiler == input_file_compiler)
4403 return true;
4404 return false;
4405}
4406
ed1f651b
RS
4407/* Process the sub-spec SPEC as a portion of a larger spec.
4408 This is like processing a whole spec except that we do
4409 not initialize at the beginning and we do not supply a
4410 newline by default at the end.
4411 INSWITCH nonzero means don't process %-sequences in SPEC;
4412 in this case, % is treated as an ordinary character.
4413 This is used while substituting switches.
4414 INSWITCH nonzero also causes SPC not to terminate an argument.
4415
4416 Value is zero unless a line was finished
4417 and the command on that line reported an error. */
4418
4419static int
1d088dee 4420do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
ed1f651b 4421{
b3694847
SS
4422 const char *p = spec;
4423 int c;
ed1f651b 4424 int i;
3279bba6 4425 int value;
ed1f651b 4426
ededb2fc 4427 while ((c = *p++))
ed1f651b
RS
4428 /* If substituting a switch, treat all chars like letters.
4429 Otherwise, NL, SPC, TAB and % are special. */
4430 switch (inswitch ? 'a' : c)
4431 {
4432 case '\n':
13e7cedb 4433 end_going_arg ();
ed1f651b 4434
5b634ee0
JM
4435 if (VEC_length (const_char_p, argbuf) > 0
4436 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
ed1f651b 4437 {
ed1f651b
RS
4438 /* A `|' before the newline means use a pipe here,
4439 but only if -pipe was specified.
4440 Otherwise, execute now and don't pass the `|' as an arg. */
4977bab6 4441 if (use_pipes)
ed1f651b 4442 {
a99bf70c 4443 input_from_pipe = 1;
ed1f651b
RS
4444 break;
4445 }
4446 else
5b634ee0 4447 VEC_pop (const_char_p, argbuf);
ed1f651b
RS
4448 }
4449
b856c15d
RO
4450 set_collect_gcc_options ();
4451
5b634ee0 4452 if (VEC_length (const_char_p, argbuf) > 0)
ed1f651b 4453 {
3279bba6 4454 value = execute ();
ed1f651b
RS
4455 if (value)
4456 return value;
4457 }
4458 /* Reinitialize for a new command, and for a new argument. */
4459 clear_args ();
4460 arg_going = 0;
4461 delete_this_arg = 0;
4462 this_is_output_file = 0;
4463 this_is_library_file = 0;
3beb864c 4464 this_is_linker_script = 0;
a99bf70c 4465 input_from_pipe = 0;
ed1f651b
RS
4466 break;
4467
4468 case '|':
13e7cedb 4469 end_going_arg ();
ed1f651b
RS
4470
4471 /* Use pipe */
4472 obstack_1grow (&obstack, c);
4473 arg_going = 1;
4474 break;
4475
4476 case '\t':
4477 case ' ':
13e7cedb
DH
4478 end_going_arg ();
4479
ed1f651b 4480 /* Reinitialize for a new argument. */
ed1f651b
RS
4481 delete_this_arg = 0;
4482 this_is_output_file = 0;
4483 this_is_library_file = 0;
3beb864c 4484 this_is_linker_script = 0;
ed1f651b
RS
4485 break;
4486
4487 case '%':
4488 switch (c = *p++)
4489 {
4490 case 0:
bdc6b402 4491 fatal_error ("spec %qs invalid", spec);
ed1f651b
RS
4492
4493 case 'b':
14fdc613
MM
4494 if (save_temps_length)
4495 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4496 else
4497 obstack_grow (&obstack, input_basename, basename_length);
2153915d
AO
4498 if (compare_debug < 0)
4499 obstack_grow (&obstack, ".gk", 3);
ed1f651b
RS
4500 arg_going = 1;
4501 break;
4502
ea414c97 4503 case 'B':
14fdc613
MM
4504 if (save_temps_length)
4505 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4506 else
4507 obstack_grow (&obstack, input_basename, suffixed_basename_length);
2153915d
AO
4508 if (compare_debug < 0)
4509 obstack_grow (&obstack, ".gk", 3);
ea414c97
ZW
4510 arg_going = 1;
4511 break;
4512
ed1f651b
RS
4513 case 'd':
4514 delete_this_arg = 2;
4515 break;
4516
4517 /* Dump out the directories specified with LIBRARY_PATH,
004fd4d5
RS
4518 followed by the absolute directories
4519 that we search for startfiles. */
ed1f651b 4520 case 'D':
8cacec76 4521 {
00dcee0c 4522 struct spec_path_info info;
59014d0a 4523
00dcee0c
AM
4524 info.option = "-L";
4525 info.append_len = 0;
76391e5a 4526#ifdef RELATIVE_PREFIX_NOT_LINKDIR
00dcee0c
AM
4527 /* Used on systems which record the specified -L dirs
4528 and use them to search for dynamic linking.
4529 Relative directories always come from -B,
4530 and it is better not to use them for searching
4531 at run time. In particular, stage1 loses. */
4532 info.omit_relative = true;
76391e5a 4533#else
00dcee0c 4534 info.omit_relative = false;
004fd4d5 4535#endif
00dcee0c
AM
4536 info.separate_options = false;
4537
4538 for_each_path (&startfile_prefixes, true, 0, spec_path, &info);
8cacec76 4539 }
ed1f651b
RS
4540 break;
4541
4542 case 'e':
ab87f8c8 4543 /* %efoo means report an error with `foo' as error message
ed1f651b
RS
4544 and don't execute any more commands for this file. */
4545 {
878f32c3 4546 const char *q = p;
ed1f651b 4547 char *buf;
d25a45d4
KH
4548 while (*p != 0 && *p != '\n')
4549 p++;
e1e4cdc4 4550 buf = (char *) alloca (p - q + 1);
ed1f651b
RS
4551 strncpy (buf, q, p - q);
4552 buf[p - q] = 0;
5bc69b92 4553 error ("%s", _(buf));
ed1f651b
RS
4554 return -1;
4555 }
4556 break;
4a88a060 4557 case 'n':
09da1532 4558 /* %nfoo means report a notice with `foo' on stderr. */
4a88a060
JH
4559 {
4560 const char *q = p;
4561 char *buf;
4562 while (*p != 0 && *p != '\n')
4563 p++;
e1e4cdc4 4564 buf = (char *) alloca (p - q + 1);
4a88a060
JH
4565 strncpy (buf, q, p - q);
4566 buf[p - q] = 0;
2dec80c7 4567 inform (0, "%s", _(buf));
4a88a060
JH
4568 if (*p)
4569 p++;
4570 }
4571 break;
ed1f651b 4572
d25a45d4
KH
4573 case 'j':
4574 {
4575 struct stat st;
4576
4977bab6
ZW
4577 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is
4578 defined, and it is not a directory, and it is
4579 writable, use it. Otherwise, treat this like any
4580 other temporary file. */
d25a45d4
KH
4581
4582 if ((!save_temps_flag)
4583 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4584 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4585 {
4586 obstack_grow (&obstack, HOST_BIT_BUCKET,
4587 strlen (HOST_BIT_BUCKET));
4588 delete_this_arg = 0;
4589 arg_going = 1;
4590 break;
4591 }
4592 }
4977bab6
ZW
4593 goto create_temp_file;
4594 case '|':
4595 if (use_pipes)
4596 {
4597 obstack_1grow (&obstack, '-');
4598 delete_this_arg = 0;
4599 arg_going = 1;
4600
4601 /* consume suffix */
a92dd235 4602 while (*p == '.' || ISALNUM ((unsigned char) *p))
4977bab6
ZW
4603 p++;
4604 if (p[0] == '%' && p[1] == 'O')
4605 p += 2;
1d088dee 4606
4977bab6
ZW
4607 break;
4608 }
4609 goto create_temp_file;
4610 case 'm':
4611 if (use_pipes)
4612 {
4613 /* consume suffix */
a92dd235 4614 while (*p == '.' || ISALNUM ((unsigned char) *p))
4977bab6
ZW
4615 p++;
4616 if (p[0] == '%' && p[1] == 'O')
4617 p += 2;
1d088dee 4618
4977bab6
ZW
4619 break;
4620 }
4621 goto create_temp_file;
ed1f651b 4622 case 'g':
d887e808 4623 case 'u':
4401b31c 4624 case 'U':
4977bab6 4625 create_temp_file:
ed1f651b 4626 {
fb266030 4627 struct temp_name *t;
dd75c292 4628 int suffix_length;
878f32c3 4629 const char *suffix = p;
a1d9074c 4630 char *saved_suffix = NULL;
dd75c292 4631
a92dd235 4632 while (*p == '.' || ISALNUM ((unsigned char) *p))
a1d9074c
TT
4633 p++;
4634 suffix_length = p - suffix;
dd75c292
CB
4635 if (p[0] == '%' && p[1] == 'O')
4636 {
cbc54665 4637 p += 2;
dd75c292 4638 /* We don't support extra suffix characters after %O. */
a92dd235 4639 if (*p == '.' || ISALNUM ((unsigned char) *p))
bdc6b402 4640 fatal_error ("spec %qs has invalid %<%%0%c%>", spec, *p);
a1d9074c 4641 if (suffix_length == 0)
45936a85 4642 suffix = TARGET_OBJECT_SUFFIX;
a1d9074c
TT
4643 else
4644 {
4645 saved_suffix
5ed6ace5 4646 = XNEWVEC (char, suffix_length
703ad42b 4647 + strlen (TARGET_OBJECT_SUFFIX));
a1d9074c
TT
4648 strncpy (saved_suffix, suffix, suffix_length);
4649 strcpy (saved_suffix + suffix_length,
45936a85 4650 TARGET_OBJECT_SUFFIX);
a1d9074c 4651 }
45936a85 4652 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
dd75c292 4653 }
589005ff 4654
2153915d
AO
4655 if (compare_debug < 0)
4656 {
4657 suffix = concat (".gk", suffix, NULL);
4658 suffix_length += 3;
4659 }
4660
14fdc613
MM
4661 /* If -save-temps=obj and -o were specified, use that for the
4662 temp file. */
4663 if (save_temps_length)
4664 {
4665 char *tmp;
4666 temp_filename_length
4667 = save_temps_length + suffix_length + 1;
4668 tmp = (char *) alloca (temp_filename_length);
4669 memcpy (tmp, save_temps_prefix, save_temps_length);
4670 memcpy (tmp + save_temps_length, suffix, suffix_length);
4671 tmp[save_temps_length + suffix_length] = '\0';
4672 temp_filename = save_string (tmp,
4673 temp_filename_length + 1);
4674 obstack_grow (&obstack, temp_filename,
4675 temp_filename_length);
4676 arg_going = 1;
4677 delete_this_arg = 0;
4678 break;
4679 }
4680
6afbc885 4681 /* If the gcc_input_filename has the same suffix specified
99f78cdd
IR
4682 for the %g, %u, or %U, and -save-temps is specified,
4683 we could end up using that file as an intermediate
4684 thus clobbering the user's source file (.e.g.,
4685 gcc -save-temps foo.s would clobber foo.s with the
4686 output of cpp0). So check for this condition and
4687 generate a temp file as the intermediate. */
589005ff 4688
99f78cdd
IR
4689 if (save_temps_flag)
4690 {
6ea2b70d 4691 char *tmp;
14fdc613
MM
4692 temp_filename_length = basename_length + suffix_length + 1;
4693 tmp = (char *) alloca (temp_filename_length);
4694 memcpy (tmp, input_basename, basename_length);
4695 memcpy (tmp + basename_length, suffix, suffix_length);
4696 tmp[basename_length + suffix_length] = '\0';
6ea2b70d 4697 temp_filename = tmp;
14fdc613 4698
6afbc885 4699 if (strcmp (temp_filename, gcc_input_filename) != 0)
99f78cdd 4700 {
a9024779 4701#ifndef HOST_LACKS_INODE_NUMBERS
1d088dee 4702 struct stat st_temp;
589005ff 4703
1d088dee
AJ
4704 /* Note, set_input() resets input_stat_set to 0. */
4705 if (input_stat_set == 0)
4706 {
6afbc885
JM
4707 input_stat_set = stat (gcc_input_filename,
4708 &input_stat);
1d088dee
AJ
4709 if (input_stat_set >= 0)
4710 input_stat_set = 1;
4711 }
589005ff 4712
6afbc885 4713 /* If we have the stat for the gcc_input_filename
1d088dee
AJ
4714 and we can do the stat for the temp_filename
4715 then the they could still refer to the same
4716 file if st_dev/st_ino's are the same. */
99f78cdd
IR
4717 if (input_stat_set != 1
4718 || stat (temp_filename, &st_temp) < 0
4719 || input_stat.st_dev != st_temp.st_dev
4720 || input_stat.st_ino != st_temp.st_ino)
a9024779
DS
4721#else
4722 /* Just compare canonical pathnames. */
6afbc885 4723 char* input_realname = lrealpath (gcc_input_filename);
a9024779
DS
4724 char* temp_realname = lrealpath (temp_filename);
4725 bool files_differ = strcmp (input_realname, temp_realname);
4726 free (input_realname);
4727 free (temp_realname);
7904f95f 4728 if (files_differ)
a9024779 4729#endif
589005ff 4730 {
99f78cdd
IR
4731 temp_filename = save_string (temp_filename,
4732 temp_filename_length + 1);
4733 obstack_grow (&obstack, temp_filename,
1d088dee 4734 temp_filename_length);
99f78cdd 4735 arg_going = 1;
d2dff06b 4736 delete_this_arg = 0;
99f78cdd
IR
4737 break;
4738 }
4739 }
4740 }
fb266030
TW
4741
4742 /* See if we already have an association of %g/%u/%U and
4743 suffix. */
4744 for (t = temp_names; t; t = t->next)
dd75c292
CB
4745 if (t->length == suffix_length
4746 && strncmp (t->suffix, suffix, suffix_length) == 0
95456d43 4747 && t->unique == (c == 'u' || c == 'U' || c == 'j'))
fb266030
TW
4748 break;
4749
2297fdf1
TT
4750 /* Make a new association if needed. %u and %j
4751 require one. */
49009afd 4752 if (t == 0 || c == 'u' || c == 'j')
fb266030
TW
4753 {
4754 if (t == 0)
4755 {
e1e4cdc4 4756 t = XNEW (struct temp_name);
fb266030
TW
4757 t->next = temp_names;
4758 temp_names = t;
4759 }
dd75c292 4760 t->length = suffix_length;
2297fdf1
TT
4761 if (saved_suffix)
4762 {
4763 t->suffix = saved_suffix;
4764 saved_suffix = NULL;
4765 }
4766 else
4767 t->suffix = save_string (suffix, suffix_length);
95456d43 4768 t->unique = (c == 'u' || c == 'U' || c == 'j');
dd75c292 4769 temp_filename = make_temp_file (t->suffix);
6aa62cff 4770 temp_filename_length = strlen (temp_filename);
fb266030
TW
4771 t->filename = temp_filename;
4772 t->filename_length = temp_filename_length;
4773 }
4774
a1d9074c
TT
4775 if (saved_suffix)
4776 free (saved_suffix);
4777
fb266030 4778 obstack_grow (&obstack, t->filename, t->filename_length);
b9490a6e 4779 delete_this_arg = 1;
ed1f651b
RS
4780 }
4781 arg_going = 1;
4782 break;
4783
4784 case 'i':
d1bd0ded
GK
4785 if (combine_inputs)
4786 {
5a691e98
RAE
4787 if (at_file_supplied)
4788 {
4789 /* We are going to expand `%i' to `@FILE', where FILE
4790 is a newly-created temporary filename. The filenames
4791 that would usually be expanded in place of %o will be
4792 written to the temporary file. */
4793 char **argv;
4794 int n_files = 0;
4795 int j;
4796
4797 for (i = 0; i < n_infiles; i++)
4798 if (compile_input_file_p (&infiles[i]))
4799 n_files++;
4800
4801 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
4802
4803 /* Copy the strings over. */
4804 for (i = 0, j = 0; i < n_infiles; i++)
4805 if (compile_input_file_p (&infiles[i]))
4806 {
4807 argv[j] = CONST_CAST (char *, infiles[i].name);
4808 infiles[i].compiled = true;
4809 j++;
4810 }
4811 argv[j] = NULL;
4812
4813 create_at_file (argv);
4814 }
4815 else
4816 for (i = 0; (int) i < n_infiles; i++)
4817 if (compile_input_file_p (&infiles[i]))
0855eab7
CT
4818 {
4819 store_arg (infiles[i].name, 0, 0);
4820 infiles[i].compiled = true;
4821 }
d1bd0ded
GK
4822 }
4823 else
4824 {
6afbc885
JM
4825 obstack_grow (&obstack, gcc_input_filename,
4826 input_filename_length);
d1bd0ded
GK
4827 arg_going = 1;
4828 }
ed1f651b
RS
4829 break;
4830
8eebb258 4831 case 'I':
2d879387 4832 {
00dcee0c 4833 struct spec_path_info info;
2d879387 4834
2b6dd222
JM
4835 if (multilib_dir)
4836 {
4837 do_spec_1 ("-imultilib", 1, NULL);
4838 /* Make this a separate argument. */
4839 do_spec_1 (" ", 0, NULL);
4840 do_spec_1 (multilib_dir, 1, NULL);
4841 do_spec_1 (" ", 0, NULL);
4842 }
4843
2d879387
JW
4844 if (gcc_exec_prefix)
4845 {
6496a589 4846 do_spec_1 ("-iprefix", 1, NULL);
2d879387 4847 /* Make this a separate argument. */
6496a589
KG
4848 do_spec_1 (" ", 0, NULL);
4849 do_spec_1 (gcc_exec_prefix, 1, NULL);
4850 do_spec_1 (" ", 0, NULL);
2d879387
JW
4851 }
4852
e7f13528
GP
4853 if (target_system_root_changed ||
4854 (target_system_root && target_sysroot_hdrs_suffix))
047d636f
DJ
4855 {
4856 do_spec_1 ("-isysroot", 1, NULL);
4857 /* Make this a separate argument. */
4858 do_spec_1 (" ", 0, NULL);
4859 do_spec_1 (target_system_root, 1, NULL);
e7f13528
GP
4860 if (target_sysroot_hdrs_suffix)
4861 do_spec_1 (target_sysroot_hdrs_suffix, 1, NULL);
047d636f
DJ
4862 do_spec_1 (" ", 0, NULL);
4863 }
4864
00dcee0c
AM
4865 info.option = "-isystem";
4866 info.append = "include";
4867 info.append_len = strlen (info.append);
4868 info.omit_relative = false;
4869 info.separate_options = true;
4870
4871 for_each_path (&include_prefixes, false, info.append_len,
4872 spec_path, &info);
f686ec05
JM
4873
4874 info.append = "include-fixed";
14da6073
JM
4875 if (*sysroot_hdrs_suffix_spec)
4876 info.append = concat (info.append, dir_separator_str,
4877 multilib_dir, NULL);
f686ec05
JM
4878 info.append_len = strlen (info.append);
4879 for_each_path (&include_prefixes, false, info.append_len,
4880 spec_path, &info);
2d879387 4881 }
8eebb258
RS
4882 break;
4883
ed1f651b 4884 case 'o':
15c5edb9
TT
4885 {
4886 int max = n_infiles;
15c5edb9 4887 max += lang_specific_extra_outfiles;
08dc830e 4888
2091ff66
NF
4889 if (HAVE_GNU_LD && at_file_supplied)
4890 {
4891 /* We are going to expand `%o' to `@FILE', where FILE
4892 is a newly-created temporary filename. The filenames
4893 that would usually be expanded in place of %o will be
4894 written to the temporary file. */
4895
2091ff66 4896 char **argv;
5a691e98 4897 int n_files, j;
2091ff66
NF
4898
4899 /* Convert OUTFILES into a form suitable for writeargv. */
4900
4901 /* Determine how many are non-NULL. */
4902 for (n_files = 0, i = 0; i < max; i++)
4903 n_files += outfiles[i] != NULL;
4904
e1e4cdc4 4905 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
2091ff66
NF
4906
4907 /* Copy the strings over. */
4908 for (i = 0, j = 0; i < max; i++)
4909 if (outfiles[i])
4910 {
b1d5455a 4911 argv[j] = CONST_CAST (char *, outfiles[i]);
2091ff66
NF
4912 j++;
4913 }
4914 argv[j] = NULL;
4915
5a691e98 4916 create_at_file (argv);
2091ff66
NF
4917 }
4918 else
4919 for (i = 0; i < max; i++)
4920 if (outfiles[i])
4921 store_arg (outfiles[i], 0, 0);
15c5edb9
TT
4922 break;
4923 }
ed1f651b 4924
ed7dae04 4925 case 'O':
45936a85 4926 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
ed7dae04
RK
4927 arg_going = 1;
4928 break;
4929
ed1f651b
RS
4930 case 's':
4931 this_is_library_file = 1;
4932 break;
4933
3beb864c
NC
4934 case 'T':
4935 this_is_linker_script = 1;
4936 break;
4937
17211ab5
GK
4938 case 'V':
4939 outfiles[input_file_number] = NULL;
4940 break;
4941
ed1f651b
RS
4942 case 'w':
4943 this_is_output_file = 1;
4944 break;
4945
4946 case 'W':
4947 {
5b634ee0 4948 unsigned int cur_index = VEC_length (const_char_p, argbuf);
ed1f651b
RS
4949 /* Handle the {...} following the %W. */
4950 if (*p != '{')
bdc6b402 4951 fatal_error ("spec %qs has invalid %<%%W%c%>", spec, *p);
ed1f651b
RS
4952 p = handle_braces (p + 1);
4953 if (p == 0)
4954 return -1;
13e7cedb 4955 end_going_arg ();
ed1f651b
RS
4956 /* If any args were output, mark the last one for deletion
4957 on failure. */
5b634ee0
JM
4958 if (VEC_length (const_char_p, argbuf) != cur_index)
4959 record_temp_file (VEC_last (const_char_p, argbuf), 0, 1);
ed1f651b
RS
4960 break;
4961 }
4962
4963 /* %x{OPTION} records OPTION for %X to output. */
4964 case 'x':
4965 {
878f32c3 4966 const char *p1 = p;
ed1f651b
RS
4967 char *string;
4968
4969 /* Skip past the option value and make a copy. */
4970 if (*p != '{')
bdc6b402 4971 fatal_error ("spec %qs has invalid %<%%x%c%>", spec, *p);
ed1f651b
RS
4972 while (*p++ != '}')
4973 ;
4974 string = save_string (p1 + 1, p - p1 - 2);
4975
4976 /* See if we already recorded this option. */
4977 for (i = 0; i < n_linker_options; i++)
4978 if (! strcmp (string, linker_options[i]))
4979 {
4980 free (string);
4981 return 0;
4982 }
4983
4984 /* This option is new; add it. */
b8468bc7 4985 add_linker_option (string, strlen (string));
ed1f651b
RS
4986 }
4987 break;
4988
368dfd3a 4989 /* Dump out the options accumulated previously using %x. */
ed1f651b
RS
4990 case 'X':
4991 for (i = 0; i < n_linker_options; i++)
4992 {
6496a589 4993 do_spec_1 (linker_options[i], 1, NULL);
ed1f651b 4994 /* Make each accumulated option a separate argument. */
6496a589 4995 do_spec_1 (" ", 0, NULL);
ed1f651b
RS
4996 }
4997 break;
4998
c9ebacb8
RS
4999 /* Dump out the options accumulated previously using -Wa,. */
5000 case 'Y':
5001 for (i = 0; i < n_assembler_options; i++)
5002 {
6496a589 5003 do_spec_1 (assembler_options[i], 1, NULL);
c9ebacb8 5004 /* Make each accumulated option a separate argument. */
6496a589 5005 do_spec_1 (" ", 0, NULL);
c9ebacb8
RS
5006 }
5007 break;
5008
57cb9b60
JW
5009 /* Dump out the options accumulated previously using -Wp,. */
5010 case 'Z':
5011 for (i = 0; i < n_preprocessor_options; i++)
5012 {
6496a589 5013 do_spec_1 (preprocessor_options[i], 1, NULL);
57cb9b60 5014 /* Make each accumulated option a separate argument. */
6496a589 5015 do_spec_1 (" ", 0, NULL);
57cb9b60
JW
5016 }
5017 break;
5018
ed1f651b
RS
5019 /* Here are digits and numbers that just process
5020 a certain constant string as a spec. */
5021
5022 case '1':
6496a589 5023 value = do_spec_1 (cc1_spec, 0, NULL);
3279bba6
RS
5024 if (value != 0)
5025 return value;
ed1f651b
RS
5026 break;
5027
5028 case '2':
6496a589 5029 value = do_spec_1 (cc1plus_spec, 0, NULL);
3279bba6
RS
5030 if (value != 0)
5031 return value;
ed1f651b
RS
5032 break;
5033
5034 case 'a':
6496a589 5035 value = do_spec_1 (asm_spec, 0, NULL);
3279bba6
RS
5036 if (value != 0)
5037 return value;
ed1f651b
RS
5038 break;
5039
5040 case 'A':
6496a589 5041 value = do_spec_1 (asm_final_spec, 0, NULL);
3279bba6
RS
5042 if (value != 0)
5043 return value;
ed1f651b
RS
5044 break;
5045
ed1f651b 5046 case 'C':
a9374841 5047 {
83182544 5048 const char *const spec
589005ff
KH
5049 = (input_file_compiler->cpp_spec
5050 ? input_file_compiler->cpp_spec
a9374841 5051 : cpp_spec);
6496a589 5052 value = do_spec_1 (spec, 0, NULL);
a9374841
MM
5053 if (value != 0)
5054 return value;
5055 }
ed1f651b
RS
5056 break;
5057
5058 case 'E':
6496a589 5059 value = do_spec_1 (endfile_spec, 0, NULL);
3279bba6
RS
5060 if (value != 0)
5061 return value;
ed1f651b
RS
5062 break;
5063
5064 case 'l':
6496a589 5065 value = do_spec_1 (link_spec, 0, NULL);
3279bba6
RS
5066 if (value != 0)
5067 return value;
ed1f651b
RS
5068 break;
5069
5070 case 'L':
6496a589 5071 value = do_spec_1 (lib_spec, 0, NULL);
3279bba6
RS
5072 if (value != 0)
5073 return value;
ed1f651b
RS
5074 break;
5075
68d69835 5076 case 'G':
6496a589 5077 value = do_spec_1 (libgcc_spec, 0, NULL);
68d69835
JM
5078 if (value != 0)
5079 return value;
5080 break;
5081
4977bab6
ZW
5082 case 'R':
5083 /* We assume there is a directory
5084 separator at the end of this string. */
5085 if (target_system_root)
1d088dee
AJ
5086 {
5087 obstack_grow (&obstack, target_system_root,
e7f13528
GP
5088 strlen (target_system_root));
5089 if (target_sysroot_suffix)
1d088dee 5090 obstack_grow (&obstack, target_sysroot_suffix,
e7f13528
GP
5091 strlen (target_sysroot_suffix));
5092 }
4977bab6
ZW
5093 break;
5094
ed1f651b 5095 case 'S':
6496a589 5096 value = do_spec_1 (startfile_spec, 0, NULL);
3279bba6
RS
5097 if (value != 0)
5098 return value;
ed1f651b
RS
5099 break;
5100
5101 /* Here we define characters other than letters and digits. */
5102
5103 case '{':
5104 p = handle_braces (p);
5105 if (p == 0)
5106 return -1;
5107 break;
5108
f3226a90
JT
5109 case ':':
5110 p = handle_spec_function (p);
5111 if (p == 0)
5112 return -1;
5113 break;
5114
ed1f651b
RS
5115 case '%':
5116 obstack_1grow (&obstack, '%');
5117 break;
5118
589005ff
KH
5119 case '.':
5120 {
5121 unsigned len = 0;
11972f66 5122
589005ff
KH
5123 while (p[len] && p[len] != ' ' && p[len] != '%')
5124 len++;
5125 suffix_subst = save_string (p - 1, len + 1);
5126 p += len;
5127 }
11972f66 5128 break;
589005ff 5129
4977bab6
ZW
5130 /* Henceforth ignore the option(s) matching the pattern
5131 after the %<. */
5132 case '<':
5133 {
5134 unsigned len = 0;
5135 int have_wildcard = 0;
5136 int i;
5137
5138 while (p[len] && p[len] != ' ' && p[len] != '\t')
5139 len++;
5140
5141 if (p[len-1] == '*')
5142 have_wildcard = 1;
5143
5144 for (i = 0; i < n_switches; i++)
5145 if (!strncmp (switches[i].part1, p, len - have_wildcard)
5146 && (have_wildcard || switches[i].part1[len] == '\0'))
5147 {
3371362c 5148 switches[i].live_cond |= SWITCH_IGNORE;
4977bab6
ZW
5149 switches[i].validated = 1;
5150 }
5151
5152 p += len;
5153 }
5154 break;
5155
ed1f651b 5156 case '*':
3ac63d94
NC
5157 if (soft_matched_part)
5158 {
6496a589
KG
5159 do_spec_1 (soft_matched_part, 1, NULL);
5160 do_spec_1 (" ", 0, NULL);
3ac63d94
NC
5161 }
5162 else
5163 /* Catch the case where a spec string contains something like
454ff5cb 5164 '%{foo:%*}'. i.e. there is no * in the pattern on the left
3ac63d94 5165 hand side of the :. */
bdc6b402 5166 error ("spec failure: %<%%*%> has not been initialized by pattern match");
ed1f651b
RS
5167 break;
5168
5169 /* Process a string found as the value of a spec given by name.
5170 This feature allows individual machine descriptions
4089dfab
JL
5171 to add and use their own specs.
5172 %[...] modifies -D options the way %P does;
5173 %(...) uses the spec unmodified. */
5174 case '[':
2dec80c7 5175 warning (0, "use of obsolete %%[ operator in specs");
ed1f651b 5176 case '(':
ed1f651b 5177 {
878f32c3 5178 const char *name = p;
ed1f651b
RS
5179 struct spec_list *sl;
5180 int len;
5181
5182 /* The string after the S/P is the name of a spec that is to be
0f41302f 5183 processed. */
4089dfab 5184 while (*p && *p != ')' && *p != ']')
ed1f651b
RS
5185 p++;
5186
3ac63d94 5187 /* See if it's in the list. */
ed1f651b 5188 for (len = p - name, sl = specs; sl; sl = sl->next)
79aff5ac 5189 if (sl->name_len == len && !strncmp (sl->name, name, len))
ed1f651b 5190 {
79aff5ac 5191 name = *(sl->ptr_spec);
20df0482 5192#ifdef DEBUG_SPECS
2dec80c7 5193 fnotice (stderr, "Processing spec %c%s%c, which is '%s'\n",
ab87f8c8 5194 c, sl->name, (c == '(') ? ')' : ']', name);
20df0482 5195#endif
ed1f651b
RS
5196 break;
5197 }
5198
5199 if (sl)
5200 {
4089dfab
JL
5201 if (c == '(')
5202 {
6496a589 5203 value = do_spec_1 (name, 0, NULL);
4089dfab
JL
5204 if (value != 0)
5205 return value;
5206 }
5207 else
5208 {
e1e4cdc4 5209 char *x = (char *) alloca (strlen (name) * 2 + 1);
4089dfab 5210 char *buf = x;
878f32c3 5211 const char *y = name;
4089dfab
JL
5212 int flag = 0;
5213
5214 /* Copy all of NAME into BUF, but put __ after
3ac63d94 5215 every -D and at the end of each arg. */
4089dfab
JL
5216 while (1)
5217 {
5218 if (! strncmp (y, "-D", 2))
5219 {
5220 *x++ = '-';
5221 *x++ = 'D';
5222 *x++ = '_';
5223 *x++ = '_';
5224 y += 2;
5225 flag = 1;
5226 continue;
5227 }
d25a45d4
KH
5228 else if (flag
5229 && (*y == ' ' || *y == '\t' || *y == '='
5230 || *y == '}' || *y == 0))
4089dfab
JL
5231 {
5232 *x++ = '_';
5233 *x++ = '_';
5234 flag = 0;
5235 }
d25a45d4 5236 if (*y == 0)
4089dfab
JL
5237 break;
5238 else
5239 *x++ = *y++;
5240 }
5241 *x = 0;
5242
6496a589 5243 value = do_spec_1 (buf, 0, NULL);
4089dfab
JL
5244 if (value != 0)
5245 return value;
5246 }
ed1f651b 5247 }
b3865ca9 5248
4089dfab 5249 /* Discard the closing paren or bracket. */
b3865ca9
RS
5250 if (*p)
5251 p++;
ed1f651b
RS
5252 }
5253 break;
5254
5255 default:
bdc6b402 5256 error ("spec failure: unrecognized spec option %qc", c);
3ac63d94 5257 break;
ed1f651b
RS
5258 }
5259 break;
5260
5261 case '\\':
5262 /* Backslash: treat next character as ordinary. */
5263 c = *p++;
5264
938d968e 5265 /* Fall through. */
ed1f651b
RS
5266 default:
5267 /* Ordinary character: put it into the current argument. */
5268 obstack_1grow (&obstack, c);
5269 arg_going = 1;
5270 }
5271
f3226a90
JT
5272 /* End of string. If we are processing a spec function, we need to
5273 end any pending argument. */
13e7cedb
DH
5274 if (processing_spec_function)
5275 end_going_arg ();
f3226a90 5276
d25a45d4 5277 return 0;
ed1f651b
RS
5278}
5279
f3226a90
JT
5280/* Look up a spec function. */
5281
5282static const struct spec_function *
1d088dee 5283lookup_spec_function (const char *name)
f3226a90 5284{
f3226a90 5285 const struct spec_function *sf;
f3226a90 5286
4c360e1f
RE
5287 for (sf = static_spec_functions; sf->name != NULL; sf++)
5288 if (strcmp (sf->name, name) == 0)
5289 return sf;
f3226a90
JT
5290
5291 return NULL;
5292}
5293
5294/* Evaluate a spec function. */
5295
5296static const char *
1d088dee 5297eval_spec_function (const char *func, const char *args)
f3226a90
JT
5298{
5299 const struct spec_function *sf;
5300 const char *funcval;
5301
5302 /* Saved spec processing context. */
5b634ee0 5303 VEC(const_char_p,heap) *save_argbuf;
f3226a90
JT
5304
5305 int save_arg_going;
5306 int save_delete_this_arg;
5307 int save_this_is_output_file;
5308 int save_this_is_library_file;
5309 int save_input_from_pipe;
3beb864c 5310 int save_this_is_linker_script;
f3226a90
JT
5311 const char *save_suffix_subst;
5312
5313
5314 sf = lookup_spec_function (func);
5315 if (sf == NULL)
bdc6b402 5316 fatal_error ("unknown spec function %qs", func);
f3226a90
JT
5317
5318 /* Push the spec processing context. */
f3226a90
JT
5319 save_argbuf = argbuf;
5320
5321 save_arg_going = arg_going;
5322 save_delete_this_arg = delete_this_arg;
5323 save_this_is_output_file = this_is_output_file;
5324 save_this_is_library_file = this_is_library_file;
3beb864c 5325 save_this_is_linker_script = this_is_linker_script;
f3226a90
JT
5326 save_input_from_pipe = input_from_pipe;
5327 save_suffix_subst = suffix_subst;
5328
5329 /* Create a new spec processing context, and build the function
5330 arguments. */
5331
5332 alloc_args ();
5333 if (do_spec_2 (args) < 0)
bdc6b402 5334 fatal_error ("error in args to spec function %qs", func);
f3226a90
JT
5335
5336 /* argbuf_index is an index for the next argument to be inserted, and
5337 so contains the count of the args already inserted. */
5338
5b634ee0
JM
5339 funcval = (*sf->func) (VEC_length (const_char_p, argbuf),
5340 VEC_address (const_char_p, argbuf));
f3226a90
JT
5341
5342 /* Pop the spec processing context. */
5b634ee0 5343 VEC_free (const_char_p, heap, argbuf);
f3226a90
JT
5344 argbuf = save_argbuf;
5345
5346 arg_going = save_arg_going;
5347 delete_this_arg = save_delete_this_arg;
5348 this_is_output_file = save_this_is_output_file;
5349 this_is_library_file = save_this_is_library_file;
3beb864c 5350 this_is_linker_script = save_this_is_linker_script;
f3226a90
JT
5351 input_from_pipe = save_input_from_pipe;
5352 suffix_subst = save_suffix_subst;
5353
5354 return funcval;
5355}
5356
5357/* Handle a spec function call of the form:
5358
5359 %:function(args)
5360
5361 ARGS is processed as a spec in a separate context and split into an
5362 argument vector in the normal fashion. The function returns a string
5363 containing a spec which we then process in the caller's context, or
5364 NULL if no processing is required. */
5365
5366static const char *
1d088dee 5367handle_spec_function (const char *p)
f3226a90
JT
5368{
5369 char *func, *args;
5370 const char *endp, *funcval;
5371 int count;
5372
5373 processing_spec_function++;
5374
5375 /* Get the function name. */
5376 for (endp = p; *endp != '\0'; endp++)
5377 {
5378 if (*endp == '(') /* ) */
5379 break;
5380 /* Only allow [A-Za-z0-9], -, and _ in function names. */
5381 if (!ISALNUM (*endp) && !(*endp == '-' || *endp == '_'))
2dec80c7 5382 fatal_error ("malformed spec function name");
f3226a90
JT
5383 }
5384 if (*endp != '(') /* ) */
2dec80c7 5385 fatal_error ("no arguments for spec function");
f3226a90
JT
5386 func = save_string (p, endp - p);
5387 p = ++endp;
5388
5389 /* Get the arguments. */
5390 for (count = 0; *endp != '\0'; endp++)
5391 {
5392 /* ( */
5393 if (*endp == ')')
5394 {
5395 if (count == 0)
5396 break;
5397 count--;
5398 }
5399 else if (*endp == '(') /* ) */
5400 count++;
5401 }
5402 /* ( */
5403 if (*endp != ')')
2dec80c7 5404 fatal_error ("malformed spec function arguments");
f3226a90
JT
5405 args = save_string (p, endp - p);
5406 p = ++endp;
5407
5408 /* p now points to just past the end of the spec function expression. */
5409
5410 funcval = eval_spec_function (func, args);
5411 if (funcval != NULL && do_spec_1 (funcval, 0, NULL) < 0)
5412 p = NULL;
5413
5414 free (func);
5415 free (args);
5416
5417 processing_spec_function--;
5418
5419 return p;
5420}
5421
4977bab6
ZW
5422/* Inline subroutine of handle_braces. Returns true if the current
5423 input suffix matches the atom bracketed by ATOM and END_ATOM. */
5424static inline bool
1d088dee 5425input_suffix_matches (const char *atom, const char *end_atom)
4977bab6
ZW
5426{
5427 return (input_suffix
5428 && !strncmp (input_suffix, atom, end_atom - atom)
5429 && input_suffix[end_atom - atom] == '\0');
5430}
ed1f651b 5431
48137d59
GK
5432/* Subroutine of handle_braces. Returns true if the current
5433 input file's spec name matches the atom bracketed by ATOM and END_ATOM. */
5434static bool
5435input_spec_matches (const char *atom, const char *end_atom)
5436{
5437 return (input_file_compiler
5438 && input_file_compiler->suffix
5439 && input_file_compiler->suffix[0] != '\0'
5440 && !strncmp (input_file_compiler->suffix + 1, atom,
5441 end_atom - atom)
5442 && input_file_compiler->suffix[end_atom - atom + 1] == '\0');
5443}
5444
953ff289 5445/* Subroutine of handle_braces. Returns true if a switch
4977bab6
ZW
5446 matching the atom bracketed by ATOM and END_ATOM appeared on the
5447 command line. */
953ff289 5448static bool
1d088dee 5449switch_matches (const char *atom, const char *end_atom, int starred)
ed1f651b 5450{
4977bab6
ZW
5451 int i;
5452 int len = end_atom - atom;
5453 int plen = starred ? len : -1;
ed1f651b 5454
4977bab6
ZW
5455 for (i = 0; i < n_switches; i++)
5456 if (!strncmp (switches[i].part1, atom, len)
5457 && (starred || switches[i].part1[len] == '\0')
5458 && check_live_switch (i, plen))
5459 return true;
8097c429 5460
4977bab6
ZW
5461 return false;
5462}
ed1f651b 5463
4977bab6
ZW
5464/* Inline subroutine of handle_braces. Mark all of the switches which
5465 match ATOM (extends to END_ATOM; STARRED indicates whether there
5466 was a star after the atom) for later processing. */
5467static inline void
1d088dee 5468mark_matching_switches (const char *atom, const char *end_atom, int starred)
4977bab6
ZW
5469{
5470 int i;
5471 int len = end_atom - atom;
5472 int plen = starred ? len : -1;
5473
5474 for (i = 0; i < n_switches; i++)
5475 if (!strncmp (switches[i].part1, atom, len)
5476 && (starred || switches[i].part1[len] == '\0')
5477 && check_live_switch (i, plen))
5478 switches[i].ordering = 1;
5479}
9bf09437 5480
4977bab6
ZW
5481/* Inline subroutine of handle_braces. Process all the currently
5482 marked switches through give_switch, and clear the marks. */
5483static inline void
1d088dee 5484process_marked_switches (void)
4977bab6
ZW
5485{
5486 int i;
ed1f651b 5487
4977bab6
ZW
5488 for (i = 0; i < n_switches; i++)
5489 if (switches[i].ordering == 1)
5490 {
5491 switches[i].ordering = 0;
5492 give_switch (i, 0);
5493 }
5494}
ed1f651b 5495
4977bab6
ZW
5496/* Handle a %{ ... } construct. P points just inside the leading {.
5497 Returns a pointer one past the end of the brace block, or 0
5498 if we call do_spec_1 and that returns -1. */
ed1f651b 5499
4977bab6 5500static const char *
1d088dee 5501handle_braces (const char *p)
4977bab6
ZW
5502{
5503 const char *atom, *end_atom;
5504 const char *d_atom = NULL, *d_end_atom = NULL;
3b5edfee 5505 const char *orig = p;
8097c429 5506
4977bab6 5507 bool a_is_suffix;
48137d59 5508 bool a_is_spectype;
4977bab6
ZW
5509 bool a_is_starred;
5510 bool a_is_negated;
5511 bool a_matched;
9bf09437 5512
4977bab6
ZW
5513 bool a_must_be_last = false;
5514 bool ordered_set = false;
5515 bool disjunct_set = false;
5516 bool disj_matched = false;
5517 bool disj_starred = true;
5518 bool n_way_choice = false;
5519 bool n_way_matched = false;
5520
5521#define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
9bf09437 5522
4977bab6 5523 do
ed1f651b 5524 {
4977bab6 5525 if (a_must_be_last)
3b5edfee 5526 goto invalid;
9bf09437 5527
4977bab6 5528 /* Scan one "atom" (S in the description above of %{}, possibly
48137d59
GK
5529 with '!', '.', '@', ',', or '*' modifiers). */
5530 a_matched = false;
5531 a_is_suffix = false;
5532 a_is_starred = false;
5533 a_is_negated = false;
5534 a_is_spectype = false;
9218435e 5535
4977bab6
ZW
5536 SKIP_WHITE();
5537 if (*p == '!')
5538 p++, a_is_negated = true;
ed1f651b 5539
4977bab6
ZW
5540 SKIP_WHITE();
5541 if (*p == '.')
5542 p++, a_is_suffix = true;
48137d59
GK
5543 else if (*p == ',')
5544 p++, a_is_spectype = true;
ed1f651b 5545
4977bab6
ZW
5546 atom = p;
5547 while (ISIDNUM(*p) || *p == '-' || *p == '+' || *p == '='
cde26509 5548 || *p == ',' || *p == '.' || *p == '@')
4977bab6
ZW
5549 p++;
5550 end_atom = p;
ed1f651b 5551
4977bab6
ZW
5552 if (*p == '*')
5553 p++, a_is_starred = 1;
196a37f4 5554
4977bab6 5555 SKIP_WHITE();
3b5edfee 5556 switch (*p)
4977bab6 5557 {
3b5edfee 5558 case '&': case '}':
4977bab6
ZW
5559 /* Substitute the switch(es) indicated by the current atom. */
5560 ordered_set = true;
5561 if (disjunct_set || n_way_choice || a_is_negated || a_is_suffix
48137d59 5562 || a_is_spectype || atom == end_atom)
3b5edfee 5563 goto invalid;
ed1f651b 5564
4977bab6
ZW
5565 mark_matching_switches (atom, end_atom, a_is_starred);
5566
5567 if (*p == '}')
5568 process_marked_switches ();
3b5edfee
NS
5569 break;
5570
5571 case '|': case ':':
4977bab6
ZW
5572 /* Substitute some text if the current atom appears as a switch
5573 or suffix. */
5574 disjunct_set = true;
5575 if (ordered_set)
3b5edfee 5576 goto invalid;
ed1f651b 5577
4977bab6 5578 if (atom == end_atom)
ed1f651b 5579 {
4977bab6 5580 if (!n_way_choice || disj_matched || *p == '|'
b8698a0f 5581 || a_is_negated || a_is_suffix || a_is_spectype
48137d59 5582 || a_is_starred)
3b5edfee 5583 goto invalid;
4977bab6
ZW
5584
5585 /* An empty term may appear as the last choice of an
5586 N-way choice set; it means "otherwise". */
5587 a_must_be_last = true;
5588 disj_matched = !n_way_matched;
5589 disj_starred = false;
ed1f651b 5590 }
4977bab6 5591 else
ed1f651b 5592 {
48137d59
GK
5593 if ((a_is_suffix || a_is_spectype) && a_is_starred)
5594 goto invalid;
3beb864c 5595
48137d59
GK
5596 if (!a_is_starred)
5597 disj_starred = false;
5598
5599 /* Don't bother testing this atom if we already have a
5600 match. */
5601 if (!disj_matched && !n_way_matched)
5602 {
5603 if (a_is_suffix)
5604 a_matched = input_suffix_matches (atom, end_atom);
5605 else if (a_is_spectype)
5606 a_matched = input_spec_matches (atom, end_atom);
5607 else
5608 a_matched = switch_matches (atom, end_atom, a_is_starred);
3beb864c 5609
48137d59
GK
5610 if (a_matched != a_is_negated)
5611 {
5612 disj_matched = true;
5613 d_atom = atom;
5614 d_end_atom = end_atom;
5615 }
5616 }
ed1f651b 5617 }
ed1f651b 5618
4977bab6 5619 if (*p == ':')
ed1f651b 5620 {
4977bab6
ZW
5621 /* Found the body, that is, the text to substitute if the
5622 current disjunction matches. */
5623 p = process_brace_body (p + 1, d_atom, d_end_atom, disj_starred,
5624 disj_matched && !n_way_matched);
5625 if (p == 0)
5626 return 0;
ed1f651b 5627
4977bab6
ZW
5628 /* If we have an N-way choice, reset state for the next
5629 disjunction. */
5630 if (*p == ';')
ed1f651b 5631 {
4977bab6
ZW
5632 n_way_choice = true;
5633 n_way_matched |= disj_matched;
5634 disj_matched = false;
5635 disj_starred = true;
5636 d_atom = d_end_atom = NULL;
ed1f651b
RS
5637 }
5638 }
3b5edfee
NS
5639 break;
5640
5641 default:
5642 goto invalid;
ed1f651b 5643 }
4977bab6
ZW
5644 }
5645 while (*p++ != '}');
ed1f651b 5646
4977bab6 5647 return p;
7904f95f 5648
3b5edfee 5649 invalid:
bdc6b402 5650 fatal_error ("braced spec %qs is invalid at %qc", orig, *p);
7904f95f 5651
4977bab6
ZW
5652#undef SKIP_WHITE
5653}
5654
5655/* Subroutine of handle_braces. Scan and process a brace substitution body
5656 (X in the description of %{} syntax). P points one past the colon;
5657 ATOM and END_ATOM bracket the first atom which was found to be true
5658 (present) in the current disjunction; STARRED indicates whether all
5659 the atoms in the current disjunction were starred (for syntax validation);
5660 MATCHED indicates whether the disjunction matched or not, and therefore
5661 whether or not the body is to be processed through do_spec_1 or just
5662 skipped. Returns a pointer to the closing } or ;, or 0 if do_spec_1
5663 returns -1. */
5664
5665static const char *
1d088dee
AJ
5666process_brace_body (const char *p, const char *atom, const char *end_atom,
5667 int starred, int matched)
4977bab6
ZW
5668{
5669 const char *body, *end_body;
5670 unsigned int nesting_level;
5671 bool have_subst = false;
5672
5673 /* Locate the closing } or ;, honoring nested braces.
5674 Trim trailing whitespace. */
5675 body = p;
5676 nesting_level = 1;
5677 for (;;)
5678 {
5679 if (*p == '{')
5680 nesting_level++;
5681 else if (*p == '}')
ed1f651b 5682 {
4977bab6
ZW
5683 if (!--nesting_level)
5684 break;
ed1f651b 5685 }
4977bab6
ZW
5686 else if (*p == ';' && nesting_level == 1)
5687 break;
5688 else if (*p == '%' && p[1] == '*' && nesting_level == 1)
5689 have_subst = true;
5690 else if (*p == '\0')
3b5edfee 5691 goto invalid;
4977bab6 5692 p++;
ed1f651b 5693 }
1d088dee 5694
4977bab6
ZW
5695 end_body = p;
5696 while (end_body[-1] == ' ' || end_body[-1] == '\t')
5697 end_body--;
ed1f651b 5698
4977bab6 5699 if (have_subst && !starred)
3b5edfee 5700 goto invalid;
9bf09437 5701
4977bab6 5702 if (matched)
196a37f4 5703 {
4977bab6
ZW
5704 /* Copy the substitution body to permanent storage and execute it.
5705 If have_subst is false, this is a simple matter of running the
5706 body through do_spec_1... */
5707 char *string = save_string (body, end_body - body);
5708 if (!have_subst)
5709 {
5710 if (do_spec_1 (string, 0, NULL) < 0)
5711 return 0;
5712 }
5713 else
5714 {
5715 /* ... but if have_subst is true, we have to process the
5716 body once for each matching switch, with %* set to the
5717 variant part of the switch. */
5718 unsigned int hard_match_len = end_atom - atom;
5719 int i;
196a37f4 5720
4977bab6
ZW
5721 for (i = 0; i < n_switches; i++)
5722 if (!strncmp (switches[i].part1, atom, hard_match_len)
5723 && check_live_switch (i, hard_match_len))
5724 {
5725 if (do_spec_1 (string, 0,
5726 &switches[i].part1[hard_match_len]) < 0)
5727 return 0;
5728 /* Pass any arguments this switch has. */
5729 give_switch (i, 1);
5730 suffix_subst = NULL;
5731 }
5732 }
10ebf5fe
NB
5733 }
5734
4977bab6 5735 return p;
3b5edfee
NS
5736
5737 invalid:
bdc6b402 5738 fatal_error ("braced spec body %qs is invalid", body);
ed1f651b 5739}
f5b0eb4e 5740\f
6c396fb5
RK
5741/* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5742 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5743 spec, or -1 if either exact match or %* is used.
f5b0eb4e
RK
5744
5745 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5746 whose value does not begin with "no-" is obsoleted by the same value
5747 with the "no-", similarly for a switch with the "no-" prefix. */
5748
5749static int
1d088dee 5750check_live_switch (int switchnum, int prefix_length)
f5b0eb4e 5751{
878f32c3 5752 const char *name = switches[switchnum].part1;
f5b0eb4e
RK
5753 int i;
5754
2153915d
AO
5755 /* If we already processed this switch and determined if it was
5756 live or not, return our past determination. */
5757 if (switches[switchnum].live_cond != 0)
5758 return ((switches[switchnum].live_cond & SWITCH_LIVE) != 0
5759 && (switches[switchnum].live_cond & SWITCH_FALSE) == 0
94b90527
JJ
5760 && (switches[switchnum].live_cond & SWITCH_IGNORE_PERMANENTLY)
5761 == 0);
2153915d 5762
6c396fb5 5763 /* In the common case of {<at-most-one-letter>*}, a negating
f5b0eb4e
RK
5764 switch would always match, so ignore that case. We will just
5765 send the conflicting switches to the compiler phase. */
6c396fb5 5766 if (prefix_length >= 0 && prefix_length <= 1)
f5b0eb4e
RK
5767 return 1;
5768
f5b0eb4e
RK
5769 /* Now search for duplicate in a manner that depends on the name. */
5770 switch (*name)
5771 {
5772 case 'O':
d25a45d4
KH
5773 for (i = switchnum + 1; i < n_switches; i++)
5774 if (switches[i].part1[0] == 'O')
5775 {
5776 switches[switchnum].validated = 1;
5777 switches[switchnum].live_cond = SWITCH_FALSE;
5778 return 0;
5779 }
f5b0eb4e 5780 break;
ed1f651b 5781
f5b0eb4e 5782 case 'W': case 'f': case 'm':
6c396fb5 5783 if (! strncmp (name + 1, "no-", 3))
f5b0eb4e 5784 {
0f41302f 5785 /* We have Xno-YYY, search for XYYY. */
f5b0eb4e
RK
5786 for (i = switchnum + 1; i < n_switches; i++)
5787 if (switches[i].part1[0] == name[0]
5788 && ! strcmp (&switches[i].part1[1], &name[4]))
d25a45d4
KH
5789 {
5790 switches[switchnum].validated = 1;
5791 switches[switchnum].live_cond = SWITCH_FALSE;
5792 return 0;
5793 }
f5b0eb4e
RK
5794 }
5795 else
5796 {
5797 /* We have XYYY, search for Xno-YYY. */
5798 for (i = switchnum + 1; i < n_switches; i++)
5799 if (switches[i].part1[0] == name[0]
5800 && switches[i].part1[1] == 'n'
5801 && switches[i].part1[2] == 'o'
5802 && switches[i].part1[3] == '-'
5803 && !strcmp (&switches[i].part1[4], &name[1]))
d25a45d4
KH
5804 {
5805 switches[switchnum].validated = 1;
5806 switches[switchnum].live_cond = SWITCH_FALSE;
5807 return 0;
5808 }
f5b0eb4e
RK
5809 }
5810 break;
5811 }
5812
5813 /* Otherwise the switch is live. */
3371362c 5814 switches[switchnum].live_cond |= SWITCH_LIVE;
f5b0eb4e
RK
5815 return 1;
5816}
5817\f
ed1f651b
RS
5818/* Pass a switch to the current accumulating command
5819 in the same form that we received it.
5820 SWITCHNUM identifies the switch; it is an index into
5821 the vector of switches gcc received, which is `switches'.
5822 This cannot fail since it never finishes a command line.
5823
4977bab6 5824 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
ed1f651b
RS
5825
5826static void
1d088dee 5827give_switch (int switchnum, int omit_first_word)
ed1f651b 5828{
3371362c 5829 if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
8097c429
TT
5830 return;
5831
ed1f651b
RS
5832 if (!omit_first_word)
5833 {
6496a589
KG
5834 do_spec_1 ("-", 0, NULL);
5835 do_spec_1 (switches[switchnum].part1, 1, NULL);
ed1f651b 5836 }
1ba9a487 5837
ed1f651b
RS
5838 if (switches[switchnum].args != 0)
5839 {
fbd40359 5840 const char **p;
ed1f651b
RS
5841 for (p = switches[switchnum].args; *p; p++)
5842 {
11972f66
NS
5843 const char *arg = *p;
5844
4977bab6 5845 do_spec_1 (" ", 0, NULL);
11972f66
NS
5846 if (suffix_subst)
5847 {
5848 unsigned length = strlen (arg);
e65677af 5849 int dot = 0;
11972f66
NS
5850
5851 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5852 if (arg[length] == '.')
5853 {
b1d5455a 5854 (CONST_CAST(char *, arg))[length] = 0;
e65677af 5855 dot = 1;
11972f66
NS
5856 break;
5857 }
6496a589 5858 do_spec_1 (arg, 1, NULL);
e65677af 5859 if (dot)
b1d5455a 5860 (CONST_CAST(char *, arg))[length] = '.';
e65677af 5861 do_spec_1 (suffix_subst, 1, NULL);
11972f66
NS
5862 }
5863 else
6496a589 5864 do_spec_1 (arg, 1, NULL);
ed1f651b
RS
5865 }
5866 }
1ba9a487 5867
6496a589 5868 do_spec_1 (" ", 0, NULL);
ab87f8c8 5869 switches[switchnum].validated = 1;
ed1f651b
RS
5870}
5871\f
5872/* Search for a file named NAME trying various prefixes including the
5873 user's -B prefix and some standard ones.
5874 Return the absolute file name found. If nothing is found, return NAME. */
5875
878f32c3 5876static const char *
1d088dee 5877find_file (const char *name)
ed1f651b 5878{
00dcee0c 5879 char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
ed1f651b
RS
5880 return newname ? newname : name;
5881}
5882
0ad5835e 5883/* Determine whether a directory exists. If LINKER, return 0 for
00dcee0c 5884 certain fixed names not needed by the linker. */
ed1f651b
RS
5885
5886static int
00dcee0c 5887is_directory (const char *path1, bool linker)
ed1f651b 5888{
00dcee0c
AM
5889 int len1;
5890 char *path;
ed1f651b
RS
5891 char *cp;
5892 struct stat st;
5893
00dcee0c
AM
5894 /* Ensure the string ends with "/.". The resulting path will be a
5895 directory even if the given path is a symbolic link. */
5896 len1 = strlen (path1);
e1e4cdc4 5897 path = (char *) alloca (3 + len1);
7e2231e7 5898 memcpy (path, path1, len1);
00dcee0c 5899 cp = path + len1;
509781a4 5900 if (!IS_DIR_SEPARATOR (cp[-1]))
48ff801b 5901 *cp++ = DIR_SEPARATOR;
ed1f651b
RS
5902 *cp++ = '.';
5903 *cp = '\0';
5904
5905 /* Exclude directories that the linker is known to search. */
0ad5835e 5906 if (linker
00dcee0c 5907 && IS_DIR_SEPARATOR (path[0])
48ff801b 5908 && ((cp - path == 6
00dcee0c 5909 && strncmp (path + 1, "lib", 3) == 0)
48ff801b 5910 || (cp - path == 10
00dcee0c
AM
5911 && strncmp (path + 1, "usr", 3) == 0
5912 && IS_DIR_SEPARATOR (path[4])
5913 && strncmp (path + 5, "lib", 3) == 0)))
ed1f651b
RS
5914 return 0;
5915
5916 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5917}
512b62fb
JM
5918
5919/* Set up the various global variables to indicate that we're processing
5920 the input file named FILENAME. */
5921
b856c15d 5922void
1d088dee 5923set_input (const char *filename)
512b62fb 5924{
b3694847 5925 const char *p;
512b62fb 5926
6afbc885
JM
5927 gcc_input_filename = filename;
5928 input_filename_length = strlen (gcc_input_filename);
5929 input_basename = lbasename (gcc_input_filename);
512b62fb
JM
5930
5931 /* Find a suffix starting with the last period,
5932 and set basename_length to exclude that suffix. */
5933 basename_length = strlen (input_basename);
ea414c97 5934 suffixed_basename_length = basename_length;
512b62fb 5935 p = input_basename + basename_length;
d25a45d4
KH
5936 while (p != input_basename && *p != '.')
5937 --p;
512b62fb
JM
5938 if (*p == '.' && p != input_basename)
5939 {
5940 basename_length = p - input_basename;
5941 input_suffix = p + 1;
5942 }
5943 else
5944 input_suffix = "";
589005ff 5945
99f78cdd 5946 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
6afbc885 5947 we will need to do a stat on the gcc_input_filename. The
99f78cdd
IR
5948 INPUT_STAT_SET signals that the stat is needed. */
5949 input_stat_set = 0;
512b62fb 5950}
ed1f651b
RS
5951\f
5952/* On fatal signals, delete all the temporary files. */
5953
5954static void
2dec80c7 5955fatal_signal (int signum)
ed1f651b
RS
5956{
5957 signal (signum, SIG_DFL);
5958 delete_failure_queue ();
5959 delete_temp_files ();
5960 /* Get the same signal again, this time not handled,
5961 so its normal effect occurs. */
5962 kill (getpid (), signum);
5963}
5964
2153915d
AO
5965/* Compare the contents of the two files named CMPFILE[0] and
5966 CMPFILE[1]. Return zero if they're identical, nonzero
5967 otherwise. */
5968
5969static int
5970compare_files (char *cmpfile[])
5971{
5972 int ret = 0;
5973 FILE *temp[2] = { NULL, NULL };
5974 int i;
5975
5976#if HAVE_MMAP_FILE
5977 {
5978 size_t length[2];
5979 void *map[2] = { NULL, NULL };
5980
5981 for (i = 0; i < 2; i++)
5982 {
5983 struct stat st;
5984
5985 if (stat (cmpfile[i], &st) < 0 || !S_ISREG (st.st_mode))
5986 {
5987 error ("%s: could not determine length of compare-debug file %s",
6afbc885 5988 gcc_input_filename, cmpfile[i]);
2153915d
AO
5989 ret = 1;
5990 break;
5991 }
5992
5993 length[i] = st.st_size;
5994 }
5995
5996 if (!ret && length[0] != length[1])
5997 {
6afbc885 5998 error ("%s: -fcompare-debug failure (length)", gcc_input_filename);
2153915d
AO
5999 ret = 1;
6000 }
6001
6002 if (!ret)
6003 for (i = 0; i < 2; i++)
6004 {
6005 int fd = open (cmpfile[i], O_RDONLY);
6006 if (fd < 0)
6007 {
6008 error ("%s: could not open compare-debug file %s",
6afbc885 6009 gcc_input_filename, cmpfile[i]);
2153915d
AO
6010 ret = 1;
6011 break;
6012 }
6013
6014 map[i] = mmap (NULL, length[i], PROT_READ, MAP_PRIVATE, fd, 0);
6015 close (fd);
6016
6017 if (map[i] == (void *) MAP_FAILED)
6018 {
6019 ret = -1;
6020 break;
6021 }
6022 }
6023
6024 if (!ret)
6025 {
6026 if (memcmp (map[0], map[1], length[0]) != 0)
6027 {
6afbc885 6028 error ("%s: -fcompare-debug failure", gcc_input_filename);
2153915d
AO
6029 ret = 1;
6030 }
6031 }
6032
6033 for (i = 0; i < 2; i++)
6034 if (map[i])
e7aae3e8 6035 munmap ((caddr_t) map[i], length[i]);
2153915d
AO
6036
6037 if (ret >= 0)
6038 return ret;
6039
6040 ret = 0;
6041 }
6042#endif
6043
6044 for (i = 0; i < 2; i++)
6045 {
6046 temp[i] = fopen (cmpfile[i], "r");
6047 if (!temp[i])
6048 {
6049 error ("%s: could not open compare-debug file %s",
6afbc885 6050 gcc_input_filename, cmpfile[i]);
2153915d
AO
6051 ret = 1;
6052 break;
6053 }
6054 }
6055
6056 if (!ret && temp[0] && temp[1])
6057 for (;;)
6058 {
6059 int c0, c1;
6060 c0 = fgetc (temp[0]);
6061 c1 = fgetc (temp[1]);
6062
6063 if (c0 != c1)
6064 {
6065 error ("%s: -fcompare-debug failure",
6afbc885 6066 gcc_input_filename);
2153915d
AO
6067 ret = 1;
6068 break;
6069 }
6070
6071 if (c0 == EOF)
6072 break;
6073 }
6074
6075 for (i = 1; i >= 0; i--)
6076 {
6077 if (temp[i])
6078 fclose (temp[i]);
6079 }
6080
6081 return ret;
6082}
6083
07a3df01 6084extern int main (int, char **);
a8f227e7 6085
ed1f651b 6086int
07a3df01 6087main (int argc, char **argv)
ed1f651b 6088{
ea414c97 6089 size_t i;
ed1f651b 6090 int value;
ed1f651b 6091 int linker_was_run = 0;
0855eab7 6092 int lang_n_infiles = 0;
17211ab5 6093 int num_linker_inputs = 0;
ed1f651b
RS
6094 char *explicit_link_files;
6095 char *specs_file;
878f32c3 6096 const char *p;
d9ac3a07 6097 struct user_specs *uptr;
2091ff66 6098 char **old_argv = argv;
60cf253a
JM
6099 struct cl_decoded_option *decoded_options;
6100 unsigned int decoded_options_count;
ed1f651b 6101
b481444e
JJ
6102 /* Initialize here, not in definition. The IRIX 6 O32 cc sometimes chokes
6103 on ?: in file-scope variable initializations. */
6104 asm_debug = ASM_DEBUG_SPEC;
6105
afcd8a02 6106 p = argv[0] + strlen (argv[0]);
509781a4
ME
6107 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
6108 --p;
6afbc885 6109 progname = p;
ed1f651b 6110
6afbc885 6111 xmalloc_set_program_name (progname);
e1a132c6 6112
9d530538
MM
6113 expandargv (&argc, &argv);
6114
2091ff66
NF
6115 /* Determine if any expansions were made. */
6116 if (argv != old_argv)
6117 at_file_supplied = true;
6118
a75bfaa6
JM
6119 global_options = global_options_init;
6120
60cf253a
JM
6121 decode_cmdline_options_to_array (argc, CONST_CAST2 (const char **, char **,
6122 argv),
6123 CL_DRIVER,
6124 &decoded_options, &decoded_options_count);
14c7833c 6125
93284395 6126#ifdef GCC_DRIVER_HOST_INITIALIZATION
ff7cc307 6127 /* Perform host dependent initialization when needed. */
93284395
ME
6128 GCC_DRIVER_HOST_INITIALIZATION;
6129#endif
6130
98a3dad4 6131 /* Unlock the stdio streams. */
2653bb0c 6132 unlock_std_streams ();
98a3dad4 6133
191bf464 6134 gcc_init_libintl ();
ab87f8c8 6135
6afbc885
JM
6136 diagnostic_initialize (global_dc, 0);
6137 if (atexit (delete_temp_files) != 0)
6138 fatal_error ("atexit failed");
6139
ed1f651b 6140 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
2dec80c7 6141 signal (SIGINT, fatal_signal);
2a353d3a 6142#ifdef SIGHUP
ed1f651b 6143 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
2dec80c7 6144 signal (SIGHUP, fatal_signal);
2a353d3a 6145#endif
ed1f651b 6146 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
2dec80c7 6147 signal (SIGTERM, fatal_signal);
ed1f651b
RS
6148#ifdef SIGPIPE
6149 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
2dec80c7 6150 signal (SIGPIPE, fatal_signal);
ed1f651b 6151#endif
798bdf70 6152#ifdef SIGCHLD
0bf679a3
BK
6153 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
6154 receive the signal. A different setting is inheritable */
6155 signal (SIGCHLD, SIG_DFL);
798bdf70 6156#endif
ed1f651b 6157
f3226a90
JT
6158 /* Allocate the argument vector. */
6159 alloc_args ();
ed1f651b
RS
6160
6161 obstack_init (&obstack);
6162
961b7009
MM
6163 /* Build multilib_select, et. al from the separate lines that make up each
6164 multilib selection. */
ffd86336 6165 {
3b304f5b 6166 const char *const *q = multilib_raw;
961b7009 6167 int need_space;
ffd86336
JW
6168
6169 obstack_init (&multilib_obstack);
0f41302f 6170 while ((p = *q++) != (char *) 0)
ffd86336
JW
6171 obstack_grow (&multilib_obstack, p, strlen (p));
6172
6173 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6174 multilib_select = XOBFINISH (&multilib_obstack, const char *);
961b7009
MM
6175
6176 q = multilib_matches_raw;
6177 while ((p = *q++) != (char *) 0)
6178 obstack_grow (&multilib_obstack, p, strlen (p));
6179
6180 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6181 multilib_matches = XOBFINISH (&multilib_obstack, const char *);
961b7009 6182
0a8d6618
BC
6183 q = multilib_exclusions_raw;
6184 while ((p = *q++) != (char *) 0)
d25a45d4 6185 obstack_grow (&multilib_obstack, p, strlen (p));
0a8d6618
BC
6186
6187 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6188 multilib_exclusions = XOBFINISH (&multilib_obstack, const char *);
9218435e 6189
961b7009 6190 need_space = FALSE;
b6a1cbae 6191 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
961b7009
MM
6192 {
6193 if (need_space)
6194 obstack_1grow (&multilib_obstack, ' ');
6195 obstack_grow (&multilib_obstack,
6196 multilib_defaults_raw[i],
6197 strlen (multilib_defaults_raw[i]));
6198 need_space = TRUE;
6199 }
6200
6201 obstack_1grow (&multilib_obstack, 0);
7973fd2a 6202 multilib_defaults = XOBFINISH (&multilib_obstack, const char *);
ffd86336
JW
6203 }
6204
8faf4a68
JW
6205#ifdef INIT_ENVIRONMENT
6206 /* Set up any other necessary machine specific environment variables. */
47d33318 6207 xputenv (INIT_ENVIRONMENT);
8faf4a68
JW
6208#endif
6209
ed1f651b
RS
6210 /* Make a table of what switches there are (switches, n_switches).
6211 Make a table of specified input files (infiles, n_infiles).
6212 Decode switches that are handled locally. */
6213
60cf253a 6214 process_command (decoded_options_count, decoded_options);
ed1f651b
RS
6215
6216 /* Initialize the vector of specs to just the default.
6217 This means one element containing 0s, as a terminator. */
6218
e1e4cdc4 6219 compilers = XNEWVAR (struct compiler, sizeof default_compilers);
703ad42b 6220 memcpy (compilers, default_compilers, sizeof default_compilers);
ed1f651b
RS
6221 n_compilers = n_default_compilers;
6222
6223 /* Read specs from a file if there is one. */
6224
6aa62cff 6225 machine_suffix = concat (spec_machine, dir_separator_str,
d4f2852f
KG
6226 spec_version, dir_separator_str, NULL);
6227 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
ed1f651b 6228
00dcee0c 6229 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
ed1f651b
RS
6230 /* Read the specs file unless it is a default one. */
6231 if (specs_file != 0 && strcmp (specs_file, "specs"))
20df0482
MM
6232 read_specs (specs_file, TRUE);
6233 else
6234 init_spec ();
841faeed 6235
5bb67e36 6236 /* We need to check standard_exec_prefix/just_machine_suffix/specs
3ac63d94 6237 for any override of as, ld and libraries. */
e1e4cdc4 6238 specs_file = (char *) alloca (strlen (standard_exec_prefix)
703ad42b 6239 + strlen (just_machine_suffix) + sizeof ("specs"));
5bb67e36
RK
6240
6241 strcpy (specs_file, standard_exec_prefix);
6242 strcat (specs_file, just_machine_suffix);
6243 strcat (specs_file, "specs");
6244 if (access (specs_file, R_OK) == 0)
6245 read_specs (specs_file, TRUE);
9218435e 6246
7816bea0
DJ
6247 /* Process any configure-time defaults specified for the command line
6248 options, via OPTION_DEFAULT_SPECS. */
6249 for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)
6250 do_option_spec (option_default_specs[i].name,
6251 option_default_specs[i].spec);
6252
3bd6d4c4
AO
6253 /* Process DRIVER_SELF_SPECS, adding any new options to the end
6254 of the command line. */
6255
6256 for (i = 0; i < ARRAY_SIZE (driver_self_specs); i++)
6257 do_self_spec (driver_self_specs[i]);
6258
2153915d
AO
6259 if (compare_debug)
6260 {
6261 enum save_temps save;
6262
6263 if (!compare_debug_second)
6264 {
6265 n_switches_debug_check[1] = n_switches;
efe5e5a0 6266 n_switches_alloc_debug_check[1] = n_switches_alloc;
2153915d 6267 switches_debug_check[1] = XDUPVEC (struct switchstr, switches,
efe5e5a0 6268 n_switches_alloc);
2153915d
AO
6269
6270 do_self_spec ("%:compare-debug-self-opt()");
6271 n_switches_debug_check[0] = n_switches;
efe5e5a0 6272 n_switches_alloc_debug_check[0] = n_switches_alloc;
2153915d
AO
6273 switches_debug_check[0] = switches;
6274
6275 n_switches = n_switches_debug_check[1];
efe5e5a0 6276 n_switches_alloc = n_switches_alloc_debug_check[1];
2153915d
AO
6277 switches = switches_debug_check[1];
6278 }
6279
6280 /* Avoid crash when computing %j in this early. */
6281 save = save_temps_flag;
6282 save_temps_flag = SAVE_TEMPS_NONE;
6283
6284 compare_debug = -compare_debug;
6285 do_self_spec ("%:compare-debug-self-opt()");
6286
6287 save_temps_flag = save;
6288
6289 if (!compare_debug_second)
6290 {
6291 n_switches_debug_check[1] = n_switches;
efe5e5a0 6292 n_switches_alloc_debug_check[1] = n_switches_alloc;
2153915d
AO
6293 switches_debug_check[1] = switches;
6294 compare_debug = -compare_debug;
6295 n_switches = n_switches_debug_check[0];
efe5e5a0 6296 n_switches_alloc = n_switches_debug_check[0];
2153915d
AO
6297 switches = switches_debug_check[0];
6298 }
6299 }
6300
4977bab6
ZW
6301 /* If not cross-compiling, look for executables in the standard
6302 places. */
6303 if (*cross_compile == '0')
004fd4d5 6304 {
2296d164
RK
6305 if (*md_exec_prefix)
6306 {
6307 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
1a5d37a1 6308 PREFIX_PRIORITY_LAST, 0, 0);
2296d164 6309 }
4977bab6
ZW
6310 }
6311
71c0e7fc 6312 /* Process sysroot_suffix_spec. */
e7f13528
GP
6313 if (*sysroot_suffix_spec != 0
6314 && do_spec_2 (sysroot_suffix_spec) == 0)
6315 {
5b634ee0 6316 if (VEC_length (const_char_p, argbuf) > 1)
ab532386 6317 error ("spec failure: more than one arg to SYSROOT_SUFFIX_SPEC");
5b634ee0
JM
6318 else if (VEC_length (const_char_p, argbuf) == 1)
6319 target_sysroot_suffix = xstrdup (VEC_last (const_char_p, argbuf));
e7f13528
GP
6320 }
6321
380e5ca4
MM
6322#ifdef HAVE_LD_SYSROOT
6323 /* Pass the --sysroot option to the linker, if it supports that. If
6324 there is a sysroot_suffix_spec, it has already been processed by
6325 this point, so target_system_root really is the system root we
6326 should be using. */
6327 if (target_system_root)
6328 {
6329 obstack_grow (&obstack, "%(sysroot_spec) ", strlen ("%(sysroot_spec) "));
6330 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
7973fd2a 6331 set_spec ("link", XOBFINISH (&obstack, const char *));
380e5ca4
MM
6332 }
6333#endif
6334
71c0e7fc 6335 /* Process sysroot_hdrs_suffix_spec. */
e7f13528
GP
6336 if (*sysroot_hdrs_suffix_spec != 0
6337 && do_spec_2 (sysroot_hdrs_suffix_spec) == 0)
6338 {
5b634ee0 6339 if (VEC_length (const_char_p, argbuf) > 1)
ab532386 6340 error ("spec failure: more than one arg to SYSROOT_HEADERS_SUFFIX_SPEC");
5b634ee0
JM
6341 else if (VEC_length (const_char_p, argbuf) == 1)
6342 target_sysroot_hdrs_suffix = xstrdup (VEC_last (const_char_p, argbuf));
e7f13528
GP
6343 }
6344
4977bab6
ZW
6345 /* Look for startfiles in the standard places. */
6346 if (*startfile_prefix_spec != 0
6347 && do_spec_2 (startfile_prefix_spec) == 0
6348 && do_spec_1 (" ", 0, NULL) == 0)
6349 {
5b634ee0 6350 const char *arg;
4977bab6 6351 int ndx;
5b634ee0
JM
6352 FOR_EACH_VEC_ELT (const_char_p, argbuf, ndx, arg)
6353 add_sysrooted_prefix (&startfile_prefixes, arg, "BINUTILS",
1a5d37a1 6354 PREFIX_PRIORITY_LAST, 0, 1);
4977bab6
ZW
6355 }
6356 /* We should eventually get rid of all these and stick to
6357 startfile_prefix_spec exclusively. */
6358 else if (*cross_compile == '0' || target_system_root)
6359 {
2296d164 6360 if (*md_startfile_prefix)
4977bab6 6361 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix,
1a5d37a1 6362 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
004fd4d5 6363
2296d164 6364 if (*md_startfile_prefix_1)
4977bab6 6365 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix_1,
1a5d37a1 6366 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
607a4f7d 6367
4dbc7773
ILT
6368 /* If standard_startfile_prefix is relative, base it on
6369 standard_exec_prefix. This lets us move the installed tree
6370 as a unit. If GCC_EXEC_PREFIX is defined, base
0d037580
DJ
6371 standard_startfile_prefix on that as well.
6372
6373 If the prefix is relative, only search it for native compilers;
6374 otherwise we will search a directory containing host libraries. */
3dce1408 6375 if (IS_ABSOLUTE_PATH (standard_startfile_prefix))
4977bab6
ZW
6376 add_sysrooted_prefix (&startfile_prefixes,
6377 standard_startfile_prefix, "BINUTILS",
1a5d37a1 6378 PREFIX_PRIORITY_LAST, 0, 1);
0d037580 6379 else if (*cross_compile == '0')
4dbc7773 6380 {
48ff801b 6381 add_prefix (&startfile_prefixes,
b8698a0f
L
6382 concat (gcc_exec_prefix
6383 ? gcc_exec_prefix : standard_exec_prefix,
6384 machine_suffix,
d4f2852f 6385 standard_startfile_prefix, NULL),
1a5d37a1 6386 NULL, PREFIX_PRIORITY_LAST, 0, 1);
9218435e 6387 }
4dbc7773 6388
f4c0a303
CD
6389 /* Sysrooted prefixes are relocated because target_system_root is
6390 also relocated by gcc_exec_prefix. */
656c7a3a
AL
6391 if (*standard_startfile_prefix_1)
6392 add_sysrooted_prefix (&startfile_prefixes,
6393 standard_startfile_prefix_1, "BINUTILS",
1a5d37a1 6394 PREFIX_PRIORITY_LAST, 0, 1);
656c7a3a
AL
6395 if (*standard_startfile_prefix_2)
6396 add_sysrooted_prefix (&startfile_prefixes,
6397 standard_startfile_prefix_2, "BINUTILS",
1a5d37a1 6398 PREFIX_PRIORITY_LAST, 0, 1);
004fd4d5 6399 }
343f59d9 6400
8607048d
TT
6401 /* Process any user specified specs in the order given on the command
6402 line. */
6403 for (uptr = user_specs_head; uptr; uptr = uptr->next)
6404 {
5bbcd587 6405 char *filename = find_a_file (&startfile_prefixes, uptr->filename,
00dcee0c 6406 R_OK, true);
8607048d
TT
6407 read_specs (filename ? filename : uptr->filename, FALSE);
6408 }
6409
e8601ecb
JW
6410 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
6411 if (gcc_exec_prefix)
cb6edbcb
KG
6412 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
6413 spec_version, dir_separator_str, NULL);
004fd4d5 6414
ed1f651b
RS
6415 /* Now we have the specs.
6416 Set the `valid' bits for switches that match anything in any spec. */
6417
6418 validate_all_switches ();
6419
60103a34
DE
6420 /* Now that we have the switches and the specs, set
6421 the subdirectory based on the options. */
6422 set_multilib_dir ();
6423
d7f09764 6424 /* Set up to remember the pathname of gcc and any options
6afbc885 6425 needed for collect. We use argv[0] instead of progname because
d7f09764
DN
6426 we need the complete pathname. */
6427 obstack_init (&collect_obstack);
6428 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
6429 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
6430 xputenv (XOBFINISH (&collect_obstack, char *));
6431
6432 /* Set up to remember the pathname of the lto wrapper. */
6433
6434 lto_wrapper_spec = find_a_file (&exec_prefixes, "lto-wrapper", X_OK, false);
6435 if (lto_wrapper_spec)
6436 {
6437 obstack_init (&collect_obstack);
6438 obstack_grow (&collect_obstack, "COLLECT_LTO_WRAPPER=",
6439 sizeof ("COLLECT_LTO_WRAPPER=") - 1);
6440 obstack_grow (&collect_obstack, lto_wrapper_spec,
6441 strlen (lto_wrapper_spec) + 1);
6442 xputenv (XOBFINISH (&collect_obstack, char *));
6443 }
6444
ed1f651b
RS
6445 /* Warn about any switches that no pass was interested in. */
6446
d25a45d4 6447 for (i = 0; (int) i < n_switches; i++)
ab87f8c8 6448 if (! switches[i].validated)
bdc6b402 6449 error ("unrecognized option %<-%s%>", switches[i].part1);
ed1f651b 6450
6a9e290e
RK
6451 /* Obey some of the options. */
6452
2628b9d3
DE
6453 if (print_search_dirs)
6454 {
f4c0a303
CD
6455 printf (_("install: %s%s\n"),
6456 gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
6457 gcc_exec_prefix ? "" : machine_suffix);
00dcee0c
AM
6458 printf (_("programs: %s\n"),
6459 build_search_list (&exec_prefixes, "", false, false));
6460 printf (_("libraries: %s\n"),
6461 build_search_list (&startfile_prefixes, "", false, true));
9257393c 6462 return (0);
2628b9d3
DE
6463 }
6464
6a9e290e 6465 if (print_file_name)
2dcb563f 6466 {
6a9e290e 6467 printf ("%s\n", find_file (print_file_name));
9257393c 6468 return (0);
2dcb563f
RS
6469 }
6470
6a9e290e
RK
6471 if (print_prog_name)
6472 {
5bbcd587 6473 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK, 0);
6a9e290e 6474 printf ("%s\n", (newname ? newname : print_prog_name));
9257393c 6475 return (0);
6a9e290e 6476 }
ed1f651b 6477
60103a34
DE
6478 if (print_multi_lib)
6479 {
6480 print_multilib_info ();
9257393c 6481 return (0);
60103a34
DE
6482 }
6483
6484 if (print_multi_directory)
6485 {
6486 if (multilib_dir == NULL)
6487 printf (".\n");
6488 else
6489 printf ("%s\n", multilib_dir);
9257393c 6490 return (0);
60103a34
DE
6491 }
6492
3def1397
VP
6493 if (print_sysroot)
6494 {
6495 if (target_system_root)
6496 {
6497 if (target_sysroot_suffix)
6498 printf ("%s%s\n", target_system_root, target_sysroot_suffix);
6499 else
6500 printf ("%s\n", target_system_root);
6501 }
6502 return (0);
6503 }
6504
5bbcd587
JJ
6505 if (print_multi_os_directory)
6506 {
6507 if (multilib_os_dir == NULL)
6508 printf (".\n");
6509 else
6510 printf ("%s\n", multilib_os_dir);
6511 return (0);
6512 }
6513
14da6073
JM
6514 if (print_sysroot_headers_suffix)
6515 {
6516 if (*sysroot_hdrs_suffix_spec)
6517 {
dc5bbad3
JM
6518 printf("%s\n", (target_sysroot_hdrs_suffix
6519 ? target_sysroot_hdrs_suffix
6520 : ""));
14da6073
JM
6521 return (0);
6522 }
6523 else
6524 /* The error status indicates that only one set of fixed
6525 headers should be built. */
2dec80c7 6526 fatal_error ("not configured with sysroot headers suffix");
14da6073
JM
6527 }
6528
b8468bc7
NC
6529 if (print_help_list)
6530 {
6531 display_help ();
6532
6533 if (! verbose_flag)
6534 {
5e4adfba 6535 printf (_("\nFor bug reporting instructions, please see:\n"));
a976603e 6536 printf ("%s.\n", bug_report_url);
9218435e 6537
9257393c 6538 return (0);
b8468bc7
NC
6539 }
6540
6541 /* We do not exit here. Instead we have created a fake input file
6542 called 'help-dummy' which needs to be compiled, and we pass this
033505fc
RW
6543 on the various sub-processes, along with the --help switch.
6544 Ensure their output appears after ours. */
6545 fputc ('\n', stdout);
6546 fflush (stdout);
b8468bc7 6547 }
9218435e 6548
41fd0f9b
RAE
6549 if (print_version)
6550 {
6afbc885 6551 printf (_("%s %s%s\n"), progname, pkgversion_string,
41fd0f9b 6552 version_string);
bd5c3aa5 6553 printf ("Copyright %s 2010 Free Software Foundation, Inc.\n",
41fd0f9b
RAE
6554 _("(C)"));
6555 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
6556warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
6557 stdout);
6558 if (! verbose_flag)
6559 return 0;
6560
6561 /* We do not exit here. We use the same mechanism of --help to print
6562 the version of the sub-processes. */
6563 fputc ('\n', stdout);
6564 fflush (stdout);
6565 }
6566
ed1f651b
RS
6567 if (verbose_flag)
6568 {
7ad9ff7a 6569 int n;
008355a6 6570 const char *thrmod;
7ad9ff7a 6571
2dec80c7
JM
6572 fnotice (stderr, "Target: %s\n", spec_machine);
6573 fnotice (stderr, "Configured with: %s\n", configuration_arguments);
f5fa9a5b 6574
008355a6
AO
6575#ifdef THREAD_MODEL_SPEC
6576 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6577 but there's no point in doing all this processing just to get
6578 thread_model back. */
6579 obstack_init (&obstack);
6580 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6581 obstack_1grow (&obstack, '\0');
7973fd2a 6582 thrmod = XOBFINISH (&obstack, const char *);
008355a6
AO
6583#else
6584 thrmod = thread_model;
6585#endif
6586
2dec80c7 6587 fnotice (stderr, "Thread model: %s\n", thrmod);
a6687d2b 6588
7ad9ff7a
DE
6589 /* compiler_version is truncated at the first space when initialized
6590 from version string, so truncate version_string at the first space
6591 before comparing. */
6592 for (n = 0; version_string[n]; n++)
6593 if (version_string[n] == ' ')
6594 break;
6595
6596 if (! strncmp (version_string, compiler_version, n)
6597 && compiler_version[n] == 0)
2dec80c7
JM
6598 fnotice (stderr, "gcc version %s %s\n", version_string,
6599 pkgversion_string);
9c4faac1 6600 else
2dec80c7
JM
6601 fnotice (stderr, "gcc driver version %s %sexecuting gcc version %s\n",
6602 version_string, pkgversion_string, compiler_version);
9c4faac1 6603
ed1f651b 6604 if (n_infiles == 0)
9257393c 6605 return (0);
ed1f651b
RS
6606 }
6607
a2a05b0a 6608 if (n_infiles == added_libraries)
2dec80c7 6609 fatal_error ("no input files");
ed1f651b
RS
6610
6611 /* Make a place to record the compiler output file names
6612 that correspond to the input files. */
6613
f271358e 6614 i = n_infiles;
e37cda9b 6615 i += lang_specific_extra_outfiles;
5ed6ace5 6616 outfiles = XCNEWVEC (const char *, i);
ed1f651b
RS
6617
6618 /* Record which files were specified explicitly as link input. */
6619
d900f77d 6620 explicit_link_files = XCNEWVEC (char, n_infiles);
ed1f651b 6621
c3224d6f 6622 combine_inputs = have_o || flag_wpa;
0855eab7
CT
6623
6624 for (i = 0; (int) i < n_infiles; i++)
d1bd0ded 6625 {
0855eab7 6626 const char *name = infiles[i].name;
7904f95f
EC
6627 struct compiler *compiler = lookup_compiler (name,
6628 strlen (name),
0855eab7 6629 infiles[i].language);
7904f95f 6630
0855eab7
CT
6631 if (compiler && !(compiler->combinable))
6632 combine_inputs = false;
7904f95f 6633
0855eab7
CT
6634 if (lang_n_infiles > 0 && compiler != input_file_compiler
6635 && infiles[i].language && infiles[i].language[0] != '*')
6636 infiles[i].incompiler = compiler;
6637 else if (compiler)
6638 {
6639 lang_n_infiles++;
6640 input_file_compiler = compiler;
6641 infiles[i].incompiler = compiler;
6642 }
6643 else
6644 {
6645 /* Since there is no compiler for this input file, assume it is a
9cf737f8 6646 linker file. */
0855eab7
CT
6647 explicit_link_files[i] = 1;
6648 infiles[i].incompiler = NULL;
6649 }
6650 infiles[i].compiled = false;
6651 infiles[i].preprocessed = false;
d1bd0ded 6652 }
953ff289 6653
de19a50e 6654 if (!combine_inputs && have_c && have_o && lang_n_infiles > 1)
c3224d6f 6655 fatal_error ("cannot specify -o with -c, -S or -E with multiple files");
0855eab7
CT
6656
6657 for (i = 0; (int) i < n_infiles; i++)
ed1f651b 6658 {
ed1f651b
RS
6659 int this_file_error = 0;
6660
6661 /* Tell do_spec what to substitute for %i. */
6662
ed1f651b 6663 input_file_number = i;
512b62fb 6664 set_input (infiles[i].name);
ed1f651b 6665
0855eab7
CT
6666 if (infiles[i].compiled)
6667 continue;
6668
ed1f651b
RS
6669 /* Use the same thing in %o, unless cp->spec says otherwise. */
6670
6afbc885 6671 outfiles[i] = gcc_input_filename;
ed1f651b
RS
6672
6673 /* Figure out which compiler from the file's suffix. */
6674
c3224d6f
RG
6675 input_file_compiler
6676 = lookup_compiler (infiles[i].name, input_filename_length,
6677 infiles[i].language);
589005ff 6678
a9374841 6679 if (input_file_compiler)
ed1f651b
RS
6680 {
6681 /* Ok, we found an applicable compiler. Run its spec. */
ed1f651b 6682
a9374841 6683 if (input_file_compiler->spec[0] == '#')
d644be7b
ZW
6684 {
6685 error ("%s: %s compiler not installed on this system",
6afbc885 6686 gcc_input_filename, &input_file_compiler->spec[1]);
d644be7b
ZW
6687 this_file_error = 1;
6688 }
6689 else
6690 {
2153915d
AO
6691 if (compare_debug)
6692 {
6693 if (debug_check_temp_file[0])
6694 free (debug_check_temp_file[0]);
6695 debug_check_temp_file[0] = NULL;
6696
6697 if (debug_check_temp_file[1])
6698 free (debug_check_temp_file[1]);
6699 debug_check_temp_file[1] = NULL;
6700 }
6701
d644be7b 6702 value = do_spec (input_file_compiler->spec);
0855eab7 6703 infiles[i].compiled = true;
d644be7b 6704 if (value < 0)
b21292d0 6705 this_file_error = 1;
2153915d
AO
6706 else if (compare_debug && debug_check_temp_file[0])
6707 {
6708 if (verbose_flag)
2dec80c7 6709 inform (0, "recompiling with -fcompare-debug");
2153915d
AO
6710
6711 compare_debug = -compare_debug;
6712 n_switches = n_switches_debug_check[1];
efe5e5a0 6713 n_switches_alloc = n_switches_alloc_debug_check[1];
2153915d
AO
6714 switches = switches_debug_check[1];
6715
6716 value = do_spec (input_file_compiler->spec);
6717
6718 compare_debug = -compare_debug;
6719 n_switches = n_switches_debug_check[0];
efe5e5a0 6720 n_switches_alloc = n_switches_alloc_debug_check[0];
2153915d
AO
6721 switches = switches_debug_check[0];
6722
6723 if (value < 0)
6724 {
6725 error ("during -fcompare-debug recompilation");
6726 this_file_error = 1;
6727 }
6728
6729 gcc_assert (debug_check_temp_file[1]
6730 && strcmp (debug_check_temp_file[0],
6731 debug_check_temp_file[1]));
6732
6733 if (verbose_flag)
2dec80c7 6734 inform (0, "comparing final insns dumps");
2153915d
AO
6735
6736 if (compare_files (debug_check_temp_file))
6737 this_file_error = 1;
6738 }
6739
6740 if (compare_debug)
6741 {
6742 if (debug_check_temp_file[0])
6743 free (debug_check_temp_file[0]);
6744 debug_check_temp_file[0] = NULL;
6745
6746 if (debug_check_temp_file[1])
6747 free (debug_check_temp_file[1]);
6748 debug_check_temp_file[1] = NULL;
6749 }
d644be7b 6750 }
ed1f651b
RS
6751 }
6752
6753 /* If this file's name does not contain a recognized suffix,
6754 record it as explicit linker input. */
6755
6756 else
6757 explicit_link_files[i] = 1;
6758
6759 /* Clear the delete-on-failure queue, deleting the files in it
6760 if this compilation failed. */
6761
6762 if (this_file_error)
6763 {
6764 delete_failure_queue ();
6afbc885 6765 errorcount++;
ed1f651b
RS
6766 }
6767 /* If this compilation succeeded, don't delete those files later. */
6768 clear_failure_queue ();
6769 }
6770
817a8255
EC
6771 /* Reset the input file name to the first compile/object file name, for use
6772 with %b in LINK_SPEC. We use the first input file that we can find
7904f95f 6773 a compiler to compile it instead of using infiles.language since for
817a8255 6774 languages other than C we use aliases that we then lookup later. */
512b62fb 6775 if (n_infiles > 0)
817a8255
EC
6776 {
6777 int i;
6778
6779 for (i = 0; i < n_infiles ; i++)
9ef0b1bd 6780 if (infiles[i].language && infiles[i].language[0] != '*')
817a8255
EC
6781 {
6782 set_input (infiles[i].name);
6783 break;
6784 }
6785 }
512b62fb 6786
6afbc885 6787 if (!seen_error ())
15c5edb9
TT
6788 {
6789 /* Make sure INPUT_FILE_NUMBER points to first available open
6790 slot. */
6791 input_file_number = n_infiles;
6792 if (lang_specific_pre_link ())
6afbc885 6793 errorcount++;
15c5edb9 6794 }
f271358e 6795
17211ab5
GK
6796 /* Determine if there are any linker input files. */
6797 num_linker_inputs = 0;
6798 for (i = 0; (int) i < n_infiles; i++)
6799 if (explicit_link_files[i] || outfiles[i] != NULL)
6800 num_linker_inputs++;
6801
ed1f651b
RS
6802 /* Run ld to link all the compiler output files. */
6803
6afbc885 6804 if (num_linker_inputs > 0 && !seen_error () && print_subprocess_help < 2)
ed1f651b
RS
6805 {
6806 int tmp = execution_count;
da18ea94 6807 const char *fuse_linker_plugin = "fuse-linker-plugin";
b3865ca9 6808
9218435e 6809 /* We'll use ld if we can't find collect2. */
10da1131
BM
6810 if (! strcmp (linker_name_spec, "collect2"))
6811 {
00dcee0c 6812 char *s = find_a_file (&exec_prefixes, "collect2", X_OK, false);
10da1131
BM
6813 if (s == NULL)
6814 linker_name_spec = "ld";
6815 }
d7f09764 6816
da18ea94
RAE
6817 if (switch_matches (fuse_linker_plugin,
6818 fuse_linker_plugin + strlen (fuse_linker_plugin), 0))
d7f09764
DN
6819 {
6820 linker_plugin_file_spec = find_a_file (&exec_prefixes,
1cd0b716 6821 LTOPLUGINSONAME, R_OK,
d7f09764
DN
6822 false);
6823 if (!linker_plugin_file_spec)
1cd0b716 6824 fatal_error ("-fuse-linker-plugin, but " LTOPLUGINSONAME " not found");
d7f09764
DN
6825
6826 lto_libgcc_spec = find_a_file (&startfile_prefixes, "libgcc.a",
6827 R_OK, true);
6828 if (!lto_libgcc_spec)
2dec80c7 6829 fatal_error ("could not find libgcc.a");
d7f09764
DN
6830 }
6831 lto_gcc_spec = argv[0];
6832
b3865ca9
RS
6833 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6834 for collect. */
00dcee0c
AM
6835 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH", false);
6836 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV, true);
b3865ca9 6837
a0f87454
RS
6838 if (print_subprocess_help == 1)
6839 {
6840 printf (_("\nLinker options\n==============\n\n"));
6841 printf (_("Use \"-Wl,OPTION\" to pass \"OPTION\""
6842 " to the linker.\n\n"));
6843 fflush (stdout);
6844 }
ed1f651b
RS
6845 value = do_spec (link_command_spec);
6846 if (value < 0)
6afbc885 6847 errorcount = 1;
ed1f651b
RS
6848 linker_was_run = (tmp != execution_count);
6849 }
6850
ed1f651b
RS
6851 /* If options said don't run linker,
6852 complain about input files to be given to the linker. */
6853
6afbc885 6854 if (! linker_was_run && !seen_error ())
d25a45d4 6855 for (i = 0; (int) i < n_infiles; i++)
01e4dd0d
KH
6856 if (explicit_link_files[i]
6857 && !(infiles[i].language && infiles[i].language[0] == '*'))
2dec80c7
JM
6858 warning (0, "%s: linker input file unused because linking not done",
6859 outfiles[i]);
ed1f651b
RS
6860
6861 /* Delete some or all of the temporary files we made. */
6862
6afbc885 6863 if (seen_error ())
ed1f651b
RS
6864 delete_failure_queue ();
6865 delete_temp_files ();
6866
b8468bc7
NC
6867 if (print_help_list)
6868 {
5e4adfba 6869 printf (("\nFor bug reporting instructions, please see:\n"));
a976603e 6870 printf ("%s\n", bug_report_url);
b8468bc7 6871 }
9218435e 6872
14a774a9 6873 return (signal_count != 0 ? 2
6afbc885 6874 : seen_error () ? (pass_exit_codes ? greatest_status : 1)
14a774a9 6875 : 0);
ed1f651b
RS
6876}
6877
6878/* Find the proper compilation spec for the file name NAME,
004fd4d5 6879 whose length is LENGTH. LANGUAGE is the specified language,
e5e809f4 6880 or 0 if this file is to be passed to the linker. */
ed1f651b
RS
6881
6882static struct compiler *
1d088dee 6883lookup_compiler (const char *name, size_t length, const char *language)
ed1f651b
RS
6884{
6885 struct compiler *cp;
6886
3ac63d94 6887 /* If this was specified by the user to be a linker input, indicate that. */
e5e809f4
JL
6888 if (language != 0 && language[0] == '*')
6889 return 0;
6890
6891 /* Otherwise, look for the language, if one is spec'd. */
ed1f651b
RS
6892 if (language != 0)
6893 {
6894 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
e5e809f4
JL
6895 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6896 return cp;
6897
ed1f651b 6898 error ("language %s not recognized", language);
e5e809f4 6899 return 0;
ed1f651b
RS
6900 }
6901
6902 /* Look for a suffix. */
6903 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6904 {
4cf3301c
RS
6905 if (/* The suffix `-' matches only the file name `-'. */
6906 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
e5e809f4
JL
6907 || (strlen (cp->suffix) < length
6908 /* See if the suffix matches the end of NAME. */
e5e809f4
JL
6909 && !strcmp (cp->suffix,
6910 name + length - strlen (cp->suffix))
e5e809f4 6911 ))
589005ff 6912 break;
03bf1c28 6913 }
e5e809f4 6914
03bf1c28 6915#if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
a1105617 6916 /* Look again, but case-insensitively this time. */
03bf1c28
MK
6917 if (cp < compilers)
6918 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6919 {
6920 if (/* The suffix `-' matches only the file name `-'. */
6921 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6922 || (strlen (cp->suffix) < length
6923 /* See if the suffix matches the end of NAME. */
6924 && ((!strcmp (cp->suffix,
6925 name + length - strlen (cp->suffix))
6926 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6927 && !strcasecmp (cp->suffix,
6928 name + length - strlen (cp->suffix)))
6929 ))
6930 break;
6931 }
6932#endif
6933
03bf1c28
MK
6934 if (cp >= compilers)
6935 {
ea414c97
ZW
6936 if (cp->spec[0] != '@')
6937 /* A non-alias entry: return it. */
6938 return cp;
9218435e 6939
ea414c97
ZW
6940 /* An alias entry maps a suffix to a language.
6941 Search for the language; pass 0 for NAME and LENGTH
6942 to avoid infinite recursion if language not found. */
6496a589 6943 return lookup_compiler (NULL, 0, cp->spec + 1);
ed1f651b 6944 }
ed1f651b
RS
6945 return 0;
6946}
6947\f
ed1f651b 6948static char *
1d088dee 6949save_string (const char *s, int len)
ed1f651b 6950{
5ed6ace5 6951 char *result = XNEWVEC (char, len + 1);
ed1f651b 6952
da61dec9 6953 memcpy (result, s, len);
ed1f651b
RS
6954 result[len] = 0;
6955 return result;
6956}
6957
d991c721 6958void
1d088dee 6959pfatal_with_name (const char *name)
ed1f651b 6960{
ab87f8c8
JL
6961 perror_with_name (name);
6962 delete_temp_files ();
6963 exit (1);
ed1f651b
RS
6964}
6965
6966static void
1d088dee 6967perror_with_name (const char *name)
ed1f651b 6968{
bdc6b402 6969 error ("%s: %m", name);
ed1f651b 6970}
ed1f651b 6971\f
4977bab6 6972static inline void
1d088dee 6973validate_switches_from_spec (const char *spec)
4977bab6
ZW
6974{
6975 const char *p = spec;
6976 char c;
6977 while ((c = *p++))
6978 if (c == '%' && (*p == '{' || *p == '<' || (*p == 'W' && *++p == '{')))
6979 /* We have a switch spec. */
6980 p = validate_switches (p + 1);
6981}
6982
ed1f651b 6983static void
1d088dee 6984validate_all_switches (void)
ed1f651b
RS
6985{
6986 struct compiler *comp;
b3865ca9 6987 struct spec_list *spec;
ed1f651b 6988
ea414c97 6989 for (comp = compilers; comp->spec; comp++)
4977bab6 6990 validate_switches_from_spec (comp->spec);
ed1f651b 6991
3ac63d94 6992 /* Look through the linked list of specs read from the specs file. */
d25a45d4 6993 for (spec = specs; spec; spec = spec->next)
4977bab6 6994 validate_switches_from_spec (*spec->ptr_spec);
b3865ca9 6995
4977bab6 6996 validate_switches_from_spec (link_command_spec);
ed1f651b
RS
6997}
6998
6999/* Look at the switch-name that comes after START
7000 and mark as valid all supplied switches that match it. */
7001
4977bab6 7002static const char *
1d088dee 7003validate_switches (const char *start)
ed1f651b 7004{
b3694847 7005 const char *p = start;
4977bab6
ZW
7006 const char *atom;
7007 size_t len;
b3694847 7008 int i;
4977bab6
ZW
7009 bool suffix = false;
7010 bool starred = false;
1d088dee 7011
4977bab6 7012#define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
1d088dee 7013
10ebf5fe 7014next_member:
4977bab6
ZW
7015 SKIP_WHITE ();
7016
ed1f651b 7017 if (*p == '!')
4977bab6 7018 p++;
ed1f651b 7019
4977bab6 7020 SKIP_WHITE ();
48137d59 7021 if (*p == '.' || *p == ',')
4977bab6 7022 suffix = true, p++;
ed1f651b 7023
4977bab6
ZW
7024 atom = p;
7025 while (ISIDNUM (*p) || *p == '-' || *p == '+' || *p == '='
7a975113 7026 || *p == ',' || *p == '.' || *p == '@')
d25a45d4 7027 p++;
4977bab6 7028 len = p - atom;
ed1f651b 7029
4977bab6
ZW
7030 if (*p == '*')
7031 starred = true, p++;
7032
7033 SKIP_WHITE ();
7034
7035 if (!suffix)
ed1f651b
RS
7036 {
7037 /* Mark all matching switches as valid. */
ed1f651b 7038 for (i = 0; i < n_switches; i++)
4977bab6
ZW
7039 if (!strncmp (switches[i].part1, atom, len)
7040 && (starred || switches[i].part1[len] == 0))
ab87f8c8 7041 switches[i].validated = 1;
ed1f651b 7042 }
4977bab6 7043
5a0ba8c9
LJR
7044 if (*p) p++;
7045 if (*p && (p[-1] == '|' || p[-1] == '&'))
4977bab6
ZW
7046 goto next_member;
7047
5a0ba8c9 7048 if (*p && p[-1] == ':')
ed1f651b 7049 {
4977bab6 7050 while (*p && *p != ';' && *p != '}')
ed1f651b 7051 {
4977bab6
ZW
7052 if (*p == '%')
7053 {
7054 p++;
7055 if (*p == '{' || *p == '<')
7056 p = validate_switches (p+1);
7057 else if (p[0] == 'W' && p[1] == '{')
7058 p = validate_switches (p+2);
7059 }
e17aafd1
GK
7060 else
7061 p++;
ed1f651b 7062 }
4977bab6 7063
5a0ba8c9
LJR
7064 if (*p) p++;
7065 if (*p && p[-1] == ';')
4977bab6 7066 goto next_member;
ed1f651b 7067 }
10ebf5fe 7068
4977bab6
ZW
7069 return p;
7070#undef SKIP_WHITE
ed1f651b 7071}
60103a34 7072\f
5bbcd587
JJ
7073struct mdswitchstr
7074{
7075 const char *str;
7076 int len;
7077};
7078
7079static struct mdswitchstr *mdswitches;
7080static int n_mdswitches;
7081
961b7009 7082/* Check whether a particular argument was used. The first time we
956d6950 7083 canonicalize the switches to keep only the ones we care about. */
60103a34
DE
7084
7085static int
1d088dee 7086used_arg (const char *p, int len)
60103a34 7087{
3ac63d94
NC
7088 struct mswitchstr
7089 {
3b304f5b
ZW
7090 const char *str;
7091 const char *replace;
961b7009
MM
7092 int len;
7093 int rep_len;
7094 };
7095
7096 static struct mswitchstr *mswitches;
7097 static int n_mswitches;
7098 int i, j;
7099
7100 if (!mswitches)
7101 {
7102 struct mswitchstr *matches;
3b304f5b 7103 const char *q;
c8c2dcdc 7104 int cnt = 0;
961b7009 7105
d25a45d4
KH
7106 /* Break multilib_matches into the component strings of string
7107 and replacement string. */
1a0bdd29
RK
7108 for (q = multilib_matches; *q != '\0'; q++)
7109 if (*q == ';')
961b7009
MM
7110 cnt++;
7111
e1e4cdc4
KG
7112 matches
7113 = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
961b7009
MM
7114 i = 0;
7115 q = multilib_matches;
7116 while (*q != '\0')
7117 {
7118 matches[i].str = q;
7119 while (*q != ' ')
7120 {
7121 if (*q == '\0')
3b5edfee
NS
7122 {
7123 invalid_matches:
bdc6b402 7124 fatal_error ("multilib spec %qs is invalid",
2dec80c7 7125 multilib_matches);
3b5edfee 7126 }
961b7009
MM
7127 q++;
7128 }
961b7009 7129 matches[i].len = q - matches[i].str;
60103a34 7130
961b7009
MM
7131 matches[i].replace = ++q;
7132 while (*q != ';' && *q != '\0')
7133 {
7134 if (*q == ' ')
3b5edfee 7135 goto invalid_matches;
961b7009
MM
7136 q++;
7137 }
7138 matches[i].rep_len = q - matches[i].replace;
7139 i++;
83a0c799
ZW
7140 if (*q == ';')
7141 q++;
961b7009 7142 }
60103a34 7143
71591a1d
JW
7144 /* Now build a list of the replacement string for switches that we care
7145 about. Make sure we allocate at least one entry. This prevents
7146 xmalloc from calling fatal, and prevents us from re-executing this
7147 block of code. */
7148 mswitches
5ed6ace5 7149 = XNEWVEC (struct mswitchstr, n_mdswitches + (n_switches ? n_switches : 1));
961b7009 7150 for (i = 0; i < n_switches; i++)
3371362c 7151 if ((switches[i].live_cond & SWITCH_IGNORE) == 0)
f645e2bd
RS
7152 {
7153 int xlen = strlen (switches[i].part1);
7154 for (j = 0; j < cnt; j++)
7155 if (xlen == matches[j].len
7156 && ! strncmp (switches[i].part1, matches[j].str, xlen))
7157 {
7158 mswitches[n_mswitches].str = matches[j].replace;
7159 mswitches[n_mswitches].len = matches[j].rep_len;
7160 mswitches[n_mswitches].replace = (char *) 0;
7161 mswitches[n_mswitches].rep_len = 0;
7162 n_mswitches++;
7163 break;
7164 }
7165 }
5bbcd587
JJ
7166
7167 /* Add MULTILIB_DEFAULTS switches too, as long as they were not present
7168 on the command line nor any options mutually incompatible with
7169 them. */
7170 for (i = 0; i < n_mdswitches; i++)
7171 {
7172 const char *r;
7173
7174 for (q = multilib_options; *q != '\0'; q++)
7175 {
7176 while (*q == ' ')
7177 q++;
7178
7179 r = q;
7180 while (strncmp (q, mdswitches[i].str, mdswitches[i].len) != 0
7181 || strchr (" /", q[mdswitches[i].len]) == NULL)
7182 {
7183 while (*q != ' ' && *q != '/' && *q != '\0')
7184 q++;
7185 if (*q != '/')
7186 break;
7187 q++;
7188 }
7189
7190 if (*q != ' ' && *q != '\0')
7191 {
7192 while (*r != ' ' && *r != '\0')
7193 {
7194 q = r;
7195 while (*q != ' ' && *q != '/' && *q != '\0')
7196 q++;
7197
7198 if (used_arg (r, q - r))
7199 break;
7200
7201 if (*q != '/')
7202 {
7203 mswitches[n_mswitches].str = mdswitches[i].str;
7204 mswitches[n_mswitches].len = mdswitches[i].len;
7205 mswitches[n_mswitches].replace = (char *) 0;
7206 mswitches[n_mswitches].rep_len = 0;
7207 n_mswitches++;
7208 break;
7209 }
7210
7211 r = q + 1;
7212 }
7213 break;
7214 }
7215 }
7216 }
961b7009 7217 }
03c42484 7218
961b7009
MM
7219 for (i = 0; i < n_mswitches; i++)
7220 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
7221 return 1;
03c42484 7222
961b7009
MM
7223 return 0;
7224}
03c42484
RK
7225
7226static int
1d088dee 7227default_arg (const char *p, int len)
03c42484 7228{
5bbcd587 7229 int i;
e29ef920 7230
5bbcd587
JJ
7231 for (i = 0; i < n_mdswitches; i++)
7232 if (len == mdswitches[i].len && ! strncmp (p, mdswitches[i].str, len))
7233 return 1;
03c42484
RK
7234
7235 return 0;
7236}
7237
0a8d6618
BC
7238/* Work out the subdirectory to use based on the options. The format of
7239 multilib_select is a list of elements. Each element is a subdirectory
7240 name followed by a list of options followed by a semicolon. The format
7241 of multilib_exclusions is the same, but without the preceding
7242 directory. First gcc will check the exclusions, if none of the options
7243 beginning with an exclamation point are present, and all of the other
7244 options are present, then we will ignore this completely. Passing
7245 that, gcc will consider each multilib_select in turn using the same
7246 rules for matching the options. If a match is found, that subdirectory
7247 will be used. */
60103a34
DE
7248
7249static void
1d088dee 7250set_multilib_dir (void)
60103a34 7251{
3b304f5b 7252 const char *p;
3ac63d94 7253 unsigned int this_path_len;
3b304f5b 7254 const char *this_path, *this_arg;
5bbcd587 7255 const char *start, *end;
03c42484 7256 int not_arg;
5bbcd587
JJ
7257 int ok, ndfltok, first;
7258
7259 n_mdswitches = 0;
7260 start = multilib_defaults;
7261 while (*start == ' ' || *start == '\t')
7262 start++;
7263 while (*start != '\0')
7264 {
7265 n_mdswitches++;
7266 while (*start != ' ' && *start != '\t' && *start != '\0')
7267 start++;
7268 while (*start == ' ' || *start == '\t')
7269 start++;
7270 }
7271
7272 if (n_mdswitches)
7273 {
7274 int i = 0;
7275
5ed6ace5 7276 mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
5bbcd587
JJ
7277 for (start = multilib_defaults; *start != '\0'; start = end + 1)
7278 {
7279 while (*start == ' ' || *start == '\t')
7280 start++;
7281
7282 if (*start == '\0')
7283 break;
1d088dee 7284
5bbcd587
JJ
7285 for (end = start + 1;
7286 *end != ' ' && *end != '\t' && *end != '\0'; end++)
7287 ;
7288
7289 obstack_grow (&multilib_obstack, start, end - start);
7290 obstack_1grow (&multilib_obstack, 0);
7973fd2a 7291 mdswitches[i].str = XOBFINISH (&multilib_obstack, const char *);
5bbcd587
JJ
7292 mdswitches[i++].len = end - start;
7293
7294 if (*end == '\0')
7295 break;
7296 }
7297 }
60103a34 7298
0a8d6618
BC
7299 p = multilib_exclusions;
7300 while (*p != '\0')
7301 {
7302 /* Ignore newlines. */
7303 if (*p == '\n')
d25a45d4
KH
7304 {
7305 ++p;
7306 continue;
7307 }
0a8d6618
BC
7308
7309 /* Check the arguments. */
7310 ok = 1;
7311 while (*p != ';')
d25a45d4
KH
7312 {
7313 if (*p == '\0')
3b5edfee
NS
7314 {
7315 invalid_exclusions:
bdc6b402 7316 fatal_error ("multilib exclusions %qs is invalid",
2dec80c7 7317 multilib_exclusions);
3b5edfee 7318 }
d25a45d4
KH
7319
7320 if (! ok)
7321 {
7322 ++p;
7323 continue;
7324 }
7325
7326 this_arg = p;
7327 while (*p != ' ' && *p != ';')
7328 {
7329 if (*p == '\0')
3b5edfee 7330 goto invalid_exclusions;
d25a45d4
KH
7331 ++p;
7332 }
7333
7334 if (*this_arg != '!')
7335 not_arg = 0;
7336 else
7337 {
7338 not_arg = 1;
7339 ++this_arg;
7340 }
9218435e 7341
0a8d6618
BC
7342 ok = used_arg (this_arg, p - this_arg);
7343 if (not_arg)
7344 ok = ! ok;
7345
d25a45d4
KH
7346 if (*p == ' ')
7347 ++p;
7348 }
0a8d6618
BC
7349
7350 if (ok)
3ac63d94 7351 return;
0a8d6618
BC
7352
7353 ++p;
7354 }
7355
5bbcd587 7356 first = 1;
0a8d6618 7357 p = multilib_select;
60103a34
DE
7358 while (*p != '\0')
7359 {
7360 /* Ignore newlines. */
7361 if (*p == '\n')
7362 {
7363 ++p;
7364 continue;
7365 }
7366
7367 /* Get the initial path. */
7368 this_path = p;
7369 while (*p != ' ')
7370 {
7371 if (*p == '\0')
3b5edfee
NS
7372 {
7373 invalid_select:
bdc6b402 7374 fatal_error ("multilib select %qs is invalid",
2dec80c7 7375 multilib_select);
3b5edfee 7376 }
60103a34
DE
7377 ++p;
7378 }
7379 this_path_len = p - this_path;
7380
7381 /* Check the arguments. */
03c42484 7382 ok = 1;
5bbcd587 7383 ndfltok = 1;
60103a34
DE
7384 ++p;
7385 while (*p != ';')
7386 {
7387 if (*p == '\0')
3b5edfee 7388 goto invalid_select;
60103a34 7389
03c42484 7390 if (! ok)
60103a34
DE
7391 {
7392 ++p;
7393 continue;
7394 }
7395
7396 this_arg = p;
7397 while (*p != ' ' && *p != ';')
7398 {
7399 if (*p == '\0')
3b5edfee 7400 goto invalid_select;
60103a34
DE
7401 ++p;
7402 }
7403
03c42484
RK
7404 if (*this_arg != '!')
7405 not_arg = 0;
60103a34 7406 else
03c42484
RK
7407 {
7408 not_arg = 1;
7409 ++this_arg;
7410 }
7411
7412 /* If this is a default argument, we can just ignore it.
7413 This is true even if this_arg begins with '!'. Beginning
7414 with '!' does not mean that this argument is necessarily
7415 inappropriate for this library: it merely means that
7416 there is a more specific library which uses this
7417 argument. If this argument is a default, we need not
7418 consider that more specific library. */
5bbcd587
JJ
7419 ok = used_arg (this_arg, p - this_arg);
7420 if (not_arg)
7421 ok = ! ok;
7422
7423 if (! ok)
7424 ndfltok = 0;
7425
7426 if (default_arg (this_arg, p - this_arg))
7427 ok = 1;
60103a34
DE
7428
7429 if (*p == ' ')
7430 ++p;
7431 }
7432
5bbcd587 7433 if (ok && first)
60103a34
DE
7434 {
7435 if (this_path_len != 1
7436 || this_path[0] != '.')
7437 {
5ed6ace5 7438 char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
5bbcd587
JJ
7439 char *q;
7440
878f32c3
KG
7441 strncpy (new_multilib_dir, this_path, this_path_len);
7442 new_multilib_dir[this_path_len] = '\0';
5bbcd587
JJ
7443 q = strchr (new_multilib_dir, ':');
7444 if (q != NULL)
7445 *q = '\0';
878f32c3 7446 multilib_dir = new_multilib_dir;
60103a34 7447 }
5bbcd587
JJ
7448 first = 0;
7449 }
7450
7451 if (ndfltok)
7452 {
7453 const char *q = this_path, *end = this_path + this_path_len;
7454
7455 while (q < end && *q != ':')
7456 q++;
c49d2df6 7457 if (q < end)
5bbcd587 7458 {
5ed6ace5 7459 char *new_multilib_os_dir = XNEWVEC (char, end - q);
c49d2df6
JJ
7460 memcpy (new_multilib_os_dir, q + 1, end - q - 1);
7461 new_multilib_os_dir[end - q - 1] = '\0';
5bbcd587
JJ
7462 multilib_os_dir = new_multilib_os_dir;
7463 break;
7464 }
60103a34
DE
7465 }
7466
7467 ++p;
9218435e 7468 }
5bbcd587
JJ
7469
7470 if (multilib_dir == NULL && multilib_os_dir != NULL
7471 && strcmp (multilib_os_dir, ".") == 0)
7472 {
b1d5455a 7473 free (CONST_CAST (char *, multilib_os_dir));
5bbcd587
JJ
7474 multilib_os_dir = NULL;
7475 }
7476 else if (multilib_dir != NULL && multilib_os_dir == NULL)
7477 multilib_os_dir = multilib_dir;
60103a34
DE
7478}
7479
7480/* Print out the multiple library subdirectory selection
7481 information. This prints out a series of lines. Each line looks
7482 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
7483 required. Only the desired options are printed out, the negative
7484 matches. The options are print without a leading dash. There are
7485 no spaces to make it easy to use the information in the shell.
7486 Each subdirectory is printed only once. This assumes the ordering
0a8d6618
BC
7487 generated by the genmultilib script. Also, we leave out ones that match
7488 the exclusions. */
60103a34
DE
7489
7490static void
1d088dee 7491print_multilib_info (void)
60103a34 7492{
3b304f5b
ZW
7493 const char *p = multilib_select;
7494 const char *last_path = 0, *this_path;
03c42484 7495 int skip;
3ac63d94 7496 unsigned int last_path_len = 0;
60103a34
DE
7497
7498 while (*p != '\0')
7499 {
0a8d6618 7500 skip = 0;
60103a34
DE
7501 /* Ignore newlines. */
7502 if (*p == '\n')
7503 {
7504 ++p;
7505 continue;
7506 }
7507
7508 /* Get the initial path. */
7509 this_path = p;
7510 while (*p != ' ')
7511 {
7512 if (*p == '\0')
3b5edfee
NS
7513 {
7514 invalid_select:
bdc6b402 7515 fatal_error ("multilib select %qs is invalid", multilib_select);
3b5edfee 7516 }
7904f95f 7517
60103a34
DE
7518 ++p;
7519 }
7520
c49d2df6
JJ
7521 /* When --disable-multilib was used but target defines
7522 MULTILIB_OSDIRNAMES, entries starting with .: are there just
7523 to find multilib_os_dir, so skip them from output. */
7524 if (this_path[0] == '.' && this_path[1] == ':')
7525 skip = 1;
7526
0a8d6618
BC
7527 /* Check for matches with the multilib_exclusions. We don't bother
7528 with the '!' in either list. If any of the exclusion rules match
7529 all of its options with the select rule, we skip it. */
d25a45d4
KH
7530 {
7531 const char *e = multilib_exclusions;
7532 const char *this_arg;
0a8d6618 7533
d25a45d4
KH
7534 while (*e != '\0')
7535 {
7536 int m = 1;
7537 /* Ignore newlines. */
7538 if (*e == '\n')
7539 {
7540 ++e;
7541 continue;
7542 }
0a8d6618 7543
d25a45d4
KH
7544 /* Check the arguments. */
7545 while (*e != ';')
7546 {
7547 const char *q;
7548 int mp = 0;
0a8d6618 7549
d25a45d4 7550 if (*e == '\0')
3b5edfee
NS
7551 {
7552 invalid_exclusion:
bdc6b402 7553 fatal_error ("multilib exclusion %qs is invalid",
2dec80c7 7554 multilib_exclusions);
3b5edfee 7555 }
9218435e 7556
d25a45d4
KH
7557 if (! m)
7558 {
7559 ++e;
7560 continue;
7561 }
0a8d6618 7562
d25a45d4 7563 this_arg = e;
9218435e 7564
d25a45d4
KH
7565 while (*e != ' ' && *e != ';')
7566 {
7567 if (*e == '\0')
3b5edfee 7568 goto invalid_exclusion;
d25a45d4
KH
7569 ++e;
7570 }
0a8d6618 7571
d25a45d4
KH
7572 q = p + 1;
7573 while (*q != ';')
7574 {
7575 const char *arg;
7576 int len = e - this_arg;
0a8d6618 7577
d25a45d4 7578 if (*q == '\0')
3b5edfee 7579 goto invalid_select;
0a8d6618 7580
d25a45d4
KH
7581 arg = q;
7582
7583 while (*q != ' ' && *q != ';')
7584 {
7585 if (*q == '\0')
3b5edfee 7586 goto invalid_select;
0a8d6618 7587 ++q;
d25a45d4 7588 }
0a8d6618 7589
3b5edfee
NS
7590 if (! strncmp (arg, this_arg,
7591 (len < q - arg) ? q - arg : len)
7592 || default_arg (this_arg, e - this_arg))
d25a45d4
KH
7593 {
7594 mp = 1;
7595 break;
7596 }
0a8d6618 7597
d25a45d4
KH
7598 if (*q == ' ')
7599 ++q;
7600 }
9218435e 7601
d25a45d4
KH
7602 if (! mp)
7603 m = 0;
0a8d6618 7604
d25a45d4
KH
7605 if (*e == ' ')
7606 ++e;
7607 }
7608
7609 if (m)
7610 {
7611 skip = 1;
7612 break;
7613 }
7614
7615 if (*e != '\0')
7616 ++e;
7617 }
7618 }
0a8d6618
BC
7619
7620 if (! skip)
d25a45d4
KH
7621 {
7622 /* If this is a duplicate, skip it. */
3b5edfee
NS
7623 skip = (last_path != 0
7624 && (unsigned int) (p - this_path) == last_path_len
d25a45d4 7625 && ! strncmp (last_path, this_path, last_path_len));
60103a34 7626
d25a45d4
KH
7627 last_path = this_path;
7628 last_path_len = p - this_path;
0a8d6618 7629 }
60103a34 7630
03c42484
RK
7631 /* If this directory requires any default arguments, we can skip
7632 it. We will already have printed a directory identical to
7633 this one which does not require that default argument. */
7634 if (! skip)
7635 {
3b304f5b 7636 const char *q;
03c42484
RK
7637
7638 q = p + 1;
7639 while (*q != ';')
7640 {
3b304f5b 7641 const char *arg;
03c42484
RK
7642
7643 if (*q == '\0')
3b5edfee 7644 goto invalid_select;
03c42484
RK
7645
7646 if (*q == '!')
7647 arg = NULL;
7648 else
7649 arg = q;
7650
7651 while (*q != ' ' && *q != ';')
7652 {
7653 if (*q == '\0')
3b5edfee 7654 goto invalid_select;
03c42484
RK
7655 ++q;
7656 }
7657
7658 if (arg != NULL
7659 && default_arg (arg, q - arg))
7660 {
7661 skip = 1;
7662 break;
7663 }
7664
7665 if (*q == ' ')
7666 ++q;
7667 }
7668 }
7669
60103a34
DE
7670 if (! skip)
7671 {
3b304f5b 7672 const char *p1;
60103a34 7673
5bbcd587 7674 for (p1 = last_path; p1 < p && *p1 != ':'; p1++)
60103a34
DE
7675 putchar (*p1);
7676 putchar (';');
7677 }
7678
7679 ++p;
7680 while (*p != ';')
7681 {
7682 int use_arg;
7683
7684 if (*p == '\0')
3b5edfee 7685 goto invalid_select;
60103a34
DE
7686
7687 if (skip)
7688 {
7689 ++p;
7690 continue;
7691 }
7692
7693 use_arg = *p != '!';
7694
7695 if (use_arg)
7696 putchar ('@');
7697
7698 while (*p != ' ' && *p != ';')
7699 {
7700 if (*p == '\0')
3b5edfee 7701 goto invalid_select;
60103a34
DE
7702 if (use_arg)
7703 putchar (*p);
7704 ++p;
7705 }
7706
7707 if (*p == ' ')
7708 ++p;
7709 }
7710
7711 if (! skip)
961b7009 7712 {
3ac63d94 7713 /* If there are extra options, print them now. */
961b7009
MM
7714 if (multilib_extra && *multilib_extra)
7715 {
7716 int print_at = TRUE;
3b304f5b 7717 const char *q;
961b7009
MM
7718
7719 for (q = multilib_extra; *q != '\0'; q++)
7720 {
7721 if (*q == ' ')
7722 print_at = TRUE;
7723 else
7724 {
7725 if (print_at)
7726 putchar ('@');
7727 putchar (*q);
7728 print_at = FALSE;
7729 }
7730 }
7731 }
9218435e 7732
961b7009
MM
7733 putchar ('\n');
7734 }
60103a34
DE
7735
7736 ++p;
7737 }
7738}
f3226a90 7739\f
30d8946b
MM
7740/* getenv built-in spec function.
7741
7742 Returns the value of the environment variable given by its first
7743 argument, concatenated with the second argument. If the
7744 environment variable is not defined, a fatal error is issued. */
7745
7746static const char *
7747getenv_spec_function (int argc, const char **argv)
7748{
7749 char *value;
5557813a
NS
7750 char *result;
7751 char *ptr;
7752 size_t len;
30d8946b
MM
7753
7754 if (argc != 2)
7755 return NULL;
7756
7757 value = getenv (argv[0]);
7758 if (!value)
bdc6b402 7759 fatal_error ("environment variable %qs not defined", argv[0]);
30d8946b 7760
5557813a 7761 /* We have to escape every character of the environment variable so
fa10beec
RW
7762 they are not interpreted as active spec characters. A
7763 particularly painful case is when we are reading a variable
5557813a
NS
7764 holding a windows path complete with \ separators. */
7765 len = strlen (value) * 2 + strlen (argv[1]) + 1;
e1e4cdc4 7766 result = XNEWVAR (char, len);
5557813a
NS
7767 for (ptr = result; *value; ptr += 2)
7768 {
7769 ptr[0] = '\\';
7770 ptr[1] = *value++;
7771 }
b8698a0f 7772
5557813a 7773 strcpy (ptr, argv[1]);
b8698a0f 7774
5557813a 7775 return result;
30d8946b
MM
7776}
7777
f3226a90
JT
7778/* if-exists built-in spec function.
7779
7780 Checks to see if the file specified by the absolute pathname in
7781 ARGS exists. Returns that pathname if found.
7782
7783 The usual use for this function is to check for a library file
7784 (whose name has been expanded with %s). */
7785
7786static const char *
1d088dee 7787if_exists_spec_function (int argc, const char **argv)
f3226a90
JT
7788{
7789 /* Must have only one argument. */
3dce1408 7790 if (argc == 1 && IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
f3226a90
JT
7791 return argv[0];
7792
7793 return NULL;
7794}
152a5a9c
JT
7795
7796/* if-exists-else built-in spec function.
7797
7798 This is like if-exists, but takes an additional argument which
7799 is returned if the first argument does not exist. */
7800
7801static const char *
1d088dee 7802if_exists_else_spec_function (int argc, const char **argv)
152a5a9c
JT
7803{
7804 /* Must have exactly two arguments. */
7805 if (argc != 2)
7806 return NULL;
7807
3dce1408 7808 if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
152a5a9c
JT
7809 return argv[0];
7810
7811 return argv[1];
7812}
3dd53121
AP
7813
7814/* replace-outfile built-in spec function.
ed5b9f96
GK
7815
7816 This looks for the first argument in the outfiles array's name and
7817 replaces it with the second argument. */
3dd53121
AP
7818
7819static const char *
7820replace_outfile_spec_function (int argc, const char **argv)
7821{
7822 int i;
7823 /* Must have exactly two arguments. */
7824 if (argc != 2)
7825 abort ();
7904f95f 7826
3dd53121
AP
7827 for (i = 0; i < n_infiles; i++)
7828 {
7829 if (outfiles[i] && !strcmp (outfiles[i], argv[0]))
7830 outfiles[i] = xstrdup (argv[1]);
7831 }
7832 return NULL;
7833}
7834
2642f659
JH
7835/* remove-outfile built-in spec function.
7836 *
7837 * This looks for the first argument in the outfiles array's name and
7838 * removes it. */
7839
7840static const char *
7841remove_outfile_spec_function (int argc, const char **argv)
7842{
7843 int i;
7844 /* Must have exactly one argument. */
7845 if (argc != 1)
7846 abort ();
7847
7848 for (i = 0; i < n_infiles; i++)
7849 {
7850 if (outfiles[i] && !strcmp (outfiles[i], argv[0]))
7851 outfiles[i] = NULL;
7852 }
7853 return NULL;
7854}
7855
7904f95f 7856/* Given two version numbers, compares the two numbers.
ed5b9f96
GK
7857 A version number must match the regular expression
7858 ([1-9][0-9]*|0)(\.([1-9][0-9]*|0))*
7859*/
7860static int
7861compare_version_strings (const char *v1, const char *v2)
7862{
7863 int rresult;
7864 regex_t r;
7904f95f 7865
ed5b9f96
GK
7866 if (regcomp (&r, "^([1-9][0-9]*|0)(\\.([1-9][0-9]*|0))*$",
7867 REG_EXTENDED | REG_NOSUB) != 0)
7868 abort ();
7869 rresult = regexec (&r, v1, 0, NULL, 0);
7870 if (rresult == REG_NOMATCH)
bdc6b402 7871 fatal_error ("invalid version number %qs", v1);
ed5b9f96
GK
7872 else if (rresult != 0)
7873 abort ();
7874 rresult = regexec (&r, v2, 0, NULL, 0);
7875 if (rresult == REG_NOMATCH)
bdc6b402 7876 fatal_error ("invalid version number %qs", v2);
ed5b9f96
GK
7877 else if (rresult != 0)
7878 abort ();
7879
7880 return strverscmp (v1, v2);
7881}
7882
7883
7884/* version_compare built-in spec function.
7885
7886 This takes an argument of the following form:
7887
7888 <comparison-op> <arg1> [<arg2>] <switch> <result>
7889
7890 and produces "result" if the comparison evaluates to true,
7891 and nothing if it doesn't.
7892
7893 The supported <comparison-op> values are:
7904f95f 7894
ed5b9f96
GK
7895 >= true if switch is a later (or same) version than arg1
7896 !> opposite of >=
7897 < true if switch is an earlier version than arg1
7898 !< opposite of <
7899 >< true if switch is arg1 or later, and earlier than arg2
7900 <> true if switch is earlier than arg1 or is arg2 or later
7901
7902 If the switch is not present, the condition is false unless
7903 the first character of the <comparison-op> is '!'.
7904
7905 For example,
7906 %:version-compare(>= 10.3 mmacosx-version-min= -lmx)
7907 adds -lmx if -mmacosx-version-min=10.3.9 was passed. */
7908
7909static const char *
7910version_compare_spec_function (int argc, const char **argv)
7911{
7912 int comp1, comp2;
7913 size_t switch_len;
7914 const char *switch_value = NULL;
7915 int nargs = 1, i;
7916 bool result;
7917
7918 if (argc < 3)
2dec80c7 7919 fatal_error ("too few arguments to %%:version-compare");
ed5b9f96
GK
7920 if (argv[0][0] == '\0')
7921 abort ();
7922 if ((argv[0][1] == '<' || argv[0][1] == '>') && argv[0][0] != '!')
7923 nargs = 2;
7924 if (argc != nargs + 3)
2dec80c7 7925 fatal_error ("too many arguments to %%:version-compare");
ed5b9f96
GK
7926
7927 switch_len = strlen (argv[nargs + 1]);
7928 for (i = 0; i < n_switches; i++)
7929 if (!strncmp (switches[i].part1, argv[nargs + 1], switch_len)
7930 && check_live_switch (i, switch_len))
7931 switch_value = switches[i].part1 + switch_len;
7932
7933 if (switch_value == NULL)
7934 comp1 = comp2 = -1;
7935 else
7936 {
7937 comp1 = compare_version_strings (switch_value, argv[1]);
7938 if (nargs == 2)
7939 comp2 = compare_version_strings (switch_value, argv[2]);
7940 else
7941 comp2 = -1; /* This value unused. */
7942 }
7943
7944 switch (argv[0][0] << 8 | argv[0][1])
7945 {
7946 case '>' << 8 | '=':
7947 result = comp1 >= 0;
7948 break;
7949 case '!' << 8 | '<':
7950 result = comp1 >= 0 || switch_value == NULL;
7951 break;
7952 case '<' << 8:
7953 result = comp1 < 0;
7954 break;
7955 case '!' << 8 | '>':
7956 result = comp1 < 0 || switch_value == NULL;
7957 break;
7958 case '>' << 8 | '<':
7959 result = comp1 >= 0 && comp2 < 0;
7960 break;
7961 case '<' << 8 | '>':
7962 result = comp1 < 0 || comp2 >= 0;
7963 break;
7904f95f 7964
ed5b9f96 7965 default:
bdc6b402 7966 fatal_error ("unknown operator %qs in %%:version-compare", argv[0]);
ed5b9f96
GK
7967 }
7968 if (! result)
7969 return NULL;
7970
7971 return argv[nargs + 2];
7972}
953ff289
DN
7973
7974/* %:include builtin spec function. This differs from %include in that it
7975 can be nested inside a spec, and thus be conditionalized. It takes
7976 one argument, the filename, and looks for it in the startfile path.
7977 The result is always NULL, i.e. an empty expansion. */
7978
7979static const char *
7980include_spec_function (int argc, const char **argv)
7981{
7982 char *file;
7983
7984 if (argc != 1)
7985 abort ();
7986
4d2b059d 7987 file = find_a_file (&startfile_prefixes, argv[0], R_OK, true);
953ff289
DN
7988 read_specs (file ? file : argv[0], FALSE);
7989
7990 return NULL;
7991}
a0f87454 7992
c1ce46a5 7993/* %:find-file spec function. This function replaces its argument by
4adbd5dd
MK
7994 the file found thru find_file, that is the -print-file-name gcc
7995 program option. */
7996static const char *
c1ce46a5 7997find_file_spec_function (int argc, const char **argv)
4adbd5dd
MK
7998{
7999 const char *file;
8000
8001 if (argc != 1)
8002 abort ();
8003
8004 file = find_file (argv[0]);
8005 return file;
8006}
8007
8008
c1ce46a5
MK
8009/* %:find-plugindir spec function. This function replaces its argument
8010 by the -iplugindir=<dir> option. `dir' is found thru find_file, that
8011 is the -print-file-name gcc program option. */
8012static const char *
8013find_plugindir_spec_function (int argc, const char **argv ATTRIBUTE_UNUSED)
8014{
8015 const char *option;
8016
8017 if (argc != 0)
8018 abort ();
8019
8020 option = concat ("-iplugindir=", find_file ("plugin"), NULL);
8021 return option;
8022}
8023
8024
a0f87454
RS
8025/* %:print-asm-header spec function. Print a banner to say that the
8026 following output is from the assembler. */
8027
8028static const char *
8029print_asm_header_spec_function (int arg ATTRIBUTE_UNUSED,
8030 const char **argv ATTRIBUTE_UNUSED)
8031{
4dad0aca 8032 printf (_("Assembler options\n=================\n\n"));
a0f87454
RS
8033 printf (_("Use \"-Wa,OPTION\" to pass \"OPTION\" to the assembler.\n\n"));
8034 fflush (stdout);
8035 return NULL;
8036}
2153915d 8037
b5b8b0ac
AO
8038/* Compute a timestamp to initialize flag_random_seed. */
8039
8040static unsigned
8041get_local_tick (void)
8042{
8043 unsigned ret = 0;
8044
8045 /* Get some more or less random data. */
8046#ifdef HAVE_GETTIMEOFDAY
8047 {
8048 struct timeval tv;
8049
8050 gettimeofday (&tv, NULL);
8051 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
8052 }
8053#else
8054 {
8055 time_t now = time (NULL);
8056
8057 if (now != (time_t)-1)
8058 ret = (unsigned) now;
8059 }
8060#endif
8061
8062 return ret;
8063}
8064
2153915d
AO
8065/* %:compare-debug-dump-opt spec function. Save the last argument,
8066 expected to be the last -fdump-final-insns option, or generate a
8067 temporary. */
8068
8069static const char *
8070compare_debug_dump_opt_spec_function (int arg,
8071 const char **argv ATTRIBUTE_UNUSED)
8072{
8073 const char *ret;
8074 char *name;
8075 int which;
b5b8b0ac 8076 static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
2153915d
AO
8077
8078 if (arg != 0)
2dec80c7 8079 fatal_error ("too many arguments to %%:compare-debug-dump-opt");
2153915d 8080
2153915d
AO
8081 do_spec_2 ("%{fdump-final-insns=*:%*}");
8082 do_spec_1 (" ", 0, NULL);
8083
5b634ee0
JM
8084 if (VEC_length (const_char_p, argbuf) > 0
8085 && strcmp (argv[VEC_length (const_char_p, argbuf) - 1], "."))
2153915d 8086 {
b5b8b0ac
AO
8087 if (!compare_debug)
8088 return NULL;
8089
5b634ee0 8090 name = xstrdup (argv[VEC_length (const_char_p, argbuf) - 1]);
2153915d
AO
8091 ret = NULL;
8092 }
8093 else
8094 {
b5b8b0ac
AO
8095 const char *ext = NULL;
8096
5b634ee0 8097 if (VEC_length (const_char_p, argbuf) > 0)
b5b8b0ac
AO
8098 {
8099 do_spec_2 ("%{o*:%*}%{!o:%{!S:%b%O}%{S:%b.s}}");
8100 ext = ".gkd";
8101 }
8102 else if (!compare_debug)
8103 return NULL;
8104 else
8105 do_spec_2 ("%g.gkd");
2153915d 8106
2153915d 8107 do_spec_1 (" ", 0, NULL);
2153915d 8108
5b634ee0 8109 gcc_assert (VEC_length (const_char_p, argbuf) > 0);
2153915d 8110
5b634ee0 8111 name = concat (VEC_last (const_char_p, argbuf), ext, NULL);
b5b8b0ac
AO
8112
8113 ret = concat ("-fdump-final-insns=", name, NULL);
2153915d
AO
8114 }
8115
8116 which = compare_debug < 0;
8117 debug_check_temp_file[which] = name;
8118
b5b8b0ac
AO
8119 if (!which)
8120 {
8121 unsigned HOST_WIDE_INT value = get_local_tick () ^ getpid ();
8122
8123 sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
8124 }
8125
8126 if (*random_seed)
8127 ret = concat ("%{!frandom-seed=*:-frandom-seed=", random_seed, "} ",
8128 ret, NULL);
8129
8130 if (which)
8131 *random_seed = 0;
2153915d
AO
8132
8133 return ret;
8134}
8135
8136static const char *debug_auxbase_opt;
8137
8138/* %:compare-debug-self-opt spec function. Expands to the options
8139 that are to be passed in the second compilation of
8140 compare-debug. */
8141
8142static const char *
8143compare_debug_self_opt_spec_function (int arg,
8144 const char **argv ATTRIBUTE_UNUSED)
8145{
8146 if (arg != 0)
2dec80c7 8147 fatal_error ("too many arguments to %%:compare-debug-self-opt");
2153915d
AO
8148
8149 if (compare_debug >= 0)
8150 return NULL;
8151
8152 do_spec_2 ("%{c|S:%{o*:%*}}");
8153 do_spec_1 (" ", 0, NULL);
8154
5b634ee0 8155 if (VEC_length (const_char_p, argbuf) > 0)
2153915d 8156 debug_auxbase_opt = concat ("-auxbase-strip ",
5b634ee0 8157 VEC_last (const_char_p, argbuf),
2153915d
AO
8158 NULL);
8159 else
8160 debug_auxbase_opt = NULL;
8161
8162 return concat ("\
8163%<o %<MD %<MMD %<MF* %<MG %<MP %<MQ* %<MT* \
8164%<fdump-final-insns=* -w -S -o %j \
8165%{!fcompare-debug-second:-fcompare-debug-second} \
8166", compare_debug_opt, NULL);
8167}
8168
8169/* %:compare-debug-auxbase-opt spec function. Expands to the auxbase
8170 options that are to be passed in the second compilation of
8171 compare-debug. It expects, as an argument, the basename of the
8172 current input file name, with the .gk suffix appended to it. */
8173
8174static const char *
8175compare_debug_auxbase_opt_spec_function (int arg,
8176 const char **argv)
8177{
8178 char *name;
8179 int len;
8180
8181 if (arg == 0)
2dec80c7 8182 fatal_error ("too few arguments to %%:compare-debug-auxbase-opt");
2153915d
AO
8183
8184 if (arg != 1)
2dec80c7 8185 fatal_error ("too many arguments to %%:compare-debug-auxbase-opt");
2153915d
AO
8186
8187 if (compare_debug >= 0)
8188 return NULL;
8189
8190 len = strlen (argv[0]);
8191 if (len < 3 || strcmp (argv[0] + len - 3, ".gk") != 0)
2dec80c7
JM
8192 fatal_error ("argument to %%:compare-debug-auxbase-opt "
8193 "does not end in .gk");
2153915d
AO
8194
8195 if (debug_auxbase_opt)
8196 return debug_auxbase_opt;
8197
8198#define OPT "-auxbase "
8199
8200 len -= 3;
8201 name = (char*) xmalloc (sizeof (OPT) + len);
8202 memcpy (name, OPT, sizeof (OPT) - 1);
8203 memcpy (name + sizeof (OPT) - 1, argv[0], len);
8204 name[sizeof (OPT) - 1 + len] = '\0';
8205
b5b8b0ac
AO
8206#undef OPT
8207
2153915d
AO
8208 return name;
8209}