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