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