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