* config/i386/netbsd-elf.h (TARGET_OS_CPP_BUILTINS): Define.
[official-gcc.git] / gcc / gcc.c
blobf5e70ca6b02b1e78e2c81da781cf7e05962e31b5
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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
22 This paragraph is here to try to keep Sun CC from dying.
23 The number of chars here seems crucial!!!! */
25 /* This program is the user interface to the C compiler and possibly to
26 other compilers. It is used because compilation is a complicated procedure
27 which involves running several programs and passing temporary files between
28 them, forwarding the users switches to those programs selectively,
29 and deleting the temporary files at the end.
31 CC recognizes how to compile each input file by suffixes in the file names.
32 Once it knows which kind of compilation to perform, the procedure for
33 compilation is specified by a string called a "spec". */
35 /* A Short Introduction to Adding a Command-Line Option.
37 Before adding a command-line option, consider if it is really
38 necessary. Each additional command-line option adds complexity and
39 is difficult to remove in subsequent versions.
41 In the following, consider adding the command-line argument
42 `--bar'.
44 1. Each command-line option is specified in the specs file. The
45 notation is described below in the comment entitled "The Specs
46 Language". Read it.
48 2. In this file, add an entry to "option_map" equating the long
49 `--' argument version and any shorter, single letter version. Read
50 the comments in the declaration of "struct option_map" for an
51 explanation. Do not omit the first `-'.
53 3. Look in the "specs" file to determine which program or option
54 list should be given the argument, e.g., "cc1_options". Add the
55 appropriate syntax for the shorter option version to the
56 corresponding "const char *" entry in this file. Omit the first
57 `-' from the option. For example, use `-bar', rather than `--bar'.
59 4. If the argument takes an argument, e.g., `--baz argument1',
60 modify either DEFAULT_SWITCH_TAKES_ARG or
61 DEFAULT_WORD_SWITCH_TAKES_ARG in this file. Omit the first `-'
62 from `--baz'.
64 5. Document the option in this file's display_help(). If the
65 option is passed to a subprogram, modify its corresponding
66 function, e.g., cppinit.c:print_help() or toplev.c:display_help(),
67 instead.
69 6. Compile and test. Make sure that your new specs file is being
70 read. For example, use a debugger to investigate the value of
71 "specs_file" in main(). */
73 #include "config.h"
74 #include "system.h"
75 #include <signal.h>
76 #if ! defined( SIGCHLD ) && defined( SIGCLD )
77 # define SIGCHLD SIGCLD
78 #endif
79 #include "obstack.h"
80 #include "intl.h"
81 #include "prefix.h"
82 #include "gcc.h"
83 #include "flags.h"
85 #ifdef HAVE_SYS_RESOURCE_H
86 #include <sys/resource.h>
87 #endif
88 #if defined (HAVE_DECL_GETRUSAGE) && !HAVE_DECL_GETRUSAGE
89 extern int getrusage PARAMS ((int, struct rusage *));
90 #endif
92 /* By default there is no special suffix for target executables. */
93 /* FIXME: when autoconf is fixed, remove the host check - dj */
94 #if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX)
95 #define HAVE_TARGET_EXECUTABLE_SUFFIX
96 #else
97 #undef TARGET_EXECUTABLE_SUFFIX
98 #define TARGET_EXECUTABLE_SUFFIX ""
99 #endif
101 /* By default there is no special suffix for host executables. */
102 #ifdef HOST_EXECUTABLE_SUFFIX
103 #define HAVE_HOST_EXECUTABLE_SUFFIX
104 #else
105 #define HOST_EXECUTABLE_SUFFIX ""
106 #endif
108 /* By default, the suffix for target object files is ".o". */
109 #ifdef TARGET_OBJECT_SUFFIX
110 #define HAVE_TARGET_OBJECT_SUFFIX
111 #else
112 #define TARGET_OBJECT_SUFFIX ".o"
113 #endif
115 #ifndef VMS
116 /* FIXME: the location independence code for VMS is hairier than this,
117 and hasn't been written. */
118 #ifndef DIR_UP
119 #define DIR_UP ".."
120 #endif /* DIR_UP */
121 #endif /* VMS */
123 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
125 #define obstack_chunk_alloc xmalloc
126 #define obstack_chunk_free free
128 #ifndef GET_ENV_PATH_LIST
129 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
130 #endif
132 /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
133 #ifndef LIBRARY_PATH_ENV
134 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
135 #endif
137 #ifndef HAVE_KILL
138 #define kill(p,s) raise(s)
139 #endif
141 /* If a stage of compilation returns an exit status >= 1,
142 compilation of that file ceases. */
144 #define MIN_FATAL_STATUS 1
146 /* Flag set by cppspec.c to 1. */
147 int is_cpp_driver;
149 /* Flag saying to pass the greatest exit code returned by a sub-process
150 to the calling program. */
151 static int pass_exit_codes;
153 /* Definition of string containing the arguments given to configure. */
154 #include "configargs.h"
156 /* Flag saying to print the directories gcc will search through looking for
157 programs, libraries, etc. */
159 static int print_search_dirs;
161 /* Flag saying to print the full filename of this file
162 as found through our usual search mechanism. */
164 static const char *print_file_name = NULL;
166 /* As print_file_name, but search for executable file. */
168 static const char *print_prog_name = NULL;
170 /* Flag saying to print the relative path we'd use to
171 find libgcc.a given the current compiler flags. */
173 static int print_multi_directory;
175 /* Flag saying to print the list of subdirectories and
176 compiler flags used to select them in a standard form. */
178 static int print_multi_lib;
180 /* Flag saying to print the command line options understood by gcc and its
181 sub-processes. */
183 static int print_help_list;
185 /* Flag indicating whether we should print the command and arguments */
187 static int verbose_flag;
189 /* Flag indicating whether we should ONLY print the command and
190 arguments (like verbose_flag) without executing the command.
191 Displayed arguments are quoted so that the generated command
192 line is suitable for execution. This is intended for use in
193 shell scripts to capture the driver-generated command line. */
194 static int verbose_only_flag;
196 /* Flag indicating to print target specific command line options. */
198 static int target_help_flag;
200 /* Flag indicating whether we should report subprocess execution times
201 (if this is supported by the system - see pexecute.c). */
203 static int report_times;
205 /* Nonzero means write "temp" files in source directory
206 and use the source file's name in them, and don't delete them. */
208 static int save_temps_flag;
210 /* The compiler version. */
212 static const char *compiler_version;
214 /* The target version specified with -V */
216 static const char *spec_version = DEFAULT_TARGET_VERSION;
218 /* The target machine specified with -b. */
220 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
222 /* Nonzero if cross-compiling.
223 When -b is used, the value comes from the `specs' file. */
225 #ifdef CROSS_COMPILE
226 static const char *cross_compile = "1";
227 #else
228 static const char *cross_compile = "0";
229 #endif
231 #ifdef MODIFY_TARGET_NAME
233 /* Information on how to alter the target name based on a command-line
234 switch. The only case we support now is simply appending or deleting a
235 string to or from the end of the first part of the configuration name. */
237 static const struct modify_target
239 const char *const sw;
240 const enum add_del {ADD, DELETE} add_del;
241 const char *const str;
243 modify_target[] = MODIFY_TARGET_NAME;
244 #endif
246 /* The number of errors that have occurred; the link phase will not be
247 run if this is non-zero. */
248 static int error_count = 0;
250 /* Greatest exit code of sub-processes that has been encountered up to
251 now. */
252 static int greatest_status = 1;
254 /* This is the obstack which we use to allocate many strings. */
256 static struct obstack obstack;
258 /* This is the obstack to build an environment variable to pass to
259 collect2 that describes all of the relevant switches of what to
260 pass the compiler in building the list of pointers to constructors
261 and destructors. */
263 static struct obstack collect_obstack;
265 /* These structs are used to collect resource usage information for
266 subprocesses. */
267 #ifdef HAVE_GETRUSAGE
268 static struct rusage rus, prus;
269 #endif
271 /* Forward declaration for prototypes. */
272 struct path_prefix;
274 static void init_spec PARAMS ((void));
275 #ifndef VMS
276 static char **split_directories PARAMS ((const char *, int *));
277 static void free_split_directories PARAMS ((char **));
278 static char *make_relative_prefix PARAMS ((const char *, const char *, const char *));
279 #endif /* VMS */
280 static void store_arg PARAMS ((const char *, int, int));
281 static char *load_specs PARAMS ((const char *));
282 static void read_specs PARAMS ((const char *, int));
283 static void set_spec PARAMS ((const char *, const char *));
284 static struct compiler *lookup_compiler PARAMS ((const char *, size_t, const char *));
285 static char *build_search_list PARAMS ((struct path_prefix *, const char *, int));
286 static void putenv_from_prefixes PARAMS ((struct path_prefix *, const char *));
287 static int access_check PARAMS ((const char *, int));
288 static char *find_a_file PARAMS ((struct path_prefix *, const char *, int));
289 static void add_prefix PARAMS ((struct path_prefix *, const char *,
290 const char *, int, int, int *));
291 static void translate_options PARAMS ((int *, const char *const **));
292 static char *skip_whitespace PARAMS ((char *));
293 static void delete_if_ordinary PARAMS ((const char *));
294 static void delete_temp_files PARAMS ((void));
295 static void delete_failure_queue PARAMS ((void));
296 static void clear_failure_queue PARAMS ((void));
297 static int check_live_switch PARAMS ((int, int));
298 static const char *handle_braces PARAMS ((const char *));
299 static char *save_string PARAMS ((const char *, int));
300 static void set_collect_gcc_options PARAMS ((void));
301 static int do_spec_1 PARAMS ((const char *, int, const char *));
302 static int do_spec_2 PARAMS ((const char *));
303 static const char *find_file PARAMS ((const char *));
304 static int is_directory PARAMS ((const char *, const char *, int));
305 static void validate_switches PARAMS ((const char *));
306 static void validate_all_switches PARAMS ((void));
307 static void give_switch PARAMS ((int, int, int));
308 static int used_arg PARAMS ((const char *, int));
309 static int default_arg PARAMS ((const char *, int));
310 static void set_multilib_dir PARAMS ((void));
311 static void print_multilib_info PARAMS ((void));
312 static void perror_with_name PARAMS ((const char *));
313 static void pfatal_pexecute PARAMS ((const char *, const char *))
314 ATTRIBUTE_NORETURN;
315 static void notice PARAMS ((const char *, ...))
316 ATTRIBUTE_PRINTF_1;
317 static void display_help PARAMS ((void));
318 static void add_preprocessor_option PARAMS ((const char *, int));
319 static void add_assembler_option PARAMS ((const char *, int));
320 static void add_linker_option PARAMS ((const char *, int));
321 static void process_command PARAMS ((int, const char *const *));
322 static int execute PARAMS ((void));
323 static void clear_args PARAMS ((void));
324 static void fatal_error PARAMS ((int));
325 #ifdef ENABLE_SHARED_LIBGCC
326 static void init_gcc_specs PARAMS ((struct obstack *,
327 const char *, const char *,
328 const char *));
329 #endif
330 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
331 static const char *convert_filename PARAMS ((const char *, int, int));
332 #endif
334 /* The Specs Language
336 Specs are strings containing lines, each of which (if not blank)
337 is made up of a program name, and arguments separated by spaces.
338 The program name must be exact and start from root, since no path
339 is searched and it is unreliable to depend on the current working directory.
340 Redirection of input or output is not supported; the subprograms must
341 accept filenames saying what files to read and write.
343 In addition, the specs can contain %-sequences to substitute variable text
344 or for conditional text. Here is a table of all defined %-sequences.
345 Note that spaces are not generated automatically around the results of
346 expanding these sequences; therefore, you can concatenate them together
347 or with constant text in a single argument.
349 %% substitute one % into the program name or argument.
350 %i substitute the name of the input file being processed.
351 %b substitute the basename of the input file being processed.
352 This is the substring up to (and not including) the last period
353 and not including the directory.
354 %B same as %b, but include the file suffix (text after the last period).
355 %gSUFFIX
356 substitute a file name that has suffix SUFFIX and is chosen
357 once per compilation, and mark the argument a la %d. To reduce
358 exposure to denial-of-service attacks, the file name is now
359 chosen in a way that is hard to predict even when previously
360 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
361 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
362 the regexp "[.A-Za-z]*%O"; "%O" is treated exactly as if it
363 had been pre-processed. Previously, %g was simply substituted
364 with a file name chosen once per compilation, without regard
365 to any appended suffix (which was therefore treated just like
366 ordinary text), making such attacks more likely to succeed.
367 %uSUFFIX
368 like %g, but generates a new temporary file name even if %uSUFFIX
369 was already seen.
370 %USUFFIX
371 substitutes the last file name generated with %uSUFFIX, generating a
372 new one if there is no such last file name. In the absence of any
373 %uSUFFIX, this is just like %gSUFFIX, except they don't share
374 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
375 would involve the generation of two distinct file names, one
376 for each `%g.s' and another for each `%U.s'. Previously, %U was
377 simply substituted with a file name chosen for the previous %u,
378 without regard to any appended suffix.
379 %jSUFFIX
380 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
381 writable, and if save-temps is off; otherwise, substitute the name
382 of a temporary file, just like %u. This temporary file is not
383 meant for communication between processes, but rather as a junk
384 disposal mechanism.
385 %.SUFFIX
386 substitutes .SUFFIX for the suffixes of a matched switch's args when
387 it is subsequently output with %*. SUFFIX is terminated by the next
388 space or %.
389 %d marks the argument containing or following the %d as a
390 temporary file name, so that that file will be deleted if CC exits
391 successfully. Unlike %g, this contributes no text to the argument.
392 %w marks the argument containing or following the %w as the
393 "output file" of this compilation. This puts the argument
394 into the sequence of arguments that %o will substitute later.
395 %W{...}
396 like %{...} but mark last argument supplied within
397 as a file to be deleted on failure.
398 %o substitutes the names of all the output files, with spaces
399 automatically placed around them. You should write spaces
400 around the %o as well or the results are undefined.
401 %o is for use in the specs for running the linker.
402 Input files whose names have no recognized suffix are not compiled
403 at all, but they are included among the output files, so they will
404 be linked.
405 %O substitutes the suffix for object files. Note that this is
406 handled specially when it immediately follows %g, %u, or %U
407 (with or without a suffix argument) because of the need for
408 those to form complete file names. The handling is such that
409 %O is treated exactly as if it had already been substituted,
410 except that %g, %u, and %U do not currently support additional
411 SUFFIX characters following %O as they would following, for
412 example, `.o'.
413 %p substitutes the standard macro predefinitions for the
414 current target machine. Use this when running cpp.
415 %P like %p, but puts `__' before and after the name of each macro.
416 (Except macros that already have __.)
417 This is for ANSI C.
418 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
419 %s current argument is the name of a library or startup file of some sort.
420 Search for that file in a standard list of directories
421 and substitute the full name found.
422 %eSTR Print STR as an error message. STR is terminated by a newline.
423 Use this when inconsistent options are detected.
424 %nSTR Print STR as an notice. STR is terminated by a newline.
425 %x{OPTION} Accumulate an option for %X.
426 %X Output the accumulated linker options specified by compilations.
427 %Y Output the accumulated assembler options specified by compilations.
428 %Z Output the accumulated preprocessor options specified by compilations.
429 %v1 Substitute the major version number of GCC.
430 (For version 2.5.3, this is 2.)
431 %v2 Substitute the minor version number of GCC.
432 (For version 2.5.3, this is 5.)
433 %v3 Substitute the patch level number of GCC.
434 (For version 2.5.3, this is 3.)
435 %a process ASM_SPEC as a spec.
436 This allows config.h to specify part of the spec for running as.
437 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
438 used here. This can be used to run a post-processor after the
439 assembler has done its job.
440 %D Dump out a -L option for each directory in startfile_prefixes.
441 If multilib_dir is set, extra entries are generated with it affixed.
442 %l process LINK_SPEC as a spec.
443 %L process LIB_SPEC as a spec.
444 %G process LIBGCC_SPEC as a spec.
445 %M output multilib_dir with directory separators replaced with "_";
446 if multilib_dir is not set or is ".", output "".
447 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
448 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
449 %C process CPP_SPEC as a spec.
450 %1 process CC1_SPEC as a spec.
451 %2 process CC1PLUS_SPEC as a spec.
452 %| output "-" if the input for the current command is coming from a pipe.
453 %* substitute the variable part of a matched option. (See below.)
454 Note that each comma in the substituted string is replaced by
455 a single space.
456 %{S} substitutes the -S switch, if that switch was given to CC.
457 If that switch was not specified, this substitutes nothing.
458 Here S is a metasyntactic variable.
459 %{S*} substitutes all the switches specified to CC whose names start
460 with -S. This is used for -o, -I, etc; switches that take
461 arguments. CC considers `-o foo' as being one switch whose
462 name starts with `o'. %{o*} would substitute this text,
463 including the space; thus, two arguments would be generated.
464 %{^S*} likewise, but don't put a blank between a switch and any args.
465 %{S*&T*} likewise, but preserve order of S and T options (the order
466 of S and T in the spec is not significant). Can be any number
467 of ampersand-separated variables; for each the wild card is
468 optional. Useful for CPP as %{D*&U*&A*}.
469 %{S*:X} substitutes X if one or more switches whose names start with -S are
470 specified to CC. Note that the tail part of the -S option
471 (i.e. the part matched by the `*') will be substituted for each
472 occurrence of %* within X.
473 %{<S} remove all occurrences of -S from the command line.
474 Note - this option is position dependent. % commands in the
475 spec string before this option will see -S, % commands in the
476 spec string after this option will not.
477 %{S:X} substitutes X, but only if the -S switch was given to CC.
478 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
479 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
480 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
481 %{.S:X} substitutes X, but only if processing a file with suffix S.
482 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
483 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
484 combined with ! and . as above binding stronger than the OR.
485 %(Spec) processes a specification defined in a specs file as *Spec:
486 %[Spec] as above, but put __ around -D arguments
488 The conditional text X in a %{S:X} or %{!S:X} construct may contain
489 other nested % constructs or spaces, or even newlines. They are
490 processed as usual, as described above.
492 The -O, -f, -m, and -W switches are handled specifically in these
493 constructs. If another value of -O or the negated form of a -f, -m, or
494 -W switch is found later in the command line, the earlier switch
495 value is ignored, except with {S*} where S is just one letter; this
496 passes all matching options.
498 The character | at the beginning of the predicate text is used to indicate
499 that a command should be piped to the following command, but only if -pipe
500 is specified.
502 Note that it is built into CC which switches take arguments and which
503 do not. You might think it would be useful to generalize this to
504 allow each compiler's spec to say which switches take arguments. But
505 this cannot be done in a consistent fashion. CC cannot even decide
506 which input files have been specified without knowing which switches
507 take arguments, and it must know which input files to compile in order
508 to tell which compilers to run.
510 CC also knows implicitly that arguments starting in `-l' are to be
511 treated as compiler output files, and passed to the linker in their
512 proper position among the other output files. */
514 /* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
516 /* config.h can define ASM_SPEC to provide extra args to the assembler
517 or extra switch-translations. */
518 #ifndef ASM_SPEC
519 #define ASM_SPEC ""
520 #endif
522 /* config.h can define ASM_FINAL_SPEC to run a post processor after
523 the assembler has run. */
524 #ifndef ASM_FINAL_SPEC
525 #define ASM_FINAL_SPEC ""
526 #endif
528 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
529 or extra switch-translations. */
530 #ifndef CPP_SPEC
531 #define CPP_SPEC ""
532 #endif
534 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
535 or extra switch-translations. */
536 #ifndef CC1_SPEC
537 #define CC1_SPEC ""
538 #endif
540 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
541 or extra switch-translations. */
542 #ifndef CC1PLUS_SPEC
543 #define CC1PLUS_SPEC ""
544 #endif
546 /* config.h can define LINK_SPEC to provide extra args to the linker
547 or extra switch-translations. */
548 #ifndef LINK_SPEC
549 #define LINK_SPEC ""
550 #endif
552 /* config.h can define LIB_SPEC to override the default libraries. */
553 #ifndef LIB_SPEC
554 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
555 #endif
557 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
558 included. */
559 #ifndef LIBGCC_SPEC
560 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
561 /* Have gcc do the search for libgcc.a. */
562 #define LIBGCC_SPEC "libgcc.a%s"
563 #else
564 #define LIBGCC_SPEC "-lgcc"
565 #endif
566 #endif
568 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
569 #ifndef STARTFILE_SPEC
570 #define STARTFILE_SPEC \
571 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
572 #endif
574 /* config.h can define SWITCHES_NEED_SPACES to control which options
575 require spaces between the option and the argument. */
576 #ifndef SWITCHES_NEED_SPACES
577 #define SWITCHES_NEED_SPACES ""
578 #endif
580 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
581 #ifndef ENDFILE_SPEC
582 #define ENDFILE_SPEC ""
583 #endif
585 #ifndef LINKER_NAME
586 #define LINKER_NAME "collect2"
587 #endif
589 /* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
590 to the assembler. */
591 #ifndef ASM_DEBUG_SPEC
592 # if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
593 && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
594 # define ASM_DEBUG_SPEC \
595 (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG \
596 ? "%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}" \
597 : "%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}")
598 # else
599 # if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
600 # define ASM_DEBUG_SPEC "%{g*:--gstabs}"
601 # endif
602 # if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
603 # define ASM_DEBUG_SPEC "%{g*:--gdwarf2}"
604 # endif
605 # endif
606 #endif
607 #ifndef ASM_DEBUG_SPEC
608 # define ASM_DEBUG_SPEC ""
609 #endif
611 /* Here is the spec for running the linker, after compiling all files. */
613 /* This is overridable by the target in case they need to specify the
614 -lgcc and -lc order specially, yet not require them to override all
615 of LINK_COMMAND_SPEC. */
616 #ifndef LINK_GCC_C_SEQUENCE_SPEC
617 #define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
618 #endif
620 /* -u* was put back because both BSD and SysV seem to support it. */
621 /* %{static:} simply prevents an error message if the target machine
622 doesn't handle -static. */
623 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
624 scripts which exist in user specified directories, or in standard
625 directories. */
626 #ifndef LINK_COMMAND_SPEC
627 #define LINK_COMMAND_SPEC "\
628 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
629 %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\
630 %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
631 %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}\
632 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
633 #endif
635 #ifndef LINK_LIBGCC_SPEC
636 # ifdef LINK_LIBGCC_SPECIAL
637 /* Don't generate -L options for startfile prefix list. */
638 # define LINK_LIBGCC_SPEC ""
639 # else
640 /* Do generate them. */
641 # define LINK_LIBGCC_SPEC "%D"
642 # endif
643 #endif
645 #ifndef STARTFILE_PREFIX_SPEC
646 # define STARTFILE_PREFIX_SPEC ""
647 #endif
649 static const char *asm_debug = ASM_DEBUG_SPEC;
650 static const char *cpp_spec = CPP_SPEC;
651 static const char *cpp_predefines = CPP_PREDEFINES;
652 static const char *cc1_spec = CC1_SPEC;
653 static const char *cc1plus_spec = CC1PLUS_SPEC;
654 static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
655 static const char *asm_spec = ASM_SPEC;
656 static const char *asm_final_spec = ASM_FINAL_SPEC;
657 static const char *link_spec = LINK_SPEC;
658 static const char *lib_spec = LIB_SPEC;
659 static const char *libgcc_spec = LIBGCC_SPEC;
660 static const char *endfile_spec = ENDFILE_SPEC;
661 static const char *startfile_spec = STARTFILE_SPEC;
662 static const char *switches_need_spaces = SWITCHES_NEED_SPACES;
663 static const char *linker_name_spec = LINKER_NAME;
664 static const char *link_command_spec = LINK_COMMAND_SPEC;
665 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
666 static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
668 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
669 There should be no need to override these in target dependent files,
670 but we need to copy them to the specs file so that newer versions
671 of the GCC driver can correctly drive older tool chains with the
672 appropriate -B options. */
674 /* When cpplib handles traditional preprocessing, get rid of this, and
675 call cc1 (or cc1obj in objc/lang-specs.h) from the main specs so
676 that we default the front end language better. */
677 static const char *trad_capable_cpp =
678 "%{traditional|ftraditional|traditional-cpp:tradcpp0}\
679 %{!traditional:%{!ftraditional:%{!traditional-cpp:cc1 -E}}}";
681 static const char *cpp_unique_options =
682 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
683 %{CC:%{!E:%eGNU C does not support -CC without using -E}}\
684 %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*} %{P} %{$} %I\
685 %{MD:-MD %W{!o: %b.d}%W{o*:%.d%*}}\
686 %{MMD:-MMD %W{!o: %b.d}%W{o*:%.d%*}}\
687 %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
688 %{!E:%{!M:%{!MM:%{MD|MMD:%{o*:-MQ %*}}}}}\
689 %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
690 %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
691 %{remap} %{g3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i\
692 %{E|M|MM:%W{o*}}";
694 /* This contains cpp options which are common with cc1_options and are passed
695 only when preprocessing only to avoid duplication. */
696 static const char *cpp_options =
697 "%(cpp_unique_options) %{std*} %{d*} %{W*&pedantic*} %{w} %{m*} %{f*}\
698 %{O*} %{undef}";
700 /* NB: This is shared amongst all front-ends. */
701 static const char *cc1_options =
702 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
703 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
704 %{g*} %{O*} %{W*&pedantic*} %{w} %{std*} %{ansi}\
705 %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
706 %{Qn:-fno-ident} %{--help:--help}\
707 %{--target-help:--target-help}\
708 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
709 %{fsyntax-only:-o %j} %{-param*}";
711 static const char *asm_options =
712 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
714 static const char *invoke_as =
715 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
717 /* Some compilers have limits on line lengths, and the multilib_select
718 and/or multilib_matches strings can be very long, so we build them at
719 run time. */
720 static struct obstack multilib_obstack;
721 static const char *multilib_select;
722 static const char *multilib_matches;
723 static const char *multilib_defaults;
724 static const char *multilib_exclusions;
725 #include "multilib.h"
727 /* Check whether a particular argument is a default argument. */
729 #ifndef MULTILIB_DEFAULTS
730 #define MULTILIB_DEFAULTS { "" }
731 #endif
733 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
735 struct user_specs
737 struct user_specs *next;
738 const char *filename;
741 static struct user_specs *user_specs_head, *user_specs_tail;
743 /* This defines which switch letters take arguments. */
745 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
746 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
747 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
748 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
749 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'B' || (CHAR) == 'b')
751 #ifndef SWITCH_TAKES_ARG
752 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
753 #endif
755 /* This defines which multi-letter switches take arguments. */
757 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
758 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
759 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
760 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
761 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
762 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
763 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
764 || !strcmp (STR, "specs") \
765 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
767 #ifndef WORD_SWITCH_TAKES_ARG
768 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
769 #endif
771 #ifdef HAVE_TARGET_EXECUTABLE_SUFFIX
772 /* This defines which switches stop a full compilation. */
773 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
774 ((CHAR) == 'c' || (CHAR) == 'S')
776 #ifndef SWITCH_CURTAILS_COMPILATION
777 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
778 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
779 #endif
780 #endif
782 /* Record the mapping from file suffixes for compilation specs. */
784 struct compiler
786 const char *suffix; /* Use this compiler for input files
787 whose names end in this suffix. */
789 const char *spec; /* To use this compiler, run this spec. */
791 const char *cpp_spec; /* If non-NULL, substitute this spec
792 for `%C', rather than the usual
793 cpp_spec. */
796 /* Pointer to a vector of `struct compiler' that gives the spec for
797 compiling a file, based on its suffix.
798 A file that does not end in any of these suffixes will be passed
799 unchanged to the loader and nothing else will be done to it.
801 An entry containing two 0s is used to terminate the vector.
803 If multiple entries match a file, the last matching one is used. */
805 static struct compiler *compilers;
807 /* Number of entries in `compilers', not counting the null terminator. */
809 static int n_compilers;
811 /* The default list of file name suffixes and their compilation specs. */
813 static const struct compiler default_compilers[] =
815 /* Add lists of suffixes of known languages here. If those languages
816 were not present when we built the driver, we will hit these copies
817 and be given a more meaningful error than "file not used since
818 linking is not done". */
819 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
820 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
821 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
822 {".ii", "#C++", 0},
823 {".ads", "#Ada", 0}, {".adb", "#Ada", 0},
824 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
825 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
826 {".r", "#Ratfor", 0},
827 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
828 {".java", "#Java", 0}, {".class", "#Java", 0},
829 {".zip", "#Java", 0}, {".jar", "#Java", 0},
830 /* Next come the entries for C. */
831 {".c", "@c", 0},
832 {"@c",
833 /* cc1 has an integrated ISO C preprocessor. We should invoke the
834 external preprocessor if -save-temps is given. */
835 "%{E|M|MM:%(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options)}\
836 %{!E:%{!M:%{!MM:\
837 %{traditional|ftraditional:\
838 %eGNU C no longer supports -traditional without -E}\
839 %{save-temps|traditional-cpp:%(trad_capable_cpp) \
840 %{ansi:-std=c89} %(cpp_options) %b.i \n\
841 cc1 -fpreprocessed %b.i %(cc1_options)}\
842 %{!save-temps:%{!traditional-cpp:\
843 cc1 %{ansi:-std=c89} %(cpp_unique_options) %(cc1_options)}}\
844 %{!fsyntax-only:%(invoke_as)}}}}", 0},
845 {"-",
846 "%{!E:%e-E required when input is from standard input}\
847 %(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options)", 0},
848 {".h", "@c-header", 0},
849 {"@c-header",
850 "%{!E:%ecompilation of header file requested} \
851 %(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options)", 0},
852 {".i", "@cpp-output", 0},
853 {"@cpp-output",
854 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
855 {".s", "@assembler", 0},
856 {"@assembler",
857 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0},
858 {".S", "@assembler-with-cpp", 0},
859 {"@assembler-with-cpp",
860 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
861 %{!M:%{!MM:%{!E:%{!S:-o %{|!pipe:%g.s} |\n\
862 as %(asm_debug) %(asm_options) %{!pipe:%g.s} %A }}}}", 0},
863 #include "specs.h"
864 /* Mark end of table */
865 {0, 0, 0}
868 /* Number of elements in default_compilers, not counting the terminator. */
870 static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
872 /* A vector of options to give to the linker.
873 These options are accumulated by %x,
874 and substituted into the linker command with %X. */
875 static int n_linker_options;
876 static char **linker_options;
878 /* A vector of options to give to the assembler.
879 These options are accumulated by -Wa,
880 and substituted into the assembler command with %Y. */
881 static int n_assembler_options;
882 static char **assembler_options;
884 /* A vector of options to give to the preprocessor.
885 These options are accumulated by -Wp,
886 and substituted into the preprocessor command with %Z. */
887 static int n_preprocessor_options;
888 static char **preprocessor_options;
890 /* Define how to map long options into short ones. */
892 /* This structure describes one mapping. */
893 struct option_map
895 /* The long option's name. */
896 const char *const name;
897 /* The equivalent short option. */
898 const char *const equivalent;
899 /* Argument info. A string of flag chars; NULL equals no options.
900 a => argument required.
901 o => argument optional.
902 j => join argument to equivalent, making one word.
903 * => require other text after NAME as an argument. */
904 const char *const arg_info;
907 /* This is the table of mappings. Mappings are tried sequentially
908 for each option encountered; the first one that matches, wins. */
910 static const struct option_map option_map[] =
912 {"--all-warnings", "-Wall", 0},
913 {"--ansi", "-ansi", 0},
914 {"--assemble", "-S", 0},
915 {"--assert", "-A", "a"},
916 {"--classpath", "-fclasspath=", "aj"},
917 {"--bootclasspath", "-fbootclasspath=", "aj"},
918 {"--CLASSPATH", "-fclasspath=", "aj"},
919 {"--comments", "-C", 0},
920 {"--comments-in-macros", "-CC", 0},
921 {"--compile", "-c", 0},
922 {"--debug", "-g", "oj"},
923 {"--define-macro", "-D", "aj"},
924 {"--dependencies", "-M", 0},
925 {"--dump", "-d", "a"},
926 {"--dumpbase", "-dumpbase", "a"},
927 {"--entry", "-e", 0},
928 {"--extra-warnings", "-W", 0},
929 {"--for-assembler", "-Wa", "a"},
930 {"--for-linker", "-Xlinker", "a"},
931 {"--force-link", "-u", "a"},
932 {"--imacros", "-imacros", "a"},
933 {"--include", "-include", "a"},
934 {"--include-barrier", "-I-", 0},
935 {"--include-directory", "-I", "aj"},
936 {"--include-directory-after", "-idirafter", "a"},
937 {"--include-prefix", "-iprefix", "a"},
938 {"--include-with-prefix", "-iwithprefix", "a"},
939 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
940 {"--include-with-prefix-after", "-iwithprefix", "a"},
941 {"--language", "-x", "a"},
942 {"--library-directory", "-L", "a"},
943 {"--machine", "-m", "aj"},
944 {"--machine-", "-m", "*j"},
945 {"--no-line-commands", "-P", 0},
946 {"--no-precompiled-includes", "-noprecomp", 0},
947 {"--no-standard-includes", "-nostdinc", 0},
948 {"--no-standard-libraries", "-nostdlib", 0},
949 {"--no-warnings", "-w", 0},
950 {"--optimize", "-O", "oj"},
951 {"--output", "-o", "a"},
952 {"--output-class-directory", "-foutput-class-dir=", "ja"},
953 {"--param", "--param", "a"},
954 {"--pedantic", "-pedantic", 0},
955 {"--pedantic-errors", "-pedantic-errors", 0},
956 {"--pipe", "-pipe", 0},
957 {"--prefix", "-B", "a"},
958 {"--preprocess", "-E", 0},
959 {"--print-search-dirs", "-print-search-dirs", 0},
960 {"--print-file-name", "-print-file-name=", "aj"},
961 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
962 {"--print-missing-file-dependencies", "-MG", 0},
963 {"--print-multi-lib", "-print-multi-lib", 0},
964 {"--print-multi-directory", "-print-multi-directory", 0},
965 {"--print-prog-name", "-print-prog-name=", "aj"},
966 {"--profile", "-p", 0},
967 {"--profile-blocks", "-a", 0},
968 {"--quiet", "-q", 0},
969 {"--resource", "-fcompile-resource=", "aj"},
970 {"--save-temps", "-save-temps", 0},
971 {"--shared", "-shared", 0},
972 {"--silent", "-q", 0},
973 {"--specs", "-specs=", "aj"},
974 {"--static", "-static", 0},
975 {"--std", "-std=", "aj"},
976 {"--symbolic", "-symbolic", 0},
977 {"--target", "-b", "a"},
978 {"--time", "-time", 0},
979 {"--trace-includes", "-H", 0},
980 {"--traditional", "-traditional", 0},
981 {"--traditional-cpp", "-traditional-cpp", 0},
982 {"--trigraphs", "-trigraphs", 0},
983 {"--undefine-macro", "-U", "aj"},
984 {"--use-version", "-V", "a"},
985 {"--user-dependencies", "-MM", 0},
986 {"--verbose", "-v", 0},
987 {"--warn-", "-W", "*j"},
988 {"--write-dependencies", "-MD", 0},
989 {"--write-user-dependencies", "-MMD", 0},
990 {"--", "-f", "*j"}
994 #ifdef TARGET_OPTION_TRANSLATE_TABLE
995 static const struct {
996 const char *const option_found;
997 const char *const replacements;
998 } target_option_translations[] =
1000 TARGET_OPTION_TRANSLATE_TABLE,
1001 { 0, 0 }
1003 #endif
1005 /* Translate the options described by *ARGCP and *ARGVP.
1006 Make a new vector and store it back in *ARGVP,
1007 and store its length in *ARGVC. */
1009 static void
1010 translate_options (argcp, argvp)
1011 int *argcp;
1012 const char *const **argvp;
1014 int i;
1015 int argc = *argcp;
1016 const char *const *argv = *argvp;
1017 int newvsize = (argc + 2) * 2 * sizeof (const char *);
1018 const char **newv =
1019 (const char **) xmalloc (newvsize);
1020 int newindex = 0;
1022 i = 0;
1023 newv[newindex++] = argv[i++];
1025 while (i < argc)
1027 #ifdef TARGET_OPTION_TRANSLATE_TABLE
1028 int tott_idx;
1030 for (tott_idx = 0;
1031 target_option_translations[tott_idx].option_found;
1032 tott_idx++)
1034 if (strcmp (target_option_translations[tott_idx].option_found,
1035 argv[i]) == 0)
1037 int spaces = 1;
1038 const char *sp;
1039 char *np;
1041 for (sp = target_option_translations[tott_idx].replacements;
1042 *sp; sp++)
1044 if (*sp == ' ')
1045 spaces ++;
1048 newvsize += spaces * sizeof (const char *);
1049 newv = (const char **) xrealloc (newv, newvsize);
1051 sp = target_option_translations[tott_idx].replacements;
1052 np = xstrdup (sp);
1054 while (1)
1056 while (*np == ' ')
1057 np++;
1058 if (*np == 0)
1059 break;
1060 newv[newindex++] = np;
1061 while (*np != ' ' && *np)
1062 np++;
1063 if (*np == 0)
1064 break;
1065 *np++ = 0;
1068 i ++;
1069 break;
1072 if (target_option_translations[tott_idx].option_found)
1073 continue;
1074 #endif
1076 /* Translate -- options. */
1077 if (argv[i][0] == '-' && argv[i][1] == '-')
1079 size_t j;
1080 /* Find a mapping that applies to this option. */
1081 for (j = 0; j < ARRAY_SIZE (option_map); j++)
1083 size_t optlen = strlen (option_map[j].name);
1084 size_t arglen = strlen (argv[i]);
1085 size_t complen = arglen > optlen ? optlen : arglen;
1086 const char *arginfo = option_map[j].arg_info;
1088 if (arginfo == 0)
1089 arginfo = "";
1091 if (!strncmp (argv[i], option_map[j].name, complen))
1093 const char *arg = 0;
1095 if (arglen < optlen)
1097 size_t k;
1098 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
1099 if (strlen (option_map[k].name) >= arglen
1100 && !strncmp (argv[i], option_map[k].name, arglen))
1102 error ("ambiguous abbreviation %s", argv[i]);
1103 break;
1106 if (k != ARRAY_SIZE (option_map))
1107 break;
1110 if (arglen > optlen)
1112 /* If the option has an argument, accept that. */
1113 if (argv[i][optlen] == '=')
1114 arg = argv[i] + optlen + 1;
1116 /* If this mapping requires extra text at end of name,
1117 accept that as "argument". */
1118 else if (strchr (arginfo, '*') != 0)
1119 arg = argv[i] + optlen;
1121 /* Otherwise, extra text at end means mismatch.
1122 Try other mappings. */
1123 else
1124 continue;
1127 else if (strchr (arginfo, '*') != 0)
1129 error ("incomplete `%s' option", option_map[j].name);
1130 break;
1133 /* Handle arguments. */
1134 if (strchr (arginfo, 'a') != 0)
1136 if (arg == 0)
1138 if (i + 1 == argc)
1140 error ("missing argument to `%s' option",
1141 option_map[j].name);
1142 break;
1145 arg = argv[++i];
1148 else if (strchr (arginfo, '*') != 0)
1150 else if (strchr (arginfo, 'o') == 0)
1152 if (arg != 0)
1153 error ("extraneous argument to `%s' option",
1154 option_map[j].name);
1155 arg = 0;
1158 /* Store the translation as one argv elt or as two. */
1159 if (arg != 0 && strchr (arginfo, 'j') != 0)
1160 newv[newindex++] = concat (option_map[j].equivalent, arg,
1161 NULL);
1162 else if (arg != 0)
1164 newv[newindex++] = option_map[j].equivalent;
1165 newv[newindex++] = arg;
1167 else
1168 newv[newindex++] = option_map[j].equivalent;
1170 break;
1173 i++;
1176 /* Handle old-fashioned options--just copy them through,
1177 with their arguments. */
1178 else if (argv[i][0] == '-')
1180 const char *p = argv[i] + 1;
1181 int c = *p;
1182 int nskip = 1;
1184 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1185 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1186 else if (WORD_SWITCH_TAKES_ARG (p))
1187 nskip += WORD_SWITCH_TAKES_ARG (p);
1188 else if ((c == 'B' || c == 'b' || c == 'x')
1189 && p[1] == 0)
1190 nskip += 1;
1191 else if (! strcmp (p, "Xlinker"))
1192 nskip += 1;
1194 /* Watch out for an option at the end of the command line that
1195 is missing arguments, and avoid skipping past the end of the
1196 command line. */
1197 if (nskip + i > argc)
1198 nskip = argc - i;
1200 while (nskip > 0)
1202 newv[newindex++] = argv[i++];
1203 nskip--;
1206 else
1207 /* Ordinary operands, or +e options. */
1208 newv[newindex++] = argv[i++];
1211 newv[newindex] = 0;
1213 *argvp = newv;
1214 *argcp = newindex;
1217 static char *
1218 skip_whitespace (p)
1219 char *p;
1221 while (1)
1223 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1224 be considered whitespace. */
1225 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1226 return p + 1;
1227 else if (*p == '\n' || *p == ' ' || *p == '\t')
1228 p++;
1229 else if (*p == '#')
1231 while (*p != '\n')
1232 p++;
1233 p++;
1235 else
1236 break;
1239 return p;
1241 /* Structures to keep track of prefixes to try when looking for files. */
1243 struct prefix_list
1245 const char *prefix; /* String to prepend to the path. */
1246 struct prefix_list *next; /* Next in linked list. */
1247 int require_machine_suffix; /* Don't use without machine_suffix. */
1248 /* 2 means try both machine_suffix and just_machine_suffix. */
1249 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1250 int priority; /* Sort key - priority within list */
1253 struct path_prefix
1255 struct prefix_list *plist; /* List of prefixes to try */
1256 int max_len; /* Max length of a prefix in PLIST */
1257 const char *name; /* Name of this list (used in config stuff) */
1260 /* List of prefixes to try when looking for executables. */
1262 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1264 /* List of prefixes to try when looking for startup (crt0) files. */
1266 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1268 /* List of prefixes to try when looking for include files. */
1270 static struct path_prefix include_prefixes = { 0, 0, "include" };
1272 /* Suffix to attach to directories searched for commands.
1273 This looks like `MACHINE/VERSION/'. */
1275 static const char *machine_suffix = 0;
1277 /* Suffix to attach to directories searched for commands.
1278 This is just `MACHINE/'. */
1280 static const char *just_machine_suffix = 0;
1282 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1284 static const char *gcc_exec_prefix;
1286 /* Default prefixes to attach to command names. */
1288 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1289 #undef MD_EXEC_PREFIX
1290 #undef MD_STARTFILE_PREFIX
1291 #undef MD_STARTFILE_PREFIX_1
1292 #endif
1294 /* If no prefixes defined, use the null string, which will disable them. */
1295 #ifndef MD_EXEC_PREFIX
1296 #define MD_EXEC_PREFIX ""
1297 #endif
1298 #ifndef MD_STARTFILE_PREFIX
1299 #define MD_STARTFILE_PREFIX ""
1300 #endif
1301 #ifndef MD_STARTFILE_PREFIX_1
1302 #define MD_STARTFILE_PREFIX_1 ""
1303 #endif
1305 /* Supply defaults for the standard prefixes. */
1307 #ifndef STANDARD_EXEC_PREFIX
1308 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1309 #endif
1310 #ifndef STANDARD_STARTFILE_PREFIX
1311 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1312 #endif
1313 #ifndef TOOLDIR_BASE_PREFIX
1314 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1315 #endif
1316 #ifndef STANDARD_BINDIR_PREFIX
1317 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1318 #endif
1320 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1321 static const char *const standard_exec_prefix_1 = "/usr/lib/gcc/";
1322 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1324 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1325 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1326 static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1327 static const char *const standard_startfile_prefix_1 = "/lib/";
1328 static const char *const standard_startfile_prefix_2 = "/usr/lib/";
1330 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1331 static const char *tooldir_prefix;
1333 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1335 /* Subdirectory to use for locating libraries. Set by
1336 set_multilib_dir based on the compilation options. */
1338 static const char *multilib_dir;
1340 /* Structure to keep track of the specs that have been defined so far.
1341 These are accessed using %(specname) or %[specname] in a compiler
1342 or link spec. */
1344 struct spec_list
1346 /* The following 2 fields must be first */
1347 /* to allow EXTRA_SPECS to be initialized */
1348 const char *name; /* name of the spec. */
1349 const char *ptr; /* available ptr if no static pointer */
1351 /* The following fields are not initialized */
1352 /* by EXTRA_SPECS */
1353 const char **ptr_spec; /* pointer to the spec itself. */
1354 struct spec_list *next; /* Next spec in linked list. */
1355 int name_len; /* length of the name */
1356 int alloc_p; /* whether string was allocated */
1359 #define INIT_STATIC_SPEC(NAME,PTR) \
1360 { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1362 /* List of statically defined specs. */
1363 static struct spec_list static_specs[] =
1365 INIT_STATIC_SPEC ("asm", &asm_spec),
1366 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
1367 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1368 INIT_STATIC_SPEC ("asm_options", &asm_options),
1369 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1370 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1371 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1372 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
1373 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1374 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1375 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1376 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1377 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
1378 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1379 INIT_STATIC_SPEC ("link", &link_spec),
1380 INIT_STATIC_SPEC ("lib", &lib_spec),
1381 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1382 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1383 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1384 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1385 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1386 INIT_STATIC_SPEC ("version", &compiler_version),
1387 INIT_STATIC_SPEC ("multilib", &multilib_select),
1388 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1389 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1390 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1391 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1392 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1393 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1394 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1395 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1396 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1397 INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
1400 #ifdef EXTRA_SPECS /* additional specs needed */
1401 /* Structure to keep track of just the first two args of a spec_list.
1402 That is all that the EXTRA_SPECS macro gives us. */
1403 struct spec_list_1
1405 const char *const name;
1406 const char *const ptr;
1409 static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1410 static struct spec_list *extra_specs = (struct spec_list *) 0;
1411 #endif
1413 /* List of dynamically allocates specs that have been defined so far. */
1415 static struct spec_list *specs = (struct spec_list *) 0;
1417 /* Add appropriate libgcc specs to OBSTACK, taking into account
1418 various permutations of -shared-libgcc, -shared, and such. */
1420 #ifdef ENABLE_SHARED_LIBGCC
1421 static void
1422 init_gcc_specs (obstack, shared_name, static_name, eh_name)
1423 struct obstack *obstack;
1424 const char *shared_name;
1425 const char *static_name;
1426 const char *eh_name;
1428 char *buf;
1430 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
1431 "}%{!static:%{!static-libgcc:",
1432 "%{!shared:%{!shared-libgcc:", static_name, " ",
1433 eh_name, "}%{shared-libgcc:", shared_name, " ",
1434 static_name, "}}%{shared:",
1435 #ifdef LINK_EH_SPEC
1436 "%{shared-libgcc:", shared_name,
1437 "}%{!shared-libgcc:", static_name, "}",
1438 #else
1439 shared_name,
1440 #endif
1441 "}}}", NULL);
1443 obstack_grow (obstack, buf, strlen (buf));
1444 free (buf);
1446 #endif /* ENABLE_SHARED_LIBGCC */
1448 /* Initialize the specs lookup routines. */
1450 static void
1451 init_spec ()
1453 struct spec_list *next = (struct spec_list *) 0;
1454 struct spec_list *sl = (struct spec_list *) 0;
1455 int i;
1457 if (specs)
1458 return; /* Already initialized. */
1460 if (verbose_flag)
1461 notice ("Using built-in specs.\n");
1463 #ifdef EXTRA_SPECS
1464 extra_specs = (struct spec_list *)
1465 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1467 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1469 sl = &extra_specs[i];
1470 sl->name = extra_specs_1[i].name;
1471 sl->ptr = extra_specs_1[i].ptr;
1472 sl->next = next;
1473 sl->name_len = strlen (sl->name);
1474 sl->ptr_spec = &sl->ptr;
1475 next = sl;
1477 #endif
1479 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1481 sl = &static_specs[i];
1482 sl->next = next;
1483 next = sl;
1486 #ifdef ENABLE_SHARED_LIBGCC
1487 /* ??? If neither -shared-libgcc nor --static-libgcc was
1488 seen, then we should be making an educated guess. Some proposed
1489 heuristics for ELF include:
1491 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1492 program will be doing dynamic loading, which will likely
1493 need the shared libgcc.
1495 (2) If "-ldl", then it's also a fair bet that we're doing
1496 dynamic loading.
1498 (3) For each ET_DYN we're linking against (either through -lfoo
1499 or /some/path/foo.so), check to see whether it or one of
1500 its dependencies depends on a shared libgcc.
1502 (4) If "-shared"
1504 If the runtime is fixed to look for program headers instead
1505 of calling __register_frame_info at all, for each object,
1506 use the shared libgcc if any EH symbol referenced.
1508 If crtstuff is fixed to not invoke __register_frame_info
1509 automatically, for each object, use the shared libgcc if
1510 any non-empty unwind section found.
1512 Doing any of this probably requires invoking an external program to
1513 do the actual object file scanning. */
1515 const char *p = libgcc_spec;
1516 int in_sep = 1;
1518 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1519 when given the proper command line arguments. */
1520 while (*p)
1522 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1524 init_gcc_specs (&obstack,
1525 #ifdef NO_SHARED_LIBGCC_MULTILIB
1526 "-lgcc_s"
1527 #else
1528 "-lgcc_s%M"
1529 #endif
1531 "-lgcc",
1532 "-lgcc_eh");
1533 p += 5;
1534 in_sep = 0;
1536 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1538 /* Ug. We don't know shared library extensions. Hope that
1539 systems that use this form don't do shared libraries. */
1540 init_gcc_specs (&obstack,
1541 #ifdef NO_SHARED_LIBGCC_MULTILIB
1542 "-lgcc_s"
1543 #else
1544 "-lgcc_s%M"
1545 #endif
1547 "libgcc.a%s",
1548 "libgcc_eh.a%s");
1549 p += 10;
1550 in_sep = 0;
1552 else
1554 obstack_1grow (&obstack, *p);
1555 in_sep = (*p == ' ');
1556 p += 1;
1560 obstack_1grow (&obstack, '\0');
1561 libgcc_spec = obstack_finish (&obstack);
1563 #endif
1564 #ifdef USE_AS_TRADITIONAL_FORMAT
1565 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1567 static const char tf[] = "--traditional-format ";
1568 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1569 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1570 asm_spec = obstack_finish (&obstack);
1572 #endif
1573 #ifdef LINK_EH_SPEC
1574 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1575 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
1576 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
1577 link_spec = obstack_finish (&obstack);
1578 #endif
1580 specs = sl;
1583 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1584 removed; If the spec starts with a + then SPEC is added to the end of the
1585 current spec. */
1587 static void
1588 set_spec (name, spec)
1589 const char *name;
1590 const char *spec;
1592 struct spec_list *sl;
1593 const char *old_spec;
1594 int name_len = strlen (name);
1595 int i;
1597 /* If this is the first call, initialize the statically allocated specs. */
1598 if (!specs)
1600 struct spec_list *next = (struct spec_list *) 0;
1601 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1603 sl = &static_specs[i];
1604 sl->next = next;
1605 next = sl;
1607 specs = sl;
1610 /* See if the spec already exists. */
1611 for (sl = specs; sl; sl = sl->next)
1612 if (name_len == sl->name_len && !strcmp (sl->name, name))
1613 break;
1615 if (!sl)
1617 /* Not found - make it. */
1618 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1619 sl->name = xstrdup (name);
1620 sl->name_len = name_len;
1621 sl->ptr_spec = &sl->ptr;
1622 sl->alloc_p = 0;
1623 *(sl->ptr_spec) = "";
1624 sl->next = specs;
1625 specs = sl;
1628 old_spec = *(sl->ptr_spec);
1629 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1630 ? concat (old_spec, spec + 1, NULL)
1631 : xstrdup (spec));
1633 #ifdef DEBUG_SPECS
1634 if (verbose_flag)
1635 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1636 #endif
1638 /* Free the old spec. */
1639 if (old_spec && sl->alloc_p)
1640 free ((PTR) old_spec);
1642 sl->alloc_p = 1;
1645 /* Accumulate a command (program name and args), and run it. */
1647 /* Vector of pointers to arguments in the current line of specifications. */
1649 static const char **argbuf;
1651 /* Number of elements allocated in argbuf. */
1653 static int argbuf_length;
1655 /* Number of elements in argbuf currently in use (containing args). */
1657 static int argbuf_index;
1659 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1660 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1661 it here. */
1663 static struct temp_name {
1664 const char *suffix; /* suffix associated with the code. */
1665 int length; /* strlen (suffix). */
1666 int unique; /* Indicates whether %g or %u/%U was used. */
1667 const char *filename; /* associated filename. */
1668 int filename_length; /* strlen (filename). */
1669 struct temp_name *next;
1670 } *temp_names;
1672 /* Number of commands executed so far. */
1674 static int execution_count;
1676 /* Number of commands that exited with a signal. */
1678 static int signal_count;
1680 /* Name with which this program was invoked. */
1682 static const char *programname;
1684 /* Clear out the vector of arguments (after a command is executed). */
1686 static void
1687 clear_args ()
1689 argbuf_index = 0;
1692 /* Add one argument to the vector at the end.
1693 This is done when a space is seen or at the end of the line.
1694 If DELETE_ALWAYS is nonzero, the arg is a filename
1695 and the file should be deleted eventually.
1696 If DELETE_FAILURE is nonzero, the arg is a filename
1697 and the file should be deleted if this compilation fails. */
1699 static void
1700 store_arg (arg, delete_always, delete_failure)
1701 const char *arg;
1702 int delete_always, delete_failure;
1704 if (argbuf_index + 1 == argbuf_length)
1705 argbuf
1706 = (const char **) xrealloc (argbuf,
1707 (argbuf_length *= 2) * sizeof (const char *));
1709 argbuf[argbuf_index++] = arg;
1710 argbuf[argbuf_index] = 0;
1712 if (delete_always || delete_failure)
1713 record_temp_file (arg, delete_always, delete_failure);
1716 /* Load specs from a file name named FILENAME, replacing occurrences of
1717 various different types of line-endings, \r\n, \n\r and just \r, with
1718 a single \n. */
1720 static char *
1721 load_specs (filename)
1722 const char *filename;
1724 int desc;
1725 int readlen;
1726 struct stat statbuf;
1727 char *buffer;
1728 char *buffer_p;
1729 char *specs;
1730 char *specs_p;
1732 if (verbose_flag)
1733 notice ("Reading specs from %s\n", filename);
1735 /* Open and stat the file. */
1736 desc = open (filename, O_RDONLY, 0);
1737 if (desc < 0)
1738 pfatal_with_name (filename);
1739 if (stat (filename, &statbuf) < 0)
1740 pfatal_with_name (filename);
1742 /* Read contents of file into BUFFER. */
1743 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1744 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1745 if (readlen < 0)
1746 pfatal_with_name (filename);
1747 buffer[readlen] = 0;
1748 close (desc);
1750 specs = xmalloc (readlen + 1);
1751 specs_p = specs;
1752 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1754 int skip = 0;
1755 char c = *buffer_p;
1756 if (c == '\r')
1758 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1759 skip = 1;
1760 else if (*(buffer_p + 1) == '\n') /* \r\n */
1761 skip = 1;
1762 else /* \r */
1763 c = '\n';
1765 if (! skip)
1766 *specs_p++ = c;
1768 *specs_p = '\0';
1770 free (buffer);
1771 return (specs);
1774 /* Read compilation specs from a file named FILENAME,
1775 replacing the default ones.
1777 A suffix which starts with `*' is a definition for
1778 one of the machine-specific sub-specs. The "suffix" should be
1779 *asm, *cc1, *cpp, *link, *startfile, etc.
1780 The corresponding spec is stored in asm_spec, etc.,
1781 rather than in the `compilers' vector.
1783 Anything invalid in the file is a fatal error. */
1785 static void
1786 read_specs (filename, main_p)
1787 const char *filename;
1788 int main_p;
1790 char *buffer;
1791 char *p;
1793 buffer = load_specs (filename);
1795 /* Scan BUFFER for specs, putting them in the vector. */
1796 p = buffer;
1797 while (1)
1799 char *suffix;
1800 char *spec;
1801 char *in, *out, *p1, *p2, *p3;
1803 /* Advance P in BUFFER to the next nonblank nocomment line. */
1804 p = skip_whitespace (p);
1805 if (*p == 0)
1806 break;
1808 /* Is this a special command that starts with '%'? */
1809 /* Don't allow this for the main specs file, since it would
1810 encourage people to overwrite it. */
1811 if (*p == '%' && !main_p)
1813 p1 = p;
1814 while (*p && *p != '\n')
1815 p++;
1817 /* Skip '\n'. */
1818 p++;
1820 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1821 && (p1[sizeof "%include" - 1] == ' '
1822 || p1[sizeof "%include" - 1] == '\t'))
1824 char *new_filename;
1826 p1 += sizeof ("%include");
1827 while (*p1 == ' ' || *p1 == '\t')
1828 p1++;
1830 if (*p1++ != '<' || p[-2] != '>')
1831 fatal ("specs %%include syntax malformed after %ld characters",
1832 (long) (p1 - buffer + 1));
1834 p[-2] = '\0';
1835 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1836 read_specs (new_filename ? new_filename : p1, FALSE);
1837 continue;
1839 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1840 && (p1[sizeof "%include_noerr" - 1] == ' '
1841 || p1[sizeof "%include_noerr" - 1] == '\t'))
1843 char *new_filename;
1845 p1 += sizeof "%include_noerr";
1846 while (*p1 == ' ' || *p1 == '\t')
1847 p1++;
1849 if (*p1++ != '<' || p[-2] != '>')
1850 fatal ("specs %%include syntax malformed after %ld characters",
1851 (long) (p1 - buffer + 1));
1853 p[-2] = '\0';
1854 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1855 if (new_filename)
1856 read_specs (new_filename, FALSE);
1857 else if (verbose_flag)
1858 notice ("could not find specs file %s\n", p1);
1859 continue;
1861 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1862 && (p1[sizeof "%rename" - 1] == ' '
1863 || p1[sizeof "%rename" - 1] == '\t'))
1865 int name_len;
1866 struct spec_list *sl;
1867 struct spec_list *newsl;
1869 /* Get original name. */
1870 p1 += sizeof "%rename";
1871 while (*p1 == ' ' || *p1 == '\t')
1872 p1++;
1874 if (! ISALPHA ((unsigned char) *p1))
1875 fatal ("specs %%rename syntax malformed after %ld characters",
1876 (long) (p1 - buffer));
1878 p2 = p1;
1879 while (*p2 && !ISSPACE ((unsigned char) *p2))
1880 p2++;
1882 if (*p2 != ' ' && *p2 != '\t')
1883 fatal ("specs %%rename syntax malformed after %ld characters",
1884 (long) (p2 - buffer));
1886 name_len = p2 - p1;
1887 *p2++ = '\0';
1888 while (*p2 == ' ' || *p2 == '\t')
1889 p2++;
1891 if (! ISALPHA ((unsigned char) *p2))
1892 fatal ("specs %%rename syntax malformed after %ld characters",
1893 (long) (p2 - buffer));
1895 /* Get new spec name. */
1896 p3 = p2;
1897 while (*p3 && !ISSPACE ((unsigned char) *p3))
1898 p3++;
1900 if (p3 != p - 1)
1901 fatal ("specs %%rename syntax malformed after %ld characters",
1902 (long) (p3 - buffer));
1903 *p3 = '\0';
1905 for (sl = specs; sl; sl = sl->next)
1906 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1907 break;
1909 if (!sl)
1910 fatal ("specs %s spec was not found to be renamed", p1);
1912 if (strcmp (p1, p2) == 0)
1913 continue;
1915 for (newsl = specs; newsl; newsl = newsl->next)
1916 if (strcmp (newsl->name, p2) == 0)
1917 fatal ("%s: attempt to rename spec '%s' to already defined spec '%s'",
1918 filename, p1, p2);
1920 if (verbose_flag)
1922 notice ("rename spec %s to %s\n", p1, p2);
1923 #ifdef DEBUG_SPECS
1924 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1925 #endif
1928 set_spec (p2, *(sl->ptr_spec));
1929 if (sl->alloc_p)
1930 free ((PTR) *(sl->ptr_spec));
1932 *(sl->ptr_spec) = "";
1933 sl->alloc_p = 0;
1934 continue;
1936 else
1937 fatal ("specs unknown %% command after %ld characters",
1938 (long) (p1 - buffer));
1941 /* Find the colon that should end the suffix. */
1942 p1 = p;
1943 while (*p1 && *p1 != ':' && *p1 != '\n')
1944 p1++;
1946 /* The colon shouldn't be missing. */
1947 if (*p1 != ':')
1948 fatal ("specs file malformed after %ld characters",
1949 (long) (p1 - buffer));
1951 /* Skip back over trailing whitespace. */
1952 p2 = p1;
1953 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1954 p2--;
1956 /* Copy the suffix to a string. */
1957 suffix = save_string (p, p2 - p);
1958 /* Find the next line. */
1959 p = skip_whitespace (p1 + 1);
1960 if (p[1] == 0)
1961 fatal ("specs file malformed after %ld characters",
1962 (long) (p - buffer));
1964 p1 = p;
1965 /* Find next blank line or end of string. */
1966 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1967 p1++;
1969 /* Specs end at the blank line and do not include the newline. */
1970 spec = save_string (p, p1 - p);
1971 p = p1;
1973 /* Delete backslash-newline sequences from the spec. */
1974 in = spec;
1975 out = spec;
1976 while (*in != 0)
1978 if (in[0] == '\\' && in[1] == '\n')
1979 in += 2;
1980 else if (in[0] == '#')
1981 while (*in && *in != '\n')
1982 in++;
1984 else
1985 *out++ = *in++;
1987 *out = 0;
1989 if (suffix[0] == '*')
1991 if (! strcmp (suffix, "*link_command"))
1992 link_command_spec = spec;
1993 else
1994 set_spec (suffix + 1, spec);
1996 else
1998 /* Add this pair to the vector. */
1999 compilers
2000 = ((struct compiler *)
2001 xrealloc (compilers,
2002 (n_compilers + 2) * sizeof (struct compiler)));
2004 compilers[n_compilers].suffix = suffix;
2005 compilers[n_compilers].spec = spec;
2006 n_compilers++;
2007 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
2010 if (*suffix == 0)
2011 link_command_spec = spec;
2014 if (link_command_spec == 0)
2015 fatal ("spec file has no spec for linking");
2018 /* Record the names of temporary files we tell compilers to write,
2019 and delete them at the end of the run. */
2021 /* This is the common prefix we use to make temp file names.
2022 It is chosen once for each run of this program.
2023 It is substituted into a spec by %g or %j.
2024 Thus, all temp file names contain this prefix.
2025 In practice, all temp file names start with this prefix.
2027 This prefix comes from the envvar TMPDIR if it is defined;
2028 otherwise, from the P_tmpdir macro if that is defined;
2029 otherwise, in /usr/tmp or /tmp;
2030 or finally the current directory if all else fails. */
2032 static const char *temp_filename;
2034 /* Length of the prefix. */
2036 static int temp_filename_length;
2038 /* Define the list of temporary files to delete. */
2040 struct temp_file
2042 const char *name;
2043 struct temp_file *next;
2046 /* Queue of files to delete on success or failure of compilation. */
2047 static struct temp_file *always_delete_queue;
2048 /* Queue of files to delete on failure of compilation. */
2049 static struct temp_file *failure_delete_queue;
2051 /* Record FILENAME as a file to be deleted automatically.
2052 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
2053 otherwise delete it in any case.
2054 FAIL_DELETE nonzero means delete it if a compilation step fails;
2055 otherwise delete it in any case. */
2057 void
2058 record_temp_file (filename, always_delete, fail_delete)
2059 const char *filename;
2060 int always_delete;
2061 int fail_delete;
2063 char *const name = xstrdup (filename);
2065 if (always_delete)
2067 struct temp_file *temp;
2068 for (temp = always_delete_queue; temp; temp = temp->next)
2069 if (! strcmp (name, temp->name))
2070 goto already1;
2072 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2073 temp->next = always_delete_queue;
2074 temp->name = name;
2075 always_delete_queue = temp;
2077 already1:;
2080 if (fail_delete)
2082 struct temp_file *temp;
2083 for (temp = failure_delete_queue; temp; temp = temp->next)
2084 if (! strcmp (name, temp->name))
2085 goto already2;
2087 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2088 temp->next = failure_delete_queue;
2089 temp->name = name;
2090 failure_delete_queue = temp;
2092 already2:;
2096 /* Delete all the temporary files whose names we previously recorded. */
2098 static void
2099 delete_if_ordinary (name)
2100 const char *name;
2102 struct stat st;
2103 #ifdef DEBUG
2104 int i, c;
2106 printf ("Delete %s? (y or n) ", name);
2107 fflush (stdout);
2108 i = getchar ();
2109 if (i != '\n')
2110 while ((c = getchar ()) != '\n' && c != EOF)
2113 if (i == 'y' || i == 'Y')
2114 #endif /* DEBUG */
2115 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
2116 if (unlink (name) < 0)
2117 if (verbose_flag)
2118 perror_with_name (name);
2121 static void
2122 delete_temp_files ()
2124 struct temp_file *temp;
2126 for (temp = always_delete_queue; temp; temp = temp->next)
2127 delete_if_ordinary (temp->name);
2128 always_delete_queue = 0;
2131 /* Delete all the files to be deleted on error. */
2133 static void
2134 delete_failure_queue ()
2136 struct temp_file *temp;
2138 for (temp = failure_delete_queue; temp; temp = temp->next)
2139 delete_if_ordinary (temp->name);
2142 static void
2143 clear_failure_queue ()
2145 failure_delete_queue = 0;
2148 /* Build a list of search directories from PATHS.
2149 PREFIX is a string to prepend to the list.
2150 If CHECK_DIR_P is non-zero we ensure the directory exists.
2151 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2152 It is also used by the --print-search-dirs flag. */
2154 static char *
2155 build_search_list (paths, prefix, check_dir_p)
2156 struct path_prefix *paths;
2157 const char *prefix;
2158 int check_dir_p;
2160 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2161 int just_suffix_len
2162 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2163 int first_time = TRUE;
2164 struct prefix_list *pprefix;
2166 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2167 obstack_1grow (&collect_obstack, '=');
2169 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2171 int len = strlen (pprefix->prefix);
2173 if (machine_suffix
2174 && (! check_dir_p
2175 || is_directory (pprefix->prefix, machine_suffix, 0)))
2177 if (!first_time)
2178 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2180 first_time = FALSE;
2181 obstack_grow (&collect_obstack, pprefix->prefix, len);
2182 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2185 if (just_machine_suffix
2186 && pprefix->require_machine_suffix == 2
2187 && (! check_dir_p
2188 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2190 if (! first_time)
2191 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2193 first_time = FALSE;
2194 obstack_grow (&collect_obstack, pprefix->prefix, len);
2195 obstack_grow (&collect_obstack, just_machine_suffix,
2196 just_suffix_len);
2199 if (! pprefix->require_machine_suffix)
2201 if (! first_time)
2202 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2204 first_time = FALSE;
2205 obstack_grow (&collect_obstack, pprefix->prefix, len);
2209 obstack_1grow (&collect_obstack, '\0');
2210 return obstack_finish (&collect_obstack);
2213 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2214 for collect. */
2216 static void
2217 putenv_from_prefixes (paths, env_var)
2218 struct path_prefix *paths;
2219 const char *env_var;
2221 putenv (build_search_list (paths, env_var, 1));
2224 #ifndef VMS
2226 /* FIXME: the location independence code for VMS is hairier than this,
2227 and hasn't been written. */
2229 /* Split a filename into component directories. */
2231 static char **
2232 split_directories (name, ptr_num_dirs)
2233 const char *name;
2234 int *ptr_num_dirs;
2236 int num_dirs = 0;
2237 char **dirs;
2238 const char *p, *q;
2239 int ch;
2241 /* Count the number of directories. Special case MSDOS disk names as part
2242 of the initial directory. */
2243 p = name;
2244 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2245 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2247 p += 3;
2248 num_dirs++;
2250 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2252 while ((ch = *p++) != '\0')
2254 if (IS_DIR_SEPARATOR (ch))
2256 num_dirs++;
2257 while (IS_DIR_SEPARATOR (*p))
2258 p++;
2262 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2264 /* Now copy the directory parts. */
2265 num_dirs = 0;
2266 p = name;
2267 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2268 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2270 dirs[num_dirs++] = save_string (p, 3);
2271 p += 3;
2273 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2275 q = p;
2276 while ((ch = *p++) != '\0')
2278 if (IS_DIR_SEPARATOR (ch))
2280 while (IS_DIR_SEPARATOR (*p))
2281 p++;
2283 dirs[num_dirs++] = save_string (q, p - q);
2284 q = p;
2288 if (p - 1 - q > 0)
2289 dirs[num_dirs++] = save_string (q, p - 1 - q);
2291 dirs[num_dirs] = NULL;
2292 if (ptr_num_dirs)
2293 *ptr_num_dirs = num_dirs;
2295 return dirs;
2298 /* Release storage held by split directories. */
2300 static void
2301 free_split_directories (dirs)
2302 char **dirs;
2304 int i = 0;
2306 while (dirs[i] != NULL)
2307 free (dirs[i++]);
2309 free ((char *) dirs);
2312 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2313 to PREFIX starting with the directory portion of PROGNAME and a relative
2314 pathname of the difference between BIN_PREFIX and PREFIX.
2316 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2317 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2318 function will return /red/green/blue/../omega.
2320 If no relative prefix can be found, return NULL. */
2322 static char *
2323 make_relative_prefix (progname, bin_prefix, prefix)
2324 const char *progname;
2325 const char *bin_prefix;
2326 const char *prefix;
2328 char **prog_dirs, **bin_dirs, **prefix_dirs;
2329 int prog_num, bin_num, prefix_num, std_loc_p;
2330 int i, n, common;
2332 prog_dirs = split_directories (progname, &prog_num);
2333 bin_dirs = split_directories (bin_prefix, &bin_num);
2335 /* If there is no full pathname, try to find the program by checking in each
2336 of the directories specified in the PATH environment variable. */
2337 if (prog_num == 1)
2339 char *temp;
2341 GET_ENV_PATH_LIST (temp, "PATH");
2342 if (temp)
2344 char *startp, *endp, *nstore;
2345 size_t prefixlen = strlen (temp) + 1;
2346 if (prefixlen < 2)
2347 prefixlen = 2;
2349 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
2351 startp = endp = temp;
2352 while (1)
2354 if (*endp == PATH_SEPARATOR || *endp == 0)
2356 if (endp == startp)
2358 nstore[0] = '.';
2359 nstore[1] = DIR_SEPARATOR;
2360 nstore[2] = '\0';
2362 else
2364 strncpy (nstore, startp, endp - startp);
2365 if (! IS_DIR_SEPARATOR (endp[-1]))
2367 nstore[endp - startp] = DIR_SEPARATOR;
2368 nstore[endp - startp + 1] = 0;
2370 else
2371 nstore[endp - startp] = 0;
2373 strcat (nstore, progname);
2374 if (! access (nstore, X_OK)
2375 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
2376 || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
2377 #endif
2380 free_split_directories (prog_dirs);
2381 progname = nstore;
2382 prog_dirs = split_directories (progname, &prog_num);
2383 break;
2386 if (*endp == 0)
2387 break;
2388 endp = startp = endp + 1;
2390 else
2391 endp++;
2396 /* Remove the program name from comparison of directory names. */
2397 prog_num--;
2399 /* Determine if the compiler is installed in the standard location, and if
2400 so, we don't need to specify relative directories. Also, if argv[0]
2401 doesn't contain any directory specifiers, there is not much we can do. */
2402 std_loc_p = 0;
2403 if (prog_num == bin_num)
2405 for (i = 0; i < bin_num; i++)
2407 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2408 break;
2411 if (prog_num <= 0 || i == bin_num)
2413 std_loc_p = 1;
2414 free_split_directories (prog_dirs);
2415 free_split_directories (bin_dirs);
2416 prog_dirs = bin_dirs = (char **) 0;
2417 return NULL;
2421 prefix_dirs = split_directories (prefix, &prefix_num);
2423 /* Find how many directories are in common between bin_prefix & prefix. */
2424 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2425 for (common = 0; common < n; common++)
2427 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2428 break;
2431 /* If there are no common directories, there can be no relative prefix. */
2432 if (common == 0)
2434 free_split_directories (prog_dirs);
2435 free_split_directories (bin_dirs);
2436 free_split_directories (prefix_dirs);
2437 return NULL;
2440 /* Build up the pathnames in argv[0]. */
2441 for (i = 0; i < prog_num; i++)
2442 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2444 /* Now build up the ..'s. */
2445 for (i = common; i < n; i++)
2447 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2448 obstack_1grow (&obstack, DIR_SEPARATOR);
2451 /* Put in directories to move over to prefix. */
2452 for (i = common; i < prefix_num; i++)
2453 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2455 free_split_directories (prog_dirs);
2456 free_split_directories (bin_dirs);
2457 free_split_directories (prefix_dirs);
2459 obstack_1grow (&obstack, '\0');
2460 return obstack_finish (&obstack);
2462 #endif /* VMS */
2464 /* Check whether NAME can be accessed in MODE. This is like access,
2465 except that it never considers directories to be executable. */
2467 static int
2468 access_check (name, mode)
2469 const char *name;
2470 int mode;
2472 if (mode == X_OK)
2474 struct stat st;
2476 if (stat (name, &st) < 0
2477 || S_ISDIR (st.st_mode))
2478 return -1;
2481 return access (name, mode);
2484 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2485 access to check permissions.
2486 Return 0 if not found, otherwise return its name, allocated with malloc. */
2488 static char *
2489 find_a_file (pprefix, name, mode)
2490 struct path_prefix *pprefix;
2491 const char *name;
2492 int mode;
2494 char *temp;
2495 const char *const file_suffix =
2496 ((mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "");
2497 struct prefix_list *pl;
2498 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2500 #ifdef DEFAULT_ASSEMBLER
2501 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2502 return xstrdup (DEFAULT_ASSEMBLER);
2503 #endif
2505 #ifdef DEFAULT_LINKER
2506 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2507 return xstrdup (DEFAULT_LINKER);
2508 #endif
2510 if (machine_suffix)
2511 len += strlen (machine_suffix);
2513 temp = xmalloc (len);
2515 /* Determine the filename to execute (special case for absolute paths). */
2517 if (IS_ABSOLUTE_PATHNAME (name))
2519 if (access (name, mode) == 0)
2521 strcpy (temp, name);
2522 return temp;
2525 else
2526 for (pl = pprefix->plist; pl; pl = pl->next)
2528 if (machine_suffix)
2530 /* Some systems have a suffix for executable files.
2531 So try appending that first. */
2532 if (file_suffix[0] != 0)
2534 strcpy (temp, pl->prefix);
2535 strcat (temp, machine_suffix);
2536 strcat (temp, name);
2537 strcat (temp, file_suffix);
2538 if (access_check (temp, mode) == 0)
2540 if (pl->used_flag_ptr != 0)
2541 *pl->used_flag_ptr = 1;
2542 return temp;
2546 /* Now try just the name. */
2547 strcpy (temp, pl->prefix);
2548 strcat (temp, machine_suffix);
2549 strcat (temp, name);
2550 if (access_check (temp, mode) == 0)
2552 if (pl->used_flag_ptr != 0)
2553 *pl->used_flag_ptr = 1;
2554 return temp;
2558 /* Certain prefixes are tried with just the machine type,
2559 not the version. This is used for finding as, ld, etc. */
2560 if (just_machine_suffix && pl->require_machine_suffix == 2)
2562 /* Some systems have a suffix for executable files.
2563 So try appending that first. */
2564 if (file_suffix[0] != 0)
2566 strcpy (temp, pl->prefix);
2567 strcat (temp, just_machine_suffix);
2568 strcat (temp, name);
2569 strcat (temp, file_suffix);
2570 if (access_check (temp, mode) == 0)
2572 if (pl->used_flag_ptr != 0)
2573 *pl->used_flag_ptr = 1;
2574 return temp;
2578 strcpy (temp, pl->prefix);
2579 strcat (temp, just_machine_suffix);
2580 strcat (temp, name);
2581 if (access_check (temp, mode) == 0)
2583 if (pl->used_flag_ptr != 0)
2584 *pl->used_flag_ptr = 1;
2585 return temp;
2589 /* Certain prefixes can't be used without the machine suffix
2590 when the machine or version is explicitly specified. */
2591 if (! pl->require_machine_suffix)
2593 /* Some systems have a suffix for executable files.
2594 So try appending that first. */
2595 if (file_suffix[0] != 0)
2597 strcpy (temp, pl->prefix);
2598 strcat (temp, name);
2599 strcat (temp, file_suffix);
2600 if (access_check (temp, mode) == 0)
2602 if (pl->used_flag_ptr != 0)
2603 *pl->used_flag_ptr = 1;
2604 return temp;
2608 strcpy (temp, pl->prefix);
2609 strcat (temp, name);
2610 if (access_check (temp, mode) == 0)
2612 if (pl->used_flag_ptr != 0)
2613 *pl->used_flag_ptr = 1;
2614 return temp;
2619 free (temp);
2620 return 0;
2623 /* Ranking of prefixes in the sort list. -B prefixes are put before
2624 all others. */
2626 enum path_prefix_priority
2628 PREFIX_PRIORITY_B_OPT,
2629 PREFIX_PRIORITY_LAST
2632 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2633 order according to PRIORITY. Within each PRIORITY, new entries are
2634 appended.
2636 If WARN is nonzero, we will warn if no file is found
2637 through this prefix. WARN should point to an int
2638 which will be set to 1 if this entry is used.
2640 COMPONENT is the value to be passed to update_path.
2642 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2643 the complete value of machine_suffix.
2644 2 means try both machine_suffix and just_machine_suffix. */
2646 static void
2647 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2648 struct path_prefix *pprefix;
2649 const char *prefix;
2650 const char *component;
2651 /* enum prefix_priority */ int priority;
2652 int require_machine_suffix;
2653 int *warn;
2655 struct prefix_list *pl, **prev;
2656 int len;
2658 for (prev = &pprefix->plist;
2659 (*prev) != NULL && (*prev)->priority <= priority;
2660 prev = &(*prev)->next)
2663 /* Keep track of the longest prefix */
2665 prefix = update_path (prefix, component);
2666 len = strlen (prefix);
2667 if (len > pprefix->max_len)
2668 pprefix->max_len = len;
2670 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2671 pl->prefix = prefix;
2672 pl->require_machine_suffix = require_machine_suffix;
2673 pl->used_flag_ptr = warn;
2674 pl->priority = priority;
2675 if (warn)
2676 *warn = 0;
2678 /* Insert after PREV */
2679 pl->next = (*prev);
2680 (*prev) = pl;
2683 /* Execute the command specified by the arguments on the current line of spec.
2684 When using pipes, this includes several piped-together commands
2685 with `|' between them.
2687 Return 0 if successful, -1 if failed. */
2689 static int
2690 execute ()
2692 int i;
2693 int n_commands; /* # of command. */
2694 char *string;
2695 struct command
2697 const char *prog; /* program name. */
2698 const char **argv; /* vector of args. */
2699 int pid; /* pid of process for this command. */
2702 struct command *commands; /* each command buffer with above info. */
2704 /* Count # of piped commands. */
2705 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2706 if (strcmp (argbuf[i], "|") == 0)
2707 n_commands++;
2709 /* Get storage for each command. */
2710 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2712 /* Split argbuf into its separate piped processes,
2713 and record info about each one.
2714 Also search for the programs that are to be run. */
2716 commands[0].prog = argbuf[0]; /* first command. */
2717 commands[0].argv = &argbuf[0];
2718 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2720 if (string)
2721 commands[0].argv[0] = string;
2723 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2724 if (strcmp (argbuf[i], "|") == 0)
2725 { /* each command. */
2726 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2727 fatal ("-pipe not supported");
2728 #endif
2729 argbuf[i] = 0; /* termination of command args. */
2730 commands[n_commands].prog = argbuf[i + 1];
2731 commands[n_commands].argv = &argbuf[i + 1];
2732 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2733 if (string)
2734 commands[n_commands].argv[0] = string;
2735 n_commands++;
2738 argbuf[argbuf_index] = 0;
2740 /* If -v, print what we are about to do, and maybe query. */
2742 if (verbose_flag)
2744 /* For help listings, put a blank line between sub-processes. */
2745 if (print_help_list)
2746 fputc ('\n', stderr);
2748 /* Print each piped command as a separate line. */
2749 for (i = 0; i < n_commands; i++)
2751 const char *const *j;
2753 if (verbose_only_flag)
2755 for (j = commands[i].argv; *j; j++)
2757 const char *p;
2758 fprintf (stderr, " \"");
2759 for (p = *j; *p; ++p)
2761 if (*p == '"' || *p == '\\' || *p == '$')
2762 fputc ('\\', stderr);
2763 fputc (*p, stderr);
2765 fputc ('"', stderr);
2768 else
2769 for (j = commands[i].argv; *j; j++)
2770 fprintf (stderr, " %s", *j);
2772 /* Print a pipe symbol after all but the last command. */
2773 if (i + 1 != n_commands)
2774 fprintf (stderr, " |");
2775 fprintf (stderr, "\n");
2777 fflush (stderr);
2778 if (verbose_only_flag != 0)
2779 return 0;
2780 #ifdef DEBUG
2781 notice ("\nGo ahead? (y or n) ");
2782 fflush (stderr);
2783 i = getchar ();
2784 if (i != '\n')
2785 while (getchar () != '\n')
2788 if (i != 'y' && i != 'Y')
2789 return 0;
2790 #endif /* DEBUG */
2793 /* Run each piped subprocess. */
2795 for (i = 0; i < n_commands; i++)
2797 char *errmsg_fmt, *errmsg_arg;
2798 const char *string = commands[i].argv[0];
2800 /* For some bizarre reason, the second argument of execvp() is
2801 char *const *, not const char *const *. */
2802 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2803 programname, temp_filename,
2804 &errmsg_fmt, &errmsg_arg,
2805 ((i == 0 ? PEXECUTE_FIRST : 0)
2806 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2807 | (string == commands[i].prog
2808 ? PEXECUTE_SEARCH : 0)
2809 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2811 if (commands[i].pid == -1)
2812 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2814 if (string != commands[i].prog)
2815 free ((PTR) string);
2818 execution_count++;
2820 /* Wait for all the subprocesses to finish.
2821 We don't care what order they finish in;
2822 we know that N_COMMANDS waits will get them all.
2823 Ignore subprocesses that we don't know about,
2824 since they can be spawned by the process that exec'ed us. */
2827 int ret_code = 0;
2828 #ifdef HAVE_GETRUSAGE
2829 struct timeval d;
2830 double ut = 0.0, st = 0.0;
2831 #endif
2833 for (i = 0; i < n_commands;)
2835 int j;
2836 int status;
2837 int pid;
2839 pid = pwait (commands[i].pid, &status, 0);
2840 if (pid < 0)
2841 abort ();
2843 #ifdef HAVE_GETRUSAGE
2844 if (report_times)
2846 /* getrusage returns the total resource usage of all children
2847 up to now. Copy the previous values into prus, get the
2848 current statistics, then take the difference. */
2850 prus = rus;
2851 getrusage (RUSAGE_CHILDREN, &rus);
2852 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2853 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2854 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2856 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2857 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2858 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2860 #endif
2862 for (j = 0; j < n_commands; j++)
2863 if (commands[j].pid == pid)
2865 i++;
2866 if (WIFSIGNALED (status))
2868 #ifdef SIGPIPE
2869 /* SIGPIPE is a special case. It happens in -pipe mode
2870 when the compiler dies before the preprocessor is
2871 done, or the assembler dies before the compiler is
2872 done. There's generally been an error already, and
2873 this is just fallout. So don't generate another error
2874 unless we would otherwise have succeeded. */
2875 if (WTERMSIG (status) == SIGPIPE
2876 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2878 else
2879 #endif
2880 fatal ("\
2881 Internal error: %s (program %s)\n\
2882 Please submit a full bug report.\n\
2883 See %s for instructions.",
2884 strsignal (WTERMSIG (status)), commands[j].prog,
2885 GCCBUGURL);
2886 signal_count++;
2887 ret_code = -1;
2889 else if (WIFEXITED (status)
2890 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2892 if (WEXITSTATUS (status) > greatest_status)
2893 greatest_status = WEXITSTATUS (status);
2894 ret_code = -1;
2896 #ifdef HAVE_GETRUSAGE
2897 if (report_times && ut + st != 0)
2898 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2899 #endif
2900 break;
2903 return ret_code;
2907 /* Find all the switches given to us
2908 and make a vector describing them.
2909 The elements of the vector are strings, one per switch given.
2910 If a switch uses following arguments, then the `part1' field
2911 is the switch itself and the `args' field
2912 is a null-terminated vector containing the following arguments.
2913 The `live_cond' field is:
2914 0 when initialized
2915 1 if the switch is true in a conditional spec,
2916 -1 if false (overridden by a later switch)
2917 -2 if this switch should be ignored (used in %{<S})
2918 The `validated' field is nonzero if any spec has looked at this switch;
2919 if it remains zero at the end of the run, it must be meaningless. */
2921 #define SWITCH_OK 0
2922 #define SWITCH_FALSE -1
2923 #define SWITCH_IGNORE -2
2924 #define SWITCH_LIVE 1
2926 struct switchstr
2928 const char *part1;
2929 const char **args;
2930 int live_cond;
2931 unsigned char validated;
2932 unsigned char ordering;
2935 static struct switchstr *switches;
2937 static int n_switches;
2939 struct infile
2941 const char *name;
2942 const char *language;
2945 /* Also a vector of input files specified. */
2947 static struct infile *infiles;
2949 int n_infiles;
2951 /* This counts the number of libraries added by lang_specific_driver, so that
2952 we can tell if there were any user supplied any files or libraries. */
2954 static int added_libraries;
2956 /* And a vector of corresponding output files is made up later. */
2958 const char **outfiles;
2960 /* Used to track if none of the -B paths are used. */
2961 static int warn_B;
2963 /* Used to track if standard path isn't used and -b or -V is specified. */
2964 static int warn_std;
2966 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2967 static int *warn_std_ptr = 0;
2969 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2971 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2972 is true if we should look for an executable suffix. DO_OBJ
2973 is true if we should look for an object suffix. */
2975 static const char *
2976 convert_filename (name, do_exe, do_obj)
2977 const char *name;
2978 int do_exe ATTRIBUTE_UNUSED;
2979 int do_obj ATTRIBUTE_UNUSED;
2981 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2982 int i;
2983 #endif
2984 int len;
2986 if (name == NULL)
2987 return NULL;
2989 len = strlen (name);
2991 #ifdef HAVE_TARGET_OBJECT_SUFFIX
2992 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
2993 if (do_obj && len > 2
2994 && name[len - 2] == '.'
2995 && name[len - 1] == 'o')
2997 obstack_grow (&obstack, name, len - 2);
2998 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
2999 name = obstack_finish (&obstack);
3001 #endif
3003 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3004 /* If there is no filetype, make it the executable suffix (which includes
3005 the "."). But don't get confused if we have just "-o". */
3006 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
3007 return name;
3009 for (i = len - 1; i >= 0; i--)
3010 if (IS_DIR_SEPARATOR (name[i]))
3011 break;
3013 for (i++; i < len; i++)
3014 if (name[i] == '.')
3015 return name;
3017 obstack_grow (&obstack, name, len);
3018 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
3019 strlen (TARGET_EXECUTABLE_SUFFIX));
3020 name = obstack_finish (&obstack);
3021 #endif
3023 return name;
3025 #endif
3027 /* Display the command line switches accepted by gcc. */
3028 static void
3029 display_help ()
3031 printf (_("Usage: %s [options] file...\n"), programname);
3032 fputs (_("Options:\n"), stdout);
3034 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
3035 fputs (_(" --help Display this information\n"), stdout);
3036 fputs (_(" --target-help Display target specific command line options\n"), stdout);
3037 if (! verbose_flag)
3038 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
3039 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
3040 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
3041 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
3042 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
3043 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
3044 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
3045 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
3046 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
3047 fputs (_("\
3048 -print-multi-lib Display the mapping between command line options and\n\
3049 multiple library search directories\n"), stdout);
3050 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
3051 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
3052 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
3053 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
3054 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
3055 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
3056 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
3057 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
3058 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
3059 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
3060 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
3061 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
3062 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
3063 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
3064 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
3065 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
3066 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
3067 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
3068 fputs (_("\
3069 -x <language> Specify the language of the following input files\n\
3070 Permissable languages include: c c++ assembler none\n\
3071 'none' means revert to the default behavior of\n\
3072 guessing the language based on the file's extension\n\
3073 "), stdout);
3075 printf (_("\
3076 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3077 passed on to the various sub-processes invoked by %s. In order to pass\n\
3078 other options on to these processes the -W<letter> options must be used.\n\
3079 "), programname);
3081 /* The rest of the options are displayed by invocations of the various
3082 sub-processes. */
3085 static void
3086 add_preprocessor_option (option, len)
3087 const char *option;
3088 int len;
3090 n_preprocessor_options++;
3092 if (! preprocessor_options)
3093 preprocessor_options
3094 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
3095 else
3096 preprocessor_options
3097 = (char **) xrealloc (preprocessor_options,
3098 n_preprocessor_options * sizeof (char *));
3100 preprocessor_options [n_preprocessor_options - 1] =
3101 save_string (option, len);
3104 static void
3105 add_assembler_option (option, len)
3106 const char *option;
3107 int len;
3109 n_assembler_options++;
3111 if (! assembler_options)
3112 assembler_options
3113 = (char **) xmalloc (n_assembler_options * sizeof (char *));
3114 else
3115 assembler_options
3116 = (char **) xrealloc (assembler_options,
3117 n_assembler_options * sizeof (char *));
3119 assembler_options [n_assembler_options - 1] = save_string (option, len);
3122 static void
3123 add_linker_option (option, len)
3124 const char *option;
3125 int len;
3127 n_linker_options++;
3129 if (! linker_options)
3130 linker_options
3131 = (char **) xmalloc (n_linker_options * sizeof (char *));
3132 else
3133 linker_options
3134 = (char **) xrealloc (linker_options,
3135 n_linker_options * sizeof (char *));
3137 linker_options [n_linker_options - 1] = save_string (option, len);
3140 /* Create the vector `switches' and its contents.
3141 Store its length in `n_switches'. */
3143 static void
3144 process_command (argc, argv)
3145 int argc;
3146 const char *const *argv;
3148 int i;
3149 const char *temp;
3150 char *temp1;
3151 const char *spec_lang = 0;
3152 int last_language_n_infiles;
3153 int have_c = 0;
3154 int have_o = 0;
3155 int lang_n_infiles = 0;
3156 #ifdef MODIFY_TARGET_NAME
3157 int is_modify_target_name;
3158 int j;
3159 #endif
3161 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3163 n_switches = 0;
3164 n_infiles = 0;
3165 added_libraries = 0;
3167 /* Figure compiler version from version string. */
3169 compiler_version = temp1 = xstrdup (version_string);
3171 for (; *temp1; ++temp1)
3173 if (*temp1 == ' ')
3175 *temp1 = '\0';
3176 break;
3180 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3181 see if we can create it from the pathname specified in argv[0]. */
3183 #ifndef VMS
3184 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3185 if (!gcc_exec_prefix)
3187 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3188 standard_exec_prefix);
3189 if (gcc_exec_prefix)
3190 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
3192 #endif
3194 if (gcc_exec_prefix)
3196 int len = strlen (gcc_exec_prefix);
3198 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3199 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3201 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3202 if (IS_DIR_SEPARATOR (*temp)
3203 && strncmp (temp + 1, "lib", 3) == 0
3204 && IS_DIR_SEPARATOR (temp[4])
3205 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3206 len -= sizeof ("/lib/gcc-lib/") - 1;
3209 set_std_prefix (gcc_exec_prefix, len);
3210 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3211 PREFIX_PRIORITY_LAST, 0, NULL);
3212 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3213 PREFIX_PRIORITY_LAST, 0, NULL);
3216 /* COMPILER_PATH and LIBRARY_PATH have values
3217 that are lists of directory names with colons. */
3219 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3220 if (temp)
3222 const char *startp, *endp;
3223 char *nstore = (char *) alloca (strlen (temp) + 3);
3225 startp = endp = temp;
3226 while (1)
3228 if (*endp == PATH_SEPARATOR || *endp == 0)
3230 strncpy (nstore, startp, endp - startp);
3231 if (endp == startp)
3232 strcpy (nstore, concat (".", dir_separator_str, NULL));
3233 else if (!IS_DIR_SEPARATOR (endp[-1]))
3235 nstore[endp - startp] = DIR_SEPARATOR;
3236 nstore[endp - startp + 1] = 0;
3238 else
3239 nstore[endp - startp] = 0;
3240 add_prefix (&exec_prefixes, nstore, 0,
3241 PREFIX_PRIORITY_LAST, 0, NULL);
3242 add_prefix (&include_prefixes,
3243 concat (nstore, "include", NULL),
3244 0, PREFIX_PRIORITY_LAST, 0, NULL);
3245 if (*endp == 0)
3246 break;
3247 endp = startp = endp + 1;
3249 else
3250 endp++;
3254 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3255 if (temp && *cross_compile == '0')
3257 const char *startp, *endp;
3258 char *nstore = (char *) alloca (strlen (temp) + 3);
3260 startp = endp = temp;
3261 while (1)
3263 if (*endp == PATH_SEPARATOR || *endp == 0)
3265 strncpy (nstore, startp, endp - startp);
3266 if (endp == startp)
3267 strcpy (nstore, concat (".", dir_separator_str, NULL));
3268 else if (!IS_DIR_SEPARATOR (endp[-1]))
3270 nstore[endp - startp] = DIR_SEPARATOR;
3271 nstore[endp - startp + 1] = 0;
3273 else
3274 nstore[endp - startp] = 0;
3275 add_prefix (&startfile_prefixes, nstore, NULL,
3276 PREFIX_PRIORITY_LAST, 0, NULL);
3277 if (*endp == 0)
3278 break;
3279 endp = startp = endp + 1;
3281 else
3282 endp++;
3286 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3287 GET_ENV_PATH_LIST (temp, "LPATH");
3288 if (temp && *cross_compile == '0')
3290 const char *startp, *endp;
3291 char *nstore = (char *) alloca (strlen (temp) + 3);
3293 startp = endp = temp;
3294 while (1)
3296 if (*endp == PATH_SEPARATOR || *endp == 0)
3298 strncpy (nstore, startp, endp - startp);
3299 if (endp == startp)
3300 strcpy (nstore, concat (".", dir_separator_str, NULL));
3301 else if (!IS_DIR_SEPARATOR (endp[-1]))
3303 nstore[endp - startp] = DIR_SEPARATOR;
3304 nstore[endp - startp + 1] = 0;
3306 else
3307 nstore[endp - startp] = 0;
3308 add_prefix (&startfile_prefixes, nstore, NULL,
3309 PREFIX_PRIORITY_LAST, 0, NULL);
3310 if (*endp == 0)
3311 break;
3312 endp = startp = endp + 1;
3314 else
3315 endp++;
3319 /* Convert new-style -- options to old-style. */
3320 translate_options (&argc, &argv);
3322 /* Do language-specific adjustment/addition of flags. */
3323 lang_specific_driver (&argc, &argv, &added_libraries);
3325 /* Scan argv twice. Here, the first time, just count how many switches
3326 there will be in their vector, and how many input files in theirs.
3327 Also parse any switches that determine the configuration name, such as -b.
3328 Here we also parse the switches that cc itself uses (e.g. -v). */
3330 for (i = 1; i < argc; i++)
3332 if (! strcmp (argv[i], "-dumpspecs"))
3334 struct spec_list *sl;
3335 init_spec ();
3336 for (sl = specs; sl; sl = sl->next)
3337 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3338 if (link_command_spec)
3339 printf ("*link_command:\n%s\n\n", link_command_spec);
3340 exit (0);
3342 else if (! strcmp (argv[i], "-dumpversion"))
3344 printf ("%s\n", spec_version);
3345 exit (0);
3347 else if (! strcmp (argv[i], "-dumpmachine"))
3349 printf ("%s\n", spec_machine);
3350 exit (0);
3352 else if (strcmp (argv[i], "-fversion") == 0)
3354 /* translate_options () has turned --version into -fversion. */
3355 printf (_("%s (GCC) %s\n"), programname, version_string);
3356 fputs (_("Copyright (C) 2002 Free Software Foundation, Inc.\n"),
3357 stdout);
3358 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
3359 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
3360 stdout);
3361 exit (0);
3363 else if (strcmp (argv[i], "-fhelp") == 0)
3365 /* translate_options () has turned --help into -fhelp. */
3366 print_help_list = 1;
3368 /* We will be passing a dummy file on to the sub-processes. */
3369 n_infiles++;
3370 n_switches++;
3372 /* CPP driver cannot obtain switch from cc1_options. */
3373 if (is_cpp_driver)
3374 add_preprocessor_option ("--help", 6);
3375 add_assembler_option ("--help", 6);
3376 add_linker_option ("--help", 6);
3378 else if (strcmp (argv[i], "-ftarget-help") == 0)
3380 /* translate_options() has turned --target-help into -ftarget-help. */
3381 target_help_flag = 1;
3383 /* We will be passing a dummy file on to the sub-processes. */
3384 n_infiles++;
3385 n_switches++;
3387 /* CPP driver cannot obtain switch from cc1_options. */
3388 if (is_cpp_driver)
3389 add_preprocessor_option ("--target-help", 13);
3390 add_assembler_option ("--target-help", 13);
3391 add_linker_option ("--target-help", 13);
3393 else if (! strcmp (argv[i], "-pass-exit-codes"))
3395 pass_exit_codes = 1;
3396 n_switches++;
3398 else if (! strcmp (argv[i], "-print-search-dirs"))
3399 print_search_dirs = 1;
3400 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3401 print_file_name = "libgcc.a";
3402 else if (! strncmp (argv[i], "-print-file-name=", 17))
3403 print_file_name = argv[i] + 17;
3404 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3405 print_prog_name = argv[i] + 17;
3406 else if (! strcmp (argv[i], "-print-multi-lib"))
3407 print_multi_lib = 1;
3408 else if (! strcmp (argv[i], "-print-multi-directory"))
3409 print_multi_directory = 1;
3410 else if (! strncmp (argv[i], "-Wa,", 4))
3412 int prev, j;
3413 /* Pass the rest of this option to the assembler. */
3415 /* Split the argument at commas. */
3416 prev = 4;
3417 for (j = 4; argv[i][j]; j++)
3418 if (argv[i][j] == ',')
3420 add_assembler_option (argv[i] + prev, j - prev);
3421 prev = j + 1;
3424 /* Record the part after the last comma. */
3425 add_assembler_option (argv[i] + prev, j - prev);
3427 else if (! strncmp (argv[i], "-Wp,", 4))
3429 int prev, j;
3430 /* Pass the rest of this option to the preprocessor. */
3432 /* Split the argument at commas. */
3433 prev = 4;
3434 for (j = 4; argv[i][j]; j++)
3435 if (argv[i][j] == ',')
3437 add_preprocessor_option (argv[i] + prev, j - prev);
3438 prev = j + 1;
3441 /* Record the part after the last comma. */
3442 add_preprocessor_option (argv[i] + prev, j - prev);
3444 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3445 /* The +e options to the C++ front-end. */
3446 n_switches++;
3447 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3449 int j;
3450 /* Split the argument at commas. */
3451 for (j = 3; argv[i][j]; j++)
3452 n_infiles += (argv[i][j] == ',');
3454 else if (strcmp (argv[i], "-Xlinker") == 0)
3456 if (i + 1 == argc)
3457 fatal ("argument to `-Xlinker' is missing");
3459 n_infiles++;
3460 i++;
3462 else if (strcmp (argv[i], "-l") == 0)
3464 if (i + 1 == argc)
3465 fatal ("argument to `-l' is missing");
3467 n_infiles++;
3468 i++;
3470 else if (strncmp (argv[i], "-l", 2) == 0)
3471 n_infiles++;
3472 else if (strcmp (argv[i], "-save-temps") == 0)
3474 save_temps_flag = 1;
3475 n_switches++;
3477 else if (strcmp (argv[i], "-specs") == 0)
3479 struct user_specs *user = (struct user_specs *)
3480 xmalloc (sizeof (struct user_specs));
3481 if (++i >= argc)
3482 fatal ("argument to `-specs' is missing");
3484 user->next = (struct user_specs *) 0;
3485 user->filename = argv[i];
3486 if (user_specs_tail)
3487 user_specs_tail->next = user;
3488 else
3489 user_specs_head = user;
3490 user_specs_tail = user;
3492 else if (strncmp (argv[i], "-specs=", 7) == 0)
3494 struct user_specs *user = (struct user_specs *)
3495 xmalloc (sizeof (struct user_specs));
3496 if (strlen (argv[i]) == 7)
3497 fatal ("argument to `-specs=' is missing");
3499 user->next = (struct user_specs *) 0;
3500 user->filename = argv[i] + 7;
3501 if (user_specs_tail)
3502 user_specs_tail->next = user;
3503 else
3504 user_specs_head = user;
3505 user_specs_tail = user;
3507 else if (strcmp (argv[i], "-time") == 0)
3508 report_times = 1;
3509 else if (strcmp (argv[i], "-###") == 0)
3511 /* This is similar to -v except that there is no execution
3512 of the commands and the echoed arguments are quoted. It
3513 is intended for use in shell scripts to capture the
3514 driver-generated command line. */
3515 verbose_only_flag++;
3516 verbose_flag++;
3518 else if (argv[i][0] == '-' && argv[i][1] != 0)
3520 const char *p = &argv[i][1];
3521 int c = *p;
3523 switch (c)
3525 case 'b':
3526 n_switches++;
3527 if (p[1] == 0 && i + 1 == argc)
3528 fatal ("argument to `-b' is missing");
3529 if (p[1] == 0)
3530 spec_machine = argv[++i];
3531 else
3532 spec_machine = p + 1;
3534 warn_std_ptr = &warn_std;
3535 break;
3537 case 'B':
3539 const char *value;
3540 int len;
3542 if (p[1] == 0 && i + 1 == argc)
3543 fatal ("argument to `-B' is missing");
3544 if (p[1] == 0)
3545 value = argv[++i];
3546 else
3547 value = p + 1;
3549 len = strlen (value);
3551 /* Catch the case where the user has forgotten to append a
3552 directory separator to the path. Note, they may be using
3553 -B to add an executable name prefix, eg "i386-elf-", in
3554 order to distinguish between multiple installations of
3555 GCC in the same directory. Hence we must check to see
3556 if appending a directory separator actually makes a
3557 valid directory name. */
3558 if (! IS_DIR_SEPARATOR (value [len - 1])
3559 && is_directory (value, "", 0))
3561 char *tmp = xmalloc (len + 2);
3562 strcpy (tmp, value);
3563 tmp[len] = DIR_SEPARATOR;
3564 tmp[++ len] = 0;
3565 value = tmp;
3568 /* As a kludge, if the arg is "[foo/]stageN/", just
3569 add "[foo/]include" to the include prefix. */
3570 if ((len == 7
3571 || (len > 7
3572 && (IS_DIR_SEPARATOR (value[len - 8]))))
3573 && strncmp (value + len - 7, "stage", 5) == 0
3574 && ISDIGIT (value[len - 2])
3575 && (IS_DIR_SEPARATOR (value[len - 1])))
3577 if (len == 7)
3578 add_prefix (&include_prefixes, "include", NULL,
3579 PREFIX_PRIORITY_B_OPT, 0, NULL);
3580 else
3582 char * string = xmalloc (len + 1);
3584 strncpy (string, value, len - 7);
3585 strcpy (string + len - 7, "include");
3586 add_prefix (&include_prefixes, string, NULL,
3587 PREFIX_PRIORITY_B_OPT, 0, NULL);
3591 add_prefix (&exec_prefixes, value, NULL,
3592 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3593 add_prefix (&startfile_prefixes, value, NULL,
3594 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3595 add_prefix (&include_prefixes, concat (value, "include", NULL),
3596 NULL, PREFIX_PRIORITY_B_OPT, 0, NULL);
3597 n_switches++;
3599 break;
3601 case 'v': /* Print our subcommands and print versions. */
3602 n_switches++;
3603 /* If they do anything other than exactly `-v', don't set
3604 verbose_flag; rather, continue on to give the error. */
3605 if (p[1] != 0)
3606 break;
3607 verbose_flag++;
3608 break;
3610 case 'S':
3611 case 'c':
3612 if (p[1] == 0)
3614 have_c = 1;
3615 n_switches++;
3616 break;
3618 goto normal_switch;
3620 case 'o':
3621 have_o = 1;
3622 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3623 if (! have_c)
3625 int skip;
3627 /* Forward scan, just in case -S or -c is specified
3628 after -o. */
3629 int j = i + 1;
3630 if (p[1] == 0)
3631 ++j;
3632 while (j < argc)
3634 if (argv[j][0] == '-')
3636 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3637 && argv[j][2] == 0)
3639 have_c = 1;
3640 break;
3642 else if ((skip = SWITCH_TAKES_ARG (argv[j][1])))
3643 j += skip - (argv[j][2] != 0);
3644 else if ((skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1)))
3645 j += skip;
3647 j++;
3650 #endif
3651 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3652 if (p[1] == 0)
3653 argv[i + 1] = convert_filename (argv[i + 1], ! have_c, 0);
3654 else
3655 argv[i] = convert_filename (argv[i], ! have_c, 0);
3656 #endif
3657 goto normal_switch;
3659 default:
3660 normal_switch:
3662 #ifdef MODIFY_TARGET_NAME
3663 is_modify_target_name = 0;
3665 for (j = 0; j < ARRAY_SIZE (modify_target); j++)
3666 if (! strcmp (argv[i], modify_target[j].sw))
3668 char *new_name
3669 = (char *) xmalloc (strlen (modify_target[j].str)
3670 + strlen (spec_machine));
3671 const char *p, *r;
3672 char *q;
3673 int made_addition = 0;
3675 is_modify_target_name = 1;
3676 for (p = spec_machine, q = new_name; *p != 0; )
3678 if (modify_target[j].add_del == DELETE
3679 && (! strncmp (q, modify_target[j].str,
3680 strlen (modify_target[j].str))))
3681 p += strlen (modify_target[j].str);
3682 else if (modify_target[j].add_del == ADD
3683 && ! made_addition && *p == '-')
3685 for (r = modify_target[j].str; *r != 0; )
3686 *q++ = *r++;
3687 made_addition = 1;
3690 *q++ = *p++;
3693 spec_machine = new_name;
3696 if (is_modify_target_name)
3697 break;
3698 #endif
3700 n_switches++;
3702 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3703 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3704 else if (WORD_SWITCH_TAKES_ARG (p))
3705 i += WORD_SWITCH_TAKES_ARG (p);
3708 else
3710 n_infiles++;
3711 lang_n_infiles++;
3715 if (have_c && have_o && lang_n_infiles > 1)
3716 fatal ("cannot specify -o with -c or -S and multiple compilations");
3718 /* Set up the search paths before we go looking for config files. */
3720 /* These come before the md prefixes so that we will find gcc's subcommands
3721 (such as cpp) rather than those of the host system. */
3722 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3723 as well as trying the machine and the version. */
3724 #ifndef OS2
3725 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3726 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3727 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3728 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3729 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3730 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3731 #endif
3733 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3734 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3735 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3736 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3738 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3739 dir_separator_str, NULL);
3741 /* If tooldir is relative, base it on exec_prefixes. A relative
3742 tooldir lets us move the installed tree as a unit.
3744 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3745 directories, so that we can search both the user specified directory
3746 and the standard place. */
3748 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3750 if (gcc_exec_prefix)
3752 char *gcc_exec_tooldir_prefix
3753 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3754 spec_version, dir_separator_str, tooldir_prefix, NULL);
3756 add_prefix (&exec_prefixes,
3757 concat (gcc_exec_tooldir_prefix, "bin",
3758 dir_separator_str, NULL),
3759 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3760 add_prefix (&startfile_prefixes,
3761 concat (gcc_exec_tooldir_prefix, "lib",
3762 dir_separator_str, NULL),
3763 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3766 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3767 dir_separator_str, spec_version,
3768 dir_separator_str, tooldir_prefix, NULL);
3771 add_prefix (&exec_prefixes,
3772 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
3773 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3774 add_prefix (&startfile_prefixes,
3775 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
3776 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3778 /* More prefixes are enabled in main, after we read the specs file
3779 and determine whether this is cross-compilation or not. */
3781 /* Then create the space for the vectors and scan again. */
3783 switches = ((struct switchstr *)
3784 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3785 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3786 n_switches = 0;
3787 n_infiles = 0;
3788 last_language_n_infiles = -1;
3790 /* This, time, copy the text of each switch and store a pointer
3791 to the copy in the vector of switches.
3792 Store all the infiles in their vector. */
3794 for (i = 1; i < argc; i++)
3796 /* Just skip the switches that were handled by the preceding loop. */
3797 #ifdef MODIFY_TARGET_NAME
3798 is_modify_target_name = 0;
3800 for (j = 0; j < ARRAY_SIZE (modify_target); j++)
3801 if (! strcmp (argv[i], modify_target[j].sw))
3802 is_modify_target_name = 1;
3804 if (is_modify_target_name)
3806 else
3807 #endif
3808 if (! strncmp (argv[i], "-Wa,", 4))
3810 else if (! strncmp (argv[i], "-Wp,", 4))
3812 else if (! strcmp (argv[i], "-pass-exit-codes"))
3814 else if (! strcmp (argv[i], "-print-search-dirs"))
3816 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3818 else if (! strncmp (argv[i], "-print-file-name=", 17))
3820 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3822 else if (! strcmp (argv[i], "-print-multi-lib"))
3824 else if (! strcmp (argv[i], "-print-multi-directory"))
3826 else if (! strcmp (argv[i], "-ftarget-help"))
3828 else if (! strcmp (argv[i], "-fhelp"))
3830 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3832 /* Compensate for the +e options to the C++ front-end;
3833 they're there simply for cfront call-compatibility. We do
3834 some magic in default_compilers to pass them down properly.
3835 Note we deliberately start at the `+' here, to avoid passing
3836 -e0 or -e1 down into the linker. */
3837 switches[n_switches].part1 = &argv[i][0];
3838 switches[n_switches].args = 0;
3839 switches[n_switches].live_cond = SWITCH_OK;
3840 switches[n_switches].validated = 0;
3841 n_switches++;
3843 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3845 int prev, j;
3846 /* Split the argument at commas. */
3847 prev = 4;
3848 for (j = 4; argv[i][j]; j++)
3849 if (argv[i][j] == ',')
3851 infiles[n_infiles].language = "*";
3852 infiles[n_infiles++].name
3853 = save_string (argv[i] + prev, j - prev);
3854 prev = j + 1;
3856 /* Record the part after the last comma. */
3857 infiles[n_infiles].language = "*";
3858 infiles[n_infiles++].name = argv[i] + prev;
3860 else if (strcmp (argv[i], "-Xlinker") == 0)
3862 infiles[n_infiles].language = "*";
3863 infiles[n_infiles++].name = argv[++i];
3865 else if (strcmp (argv[i], "-l") == 0)
3866 { /* POSIX allows separation of -l and the lib arg;
3867 canonicalize by concatenating -l with its arg */
3868 infiles[n_infiles].language = "*";
3869 infiles[n_infiles++].name = concat ("-l", argv[++i], NULL);
3871 else if (strncmp (argv[i], "-l", 2) == 0)
3873 infiles[n_infiles].language = "*";
3874 infiles[n_infiles++].name = argv[i];
3876 else if (strcmp (argv[i], "-specs") == 0)
3877 i++;
3878 else if (strncmp (argv[i], "-specs=", 7) == 0)
3880 else if (strcmp (argv[i], "-time") == 0)
3882 else if ((save_temps_flag || report_times)
3883 && strcmp (argv[i], "-pipe") == 0)
3885 /* -save-temps overrides -pipe, so that temp files are produced */
3886 if (save_temps_flag)
3887 error ("warning: -pipe ignored because -save-temps specified");
3888 /* -time overrides -pipe because we can't get correct stats when
3889 multiple children are running at once. */
3890 else if (report_times)
3891 error ("warning: -pipe ignored because -time specified");
3893 else if (strcmp (argv[i], "-###") == 0)
3895 else if (argv[i][0] == '-' && argv[i][1] != 0)
3897 const char *p = &argv[i][1];
3898 int c = *p;
3900 if (c == 'x')
3902 if (p[1] == 0 && i + 1 == argc)
3903 fatal ("argument to `-x' is missing");
3904 if (p[1] == 0)
3905 spec_lang = argv[++i];
3906 else
3907 spec_lang = p + 1;
3908 if (! strcmp (spec_lang, "none"))
3909 /* Suppress the warning if -xnone comes after the last input
3910 file, because alternate command interfaces like g++ might
3911 find it useful to place -xnone after each input file. */
3912 spec_lang = 0;
3913 else
3914 last_language_n_infiles = n_infiles;
3915 continue;
3917 switches[n_switches].part1 = p;
3918 /* Deal with option arguments in separate argv elements. */
3919 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3920 || WORD_SWITCH_TAKES_ARG (p))
3922 int j = 0;
3923 int n_args = WORD_SWITCH_TAKES_ARG (p);
3925 if (n_args == 0)
3927 /* Count only the option arguments in separate argv elements. */
3928 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3930 if (i + n_args >= argc)
3931 fatal ("argument to `-%s' is missing", p);
3932 switches[n_switches].args
3933 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3934 while (j < n_args)
3935 switches[n_switches].args[j++] = argv[++i];
3936 /* Null-terminate the vector. */
3937 switches[n_switches].args[j] = 0;
3939 else if (strchr (switches_need_spaces, c))
3941 /* On some systems, ld cannot handle some options without
3942 a space. So split the option from its argument. */
3943 char *part1 = (char *) xmalloc (2);
3944 part1[0] = c;
3945 part1[1] = '\0';
3947 switches[n_switches].part1 = part1;
3948 switches[n_switches].args
3949 = (const char **) xmalloc (2 * sizeof (const char *));
3950 switches[n_switches].args[0] = xstrdup (p+1);
3951 switches[n_switches].args[1] = 0;
3953 else
3954 switches[n_switches].args = 0;
3956 switches[n_switches].live_cond = SWITCH_OK;
3957 switches[n_switches].validated = 0;
3958 switches[n_switches].ordering = 0;
3959 /* These are always valid, since gcc.c itself understands it. */
3960 if (!strcmp (p, "save-temps")
3961 || !strcmp (p, "static-libgcc")
3962 || !strcmp (p, "shared-libgcc"))
3963 switches[n_switches].validated = 1;
3964 else
3966 char ch = switches[n_switches].part1[0];
3967 if (ch == 'b' || ch == 'B')
3968 switches[n_switches].validated = 1;
3970 n_switches++;
3972 else
3974 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3975 argv[i] = convert_filename (argv[i], 0, access (argv[i], F_OK));
3976 #endif
3978 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
3980 perror_with_name (argv[i]);
3981 error_count++;
3983 else
3985 infiles[n_infiles].language = spec_lang;
3986 infiles[n_infiles++].name = argv[i];
3991 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3992 error ("warning: `-x %s' after last input file has no effect", spec_lang);
3994 /* Ensure we only invoke each subprocess once. */
3995 if (target_help_flag || print_help_list)
3997 n_infiles = 1;
3999 /* Create a dummy input file, so that we can pass --target-help on to
4000 the various sub-processes. */
4001 infiles[0].language = "c";
4002 infiles[0].name = "help-dummy";
4004 if (target_help_flag)
4006 switches[n_switches].part1 = "--target-help";
4007 switches[n_switches].args = 0;
4008 switches[n_switches].live_cond = SWITCH_OK;
4009 switches[n_switches].validated = 0;
4011 n_switches++;
4014 if (print_help_list)
4016 switches[n_switches].part1 = "--help";
4017 switches[n_switches].args = 0;
4018 switches[n_switches].live_cond = SWITCH_OK;
4019 switches[n_switches].validated = 0;
4021 n_switches++;
4025 switches[n_switches].part1 = 0;
4026 infiles[n_infiles].name = 0;
4029 /* Store switches not filtered out by %{<S} in spec in COLLECT_GCC_OPTIONS
4030 and place that in the environment. */
4032 static void
4033 set_collect_gcc_options ()
4035 int i;
4036 int first_time;
4038 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4039 the compiler. */
4040 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4041 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
4043 first_time = TRUE;
4044 for (i = 0; (int) i < n_switches; i++)
4046 const char *const *args;
4047 const char *p, *q;
4048 if (!first_time)
4049 obstack_grow (&collect_obstack, " ", 1);
4051 first_time = FALSE;
4053 /* Ignore elided switches. */
4054 if (switches[i].live_cond == SWITCH_IGNORE)
4055 continue;
4057 obstack_grow (&collect_obstack, "'-", 2);
4058 q = switches[i].part1;
4059 while ((p = strchr (q, '\'')))
4061 obstack_grow (&collect_obstack, q, p - q);
4062 obstack_grow (&collect_obstack, "'\\''", 4);
4063 q = ++p;
4065 obstack_grow (&collect_obstack, q, strlen (q));
4066 obstack_grow (&collect_obstack, "'", 1);
4068 for (args = switches[i].args; args && *args; args++)
4070 obstack_grow (&collect_obstack, " '", 2);
4071 q = *args;
4072 while ((p = strchr (q, '\'')))
4074 obstack_grow (&collect_obstack, q, p - q);
4075 obstack_grow (&collect_obstack, "'\\''", 4);
4076 q = ++p;
4078 obstack_grow (&collect_obstack, q, strlen (q));
4079 obstack_grow (&collect_obstack, "'", 1);
4082 obstack_grow (&collect_obstack, "\0", 1);
4083 putenv (obstack_finish (&collect_obstack));
4086 /* Process a spec string, accumulating and running commands. */
4088 /* These variables describe the input file name.
4089 input_file_number is the index on outfiles of this file,
4090 so that the output file name can be stored for later use by %o.
4091 input_basename is the start of the part of the input file
4092 sans all directory names, and basename_length is the number
4093 of characters starting there excluding the suffix .c or whatever. */
4095 const char *input_filename;
4096 static int input_file_number;
4097 size_t input_filename_length;
4098 static int basename_length;
4099 static int suffixed_basename_length;
4100 static const char *input_basename;
4101 static const char *input_suffix;
4102 static struct stat input_stat;
4103 static int input_stat_set;
4105 /* The compiler used to process the current input file. */
4106 static struct compiler *input_file_compiler;
4108 /* These are variables used within do_spec and do_spec_1. */
4110 /* Nonzero if an arg has been started and not yet terminated
4111 (with space, tab or newline). */
4112 static int arg_going;
4114 /* Nonzero means %d or %g has been seen; the next arg to be terminated
4115 is a temporary file name. */
4116 static int delete_this_arg;
4118 /* Nonzero means %w has been seen; the next arg to be terminated
4119 is the output file name of this compilation. */
4120 static int this_is_output_file;
4122 /* Nonzero means %s has been seen; the next arg to be terminated
4123 is the name of a library file and we should try the standard
4124 search dirs for it. */
4125 static int this_is_library_file;
4127 /* Nonzero means that the input of this command is coming from a pipe. */
4128 static int input_from_pipe;
4130 /* Nonnull means substitute this for any suffix when outputting a switches
4131 arguments. */
4132 static const char *suffix_subst;
4134 /* Process the spec SPEC and run the commands specified therein.
4135 Returns 0 if the spec is successfully processed; -1 if failed. */
4138 do_spec (spec)
4139 const char *spec;
4141 int value;
4143 value = do_spec_2 (spec);
4145 /* Force out any unfinished command.
4146 If -pipe, this forces out the last command if it ended in `|'. */
4147 if (value == 0)
4149 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4150 argbuf_index--;
4152 set_collect_gcc_options ();
4154 if (argbuf_index > 0)
4155 value = execute ();
4158 return value;
4161 static int
4162 do_spec_2 (spec)
4163 const char *spec;
4165 clear_args ();
4166 arg_going = 0;
4167 delete_this_arg = 0;
4168 this_is_output_file = 0;
4169 this_is_library_file = 0;
4170 input_from_pipe = 0;
4171 suffix_subst = NULL;
4173 return do_spec_1 (spec, 0, NULL);
4176 /* Process the sub-spec SPEC as a portion of a larger spec.
4177 This is like processing a whole spec except that we do
4178 not initialize at the beginning and we do not supply a
4179 newline by default at the end.
4180 INSWITCH nonzero means don't process %-sequences in SPEC;
4181 in this case, % is treated as an ordinary character.
4182 This is used while substituting switches.
4183 INSWITCH nonzero also causes SPC not to terminate an argument.
4185 Value is zero unless a line was finished
4186 and the command on that line reported an error. */
4188 static int
4189 do_spec_1 (spec, inswitch, soft_matched_part)
4190 const char *spec;
4191 int inswitch;
4192 const char *soft_matched_part;
4194 const char *p = spec;
4195 int c;
4196 int i;
4197 const char *string;
4198 int value;
4200 while ((c = *p++))
4201 /* If substituting a switch, treat all chars like letters.
4202 Otherwise, NL, SPC, TAB and % are special. */
4203 switch (inswitch ? 'a' : c)
4205 case '\n':
4206 /* End of line: finish any pending argument,
4207 then run the pending command if one has been started. */
4208 if (arg_going)
4210 obstack_1grow (&obstack, 0);
4211 string = obstack_finish (&obstack);
4212 if (this_is_library_file)
4213 string = find_file (string);
4214 store_arg (string, delete_this_arg, this_is_output_file);
4215 if (this_is_output_file)
4216 outfiles[input_file_number] = string;
4218 arg_going = 0;
4220 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4222 for (i = 0; i < n_switches; i++)
4223 if (!strcmp (switches[i].part1, "pipe"))
4224 break;
4226 /* A `|' before the newline means use a pipe here,
4227 but only if -pipe was specified.
4228 Otherwise, execute now and don't pass the `|' as an arg. */
4229 if (i < n_switches)
4231 input_from_pipe = 1;
4232 switches[i].validated = 1;
4233 break;
4235 else
4236 argbuf_index--;
4239 set_collect_gcc_options ();
4241 if (argbuf_index > 0)
4243 value = execute ();
4244 if (value)
4245 return value;
4247 /* Reinitialize for a new command, and for a new argument. */
4248 clear_args ();
4249 arg_going = 0;
4250 delete_this_arg = 0;
4251 this_is_output_file = 0;
4252 this_is_library_file = 0;
4253 input_from_pipe = 0;
4254 break;
4256 case '|':
4257 /* End any pending argument. */
4258 if (arg_going)
4260 obstack_1grow (&obstack, 0);
4261 string = obstack_finish (&obstack);
4262 if (this_is_library_file)
4263 string = find_file (string);
4264 store_arg (string, delete_this_arg, this_is_output_file);
4265 if (this_is_output_file)
4266 outfiles[input_file_number] = string;
4269 /* Use pipe */
4270 obstack_1grow (&obstack, c);
4271 arg_going = 1;
4272 break;
4274 case '\t':
4275 case ' ':
4276 /* Space or tab ends an argument if one is pending. */
4277 if (arg_going)
4279 obstack_1grow (&obstack, 0);
4280 string = obstack_finish (&obstack);
4281 if (this_is_library_file)
4282 string = find_file (string);
4283 store_arg (string, delete_this_arg, this_is_output_file);
4284 if (this_is_output_file)
4285 outfiles[input_file_number] = string;
4287 /* Reinitialize for a new argument. */
4288 arg_going = 0;
4289 delete_this_arg = 0;
4290 this_is_output_file = 0;
4291 this_is_library_file = 0;
4292 break;
4294 case '%':
4295 switch (c = *p++)
4297 case 0:
4298 fatal ("invalid specification! Bug in cc");
4300 case 'b':
4301 obstack_grow (&obstack, input_basename, basename_length);
4302 arg_going = 1;
4303 break;
4305 case 'B':
4306 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4307 arg_going = 1;
4308 break;
4310 case 'd':
4311 delete_this_arg = 2;
4312 break;
4314 /* Dump out the directories specified with LIBRARY_PATH,
4315 followed by the absolute directories
4316 that we search for startfiles. */
4317 case 'D':
4319 struct prefix_list *pl = startfile_prefixes.plist;
4320 size_t bufsize = 100;
4321 char *buffer = (char *) xmalloc (bufsize);
4322 int idx;
4324 for (; pl; pl = pl->next)
4326 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4327 /* Used on systems which record the specified -L dirs
4328 and use them to search for dynamic linking. */
4329 /* Relative directories always come from -B,
4330 and it is better not to use them for searching
4331 at run time. In particular, stage1 loses. */
4332 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4333 continue;
4334 #endif
4335 /* Try subdirectory if there is one. */
4336 if (multilib_dir != NULL)
4338 if (machine_suffix)
4340 if (strlen (pl->prefix) + strlen (machine_suffix)
4341 >= bufsize)
4342 bufsize = (strlen (pl->prefix)
4343 + strlen (machine_suffix)) * 2 + 1;
4344 buffer = (char *) xrealloc (buffer, bufsize);
4345 strcpy (buffer, pl->prefix);
4346 strcat (buffer, machine_suffix);
4347 if (is_directory (buffer, multilib_dir, 1))
4349 do_spec_1 ("-L", 0, NULL);
4350 #ifdef SPACE_AFTER_L_OPTION
4351 do_spec_1 (" ", 0, NULL);
4352 #endif
4353 do_spec_1 (buffer, 1, NULL);
4354 do_spec_1 (multilib_dir, 1, NULL);
4355 /* Make this a separate argument. */
4356 do_spec_1 (" ", 0, NULL);
4359 if (!pl->require_machine_suffix)
4361 if (is_directory (pl->prefix, multilib_dir, 1))
4363 do_spec_1 ("-L", 0, NULL);
4364 #ifdef SPACE_AFTER_L_OPTION
4365 do_spec_1 (" ", 0, NULL);
4366 #endif
4367 do_spec_1 (pl->prefix, 1, NULL);
4368 do_spec_1 (multilib_dir, 1, NULL);
4369 /* Make this a separate argument. */
4370 do_spec_1 (" ", 0, NULL);
4374 if (machine_suffix)
4376 if (is_directory (pl->prefix, machine_suffix, 1))
4378 do_spec_1 ("-L", 0, NULL);
4379 #ifdef SPACE_AFTER_L_OPTION
4380 do_spec_1 (" ", 0, NULL);
4381 #endif
4382 do_spec_1 (pl->prefix, 1, NULL);
4383 /* Remove slash from machine_suffix. */
4384 if (strlen (machine_suffix) >= bufsize)
4385 bufsize = strlen (machine_suffix) * 2 + 1;
4386 buffer = (char *) xrealloc (buffer, bufsize);
4387 strcpy (buffer, machine_suffix);
4388 idx = strlen (buffer);
4389 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4390 buffer[idx - 1] = 0;
4391 do_spec_1 (buffer, 1, NULL);
4392 /* Make this a separate argument. */
4393 do_spec_1 (" ", 0, NULL);
4396 if (!pl->require_machine_suffix)
4398 if (is_directory (pl->prefix, "", 1))
4400 do_spec_1 ("-L", 0, NULL);
4401 #ifdef SPACE_AFTER_L_OPTION
4402 do_spec_1 (" ", 0, NULL);
4403 #endif
4404 /* Remove slash from pl->prefix. */
4405 if (strlen (pl->prefix) >= bufsize)
4406 bufsize = strlen (pl->prefix) * 2 + 1;
4407 buffer = (char *) xrealloc (buffer, bufsize);
4408 strcpy (buffer, pl->prefix);
4409 idx = strlen (buffer);
4410 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4411 buffer[idx - 1] = 0;
4412 do_spec_1 (buffer, 1, NULL);
4413 /* Make this a separate argument. */
4414 do_spec_1 (" ", 0, NULL);
4418 free (buffer);
4420 break;
4422 case 'e':
4423 /* %efoo means report an error with `foo' as error message
4424 and don't execute any more commands for this file. */
4426 const char *q = p;
4427 char *buf;
4428 while (*p != 0 && *p != '\n')
4429 p++;
4430 buf = (char *) alloca (p - q + 1);
4431 strncpy (buf, q, p - q);
4432 buf[p - q] = 0;
4433 error ("%s", buf);
4434 return -1;
4436 break;
4437 case 'n':
4438 /* %nfoo means report an notice with `foo' on stderr. */
4440 const char *q = p;
4441 char *buf;
4442 while (*p != 0 && *p != '\n')
4443 p++;
4444 buf = (char *) alloca (p - q + 1);
4445 strncpy (buf, q, p - q);
4446 buf[p - q] = 0;
4447 notice ("%s\n", buf);
4448 if (*p)
4449 p++;
4451 break;
4453 case 'j':
4455 struct stat st;
4457 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4458 and it is not a directory, and it is writable, use it.
4459 Otherwise, fall through and treat this like any other
4460 temporary file. */
4462 if ((!save_temps_flag)
4463 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4464 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4466 obstack_grow (&obstack, HOST_BIT_BUCKET,
4467 strlen (HOST_BIT_BUCKET));
4468 delete_this_arg = 0;
4469 arg_going = 1;
4470 break;
4473 case 'g':
4474 case 'u':
4475 case 'U':
4477 struct temp_name *t;
4478 int suffix_length;
4479 const char *suffix = p;
4480 char *saved_suffix = NULL;
4482 while (*p == '.' || ISALPHA ((unsigned char) *p))
4483 p++;
4484 suffix_length = p - suffix;
4485 if (p[0] == '%' && p[1] == 'O')
4487 p += 2;
4488 /* We don't support extra suffix characters after %O. */
4489 if (*p == '.' || ISALPHA ((unsigned char) *p))
4490 abort ();
4491 if (suffix_length == 0)
4492 suffix = TARGET_OBJECT_SUFFIX;
4493 else
4495 saved_suffix
4496 = (char *) xmalloc (suffix_length
4497 + strlen (TARGET_OBJECT_SUFFIX));
4498 strncpy (saved_suffix, suffix, suffix_length);
4499 strcpy (saved_suffix + suffix_length,
4500 TARGET_OBJECT_SUFFIX);
4502 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
4505 /* If the input_filename has the same suffix specified
4506 for the %g, %u, or %U, and -save-temps is specified,
4507 we could end up using that file as an intermediate
4508 thus clobbering the user's source file (.e.g.,
4509 gcc -save-temps foo.s would clobber foo.s with the
4510 output of cpp0). So check for this condition and
4511 generate a temp file as the intermediate. */
4513 if (save_temps_flag)
4515 temp_filename_length = basename_length + suffix_length;
4516 temp_filename = alloca (temp_filename_length + 1);
4517 strncpy ((char *) temp_filename, input_basename, basename_length);
4518 strncpy ((char *) temp_filename + basename_length, suffix,
4519 suffix_length);
4520 *((char *) temp_filename + temp_filename_length) = '\0';
4521 if (strcmp (temp_filename, input_filename) != 0)
4523 struct stat st_temp;
4525 /* Note, set_input() resets input_stat_set to 0. */
4526 if (input_stat_set == 0)
4528 input_stat_set = stat (input_filename, &input_stat);
4529 if (input_stat_set >= 0)
4530 input_stat_set = 1;
4533 /* If we have the stat for the input_filename
4534 and we can do the stat for the temp_filename
4535 then the they could still refer to the same
4536 file if st_dev/st_ino's are the same. */
4538 if (input_stat_set != 1
4539 || stat (temp_filename, &st_temp) < 0
4540 || input_stat.st_dev != st_temp.st_dev
4541 || input_stat.st_ino != st_temp.st_ino)
4543 temp_filename = save_string (temp_filename,
4544 temp_filename_length + 1);
4545 obstack_grow (&obstack, temp_filename,
4546 temp_filename_length);
4547 arg_going = 1;
4548 break;
4553 /* See if we already have an association of %g/%u/%U and
4554 suffix. */
4555 for (t = temp_names; t; t = t->next)
4556 if (t->length == suffix_length
4557 && strncmp (t->suffix, suffix, suffix_length) == 0
4558 && t->unique == (c != 'g'))
4559 break;
4561 /* Make a new association if needed. %u and %j
4562 require one. */
4563 if (t == 0 || c == 'u' || c == 'j')
4565 if (t == 0)
4567 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4568 t->next = temp_names;
4569 temp_names = t;
4571 t->length = suffix_length;
4572 if (saved_suffix)
4574 t->suffix = saved_suffix;
4575 saved_suffix = NULL;
4577 else
4578 t->suffix = save_string (suffix, suffix_length);
4579 t->unique = (c != 'g');
4580 temp_filename = make_temp_file (t->suffix);
4581 temp_filename_length = strlen (temp_filename);
4582 t->filename = temp_filename;
4583 t->filename_length = temp_filename_length;
4586 if (saved_suffix)
4587 free (saved_suffix);
4589 obstack_grow (&obstack, t->filename, t->filename_length);
4590 delete_this_arg = 1;
4592 arg_going = 1;
4593 break;
4595 case 'i':
4596 obstack_grow (&obstack, input_filename, input_filename_length);
4597 arg_going = 1;
4598 break;
4600 case 'I':
4602 struct prefix_list *pl = include_prefixes.plist;
4604 if (gcc_exec_prefix)
4606 do_spec_1 ("-iprefix", 1, NULL);
4607 /* Make this a separate argument. */
4608 do_spec_1 (" ", 0, NULL);
4609 do_spec_1 (gcc_exec_prefix, 1, NULL);
4610 do_spec_1 (" ", 0, NULL);
4613 for (; pl; pl = pl->next)
4615 do_spec_1 ("-isystem", 1, NULL);
4616 /* Make this a separate argument. */
4617 do_spec_1 (" ", 0, NULL);
4618 do_spec_1 (pl->prefix, 1, NULL);
4619 do_spec_1 (" ", 0, NULL);
4622 break;
4624 case 'o':
4626 int max = n_infiles;
4627 max += lang_specific_extra_outfiles;
4629 for (i = 0; i < max; i++)
4630 if (outfiles[i])
4631 store_arg (outfiles[i], 0, 0);
4632 break;
4635 case 'O':
4636 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
4637 arg_going = 1;
4638 break;
4640 case 's':
4641 this_is_library_file = 1;
4642 break;
4644 case 'w':
4645 this_is_output_file = 1;
4646 break;
4648 case 'W':
4650 int cur_index = argbuf_index;
4651 /* Handle the {...} following the %W. */
4652 if (*p != '{')
4653 abort ();
4654 p = handle_braces (p + 1);
4655 if (p == 0)
4656 return -1;
4657 /* If any args were output, mark the last one for deletion
4658 on failure. */
4659 if (argbuf_index != cur_index)
4660 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4661 break;
4664 /* %x{OPTION} records OPTION for %X to output. */
4665 case 'x':
4667 const char *p1 = p;
4668 char *string;
4670 /* Skip past the option value and make a copy. */
4671 if (*p != '{')
4672 abort ();
4673 while (*p++ != '}')
4675 string = save_string (p1 + 1, p - p1 - 2);
4677 /* See if we already recorded this option. */
4678 for (i = 0; i < n_linker_options; i++)
4679 if (! strcmp (string, linker_options[i]))
4681 free (string);
4682 return 0;
4685 /* This option is new; add it. */
4686 add_linker_option (string, strlen (string));
4688 break;
4690 /* Dump out the options accumulated previously using %x. */
4691 case 'X':
4692 for (i = 0; i < n_linker_options; i++)
4694 do_spec_1 (linker_options[i], 1, NULL);
4695 /* Make each accumulated option a separate argument. */
4696 do_spec_1 (" ", 0, NULL);
4698 break;
4700 /* Dump out the options accumulated previously using -Wa,. */
4701 case 'Y':
4702 for (i = 0; i < n_assembler_options; i++)
4704 do_spec_1 (assembler_options[i], 1, NULL);
4705 /* Make each accumulated option a separate argument. */
4706 do_spec_1 (" ", 0, NULL);
4708 break;
4710 /* Dump out the options accumulated previously using -Wp,. */
4711 case 'Z':
4712 for (i = 0; i < n_preprocessor_options; i++)
4714 do_spec_1 (preprocessor_options[i], 1, NULL);
4715 /* Make each accumulated option a separate argument. */
4716 do_spec_1 (" ", 0, NULL);
4718 break;
4720 /* Here are digits and numbers that just process
4721 a certain constant string as a spec. */
4723 case '1':
4724 value = do_spec_1 (cc1_spec, 0, NULL);
4725 if (value != 0)
4726 return value;
4727 break;
4729 case '2':
4730 value = do_spec_1 (cc1plus_spec, 0, NULL);
4731 if (value != 0)
4732 return value;
4733 break;
4735 case 'a':
4736 value = do_spec_1 (asm_spec, 0, NULL);
4737 if (value != 0)
4738 return value;
4739 break;
4741 case 'A':
4742 value = do_spec_1 (asm_final_spec, 0, NULL);
4743 if (value != 0)
4744 return value;
4745 break;
4747 case 'C':
4749 const char *const spec
4750 = (input_file_compiler->cpp_spec
4751 ? input_file_compiler->cpp_spec
4752 : cpp_spec);
4753 value = do_spec_1 (spec, 0, NULL);
4754 if (value != 0)
4755 return value;
4757 break;
4759 case 'E':
4760 value = do_spec_1 (endfile_spec, 0, NULL);
4761 if (value != 0)
4762 return value;
4763 break;
4765 case 'l':
4766 value = do_spec_1 (link_spec, 0, NULL);
4767 if (value != 0)
4768 return value;
4769 break;
4771 case 'L':
4772 value = do_spec_1 (lib_spec, 0, NULL);
4773 if (value != 0)
4774 return value;
4775 break;
4777 case 'G':
4778 value = do_spec_1 (libgcc_spec, 0, NULL);
4779 if (value != 0)
4780 return value;
4781 break;
4783 case 'M':
4784 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4786 char *p;
4787 const char *q;
4788 size_t len;
4790 len = strlen (multilib_dir);
4791 obstack_blank (&obstack, len + 1);
4792 p = obstack_next_free (&obstack) - (len + 1);
4794 *p++ = '_';
4795 for (q = multilib_dir; *q ; ++q, ++p)
4796 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4798 break;
4800 case 'p':
4802 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4803 char *buf = x;
4804 const char *y;
4806 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4807 y = cpp_predefines;
4808 while (*y != 0)
4810 if (! strncmp (y, "-D", 2))
4811 /* Copy the whole option. */
4812 while (*y && *y != ' ' && *y != '\t')
4813 *x++ = *y++;
4814 else if (*y == ' ' || *y == '\t')
4815 /* Copy whitespace to the result. */
4816 *x++ = *y++;
4817 /* Don't copy other options. */
4818 else
4819 y++;
4822 *x = 0;
4824 value = do_spec_1 (buf, 0, NULL);
4825 if (value != 0)
4826 return value;
4828 break;
4830 case 'P':
4832 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4833 char *buf = x;
4834 const char *y;
4836 /* Copy all of CPP_PREDEFINES into BUF,
4837 but force them all into the reserved name space if they
4838 aren't already there. The reserved name space is all
4839 identifiers beginning with two underscores or with one
4840 underscore and a capital letter. We do the forcing by
4841 adding up to two underscores to the beginning and end
4842 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4843 become __mips__. */
4844 y = cpp_predefines;
4845 while (*y != 0)
4847 if (! strncmp (y, "-D", 2))
4849 int flag = 0;
4851 *x++ = *y++;
4852 *x++ = *y++;
4854 if (*y != '_'
4855 || (*(y + 1) != '_'
4856 && ! ISUPPER ((unsigned char) *(y + 1))))
4858 /* Stick __ at front of macro name. */
4859 if (*y != '_')
4860 *x++ = '_';
4861 *x++ = '_';
4862 /* Arrange to stick __ at the end as well. */
4863 flag = 1;
4866 /* Copy the macro name. */
4867 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4868 *x++ = *y++;
4870 if (flag)
4872 if (x[-1] != '_')
4874 if (x[-2] != '_')
4875 *x++ = '_';
4876 *x++ = '_';
4880 /* Copy the value given, if any. */
4881 while (*y && *y != ' ' && *y != '\t')
4882 *x++ = *y++;
4884 else if (*y == ' ' || *y == '\t')
4885 /* Copy whitespace to the result. */
4886 *x++ = *y++;
4887 /* Don't copy -A options */
4888 else
4889 y++;
4891 *x++ = ' ';
4893 /* Copy all of CPP_PREDEFINES into BUF,
4894 but put __ after every -D. */
4895 y = cpp_predefines;
4896 while (*y != 0)
4898 if (! strncmp (y, "-D", 2))
4900 y += 2;
4902 if (*y != '_'
4903 || (*(y + 1) != '_'
4904 && ! ISUPPER ((unsigned char) *(y + 1))))
4906 /* Stick -D__ at front of macro name. */
4907 *x++ = '-';
4908 *x++ = 'D';
4909 if (*y != '_')
4910 *x++ = '_';
4911 *x++ = '_';
4913 /* Copy the macro name. */
4914 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4915 *x++ = *y++;
4917 /* Copy the value given, if any. */
4918 while (*y && *y != ' ' && *y != '\t')
4919 *x++ = *y++;
4921 else
4923 /* Do not copy this macro - we have just done it before */
4924 while (*y && *y != ' ' && *y != '\t')
4925 y++;
4928 else if (*y == ' ' || *y == '\t')
4929 /* Copy whitespace to the result. */
4930 *x++ = *y++;
4931 /* Don't copy -A options. */
4932 else
4933 y++;
4935 *x++ = ' ';
4937 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4938 y = cpp_predefines;
4939 while (*y != 0)
4941 if (! strncmp (y, "-A", 2))
4942 /* Copy the whole option. */
4943 while (*y && *y != ' ' && *y != '\t')
4944 *x++ = *y++;
4945 else if (*y == ' ' || *y == '\t')
4946 /* Copy whitespace to the result. */
4947 *x++ = *y++;
4948 /* Don't copy other options. */
4949 else
4950 y++;
4953 *x = 0;
4955 value = do_spec_1 (buf, 0, NULL);
4956 if (value != 0)
4957 return value;
4959 break;
4961 case 'S':
4962 value = do_spec_1 (startfile_spec, 0, NULL);
4963 if (value != 0)
4964 return value;
4965 break;
4967 /* Here we define characters other than letters and digits. */
4969 case '{':
4970 p = handle_braces (p);
4971 if (p == 0)
4972 return -1;
4973 break;
4975 case '%':
4976 obstack_1grow (&obstack, '%');
4977 break;
4979 case '.':
4981 unsigned len = 0;
4983 while (p[len] && p[len] != ' ' && p[len] != '%')
4984 len++;
4985 suffix_subst = save_string (p - 1, len + 1);
4986 p += len;
4988 break;
4990 case '*':
4991 if (soft_matched_part)
4993 do_spec_1 (soft_matched_part, 1, NULL);
4994 do_spec_1 (" ", 0, NULL);
4996 else
4997 /* Catch the case where a spec string contains something like
4998 '%{foo:%*}'. ie there is no * in the pattern on the left
4999 hand side of the :. */
5000 error ("spec failure: '%%*' has not been initialized by pattern match");
5001 break;
5003 /* Process a string found as the value of a spec given by name.
5004 This feature allows individual machine descriptions
5005 to add and use their own specs.
5006 %[...] modifies -D options the way %P does;
5007 %(...) uses the spec unmodified. */
5008 case '[':
5009 error ("warning: use of obsolete %%[ operator in specs");
5010 case '(':
5012 const char *name = p;
5013 struct spec_list *sl;
5014 int len;
5016 /* The string after the S/P is the name of a spec that is to be
5017 processed. */
5018 while (*p && *p != ')' && *p != ']')
5019 p++;
5021 /* See if it's in the list. */
5022 for (len = p - name, sl = specs; sl; sl = sl->next)
5023 if (sl->name_len == len && !strncmp (sl->name, name, len))
5025 name = *(sl->ptr_spec);
5026 #ifdef DEBUG_SPECS
5027 notice ("Processing spec %c%s%c, which is '%s'\n",
5028 c, sl->name, (c == '(') ? ')' : ']', name);
5029 #endif
5030 break;
5033 if (sl)
5035 if (c == '(')
5037 value = do_spec_1 (name, 0, NULL);
5038 if (value != 0)
5039 return value;
5041 else
5043 char *x = (char *) alloca (strlen (name) * 2 + 1);
5044 char *buf = x;
5045 const char *y = name;
5046 int flag = 0;
5048 /* Copy all of NAME into BUF, but put __ after
5049 every -D and at the end of each arg. */
5050 while (1)
5052 if (! strncmp (y, "-D", 2))
5054 *x++ = '-';
5055 *x++ = 'D';
5056 *x++ = '_';
5057 *x++ = '_';
5058 y += 2;
5059 flag = 1;
5060 continue;
5062 else if (flag
5063 && (*y == ' ' || *y == '\t' || *y == '='
5064 || *y == '}' || *y == 0))
5066 *x++ = '_';
5067 *x++ = '_';
5068 flag = 0;
5070 if (*y == 0)
5071 break;
5072 else
5073 *x++ = *y++;
5075 *x = 0;
5077 value = do_spec_1 (buf, 0, NULL);
5078 if (value != 0)
5079 return value;
5083 /* Discard the closing paren or bracket. */
5084 if (*p)
5085 p++;
5087 break;
5089 case 'v':
5091 int c1 = *p++; /* Select first or second version number. */
5092 const char *v = compiler_version;
5093 const char *q;
5094 static const char zeroc = '0';
5096 /* The format of the version string is
5097 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
5099 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
5100 while (! ISDIGIT (*v))
5101 v++;
5102 if (v > compiler_version && v[-1] != '-')
5103 abort ();
5105 /* If desired, advance to second version number. */
5106 if (c1 >= '2')
5108 /* Set V after the first period. */
5109 while (ISDIGIT (*v))
5110 v++;
5111 if (*v != '.')
5112 abort ();
5113 v++;
5116 /* If desired, advance to third version number.
5117 But don't complain if it's not present */
5118 if (c1 == '3')
5120 /* Set V after the second period. */
5121 while (ISDIGIT (*v))
5122 v++;
5123 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
5124 abort ();
5125 if (*v != 0)
5126 v++;
5129 /* Set Q at the next period or at the end. */
5130 q = v;
5131 while (ISDIGIT (*q))
5132 q++;
5133 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
5134 abort ();
5136 if (q > v)
5137 /* Put that part into the command. */
5138 obstack_grow (&obstack, v, q - v);
5139 else
5140 /* Default to "0" */
5141 obstack_grow (&obstack, &zeroc, 1);
5142 arg_going = 1;
5144 break;
5146 case '|':
5147 if (input_from_pipe)
5148 do_spec_1 ("-", 0, NULL);
5149 break;
5151 default:
5152 error ("spec failure: unrecognized spec option '%c'", c);
5153 break;
5155 break;
5157 case '\\':
5158 /* Backslash: treat next character as ordinary. */
5159 c = *p++;
5161 /* fall through */
5162 default:
5163 /* Ordinary character: put it into the current argument. */
5164 obstack_1grow (&obstack, c);
5165 arg_going = 1;
5168 /* End of string. */
5169 return 0;
5172 /* Return 0 if we call do_spec_1 and that returns -1. */
5174 static const char *
5175 handle_braces (p)
5176 const char *p;
5178 const char *filter, *body = NULL, *endbody = NULL;
5179 int pipe_p = 0;
5180 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
5181 int negate;
5182 int suffix;
5183 int include_blanks = 1;
5184 int elide_switch = 0;
5185 int ordered = 0;
5187 if (*p == '^')
5189 /* A '^' after the open-brace means to not give blanks before args. */
5190 include_blanks = 0;
5191 ++p;
5194 if (*p == '|')
5196 /* A `|' after the open-brace means,
5197 if the test fails, output a single minus sign rather than nothing.
5198 This is used in %{|!pipe:...}. */
5199 pipe_p = 1;
5200 ++p;
5203 if (*p == '<')
5205 /* A `<' after the open-brace means that the switch should be
5206 removed from the command-line. */
5207 elide_switch = 1;
5208 ++p;
5211 next_member:
5212 negate = suffix = 0;
5214 if (*p == '!')
5215 /* A `!' after the open-brace negates the condition:
5216 succeed if the specified switch is not present. */
5217 negate = 1, ++p;
5219 if (*p == '.')
5220 /* A `.' after the open-brace means test against the current suffix. */
5222 if (pipe_p)
5223 abort ();
5225 suffix = 1;
5226 ++p;
5229 if (elide_switch && (negate || pipe_p || suffix))
5231 /* It doesn't make sense to mix elision with other flags. We
5232 could fatal() here, but the standard seems to be to abort. */
5233 abort ();
5236 next_ampersand:
5237 filter = p;
5238 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
5239 p++;
5241 if (*p == '|' && (pipe_p || ordered))
5242 abort ();
5244 if (!body)
5246 if (*p != '}' && *p != '&')
5248 int count = 1;
5249 const char *q = p;
5251 while (*q++ != ':')
5252 continue;
5253 body = q;
5255 while (count > 0)
5257 if (*q == '{')
5258 count++;
5259 else if (*q == '}')
5260 count--;
5261 else if (*q == 0)
5262 fatal ("mismatched braces in specs");
5263 q++;
5265 endbody = q;
5267 else
5268 body = p, endbody = p + 1;
5271 if (suffix)
5273 int found = (input_suffix != 0
5274 && (long) strlen (input_suffix) == (long) (p - filter)
5275 && strncmp (input_suffix, filter, p - filter) == 0);
5277 if (body[0] == '}')
5278 abort ();
5280 if (negate != found
5281 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL) < 0)
5282 return 0;
5284 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5286 /* Substitute all matching switches as separate args. */
5287 int i;
5289 for (i = 0; i < n_switches; i++)
5290 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5291 && check_live_switch (i, p - 1 - filter))
5293 if (elide_switch)
5295 switches[i].live_cond = SWITCH_IGNORE;
5296 switches[i].validated = 1;
5298 else
5299 ordered = 1, switches[i].ordering = 1;
5302 else
5304 /* Test for presence of the specified switch. */
5305 int i;
5306 int present = 0;
5308 /* If name specified ends in *, as in {x*:...},
5309 check for %* and handle that case. */
5310 if (p[-1] == '*' && !negate)
5312 int substitution;
5313 const char *r = body;
5315 /* First see whether we have %*. */
5316 substitution = 0;
5317 while (r < endbody)
5319 if (*r == '%' && r[1] == '*')
5320 substitution = 1;
5321 r++;
5323 /* If we do, handle that case. */
5324 if (substitution)
5326 /* Substitute all matching switches as separate args.
5327 But do this by substituting for %*
5328 in the text that follows the colon. */
5330 unsigned hard_match_len = p - filter - 1;
5331 char *string = save_string (body, endbody - body - 1);
5333 for (i = 0; i < n_switches; i++)
5334 if (!strncmp (switches[i].part1, filter, hard_match_len)
5335 && check_live_switch (i, -1))
5337 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5338 /* Pass any arguments this switch has. */
5339 give_switch (i, 1, 1);
5340 suffix_subst = NULL;
5343 /* We didn't match. Try again. */
5344 if (*p++ == '|')
5345 goto next_member;
5346 return endbody;
5350 /* If name specified ends in *, as in {x*:...},
5351 check for presence of any switch name starting with x. */
5352 if (p[-1] == '*')
5354 for (i = 0; i < n_switches; i++)
5356 unsigned hard_match_len = p - filter - 1;
5358 if (!strncmp (switches[i].part1, filter, hard_match_len)
5359 && check_live_switch (i, hard_match_len))
5361 present = 1;
5362 break;
5366 /* Otherwise, check for presence of exact name specified. */
5367 else
5369 for (i = 0; i < n_switches; i++)
5371 if (!strncmp (switches[i].part1, filter, p - filter)
5372 && switches[i].part1[p - filter] == 0
5373 && check_live_switch (i, -1))
5375 present = 1;
5376 break;
5381 /* If it is as desired (present for %{s...}, absent for %{!s...})
5382 then substitute either the switch or the specified
5383 conditional text. */
5384 if (present != negate)
5386 if (elide_switch)
5388 switches[i].live_cond = SWITCH_IGNORE;
5389 switches[i].validated = 1;
5391 else if (ordered || *p == '&')
5392 ordered = 1, switches[i].ordering = 1;
5393 else if (*p == '}')
5394 give_switch (i, 0, include_blanks);
5395 else
5396 /* Even if many alternatives are matched, only output once. */
5397 true_once = 1;
5399 else if (pipe_p)
5401 /* Here if a %{|...} conditional fails: output a minus sign,
5402 which means "standard output" or "standard input". */
5403 do_spec_1 ("-", 0, NULL);
5404 return endbody;
5408 /* We didn't match; try again. */
5409 if (*p++ == '|')
5410 goto next_member;
5412 if (p[-1] == '&')
5414 body = 0;
5415 goto next_ampersand;
5418 if (ordered)
5420 int i;
5421 /* Doing this set of switches later preserves their command-line
5422 ordering. This is needed for e.g. -U, -D and -A. */
5423 for (i = 0; i < n_switches; i++)
5424 if (switches[i].ordering == 1)
5426 switches[i].ordering = 0;
5427 give_switch (i, 0, include_blanks);
5430 /* Process the spec just once, regardless of match count. */
5431 else if (true_once)
5433 if (do_spec_1 (save_string (body, endbody - body - 1),
5434 0, NULL) < 0)
5435 return 0;
5438 return endbody;
5441 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5442 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5443 spec, or -1 if either exact match or %* is used.
5445 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5446 whose value does not begin with "no-" is obsoleted by the same value
5447 with the "no-", similarly for a switch with the "no-" prefix. */
5449 static int
5450 check_live_switch (switchnum, prefix_length)
5451 int switchnum;
5452 int prefix_length;
5454 const char *name = switches[switchnum].part1;
5455 int i;
5457 /* In the common case of {<at-most-one-letter>*}, a negating
5458 switch would always match, so ignore that case. We will just
5459 send the conflicting switches to the compiler phase. */
5460 if (prefix_length >= 0 && prefix_length <= 1)
5461 return 1;
5463 /* If we already processed this switch and determined if it was
5464 live or not, return our past determination. */
5465 if (switches[switchnum].live_cond != 0)
5466 return switches[switchnum].live_cond > 0;
5468 /* Now search for duplicate in a manner that depends on the name. */
5469 switch (*name)
5471 case 'O':
5472 for (i = switchnum + 1; i < n_switches; i++)
5473 if (switches[i].part1[0] == 'O')
5475 switches[switchnum].validated = 1;
5476 switches[switchnum].live_cond = SWITCH_FALSE;
5477 return 0;
5479 break;
5481 case 'W': case 'f': case 'm':
5482 if (! strncmp (name + 1, "no-", 3))
5484 /* We have Xno-YYY, search for XYYY. */
5485 for (i = switchnum + 1; i < n_switches; i++)
5486 if (switches[i].part1[0] == name[0]
5487 && ! strcmp (&switches[i].part1[1], &name[4]))
5489 switches[switchnum].validated = 1;
5490 switches[switchnum].live_cond = SWITCH_FALSE;
5491 return 0;
5494 else
5496 /* We have XYYY, search for Xno-YYY. */
5497 for (i = switchnum + 1; i < n_switches; i++)
5498 if (switches[i].part1[0] == name[0]
5499 && switches[i].part1[1] == 'n'
5500 && switches[i].part1[2] == 'o'
5501 && switches[i].part1[3] == '-'
5502 && !strcmp (&switches[i].part1[4], &name[1]))
5504 switches[switchnum].validated = 1;
5505 switches[switchnum].live_cond = SWITCH_FALSE;
5506 return 0;
5509 break;
5512 /* Otherwise the switch is live. */
5513 switches[switchnum].live_cond = SWITCH_LIVE;
5514 return 1;
5517 /* Pass a switch to the current accumulating command
5518 in the same form that we received it.
5519 SWITCHNUM identifies the switch; it is an index into
5520 the vector of switches gcc received, which is `switches'.
5521 This cannot fail since it never finishes a command line.
5523 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5525 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5526 of the switch. */
5528 static void
5529 give_switch (switchnum, omit_first_word, include_blanks)
5530 int switchnum;
5531 int omit_first_word;
5532 int include_blanks;
5534 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5535 return;
5537 if (!omit_first_word)
5539 do_spec_1 ("-", 0, NULL);
5540 do_spec_1 (switches[switchnum].part1, 1, NULL);
5543 if (switches[switchnum].args != 0)
5545 const char **p;
5546 for (p = switches[switchnum].args; *p; p++)
5548 const char *arg = *p;
5550 if (include_blanks)
5551 do_spec_1 (" ", 0, NULL);
5552 if (suffix_subst)
5554 unsigned length = strlen (arg);
5555 int dot = 0;
5557 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5558 if (arg[length] == '.')
5560 ((char *)arg)[length] = 0;
5561 dot = 1;
5562 break;
5564 do_spec_1 (arg, 1, NULL);
5565 if (dot)
5566 ((char *)arg)[length] = '.';
5567 do_spec_1 (suffix_subst, 1, NULL);
5569 else
5570 do_spec_1 (arg, 1, NULL);
5574 do_spec_1 (" ", 0, NULL);
5575 switches[switchnum].validated = 1;
5578 /* Search for a file named NAME trying various prefixes including the
5579 user's -B prefix and some standard ones.
5580 Return the absolute file name found. If nothing is found, return NAME. */
5582 static const char *
5583 find_file (name)
5584 const char *name;
5586 char *newname;
5588 /* Try multilib_dir if it is defined. */
5589 if (multilib_dir != NULL)
5591 const char *const try = ACONCAT ((multilib_dir, dir_separator_str, name, NULL));
5593 newname = find_a_file (&startfile_prefixes, try, R_OK);
5595 /* If we don't find it in the multi library dir, then fall
5596 through and look for it in the normal places. */
5597 if (newname != NULL)
5598 return newname;
5601 newname = find_a_file (&startfile_prefixes, name, R_OK);
5602 return newname ? newname : name;
5605 /* Determine whether a directory exists. If LINKER, return 0 for
5606 certain fixed names not needed by the linker. If not LINKER, it is
5607 only important to return 0 if the host machine has a small ARG_MAX
5608 limit. */
5610 static int
5611 is_directory (path1, path2, linker)
5612 const char *path1;
5613 const char *path2;
5614 int linker;
5616 int len1 = strlen (path1);
5617 int len2 = strlen (path2);
5618 char *path = (char *) alloca (3 + len1 + len2);
5619 char *cp;
5620 struct stat st;
5622 #ifndef SMALL_ARG_MAX
5623 if (! linker)
5624 return 1;
5625 #endif
5627 /* Construct the path from the two parts. Ensure the string ends with "/.".
5628 The resulting path will be a directory even if the given path is a
5629 symbolic link. */
5630 memcpy (path, path1, len1);
5631 memcpy (path + len1, path2, len2);
5632 cp = path + len1 + len2;
5633 if (!IS_DIR_SEPARATOR (cp[-1]))
5634 *cp++ = DIR_SEPARATOR;
5635 *cp++ = '.';
5636 *cp = '\0';
5638 /* Exclude directories that the linker is known to search. */
5639 if (linker
5640 && ((cp - path == 6
5641 && strcmp (path, concat (dir_separator_str, "lib",
5642 dir_separator_str, ".", NULL)) == 0)
5643 || (cp - path == 10
5644 && strcmp (path, concat (dir_separator_str, "usr",
5645 dir_separator_str, "lib",
5646 dir_separator_str, ".", NULL)) == 0)))
5647 return 0;
5649 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5652 /* Set up the various global variables to indicate that we're processing
5653 the input file named FILENAME. */
5655 void
5656 set_input (filename)
5657 const char *filename;
5659 const char *p;
5661 input_filename = filename;
5662 input_filename_length = strlen (input_filename);
5664 input_basename = input_filename;
5665 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5666 /* Skip drive name so 'x:foo' is handled properly. */
5667 if (input_basename[1] == ':')
5668 input_basename += 2;
5669 #endif
5670 for (p = input_basename; *p; p++)
5671 if (IS_DIR_SEPARATOR (*p))
5672 input_basename = p + 1;
5674 /* Find a suffix starting with the last period,
5675 and set basename_length to exclude that suffix. */
5676 basename_length = strlen (input_basename);
5677 suffixed_basename_length = basename_length;
5678 p = input_basename + basename_length;
5679 while (p != input_basename && *p != '.')
5680 --p;
5681 if (*p == '.' && p != input_basename)
5683 basename_length = p - input_basename;
5684 input_suffix = p + 1;
5686 else
5687 input_suffix = "";
5689 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
5690 we will need to do a stat on the input_filename. The
5691 INPUT_STAT_SET signals that the stat is needed. */
5692 input_stat_set = 0;
5695 /* On fatal signals, delete all the temporary files. */
5697 static void
5698 fatal_error (signum)
5699 int signum;
5701 signal (signum, SIG_DFL);
5702 delete_failure_queue ();
5703 delete_temp_files ();
5704 /* Get the same signal again, this time not handled,
5705 so its normal effect occurs. */
5706 kill (getpid (), signum);
5709 extern int main PARAMS ((int, const char *const *));
5712 main (argc, argv)
5713 int argc;
5714 const char *const *argv;
5716 size_t i;
5717 int value;
5718 int linker_was_run = 0;
5719 char *explicit_link_files;
5720 char *specs_file;
5721 const char *p;
5722 struct user_specs *uptr;
5724 p = argv[0] + strlen (argv[0]);
5725 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5726 --p;
5727 programname = p;
5729 xmalloc_set_program_name (programname);
5731 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5732 /* Perform host dependent initialization when needed. */
5733 GCC_DRIVER_HOST_INITIALIZATION;
5734 #endif
5736 gcc_init_libintl ();
5738 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5739 signal (SIGINT, fatal_error);
5740 #ifdef SIGHUP
5741 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5742 signal (SIGHUP, fatal_error);
5743 #endif
5744 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5745 signal (SIGTERM, fatal_error);
5746 #ifdef SIGPIPE
5747 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5748 signal (SIGPIPE, fatal_error);
5749 #endif
5750 #ifdef SIGCHLD
5751 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5752 receive the signal. A different setting is inheritable */
5753 signal (SIGCHLD, SIG_DFL);
5754 #endif
5756 argbuf_length = 10;
5757 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5759 obstack_init (&obstack);
5761 /* Build multilib_select, et. al from the separate lines that make up each
5762 multilib selection. */
5764 const char *const *q = multilib_raw;
5765 int need_space;
5767 obstack_init (&multilib_obstack);
5768 while ((p = *q++) != (char *) 0)
5769 obstack_grow (&multilib_obstack, p, strlen (p));
5771 obstack_1grow (&multilib_obstack, 0);
5772 multilib_select = obstack_finish (&multilib_obstack);
5774 q = multilib_matches_raw;
5775 while ((p = *q++) != (char *) 0)
5776 obstack_grow (&multilib_obstack, p, strlen (p));
5778 obstack_1grow (&multilib_obstack, 0);
5779 multilib_matches = obstack_finish (&multilib_obstack);
5781 q = multilib_exclusions_raw;
5782 while ((p = *q++) != (char *) 0)
5783 obstack_grow (&multilib_obstack, p, strlen (p));
5785 obstack_1grow (&multilib_obstack, 0);
5786 multilib_exclusions = obstack_finish (&multilib_obstack);
5788 need_space = FALSE;
5789 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5791 if (need_space)
5792 obstack_1grow (&multilib_obstack, ' ');
5793 obstack_grow (&multilib_obstack,
5794 multilib_defaults_raw[i],
5795 strlen (multilib_defaults_raw[i]));
5796 need_space = TRUE;
5799 obstack_1grow (&multilib_obstack, 0);
5800 multilib_defaults = obstack_finish (&multilib_obstack);
5803 /* Set up to remember the pathname of gcc and any options
5804 needed for collect. We use argv[0] instead of programname because
5805 we need the complete pathname. */
5806 obstack_init (&collect_obstack);
5807 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5808 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5809 putenv (obstack_finish (&collect_obstack));
5811 #ifdef INIT_ENVIRONMENT
5812 /* Set up any other necessary machine specific environment variables. */
5813 putenv (INIT_ENVIRONMENT);
5814 #endif
5816 /* Make a table of what switches there are (switches, n_switches).
5817 Make a table of specified input files (infiles, n_infiles).
5818 Decode switches that are handled locally. */
5820 process_command (argc, argv);
5822 /* Initialize the vector of specs to just the default.
5823 This means one element containing 0s, as a terminator. */
5825 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5826 memcpy ((char *) compilers, (char *) default_compilers,
5827 sizeof default_compilers);
5828 n_compilers = n_default_compilers;
5830 /* Read specs from a file if there is one. */
5832 machine_suffix = concat (spec_machine, dir_separator_str,
5833 spec_version, dir_separator_str, NULL);
5834 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
5836 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5837 /* Read the specs file unless it is a default one. */
5838 if (specs_file != 0 && strcmp (specs_file, "specs"))
5839 read_specs (specs_file, TRUE);
5840 else
5841 init_spec ();
5843 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5844 for any override of as, ld and libraries. */
5845 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5846 + strlen (just_machine_suffix)
5847 + sizeof ("specs"));
5849 strcpy (specs_file, standard_exec_prefix);
5850 strcat (specs_file, just_machine_suffix);
5851 strcat (specs_file, "specs");
5852 if (access (specs_file, R_OK) == 0)
5853 read_specs (specs_file, TRUE);
5855 /* If not cross-compiling, look for startfiles in the standard places.
5856 Similarly, don't add the standard prefixes if startfile handling
5857 will be under control of startfile_prefix_spec. */
5858 if (*cross_compile == '0' || *startfile_prefix_spec == 0)
5860 if (*md_exec_prefix)
5862 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5863 PREFIX_PRIORITY_LAST, 0, NULL);
5864 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5865 PREFIX_PRIORITY_LAST, 0, NULL);
5868 if (*md_startfile_prefix)
5869 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5870 PREFIX_PRIORITY_LAST, 0, NULL);
5872 if (*md_startfile_prefix_1)
5873 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5874 PREFIX_PRIORITY_LAST, 0, NULL);
5876 /* If standard_startfile_prefix is relative, base it on
5877 standard_exec_prefix. This lets us move the installed tree
5878 as a unit. If GCC_EXEC_PREFIX is defined, base
5879 standard_startfile_prefix on that as well. */
5880 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5881 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5882 PREFIX_PRIORITY_LAST, 0, NULL);
5883 else
5885 if (gcc_exec_prefix)
5886 add_prefix (&startfile_prefixes,
5887 concat (gcc_exec_prefix, machine_suffix,
5888 standard_startfile_prefix, NULL),
5889 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5890 add_prefix (&startfile_prefixes,
5891 concat (standard_exec_prefix,
5892 machine_suffix,
5893 standard_startfile_prefix, NULL),
5894 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5897 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5898 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5899 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5900 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5901 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5902 add_prefix (&startfile_prefixes, "./", NULL,
5903 PREFIX_PRIORITY_LAST, 1, NULL);
5904 #endif
5906 else
5908 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5909 && gcc_exec_prefix)
5910 add_prefix (&startfile_prefixes,
5911 concat (gcc_exec_prefix, machine_suffix,
5912 standard_startfile_prefix, NULL),
5913 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5916 if (*startfile_prefix_spec != 0
5917 && do_spec_2 (startfile_prefix_spec) == 0
5918 && do_spec_1 (" ", 0, NULL) == 0)
5920 int ndx;
5921 for (ndx = 0; ndx < argbuf_index; ndx++)
5922 add_prefix (&startfile_prefixes, argbuf[ndx], "BINUTILS",
5923 PREFIX_PRIORITY_LAST, 0, NULL);
5926 /* Process any user specified specs in the order given on the command
5927 line. */
5928 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5930 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5931 read_specs (filename ? filename : uptr->filename, FALSE);
5934 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5935 if (gcc_exec_prefix)
5936 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
5937 spec_version, dir_separator_str, NULL);
5939 /* Now we have the specs.
5940 Set the `valid' bits for switches that match anything in any spec. */
5942 validate_all_switches ();
5944 /* Now that we have the switches and the specs, set
5945 the subdirectory based on the options. */
5946 set_multilib_dir ();
5948 /* Warn about any switches that no pass was interested in. */
5950 for (i = 0; (int) i < n_switches; i++)
5951 if (! switches[i].validated)
5952 error ("unrecognized option `-%s'", switches[i].part1);
5954 /* Obey some of the options. */
5956 if (print_search_dirs)
5958 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5959 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5960 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5961 return (0);
5964 if (print_file_name)
5966 printf ("%s\n", find_file (print_file_name));
5967 return (0);
5970 if (print_prog_name)
5972 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5973 printf ("%s\n", (newname ? newname : print_prog_name));
5974 return (0);
5977 if (print_multi_lib)
5979 print_multilib_info ();
5980 return (0);
5983 if (print_multi_directory)
5985 if (multilib_dir == NULL)
5986 printf (".\n");
5987 else
5988 printf ("%s\n", multilib_dir);
5989 return (0);
5992 if (target_help_flag)
5994 /* Print if any target specific options. */
5996 /* We do not exit here. Instead we have created a fake input file
5997 called 'target-dummy' which needs to be compiled, and we pass this
5998 on to the various sub-processes, along with the --target-help
5999 switch. */
6002 if (print_help_list)
6004 display_help ();
6006 if (! verbose_flag)
6008 printf (_("\nFor bug reporting instructions, please see:\n"));
6009 printf ("%s.\n", GCCBUGURL);
6011 return (0);
6014 /* We do not exit here. Instead we have created a fake input file
6015 called 'help-dummy' which needs to be compiled, and we pass this
6016 on the various sub-processes, along with the --help switch. */
6019 if (verbose_flag)
6021 int n;
6022 const char *thrmod;
6024 notice ("Configured with: %s\n", configuration_arguments);
6026 #ifdef THREAD_MODEL_SPEC
6027 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6028 but there's no point in doing all this processing just to get
6029 thread_model back. */
6030 obstack_init (&obstack);
6031 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6032 obstack_1grow (&obstack, '\0');
6033 thrmod = obstack_finish (&obstack);
6034 #else
6035 thrmod = thread_model;
6036 #endif
6038 notice ("Thread model: %s\n", thrmod);
6040 /* compiler_version is truncated at the first space when initialized
6041 from version string, so truncate version_string at the first space
6042 before comparing. */
6043 for (n = 0; version_string[n]; n++)
6044 if (version_string[n] == ' ')
6045 break;
6047 if (! strncmp (version_string, compiler_version, n)
6048 && compiler_version[n] == 0)
6049 notice ("gcc version %s\n", version_string);
6050 else
6051 notice ("gcc driver version %s executing gcc version %s\n",
6052 version_string, compiler_version);
6054 if (n_infiles == 0)
6055 return (0);
6058 if (n_infiles == added_libraries)
6059 fatal ("no input files");
6061 /* Make a place to record the compiler output file names
6062 that correspond to the input files. */
6064 i = n_infiles;
6065 i += lang_specific_extra_outfiles;
6066 outfiles = (const char **) xcalloc (i, sizeof (char *));
6068 /* Record which files were specified explicitly as link input. */
6070 explicit_link_files = xcalloc (1, n_infiles);
6072 for (i = 0; (int) i < n_infiles; i++)
6074 int this_file_error = 0;
6076 /* Tell do_spec what to substitute for %i. */
6078 input_file_number = i;
6079 set_input (infiles[i].name);
6081 /* Use the same thing in %o, unless cp->spec says otherwise. */
6083 outfiles[i] = input_filename;
6085 /* Figure out which compiler from the file's suffix. */
6087 input_file_compiler
6088 = lookup_compiler (infiles[i].name, input_filename_length,
6089 infiles[i].language);
6091 if (input_file_compiler)
6093 /* Ok, we found an applicable compiler. Run its spec. */
6095 if (input_file_compiler->spec[0] == '#')
6097 error ("%s: %s compiler not installed on this system",
6098 input_filename, &input_file_compiler->spec[1]);
6099 this_file_error = 1;
6101 else
6103 value = do_spec (input_file_compiler->spec);
6104 if (value < 0)
6105 this_file_error = 1;
6109 /* If this file's name does not contain a recognized suffix,
6110 record it as explicit linker input. */
6112 else
6113 explicit_link_files[i] = 1;
6115 /* Clear the delete-on-failure queue, deleting the files in it
6116 if this compilation failed. */
6118 if (this_file_error)
6120 delete_failure_queue ();
6121 error_count++;
6123 /* If this compilation succeeded, don't delete those files later. */
6124 clear_failure_queue ();
6127 /* Reset the output file name to the first input file name, for use
6128 with %b in LINK_SPEC on a target that prefers not to emit a.out
6129 by default. */
6130 if (n_infiles > 0)
6131 set_input (infiles[0].name);
6133 if (error_count == 0)
6135 /* Make sure INPUT_FILE_NUMBER points to first available open
6136 slot. */
6137 input_file_number = n_infiles;
6138 if (lang_specific_pre_link ())
6139 error_count++;
6142 /* Run ld to link all the compiler output files. */
6144 if (error_count == 0)
6146 int tmp = execution_count;
6148 /* We'll use ld if we can't find collect2. */
6149 if (! strcmp (linker_name_spec, "collect2"))
6151 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
6152 if (s == NULL)
6153 linker_name_spec = "ld";
6155 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6156 for collect. */
6157 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
6158 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
6160 value = do_spec (link_command_spec);
6161 if (value < 0)
6162 error_count = 1;
6163 linker_was_run = (tmp != execution_count);
6166 /* If options said don't run linker,
6167 complain about input files to be given to the linker. */
6169 if (! linker_was_run && error_count == 0)
6170 for (i = 0; (int) i < n_infiles; i++)
6171 if (explicit_link_files[i])
6172 error ("%s: linker input file unused because linking not done",
6173 outfiles[i]);
6175 /* Delete some or all of the temporary files we made. */
6177 if (error_count)
6178 delete_failure_queue ();
6179 delete_temp_files ();
6181 if (print_help_list)
6183 printf (("\nFor bug reporting instructions, please see:\n"));
6184 printf ("%s\n", GCCBUGURL);
6187 return (signal_count != 0 ? 2
6188 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
6189 : 0);
6192 /* Find the proper compilation spec for the file name NAME,
6193 whose length is LENGTH. LANGUAGE is the specified language,
6194 or 0 if this file is to be passed to the linker. */
6196 static struct compiler *
6197 lookup_compiler (name, length, language)
6198 const char *name;
6199 size_t length;
6200 const char *language;
6202 struct compiler *cp;
6204 /* If this was specified by the user to be a linker input, indicate that. */
6205 if (language != 0 && language[0] == '*')
6206 return 0;
6208 /* Otherwise, look for the language, if one is spec'd. */
6209 if (language != 0)
6211 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6212 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6213 return cp;
6215 error ("language %s not recognized", language);
6216 return 0;
6219 /* Look for a suffix. */
6220 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6222 if (/* The suffix `-' matches only the file name `-'. */
6223 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6224 || (strlen (cp->suffix) < length
6225 /* See if the suffix matches the end of NAME. */
6226 && !strcmp (cp->suffix,
6227 name + length - strlen (cp->suffix))
6229 break;
6232 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6233 /* look again, but case-insensitively this time. */
6234 if (cp < compilers)
6235 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6237 if (/* The suffix `-' matches only the file name `-'. */
6238 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6239 || (strlen (cp->suffix) < length
6240 /* See if the suffix matches the end of NAME. */
6241 && ((!strcmp (cp->suffix,
6242 name + length - strlen (cp->suffix))
6243 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6244 && !strcasecmp (cp->suffix,
6245 name + length - strlen (cp->suffix)))
6247 break;
6249 #endif
6251 if (cp >= compilers)
6253 if (cp->spec[0] != '@')
6254 /* A non-alias entry: return it. */
6255 return cp;
6257 /* An alias entry maps a suffix to a language.
6258 Search for the language; pass 0 for NAME and LENGTH
6259 to avoid infinite recursion if language not found. */
6260 return lookup_compiler (NULL, 0, cp->spec + 1);
6262 return 0;
6265 static char *
6266 save_string (s, len)
6267 const char *s;
6268 int len;
6270 char *result = xmalloc (len + 1);
6272 memcpy (result, s, len);
6273 result[len] = 0;
6274 return result;
6277 void
6278 pfatal_with_name (name)
6279 const char *name;
6281 perror_with_name (name);
6282 delete_temp_files ();
6283 exit (1);
6286 static void
6287 perror_with_name (name)
6288 const char *name;
6290 error ("%s: %s", name, xstrerror (errno));
6293 static void
6294 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6295 const char *errmsg_fmt;
6296 const char *errmsg_arg;
6298 if (errmsg_arg)
6300 int save_errno = errno;
6302 /* Space for trailing '\0' is in %s. */
6303 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6304 sprintf (msg, errmsg_fmt, errmsg_arg);
6305 errmsg_fmt = msg;
6307 errno = save_errno;
6310 pfatal_with_name (errmsg_fmt);
6313 /* Output an error message and exit */
6315 void
6316 fancy_abort ()
6318 fatal ("internal gcc abort");
6321 /* Output an error message and exit */
6323 void
6324 fatal VPARAMS ((const char *msgid, ...))
6326 VA_OPEN (ap, msgid);
6327 VA_FIXEDARG (ap, const char *, msgid);
6329 fprintf (stderr, "%s: ", programname);
6330 vfprintf (stderr, _(msgid), ap);
6331 VA_CLOSE (ap);
6332 fprintf (stderr, "\n");
6333 delete_temp_files ();
6334 exit (1);
6337 void
6338 error VPARAMS ((const char *msgid, ...))
6340 VA_OPEN (ap, msgid);
6341 VA_FIXEDARG (ap, const char *, msgid);
6343 fprintf (stderr, "%s: ", programname);
6344 vfprintf (stderr, _(msgid), ap);
6345 VA_CLOSE (ap);
6347 fprintf (stderr, "\n");
6350 static void
6351 notice VPARAMS ((const char *msgid, ...))
6353 VA_OPEN (ap, msgid);
6354 VA_FIXEDARG (ap, const char *, msgid);
6356 vfprintf (stderr, _(msgid), ap);
6357 VA_CLOSE (ap);
6360 static void
6361 validate_all_switches ()
6363 struct compiler *comp;
6364 const char *p;
6365 char c;
6366 struct spec_list *spec;
6368 for (comp = compilers; comp->spec; comp++)
6370 p = comp->spec;
6371 while ((c = *p++))
6372 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6373 /* We have a switch spec. */
6374 validate_switches (p + 1);
6377 /* Look through the linked list of specs read from the specs file. */
6378 for (spec = specs; spec; spec = spec->next)
6380 p = *(spec->ptr_spec);
6381 while ((c = *p++))
6382 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6383 /* We have a switch spec. */
6384 validate_switches (p + 1);
6387 p = link_command_spec;
6388 while ((c = *p++))
6389 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6390 /* We have a switch spec. */
6391 validate_switches (p + 1);
6394 /* Look at the switch-name that comes after START
6395 and mark as valid all supplied switches that match it. */
6397 static void
6398 validate_switches (start)
6399 const char *start;
6401 const char *p = start;
6402 const char *filter;
6403 int i;
6404 int suffix;
6406 if (*p == '|')
6407 ++p;
6409 next_member:
6410 if (*p == '!')
6411 ++p;
6413 suffix = 0;
6414 if (*p == '.')
6415 suffix = 1, ++p;
6417 filter = p;
6418 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6419 p++;
6421 if (suffix)
6423 else if (p[-1] == '*')
6425 /* Mark all matching switches as valid. */
6426 for (i = 0; i < n_switches; i++)
6427 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6428 switches[i].validated = 1;
6430 else
6432 /* Mark an exact matching switch as valid. */
6433 for (i = 0; i < n_switches; i++)
6435 if (!strncmp (switches[i].part1, filter, p - filter)
6436 && switches[i].part1[p - filter] == 0)
6437 switches[i].validated = 1;
6441 if (*p++ == '|' || p[-1] == '&')
6442 goto next_member;
6445 /* Check whether a particular argument was used. The first time we
6446 canonicalize the switches to keep only the ones we care about. */
6448 static int
6449 used_arg (p, len)
6450 const char *p;
6451 int len;
6453 struct mswitchstr
6455 const char *str;
6456 const char *replace;
6457 int len;
6458 int rep_len;
6461 static struct mswitchstr *mswitches;
6462 static int n_mswitches;
6463 int i, j;
6465 if (!mswitches)
6467 struct mswitchstr *matches;
6468 const char *q;
6469 int cnt = 0;
6471 /* Break multilib_matches into the component strings of string
6472 and replacement string. */
6473 for (q = multilib_matches; *q != '\0'; q++)
6474 if (*q == ';')
6475 cnt++;
6477 matches =
6478 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6479 i = 0;
6480 q = multilib_matches;
6481 while (*q != '\0')
6483 matches[i].str = q;
6484 while (*q != ' ')
6486 if (*q == '\0')
6487 abort ();
6488 q++;
6490 matches[i].len = q - matches[i].str;
6492 matches[i].replace = ++q;
6493 while (*q != ';' && *q != '\0')
6495 if (*q == ' ')
6496 abort ();
6497 q++;
6499 matches[i].rep_len = q - matches[i].replace;
6500 i++;
6501 if (*q == ';')
6502 q++;
6505 /* Now build a list of the replacement string for switches that we care
6506 about. Make sure we allocate at least one entry. This prevents
6507 xmalloc from calling fatal, and prevents us from re-executing this
6508 block of code. */
6509 mswitches
6510 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6511 * (n_switches ? n_switches : 1));
6512 for (i = 0; i < n_switches; i++)
6514 int xlen = strlen (switches[i].part1);
6515 for (j = 0; j < cnt; j++)
6516 if (xlen == matches[j].len
6517 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6519 mswitches[n_mswitches].str = matches[j].replace;
6520 mswitches[n_mswitches].len = matches[j].rep_len;
6521 mswitches[n_mswitches].replace = (char *) 0;
6522 mswitches[n_mswitches].rep_len = 0;
6523 n_mswitches++;
6524 break;
6529 for (i = 0; i < n_mswitches; i++)
6530 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6531 return 1;
6533 return 0;
6536 static int
6537 default_arg (p, len)
6538 const char *p;
6539 int len;
6541 const char *start, *end;
6543 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6545 while (*start == ' ' || *start == '\t')
6546 start++;
6548 if (*start == '\0')
6549 break;
6551 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6554 if ((end - start) == len && strncmp (p, start, len) == 0)
6555 return 1;
6557 if (*end == '\0')
6558 break;
6561 return 0;
6564 /* Work out the subdirectory to use based on the options. The format of
6565 multilib_select is a list of elements. Each element is a subdirectory
6566 name followed by a list of options followed by a semicolon. The format
6567 of multilib_exclusions is the same, but without the preceding
6568 directory. First gcc will check the exclusions, if none of the options
6569 beginning with an exclamation point are present, and all of the other
6570 options are present, then we will ignore this completely. Passing
6571 that, gcc will consider each multilib_select in turn using the same
6572 rules for matching the options. If a match is found, that subdirectory
6573 will be used. */
6575 static void
6576 set_multilib_dir ()
6578 const char *p;
6579 unsigned int this_path_len;
6580 const char *this_path, *this_arg;
6581 int not_arg;
6582 int ok;
6584 p = multilib_exclusions;
6585 while (*p != '\0')
6587 /* Ignore newlines. */
6588 if (*p == '\n')
6590 ++p;
6591 continue;
6594 /* Check the arguments. */
6595 ok = 1;
6596 while (*p != ';')
6598 if (*p == '\0')
6599 abort ();
6601 if (! ok)
6603 ++p;
6604 continue;
6607 this_arg = p;
6608 while (*p != ' ' && *p != ';')
6610 if (*p == '\0')
6611 abort ();
6612 ++p;
6615 if (*this_arg != '!')
6616 not_arg = 0;
6617 else
6619 not_arg = 1;
6620 ++this_arg;
6623 ok = used_arg (this_arg, p - this_arg);
6624 if (not_arg)
6625 ok = ! ok;
6627 if (*p == ' ')
6628 ++p;
6631 if (ok)
6632 return;
6634 ++p;
6637 p = multilib_select;
6638 while (*p != '\0')
6640 /* Ignore newlines. */
6641 if (*p == '\n')
6643 ++p;
6644 continue;
6647 /* Get the initial path. */
6648 this_path = p;
6649 while (*p != ' ')
6651 if (*p == '\0')
6652 abort ();
6653 ++p;
6655 this_path_len = p - this_path;
6657 /* Check the arguments. */
6658 ok = 1;
6659 ++p;
6660 while (*p != ';')
6662 if (*p == '\0')
6663 abort ();
6665 if (! ok)
6667 ++p;
6668 continue;
6671 this_arg = p;
6672 while (*p != ' ' && *p != ';')
6674 if (*p == '\0')
6675 abort ();
6676 ++p;
6679 if (*this_arg != '!')
6680 not_arg = 0;
6681 else
6683 not_arg = 1;
6684 ++this_arg;
6687 /* If this is a default argument, we can just ignore it.
6688 This is true even if this_arg begins with '!'. Beginning
6689 with '!' does not mean that this argument is necessarily
6690 inappropriate for this library: it merely means that
6691 there is a more specific library which uses this
6692 argument. If this argument is a default, we need not
6693 consider that more specific library. */
6694 if (! default_arg (this_arg, p - this_arg))
6696 ok = used_arg (this_arg, p - this_arg);
6697 if (not_arg)
6698 ok = ! ok;
6701 if (*p == ' ')
6702 ++p;
6705 if (ok)
6707 if (this_path_len != 1
6708 || this_path[0] != '.')
6710 char *new_multilib_dir = xmalloc (this_path_len + 1);
6711 strncpy (new_multilib_dir, this_path, this_path_len);
6712 new_multilib_dir[this_path_len] = '\0';
6713 multilib_dir = new_multilib_dir;
6715 break;
6718 ++p;
6722 /* Print out the multiple library subdirectory selection
6723 information. This prints out a series of lines. Each line looks
6724 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6725 required. Only the desired options are printed out, the negative
6726 matches. The options are print without a leading dash. There are
6727 no spaces to make it easy to use the information in the shell.
6728 Each subdirectory is printed only once. This assumes the ordering
6729 generated by the genmultilib script. Also, we leave out ones that match
6730 the exclusions. */
6732 static void
6733 print_multilib_info ()
6735 const char *p = multilib_select;
6736 const char *last_path = 0, *this_path;
6737 int skip;
6738 unsigned int last_path_len = 0;
6740 while (*p != '\0')
6742 skip = 0;
6743 /* Ignore newlines. */
6744 if (*p == '\n')
6746 ++p;
6747 continue;
6750 /* Get the initial path. */
6751 this_path = p;
6752 while (*p != ' ')
6754 if (*p == '\0')
6755 abort ();
6756 ++p;
6759 /* Check for matches with the multilib_exclusions. We don't bother
6760 with the '!' in either list. If any of the exclusion rules match
6761 all of its options with the select rule, we skip it. */
6763 const char *e = multilib_exclusions;
6764 const char *this_arg;
6766 while (*e != '\0')
6768 int m = 1;
6769 /* Ignore newlines. */
6770 if (*e == '\n')
6772 ++e;
6773 continue;
6776 /* Check the arguments. */
6777 while (*e != ';')
6779 const char *q;
6780 int mp = 0;
6782 if (*e == '\0')
6783 abort ();
6785 if (! m)
6787 ++e;
6788 continue;
6791 this_arg = e;
6793 while (*e != ' ' && *e != ';')
6795 if (*e == '\0')
6796 abort ();
6797 ++e;
6800 q = p + 1;
6801 while (*q != ';')
6803 const char *arg;
6804 int len = e - this_arg;
6806 if (*q == '\0')
6807 abort ();
6809 arg = q;
6811 while (*q != ' ' && *q != ';')
6813 if (*q == '\0')
6814 abort ();
6815 ++q;
6818 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6819 default_arg (this_arg, e - this_arg))
6821 mp = 1;
6822 break;
6825 if (*q == ' ')
6826 ++q;
6829 if (! mp)
6830 m = 0;
6832 if (*e == ' ')
6833 ++e;
6836 if (m)
6838 skip = 1;
6839 break;
6842 if (*e != '\0')
6843 ++e;
6847 if (! skip)
6849 /* If this is a duplicate, skip it. */
6850 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6851 && ! strncmp (last_path, this_path, last_path_len));
6853 last_path = this_path;
6854 last_path_len = p - this_path;
6857 /* If this directory requires any default arguments, we can skip
6858 it. We will already have printed a directory identical to
6859 this one which does not require that default argument. */
6860 if (! skip)
6862 const char *q;
6864 q = p + 1;
6865 while (*q != ';')
6867 const char *arg;
6869 if (*q == '\0')
6870 abort ();
6872 if (*q == '!')
6873 arg = NULL;
6874 else
6875 arg = q;
6877 while (*q != ' ' && *q != ';')
6879 if (*q == '\0')
6880 abort ();
6881 ++q;
6884 if (arg != NULL
6885 && default_arg (arg, q - arg))
6887 skip = 1;
6888 break;
6891 if (*q == ' ')
6892 ++q;
6896 if (! skip)
6898 const char *p1;
6900 for (p1 = last_path; p1 < p; p1++)
6901 putchar (*p1);
6902 putchar (';');
6905 ++p;
6906 while (*p != ';')
6908 int use_arg;
6910 if (*p == '\0')
6911 abort ();
6913 if (skip)
6915 ++p;
6916 continue;
6919 use_arg = *p != '!';
6921 if (use_arg)
6922 putchar ('@');
6924 while (*p != ' ' && *p != ';')
6926 if (*p == '\0')
6927 abort ();
6928 if (use_arg)
6929 putchar (*p);
6930 ++p;
6933 if (*p == ' ')
6934 ++p;
6937 if (! skip)
6939 /* If there are extra options, print them now. */
6940 if (multilib_extra && *multilib_extra)
6942 int print_at = TRUE;
6943 const char *q;
6945 for (q = multilib_extra; *q != '\0'; q++)
6947 if (*q == ' ')
6948 print_at = TRUE;
6949 else
6951 if (print_at)
6952 putchar ('@');
6953 putchar (*q);
6954 print_at = FALSE;
6959 putchar ('\n');
6962 ++p;