2011-05-06 Gary Funck <gary@intrepid.com>
[official-gcc.git] / gcc / gcc.c
blob01144fbdb264c484dd82c7caac206a6d471b4252
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, 2011
5 Free Software Foundation, Inc.
7 This file is part of GCC.
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.
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.
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/>. */
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.
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". */
33 #include "config.h"
34 #include "system.h"
35 #include "coretypes.h"
36 #include "multilib.h" /* before tm.h */
37 #include "tm.h"
38 #include "xregex.h"
39 #include "obstack.h"
40 #include "intl.h"
41 #include "prefix.h"
42 #include "gcc.h"
43 #include "diagnostic.h"
44 #include "flags.h"
45 #include "opts.h"
46 #include "vec.h"
47 #include "filenames.h"
49 /* By default there is no special suffix for target executables. */
50 /* FIXME: when autoconf is fixed, remove the host check - dj */
51 #if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX)
52 #define HAVE_TARGET_EXECUTABLE_SUFFIX
53 #endif
55 /* By default there is no special suffix for host executables. */
56 #ifdef HOST_EXECUTABLE_SUFFIX
57 #define HAVE_HOST_EXECUTABLE_SUFFIX
58 #else
59 #define HOST_EXECUTABLE_SUFFIX ""
60 #endif
62 /* By default, the suffix for target object files is ".o". */
63 #ifdef TARGET_OBJECT_SUFFIX
64 #define HAVE_TARGET_OBJECT_SUFFIX
65 #else
66 #define TARGET_OBJECT_SUFFIX ".o"
67 #endif
69 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
71 /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
72 #ifndef LIBRARY_PATH_ENV
73 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
74 #endif
76 /* If a stage of compilation returns an exit status >= 1,
77 compilation of that file ceases. */
79 #define MIN_FATAL_STATUS 1
81 /* Flag set by cppspec.c to 1. */
82 int is_cpp_driver;
84 /* Flag set to nonzero if an @file argument has been supplied to gcc. */
85 static bool at_file_supplied;
87 /* Definition of string containing the arguments given to configure. */
88 #include "configargs.h"
90 /* Flag saying to print the command line options understood by gcc and its
91 sub-processes. */
93 static int print_help_list;
95 /* Flag saying to print the version of gcc and its sub-processes. */
97 static int print_version;
99 /* Flag indicating whether we should ONLY print the command and
100 arguments (like verbose_flag) without executing the command.
101 Displayed arguments are quoted so that the generated command
102 line is suitable for execution. This is intended for use in
103 shell scripts to capture the driver-generated command line. */
104 static int verbose_only_flag;
106 /* Flag indicating how to print command line options of sub-processes. */
108 static int print_subprocess_help;
110 /* Whether we should report subprocess execution times to a file. */
112 FILE *report_times_to_file = NULL;
114 /* Nonzero means place this string before uses of /, so that include
115 and library files can be found in an alternate location. */
117 #ifdef TARGET_SYSTEM_ROOT
118 static const char *target_system_root = TARGET_SYSTEM_ROOT;
119 #else
120 static const char *target_system_root = 0;
121 #endif
123 /* Nonzero means pass the updated target_system_root to the compiler. */
125 static int target_system_root_changed;
127 /* Nonzero means append this string to target_system_root. */
129 static const char *target_sysroot_suffix = 0;
131 /* Nonzero means append this string to target_system_root for headers. */
133 static const char *target_sysroot_hdrs_suffix = 0;
135 /* Nonzero means write "temp" files in source directory
136 and use the source file's name in them, and don't delete them. */
138 static enum save_temps {
139 SAVE_TEMPS_NONE, /* no -save-temps */
140 SAVE_TEMPS_CWD, /* -save-temps in current directory */
141 SAVE_TEMPS_OBJ /* -save-temps in object directory */
142 } save_temps_flag;
144 /* Output file to use to get the object directory for -save-temps=obj */
145 static char *save_temps_prefix = 0;
146 static size_t save_temps_length = 0;
148 /* The compiler version. */
150 static const char *compiler_version;
152 /* The target version. */
154 static const char *const spec_version = DEFAULT_TARGET_VERSION;
156 /* The target machine. */
158 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
160 /* Nonzero if cross-compiling.
161 When -b is used, the value comes from the `specs' file. */
163 #ifdef CROSS_DIRECTORY_STRUCTURE
164 static const char *cross_compile = "1";
165 #else
166 static const char *cross_compile = "0";
167 #endif
169 /* Greatest exit code of sub-processes that has been encountered up to
170 now. */
171 static int greatest_status = 1;
173 /* This is the obstack which we use to allocate many strings. */
175 static struct obstack obstack;
177 /* This is the obstack to build an environment variable to pass to
178 collect2 that describes all of the relevant switches of what to
179 pass the compiler in building the list of pointers to constructors
180 and destructors. */
182 static struct obstack collect_obstack;
184 /* Forward declaration for prototypes. */
185 struct path_prefix;
186 struct prefix_list;
188 static void init_spec (void);
189 static void store_arg (const char *, int, int);
190 static void insert_wrapper (const char *);
191 static char *load_specs (const char *);
192 static void read_specs (const char *, int);
193 static void set_spec (const char *, const char *);
194 static struct compiler *lookup_compiler (const char *, size_t, const char *);
195 static char *build_search_list (const struct path_prefix *, const char *,
196 bool, bool);
197 static void xputenv (const char *);
198 static void putenv_from_prefixes (const struct path_prefix *, const char *,
199 bool);
200 static int access_check (const char *, int);
201 static char *find_a_file (const struct path_prefix *, const char *, int, bool);
202 static void add_prefix (struct path_prefix *, const char *, const char *,
203 int, int, int);
204 static void add_sysrooted_prefix (struct path_prefix *, const char *,
205 const char *, int, int, int);
206 static char *skip_whitespace (char *);
207 static void delete_if_ordinary (const char *);
208 static void delete_temp_files (void);
209 static void delete_failure_queue (void);
210 static void clear_failure_queue (void);
211 static int check_live_switch (int, int);
212 static const char *handle_braces (const char *);
213 static inline bool input_suffix_matches (const char *, const char *);
214 static inline bool switch_matches (const char *, const char *, int);
215 static inline void mark_matching_switches (const char *, const char *, int);
216 static inline void process_marked_switches (void);
217 static const char *process_brace_body (const char *, const char *, const char *, int, int);
218 static const struct spec_function *lookup_spec_function (const char *);
219 static const char *eval_spec_function (const char *, const char *);
220 static const char *handle_spec_function (const char *);
221 static char *save_string (const char *, int);
222 static void set_collect_gcc_options (void);
223 static int do_spec_1 (const char *, int, const char *);
224 static int do_spec_2 (const char *);
225 static void do_option_spec (const char *, const char *);
226 static void do_self_spec (const char *);
227 static const char *find_file (const char *);
228 static int is_directory (const char *, bool);
229 static const char *validate_switches (const char *);
230 static void validate_all_switches (void);
231 static inline void validate_switches_from_spec (const char *);
232 static void give_switch (int, int);
233 static int used_arg (const char *, int);
234 static int default_arg (const char *, int);
235 static void set_multilib_dir (void);
236 static void print_multilib_info (void);
237 static void perror_with_name (const char *);
238 static void display_help (void);
239 static void add_preprocessor_option (const char *, int);
240 static void add_assembler_option (const char *, int);
241 static void add_linker_option (const char *, int);
242 static void process_command (unsigned int, struct cl_decoded_option *);
243 static int execute (void);
244 static void alloc_args (void);
245 static void clear_args (void);
246 static void fatal_signal (int);
247 #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
248 static void init_gcc_specs (struct obstack *, const char *, const char *,
249 const char *);
250 #endif
251 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
252 static const char *convert_filename (const char *, int, int);
253 #endif
255 static const char *getenv_spec_function (int, const char **);
256 static const char *if_exists_spec_function (int, const char **);
257 static const char *if_exists_else_spec_function (int, const char **);
258 static const char *replace_outfile_spec_function (int, const char **);
259 static const char *remove_outfile_spec_function (int, const char **);
260 static const char *version_compare_spec_function (int, const char **);
261 static const char *include_spec_function (int, const char **);
262 static const char *find_file_spec_function (int, const char **);
263 static const char *find_plugindir_spec_function (int, const char **);
264 static const char *print_asm_header_spec_function (int, const char **);
265 static const char *compare_debug_dump_opt_spec_function (int, const char **);
266 static const char *compare_debug_self_opt_spec_function (int, const char **);
267 static const char *compare_debug_auxbase_opt_spec_function (int, const char **);
268 static const char *pass_through_libs_spec_func (int, const char **);
270 /* The Specs Language
272 Specs are strings containing lines, each of which (if not blank)
273 is made up of a program name, and arguments separated by spaces.
274 The program name must be exact and start from root, since no path
275 is searched and it is unreliable to depend on the current working directory.
276 Redirection of input or output is not supported; the subprograms must
277 accept filenames saying what files to read and write.
279 In addition, the specs can contain %-sequences to substitute variable text
280 or for conditional text. Here is a table of all defined %-sequences.
281 Note that spaces are not generated automatically around the results of
282 expanding these sequences; therefore, you can concatenate them together
283 or with constant text in a single argument.
285 %% substitute one % into the program name or argument.
286 %i substitute the name of the input file being processed.
287 %b substitute the basename of the input file being processed.
288 This is the substring up to (and not including) the last period
289 and not including the directory unless -save-temps was specified
290 to put temporaries in a different location.
291 %B same as %b, but include the file suffix (text after the last period).
292 %gSUFFIX
293 substitute a file name that has suffix SUFFIX and is chosen
294 once per compilation, and mark the argument a la %d. To reduce
295 exposure to denial-of-service attacks, the file name is now
296 chosen in a way that is hard to predict even when previously
297 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
298 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
299 the regexp "[.0-9A-Za-z]*%O"; "%O" is treated exactly as if it
300 had been pre-processed. Previously, %g was simply substituted
301 with a file name chosen once per compilation, without regard
302 to any appended suffix (which was therefore treated just like
303 ordinary text), making such attacks more likely to succeed.
304 %|SUFFIX
305 like %g, but if -pipe is in effect, expands simply to "-".
306 %mSUFFIX
307 like %g, but if -pipe is in effect, expands to nothing. (We have both
308 %| and %m to accommodate differences between system assemblers; see
309 the AS_NEEDS_DASH_FOR_PIPED_INPUT target macro.)
310 %uSUFFIX
311 like %g, but generates a new temporary file name even if %uSUFFIX
312 was already seen.
313 %USUFFIX
314 substitutes the last file name generated with %uSUFFIX, generating a
315 new one if there is no such last file name. In the absence of any
316 %uSUFFIX, this is just like %gSUFFIX, except they don't share
317 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
318 would involve the generation of two distinct file names, one
319 for each `%g.s' and another for each `%U.s'. Previously, %U was
320 simply substituted with a file name chosen for the previous %u,
321 without regard to any appended suffix.
322 %jSUFFIX
323 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
324 writable, and if save-temps is off; otherwise, substitute the name
325 of a temporary file, just like %u. This temporary file is not
326 meant for communication between processes, but rather as a junk
327 disposal mechanism.
328 %.SUFFIX
329 substitutes .SUFFIX for the suffixes of a matched switch's args when
330 it is subsequently output with %*. SUFFIX is terminated by the next
331 space or %.
332 %d marks the argument containing or following the %d as a
333 temporary file name, so that that file will be deleted if GCC exits
334 successfully. Unlike %g, this contributes no text to the argument.
335 %w marks the argument containing or following the %w as the
336 "output file" of this compilation. This puts the argument
337 into the sequence of arguments that %o will substitute later.
338 %V indicates that this compilation produces no "output file".
339 %W{...}
340 like %{...} but mark last argument supplied within
341 as a file to be deleted on failure.
342 %o substitutes the names of all the output files, with spaces
343 automatically placed around them. You should write spaces
344 around the %o as well or the results are undefined.
345 %o is for use in the specs for running the linker.
346 Input files whose names have no recognized suffix are not compiled
347 at all, but they are included among the output files, so they will
348 be linked.
349 %O substitutes the suffix for object files. Note that this is
350 handled specially when it immediately follows %g, %u, or %U
351 (with or without a suffix argument) because of the need for
352 those to form complete file names. The handling is such that
353 %O is treated exactly as if it had already been substituted,
354 except that %g, %u, and %U do not currently support additional
355 SUFFIX characters following %O as they would following, for
356 example, `.o'.
357 %I Substitute any of -iprefix (made from GCC_EXEC_PREFIX), -isysroot
358 (made from TARGET_SYSTEM_ROOT), -isystem (made from COMPILER_PATH
359 and -B options) and -imultilib as necessary.
360 %s current argument is the name of a library or startup file of some sort.
361 Search for that file in a standard list of directories
362 and substitute the full name found.
363 %eSTR Print STR as an error message. STR is terminated by a newline.
364 Use this when inconsistent options are detected.
365 %nSTR Print STR as a notice. STR is terminated by a newline.
366 %x{OPTION} Accumulate an option for %X.
367 %X Output the accumulated linker options specified by compilations.
368 %Y Output the accumulated assembler options specified by compilations.
369 %Z Output the accumulated preprocessor options specified by compilations.
370 %a process ASM_SPEC as a spec.
371 This allows config.h to specify part of the spec for running as.
372 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
373 used here. This can be used to run a post-processor after the
374 assembler has done its job.
375 %D Dump out a -L option for each directory in startfile_prefixes.
376 If multilib_dir is set, extra entries are generated with it affixed.
377 %l process LINK_SPEC as a spec.
378 %L process LIB_SPEC as a spec.
379 %G process LIBGCC_SPEC as a spec.
380 %R Output the concatenation of target_system_root and
381 target_sysroot_suffix.
382 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
383 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
384 %C process CPP_SPEC as a spec.
385 %1 process CC1_SPEC as a spec.
386 %2 process CC1PLUS_SPEC as a spec.
387 %* substitute the variable part of a matched option. (See below.)
388 Note that each comma in the substituted string is replaced by
389 a single space.
390 %<S remove all occurrences of -S from the command line.
391 Note - this command is position dependent. % commands in the
392 spec string before this one will see -S, % commands in the
393 spec string after this one will not.
394 %>S Similar to "%<S", but keep it in the GCC command line.
395 %<S* remove all occurrences of all switches beginning with -S from the
396 command line.
397 %:function(args)
398 Call the named function FUNCTION, passing it ARGS. ARGS is
399 first processed as a nested spec string, then split into an
400 argument vector in the usual fashion. The function returns
401 a string which is processed as if it had appeared literally
402 as part of the current spec.
403 %{S} substitutes the -S switch, if that switch was given to GCC.
404 If that switch was not specified, this substitutes nothing.
405 Here S is a metasyntactic variable.
406 %{S*} substitutes all the switches specified to GCC whose names start
407 with -S. This is used for -o, -I, etc; switches that take
408 arguments. GCC considers `-o foo' as being one switch whose
409 name starts with `o'. %{o*} would substitute this text,
410 including the space; thus, two arguments would be generated.
411 %{S*&T*} likewise, but preserve order of S and T options (the order
412 of S and T in the spec is not significant). Can be any number
413 of ampersand-separated variables; for each the wild card is
414 optional. Useful for CPP as %{D*&U*&A*}.
416 %{S:X} substitutes X, if the -S switch was given to GCC.
417 %{!S:X} substitutes X, if the -S switch was NOT given to GCC.
418 %{S*:X} substitutes X if one or more switches whose names start
419 with -S was given to GCC. Normally X is substituted only
420 once, no matter how many such switches appeared. However,
421 if %* appears somewhere in X, then X will be substituted
422 once for each matching switch, with the %* replaced by the
423 part of that switch that matched the '*'.
424 %{.S:X} substitutes X, if processing a file with suffix S.
425 %{!.S:X} substitutes X, if NOT processing a file with suffix S.
426 %{,S:X} substitutes X, if processing a file which will use spec S.
427 %{!,S:X} substitutes X, if NOT processing a file which will use spec S.
429 %{S|T:X} substitutes X if either -S or -T was given to GCC. This may be
430 combined with '!', '.', ',', and '*' as above binding stronger
431 than the OR.
432 If %* appears in X, all of the alternatives must be starred, and
433 only the first matching alternative is substituted.
434 %{S:X; if S was given to GCC, substitutes X;
435 T:Y; else if T was given to GCC, substitutes Y;
436 :D} else substitutes D. There can be as many clauses as you need.
437 This may be combined with '.', '!', ',', '|', and '*' as above.
439 %(Spec) processes a specification defined in a specs file as *Spec:
440 %[Spec] as above, but put __ around -D arguments
442 The conditional text X in a %{S:X} or similar construct may contain
443 other nested % constructs or spaces, or even newlines. They are
444 processed as usual, as described above. Trailing white space in X is
445 ignored. White space may also appear anywhere on the left side of the
446 colon in these constructs, except between . or * and the corresponding
447 word.
449 The -O, -f, -m, and -W switches are handled specifically in these
450 constructs. If another value of -O or the negated form of a -f, -m, or
451 -W switch is found later in the command line, the earlier switch
452 value is ignored, except with {S*} where S is just one letter; this
453 passes all matching options.
455 The character | at the beginning of the predicate text is used to indicate
456 that a command should be piped to the following command, but only if -pipe
457 is specified.
459 Note that it is built into GCC which switches take arguments and which
460 do not. You might think it would be useful to generalize this to
461 allow each compiler's spec to say which switches take arguments. But
462 this cannot be done in a consistent fashion. GCC cannot even decide
463 which input files have been specified without knowing which switches
464 take arguments, and it must know which input files to compile in order
465 to tell which compilers to run.
467 GCC also knows implicitly that arguments starting in `-l' are to be
468 treated as compiler output files, and passed to the linker in their
469 proper position among the other output files. */
471 /* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
473 /* config.h can define ASM_SPEC to provide extra args to the assembler
474 or extra switch-translations. */
475 #ifndef ASM_SPEC
476 #define ASM_SPEC ""
477 #endif
479 /* config.h can define ASM_FINAL_SPEC to run a post processor after
480 the assembler has run. */
481 #ifndef ASM_FINAL_SPEC
482 #define ASM_FINAL_SPEC ""
483 #endif
485 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
486 or extra switch-translations. */
487 #ifndef CPP_SPEC
488 #define CPP_SPEC ""
489 #endif
491 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
492 or extra switch-translations. */
493 #ifndef CC1_SPEC
494 #define CC1_SPEC ""
495 #endif
497 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
498 or extra switch-translations. */
499 #ifndef CC1PLUS_SPEC
500 #define CC1PLUS_SPEC ""
501 #endif
503 /* config.h can define LINK_SPEC to provide extra args to the linker
504 or extra switch-translations. */
505 #ifndef LINK_SPEC
506 #define LINK_SPEC ""
507 #endif
509 /* config.h can define LIB_SPEC to override the default libraries. */
510 #ifndef LIB_SPEC
511 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
512 #endif
514 /* mudflap specs */
515 #ifndef MFWRAP_SPEC
516 /* XXX: valid only for GNU ld */
517 /* XXX: should exactly match hooks provided by libmudflap.a */
518 #define MFWRAP_SPEC " %{static: %{fmudflap|fmudflapth: \
519 --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc\
520 --wrap=mmap --wrap=munmap --wrap=alloca\
521 } %{fmudflapth: --wrap=pthread_create\
522 }} %{fmudflap|fmudflapth: --wrap=main}"
523 #endif
524 #ifndef MFLIB_SPEC
525 #define MFLIB_SPEC "%{fmudflap|fmudflapth: -export-dynamic}"
526 #endif
528 /* When using -fsplit-stack we need to wrap pthread_create, in order
529 to initialize the stack guard. We always use wrapping, rather than
530 shared library ordering, and we keep the wrapper function in
531 libgcc. This is not yet a real spec, though it could become one;
532 it is currently just stuffed into LINK_SPEC. FIXME: This wrapping
533 only works with GNU ld and gold. FIXME: This is incompatible with
534 -fmudflap when linking statically, which wants to do its own
535 wrapping. */
536 #define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
538 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
539 included. */
540 #ifndef LIBGCC_SPEC
541 #if defined(REAL_LIBGCC_SPEC)
542 #define LIBGCC_SPEC REAL_LIBGCC_SPEC
543 #elif defined(LINK_LIBGCC_SPECIAL_1)
544 /* Have gcc do the search for libgcc.a. */
545 #define LIBGCC_SPEC "libgcc.a%s"
546 #else
547 #define LIBGCC_SPEC "-lgcc"
548 #endif
549 #endif
551 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
552 #ifndef STARTFILE_SPEC
553 #define STARTFILE_SPEC \
554 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
555 #endif
557 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
558 #ifndef ENDFILE_SPEC
559 #define ENDFILE_SPEC ""
560 #endif
562 #ifndef LINKER_NAME
563 #define LINKER_NAME "collect2"
564 #endif
566 #ifdef HAVE_AS_DEBUG_PREFIX_MAP
567 #define ASM_MAP " %{fdebug-prefix-map=*:--debug-prefix-map %*}"
568 #else
569 #define ASM_MAP ""
570 #endif
572 /* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
573 to the assembler. */
574 #ifndef ASM_DEBUG_SPEC
575 # if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
576 && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
577 # define ASM_DEBUG_SPEC \
578 (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG \
579 ? "%{!g0:%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}}" ASM_MAP \
580 : "%{!g0:%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}}" ASM_MAP)
581 # else
582 # if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
583 # define ASM_DEBUG_SPEC "%{g*:%{!g0:--gstabs}}" ASM_MAP
584 # endif
585 # if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
586 # define ASM_DEBUG_SPEC "%{g*:%{!g0:--gdwarf2}}" ASM_MAP
587 # endif
588 # endif
589 #endif
590 #ifndef ASM_DEBUG_SPEC
591 # define ASM_DEBUG_SPEC ""
592 #endif
594 /* Here is the spec for running the linker, after compiling all files. */
596 /* This is overridable by the target in case they need to specify the
597 -lgcc and -lc order specially, yet not require them to override all
598 of LINK_COMMAND_SPEC. */
599 #ifndef LINK_GCC_C_SEQUENCE_SPEC
600 #define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
601 #endif
603 #ifndef LINK_SSP_SPEC
604 #ifdef TARGET_LIBC_PROVIDES_SSP
605 #define LINK_SSP_SPEC "%{fstack-protector:}"
606 #else
607 #define LINK_SSP_SPEC "%{fstack-protector|fstack-protector-all:-lssp_nonshared -lssp}"
608 #endif
609 #endif
611 #ifndef LINK_PIE_SPEC
612 #ifdef HAVE_LD_PIE
613 #define LINK_PIE_SPEC "%{pie:-pie} "
614 #else
615 #define LINK_PIE_SPEC "%{pie:} "
616 #endif
617 #endif
619 #ifndef LINK_BUILDID_SPEC
620 # if defined(HAVE_LD_BUILDID) && defined(ENABLE_LD_BUILDID)
621 # define LINK_BUILDID_SPEC "%{!r:--build-id} "
622 # endif
623 #endif
625 /* Conditional to test whether the LTO plugin is used or not.
626 FIXME: For slim LTO we will need to enable plugin unconditionally. This
627 still cause problems with PLUGIN_LD != LD and when plugin is built but
628 not useable. For GCC 4.6 we don't support slim LTO and thus we can enable
629 plugin only when LTO is enabled. We still honor explicit
630 -fuse-linker-plugin if the linker used understands -plugin. */
632 /* The linker has some plugin support. */
633 #if HAVE_LTO_PLUGIN > 0
634 /* The linker used has full plugin support, use LTO plugin by default. */
635 #if HAVE_LTO_PLUGIN == 2
636 #define PLUGIN_COND "!fno-use-linker-plugin:%{flto|flto=*|fuse-linker-plugin"
637 #define PLUGIN_COND_CLOSE "}"
638 #else
639 /* The linker used has limited plugin support, use LTO plugin with explicit
640 -fuse-linker-plugin. */
641 #define PLUGIN_COND "fuse-linker-plugin"
642 #define PLUGIN_COND_CLOSE ""
643 #endif
644 #define LINK_PLUGIN_SPEC \
645 "%{"PLUGIN_COND": \
646 -plugin %(linker_plugin_file) \
647 -plugin-opt=%(lto_wrapper) \
648 -plugin-opt=-fresolution=%u.res \
649 %{!nostdlib:%{!nodefaultlibs:%:pass-through-libs(%(link_gcc_c_sequence))}} \
650 }"PLUGIN_COND_CLOSE
651 #else
652 /* The linker used doesn't support -plugin, reject -fuse-linker-plugin. */
653 #define LINK_PLUGIN_SPEC "%{fuse-linker-plugin:\
654 %e-fuse-linker-plugin is not supported in this configuration}"
655 #endif
658 /* -u* was put back because both BSD and SysV seem to support it. */
659 /* %{static:} simply prevents an error message if the target machine
660 doesn't handle -static. */
661 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
662 scripts which exist in user specified directories, or in standard
663 directories. */
664 /* We pass any -flto flags on to the linker, which is expected
665 to understand them. In practice, this means it had better be collect2. */
666 /* %{e*} includes -export-dynamic; see comment in common.opt. */
667 #ifndef LINK_COMMAND_SPEC
668 #define LINK_COMMAND_SPEC "\
669 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
670 %(linker) " \
671 LINK_PLUGIN_SPEC \
672 "%{flto|flto=*:%<fcompare-debug*} \
673 %{flto} %{flto=*} %l " LINK_PIE_SPEC \
674 "%X %{o*} %{e*} %{N} %{n} %{r}\
675 %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}}\
676 %{!nostdlib:%{!nostartfiles:%{fupc-link:%:include(upc-crtbegin.spec)%(upc_crtbegin)}}}\
677 %{static:} %{L*} %(mfwrap) %(link_libgcc) %o\
678 %{fopenmp|ftree-parallelize-loops=*:%:include(libgomp.spec)%(link_gomp)}\
679 %{fupc-link:%:include(libupc.spec)%(link_upc)}\
680 %(mflib) " STACK_SPLIT_SPEC "\
681 %{fprofile-arcs|fprofile-generate*|coverage:-lgcov}\
682 %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
683 %{!nostdlib:%{!nostartfiles:%{fupc-link:%:include(upc-crtend.spec)%(upc_crtend)}}}\
684 %{!nostdlib:%{!nostartfiles:%E}} %{T*} }}}}}}"
685 #endif
687 #ifndef LINK_LIBGCC_SPEC
688 /* Generate -L options for startfile prefix list. */
689 # define LINK_LIBGCC_SPEC "%D"
690 #endif
692 #ifndef STARTFILE_PREFIX_SPEC
693 # define STARTFILE_PREFIX_SPEC ""
694 #endif
696 #ifndef SYSROOT_SPEC
697 # define SYSROOT_SPEC "--sysroot=%R"
698 #endif
700 #ifndef SYSROOT_SUFFIX_SPEC
701 # define SYSROOT_SUFFIX_SPEC ""
702 #endif
704 #ifndef SYSROOT_HEADERS_SUFFIX_SPEC
705 # define SYSROOT_HEADERS_SUFFIX_SPEC ""
706 #endif
708 static const char *asm_debug;
709 static const char *cpp_spec = CPP_SPEC;
710 static const char *cc1_spec = CC1_SPEC;
711 static const char *cc1plus_spec = CC1PLUS_SPEC;
712 static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
713 static const char *link_ssp_spec = LINK_SSP_SPEC;
714 static const char *asm_spec = ASM_SPEC;
715 static const char *asm_final_spec = ASM_FINAL_SPEC;
716 static const char *link_spec = LINK_SPEC;
717 static const char *lib_spec = LIB_SPEC;
718 static const char *mfwrap_spec = MFWRAP_SPEC;
719 static const char *mflib_spec = MFLIB_SPEC;
720 static const char *link_gomp_spec = "";
721 static const char *upc_crtbegin_spec = "";
722 static const char *link_upc_spec = "";
723 static const char *upc_crtend_spec = "";
724 static const char *libgcc_spec = LIBGCC_SPEC;
725 static const char *endfile_spec = ENDFILE_SPEC;
726 static const char *startfile_spec = STARTFILE_SPEC;
727 static const char *linker_name_spec = LINKER_NAME;
728 static const char *linker_plugin_file_spec = "";
729 static const char *lto_wrapper_spec = "";
730 static const char *lto_gcc_spec = "";
731 static const char *link_command_spec = LINK_COMMAND_SPEC;
732 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
733 static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
734 static const char *sysroot_spec = SYSROOT_SPEC;
735 static const char *sysroot_suffix_spec = SYSROOT_SUFFIX_SPEC;
736 static const char *sysroot_hdrs_suffix_spec = SYSROOT_HEADERS_SUFFIX_SPEC;
738 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
739 There should be no need to override these in target dependent files,
740 but we need to copy them to the specs file so that newer versions
741 of the GCC driver can correctly drive older tool chains with the
742 appropriate -B options. */
744 /* When cpplib handles traditional preprocessing, get rid of this, and
745 call cc1 (or cc1obj in objc/lang-specs.h) from the main specs so
746 that we default the front end language better. */
747 static const char *trad_capable_cpp =
748 "cc1 -E %{traditional|traditional-cpp:-traditional-cpp}";
750 /* We don't wrap .d files in %W{} since a missing .d file, and
751 therefore no dependency entry, confuses make into thinking a .o
752 file that happens to exist is up-to-date. */
753 static const char *cpp_unique_options =
754 "%{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*&F*} %{P} %I\
755 %{MD:-MD %{!o:%b.d}%{o*:%.d%*}}\
756 %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}}\
757 %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
758 %{!E:%{!M:%{!MM:%{!MT:%{!MQ:%{MD|MMD:%{o*:-MQ %*}}}}}}}\
759 %{remap} %{g3|ggdb3|gstabs3|gcoff3|gxcoff3|gvms3:-dD}\
760 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
761 %{H} %C %{D*&U*&A*} %{i*} %Z %i\
762 %{fmudflap:-D_MUDFLAP -include mf-runtime.h}\
763 %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h}\
764 %{E|M|MM:%W{o*}}";
766 /* This contains cpp options which are common with cc1_options and are passed
767 only when preprocessing only to avoid duplication. We pass the cc1 spec
768 options to the preprocessor so that it the cc1 spec may manipulate
769 options used to set target flags. Those special target flags settings may
770 in turn cause preprocessor symbols to be defined specially. */
771 static const char *cpp_options =
772 "%(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w}\
773 %{f*} %{g*:%{!g0:%{g*} %{!fno-working-directory:-fworking-directory}}} %{O*}\
774 %{undef} %{save-temps*:-fpch-preprocess}";
776 /* This contains cpp options which are not passed when the preprocessor
777 output will be used by another program. */
778 static const char *cpp_debug_options = "%{d*}";
780 /* NB: This is shared amongst all front-ends, except for Ada. */
781 static const char *cc1_options =
782 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
783 %{!iplugindir*:%{fplugin*:%:find-plugindir()}}\
784 %1 %{!Q:-quiet} %{!dumpbase:-dumpbase %B} %{d*} %{m*} %{aux-info*}\
785 %{fcompare-debug-second:%:compare-debug-auxbase-opt(%b)} \
786 %{!fcompare-debug-second:%{c|S:%{o*:-auxbase-strip %*}%{!o*:-auxbase %b}}}%{!c:%{!S:-auxbase %b}} \
787 %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs}\
788 %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
789 %{Qn:-fno-ident} %{Qy:} %{-help:--help}\
790 %{-target-help:--target-help}\
791 %{-version:--version}\
792 %{-help=*:--help=%*}\
793 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
794 %{fsyntax-only:-o %j} %{-param*}\
795 %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants}\
796 %{coverage:-fprofile-arcs -ftest-coverage}";
798 static const char *upc_options = "-lang-upc \
799 %{!fno-upc-pre-include:-include gcc-upc.h}";
801 static const char *asm_options =
802 "%{-target-help:%:print-asm-header()} "
803 #if HAVE_GNU_AS
804 /* If GNU AS is used, then convert -w (no warnings), -I, and -v
805 to the assembler equivalents. */
806 "%{v} %{w:-W} %{I*} "
807 #endif
808 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
810 static const char *invoke_as =
811 #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
812 "%{!fwpa:\
813 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
814 %{!S:-o %|.s |\n as %(asm_options) %|.s %A }\
816 #else
817 "%{!fwpa:\
818 %{fcompare-debug=*|fdump-final-insns=*:%:compare-debug-dump-opt()}\
819 %{!S:-o %|.s |\n as %(asm_options) %m.s %A }\
821 #endif
823 /* Some compilers have limits on line lengths, and the multilib_select
824 and/or multilib_matches strings can be very long, so we build them at
825 run time. */
826 static struct obstack multilib_obstack;
827 static const char *multilib_select;
828 static const char *multilib_matches;
829 static const char *multilib_defaults;
830 static const char *multilib_exclusions;
832 /* Check whether a particular argument is a default argument. */
834 #ifndef MULTILIB_DEFAULTS
835 #define MULTILIB_DEFAULTS { "" }
836 #endif
838 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
840 #ifndef DRIVER_SELF_SPECS
841 #define DRIVER_SELF_SPECS ""
842 #endif
844 /* Adding -fopenmp should imply pthreads. This is particularly important
845 for targets that use different start files and suchlike. */
846 #ifndef GOMP_SELF_SPECS
847 #define GOMP_SELF_SPECS "%{fopenmp|ftree-parallelize-loops=*: -pthread}"
848 #endif
850 static const char *const driver_self_specs[] = {
851 "%{fdump-final-insns:-fdump-final-insns=.} %<fdump-final-insns",
852 DRIVER_SELF_SPECS, CONFIGURE_SPECS, GOMP_SELF_SPECS
855 #ifndef OPTION_DEFAULT_SPECS
856 #define OPTION_DEFAULT_SPECS { "", "" }
857 #endif
859 struct default_spec
861 const char *name;
862 const char *spec;
865 static const struct default_spec
866 option_default_specs[] = { OPTION_DEFAULT_SPECS };
868 struct user_specs
870 struct user_specs *next;
871 const char *filename;
874 static struct user_specs *user_specs_head, *user_specs_tail;
877 /* Record the mapping from file suffixes for compilation specs. */
879 struct compiler
881 const char *suffix; /* Use this compiler for input files
882 whose names end in this suffix. */
884 const char *spec; /* To use this compiler, run this spec. */
886 const char *cpp_spec; /* If non-NULL, substitute this spec
887 for `%C', rather than the usual
888 cpp_spec. */
889 const int combinable; /* If nonzero, compiler can deal with
890 multiple source files at once (IMA). */
891 const int needs_preprocessing; /* If nonzero, source files need to
892 be run through a preprocessor. */
895 /* Pointer to a vector of `struct compiler' that gives the spec for
896 compiling a file, based on its suffix.
897 A file that does not end in any of these suffixes will be passed
898 unchanged to the loader and nothing else will be done to it.
900 An entry containing two 0s is used to terminate the vector.
902 If multiple entries match a file, the last matching one is used. */
904 static struct compiler *compilers;
906 /* Number of entries in `compilers', not counting the null terminator. */
908 static int n_compilers;
910 /* The default list of file name suffixes and their compilation specs. */
912 static const struct compiler default_compilers[] =
914 /* Add lists of suffixes of known languages here. If those languages
915 were not present when we built the driver, we will hit these copies
916 and be given a more meaningful error than "file not used since
917 linking is not done". */
918 {".m", "#Objective-C", 0, 0, 0}, {".mi", "#Objective-C", 0, 0, 0},
919 {".mm", "#Objective-C++", 0, 0, 0}, {".M", "#Objective-C++", 0, 0, 0},
920 {".mii", "#Objective-C++", 0, 0, 0},
921 {".cc", "#C++", 0, 0, 0}, {".cxx", "#C++", 0, 0, 0},
922 {".cpp", "#C++", 0, 0, 0}, {".cp", "#C++", 0, 0, 0},
923 {".c++", "#C++", 0, 0, 0}, {".C", "#C++", 0, 0, 0},
924 {".CPP", "#C++", 0, 0, 0}, {".ii", "#C++", 0, 0, 0},
925 {".ads", "#Ada", 0, 0, 0}, {".adb", "#Ada", 0, 0, 0},
926 {".f", "#Fortran", 0, 0, 0}, {".F", "#Fortran", 0, 0, 0},
927 {".for", "#Fortran", 0, 0, 0}, {".FOR", "#Fortran", 0, 0, 0},
928 {".ftn", "#Fortran", 0, 0, 0}, {".FTN", "#Fortran", 0, 0, 0},
929 {".fpp", "#Fortran", 0, 0, 0}, {".FPP", "#Fortran", 0, 0, 0},
930 {".f90", "#Fortran", 0, 0, 0}, {".F90", "#Fortran", 0, 0, 0},
931 {".f95", "#Fortran", 0, 0, 0}, {".F95", "#Fortran", 0, 0, 0},
932 {".f03", "#Fortran", 0, 0, 0}, {".F03", "#Fortran", 0, 0, 0},
933 {".f08", "#Fortran", 0, 0, 0}, {".F08", "#Fortran", 0, 0, 0},
934 {".r", "#Ratfor", 0, 0, 0},
935 {".p", "#Pascal", 0, 0, 0}, {".pas", "#Pascal", 0, 0, 0},
936 {".java", "#Java", 0, 0, 0}, {".class", "#Java", 0, 0, 0},
937 {".upc", "#UPC", 0, 0, 0},
938 {".zip", "#Java", 0, 0, 0}, {".jar", "#Java", 0, 0, 0},
939 {".go", "#Go", 0, 1, 0},
940 /* Next come the entries for C. */
941 {".c", "@c", 0, 0, 1},
942 {"@c",
943 /* cc1 has an integrated ISO C preprocessor. We should invoke the
944 external preprocessor if -save-temps is given. */
945 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
946 %{!E:%{!M:%{!MM:\
947 %{traditional:\
948 %eGNU C no longer supports -traditional without -E}\
949 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
950 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
951 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
952 %(cc1_options)}\
953 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
954 cc1 %(cpp_unique_options) %(cc1_options)}}}\
955 %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 1},
956 {"-",
957 "%{!E:%e-E or -x required when input is from standard input}\
958 %(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)", 0, 0, 0},
959 {".h", "@c-header", 0, 0, 0},
960 {"@c-header",
961 /* cc1 has an integrated ISO C preprocessor. We should invoke the
962 external preprocessor if -save-temps is given. */
963 "%{E|M|MM:%(trad_capable_cpp) %(cpp_options) %(cpp_debug_options)}\
964 %{!E:%{!M:%{!MM:\
965 %{save-temps*|traditional-cpp|no-integrated-cpp:%(trad_capable_cpp) \
966 %(cpp_options) -o %{save-temps*:%b.i} %{!save-temps*:%g.i} \n\
967 cc1 -fpreprocessed %{save-temps*:%b.i} %{!save-temps*:%g.i} \
968 %(cc1_options)\
969 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
970 %W{o*:--output-pch=%*}}%V}\
971 %{!save-temps*:%{!traditional-cpp:%{!no-integrated-cpp:\
972 cc1 %(cpp_unique_options) %(cc1_options)\
973 %{!fdump-ada-spec*:-o %g.s %{!o*:--output-pch=%i.gch}\
974 %W{o*:--output-pch=%*}}%V}}}}}}", 0, 0, 0},
975 {".i", "@cpp-output", 0, 0, 0},
976 {"@cpp-output",
977 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0, 0, 0},
978 {".s", "@assembler", 0, 0, 0},
979 {"@assembler",
980 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0, 0, 0},
981 {".sx", "@assembler-with-cpp", 0, 0, 0},
982 {".S", "@assembler-with-cpp", 0, 0, 0},
983 {"@assembler-with-cpp",
984 #ifdef AS_NEEDS_DASH_FOR_PIPED_INPUT
985 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
986 %{E|M|MM:%(cpp_debug_options)}\
987 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
988 as %(asm_debug) %(asm_options) %|.s %A }}}}"
989 #else
990 "%(trad_capable_cpp) -lang-asm %(cpp_options) -fno-directives-only\
991 %{E|M|MM:%(cpp_debug_options)}\
992 %{!M:%{!MM:%{!E:%{!S:-o %|.s |\n\
993 as %(asm_debug) %(asm_options) %m.s %A }}}}"
994 #endif
995 , 0, 0, 0},
997 #include "specs.h"
998 /* Mark end of table. */
999 {0, 0, 0, 0, 0}
1002 /* Number of elements in default_compilers, not counting the terminator. */
1004 static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
1006 typedef char *char_p; /* For DEF_VEC_P. */
1007 DEF_VEC_P(char_p);
1008 DEF_VEC_ALLOC_P(char_p,heap);
1010 /* A vector of options to give to the linker.
1011 These options are accumulated by %x,
1012 and substituted into the linker command with %X. */
1013 static VEC(char_p,heap) *linker_options;
1015 /* A vector of options to give to the assembler.
1016 These options are accumulated by -Wa,
1017 and substituted into the assembler command with %Y. */
1018 static VEC(char_p,heap) *assembler_options;
1020 /* A vector of options to give to the preprocessor.
1021 These options are accumulated by -Wp,
1022 and substituted into the preprocessor command with %Z. */
1023 static VEC(char_p,heap) *preprocessor_options;
1025 static char *
1026 skip_whitespace (char *p)
1028 while (1)
1030 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1031 be considered whitespace. */
1032 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1033 return p + 1;
1034 else if (*p == '\n' || *p == ' ' || *p == '\t')
1035 p++;
1036 else if (*p == '#')
1038 while (*p != '\n')
1039 p++;
1040 p++;
1042 else
1043 break;
1046 return p;
1048 /* Structures to keep track of prefixes to try when looking for files. */
1050 struct prefix_list
1052 const char *prefix; /* String to prepend to the path. */
1053 struct prefix_list *next; /* Next in linked list. */
1054 int require_machine_suffix; /* Don't use without machine_suffix. */
1055 /* 2 means try both machine_suffix and just_machine_suffix. */
1056 int priority; /* Sort key - priority within list. */
1057 int os_multilib; /* 1 if OS multilib scheme should be used,
1058 0 for GCC multilib scheme. */
1061 struct path_prefix
1063 struct prefix_list *plist; /* List of prefixes to try */
1064 int max_len; /* Max length of a prefix in PLIST */
1065 const char *name; /* Name of this list (used in config stuff) */
1068 /* List of prefixes to try when looking for executables. */
1070 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1072 /* List of prefixes to try when looking for startup (crt0) files. */
1074 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1076 /* List of prefixes to try when looking for include files. */
1078 static struct path_prefix include_prefixes = { 0, 0, "include" };
1080 /* Suffix to attach to directories searched for commands.
1081 This looks like `MACHINE/VERSION/'. */
1083 static const char *machine_suffix = 0;
1085 /* Suffix to attach to directories searched for commands.
1086 This is just `MACHINE/'. */
1088 static const char *just_machine_suffix = 0;
1090 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1092 static const char *gcc_exec_prefix;
1094 /* Adjusted value of standard_libexec_prefix. */
1096 static const char *gcc_libexec_prefix;
1098 /* Default prefixes to attach to command names. */
1100 #ifndef STANDARD_STARTFILE_PREFIX_1
1101 #define STANDARD_STARTFILE_PREFIX_1 "/lib/"
1102 #endif
1103 #ifndef STANDARD_STARTFILE_PREFIX_2
1104 #define STANDARD_STARTFILE_PREFIX_2 "/usr/lib/"
1105 #endif
1107 #ifdef CROSS_DIRECTORY_STRUCTURE /* Don't use these prefixes for a cross compiler. */
1108 #undef MD_EXEC_PREFIX
1109 #undef MD_STARTFILE_PREFIX
1110 #undef MD_STARTFILE_PREFIX_1
1111 #endif
1113 /* If no prefixes defined, use the null string, which will disable them. */
1114 #ifndef MD_EXEC_PREFIX
1115 #define MD_EXEC_PREFIX ""
1116 #endif
1117 #ifndef MD_STARTFILE_PREFIX
1118 #define MD_STARTFILE_PREFIX ""
1119 #endif
1120 #ifndef MD_STARTFILE_PREFIX_1
1121 #define MD_STARTFILE_PREFIX_1 ""
1122 #endif
1124 /* These directories are locations set at configure-time based on the
1125 --prefix option provided to configure. Their initializers are
1126 defined in Makefile.in. These paths are not *directly* used when
1127 gcc_exec_prefix is set because, in that case, we know where the
1128 compiler has been installed, and use paths relative to that
1129 location instead. */
1130 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1131 static const char *const standard_libexec_prefix = STANDARD_LIBEXEC_PREFIX;
1132 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1133 static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1135 /* For native compilers, these are well-known paths containing
1136 components that may be provided by the system. For cross
1137 compilers, these paths are not used. */
1138 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1139 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1140 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1141 static const char *const standard_startfile_prefix_1
1142 = STANDARD_STARTFILE_PREFIX_1;
1143 static const char *const standard_startfile_prefix_2
1144 = STANDARD_STARTFILE_PREFIX_2;
1146 /* A relative path to be used in finding the location of tools
1147 relative to the driver. */
1148 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1150 /* Subdirectory to use for locating libraries. Set by
1151 set_multilib_dir based on the compilation options. */
1153 static const char *multilib_dir;
1155 /* Subdirectory to use for locating libraries in OS conventions. Set by
1156 set_multilib_dir based on the compilation options. */
1158 static const char *multilib_os_dir;
1160 /* Structure to keep track of the specs that have been defined so far.
1161 These are accessed using %(specname) or %[specname] in a compiler
1162 or link spec. */
1164 struct spec_list
1166 /* The following 2 fields must be first */
1167 /* to allow EXTRA_SPECS to be initialized */
1168 const char *name; /* name of the spec. */
1169 const char *ptr; /* available ptr if no static pointer */
1171 /* The following fields are not initialized */
1172 /* by EXTRA_SPECS */
1173 const char **ptr_spec; /* pointer to the spec itself. */
1174 struct spec_list *next; /* Next spec in linked list. */
1175 int name_len; /* length of the name */
1176 int alloc_p; /* whether string was allocated */
1179 #define INIT_STATIC_SPEC(NAME,PTR) \
1180 { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1182 /* List of statically defined specs. */
1183 static struct spec_list static_specs[] =
1185 INIT_STATIC_SPEC ("asm", &asm_spec),
1186 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
1187 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1188 INIT_STATIC_SPEC ("asm_options", &asm_options),
1189 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1190 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1191 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1192 INIT_STATIC_SPEC ("cpp_debug_options", &cpp_debug_options),
1193 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
1194 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1195 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1196 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1197 INIT_STATIC_SPEC ("upc_options", &upc_options),
1198 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1199 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
1200 INIT_STATIC_SPEC ("link_ssp", &link_ssp_spec),
1201 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1202 INIT_STATIC_SPEC ("link", &link_spec),
1203 INIT_STATIC_SPEC ("lib", &lib_spec),
1204 INIT_STATIC_SPEC ("mfwrap", &mfwrap_spec),
1205 INIT_STATIC_SPEC ("mflib", &mflib_spec),
1206 INIT_STATIC_SPEC ("link_gomp", &link_gomp_spec),
1207 INIT_STATIC_SPEC ("upc_crtbegin", &upc_crtbegin_spec),
1208 INIT_STATIC_SPEC ("link_upc", &link_upc_spec),
1209 INIT_STATIC_SPEC ("upc_crtend", &upc_crtend_spec),
1210 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1211 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1212 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1213 INIT_STATIC_SPEC ("version", &compiler_version),
1214 INIT_STATIC_SPEC ("multilib", &multilib_select),
1215 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1216 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1217 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1218 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1219 INIT_STATIC_SPEC ("multilib_options", &multilib_options),
1220 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1221 INIT_STATIC_SPEC ("linker_plugin_file", &linker_plugin_file_spec),
1222 INIT_STATIC_SPEC ("lto_wrapper", &lto_wrapper_spec),
1223 INIT_STATIC_SPEC ("lto_gcc", &lto_gcc_spec),
1224 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1225 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1226 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1227 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1228 INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
1229 INIT_STATIC_SPEC ("sysroot_spec", &sysroot_spec),
1230 INIT_STATIC_SPEC ("sysroot_suffix_spec", &sysroot_suffix_spec),
1231 INIT_STATIC_SPEC ("sysroot_hdrs_suffix_spec", &sysroot_hdrs_suffix_spec),
1234 #ifdef EXTRA_SPECS /* additional specs needed */
1235 /* Structure to keep track of just the first two args of a spec_list.
1236 That is all that the EXTRA_SPECS macro gives us. */
1237 struct spec_list_1
1239 const char *const name;
1240 const char *const ptr;
1243 static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1244 static struct spec_list *extra_specs = (struct spec_list *) 0;
1245 #endif
1247 /* List of dynamically allocates specs that have been defined so far. */
1249 static struct spec_list *specs = (struct spec_list *) 0;
1251 /* List of static spec functions. */
1253 static const struct spec_function static_spec_functions[] =
1255 { "getenv", getenv_spec_function },
1256 { "if-exists", if_exists_spec_function },
1257 { "if-exists-else", if_exists_else_spec_function },
1258 { "replace-outfile", replace_outfile_spec_function },
1259 { "remove-outfile", remove_outfile_spec_function },
1260 { "version-compare", version_compare_spec_function },
1261 { "include", include_spec_function },
1262 { "find-file", find_file_spec_function },
1263 { "find-plugindir", find_plugindir_spec_function },
1264 { "print-asm-header", print_asm_header_spec_function },
1265 { "compare-debug-dump-opt", compare_debug_dump_opt_spec_function },
1266 { "compare-debug-self-opt", compare_debug_self_opt_spec_function },
1267 { "compare-debug-auxbase-opt", compare_debug_auxbase_opt_spec_function },
1268 { "pass-through-libs", pass_through_libs_spec_func },
1269 #ifdef EXTRA_SPEC_FUNCTIONS
1270 EXTRA_SPEC_FUNCTIONS
1271 #endif
1272 { 0, 0 }
1275 static int processing_spec_function;
1277 /* Add appropriate libgcc specs to OBSTACK, taking into account
1278 various permutations of -shared-libgcc, -shared, and such. */
1280 #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1282 #ifndef USE_LD_AS_NEEDED
1283 #define USE_LD_AS_NEEDED 0
1284 #endif
1286 static void
1287 init_gcc_specs (struct obstack *obstack, const char *shared_name,
1288 const char *static_name, const char *eh_name)
1290 char *buf;
1292 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name, "}"
1293 "%{!static:%{!static-libgcc:"
1294 #if USE_LD_AS_NEEDED
1295 "%{!shared-libgcc:",
1296 static_name, " --as-needed ", shared_name, " --no-as-needed"
1298 "%{shared-libgcc:",
1299 shared_name, "%{!shared: ", static_name, "}"
1301 #else
1302 "%{!shared:"
1303 "%{!shared-libgcc:", static_name, " ", eh_name, "}"
1304 "%{shared-libgcc:", shared_name, " ", static_name, "}"
1306 #ifdef LINK_EH_SPEC
1307 "%{shared:"
1308 "%{shared-libgcc:", shared_name, "}"
1309 "%{!shared-libgcc:", static_name, "}"
1311 #else
1312 "%{shared:", shared_name, "}"
1313 #endif
1314 #endif
1315 "}}", NULL);
1317 obstack_grow (obstack, buf, strlen (buf));
1318 free (buf);
1320 #endif /* ENABLE_SHARED_LIBGCC */
1322 /* Initialize the specs lookup routines. */
1324 static void
1325 init_spec (void)
1327 struct spec_list *next = (struct spec_list *) 0;
1328 struct spec_list *sl = (struct spec_list *) 0;
1329 int i;
1331 if (specs)
1332 return; /* Already initialized. */
1334 if (verbose_flag)
1335 fnotice (stderr, "Using built-in specs.\n");
1337 #ifdef EXTRA_SPECS
1338 extra_specs = XCNEWVEC (struct spec_list, ARRAY_SIZE (extra_specs_1));
1340 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1342 sl = &extra_specs[i];
1343 sl->name = extra_specs_1[i].name;
1344 sl->ptr = extra_specs_1[i].ptr;
1345 sl->next = next;
1346 sl->name_len = strlen (sl->name);
1347 sl->ptr_spec = &sl->ptr;
1348 next = sl;
1350 #endif
1352 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1354 sl = &static_specs[i];
1355 sl->next = next;
1356 next = sl;
1359 #if defined(ENABLE_SHARED_LIBGCC) && !defined(REAL_LIBGCC_SPEC)
1360 /* ??? If neither -shared-libgcc nor --static-libgcc was
1361 seen, then we should be making an educated guess. Some proposed
1362 heuristics for ELF include:
1364 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1365 program will be doing dynamic loading, which will likely
1366 need the shared libgcc.
1368 (2) If "-ldl", then it's also a fair bet that we're doing
1369 dynamic loading.
1371 (3) For each ET_DYN we're linking against (either through -lfoo
1372 or /some/path/foo.so), check to see whether it or one of
1373 its dependencies depends on a shared libgcc.
1375 (4) If "-shared"
1377 If the runtime is fixed to look for program headers instead
1378 of calling __register_frame_info at all, for each object,
1379 use the shared libgcc if any EH symbol referenced.
1381 If crtstuff is fixed to not invoke __register_frame_info
1382 automatically, for each object, use the shared libgcc if
1383 any non-empty unwind section found.
1385 Doing any of this probably requires invoking an external program to
1386 do the actual object file scanning. */
1388 const char *p = libgcc_spec;
1389 int in_sep = 1;
1391 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1392 when given the proper command line arguments. */
1393 while (*p)
1395 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1397 init_gcc_specs (&obstack,
1398 "-lgcc_s"
1399 #ifdef USE_LIBUNWIND_EXCEPTIONS
1400 " -lunwind"
1401 #endif
1403 "-lgcc",
1404 "-lgcc_eh"
1405 #ifdef USE_LIBUNWIND_EXCEPTIONS
1406 # ifdef HAVE_LD_STATIC_DYNAMIC
1407 " %{!static:" LD_STATIC_OPTION "} -lunwind"
1408 " %{!static:" LD_DYNAMIC_OPTION "}"
1409 # else
1410 " -lunwind"
1411 # endif
1412 #endif
1415 p += 5;
1416 in_sep = 0;
1418 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1420 /* Ug. We don't know shared library extensions. Hope that
1421 systems that use this form don't do shared libraries. */
1422 init_gcc_specs (&obstack,
1423 "-lgcc_s",
1424 "libgcc.a%s",
1425 "libgcc_eh.a%s"
1426 #ifdef USE_LIBUNWIND_EXCEPTIONS
1427 " -lunwind"
1428 #endif
1430 p += 10;
1431 in_sep = 0;
1433 else
1435 obstack_1grow (&obstack, *p);
1436 in_sep = (*p == ' ');
1437 p += 1;
1441 obstack_1grow (&obstack, '\0');
1442 libgcc_spec = XOBFINISH (&obstack, const char *);
1444 #endif
1445 #ifdef USE_AS_TRADITIONAL_FORMAT
1446 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1448 static const char tf[] = "--traditional-format ";
1449 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1450 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1451 asm_spec = XOBFINISH (&obstack, const char *);
1453 #endif
1455 #if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC
1456 # ifdef LINK_BUILDID_SPEC
1457 /* Prepend LINK_BUILDID_SPEC to whatever link_spec we had before. */
1458 obstack_grow (&obstack, LINK_BUILDID_SPEC, sizeof(LINK_BUILDID_SPEC) - 1);
1459 # endif
1460 # ifdef LINK_EH_SPEC
1461 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1462 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
1463 # endif
1464 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
1465 link_spec = XOBFINISH (&obstack, const char *);
1466 #endif
1468 specs = sl;
1471 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1472 removed; If the spec starts with a + then SPEC is added to the end of the
1473 current spec. */
1475 static void
1476 set_spec (const char *name, const char *spec)
1478 struct spec_list *sl;
1479 const char *old_spec;
1480 int name_len = strlen (name);
1481 int i;
1483 /* If this is the first call, initialize the statically allocated specs. */
1484 if (!specs)
1486 struct spec_list *next = (struct spec_list *) 0;
1487 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1489 sl = &static_specs[i];
1490 sl->next = next;
1491 next = sl;
1493 specs = sl;
1496 /* See if the spec already exists. */
1497 for (sl = specs; sl; sl = sl->next)
1498 if (name_len == sl->name_len && !strcmp (sl->name, name))
1499 break;
1501 if (!sl)
1503 /* Not found - make it. */
1504 sl = XNEW (struct spec_list);
1505 sl->name = xstrdup (name);
1506 sl->name_len = name_len;
1507 sl->ptr_spec = &sl->ptr;
1508 sl->alloc_p = 0;
1509 *(sl->ptr_spec) = "";
1510 sl->next = specs;
1511 specs = sl;
1514 old_spec = *(sl->ptr_spec);
1515 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1516 ? concat (old_spec, spec + 1, NULL)
1517 : xstrdup (spec));
1519 #ifdef DEBUG_SPECS
1520 if (verbose_flag)
1521 fnotice (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1522 #endif
1524 /* Free the old spec. */
1525 if (old_spec && sl->alloc_p)
1526 free (CONST_CAST(char *, old_spec));
1528 sl->alloc_p = 1;
1531 /* Accumulate a command (program name and args), and run it. */
1533 typedef const char *const_char_p; /* For DEF_VEC_P. */
1534 DEF_VEC_P(const_char_p);
1535 DEF_VEC_ALLOC_P(const_char_p,heap);
1537 /* Vector of pointers to arguments in the current line of specifications. */
1539 static VEC(const_char_p,heap) *argbuf;
1541 /* Position in the argbuf vector containing the name of the output file
1542 (the value associated with the "-o" flag). */
1544 static int have_o_argbuf_index = 0;
1546 /* Were the options -c, -S or -E passed. */
1547 static int have_c = 0;
1549 /* Was the option -o passed. */
1550 static int have_o = 0;
1552 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1553 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1554 it here. */
1556 static struct temp_name {
1557 const char *suffix; /* suffix associated with the code. */
1558 int length; /* strlen (suffix). */
1559 int unique; /* Indicates whether %g or %u/%U was used. */
1560 const char *filename; /* associated filename. */
1561 int filename_length; /* strlen (filename). */
1562 struct temp_name *next;
1563 } *temp_names;
1565 /* Number of commands executed so far. */
1567 static int execution_count;
1569 /* Number of commands that exited with a signal. */
1571 static int signal_count;
1573 /* Allocate the argument vector. */
1575 static void
1576 alloc_args (void)
1578 argbuf = VEC_alloc (const_char_p, heap, 10);
1581 /* Clear out the vector of arguments (after a command is executed). */
1583 static void
1584 clear_args (void)
1586 VEC_truncate (const_char_p, argbuf, 0);
1589 /* Add one argument to the vector at the end.
1590 This is done when a space is seen or at the end of the line.
1591 If DELETE_ALWAYS is nonzero, the arg is a filename
1592 and the file should be deleted eventually.
1593 If DELETE_FAILURE is nonzero, the arg is a filename
1594 and the file should be deleted if this compilation fails. */
1596 static void
1597 store_arg (const char *arg, int delete_always, int delete_failure)
1599 VEC_safe_push (const_char_p, heap, argbuf, arg);
1601 if (strcmp (arg, "-o") == 0)
1602 have_o_argbuf_index = VEC_length (const_char_p, argbuf);
1603 if (delete_always || delete_failure)
1605 const char *p;
1606 /* If the temporary file we should delete is specified as
1607 part of a joined argument extract the filename. */
1608 if (arg[0] == '-'
1609 && (p = strrchr (arg, '=')))
1610 arg = p + 1;
1611 record_temp_file (arg, delete_always, delete_failure);
1615 /* Load specs from a file name named FILENAME, replacing occurrences of
1616 various different types of line-endings, \r\n, \n\r and just \r, with
1617 a single \n. */
1619 static char *
1620 load_specs (const char *filename)
1622 int desc;
1623 int readlen;
1624 struct stat statbuf;
1625 char *buffer;
1626 char *buffer_p;
1627 char *specs;
1628 char *specs_p;
1630 if (verbose_flag)
1631 fnotice (stderr, "Reading specs from %s\n", filename);
1633 /* Open and stat the file. */
1634 desc = open (filename, O_RDONLY, 0);
1635 if (desc < 0)
1636 pfatal_with_name (filename);
1637 if (stat (filename, &statbuf) < 0)
1638 pfatal_with_name (filename);
1640 /* Read contents of file into BUFFER. */
1641 buffer = XNEWVEC (char, statbuf.st_size + 1);
1642 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1643 if (readlen < 0)
1644 pfatal_with_name (filename);
1645 buffer[readlen] = 0;
1646 close (desc);
1648 specs = XNEWVEC (char, readlen + 1);
1649 specs_p = specs;
1650 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1652 int skip = 0;
1653 char c = *buffer_p;
1654 if (c == '\r')
1656 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1657 skip = 1;
1658 else if (*(buffer_p + 1) == '\n') /* \r\n */
1659 skip = 1;
1660 else /* \r */
1661 c = '\n';
1663 if (! skip)
1664 *specs_p++ = c;
1666 *specs_p = '\0';
1668 free (buffer);
1669 return (specs);
1672 /* Read compilation specs from a file named FILENAME,
1673 replacing the default ones.
1675 A suffix which starts with `*' is a definition for
1676 one of the machine-specific sub-specs. The "suffix" should be
1677 *asm, *cc1, *cpp, *link, *startfile, etc.
1678 The corresponding spec is stored in asm_spec, etc.,
1679 rather than in the `compilers' vector.
1681 Anything invalid in the file is a fatal error. */
1683 static void
1684 read_specs (const char *filename, int main_p)
1686 char *buffer;
1687 char *p;
1689 buffer = load_specs (filename);
1691 /* Scan BUFFER for specs, putting them in the vector. */
1692 p = buffer;
1693 while (1)
1695 char *suffix;
1696 char *spec;
1697 char *in, *out, *p1, *p2, *p3;
1699 /* Advance P in BUFFER to the next nonblank nocomment line. */
1700 p = skip_whitespace (p);
1701 if (*p == 0)
1702 break;
1704 /* Is this a special command that starts with '%'? */
1705 /* Don't allow this for the main specs file, since it would
1706 encourage people to overwrite it. */
1707 if (*p == '%' && !main_p)
1709 p1 = p;
1710 while (*p && *p != '\n')
1711 p++;
1713 /* Skip '\n'. */
1714 p++;
1716 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1717 && (p1[sizeof "%include" - 1] == ' '
1718 || p1[sizeof "%include" - 1] == '\t'))
1720 char *new_filename;
1722 p1 += sizeof ("%include");
1723 while (*p1 == ' ' || *p1 == '\t')
1724 p1++;
1726 if (*p1++ != '<' || p[-2] != '>')
1727 fatal_error ("specs %%include syntax malformed after "
1728 "%ld characters",
1729 (long) (p1 - buffer + 1));
1731 p[-2] = '\0';
1732 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
1733 read_specs (new_filename ? new_filename : p1, FALSE);
1734 continue;
1736 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1737 && (p1[sizeof "%include_noerr" - 1] == ' '
1738 || p1[sizeof "%include_noerr" - 1] == '\t'))
1740 char *new_filename;
1742 p1 += sizeof "%include_noerr";
1743 while (*p1 == ' ' || *p1 == '\t')
1744 p1++;
1746 if (*p1++ != '<' || p[-2] != '>')
1747 fatal_error ("specs %%include syntax malformed after "
1748 "%ld characters",
1749 (long) (p1 - buffer + 1));
1751 p[-2] = '\0';
1752 new_filename = find_a_file (&startfile_prefixes, p1, R_OK, true);
1753 if (new_filename)
1754 read_specs (new_filename, FALSE);
1755 else if (verbose_flag)
1756 fnotice (stderr, "could not find specs file %s\n", p1);
1757 continue;
1759 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1760 && (p1[sizeof "%rename" - 1] == ' '
1761 || p1[sizeof "%rename" - 1] == '\t'))
1763 int name_len;
1764 struct spec_list *sl;
1765 struct spec_list *newsl;
1767 /* Get original name. */
1768 p1 += sizeof "%rename";
1769 while (*p1 == ' ' || *p1 == '\t')
1770 p1++;
1772 if (! ISALPHA ((unsigned char) *p1))
1773 fatal_error ("specs %%rename syntax malformed after "
1774 "%ld characters",
1775 (long) (p1 - buffer));
1777 p2 = p1;
1778 while (*p2 && !ISSPACE ((unsigned char) *p2))
1779 p2++;
1781 if (*p2 != ' ' && *p2 != '\t')
1782 fatal_error ("specs %%rename syntax malformed after "
1783 "%ld characters",
1784 (long) (p2 - buffer));
1786 name_len = p2 - p1;
1787 *p2++ = '\0';
1788 while (*p2 == ' ' || *p2 == '\t')
1789 p2++;
1791 if (! ISALPHA ((unsigned char) *p2))
1792 fatal_error ("specs %%rename syntax malformed after "
1793 "%ld characters",
1794 (long) (p2 - buffer));
1796 /* Get new spec name. */
1797 p3 = p2;
1798 while (*p3 && !ISSPACE ((unsigned char) *p3))
1799 p3++;
1801 if (p3 != p - 1)
1802 fatal_error ("specs %%rename syntax malformed after "
1803 "%ld characters",
1804 (long) (p3 - buffer));
1805 *p3 = '\0';
1807 for (sl = specs; sl; sl = sl->next)
1808 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1809 break;
1811 if (!sl)
1812 fatal_error ("specs %s spec was not found to be renamed", p1);
1814 if (strcmp (p1, p2) == 0)
1815 continue;
1817 for (newsl = specs; newsl; newsl = newsl->next)
1818 if (strcmp (newsl->name, p2) == 0)
1819 fatal_error ("%s: attempt to rename spec %qs to "
1820 "already defined spec %qs",
1821 filename, p1, p2);
1823 if (verbose_flag)
1825 fnotice (stderr, "rename spec %s to %s\n", p1, p2);
1826 #ifdef DEBUG_SPECS
1827 fnotice (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
1828 #endif
1831 set_spec (p2, *(sl->ptr_spec));
1832 if (sl->alloc_p)
1833 free (CONST_CAST (char *, *(sl->ptr_spec)));
1835 *(sl->ptr_spec) = "";
1836 sl->alloc_p = 0;
1837 continue;
1839 else
1840 fatal_error ("specs unknown %% command after %ld characters",
1841 (long) (p1 - buffer));
1844 /* Find the colon that should end the suffix. */
1845 p1 = p;
1846 while (*p1 && *p1 != ':' && *p1 != '\n')
1847 p1++;
1849 /* The colon shouldn't be missing. */
1850 if (*p1 != ':')
1851 fatal_error ("specs file malformed after %ld characters",
1852 (long) (p1 - buffer));
1854 /* Skip back over trailing whitespace. */
1855 p2 = p1;
1856 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1857 p2--;
1859 /* Copy the suffix to a string. */
1860 suffix = save_string (p, p2 - p);
1861 /* Find the next line. */
1862 p = skip_whitespace (p1 + 1);
1863 if (p[1] == 0)
1864 fatal_error ("specs file malformed after %ld characters",
1865 (long) (p - buffer));
1867 p1 = p;
1868 /* Find next blank line or end of string. */
1869 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1870 p1++;
1872 /* Specs end at the blank line and do not include the newline. */
1873 spec = save_string (p, p1 - p);
1874 p = p1;
1876 /* Delete backslash-newline sequences from the spec. */
1877 in = spec;
1878 out = spec;
1879 while (*in != 0)
1881 if (in[0] == '\\' && in[1] == '\n')
1882 in += 2;
1883 else if (in[0] == '#')
1884 while (*in && *in != '\n')
1885 in++;
1887 else
1888 *out++ = *in++;
1890 *out = 0;
1892 if (suffix[0] == '*')
1894 if (! strcmp (suffix, "*link_command"))
1895 link_command_spec = spec;
1896 else
1897 set_spec (suffix + 1, spec);
1899 else
1901 /* Add this pair to the vector. */
1902 compilers
1903 = XRESIZEVEC (struct compiler, compilers, n_compilers + 2);
1905 compilers[n_compilers].suffix = suffix;
1906 compilers[n_compilers].spec = spec;
1907 n_compilers++;
1908 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
1911 if (*suffix == 0)
1912 link_command_spec = spec;
1915 if (link_command_spec == 0)
1916 fatal_error ("spec file has no spec for linking");
1919 /* Record the names of temporary files we tell compilers to write,
1920 and delete them at the end of the run. */
1922 /* This is the common prefix we use to make temp file names.
1923 It is chosen once for each run of this program.
1924 It is substituted into a spec by %g or %j.
1925 Thus, all temp file names contain this prefix.
1926 In practice, all temp file names start with this prefix.
1928 This prefix comes from the envvar TMPDIR if it is defined;
1929 otherwise, from the P_tmpdir macro if that is defined;
1930 otherwise, in /usr/tmp or /tmp;
1931 or finally the current directory if all else fails. */
1933 static const char *temp_filename;
1935 /* Length of the prefix. */
1937 static int temp_filename_length;
1939 /* Define the list of temporary files to delete. */
1941 struct temp_file
1943 const char *name;
1944 struct temp_file *next;
1947 /* Queue of files to delete on success or failure of compilation. */
1948 static struct temp_file *always_delete_queue;
1949 /* Queue of files to delete on failure of compilation. */
1950 static struct temp_file *failure_delete_queue;
1952 /* Record FILENAME as a file to be deleted automatically.
1953 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1954 otherwise delete it in any case.
1955 FAIL_DELETE nonzero means delete it if a compilation step fails;
1956 otherwise delete it in any case. */
1958 void
1959 record_temp_file (const char *filename, int always_delete, int fail_delete)
1961 char *const name = xstrdup (filename);
1963 if (always_delete)
1965 struct temp_file *temp;
1966 for (temp = always_delete_queue; temp; temp = temp->next)
1967 if (! filename_cmp (name, temp->name))
1968 goto already1;
1970 temp = XNEW (struct temp_file);
1971 temp->next = always_delete_queue;
1972 temp->name = name;
1973 always_delete_queue = temp;
1975 already1:;
1978 if (fail_delete)
1980 struct temp_file *temp;
1981 for (temp = failure_delete_queue; temp; temp = temp->next)
1982 if (! filename_cmp (name, temp->name))
1983 goto already2;
1985 temp = XNEW (struct temp_file);
1986 temp->next = failure_delete_queue;
1987 temp->name = name;
1988 failure_delete_queue = temp;
1990 already2:;
1994 /* Delete all the temporary files whose names we previously recorded. */
1996 #ifndef DELETE_IF_ORDINARY
1997 #define DELETE_IF_ORDINARY(NAME,ST,VERBOSE_FLAG) \
1998 do \
2000 if (stat (NAME, &ST) >= 0 && S_ISREG (ST.st_mode)) \
2001 if (unlink (NAME) < 0) \
2002 if (VERBOSE_FLAG) \
2003 perror_with_name (NAME); \
2004 } while (0)
2005 #endif
2007 static void
2008 delete_if_ordinary (const char *name)
2010 struct stat st;
2011 #ifdef DEBUG
2012 int i, c;
2014 printf ("Delete %s? (y or n) ", name);
2015 fflush (stdout);
2016 i = getchar ();
2017 if (i != '\n')
2018 while ((c = getchar ()) != '\n' && c != EOF)
2021 if (i == 'y' || i == 'Y')
2022 #endif /* DEBUG */
2023 DELETE_IF_ORDINARY (name, st, verbose_flag);
2026 static void
2027 delete_temp_files (void)
2029 struct temp_file *temp;
2031 for (temp = always_delete_queue; temp; temp = temp->next)
2032 delete_if_ordinary (temp->name);
2033 always_delete_queue = 0;
2036 /* Delete all the files to be deleted on error. */
2038 static void
2039 delete_failure_queue (void)
2041 struct temp_file *temp;
2043 for (temp = failure_delete_queue; temp; temp = temp->next)
2044 delete_if_ordinary (temp->name);
2047 static void
2048 clear_failure_queue (void)
2050 failure_delete_queue = 0;
2053 /* Call CALLBACK for each path in PATHS, breaking out early if CALLBACK
2054 returns non-NULL.
2055 If DO_MULTI is true iterate over the paths twice, first with multilib
2056 suffix then without, otherwise iterate over the paths once without
2057 adding a multilib suffix. When DO_MULTI is true, some attempt is made
2058 to avoid visiting the same path twice, but we could do better. For
2059 instance, /usr/lib/../lib is considered different from /usr/lib.
2060 At least EXTRA_SPACE chars past the end of the path passed to
2061 CALLBACK are available for use by the callback.
2062 CALLBACK_INFO allows extra parameters to be passed to CALLBACK.
2064 Returns the value returned by CALLBACK. */
2066 static void *
2067 for_each_path (const struct path_prefix *paths,
2068 bool do_multi,
2069 size_t extra_space,
2070 void *(*callback) (char *, void *),
2071 void *callback_info)
2073 struct prefix_list *pl;
2074 const char *multi_dir = NULL;
2075 const char *multi_os_dir = NULL;
2076 const char *multi_suffix;
2077 const char *just_multi_suffix;
2078 char *path = NULL;
2079 void *ret = NULL;
2080 bool skip_multi_dir = false;
2081 bool skip_multi_os_dir = false;
2083 multi_suffix = machine_suffix;
2084 just_multi_suffix = just_machine_suffix;
2085 if (do_multi && multilib_dir && strcmp (multilib_dir, ".") != 0)
2087 multi_dir = concat (multilib_dir, dir_separator_str, NULL);
2088 multi_suffix = concat (multi_suffix, multi_dir, NULL);
2089 just_multi_suffix = concat (just_multi_suffix, multi_dir, NULL);
2091 if (do_multi && multilib_os_dir && strcmp (multilib_os_dir, ".") != 0)
2092 multi_os_dir = concat (multilib_os_dir, dir_separator_str, NULL);
2094 while (1)
2096 size_t multi_dir_len = 0;
2097 size_t multi_os_dir_len = 0;
2098 size_t suffix_len;
2099 size_t just_suffix_len;
2100 size_t len;
2102 if (multi_dir)
2103 multi_dir_len = strlen (multi_dir);
2104 if (multi_os_dir)
2105 multi_os_dir_len = strlen (multi_os_dir);
2106 suffix_len = strlen (multi_suffix);
2107 just_suffix_len = strlen (just_multi_suffix);
2109 if (path == NULL)
2111 len = paths->max_len + extra_space + 1;
2112 if (suffix_len > multi_os_dir_len)
2113 len += suffix_len;
2114 else
2115 len += multi_os_dir_len;
2116 path = XNEWVEC (char, len);
2119 for (pl = paths->plist; pl != 0; pl = pl->next)
2121 len = strlen (pl->prefix);
2122 memcpy (path, pl->prefix, len);
2124 /* Look first in MACHINE/VERSION subdirectory. */
2125 if (!skip_multi_dir)
2127 memcpy (path + len, multi_suffix, suffix_len + 1);
2128 ret = callback (path, callback_info);
2129 if (ret)
2130 break;
2133 /* Some paths are tried with just the machine (ie. target)
2134 subdir. This is used for finding as, ld, etc. */
2135 if (!skip_multi_dir
2136 && pl->require_machine_suffix == 2)
2138 memcpy (path + len, just_multi_suffix, just_suffix_len + 1);
2139 ret = callback (path, callback_info);
2140 if (ret)
2141 break;
2144 /* Now try the base path. */
2145 if (!pl->require_machine_suffix
2146 && !(pl->os_multilib ? skip_multi_os_dir : skip_multi_dir))
2148 const char *this_multi;
2149 size_t this_multi_len;
2151 if (pl->os_multilib)
2153 this_multi = multi_os_dir;
2154 this_multi_len = multi_os_dir_len;
2156 else
2158 this_multi = multi_dir;
2159 this_multi_len = multi_dir_len;
2162 if (this_multi_len)
2163 memcpy (path + len, this_multi, this_multi_len + 1);
2164 else
2165 path[len] = '\0';
2167 ret = callback (path, callback_info);
2168 if (ret)
2169 break;
2172 if (pl)
2173 break;
2175 if (multi_dir == NULL && multi_os_dir == NULL)
2176 break;
2178 /* Run through the paths again, this time without multilibs.
2179 Don't repeat any we have already seen. */
2180 if (multi_dir)
2182 free (CONST_CAST (char *, multi_dir));
2183 multi_dir = NULL;
2184 free (CONST_CAST (char *, multi_suffix));
2185 multi_suffix = machine_suffix;
2186 free (CONST_CAST (char *, just_multi_suffix));
2187 just_multi_suffix = just_machine_suffix;
2189 else
2190 skip_multi_dir = true;
2191 if (multi_os_dir)
2193 free (CONST_CAST (char *, multi_os_dir));
2194 multi_os_dir = NULL;
2196 else
2197 skip_multi_os_dir = true;
2200 if (multi_dir)
2202 free (CONST_CAST (char *, multi_dir));
2203 free (CONST_CAST (char *, multi_suffix));
2204 free (CONST_CAST (char *, just_multi_suffix));
2206 if (multi_os_dir)
2207 free (CONST_CAST (char *, multi_os_dir));
2208 if (ret != path)
2209 free (path);
2210 return ret;
2213 /* Callback for build_search_list. Adds path to obstack being built. */
2215 struct add_to_obstack_info {
2216 struct obstack *ob;
2217 bool check_dir;
2218 bool first_time;
2221 static void *
2222 add_to_obstack (char *path, void *data)
2224 struct add_to_obstack_info *info = (struct add_to_obstack_info *) data;
2226 if (info->check_dir && !is_directory (path, false))
2227 return NULL;
2229 if (!info->first_time)
2230 obstack_1grow (info->ob, PATH_SEPARATOR);
2232 obstack_grow (info->ob, path, strlen (path));
2234 info->first_time = false;
2235 return NULL;
2238 /* Add or change the value of an environment variable, outputting the
2239 change to standard error if in verbose mode. */
2240 static void
2241 xputenv (const char *string)
2243 if (verbose_flag)
2244 fnotice (stderr, "%s\n", string);
2245 putenv (CONST_CAST (char *, string));
2248 /* Build a list of search directories from PATHS.
2249 PREFIX is a string to prepend to the list.
2250 If CHECK_DIR_P is true we ensure the directory exists.
2251 If DO_MULTI is true, multilib paths are output first, then
2252 non-multilib paths.
2253 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2254 It is also used by the --print-search-dirs flag. */
2256 static char *
2257 build_search_list (const struct path_prefix *paths, const char *prefix,
2258 bool check_dir, bool do_multi)
2260 struct add_to_obstack_info info;
2262 info.ob = &collect_obstack;
2263 info.check_dir = check_dir;
2264 info.first_time = true;
2266 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2267 obstack_1grow (&collect_obstack, '=');
2269 for_each_path (paths, do_multi, 0, add_to_obstack, &info);
2271 obstack_1grow (&collect_obstack, '\0');
2272 return XOBFINISH (&collect_obstack, char *);
2275 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2276 for collect. */
2278 static void
2279 putenv_from_prefixes (const struct path_prefix *paths, const char *env_var,
2280 bool do_multi)
2282 xputenv (build_search_list (paths, env_var, true, do_multi));
2285 /* Check whether NAME can be accessed in MODE. This is like access,
2286 except that it never considers directories to be executable. */
2288 static int
2289 access_check (const char *name, int mode)
2291 if (mode == X_OK)
2293 struct stat st;
2295 if (stat (name, &st) < 0
2296 || S_ISDIR (st.st_mode))
2297 return -1;
2300 return access (name, mode);
2303 /* Callback for find_a_file. Appends the file name to the directory
2304 path. If the resulting file exists in the right mode, return the
2305 full pathname to the file. */
2307 struct file_at_path_info {
2308 const char *name;
2309 const char *suffix;
2310 int name_len;
2311 int suffix_len;
2312 int mode;
2315 static void *
2316 file_at_path (char *path, void *data)
2318 struct file_at_path_info *info = (struct file_at_path_info *) data;
2319 size_t len = strlen (path);
2321 memcpy (path + len, info->name, info->name_len);
2322 len += info->name_len;
2324 /* Some systems have a suffix for executable files.
2325 So try appending that first. */
2326 if (info->suffix_len)
2328 memcpy (path + len, info->suffix, info->suffix_len + 1);
2329 if (access_check (path, info->mode) == 0)
2330 return path;
2333 path[len] = '\0';
2334 if (access_check (path, info->mode) == 0)
2335 return path;
2337 return NULL;
2340 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2341 access to check permissions. If DO_MULTI is true, search multilib
2342 paths then non-multilib paths, otherwise do not search multilib paths.
2343 Return 0 if not found, otherwise return its name, allocated with malloc. */
2345 static char *
2346 find_a_file (const struct path_prefix *pprefix, const char *name, int mode,
2347 bool do_multi)
2349 struct file_at_path_info info;
2351 #ifdef DEFAULT_ASSEMBLER
2352 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2353 return xstrdup (DEFAULT_ASSEMBLER);
2354 #endif
2356 #ifdef DEFAULT_LINKER
2357 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2358 return xstrdup (DEFAULT_LINKER);
2359 #endif
2361 /* Determine the filename to execute (special case for absolute paths). */
2363 if (IS_ABSOLUTE_PATH (name))
2365 if (access (name, mode) == 0)
2366 return xstrdup (name);
2368 return NULL;
2371 info.name = name;
2372 info.suffix = (mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "";
2373 info.name_len = strlen (info.name);
2374 info.suffix_len = strlen (info.suffix);
2375 info.mode = mode;
2377 return (char*) for_each_path (pprefix, do_multi,
2378 info.name_len + info.suffix_len,
2379 file_at_path, &info);
2382 /* Ranking of prefixes in the sort list. -B prefixes are put before
2383 all others. */
2385 enum path_prefix_priority
2387 PREFIX_PRIORITY_B_OPT,
2388 PREFIX_PRIORITY_LAST
2391 /* Add an entry for PREFIX in PLIST. The PLIST is kept in ascending
2392 order according to PRIORITY. Within each PRIORITY, new entries are
2393 appended.
2395 If WARN is nonzero, we will warn if no file is found
2396 through this prefix. WARN should point to an int
2397 which will be set to 1 if this entry is used.
2399 COMPONENT is the value to be passed to update_path.
2401 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2402 the complete value of machine_suffix.
2403 2 means try both machine_suffix and just_machine_suffix. */
2405 static void
2406 add_prefix (struct path_prefix *pprefix, const char *prefix,
2407 const char *component, /* enum prefix_priority */ int priority,
2408 int require_machine_suffix, int os_multilib)
2410 struct prefix_list *pl, **prev;
2411 int len;
2413 for (prev = &pprefix->plist;
2414 (*prev) != NULL && (*prev)->priority <= priority;
2415 prev = &(*prev)->next)
2418 /* Keep track of the longest prefix. */
2420 prefix = update_path (prefix, component);
2421 len = strlen (prefix);
2422 if (len > pprefix->max_len)
2423 pprefix->max_len = len;
2425 pl = XNEW (struct prefix_list);
2426 pl->prefix = prefix;
2427 pl->require_machine_suffix = require_machine_suffix;
2428 pl->priority = priority;
2429 pl->os_multilib = os_multilib;
2431 /* Insert after PREV. */
2432 pl->next = (*prev);
2433 (*prev) = pl;
2436 /* Same as add_prefix, but prepending target_system_root to prefix. */
2437 /* The target_system_root prefix has been relocated by gcc_exec_prefix. */
2438 static void
2439 add_sysrooted_prefix (struct path_prefix *pprefix, const char *prefix,
2440 const char *component,
2441 /* enum prefix_priority */ int priority,
2442 int require_machine_suffix, int os_multilib)
2444 if (!IS_ABSOLUTE_PATH (prefix))
2445 fatal_error ("system path %qs is not absolute", prefix);
2447 if (target_system_root)
2449 if (target_sysroot_suffix)
2450 prefix = concat (target_sysroot_suffix, prefix, NULL);
2451 prefix = concat (target_system_root, prefix, NULL);
2453 /* We have to override this because GCC's notion of sysroot
2454 moves along with GCC. */
2455 component = "GCC";
2458 add_prefix (pprefix, prefix, component, priority,
2459 require_machine_suffix, os_multilib);
2462 /* Execute the command specified by the arguments on the current line of spec.
2463 When using pipes, this includes several piped-together commands
2464 with `|' between them.
2466 Return 0 if successful, -1 if failed. */
2468 static int
2469 execute (void)
2471 int i;
2472 int n_commands; /* # of command. */
2473 char *string;
2474 struct pex_obj *pex;
2475 struct command
2477 const char *prog; /* program name. */
2478 const char **argv; /* vector of args. */
2480 const char *arg;
2482 struct command *commands; /* each command buffer with above info. */
2484 gcc_assert (!processing_spec_function);
2486 if (wrapper_string)
2488 string = find_a_file (&exec_prefixes,
2489 VEC_index (const_char_p, argbuf, 0), X_OK, false);
2490 if (string)
2491 VEC_replace (const_char_p, argbuf, 0, string);
2492 insert_wrapper (wrapper_string);
2495 /* Count # of piped commands. */
2496 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2497 if (strcmp (arg, "|") == 0)
2498 n_commands++;
2500 /* Get storage for each command. */
2501 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2503 /* Split argbuf into its separate piped processes,
2504 and record info about each one.
2505 Also search for the programs that are to be run. */
2507 VEC_safe_push (const_char_p, heap, argbuf, 0);
2509 commands[0].prog = VEC_index (const_char_p, argbuf, 0); /* first command. */
2510 commands[0].argv = VEC_address (const_char_p, argbuf);
2512 if (!wrapper_string)
2514 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK, false);
2515 commands[0].argv[0] = (string) ? string : commands[0].argv[0];
2518 for (n_commands = 1, i = 0; VEC_iterate (const_char_p, argbuf, i, arg); i++)
2519 if (arg && strcmp (arg, "|") == 0)
2520 { /* each command. */
2521 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2522 fatal_error ("-pipe not supported");
2523 #endif
2524 VEC_replace (const_char_p, argbuf, i, 0); /* Termination of
2525 command args. */
2526 commands[n_commands].prog = VEC_index (const_char_p, argbuf, i + 1);
2527 commands[n_commands].argv
2528 = &(VEC_address (const_char_p, argbuf))[i + 1];
2529 string = find_a_file (&exec_prefixes, commands[n_commands].prog,
2530 X_OK, false);
2531 if (string)
2532 commands[n_commands].argv[0] = string;
2533 n_commands++;
2536 /* If -v, print what we are about to do, and maybe query. */
2538 if (verbose_flag)
2540 /* For help listings, put a blank line between sub-processes. */
2541 if (print_help_list)
2542 fputc ('\n', stderr);
2544 /* Print each piped command as a separate line. */
2545 for (i = 0; i < n_commands; i++)
2547 const char *const *j;
2549 if (verbose_only_flag)
2551 for (j = commands[i].argv; *j; j++)
2553 const char *p;
2554 for (p = *j; *p; ++p)
2555 if (!ISALNUM ((unsigned char) *p)
2556 && *p != '_' && *p != '/' && *p != '-' && *p != '.')
2557 break;
2558 if (*p || !*j)
2560 fprintf (stderr, " \"");
2561 for (p = *j; *p; ++p)
2563 if (*p == '"' || *p == '\\' || *p == '$')
2564 fputc ('\\', stderr);
2565 fputc (*p, stderr);
2567 fputc ('"', stderr);
2569 /* If it's empty, print "". */
2570 else if (!**j)
2571 fprintf (stderr, " \"\"");
2572 else
2573 fprintf (stderr, " %s", *j);
2576 else
2577 for (j = commands[i].argv; *j; j++)
2578 /* If it's empty, print "". */
2579 if (!**j)
2580 fprintf (stderr, " \"\"");
2581 else
2582 fprintf (stderr, " %s", *j);
2584 /* Print a pipe symbol after all but the last command. */
2585 if (i + 1 != n_commands)
2586 fprintf (stderr, " |");
2587 fprintf (stderr, "\n");
2589 fflush (stderr);
2590 if (verbose_only_flag != 0)
2592 /* verbose_only_flag should act as if the spec was
2593 executed, so increment execution_count before
2594 returning. This prevents spurious warnings about
2595 unused linker input files, etc. */
2596 execution_count++;
2597 return 0;
2599 #ifdef DEBUG
2600 fnotice (stderr, "\nGo ahead? (y or n) ");
2601 fflush (stderr);
2602 i = getchar ();
2603 if (i != '\n')
2604 while (getchar () != '\n')
2607 if (i != 'y' && i != 'Y')
2608 return 0;
2609 #endif /* DEBUG */
2612 #ifdef ENABLE_VALGRIND_CHECKING
2613 /* Run the each command through valgrind. To simplify prepending the
2614 path to valgrind and the option "-q" (for quiet operation unless
2615 something triggers), we allocate a separate argv array. */
2617 for (i = 0; i < n_commands; i++)
2619 const char **argv;
2620 int argc;
2621 int j;
2623 for (argc = 0; commands[i].argv[argc] != NULL; argc++)
2626 argv = XALLOCAVEC (const char *, argc + 3);
2628 argv[0] = VALGRIND_PATH;
2629 argv[1] = "-q";
2630 for (j = 2; j < argc + 2; j++)
2631 argv[j] = commands[i].argv[j - 2];
2632 argv[j] = NULL;
2634 commands[i].argv = argv;
2635 commands[i].prog = argv[0];
2637 #endif
2639 /* Run each piped subprocess. */
2641 pex = pex_init (PEX_USE_PIPES | ((report_times || report_times_to_file)
2642 ? PEX_RECORD_TIMES : 0),
2643 progname, temp_filename);
2644 if (pex == NULL)
2645 fatal_error ("pex_init failed: %m");
2647 for (i = 0; i < n_commands; i++)
2649 const char *errmsg;
2650 int err;
2651 const char *string = commands[i].argv[0];
2653 errmsg = pex_run (pex,
2654 ((i + 1 == n_commands ? PEX_LAST : 0)
2655 | (string == commands[i].prog ? PEX_SEARCH : 0)),
2656 string, CONST_CAST (char **, commands[i].argv),
2657 NULL, NULL, &err);
2658 if (errmsg != NULL)
2660 if (err == 0)
2661 fatal_error (errmsg);
2662 else
2664 errno = err;
2665 pfatal_with_name (errmsg);
2669 if (string != commands[i].prog)
2670 free (CONST_CAST (char *, string));
2673 execution_count++;
2675 /* Wait for all the subprocesses to finish. */
2678 int *statuses;
2679 struct pex_time *times = NULL;
2680 int ret_code = 0;
2682 statuses = (int *) alloca (n_commands * sizeof (int));
2683 if (!pex_get_status (pex, n_commands, statuses))
2684 fatal_error ("failed to get exit status: %m");
2686 if (report_times || report_times_to_file)
2688 times = (struct pex_time *) alloca (n_commands * sizeof (struct pex_time));
2689 if (!pex_get_times (pex, n_commands, times))
2690 fatal_error ("failed to get process times: %m");
2693 pex_free (pex);
2695 for (i = 0; i < n_commands; ++i)
2697 int status = statuses[i];
2699 if (WIFSIGNALED (status))
2701 #ifdef SIGPIPE
2702 /* SIGPIPE is a special case. It happens in -pipe mode
2703 when the compiler dies before the preprocessor is done,
2704 or the assembler dies before the compiler is done.
2705 There's generally been an error already, and this is
2706 just fallout. So don't generate another error unless
2707 we would otherwise have succeeded. */
2708 if (WTERMSIG (status) == SIGPIPE
2709 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2711 signal_count++;
2712 ret_code = -1;
2714 else
2715 #endif
2716 internal_error ("%s (program %s)",
2717 strsignal (WTERMSIG (status)), commands[i].prog);
2719 else if (WIFEXITED (status)
2720 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2722 if (WEXITSTATUS (status) > greatest_status)
2723 greatest_status = WEXITSTATUS (status);
2724 ret_code = -1;
2727 if (report_times || report_times_to_file)
2729 struct pex_time *pt = &times[i];
2730 double ut, st;
2732 ut = ((double) pt->user_seconds
2733 + (double) pt->user_microseconds / 1.0e6);
2734 st = ((double) pt->system_seconds
2735 + (double) pt->system_microseconds / 1.0e6);
2737 if (ut + st != 0)
2739 if (report_times)
2740 fnotice (stderr, "# %s %.2f %.2f\n",
2741 commands[i].prog, ut, st);
2743 if (report_times_to_file)
2745 int c = 0;
2746 const char *const *j;
2748 fprintf (report_times_to_file, "%g %g", ut, st);
2750 for (j = &commands[i].prog; *j; j = &commands[i].argv[++c])
2752 const char *p;
2753 for (p = *j; *p; ++p)
2754 if (*p == '"' || *p == '\\' || *p == '$'
2755 || ISSPACE (*p))
2756 break;
2758 if (*p)
2760 fprintf (report_times_to_file, " \"");
2761 for (p = *j; *p; ++p)
2763 if (*p == '"' || *p == '\\' || *p == '$')
2764 fputc ('\\', report_times_to_file);
2765 fputc (*p, report_times_to_file);
2767 fputc ('"', report_times_to_file);
2769 else
2770 fprintf (report_times_to_file, " %s", *j);
2773 fputc ('\n', report_times_to_file);
2779 return ret_code;
2783 /* Find all the switches given to us
2784 and make a vector describing them.
2785 The elements of the vector are strings, one per switch given.
2786 If a switch uses following arguments, then the `part1' field
2787 is the switch itself and the `args' field
2788 is a null-terminated vector containing the following arguments.
2789 Bits in the `live_cond' field are:
2790 SWITCH_LIVE to indicate this switch is true in a conditional spec.
2791 SWITCH_FALSE to indicate this switch is overridden by a later switch.
2792 SWITCH_IGNORE to indicate this switch should be ignored (used in %<S).
2793 SWITCH_IGNORE_PERMANENTLY to indicate this switch should be ignored
2794 in all do_spec calls afterwards. Used for %<S from self specs.
2795 The `validated' field is nonzero if any spec has looked at this switch;
2796 if it remains zero at the end of the run, it must be meaningless. */
2798 #define SWITCH_LIVE (1 << 0)
2799 #define SWITCH_FALSE (1 << 1)
2800 #define SWITCH_IGNORE (1 << 2)
2801 #define SWITCH_IGNORE_PERMANENTLY (1 << 3)
2802 #define SWITCH_KEEP_FOR_GCC (1 << 4)
2804 struct switchstr
2806 const char *part1;
2807 const char **args;
2808 unsigned int live_cond;
2809 unsigned char validated;
2810 unsigned char ordering;
2813 static struct switchstr *switches;
2815 static int n_switches;
2817 static int n_switches_alloc;
2819 /* Set to zero if -fcompare-debug is disabled, positive if it's
2820 enabled and we're running the first compilation, negative if it's
2821 enabled and we're running the second compilation. For most of the
2822 time, it's in the range -1..1, but it can be temporarily set to 2
2823 or 3 to indicate that the -fcompare-debug flags didn't come from
2824 the command-line, but rather from the GCC_COMPARE_DEBUG environment
2825 variable, until a synthesized -fcompare-debug flag is added to the
2826 command line. */
2827 int compare_debug;
2829 /* Set to nonzero if we've seen the -fcompare-debug-second flag. */
2830 int compare_debug_second;
2832 /* Set to the flags that should be passed to the second compilation in
2833 a -fcompare-debug compilation. */
2834 const char *compare_debug_opt;
2836 static struct switchstr *switches_debug_check[2];
2838 static int n_switches_debug_check[2];
2840 static int n_switches_alloc_debug_check[2];
2842 static char *debug_check_temp_file[2];
2844 /* Language is one of three things:
2846 1) The name of a real programming language.
2847 2) NULL, indicating that no one has figured out
2848 what it is yet.
2849 3) '*', indicating that the file should be passed
2850 to the linker. */
2851 struct infile
2853 const char *name;
2854 const char *language;
2855 struct compiler *incompiler;
2856 bool compiled;
2857 bool preprocessed;
2860 /* Also a vector of input files specified. */
2862 static struct infile *infiles;
2864 int n_infiles;
2866 static int n_infiles_alloc;
2868 /* True if multiple input files are being compiled to a single
2869 assembly file. */
2871 static bool combine_inputs;
2873 /* This counts the number of libraries added by lang_specific_driver, so that
2874 we can tell if there were any user supplied any files or libraries. */
2876 static int added_libraries;
2878 /* And a vector of corresponding output files is made up later. */
2880 const char **outfiles;
2882 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2884 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2885 is true if we should look for an executable suffix. DO_OBJ
2886 is true if we should look for an object suffix. */
2888 static const char *
2889 convert_filename (const char *name, int do_exe ATTRIBUTE_UNUSED,
2890 int do_obj ATTRIBUTE_UNUSED)
2892 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2893 int i;
2894 #endif
2895 int len;
2897 if (name == NULL)
2898 return NULL;
2900 len = strlen (name);
2902 #ifdef HAVE_TARGET_OBJECT_SUFFIX
2903 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
2904 if (do_obj && len > 2
2905 && name[len - 2] == '.'
2906 && name[len - 1] == 'o')
2908 obstack_grow (&obstack, name, len - 2);
2909 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
2910 name = XOBFINISH (&obstack, const char *);
2912 #endif
2914 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2915 /* If there is no filetype, make it the executable suffix (which includes
2916 the "."). But don't get confused if we have just "-o". */
2917 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2918 return name;
2920 for (i = len - 1; i >= 0; i--)
2921 if (IS_DIR_SEPARATOR (name[i]))
2922 break;
2924 for (i++; i < len; i++)
2925 if (name[i] == '.')
2926 return name;
2928 obstack_grow (&obstack, name, len);
2929 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
2930 strlen (TARGET_EXECUTABLE_SUFFIX));
2931 name = XOBFINISH (&obstack, const char *);
2932 #endif
2934 return name;
2936 #endif
2938 /* Display the command line switches accepted by gcc. */
2939 static void
2940 display_help (void)
2942 printf (_("Usage: %s [options] file...\n"), progname);
2943 fputs (_("Options:\n"), stdout);
2945 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2946 fputs (_(" --help Display this information\n"), stdout);
2947 fputs (_(" --target-help Display target specific command line options\n"), stdout);
2948 fputs (_(" --help={target|optimizers|warnings|params|[^]{joined|separate|undocumented}}[,...]\n"), stdout);
2949 fputs (_(" Display specific types of command line options\n"), stdout);
2950 if (! verbose_flag)
2951 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
2952 fputs (_(" --version Display compiler version information\n"), stdout);
2953 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2954 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2955 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2956 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2957 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2958 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2959 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2960 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2961 fputs (_("\
2962 -print-multi-lib Display the mapping between command line options and\n\
2963 multiple library search directories\n"), stdout);
2964 fputs (_(" -print-multi-os-directory Display the relative path to OS libraries\n"), stdout);
2965 fputs (_(" -print-sysroot Display the target libraries directory\n"), stdout);
2966 fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers\n"), stdout);
2967 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2968 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2969 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
2970 fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler\n"), stdout);
2971 fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor\n"), stdout);
2972 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2973 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
2974 fputs (_(" -save-temps=<arg> Do not delete intermediate files\n"), stdout);
2975 fputs (_("\
2976 -no-canonical-prefixes Do not canonicalize paths when building relative\n\
2977 prefixes to other gcc components\n"), stdout);
2978 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2979 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
2980 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
2981 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
2982 fputs (_("\
2983 --sysroot=<directory> Use <directory> as the root directory for headers\n\
2984 and libraries\n"), stdout);
2985 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
2986 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
2987 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
2988 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2989 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2990 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2991 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2992 fputs (_("\
2993 -x <language> Specify the language of the following input files\n\
2994 Permissible languages include: c c++ assembler none\n\
2995 'none' means revert to the default behavior of\n\
2996 guessing the language based on the file's extension\n\
2997 "), stdout);
2999 printf (_("\
3000 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3001 passed on to the various sub-processes invoked by %s. In order to pass\n\
3002 other options on to these processes the -W<letter> options must be used.\n\
3003 "), progname);
3005 /* The rest of the options are displayed by invocations of the various
3006 sub-processes. */
3009 static void
3010 add_preprocessor_option (const char *option, int len)
3012 VEC_safe_push (char_p, heap, preprocessor_options,
3013 save_string (option, len));
3016 static void
3017 add_assembler_option (const char *option, int len)
3019 VEC_safe_push (char_p, heap, assembler_options, save_string (option, len));
3022 static void
3023 add_linker_option (const char *option, int len)
3025 VEC_safe_push (char_p, heap, linker_options, save_string (option, len));
3028 /* Allocate space for an input file in infiles. */
3030 static void
3031 alloc_infile (void)
3033 if (n_infiles_alloc == 0)
3035 n_infiles_alloc = 16;
3036 infiles = XNEWVEC (struct infile, n_infiles_alloc);
3038 else if (n_infiles_alloc == n_infiles)
3040 n_infiles_alloc *= 2;
3041 infiles = XRESIZEVEC (struct infile, infiles, n_infiles_alloc);
3045 /* Store an input file with the given NAME and LANGUAGE in
3046 infiles. */
3048 static void
3049 add_infile (const char *name, const char *language)
3051 alloc_infile ();
3052 infiles[n_infiles].name = name;
3053 infiles[n_infiles++].language = language;
3056 /* Allocate space for a switch in switches. */
3058 static void
3059 alloc_switch (void)
3061 if (n_switches_alloc == 0)
3063 n_switches_alloc = 16;
3064 switches = XNEWVEC (struct switchstr, n_switches_alloc);
3066 else if (n_switches_alloc == n_switches)
3068 n_switches_alloc *= 2;
3069 switches = XRESIZEVEC (struct switchstr, switches, n_switches_alloc);
3073 /* Save an option OPT with N_ARGS arguments in array ARGS, marking it
3074 as validated if VALIDATED. */
3076 static void
3077 save_switch (const char *opt, size_t n_args, const char *const *args,
3078 bool validated)
3080 alloc_switch ();
3081 switches[n_switches].part1 = opt + 1;
3082 if (n_args == 0)
3083 switches[n_switches].args = 0;
3084 else
3086 switches[n_switches].args = XNEWVEC (const char *, n_args + 1);
3087 memcpy (switches[n_switches].args, args, n_args * sizeof (const char *));
3088 switches[n_switches].args[n_args] = NULL;
3091 switches[n_switches].live_cond = 0;
3092 switches[n_switches].validated = validated;
3093 switches[n_switches].ordering = 0;
3094 n_switches++;
3097 /* Handle an option DECODED that is unknown to the option-processing
3098 machinery. */
3100 static bool
3101 driver_unknown_option_callback (const struct cl_decoded_option *decoded)
3103 const char *opt = decoded->arg;
3104 if (opt[1] == 'W' && opt[2] == 'n' && opt[3] == 'o' && opt[4] == '-'
3105 && !(decoded->errors & CL_ERR_NEGATIVE))
3107 /* Leave unknown -Wno-* options for the compiler proper, to be
3108 diagnosed only if there are warnings. */
3109 save_switch (decoded->canonical_option[0],
3110 decoded->canonical_option_num_elements - 1,
3111 &decoded->canonical_option[1], false);
3112 return false;
3114 else
3115 return true;
3118 /* Handle an option DECODED that is not marked as CL_DRIVER.
3119 LANG_MASK will always be CL_DRIVER. */
3121 static void
3122 driver_wrong_lang_callback (const struct cl_decoded_option *decoded,
3123 unsigned int lang_mask ATTRIBUTE_UNUSED)
3125 /* At this point, non-driver options are accepted (and expected to
3126 be passed down by specs) unless marked to be rejected by the
3127 driver. Options to be rejected by the driver but accepted by the
3128 compilers proper are treated just like completely unknown
3129 options. */
3130 const struct cl_option *option = &cl_options[decoded->opt_index];
3132 if (option->cl_reject_driver)
3133 error ("unrecognized command line option %qs",
3134 decoded->orig_option_with_args_text);
3135 else
3136 save_switch (decoded->canonical_option[0],
3137 decoded->canonical_option_num_elements - 1,
3138 &decoded->canonical_option[1], false);
3141 /* Note that an option (index OPT_INDEX, argument ARG, value VALUE)
3142 has been successfully handled with a handler for mask MASK. */
3144 static void
3145 driver_post_handling_callback (const struct cl_decoded_option *decoded ATTRIBUTE_UNUSED,
3146 unsigned int mask ATTRIBUTE_UNUSED)
3148 /* Nothing to do here. */
3151 static const char *spec_lang = 0;
3152 static int last_language_n_infiles;
3154 /* Handle a driver option; arguments and return value as for
3155 handle_option. */
3157 static bool
3158 driver_handle_option (struct gcc_options *opts,
3159 struct gcc_options *opts_set,
3160 const struct cl_decoded_option *decoded,
3161 unsigned int lang_mask ATTRIBUTE_UNUSED, int kind,
3162 location_t loc,
3163 const struct cl_option_handlers *handlers ATTRIBUTE_UNUSED,
3164 diagnostic_context *dc)
3166 size_t opt_index = decoded->opt_index;
3167 const char *arg = decoded->arg;
3168 const char *compare_debug_replacement_opt;
3169 int value = decoded->value;
3170 bool validated = false;
3171 bool do_save = true;
3173 gcc_assert (opts == &global_options);
3174 gcc_assert (opts_set == &global_options_set);
3175 gcc_assert (kind == DK_UNSPECIFIED);
3176 gcc_assert (loc == UNKNOWN_LOCATION);
3177 gcc_assert (dc == global_dc);
3179 switch (opt_index)
3181 case OPT_dumpspecs:
3183 struct spec_list *sl;
3184 init_spec ();
3185 for (sl = specs; sl; sl = sl->next)
3186 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3187 if (link_command_spec)
3188 printf ("*link_command:\n%s\n\n", link_command_spec);
3189 exit (0);
3192 case OPT_dumpversion:
3193 printf ("%s\n", spec_version);
3194 exit (0);
3196 case OPT_dumpmachine:
3197 printf ("%s\n", spec_machine);
3198 exit (0);
3200 case OPT__version:
3201 print_version = 1;
3203 /* CPP driver cannot obtain switch from cc1_options. */
3204 if (is_cpp_driver)
3205 add_preprocessor_option ("--version", strlen ("--version"));
3206 add_assembler_option ("--version", strlen ("--version"));
3207 add_linker_option ("--version", strlen ("--version"));
3208 break;
3210 case OPT__help:
3211 print_help_list = 1;
3213 /* CPP driver cannot obtain switch from cc1_options. */
3214 if (is_cpp_driver)
3215 add_preprocessor_option ("--help", 6);
3216 add_assembler_option ("--help", 6);
3217 add_linker_option ("--help", 6);
3218 break;
3220 case OPT__help_:
3221 print_subprocess_help = 2;
3222 break;
3224 case OPT__target_help:
3225 print_subprocess_help = 1;
3227 /* CPP driver cannot obtain switch from cc1_options. */
3228 if (is_cpp_driver)
3229 add_preprocessor_option ("--target-help", 13);
3230 add_assembler_option ("--target-help", 13);
3231 add_linker_option ("--target-help", 13);
3232 break;
3234 case OPT_pass_exit_codes:
3235 case OPT_print_search_dirs:
3236 case OPT_print_file_name_:
3237 case OPT_print_prog_name_:
3238 case OPT_print_multi_lib:
3239 case OPT_print_multi_directory:
3240 case OPT_print_sysroot:
3241 case OPT_print_multi_os_directory:
3242 case OPT_print_sysroot_headers_suffix:
3243 case OPT_time:
3244 case OPT_wrapper:
3245 /* These options set the variables specified in common.opt
3246 automatically, and do not need to be saved for spec
3247 processing. */
3248 do_save = false;
3249 break;
3251 case OPT_print_libgcc_file_name:
3252 print_file_name = "libgcc.a";
3253 do_save = false;
3254 break;
3256 case OPT_fcompare_debug_second:
3257 compare_debug_second = 1;
3258 break;
3260 case OPT_fcompare_debug:
3261 switch (value)
3263 case 0:
3264 compare_debug_replacement_opt = "-fcompare-debug=";
3265 arg = "";
3266 goto compare_debug_with_arg;
3268 case 1:
3269 compare_debug_replacement_opt = "-fcompare-debug=-gtoggle";
3270 arg = "-gtoggle";
3271 goto compare_debug_with_arg;
3273 default:
3274 gcc_unreachable ();
3276 break;
3278 case OPT_fcompare_debug_:
3279 compare_debug_replacement_opt = decoded->canonical_option[0];
3280 compare_debug_with_arg:
3281 gcc_assert (decoded->canonical_option_num_elements == 1);
3282 gcc_assert (arg != NULL);
3283 if (arg)
3284 compare_debug = 1;
3285 else
3286 compare_debug = -1;
3287 if (compare_debug < 0)
3288 compare_debug_opt = NULL;
3289 else
3290 compare_debug_opt = arg;
3291 save_switch (compare_debug_replacement_opt, 0, NULL, validated);
3292 return true;
3294 case OPT_Wa_:
3296 int prev, j;
3297 /* Pass the rest of this option to the assembler. */
3299 /* Split the argument at commas. */
3300 prev = 0;
3301 for (j = 0; arg[j]; j++)
3302 if (arg[j] == ',')
3304 add_assembler_option (arg + prev, j - prev);
3305 prev = j + 1;
3308 /* Record the part after the last comma. */
3309 add_assembler_option (arg + prev, j - prev);
3311 do_save = false;
3312 break;
3314 case OPT_Wp_:
3316 int prev, j;
3317 /* Pass the rest of this option to the preprocessor. */
3319 /* Split the argument at commas. */
3320 prev = 0;
3321 for (j = 0; arg[j]; j++)
3322 if (arg[j] == ',')
3324 add_preprocessor_option (arg + prev, j - prev);
3325 prev = j + 1;
3328 /* Record the part after the last comma. */
3329 add_preprocessor_option (arg + prev, j - prev);
3331 do_save = false;
3332 break;
3334 case OPT_Wl_:
3336 int prev, j;
3337 /* Split the argument at commas. */
3338 prev = 0;
3339 for (j = 0; arg[j]; j++)
3340 if (arg[j] == ',')
3342 add_infile (save_string (arg + prev, j - prev), "*");
3343 prev = j + 1;
3345 /* Record the part after the last comma. */
3346 add_infile (arg + prev, "*");
3348 do_save = false;
3349 break;
3351 case OPT_Xlinker:
3352 add_infile (arg, "*");
3353 do_save = false;
3354 break;
3356 case OPT_Xpreprocessor:
3357 add_preprocessor_option (arg, strlen (arg));
3358 do_save = false;
3359 break;
3361 case OPT_Xassembler:
3362 add_assembler_option (arg, strlen (arg));
3363 do_save = false;
3364 break;
3366 case OPT_l:
3367 /* POSIX allows separation of -l and the lib arg; canonicalize
3368 by concatenating -l with its arg */
3369 add_infile (concat ("-l", arg, NULL), "*");
3370 do_save = false;
3371 break;
3373 case OPT_L:
3374 /* Similarly, canonicalize -L for linkers that may not accept
3375 separate arguments. */
3376 save_switch (concat ("-L", arg, NULL), 0, NULL, validated);
3377 return true;
3379 case OPT_F:
3380 /* Likewise -F. */
3381 save_switch (concat ("-F", arg, NULL), 0, NULL, validated);
3382 return true;
3384 case OPT_save_temps:
3385 save_temps_flag = SAVE_TEMPS_CWD;
3386 validated = true;
3387 break;
3389 case OPT_save_temps_:
3390 if (strcmp (arg, "cwd") == 0)
3391 save_temps_flag = SAVE_TEMPS_CWD;
3392 else if (strcmp (arg, "obj") == 0
3393 || strcmp (arg, "object") == 0)
3394 save_temps_flag = SAVE_TEMPS_OBJ;
3395 else
3396 fatal_error ("%qs is an unknown -save-temps option",
3397 decoded->orig_option_with_args_text);
3398 break;
3400 case OPT_no_canonical_prefixes:
3401 /* Already handled as a special case, so ignored here. */
3402 do_save = false;
3403 break;
3405 case OPT_pipe:
3406 validated = true;
3407 /* These options set the variables specified in common.opt
3408 automatically, but do need to be saved for spec
3409 processing. */
3410 break;
3412 case OPT_specs_:
3414 struct user_specs *user = XNEW (struct user_specs);
3416 user->next = (struct user_specs *) 0;
3417 user->filename = arg;
3418 if (user_specs_tail)
3419 user_specs_tail->next = user;
3420 else
3421 user_specs_head = user;
3422 user_specs_tail = user;
3424 do_save = false;
3425 break;
3427 case OPT__sysroot_:
3428 target_system_root = arg;
3429 target_system_root_changed = 1;
3430 do_save = false;
3431 break;
3433 case OPT_time_:
3434 if (report_times_to_file)
3435 fclose (report_times_to_file);
3436 report_times_to_file = fopen (arg, "a");
3437 do_save = false;
3438 break;
3440 case OPT____:
3441 /* "-###"
3442 This is similar to -v except that there is no execution
3443 of the commands and the echoed arguments are quoted. It
3444 is intended for use in shell scripts to capture the
3445 driver-generated command line. */
3446 verbose_only_flag++;
3447 verbose_flag = 1;
3448 do_save = false;
3449 break;
3451 case OPT_B:
3453 size_t len = strlen (arg);
3455 /* Catch the case where the user has forgotten to append a
3456 directory separator to the path. Note, they may be using
3457 -B to add an executable name prefix, eg "i386-elf-", in
3458 order to distinguish between multiple installations of
3459 GCC in the same directory. Hence we must check to see
3460 if appending a directory separator actually makes a
3461 valid directory name. */
3462 if (!IS_DIR_SEPARATOR (arg[len - 1])
3463 && is_directory (arg, false))
3465 char *tmp = XNEWVEC (char, len + 2);
3466 strcpy (tmp, arg);
3467 tmp[len] = DIR_SEPARATOR;
3468 tmp[++len] = 0;
3469 arg = tmp;
3472 add_prefix (&exec_prefixes, arg, NULL,
3473 PREFIX_PRIORITY_B_OPT, 0, 0);
3474 add_prefix (&startfile_prefixes, arg, NULL,
3475 PREFIX_PRIORITY_B_OPT, 0, 0);
3476 add_prefix (&include_prefixes, arg, NULL,
3477 PREFIX_PRIORITY_B_OPT, 0, 0);
3479 validated = true;
3480 break;
3482 case OPT_x:
3483 spec_lang = arg;
3484 if (!strcmp (spec_lang, "none"))
3485 /* Suppress the warning if -xnone comes after the last input
3486 file, because alternate command interfaces like g++ might
3487 find it useful to place -xnone after each input file. */
3488 spec_lang = 0;
3489 else
3490 last_language_n_infiles = n_infiles;
3491 do_save = false;
3492 break;
3494 case OPT_o:
3495 have_o = 1;
3496 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3497 arg = convert_filename (arg, ! have_c, 0);
3498 #endif
3499 /* Save the output name in case -save-temps=obj was used. */
3500 save_temps_prefix = xstrdup (arg);
3501 /* On some systems, ld cannot handle "-o" without a space. So
3502 split the option from its argument. */
3503 save_switch ("-o", 1, &arg, validated);
3504 return true;
3506 case OPT_static_libgcc:
3507 case OPT_shared_libgcc:
3508 case OPT_static_libgfortran:
3509 case OPT_static_libstdc__:
3510 /* These are always valid, since gcc.c itself understands the
3511 first two, gfortranspec.c understands -static-libgfortran and
3512 g++spec.c understands -static-libstdc++ */
3513 validated = true;
3514 break;
3516 default:
3517 /* Various driver options need no special processing at this
3518 point, having been handled in a prescan above or being
3519 handled by specs. */
3520 break;
3523 if (do_save)
3524 save_switch (decoded->canonical_option[0],
3525 decoded->canonical_option_num_elements - 1,
3526 &decoded->canonical_option[1], validated);
3527 return true;
3530 /* Put the driver's standard set of option handlers in *HANDLERS. */
3532 static void
3533 set_option_handlers (struct cl_option_handlers *handlers)
3535 handlers->unknown_option_callback = driver_unknown_option_callback;
3536 handlers->wrong_lang_callback = driver_wrong_lang_callback;
3537 handlers->post_handling_callback = driver_post_handling_callback;
3538 handlers->num_handlers = 1;
3539 handlers->handlers[0].handler = driver_handle_option;
3540 handlers->handlers[0].mask = CL_DRIVER;
3543 /* Create the vector `switches' and its contents.
3544 Store its length in `n_switches'. */
3546 static void
3547 process_command (unsigned int decoded_options_count,
3548 struct cl_decoded_option *decoded_options)
3550 const char *temp;
3551 char *temp1;
3552 const char *tooldir_prefix;
3553 char *(*get_relative_prefix) (const char *, const char *,
3554 const char *) = NULL;
3555 struct cl_option_handlers handlers;
3556 unsigned int j;
3558 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
3560 n_switches = 0;
3561 n_infiles = 0;
3562 added_libraries = 0;
3564 /* Figure compiler version from version string. */
3566 compiler_version = temp1 = xstrdup (version_string);
3568 for (; *temp1; ++temp1)
3570 if (*temp1 == ' ')
3572 *temp1 = '\0';
3573 break;
3577 /* Handle any -no-canonical-prefixes flag early, to assign the function
3578 that builds relative prefixes. This function creates default search
3579 paths that are needed later in normal option handling. */
3581 for (j = 1; j < decoded_options_count; j++)
3583 if (decoded_options[j].opt_index == OPT_no_canonical_prefixes)
3585 get_relative_prefix = make_relative_prefix_ignore_links;
3586 break;
3589 if (! get_relative_prefix)
3590 get_relative_prefix = make_relative_prefix;
3592 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3593 see if we can create it from the pathname specified in
3594 decoded_options[0].arg. */
3596 gcc_libexec_prefix = standard_libexec_prefix;
3597 #ifndef VMS
3598 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3599 if (!gcc_exec_prefix)
3601 gcc_exec_prefix = get_relative_prefix (decoded_options[0].arg,
3602 standard_bindir_prefix,
3603 standard_exec_prefix);
3604 gcc_libexec_prefix = get_relative_prefix (decoded_options[0].arg,
3605 standard_bindir_prefix,
3606 standard_libexec_prefix);
3607 if (gcc_exec_prefix)
3608 xputenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
3610 else
3612 /* make_relative_prefix requires a program name, but
3613 GCC_EXEC_PREFIX is typically a directory name with a trailing
3614 / (which is ignored by make_relative_prefix), so append a
3615 program name. */
3616 char *tmp_prefix = concat (gcc_exec_prefix, "gcc", NULL);
3617 gcc_libexec_prefix = get_relative_prefix (tmp_prefix,
3618 standard_exec_prefix,
3619 standard_libexec_prefix);
3621 /* The path is unrelocated, so fallback to the original setting. */
3622 if (!gcc_libexec_prefix)
3623 gcc_libexec_prefix = standard_libexec_prefix;
3625 free (tmp_prefix);
3627 #else
3628 #endif
3629 /* From this point onward, gcc_exec_prefix is non-null if the toolchain
3630 is relocated. The toolchain was either relocated using GCC_EXEC_PREFIX
3631 or an automatically created GCC_EXEC_PREFIX from
3632 decoded_options[0].arg. */
3634 /* Do language-specific adjustment/addition of flags. */
3635 lang_specific_driver (&decoded_options, &decoded_options_count,
3636 &added_libraries);
3638 if (gcc_exec_prefix)
3640 int len = strlen (gcc_exec_prefix);
3642 if (len > (int) sizeof ("/lib/gcc/") - 1
3643 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3645 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc/") + 1;
3646 if (IS_DIR_SEPARATOR (*temp)
3647 && filename_ncmp (temp + 1, "lib", 3) == 0
3648 && IS_DIR_SEPARATOR (temp[4])
3649 && filename_ncmp (temp + 5, "gcc", 3) == 0)
3650 len -= sizeof ("/lib/gcc/") - 1;
3653 set_std_prefix (gcc_exec_prefix, len);
3654 add_prefix (&exec_prefixes, gcc_libexec_prefix, "GCC",
3655 PREFIX_PRIORITY_LAST, 0, 0);
3656 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3657 PREFIX_PRIORITY_LAST, 0, 0);
3660 /* COMPILER_PATH and LIBRARY_PATH have values
3661 that are lists of directory names with colons. */
3663 temp = getenv ("COMPILER_PATH");
3664 if (temp)
3666 const char *startp, *endp;
3667 char *nstore = (char *) alloca (strlen (temp) + 3);
3669 startp = endp = temp;
3670 while (1)
3672 if (*endp == PATH_SEPARATOR || *endp == 0)
3674 strncpy (nstore, startp, endp - startp);
3675 if (endp == startp)
3676 strcpy (nstore, concat (".", dir_separator_str, NULL));
3677 else if (!IS_DIR_SEPARATOR (endp[-1]))
3679 nstore[endp - startp] = DIR_SEPARATOR;
3680 nstore[endp - startp + 1] = 0;
3682 else
3683 nstore[endp - startp] = 0;
3684 add_prefix (&exec_prefixes, nstore, 0,
3685 PREFIX_PRIORITY_LAST, 0, 0);
3686 add_prefix (&include_prefixes, nstore, 0,
3687 PREFIX_PRIORITY_LAST, 0, 0);
3688 if (*endp == 0)
3689 break;
3690 endp = startp = endp + 1;
3692 else
3693 endp++;
3697 temp = getenv (LIBRARY_PATH_ENV);
3698 if (temp && *cross_compile == '0')
3700 const char *startp, *endp;
3701 char *nstore = (char *) alloca (strlen (temp) + 3);
3703 startp = endp = temp;
3704 while (1)
3706 if (*endp == PATH_SEPARATOR || *endp == 0)
3708 strncpy (nstore, startp, endp - startp);
3709 if (endp == startp)
3710 strcpy (nstore, concat (".", dir_separator_str, NULL));
3711 else if (!IS_DIR_SEPARATOR (endp[-1]))
3713 nstore[endp - startp] = DIR_SEPARATOR;
3714 nstore[endp - startp + 1] = 0;
3716 else
3717 nstore[endp - startp] = 0;
3718 add_prefix (&startfile_prefixes, nstore, NULL,
3719 PREFIX_PRIORITY_LAST, 0, 1);
3720 if (*endp == 0)
3721 break;
3722 endp = startp = endp + 1;
3724 else
3725 endp++;
3729 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3730 temp = getenv ("LPATH");
3731 if (temp && *cross_compile == '0')
3733 const char *startp, *endp;
3734 char *nstore = (char *) alloca (strlen (temp) + 3);
3736 startp = endp = temp;
3737 while (1)
3739 if (*endp == PATH_SEPARATOR || *endp == 0)
3741 strncpy (nstore, startp, endp - startp);
3742 if (endp == startp)
3743 strcpy (nstore, concat (".", dir_separator_str, NULL));
3744 else if (!IS_DIR_SEPARATOR (endp[-1]))
3746 nstore[endp - startp] = DIR_SEPARATOR;
3747 nstore[endp - startp + 1] = 0;
3749 else
3750 nstore[endp - startp] = 0;
3751 add_prefix (&startfile_prefixes, nstore, NULL,
3752 PREFIX_PRIORITY_LAST, 0, 1);
3753 if (*endp == 0)
3754 break;
3755 endp = startp = endp + 1;
3757 else
3758 endp++;
3762 /* Process the options and store input files and switches in their
3763 vectors. */
3765 last_language_n_infiles = -1;
3767 set_option_handlers (&handlers);
3769 for (j = 1; j < decoded_options_count; j++)
3771 switch (decoded_options[j].opt_index)
3773 case OPT_S:
3774 case OPT_c:
3775 case OPT_E:
3776 have_c = 1;
3777 break;
3779 if (have_c)
3780 break;
3783 for (j = 1; j < decoded_options_count; j++)
3785 if (decoded_options[j].opt_index == OPT_SPECIAL_input_file)
3787 const char *arg = decoded_options[j].arg;
3788 const char *p = strrchr (arg, '@');
3789 char *fname;
3790 long offset;
3791 int consumed;
3792 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3793 arg = convert_filename (arg, 0, access (arg, F_OK));
3794 #endif
3795 /* For LTO static archive support we handle input file
3796 specifications that are composed of a filename and
3797 an offset like FNAME@OFFSET. */
3798 if (p
3799 && p != arg
3800 && sscanf (p, "@%li%n", &offset, &consumed) >= 1
3801 && strlen (p) == (unsigned int)consumed)
3803 fname = (char *)xmalloc (p - arg + 1);
3804 memcpy (fname, arg, p - arg);
3805 fname[p - arg] = '\0';
3806 /* Only accept non-stdin and existing FNAME parts, otherwise
3807 try with the full name. */
3808 if (strcmp (fname, "-") == 0 || access (fname, F_OK) < 0)
3810 free (fname);
3811 fname = xstrdup (arg);
3814 else
3815 fname = xstrdup (arg);
3817 if (strcmp (fname, "-") != 0 && access (fname, F_OK) < 0)
3818 perror_with_name (fname);
3819 else
3820 add_infile (arg, spec_lang);
3822 free (fname);
3823 continue;
3826 read_cmdline_option (&global_options, &global_options_set,
3827 decoded_options + j, UNKNOWN_LOCATION,
3828 CL_DRIVER, &handlers, global_dc);
3831 /* If -save-temps=obj and -o name, create the prefix to use for %b.
3832 Otherwise just make -save-temps=obj the same as -save-temps=cwd. */
3833 if (save_temps_flag == SAVE_TEMPS_OBJ && save_temps_prefix != NULL)
3835 save_temps_length = strlen (save_temps_prefix);
3836 temp = strrchr (lbasename (save_temps_prefix), '.');
3837 if (temp)
3839 save_temps_length -= strlen (temp);
3840 save_temps_prefix[save_temps_length] = '\0';
3844 else if (save_temps_prefix != NULL)
3846 free (save_temps_prefix);
3847 save_temps_prefix = NULL;
3850 if (save_temps_flag && use_pipes)
3852 /* -save-temps overrides -pipe, so that temp files are produced */
3853 if (save_temps_flag)
3854 warning (0, "-pipe ignored because -save-temps specified");
3855 use_pipes = 0;
3858 if (!compare_debug)
3860 const char *gcd = getenv ("GCC_COMPARE_DEBUG");
3862 if (gcd && gcd[0] == '-')
3864 compare_debug = 2;
3865 compare_debug_opt = gcd;
3867 else if (gcd && *gcd && strcmp (gcd, "0"))
3869 compare_debug = 3;
3870 compare_debug_opt = "-gtoggle";
3873 else if (compare_debug < 0)
3875 compare_debug = 0;
3876 gcc_assert (!compare_debug_opt);
3879 /* Set up the search paths. We add directories that we expect to
3880 contain GNU Toolchain components before directories specified by
3881 the machine description so that we will find GNU components (like
3882 the GNU assembler) before those of the host system. */
3884 /* If we don't know where the toolchain has been installed, use the
3885 configured-in locations. */
3886 if (!gcc_exec_prefix)
3888 #ifndef OS2
3889 add_prefix (&exec_prefixes, standard_libexec_prefix, "GCC",
3890 PREFIX_PRIORITY_LAST, 1, 0);
3891 add_prefix (&exec_prefixes, standard_libexec_prefix, "BINUTILS",
3892 PREFIX_PRIORITY_LAST, 2, 0);
3893 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3894 PREFIX_PRIORITY_LAST, 2, 0);
3895 #endif
3896 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3897 PREFIX_PRIORITY_LAST, 1, 0);
3900 gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
3901 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3902 dir_separator_str, NULL);
3904 /* Look for tools relative to the location from which the driver is
3905 running, or, if that is not available, the configured prefix. */
3906 tooldir_prefix
3907 = concat (gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
3908 spec_machine, dir_separator_str,
3909 spec_version, dir_separator_str, tooldir_prefix, NULL);
3911 add_prefix (&exec_prefixes,
3912 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
3913 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 0);
3914 add_prefix (&startfile_prefixes,
3915 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
3916 "BINUTILS", PREFIX_PRIORITY_LAST, 0, 1);
3918 #if defined(TARGET_SYSTEM_ROOT_RELOCATABLE) && !defined(VMS)
3919 /* If the normal TARGET_SYSTEM_ROOT is inside of $exec_prefix,
3920 then consider it to relocate with the rest of the GCC installation
3921 if GCC_EXEC_PREFIX is set.
3922 ``make_relative_prefix'' is not compiled for VMS, so don't call it. */
3923 if (target_system_root && !target_system_root_changed && gcc_exec_prefix)
3925 char *tmp_prefix = get_relative_prefix (decoded_options[0].arg,
3926 standard_bindir_prefix,
3927 target_system_root);
3928 if (tmp_prefix && access_check (tmp_prefix, F_OK) == 0)
3930 target_system_root = tmp_prefix;
3931 target_system_root_changed = 1;
3934 #endif
3936 /* More prefixes are enabled in main, after we read the specs file
3937 and determine whether this is cross-compilation or not. */
3939 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3940 warning (0, "%<-x %s%> after last input file has no effect", spec_lang);
3942 if (compare_debug == 2 || compare_debug == 3)
3944 alloc_switch ();
3945 switches[n_switches].part1 = concat ("fcompare-debug=",
3946 compare_debug_opt,
3947 NULL);
3948 switches[n_switches].args = 0;
3949 switches[n_switches].live_cond = 0;
3950 switches[n_switches].validated = 0;
3951 switches[n_switches].ordering = 0;
3952 n_switches++;
3953 compare_debug = 1;
3956 /* Ensure we only invoke each subprocess once. */
3957 if (print_subprocess_help || print_help_list || print_version)
3959 n_infiles = 0;
3961 /* Create a dummy input file, so that we can pass
3962 the help option on to the various sub-processes. */
3963 add_infile ("help-dummy", "c");
3966 alloc_switch ();
3967 switches[n_switches].part1 = 0;
3968 alloc_infile ();
3969 infiles[n_infiles].name = 0;
3972 /* Store switches not filtered out by %<S in spec in COLLECT_GCC_OPTIONS
3973 and place that in the environment. */
3975 static void
3976 set_collect_gcc_options (void)
3978 int i;
3979 int first_time;
3981 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3982 the compiler. */
3983 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3984 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
3986 first_time = TRUE;
3987 for (i = 0; (int) i < n_switches; i++)
3989 const char *const *args;
3990 const char *p, *q;
3991 if (!first_time)
3992 obstack_grow (&collect_obstack, " ", 1);
3994 first_time = FALSE;
3996 /* Ignore elided switches. */
3997 if ((switches[i].live_cond
3998 & (SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC))
3999 == SWITCH_IGNORE)
4000 continue;
4002 obstack_grow (&collect_obstack, "'-", 2);
4003 q = switches[i].part1;
4004 while ((p = strchr (q, '\'')))
4006 obstack_grow (&collect_obstack, q, p - q);
4007 obstack_grow (&collect_obstack, "'\\''", 4);
4008 q = ++p;
4010 obstack_grow (&collect_obstack, q, strlen (q));
4011 obstack_grow (&collect_obstack, "'", 1);
4013 for (args = switches[i].args; args && *args; args++)
4015 obstack_grow (&collect_obstack, " '", 2);
4016 q = *args;
4017 while ((p = strchr (q, '\'')))
4019 obstack_grow (&collect_obstack, q, p - q);
4020 obstack_grow (&collect_obstack, "'\\''", 4);
4021 q = ++p;
4023 obstack_grow (&collect_obstack, q, strlen (q));
4024 obstack_grow (&collect_obstack, "'", 1);
4027 obstack_grow (&collect_obstack, "\0", 1);
4028 xputenv (XOBFINISH (&collect_obstack, char *));
4031 /* Process a spec string, accumulating and running commands. */
4033 /* These variables describe the input file name.
4034 input_file_number is the index on outfiles of this file,
4035 so that the output file name can be stored for later use by %o.
4036 input_basename is the start of the part of the input file
4037 sans all directory names, and basename_length is the number
4038 of characters starting there excluding the suffix .c or whatever. */
4040 static const char *gcc_input_filename;
4041 static int input_file_number;
4042 size_t input_filename_length;
4043 static int basename_length;
4044 static int suffixed_basename_length;
4045 static const char *input_basename;
4046 static const char *input_suffix;
4047 #ifndef HOST_LACKS_INODE_NUMBERS
4048 static struct stat input_stat;
4049 #endif
4050 static int input_stat_set;
4052 /* The compiler used to process the current input file. */
4053 static struct compiler *input_file_compiler;
4055 /* These are variables used within do_spec and do_spec_1. */
4057 /* Nonzero if an arg has been started and not yet terminated
4058 (with space, tab or newline). */
4059 static int arg_going;
4061 /* Nonzero means %d or %g has been seen; the next arg to be terminated
4062 is a temporary file name. */
4063 static int delete_this_arg;
4065 /* Nonzero means %w has been seen; the next arg to be terminated
4066 is the output file name of this compilation. */
4067 static int this_is_output_file;
4069 /* Nonzero means %s has been seen; the next arg to be terminated
4070 is the name of a library file and we should try the standard
4071 search dirs for it. */
4072 static int this_is_library_file;
4074 /* Nonzero means %T has been seen; the next arg to be terminated
4075 is the name of a linker script and we should try all of the
4076 standard search dirs for it. If it is found insert a --script
4077 command line switch and then substitute the full path in place,
4078 otherwise generate an error message. */
4079 static int this_is_linker_script;
4081 /* Nonzero means that the input of this command is coming from a pipe. */
4082 static int input_from_pipe;
4084 /* Nonnull means substitute this for any suffix when outputting a switches
4085 arguments. */
4086 static const char *suffix_subst;
4088 /* If there is an argument being accumulated, terminate it and store it. */
4090 static void
4091 end_going_arg (void)
4093 if (arg_going)
4095 const char *string;
4097 obstack_1grow (&obstack, 0);
4098 string = XOBFINISH (&obstack, const char *);
4099 if (this_is_library_file)
4100 string = find_file (string);
4101 if (this_is_linker_script)
4103 char * full_script_path = find_a_file (&startfile_prefixes, string, R_OK, true);
4105 if (full_script_path == NULL)
4107 error ("unable to locate default linker script %qs in the library search paths", string);
4108 /* Script was not found on search path. */
4109 return;
4111 store_arg ("--script", false, false);
4112 string = full_script_path;
4114 store_arg (string, delete_this_arg, this_is_output_file);
4115 if (this_is_output_file)
4116 outfiles[input_file_number] = string;
4117 arg_going = 0;
4122 /* Parse the WRAPPER string which is a comma separated list of the command line
4123 and insert them into the beginning of argbuf. */
4125 static void
4126 insert_wrapper (const char *wrapper)
4128 int n = 0;
4129 int i;
4130 char *buf = xstrdup (wrapper);
4131 char *p = buf;
4132 unsigned int old_length = VEC_length (const_char_p, argbuf);
4136 n++;
4137 while (*p == ',')
4138 p++;
4140 while ((p = strchr (p, ',')) != NULL);
4142 VEC_safe_grow (const_char_p, heap, argbuf, old_length + n);
4143 memmove (VEC_address (const_char_p, argbuf) + n,
4144 VEC_address (const_char_p, argbuf),
4145 old_length * sizeof (const_char_p));
4147 i = 0;
4148 p = buf;
4151 while (*p == ',')
4153 *p = 0;
4154 p++;
4156 VEC_replace (const_char_p, argbuf, i, p);
4157 i++;
4159 while ((p = strchr (p, ',')) != NULL);
4160 gcc_assert (i == n);
4163 /* Process the spec SPEC and run the commands specified therein.
4164 Returns 0 if the spec is successfully processed; -1 if failed. */
4167 do_spec (const char *spec)
4169 int value;
4171 value = do_spec_2 (spec);
4173 /* Force out any unfinished command.
4174 If -pipe, this forces out the last command if it ended in `|'. */
4175 if (value == 0)
4177 if (VEC_length (const_char_p, argbuf) > 0
4178 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
4179 VEC_pop (const_char_p, argbuf);
4181 set_collect_gcc_options ();
4183 if (VEC_length (const_char_p, argbuf) > 0)
4184 value = execute ();
4187 return value;
4190 static int
4191 do_spec_2 (const char *spec)
4193 int result;
4195 clear_args ();
4196 arg_going = 0;
4197 delete_this_arg = 0;
4198 this_is_output_file = 0;
4199 this_is_library_file = 0;
4200 this_is_linker_script = 0;
4201 input_from_pipe = 0;
4202 suffix_subst = NULL;
4204 result = do_spec_1 (spec, 0, NULL);
4206 end_going_arg ();
4208 return result;
4212 /* Process the given spec string and add any new options to the end
4213 of the switches/n_switches array. */
4215 static void
4216 do_option_spec (const char *name, const char *spec)
4218 unsigned int i, value_count, value_len;
4219 const char *p, *q, *value;
4220 char *tmp_spec, *tmp_spec_p;
4222 if (configure_default_options[0].name == NULL)
4223 return;
4225 for (i = 0; i < ARRAY_SIZE (configure_default_options); i++)
4226 if (strcmp (configure_default_options[i].name, name) == 0)
4227 break;
4228 if (i == ARRAY_SIZE (configure_default_options))
4229 return;
4231 value = configure_default_options[i].value;
4232 value_len = strlen (value);
4234 /* Compute the size of the final spec. */
4235 value_count = 0;
4236 p = spec;
4237 while ((p = strstr (p, "%(VALUE)")) != NULL)
4239 p ++;
4240 value_count ++;
4243 /* Replace each %(VALUE) by the specified value. */
4244 tmp_spec = (char *) alloca (strlen (spec) + 1
4245 + value_count * (value_len - strlen ("%(VALUE)")));
4246 tmp_spec_p = tmp_spec;
4247 q = spec;
4248 while ((p = strstr (q, "%(VALUE)")) != NULL)
4250 memcpy (tmp_spec_p, q, p - q);
4251 tmp_spec_p = tmp_spec_p + (p - q);
4252 memcpy (tmp_spec_p, value, value_len);
4253 tmp_spec_p += value_len;
4254 q = p + strlen ("%(VALUE)");
4256 strcpy (tmp_spec_p, q);
4258 do_self_spec (tmp_spec);
4261 /* Process the given spec string and add any new options to the end
4262 of the switches/n_switches array. */
4264 static void
4265 do_self_spec (const char *spec)
4267 int i;
4269 do_spec_2 (spec);
4270 do_spec_1 (" ", 0, NULL);
4272 /* Mark %<S switches processed by do_self_spec to be ignored permanently.
4273 do_self_specs adds the replacements to switches array, so it shouldn't
4274 be processed afterwards. */
4275 for (i = 0; i < n_switches; i++)
4276 if ((switches[i].live_cond & SWITCH_IGNORE))
4277 switches[i].live_cond |= SWITCH_IGNORE_PERMANENTLY;
4279 if (VEC_length (const_char_p, argbuf) > 0)
4281 const char **argbuf_copy;
4282 struct cl_decoded_option *decoded_options;
4283 struct cl_option_handlers handlers;
4284 unsigned int decoded_options_count;
4285 unsigned int j;
4287 /* Create a copy of argbuf with a dummy argv[0] entry for
4288 decode_cmdline_options_to_array. */
4289 argbuf_copy = XNEWVEC (const char *,
4290 VEC_length (const_char_p, argbuf) + 1);
4291 argbuf_copy[0] = "";
4292 memcpy (argbuf_copy + 1, VEC_address (const_char_p, argbuf),
4293 VEC_length (const_char_p, argbuf) * sizeof (const char *));
4295 decode_cmdline_options_to_array (VEC_length (const_char_p, argbuf) + 1,
4296 argbuf_copy,
4297 CL_DRIVER, &decoded_options,
4298 &decoded_options_count);
4300 set_option_handlers (&handlers);
4302 for (j = 1; j < decoded_options_count; j++)
4304 switch (decoded_options[j].opt_index)
4306 case OPT_SPECIAL_input_file:
4307 /* Specs should only generate options, not input
4308 files. */
4309 if (strcmp (decoded_options[j].arg, "-") != 0)
4310 fatal_error ("switch %qs does not start with %<-%>",
4311 decoded_options[j].arg);
4312 else
4313 fatal_error ("spec-generated switch is just %<-%>");
4314 break;
4316 case OPT_fcompare_debug_second:
4317 case OPT_fcompare_debug:
4318 case OPT_fcompare_debug_:
4319 case OPT_o:
4320 /* Avoid duplicate processing of some options from
4321 compare-debug specs; just save them here. */
4322 save_switch (decoded_options[j].canonical_option[0],
4323 (decoded_options[j].canonical_option_num_elements
4324 - 1),
4325 &decoded_options[j].canonical_option[1], false);
4326 break;
4328 default:
4329 read_cmdline_option (&global_options, &global_options_set,
4330 decoded_options + j, UNKNOWN_LOCATION,
4331 CL_DRIVER, &handlers, global_dc);
4332 break;
4336 alloc_switch ();
4337 switches[n_switches].part1 = 0;
4341 /* Callback for processing %D and %I specs. */
4343 struct spec_path_info {
4344 const char *option;
4345 const char *append;
4346 size_t append_len;
4347 bool omit_relative;
4348 bool separate_options;
4351 static void *
4352 spec_path (char *path, void *data)
4354 struct spec_path_info *info = (struct spec_path_info *) data;
4355 size_t len = 0;
4356 char save = 0;
4358 if (info->omit_relative && !IS_ABSOLUTE_PATH (path))
4359 return NULL;
4361 if (info->append_len != 0)
4363 len = strlen (path);
4364 memcpy (path + len, info->append, info->append_len + 1);
4367 if (!is_directory (path, true))
4368 return NULL;
4370 do_spec_1 (info->option, 1, NULL);
4371 if (info->separate_options)
4372 do_spec_1 (" ", 0, NULL);
4374 if (info->append_len == 0)
4376 len = strlen (path);
4377 save = path[len - 1];
4378 if (IS_DIR_SEPARATOR (path[len - 1]))
4379 path[len - 1] = '\0';
4382 do_spec_1 (path, 1, NULL);
4383 do_spec_1 (" ", 0, NULL);
4385 /* Must not damage the original path. */
4386 if (info->append_len == 0)
4387 path[len - 1] = save;
4389 return NULL;
4392 /* Create a temporary FILE with the contents of ARGV. Add @FILE to the
4393 argument list. */
4395 static void
4396 create_at_file (char **argv)
4398 char *temp_file = make_temp_file ("");
4399 char *at_argument = concat ("@", temp_file, NULL);
4400 FILE *f = fopen (temp_file, "w");
4401 int status;
4403 if (f == NULL)
4404 fatal_error ("could not open temporary response file %s",
4405 temp_file);
4407 status = writeargv (argv, f);
4409 if (status)
4410 fatal_error ("could not write to temporary response file %s",
4411 temp_file);
4413 status = fclose (f);
4415 if (EOF == status)
4416 fatal_error ("could not close temporary response file %s",
4417 temp_file);
4419 store_arg (at_argument, 0, 0);
4421 record_temp_file (temp_file, !save_temps_flag, !save_temps_flag);
4424 /* True if we should compile INFILE. */
4426 static bool
4427 compile_input_file_p (struct infile *infile)
4429 if ((!infile->language) || (infile->language[0] != '*'))
4430 if (infile->incompiler == input_file_compiler)
4431 return true;
4432 return false;
4435 /* Process each member of VEC as a spec. */
4437 static void
4438 do_specs_vec (VEC(char_p,heap) *vec)
4440 unsigned ix;
4441 char *opt;
4443 FOR_EACH_VEC_ELT (char_p, vec, ix, opt)
4445 do_spec_1 (opt, 1, NULL);
4446 /* Make each accumulated option a separate argument. */
4447 do_spec_1 (" ", 0, NULL);
4451 /* Process the sub-spec SPEC as a portion of a larger spec.
4452 This is like processing a whole spec except that we do
4453 not initialize at the beginning and we do not supply a
4454 newline by default at the end.
4455 INSWITCH nonzero means don't process %-sequences in SPEC;
4456 in this case, % is treated as an ordinary character.
4457 This is used while substituting switches.
4458 INSWITCH nonzero also causes SPC not to terminate an argument.
4460 Value is zero unless a line was finished
4461 and the command on that line reported an error. */
4463 static int
4464 do_spec_1 (const char *spec, int inswitch, const char *soft_matched_part)
4466 const char *p = spec;
4467 int c;
4468 int i;
4469 int value;
4471 /* If it's an empty string argument to a switch, keep it as is. */
4472 if (inswitch && !*p)
4473 arg_going = 1;
4475 while ((c = *p++))
4476 /* If substituting a switch, treat all chars like letters.
4477 Otherwise, NL, SPC, TAB and % are special. */
4478 switch (inswitch ? 'a' : c)
4480 case '\n':
4481 end_going_arg ();
4483 if (VEC_length (const_char_p, argbuf) > 0
4484 && !strcmp (VEC_last (const_char_p, argbuf), "|"))
4486 /* A `|' before the newline means use a pipe here,
4487 but only if -pipe was specified.
4488 Otherwise, execute now and don't pass the `|' as an arg. */
4489 if (use_pipes)
4491 input_from_pipe = 1;
4492 break;
4494 else
4495 VEC_pop (const_char_p, argbuf);
4498 set_collect_gcc_options ();
4500 if (VEC_length (const_char_p, argbuf) > 0)
4502 value = execute ();
4503 if (value)
4504 return value;
4506 /* Reinitialize for a new command, and for a new argument. */
4507 clear_args ();
4508 arg_going = 0;
4509 delete_this_arg = 0;
4510 this_is_output_file = 0;
4511 this_is_library_file = 0;
4512 this_is_linker_script = 0;
4513 input_from_pipe = 0;
4514 break;
4516 case '|':
4517 end_going_arg ();
4519 /* Use pipe */
4520 obstack_1grow (&obstack, c);
4521 arg_going = 1;
4522 break;
4524 case '\t':
4525 case ' ':
4526 end_going_arg ();
4528 /* Reinitialize for a new argument. */
4529 delete_this_arg = 0;
4530 this_is_output_file = 0;
4531 this_is_library_file = 0;
4532 this_is_linker_script = 0;
4533 break;
4535 case '%':
4536 switch (c = *p++)
4538 case 0:
4539 fatal_error ("spec %qs invalid", spec);
4541 case 'b':
4542 if (save_temps_length)
4543 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4544 else
4545 obstack_grow (&obstack, input_basename, basename_length);
4546 if (compare_debug < 0)
4547 obstack_grow (&obstack, ".gk", 3);
4548 arg_going = 1;
4549 break;
4551 case 'B':
4552 if (save_temps_length)
4553 obstack_grow (&obstack, save_temps_prefix, save_temps_length);
4554 else
4555 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4556 if (compare_debug < 0)
4557 obstack_grow (&obstack, ".gk", 3);
4558 arg_going = 1;
4559 break;
4561 case 'd':
4562 delete_this_arg = 2;
4563 break;
4565 /* Dump out the directories specified with LIBRARY_PATH,
4566 followed by the absolute directories
4567 that we search for startfiles. */
4568 case 'D':
4570 struct spec_path_info info;
4572 info.option = "-L";
4573 info.append_len = 0;
4574 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4575 /* Used on systems which record the specified -L dirs
4576 and use them to search for dynamic linking.
4577 Relative directories always come from -B,
4578 and it is better not to use them for searching
4579 at run time. In particular, stage1 loses. */
4580 info.omit_relative = true;
4581 #else
4582 info.omit_relative = false;
4583 #endif
4584 info.separate_options = false;
4586 for_each_path (&startfile_prefixes, true, 0, spec_path, &info);
4588 break;
4590 case 'e':
4591 /* %efoo means report an error with `foo' as error message
4592 and don't execute any more commands for this file. */
4594 const char *q = p;
4595 char *buf;
4596 while (*p != 0 && *p != '\n')
4597 p++;
4598 buf = (char *) alloca (p - q + 1);
4599 strncpy (buf, q, p - q);
4600 buf[p - q] = 0;
4601 error ("%s", _(buf));
4602 return -1;
4604 break;
4605 case 'n':
4606 /* %nfoo means report a notice with `foo' on stderr. */
4608 const char *q = p;
4609 char *buf;
4610 while (*p != 0 && *p != '\n')
4611 p++;
4612 buf = (char *) alloca (p - q + 1);
4613 strncpy (buf, q, p - q);
4614 buf[p - q] = 0;
4615 inform (0, "%s", _(buf));
4616 if (*p)
4617 p++;
4619 break;
4621 case 'j':
4623 struct stat st;
4625 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is
4626 defined, and it is not a directory, and it is
4627 writable, use it. Otherwise, treat this like any
4628 other temporary file. */
4630 if ((!save_temps_flag)
4631 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4632 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4634 obstack_grow (&obstack, HOST_BIT_BUCKET,
4635 strlen (HOST_BIT_BUCKET));
4636 delete_this_arg = 0;
4637 arg_going = 1;
4638 break;
4641 goto create_temp_file;
4642 case '|':
4643 if (use_pipes)
4645 obstack_1grow (&obstack, '-');
4646 delete_this_arg = 0;
4647 arg_going = 1;
4649 /* consume suffix */
4650 while (*p == '.' || ISALNUM ((unsigned char) *p))
4651 p++;
4652 if (p[0] == '%' && p[1] == 'O')
4653 p += 2;
4655 break;
4657 goto create_temp_file;
4658 case 'm':
4659 if (use_pipes)
4661 /* consume suffix */
4662 while (*p == '.' || ISALNUM ((unsigned char) *p))
4663 p++;
4664 if (p[0] == '%' && p[1] == 'O')
4665 p += 2;
4667 break;
4669 goto create_temp_file;
4670 case 'g':
4671 case 'u':
4672 case 'U':
4673 create_temp_file:
4675 struct temp_name *t;
4676 int suffix_length;
4677 const char *suffix = p;
4678 char *saved_suffix = NULL;
4680 while (*p == '.' || ISALNUM ((unsigned char) *p))
4681 p++;
4682 suffix_length = p - suffix;
4683 if (p[0] == '%' && p[1] == 'O')
4685 p += 2;
4686 /* We don't support extra suffix characters after %O. */
4687 if (*p == '.' || ISALNUM ((unsigned char) *p))
4688 fatal_error ("spec %qs has invalid %<%%0%c%>", spec, *p);
4689 if (suffix_length == 0)
4690 suffix = TARGET_OBJECT_SUFFIX;
4691 else
4693 saved_suffix
4694 = XNEWVEC (char, suffix_length
4695 + strlen (TARGET_OBJECT_SUFFIX));
4696 strncpy (saved_suffix, suffix, suffix_length);
4697 strcpy (saved_suffix + suffix_length,
4698 TARGET_OBJECT_SUFFIX);
4700 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
4703 if (compare_debug < 0)
4705 suffix = concat (".gk", suffix, NULL);
4706 suffix_length += 3;
4709 /* If -save-temps=obj and -o were specified, use that for the
4710 temp file. */
4711 if (save_temps_length)
4713 char *tmp;
4714 temp_filename_length
4715 = save_temps_length + suffix_length + 1;
4716 tmp = (char *) alloca (temp_filename_length);
4717 memcpy (tmp, save_temps_prefix, save_temps_length);
4718 memcpy (tmp + save_temps_length, suffix, suffix_length);
4719 tmp[save_temps_length + suffix_length] = '\0';
4720 temp_filename = save_string (tmp,
4721 temp_filename_length + 1);
4722 obstack_grow (&obstack, temp_filename,
4723 temp_filename_length);
4724 arg_going = 1;
4725 delete_this_arg = 0;
4726 break;
4729 /* If the gcc_input_filename has the same suffix specified
4730 for the %g, %u, or %U, and -save-temps is specified,
4731 we could end up using that file as an intermediate
4732 thus clobbering the user's source file (.e.g.,
4733 gcc -save-temps foo.s would clobber foo.s with the
4734 output of cpp0). So check for this condition and
4735 generate a temp file as the intermediate. */
4737 if (save_temps_flag)
4739 char *tmp;
4740 temp_filename_length = basename_length + suffix_length + 1;
4741 tmp = (char *) alloca (temp_filename_length);
4742 memcpy (tmp, input_basename, basename_length);
4743 memcpy (tmp + basename_length, suffix, suffix_length);
4744 tmp[basename_length + suffix_length] = '\0';
4745 temp_filename = tmp;
4747 if (filename_cmp (temp_filename, gcc_input_filename) != 0)
4749 #ifndef HOST_LACKS_INODE_NUMBERS
4750 struct stat st_temp;
4752 /* Note, set_input() resets input_stat_set to 0. */
4753 if (input_stat_set == 0)
4755 input_stat_set = stat (gcc_input_filename,
4756 &input_stat);
4757 if (input_stat_set >= 0)
4758 input_stat_set = 1;
4761 /* If we have the stat for the gcc_input_filename
4762 and we can do the stat for the temp_filename
4763 then the they could still refer to the same
4764 file if st_dev/st_ino's are the same. */
4765 if (input_stat_set != 1
4766 || stat (temp_filename, &st_temp) < 0
4767 || input_stat.st_dev != st_temp.st_dev
4768 || input_stat.st_ino != st_temp.st_ino)
4769 #else
4770 /* Just compare canonical pathnames. */
4771 char* input_realname = lrealpath (gcc_input_filename);
4772 char* temp_realname = lrealpath (temp_filename);
4773 bool files_differ = filename_cmp (input_realname, temp_realname);
4774 free (input_realname);
4775 free (temp_realname);
4776 if (files_differ)
4777 #endif
4779 temp_filename = save_string (temp_filename,
4780 temp_filename_length + 1);
4781 obstack_grow (&obstack, temp_filename,
4782 temp_filename_length);
4783 arg_going = 1;
4784 delete_this_arg = 0;
4785 break;
4790 /* See if we already have an association of %g/%u/%U and
4791 suffix. */
4792 for (t = temp_names; t; t = t->next)
4793 if (t->length == suffix_length
4794 && strncmp (t->suffix, suffix, suffix_length) == 0
4795 && t->unique == (c == 'u' || c == 'U' || c == 'j'))
4796 break;
4798 /* Make a new association if needed. %u and %j
4799 require one. */
4800 if (t == 0 || c == 'u' || c == 'j')
4802 if (t == 0)
4804 t = XNEW (struct temp_name);
4805 t->next = temp_names;
4806 temp_names = t;
4808 t->length = suffix_length;
4809 if (saved_suffix)
4811 t->suffix = saved_suffix;
4812 saved_suffix = NULL;
4814 else
4815 t->suffix = save_string (suffix, suffix_length);
4816 t->unique = (c == 'u' || c == 'U' || c == 'j');
4817 temp_filename = make_temp_file (t->suffix);
4818 temp_filename_length = strlen (temp_filename);
4819 t->filename = temp_filename;
4820 t->filename_length = temp_filename_length;
4823 free (saved_suffix);
4825 obstack_grow (&obstack, t->filename, t->filename_length);
4826 delete_this_arg = 1;
4828 arg_going = 1;
4829 break;
4831 case 'i':
4832 if (combine_inputs)
4834 if (at_file_supplied)
4836 /* We are going to expand `%i' to `@FILE', where FILE
4837 is a newly-created temporary filename. The filenames
4838 that would usually be expanded in place of %o will be
4839 written to the temporary file. */
4840 char **argv;
4841 int n_files = 0;
4842 int j;
4844 for (i = 0; i < n_infiles; i++)
4845 if (compile_input_file_p (&infiles[i]))
4846 n_files++;
4848 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
4850 /* Copy the strings over. */
4851 for (i = 0, j = 0; i < n_infiles; i++)
4852 if (compile_input_file_p (&infiles[i]))
4854 argv[j] = CONST_CAST (char *, infiles[i].name);
4855 infiles[i].compiled = true;
4856 j++;
4858 argv[j] = NULL;
4860 create_at_file (argv);
4862 else
4863 for (i = 0; (int) i < n_infiles; i++)
4864 if (compile_input_file_p (&infiles[i]))
4866 store_arg (infiles[i].name, 0, 0);
4867 infiles[i].compiled = true;
4870 else
4872 obstack_grow (&obstack, gcc_input_filename,
4873 input_filename_length);
4874 arg_going = 1;
4876 break;
4878 case 'I':
4880 struct spec_path_info info;
4882 if (multilib_dir)
4884 do_spec_1 ("-imultilib", 1, NULL);
4885 /* Make this a separate argument. */
4886 do_spec_1 (" ", 0, NULL);
4887 do_spec_1 (multilib_dir, 1, NULL);
4888 do_spec_1 (" ", 0, NULL);
4891 if (gcc_exec_prefix)
4893 do_spec_1 ("-iprefix", 1, NULL);
4894 /* Make this a separate argument. */
4895 do_spec_1 (" ", 0, NULL);
4896 do_spec_1 (gcc_exec_prefix, 1, NULL);
4897 do_spec_1 (" ", 0, NULL);
4900 if (target_system_root_changed ||
4901 (target_system_root && target_sysroot_hdrs_suffix))
4903 do_spec_1 ("-isysroot", 1, NULL);
4904 /* Make this a separate argument. */
4905 do_spec_1 (" ", 0, NULL);
4906 do_spec_1 (target_system_root, 1, NULL);
4907 if (target_sysroot_hdrs_suffix)
4908 do_spec_1 (target_sysroot_hdrs_suffix, 1, NULL);
4909 do_spec_1 (" ", 0, NULL);
4912 info.option = "-isystem";
4913 info.append = "include";
4914 info.append_len = strlen (info.append);
4915 info.omit_relative = false;
4916 info.separate_options = true;
4918 for_each_path (&include_prefixes, false, info.append_len,
4919 spec_path, &info);
4921 info.append = "include-fixed";
4922 if (*sysroot_hdrs_suffix_spec)
4923 info.append = concat (info.append, dir_separator_str,
4924 multilib_dir, NULL);
4925 info.append_len = strlen (info.append);
4926 for_each_path (&include_prefixes, false, info.append_len,
4927 spec_path, &info);
4929 break;
4931 case 'o':
4933 int max = n_infiles;
4934 max += lang_specific_extra_outfiles;
4936 if (HAVE_GNU_LD && at_file_supplied)
4938 /* We are going to expand `%o' to `@FILE', where FILE
4939 is a newly-created temporary filename. The filenames
4940 that would usually be expanded in place of %o will be
4941 written to the temporary file. */
4943 char **argv;
4944 int n_files, j;
4946 /* Convert OUTFILES into a form suitable for writeargv. */
4948 /* Determine how many are non-NULL. */
4949 for (n_files = 0, i = 0; i < max; i++)
4950 n_files += outfiles[i] != NULL;
4952 argv = (char **) alloca (sizeof (char *) * (n_files + 1));
4954 /* Copy the strings over. */
4955 for (i = 0, j = 0; i < max; i++)
4956 if (outfiles[i])
4958 argv[j] = CONST_CAST (char *, outfiles[i]);
4959 j++;
4961 argv[j] = NULL;
4963 create_at_file (argv);
4965 else
4966 for (i = 0; i < max; i++)
4967 if (outfiles[i])
4968 store_arg (outfiles[i], 0, 0);
4969 break;
4972 case 'O':
4973 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
4974 arg_going = 1;
4975 break;
4977 case 's':
4978 this_is_library_file = 1;
4979 break;
4981 case 'T':
4982 this_is_linker_script = 1;
4983 break;
4985 case 'V':
4986 outfiles[input_file_number] = NULL;
4987 break;
4989 case 'w':
4990 this_is_output_file = 1;
4991 break;
4993 case 'W':
4995 unsigned int cur_index = VEC_length (const_char_p, argbuf);
4996 /* Handle the {...} following the %W. */
4997 if (*p != '{')
4998 fatal_error ("spec %qs has invalid %<%%W%c%>", spec, *p);
4999 p = handle_braces (p + 1);
5000 if (p == 0)
5001 return -1;
5002 end_going_arg ();
5003 /* If any args were output, mark the last one for deletion
5004 on failure. */
5005 if (VEC_length (const_char_p, argbuf) != cur_index)
5006 record_temp_file (VEC_last (const_char_p, argbuf), 0, 1);
5007 break;
5010 /* %x{OPTION} records OPTION for %X to output. */
5011 case 'x':
5013 const char *p1 = p;
5014 char *string;
5015 char *opt;
5016 unsigned ix;
5018 /* Skip past the option value and make a copy. */
5019 if (*p != '{')
5020 fatal_error ("spec %qs has invalid %<%%x%c%>", spec, *p);
5021 while (*p++ != '}')
5023 string = save_string (p1 + 1, p - p1 - 2);
5025 /* See if we already recorded this option. */
5026 FOR_EACH_VEC_ELT (char_p, linker_options, ix, opt)
5027 if (! strcmp (string, opt))
5029 free (string);
5030 return 0;
5033 /* This option is new; add it. */
5034 add_linker_option (string, strlen (string));
5036 break;
5038 /* Dump out the options accumulated previously using %x. */
5039 case 'X':
5040 do_specs_vec (linker_options);
5041 break;
5043 /* Dump out the options accumulated previously using -Wa,. */
5044 case 'Y':
5045 do_specs_vec (assembler_options);
5046 break;
5048 /* Dump out the options accumulated previously using -Wp,. */
5049 case 'Z':
5050 do_specs_vec (preprocessor_options);
5051 break;
5053 /* Here are digits and numbers that just process
5054 a certain constant string as a spec. */
5056 case '1':
5057 value = do_spec_1 (cc1_spec, 0, NULL);
5058 if (value != 0)
5059 return value;
5060 break;
5062 case '2':
5063 value = do_spec_1 (cc1plus_spec, 0, NULL);
5064 if (value != 0)
5065 return value;
5066 break;
5068 case 'a':
5069 value = do_spec_1 (asm_spec, 0, NULL);
5070 if (value != 0)
5071 return value;
5072 break;
5074 case 'A':
5075 value = do_spec_1 (asm_final_spec, 0, NULL);
5076 if (value != 0)
5077 return value;
5078 break;
5080 case 'C':
5082 const char *const spec
5083 = (input_file_compiler->cpp_spec
5084 ? input_file_compiler->cpp_spec
5085 : cpp_spec);
5086 value = do_spec_1 (spec, 0, NULL);
5087 if (value != 0)
5088 return value;
5090 break;
5092 case 'E':
5093 value = do_spec_1 (endfile_spec, 0, NULL);
5094 if (value != 0)
5095 return value;
5096 break;
5098 case 'l':
5099 value = do_spec_1 (link_spec, 0, NULL);
5100 if (value != 0)
5101 return value;
5102 break;
5104 case 'L':
5105 value = do_spec_1 (lib_spec, 0, NULL);
5106 if (value != 0)
5107 return value;
5108 break;
5110 case 'G':
5111 value = do_spec_1 (libgcc_spec, 0, NULL);
5112 if (value != 0)
5113 return value;
5114 break;
5116 case 'R':
5117 /* We assume there is a directory
5118 separator at the end of this string. */
5119 if (target_system_root)
5121 obstack_grow (&obstack, target_system_root,
5122 strlen (target_system_root));
5123 if (target_sysroot_suffix)
5124 obstack_grow (&obstack, target_sysroot_suffix,
5125 strlen (target_sysroot_suffix));
5127 break;
5129 case 'S':
5130 value = do_spec_1 (startfile_spec, 0, NULL);
5131 if (value != 0)
5132 return value;
5133 break;
5135 /* Here we define characters other than letters and digits. */
5137 case '{':
5138 p = handle_braces (p);
5139 if (p == 0)
5140 return -1;
5141 break;
5143 case ':':
5144 p = handle_spec_function (p);
5145 if (p == 0)
5146 return -1;
5147 break;
5149 case '%':
5150 obstack_1grow (&obstack, '%');
5151 break;
5153 case '.':
5155 unsigned len = 0;
5157 while (p[len] && p[len] != ' ' && p[len] != '%')
5158 len++;
5159 suffix_subst = save_string (p - 1, len + 1);
5160 p += len;
5162 break;
5164 /* Henceforth ignore the option(s) matching the pattern
5165 after the %<. */
5166 case '<':
5167 case '>':
5169 unsigned len = 0;
5170 int have_wildcard = 0;
5171 int i;
5172 int switch_option;
5174 if (c == '>')
5175 switch_option = SWITCH_IGNORE | SWITCH_KEEP_FOR_GCC;
5176 else
5177 switch_option = SWITCH_IGNORE;
5179 while (p[len] && p[len] != ' ' && p[len] != '\t')
5180 len++;
5182 if (p[len-1] == '*')
5183 have_wildcard = 1;
5185 for (i = 0; i < n_switches; i++)
5186 if (!strncmp (switches[i].part1, p, len - have_wildcard)
5187 && (have_wildcard || switches[i].part1[len] == '\0'))
5189 switches[i].live_cond |= switch_option;
5190 switches[i].validated = 1;
5193 p += len;
5195 break;
5197 case '*':
5198 if (soft_matched_part)
5200 if (soft_matched_part[0])
5201 do_spec_1 (soft_matched_part, 1, NULL);
5202 do_spec_1 (" ", 0, NULL);
5204 else
5205 /* Catch the case where a spec string contains something like
5206 '%{foo:%*}'. i.e. there is no * in the pattern on the left
5207 hand side of the :. */
5208 error ("spec failure: %<%%*%> has not been initialized by pattern match");
5209 break;
5211 /* Process a string found as the value of a spec given by name.
5212 This feature allows individual machine descriptions
5213 to add and use their own specs.
5214 %[...] modifies -D options the way %P does;
5215 %(...) uses the spec unmodified. */
5216 case '[':
5217 warning (0, "use of obsolete %%[ operator in specs");
5218 case '(':
5220 const char *name = p;
5221 struct spec_list *sl;
5222 int len;
5224 /* The string after the S/P is the name of a spec that is to be
5225 processed. */
5226 while (*p && *p != ')' && *p != ']')
5227 p++;
5229 /* See if it's in the list. */
5230 for (len = p - name, sl = specs; sl; sl = sl->next)
5231 if (sl->name_len == len && !strncmp (sl->name, name, len))
5233 name = *(sl->ptr_spec);
5234 #ifdef DEBUG_SPECS
5235 fnotice (stderr, "Processing spec %c%s%c, which is '%s'\n",
5236 c, sl->name, (c == '(') ? ')' : ']', name);
5237 #endif
5238 break;
5241 if (sl)
5243 if (c == '(')
5245 value = do_spec_1 (name, 0, NULL);
5246 if (value != 0)
5247 return value;
5249 else
5251 char *x = (char *) alloca (strlen (name) * 2 + 1);
5252 char *buf = x;
5253 const char *y = name;
5254 int flag = 0;
5256 /* Copy all of NAME into BUF, but put __ after
5257 every -D and at the end of each arg. */
5258 while (1)
5260 if (! strncmp (y, "-D", 2))
5262 *x++ = '-';
5263 *x++ = 'D';
5264 *x++ = '_';
5265 *x++ = '_';
5266 y += 2;
5267 flag = 1;
5268 continue;
5270 else if (flag
5271 && (*y == ' ' || *y == '\t' || *y == '='
5272 || *y == '}' || *y == 0))
5274 *x++ = '_';
5275 *x++ = '_';
5276 flag = 0;
5278 if (*y == 0)
5279 break;
5280 else
5281 *x++ = *y++;
5283 *x = 0;
5285 value = do_spec_1 (buf, 0, NULL);
5286 if (value != 0)
5287 return value;
5291 /* Discard the closing paren or bracket. */
5292 if (*p)
5293 p++;
5295 break;
5297 default:
5298 error ("spec failure: unrecognized spec option %qc", c);
5299 break;
5301 break;
5303 case '\\':
5304 /* Backslash: treat next character as ordinary. */
5305 c = *p++;
5307 /* Fall through. */
5308 default:
5309 /* Ordinary character: put it into the current argument. */
5310 obstack_1grow (&obstack, c);
5311 arg_going = 1;
5314 /* End of string. If we are processing a spec function, we need to
5315 end any pending argument. */
5316 if (processing_spec_function)
5317 end_going_arg ();
5319 return 0;
5322 /* Look up a spec function. */
5324 static const struct spec_function *
5325 lookup_spec_function (const char *name)
5327 const struct spec_function *sf;
5329 for (sf = static_spec_functions; sf->name != NULL; sf++)
5330 if (strcmp (sf->name, name) == 0)
5331 return sf;
5333 return NULL;
5336 /* Evaluate a spec function. */
5338 static const char *
5339 eval_spec_function (const char *func, const char *args)
5341 const struct spec_function *sf;
5342 const char *funcval;
5344 /* Saved spec processing context. */
5345 VEC(const_char_p,heap) *save_argbuf;
5347 int save_arg_going;
5348 int save_delete_this_arg;
5349 int save_this_is_output_file;
5350 int save_this_is_library_file;
5351 int save_input_from_pipe;
5352 int save_this_is_linker_script;
5353 const char *save_suffix_subst;
5356 sf = lookup_spec_function (func);
5357 if (sf == NULL)
5358 fatal_error ("unknown spec function %qs", func);
5360 /* Push the spec processing context. */
5361 save_argbuf = argbuf;
5363 save_arg_going = arg_going;
5364 save_delete_this_arg = delete_this_arg;
5365 save_this_is_output_file = this_is_output_file;
5366 save_this_is_library_file = this_is_library_file;
5367 save_this_is_linker_script = this_is_linker_script;
5368 save_input_from_pipe = input_from_pipe;
5369 save_suffix_subst = suffix_subst;
5371 /* Create a new spec processing context, and build the function
5372 arguments. */
5374 alloc_args ();
5375 if (do_spec_2 (args) < 0)
5376 fatal_error ("error in args to spec function %qs", func);
5378 /* argbuf_index is an index for the next argument to be inserted, and
5379 so contains the count of the args already inserted. */
5381 funcval = (*sf->func) (VEC_length (const_char_p, argbuf),
5382 VEC_address (const_char_p, argbuf));
5384 /* Pop the spec processing context. */
5385 VEC_free (const_char_p, heap, argbuf);
5386 argbuf = save_argbuf;
5388 arg_going = save_arg_going;
5389 delete_this_arg = save_delete_this_arg;
5390 this_is_output_file = save_this_is_output_file;
5391 this_is_library_file = save_this_is_library_file;
5392 this_is_linker_script = save_this_is_linker_script;
5393 input_from_pipe = save_input_from_pipe;
5394 suffix_subst = save_suffix_subst;
5396 return funcval;
5399 /* Handle a spec function call of the form:
5401 %:function(args)
5403 ARGS is processed as a spec in a separate context and split into an
5404 argument vector in the normal fashion. The function returns a string
5405 containing a spec which we then process in the caller's context, or
5406 NULL if no processing is required. */
5408 static const char *
5409 handle_spec_function (const char *p)
5411 char *func, *args;
5412 const char *endp, *funcval;
5413 int count;
5415 processing_spec_function++;
5417 /* Get the function name. */
5418 for (endp = p; *endp != '\0'; endp++)
5420 if (*endp == '(') /* ) */
5421 break;
5422 /* Only allow [A-Za-z0-9], -, and _ in function names. */
5423 if (!ISALNUM (*endp) && !(*endp == '-' || *endp == '_'))
5424 fatal_error ("malformed spec function name");
5426 if (*endp != '(') /* ) */
5427 fatal_error ("no arguments for spec function");
5428 func = save_string (p, endp - p);
5429 p = ++endp;
5431 /* Get the arguments. */
5432 for (count = 0; *endp != '\0'; endp++)
5434 /* ( */
5435 if (*endp == ')')
5437 if (count == 0)
5438 break;
5439 count--;
5441 else if (*endp == '(') /* ) */
5442 count++;
5444 /* ( */
5445 if (*endp != ')')
5446 fatal_error ("malformed spec function arguments");
5447 args = save_string (p, endp - p);
5448 p = ++endp;
5450 /* p now points to just past the end of the spec function expression. */
5452 funcval = eval_spec_function (func, args);
5453 if (funcval != NULL && do_spec_1 (funcval, 0, NULL) < 0)
5454 p = NULL;
5456 free (func);
5457 free (args);
5459 processing_spec_function--;
5461 return p;
5464 /* Inline subroutine of handle_braces. Returns true if the current
5465 input suffix matches the atom bracketed by ATOM and END_ATOM. */
5466 static inline bool
5467 input_suffix_matches (const char *atom, const char *end_atom)
5469 return (input_suffix
5470 && !strncmp (input_suffix, atom, end_atom - atom)
5471 && input_suffix[end_atom - atom] == '\0');
5474 /* Subroutine of handle_braces. Returns true if the current
5475 input file's spec name matches the atom bracketed by ATOM and END_ATOM. */
5476 static bool
5477 input_spec_matches (const char *atom, const char *end_atom)
5479 return (input_file_compiler
5480 && input_file_compiler->suffix
5481 && input_file_compiler->suffix[0] != '\0'
5482 && !strncmp (input_file_compiler->suffix + 1, atom,
5483 end_atom - atom)
5484 && input_file_compiler->suffix[end_atom - atom + 1] == '\0');
5487 /* Subroutine of handle_braces. Returns true if a switch
5488 matching the atom bracketed by ATOM and END_ATOM appeared on the
5489 command line. */
5490 static bool
5491 switch_matches (const char *atom, const char *end_atom, int starred)
5493 int i;
5494 int len = end_atom - atom;
5495 int plen = starred ? len : -1;
5497 for (i = 0; i < n_switches; i++)
5498 if (!strncmp (switches[i].part1, atom, len)
5499 && (starred || switches[i].part1[len] == '\0')
5500 && check_live_switch (i, plen))
5501 return true;
5503 return false;
5506 /* Inline subroutine of handle_braces. Mark all of the switches which
5507 match ATOM (extends to END_ATOM; STARRED indicates whether there
5508 was a star after the atom) for later processing. */
5509 static inline void
5510 mark_matching_switches (const char *atom, const char *end_atom, int starred)
5512 int i;
5513 int len = end_atom - atom;
5514 int plen = starred ? len : -1;
5516 for (i = 0; i < n_switches; i++)
5517 if (!strncmp (switches[i].part1, atom, len)
5518 && (starred || switches[i].part1[len] == '\0')
5519 && check_live_switch (i, plen))
5520 switches[i].ordering = 1;
5523 /* Inline subroutine of handle_braces. Process all the currently
5524 marked switches through give_switch, and clear the marks. */
5525 static inline void
5526 process_marked_switches (void)
5528 int i;
5530 for (i = 0; i < n_switches; i++)
5531 if (switches[i].ordering == 1)
5533 switches[i].ordering = 0;
5534 give_switch (i, 0);
5538 /* Handle a %{ ... } construct. P points just inside the leading {.
5539 Returns a pointer one past the end of the brace block, or 0
5540 if we call do_spec_1 and that returns -1. */
5542 static const char *
5543 handle_braces (const char *p)
5545 const char *atom, *end_atom;
5546 const char *d_atom = NULL, *d_end_atom = NULL;
5547 const char *orig = p;
5549 bool a_is_suffix;
5550 bool a_is_spectype;
5551 bool a_is_starred;
5552 bool a_is_negated;
5553 bool a_matched;
5555 bool a_must_be_last = false;
5556 bool ordered_set = false;
5557 bool disjunct_set = false;
5558 bool disj_matched = false;
5559 bool disj_starred = true;
5560 bool n_way_choice = false;
5561 bool n_way_matched = false;
5563 #define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
5567 if (a_must_be_last)
5568 goto invalid;
5570 /* Scan one "atom" (S in the description above of %{}, possibly
5571 with '!', '.', '@', ',', or '*' modifiers). */
5572 a_matched = false;
5573 a_is_suffix = false;
5574 a_is_starred = false;
5575 a_is_negated = false;
5576 a_is_spectype = false;
5578 SKIP_WHITE();
5579 if (*p == '!')
5580 p++, a_is_negated = true;
5582 SKIP_WHITE();
5583 if (*p == '.')
5584 p++, a_is_suffix = true;
5585 else if (*p == ',')
5586 p++, a_is_spectype = true;
5588 atom = p;
5589 while (ISIDNUM(*p) || *p == '-' || *p == '+' || *p == '='
5590 || *p == ',' || *p == '.' || *p == '@')
5591 p++;
5592 end_atom = p;
5594 if (*p == '*')
5595 p++, a_is_starred = 1;
5597 SKIP_WHITE();
5598 switch (*p)
5600 case '&': case '}':
5601 /* Substitute the switch(es) indicated by the current atom. */
5602 ordered_set = true;
5603 if (disjunct_set || n_way_choice || a_is_negated || a_is_suffix
5604 || a_is_spectype || atom == end_atom)
5605 goto invalid;
5607 mark_matching_switches (atom, end_atom, a_is_starred);
5609 if (*p == '}')
5610 process_marked_switches ();
5611 break;
5613 case '|': case ':':
5614 /* Substitute some text if the current atom appears as a switch
5615 or suffix. */
5616 disjunct_set = true;
5617 if (ordered_set)
5618 goto invalid;
5620 if (atom == end_atom)
5622 if (!n_way_choice || disj_matched || *p == '|'
5623 || a_is_negated || a_is_suffix || a_is_spectype
5624 || a_is_starred)
5625 goto invalid;
5627 /* An empty term may appear as the last choice of an
5628 N-way choice set; it means "otherwise". */
5629 a_must_be_last = true;
5630 disj_matched = !n_way_matched;
5631 disj_starred = false;
5633 else
5635 if ((a_is_suffix || a_is_spectype) && a_is_starred)
5636 goto invalid;
5638 if (!a_is_starred)
5639 disj_starred = false;
5641 /* Don't bother testing this atom if we already have a
5642 match. */
5643 if (!disj_matched && !n_way_matched)
5645 if (a_is_suffix)
5646 a_matched = input_suffix_matches (atom, end_atom);
5647 else if (a_is_spectype)
5648 a_matched = input_spec_matches (atom, end_atom);
5649 else
5650 a_matched = switch_matches (atom, end_atom, a_is_starred);
5652 if (a_matched != a_is_negated)
5654 disj_matched = true;
5655 d_atom = atom;
5656 d_end_atom = end_atom;
5661 if (*p == ':')
5663 /* Found the body, that is, the text to substitute if the
5664 current disjunction matches. */
5665 p = process_brace_body (p + 1, d_atom, d_end_atom, disj_starred,
5666 disj_matched && !n_way_matched);
5667 if (p == 0)
5668 return 0;
5670 /* If we have an N-way choice, reset state for the next
5671 disjunction. */
5672 if (*p == ';')
5674 n_way_choice = true;
5675 n_way_matched |= disj_matched;
5676 disj_matched = false;
5677 disj_starred = true;
5678 d_atom = d_end_atom = NULL;
5681 break;
5683 default:
5684 goto invalid;
5687 while (*p++ != '}');
5689 return p;
5691 invalid:
5692 fatal_error ("braced spec %qs is invalid at %qc", orig, *p);
5694 #undef SKIP_WHITE
5697 /* Subroutine of handle_braces. Scan and process a brace substitution body
5698 (X in the description of %{} syntax). P points one past the colon;
5699 ATOM and END_ATOM bracket the first atom which was found to be true
5700 (present) in the current disjunction; STARRED indicates whether all
5701 the atoms in the current disjunction were starred (for syntax validation);
5702 MATCHED indicates whether the disjunction matched or not, and therefore
5703 whether or not the body is to be processed through do_spec_1 or just
5704 skipped. Returns a pointer to the closing } or ;, or 0 if do_spec_1
5705 returns -1. */
5707 static const char *
5708 process_brace_body (const char *p, const char *atom, const char *end_atom,
5709 int starred, int matched)
5711 const char *body, *end_body;
5712 unsigned int nesting_level;
5713 bool have_subst = false;
5715 /* Locate the closing } or ;, honoring nested braces.
5716 Trim trailing whitespace. */
5717 body = p;
5718 nesting_level = 1;
5719 for (;;)
5721 if (*p == '{')
5722 nesting_level++;
5723 else if (*p == '}')
5725 if (!--nesting_level)
5726 break;
5728 else if (*p == ';' && nesting_level == 1)
5729 break;
5730 else if (*p == '%' && p[1] == '*' && nesting_level == 1)
5731 have_subst = true;
5732 else if (*p == '\0')
5733 goto invalid;
5734 p++;
5737 end_body = p;
5738 while (end_body[-1] == ' ' || end_body[-1] == '\t')
5739 end_body--;
5741 if (have_subst && !starred)
5742 goto invalid;
5744 if (matched)
5746 /* Copy the substitution body to permanent storage and execute it.
5747 If have_subst is false, this is a simple matter of running the
5748 body through do_spec_1... */
5749 char *string = save_string (body, end_body - body);
5750 if (!have_subst)
5752 if (do_spec_1 (string, 0, NULL) < 0)
5753 return 0;
5755 else
5757 /* ... but if have_subst is true, we have to process the
5758 body once for each matching switch, with %* set to the
5759 variant part of the switch. */
5760 unsigned int hard_match_len = end_atom - atom;
5761 int i;
5763 for (i = 0; i < n_switches; i++)
5764 if (!strncmp (switches[i].part1, atom, hard_match_len)
5765 && check_live_switch (i, hard_match_len))
5767 if (do_spec_1 (string, 0,
5768 &switches[i].part1[hard_match_len]) < 0)
5769 return 0;
5770 /* Pass any arguments this switch has. */
5771 give_switch (i, 1);
5772 suffix_subst = NULL;
5777 return p;
5779 invalid:
5780 fatal_error ("braced spec body %qs is invalid", body);
5783 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5784 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5785 spec, or -1 if either exact match or %* is used.
5787 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5788 whose value does not begin with "no-" is obsoleted by the same value
5789 with the "no-", similarly for a switch with the "no-" prefix. */
5791 static int
5792 check_live_switch (int switchnum, int prefix_length)
5794 const char *name = switches[switchnum].part1;
5795 int i;
5797 /* If we already processed this switch and determined if it was
5798 live or not, return our past determination. */
5799 if (switches[switchnum].live_cond != 0)
5800 return ((switches[switchnum].live_cond & SWITCH_LIVE) != 0
5801 && (switches[switchnum].live_cond & SWITCH_FALSE) == 0
5802 && (switches[switchnum].live_cond & SWITCH_IGNORE_PERMANENTLY)
5803 == 0);
5805 /* In the common case of {<at-most-one-letter>*}, a negating
5806 switch would always match, so ignore that case. We will just
5807 send the conflicting switches to the compiler phase. */
5808 if (prefix_length >= 0 && prefix_length <= 1)
5809 return 1;
5811 /* Now search for duplicate in a manner that depends on the name. */
5812 switch (*name)
5814 case 'O':
5815 for (i = switchnum + 1; i < n_switches; i++)
5816 if (switches[i].part1[0] == 'O')
5818 switches[switchnum].validated = 1;
5819 switches[switchnum].live_cond = SWITCH_FALSE;
5820 return 0;
5822 break;
5824 case 'W': case 'f': case 'm':
5825 if (! strncmp (name + 1, "no-", 3))
5827 /* We have Xno-YYY, search for XYYY. */
5828 for (i = switchnum + 1; i < n_switches; i++)
5829 if (switches[i].part1[0] == name[0]
5830 && ! strcmp (&switches[i].part1[1], &name[4]))
5832 switches[switchnum].validated = 1;
5833 switches[switchnum].live_cond = SWITCH_FALSE;
5834 return 0;
5837 else
5839 /* We have XYYY, search for Xno-YYY. */
5840 for (i = switchnum + 1; i < n_switches; i++)
5841 if (switches[i].part1[0] == name[0]
5842 && switches[i].part1[1] == 'n'
5843 && switches[i].part1[2] == 'o'
5844 && switches[i].part1[3] == '-'
5845 && !strcmp (&switches[i].part1[4], &name[1]))
5847 switches[switchnum].validated = 1;
5848 switches[switchnum].live_cond = SWITCH_FALSE;
5849 return 0;
5852 break;
5855 /* Otherwise the switch is live. */
5856 switches[switchnum].live_cond |= SWITCH_LIVE;
5857 return 1;
5860 /* Pass a switch to the current accumulating command
5861 in the same form that we received it.
5862 SWITCHNUM identifies the switch; it is an index into
5863 the vector of switches gcc received, which is `switches'.
5864 This cannot fail since it never finishes a command line.
5866 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
5868 static void
5869 give_switch (int switchnum, int omit_first_word)
5871 if ((switches[switchnum].live_cond & SWITCH_IGNORE) != 0)
5872 return;
5874 if (!omit_first_word)
5876 do_spec_1 ("-", 0, NULL);
5877 do_spec_1 (switches[switchnum].part1, 1, NULL);
5880 if (switches[switchnum].args != 0)
5882 const char **p;
5883 for (p = switches[switchnum].args; *p; p++)
5885 const char *arg = *p;
5887 do_spec_1 (" ", 0, NULL);
5888 if (suffix_subst)
5890 unsigned length = strlen (arg);
5891 int dot = 0;
5893 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5894 if (arg[length] == '.')
5896 (CONST_CAST(char *, arg))[length] = 0;
5897 dot = 1;
5898 break;
5900 do_spec_1 (arg, 1, NULL);
5901 if (dot)
5902 (CONST_CAST(char *, arg))[length] = '.';
5903 do_spec_1 (suffix_subst, 1, NULL);
5905 else
5906 do_spec_1 (arg, 1, NULL);
5910 do_spec_1 (" ", 0, NULL);
5911 switches[switchnum].validated = 1;
5914 /* Search for a file named NAME trying various prefixes including the
5915 user's -B prefix and some standard ones.
5916 Return the absolute file name found. If nothing is found, return NAME. */
5918 static const char *
5919 find_file (const char *name)
5921 char *newname = find_a_file (&startfile_prefixes, name, R_OK, true);
5922 return newname ? newname : name;
5925 /* Determine whether a directory exists. If LINKER, return 0 for
5926 certain fixed names not needed by the linker. */
5928 static int
5929 is_directory (const char *path1, bool linker)
5931 int len1;
5932 char *path;
5933 char *cp;
5934 struct stat st;
5936 /* Ensure the string ends with "/.". The resulting path will be a
5937 directory even if the given path is a symbolic link. */
5938 len1 = strlen (path1);
5939 path = (char *) alloca (3 + len1);
5940 memcpy (path, path1, len1);
5941 cp = path + len1;
5942 if (!IS_DIR_SEPARATOR (cp[-1]))
5943 *cp++ = DIR_SEPARATOR;
5944 *cp++ = '.';
5945 *cp = '\0';
5947 /* Exclude directories that the linker is known to search. */
5948 if (linker
5949 && IS_DIR_SEPARATOR (path[0])
5950 && ((cp - path == 6
5951 && filename_ncmp (path + 1, "lib", 3) == 0)
5952 || (cp - path == 10
5953 && filename_ncmp (path + 1, "usr", 3) == 0
5954 && IS_DIR_SEPARATOR (path[4])
5955 && filename_ncmp (path + 5, "lib", 3) == 0)))
5956 return 0;
5958 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5961 /* Set up the various global variables to indicate that we're processing
5962 the input file named FILENAME. */
5964 void
5965 set_input (const char *filename)
5967 const char *p;
5969 gcc_input_filename = filename;
5970 input_filename_length = strlen (gcc_input_filename);
5971 input_basename = lbasename (gcc_input_filename);
5973 /* Find a suffix starting with the last period,
5974 and set basename_length to exclude that suffix. */
5975 basename_length = strlen (input_basename);
5976 suffixed_basename_length = basename_length;
5977 p = input_basename + basename_length;
5978 while (p != input_basename && *p != '.')
5979 --p;
5980 if (*p == '.' && p != input_basename)
5982 basename_length = p - input_basename;
5983 input_suffix = p + 1;
5985 else
5986 input_suffix = "";
5988 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
5989 we will need to do a stat on the gcc_input_filename. The
5990 INPUT_STAT_SET signals that the stat is needed. */
5991 input_stat_set = 0;
5994 /* On fatal signals, delete all the temporary files. */
5996 static void
5997 fatal_signal (int signum)
5999 signal (signum, SIG_DFL);
6000 delete_failure_queue ();
6001 delete_temp_files ();
6002 /* Get the same signal again, this time not handled,
6003 so its normal effect occurs. */
6004 kill (getpid (), signum);
6007 /* Compare the contents of the two files named CMPFILE[0] and
6008 CMPFILE[1]. Return zero if they're identical, nonzero
6009 otherwise. */
6011 static int
6012 compare_files (char *cmpfile[])
6014 int ret = 0;
6015 FILE *temp[2] = { NULL, NULL };
6016 int i;
6018 #if HAVE_MMAP_FILE
6020 size_t length[2];
6021 void *map[2] = { NULL, NULL };
6023 for (i = 0; i < 2; i++)
6025 struct stat st;
6027 if (stat (cmpfile[i], &st) < 0 || !S_ISREG (st.st_mode))
6029 error ("%s: could not determine length of compare-debug file %s",
6030 gcc_input_filename, cmpfile[i]);
6031 ret = 1;
6032 break;
6035 length[i] = st.st_size;
6038 if (!ret && length[0] != length[1])
6040 error ("%s: -fcompare-debug failure (length)", gcc_input_filename);
6041 ret = 1;
6044 if (!ret)
6045 for (i = 0; i < 2; i++)
6047 int fd = open (cmpfile[i], O_RDONLY);
6048 if (fd < 0)
6050 error ("%s: could not open compare-debug file %s",
6051 gcc_input_filename, cmpfile[i]);
6052 ret = 1;
6053 break;
6056 map[i] = mmap (NULL, length[i], PROT_READ, MAP_PRIVATE, fd, 0);
6057 close (fd);
6059 if (map[i] == (void *) MAP_FAILED)
6061 ret = -1;
6062 break;
6066 if (!ret)
6068 if (memcmp (map[0], map[1], length[0]) != 0)
6070 error ("%s: -fcompare-debug failure", gcc_input_filename);
6071 ret = 1;
6075 for (i = 0; i < 2; i++)
6076 if (map[i])
6077 munmap ((caddr_t) map[i], length[i]);
6079 if (ret >= 0)
6080 return ret;
6082 ret = 0;
6084 #endif
6086 for (i = 0; i < 2; i++)
6088 temp[i] = fopen (cmpfile[i], "r");
6089 if (!temp[i])
6091 error ("%s: could not open compare-debug file %s",
6092 gcc_input_filename, cmpfile[i]);
6093 ret = 1;
6094 break;
6098 if (!ret && temp[0] && temp[1])
6099 for (;;)
6101 int c0, c1;
6102 c0 = fgetc (temp[0]);
6103 c1 = fgetc (temp[1]);
6105 if (c0 != c1)
6107 error ("%s: -fcompare-debug failure",
6108 gcc_input_filename);
6109 ret = 1;
6110 break;
6113 if (c0 == EOF)
6114 break;
6117 for (i = 1; i >= 0; i--)
6119 if (temp[i])
6120 fclose (temp[i]);
6123 return ret;
6126 extern int main (int, char **);
6129 main (int argc, char **argv)
6131 size_t i;
6132 int value;
6133 int linker_was_run = 0;
6134 int lang_n_infiles = 0;
6135 int num_linker_inputs = 0;
6136 char *explicit_link_files;
6137 char *specs_file;
6138 char *lto_wrapper_file;
6139 const char *p;
6140 struct user_specs *uptr;
6141 char **old_argv = argv;
6142 struct cl_decoded_option *decoded_options;
6143 unsigned int decoded_options_count;
6145 /* Initialize here, not in definition. The IRIX 6 O32 cc sometimes chokes
6146 on ?: in file-scope variable initializations. */
6147 asm_debug = ASM_DEBUG_SPEC;
6149 p = argv[0] + strlen (argv[0]);
6150 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
6151 --p;
6152 progname = p;
6154 xmalloc_set_program_name (progname);
6156 expandargv (&argc, &argv);
6158 /* Determine if any expansions were made. */
6159 if (argv != old_argv)
6160 at_file_supplied = true;
6162 global_options = global_options_init;
6164 decode_cmdline_options_to_array (argc, CONST_CAST2 (const char **, char **,
6165 argv),
6166 CL_DRIVER,
6167 &decoded_options, &decoded_options_count);
6169 #ifdef GCC_DRIVER_HOST_INITIALIZATION
6170 /* Perform host dependent initialization when needed. */
6171 GCC_DRIVER_HOST_INITIALIZATION;
6172 #endif
6174 /* Unlock the stdio streams. */
6175 unlock_std_streams ();
6177 gcc_init_libintl ();
6179 diagnostic_initialize (global_dc, 0);
6180 if (atexit (delete_temp_files) != 0)
6181 fatal_error ("atexit failed");
6183 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
6184 signal (SIGINT, fatal_signal);
6185 #ifdef SIGHUP
6186 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
6187 signal (SIGHUP, fatal_signal);
6188 #endif
6189 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
6190 signal (SIGTERM, fatal_signal);
6191 #ifdef SIGPIPE
6192 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
6193 signal (SIGPIPE, fatal_signal);
6194 #endif
6195 #ifdef SIGCHLD
6196 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
6197 receive the signal. A different setting is inheritable */
6198 signal (SIGCHLD, SIG_DFL);
6199 #endif
6201 /* Allocate the argument vector. */
6202 alloc_args ();
6204 obstack_init (&obstack);
6206 /* Build multilib_select, et. al from the separate lines that make up each
6207 multilib selection. */
6209 const char *const *q = multilib_raw;
6210 int need_space;
6212 obstack_init (&multilib_obstack);
6213 while ((p = *q++) != (char *) 0)
6214 obstack_grow (&multilib_obstack, p, strlen (p));
6216 obstack_1grow (&multilib_obstack, 0);
6217 multilib_select = XOBFINISH (&multilib_obstack, const char *);
6219 q = multilib_matches_raw;
6220 while ((p = *q++) != (char *) 0)
6221 obstack_grow (&multilib_obstack, p, strlen (p));
6223 obstack_1grow (&multilib_obstack, 0);
6224 multilib_matches = XOBFINISH (&multilib_obstack, const char *);
6226 q = multilib_exclusions_raw;
6227 while ((p = *q++) != (char *) 0)
6228 obstack_grow (&multilib_obstack, p, strlen (p));
6230 obstack_1grow (&multilib_obstack, 0);
6231 multilib_exclusions = XOBFINISH (&multilib_obstack, const char *);
6233 need_space = FALSE;
6234 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
6236 if (need_space)
6237 obstack_1grow (&multilib_obstack, ' ');
6238 obstack_grow (&multilib_obstack,
6239 multilib_defaults_raw[i],
6240 strlen (multilib_defaults_raw[i]));
6241 need_space = TRUE;
6244 obstack_1grow (&multilib_obstack, 0);
6245 multilib_defaults = XOBFINISH (&multilib_obstack, const char *);
6248 #ifdef INIT_ENVIRONMENT
6249 /* Set up any other necessary machine specific environment variables. */
6250 xputenv (INIT_ENVIRONMENT);
6251 #endif
6253 /* Make a table of what switches there are (switches, n_switches).
6254 Make a table of specified input files (infiles, n_infiles).
6255 Decode switches that are handled locally. */
6257 process_command (decoded_options_count, decoded_options);
6259 /* Initialize the vector of specs to just the default.
6260 This means one element containing 0s, as a terminator. */
6262 compilers = XNEWVAR (struct compiler, sizeof default_compilers);
6263 memcpy (compilers, default_compilers, sizeof default_compilers);
6264 n_compilers = n_default_compilers;
6266 /* Read specs from a file if there is one. */
6268 machine_suffix = concat (spec_machine, dir_separator_str,
6269 spec_version, dir_separator_str, NULL);
6270 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
6272 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK, true);
6273 /* Read the specs file unless it is a default one. */
6274 if (specs_file != 0 && strcmp (specs_file, "specs"))
6275 read_specs (specs_file, TRUE);
6276 else
6277 init_spec ();
6279 /* We need to check standard_exec_prefix/just_machine_suffix/specs
6280 for any override of as, ld and libraries. */
6281 specs_file = (char *) alloca (strlen (standard_exec_prefix)
6282 + strlen (just_machine_suffix) + sizeof ("specs"));
6284 strcpy (specs_file, standard_exec_prefix);
6285 strcat (specs_file, just_machine_suffix);
6286 strcat (specs_file, "specs");
6287 if (access (specs_file, R_OK) == 0)
6288 read_specs (specs_file, TRUE);
6290 /* Process any configure-time defaults specified for the command line
6291 options, via OPTION_DEFAULT_SPECS. */
6292 for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)
6293 do_option_spec (option_default_specs[i].name,
6294 option_default_specs[i].spec);
6296 /* Process DRIVER_SELF_SPECS, adding any new options to the end
6297 of the command line. */
6299 for (i = 0; i < ARRAY_SIZE (driver_self_specs); i++)
6300 do_self_spec (driver_self_specs[i]);
6302 if (compare_debug)
6304 enum save_temps save;
6306 if (!compare_debug_second)
6308 n_switches_debug_check[1] = n_switches;
6309 n_switches_alloc_debug_check[1] = n_switches_alloc;
6310 switches_debug_check[1] = XDUPVEC (struct switchstr, switches,
6311 n_switches_alloc);
6313 do_self_spec ("%:compare-debug-self-opt()");
6314 n_switches_debug_check[0] = n_switches;
6315 n_switches_alloc_debug_check[0] = n_switches_alloc;
6316 switches_debug_check[0] = switches;
6318 n_switches = n_switches_debug_check[1];
6319 n_switches_alloc = n_switches_alloc_debug_check[1];
6320 switches = switches_debug_check[1];
6323 /* Avoid crash when computing %j in this early. */
6324 save = save_temps_flag;
6325 save_temps_flag = SAVE_TEMPS_NONE;
6327 compare_debug = -compare_debug;
6328 do_self_spec ("%:compare-debug-self-opt()");
6330 save_temps_flag = save;
6332 if (!compare_debug_second)
6334 n_switches_debug_check[1] = n_switches;
6335 n_switches_alloc_debug_check[1] = n_switches_alloc;
6336 switches_debug_check[1] = switches;
6337 compare_debug = -compare_debug;
6338 n_switches = n_switches_debug_check[0];
6339 n_switches_alloc = n_switches_debug_check[0];
6340 switches = switches_debug_check[0];
6344 /* If not cross-compiling, look for executables in the standard
6345 places. */
6346 if (*cross_compile == '0')
6348 if (*md_exec_prefix)
6350 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
6351 PREFIX_PRIORITY_LAST, 0, 0);
6355 /* Process sysroot_suffix_spec. */
6356 if (*sysroot_suffix_spec != 0
6357 && do_spec_2 (sysroot_suffix_spec) == 0)
6359 if (VEC_length (const_char_p, argbuf) > 1)
6360 error ("spec failure: more than one arg to SYSROOT_SUFFIX_SPEC");
6361 else if (VEC_length (const_char_p, argbuf) == 1)
6362 target_sysroot_suffix = xstrdup (VEC_last (const_char_p, argbuf));
6365 #ifdef HAVE_LD_SYSROOT
6366 /* Pass the --sysroot option to the linker, if it supports that. If
6367 there is a sysroot_suffix_spec, it has already been processed by
6368 this point, so target_system_root really is the system root we
6369 should be using. */
6370 if (target_system_root)
6372 obstack_grow (&obstack, "%(sysroot_spec) ", strlen ("%(sysroot_spec) "));
6373 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
6374 set_spec ("link", XOBFINISH (&obstack, const char *));
6376 #endif
6378 /* Process sysroot_hdrs_suffix_spec. */
6379 if (*sysroot_hdrs_suffix_spec != 0
6380 && do_spec_2 (sysroot_hdrs_suffix_spec) == 0)
6382 if (VEC_length (const_char_p, argbuf) > 1)
6383 error ("spec failure: more than one arg to SYSROOT_HEADERS_SUFFIX_SPEC");
6384 else if (VEC_length (const_char_p, argbuf) == 1)
6385 target_sysroot_hdrs_suffix = xstrdup (VEC_last (const_char_p, argbuf));
6388 /* Look for startfiles in the standard places. */
6389 if (*startfile_prefix_spec != 0
6390 && do_spec_2 (startfile_prefix_spec) == 0
6391 && do_spec_1 (" ", 0, NULL) == 0)
6393 const char *arg;
6394 int ndx;
6395 FOR_EACH_VEC_ELT (const_char_p, argbuf, ndx, arg)
6396 add_sysrooted_prefix (&startfile_prefixes, arg, "BINUTILS",
6397 PREFIX_PRIORITY_LAST, 0, 1);
6399 /* We should eventually get rid of all these and stick to
6400 startfile_prefix_spec exclusively. */
6401 else if (*cross_compile == '0' || target_system_root)
6403 if (*md_startfile_prefix)
6404 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix,
6405 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
6407 if (*md_startfile_prefix_1)
6408 add_sysrooted_prefix (&startfile_prefixes, md_startfile_prefix_1,
6409 "GCC", PREFIX_PRIORITY_LAST, 0, 1);
6411 /* If standard_startfile_prefix is relative, base it on
6412 standard_exec_prefix. This lets us move the installed tree
6413 as a unit. If GCC_EXEC_PREFIX is defined, base
6414 standard_startfile_prefix on that as well.
6416 If the prefix is relative, only search it for native compilers;
6417 otherwise we will search a directory containing host libraries. */
6418 if (IS_ABSOLUTE_PATH (standard_startfile_prefix))
6419 add_sysrooted_prefix (&startfile_prefixes,
6420 standard_startfile_prefix, "BINUTILS",
6421 PREFIX_PRIORITY_LAST, 0, 1);
6422 else if (*cross_compile == '0')
6424 add_prefix (&startfile_prefixes,
6425 concat (gcc_exec_prefix
6426 ? gcc_exec_prefix : standard_exec_prefix,
6427 machine_suffix,
6428 standard_startfile_prefix, NULL),
6429 NULL, PREFIX_PRIORITY_LAST, 0, 1);
6432 /* Sysrooted prefixes are relocated because target_system_root is
6433 also relocated by gcc_exec_prefix. */
6434 if (*standard_startfile_prefix_1)
6435 add_sysrooted_prefix (&startfile_prefixes,
6436 standard_startfile_prefix_1, "BINUTILS",
6437 PREFIX_PRIORITY_LAST, 0, 1);
6438 if (*standard_startfile_prefix_2)
6439 add_sysrooted_prefix (&startfile_prefixes,
6440 standard_startfile_prefix_2, "BINUTILS",
6441 PREFIX_PRIORITY_LAST, 0, 1);
6444 /* Process any user specified specs in the order given on the command
6445 line. */
6446 for (uptr = user_specs_head; uptr; uptr = uptr->next)
6448 char *filename = find_a_file (&startfile_prefixes, uptr->filename,
6449 R_OK, true);
6450 read_specs (filename ? filename : uptr->filename, FALSE);
6453 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
6454 if (gcc_exec_prefix)
6455 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
6456 spec_version, dir_separator_str, NULL);
6458 /* Now we have the specs.
6459 Set the `valid' bits for switches that match anything in any spec. */
6461 validate_all_switches ();
6463 /* Now that we have the switches and the specs, set
6464 the subdirectory based on the options. */
6465 set_multilib_dir ();
6467 /* Set up to remember the pathname of gcc and any options
6468 needed for collect. We use argv[0] instead of progname because
6469 we need the complete pathname. */
6470 obstack_init (&collect_obstack);
6471 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
6472 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
6473 xputenv (XOBFINISH (&collect_obstack, char *));
6475 /* Set up to remember the pathname of the lto wrapper. */
6477 lto_wrapper_file = find_a_file (&exec_prefixes, "lto-wrapper", X_OK, false);
6478 if (lto_wrapper_file)
6480 lto_wrapper_spec = lto_wrapper_file;
6481 obstack_init (&collect_obstack);
6482 obstack_grow (&collect_obstack, "COLLECT_LTO_WRAPPER=",
6483 sizeof ("COLLECT_LTO_WRAPPER=") - 1);
6484 obstack_grow (&collect_obstack, lto_wrapper_spec,
6485 strlen (lto_wrapper_spec) + 1);
6486 xputenv (XOBFINISH (&collect_obstack, char *));
6489 /* Warn about any switches that no pass was interested in. */
6491 for (i = 0; (int) i < n_switches; i++)
6492 if (! switches[i].validated)
6493 error ("unrecognized option %<-%s%>", switches[i].part1);
6495 /* Obey some of the options. */
6497 if (print_search_dirs)
6499 printf (_("install: %s%s\n"),
6500 gcc_exec_prefix ? gcc_exec_prefix : standard_exec_prefix,
6501 gcc_exec_prefix ? "" : machine_suffix);
6502 printf (_("programs: %s\n"),
6503 build_search_list (&exec_prefixes, "", false, false));
6504 printf (_("libraries: %s\n"),
6505 build_search_list (&startfile_prefixes, "", false, true));
6506 return (0);
6509 if (print_file_name)
6511 printf ("%s\n", find_file (print_file_name));
6512 return (0);
6515 if (print_prog_name)
6517 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK, 0);
6518 printf ("%s\n", (newname ? newname : print_prog_name));
6519 return (0);
6522 if (print_multi_lib)
6524 print_multilib_info ();
6525 return (0);
6528 if (print_multi_directory)
6530 if (multilib_dir == NULL)
6531 printf (".\n");
6532 else
6533 printf ("%s\n", multilib_dir);
6534 return (0);
6537 if (print_sysroot)
6539 if (target_system_root)
6541 if (target_sysroot_suffix)
6542 printf ("%s%s\n", target_system_root, target_sysroot_suffix);
6543 else
6544 printf ("%s\n", target_system_root);
6546 return (0);
6549 if (print_multi_os_directory)
6551 if (multilib_os_dir == NULL)
6552 printf (".\n");
6553 else
6554 printf ("%s\n", multilib_os_dir);
6555 return (0);
6558 if (print_sysroot_headers_suffix)
6560 if (*sysroot_hdrs_suffix_spec)
6562 printf("%s\n", (target_sysroot_hdrs_suffix
6563 ? target_sysroot_hdrs_suffix
6564 : ""));
6565 return (0);
6567 else
6568 /* The error status indicates that only one set of fixed
6569 headers should be built. */
6570 fatal_error ("not configured with sysroot headers suffix");
6573 if (print_help_list)
6575 display_help ();
6577 if (! verbose_flag)
6579 printf (_("\nFor bug reporting instructions, please see:\n"));
6580 printf ("%s.\n", bug_report_url);
6582 return (0);
6585 /* We do not exit here. Instead we have created a fake input file
6586 called 'help-dummy' which needs to be compiled, and we pass this
6587 on the various sub-processes, along with the --help switch.
6588 Ensure their output appears after ours. */
6589 fputc ('\n', stdout);
6590 fflush (stdout);
6593 if (print_version)
6595 printf (_("%s %s%s\n"), progname, pkgversion_string,
6596 version_string);
6597 printf ("Copyright %s 2011 Free Software Foundation, Inc.\n",
6598 _("(C)"));
6599 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
6600 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
6601 stdout);
6602 if (! verbose_flag)
6603 return 0;
6605 /* We do not exit here. We use the same mechanism of --help to print
6606 the version of the sub-processes. */
6607 fputc ('\n', stdout);
6608 fflush (stdout);
6611 if (verbose_flag)
6613 int n;
6614 const char *thrmod;
6616 fnotice (stderr, "Target: %s\n", spec_machine);
6617 fnotice (stderr, "Configured with: %s\n", configuration_arguments);
6619 #ifdef THREAD_MODEL_SPEC
6620 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6621 but there's no point in doing all this processing just to get
6622 thread_model back. */
6623 obstack_init (&obstack);
6624 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6625 obstack_1grow (&obstack, '\0');
6626 thrmod = XOBFINISH (&obstack, const char *);
6627 #else
6628 thrmod = thread_model;
6629 #endif
6631 fnotice (stderr, "Thread model: %s\n", thrmod);
6633 /* compiler_version is truncated at the first space when initialized
6634 from version string, so truncate version_string at the first space
6635 before comparing. */
6636 for (n = 0; version_string[n]; n++)
6637 if (version_string[n] == ' ')
6638 break;
6640 if (! strncmp (version_string, compiler_version, n)
6641 && compiler_version[n] == 0)
6642 fnotice (stderr, "gcc version %s %s\n", version_string,
6643 pkgversion_string);
6644 else
6645 fnotice (stderr, "gcc driver version %s %sexecuting gcc version %s\n",
6646 version_string, pkgversion_string, compiler_version);
6648 if (n_infiles == 0)
6649 return (0);
6652 if (n_infiles == added_libraries)
6653 fatal_error ("no input files");
6655 if (seen_error ())
6656 goto out;
6658 /* Make a place to record the compiler output file names
6659 that correspond to the input files. */
6661 i = n_infiles;
6662 i += lang_specific_extra_outfiles;
6663 outfiles = XCNEWVEC (const char *, i);
6665 /* Record which files were specified explicitly as link input. */
6667 explicit_link_files = XCNEWVEC (char, n_infiles);
6669 combine_inputs = have_o || flag_wpa;
6671 for (i = 0; (int) i < n_infiles; i++)
6673 const char *name = infiles[i].name;
6674 struct compiler *compiler = lookup_compiler (name,
6675 strlen (name),
6676 infiles[i].language);
6678 if (compiler && !(compiler->combinable))
6679 combine_inputs = false;
6681 if (lang_n_infiles > 0 && compiler != input_file_compiler
6682 && infiles[i].language && infiles[i].language[0] != '*')
6683 infiles[i].incompiler = compiler;
6684 else if (compiler)
6686 lang_n_infiles++;
6687 input_file_compiler = compiler;
6688 infiles[i].incompiler = compiler;
6690 else
6692 /* Since there is no compiler for this input file, assume it is a
6693 linker file. */
6694 explicit_link_files[i] = 1;
6695 infiles[i].incompiler = NULL;
6697 infiles[i].compiled = false;
6698 infiles[i].preprocessed = false;
6701 if (!combine_inputs && have_c && have_o && lang_n_infiles > 1)
6702 fatal_error ("cannot specify -o with -c, -S or -E with multiple files");
6704 for (i = 0; (int) i < n_infiles; i++)
6706 int this_file_error = 0;
6708 /* Tell do_spec what to substitute for %i. */
6710 input_file_number = i;
6711 set_input (infiles[i].name);
6713 if (infiles[i].compiled)
6714 continue;
6716 /* Use the same thing in %o, unless cp->spec says otherwise. */
6718 outfiles[i] = gcc_input_filename;
6720 /* Figure out which compiler from the file's suffix. */
6722 input_file_compiler
6723 = lookup_compiler (infiles[i].name, input_filename_length,
6724 infiles[i].language);
6726 if (input_file_compiler)
6728 /* Ok, we found an applicable compiler. Run its spec. */
6730 if (input_file_compiler->spec[0] == '#')
6732 error ("%s: %s compiler not installed on this system",
6733 gcc_input_filename, &input_file_compiler->spec[1]);
6734 this_file_error = 1;
6736 else
6738 if (compare_debug)
6740 free (debug_check_temp_file[0]);
6741 debug_check_temp_file[0] = NULL;
6743 free (debug_check_temp_file[1]);
6744 debug_check_temp_file[1] = NULL;
6747 value = do_spec (input_file_compiler->spec);
6748 infiles[i].compiled = true;
6749 if (value < 0)
6750 this_file_error = 1;
6751 else if (compare_debug && debug_check_temp_file[0])
6753 if (verbose_flag)
6754 inform (0, "recompiling with -fcompare-debug");
6756 compare_debug = -compare_debug;
6757 n_switches = n_switches_debug_check[1];
6758 n_switches_alloc = n_switches_alloc_debug_check[1];
6759 switches = switches_debug_check[1];
6761 value = do_spec (input_file_compiler->spec);
6763 compare_debug = -compare_debug;
6764 n_switches = n_switches_debug_check[0];
6765 n_switches_alloc = n_switches_alloc_debug_check[0];
6766 switches = switches_debug_check[0];
6768 if (value < 0)
6770 error ("during -fcompare-debug recompilation");
6771 this_file_error = 1;
6774 gcc_assert (debug_check_temp_file[1]
6775 && filename_cmp (debug_check_temp_file[0],
6776 debug_check_temp_file[1]));
6778 if (verbose_flag)
6779 inform (0, "comparing final insns dumps");
6781 if (compare_files (debug_check_temp_file))
6782 this_file_error = 1;
6785 if (compare_debug)
6787 free (debug_check_temp_file[0]);
6788 debug_check_temp_file[0] = NULL;
6790 free (debug_check_temp_file[1]);
6791 debug_check_temp_file[1] = NULL;
6796 /* If this file's name does not contain a recognized suffix,
6797 record it as explicit linker input. */
6799 else
6800 explicit_link_files[i] = 1;
6802 /* Clear the delete-on-failure queue, deleting the files in it
6803 if this compilation failed. */
6805 if (this_file_error)
6807 delete_failure_queue ();
6808 errorcount++;
6810 /* If this compilation succeeded, don't delete those files later. */
6811 clear_failure_queue ();
6814 /* Reset the input file name to the first compile/object file name, for use
6815 with %b in LINK_SPEC. We use the first input file that we can find
6816 a compiler to compile it instead of using infiles.language since for
6817 languages other than C we use aliases that we then lookup later. */
6818 if (n_infiles > 0)
6820 int i;
6822 for (i = 0; i < n_infiles ; i++)
6823 if (infiles[i].incompiler
6824 || (infiles[i].language && infiles[i].language[0] != '*'))
6826 set_input (infiles[i].name);
6827 break;
6831 if (!seen_error ())
6833 /* Make sure INPUT_FILE_NUMBER points to first available open
6834 slot. */
6835 input_file_number = n_infiles;
6836 if (lang_specific_pre_link ())
6837 errorcount++;
6840 /* Determine if there are any linker input files. */
6841 num_linker_inputs = 0;
6842 for (i = 0; (int) i < n_infiles; i++)
6843 if (explicit_link_files[i] || outfiles[i] != NULL)
6844 num_linker_inputs++;
6846 /* Run ld to link all the compiler output files. */
6848 if (num_linker_inputs > 0 && !seen_error () && print_subprocess_help < 2)
6850 int tmp = execution_count;
6851 #if HAVE_LTO_PLUGIN > 0
6852 #if HAVE_LTO_PLUGIN == 2
6853 const char *fno_use_linker_plugin = "fno-use-linker-plugin";
6854 #else
6855 const char *fuse_linker_plugin = "fuse-linker-plugin";
6856 #endif
6857 #endif
6859 /* We'll use ld if we can't find collect2. */
6860 if (! strcmp (linker_name_spec, "collect2"))
6862 char *s = find_a_file (&exec_prefixes, "collect2", X_OK, false);
6863 if (s == NULL)
6864 linker_name_spec = "ld";
6867 #if HAVE_LTO_PLUGIN > 0
6868 #if HAVE_LTO_PLUGIN == 2
6869 if (!switch_matches (fno_use_linker_plugin,
6870 fno_use_linker_plugin + strlen (fno_use_linker_plugin), 0))
6871 #else
6872 if (switch_matches (fuse_linker_plugin,
6873 fuse_linker_plugin + strlen (fuse_linker_plugin), 0))
6874 #endif
6876 linker_plugin_file_spec = find_a_file (&exec_prefixes,
6877 LTOPLUGINSONAME, R_OK,
6878 false);
6879 if (!linker_plugin_file_spec)
6880 fatal_error ("-fuse-linker-plugin, but " LTOPLUGINSONAME " not found");
6882 #endif
6883 lto_gcc_spec = argv[0];
6885 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6886 for collect. */
6887 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH", false);
6888 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV, true);
6890 if (print_subprocess_help == 1)
6892 printf (_("\nLinker options\n==============\n\n"));
6893 printf (_("Use \"-Wl,OPTION\" to pass \"OPTION\""
6894 " to the linker.\n\n"));
6895 fflush (stdout);
6897 value = do_spec (link_command_spec);
6898 if (value < 0)
6899 errorcount = 1;
6900 linker_was_run = (tmp != execution_count);
6903 /* If options said don't run linker,
6904 complain about input files to be given to the linker. */
6906 if (! linker_was_run && !seen_error ())
6907 for (i = 0; (int) i < n_infiles; i++)
6908 if (explicit_link_files[i]
6909 && !(infiles[i].language && infiles[i].language[0] == '*'))
6910 warning (0, "%s: linker input file unused because linking not done",
6911 outfiles[i]);
6913 /* Delete some or all of the temporary files we made. */
6915 if (seen_error ())
6916 delete_failure_queue ();
6917 delete_temp_files ();
6919 if (print_help_list)
6921 printf (("\nFor bug reporting instructions, please see:\n"));
6922 printf ("%s\n", bug_report_url);
6925 out:
6926 return (signal_count != 0 ? 2
6927 : seen_error () ? (pass_exit_codes ? greatest_status : 1)
6928 : 0);
6931 /* Find the proper compilation spec for the file name NAME,
6932 whose length is LENGTH. LANGUAGE is the specified language,
6933 or 0 if this file is to be passed to the linker. */
6935 static struct compiler *
6936 lookup_compiler (const char *name, size_t length, const char *language)
6938 struct compiler *cp;
6940 /* If this was specified by the user to be a linker input, indicate that. */
6941 if (language != 0 && language[0] == '*')
6942 return 0;
6944 /* Otherwise, look for the language, if one is spec'd. */
6945 if (language != 0)
6947 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6948 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6949 return cp;
6951 error ("language %s not recognized", language);
6952 return 0;
6955 /* Look for a suffix. */
6956 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6958 if (/* The suffix `-' matches only the file name `-'. */
6959 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6960 || (strlen (cp->suffix) < length
6961 /* See if the suffix matches the end of NAME. */
6962 && !strcmp (cp->suffix,
6963 name + length - strlen (cp->suffix))
6965 break;
6968 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6969 /* Look again, but case-insensitively this time. */
6970 if (cp < compilers)
6971 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6973 if (/* The suffix `-' matches only the file name `-'. */
6974 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6975 || (strlen (cp->suffix) < length
6976 /* See if the suffix matches the end of NAME. */
6977 && ((!strcmp (cp->suffix,
6978 name + length - strlen (cp->suffix))
6979 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6980 && !strcasecmp (cp->suffix,
6981 name + length - strlen (cp->suffix)))
6983 break;
6985 #endif
6987 if (cp >= compilers)
6989 if (cp->spec[0] != '@')
6990 /* A non-alias entry: return it. */
6991 return cp;
6993 /* An alias entry maps a suffix to a language.
6994 Search for the language; pass 0 for NAME and LENGTH
6995 to avoid infinite recursion if language not found. */
6996 return lookup_compiler (NULL, 0, cp->spec + 1);
6998 return 0;
7001 static char *
7002 save_string (const char *s, int len)
7004 char *result = XNEWVEC (char, len + 1);
7006 memcpy (result, s, len);
7007 result[len] = 0;
7008 return result;
7011 void
7012 pfatal_with_name (const char *name)
7014 perror_with_name (name);
7015 delete_temp_files ();
7016 exit (1);
7019 static void
7020 perror_with_name (const char *name)
7022 error ("%s: %m", name);
7025 static inline void
7026 validate_switches_from_spec (const char *spec)
7028 const char *p = spec;
7029 char c;
7030 while ((c = *p++))
7031 if (c == '%' && (*p == '{' || *p == '<' || (*p == 'W' && *++p == '{')))
7032 /* We have a switch spec. */
7033 p = validate_switches (p + 1);
7036 static void
7037 validate_all_switches (void)
7039 struct compiler *comp;
7040 struct spec_list *spec;
7042 for (comp = compilers; comp->spec; comp++)
7043 validate_switches_from_spec (comp->spec);
7045 /* Look through the linked list of specs read from the specs file. */
7046 for (spec = specs; spec; spec = spec->next)
7047 validate_switches_from_spec (*spec->ptr_spec);
7049 validate_switches_from_spec (link_command_spec);
7052 /* Look at the switch-name that comes after START
7053 and mark as valid all supplied switches that match it. */
7055 static const char *
7056 validate_switches (const char *start)
7058 const char *p = start;
7059 const char *atom;
7060 size_t len;
7061 int i;
7062 bool suffix = false;
7063 bool starred = false;
7065 #define SKIP_WHITE() do { while (*p == ' ' || *p == '\t') p++; } while (0)
7067 next_member:
7068 SKIP_WHITE ();
7070 if (*p == '!')
7071 p++;
7073 SKIP_WHITE ();
7074 if (*p == '.' || *p == ',')
7075 suffix = true, p++;
7077 atom = p;
7078 while (ISIDNUM (*p) || *p == '-' || *p == '+' || *p == '='
7079 || *p == ',' || *p == '.' || *p == '@')
7080 p++;
7081 len = p - atom;
7083 if (*p == '*')
7084 starred = true, p++;
7086 SKIP_WHITE ();
7088 if (!suffix)
7090 /* Mark all matching switches as valid. */
7091 for (i = 0; i < n_switches; i++)
7092 if (!strncmp (switches[i].part1, atom, len)
7093 && (starred || switches[i].part1[len] == 0))
7094 switches[i].validated = 1;
7097 if (*p) p++;
7098 if (*p && (p[-1] == '|' || p[-1] == '&'))
7099 goto next_member;
7101 if (*p && p[-1] == ':')
7103 while (*p && *p != ';' && *p != '}')
7105 if (*p == '%')
7107 p++;
7108 if (*p == '{' || *p == '<')
7109 p = validate_switches (p+1);
7110 else if (p[0] == 'W' && p[1] == '{')
7111 p = validate_switches (p+2);
7113 else
7114 p++;
7117 if (*p) p++;
7118 if (*p && p[-1] == ';')
7119 goto next_member;
7122 return p;
7123 #undef SKIP_WHITE
7126 struct mdswitchstr
7128 const char *str;
7129 int len;
7132 static struct mdswitchstr *mdswitches;
7133 static int n_mdswitches;
7135 /* Check whether a particular argument was used. The first time we
7136 canonicalize the switches to keep only the ones we care about. */
7138 static int
7139 used_arg (const char *p, int len)
7141 struct mswitchstr
7143 const char *str;
7144 const char *replace;
7145 int len;
7146 int rep_len;
7149 static struct mswitchstr *mswitches;
7150 static int n_mswitches;
7151 int i, j;
7153 if (!mswitches)
7155 struct mswitchstr *matches;
7156 const char *q;
7157 int cnt = 0;
7159 /* Break multilib_matches into the component strings of string
7160 and replacement string. */
7161 for (q = multilib_matches; *q != '\0'; q++)
7162 if (*q == ';')
7163 cnt++;
7165 matches
7166 = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
7167 i = 0;
7168 q = multilib_matches;
7169 while (*q != '\0')
7171 matches[i].str = q;
7172 while (*q != ' ')
7174 if (*q == '\0')
7176 invalid_matches:
7177 fatal_error ("multilib spec %qs is invalid",
7178 multilib_matches);
7180 q++;
7182 matches[i].len = q - matches[i].str;
7184 matches[i].replace = ++q;
7185 while (*q != ';' && *q != '\0')
7187 if (*q == ' ')
7188 goto invalid_matches;
7189 q++;
7191 matches[i].rep_len = q - matches[i].replace;
7192 i++;
7193 if (*q == ';')
7194 q++;
7197 /* Now build a list of the replacement string for switches that we care
7198 about. Make sure we allocate at least one entry. This prevents
7199 xmalloc from calling fatal, and prevents us from re-executing this
7200 block of code. */
7201 mswitches
7202 = XNEWVEC (struct mswitchstr, n_mdswitches + (n_switches ? n_switches : 1));
7203 for (i = 0; i < n_switches; i++)
7204 if ((switches[i].live_cond & SWITCH_IGNORE) == 0)
7206 int xlen = strlen (switches[i].part1);
7207 for (j = 0; j < cnt; j++)
7208 if (xlen == matches[j].len
7209 && ! strncmp (switches[i].part1, matches[j].str, xlen))
7211 mswitches[n_mswitches].str = matches[j].replace;
7212 mswitches[n_mswitches].len = matches[j].rep_len;
7213 mswitches[n_mswitches].replace = (char *) 0;
7214 mswitches[n_mswitches].rep_len = 0;
7215 n_mswitches++;
7216 break;
7220 /* Add MULTILIB_DEFAULTS switches too, as long as they were not present
7221 on the command line nor any options mutually incompatible with
7222 them. */
7223 for (i = 0; i < n_mdswitches; i++)
7225 const char *r;
7227 for (q = multilib_options; *q != '\0'; q++)
7229 while (*q == ' ')
7230 q++;
7232 r = q;
7233 while (strncmp (q, mdswitches[i].str, mdswitches[i].len) != 0
7234 || strchr (" /", q[mdswitches[i].len]) == NULL)
7236 while (*q != ' ' && *q != '/' && *q != '\0')
7237 q++;
7238 if (*q != '/')
7239 break;
7240 q++;
7243 if (*q != ' ' && *q != '\0')
7245 while (*r != ' ' && *r != '\0')
7247 q = r;
7248 while (*q != ' ' && *q != '/' && *q != '\0')
7249 q++;
7251 if (used_arg (r, q - r))
7252 break;
7254 if (*q != '/')
7256 mswitches[n_mswitches].str = mdswitches[i].str;
7257 mswitches[n_mswitches].len = mdswitches[i].len;
7258 mswitches[n_mswitches].replace = (char *) 0;
7259 mswitches[n_mswitches].rep_len = 0;
7260 n_mswitches++;
7261 break;
7264 r = q + 1;
7266 break;
7272 for (i = 0; i < n_mswitches; i++)
7273 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
7274 return 1;
7276 return 0;
7279 static int
7280 default_arg (const char *p, int len)
7282 int i;
7284 for (i = 0; i < n_mdswitches; i++)
7285 if (len == mdswitches[i].len && ! strncmp (p, mdswitches[i].str, len))
7286 return 1;
7288 return 0;
7291 /* Work out the subdirectory to use based on the options. The format of
7292 multilib_select is a list of elements. Each element is a subdirectory
7293 name followed by a list of options followed by a semicolon. The format
7294 of multilib_exclusions is the same, but without the preceding
7295 directory. First gcc will check the exclusions, if none of the options
7296 beginning with an exclamation point are present, and all of the other
7297 options are present, then we will ignore this completely. Passing
7298 that, gcc will consider each multilib_select in turn using the same
7299 rules for matching the options. If a match is found, that subdirectory
7300 will be used. */
7302 static void
7303 set_multilib_dir (void)
7305 const char *p;
7306 unsigned int this_path_len;
7307 const char *this_path, *this_arg;
7308 const char *start, *end;
7309 int not_arg;
7310 int ok, ndfltok, first;
7312 n_mdswitches = 0;
7313 start = multilib_defaults;
7314 while (*start == ' ' || *start == '\t')
7315 start++;
7316 while (*start != '\0')
7318 n_mdswitches++;
7319 while (*start != ' ' && *start != '\t' && *start != '\0')
7320 start++;
7321 while (*start == ' ' || *start == '\t')
7322 start++;
7325 if (n_mdswitches)
7327 int i = 0;
7329 mdswitches = XNEWVEC (struct mdswitchstr, n_mdswitches);
7330 for (start = multilib_defaults; *start != '\0'; start = end + 1)
7332 while (*start == ' ' || *start == '\t')
7333 start++;
7335 if (*start == '\0')
7336 break;
7338 for (end = start + 1;
7339 *end != ' ' && *end != '\t' && *end != '\0'; end++)
7342 obstack_grow (&multilib_obstack, start, end - start);
7343 obstack_1grow (&multilib_obstack, 0);
7344 mdswitches[i].str = XOBFINISH (&multilib_obstack, const char *);
7345 mdswitches[i++].len = end - start;
7347 if (*end == '\0')
7348 break;
7352 p = multilib_exclusions;
7353 while (*p != '\0')
7355 /* Ignore newlines. */
7356 if (*p == '\n')
7358 ++p;
7359 continue;
7362 /* Check the arguments. */
7363 ok = 1;
7364 while (*p != ';')
7366 if (*p == '\0')
7368 invalid_exclusions:
7369 fatal_error ("multilib exclusions %qs is invalid",
7370 multilib_exclusions);
7373 if (! ok)
7375 ++p;
7376 continue;
7379 this_arg = p;
7380 while (*p != ' ' && *p != ';')
7382 if (*p == '\0')
7383 goto invalid_exclusions;
7384 ++p;
7387 if (*this_arg != '!')
7388 not_arg = 0;
7389 else
7391 not_arg = 1;
7392 ++this_arg;
7395 ok = used_arg (this_arg, p - this_arg);
7396 if (not_arg)
7397 ok = ! ok;
7399 if (*p == ' ')
7400 ++p;
7403 if (ok)
7404 return;
7406 ++p;
7409 first = 1;
7410 p = multilib_select;
7411 while (*p != '\0')
7413 /* Ignore newlines. */
7414 if (*p == '\n')
7416 ++p;
7417 continue;
7420 /* Get the initial path. */
7421 this_path = p;
7422 while (*p != ' ')
7424 if (*p == '\0')
7426 invalid_select:
7427 fatal_error ("multilib select %qs is invalid",
7428 multilib_select);
7430 ++p;
7432 this_path_len = p - this_path;
7434 /* Check the arguments. */
7435 ok = 1;
7436 ndfltok = 1;
7437 ++p;
7438 while (*p != ';')
7440 if (*p == '\0')
7441 goto invalid_select;
7443 if (! ok)
7445 ++p;
7446 continue;
7449 this_arg = p;
7450 while (*p != ' ' && *p != ';')
7452 if (*p == '\0')
7453 goto invalid_select;
7454 ++p;
7457 if (*this_arg != '!')
7458 not_arg = 0;
7459 else
7461 not_arg = 1;
7462 ++this_arg;
7465 /* If this is a default argument, we can just ignore it.
7466 This is true even if this_arg begins with '!'. Beginning
7467 with '!' does not mean that this argument is necessarily
7468 inappropriate for this library: it merely means that
7469 there is a more specific library which uses this
7470 argument. If this argument is a default, we need not
7471 consider that more specific library. */
7472 ok = used_arg (this_arg, p - this_arg);
7473 if (not_arg)
7474 ok = ! ok;
7476 if (! ok)
7477 ndfltok = 0;
7479 if (default_arg (this_arg, p - this_arg))
7480 ok = 1;
7482 if (*p == ' ')
7483 ++p;
7486 if (ok && first)
7488 if (this_path_len != 1
7489 || this_path[0] != '.')
7491 char *new_multilib_dir = XNEWVEC (char, this_path_len + 1);
7492 char *q;
7494 strncpy (new_multilib_dir, this_path, this_path_len);
7495 new_multilib_dir[this_path_len] = '\0';
7496 q = strchr (new_multilib_dir, ':');
7497 if (q != NULL)
7498 *q = '\0';
7499 multilib_dir = new_multilib_dir;
7501 first = 0;
7504 if (ndfltok)
7506 const char *q = this_path, *end = this_path + this_path_len;
7508 while (q < end && *q != ':')
7509 q++;
7510 if (q < end)
7512 char *new_multilib_os_dir = XNEWVEC (char, end - q);
7513 memcpy (new_multilib_os_dir, q + 1, end - q - 1);
7514 new_multilib_os_dir[end - q - 1] = '\0';
7515 multilib_os_dir = new_multilib_os_dir;
7516 break;
7520 ++p;
7523 if (multilib_dir == NULL && multilib_os_dir != NULL
7524 && strcmp (multilib_os_dir, ".") == 0)
7526 free (CONST_CAST (char *, multilib_os_dir));
7527 multilib_os_dir = NULL;
7529 else if (multilib_dir != NULL && multilib_os_dir == NULL)
7530 multilib_os_dir = multilib_dir;
7533 /* Print out the multiple library subdirectory selection
7534 information. This prints out a series of lines. Each line looks
7535 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
7536 required. Only the desired options are printed out, the negative
7537 matches. The options are print without a leading dash. There are
7538 no spaces to make it easy to use the information in the shell.
7539 Each subdirectory is printed only once. This assumes the ordering
7540 generated by the genmultilib script. Also, we leave out ones that match
7541 the exclusions. */
7543 static void
7544 print_multilib_info (void)
7546 const char *p = multilib_select;
7547 const char *last_path = 0, *this_path;
7548 int skip;
7549 unsigned int last_path_len = 0;
7551 while (*p != '\0')
7553 skip = 0;
7554 /* Ignore newlines. */
7555 if (*p == '\n')
7557 ++p;
7558 continue;
7561 /* Get the initial path. */
7562 this_path = p;
7563 while (*p != ' ')
7565 if (*p == '\0')
7567 invalid_select:
7568 fatal_error ("multilib select %qs is invalid", multilib_select);
7571 ++p;
7574 /* When --disable-multilib was used but target defines
7575 MULTILIB_OSDIRNAMES, entries starting with .: are there just
7576 to find multilib_os_dir, so skip them from output. */
7577 if (this_path[0] == '.' && this_path[1] == ':')
7578 skip = 1;
7580 /* Check for matches with the multilib_exclusions. We don't bother
7581 with the '!' in either list. If any of the exclusion rules match
7582 all of its options with the select rule, we skip it. */
7584 const char *e = multilib_exclusions;
7585 const char *this_arg;
7587 while (*e != '\0')
7589 int m = 1;
7590 /* Ignore newlines. */
7591 if (*e == '\n')
7593 ++e;
7594 continue;
7597 /* Check the arguments. */
7598 while (*e != ';')
7600 const char *q;
7601 int mp = 0;
7603 if (*e == '\0')
7605 invalid_exclusion:
7606 fatal_error ("multilib exclusion %qs is invalid",
7607 multilib_exclusions);
7610 if (! m)
7612 ++e;
7613 continue;
7616 this_arg = e;
7618 while (*e != ' ' && *e != ';')
7620 if (*e == '\0')
7621 goto invalid_exclusion;
7622 ++e;
7625 q = p + 1;
7626 while (*q != ';')
7628 const char *arg;
7629 int len = e - this_arg;
7631 if (*q == '\0')
7632 goto invalid_select;
7634 arg = q;
7636 while (*q != ' ' && *q != ';')
7638 if (*q == '\0')
7639 goto invalid_select;
7640 ++q;
7643 if (! strncmp (arg, this_arg,
7644 (len < q - arg) ? q - arg : len)
7645 || default_arg (this_arg, e - this_arg))
7647 mp = 1;
7648 break;
7651 if (*q == ' ')
7652 ++q;
7655 if (! mp)
7656 m = 0;
7658 if (*e == ' ')
7659 ++e;
7662 if (m)
7664 skip = 1;
7665 break;
7668 if (*e != '\0')
7669 ++e;
7673 if (! skip)
7675 /* If this is a duplicate, skip it. */
7676 skip = (last_path != 0
7677 && (unsigned int) (p - this_path) == last_path_len
7678 && ! filename_ncmp (last_path, this_path, last_path_len));
7680 last_path = this_path;
7681 last_path_len = p - this_path;
7684 /* If this directory requires any default arguments, we can skip
7685 it. We will already have printed a directory identical to
7686 this one which does not require that default argument. */
7687 if (! skip)
7689 const char *q;
7691 q = p + 1;
7692 while (*q != ';')
7694 const char *arg;
7696 if (*q == '\0')
7697 goto invalid_select;
7699 if (*q == '!')
7700 arg = NULL;
7701 else
7702 arg = q;
7704 while (*q != ' ' && *q != ';')
7706 if (*q == '\0')
7707 goto invalid_select;
7708 ++q;
7711 if (arg != NULL
7712 && default_arg (arg, q - arg))
7714 skip = 1;
7715 break;
7718 if (*q == ' ')
7719 ++q;
7723 if (! skip)
7725 const char *p1;
7727 for (p1 = last_path; p1 < p && *p1 != ':'; p1++)
7728 putchar (*p1);
7729 putchar (';');
7732 ++p;
7733 while (*p != ';')
7735 int use_arg;
7737 if (*p == '\0')
7738 goto invalid_select;
7740 if (skip)
7742 ++p;
7743 continue;
7746 use_arg = *p != '!';
7748 if (use_arg)
7749 putchar ('@');
7751 while (*p != ' ' && *p != ';')
7753 if (*p == '\0')
7754 goto invalid_select;
7755 if (use_arg)
7756 putchar (*p);
7757 ++p;
7760 if (*p == ' ')
7761 ++p;
7764 if (! skip)
7766 /* If there are extra options, print them now. */
7767 if (multilib_extra && *multilib_extra)
7769 int print_at = TRUE;
7770 const char *q;
7772 for (q = multilib_extra; *q != '\0'; q++)
7774 if (*q == ' ')
7775 print_at = TRUE;
7776 else
7778 if (print_at)
7779 putchar ('@');
7780 putchar (*q);
7781 print_at = FALSE;
7786 putchar ('\n');
7789 ++p;
7793 /* getenv built-in spec function.
7795 Returns the value of the environment variable given by its first
7796 argument, concatenated with the second argument. If the
7797 environment variable is not defined, a fatal error is issued. */
7799 static const char *
7800 getenv_spec_function (int argc, const char **argv)
7802 char *value;
7803 char *result;
7804 char *ptr;
7805 size_t len;
7807 if (argc != 2)
7808 return NULL;
7810 value = getenv (argv[0]);
7811 if (!value)
7812 fatal_error ("environment variable %qs not defined", argv[0]);
7814 /* We have to escape every character of the environment variable so
7815 they are not interpreted as active spec characters. A
7816 particularly painful case is when we are reading a variable
7817 holding a windows path complete with \ separators. */
7818 len = strlen (value) * 2 + strlen (argv[1]) + 1;
7819 result = XNEWVAR (char, len);
7820 for (ptr = result; *value; ptr += 2)
7822 ptr[0] = '\\';
7823 ptr[1] = *value++;
7826 strcpy (ptr, argv[1]);
7828 return result;
7831 /* if-exists built-in spec function.
7833 Checks to see if the file specified by the absolute pathname in
7834 ARGS exists. Returns that pathname if found.
7836 The usual use for this function is to check for a library file
7837 (whose name has been expanded with %s). */
7839 static const char *
7840 if_exists_spec_function (int argc, const char **argv)
7842 /* Must have only one argument. */
7843 if (argc == 1 && IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
7844 return argv[0];
7846 return NULL;
7849 /* if-exists-else built-in spec function.
7851 This is like if-exists, but takes an additional argument which
7852 is returned if the first argument does not exist. */
7854 static const char *
7855 if_exists_else_spec_function (int argc, const char **argv)
7857 /* Must have exactly two arguments. */
7858 if (argc != 2)
7859 return NULL;
7861 if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
7862 return argv[0];
7864 return argv[1];
7867 /* replace-outfile built-in spec function.
7869 This looks for the first argument in the outfiles array's name and
7870 replaces it with the second argument. */
7872 static const char *
7873 replace_outfile_spec_function (int argc, const char **argv)
7875 int i;
7876 /* Must have exactly two arguments. */
7877 if (argc != 2)
7878 abort ();
7880 for (i = 0; i < n_infiles; i++)
7882 if (outfiles[i] && !filename_cmp (outfiles[i], argv[0]))
7883 outfiles[i] = xstrdup (argv[1]);
7885 return NULL;
7888 /* remove-outfile built-in spec function.
7890 * This looks for the first argument in the outfiles array's name and
7891 * removes it. */
7893 static const char *
7894 remove_outfile_spec_function (int argc, const char **argv)
7896 int i;
7897 /* Must have exactly one argument. */
7898 if (argc != 1)
7899 abort ();
7901 for (i = 0; i < n_infiles; i++)
7903 if (outfiles[i] && !filename_cmp (outfiles[i], argv[0]))
7904 outfiles[i] = NULL;
7906 return NULL;
7909 /* Given two version numbers, compares the two numbers.
7910 A version number must match the regular expression
7911 ([1-9][0-9]*|0)(\.([1-9][0-9]*|0))*
7913 static int
7914 compare_version_strings (const char *v1, const char *v2)
7916 int rresult;
7917 regex_t r;
7919 if (regcomp (&r, "^([1-9][0-9]*|0)(\\.([1-9][0-9]*|0))*$",
7920 REG_EXTENDED | REG_NOSUB) != 0)
7921 abort ();
7922 rresult = regexec (&r, v1, 0, NULL, 0);
7923 if (rresult == REG_NOMATCH)
7924 fatal_error ("invalid version number %qs", v1);
7925 else if (rresult != 0)
7926 abort ();
7927 rresult = regexec (&r, v2, 0, NULL, 0);
7928 if (rresult == REG_NOMATCH)
7929 fatal_error ("invalid version number %qs", v2);
7930 else if (rresult != 0)
7931 abort ();
7933 return strverscmp (v1, v2);
7937 /* version_compare built-in spec function.
7939 This takes an argument of the following form:
7941 <comparison-op> <arg1> [<arg2>] <switch> <result>
7943 and produces "result" if the comparison evaluates to true,
7944 and nothing if it doesn't.
7946 The supported <comparison-op> values are:
7948 >= true if switch is a later (or same) version than arg1
7949 !> opposite of >=
7950 < true if switch is an earlier version than arg1
7951 !< opposite of <
7952 >< true if switch is arg1 or later, and earlier than arg2
7953 <> true if switch is earlier than arg1 or is arg2 or later
7955 If the switch is not present, the condition is false unless
7956 the first character of the <comparison-op> is '!'.
7958 For example,
7959 %:version-compare(>= 10.3 mmacosx-version-min= -lmx)
7960 adds -lmx if -mmacosx-version-min=10.3.9 was passed. */
7962 static const char *
7963 version_compare_spec_function (int argc, const char **argv)
7965 int comp1, comp2;
7966 size_t switch_len;
7967 const char *switch_value = NULL;
7968 int nargs = 1, i;
7969 bool result;
7971 if (argc < 3)
7972 fatal_error ("too few arguments to %%:version-compare");
7973 if (argv[0][0] == '\0')
7974 abort ();
7975 if ((argv[0][1] == '<' || argv[0][1] == '>') && argv[0][0] != '!')
7976 nargs = 2;
7977 if (argc != nargs + 3)
7978 fatal_error ("too many arguments to %%:version-compare");
7980 switch_len = strlen (argv[nargs + 1]);
7981 for (i = 0; i < n_switches; i++)
7982 if (!strncmp (switches[i].part1, argv[nargs + 1], switch_len)
7983 && check_live_switch (i, switch_len))
7984 switch_value = switches[i].part1 + switch_len;
7986 if (switch_value == NULL)
7987 comp1 = comp2 = -1;
7988 else
7990 comp1 = compare_version_strings (switch_value, argv[1]);
7991 if (nargs == 2)
7992 comp2 = compare_version_strings (switch_value, argv[2]);
7993 else
7994 comp2 = -1; /* This value unused. */
7997 switch (argv[0][0] << 8 | argv[0][1])
7999 case '>' << 8 | '=':
8000 result = comp1 >= 0;
8001 break;
8002 case '!' << 8 | '<':
8003 result = comp1 >= 0 || switch_value == NULL;
8004 break;
8005 case '<' << 8:
8006 result = comp1 < 0;
8007 break;
8008 case '!' << 8 | '>':
8009 result = comp1 < 0 || switch_value == NULL;
8010 break;
8011 case '>' << 8 | '<':
8012 result = comp1 >= 0 && comp2 < 0;
8013 break;
8014 case '<' << 8 | '>':
8015 result = comp1 < 0 || comp2 >= 0;
8016 break;
8018 default:
8019 fatal_error ("unknown operator %qs in %%:version-compare", argv[0]);
8021 if (! result)
8022 return NULL;
8024 return argv[nargs + 2];
8027 /* %:include builtin spec function. This differs from %include in that it
8028 can be nested inside a spec, and thus be conditionalized. It takes
8029 one argument, the filename, and looks for it in the startfile path.
8030 The result is always NULL, i.e. an empty expansion. */
8032 static const char *
8033 include_spec_function (int argc, const char **argv)
8035 char *file;
8037 if (argc != 1)
8038 abort ();
8040 file = find_a_file (&startfile_prefixes, argv[0], R_OK, true);
8041 read_specs (file ? file : argv[0], FALSE);
8043 return NULL;
8046 /* %:find-file spec function. This function replaces its argument by
8047 the file found thru find_file, that is the -print-file-name gcc
8048 program option. */
8049 static const char *
8050 find_file_spec_function (int argc, const char **argv)
8052 const char *file;
8054 if (argc != 1)
8055 abort ();
8057 file = find_file (argv[0]);
8058 return file;
8062 /* %:find-plugindir spec function. This function replaces its argument
8063 by the -iplugindir=<dir> option. `dir' is found thru find_file, that
8064 is the -print-file-name gcc program option. */
8065 static const char *
8066 find_plugindir_spec_function (int argc, const char **argv ATTRIBUTE_UNUSED)
8068 const char *option;
8070 if (argc != 0)
8071 abort ();
8073 option = concat ("-iplugindir=", find_file ("plugin"), NULL);
8074 return option;
8078 /* %:print-asm-header spec function. Print a banner to say that the
8079 following output is from the assembler. */
8081 static const char *
8082 print_asm_header_spec_function (int arg ATTRIBUTE_UNUSED,
8083 const char **argv ATTRIBUTE_UNUSED)
8085 printf (_("Assembler options\n=================\n\n"));
8086 printf (_("Use \"-Wa,OPTION\" to pass \"OPTION\" to the assembler.\n\n"));
8087 fflush (stdout);
8088 return NULL;
8091 /* Compute a timestamp to initialize flag_random_seed. */
8093 static unsigned
8094 get_local_tick (void)
8096 unsigned ret = 0;
8098 /* Get some more or less random data. */
8099 #ifdef HAVE_GETTIMEOFDAY
8101 struct timeval tv;
8103 gettimeofday (&tv, NULL);
8104 ret = tv.tv_sec * 1000 + tv.tv_usec / 1000;
8106 #else
8108 time_t now = time (NULL);
8110 if (now != (time_t)-1)
8111 ret = (unsigned) now;
8113 #endif
8115 return ret;
8118 /* %:compare-debug-dump-opt spec function. Save the last argument,
8119 expected to be the last -fdump-final-insns option, or generate a
8120 temporary. */
8122 static const char *
8123 compare_debug_dump_opt_spec_function (int arg,
8124 const char **argv ATTRIBUTE_UNUSED)
8126 const char *ret;
8127 char *name;
8128 int which;
8129 static char random_seed[HOST_BITS_PER_WIDE_INT / 4 + 3];
8131 if (arg != 0)
8132 fatal_error ("too many arguments to %%:compare-debug-dump-opt");
8134 do_spec_2 ("%{fdump-final-insns=*:%*}");
8135 do_spec_1 (" ", 0, NULL);
8137 if (VEC_length (const_char_p, argbuf) > 0
8138 && strcmp (argv[VEC_length (const_char_p, argbuf) - 1], "."))
8140 if (!compare_debug)
8141 return NULL;
8143 name = xstrdup (argv[VEC_length (const_char_p, argbuf) - 1]);
8144 ret = NULL;
8146 else
8148 const char *ext = NULL;
8150 if (VEC_length (const_char_p, argbuf) > 0)
8152 do_spec_2 ("%{o*:%*}%{!o:%{!S:%b%O}%{S:%b.s}}");
8153 ext = ".gkd";
8155 else if (!compare_debug)
8156 return NULL;
8157 else
8158 do_spec_2 ("%g.gkd");
8160 do_spec_1 (" ", 0, NULL);
8162 gcc_assert (VEC_length (const_char_p, argbuf) > 0);
8164 name = concat (VEC_last (const_char_p, argbuf), ext, NULL);
8166 ret = concat ("-fdump-final-insns=", name, NULL);
8169 which = compare_debug < 0;
8170 debug_check_temp_file[which] = name;
8172 if (!which)
8174 unsigned HOST_WIDE_INT value = get_local_tick () ^ getpid ();
8176 sprintf (random_seed, HOST_WIDE_INT_PRINT_HEX, value);
8179 if (*random_seed)
8180 ret = concat ("%{!frandom-seed=*:-frandom-seed=", random_seed, "} ",
8181 ret, NULL);
8183 if (which)
8184 *random_seed = 0;
8186 return ret;
8189 static const char *debug_auxbase_opt;
8191 /* %:compare-debug-self-opt spec function. Expands to the options
8192 that are to be passed in the second compilation of
8193 compare-debug. */
8195 static const char *
8196 compare_debug_self_opt_spec_function (int arg,
8197 const char **argv ATTRIBUTE_UNUSED)
8199 if (arg != 0)
8200 fatal_error ("too many arguments to %%:compare-debug-self-opt");
8202 if (compare_debug >= 0)
8203 return NULL;
8205 do_spec_2 ("%{c|S:%{o*:%*}}");
8206 do_spec_1 (" ", 0, NULL);
8208 if (VEC_length (const_char_p, argbuf) > 0)
8209 debug_auxbase_opt = concat ("-auxbase-strip ",
8210 VEC_last (const_char_p, argbuf),
8211 NULL);
8212 else
8213 debug_auxbase_opt = NULL;
8215 return concat ("\
8216 %<o %<MD %<MMD %<MF* %<MG %<MP %<MQ* %<MT* \
8217 %<fdump-final-insns=* -w -S -o %j \
8218 %{!fcompare-debug-second:-fcompare-debug-second} \
8219 ", compare_debug_opt, NULL);
8222 /* %:compare-debug-auxbase-opt spec function. Expands to the auxbase
8223 options that are to be passed in the second compilation of
8224 compare-debug. It expects, as an argument, the basename of the
8225 current input file name, with the .gk suffix appended to it. */
8227 static const char *
8228 compare_debug_auxbase_opt_spec_function (int arg,
8229 const char **argv)
8231 char *name;
8232 int len;
8234 if (arg == 0)
8235 fatal_error ("too few arguments to %%:compare-debug-auxbase-opt");
8237 if (arg != 1)
8238 fatal_error ("too many arguments to %%:compare-debug-auxbase-opt");
8240 if (compare_debug >= 0)
8241 return NULL;
8243 len = strlen (argv[0]);
8244 if (len < 3 || strcmp (argv[0] + len - 3, ".gk") != 0)
8245 fatal_error ("argument to %%:compare-debug-auxbase-opt "
8246 "does not end in .gk");
8248 if (debug_auxbase_opt)
8249 return debug_auxbase_opt;
8251 #define OPT "-auxbase "
8253 len -= 3;
8254 name = (char*) xmalloc (sizeof (OPT) + len);
8255 memcpy (name, OPT, sizeof (OPT) - 1);
8256 memcpy (name + sizeof (OPT) - 1, argv[0], len);
8257 name[sizeof (OPT) - 1 + len] = '\0';
8259 #undef OPT
8261 return name;
8264 /* %:pass-through-libs spec function. Finds all -l options and input
8265 file names in the lib spec passed to it, and makes a list of them
8266 prepended with the plugin option to cause them to be passed through
8267 to the final link after all the new object files have been added. */
8269 const char *
8270 pass_through_libs_spec_func (int argc, const char **argv)
8272 char *prepended = xstrdup (" ");
8273 int n;
8274 /* Shlemiel the painter's algorithm. Innately horrible, but at least
8275 we know that there will never be more than a handful of strings to
8276 concat, and it's only once per run, so it's not worth optimising. */
8277 for (n = 0; n < argc; n++)
8279 char *old = prepended;
8280 /* Anything that isn't an option is a full path to an output
8281 file; pass it through if it ends in '.a'. Among options,
8282 pass only -l. */
8283 if (argv[n][0] == '-' && argv[n][1] == 'l')
8285 const char *lopt = argv[n] + 2;
8286 /* Handle both joined and non-joined -l options. If for any
8287 reason there's a trailing -l with no joined or following
8288 arg just discard it. */
8289 if (!*lopt && ++n >= argc)
8290 break;
8291 else if (!*lopt)
8292 lopt = argv[n];
8293 prepended = concat (prepended, "-plugin-opt=-pass-through=-l",
8294 lopt, " ", NULL);
8296 else if (!strcmp (".a", argv[n] + strlen (argv[n]) - 2))
8298 prepended = concat (prepended, "-plugin-opt=-pass-through=",
8299 argv[n], " ", NULL);
8301 if (prepended != old)
8302 free (old);
8304 return prepended;