stl_bvector.h (swap(_Bit_reference,_Bit_reference)): Move/rename...
[official-gcc.git] / gcc / gcc.c
blob50b219f3569e660073697df437705c7d4ad67022
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*} %{W*&pedantic*} %{w} %{m*} %{f*}\
698 %{O*} %{undef}";
700 /* This contains cpp options which are not passed when the preprocessor
701 output will be used by another program. */
702 static const char *cpp_debug_options = "%{d*}";
704 /* NB: This is shared amongst all front-ends. */
705 static const char *cc1_options =
706 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
707 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
708 %{g*} %{O*} %{W*&pedantic*} %{w} %{std*} %{ansi}\
709 %{v:-version} %{pg:-p} %{p} %{f*} %{undef}\
710 %{Qn:-fno-ident} %{--help:--help}\
711 %{--target-help:--target-help}\
712 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
713 %{fsyntax-only:-o %j} %{-param*}";
715 static const char *asm_options =
716 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
718 static const char *invoke_as =
719 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
721 /* Some compilers have limits on line lengths, and the multilib_select
722 and/or multilib_matches strings can be very long, so we build them at
723 run time. */
724 static struct obstack multilib_obstack;
725 static const char *multilib_select;
726 static const char *multilib_matches;
727 static const char *multilib_defaults;
728 static const char *multilib_exclusions;
729 #include "multilib.h"
731 /* Check whether a particular argument is a default argument. */
733 #ifndef MULTILIB_DEFAULTS
734 #define MULTILIB_DEFAULTS { "" }
735 #endif
737 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
739 struct user_specs
741 struct user_specs *next;
742 const char *filename;
745 static struct user_specs *user_specs_head, *user_specs_tail;
747 /* This defines which switch letters take arguments. */
749 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
750 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
751 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
752 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
753 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'B' || (CHAR) == 'b')
755 #ifndef SWITCH_TAKES_ARG
756 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
757 #endif
759 /* This defines which multi-letter switches take arguments. */
761 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
762 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
763 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
764 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
765 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
766 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
767 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
768 || !strcmp (STR, "specs") \
769 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
771 #ifndef WORD_SWITCH_TAKES_ARG
772 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
773 #endif
775 #ifdef HAVE_TARGET_EXECUTABLE_SUFFIX
776 /* This defines which switches stop a full compilation. */
777 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
778 ((CHAR) == 'c' || (CHAR) == 'S')
780 #ifndef SWITCH_CURTAILS_COMPILATION
781 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
782 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
783 #endif
784 #endif
786 /* Record the mapping from file suffixes for compilation specs. */
788 struct compiler
790 const char *suffix; /* Use this compiler for input files
791 whose names end in this suffix. */
793 const char *spec; /* To use this compiler, run this spec. */
795 const char *cpp_spec; /* If non-NULL, substitute this spec
796 for `%C', rather than the usual
797 cpp_spec. */
800 /* Pointer to a vector of `struct compiler' that gives the spec for
801 compiling a file, based on its suffix.
802 A file that does not end in any of these suffixes will be passed
803 unchanged to the loader and nothing else will be done to it.
805 An entry containing two 0s is used to terminate the vector.
807 If multiple entries match a file, the last matching one is used. */
809 static struct compiler *compilers;
811 /* Number of entries in `compilers', not counting the null terminator. */
813 static int n_compilers;
815 /* The default list of file name suffixes and their compilation specs. */
817 static const struct compiler default_compilers[] =
819 /* Add lists of suffixes of known languages here. If those languages
820 were not present when we built the driver, we will hit these copies
821 and be given a more meaningful error than "file not used since
822 linking is not done". */
823 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
824 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
825 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
826 {".ii", "#C++", 0},
827 {".ads", "#Ada", 0}, {".adb", "#Ada", 0},
828 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
829 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
830 {".r", "#Ratfor", 0},
831 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
832 {".java", "#Java", 0}, {".class", "#Java", 0},
833 {".zip", "#Java", 0}, {".jar", "#Java", 0},
834 /* Next come the entries for C. */
835 {".c", "@c", 0},
836 {"@c",
837 /* cc1 has an integrated ISO C preprocessor. We should invoke the
838 external preprocessor if -save-temps is given. */
839 "%{E|M|MM:%(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options)\
840 %(cpp_debug_options)}\
841 %{!E:%{!M:%{!MM:\
842 %{traditional|ftraditional:\
843 %eGNU C no longer supports -traditional without -E}\
844 %{save-temps|traditional-cpp:%(trad_capable_cpp) \
845 %{ansi:-std=c89} %(cpp_options) %b.i \n\
846 cc1 -fpreprocessed %b.i %(cc1_options)}\
847 %{!save-temps:%{!traditional-cpp:\
848 cc1 %{ansi:-std=c89} %(cpp_unique_options) %(cc1_options)}}\
849 %{!fsyntax-only:%(invoke_as)}}}}", 0},
850 {"-",
851 "%{!E:%e-E required when input is from standard input}\
852 %(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options)", 0},
853 {".h", "@c-header", 0},
854 {"@c-header",
855 "%{!E:%ecompilation of header file requested} \
856 %(trad_capable_cpp) %{ansi:-std=c89} %(cpp_options) %(cpp_debug_options)",
858 {".i", "@cpp-output", 0},
859 {"@cpp-output",
860 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
861 {".s", "@assembler", 0},
862 {"@assembler",
863 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0},
864 {".S", "@assembler-with-cpp", 0},
865 {"@assembler-with-cpp",
866 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
867 %{E|M|MM:%(cpp_debug_options)}\
868 %{!M:%{!MM:%{!E:%{!S:-o %{|!pipe:%g.s} |\n\
869 as %(asm_debug) %(asm_options) %{!pipe:%g.s} %A }}}}", 0},
870 #include "specs.h"
871 /* Mark end of table */
872 {0, 0, 0}
875 /* Number of elements in default_compilers, not counting the terminator. */
877 static const int n_default_compilers = ARRAY_SIZE (default_compilers) - 1;
879 /* A vector of options to give to the linker.
880 These options are accumulated by %x,
881 and substituted into the linker command with %X. */
882 static int n_linker_options;
883 static char **linker_options;
885 /* A vector of options to give to the assembler.
886 These options are accumulated by -Wa,
887 and substituted into the assembler command with %Y. */
888 static int n_assembler_options;
889 static char **assembler_options;
891 /* A vector of options to give to the preprocessor.
892 These options are accumulated by -Wp,
893 and substituted into the preprocessor command with %Z. */
894 static int n_preprocessor_options;
895 static char **preprocessor_options;
897 /* Define how to map long options into short ones. */
899 /* This structure describes one mapping. */
900 struct option_map
902 /* The long option's name. */
903 const char *const name;
904 /* The equivalent short option. */
905 const char *const equivalent;
906 /* Argument info. A string of flag chars; NULL equals no options.
907 a => argument required.
908 o => argument optional.
909 j => join argument to equivalent, making one word.
910 * => require other text after NAME as an argument. */
911 const char *const arg_info;
914 /* This is the table of mappings. Mappings are tried sequentially
915 for each option encountered; the first one that matches, wins. */
917 static const struct option_map option_map[] =
919 {"--all-warnings", "-Wall", 0},
920 {"--ansi", "-ansi", 0},
921 {"--assemble", "-S", 0},
922 {"--assert", "-A", "a"},
923 {"--classpath", "-fclasspath=", "aj"},
924 {"--bootclasspath", "-fbootclasspath=", "aj"},
925 {"--CLASSPATH", "-fclasspath=", "aj"},
926 {"--comments", "-C", 0},
927 {"--comments-in-macros", "-CC", 0},
928 {"--compile", "-c", 0},
929 {"--debug", "-g", "oj"},
930 {"--define-macro", "-D", "aj"},
931 {"--dependencies", "-M", 0},
932 {"--dump", "-d", "a"},
933 {"--dumpbase", "-dumpbase", "a"},
934 {"--entry", "-e", 0},
935 {"--extra-warnings", "-W", 0},
936 {"--for-assembler", "-Wa", "a"},
937 {"--for-linker", "-Xlinker", "a"},
938 {"--force-link", "-u", "a"},
939 {"--imacros", "-imacros", "a"},
940 {"--include", "-include", "a"},
941 {"--include-barrier", "-I-", 0},
942 {"--include-directory", "-I", "aj"},
943 {"--include-directory-after", "-idirafter", "a"},
944 {"--include-prefix", "-iprefix", "a"},
945 {"--include-with-prefix", "-iwithprefix", "a"},
946 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
947 {"--include-with-prefix-after", "-iwithprefix", "a"},
948 {"--language", "-x", "a"},
949 {"--library-directory", "-L", "a"},
950 {"--machine", "-m", "aj"},
951 {"--machine-", "-m", "*j"},
952 {"--no-line-commands", "-P", 0},
953 {"--no-precompiled-includes", "-noprecomp", 0},
954 {"--no-standard-includes", "-nostdinc", 0},
955 {"--no-standard-libraries", "-nostdlib", 0},
956 {"--no-warnings", "-w", 0},
957 {"--optimize", "-O", "oj"},
958 {"--output", "-o", "a"},
959 {"--output-class-directory", "-foutput-class-dir=", "ja"},
960 {"--param", "--param", "a"},
961 {"--pedantic", "-pedantic", 0},
962 {"--pedantic-errors", "-pedantic-errors", 0},
963 {"--pipe", "-pipe", 0},
964 {"--prefix", "-B", "a"},
965 {"--preprocess", "-E", 0},
966 {"--print-search-dirs", "-print-search-dirs", 0},
967 {"--print-file-name", "-print-file-name=", "aj"},
968 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
969 {"--print-missing-file-dependencies", "-MG", 0},
970 {"--print-multi-lib", "-print-multi-lib", 0},
971 {"--print-multi-directory", "-print-multi-directory", 0},
972 {"--print-prog-name", "-print-prog-name=", "aj"},
973 {"--profile", "-p", 0},
974 {"--profile-blocks", "-a", 0},
975 {"--quiet", "-q", 0},
976 {"--resource", "-fcompile-resource=", "aj"},
977 {"--save-temps", "-save-temps", 0},
978 {"--shared", "-shared", 0},
979 {"--silent", "-q", 0},
980 {"--specs", "-specs=", "aj"},
981 {"--static", "-static", 0},
982 {"--std", "-std=", "aj"},
983 {"--symbolic", "-symbolic", 0},
984 {"--target", "-b", "a"},
985 {"--time", "-time", 0},
986 {"--trace-includes", "-H", 0},
987 {"--traditional", "-traditional", 0},
988 {"--traditional-cpp", "-traditional-cpp", 0},
989 {"--trigraphs", "-trigraphs", 0},
990 {"--undefine-macro", "-U", "aj"},
991 {"--use-version", "-V", "a"},
992 {"--user-dependencies", "-MM", 0},
993 {"--verbose", "-v", 0},
994 {"--warn-", "-W", "*j"},
995 {"--write-dependencies", "-MD", 0},
996 {"--write-user-dependencies", "-MMD", 0},
997 {"--", "-f", "*j"}
1001 #ifdef TARGET_OPTION_TRANSLATE_TABLE
1002 static const struct {
1003 const char *const option_found;
1004 const char *const replacements;
1005 } target_option_translations[] =
1007 TARGET_OPTION_TRANSLATE_TABLE,
1008 { 0, 0 }
1010 #endif
1012 /* Translate the options described by *ARGCP and *ARGVP.
1013 Make a new vector and store it back in *ARGVP,
1014 and store its length in *ARGVC. */
1016 static void
1017 translate_options (argcp, argvp)
1018 int *argcp;
1019 const char *const **argvp;
1021 int i;
1022 int argc = *argcp;
1023 const char *const *argv = *argvp;
1024 int newvsize = (argc + 2) * 2 * sizeof (const char *);
1025 const char **newv =
1026 (const char **) xmalloc (newvsize);
1027 int newindex = 0;
1029 i = 0;
1030 newv[newindex++] = argv[i++];
1032 while (i < argc)
1034 #ifdef TARGET_OPTION_TRANSLATE_TABLE
1035 int tott_idx;
1037 for (tott_idx = 0;
1038 target_option_translations[tott_idx].option_found;
1039 tott_idx++)
1041 if (strcmp (target_option_translations[tott_idx].option_found,
1042 argv[i]) == 0)
1044 int spaces = 1;
1045 const char *sp;
1046 char *np;
1048 for (sp = target_option_translations[tott_idx].replacements;
1049 *sp; sp++)
1051 if (*sp == ' ')
1052 spaces ++;
1055 newvsize += spaces * sizeof (const char *);
1056 newv = (const char **) xrealloc (newv, newvsize);
1058 sp = target_option_translations[tott_idx].replacements;
1059 np = xstrdup (sp);
1061 while (1)
1063 while (*np == ' ')
1064 np++;
1065 if (*np == 0)
1066 break;
1067 newv[newindex++] = np;
1068 while (*np != ' ' && *np)
1069 np++;
1070 if (*np == 0)
1071 break;
1072 *np++ = 0;
1075 i ++;
1076 break;
1079 if (target_option_translations[tott_idx].option_found)
1080 continue;
1081 #endif
1083 /* Translate -- options. */
1084 if (argv[i][0] == '-' && argv[i][1] == '-')
1086 size_t j;
1087 /* Find a mapping that applies to this option. */
1088 for (j = 0; j < ARRAY_SIZE (option_map); j++)
1090 size_t optlen = strlen (option_map[j].name);
1091 size_t arglen = strlen (argv[i]);
1092 size_t complen = arglen > optlen ? optlen : arglen;
1093 const char *arginfo = option_map[j].arg_info;
1095 if (arginfo == 0)
1096 arginfo = "";
1098 if (!strncmp (argv[i], option_map[j].name, complen))
1100 const char *arg = 0;
1102 if (arglen < optlen)
1104 size_t k;
1105 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
1106 if (strlen (option_map[k].name) >= arglen
1107 && !strncmp (argv[i], option_map[k].name, arglen))
1109 error ("ambiguous abbreviation %s", argv[i]);
1110 break;
1113 if (k != ARRAY_SIZE (option_map))
1114 break;
1117 if (arglen > optlen)
1119 /* If the option has an argument, accept that. */
1120 if (argv[i][optlen] == '=')
1121 arg = argv[i] + optlen + 1;
1123 /* If this mapping requires extra text at end of name,
1124 accept that as "argument". */
1125 else if (strchr (arginfo, '*') != 0)
1126 arg = argv[i] + optlen;
1128 /* Otherwise, extra text at end means mismatch.
1129 Try other mappings. */
1130 else
1131 continue;
1134 else if (strchr (arginfo, '*') != 0)
1136 error ("incomplete `%s' option", option_map[j].name);
1137 break;
1140 /* Handle arguments. */
1141 if (strchr (arginfo, 'a') != 0)
1143 if (arg == 0)
1145 if (i + 1 == argc)
1147 error ("missing argument to `%s' option",
1148 option_map[j].name);
1149 break;
1152 arg = argv[++i];
1155 else if (strchr (arginfo, '*') != 0)
1157 else if (strchr (arginfo, 'o') == 0)
1159 if (arg != 0)
1160 error ("extraneous argument to `%s' option",
1161 option_map[j].name);
1162 arg = 0;
1165 /* Store the translation as one argv elt or as two. */
1166 if (arg != 0 && strchr (arginfo, 'j') != 0)
1167 newv[newindex++] = concat (option_map[j].equivalent, arg,
1168 NULL);
1169 else if (arg != 0)
1171 newv[newindex++] = option_map[j].equivalent;
1172 newv[newindex++] = arg;
1174 else
1175 newv[newindex++] = option_map[j].equivalent;
1177 break;
1180 i++;
1183 /* Handle old-fashioned options--just copy them through,
1184 with their arguments. */
1185 else if (argv[i][0] == '-')
1187 const char *p = argv[i] + 1;
1188 int c = *p;
1189 int nskip = 1;
1191 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1192 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1193 else if (WORD_SWITCH_TAKES_ARG (p))
1194 nskip += WORD_SWITCH_TAKES_ARG (p);
1195 else if ((c == 'B' || c == 'b' || c == 'x')
1196 && p[1] == 0)
1197 nskip += 1;
1198 else if (! strcmp (p, "Xlinker"))
1199 nskip += 1;
1201 /* Watch out for an option at the end of the command line that
1202 is missing arguments, and avoid skipping past the end of the
1203 command line. */
1204 if (nskip + i > argc)
1205 nskip = argc - i;
1207 while (nskip > 0)
1209 newv[newindex++] = argv[i++];
1210 nskip--;
1213 else
1214 /* Ordinary operands, or +e options. */
1215 newv[newindex++] = argv[i++];
1218 newv[newindex] = 0;
1220 *argvp = newv;
1221 *argcp = newindex;
1224 static char *
1225 skip_whitespace (p)
1226 char *p;
1228 while (1)
1230 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1231 be considered whitespace. */
1232 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1233 return p + 1;
1234 else if (*p == '\n' || *p == ' ' || *p == '\t')
1235 p++;
1236 else if (*p == '#')
1238 while (*p != '\n')
1239 p++;
1240 p++;
1242 else
1243 break;
1246 return p;
1248 /* Structures to keep track of prefixes to try when looking for files. */
1250 struct prefix_list
1252 const char *prefix; /* String to prepend to the path. */
1253 struct prefix_list *next; /* Next in linked list. */
1254 int require_machine_suffix; /* Don't use without machine_suffix. */
1255 /* 2 means try both machine_suffix and just_machine_suffix. */
1256 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1257 int priority; /* Sort key - priority within list */
1260 struct path_prefix
1262 struct prefix_list *plist; /* List of prefixes to try */
1263 int max_len; /* Max length of a prefix in PLIST */
1264 const char *name; /* Name of this list (used in config stuff) */
1267 /* List of prefixes to try when looking for executables. */
1269 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1271 /* List of prefixes to try when looking for startup (crt0) files. */
1273 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1275 /* List of prefixes to try when looking for include files. */
1277 static struct path_prefix include_prefixes = { 0, 0, "include" };
1279 /* Suffix to attach to directories searched for commands.
1280 This looks like `MACHINE/VERSION/'. */
1282 static const char *machine_suffix = 0;
1284 /* Suffix to attach to directories searched for commands.
1285 This is just `MACHINE/'. */
1287 static const char *just_machine_suffix = 0;
1289 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1291 static const char *gcc_exec_prefix;
1293 /* Default prefixes to attach to command names. */
1295 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1296 #undef MD_EXEC_PREFIX
1297 #undef MD_STARTFILE_PREFIX
1298 #undef MD_STARTFILE_PREFIX_1
1299 #endif
1301 /* If no prefixes defined, use the null string, which will disable them. */
1302 #ifndef MD_EXEC_PREFIX
1303 #define MD_EXEC_PREFIX ""
1304 #endif
1305 #ifndef MD_STARTFILE_PREFIX
1306 #define MD_STARTFILE_PREFIX ""
1307 #endif
1308 #ifndef MD_STARTFILE_PREFIX_1
1309 #define MD_STARTFILE_PREFIX_1 ""
1310 #endif
1312 /* Supply defaults for the standard prefixes. */
1314 #ifndef STANDARD_EXEC_PREFIX
1315 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1316 #endif
1317 #ifndef STANDARD_STARTFILE_PREFIX
1318 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1319 #endif
1320 #ifndef TOOLDIR_BASE_PREFIX
1321 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1322 #endif
1323 #ifndef STANDARD_BINDIR_PREFIX
1324 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1325 #endif
1327 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1328 static const char *const standard_exec_prefix_1 = "/usr/lib/gcc/";
1329 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1331 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1332 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1333 static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1334 static const char *const standard_startfile_prefix_1 = "/lib/";
1335 static const char *const standard_startfile_prefix_2 = "/usr/lib/";
1337 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1338 static const char *tooldir_prefix;
1340 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1342 /* Subdirectory to use for locating libraries. Set by
1343 set_multilib_dir based on the compilation options. */
1345 static const char *multilib_dir;
1347 /* Structure to keep track of the specs that have been defined so far.
1348 These are accessed using %(specname) or %[specname] in a compiler
1349 or link spec. */
1351 struct spec_list
1353 /* The following 2 fields must be first */
1354 /* to allow EXTRA_SPECS to be initialized */
1355 const char *name; /* name of the spec. */
1356 const char *ptr; /* available ptr if no static pointer */
1358 /* The following fields are not initialized */
1359 /* by EXTRA_SPECS */
1360 const char **ptr_spec; /* pointer to the spec itself. */
1361 struct spec_list *next; /* Next spec in linked list. */
1362 int name_len; /* length of the name */
1363 int alloc_p; /* whether string was allocated */
1366 #define INIT_STATIC_SPEC(NAME,PTR) \
1367 { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1369 /* List of statically defined specs. */
1370 static struct spec_list static_specs[] =
1372 INIT_STATIC_SPEC ("asm", &asm_spec),
1373 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
1374 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1375 INIT_STATIC_SPEC ("asm_options", &asm_options),
1376 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1377 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1378 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1379 INIT_STATIC_SPEC ("cpp_debug_options", &cpp_debug_options),
1380 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
1381 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1382 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1383 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1384 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1385 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
1386 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1387 INIT_STATIC_SPEC ("link", &link_spec),
1388 INIT_STATIC_SPEC ("lib", &lib_spec),
1389 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1390 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1391 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1392 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1393 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1394 INIT_STATIC_SPEC ("version", &compiler_version),
1395 INIT_STATIC_SPEC ("multilib", &multilib_select),
1396 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1397 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1398 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1399 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1400 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1401 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1402 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1403 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1404 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1405 INIT_STATIC_SPEC ("startfile_prefix_spec", &startfile_prefix_spec),
1408 #ifdef EXTRA_SPECS /* additional specs needed */
1409 /* Structure to keep track of just the first two args of a spec_list.
1410 That is all that the EXTRA_SPECS macro gives us. */
1411 struct spec_list_1
1413 const char *const name;
1414 const char *const ptr;
1417 static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1418 static struct spec_list *extra_specs = (struct spec_list *) 0;
1419 #endif
1421 /* List of dynamically allocates specs that have been defined so far. */
1423 static struct spec_list *specs = (struct spec_list *) 0;
1425 /* Add appropriate libgcc specs to OBSTACK, taking into account
1426 various permutations of -shared-libgcc, -shared, and such. */
1428 #ifdef ENABLE_SHARED_LIBGCC
1429 static void
1430 init_gcc_specs (obstack, shared_name, static_name, eh_name)
1431 struct obstack *obstack;
1432 const char *shared_name;
1433 const char *static_name;
1434 const char *eh_name;
1436 char *buf;
1438 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
1439 "}%{!static:%{!static-libgcc:",
1440 "%{!shared:%{!shared-libgcc:", static_name, " ",
1441 eh_name, "}%{shared-libgcc:", shared_name, " ",
1442 static_name, "}}%{shared:",
1443 #ifdef LINK_EH_SPEC
1444 "%{shared-libgcc:", shared_name,
1445 "}%{!shared-libgcc:", static_name, "}",
1446 #else
1447 shared_name,
1448 #endif
1449 "}}}", NULL);
1451 obstack_grow (obstack, buf, strlen (buf));
1452 free (buf);
1454 #endif /* ENABLE_SHARED_LIBGCC */
1456 /* Initialize the specs lookup routines. */
1458 static void
1459 init_spec ()
1461 struct spec_list *next = (struct spec_list *) 0;
1462 struct spec_list *sl = (struct spec_list *) 0;
1463 int i;
1465 if (specs)
1466 return; /* Already initialized. */
1468 if (verbose_flag)
1469 notice ("Using built-in specs.\n");
1471 #ifdef EXTRA_SPECS
1472 extra_specs = (struct spec_list *)
1473 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1475 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1477 sl = &extra_specs[i];
1478 sl->name = extra_specs_1[i].name;
1479 sl->ptr = extra_specs_1[i].ptr;
1480 sl->next = next;
1481 sl->name_len = strlen (sl->name);
1482 sl->ptr_spec = &sl->ptr;
1483 next = sl;
1485 #endif
1487 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1489 sl = &static_specs[i];
1490 sl->next = next;
1491 next = sl;
1494 #ifdef ENABLE_SHARED_LIBGCC
1495 /* ??? If neither -shared-libgcc nor --static-libgcc was
1496 seen, then we should be making an educated guess. Some proposed
1497 heuristics for ELF include:
1499 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1500 program will be doing dynamic loading, which will likely
1501 need the shared libgcc.
1503 (2) If "-ldl", then it's also a fair bet that we're doing
1504 dynamic loading.
1506 (3) For each ET_DYN we're linking against (either through -lfoo
1507 or /some/path/foo.so), check to see whether it or one of
1508 its dependencies depends on a shared libgcc.
1510 (4) If "-shared"
1512 If the runtime is fixed to look for program headers instead
1513 of calling __register_frame_info at all, for each object,
1514 use the shared libgcc if any EH symbol referenced.
1516 If crtstuff is fixed to not invoke __register_frame_info
1517 automatically, for each object, use the shared libgcc if
1518 any non-empty unwind section found.
1520 Doing any of this probably requires invoking an external program to
1521 do the actual object file scanning. */
1523 const char *p = libgcc_spec;
1524 int in_sep = 1;
1526 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1527 when given the proper command line arguments. */
1528 while (*p)
1530 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1532 init_gcc_specs (&obstack,
1533 #ifdef NO_SHARED_LIBGCC_MULTILIB
1534 "-lgcc_s"
1535 #else
1536 "-lgcc_s%M"
1537 #endif
1539 "-lgcc",
1540 "-lgcc_eh");
1541 p += 5;
1542 in_sep = 0;
1544 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1546 /* Ug. We don't know shared library extensions. Hope that
1547 systems that use this form don't do shared libraries. */
1548 init_gcc_specs (&obstack,
1549 #ifdef NO_SHARED_LIBGCC_MULTILIB
1550 "-lgcc_s"
1551 #else
1552 "-lgcc_s%M"
1553 #endif
1555 "libgcc.a%s",
1556 "libgcc_eh.a%s");
1557 p += 10;
1558 in_sep = 0;
1560 else
1562 obstack_1grow (&obstack, *p);
1563 in_sep = (*p == ' ');
1564 p += 1;
1568 obstack_1grow (&obstack, '\0');
1569 libgcc_spec = obstack_finish (&obstack);
1571 #endif
1572 #ifdef USE_AS_TRADITIONAL_FORMAT
1573 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1575 static const char tf[] = "--traditional-format ";
1576 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1577 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1578 asm_spec = obstack_finish (&obstack);
1580 #endif
1581 #ifdef LINK_EH_SPEC
1582 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1583 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
1584 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
1585 link_spec = obstack_finish (&obstack);
1586 #endif
1588 specs = sl;
1591 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1592 removed; If the spec starts with a + then SPEC is added to the end of the
1593 current spec. */
1595 static void
1596 set_spec (name, spec)
1597 const char *name;
1598 const char *spec;
1600 struct spec_list *sl;
1601 const char *old_spec;
1602 int name_len = strlen (name);
1603 int i;
1605 /* If this is the first call, initialize the statically allocated specs. */
1606 if (!specs)
1608 struct spec_list *next = (struct spec_list *) 0;
1609 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1611 sl = &static_specs[i];
1612 sl->next = next;
1613 next = sl;
1615 specs = sl;
1618 /* See if the spec already exists. */
1619 for (sl = specs; sl; sl = sl->next)
1620 if (name_len == sl->name_len && !strcmp (sl->name, name))
1621 break;
1623 if (!sl)
1625 /* Not found - make it. */
1626 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1627 sl->name = xstrdup (name);
1628 sl->name_len = name_len;
1629 sl->ptr_spec = &sl->ptr;
1630 sl->alloc_p = 0;
1631 *(sl->ptr_spec) = "";
1632 sl->next = specs;
1633 specs = sl;
1636 old_spec = *(sl->ptr_spec);
1637 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1638 ? concat (old_spec, spec + 1, NULL)
1639 : xstrdup (spec));
1641 #ifdef DEBUG_SPECS
1642 if (verbose_flag)
1643 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1644 #endif
1646 /* Free the old spec. */
1647 if (old_spec && sl->alloc_p)
1648 free ((PTR) old_spec);
1650 sl->alloc_p = 1;
1653 /* Accumulate a command (program name and args), and run it. */
1655 /* Vector of pointers to arguments in the current line of specifications. */
1657 static const char **argbuf;
1659 /* Number of elements allocated in argbuf. */
1661 static int argbuf_length;
1663 /* Number of elements in argbuf currently in use (containing args). */
1665 static int argbuf_index;
1667 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1668 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1669 it here. */
1671 static struct temp_name {
1672 const char *suffix; /* suffix associated with the code. */
1673 int length; /* strlen (suffix). */
1674 int unique; /* Indicates whether %g or %u/%U was used. */
1675 const char *filename; /* associated filename. */
1676 int filename_length; /* strlen (filename). */
1677 struct temp_name *next;
1678 } *temp_names;
1680 /* Number of commands executed so far. */
1682 static int execution_count;
1684 /* Number of commands that exited with a signal. */
1686 static int signal_count;
1688 /* Name with which this program was invoked. */
1690 static const char *programname;
1692 /* Clear out the vector of arguments (after a command is executed). */
1694 static void
1695 clear_args ()
1697 argbuf_index = 0;
1700 /* Add one argument to the vector at the end.
1701 This is done when a space is seen or at the end of the line.
1702 If DELETE_ALWAYS is nonzero, the arg is a filename
1703 and the file should be deleted eventually.
1704 If DELETE_FAILURE is nonzero, the arg is a filename
1705 and the file should be deleted if this compilation fails. */
1707 static void
1708 store_arg (arg, delete_always, delete_failure)
1709 const char *arg;
1710 int delete_always, delete_failure;
1712 if (argbuf_index + 1 == argbuf_length)
1713 argbuf
1714 = (const char **) xrealloc (argbuf,
1715 (argbuf_length *= 2) * sizeof (const char *));
1717 argbuf[argbuf_index++] = arg;
1718 argbuf[argbuf_index] = 0;
1720 if (delete_always || delete_failure)
1721 record_temp_file (arg, delete_always, delete_failure);
1724 /* Load specs from a file name named FILENAME, replacing occurrences of
1725 various different types of line-endings, \r\n, \n\r and just \r, with
1726 a single \n. */
1728 static char *
1729 load_specs (filename)
1730 const char *filename;
1732 int desc;
1733 int readlen;
1734 struct stat statbuf;
1735 char *buffer;
1736 char *buffer_p;
1737 char *specs;
1738 char *specs_p;
1740 if (verbose_flag)
1741 notice ("Reading specs from %s\n", filename);
1743 /* Open and stat the file. */
1744 desc = open (filename, O_RDONLY, 0);
1745 if (desc < 0)
1746 pfatal_with_name (filename);
1747 if (stat (filename, &statbuf) < 0)
1748 pfatal_with_name (filename);
1750 /* Read contents of file into BUFFER. */
1751 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1752 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1753 if (readlen < 0)
1754 pfatal_with_name (filename);
1755 buffer[readlen] = 0;
1756 close (desc);
1758 specs = xmalloc (readlen + 1);
1759 specs_p = specs;
1760 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1762 int skip = 0;
1763 char c = *buffer_p;
1764 if (c == '\r')
1766 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1767 skip = 1;
1768 else if (*(buffer_p + 1) == '\n') /* \r\n */
1769 skip = 1;
1770 else /* \r */
1771 c = '\n';
1773 if (! skip)
1774 *specs_p++ = c;
1776 *specs_p = '\0';
1778 free (buffer);
1779 return (specs);
1782 /* Read compilation specs from a file named FILENAME,
1783 replacing the default ones.
1785 A suffix which starts with `*' is a definition for
1786 one of the machine-specific sub-specs. The "suffix" should be
1787 *asm, *cc1, *cpp, *link, *startfile, etc.
1788 The corresponding spec is stored in asm_spec, etc.,
1789 rather than in the `compilers' vector.
1791 Anything invalid in the file is a fatal error. */
1793 static void
1794 read_specs (filename, main_p)
1795 const char *filename;
1796 int main_p;
1798 char *buffer;
1799 char *p;
1801 buffer = load_specs (filename);
1803 /* Scan BUFFER for specs, putting them in the vector. */
1804 p = buffer;
1805 while (1)
1807 char *suffix;
1808 char *spec;
1809 char *in, *out, *p1, *p2, *p3;
1811 /* Advance P in BUFFER to the next nonblank nocomment line. */
1812 p = skip_whitespace (p);
1813 if (*p == 0)
1814 break;
1816 /* Is this a special command that starts with '%'? */
1817 /* Don't allow this for the main specs file, since it would
1818 encourage people to overwrite it. */
1819 if (*p == '%' && !main_p)
1821 p1 = p;
1822 while (*p && *p != '\n')
1823 p++;
1825 /* Skip '\n'. */
1826 p++;
1828 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1829 && (p1[sizeof "%include" - 1] == ' '
1830 || p1[sizeof "%include" - 1] == '\t'))
1832 char *new_filename;
1834 p1 += sizeof ("%include");
1835 while (*p1 == ' ' || *p1 == '\t')
1836 p1++;
1838 if (*p1++ != '<' || p[-2] != '>')
1839 fatal ("specs %%include syntax malformed after %ld characters",
1840 (long) (p1 - buffer + 1));
1842 p[-2] = '\0';
1843 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1844 read_specs (new_filename ? new_filename : p1, FALSE);
1845 continue;
1847 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1848 && (p1[sizeof "%include_noerr" - 1] == ' '
1849 || p1[sizeof "%include_noerr" - 1] == '\t'))
1851 char *new_filename;
1853 p1 += sizeof "%include_noerr";
1854 while (*p1 == ' ' || *p1 == '\t')
1855 p1++;
1857 if (*p1++ != '<' || p[-2] != '>')
1858 fatal ("specs %%include syntax malformed after %ld characters",
1859 (long) (p1 - buffer + 1));
1861 p[-2] = '\0';
1862 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1863 if (new_filename)
1864 read_specs (new_filename, FALSE);
1865 else if (verbose_flag)
1866 notice ("could not find specs file %s\n", p1);
1867 continue;
1869 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1870 && (p1[sizeof "%rename" - 1] == ' '
1871 || p1[sizeof "%rename" - 1] == '\t'))
1873 int name_len;
1874 struct spec_list *sl;
1875 struct spec_list *newsl;
1877 /* Get original name. */
1878 p1 += sizeof "%rename";
1879 while (*p1 == ' ' || *p1 == '\t')
1880 p1++;
1882 if (! ISALPHA ((unsigned char) *p1))
1883 fatal ("specs %%rename syntax malformed after %ld characters",
1884 (long) (p1 - buffer));
1886 p2 = p1;
1887 while (*p2 && !ISSPACE ((unsigned char) *p2))
1888 p2++;
1890 if (*p2 != ' ' && *p2 != '\t')
1891 fatal ("specs %%rename syntax malformed after %ld characters",
1892 (long) (p2 - buffer));
1894 name_len = p2 - p1;
1895 *p2++ = '\0';
1896 while (*p2 == ' ' || *p2 == '\t')
1897 p2++;
1899 if (! ISALPHA ((unsigned char) *p2))
1900 fatal ("specs %%rename syntax malformed after %ld characters",
1901 (long) (p2 - buffer));
1903 /* Get new spec name. */
1904 p3 = p2;
1905 while (*p3 && !ISSPACE ((unsigned char) *p3))
1906 p3++;
1908 if (p3 != p - 1)
1909 fatal ("specs %%rename syntax malformed after %ld characters",
1910 (long) (p3 - buffer));
1911 *p3 = '\0';
1913 for (sl = specs; sl; sl = sl->next)
1914 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1915 break;
1917 if (!sl)
1918 fatal ("specs %s spec was not found to be renamed", p1);
1920 if (strcmp (p1, p2) == 0)
1921 continue;
1923 for (newsl = specs; newsl; newsl = newsl->next)
1924 if (strcmp (newsl->name, p2) == 0)
1925 fatal ("%s: attempt to rename spec '%s' to already defined spec '%s'",
1926 filename, p1, p2);
1928 if (verbose_flag)
1930 notice ("rename spec %s to %s\n", p1, p2);
1931 #ifdef DEBUG_SPECS
1932 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1933 #endif
1936 set_spec (p2, *(sl->ptr_spec));
1937 if (sl->alloc_p)
1938 free ((PTR) *(sl->ptr_spec));
1940 *(sl->ptr_spec) = "";
1941 sl->alloc_p = 0;
1942 continue;
1944 else
1945 fatal ("specs unknown %% command after %ld characters",
1946 (long) (p1 - buffer));
1949 /* Find the colon that should end the suffix. */
1950 p1 = p;
1951 while (*p1 && *p1 != ':' && *p1 != '\n')
1952 p1++;
1954 /* The colon shouldn't be missing. */
1955 if (*p1 != ':')
1956 fatal ("specs file malformed after %ld characters",
1957 (long) (p1 - buffer));
1959 /* Skip back over trailing whitespace. */
1960 p2 = p1;
1961 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1962 p2--;
1964 /* Copy the suffix to a string. */
1965 suffix = save_string (p, p2 - p);
1966 /* Find the next line. */
1967 p = skip_whitespace (p1 + 1);
1968 if (p[1] == 0)
1969 fatal ("specs file malformed after %ld characters",
1970 (long) (p - buffer));
1972 p1 = p;
1973 /* Find next blank line or end of string. */
1974 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1975 p1++;
1977 /* Specs end at the blank line and do not include the newline. */
1978 spec = save_string (p, p1 - p);
1979 p = p1;
1981 /* Delete backslash-newline sequences from the spec. */
1982 in = spec;
1983 out = spec;
1984 while (*in != 0)
1986 if (in[0] == '\\' && in[1] == '\n')
1987 in += 2;
1988 else if (in[0] == '#')
1989 while (*in && *in != '\n')
1990 in++;
1992 else
1993 *out++ = *in++;
1995 *out = 0;
1997 if (suffix[0] == '*')
1999 if (! strcmp (suffix, "*link_command"))
2000 link_command_spec = spec;
2001 else
2002 set_spec (suffix + 1, spec);
2004 else
2006 /* Add this pair to the vector. */
2007 compilers
2008 = ((struct compiler *)
2009 xrealloc (compilers,
2010 (n_compilers + 2) * sizeof (struct compiler)));
2012 compilers[n_compilers].suffix = suffix;
2013 compilers[n_compilers].spec = spec;
2014 n_compilers++;
2015 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
2018 if (*suffix == 0)
2019 link_command_spec = spec;
2022 if (link_command_spec == 0)
2023 fatal ("spec file has no spec for linking");
2026 /* Record the names of temporary files we tell compilers to write,
2027 and delete them at the end of the run. */
2029 /* This is the common prefix we use to make temp file names.
2030 It is chosen once for each run of this program.
2031 It is substituted into a spec by %g or %j.
2032 Thus, all temp file names contain this prefix.
2033 In practice, all temp file names start with this prefix.
2035 This prefix comes from the envvar TMPDIR if it is defined;
2036 otherwise, from the P_tmpdir macro if that is defined;
2037 otherwise, in /usr/tmp or /tmp;
2038 or finally the current directory if all else fails. */
2040 static const char *temp_filename;
2042 /* Length of the prefix. */
2044 static int temp_filename_length;
2046 /* Define the list of temporary files to delete. */
2048 struct temp_file
2050 const char *name;
2051 struct temp_file *next;
2054 /* Queue of files to delete on success or failure of compilation. */
2055 static struct temp_file *always_delete_queue;
2056 /* Queue of files to delete on failure of compilation. */
2057 static struct temp_file *failure_delete_queue;
2059 /* Record FILENAME as a file to be deleted automatically.
2060 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
2061 otherwise delete it in any case.
2062 FAIL_DELETE nonzero means delete it if a compilation step fails;
2063 otherwise delete it in any case. */
2065 void
2066 record_temp_file (filename, always_delete, fail_delete)
2067 const char *filename;
2068 int always_delete;
2069 int fail_delete;
2071 char *const name = xstrdup (filename);
2073 if (always_delete)
2075 struct temp_file *temp;
2076 for (temp = always_delete_queue; temp; temp = temp->next)
2077 if (! strcmp (name, temp->name))
2078 goto already1;
2080 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2081 temp->next = always_delete_queue;
2082 temp->name = name;
2083 always_delete_queue = temp;
2085 already1:;
2088 if (fail_delete)
2090 struct temp_file *temp;
2091 for (temp = failure_delete_queue; temp; temp = temp->next)
2092 if (! strcmp (name, temp->name))
2093 goto already2;
2095 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2096 temp->next = failure_delete_queue;
2097 temp->name = name;
2098 failure_delete_queue = temp;
2100 already2:;
2104 /* Delete all the temporary files whose names we previously recorded. */
2106 static void
2107 delete_if_ordinary (name)
2108 const char *name;
2110 struct stat st;
2111 #ifdef DEBUG
2112 int i, c;
2114 printf ("Delete %s? (y or n) ", name);
2115 fflush (stdout);
2116 i = getchar ();
2117 if (i != '\n')
2118 while ((c = getchar ()) != '\n' && c != EOF)
2121 if (i == 'y' || i == 'Y')
2122 #endif /* DEBUG */
2123 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
2124 if (unlink (name) < 0)
2125 if (verbose_flag)
2126 perror_with_name (name);
2129 static void
2130 delete_temp_files ()
2132 struct temp_file *temp;
2134 for (temp = always_delete_queue; temp; temp = temp->next)
2135 delete_if_ordinary (temp->name);
2136 always_delete_queue = 0;
2139 /* Delete all the files to be deleted on error. */
2141 static void
2142 delete_failure_queue ()
2144 struct temp_file *temp;
2146 for (temp = failure_delete_queue; temp; temp = temp->next)
2147 delete_if_ordinary (temp->name);
2150 static void
2151 clear_failure_queue ()
2153 failure_delete_queue = 0;
2156 /* Build a list of search directories from PATHS.
2157 PREFIX is a string to prepend to the list.
2158 If CHECK_DIR_P is non-zero we ensure the directory exists.
2159 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2160 It is also used by the --print-search-dirs flag. */
2162 static char *
2163 build_search_list (paths, prefix, check_dir_p)
2164 struct path_prefix *paths;
2165 const char *prefix;
2166 int check_dir_p;
2168 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2169 int just_suffix_len
2170 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2171 int first_time = TRUE;
2172 struct prefix_list *pprefix;
2174 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2175 obstack_1grow (&collect_obstack, '=');
2177 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2179 int len = strlen (pprefix->prefix);
2181 if (machine_suffix
2182 && (! check_dir_p
2183 || is_directory (pprefix->prefix, machine_suffix, 0)))
2185 if (!first_time)
2186 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2188 first_time = FALSE;
2189 obstack_grow (&collect_obstack, pprefix->prefix, len);
2190 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2193 if (just_machine_suffix
2194 && pprefix->require_machine_suffix == 2
2195 && (! check_dir_p
2196 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2198 if (! first_time)
2199 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2201 first_time = FALSE;
2202 obstack_grow (&collect_obstack, pprefix->prefix, len);
2203 obstack_grow (&collect_obstack, just_machine_suffix,
2204 just_suffix_len);
2207 if (! pprefix->require_machine_suffix)
2209 if (! first_time)
2210 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2212 first_time = FALSE;
2213 obstack_grow (&collect_obstack, pprefix->prefix, len);
2217 obstack_1grow (&collect_obstack, '\0');
2218 return obstack_finish (&collect_obstack);
2221 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2222 for collect. */
2224 static void
2225 putenv_from_prefixes (paths, env_var)
2226 struct path_prefix *paths;
2227 const char *env_var;
2229 putenv (build_search_list (paths, env_var, 1));
2232 #ifndef VMS
2234 /* FIXME: the location independence code for VMS is hairier than this,
2235 and hasn't been written. */
2237 /* Split a filename into component directories. */
2239 static char **
2240 split_directories (name, ptr_num_dirs)
2241 const char *name;
2242 int *ptr_num_dirs;
2244 int num_dirs = 0;
2245 char **dirs;
2246 const char *p, *q;
2247 int ch;
2249 /* Count the number of directories. Special case MSDOS disk names as part
2250 of the initial directory. */
2251 p = name;
2252 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2253 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2255 p += 3;
2256 num_dirs++;
2258 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2260 while ((ch = *p++) != '\0')
2262 if (IS_DIR_SEPARATOR (ch))
2264 num_dirs++;
2265 while (IS_DIR_SEPARATOR (*p))
2266 p++;
2270 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2272 /* Now copy the directory parts. */
2273 num_dirs = 0;
2274 p = name;
2275 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2276 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2278 dirs[num_dirs++] = save_string (p, 3);
2279 p += 3;
2281 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2283 q = p;
2284 while ((ch = *p++) != '\0')
2286 if (IS_DIR_SEPARATOR (ch))
2288 while (IS_DIR_SEPARATOR (*p))
2289 p++;
2291 dirs[num_dirs++] = save_string (q, p - q);
2292 q = p;
2296 if (p - 1 - q > 0)
2297 dirs[num_dirs++] = save_string (q, p - 1 - q);
2299 dirs[num_dirs] = NULL;
2300 if (ptr_num_dirs)
2301 *ptr_num_dirs = num_dirs;
2303 return dirs;
2306 /* Release storage held by split directories. */
2308 static void
2309 free_split_directories (dirs)
2310 char **dirs;
2312 int i = 0;
2314 while (dirs[i] != NULL)
2315 free (dirs[i++]);
2317 free ((char *) dirs);
2320 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2321 to PREFIX starting with the directory portion of PROGNAME and a relative
2322 pathname of the difference between BIN_PREFIX and PREFIX.
2324 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2325 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2326 function will return /red/green/blue/../omega.
2328 If no relative prefix can be found, return NULL. */
2330 static char *
2331 make_relative_prefix (progname, bin_prefix, prefix)
2332 const char *progname;
2333 const char *bin_prefix;
2334 const char *prefix;
2336 char **prog_dirs, **bin_dirs, **prefix_dirs;
2337 int prog_num, bin_num, prefix_num, std_loc_p;
2338 int i, n, common;
2340 prog_dirs = split_directories (progname, &prog_num);
2341 bin_dirs = split_directories (bin_prefix, &bin_num);
2343 /* If there is no full pathname, try to find the program by checking in each
2344 of the directories specified in the PATH environment variable. */
2345 if (prog_num == 1)
2347 char *temp;
2349 GET_ENV_PATH_LIST (temp, "PATH");
2350 if (temp)
2352 char *startp, *endp, *nstore;
2353 size_t prefixlen = strlen (temp) + 1;
2354 if (prefixlen < 2)
2355 prefixlen = 2;
2357 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
2359 startp = endp = temp;
2360 while (1)
2362 if (*endp == PATH_SEPARATOR || *endp == 0)
2364 if (endp == startp)
2366 nstore[0] = '.';
2367 nstore[1] = DIR_SEPARATOR;
2368 nstore[2] = '\0';
2370 else
2372 strncpy (nstore, startp, endp - startp);
2373 if (! IS_DIR_SEPARATOR (endp[-1]))
2375 nstore[endp - startp] = DIR_SEPARATOR;
2376 nstore[endp - startp + 1] = 0;
2378 else
2379 nstore[endp - startp] = 0;
2381 strcat (nstore, progname);
2382 if (! access (nstore, X_OK)
2383 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
2384 || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
2385 #endif
2388 free_split_directories (prog_dirs);
2389 progname = nstore;
2390 prog_dirs = split_directories (progname, &prog_num);
2391 break;
2394 if (*endp == 0)
2395 break;
2396 endp = startp = endp + 1;
2398 else
2399 endp++;
2404 /* Remove the program name from comparison of directory names. */
2405 prog_num--;
2407 /* Determine if the compiler is installed in the standard location, and if
2408 so, we don't need to specify relative directories. Also, if argv[0]
2409 doesn't contain any directory specifiers, there is not much we can do. */
2410 std_loc_p = 0;
2411 if (prog_num == bin_num)
2413 for (i = 0; i < bin_num; i++)
2415 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2416 break;
2419 if (prog_num <= 0 || i == bin_num)
2421 std_loc_p = 1;
2422 free_split_directories (prog_dirs);
2423 free_split_directories (bin_dirs);
2424 prog_dirs = bin_dirs = (char **) 0;
2425 return NULL;
2429 prefix_dirs = split_directories (prefix, &prefix_num);
2431 /* Find how many directories are in common between bin_prefix & prefix. */
2432 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2433 for (common = 0; common < n; common++)
2435 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2436 break;
2439 /* If there are no common directories, there can be no relative prefix. */
2440 if (common == 0)
2442 free_split_directories (prog_dirs);
2443 free_split_directories (bin_dirs);
2444 free_split_directories (prefix_dirs);
2445 return NULL;
2448 /* Build up the pathnames in argv[0]. */
2449 for (i = 0; i < prog_num; i++)
2450 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2452 /* Now build up the ..'s. */
2453 for (i = common; i < n; i++)
2455 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2456 obstack_1grow (&obstack, DIR_SEPARATOR);
2459 /* Put in directories to move over to prefix. */
2460 for (i = common; i < prefix_num; i++)
2461 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2463 free_split_directories (prog_dirs);
2464 free_split_directories (bin_dirs);
2465 free_split_directories (prefix_dirs);
2467 obstack_1grow (&obstack, '\0');
2468 return obstack_finish (&obstack);
2470 #endif /* VMS */
2472 /* Check whether NAME can be accessed in MODE. This is like access,
2473 except that it never considers directories to be executable. */
2475 static int
2476 access_check (name, mode)
2477 const char *name;
2478 int mode;
2480 if (mode == X_OK)
2482 struct stat st;
2484 if (stat (name, &st) < 0
2485 || S_ISDIR (st.st_mode))
2486 return -1;
2489 return access (name, mode);
2492 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2493 access to check permissions.
2494 Return 0 if not found, otherwise return its name, allocated with malloc. */
2496 static char *
2497 find_a_file (pprefix, name, mode)
2498 struct path_prefix *pprefix;
2499 const char *name;
2500 int mode;
2502 char *temp;
2503 const char *const file_suffix =
2504 ((mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "");
2505 struct prefix_list *pl;
2506 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2508 #ifdef DEFAULT_ASSEMBLER
2509 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2510 return xstrdup (DEFAULT_ASSEMBLER);
2511 #endif
2513 #ifdef DEFAULT_LINKER
2514 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2515 return xstrdup (DEFAULT_LINKER);
2516 #endif
2518 if (machine_suffix)
2519 len += strlen (machine_suffix);
2521 temp = xmalloc (len);
2523 /* Determine the filename to execute (special case for absolute paths). */
2525 if (IS_ABSOLUTE_PATHNAME (name))
2527 if (access (name, mode) == 0)
2529 strcpy (temp, name);
2530 return temp;
2533 else
2534 for (pl = pprefix->plist; pl; pl = pl->next)
2536 if (machine_suffix)
2538 /* Some systems have a suffix for executable files.
2539 So try appending that first. */
2540 if (file_suffix[0] != 0)
2542 strcpy (temp, pl->prefix);
2543 strcat (temp, machine_suffix);
2544 strcat (temp, name);
2545 strcat (temp, file_suffix);
2546 if (access_check (temp, mode) == 0)
2548 if (pl->used_flag_ptr != 0)
2549 *pl->used_flag_ptr = 1;
2550 return temp;
2554 /* Now try just the name. */
2555 strcpy (temp, pl->prefix);
2556 strcat (temp, machine_suffix);
2557 strcat (temp, name);
2558 if (access_check (temp, mode) == 0)
2560 if (pl->used_flag_ptr != 0)
2561 *pl->used_flag_ptr = 1;
2562 return temp;
2566 /* Certain prefixes are tried with just the machine type,
2567 not the version. This is used for finding as, ld, etc. */
2568 if (just_machine_suffix && pl->require_machine_suffix == 2)
2570 /* Some systems have a suffix for executable files.
2571 So try appending that first. */
2572 if (file_suffix[0] != 0)
2574 strcpy (temp, pl->prefix);
2575 strcat (temp, just_machine_suffix);
2576 strcat (temp, name);
2577 strcat (temp, file_suffix);
2578 if (access_check (temp, mode) == 0)
2580 if (pl->used_flag_ptr != 0)
2581 *pl->used_flag_ptr = 1;
2582 return temp;
2586 strcpy (temp, pl->prefix);
2587 strcat (temp, just_machine_suffix);
2588 strcat (temp, name);
2589 if (access_check (temp, mode) == 0)
2591 if (pl->used_flag_ptr != 0)
2592 *pl->used_flag_ptr = 1;
2593 return temp;
2597 /* Certain prefixes can't be used without the machine suffix
2598 when the machine or version is explicitly specified. */
2599 if (! pl->require_machine_suffix)
2601 /* Some systems have a suffix for executable files.
2602 So try appending that first. */
2603 if (file_suffix[0] != 0)
2605 strcpy (temp, pl->prefix);
2606 strcat (temp, name);
2607 strcat (temp, file_suffix);
2608 if (access_check (temp, mode) == 0)
2610 if (pl->used_flag_ptr != 0)
2611 *pl->used_flag_ptr = 1;
2612 return temp;
2616 strcpy (temp, pl->prefix);
2617 strcat (temp, name);
2618 if (access_check (temp, mode) == 0)
2620 if (pl->used_flag_ptr != 0)
2621 *pl->used_flag_ptr = 1;
2622 return temp;
2627 free (temp);
2628 return 0;
2631 /* Ranking of prefixes in the sort list. -B prefixes are put before
2632 all others. */
2634 enum path_prefix_priority
2636 PREFIX_PRIORITY_B_OPT,
2637 PREFIX_PRIORITY_LAST
2640 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2641 order according to PRIORITY. Within each PRIORITY, new entries are
2642 appended.
2644 If WARN is nonzero, we will warn if no file is found
2645 through this prefix. WARN should point to an int
2646 which will be set to 1 if this entry is used.
2648 COMPONENT is the value to be passed to update_path.
2650 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2651 the complete value of machine_suffix.
2652 2 means try both machine_suffix and just_machine_suffix. */
2654 static void
2655 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2656 struct path_prefix *pprefix;
2657 const char *prefix;
2658 const char *component;
2659 /* enum prefix_priority */ int priority;
2660 int require_machine_suffix;
2661 int *warn;
2663 struct prefix_list *pl, **prev;
2664 int len;
2666 for (prev = &pprefix->plist;
2667 (*prev) != NULL && (*prev)->priority <= priority;
2668 prev = &(*prev)->next)
2671 /* Keep track of the longest prefix */
2673 prefix = update_path (prefix, component);
2674 len = strlen (prefix);
2675 if (len > pprefix->max_len)
2676 pprefix->max_len = len;
2678 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2679 pl->prefix = prefix;
2680 pl->require_machine_suffix = require_machine_suffix;
2681 pl->used_flag_ptr = warn;
2682 pl->priority = priority;
2683 if (warn)
2684 *warn = 0;
2686 /* Insert after PREV */
2687 pl->next = (*prev);
2688 (*prev) = pl;
2691 /* Execute the command specified by the arguments on the current line of spec.
2692 When using pipes, this includes several piped-together commands
2693 with `|' between them.
2695 Return 0 if successful, -1 if failed. */
2697 static int
2698 execute ()
2700 int i;
2701 int n_commands; /* # of command. */
2702 char *string;
2703 struct command
2705 const char *prog; /* program name. */
2706 const char **argv; /* vector of args. */
2707 int pid; /* pid of process for this command. */
2710 struct command *commands; /* each command buffer with above info. */
2712 /* Count # of piped commands. */
2713 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2714 if (strcmp (argbuf[i], "|") == 0)
2715 n_commands++;
2717 /* Get storage for each command. */
2718 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2720 /* Split argbuf into its separate piped processes,
2721 and record info about each one.
2722 Also search for the programs that are to be run. */
2724 commands[0].prog = argbuf[0]; /* first command. */
2725 commands[0].argv = &argbuf[0];
2726 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2728 if (string)
2729 commands[0].argv[0] = string;
2731 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2732 if (strcmp (argbuf[i], "|") == 0)
2733 { /* each command. */
2734 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2735 fatal ("-pipe not supported");
2736 #endif
2737 argbuf[i] = 0; /* termination of command args. */
2738 commands[n_commands].prog = argbuf[i + 1];
2739 commands[n_commands].argv = &argbuf[i + 1];
2740 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2741 if (string)
2742 commands[n_commands].argv[0] = string;
2743 n_commands++;
2746 argbuf[argbuf_index] = 0;
2748 /* If -v, print what we are about to do, and maybe query. */
2750 if (verbose_flag)
2752 /* For help listings, put a blank line between sub-processes. */
2753 if (print_help_list)
2754 fputc ('\n', stderr);
2756 /* Print each piped command as a separate line. */
2757 for (i = 0; i < n_commands; i++)
2759 const char *const *j;
2761 if (verbose_only_flag)
2763 for (j = commands[i].argv; *j; j++)
2765 const char *p;
2766 fprintf (stderr, " \"");
2767 for (p = *j; *p; ++p)
2769 if (*p == '"' || *p == '\\' || *p == '$')
2770 fputc ('\\', stderr);
2771 fputc (*p, stderr);
2773 fputc ('"', stderr);
2776 else
2777 for (j = commands[i].argv; *j; j++)
2778 fprintf (stderr, " %s", *j);
2780 /* Print a pipe symbol after all but the last command. */
2781 if (i + 1 != n_commands)
2782 fprintf (stderr, " |");
2783 fprintf (stderr, "\n");
2785 fflush (stderr);
2786 if (verbose_only_flag != 0)
2787 return 0;
2788 #ifdef DEBUG
2789 notice ("\nGo ahead? (y or n) ");
2790 fflush (stderr);
2791 i = getchar ();
2792 if (i != '\n')
2793 while (getchar () != '\n')
2796 if (i != 'y' && i != 'Y')
2797 return 0;
2798 #endif /* DEBUG */
2801 /* Run each piped subprocess. */
2803 for (i = 0; i < n_commands; i++)
2805 char *errmsg_fmt, *errmsg_arg;
2806 const char *string = commands[i].argv[0];
2808 /* For some bizarre reason, the second argument of execvp() is
2809 char *const *, not const char *const *. */
2810 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2811 programname, temp_filename,
2812 &errmsg_fmt, &errmsg_arg,
2813 ((i == 0 ? PEXECUTE_FIRST : 0)
2814 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2815 | (string == commands[i].prog
2816 ? PEXECUTE_SEARCH : 0)
2817 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2819 if (commands[i].pid == -1)
2820 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2822 if (string != commands[i].prog)
2823 free ((PTR) string);
2826 execution_count++;
2828 /* Wait for all the subprocesses to finish.
2829 We don't care what order they finish in;
2830 we know that N_COMMANDS waits will get them all.
2831 Ignore subprocesses that we don't know about,
2832 since they can be spawned by the process that exec'ed us. */
2835 int ret_code = 0;
2836 #ifdef HAVE_GETRUSAGE
2837 struct timeval d;
2838 double ut = 0.0, st = 0.0;
2839 #endif
2841 for (i = 0; i < n_commands;)
2843 int j;
2844 int status;
2845 int pid;
2847 pid = pwait (commands[i].pid, &status, 0);
2848 if (pid < 0)
2849 abort ();
2851 #ifdef HAVE_GETRUSAGE
2852 if (report_times)
2854 /* getrusage returns the total resource usage of all children
2855 up to now. Copy the previous values into prus, get the
2856 current statistics, then take the difference. */
2858 prus = rus;
2859 getrusage (RUSAGE_CHILDREN, &rus);
2860 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2861 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2862 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2864 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2865 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2866 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2868 #endif
2870 for (j = 0; j < n_commands; j++)
2871 if (commands[j].pid == pid)
2873 i++;
2874 if (WIFSIGNALED (status))
2876 #ifdef SIGPIPE
2877 /* SIGPIPE is a special case. It happens in -pipe mode
2878 when the compiler dies before the preprocessor is
2879 done, or the assembler dies before the compiler is
2880 done. There's generally been an error already, and
2881 this is just fallout. So don't generate another error
2882 unless we would otherwise have succeeded. */
2883 if (WTERMSIG (status) == SIGPIPE
2884 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2886 else
2887 #endif
2888 fatal ("\
2889 Internal error: %s (program %s)\n\
2890 Please submit a full bug report.\n\
2891 See %s for instructions.",
2892 strsignal (WTERMSIG (status)), commands[j].prog,
2893 GCCBUGURL);
2894 signal_count++;
2895 ret_code = -1;
2897 else if (WIFEXITED (status)
2898 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2900 if (WEXITSTATUS (status) > greatest_status)
2901 greatest_status = WEXITSTATUS (status);
2902 ret_code = -1;
2904 #ifdef HAVE_GETRUSAGE
2905 if (report_times && ut + st != 0)
2906 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2907 #endif
2908 break;
2911 return ret_code;
2915 /* Find all the switches given to us
2916 and make a vector describing them.
2917 The elements of the vector are strings, one per switch given.
2918 If a switch uses following arguments, then the `part1' field
2919 is the switch itself and the `args' field
2920 is a null-terminated vector containing the following arguments.
2921 The `live_cond' field is:
2922 0 when initialized
2923 1 if the switch is true in a conditional spec,
2924 -1 if false (overridden by a later switch)
2925 -2 if this switch should be ignored (used in %{<S})
2926 The `validated' field is nonzero if any spec has looked at this switch;
2927 if it remains zero at the end of the run, it must be meaningless. */
2929 #define SWITCH_OK 0
2930 #define SWITCH_FALSE -1
2931 #define SWITCH_IGNORE -2
2932 #define SWITCH_LIVE 1
2934 struct switchstr
2936 const char *part1;
2937 const char **args;
2938 int live_cond;
2939 unsigned char validated;
2940 unsigned char ordering;
2943 static struct switchstr *switches;
2945 static int n_switches;
2947 struct infile
2949 const char *name;
2950 const char *language;
2953 /* Also a vector of input files specified. */
2955 static struct infile *infiles;
2957 int n_infiles;
2959 /* This counts the number of libraries added by lang_specific_driver, so that
2960 we can tell if there were any user supplied any files or libraries. */
2962 static int added_libraries;
2964 /* And a vector of corresponding output files is made up later. */
2966 const char **outfiles;
2968 /* Used to track if none of the -B paths are used. */
2969 static int warn_B;
2971 /* Used to track if standard path isn't used and -b or -V is specified. */
2972 static int warn_std;
2974 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2975 static int *warn_std_ptr = 0;
2977 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2979 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2980 is true if we should look for an executable suffix. DO_OBJ
2981 is true if we should look for an object suffix. */
2983 static const char *
2984 convert_filename (name, do_exe, do_obj)
2985 const char *name;
2986 int do_exe ATTRIBUTE_UNUSED;
2987 int do_obj ATTRIBUTE_UNUSED;
2989 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2990 int i;
2991 #endif
2992 int len;
2994 if (name == NULL)
2995 return NULL;
2997 len = strlen (name);
2999 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3000 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
3001 if (do_obj && len > 2
3002 && name[len - 2] == '.'
3003 && name[len - 1] == 'o')
3005 obstack_grow (&obstack, name, len - 2);
3006 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
3007 name = obstack_finish (&obstack);
3009 #endif
3011 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3012 /* If there is no filetype, make it the executable suffix (which includes
3013 the "."). But don't get confused if we have just "-o". */
3014 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
3015 return name;
3017 for (i = len - 1; i >= 0; i--)
3018 if (IS_DIR_SEPARATOR (name[i]))
3019 break;
3021 for (i++; i < len; i++)
3022 if (name[i] == '.')
3023 return name;
3025 obstack_grow (&obstack, name, len);
3026 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
3027 strlen (TARGET_EXECUTABLE_SUFFIX));
3028 name = obstack_finish (&obstack);
3029 #endif
3031 return name;
3033 #endif
3035 /* Display the command line switches accepted by gcc. */
3036 static void
3037 display_help ()
3039 printf (_("Usage: %s [options] file...\n"), programname);
3040 fputs (_("Options:\n"), stdout);
3042 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
3043 fputs (_(" --help Display this information\n"), stdout);
3044 fputs (_(" --target-help Display target specific command line options\n"), stdout);
3045 if (! verbose_flag)
3046 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
3047 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
3048 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
3049 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
3050 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
3051 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
3052 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
3053 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
3054 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
3055 fputs (_("\
3056 -print-multi-lib Display the mapping between command line options and\n\
3057 multiple library search directories\n"), stdout);
3058 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
3059 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
3060 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
3061 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
3062 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
3063 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
3064 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
3065 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
3066 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
3067 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
3068 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
3069 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
3070 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
3071 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
3072 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
3073 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
3074 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
3075 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
3076 fputs (_("\
3077 -x <language> Specify the language of the following input files\n\
3078 Permissable languages include: c c++ assembler none\n\
3079 'none' means revert to the default behavior of\n\
3080 guessing the language based on the file's extension\n\
3081 "), stdout);
3083 printf (_("\
3084 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3085 passed on to the various sub-processes invoked by %s. In order to pass\n\
3086 other options on to these processes the -W<letter> options must be used.\n\
3087 "), programname);
3089 /* The rest of the options are displayed by invocations of the various
3090 sub-processes. */
3093 static void
3094 add_preprocessor_option (option, len)
3095 const char *option;
3096 int len;
3098 n_preprocessor_options++;
3100 if (! preprocessor_options)
3101 preprocessor_options
3102 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
3103 else
3104 preprocessor_options
3105 = (char **) xrealloc (preprocessor_options,
3106 n_preprocessor_options * sizeof (char *));
3108 preprocessor_options [n_preprocessor_options - 1] =
3109 save_string (option, len);
3112 static void
3113 add_assembler_option (option, len)
3114 const char *option;
3115 int len;
3117 n_assembler_options++;
3119 if (! assembler_options)
3120 assembler_options
3121 = (char **) xmalloc (n_assembler_options * sizeof (char *));
3122 else
3123 assembler_options
3124 = (char **) xrealloc (assembler_options,
3125 n_assembler_options * sizeof (char *));
3127 assembler_options [n_assembler_options - 1] = save_string (option, len);
3130 static void
3131 add_linker_option (option, len)
3132 const char *option;
3133 int len;
3135 n_linker_options++;
3137 if (! linker_options)
3138 linker_options
3139 = (char **) xmalloc (n_linker_options * sizeof (char *));
3140 else
3141 linker_options
3142 = (char **) xrealloc (linker_options,
3143 n_linker_options * sizeof (char *));
3145 linker_options [n_linker_options - 1] = save_string (option, len);
3148 /* Create the vector `switches' and its contents.
3149 Store its length in `n_switches'. */
3151 static void
3152 process_command (argc, argv)
3153 int argc;
3154 const char *const *argv;
3156 int i;
3157 const char *temp;
3158 char *temp1;
3159 const char *spec_lang = 0;
3160 int last_language_n_infiles;
3161 int have_c = 0;
3162 int have_o = 0;
3163 int lang_n_infiles = 0;
3164 #ifdef MODIFY_TARGET_NAME
3165 int is_modify_target_name;
3166 int j;
3167 #endif
3169 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3171 n_switches = 0;
3172 n_infiles = 0;
3173 added_libraries = 0;
3175 /* Figure compiler version from version string. */
3177 compiler_version = temp1 = xstrdup (version_string);
3179 for (; *temp1; ++temp1)
3181 if (*temp1 == ' ')
3183 *temp1 = '\0';
3184 break;
3188 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3189 see if we can create it from the pathname specified in argv[0]. */
3191 #ifndef VMS
3192 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3193 if (!gcc_exec_prefix)
3195 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3196 standard_exec_prefix);
3197 if (gcc_exec_prefix)
3198 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
3200 #endif
3202 if (gcc_exec_prefix)
3204 int len = strlen (gcc_exec_prefix);
3206 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3207 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3209 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3210 if (IS_DIR_SEPARATOR (*temp)
3211 && strncmp (temp + 1, "lib", 3) == 0
3212 && IS_DIR_SEPARATOR (temp[4])
3213 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3214 len -= sizeof ("/lib/gcc-lib/") - 1;
3217 set_std_prefix (gcc_exec_prefix, len);
3218 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3219 PREFIX_PRIORITY_LAST, 0, NULL);
3220 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3221 PREFIX_PRIORITY_LAST, 0, NULL);
3224 /* COMPILER_PATH and LIBRARY_PATH have values
3225 that are lists of directory names with colons. */
3227 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3228 if (temp)
3230 const char *startp, *endp;
3231 char *nstore = (char *) alloca (strlen (temp) + 3);
3233 startp = endp = temp;
3234 while (1)
3236 if (*endp == PATH_SEPARATOR || *endp == 0)
3238 strncpy (nstore, startp, endp - startp);
3239 if (endp == startp)
3240 strcpy (nstore, concat (".", dir_separator_str, NULL));
3241 else if (!IS_DIR_SEPARATOR (endp[-1]))
3243 nstore[endp - startp] = DIR_SEPARATOR;
3244 nstore[endp - startp + 1] = 0;
3246 else
3247 nstore[endp - startp] = 0;
3248 add_prefix (&exec_prefixes, nstore, 0,
3249 PREFIX_PRIORITY_LAST, 0, NULL);
3250 add_prefix (&include_prefixes,
3251 concat (nstore, "include", NULL),
3252 0, PREFIX_PRIORITY_LAST, 0, NULL);
3253 if (*endp == 0)
3254 break;
3255 endp = startp = endp + 1;
3257 else
3258 endp++;
3262 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3263 if (temp && *cross_compile == '0')
3265 const char *startp, *endp;
3266 char *nstore = (char *) alloca (strlen (temp) + 3);
3268 startp = endp = temp;
3269 while (1)
3271 if (*endp == PATH_SEPARATOR || *endp == 0)
3273 strncpy (nstore, startp, endp - startp);
3274 if (endp == startp)
3275 strcpy (nstore, concat (".", dir_separator_str, NULL));
3276 else if (!IS_DIR_SEPARATOR (endp[-1]))
3278 nstore[endp - startp] = DIR_SEPARATOR;
3279 nstore[endp - startp + 1] = 0;
3281 else
3282 nstore[endp - startp] = 0;
3283 add_prefix (&startfile_prefixes, nstore, NULL,
3284 PREFIX_PRIORITY_LAST, 0, NULL);
3285 if (*endp == 0)
3286 break;
3287 endp = startp = endp + 1;
3289 else
3290 endp++;
3294 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3295 GET_ENV_PATH_LIST (temp, "LPATH");
3296 if (temp && *cross_compile == '0')
3298 const char *startp, *endp;
3299 char *nstore = (char *) alloca (strlen (temp) + 3);
3301 startp = endp = temp;
3302 while (1)
3304 if (*endp == PATH_SEPARATOR || *endp == 0)
3306 strncpy (nstore, startp, endp - startp);
3307 if (endp == startp)
3308 strcpy (nstore, concat (".", dir_separator_str, NULL));
3309 else if (!IS_DIR_SEPARATOR (endp[-1]))
3311 nstore[endp - startp] = DIR_SEPARATOR;
3312 nstore[endp - startp + 1] = 0;
3314 else
3315 nstore[endp - startp] = 0;
3316 add_prefix (&startfile_prefixes, nstore, NULL,
3317 PREFIX_PRIORITY_LAST, 0, NULL);
3318 if (*endp == 0)
3319 break;
3320 endp = startp = endp + 1;
3322 else
3323 endp++;
3327 /* Convert new-style -- options to old-style. */
3328 translate_options (&argc, &argv);
3330 /* Do language-specific adjustment/addition of flags. */
3331 lang_specific_driver (&argc, &argv, &added_libraries);
3333 /* Scan argv twice. Here, the first time, just count how many switches
3334 there will be in their vector, and how many input files in theirs.
3335 Also parse any switches that determine the configuration name, such as -b.
3336 Here we also parse the switches that cc itself uses (e.g. -v). */
3338 for (i = 1; i < argc; i++)
3340 if (! strcmp (argv[i], "-dumpspecs"))
3342 struct spec_list *sl;
3343 init_spec ();
3344 for (sl = specs; sl; sl = sl->next)
3345 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3346 if (link_command_spec)
3347 printf ("*link_command:\n%s\n\n", link_command_spec);
3348 exit (0);
3350 else if (! strcmp (argv[i], "-dumpversion"))
3352 printf ("%s\n", spec_version);
3353 exit (0);
3355 else if (! strcmp (argv[i], "-dumpmachine"))
3357 printf ("%s\n", spec_machine);
3358 exit (0);
3360 else if (strcmp (argv[i], "-fversion") == 0)
3362 /* translate_options () has turned --version into -fversion. */
3363 printf (_("%s (GCC) %s\n"), programname, version_string);
3364 fputs (_("Copyright (C) 2002 Free Software Foundation, Inc.\n"),
3365 stdout);
3366 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
3367 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
3368 stdout);
3369 exit (0);
3371 else if (strcmp (argv[i], "-fhelp") == 0)
3373 /* translate_options () has turned --help into -fhelp. */
3374 print_help_list = 1;
3376 /* We will be passing a dummy file on to the sub-processes. */
3377 n_infiles++;
3378 n_switches++;
3380 /* CPP driver cannot obtain switch from cc1_options. */
3381 if (is_cpp_driver)
3382 add_preprocessor_option ("--help", 6);
3383 add_assembler_option ("--help", 6);
3384 add_linker_option ("--help", 6);
3386 else if (strcmp (argv[i], "-ftarget-help") == 0)
3388 /* translate_options() has turned --target-help into -ftarget-help. */
3389 target_help_flag = 1;
3391 /* We will be passing a dummy file on to the sub-processes. */
3392 n_infiles++;
3393 n_switches++;
3395 /* CPP driver cannot obtain switch from cc1_options. */
3396 if (is_cpp_driver)
3397 add_preprocessor_option ("--target-help", 13);
3398 add_assembler_option ("--target-help", 13);
3399 add_linker_option ("--target-help", 13);
3401 else if (! strcmp (argv[i], "-pass-exit-codes"))
3403 pass_exit_codes = 1;
3404 n_switches++;
3406 else if (! strcmp (argv[i], "-print-search-dirs"))
3407 print_search_dirs = 1;
3408 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3409 print_file_name = "libgcc.a";
3410 else if (! strncmp (argv[i], "-print-file-name=", 17))
3411 print_file_name = argv[i] + 17;
3412 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3413 print_prog_name = argv[i] + 17;
3414 else if (! strcmp (argv[i], "-print-multi-lib"))
3415 print_multi_lib = 1;
3416 else if (! strcmp (argv[i], "-print-multi-directory"))
3417 print_multi_directory = 1;
3418 else if (! strncmp (argv[i], "-Wa,", 4))
3420 int prev, j;
3421 /* Pass the rest of this option to the assembler. */
3423 /* Split the argument at commas. */
3424 prev = 4;
3425 for (j = 4; argv[i][j]; j++)
3426 if (argv[i][j] == ',')
3428 add_assembler_option (argv[i] + prev, j - prev);
3429 prev = j + 1;
3432 /* Record the part after the last comma. */
3433 add_assembler_option (argv[i] + prev, j - prev);
3435 else if (! strncmp (argv[i], "-Wp,", 4))
3437 int prev, j;
3438 /* Pass the rest of this option to the preprocessor. */
3440 /* Split the argument at commas. */
3441 prev = 4;
3442 for (j = 4; argv[i][j]; j++)
3443 if (argv[i][j] == ',')
3445 add_preprocessor_option (argv[i] + prev, j - prev);
3446 prev = j + 1;
3449 /* Record the part after the last comma. */
3450 add_preprocessor_option (argv[i] + prev, j - prev);
3452 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3453 /* The +e options to the C++ front-end. */
3454 n_switches++;
3455 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3457 int j;
3458 /* Split the argument at commas. */
3459 for (j = 3; argv[i][j]; j++)
3460 n_infiles += (argv[i][j] == ',');
3462 else if (strcmp (argv[i], "-Xlinker") == 0)
3464 if (i + 1 == argc)
3465 fatal ("argument to `-Xlinker' is missing");
3467 n_infiles++;
3468 i++;
3470 else if (strcmp (argv[i], "-l") == 0)
3472 if (i + 1 == argc)
3473 fatal ("argument to `-l' is missing");
3475 n_infiles++;
3476 i++;
3478 else if (strncmp (argv[i], "-l", 2) == 0)
3479 n_infiles++;
3480 else if (strcmp (argv[i], "-save-temps") == 0)
3482 save_temps_flag = 1;
3483 n_switches++;
3485 else if (strcmp (argv[i], "-specs") == 0)
3487 struct user_specs *user = (struct user_specs *)
3488 xmalloc (sizeof (struct user_specs));
3489 if (++i >= argc)
3490 fatal ("argument to `-specs' is missing");
3492 user->next = (struct user_specs *) 0;
3493 user->filename = argv[i];
3494 if (user_specs_tail)
3495 user_specs_tail->next = user;
3496 else
3497 user_specs_head = user;
3498 user_specs_tail = user;
3500 else if (strncmp (argv[i], "-specs=", 7) == 0)
3502 struct user_specs *user = (struct user_specs *)
3503 xmalloc (sizeof (struct user_specs));
3504 if (strlen (argv[i]) == 7)
3505 fatal ("argument to `-specs=' is missing");
3507 user->next = (struct user_specs *) 0;
3508 user->filename = argv[i] + 7;
3509 if (user_specs_tail)
3510 user_specs_tail->next = user;
3511 else
3512 user_specs_head = user;
3513 user_specs_tail = user;
3515 else if (strcmp (argv[i], "-time") == 0)
3516 report_times = 1;
3517 else if (strcmp (argv[i], "-###") == 0)
3519 /* This is similar to -v except that there is no execution
3520 of the commands and the echoed arguments are quoted. It
3521 is intended for use in shell scripts to capture the
3522 driver-generated command line. */
3523 verbose_only_flag++;
3524 verbose_flag++;
3526 else if (argv[i][0] == '-' && argv[i][1] != 0)
3528 const char *p = &argv[i][1];
3529 int c = *p;
3531 switch (c)
3533 case 'b':
3534 n_switches++;
3535 if (p[1] == 0 && i + 1 == argc)
3536 fatal ("argument to `-b' is missing");
3537 if (p[1] == 0)
3538 spec_machine = argv[++i];
3539 else
3540 spec_machine = p + 1;
3542 warn_std_ptr = &warn_std;
3543 break;
3545 case 'B':
3547 const char *value;
3548 int len;
3550 if (p[1] == 0 && i + 1 == argc)
3551 fatal ("argument to `-B' is missing");
3552 if (p[1] == 0)
3553 value = argv[++i];
3554 else
3555 value = p + 1;
3557 len = strlen (value);
3559 /* Catch the case where the user has forgotten to append a
3560 directory separator to the path. Note, they may be using
3561 -B to add an executable name prefix, eg "i386-elf-", in
3562 order to distinguish between multiple installations of
3563 GCC in the same directory. Hence we must check to see
3564 if appending a directory separator actually makes a
3565 valid directory name. */
3566 if (! IS_DIR_SEPARATOR (value [len - 1])
3567 && is_directory (value, "", 0))
3569 char *tmp = xmalloc (len + 2);
3570 strcpy (tmp, value);
3571 tmp[len] = DIR_SEPARATOR;
3572 tmp[++ len] = 0;
3573 value = tmp;
3576 /* As a kludge, if the arg is "[foo/]stageN/", just
3577 add "[foo/]include" to the include prefix. */
3578 if ((len == 7
3579 || (len > 7
3580 && (IS_DIR_SEPARATOR (value[len - 8]))))
3581 && strncmp (value + len - 7, "stage", 5) == 0
3582 && ISDIGIT (value[len - 2])
3583 && (IS_DIR_SEPARATOR (value[len - 1])))
3585 if (len == 7)
3586 add_prefix (&include_prefixes, "include", NULL,
3587 PREFIX_PRIORITY_B_OPT, 0, NULL);
3588 else
3590 char * string = xmalloc (len + 1);
3592 strncpy (string, value, len - 7);
3593 strcpy (string + len - 7, "include");
3594 add_prefix (&include_prefixes, string, NULL,
3595 PREFIX_PRIORITY_B_OPT, 0, NULL);
3599 add_prefix (&exec_prefixes, value, NULL,
3600 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3601 add_prefix (&startfile_prefixes, value, NULL,
3602 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3603 add_prefix (&include_prefixes, concat (value, "include", NULL),
3604 NULL, PREFIX_PRIORITY_B_OPT, 0, NULL);
3605 n_switches++;
3607 break;
3609 case 'v': /* Print our subcommands and print versions. */
3610 n_switches++;
3611 /* If they do anything other than exactly `-v', don't set
3612 verbose_flag; rather, continue on to give the error. */
3613 if (p[1] != 0)
3614 break;
3615 verbose_flag++;
3616 break;
3618 case 'S':
3619 case 'c':
3620 if (p[1] == 0)
3622 have_c = 1;
3623 n_switches++;
3624 break;
3626 goto normal_switch;
3628 case 'o':
3629 have_o = 1;
3630 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3631 if (! have_c)
3633 int skip;
3635 /* Forward scan, just in case -S or -c is specified
3636 after -o. */
3637 int j = i + 1;
3638 if (p[1] == 0)
3639 ++j;
3640 while (j < argc)
3642 if (argv[j][0] == '-')
3644 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3645 && argv[j][2] == 0)
3647 have_c = 1;
3648 break;
3650 else if ((skip = SWITCH_TAKES_ARG (argv[j][1])))
3651 j += skip - (argv[j][2] != 0);
3652 else if ((skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1)))
3653 j += skip;
3655 j++;
3658 #endif
3659 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3660 if (p[1] == 0)
3661 argv[i + 1] = convert_filename (argv[i + 1], ! have_c, 0);
3662 else
3663 argv[i] = convert_filename (argv[i], ! have_c, 0);
3664 #endif
3665 goto normal_switch;
3667 default:
3668 normal_switch:
3670 #ifdef MODIFY_TARGET_NAME
3671 is_modify_target_name = 0;
3673 for (j = 0; j < ARRAY_SIZE (modify_target); j++)
3674 if (! strcmp (argv[i], modify_target[j].sw))
3676 char *new_name
3677 = (char *) xmalloc (strlen (modify_target[j].str)
3678 + strlen (spec_machine));
3679 const char *p, *r;
3680 char *q;
3681 int made_addition = 0;
3683 is_modify_target_name = 1;
3684 for (p = spec_machine, q = new_name; *p != 0; )
3686 if (modify_target[j].add_del == DELETE
3687 && (! strncmp (q, modify_target[j].str,
3688 strlen (modify_target[j].str))))
3689 p += strlen (modify_target[j].str);
3690 else if (modify_target[j].add_del == ADD
3691 && ! made_addition && *p == '-')
3693 for (r = modify_target[j].str; *r != 0; )
3694 *q++ = *r++;
3695 made_addition = 1;
3698 *q++ = *p++;
3701 spec_machine = new_name;
3704 if (is_modify_target_name)
3705 break;
3706 #endif
3708 n_switches++;
3710 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3711 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3712 else if (WORD_SWITCH_TAKES_ARG (p))
3713 i += WORD_SWITCH_TAKES_ARG (p);
3716 else
3718 n_infiles++;
3719 lang_n_infiles++;
3723 if (have_c && have_o && lang_n_infiles > 1)
3724 fatal ("cannot specify -o with -c or -S and multiple compilations");
3726 /* Set up the search paths before we go looking for config files. */
3728 /* These come before the md prefixes so that we will find gcc's subcommands
3729 (such as cpp) rather than those of the host system. */
3730 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3731 as well as trying the machine and the version. */
3732 #ifndef OS2
3733 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3734 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3735 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3736 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3737 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3738 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3739 #endif
3741 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3742 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3743 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3744 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3746 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3747 dir_separator_str, NULL);
3749 /* If tooldir is relative, base it on exec_prefixes. A relative
3750 tooldir lets us move the installed tree as a unit.
3752 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3753 directories, so that we can search both the user specified directory
3754 and the standard place. */
3756 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3758 if (gcc_exec_prefix)
3760 char *gcc_exec_tooldir_prefix
3761 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3762 spec_version, dir_separator_str, tooldir_prefix, NULL);
3764 add_prefix (&exec_prefixes,
3765 concat (gcc_exec_tooldir_prefix, "bin",
3766 dir_separator_str, NULL),
3767 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3768 add_prefix (&startfile_prefixes,
3769 concat (gcc_exec_tooldir_prefix, "lib",
3770 dir_separator_str, NULL),
3771 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3774 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3775 dir_separator_str, spec_version,
3776 dir_separator_str, tooldir_prefix, NULL);
3779 add_prefix (&exec_prefixes,
3780 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
3781 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3782 add_prefix (&startfile_prefixes,
3783 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
3784 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3786 /* More prefixes are enabled in main, after we read the specs file
3787 and determine whether this is cross-compilation or not. */
3789 /* Then create the space for the vectors and scan again. */
3791 switches = ((struct switchstr *)
3792 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3793 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3794 n_switches = 0;
3795 n_infiles = 0;
3796 last_language_n_infiles = -1;
3798 /* This, time, copy the text of each switch and store a pointer
3799 to the copy in the vector of switches.
3800 Store all the infiles in their vector. */
3802 for (i = 1; i < argc; i++)
3804 /* Just skip the switches that were handled by the preceding loop. */
3805 #ifdef MODIFY_TARGET_NAME
3806 is_modify_target_name = 0;
3808 for (j = 0; j < ARRAY_SIZE (modify_target); j++)
3809 if (! strcmp (argv[i], modify_target[j].sw))
3810 is_modify_target_name = 1;
3812 if (is_modify_target_name)
3814 else
3815 #endif
3816 if (! strncmp (argv[i], "-Wa,", 4))
3818 else if (! strncmp (argv[i], "-Wp,", 4))
3820 else if (! strcmp (argv[i], "-pass-exit-codes"))
3822 else if (! strcmp (argv[i], "-print-search-dirs"))
3824 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3826 else if (! strncmp (argv[i], "-print-file-name=", 17))
3828 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3830 else if (! strcmp (argv[i], "-print-multi-lib"))
3832 else if (! strcmp (argv[i], "-print-multi-directory"))
3834 else if (! strcmp (argv[i], "-ftarget-help"))
3836 else if (! strcmp (argv[i], "-fhelp"))
3838 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3840 /* Compensate for the +e options to the C++ front-end;
3841 they're there simply for cfront call-compatibility. We do
3842 some magic in default_compilers to pass them down properly.
3843 Note we deliberately start at the `+' here, to avoid passing
3844 -e0 or -e1 down into the linker. */
3845 switches[n_switches].part1 = &argv[i][0];
3846 switches[n_switches].args = 0;
3847 switches[n_switches].live_cond = SWITCH_OK;
3848 switches[n_switches].validated = 0;
3849 n_switches++;
3851 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3853 int prev, j;
3854 /* Split the argument at commas. */
3855 prev = 4;
3856 for (j = 4; argv[i][j]; j++)
3857 if (argv[i][j] == ',')
3859 infiles[n_infiles].language = "*";
3860 infiles[n_infiles++].name
3861 = save_string (argv[i] + prev, j - prev);
3862 prev = j + 1;
3864 /* Record the part after the last comma. */
3865 infiles[n_infiles].language = "*";
3866 infiles[n_infiles++].name = argv[i] + prev;
3868 else if (strcmp (argv[i], "-Xlinker") == 0)
3870 infiles[n_infiles].language = "*";
3871 infiles[n_infiles++].name = argv[++i];
3873 else if (strcmp (argv[i], "-l") == 0)
3874 { /* POSIX allows separation of -l and the lib arg;
3875 canonicalize by concatenating -l with its arg */
3876 infiles[n_infiles].language = "*";
3877 infiles[n_infiles++].name = concat ("-l", argv[++i], NULL);
3879 else if (strncmp (argv[i], "-l", 2) == 0)
3881 infiles[n_infiles].language = "*";
3882 infiles[n_infiles++].name = argv[i];
3884 else if (strcmp (argv[i], "-specs") == 0)
3885 i++;
3886 else if (strncmp (argv[i], "-specs=", 7) == 0)
3888 else if (strcmp (argv[i], "-time") == 0)
3890 else if ((save_temps_flag || report_times)
3891 && strcmp (argv[i], "-pipe") == 0)
3893 /* -save-temps overrides -pipe, so that temp files are produced */
3894 if (save_temps_flag)
3895 error ("warning: -pipe ignored because -save-temps specified");
3896 /* -time overrides -pipe because we can't get correct stats when
3897 multiple children are running at once. */
3898 else if (report_times)
3899 error ("warning: -pipe ignored because -time specified");
3901 else if (strcmp (argv[i], "-###") == 0)
3903 else if (argv[i][0] == '-' && argv[i][1] != 0)
3905 const char *p = &argv[i][1];
3906 int c = *p;
3908 if (c == 'x')
3910 if (p[1] == 0 && i + 1 == argc)
3911 fatal ("argument to `-x' is missing");
3912 if (p[1] == 0)
3913 spec_lang = argv[++i];
3914 else
3915 spec_lang = p + 1;
3916 if (! strcmp (spec_lang, "none"))
3917 /* Suppress the warning if -xnone comes after the last input
3918 file, because alternate command interfaces like g++ might
3919 find it useful to place -xnone after each input file. */
3920 spec_lang = 0;
3921 else
3922 last_language_n_infiles = n_infiles;
3923 continue;
3925 switches[n_switches].part1 = p;
3926 /* Deal with option arguments in separate argv elements. */
3927 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3928 || WORD_SWITCH_TAKES_ARG (p))
3930 int j = 0;
3931 int n_args = WORD_SWITCH_TAKES_ARG (p);
3933 if (n_args == 0)
3935 /* Count only the option arguments in separate argv elements. */
3936 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3938 if (i + n_args >= argc)
3939 fatal ("argument to `-%s' is missing", p);
3940 switches[n_switches].args
3941 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3942 while (j < n_args)
3943 switches[n_switches].args[j++] = argv[++i];
3944 /* Null-terminate the vector. */
3945 switches[n_switches].args[j] = 0;
3947 else if (strchr (switches_need_spaces, c))
3949 /* On some systems, ld cannot handle some options without
3950 a space. So split the option from its argument. */
3951 char *part1 = (char *) xmalloc (2);
3952 part1[0] = c;
3953 part1[1] = '\0';
3955 switches[n_switches].part1 = part1;
3956 switches[n_switches].args
3957 = (const char **) xmalloc (2 * sizeof (const char *));
3958 switches[n_switches].args[0] = xstrdup (p+1);
3959 switches[n_switches].args[1] = 0;
3961 else
3962 switches[n_switches].args = 0;
3964 switches[n_switches].live_cond = SWITCH_OK;
3965 switches[n_switches].validated = 0;
3966 switches[n_switches].ordering = 0;
3967 /* These are always valid, since gcc.c itself understands it. */
3968 if (!strcmp (p, "save-temps")
3969 || !strcmp (p, "static-libgcc")
3970 || !strcmp (p, "shared-libgcc"))
3971 switches[n_switches].validated = 1;
3972 else
3974 char ch = switches[n_switches].part1[0];
3975 if (ch == 'b' || ch == 'B')
3976 switches[n_switches].validated = 1;
3978 n_switches++;
3980 else
3982 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3983 argv[i] = convert_filename (argv[i], 0, access (argv[i], F_OK));
3984 #endif
3986 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
3988 perror_with_name (argv[i]);
3989 error_count++;
3991 else
3993 infiles[n_infiles].language = spec_lang;
3994 infiles[n_infiles++].name = argv[i];
3999 if (n_infiles == last_language_n_infiles && spec_lang != 0)
4000 error ("warning: `-x %s' after last input file has no effect", spec_lang);
4002 /* Ensure we only invoke each subprocess once. */
4003 if (target_help_flag || print_help_list)
4005 n_infiles = 1;
4007 /* Create a dummy input file, so that we can pass --target-help on to
4008 the various sub-processes. */
4009 infiles[0].language = "c";
4010 infiles[0].name = "help-dummy";
4012 if (target_help_flag)
4014 switches[n_switches].part1 = "--target-help";
4015 switches[n_switches].args = 0;
4016 switches[n_switches].live_cond = SWITCH_OK;
4017 switches[n_switches].validated = 0;
4019 n_switches++;
4022 if (print_help_list)
4024 switches[n_switches].part1 = "--help";
4025 switches[n_switches].args = 0;
4026 switches[n_switches].live_cond = SWITCH_OK;
4027 switches[n_switches].validated = 0;
4029 n_switches++;
4033 switches[n_switches].part1 = 0;
4034 infiles[n_infiles].name = 0;
4037 /* Store switches not filtered out by %{<S} in spec in COLLECT_GCC_OPTIONS
4038 and place that in the environment. */
4040 static void
4041 set_collect_gcc_options ()
4043 int i;
4044 int first_time;
4046 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4047 the compiler. */
4048 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4049 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
4051 first_time = TRUE;
4052 for (i = 0; (int) i < n_switches; i++)
4054 const char *const *args;
4055 const char *p, *q;
4056 if (!first_time)
4057 obstack_grow (&collect_obstack, " ", 1);
4059 first_time = FALSE;
4061 /* Ignore elided switches. */
4062 if (switches[i].live_cond == SWITCH_IGNORE)
4063 continue;
4065 obstack_grow (&collect_obstack, "'-", 2);
4066 q = switches[i].part1;
4067 while ((p = strchr (q, '\'')))
4069 obstack_grow (&collect_obstack, q, p - q);
4070 obstack_grow (&collect_obstack, "'\\''", 4);
4071 q = ++p;
4073 obstack_grow (&collect_obstack, q, strlen (q));
4074 obstack_grow (&collect_obstack, "'", 1);
4076 for (args = switches[i].args; args && *args; args++)
4078 obstack_grow (&collect_obstack, " '", 2);
4079 q = *args;
4080 while ((p = strchr (q, '\'')))
4082 obstack_grow (&collect_obstack, q, p - q);
4083 obstack_grow (&collect_obstack, "'\\''", 4);
4084 q = ++p;
4086 obstack_grow (&collect_obstack, q, strlen (q));
4087 obstack_grow (&collect_obstack, "'", 1);
4090 obstack_grow (&collect_obstack, "\0", 1);
4091 putenv (obstack_finish (&collect_obstack));
4094 /* Process a spec string, accumulating and running commands. */
4096 /* These variables describe the input file name.
4097 input_file_number is the index on outfiles of this file,
4098 so that the output file name can be stored for later use by %o.
4099 input_basename is the start of the part of the input file
4100 sans all directory names, and basename_length is the number
4101 of characters starting there excluding the suffix .c or whatever. */
4103 const char *input_filename;
4104 static int input_file_number;
4105 size_t input_filename_length;
4106 static int basename_length;
4107 static int suffixed_basename_length;
4108 static const char *input_basename;
4109 static const char *input_suffix;
4110 static struct stat input_stat;
4111 static int input_stat_set;
4113 /* The compiler used to process the current input file. */
4114 static struct compiler *input_file_compiler;
4116 /* These are variables used within do_spec and do_spec_1. */
4118 /* Nonzero if an arg has been started and not yet terminated
4119 (with space, tab or newline). */
4120 static int arg_going;
4122 /* Nonzero means %d or %g has been seen; the next arg to be terminated
4123 is a temporary file name. */
4124 static int delete_this_arg;
4126 /* Nonzero means %w has been seen; the next arg to be terminated
4127 is the output file name of this compilation. */
4128 static int this_is_output_file;
4130 /* Nonzero means %s has been seen; the next arg to be terminated
4131 is the name of a library file and we should try the standard
4132 search dirs for it. */
4133 static int this_is_library_file;
4135 /* Nonzero means that the input of this command is coming from a pipe. */
4136 static int input_from_pipe;
4138 /* Nonnull means substitute this for any suffix when outputting a switches
4139 arguments. */
4140 static const char *suffix_subst;
4142 /* Process the spec SPEC and run the commands specified therein.
4143 Returns 0 if the spec is successfully processed; -1 if failed. */
4146 do_spec (spec)
4147 const char *spec;
4149 int value;
4151 value = do_spec_2 (spec);
4153 /* Force out any unfinished command.
4154 If -pipe, this forces out the last command if it ended in `|'. */
4155 if (value == 0)
4157 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4158 argbuf_index--;
4160 set_collect_gcc_options ();
4162 if (argbuf_index > 0)
4163 value = execute ();
4166 return value;
4169 static int
4170 do_spec_2 (spec)
4171 const char *spec;
4173 clear_args ();
4174 arg_going = 0;
4175 delete_this_arg = 0;
4176 this_is_output_file = 0;
4177 this_is_library_file = 0;
4178 input_from_pipe = 0;
4179 suffix_subst = NULL;
4181 return do_spec_1 (spec, 0, NULL);
4184 /* Process the sub-spec SPEC as a portion of a larger spec.
4185 This is like processing a whole spec except that we do
4186 not initialize at the beginning and we do not supply a
4187 newline by default at the end.
4188 INSWITCH nonzero means don't process %-sequences in SPEC;
4189 in this case, % is treated as an ordinary character.
4190 This is used while substituting switches.
4191 INSWITCH nonzero also causes SPC not to terminate an argument.
4193 Value is zero unless a line was finished
4194 and the command on that line reported an error. */
4196 static int
4197 do_spec_1 (spec, inswitch, soft_matched_part)
4198 const char *spec;
4199 int inswitch;
4200 const char *soft_matched_part;
4202 const char *p = spec;
4203 int c;
4204 int i;
4205 const char *string;
4206 int value;
4208 while ((c = *p++))
4209 /* If substituting a switch, treat all chars like letters.
4210 Otherwise, NL, SPC, TAB and % are special. */
4211 switch (inswitch ? 'a' : c)
4213 case '\n':
4214 /* End of line: finish any pending argument,
4215 then run the pending command if one has been started. */
4216 if (arg_going)
4218 obstack_1grow (&obstack, 0);
4219 string = obstack_finish (&obstack);
4220 if (this_is_library_file)
4221 string = find_file (string);
4222 store_arg (string, delete_this_arg, this_is_output_file);
4223 if (this_is_output_file)
4224 outfiles[input_file_number] = string;
4226 arg_going = 0;
4228 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4230 for (i = 0; i < n_switches; i++)
4231 if (!strcmp (switches[i].part1, "pipe"))
4232 break;
4234 /* A `|' before the newline means use a pipe here,
4235 but only if -pipe was specified.
4236 Otherwise, execute now and don't pass the `|' as an arg. */
4237 if (i < n_switches)
4239 input_from_pipe = 1;
4240 switches[i].validated = 1;
4241 break;
4243 else
4244 argbuf_index--;
4247 set_collect_gcc_options ();
4249 if (argbuf_index > 0)
4251 value = execute ();
4252 if (value)
4253 return value;
4255 /* Reinitialize for a new command, and for a new argument. */
4256 clear_args ();
4257 arg_going = 0;
4258 delete_this_arg = 0;
4259 this_is_output_file = 0;
4260 this_is_library_file = 0;
4261 input_from_pipe = 0;
4262 break;
4264 case '|':
4265 /* End any pending argument. */
4266 if (arg_going)
4268 obstack_1grow (&obstack, 0);
4269 string = obstack_finish (&obstack);
4270 if (this_is_library_file)
4271 string = find_file (string);
4272 store_arg (string, delete_this_arg, this_is_output_file);
4273 if (this_is_output_file)
4274 outfiles[input_file_number] = string;
4277 /* Use pipe */
4278 obstack_1grow (&obstack, c);
4279 arg_going = 1;
4280 break;
4282 case '\t':
4283 case ' ':
4284 /* Space or tab ends an argument if one is pending. */
4285 if (arg_going)
4287 obstack_1grow (&obstack, 0);
4288 string = obstack_finish (&obstack);
4289 if (this_is_library_file)
4290 string = find_file (string);
4291 store_arg (string, delete_this_arg, this_is_output_file);
4292 if (this_is_output_file)
4293 outfiles[input_file_number] = string;
4295 /* Reinitialize for a new argument. */
4296 arg_going = 0;
4297 delete_this_arg = 0;
4298 this_is_output_file = 0;
4299 this_is_library_file = 0;
4300 break;
4302 case '%':
4303 switch (c = *p++)
4305 case 0:
4306 fatal ("invalid specification! Bug in cc");
4308 case 'b':
4309 obstack_grow (&obstack, input_basename, basename_length);
4310 arg_going = 1;
4311 break;
4313 case 'B':
4314 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4315 arg_going = 1;
4316 break;
4318 case 'd':
4319 delete_this_arg = 2;
4320 break;
4322 /* Dump out the directories specified with LIBRARY_PATH,
4323 followed by the absolute directories
4324 that we search for startfiles. */
4325 case 'D':
4327 struct prefix_list *pl = startfile_prefixes.plist;
4328 size_t bufsize = 100;
4329 char *buffer = (char *) xmalloc (bufsize);
4330 int idx;
4332 for (; pl; pl = pl->next)
4334 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4335 /* Used on systems which record the specified -L dirs
4336 and use them to search for dynamic linking. */
4337 /* Relative directories always come from -B,
4338 and it is better not to use them for searching
4339 at run time. In particular, stage1 loses. */
4340 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4341 continue;
4342 #endif
4343 /* Try subdirectory if there is one. */
4344 if (multilib_dir != NULL)
4346 if (machine_suffix)
4348 if (strlen (pl->prefix) + strlen (machine_suffix)
4349 >= bufsize)
4350 bufsize = (strlen (pl->prefix)
4351 + strlen (machine_suffix)) * 2 + 1;
4352 buffer = (char *) xrealloc (buffer, bufsize);
4353 strcpy (buffer, pl->prefix);
4354 strcat (buffer, machine_suffix);
4355 if (is_directory (buffer, multilib_dir, 1))
4357 do_spec_1 ("-L", 0, NULL);
4358 #ifdef SPACE_AFTER_L_OPTION
4359 do_spec_1 (" ", 0, NULL);
4360 #endif
4361 do_spec_1 (buffer, 1, NULL);
4362 do_spec_1 (multilib_dir, 1, NULL);
4363 /* Make this a separate argument. */
4364 do_spec_1 (" ", 0, NULL);
4367 if (!pl->require_machine_suffix)
4369 if (is_directory (pl->prefix, multilib_dir, 1))
4371 do_spec_1 ("-L", 0, NULL);
4372 #ifdef SPACE_AFTER_L_OPTION
4373 do_spec_1 (" ", 0, NULL);
4374 #endif
4375 do_spec_1 (pl->prefix, 1, NULL);
4376 do_spec_1 (multilib_dir, 1, NULL);
4377 /* Make this a separate argument. */
4378 do_spec_1 (" ", 0, NULL);
4382 if (machine_suffix)
4384 if (is_directory (pl->prefix, machine_suffix, 1))
4386 do_spec_1 ("-L", 0, NULL);
4387 #ifdef SPACE_AFTER_L_OPTION
4388 do_spec_1 (" ", 0, NULL);
4389 #endif
4390 do_spec_1 (pl->prefix, 1, NULL);
4391 /* Remove slash from machine_suffix. */
4392 if (strlen (machine_suffix) >= bufsize)
4393 bufsize = strlen (machine_suffix) * 2 + 1;
4394 buffer = (char *) xrealloc (buffer, bufsize);
4395 strcpy (buffer, machine_suffix);
4396 idx = strlen (buffer);
4397 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4398 buffer[idx - 1] = 0;
4399 do_spec_1 (buffer, 1, NULL);
4400 /* Make this a separate argument. */
4401 do_spec_1 (" ", 0, NULL);
4404 if (!pl->require_machine_suffix)
4406 if (is_directory (pl->prefix, "", 1))
4408 do_spec_1 ("-L", 0, NULL);
4409 #ifdef SPACE_AFTER_L_OPTION
4410 do_spec_1 (" ", 0, NULL);
4411 #endif
4412 /* Remove slash from pl->prefix. */
4413 if (strlen (pl->prefix) >= bufsize)
4414 bufsize = strlen (pl->prefix) * 2 + 1;
4415 buffer = (char *) xrealloc (buffer, bufsize);
4416 strcpy (buffer, pl->prefix);
4417 idx = strlen (buffer);
4418 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4419 buffer[idx - 1] = 0;
4420 do_spec_1 (buffer, 1, NULL);
4421 /* Make this a separate argument. */
4422 do_spec_1 (" ", 0, NULL);
4426 free (buffer);
4428 break;
4430 case 'e':
4431 /* %efoo means report an error with `foo' as error message
4432 and don't execute any more commands for this file. */
4434 const char *q = p;
4435 char *buf;
4436 while (*p != 0 && *p != '\n')
4437 p++;
4438 buf = (char *) alloca (p - q + 1);
4439 strncpy (buf, q, p - q);
4440 buf[p - q] = 0;
4441 error ("%s", buf);
4442 return -1;
4444 break;
4445 case 'n':
4446 /* %nfoo means report an notice with `foo' on stderr. */
4448 const char *q = p;
4449 char *buf;
4450 while (*p != 0 && *p != '\n')
4451 p++;
4452 buf = (char *) alloca (p - q + 1);
4453 strncpy (buf, q, p - q);
4454 buf[p - q] = 0;
4455 notice ("%s\n", buf);
4456 if (*p)
4457 p++;
4459 break;
4461 case 'j':
4463 struct stat st;
4465 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4466 and it is not a directory, and it is writable, use it.
4467 Otherwise, fall through and treat this like any other
4468 temporary file. */
4470 if ((!save_temps_flag)
4471 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4472 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4474 obstack_grow (&obstack, HOST_BIT_BUCKET,
4475 strlen (HOST_BIT_BUCKET));
4476 delete_this_arg = 0;
4477 arg_going = 1;
4478 break;
4481 case 'g':
4482 case 'u':
4483 case 'U':
4485 struct temp_name *t;
4486 int suffix_length;
4487 const char *suffix = p;
4488 char *saved_suffix = NULL;
4490 while (*p == '.' || ISALPHA ((unsigned char) *p))
4491 p++;
4492 suffix_length = p - suffix;
4493 if (p[0] == '%' && p[1] == 'O')
4495 p += 2;
4496 /* We don't support extra suffix characters after %O. */
4497 if (*p == '.' || ISALPHA ((unsigned char) *p))
4498 abort ();
4499 if (suffix_length == 0)
4500 suffix = TARGET_OBJECT_SUFFIX;
4501 else
4503 saved_suffix
4504 = (char *) xmalloc (suffix_length
4505 + strlen (TARGET_OBJECT_SUFFIX));
4506 strncpy (saved_suffix, suffix, suffix_length);
4507 strcpy (saved_suffix + suffix_length,
4508 TARGET_OBJECT_SUFFIX);
4510 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
4513 /* If the input_filename has the same suffix specified
4514 for the %g, %u, or %U, and -save-temps is specified,
4515 we could end up using that file as an intermediate
4516 thus clobbering the user's source file (.e.g.,
4517 gcc -save-temps foo.s would clobber foo.s with the
4518 output of cpp0). So check for this condition and
4519 generate a temp file as the intermediate. */
4521 if (save_temps_flag)
4523 temp_filename_length = basename_length + suffix_length;
4524 temp_filename = alloca (temp_filename_length + 1);
4525 strncpy ((char *) temp_filename, input_basename, basename_length);
4526 strncpy ((char *) temp_filename + basename_length, suffix,
4527 suffix_length);
4528 *((char *) temp_filename + temp_filename_length) = '\0';
4529 if (strcmp (temp_filename, input_filename) != 0)
4531 struct stat st_temp;
4533 /* Note, set_input() resets input_stat_set to 0. */
4534 if (input_stat_set == 0)
4536 input_stat_set = stat (input_filename, &input_stat);
4537 if (input_stat_set >= 0)
4538 input_stat_set = 1;
4541 /* If we have the stat for the input_filename
4542 and we can do the stat for the temp_filename
4543 then the they could still refer to the same
4544 file if st_dev/st_ino's are the same. */
4546 if (input_stat_set != 1
4547 || stat (temp_filename, &st_temp) < 0
4548 || input_stat.st_dev != st_temp.st_dev
4549 || input_stat.st_ino != st_temp.st_ino)
4551 temp_filename = save_string (temp_filename,
4552 temp_filename_length + 1);
4553 obstack_grow (&obstack, temp_filename,
4554 temp_filename_length);
4555 arg_going = 1;
4556 break;
4561 /* See if we already have an association of %g/%u/%U and
4562 suffix. */
4563 for (t = temp_names; t; t = t->next)
4564 if (t->length == suffix_length
4565 && strncmp (t->suffix, suffix, suffix_length) == 0
4566 && t->unique == (c != 'g'))
4567 break;
4569 /* Make a new association if needed. %u and %j
4570 require one. */
4571 if (t == 0 || c == 'u' || c == 'j')
4573 if (t == 0)
4575 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4576 t->next = temp_names;
4577 temp_names = t;
4579 t->length = suffix_length;
4580 if (saved_suffix)
4582 t->suffix = saved_suffix;
4583 saved_suffix = NULL;
4585 else
4586 t->suffix = save_string (suffix, suffix_length);
4587 t->unique = (c != 'g');
4588 temp_filename = make_temp_file (t->suffix);
4589 temp_filename_length = strlen (temp_filename);
4590 t->filename = temp_filename;
4591 t->filename_length = temp_filename_length;
4594 if (saved_suffix)
4595 free (saved_suffix);
4597 obstack_grow (&obstack, t->filename, t->filename_length);
4598 delete_this_arg = 1;
4600 arg_going = 1;
4601 break;
4603 case 'i':
4604 obstack_grow (&obstack, input_filename, input_filename_length);
4605 arg_going = 1;
4606 break;
4608 case 'I':
4610 struct prefix_list *pl = include_prefixes.plist;
4612 if (gcc_exec_prefix)
4614 do_spec_1 ("-iprefix", 1, NULL);
4615 /* Make this a separate argument. */
4616 do_spec_1 (" ", 0, NULL);
4617 do_spec_1 (gcc_exec_prefix, 1, NULL);
4618 do_spec_1 (" ", 0, NULL);
4621 for (; pl; pl = pl->next)
4623 do_spec_1 ("-isystem", 1, NULL);
4624 /* Make this a separate argument. */
4625 do_spec_1 (" ", 0, NULL);
4626 do_spec_1 (pl->prefix, 1, NULL);
4627 do_spec_1 (" ", 0, NULL);
4630 break;
4632 case 'o':
4634 int max = n_infiles;
4635 max += lang_specific_extra_outfiles;
4637 for (i = 0; i < max; i++)
4638 if (outfiles[i])
4639 store_arg (outfiles[i], 0, 0);
4640 break;
4643 case 'O':
4644 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
4645 arg_going = 1;
4646 break;
4648 case 's':
4649 this_is_library_file = 1;
4650 break;
4652 case 'w':
4653 this_is_output_file = 1;
4654 break;
4656 case 'W':
4658 int cur_index = argbuf_index;
4659 /* Handle the {...} following the %W. */
4660 if (*p != '{')
4661 abort ();
4662 p = handle_braces (p + 1);
4663 if (p == 0)
4664 return -1;
4665 /* If any args were output, mark the last one for deletion
4666 on failure. */
4667 if (argbuf_index != cur_index)
4668 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4669 break;
4672 /* %x{OPTION} records OPTION for %X to output. */
4673 case 'x':
4675 const char *p1 = p;
4676 char *string;
4678 /* Skip past the option value and make a copy. */
4679 if (*p != '{')
4680 abort ();
4681 while (*p++ != '}')
4683 string = save_string (p1 + 1, p - p1 - 2);
4685 /* See if we already recorded this option. */
4686 for (i = 0; i < n_linker_options; i++)
4687 if (! strcmp (string, linker_options[i]))
4689 free (string);
4690 return 0;
4693 /* This option is new; add it. */
4694 add_linker_option (string, strlen (string));
4696 break;
4698 /* Dump out the options accumulated previously using %x. */
4699 case 'X':
4700 for (i = 0; i < n_linker_options; i++)
4702 do_spec_1 (linker_options[i], 1, NULL);
4703 /* Make each accumulated option a separate argument. */
4704 do_spec_1 (" ", 0, NULL);
4706 break;
4708 /* Dump out the options accumulated previously using -Wa,. */
4709 case 'Y':
4710 for (i = 0; i < n_assembler_options; i++)
4712 do_spec_1 (assembler_options[i], 1, NULL);
4713 /* Make each accumulated option a separate argument. */
4714 do_spec_1 (" ", 0, NULL);
4716 break;
4718 /* Dump out the options accumulated previously using -Wp,. */
4719 case 'Z':
4720 for (i = 0; i < n_preprocessor_options; i++)
4722 do_spec_1 (preprocessor_options[i], 1, NULL);
4723 /* Make each accumulated option a separate argument. */
4724 do_spec_1 (" ", 0, NULL);
4726 break;
4728 /* Here are digits and numbers that just process
4729 a certain constant string as a spec. */
4731 case '1':
4732 value = do_spec_1 (cc1_spec, 0, NULL);
4733 if (value != 0)
4734 return value;
4735 break;
4737 case '2':
4738 value = do_spec_1 (cc1plus_spec, 0, NULL);
4739 if (value != 0)
4740 return value;
4741 break;
4743 case 'a':
4744 value = do_spec_1 (asm_spec, 0, NULL);
4745 if (value != 0)
4746 return value;
4747 break;
4749 case 'A':
4750 value = do_spec_1 (asm_final_spec, 0, NULL);
4751 if (value != 0)
4752 return value;
4753 break;
4755 case 'C':
4757 const char *const spec
4758 = (input_file_compiler->cpp_spec
4759 ? input_file_compiler->cpp_spec
4760 : cpp_spec);
4761 value = do_spec_1 (spec, 0, NULL);
4762 if (value != 0)
4763 return value;
4765 break;
4767 case 'E':
4768 value = do_spec_1 (endfile_spec, 0, NULL);
4769 if (value != 0)
4770 return value;
4771 break;
4773 case 'l':
4774 value = do_spec_1 (link_spec, 0, NULL);
4775 if (value != 0)
4776 return value;
4777 break;
4779 case 'L':
4780 value = do_spec_1 (lib_spec, 0, NULL);
4781 if (value != 0)
4782 return value;
4783 break;
4785 case 'G':
4786 value = do_spec_1 (libgcc_spec, 0, NULL);
4787 if (value != 0)
4788 return value;
4789 break;
4791 case 'M':
4792 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4794 char *p;
4795 const char *q;
4796 size_t len;
4798 len = strlen (multilib_dir);
4799 obstack_blank (&obstack, len + 1);
4800 p = obstack_next_free (&obstack) - (len + 1);
4802 *p++ = '_';
4803 for (q = multilib_dir; *q ; ++q, ++p)
4804 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4806 break;
4808 case 'p':
4810 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4811 char *buf = x;
4812 const char *y;
4814 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4815 y = cpp_predefines;
4816 while (*y != 0)
4818 if (! strncmp (y, "-D", 2))
4819 /* Copy the whole option. */
4820 while (*y && *y != ' ' && *y != '\t')
4821 *x++ = *y++;
4822 else if (*y == ' ' || *y == '\t')
4823 /* Copy whitespace to the result. */
4824 *x++ = *y++;
4825 /* Don't copy other options. */
4826 else
4827 y++;
4830 *x = 0;
4832 value = do_spec_1 (buf, 0, NULL);
4833 if (value != 0)
4834 return value;
4836 break;
4838 case 'P':
4840 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4841 char *buf = x;
4842 const char *y;
4844 /* Copy all of CPP_PREDEFINES into BUF,
4845 but force them all into the reserved name space if they
4846 aren't already there. The reserved name space is all
4847 identifiers beginning with two underscores or with one
4848 underscore and a capital letter. We do the forcing by
4849 adding up to two underscores to the beginning and end
4850 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4851 become __mips__. */
4852 y = cpp_predefines;
4853 while (*y != 0)
4855 if (! strncmp (y, "-D", 2))
4857 int flag = 0;
4859 *x++ = *y++;
4860 *x++ = *y++;
4862 if (*y != '_'
4863 || (*(y + 1) != '_'
4864 && ! ISUPPER ((unsigned char) *(y + 1))))
4866 /* Stick __ at front of macro name. */
4867 if (*y != '_')
4868 *x++ = '_';
4869 *x++ = '_';
4870 /* Arrange to stick __ at the end as well. */
4871 flag = 1;
4874 /* Copy the macro name. */
4875 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4876 *x++ = *y++;
4878 if (flag)
4880 if (x[-1] != '_')
4882 if (x[-2] != '_')
4883 *x++ = '_';
4884 *x++ = '_';
4888 /* Copy the value given, if any. */
4889 while (*y && *y != ' ' && *y != '\t')
4890 *x++ = *y++;
4892 else if (*y == ' ' || *y == '\t')
4893 /* Copy whitespace to the result. */
4894 *x++ = *y++;
4895 /* Don't copy -A options */
4896 else
4897 y++;
4899 *x++ = ' ';
4901 /* Copy all of CPP_PREDEFINES into BUF,
4902 but put __ after every -D. */
4903 y = cpp_predefines;
4904 while (*y != 0)
4906 if (! strncmp (y, "-D", 2))
4908 y += 2;
4910 if (*y != '_'
4911 || (*(y + 1) != '_'
4912 && ! ISUPPER ((unsigned char) *(y + 1))))
4914 /* Stick -D__ at front of macro name. */
4915 *x++ = '-';
4916 *x++ = 'D';
4917 if (*y != '_')
4918 *x++ = '_';
4919 *x++ = '_';
4921 /* Copy the macro name. */
4922 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4923 *x++ = *y++;
4925 /* Copy the value given, if any. */
4926 while (*y && *y != ' ' && *y != '\t')
4927 *x++ = *y++;
4929 else
4931 /* Do not copy this macro - we have just done it before */
4932 while (*y && *y != ' ' && *y != '\t')
4933 y++;
4936 else if (*y == ' ' || *y == '\t')
4937 /* Copy whitespace to the result. */
4938 *x++ = *y++;
4939 /* Don't copy -A options. */
4940 else
4941 y++;
4943 *x++ = ' ';
4945 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4946 y = cpp_predefines;
4947 while (*y != 0)
4949 if (! strncmp (y, "-A", 2))
4950 /* Copy the whole option. */
4951 while (*y && *y != ' ' && *y != '\t')
4952 *x++ = *y++;
4953 else if (*y == ' ' || *y == '\t')
4954 /* Copy whitespace to the result. */
4955 *x++ = *y++;
4956 /* Don't copy other options. */
4957 else
4958 y++;
4961 *x = 0;
4963 value = do_spec_1 (buf, 0, NULL);
4964 if (value != 0)
4965 return value;
4967 break;
4969 case 'S':
4970 value = do_spec_1 (startfile_spec, 0, NULL);
4971 if (value != 0)
4972 return value;
4973 break;
4975 /* Here we define characters other than letters and digits. */
4977 case '{':
4978 p = handle_braces (p);
4979 if (p == 0)
4980 return -1;
4981 break;
4983 case '%':
4984 obstack_1grow (&obstack, '%');
4985 break;
4987 case '.':
4989 unsigned len = 0;
4991 while (p[len] && p[len] != ' ' && p[len] != '%')
4992 len++;
4993 suffix_subst = save_string (p - 1, len + 1);
4994 p += len;
4996 break;
4998 case '*':
4999 if (soft_matched_part)
5001 do_spec_1 (soft_matched_part, 1, NULL);
5002 do_spec_1 (" ", 0, NULL);
5004 else
5005 /* Catch the case where a spec string contains something like
5006 '%{foo:%*}'. ie there is no * in the pattern on the left
5007 hand side of the :. */
5008 error ("spec failure: '%%*' has not been initialized by pattern match");
5009 break;
5011 /* Process a string found as the value of a spec given by name.
5012 This feature allows individual machine descriptions
5013 to add and use their own specs.
5014 %[...] modifies -D options the way %P does;
5015 %(...) uses the spec unmodified. */
5016 case '[':
5017 error ("warning: use of obsolete %%[ operator in specs");
5018 case '(':
5020 const char *name = p;
5021 struct spec_list *sl;
5022 int len;
5024 /* The string after the S/P is the name of a spec that is to be
5025 processed. */
5026 while (*p && *p != ')' && *p != ']')
5027 p++;
5029 /* See if it's in the list. */
5030 for (len = p - name, sl = specs; sl; sl = sl->next)
5031 if (sl->name_len == len && !strncmp (sl->name, name, len))
5033 name = *(sl->ptr_spec);
5034 #ifdef DEBUG_SPECS
5035 notice ("Processing spec %c%s%c, which is '%s'\n",
5036 c, sl->name, (c == '(') ? ')' : ']', name);
5037 #endif
5038 break;
5041 if (sl)
5043 if (c == '(')
5045 value = do_spec_1 (name, 0, NULL);
5046 if (value != 0)
5047 return value;
5049 else
5051 char *x = (char *) alloca (strlen (name) * 2 + 1);
5052 char *buf = x;
5053 const char *y = name;
5054 int flag = 0;
5056 /* Copy all of NAME into BUF, but put __ after
5057 every -D and at the end of each arg. */
5058 while (1)
5060 if (! strncmp (y, "-D", 2))
5062 *x++ = '-';
5063 *x++ = 'D';
5064 *x++ = '_';
5065 *x++ = '_';
5066 y += 2;
5067 flag = 1;
5068 continue;
5070 else if (flag
5071 && (*y == ' ' || *y == '\t' || *y == '='
5072 || *y == '}' || *y == 0))
5074 *x++ = '_';
5075 *x++ = '_';
5076 flag = 0;
5078 if (*y == 0)
5079 break;
5080 else
5081 *x++ = *y++;
5083 *x = 0;
5085 value = do_spec_1 (buf, 0, NULL);
5086 if (value != 0)
5087 return value;
5091 /* Discard the closing paren or bracket. */
5092 if (*p)
5093 p++;
5095 break;
5097 case 'v':
5099 int c1 = *p++; /* Select first or second version number. */
5100 const char *v = compiler_version;
5101 const char *q;
5102 static const char zeroc = '0';
5104 /* The format of the version string is
5105 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
5107 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
5108 while (! ISDIGIT (*v))
5109 v++;
5110 if (v > compiler_version && v[-1] != '-')
5111 abort ();
5113 /* If desired, advance to second version number. */
5114 if (c1 >= '2')
5116 /* Set V after the first period. */
5117 while (ISDIGIT (*v))
5118 v++;
5119 if (*v != '.')
5120 abort ();
5121 v++;
5124 /* If desired, advance to third version number.
5125 But don't complain if it's not present */
5126 if (c1 == '3')
5128 /* Set V after the second period. */
5129 while (ISDIGIT (*v))
5130 v++;
5131 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
5132 abort ();
5133 if (*v != 0)
5134 v++;
5137 /* Set Q at the next period or at the end. */
5138 q = v;
5139 while (ISDIGIT (*q))
5140 q++;
5141 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
5142 abort ();
5144 if (q > v)
5145 /* Put that part into the command. */
5146 obstack_grow (&obstack, v, q - v);
5147 else
5148 /* Default to "0" */
5149 obstack_grow (&obstack, &zeroc, 1);
5150 arg_going = 1;
5152 break;
5154 case '|':
5155 if (input_from_pipe)
5156 do_spec_1 ("-", 0, NULL);
5157 break;
5159 default:
5160 error ("spec failure: unrecognized spec option '%c'", c);
5161 break;
5163 break;
5165 case '\\':
5166 /* Backslash: treat next character as ordinary. */
5167 c = *p++;
5169 /* fall through */
5170 default:
5171 /* Ordinary character: put it into the current argument. */
5172 obstack_1grow (&obstack, c);
5173 arg_going = 1;
5176 /* End of string. */
5177 return 0;
5180 /* Return 0 if we call do_spec_1 and that returns -1. */
5182 static const char *
5183 handle_braces (p)
5184 const char *p;
5186 const char *filter, *body = NULL, *endbody = NULL;
5187 int pipe_p = 0;
5188 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
5189 int negate;
5190 int suffix;
5191 int include_blanks = 1;
5192 int elide_switch = 0;
5193 int ordered = 0;
5195 if (*p == '^')
5197 /* A '^' after the open-brace means to not give blanks before args. */
5198 include_blanks = 0;
5199 ++p;
5202 if (*p == '|')
5204 /* A `|' after the open-brace means,
5205 if the test fails, output a single minus sign rather than nothing.
5206 This is used in %{|!pipe:...}. */
5207 pipe_p = 1;
5208 ++p;
5211 if (*p == '<')
5213 /* A `<' after the open-brace means that the switch should be
5214 removed from the command-line. */
5215 elide_switch = 1;
5216 ++p;
5219 next_member:
5220 negate = suffix = 0;
5222 if (*p == '!')
5223 /* A `!' after the open-brace negates the condition:
5224 succeed if the specified switch is not present. */
5225 negate = 1, ++p;
5227 if (*p == '.')
5228 /* A `.' after the open-brace means test against the current suffix. */
5230 if (pipe_p)
5231 abort ();
5233 suffix = 1;
5234 ++p;
5237 if (elide_switch && (negate || pipe_p || suffix))
5239 /* It doesn't make sense to mix elision with other flags. We
5240 could fatal() here, but the standard seems to be to abort. */
5241 abort ();
5244 next_ampersand:
5245 filter = p;
5246 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
5247 p++;
5249 if (*p == '|' && (pipe_p || ordered))
5250 abort ();
5252 if (!body)
5254 if (*p != '}' && *p != '&')
5256 int count = 1;
5257 const char *q = p;
5259 while (*q++ != ':')
5260 continue;
5261 body = q;
5263 while (count > 0)
5265 if (*q == '{')
5266 count++;
5267 else if (*q == '}')
5268 count--;
5269 else if (*q == 0)
5270 fatal ("mismatched braces in specs");
5271 q++;
5273 endbody = q;
5275 else
5276 body = p, endbody = p + 1;
5279 if (suffix)
5281 int found = (input_suffix != 0
5282 && (long) strlen (input_suffix) == (long) (p - filter)
5283 && strncmp (input_suffix, filter, p - filter) == 0);
5285 if (body[0] == '}')
5286 abort ();
5288 if (negate != found
5289 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL) < 0)
5290 return 0;
5292 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5294 /* Substitute all matching switches as separate args. */
5295 int i;
5297 for (i = 0; i < n_switches; i++)
5298 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5299 && check_live_switch (i, p - 1 - filter))
5301 if (elide_switch)
5303 switches[i].live_cond = SWITCH_IGNORE;
5304 switches[i].validated = 1;
5306 else
5307 ordered = 1, switches[i].ordering = 1;
5310 else
5312 /* Test for presence of the specified switch. */
5313 int i;
5314 int present = 0;
5316 /* If name specified ends in *, as in {x*:...},
5317 check for %* and handle that case. */
5318 if (p[-1] == '*' && !negate)
5320 int substitution;
5321 const char *r = body;
5323 /* First see whether we have %*. */
5324 substitution = 0;
5325 while (r < endbody)
5327 if (*r == '%' && r[1] == '*')
5328 substitution = 1;
5329 r++;
5331 /* If we do, handle that case. */
5332 if (substitution)
5334 /* Substitute all matching switches as separate args.
5335 But do this by substituting for %*
5336 in the text that follows the colon. */
5338 unsigned hard_match_len = p - filter - 1;
5339 char *string = save_string (body, endbody - body - 1);
5341 for (i = 0; i < n_switches; i++)
5342 if (!strncmp (switches[i].part1, filter, hard_match_len)
5343 && check_live_switch (i, -1))
5345 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5346 /* Pass any arguments this switch has. */
5347 give_switch (i, 1, 1);
5348 suffix_subst = NULL;
5351 /* We didn't match. Try again. */
5352 if (*p++ == '|')
5353 goto next_member;
5354 return endbody;
5358 /* If name specified ends in *, as in {x*:...},
5359 check for presence of any switch name starting with x. */
5360 if (p[-1] == '*')
5362 for (i = 0; i < n_switches; i++)
5364 unsigned hard_match_len = p - filter - 1;
5366 if (!strncmp (switches[i].part1, filter, hard_match_len)
5367 && check_live_switch (i, hard_match_len))
5369 present = 1;
5370 break;
5374 /* Otherwise, check for presence of exact name specified. */
5375 else
5377 for (i = 0; i < n_switches; i++)
5379 if (!strncmp (switches[i].part1, filter, p - filter)
5380 && switches[i].part1[p - filter] == 0
5381 && check_live_switch (i, -1))
5383 present = 1;
5384 break;
5389 /* If it is as desired (present for %{s...}, absent for %{!s...})
5390 then substitute either the switch or the specified
5391 conditional text. */
5392 if (present != negate)
5394 if (elide_switch)
5396 switches[i].live_cond = SWITCH_IGNORE;
5397 switches[i].validated = 1;
5399 else if (ordered || *p == '&')
5400 ordered = 1, switches[i].ordering = 1;
5401 else if (*p == '}')
5402 give_switch (i, 0, include_blanks);
5403 else
5404 /* Even if many alternatives are matched, only output once. */
5405 true_once = 1;
5407 else if (pipe_p)
5409 /* Here if a %{|...} conditional fails: output a minus sign,
5410 which means "standard output" or "standard input". */
5411 do_spec_1 ("-", 0, NULL);
5412 return endbody;
5416 /* We didn't match; try again. */
5417 if (*p++ == '|')
5418 goto next_member;
5420 if (p[-1] == '&')
5422 body = 0;
5423 goto next_ampersand;
5426 if (ordered)
5428 int i;
5429 /* Doing this set of switches later preserves their command-line
5430 ordering. This is needed for e.g. -U, -D and -A. */
5431 for (i = 0; i < n_switches; i++)
5432 if (switches[i].ordering == 1)
5434 switches[i].ordering = 0;
5435 give_switch (i, 0, include_blanks);
5438 /* Process the spec just once, regardless of match count. */
5439 else if (true_once)
5441 if (do_spec_1 (save_string (body, endbody - body - 1),
5442 0, NULL) < 0)
5443 return 0;
5446 return endbody;
5449 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5450 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5451 spec, or -1 if either exact match or %* is used.
5453 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5454 whose value does not begin with "no-" is obsoleted by the same value
5455 with the "no-", similarly for a switch with the "no-" prefix. */
5457 static int
5458 check_live_switch (switchnum, prefix_length)
5459 int switchnum;
5460 int prefix_length;
5462 const char *name = switches[switchnum].part1;
5463 int i;
5465 /* In the common case of {<at-most-one-letter>*}, a negating
5466 switch would always match, so ignore that case. We will just
5467 send the conflicting switches to the compiler phase. */
5468 if (prefix_length >= 0 && prefix_length <= 1)
5469 return 1;
5471 /* If we already processed this switch and determined if it was
5472 live or not, return our past determination. */
5473 if (switches[switchnum].live_cond != 0)
5474 return switches[switchnum].live_cond > 0;
5476 /* Now search for duplicate in a manner that depends on the name. */
5477 switch (*name)
5479 case 'O':
5480 for (i = switchnum + 1; i < n_switches; i++)
5481 if (switches[i].part1[0] == 'O')
5483 switches[switchnum].validated = 1;
5484 switches[switchnum].live_cond = SWITCH_FALSE;
5485 return 0;
5487 break;
5489 case 'W': case 'f': case 'm':
5490 if (! strncmp (name + 1, "no-", 3))
5492 /* We have Xno-YYY, search for XYYY. */
5493 for (i = switchnum + 1; i < n_switches; i++)
5494 if (switches[i].part1[0] == name[0]
5495 && ! strcmp (&switches[i].part1[1], &name[4]))
5497 switches[switchnum].validated = 1;
5498 switches[switchnum].live_cond = SWITCH_FALSE;
5499 return 0;
5502 else
5504 /* We have XYYY, search for Xno-YYY. */
5505 for (i = switchnum + 1; i < n_switches; i++)
5506 if (switches[i].part1[0] == name[0]
5507 && switches[i].part1[1] == 'n'
5508 && switches[i].part1[2] == 'o'
5509 && switches[i].part1[3] == '-'
5510 && !strcmp (&switches[i].part1[4], &name[1]))
5512 switches[switchnum].validated = 1;
5513 switches[switchnum].live_cond = SWITCH_FALSE;
5514 return 0;
5517 break;
5520 /* Otherwise the switch is live. */
5521 switches[switchnum].live_cond = SWITCH_LIVE;
5522 return 1;
5525 /* Pass a switch to the current accumulating command
5526 in the same form that we received it.
5527 SWITCHNUM identifies the switch; it is an index into
5528 the vector of switches gcc received, which is `switches'.
5529 This cannot fail since it never finishes a command line.
5531 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5533 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5534 of the switch. */
5536 static void
5537 give_switch (switchnum, omit_first_word, include_blanks)
5538 int switchnum;
5539 int omit_first_word;
5540 int include_blanks;
5542 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5543 return;
5545 if (!omit_first_word)
5547 do_spec_1 ("-", 0, NULL);
5548 do_spec_1 (switches[switchnum].part1, 1, NULL);
5551 if (switches[switchnum].args != 0)
5553 const char **p;
5554 for (p = switches[switchnum].args; *p; p++)
5556 const char *arg = *p;
5558 if (include_blanks)
5559 do_spec_1 (" ", 0, NULL);
5560 if (suffix_subst)
5562 unsigned length = strlen (arg);
5563 int dot = 0;
5565 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5566 if (arg[length] == '.')
5568 ((char *)arg)[length] = 0;
5569 dot = 1;
5570 break;
5572 do_spec_1 (arg, 1, NULL);
5573 if (dot)
5574 ((char *)arg)[length] = '.';
5575 do_spec_1 (suffix_subst, 1, NULL);
5577 else
5578 do_spec_1 (arg, 1, NULL);
5582 do_spec_1 (" ", 0, NULL);
5583 switches[switchnum].validated = 1;
5586 /* Search for a file named NAME trying various prefixes including the
5587 user's -B prefix and some standard ones.
5588 Return the absolute file name found. If nothing is found, return NAME. */
5590 static const char *
5591 find_file (name)
5592 const char *name;
5594 char *newname;
5596 /* Try multilib_dir if it is defined. */
5597 if (multilib_dir != NULL)
5599 const char *const try = ACONCAT ((multilib_dir, dir_separator_str, name, NULL));
5601 newname = find_a_file (&startfile_prefixes, try, R_OK);
5603 /* If we don't find it in the multi library dir, then fall
5604 through and look for it in the normal places. */
5605 if (newname != NULL)
5606 return newname;
5609 newname = find_a_file (&startfile_prefixes, name, R_OK);
5610 return newname ? newname : name;
5613 /* Determine whether a directory exists. If LINKER, return 0 for
5614 certain fixed names not needed by the linker. If not LINKER, it is
5615 only important to return 0 if the host machine has a small ARG_MAX
5616 limit. */
5618 static int
5619 is_directory (path1, path2, linker)
5620 const char *path1;
5621 const char *path2;
5622 int linker;
5624 int len1 = strlen (path1);
5625 int len2 = strlen (path2);
5626 char *path = (char *) alloca (3 + len1 + len2);
5627 char *cp;
5628 struct stat st;
5630 #ifndef SMALL_ARG_MAX
5631 if (! linker)
5632 return 1;
5633 #endif
5635 /* Construct the path from the two parts. Ensure the string ends with "/.".
5636 The resulting path will be a directory even if the given path is a
5637 symbolic link. */
5638 memcpy (path, path1, len1);
5639 memcpy (path + len1, path2, len2);
5640 cp = path + len1 + len2;
5641 if (!IS_DIR_SEPARATOR (cp[-1]))
5642 *cp++ = DIR_SEPARATOR;
5643 *cp++ = '.';
5644 *cp = '\0';
5646 /* Exclude directories that the linker is known to search. */
5647 if (linker
5648 && ((cp - path == 6
5649 && strcmp (path, concat (dir_separator_str, "lib",
5650 dir_separator_str, ".", NULL)) == 0)
5651 || (cp - path == 10
5652 && strcmp (path, concat (dir_separator_str, "usr",
5653 dir_separator_str, "lib",
5654 dir_separator_str, ".", NULL)) == 0)))
5655 return 0;
5657 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5660 /* Set up the various global variables to indicate that we're processing
5661 the input file named FILENAME. */
5663 void
5664 set_input (filename)
5665 const char *filename;
5667 const char *p;
5669 input_filename = filename;
5670 input_filename_length = strlen (input_filename);
5672 input_basename = input_filename;
5673 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5674 /* Skip drive name so 'x:foo' is handled properly. */
5675 if (input_basename[1] == ':')
5676 input_basename += 2;
5677 #endif
5678 for (p = input_basename; *p; p++)
5679 if (IS_DIR_SEPARATOR (*p))
5680 input_basename = p + 1;
5682 /* Find a suffix starting with the last period,
5683 and set basename_length to exclude that suffix. */
5684 basename_length = strlen (input_basename);
5685 suffixed_basename_length = basename_length;
5686 p = input_basename + basename_length;
5687 while (p != input_basename && *p != '.')
5688 --p;
5689 if (*p == '.' && p != input_basename)
5691 basename_length = p - input_basename;
5692 input_suffix = p + 1;
5694 else
5695 input_suffix = "";
5697 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
5698 we will need to do a stat on the input_filename. The
5699 INPUT_STAT_SET signals that the stat is needed. */
5700 input_stat_set = 0;
5703 /* On fatal signals, delete all the temporary files. */
5705 static void
5706 fatal_error (signum)
5707 int signum;
5709 signal (signum, SIG_DFL);
5710 delete_failure_queue ();
5711 delete_temp_files ();
5712 /* Get the same signal again, this time not handled,
5713 so its normal effect occurs. */
5714 kill (getpid (), signum);
5717 extern int main PARAMS ((int, const char *const *));
5720 main (argc, argv)
5721 int argc;
5722 const char *const *argv;
5724 size_t i;
5725 int value;
5726 int linker_was_run = 0;
5727 char *explicit_link_files;
5728 char *specs_file;
5729 const char *p;
5730 struct user_specs *uptr;
5732 p = argv[0] + strlen (argv[0]);
5733 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5734 --p;
5735 programname = p;
5737 xmalloc_set_program_name (programname);
5739 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5740 /* Perform host dependent initialization when needed. */
5741 GCC_DRIVER_HOST_INITIALIZATION;
5742 #endif
5744 gcc_init_libintl ();
5746 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5747 signal (SIGINT, fatal_error);
5748 #ifdef SIGHUP
5749 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5750 signal (SIGHUP, fatal_error);
5751 #endif
5752 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5753 signal (SIGTERM, fatal_error);
5754 #ifdef SIGPIPE
5755 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5756 signal (SIGPIPE, fatal_error);
5757 #endif
5758 #ifdef SIGCHLD
5759 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5760 receive the signal. A different setting is inheritable */
5761 signal (SIGCHLD, SIG_DFL);
5762 #endif
5764 argbuf_length = 10;
5765 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5767 obstack_init (&obstack);
5769 /* Build multilib_select, et. al from the separate lines that make up each
5770 multilib selection. */
5772 const char *const *q = multilib_raw;
5773 int need_space;
5775 obstack_init (&multilib_obstack);
5776 while ((p = *q++) != (char *) 0)
5777 obstack_grow (&multilib_obstack, p, strlen (p));
5779 obstack_1grow (&multilib_obstack, 0);
5780 multilib_select = obstack_finish (&multilib_obstack);
5782 q = multilib_matches_raw;
5783 while ((p = *q++) != (char *) 0)
5784 obstack_grow (&multilib_obstack, p, strlen (p));
5786 obstack_1grow (&multilib_obstack, 0);
5787 multilib_matches = obstack_finish (&multilib_obstack);
5789 q = multilib_exclusions_raw;
5790 while ((p = *q++) != (char *) 0)
5791 obstack_grow (&multilib_obstack, p, strlen (p));
5793 obstack_1grow (&multilib_obstack, 0);
5794 multilib_exclusions = obstack_finish (&multilib_obstack);
5796 need_space = FALSE;
5797 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5799 if (need_space)
5800 obstack_1grow (&multilib_obstack, ' ');
5801 obstack_grow (&multilib_obstack,
5802 multilib_defaults_raw[i],
5803 strlen (multilib_defaults_raw[i]));
5804 need_space = TRUE;
5807 obstack_1grow (&multilib_obstack, 0);
5808 multilib_defaults = obstack_finish (&multilib_obstack);
5811 /* Set up to remember the pathname of gcc and any options
5812 needed for collect. We use argv[0] instead of programname because
5813 we need the complete pathname. */
5814 obstack_init (&collect_obstack);
5815 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5816 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5817 putenv (obstack_finish (&collect_obstack));
5819 #ifdef INIT_ENVIRONMENT
5820 /* Set up any other necessary machine specific environment variables. */
5821 putenv (INIT_ENVIRONMENT);
5822 #endif
5824 /* Make a table of what switches there are (switches, n_switches).
5825 Make a table of specified input files (infiles, n_infiles).
5826 Decode switches that are handled locally. */
5828 process_command (argc, argv);
5830 /* Initialize the vector of specs to just the default.
5831 This means one element containing 0s, as a terminator. */
5833 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5834 memcpy ((char *) compilers, (char *) default_compilers,
5835 sizeof default_compilers);
5836 n_compilers = n_default_compilers;
5838 /* Read specs from a file if there is one. */
5840 machine_suffix = concat (spec_machine, dir_separator_str,
5841 spec_version, dir_separator_str, NULL);
5842 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
5844 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5845 /* Read the specs file unless it is a default one. */
5846 if (specs_file != 0 && strcmp (specs_file, "specs"))
5847 read_specs (specs_file, TRUE);
5848 else
5849 init_spec ();
5851 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5852 for any override of as, ld and libraries. */
5853 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5854 + strlen (just_machine_suffix)
5855 + sizeof ("specs"));
5857 strcpy (specs_file, standard_exec_prefix);
5858 strcat (specs_file, just_machine_suffix);
5859 strcat (specs_file, "specs");
5860 if (access (specs_file, R_OK) == 0)
5861 read_specs (specs_file, TRUE);
5863 /* If not cross-compiling, look for startfiles in the standard places.
5864 Similarly, don't add the standard prefixes if startfile handling
5865 will be under control of startfile_prefix_spec. */
5866 if (*cross_compile == '0' || *startfile_prefix_spec == 0)
5868 if (*md_exec_prefix)
5870 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5871 PREFIX_PRIORITY_LAST, 0, NULL);
5872 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5873 PREFIX_PRIORITY_LAST, 0, NULL);
5876 if (*md_startfile_prefix)
5877 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5878 PREFIX_PRIORITY_LAST, 0, NULL);
5880 if (*md_startfile_prefix_1)
5881 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5882 PREFIX_PRIORITY_LAST, 0, NULL);
5884 /* If standard_startfile_prefix is relative, base it on
5885 standard_exec_prefix. This lets us move the installed tree
5886 as a unit. If GCC_EXEC_PREFIX is defined, base
5887 standard_startfile_prefix on that as well. */
5888 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5889 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5890 PREFIX_PRIORITY_LAST, 0, NULL);
5891 else
5893 if (gcc_exec_prefix)
5894 add_prefix (&startfile_prefixes,
5895 concat (gcc_exec_prefix, machine_suffix,
5896 standard_startfile_prefix, NULL),
5897 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5898 add_prefix (&startfile_prefixes,
5899 concat (standard_exec_prefix,
5900 machine_suffix,
5901 standard_startfile_prefix, NULL),
5902 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5905 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5906 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5907 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5908 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5909 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5910 add_prefix (&startfile_prefixes, "./", NULL,
5911 PREFIX_PRIORITY_LAST, 1, NULL);
5912 #endif
5914 else
5916 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5917 && gcc_exec_prefix)
5918 add_prefix (&startfile_prefixes,
5919 concat (gcc_exec_prefix, machine_suffix,
5920 standard_startfile_prefix, NULL),
5921 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5924 if (*startfile_prefix_spec != 0
5925 && do_spec_2 (startfile_prefix_spec) == 0
5926 && do_spec_1 (" ", 0, NULL) == 0)
5928 int ndx;
5929 for (ndx = 0; ndx < argbuf_index; ndx++)
5930 add_prefix (&startfile_prefixes, argbuf[ndx], "BINUTILS",
5931 PREFIX_PRIORITY_LAST, 0, NULL);
5934 /* Process any user specified specs in the order given on the command
5935 line. */
5936 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5938 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5939 read_specs (filename ? filename : uptr->filename, FALSE);
5942 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5943 if (gcc_exec_prefix)
5944 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
5945 spec_version, dir_separator_str, NULL);
5947 /* Now we have the specs.
5948 Set the `valid' bits for switches that match anything in any spec. */
5950 validate_all_switches ();
5952 /* Now that we have the switches and the specs, set
5953 the subdirectory based on the options. */
5954 set_multilib_dir ();
5956 /* Warn about any switches that no pass was interested in. */
5958 for (i = 0; (int) i < n_switches; i++)
5959 if (! switches[i].validated)
5960 error ("unrecognized option `-%s'", switches[i].part1);
5962 /* Obey some of the options. */
5964 if (print_search_dirs)
5966 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5967 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5968 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5969 return (0);
5972 if (print_file_name)
5974 printf ("%s\n", find_file (print_file_name));
5975 return (0);
5978 if (print_prog_name)
5980 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5981 printf ("%s\n", (newname ? newname : print_prog_name));
5982 return (0);
5985 if (print_multi_lib)
5987 print_multilib_info ();
5988 return (0);
5991 if (print_multi_directory)
5993 if (multilib_dir == NULL)
5994 printf (".\n");
5995 else
5996 printf ("%s\n", multilib_dir);
5997 return (0);
6000 if (target_help_flag)
6002 /* Print if any target specific options. */
6004 /* We do not exit here. Instead we have created a fake input file
6005 called 'target-dummy' which needs to be compiled, and we pass this
6006 on to the various sub-processes, along with the --target-help
6007 switch. */
6010 if (print_help_list)
6012 display_help ();
6014 if (! verbose_flag)
6016 printf (_("\nFor bug reporting instructions, please see:\n"));
6017 printf ("%s.\n", GCCBUGURL);
6019 return (0);
6022 /* We do not exit here. Instead we have created a fake input file
6023 called 'help-dummy' which needs to be compiled, and we pass this
6024 on the various sub-processes, along with the --help switch. */
6027 if (verbose_flag)
6029 int n;
6030 const char *thrmod;
6032 notice ("Configured with: %s\n", configuration_arguments);
6034 #ifdef THREAD_MODEL_SPEC
6035 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6036 but there's no point in doing all this processing just to get
6037 thread_model back. */
6038 obstack_init (&obstack);
6039 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6040 obstack_1grow (&obstack, '\0');
6041 thrmod = obstack_finish (&obstack);
6042 #else
6043 thrmod = thread_model;
6044 #endif
6046 notice ("Thread model: %s\n", thrmod);
6048 /* compiler_version is truncated at the first space when initialized
6049 from version string, so truncate version_string at the first space
6050 before comparing. */
6051 for (n = 0; version_string[n]; n++)
6052 if (version_string[n] == ' ')
6053 break;
6055 if (! strncmp (version_string, compiler_version, n)
6056 && compiler_version[n] == 0)
6057 notice ("gcc version %s\n", version_string);
6058 else
6059 notice ("gcc driver version %s executing gcc version %s\n",
6060 version_string, compiler_version);
6062 if (n_infiles == 0)
6063 return (0);
6066 if (n_infiles == added_libraries)
6067 fatal ("no input files");
6069 /* Make a place to record the compiler output file names
6070 that correspond to the input files. */
6072 i = n_infiles;
6073 i += lang_specific_extra_outfiles;
6074 outfiles = (const char **) xcalloc (i, sizeof (char *));
6076 /* Record which files were specified explicitly as link input. */
6078 explicit_link_files = xcalloc (1, n_infiles);
6080 for (i = 0; (int) i < n_infiles; i++)
6082 int this_file_error = 0;
6084 /* Tell do_spec what to substitute for %i. */
6086 input_file_number = i;
6087 set_input (infiles[i].name);
6089 /* Use the same thing in %o, unless cp->spec says otherwise. */
6091 outfiles[i] = input_filename;
6093 /* Figure out which compiler from the file's suffix. */
6095 input_file_compiler
6096 = lookup_compiler (infiles[i].name, input_filename_length,
6097 infiles[i].language);
6099 if (input_file_compiler)
6101 /* Ok, we found an applicable compiler. Run its spec. */
6103 if (input_file_compiler->spec[0] == '#')
6105 error ("%s: %s compiler not installed on this system",
6106 input_filename, &input_file_compiler->spec[1]);
6107 this_file_error = 1;
6109 else
6111 value = do_spec (input_file_compiler->spec);
6112 if (value < 0)
6113 this_file_error = 1;
6117 /* If this file's name does not contain a recognized suffix,
6118 record it as explicit linker input. */
6120 else
6121 explicit_link_files[i] = 1;
6123 /* Clear the delete-on-failure queue, deleting the files in it
6124 if this compilation failed. */
6126 if (this_file_error)
6128 delete_failure_queue ();
6129 error_count++;
6131 /* If this compilation succeeded, don't delete those files later. */
6132 clear_failure_queue ();
6135 /* Reset the output file name to the first input file name, for use
6136 with %b in LINK_SPEC on a target that prefers not to emit a.out
6137 by default. */
6138 if (n_infiles > 0)
6139 set_input (infiles[0].name);
6141 if (error_count == 0)
6143 /* Make sure INPUT_FILE_NUMBER points to first available open
6144 slot. */
6145 input_file_number = n_infiles;
6146 if (lang_specific_pre_link ())
6147 error_count++;
6150 /* Run ld to link all the compiler output files. */
6152 if (error_count == 0)
6154 int tmp = execution_count;
6156 /* We'll use ld if we can't find collect2. */
6157 if (! strcmp (linker_name_spec, "collect2"))
6159 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
6160 if (s == NULL)
6161 linker_name_spec = "ld";
6163 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6164 for collect. */
6165 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
6166 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
6168 value = do_spec (link_command_spec);
6169 if (value < 0)
6170 error_count = 1;
6171 linker_was_run = (tmp != execution_count);
6174 /* If options said don't run linker,
6175 complain about input files to be given to the linker. */
6177 if (! linker_was_run && error_count == 0)
6178 for (i = 0; (int) i < n_infiles; i++)
6179 if (explicit_link_files[i])
6180 error ("%s: linker input file unused because linking not done",
6181 outfiles[i]);
6183 /* Delete some or all of the temporary files we made. */
6185 if (error_count)
6186 delete_failure_queue ();
6187 delete_temp_files ();
6189 if (print_help_list)
6191 printf (("\nFor bug reporting instructions, please see:\n"));
6192 printf ("%s\n", GCCBUGURL);
6195 return (signal_count != 0 ? 2
6196 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
6197 : 0);
6200 /* Find the proper compilation spec for the file name NAME,
6201 whose length is LENGTH. LANGUAGE is the specified language,
6202 or 0 if this file is to be passed to the linker. */
6204 static struct compiler *
6205 lookup_compiler (name, length, language)
6206 const char *name;
6207 size_t length;
6208 const char *language;
6210 struct compiler *cp;
6212 /* If this was specified by the user to be a linker input, indicate that. */
6213 if (language != 0 && language[0] == '*')
6214 return 0;
6216 /* Otherwise, look for the language, if one is spec'd. */
6217 if (language != 0)
6219 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6220 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6221 return cp;
6223 error ("language %s not recognized", language);
6224 return 0;
6227 /* Look for a suffix. */
6228 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6230 if (/* The suffix `-' matches only the file name `-'. */
6231 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6232 || (strlen (cp->suffix) < length
6233 /* See if the suffix matches the end of NAME. */
6234 && !strcmp (cp->suffix,
6235 name + length - strlen (cp->suffix))
6237 break;
6240 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6241 /* look again, but case-insensitively this time. */
6242 if (cp < compilers)
6243 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6245 if (/* The suffix `-' matches only the file name `-'. */
6246 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6247 || (strlen (cp->suffix) < length
6248 /* See if the suffix matches the end of NAME. */
6249 && ((!strcmp (cp->suffix,
6250 name + length - strlen (cp->suffix))
6251 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6252 && !strcasecmp (cp->suffix,
6253 name + length - strlen (cp->suffix)))
6255 break;
6257 #endif
6259 if (cp >= compilers)
6261 if (cp->spec[0] != '@')
6262 /* A non-alias entry: return it. */
6263 return cp;
6265 /* An alias entry maps a suffix to a language.
6266 Search for the language; pass 0 for NAME and LENGTH
6267 to avoid infinite recursion if language not found. */
6268 return lookup_compiler (NULL, 0, cp->spec + 1);
6270 return 0;
6273 static char *
6274 save_string (s, len)
6275 const char *s;
6276 int len;
6278 char *result = xmalloc (len + 1);
6280 memcpy (result, s, len);
6281 result[len] = 0;
6282 return result;
6285 void
6286 pfatal_with_name (name)
6287 const char *name;
6289 perror_with_name (name);
6290 delete_temp_files ();
6291 exit (1);
6294 static void
6295 perror_with_name (name)
6296 const char *name;
6298 error ("%s: %s", name, xstrerror (errno));
6301 static void
6302 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6303 const char *errmsg_fmt;
6304 const char *errmsg_arg;
6306 if (errmsg_arg)
6308 int save_errno = errno;
6310 /* Space for trailing '\0' is in %s. */
6311 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6312 sprintf (msg, errmsg_fmt, errmsg_arg);
6313 errmsg_fmt = msg;
6315 errno = save_errno;
6318 pfatal_with_name (errmsg_fmt);
6321 /* Output an error message and exit */
6323 void
6324 fancy_abort ()
6326 fatal ("internal gcc abort");
6329 /* Output an error message and exit */
6331 void
6332 fatal VPARAMS ((const char *msgid, ...))
6334 VA_OPEN (ap, msgid);
6335 VA_FIXEDARG (ap, const char *, msgid);
6337 fprintf (stderr, "%s: ", programname);
6338 vfprintf (stderr, _(msgid), ap);
6339 VA_CLOSE (ap);
6340 fprintf (stderr, "\n");
6341 delete_temp_files ();
6342 exit (1);
6345 void
6346 error VPARAMS ((const char *msgid, ...))
6348 VA_OPEN (ap, msgid);
6349 VA_FIXEDARG (ap, const char *, msgid);
6351 fprintf (stderr, "%s: ", programname);
6352 vfprintf (stderr, _(msgid), ap);
6353 VA_CLOSE (ap);
6355 fprintf (stderr, "\n");
6358 static void
6359 notice VPARAMS ((const char *msgid, ...))
6361 VA_OPEN (ap, msgid);
6362 VA_FIXEDARG (ap, const char *, msgid);
6364 vfprintf (stderr, _(msgid), ap);
6365 VA_CLOSE (ap);
6368 static void
6369 validate_all_switches ()
6371 struct compiler *comp;
6372 const char *p;
6373 char c;
6374 struct spec_list *spec;
6376 for (comp = compilers; comp->spec; comp++)
6378 p = comp->spec;
6379 while ((c = *p++))
6380 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6381 /* We have a switch spec. */
6382 validate_switches (p + 1);
6385 /* Look through the linked list of specs read from the specs file. */
6386 for (spec = specs; spec; spec = spec->next)
6388 p = *(spec->ptr_spec);
6389 while ((c = *p++))
6390 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6391 /* We have a switch spec. */
6392 validate_switches (p + 1);
6395 p = link_command_spec;
6396 while ((c = *p++))
6397 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6398 /* We have a switch spec. */
6399 validate_switches (p + 1);
6402 /* Look at the switch-name that comes after START
6403 and mark as valid all supplied switches that match it. */
6405 static void
6406 validate_switches (start)
6407 const char *start;
6409 const char *p = start;
6410 const char *filter;
6411 int i;
6412 int suffix;
6414 if (*p == '|')
6415 ++p;
6417 next_member:
6418 if (*p == '!')
6419 ++p;
6421 suffix = 0;
6422 if (*p == '.')
6423 suffix = 1, ++p;
6425 filter = p;
6426 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6427 p++;
6429 if (suffix)
6431 else if (p[-1] == '*')
6433 /* Mark all matching switches as valid. */
6434 for (i = 0; i < n_switches; i++)
6435 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6436 switches[i].validated = 1;
6438 else
6440 /* Mark an exact matching switch as valid. */
6441 for (i = 0; i < n_switches; i++)
6443 if (!strncmp (switches[i].part1, filter, p - filter)
6444 && switches[i].part1[p - filter] == 0)
6445 switches[i].validated = 1;
6449 if (*p++ == '|' || p[-1] == '&')
6450 goto next_member;
6453 /* Check whether a particular argument was used. The first time we
6454 canonicalize the switches to keep only the ones we care about. */
6456 static int
6457 used_arg (p, len)
6458 const char *p;
6459 int len;
6461 struct mswitchstr
6463 const char *str;
6464 const char *replace;
6465 int len;
6466 int rep_len;
6469 static struct mswitchstr *mswitches;
6470 static int n_mswitches;
6471 int i, j;
6473 if (!mswitches)
6475 struct mswitchstr *matches;
6476 const char *q;
6477 int cnt = 0;
6479 /* Break multilib_matches into the component strings of string
6480 and replacement string. */
6481 for (q = multilib_matches; *q != '\0'; q++)
6482 if (*q == ';')
6483 cnt++;
6485 matches =
6486 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6487 i = 0;
6488 q = multilib_matches;
6489 while (*q != '\0')
6491 matches[i].str = q;
6492 while (*q != ' ')
6494 if (*q == '\0')
6495 abort ();
6496 q++;
6498 matches[i].len = q - matches[i].str;
6500 matches[i].replace = ++q;
6501 while (*q != ';' && *q != '\0')
6503 if (*q == ' ')
6504 abort ();
6505 q++;
6507 matches[i].rep_len = q - matches[i].replace;
6508 i++;
6509 if (*q == ';')
6510 q++;
6513 /* Now build a list of the replacement string for switches that we care
6514 about. Make sure we allocate at least one entry. This prevents
6515 xmalloc from calling fatal, and prevents us from re-executing this
6516 block of code. */
6517 mswitches
6518 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6519 * (n_switches ? n_switches : 1));
6520 for (i = 0; i < n_switches; i++)
6522 int xlen = strlen (switches[i].part1);
6523 for (j = 0; j < cnt; j++)
6524 if (xlen == matches[j].len
6525 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6527 mswitches[n_mswitches].str = matches[j].replace;
6528 mswitches[n_mswitches].len = matches[j].rep_len;
6529 mswitches[n_mswitches].replace = (char *) 0;
6530 mswitches[n_mswitches].rep_len = 0;
6531 n_mswitches++;
6532 break;
6537 for (i = 0; i < n_mswitches; i++)
6538 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6539 return 1;
6541 return 0;
6544 static int
6545 default_arg (p, len)
6546 const char *p;
6547 int len;
6549 const char *start, *end;
6551 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6553 while (*start == ' ' || *start == '\t')
6554 start++;
6556 if (*start == '\0')
6557 break;
6559 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6562 if ((end - start) == len && strncmp (p, start, len) == 0)
6563 return 1;
6565 if (*end == '\0')
6566 break;
6569 return 0;
6572 /* Work out the subdirectory to use based on the options. The format of
6573 multilib_select is a list of elements. Each element is a subdirectory
6574 name followed by a list of options followed by a semicolon. The format
6575 of multilib_exclusions is the same, but without the preceding
6576 directory. First gcc will check the exclusions, if none of the options
6577 beginning with an exclamation point are present, and all of the other
6578 options are present, then we will ignore this completely. Passing
6579 that, gcc will consider each multilib_select in turn using the same
6580 rules for matching the options. If a match is found, that subdirectory
6581 will be used. */
6583 static void
6584 set_multilib_dir ()
6586 const char *p;
6587 unsigned int this_path_len;
6588 const char *this_path, *this_arg;
6589 int not_arg;
6590 int ok;
6592 p = multilib_exclusions;
6593 while (*p != '\0')
6595 /* Ignore newlines. */
6596 if (*p == '\n')
6598 ++p;
6599 continue;
6602 /* Check the arguments. */
6603 ok = 1;
6604 while (*p != ';')
6606 if (*p == '\0')
6607 abort ();
6609 if (! ok)
6611 ++p;
6612 continue;
6615 this_arg = p;
6616 while (*p != ' ' && *p != ';')
6618 if (*p == '\0')
6619 abort ();
6620 ++p;
6623 if (*this_arg != '!')
6624 not_arg = 0;
6625 else
6627 not_arg = 1;
6628 ++this_arg;
6631 ok = used_arg (this_arg, p - this_arg);
6632 if (not_arg)
6633 ok = ! ok;
6635 if (*p == ' ')
6636 ++p;
6639 if (ok)
6640 return;
6642 ++p;
6645 p = multilib_select;
6646 while (*p != '\0')
6648 /* Ignore newlines. */
6649 if (*p == '\n')
6651 ++p;
6652 continue;
6655 /* Get the initial path. */
6656 this_path = p;
6657 while (*p != ' ')
6659 if (*p == '\0')
6660 abort ();
6661 ++p;
6663 this_path_len = p - this_path;
6665 /* Check the arguments. */
6666 ok = 1;
6667 ++p;
6668 while (*p != ';')
6670 if (*p == '\0')
6671 abort ();
6673 if (! ok)
6675 ++p;
6676 continue;
6679 this_arg = p;
6680 while (*p != ' ' && *p != ';')
6682 if (*p == '\0')
6683 abort ();
6684 ++p;
6687 if (*this_arg != '!')
6688 not_arg = 0;
6689 else
6691 not_arg = 1;
6692 ++this_arg;
6695 /* If this is a default argument, we can just ignore it.
6696 This is true even if this_arg begins with '!'. Beginning
6697 with '!' does not mean that this argument is necessarily
6698 inappropriate for this library: it merely means that
6699 there is a more specific library which uses this
6700 argument. If this argument is a default, we need not
6701 consider that more specific library. */
6702 if (! default_arg (this_arg, p - this_arg))
6704 ok = used_arg (this_arg, p - this_arg);
6705 if (not_arg)
6706 ok = ! ok;
6709 if (*p == ' ')
6710 ++p;
6713 if (ok)
6715 if (this_path_len != 1
6716 || this_path[0] != '.')
6718 char *new_multilib_dir = xmalloc (this_path_len + 1);
6719 strncpy (new_multilib_dir, this_path, this_path_len);
6720 new_multilib_dir[this_path_len] = '\0';
6721 multilib_dir = new_multilib_dir;
6723 break;
6726 ++p;
6730 /* Print out the multiple library subdirectory selection
6731 information. This prints out a series of lines. Each line looks
6732 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6733 required. Only the desired options are printed out, the negative
6734 matches. The options are print without a leading dash. There are
6735 no spaces to make it easy to use the information in the shell.
6736 Each subdirectory is printed only once. This assumes the ordering
6737 generated by the genmultilib script. Also, we leave out ones that match
6738 the exclusions. */
6740 static void
6741 print_multilib_info ()
6743 const char *p = multilib_select;
6744 const char *last_path = 0, *this_path;
6745 int skip;
6746 unsigned int last_path_len = 0;
6748 while (*p != '\0')
6750 skip = 0;
6751 /* Ignore newlines. */
6752 if (*p == '\n')
6754 ++p;
6755 continue;
6758 /* Get the initial path. */
6759 this_path = p;
6760 while (*p != ' ')
6762 if (*p == '\0')
6763 abort ();
6764 ++p;
6767 /* Check for matches with the multilib_exclusions. We don't bother
6768 with the '!' in either list. If any of the exclusion rules match
6769 all of its options with the select rule, we skip it. */
6771 const char *e = multilib_exclusions;
6772 const char *this_arg;
6774 while (*e != '\0')
6776 int m = 1;
6777 /* Ignore newlines. */
6778 if (*e == '\n')
6780 ++e;
6781 continue;
6784 /* Check the arguments. */
6785 while (*e != ';')
6787 const char *q;
6788 int mp = 0;
6790 if (*e == '\0')
6791 abort ();
6793 if (! m)
6795 ++e;
6796 continue;
6799 this_arg = e;
6801 while (*e != ' ' && *e != ';')
6803 if (*e == '\0')
6804 abort ();
6805 ++e;
6808 q = p + 1;
6809 while (*q != ';')
6811 const char *arg;
6812 int len = e - this_arg;
6814 if (*q == '\0')
6815 abort ();
6817 arg = q;
6819 while (*q != ' ' && *q != ';')
6821 if (*q == '\0')
6822 abort ();
6823 ++q;
6826 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6827 default_arg (this_arg, e - this_arg))
6829 mp = 1;
6830 break;
6833 if (*q == ' ')
6834 ++q;
6837 if (! mp)
6838 m = 0;
6840 if (*e == ' ')
6841 ++e;
6844 if (m)
6846 skip = 1;
6847 break;
6850 if (*e != '\0')
6851 ++e;
6855 if (! skip)
6857 /* If this is a duplicate, skip it. */
6858 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6859 && ! strncmp (last_path, this_path, last_path_len));
6861 last_path = this_path;
6862 last_path_len = p - this_path;
6865 /* If this directory requires any default arguments, we can skip
6866 it. We will already have printed a directory identical to
6867 this one which does not require that default argument. */
6868 if (! skip)
6870 const char *q;
6872 q = p + 1;
6873 while (*q != ';')
6875 const char *arg;
6877 if (*q == '\0')
6878 abort ();
6880 if (*q == '!')
6881 arg = NULL;
6882 else
6883 arg = q;
6885 while (*q != ' ' && *q != ';')
6887 if (*q == '\0')
6888 abort ();
6889 ++q;
6892 if (arg != NULL
6893 && default_arg (arg, q - arg))
6895 skip = 1;
6896 break;
6899 if (*q == ' ')
6900 ++q;
6904 if (! skip)
6906 const char *p1;
6908 for (p1 = last_path; p1 < p; p1++)
6909 putchar (*p1);
6910 putchar (';');
6913 ++p;
6914 while (*p != ';')
6916 int use_arg;
6918 if (*p == '\0')
6919 abort ();
6921 if (skip)
6923 ++p;
6924 continue;
6927 use_arg = *p != '!';
6929 if (use_arg)
6930 putchar ('@');
6932 while (*p != ' ' && *p != ';')
6934 if (*p == '\0')
6935 abort ();
6936 if (use_arg)
6937 putchar (*p);
6938 ++p;
6941 if (*p == ' ')
6942 ++p;
6945 if (! skip)
6947 /* If there are extra options, print them now. */
6948 if (multilib_extra && *multilib_extra)
6950 int print_at = TRUE;
6951 const char *q;
6953 for (q = multilib_extra; *q != '\0'; q++)
6955 if (*q == ' ')
6956 print_at = TRUE;
6957 else
6959 if (print_at)
6960 putchar ('@');
6961 putchar (*q);
6962 print_at = FALSE;
6967 putchar ('\n');
6970 ++p;