Daily bump.
[official-gcc.git] / gcc / gcc.c
blobb46b71f74e9fd0a06fab0d652f9855a6dda087ae
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 const char *find_file PARAMS ((const char *));
303 static int is_directory PARAMS ((const char *, const char *, int));
304 static void validate_switches PARAMS ((const char *));
305 static void validate_all_switches PARAMS ((void));
306 static void give_switch PARAMS ((int, int, int));
307 static int used_arg PARAMS ((const char *, int));
308 static int default_arg PARAMS ((const char *, int));
309 static void set_multilib_dir PARAMS ((void));
310 static void print_multilib_info PARAMS ((void));
311 static void perror_with_name PARAMS ((const char *));
312 static void pfatal_pexecute PARAMS ((const char *, const char *))
313 ATTRIBUTE_NORETURN;
314 static void notice PARAMS ((const char *, ...))
315 ATTRIBUTE_PRINTF_1;
316 static void display_help PARAMS ((void));
317 static void add_preprocessor_option PARAMS ((const char *, int));
318 static void add_assembler_option PARAMS ((const char *, int));
319 static void add_linker_option PARAMS ((const char *, int));
320 static void process_command PARAMS ((int, const char *const *));
321 static int execute PARAMS ((void));
322 static void clear_args PARAMS ((void));
323 static void fatal_error PARAMS ((int));
324 #ifdef ENABLE_SHARED_LIBGCC
325 static void init_gcc_specs PARAMS ((struct obstack *,
326 const char *, const char *,
327 const char *));
328 #endif
329 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
330 static const char *convert_filename PARAMS ((const char *, int, int));
331 #endif
333 /* The Specs Language
335 Specs are strings containing lines, each of which (if not blank)
336 is made up of a program name, and arguments separated by spaces.
337 The program name must be exact and start from root, since no path
338 is searched and it is unreliable to depend on the current working directory.
339 Redirection of input or output is not supported; the subprograms must
340 accept filenames saying what files to read and write.
342 In addition, the specs can contain %-sequences to substitute variable text
343 or for conditional text. Here is a table of all defined %-sequences.
344 Note that spaces are not generated automatically around the results of
345 expanding these sequences; therefore, you can concatenate them together
346 or with constant text in a single argument.
348 %% substitute one % into the program name or argument.
349 %i substitute the name of the input file being processed.
350 %b substitute the basename of the input file being processed.
351 This is the substring up to (and not including) the last period
352 and not including the directory.
353 %B same as %b, but include the file suffix (text after the last period).
354 %gSUFFIX
355 substitute a file name that has suffix SUFFIX and is chosen
356 once per compilation, and mark the argument a la %d. To reduce
357 exposure to denial-of-service attacks, the file name is now
358 chosen in a way that is hard to predict even when previously
359 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
360 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
361 the regexp "[.A-Za-z]*%O"; "%O" is treated exactly as if it
362 had been pre-processed. Previously, %g was simply substituted
363 with a file name chosen once per compilation, without regard
364 to any appended suffix (which was therefore treated just like
365 ordinary text), making such attacks more likely to succeed.
366 %uSUFFIX
367 like %g, but generates a new temporary file name even if %uSUFFIX
368 was already seen.
369 %USUFFIX
370 substitutes the last file name generated with %uSUFFIX, generating a
371 new one if there is no such last file name. In the absence of any
372 %uSUFFIX, this is just like %gSUFFIX, except they don't share
373 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
374 would involve the generation of two distinct file names, one
375 for each `%g.s' and another for each `%U.s'. Previously, %U was
376 simply substituted with a file name chosen for the previous %u,
377 without regard to any appended suffix.
378 %jSUFFIX
379 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
380 writable, and if save-temps is off; otherwise, substitute the name
381 of a temporary file, just like %u. This temporary file is not
382 meant for communication between processes, but rather as a junk
383 disposal mechanism.
384 %.SUFFIX
385 substitutes .SUFFIX for the suffixes of a matched switch's args when
386 it is subsequently output with %*. SUFFIX is terminated by the next
387 space or %.
388 %d marks the argument containing or following the %d as a
389 temporary file name, so that that file will be deleted if CC exits
390 successfully. Unlike %g, this contributes no text to the argument.
391 %w marks the argument containing or following the %w as the
392 "output file" of this compilation. This puts the argument
393 into the sequence of arguments that %o will substitute later.
394 %W{...}
395 like %{...} but mark last argument supplied within
396 as a file to be deleted on failure.
397 %o substitutes the names of all the output files, with spaces
398 automatically placed around them. You should write spaces
399 around the %o as well or the results are undefined.
400 %o is for use in the specs for running the linker.
401 Input files whose names have no recognized suffix are not compiled
402 at all, but they are included among the output files, so they will
403 be linked.
404 %O substitutes the suffix for object files. Note that this is
405 handled specially when it immediately follows %g, %u, or %U
406 (with or without a suffix argument) because of the need for
407 those to form complete file names. The handling is such that
408 %O is treated exactly as if it had already been substituted,
409 except that %g, %u, and %U do not currently support additional
410 SUFFIX characters following %O as they would following, for
411 example, `.o'.
412 %p substitutes the standard macro predefinitions for the
413 current target machine. Use this when running cpp.
414 %P like %p, but puts `__' before and after the name of each macro.
415 (Except macros that already have __.)
416 This is for ANSI C.
417 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
418 %s current argument is the name of a library or startup file of some sort.
419 Search for that file in a standard list of directories
420 and substitute the full name found.
421 %eSTR Print STR as an error message. STR is terminated by a newline.
422 Use this when inconsistent options are detected.
423 %nSTR Print STR as an notice. STR is terminated by a newline.
424 %x{OPTION} Accumulate an option for %X.
425 %X Output the accumulated linker options specified by compilations.
426 %Y Output the accumulated assembler options specified by compilations.
427 %Z Output the accumulated preprocessor options specified by compilations.
428 %v1 Substitute the major version number of GCC.
429 (For version 2.5.3, this is 2.)
430 %v2 Substitute the minor version number of GCC.
431 (For version 2.5.3, this is 5.)
432 %v3 Substitute the patch level number of GCC.
433 (For version 2.5.3, this is 3.)
434 %a process ASM_SPEC as a spec.
435 This allows config.h to specify part of the spec for running as.
436 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
437 used here. This can be used to run a post-processor after the
438 assembler has done its job.
439 %D Dump out a -L option for each directory in startfile_prefixes.
440 If multilib_dir is set, extra entries are generated with it affixed.
441 %l process LINK_SPEC as a spec.
442 %L process LIB_SPEC as a spec.
443 %G process LIBGCC_SPEC as a spec.
444 %M output multilib_dir with directory separators replaced with "_";
445 if multilib_dir is not set or is ".", output "".
446 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
447 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
448 %C process CPP_SPEC as a spec.
449 %1 process CC1_SPEC as a spec.
450 %2 process CC1PLUS_SPEC as a spec.
451 %| output "-" if the input for the current command is coming from a pipe.
452 %* substitute the variable part of a matched option. (See below.)
453 Note that each comma in the substituted string is replaced by
454 a single space.
455 %{S} substitutes the -S switch, if that switch was given to CC.
456 If that switch was not specified, this substitutes nothing.
457 Here S is a metasyntactic variable.
458 %{S*} substitutes all the switches specified to CC whose names start
459 with -S. This is used for -o, -I, etc; switches that take
460 arguments. CC considers `-o foo' as being one switch whose
461 name starts with `o'. %{o*} would substitute this text,
462 including the space; thus, two arguments would be generated.
463 %{^S*} likewise, but don't put a blank between a switch and any args.
464 %{S*&T*} likewise, but preserve order of S and T options (the order
465 of S and T in the spec is not significant). Can be any number
466 of ampersand-separated variables; for each the wild card is
467 optional. Useful for CPP as %{D*&U*&A*}.
468 %{S*:X} substitutes X if one or more switches whose names start with -S are
469 specified to CC. Note that the tail part of the -S option
470 (i.e. the part matched by the `*') will be substituted for each
471 occurrence of %* within X.
472 %{<S} remove all occurrences of -S from the command line.
473 Note - this option is position dependent. % commands in the
474 spec string before this option will see -S, % commands in the
475 spec string after this option will not.
476 %{S:X} substitutes X, but only if the -S switch was given to CC.
477 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
478 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
479 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
480 %{.S:X} substitutes X, but only if processing a file with suffix S.
481 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
482 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
483 combined with ! and . as above binding stronger than the OR.
484 %(Spec) processes a specification defined in a specs file as *Spec:
485 %[Spec] as above, but put __ around -D arguments
487 The conditional text X in a %{S:X} or %{!S:X} construct may contain
488 other nested % constructs or spaces, or even newlines. They are
489 processed as usual, as described above.
491 The -O, -f, -m, and -W switches are handled specifically in these
492 constructs. If another value of -O or the negated form of a -f, -m, or
493 -W switch is found later in the command line, the earlier switch
494 value is ignored, except with {S*} where S is just one letter; this
495 passes all matching options.
497 The character | at the beginning of the predicate text is used to indicate
498 that a command should be piped to the following command, but only if -pipe
499 is specified.
501 Note that it is built into CC which switches take arguments and which
502 do not. You might think it would be useful to generalize this to
503 allow each compiler's spec to say which switches take arguments. But
504 this cannot be done in a consistent fashion. CC cannot even decide
505 which input files have been specified without knowing which switches
506 take arguments, and it must know which input files to compile in order
507 to tell which compilers to run.
509 CC also knows implicitly that arguments starting in `-l' are to be
510 treated as compiler output files, and passed to the linker in their
511 proper position among the other output files. */
513 /* Define the macros used for specs %a, %l, %L, %S, %C, %1. */
515 /* config.h can define ASM_SPEC to provide extra args to the assembler
516 or extra switch-translations. */
517 #ifndef ASM_SPEC
518 #define ASM_SPEC ""
519 #endif
521 /* config.h can define ASM_FINAL_SPEC to run a post processor after
522 the assembler has run. */
523 #ifndef ASM_FINAL_SPEC
524 #define ASM_FINAL_SPEC ""
525 #endif
527 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
528 or extra switch-translations. */
529 #ifndef CPP_SPEC
530 #define CPP_SPEC ""
531 #endif
533 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
534 or extra switch-translations. */
535 #ifndef CC1_SPEC
536 #define CC1_SPEC ""
537 #endif
539 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
540 or extra switch-translations. */
541 #ifndef CC1PLUS_SPEC
542 #define CC1PLUS_SPEC ""
543 #endif
545 /* config.h can define LINK_SPEC to provide extra args to the linker
546 or extra switch-translations. */
547 #ifndef LINK_SPEC
548 #define LINK_SPEC ""
549 #endif
551 /* config.h can define LIB_SPEC to override the default libraries. */
552 #ifndef LIB_SPEC
553 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
554 #endif
556 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
557 included. */
558 #ifndef LIBGCC_SPEC
559 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
560 /* Have gcc do the search for libgcc.a. */
561 #define LIBGCC_SPEC "libgcc.a%s"
562 #else
563 #define LIBGCC_SPEC "-lgcc"
564 #endif
565 #endif
567 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
568 #ifndef STARTFILE_SPEC
569 #define STARTFILE_SPEC \
570 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
571 #endif
573 /* config.h can define SWITCHES_NEED_SPACES to control which options
574 require spaces between the option and the argument. */
575 #ifndef SWITCHES_NEED_SPACES
576 #define SWITCHES_NEED_SPACES ""
577 #endif
579 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
580 #ifndef ENDFILE_SPEC
581 #define ENDFILE_SPEC ""
582 #endif
584 #ifndef LINKER_NAME
585 #define LINKER_NAME "collect2"
586 #endif
588 /* Define ASM_DEBUG_SPEC to be a spec suitable for translating '-g'
589 to the assembler. */
590 #ifndef ASM_DEBUG_SPEC
591 # if defined(DBX_DEBUGGING_INFO) && defined(DWARF2_DEBUGGING_INFO) \
592 && defined(HAVE_AS_GDWARF2_DEBUG_FLAG) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
593 # define ASM_DEBUG_SPEC \
594 (PREFERRED_DEBUGGING_TYPE == DBX_DEBUG \
595 ? "%{gdwarf-2*:--gdwarf2}%{!gdwarf-2*:%{g*:--gstabs}}" \
596 : "%{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}}")
597 # else
598 # if defined(DBX_DEBUGGING_INFO) && defined(HAVE_AS_GSTABS_DEBUG_FLAG)
599 # define ASM_DEBUG_SPEC "%{g*:--gstabs}"
600 # endif
601 # if defined(DWARF2_DEBUGGING_INFO) && defined(HAVE_AS_GDWARF2_DEBUG_FLAG)
602 # define ASM_DEBUG_SPEC "%{g*:--gdwarf2}"
603 # endif
604 # endif
605 #endif
606 #ifndef ASM_DEBUG_SPEC
607 # define ASM_DEBUG_SPEC ""
608 #endif
610 /* Here is the spec for running the linker, after compiling all files. */
612 /* This is overridable by the target in case they need to specify the
613 -lgcc and -lc order specially, yet not require them to override all
614 of LINK_COMMAND_SPEC. */
615 #ifndef LINK_GCC_C_SEQUENCE_SPEC
616 #define LINK_GCC_C_SEQUENCE_SPEC "%G %L %G"
617 #endif
619 /* -u* was put back because both BSD and SysV seem to support it. */
620 /* %{static:} simply prevents an error message if the target machine
621 doesn't handle -static. */
622 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
623 scripts which exist in user specified directories, or in standard
624 directories. */
625 #ifndef LINK_COMMAND_SPEC
626 #define LINK_COMMAND_SPEC "\
627 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
628 %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\
629 %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
630 %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%(link_gcc_c_sequence)}}\
631 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
632 #endif
634 #ifndef LINK_LIBGCC_SPEC
635 # ifdef LINK_LIBGCC_SPECIAL
636 /* Don't generate -L options for startfile prefix list. */
637 # define LINK_LIBGCC_SPEC ""
638 # else
639 /* Do generate them. */
640 # define LINK_LIBGCC_SPEC "%D"
641 # endif
642 #endif
644 static const char *asm_debug = ASM_DEBUG_SPEC;
645 static const char *cpp_spec = CPP_SPEC;
646 static const char *cpp_predefines = CPP_PREDEFINES;
647 static const char *cc1_spec = CC1_SPEC;
648 static const char *cc1plus_spec = CC1PLUS_SPEC;
649 static const char *link_gcc_c_sequence_spec = LINK_GCC_C_SEQUENCE_SPEC;
650 static const char *asm_spec = ASM_SPEC;
651 static const char *asm_final_spec = ASM_FINAL_SPEC;
652 static const char *link_spec = LINK_SPEC;
653 static const char *lib_spec = LIB_SPEC;
654 static const char *libgcc_spec = LIBGCC_SPEC;
655 static const char *endfile_spec = ENDFILE_SPEC;
656 static const char *startfile_spec = STARTFILE_SPEC;
657 static const char *switches_need_spaces = SWITCHES_NEED_SPACES;
658 static const char *linker_name_spec = LINKER_NAME;
659 static const char *link_command_spec = LINK_COMMAND_SPEC;
660 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
662 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
663 There should be no need to override these in target dependent files,
664 but we need to copy them to the specs file so that newer versions
665 of the GCC driver can correctly drive older tool chains with the
666 appropriate -B options. */
668 static const char *trad_capable_cpp =
669 "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
671 static const char *cpp_unique_options =
672 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
673 %{nostdinc*} %{C} %{v} %{I*} %{P} %{$} %I\
674 %{MD:-MD %W{!o: %b.d}%W{o*:%.d%*}}\
675 %{MMD:-MMD %W{!o: %b.d}%W{o*:%.d%*}}\
676 %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*}\
677 %{!E:%{!M:%{!MM:%{MD|MMD:%{o*:-MQ %*}}}}}\
678 %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
679 %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
680 %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
681 %{fno-inline|O0|!O*:-D__NO_INLINE__} %{ffast-math:-D__FAST_MATH__}\
682 %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
683 %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
684 %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}} %{remap}\
685 %{g3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i\
686 %{E|M|MM:%W{o*}}";
688 /* This contains cpp options which are common with cc1_options and are passed
689 only when preprocessing only to avoid duplication. */
690 static const char *cpp_options =
691 "%(cpp_unique_options) %{std*} %{d*} %{W*} %{w} %{pedantic*}\
692 %{fshow-column} %{fno-show-column}\
693 %{fsigned-char&funsigned-char}\
694 %{fleading-underscore} %{fno-leading-underscore}\
695 %{fno-operator-names} %{ftabstop=*}";
697 /* NB: This is shared amongst all front-ends. */
698 static const char *cc1_options =
699 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
700 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
701 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*} %{ansi}\
702 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
703 %{Qn:-fno-ident} %{--help:--help}\
704 %{--target-help:--target-help}\
705 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
706 %{fsyntax-only:-o %j} %{-param*}";
708 static const char *asm_options =
709 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
711 static const char *invoke_as =
712 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
714 /* Some compilers have limits on line lengths, and the multilib_select
715 and/or multilib_matches strings can be very long, so we build them at
716 run time. */
717 static struct obstack multilib_obstack;
718 static const char *multilib_select;
719 static const char *multilib_matches;
720 static const char *multilib_defaults;
721 static const char *multilib_exclusions;
722 #include "multilib.h"
724 /* Check whether a particular argument is a default argument. */
726 #ifndef MULTILIB_DEFAULTS
727 #define MULTILIB_DEFAULTS { "" }
728 #endif
730 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
732 struct user_specs
734 struct user_specs *next;
735 const char *filename;
738 static struct user_specs *user_specs_head, *user_specs_tail;
740 /* This defines which switch letters take arguments. */
742 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
743 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
744 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
745 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
746 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
747 || (CHAR) == 'B' || (CHAR) == 'b')
749 #ifndef SWITCH_TAKES_ARG
750 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
751 #endif
753 /* This defines which multi-letter switches take arguments. */
755 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
756 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
757 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
758 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
759 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
760 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
761 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
762 || !strcmp (STR, "specs") \
763 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
765 #ifndef WORD_SWITCH_TAKES_ARG
766 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
767 #endif
769 #ifdef HAVE_TARGET_EXECUTABLE_SUFFIX
770 /* This defines which switches stop a full compilation. */
771 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
772 ((CHAR) == 'c' || (CHAR) == 'S')
774 #ifndef SWITCH_CURTAILS_COMPILATION
775 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
776 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
777 #endif
778 #endif
780 /* Record the mapping from file suffixes for compilation specs. */
782 struct compiler
784 const char *suffix; /* Use this compiler for input files
785 whose names end in this suffix. */
787 const char *spec; /* To use this compiler, run this spec. */
789 const char *cpp_spec; /* If non-NULL, substitute this spec
790 for `%C', rather than the usual
791 cpp_spec. */
794 /* Pointer to a vector of `struct compiler' that gives the spec for
795 compiling a file, based on its suffix.
796 A file that does not end in any of these suffixes will be passed
797 unchanged to the loader and nothing else will be done to it.
799 An entry containing two 0s is used to terminate the vector.
801 If multiple entries match a file, the last matching one is used. */
803 static struct compiler *compilers;
805 /* Number of entries in `compilers', not counting the null terminator. */
807 static int n_compilers;
809 /* The default list of file name suffixes and their compilation specs. */
811 static const struct compiler default_compilers[] =
813 /* Add lists of suffixes of known languages here. If those languages
814 were not present when we built the driver, we will hit these copies
815 and be given a more meaningful error than "file not used since
816 linking is not done". */
817 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
818 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
819 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
820 {".ii", "#C++", 0},
821 {".ads", "#Ada", 0}, {".adb", "#Ada", 0},
822 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
823 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
824 {".r", "#Ratfor", 0},
825 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
826 {".java", "#Java", 0}, {".class", "#Java", 0},
827 {".zip", "#Java", 0}, {".jar", "#Java", 0},
828 /* Next come the entries for C. */
829 {".c", "@c", 0},
830 {"@c",
831 /* cc1 has an integrated ISO C preprocessor. We should invoke the
832 external preprocessor if -save-temps or -traditional is given. */
833 "%{E|M|MM:%(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)}\
834 %{!E:%{!M:%{!MM:\
835 %{save-temps|no-integrated-cpp:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
836 %(cpp_options) %{save-temps:%b.i} %{!save-temps:%g.i} \n\
837 cc1 -fpreprocessed %{save-temps:%b.i} %{!save-temps:%g.i} %(cc1_options)}\
838 %{!save-temps:%{!no-integrated-cpp:\
839 %{traditional|ftraditional|traditional-cpp:\
840 tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
841 cc1 -fpreprocessed %{!pipe:%g.i} %(cc1_options)}\
842 %{!traditional:%{!ftraditional:%{!traditional-cpp:\
843 cc1 -lang-c %{ansi:-std=c89} %(cpp_unique_options) %(cc1_options)}}}}}\
844 %{!fsyntax-only:%(invoke_as)}}}}", 0},
845 {"-",
846 "%{!E:%e-E required when input is from standard input}\
847 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
848 {".h", "@c-header", 0},
849 {"@c-header",
850 "%{!E:%ecompilation of header file requested} \
851 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
852 {".i", "@cpp-output", 0},
853 {"@cpp-output",
854 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
855 {".s", "@assembler", 0},
856 {"@assembler",
857 "%{!M:%{!MM:%{!E:%{!S:as %(asm_debug) %(asm_options) %i %A }}}}", 0},
858 {".S", "@assembler-with-cpp", 0},
859 {"@assembler-with-cpp",
860 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
861 %{!M:%{!MM:%{!E:%{!S:-o %{|!pipe:%g.s} |\n\
862 as %(asm_debug) %(asm_options) %{!pipe:%g.s} %A }}}}", 0},
863 #include "specs.h"
864 /* Mark end of table */
865 {0, 0, 0}
868 /* Number of elements in default_compilers, not counting the terminator. */
870 static int n_default_compilers
871 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
873 /* A vector of options to give to the linker.
874 These options are accumulated by %x,
875 and substituted into the linker command with %X. */
876 static int n_linker_options;
877 static char **linker_options;
879 /* A vector of options to give to the assembler.
880 These options are accumulated by -Wa,
881 and substituted into the assembler command with %Y. */
882 static int n_assembler_options;
883 static char **assembler_options;
885 /* A vector of options to give to the preprocessor.
886 These options are accumulated by -Wp,
887 and substituted into the preprocessor command with %Z. */
888 static int n_preprocessor_options;
889 static char **preprocessor_options;
891 /* Define how to map long options into short ones. */
893 /* This structure describes one mapping. */
894 struct option_map
896 /* The long option's name. */
897 const char *const name;
898 /* The equivalent short option. */
899 const char *const equivalent;
900 /* Argument info. A string of flag chars; NULL equals no options.
901 a => argument required.
902 o => argument optional.
903 j => join argument to equivalent, making one word.
904 * => require other text after NAME as an argument. */
905 const char *const arg_info;
908 /* This is the table of mappings. Mappings are tried sequentially
909 for each option encountered; the first one that matches, wins. */
911 static const struct option_map option_map[] =
913 {"--all-warnings", "-Wall", 0},
914 {"--ansi", "-ansi", 0},
915 {"--assemble", "-S", 0},
916 {"--assert", "-A", "a"},
917 {"--classpath", "-fclasspath=", "aj"},
918 {"--bootclasspath", "-fbootclasspath=", "aj"},
919 {"--CLASSPATH", "-fclasspath=", "aj"},
920 {"--comments", "-C", 0},
921 {"--compile", "-c", 0},
922 {"--debug", "-g", "oj"},
923 {"--define-macro", "-D", "aj"},
924 {"--dependencies", "-M", 0},
925 {"--dump", "-d", "a"},
926 {"--dumpbase", "-dumpbase", "a"},
927 {"--entry", "-e", 0},
928 {"--extra-warnings", "-W", 0},
929 {"--for-assembler", "-Wa", "a"},
930 {"--for-linker", "-Xlinker", "a"},
931 {"--force-link", "-u", "a"},
932 {"--imacros", "-imacros", "a"},
933 {"--include", "-include", "a"},
934 {"--include-barrier", "-I-", 0},
935 {"--include-directory", "-I", "aj"},
936 {"--include-directory-after", "-idirafter", "a"},
937 {"--include-prefix", "-iprefix", "a"},
938 {"--include-with-prefix", "-iwithprefix", "a"},
939 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
940 {"--include-with-prefix-after", "-iwithprefix", "a"},
941 {"--language", "-x", "a"},
942 {"--library-directory", "-L", "a"},
943 {"--machine", "-m", "aj"},
944 {"--machine-", "-m", "*j"},
945 {"--no-integrated-cpp", "-no-integrated-cpp", 0},
946 {"--no-line-commands", "-P", 0},
947 {"--no-precompiled-includes", "-noprecomp", 0},
948 {"--no-standard-includes", "-nostdinc", 0},
949 {"--no-standard-libraries", "-nostdlib", 0},
950 {"--no-warnings", "-w", 0},
951 {"--optimize", "-O", "oj"},
952 {"--output", "-o", "a"},
953 {"--output-class-directory", "-foutput-class-dir=", "ja"},
954 {"--param", "--param", "a"},
955 {"--pedantic", "-pedantic", 0},
956 {"--pedantic-errors", "-pedantic-errors", 0},
957 {"--pipe", "-pipe", 0},
958 {"--prefix", "-B", "a"},
959 {"--preprocess", "-E", 0},
960 {"--print-search-dirs", "-print-search-dirs", 0},
961 {"--print-file-name", "-print-file-name=", "aj"},
962 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
963 {"--print-missing-file-dependencies", "-MG", 0},
964 {"--print-multi-lib", "-print-multi-lib", 0},
965 {"--print-multi-directory", "-print-multi-directory", 0},
966 {"--print-prog-name", "-print-prog-name=", "aj"},
967 {"--profile", "-p", 0},
968 {"--profile-blocks", "-a", 0},
969 {"--quiet", "-q", 0},
970 {"--resource", "-fcompile-resource=", "aj"},
971 {"--save-temps", "-save-temps", 0},
972 {"--shared", "-shared", 0},
973 {"--silent", "-q", 0},
974 {"--specs", "-specs=", "aj"},
975 {"--static", "-static", 0},
976 {"--std", "-std=", "aj"},
977 {"--symbolic", "-symbolic", 0},
978 {"--target", "-b", "a"},
979 {"--time", "-time", 0},
980 {"--trace-includes", "-H", 0},
981 {"--traditional", "-traditional", 0},
982 {"--traditional-cpp", "-traditional-cpp", 0},
983 {"--trigraphs", "-trigraphs", 0},
984 {"--undefine-macro", "-U", "aj"},
985 {"--use-version", "-V", "a"},
986 {"--user-dependencies", "-MM", 0},
987 {"--verbose", "-v", 0},
988 {"--warn-", "-W", "*j"},
989 {"--write-dependencies", "-MD", 0},
990 {"--write-user-dependencies", "-MMD", 0},
991 {"--", "-f", "*j"}
995 #ifdef TARGET_OPTION_TRANSLATE_TABLE
996 static const struct {
997 const char *const option_found;
998 const char *const replacements;
999 } target_option_translations[] =
1001 TARGET_OPTION_TRANSLATE_TABLE,
1002 { 0, 0 }
1004 #endif
1006 /* Translate the options described by *ARGCP and *ARGVP.
1007 Make a new vector and store it back in *ARGVP,
1008 and store its length in *ARGVC. */
1010 static void
1011 translate_options (argcp, argvp)
1012 int *argcp;
1013 const char *const **argvp;
1015 int i;
1016 int argc = *argcp;
1017 const char *const *argv = *argvp;
1018 int newvsize = (argc + 2) * 2 * sizeof (const char *);
1019 const char **newv =
1020 (const char **) xmalloc (newvsize);
1021 int newindex = 0;
1023 i = 0;
1024 newv[newindex++] = argv[i++];
1026 while (i < argc)
1028 #ifdef TARGET_OPTION_TRANSLATE_TABLE
1029 int tott_idx;
1031 for (tott_idx = 0;
1032 target_option_translations[tott_idx].option_found;
1033 tott_idx++)
1035 if (strcmp (target_option_translations[tott_idx].option_found,
1036 argv[i]) == 0)
1038 int spaces = 1;
1039 const char *sp;
1040 char *np;
1042 for (sp = target_option_translations[tott_idx].replacements;
1043 *sp; sp++)
1045 if (*sp == ' ')
1046 spaces ++;
1049 newvsize += spaces * sizeof (const char *);
1050 newv = (const char **) xrealloc (newv, newvsize);
1052 sp = target_option_translations[tott_idx].replacements;
1053 np = xstrdup (sp);
1055 while (1)
1057 while (*np == ' ')
1058 np++;
1059 if (*np == 0)
1060 break;
1061 newv[newindex++] = np;
1062 while (*np != ' ' && *np)
1063 np++;
1064 if (*np == 0)
1065 break;
1066 *np++ = 0;
1069 i ++;
1070 break;
1073 if (target_option_translations[tott_idx].option_found)
1074 continue;
1075 #endif
1077 /* Translate -- options. */
1078 if (argv[i][0] == '-' && argv[i][1] == '-')
1080 size_t j;
1081 /* Find a mapping that applies to this option. */
1082 for (j = 0; j < ARRAY_SIZE (option_map); j++)
1084 size_t optlen = strlen (option_map[j].name);
1085 size_t arglen = strlen (argv[i]);
1086 size_t complen = arglen > optlen ? optlen : arglen;
1087 const char *arginfo = option_map[j].arg_info;
1089 if (arginfo == 0)
1090 arginfo = "";
1092 if (!strncmp (argv[i], option_map[j].name, complen))
1094 const char *arg = 0;
1096 if (arglen < optlen)
1098 size_t k;
1099 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
1100 if (strlen (option_map[k].name) >= arglen
1101 && !strncmp (argv[i], option_map[k].name, arglen))
1103 error ("ambiguous abbreviation %s", argv[i]);
1104 break;
1107 if (k != ARRAY_SIZE (option_map))
1108 break;
1111 if (arglen > optlen)
1113 /* If the option has an argument, accept that. */
1114 if (argv[i][optlen] == '=')
1115 arg = argv[i] + optlen + 1;
1117 /* If this mapping requires extra text at end of name,
1118 accept that as "argument". */
1119 else if (strchr (arginfo, '*') != 0)
1120 arg = argv[i] + optlen;
1122 /* Otherwise, extra text at end means mismatch.
1123 Try other mappings. */
1124 else
1125 continue;
1128 else if (strchr (arginfo, '*') != 0)
1130 error ("incomplete `%s' option", option_map[j].name);
1131 break;
1134 /* Handle arguments. */
1135 if (strchr (arginfo, 'a') != 0)
1137 if (arg == 0)
1139 if (i + 1 == argc)
1141 error ("missing argument to `%s' option",
1142 option_map[j].name);
1143 break;
1146 arg = argv[++i];
1149 else if (strchr (arginfo, '*') != 0)
1151 else if (strchr (arginfo, 'o') == 0)
1153 if (arg != 0)
1154 error ("extraneous argument to `%s' option",
1155 option_map[j].name);
1156 arg = 0;
1159 /* Store the translation as one argv elt or as two. */
1160 if (arg != 0 && strchr (arginfo, 'j') != 0)
1161 newv[newindex++] = concat (option_map[j].equivalent, arg,
1162 NULL);
1163 else if (arg != 0)
1165 newv[newindex++] = option_map[j].equivalent;
1166 newv[newindex++] = arg;
1168 else
1169 newv[newindex++] = option_map[j].equivalent;
1171 break;
1174 i++;
1177 /* Handle old-fashioned options--just copy them through,
1178 with their arguments. */
1179 else if (argv[i][0] == '-')
1181 const char *p = argv[i] + 1;
1182 int c = *p;
1183 int nskip = 1;
1185 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1186 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1187 else if (WORD_SWITCH_TAKES_ARG (p))
1188 nskip += WORD_SWITCH_TAKES_ARG (p);
1189 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1190 && p[1] == 0)
1191 nskip += 1;
1192 else if (! strcmp (p, "Xlinker"))
1193 nskip += 1;
1195 /* Watch out for an option at the end of the command line that
1196 is missing arguments, and avoid skipping past the end of the
1197 command line. */
1198 if (nskip + i > argc)
1199 nskip = argc - i;
1201 while (nskip > 0)
1203 newv[newindex++] = argv[i++];
1204 nskip--;
1207 else
1208 /* Ordinary operands, or +e options. */
1209 newv[newindex++] = argv[i++];
1212 newv[newindex] = 0;
1214 *argvp = newv;
1215 *argcp = newindex;
1218 static char *
1219 skip_whitespace (p)
1220 char *p;
1222 while (1)
1224 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1225 be considered whitespace. */
1226 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1227 return p + 1;
1228 else if (*p == '\n' || *p == ' ' || *p == '\t')
1229 p++;
1230 else if (*p == '#')
1232 while (*p != '\n')
1233 p++;
1234 p++;
1236 else
1237 break;
1240 return p;
1242 /* Structures to keep track of prefixes to try when looking for files. */
1244 struct prefix_list
1246 const char *prefix; /* String to prepend to the path. */
1247 struct prefix_list *next; /* Next in linked list. */
1248 int require_machine_suffix; /* Don't use without machine_suffix. */
1249 /* 2 means try both machine_suffix and just_machine_suffix. */
1250 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1251 int priority; /* Sort key - priority within list */
1254 struct path_prefix
1256 struct prefix_list *plist; /* List of prefixes to try */
1257 int max_len; /* Max length of a prefix in PLIST */
1258 const char *name; /* Name of this list (used in config stuff) */
1261 /* List of prefixes to try when looking for executables. */
1263 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1265 /* List of prefixes to try when looking for startup (crt0) files. */
1267 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1269 /* List of prefixes to try when looking for include files. */
1271 static struct path_prefix include_prefixes = { 0, 0, "include" };
1273 /* Suffix to attach to directories searched for commands.
1274 This looks like `MACHINE/VERSION/'. */
1276 static const char *machine_suffix = 0;
1278 /* Suffix to attach to directories searched for commands.
1279 This is just `MACHINE/'. */
1281 static const char *just_machine_suffix = 0;
1283 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1285 static const char *gcc_exec_prefix;
1287 /* Default prefixes to attach to command names. */
1289 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1290 #undef MD_EXEC_PREFIX
1291 #undef MD_STARTFILE_PREFIX
1292 #undef MD_STARTFILE_PREFIX_1
1293 #endif
1295 /* If no prefixes defined, use the null string, which will disable them. */
1296 #ifndef MD_EXEC_PREFIX
1297 #define MD_EXEC_PREFIX ""
1298 #endif
1299 #ifndef MD_STARTFILE_PREFIX
1300 #define MD_STARTFILE_PREFIX ""
1301 #endif
1302 #ifndef MD_STARTFILE_PREFIX_1
1303 #define MD_STARTFILE_PREFIX_1 ""
1304 #endif
1306 /* Supply defaults for the standard prefixes. */
1308 #ifndef STANDARD_EXEC_PREFIX
1309 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1310 #endif
1311 #ifndef STANDARD_STARTFILE_PREFIX
1312 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1313 #endif
1314 #ifndef TOOLDIR_BASE_PREFIX
1315 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1316 #endif
1317 #ifndef STANDARD_BINDIR_PREFIX
1318 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1319 #endif
1321 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1322 static const char *const standard_exec_prefix_1 = "/usr/lib/gcc/";
1323 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1325 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1326 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1327 static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1328 static const char *const standard_startfile_prefix_1 = "/lib/";
1329 static const char *const standard_startfile_prefix_2 = "/usr/lib/";
1331 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1332 static const char *tooldir_prefix;
1334 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1336 /* Subdirectory to use for locating libraries. Set by
1337 set_multilib_dir based on the compilation options. */
1339 static const char *multilib_dir;
1341 /* Structure to keep track of the specs that have been defined so far.
1342 These are accessed using %(specname) or %[specname] in a compiler
1343 or link spec. */
1345 struct spec_list
1347 /* The following 2 fields must be first */
1348 /* to allow EXTRA_SPECS to be initialized */
1349 const char *name; /* name of the spec. */
1350 const char *ptr; /* available ptr if no static pointer */
1352 /* The following fields are not initialized */
1353 /* by EXTRA_SPECS */
1354 const char **ptr_spec; /* pointer to the spec itself. */
1355 struct spec_list *next; /* Next spec in linked list. */
1356 int name_len; /* length of the name */
1357 int alloc_p; /* whether string was allocated */
1360 #define INIT_STATIC_SPEC(NAME,PTR) \
1361 { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1363 /* List of statically defined specs. */
1364 static struct spec_list static_specs[] =
1366 INIT_STATIC_SPEC ("asm", &asm_spec),
1367 INIT_STATIC_SPEC ("asm_debug", &asm_debug),
1368 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1369 INIT_STATIC_SPEC ("asm_options", &asm_options),
1370 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1371 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1372 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1373 INIT_STATIC_SPEC ("cpp_unique_options", &cpp_unique_options),
1374 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1375 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1376 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1377 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1378 INIT_STATIC_SPEC ("link_gcc_c_sequence", &link_gcc_c_sequence_spec),
1379 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1380 INIT_STATIC_SPEC ("link", &link_spec),
1381 INIT_STATIC_SPEC ("lib", &lib_spec),
1382 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1383 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1384 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1385 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1386 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1387 INIT_STATIC_SPEC ("version", &compiler_version),
1388 INIT_STATIC_SPEC ("multilib", &multilib_select),
1389 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1390 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1391 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1392 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1393 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1394 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1395 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1396 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1397 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1400 #ifdef EXTRA_SPECS /* additional specs needed */
1401 /* Structure to keep track of just the first two args of a spec_list.
1402 That is all that the EXTRA_SPECS macro gives us. */
1403 struct spec_list_1
1405 const char *const name;
1406 const char *const ptr;
1409 static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1410 static struct spec_list *extra_specs = (struct spec_list *) 0;
1411 #endif
1413 /* List of dynamically allocates specs that have been defined so far. */
1415 static struct spec_list *specs = (struct spec_list *) 0;
1417 /* Add appropriate libgcc specs to OBSTACK, taking into account
1418 various permutations of -shared-libgcc, -shared, and such. */
1420 #ifdef ENABLE_SHARED_LIBGCC
1421 static void
1422 init_gcc_specs (obstack, shared_name, static_name, eh_name)
1423 struct obstack *obstack;
1424 const char *shared_name;
1425 const char *static_name;
1426 const char *eh_name;
1428 char *buf;
1430 buf = concat ("%{static|static-libgcc:", static_name, " ", eh_name,
1431 "}%{!static:%{!static-libgcc:",
1432 "%{!shared:%{!shared-libgcc:", static_name, " ",
1433 eh_name, "}%{shared-libgcc:", shared_name, " ",
1434 static_name, "}}%{shared:",
1435 #ifdef LINK_EH_SPEC
1436 "%{shared-libgcc:", shared_name,
1437 "}%{!shared-libgcc:", static_name, "}",
1438 #else
1439 shared_name,
1440 #endif
1441 "}}}", NULL);
1443 obstack_grow (obstack, buf, strlen (buf));
1444 free (buf);
1446 #endif /* ENABLE_SHARED_LIBGCC */
1448 /* Initialize the specs lookup routines. */
1450 static void
1451 init_spec ()
1453 struct spec_list *next = (struct spec_list *) 0;
1454 struct spec_list *sl = (struct spec_list *) 0;
1455 int i;
1457 if (specs)
1458 return; /* Already initialized. */
1460 if (verbose_flag)
1461 notice ("Using built-in specs.\n");
1463 #ifdef EXTRA_SPECS
1464 extra_specs = (struct spec_list *)
1465 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1467 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1469 sl = &extra_specs[i];
1470 sl->name = extra_specs_1[i].name;
1471 sl->ptr = extra_specs_1[i].ptr;
1472 sl->next = next;
1473 sl->name_len = strlen (sl->name);
1474 sl->ptr_spec = &sl->ptr;
1475 next = sl;
1477 #endif
1479 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1481 sl = &static_specs[i];
1482 sl->next = next;
1483 next = sl;
1486 #ifdef ENABLE_SHARED_LIBGCC
1487 /* ??? If neither -shared-libgcc nor --static-libgcc was
1488 seen, then we should be making an educated guess. Some proposed
1489 heuristics for ELF include:
1491 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1492 program will be doing dynamic loading, which will likely
1493 need the shared libgcc.
1495 (2) If "-ldl", then it's also a fair bet that we're doing
1496 dynamic loading.
1498 (3) For each ET_DYN we're linking against (either through -lfoo
1499 or /some/path/foo.so), check to see whether it or one of
1500 its dependencies depends on a shared libgcc.
1502 (4) If "-shared"
1504 If the runtime is fixed to look for program headers instead
1505 of calling __register_frame_info at all, for each object,
1506 use the shared libgcc if any EH symbol referenced.
1508 If crtstuff is fixed to not invoke __register_frame_info
1509 automatically, for each object, use the shared libgcc if
1510 any non-empty unwind section found.
1512 Doing any of this probably requires invoking an external program to
1513 do the actual object file scanning. */
1515 const char *p = libgcc_spec;
1516 int in_sep = 1;
1518 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1519 when given the proper command line arguments. */
1520 while (*p)
1522 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1524 init_gcc_specs (&obstack,
1525 #ifdef NO_SHARED_LIBGCC_MULTILIB
1526 "-lgcc_s"
1527 #else
1528 "-lgcc_s%M"
1529 #endif
1531 "-lgcc",
1532 "-lgcc_eh");
1533 p += 5;
1534 in_sep = 0;
1536 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1538 /* Ug. We don't know shared library extensions. Hope that
1539 systems that use this form don't do shared libraries. */
1540 init_gcc_specs (&obstack,
1541 #ifdef NO_SHARED_LIBGCC_MULTILIB
1542 "-lgcc_s"
1543 #else
1544 "-lgcc_s%M"
1545 #endif
1547 "libgcc.a%s",
1548 "libgcc_eh.a%s");
1549 p += 10;
1550 in_sep = 0;
1552 else
1554 obstack_1grow (&obstack, *p);
1555 in_sep = (*p == ' ');
1556 p += 1;
1560 obstack_1grow (&obstack, '\0');
1561 libgcc_spec = obstack_finish (&obstack);
1563 #endif
1564 #ifdef USE_AS_TRADITIONAL_FORMAT
1565 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1567 static const char tf[] = "--traditional-format ";
1568 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1569 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1570 asm_spec = obstack_finish (&obstack);
1572 #endif
1573 #ifdef LINK_EH_SPEC
1574 /* Prepend LINK_EH_SPEC to whatever link_spec we had before. */
1575 obstack_grow (&obstack, LINK_EH_SPEC, sizeof(LINK_EH_SPEC) - 1);
1576 obstack_grow0 (&obstack, link_spec, strlen (link_spec));
1577 link_spec = obstack_finish (&obstack);
1578 #endif
1580 specs = sl;
1583 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1584 removed; If the spec starts with a + then SPEC is added to the end of the
1585 current spec. */
1587 static void
1588 set_spec (name, spec)
1589 const char *name;
1590 const char *spec;
1592 struct spec_list *sl;
1593 const char *old_spec;
1594 int name_len = strlen (name);
1595 int i;
1597 /* If this is the first call, initialize the statically allocated specs. */
1598 if (!specs)
1600 struct spec_list *next = (struct spec_list *) 0;
1601 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1603 sl = &static_specs[i];
1604 sl->next = next;
1605 next = sl;
1607 specs = sl;
1610 /* See if the spec already exists. */
1611 for (sl = specs; sl; sl = sl->next)
1612 if (name_len == sl->name_len && !strcmp (sl->name, name))
1613 break;
1615 if (!sl)
1617 /* Not found - make it. */
1618 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1619 sl->name = xstrdup (name);
1620 sl->name_len = name_len;
1621 sl->ptr_spec = &sl->ptr;
1622 sl->alloc_p = 0;
1623 *(sl->ptr_spec) = "";
1624 sl->next = specs;
1625 specs = sl;
1628 old_spec = *(sl->ptr_spec);
1629 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1630 ? concat (old_spec, spec + 1, NULL)
1631 : xstrdup (spec));
1633 #ifdef DEBUG_SPECS
1634 if (verbose_flag)
1635 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1636 #endif
1638 /* Free the old spec. */
1639 if (old_spec && sl->alloc_p)
1640 free ((PTR) old_spec);
1642 sl->alloc_p = 1;
1645 /* Accumulate a command (program name and args), and run it. */
1647 /* Vector of pointers to arguments in the current line of specifications. */
1649 static const char **argbuf;
1651 /* Number of elements allocated in argbuf. */
1653 static int argbuf_length;
1655 /* Number of elements in argbuf currently in use (containing args). */
1657 static int argbuf_index;
1659 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1660 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1661 it here. */
1663 static struct temp_name {
1664 const char *suffix; /* suffix associated with the code. */
1665 int length; /* strlen (suffix). */
1666 int unique; /* Indicates whether %g or %u/%U was used. */
1667 const char *filename; /* associated filename. */
1668 int filename_length; /* strlen (filename). */
1669 struct temp_name *next;
1670 } *temp_names;
1672 /* Number of commands executed so far. */
1674 static int execution_count;
1676 /* Number of commands that exited with a signal. */
1678 static int signal_count;
1680 /* Name with which this program was invoked. */
1682 static const char *programname;
1684 /* Clear out the vector of arguments (after a command is executed). */
1686 static void
1687 clear_args ()
1689 argbuf_index = 0;
1692 /* Add one argument to the vector at the end.
1693 This is done when a space is seen or at the end of the line.
1694 If DELETE_ALWAYS is nonzero, the arg is a filename
1695 and the file should be deleted eventually.
1696 If DELETE_FAILURE is nonzero, the arg is a filename
1697 and the file should be deleted if this compilation fails. */
1699 static void
1700 store_arg (arg, delete_always, delete_failure)
1701 const char *arg;
1702 int delete_always, delete_failure;
1704 if (argbuf_index + 1 == argbuf_length)
1705 argbuf
1706 = (const char **) xrealloc (argbuf,
1707 (argbuf_length *= 2) * sizeof (const char *));
1709 argbuf[argbuf_index++] = arg;
1710 argbuf[argbuf_index] = 0;
1712 if (delete_always || delete_failure)
1713 record_temp_file (arg, delete_always, delete_failure);
1716 /* Load specs from a file name named FILENAME, replacing occurrences of
1717 various different types of line-endings, \r\n, \n\r and just \r, with
1718 a single \n. */
1720 static char *
1721 load_specs (filename)
1722 const char *filename;
1724 int desc;
1725 int readlen;
1726 struct stat statbuf;
1727 char *buffer;
1728 char *buffer_p;
1729 char *specs;
1730 char *specs_p;
1732 if (verbose_flag)
1733 notice ("Reading specs from %s\n", filename);
1735 /* Open and stat the file. */
1736 desc = open (filename, O_RDONLY, 0);
1737 if (desc < 0)
1738 pfatal_with_name (filename);
1739 if (stat (filename, &statbuf) < 0)
1740 pfatal_with_name (filename);
1742 /* Read contents of file into BUFFER. */
1743 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1744 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1745 if (readlen < 0)
1746 pfatal_with_name (filename);
1747 buffer[readlen] = 0;
1748 close (desc);
1750 specs = xmalloc (readlen + 1);
1751 specs_p = specs;
1752 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1754 int skip = 0;
1755 char c = *buffer_p;
1756 if (c == '\r')
1758 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1759 skip = 1;
1760 else if (*(buffer_p + 1) == '\n') /* \r\n */
1761 skip = 1;
1762 else /* \r */
1763 c = '\n';
1765 if (! skip)
1766 *specs_p++ = c;
1768 *specs_p = '\0';
1770 free (buffer);
1771 return (specs);
1774 /* Read compilation specs from a file named FILENAME,
1775 replacing the default ones.
1777 A suffix which starts with `*' is a definition for
1778 one of the machine-specific sub-specs. The "suffix" should be
1779 *asm, *cc1, *cpp, *link, *startfile, etc.
1780 The corresponding spec is stored in asm_spec, etc.,
1781 rather than in the `compilers' vector.
1783 Anything invalid in the file is a fatal error. */
1785 static void
1786 read_specs (filename, main_p)
1787 const char *filename;
1788 int main_p;
1790 char *buffer;
1791 char *p;
1793 buffer = load_specs (filename);
1795 /* Scan BUFFER for specs, putting them in the vector. */
1796 p = buffer;
1797 while (1)
1799 char *suffix;
1800 char *spec;
1801 char *in, *out, *p1, *p2, *p3;
1803 /* Advance P in BUFFER to the next nonblank nocomment line. */
1804 p = skip_whitespace (p);
1805 if (*p == 0)
1806 break;
1808 /* Is this a special command that starts with '%'? */
1809 /* Don't allow this for the main specs file, since it would
1810 encourage people to overwrite it. */
1811 if (*p == '%' && !main_p)
1813 p1 = p;
1814 while (*p && *p != '\n')
1815 p++;
1817 /* Skip '\n'. */
1818 p++;
1820 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1821 && (p1[sizeof "%include" - 1] == ' '
1822 || p1[sizeof "%include" - 1] == '\t'))
1824 char *new_filename;
1826 p1 += sizeof ("%include");
1827 while (*p1 == ' ' || *p1 == '\t')
1828 p1++;
1830 if (*p1++ != '<' || p[-2] != '>')
1831 fatal ("specs %%include syntax malformed after %ld characters",
1832 (long) (p1 - buffer + 1));
1834 p[-2] = '\0';
1835 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1836 read_specs (new_filename ? new_filename : p1, FALSE);
1837 continue;
1839 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1840 && (p1[sizeof "%include_noerr" - 1] == ' '
1841 || p1[sizeof "%include_noerr" - 1] == '\t'))
1843 char *new_filename;
1845 p1 += sizeof "%include_noerr";
1846 while (*p1 == ' ' || *p1 == '\t')
1847 p1++;
1849 if (*p1++ != '<' || p[-2] != '>')
1850 fatal ("specs %%include syntax malformed after %ld characters",
1851 (long) (p1 - buffer + 1));
1853 p[-2] = '\0';
1854 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1855 if (new_filename)
1856 read_specs (new_filename, FALSE);
1857 else if (verbose_flag)
1858 notice ("could not find specs file %s\n", p1);
1859 continue;
1861 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1862 && (p1[sizeof "%rename" - 1] == ' '
1863 || p1[sizeof "%rename" - 1] == '\t'))
1865 int name_len;
1866 struct spec_list *sl;
1868 /* Get original name */
1869 p1 += sizeof "%rename";
1870 while (*p1 == ' ' || *p1 == '\t')
1871 p1++;
1873 if (! ISALPHA ((unsigned char) *p1))
1874 fatal ("specs %%rename syntax malformed after %ld characters",
1875 (long) (p1 - buffer));
1877 p2 = p1;
1878 while (*p2 && !ISSPACE ((unsigned char) *p2))
1879 p2++;
1881 if (*p2 != ' ' && *p2 != '\t')
1882 fatal ("specs %%rename syntax malformed after %ld characters",
1883 (long) (p2 - buffer));
1885 name_len = p2 - p1;
1886 *p2++ = '\0';
1887 while (*p2 == ' ' || *p2 == '\t')
1888 p2++;
1890 if (! ISALPHA ((unsigned char) *p2))
1891 fatal ("specs %%rename syntax malformed after %ld characters",
1892 (long) (p2 - buffer));
1894 /* Get new spec name. */
1895 p3 = p2;
1896 while (*p3 && !ISSPACE ((unsigned char) *p3))
1897 p3++;
1899 if (p3 != p - 1)
1900 fatal ("specs %%rename syntax malformed after %ld characters",
1901 (long) (p3 - buffer));
1902 *p3 = '\0';
1904 for (sl = specs; sl; sl = sl->next)
1905 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1906 break;
1908 if (!sl)
1909 fatal ("specs %s spec was not found to be renamed", p1);
1911 if (strcmp (p1, p2) == 0)
1912 continue;
1914 if (verbose_flag)
1916 notice ("rename spec %s to %s\n", p1, p2);
1917 #ifdef DEBUG_SPECS
1918 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1919 #endif
1922 set_spec (p2, *(sl->ptr_spec));
1923 if (sl->alloc_p)
1924 free ((PTR) *(sl->ptr_spec));
1926 *(sl->ptr_spec) = "";
1927 sl->alloc_p = 0;
1928 continue;
1930 else
1931 fatal ("specs unknown %% command after %ld characters",
1932 (long) (p1 - buffer));
1935 /* Find the colon that should end the suffix. */
1936 p1 = p;
1937 while (*p1 && *p1 != ':' && *p1 != '\n')
1938 p1++;
1940 /* The colon shouldn't be missing. */
1941 if (*p1 != ':')
1942 fatal ("specs file malformed after %ld characters",
1943 (long) (p1 - buffer));
1945 /* Skip back over trailing whitespace. */
1946 p2 = p1;
1947 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1948 p2--;
1950 /* Copy the suffix to a string. */
1951 suffix = save_string (p, p2 - p);
1952 /* Find the next line. */
1953 p = skip_whitespace (p1 + 1);
1954 if (p[1] == 0)
1955 fatal ("specs file malformed after %ld characters",
1956 (long) (p - buffer));
1958 p1 = p;
1959 /* Find next blank line or end of string. */
1960 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1961 p1++;
1963 /* Specs end at the blank line and do not include the newline. */
1964 spec = save_string (p, p1 - p);
1965 p = p1;
1967 /* Delete backslash-newline sequences from the spec. */
1968 in = spec;
1969 out = spec;
1970 while (*in != 0)
1972 if (in[0] == '\\' && in[1] == '\n')
1973 in += 2;
1974 else if (in[0] == '#')
1975 while (*in && *in != '\n')
1976 in++;
1978 else
1979 *out++ = *in++;
1981 *out = 0;
1983 if (suffix[0] == '*')
1985 if (! strcmp (suffix, "*link_command"))
1986 link_command_spec = spec;
1987 else
1988 set_spec (suffix + 1, spec);
1990 else
1992 /* Add this pair to the vector. */
1993 compilers
1994 = ((struct compiler *)
1995 xrealloc (compilers,
1996 (n_compilers + 2) * sizeof (struct compiler)));
1998 compilers[n_compilers].suffix = suffix;
1999 compilers[n_compilers].spec = spec;
2000 n_compilers++;
2001 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
2004 if (*suffix == 0)
2005 link_command_spec = spec;
2008 if (link_command_spec == 0)
2009 fatal ("spec file has no spec for linking");
2012 /* Record the names of temporary files we tell compilers to write,
2013 and delete them at the end of the run. */
2015 /* This is the common prefix we use to make temp file names.
2016 It is chosen once for each run of this program.
2017 It is substituted into a spec by %g or %j.
2018 Thus, all temp file names contain this prefix.
2019 In practice, all temp file names start with this prefix.
2021 This prefix comes from the envvar TMPDIR if it is defined;
2022 otherwise, from the P_tmpdir macro if that is defined;
2023 otherwise, in /usr/tmp or /tmp;
2024 or finally the current directory if all else fails. */
2026 static const char *temp_filename;
2028 /* Length of the prefix. */
2030 static int temp_filename_length;
2032 /* Define the list of temporary files to delete. */
2034 struct temp_file
2036 const char *name;
2037 struct temp_file *next;
2040 /* Queue of files to delete on success or failure of compilation. */
2041 static struct temp_file *always_delete_queue;
2042 /* Queue of files to delete on failure of compilation. */
2043 static struct temp_file *failure_delete_queue;
2045 /* Record FILENAME as a file to be deleted automatically.
2046 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
2047 otherwise delete it in any case.
2048 FAIL_DELETE nonzero means delete it if a compilation step fails;
2049 otherwise delete it in any case. */
2051 void
2052 record_temp_file (filename, always_delete, fail_delete)
2053 const char *filename;
2054 int always_delete;
2055 int fail_delete;
2057 char *const name = xstrdup (filename);
2059 if (always_delete)
2061 struct temp_file *temp;
2062 for (temp = always_delete_queue; temp; temp = temp->next)
2063 if (! strcmp (name, temp->name))
2064 goto already1;
2066 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2067 temp->next = always_delete_queue;
2068 temp->name = name;
2069 always_delete_queue = temp;
2071 already1:;
2074 if (fail_delete)
2076 struct temp_file *temp;
2077 for (temp = failure_delete_queue; temp; temp = temp->next)
2078 if (! strcmp (name, temp->name))
2079 goto already2;
2081 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2082 temp->next = failure_delete_queue;
2083 temp->name = name;
2084 failure_delete_queue = temp;
2086 already2:;
2090 /* Delete all the temporary files whose names we previously recorded. */
2092 static void
2093 delete_if_ordinary (name)
2094 const char *name;
2096 struct stat st;
2097 #ifdef DEBUG
2098 int i, c;
2100 printf ("Delete %s? (y or n) ", name);
2101 fflush (stdout);
2102 i = getchar ();
2103 if (i != '\n')
2104 while ((c = getchar ()) != '\n' && c != EOF)
2107 if (i == 'y' || i == 'Y')
2108 #endif /* DEBUG */
2109 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
2110 if (unlink (name) < 0)
2111 if (verbose_flag)
2112 perror_with_name (name);
2115 static void
2116 delete_temp_files ()
2118 struct temp_file *temp;
2120 for (temp = always_delete_queue; temp; temp = temp->next)
2121 delete_if_ordinary (temp->name);
2122 always_delete_queue = 0;
2125 /* Delete all the files to be deleted on error. */
2127 static void
2128 delete_failure_queue ()
2130 struct temp_file *temp;
2132 for (temp = failure_delete_queue; temp; temp = temp->next)
2133 delete_if_ordinary (temp->name);
2136 static void
2137 clear_failure_queue ()
2139 failure_delete_queue = 0;
2142 /* Build a list of search directories from PATHS.
2143 PREFIX is a string to prepend to the list.
2144 If CHECK_DIR_P is non-zero we ensure the directory exists.
2145 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2146 It is also used by the --print-search-dirs flag. */
2148 static char *
2149 build_search_list (paths, prefix, check_dir_p)
2150 struct path_prefix *paths;
2151 const char *prefix;
2152 int check_dir_p;
2154 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2155 int just_suffix_len
2156 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2157 int first_time = TRUE;
2158 struct prefix_list *pprefix;
2160 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2161 obstack_1grow (&collect_obstack, '=');
2163 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2165 int len = strlen (pprefix->prefix);
2167 if (machine_suffix
2168 && (! check_dir_p
2169 || is_directory (pprefix->prefix, machine_suffix, 0)))
2171 if (!first_time)
2172 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2174 first_time = FALSE;
2175 obstack_grow (&collect_obstack, pprefix->prefix, len);
2176 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2179 if (just_machine_suffix
2180 && pprefix->require_machine_suffix == 2
2181 && (! check_dir_p
2182 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2184 if (! first_time)
2185 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2187 first_time = FALSE;
2188 obstack_grow (&collect_obstack, pprefix->prefix, len);
2189 obstack_grow (&collect_obstack, just_machine_suffix,
2190 just_suffix_len);
2193 if (! pprefix->require_machine_suffix)
2195 if (! first_time)
2196 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2198 first_time = FALSE;
2199 obstack_grow (&collect_obstack, pprefix->prefix, len);
2203 obstack_1grow (&collect_obstack, '\0');
2204 return obstack_finish (&collect_obstack);
2207 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2208 for collect. */
2210 static void
2211 putenv_from_prefixes (paths, env_var)
2212 struct path_prefix *paths;
2213 const char *env_var;
2215 putenv (build_search_list (paths, env_var, 1));
2218 #ifndef VMS
2220 /* FIXME: the location independence code for VMS is hairier than this,
2221 and hasn't been written. */
2223 /* Split a filename into component directories. */
2225 static char **
2226 split_directories (name, ptr_num_dirs)
2227 const char *name;
2228 int *ptr_num_dirs;
2230 int num_dirs = 0;
2231 char **dirs;
2232 const char *p, *q;
2233 int ch;
2235 /* Count the number of directories. Special case MSDOS disk names as part
2236 of the initial directory. */
2237 p = name;
2238 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2239 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2241 p += 3;
2242 num_dirs++;
2244 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2246 while ((ch = *p++) != '\0')
2248 if (IS_DIR_SEPARATOR (ch))
2250 num_dirs++;
2251 while (IS_DIR_SEPARATOR (*p))
2252 p++;
2256 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2258 /* Now copy the directory parts. */
2259 num_dirs = 0;
2260 p = name;
2261 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2262 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2264 dirs[num_dirs++] = save_string (p, 3);
2265 p += 3;
2267 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2269 q = p;
2270 while ((ch = *p++) != '\0')
2272 if (IS_DIR_SEPARATOR (ch))
2274 while (IS_DIR_SEPARATOR (*p))
2275 p++;
2277 dirs[num_dirs++] = save_string (q, p - q);
2278 q = p;
2282 if (p - 1 - q > 0)
2283 dirs[num_dirs++] = save_string (q, p - 1 - q);
2285 dirs[num_dirs] = NULL;
2286 if (ptr_num_dirs)
2287 *ptr_num_dirs = num_dirs;
2289 return dirs;
2292 /* Release storage held by split directories. */
2294 static void
2295 free_split_directories (dirs)
2296 char **dirs;
2298 int i = 0;
2300 while (dirs[i] != NULL)
2301 free (dirs[i++]);
2303 free ((char *) dirs);
2306 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2307 to PREFIX starting with the directory portion of PROGNAME and a relative
2308 pathname of the difference between BIN_PREFIX and PREFIX.
2310 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2311 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2312 function will return /red/green/blue/../omega.
2314 If no relative prefix can be found, return NULL. */
2316 static char *
2317 make_relative_prefix (progname, bin_prefix, prefix)
2318 const char *progname;
2319 const char *bin_prefix;
2320 const char *prefix;
2322 char **prog_dirs, **bin_dirs, **prefix_dirs;
2323 int prog_num, bin_num, prefix_num, std_loc_p;
2324 int i, n, common;
2326 prog_dirs = split_directories (progname, &prog_num);
2327 bin_dirs = split_directories (bin_prefix, &bin_num);
2329 /* If there is no full pathname, try to find the program by checking in each
2330 of the directories specified in the PATH environment variable. */
2331 if (prog_num == 1)
2333 char *temp;
2335 GET_ENV_PATH_LIST (temp, "PATH");
2336 if (temp)
2338 char *startp, *endp, *nstore;
2339 size_t prefixlen = strlen (temp) + 1;
2340 if (prefixlen < 2)
2341 prefixlen = 2;
2343 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
2345 startp = endp = temp;
2346 while (1)
2348 if (*endp == PATH_SEPARATOR || *endp == 0)
2350 if (endp == startp)
2352 nstore[0] = '.';
2353 nstore[1] = DIR_SEPARATOR;
2354 nstore[2] = '\0';
2356 else
2358 strncpy (nstore, startp, endp - startp);
2359 if (! IS_DIR_SEPARATOR (endp[-1]))
2361 nstore[endp - startp] = DIR_SEPARATOR;
2362 nstore[endp - startp + 1] = 0;
2364 else
2365 nstore[endp - startp] = 0;
2367 strcat (nstore, progname);
2368 if (! access (nstore, X_OK)
2369 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
2370 || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
2371 #endif
2374 free_split_directories (prog_dirs);
2375 progname = nstore;
2376 prog_dirs = split_directories (progname, &prog_num);
2377 break;
2380 if (*endp == 0)
2381 break;
2382 endp = startp = endp + 1;
2384 else
2385 endp++;
2390 /* Remove the program name from comparison of directory names. */
2391 prog_num--;
2393 /* Determine if the compiler is installed in the standard location, and if
2394 so, we don't need to specify relative directories. Also, if argv[0]
2395 doesn't contain any directory specifiers, there is not much we can do. */
2396 std_loc_p = 0;
2397 if (prog_num == bin_num)
2399 for (i = 0; i < bin_num; i++)
2401 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2402 break;
2405 if (prog_num <= 0 || i == bin_num)
2407 std_loc_p = 1;
2408 free_split_directories (prog_dirs);
2409 free_split_directories (bin_dirs);
2410 prog_dirs = bin_dirs = (char **) 0;
2411 return NULL;
2415 prefix_dirs = split_directories (prefix, &prefix_num);
2417 /* Find how many directories are in common between bin_prefix & prefix. */
2418 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2419 for (common = 0; common < n; common++)
2421 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2422 break;
2425 /* If there are no common directories, there can be no relative prefix. */
2426 if (common == 0)
2428 free_split_directories (prog_dirs);
2429 free_split_directories (bin_dirs);
2430 free_split_directories (prefix_dirs);
2431 return NULL;
2434 /* Build up the pathnames in argv[0]. */
2435 for (i = 0; i < prog_num; i++)
2436 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2438 /* Now build up the ..'s. */
2439 for (i = common; i < n; i++)
2441 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2442 obstack_1grow (&obstack, DIR_SEPARATOR);
2445 /* Put in directories to move over to prefix. */
2446 for (i = common; i < prefix_num; i++)
2447 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2449 free_split_directories (prog_dirs);
2450 free_split_directories (bin_dirs);
2451 free_split_directories (prefix_dirs);
2453 obstack_1grow (&obstack, '\0');
2454 return obstack_finish (&obstack);
2456 #endif /* VMS */
2458 /* Check whether NAME can be accessed in MODE. This is like access,
2459 except that it never considers directories to be executable. */
2461 static int
2462 access_check (name, mode)
2463 const char *name;
2464 int mode;
2466 if (mode == X_OK)
2468 struct stat st;
2470 if (stat (name, &st) < 0
2471 || S_ISDIR (st.st_mode))
2472 return -1;
2475 return access (name, mode);
2478 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2479 access to check permissions.
2480 Return 0 if not found, otherwise return its name, allocated with malloc. */
2482 static char *
2483 find_a_file (pprefix, name, mode)
2484 struct path_prefix *pprefix;
2485 const char *name;
2486 int mode;
2488 char *temp;
2489 const char *const file_suffix =
2490 ((mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "");
2491 struct prefix_list *pl;
2492 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2494 #ifdef DEFAULT_ASSEMBLER
2495 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2496 return xstrdup (DEFAULT_ASSEMBLER);
2497 #endif
2499 #ifdef DEFAULT_LINKER
2500 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2501 return xstrdup (DEFAULT_LINKER);
2502 #endif
2504 if (machine_suffix)
2505 len += strlen (machine_suffix);
2507 temp = xmalloc (len);
2509 /* Determine the filename to execute (special case for absolute paths). */
2511 if (IS_ABSOLUTE_PATHNAME (name))
2513 if (access (name, mode) == 0)
2515 strcpy (temp, name);
2516 return temp;
2519 else
2520 for (pl = pprefix->plist; pl; pl = pl->next)
2522 if (machine_suffix)
2524 /* Some systems have a suffix for executable files.
2525 So try appending that first. */
2526 if (file_suffix[0] != 0)
2528 strcpy (temp, pl->prefix);
2529 strcat (temp, machine_suffix);
2530 strcat (temp, name);
2531 strcat (temp, file_suffix);
2532 if (access_check (temp, mode) == 0)
2534 if (pl->used_flag_ptr != 0)
2535 *pl->used_flag_ptr = 1;
2536 return temp;
2540 /* Now try just the name. */
2541 strcpy (temp, pl->prefix);
2542 strcat (temp, machine_suffix);
2543 strcat (temp, name);
2544 if (access_check (temp, mode) == 0)
2546 if (pl->used_flag_ptr != 0)
2547 *pl->used_flag_ptr = 1;
2548 return temp;
2552 /* Certain prefixes are tried with just the machine type,
2553 not the version. This is used for finding as, ld, etc. */
2554 if (just_machine_suffix && pl->require_machine_suffix == 2)
2556 /* Some systems have a suffix for executable files.
2557 So try appending that first. */
2558 if (file_suffix[0] != 0)
2560 strcpy (temp, pl->prefix);
2561 strcat (temp, just_machine_suffix);
2562 strcat (temp, name);
2563 strcat (temp, file_suffix);
2564 if (access_check (temp, mode) == 0)
2566 if (pl->used_flag_ptr != 0)
2567 *pl->used_flag_ptr = 1;
2568 return temp;
2572 strcpy (temp, pl->prefix);
2573 strcat (temp, just_machine_suffix);
2574 strcat (temp, name);
2575 if (access_check (temp, mode) == 0)
2577 if (pl->used_flag_ptr != 0)
2578 *pl->used_flag_ptr = 1;
2579 return temp;
2583 /* Certain prefixes can't be used without the machine suffix
2584 when the machine or version is explicitly specified. */
2585 if (! pl->require_machine_suffix)
2587 /* Some systems have a suffix for executable files.
2588 So try appending that first. */
2589 if (file_suffix[0] != 0)
2591 strcpy (temp, pl->prefix);
2592 strcat (temp, name);
2593 strcat (temp, file_suffix);
2594 if (access_check (temp, mode) == 0)
2596 if (pl->used_flag_ptr != 0)
2597 *pl->used_flag_ptr = 1;
2598 return temp;
2602 strcpy (temp, pl->prefix);
2603 strcat (temp, name);
2604 if (access_check (temp, mode) == 0)
2606 if (pl->used_flag_ptr != 0)
2607 *pl->used_flag_ptr = 1;
2608 return temp;
2613 free (temp);
2614 return 0;
2617 /* Ranking of prefixes in the sort list. -B prefixes are put before
2618 all others. */
2620 enum path_prefix_priority
2622 PREFIX_PRIORITY_B_OPT,
2623 PREFIX_PRIORITY_LAST
2626 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2627 order according to PRIORITY. Within each PRIORITY, new entries are
2628 appended.
2630 If WARN is nonzero, we will warn if no file is found
2631 through this prefix. WARN should point to an int
2632 which will be set to 1 if this entry is used.
2634 COMPONENT is the value to be passed to update_path.
2636 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2637 the complete value of machine_suffix.
2638 2 means try both machine_suffix and just_machine_suffix. */
2640 static void
2641 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2642 struct path_prefix *pprefix;
2643 const char *prefix;
2644 const char *component;
2645 /* enum prefix_priority */ int priority;
2646 int require_machine_suffix;
2647 int *warn;
2649 struct prefix_list *pl, **prev;
2650 int len;
2652 for (prev = &pprefix->plist;
2653 (*prev) != NULL && (*prev)->priority <= priority;
2654 prev = &(*prev)->next)
2657 /* Keep track of the longest prefix */
2659 prefix = update_path (prefix, component);
2660 len = strlen (prefix);
2661 if (len > pprefix->max_len)
2662 pprefix->max_len = len;
2664 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2665 pl->prefix = prefix;
2666 pl->require_machine_suffix = require_machine_suffix;
2667 pl->used_flag_ptr = warn;
2668 pl->priority = priority;
2669 if (warn)
2670 *warn = 0;
2672 /* Insert after PREV */
2673 pl->next = (*prev);
2674 (*prev) = pl;
2677 #if defined (__MINGW32__)
2678 static char *
2679 canon_filename (char *fname)
2681 char* p = fname;
2683 while (*p)
2685 if (*p == '/')
2686 *p = '\\';
2687 p++;
2689 return fname;
2691 #else
2692 #define canon_filename(f) f
2693 #endif
2696 /* Execute the command specified by the arguments on the current line of spec.
2697 When using pipes, this includes several piped-together commands
2698 with `|' between them.
2700 Return 0 if successful, -1 if failed. */
2702 static int
2703 execute ()
2705 int i;
2706 int n_commands; /* # of command. */
2707 char *string;
2708 struct command
2710 const char *prog; /* program name. */
2711 const char **argv; /* vector of args. */
2712 int pid; /* pid of process for this command. */
2715 struct command *commands; /* each command buffer with above info. */
2717 /* Count # of piped commands. */
2718 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2719 if (strcmp (argbuf[i], "|") == 0)
2720 n_commands++;
2722 /* Get storage for each command. */
2723 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2725 /* Split argbuf into its separate piped processes,
2726 and record info about each one.
2727 Also search for the programs that are to be run. */
2729 commands[0].prog = argbuf[0]; /* first command. */
2730 commands[0].argv = &argbuf[0];
2731 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2733 if (string)
2734 commands[0].argv[0] = canon_filename(string);
2736 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2737 if (strcmp (argbuf[i], "|") == 0)
2738 { /* each command. */
2739 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2740 fatal ("-pipe not supported");
2741 #endif
2742 argbuf[i] = 0; /* termination of command args. */
2743 commands[n_commands].prog = argbuf[i + 1];
2744 commands[n_commands].argv = &argbuf[i + 1];
2745 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2746 if (string)
2747 commands[n_commands].argv[0] = canon_filename(string);
2748 n_commands++;
2751 argbuf[argbuf_index] = 0;
2753 /* If -v, print what we are about to do, and maybe query. */
2755 if (verbose_flag)
2757 /* For help listings, put a blank line between sub-processes. */
2758 if (print_help_list)
2759 fputc ('\n', stderr);
2761 /* Print each piped command as a separate line. */
2762 for (i = 0; i < n_commands; i++)
2764 const char *const *j;
2766 if (verbose_only_flag)
2768 for (j = commands[i].argv; *j; j++)
2770 const char *p;
2771 fprintf (stderr, " \"");
2772 for (p = *j; *p; ++p)
2774 if (*p == '"' || *p == '\\' || *p == '$')
2775 fputc ('\\', stderr);
2776 fputc (*p, stderr);
2778 fputc ('"', stderr);
2781 else
2782 for (j = commands[i].argv; *j; j++)
2783 fprintf (stderr, " %s", *j);
2785 /* Print a pipe symbol after all but the last command. */
2786 if (i + 1 != n_commands)
2787 fprintf (stderr, " |");
2788 fprintf (stderr, "\n");
2790 fflush (stderr);
2791 if (verbose_only_flag != 0)
2792 return 0;
2793 #ifdef DEBUG
2794 notice ("\nGo ahead? (y or n) ");
2795 fflush (stderr);
2796 i = getchar ();
2797 if (i != '\n')
2798 while (getchar () != '\n')
2801 if (i != 'y' && i != 'Y')
2802 return 0;
2803 #endif /* DEBUG */
2806 /* Run each piped subprocess. */
2808 for (i = 0; i < n_commands; i++)
2810 char *errmsg_fmt, *errmsg_arg;
2811 const char *string = commands[i].argv[0];
2813 /* For some bizarre reason, the second argument of execvp() is
2814 char *const *, not const char *const *. */
2815 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2816 programname, temp_filename,
2817 &errmsg_fmt, &errmsg_arg,
2818 ((i == 0 ? PEXECUTE_FIRST : 0)
2819 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2820 | (string == commands[i].prog
2821 ? PEXECUTE_SEARCH : 0)
2822 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2824 if (commands[i].pid == -1)
2825 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2827 if (string != commands[i].prog)
2828 free ((PTR) string);
2831 execution_count++;
2833 /* Wait for all the subprocesses to finish.
2834 We don't care what order they finish in;
2835 we know that N_COMMANDS waits will get them all.
2836 Ignore subprocesses that we don't know about,
2837 since they can be spawned by the process that exec'ed us. */
2840 int ret_code = 0;
2841 #ifdef HAVE_GETRUSAGE
2842 struct timeval d;
2843 double ut = 0.0, st = 0.0;
2844 #endif
2846 for (i = 0; i < n_commands;)
2848 int j;
2849 int status;
2850 int pid;
2852 pid = pwait (commands[i].pid, &status, 0);
2853 if (pid < 0)
2854 abort ();
2856 #ifdef HAVE_GETRUSAGE
2857 if (report_times)
2859 /* getrusage returns the total resource usage of all children
2860 up to now. Copy the previous values into prus, get the
2861 current statistics, then take the difference. */
2863 prus = rus;
2864 getrusage (RUSAGE_CHILDREN, &rus);
2865 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2866 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2867 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2869 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2870 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2871 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2873 #endif
2875 for (j = 0; j < n_commands; j++)
2876 if (commands[j].pid == pid)
2878 i++;
2879 if (WIFSIGNALED (status))
2881 #ifdef SIGPIPE
2882 /* SIGPIPE is a special case. It happens in -pipe mode
2883 when the compiler dies before the preprocessor is
2884 done, or the assembler dies before the compiler is
2885 done. There's generally been an error already, and
2886 this is just fallout. So don't generate another error
2887 unless we would otherwise have succeeded. */
2888 if (WTERMSIG (status) == SIGPIPE
2889 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2891 else
2892 #endif
2893 fatal ("\
2894 Internal error: %s (program %s)\n\
2895 Please submit a full bug report.\n\
2896 See %s for instructions.",
2897 strsignal (WTERMSIG (status)), commands[j].prog,
2898 GCCBUGURL);
2899 signal_count++;
2900 ret_code = -1;
2902 else if (WIFEXITED (status)
2903 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2905 if (WEXITSTATUS (status) > greatest_status)
2906 greatest_status = WEXITSTATUS (status);
2907 ret_code = -1;
2909 #ifdef HAVE_GETRUSAGE
2910 if (report_times && ut + st != 0)
2911 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2912 #endif
2913 break;
2916 return ret_code;
2920 /* Find all the switches given to us
2921 and make a vector describing them.
2922 The elements of the vector are strings, one per switch given.
2923 If a switch uses following arguments, then the `part1' field
2924 is the switch itself and the `args' field
2925 is a null-terminated vector containing the following arguments.
2926 The `live_cond' field is:
2927 0 when initialized
2928 1 if the switch is true in a conditional spec,
2929 -1 if false (overridden by a later switch)
2930 -2 if this switch should be ignored (used in %{<S})
2931 The `validated' field is nonzero if any spec has looked at this switch;
2932 if it remains zero at the end of the run, it must be meaningless. */
2934 #define SWITCH_OK 0
2935 #define SWITCH_FALSE -1
2936 #define SWITCH_IGNORE -2
2937 #define SWITCH_LIVE 1
2939 struct switchstr
2941 const char *part1;
2942 const char **args;
2943 int live_cond;
2944 unsigned char validated;
2945 unsigned char ordering;
2948 static struct switchstr *switches;
2950 static int n_switches;
2952 struct infile
2954 const char *name;
2955 const char *language;
2958 /* Also a vector of input files specified. */
2960 static struct infile *infiles;
2962 int n_infiles;
2964 /* This counts the number of libraries added by lang_specific_driver, so that
2965 we can tell if there were any user supplied any files or libraries. */
2967 static int added_libraries;
2969 /* And a vector of corresponding output files is made up later. */
2971 const char **outfiles;
2973 /* Used to track if none of the -B paths are used. */
2974 static int warn_B;
2976 /* Used to track if standard path isn't used and -b or -V is specified. */
2977 static int warn_std;
2979 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2980 static int *warn_std_ptr = 0;
2982 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2984 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2985 is true if we should look for an executable suffix. DO_OBJ
2986 is true if we should look for an object suffix. */
2988 static const char *
2989 convert_filename (name, do_exe, do_obj)
2990 const char *name;
2991 int do_exe ATTRIBUTE_UNUSED;
2992 int do_obj ATTRIBUTE_UNUSED;
2994 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2995 int i;
2996 #endif
2997 int len;
2999 if (name == NULL)
3000 return NULL;
3002 len = strlen (name);
3004 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3005 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
3006 if (do_obj && len > 2
3007 && name[len - 2] == '.'
3008 && name[len - 1] == 'o')
3010 obstack_grow (&obstack, name, len - 2);
3011 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
3012 name = obstack_finish (&obstack);
3014 #endif
3016 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3017 /* If there is no filetype, make it the executable suffix (which includes
3018 the "."). But don't get confused if we have just "-o". */
3019 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
3020 return name;
3022 for (i = len - 1; i >= 0; i--)
3023 if (IS_DIR_SEPARATOR (name[i]))
3024 break;
3026 for (i++; i < len; i++)
3027 if (name[i] == '.')
3028 return name;
3030 obstack_grow (&obstack, name, len);
3031 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
3032 strlen (TARGET_EXECUTABLE_SUFFIX));
3033 name = obstack_finish (&obstack);
3034 #endif
3036 return name;
3038 #endif
3040 /* Display the command line switches accepted by gcc. */
3041 static void
3042 display_help ()
3044 printf (_("Usage: %s [options] file...\n"), programname);
3045 fputs (_("Options:\n"), stdout);
3047 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
3048 fputs (_(" --help Display this information\n"), stdout);
3049 fputs (_(" --target-help Display target specific command line options\n"), stdout);
3050 if (! verbose_flag)
3051 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
3052 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
3053 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
3054 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
3055 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
3056 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
3057 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
3058 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
3059 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
3060 fputs (_("\
3061 -print-multi-lib Display the mapping between command line options and\n\
3062 multiple library search directories\n"), stdout);
3063 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
3064 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
3065 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
3066 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
3067 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
3068 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
3069 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
3070 fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
3071 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
3072 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
3073 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
3074 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
3075 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
3076 fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
3077 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
3078 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
3079 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
3080 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
3081 fputs (_("\
3082 -x <language> Specify the language of the following input files\n\
3083 Permissable languages include: c c++ assembler none\n\
3084 'none' means revert to the default behavior of\n\
3085 guessing the language based on the file's extension\n\
3086 "), stdout);
3088 printf (_("\
3089 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3090 passed on to the various sub-processes invoked by %s. In order to pass\n\
3091 other options on to these processes the -W<letter> options must be used.\n\
3092 "), programname);
3094 /* The rest of the options are displayed by invocations of the various
3095 sub-processes. */
3098 static void
3099 add_preprocessor_option (option, len)
3100 const char *option;
3101 int len;
3103 n_preprocessor_options++;
3105 if (! preprocessor_options)
3106 preprocessor_options
3107 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
3108 else
3109 preprocessor_options
3110 = (char **) xrealloc (preprocessor_options,
3111 n_preprocessor_options * sizeof (char *));
3113 preprocessor_options [n_preprocessor_options - 1] =
3114 save_string (option, len);
3117 static void
3118 add_assembler_option (option, len)
3119 const char *option;
3120 int len;
3122 n_assembler_options++;
3124 if (! assembler_options)
3125 assembler_options
3126 = (char **) xmalloc (n_assembler_options * sizeof (char *));
3127 else
3128 assembler_options
3129 = (char **) xrealloc (assembler_options,
3130 n_assembler_options * sizeof (char *));
3132 assembler_options [n_assembler_options - 1] = save_string (option, len);
3135 static void
3136 add_linker_option (option, len)
3137 const char *option;
3138 int len;
3140 n_linker_options++;
3142 if (! linker_options)
3143 linker_options
3144 = (char **) xmalloc (n_linker_options * sizeof (char *));
3145 else
3146 linker_options
3147 = (char **) xrealloc (linker_options,
3148 n_linker_options * sizeof (char *));
3150 linker_options [n_linker_options - 1] = save_string (option, len);
3153 /* Create the vector `switches' and its contents.
3154 Store its length in `n_switches'. */
3156 static void
3157 process_command (argc, argv)
3158 int argc;
3159 const char *const *argv;
3161 int i;
3162 const char *temp;
3163 char *temp1;
3164 const char *spec_lang = 0;
3165 int last_language_n_infiles;
3166 int have_c = 0;
3167 int have_o = 0;
3168 int lang_n_infiles = 0;
3169 #ifdef MODIFY_TARGET_NAME
3170 int is_modify_target_name;
3171 int j;
3172 #endif
3174 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3176 n_switches = 0;
3177 n_infiles = 0;
3178 added_libraries = 0;
3180 /* Figure compiler version from version string. */
3182 compiler_version = temp1 = xstrdup (version_string);
3184 for (; *temp1; ++temp1)
3186 if (*temp1 == ' ')
3188 *temp1 = '\0';
3189 break;
3193 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3194 see if we can create it from the pathname specified in argv[0]. */
3196 #ifndef VMS
3197 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3198 if (!gcc_exec_prefix)
3200 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3201 standard_exec_prefix);
3202 if (gcc_exec_prefix)
3203 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
3205 #endif
3207 if (gcc_exec_prefix)
3209 int len = strlen (gcc_exec_prefix);
3211 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3212 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3214 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3215 if (IS_DIR_SEPARATOR (*temp)
3216 && strncmp (temp + 1, "lib", 3) == 0
3217 && IS_DIR_SEPARATOR (temp[4])
3218 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3219 len -= sizeof ("/lib/gcc-lib/") - 1;
3222 set_std_prefix (gcc_exec_prefix, len);
3223 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3224 PREFIX_PRIORITY_LAST, 0, NULL);
3225 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3226 PREFIX_PRIORITY_LAST, 0, NULL);
3229 /* COMPILER_PATH and LIBRARY_PATH have values
3230 that are lists of directory names with colons. */
3232 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3233 if (temp)
3235 const char *startp, *endp;
3236 char *nstore = (char *) alloca (strlen (temp) + 3);
3238 startp = endp = temp;
3239 while (1)
3241 if (*endp == PATH_SEPARATOR || *endp == 0)
3243 strncpy (nstore, startp, endp - startp);
3244 if (endp == startp)
3245 strcpy (nstore, concat (".", dir_separator_str, NULL));
3246 else if (!IS_DIR_SEPARATOR (endp[-1]))
3248 nstore[endp - startp] = DIR_SEPARATOR;
3249 nstore[endp - startp + 1] = 0;
3251 else
3252 nstore[endp - startp] = 0;
3253 add_prefix (&exec_prefixes, nstore, 0,
3254 PREFIX_PRIORITY_LAST, 0, NULL);
3255 add_prefix (&include_prefixes,
3256 concat (nstore, "include", NULL),
3257 0, PREFIX_PRIORITY_LAST, 0, NULL);
3258 if (*endp == 0)
3259 break;
3260 endp = startp = endp + 1;
3262 else
3263 endp++;
3267 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3268 if (temp && *cross_compile == '0')
3270 const char *startp, *endp;
3271 char *nstore = (char *) alloca (strlen (temp) + 3);
3273 startp = endp = temp;
3274 while (1)
3276 if (*endp == PATH_SEPARATOR || *endp == 0)
3278 strncpy (nstore, startp, endp - startp);
3279 if (endp == startp)
3280 strcpy (nstore, concat (".", dir_separator_str, NULL));
3281 else if (!IS_DIR_SEPARATOR (endp[-1]))
3283 nstore[endp - startp] = DIR_SEPARATOR;
3284 nstore[endp - startp + 1] = 0;
3286 else
3287 nstore[endp - startp] = 0;
3288 add_prefix (&startfile_prefixes, nstore, NULL,
3289 PREFIX_PRIORITY_LAST, 0, NULL);
3290 if (*endp == 0)
3291 break;
3292 endp = startp = endp + 1;
3294 else
3295 endp++;
3299 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3300 GET_ENV_PATH_LIST (temp, "LPATH");
3301 if (temp && *cross_compile == '0')
3303 const char *startp, *endp;
3304 char *nstore = (char *) alloca (strlen (temp) + 3);
3306 startp = endp = temp;
3307 while (1)
3309 if (*endp == PATH_SEPARATOR || *endp == 0)
3311 strncpy (nstore, startp, endp - startp);
3312 if (endp == startp)
3313 strcpy (nstore, concat (".", dir_separator_str, NULL));
3314 else if (!IS_DIR_SEPARATOR (endp[-1]))
3316 nstore[endp - startp] = DIR_SEPARATOR;
3317 nstore[endp - startp + 1] = 0;
3319 else
3320 nstore[endp - startp] = 0;
3321 add_prefix (&startfile_prefixes, nstore, NULL,
3322 PREFIX_PRIORITY_LAST, 0, NULL);
3323 if (*endp == 0)
3324 break;
3325 endp = startp = endp + 1;
3327 else
3328 endp++;
3332 /* Convert new-style -- options to old-style. */
3333 translate_options (&argc, &argv);
3335 /* Do language-specific adjustment/addition of flags. */
3336 lang_specific_driver (&argc, &argv, &added_libraries);
3338 /* Scan argv twice. Here, the first time, just count how many switches
3339 there will be in their vector, and how many input files in theirs.
3340 Also parse any switches that determine the configuration name, such as -b.
3341 Here we also parse the switches that cc itself uses (e.g. -v). */
3343 for (i = 1; i < argc; i++)
3345 if (! strcmp (argv[i], "-dumpspecs"))
3347 struct spec_list *sl;
3348 init_spec ();
3349 for (sl = specs; sl; sl = sl->next)
3350 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3351 if (link_command_spec)
3352 printf ("*link_command:\n%s\n\n", link_command_spec);
3353 exit (0);
3355 else if (! strcmp (argv[i], "-dumpversion"))
3357 printf ("%s\n", spec_version);
3358 exit (0);
3360 else if (! strcmp (argv[i], "-dumpmachine"))
3362 printf ("%s\n", spec_machine);
3363 exit (0);
3365 else if (strcmp (argv[i], "-fversion") == 0)
3367 /* translate_options () has turned --version into -fversion. */
3368 printf (_("%s (GCC) %s\n"), programname, version_string);
3369 fputs (_("Copyright (C) 2002 Free Software Foundation, Inc.\n"),
3370 stdout);
3371 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
3372 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
3373 stdout);
3374 exit (0);
3376 else if (strcmp (argv[i], "-fhelp") == 0)
3378 /* translate_options () has turned --help into -fhelp. */
3379 print_help_list = 1;
3381 /* We will be passing a dummy file on to the sub-processes. */
3382 n_infiles++;
3383 n_switches++;
3385 /* CPP driver cannot obtain switch from cc1_options. */
3386 if (is_cpp_driver)
3387 add_preprocessor_option ("--help", 6);
3388 add_assembler_option ("--help", 6);
3389 add_linker_option ("--help", 6);
3391 else if (strcmp (argv[i], "-ftarget-help") == 0)
3393 /* translate_options() has turned --target-help into -ftarget-help. */
3394 target_help_flag = 1;
3396 /* We will be passing a dummy file on to the sub-processes. */
3397 n_infiles++;
3398 n_switches++;
3400 /* CPP driver cannot obtain switch from cc1_options. */
3401 if (is_cpp_driver)
3402 add_preprocessor_option ("--target-help", 13);
3403 add_assembler_option ("--target-help", 13);
3404 add_linker_option ("--target-help", 13);
3406 else if (! strcmp (argv[i], "-pass-exit-codes"))
3408 pass_exit_codes = 1;
3409 n_switches++;
3411 else if (! strcmp (argv[i], "-print-search-dirs"))
3412 print_search_dirs = 1;
3413 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3414 print_file_name = "libgcc.a";
3415 else if (! strncmp (argv[i], "-print-file-name=", 17))
3416 print_file_name = argv[i] + 17;
3417 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3418 print_prog_name = argv[i] + 17;
3419 else if (! strcmp (argv[i], "-print-multi-lib"))
3420 print_multi_lib = 1;
3421 else if (! strcmp (argv[i], "-print-multi-directory"))
3422 print_multi_directory = 1;
3423 else if (! strncmp (argv[i], "-Wa,", 4))
3425 int prev, j;
3426 /* Pass the rest of this option to the assembler. */
3428 /* Split the argument at commas. */
3429 prev = 4;
3430 for (j = 4; argv[i][j]; j++)
3431 if (argv[i][j] == ',')
3433 add_assembler_option (argv[i] + prev, j - prev);
3434 prev = j + 1;
3437 /* Record the part after the last comma. */
3438 add_assembler_option (argv[i] + prev, j - prev);
3440 else if (! strncmp (argv[i], "-Wp,", 4))
3442 int prev, j;
3443 /* Pass the rest of this option to the preprocessor. */
3445 /* Split the argument at commas. */
3446 prev = 4;
3447 for (j = 4; argv[i][j]; j++)
3448 if (argv[i][j] == ',')
3450 add_preprocessor_option (argv[i] + prev, j - prev);
3451 prev = j + 1;
3454 /* Record the part after the last comma. */
3455 add_preprocessor_option (argv[i] + prev, j - prev);
3457 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3458 /* The +e options to the C++ front-end. */
3459 n_switches++;
3460 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3462 int j;
3463 /* Split the argument at commas. */
3464 for (j = 3; argv[i][j]; j++)
3465 n_infiles += (argv[i][j] == ',');
3467 else if (strcmp (argv[i], "-Xlinker") == 0)
3469 if (i + 1 == argc)
3470 fatal ("argument to `-Xlinker' is missing");
3472 n_infiles++;
3473 i++;
3475 else if (strcmp (argv[i], "-l") == 0)
3477 if (i + 1 == argc)
3478 fatal ("argument to `-l' is missing");
3480 n_infiles++;
3481 i++;
3483 else if (strncmp (argv[i], "-l", 2) == 0)
3484 n_infiles++;
3485 else if (strcmp (argv[i], "-save-temps") == 0)
3487 save_temps_flag = 1;
3488 n_switches++;
3490 else if (strcmp (argv[i], "-specs") == 0)
3492 struct user_specs *user = (struct user_specs *)
3493 xmalloc (sizeof (struct user_specs));
3494 if (++i >= argc)
3495 fatal ("argument to `-specs' is missing");
3497 user->next = (struct user_specs *) 0;
3498 user->filename = argv[i];
3499 if (user_specs_tail)
3500 user_specs_tail->next = user;
3501 else
3502 user_specs_head = user;
3503 user_specs_tail = user;
3505 else if (strncmp (argv[i], "-specs=", 7) == 0)
3507 struct user_specs *user = (struct user_specs *)
3508 xmalloc (sizeof (struct user_specs));
3509 if (strlen (argv[i]) == 7)
3510 fatal ("argument to `-specs=' is missing");
3512 user->next = (struct user_specs *) 0;
3513 user->filename = argv[i] + 7;
3514 if (user_specs_tail)
3515 user_specs_tail->next = user;
3516 else
3517 user_specs_head = user;
3518 user_specs_tail = user;
3520 else if (strcmp (argv[i], "-time") == 0)
3521 report_times = 1;
3522 else if (strcmp (argv[i], "-###") == 0)
3524 /* This is similar to -v except that there is no execution
3525 of the commands and the echoed arguments are quoted. It
3526 is intended for use in shell scripts to capture the
3527 driver-generated command line. */
3528 verbose_only_flag++;
3529 verbose_flag++;
3531 else if (argv[i][0] == '-' && argv[i][1] != 0)
3533 const char *p = &argv[i][1];
3534 int c = *p;
3536 switch (c)
3538 case 'b':
3539 n_switches++;
3540 if (p[1] == 0 && i + 1 == argc)
3541 fatal ("argument to `-b' is missing");
3542 if (p[1] == 0)
3543 spec_machine = argv[++i];
3544 else
3545 spec_machine = p + 1;
3547 warn_std_ptr = &warn_std;
3548 break;
3550 case 'B':
3552 const char *value;
3553 int len;
3555 if (p[1] == 0 && i + 1 == argc)
3556 fatal ("argument to `-B' is missing");
3557 if (p[1] == 0)
3558 value = argv[++i];
3559 else
3560 value = p + 1;
3562 len = strlen (value);
3564 /* Catch the case where the user has forgotten to append a
3565 directory separator to the path. Note, they may be using
3566 -B to add an executable name prefix, eg "i386-elf-", in
3567 order to distinguish between multiple installations of
3568 GCC in the same directory. Hence we must check to see
3569 if appending a directory separator actually makes a
3570 valid directory name. */
3571 if (! IS_DIR_SEPARATOR (value [len - 1])
3572 && is_directory (value, "", 0))
3574 char *tmp = xmalloc (len + 2);
3575 strcpy (tmp, value);
3576 tmp[len] = DIR_SEPARATOR;
3577 tmp[++ len] = 0;
3578 value = tmp;
3581 /* As a kludge, if the arg is "[foo/]stageN/", just
3582 add "[foo/]include" to the include prefix. */
3583 if ((len == 7
3584 || (len > 7
3585 && (IS_DIR_SEPARATOR (value[len - 8]))))
3586 && strncmp (value + len - 7, "stage", 5) == 0
3587 && ISDIGIT (value[len - 2])
3588 && (IS_DIR_SEPARATOR (value[len - 1])))
3590 if (len == 7)
3591 add_prefix (&include_prefixes, "include", NULL,
3592 PREFIX_PRIORITY_B_OPT, 0, NULL);
3593 else
3595 char * string = xmalloc (len + 1);
3597 strncpy (string, value, len - 7);
3598 strcpy (string + len - 7, "include");
3599 add_prefix (&include_prefixes, string, NULL,
3600 PREFIX_PRIORITY_B_OPT, 0, NULL);
3604 add_prefix (&exec_prefixes, value, NULL,
3605 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3606 add_prefix (&startfile_prefixes, value, NULL,
3607 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3608 add_prefix (&include_prefixes, concat (value, "include", NULL),
3609 NULL, PREFIX_PRIORITY_B_OPT, 0, NULL);
3610 n_switches++;
3612 break;
3614 case 'v': /* Print our subcommands and print versions. */
3615 n_switches++;
3616 /* If they do anything other than exactly `-v', don't set
3617 verbose_flag; rather, continue on to give the error. */
3618 if (p[1] != 0)
3619 break;
3620 verbose_flag++;
3621 break;
3623 case 'V':
3624 n_switches++;
3625 if (p[1] == 0 && i + 1 == argc)
3626 fatal ("argument to `-V' is missing");
3627 if (p[1] == 0)
3628 spec_version = argv[++i];
3629 else
3630 spec_version = p + 1;
3631 compiler_version = spec_version;
3632 warn_std_ptr = &warn_std;
3634 /* Validate the version number. Use the same checks
3635 done when inserting it into a spec.
3637 The format of the version string is
3638 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3640 const char *v = compiler_version;
3642 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3643 while (! ISDIGIT (*v))
3644 v++;
3646 if (v > compiler_version && v[-1] != '-')
3647 fatal ("invalid version number format");
3649 /* Set V after the first period. */
3650 while (ISDIGIT (*v))
3651 v++;
3653 if (*v != '.')
3654 fatal ("invalid version number format");
3656 v++;
3657 while (ISDIGIT (*v))
3658 v++;
3660 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
3661 fatal ("invalid version number format");
3663 break;
3665 case 'S':
3666 case 'c':
3667 if (p[1] == 0)
3669 have_c = 1;
3670 n_switches++;
3671 break;
3673 goto normal_switch;
3675 case 'o':
3676 have_o = 1;
3677 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3678 if (! have_c)
3680 int skip;
3682 /* Forward scan, just in case -S or -c is specified
3683 after -o. */
3684 int j = i + 1;
3685 if (p[1] == 0)
3686 ++j;
3687 while (j < argc)
3689 if (argv[j][0] == '-')
3691 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3692 && argv[j][2] == 0)
3694 have_c = 1;
3695 break;
3697 else if ((skip = SWITCH_TAKES_ARG (argv[j][1])))
3698 j += skip - (argv[j][2] != 0);
3699 else if ((skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1)))
3700 j += skip;
3702 j++;
3705 #endif
3706 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3707 if (p[1] == 0)
3708 argv[i + 1] = convert_filename (argv[i + 1], ! have_c, 0);
3709 else
3710 argv[i] = convert_filename (argv[i], ! have_c, 0);
3711 #endif
3712 goto normal_switch;
3714 default:
3715 normal_switch:
3717 #ifdef MODIFY_TARGET_NAME
3718 is_modify_target_name = 0;
3720 for (j = 0;
3721 j < sizeof modify_target / sizeof modify_target[0]; j++)
3722 if (! strcmp (argv[i], modify_target[j].sw))
3724 char *new_name
3725 = (char *) xmalloc (strlen (modify_target[j].str)
3726 + strlen (spec_machine));
3727 const char *p, *r;
3728 char *q;
3729 int made_addition = 0;
3731 is_modify_target_name = 1;
3732 for (p = spec_machine, q = new_name; *p != 0; )
3734 if (modify_target[j].add_del == DELETE
3735 && (! strncmp (q, modify_target[j].str,
3736 strlen (modify_target[j].str))))
3737 p += strlen (modify_target[j].str);
3738 else if (modify_target[j].add_del == ADD
3739 && ! made_addition && *p == '-')
3741 for (r = modify_target[j].str; *r != 0; )
3742 *q++ = *r++;
3743 made_addition = 1;
3746 *q++ = *p++;
3749 spec_machine = new_name;
3752 if (is_modify_target_name)
3753 break;
3754 #endif
3756 n_switches++;
3758 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3759 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3760 else if (WORD_SWITCH_TAKES_ARG (p))
3761 i += WORD_SWITCH_TAKES_ARG (p);
3764 else
3766 n_infiles++;
3767 lang_n_infiles++;
3771 if (have_c && have_o && lang_n_infiles > 1)
3772 fatal ("cannot specify -o with -c or -S and multiple compilations");
3774 /* Set up the search paths before we go looking for config files. */
3776 /* These come before the md prefixes so that we will find gcc's subcommands
3777 (such as cpp) rather than those of the host system. */
3778 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3779 as well as trying the machine and the version. */
3780 #ifndef OS2
3781 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3782 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3783 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3784 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3785 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3786 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3787 #endif
3789 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3790 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3791 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3792 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3794 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3795 dir_separator_str, NULL);
3797 /* If tooldir is relative, base it on exec_prefixes. A relative
3798 tooldir lets us move the installed tree as a unit.
3800 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3801 directories, so that we can search both the user specified directory
3802 and the standard place. */
3804 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3806 if (gcc_exec_prefix)
3808 char *gcc_exec_tooldir_prefix
3809 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3810 spec_version, dir_separator_str, tooldir_prefix, NULL);
3812 add_prefix (&exec_prefixes,
3813 concat (gcc_exec_tooldir_prefix, "bin",
3814 dir_separator_str, NULL),
3815 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3816 add_prefix (&startfile_prefixes,
3817 concat (gcc_exec_tooldir_prefix, "lib",
3818 dir_separator_str, NULL),
3819 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3822 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3823 dir_separator_str, spec_version,
3824 dir_separator_str, tooldir_prefix, NULL);
3827 add_prefix (&exec_prefixes,
3828 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
3829 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3830 add_prefix (&startfile_prefixes,
3831 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
3832 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3834 /* More prefixes are enabled in main, after we read the specs file
3835 and determine whether this is cross-compilation or not. */
3837 /* Then create the space for the vectors and scan again. */
3839 switches = ((struct switchstr *)
3840 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3841 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3842 n_switches = 0;
3843 n_infiles = 0;
3844 last_language_n_infiles = -1;
3846 /* This, time, copy the text of each switch and store a pointer
3847 to the copy in the vector of switches.
3848 Store all the infiles in their vector. */
3850 for (i = 1; i < argc; i++)
3852 /* Just skip the switches that were handled by the preceding loop. */
3853 #ifdef MODIFY_TARGET_NAME
3854 is_modify_target_name = 0;
3856 for (j = 0; j < sizeof modify_target / sizeof modify_target[0]; j++)
3857 if (! strcmp (argv[i], modify_target[j].sw))
3858 is_modify_target_name = 1;
3860 if (is_modify_target_name)
3862 else
3863 #endif
3864 if (! strncmp (argv[i], "-Wa,", 4))
3866 else if (! strncmp (argv[i], "-Wp,", 4))
3868 else if (! strcmp (argv[i], "-pass-exit-codes"))
3870 else if (! strcmp (argv[i], "-print-search-dirs"))
3872 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3874 else if (! strncmp (argv[i], "-print-file-name=", 17))
3876 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3878 else if (! strcmp (argv[i], "-print-multi-lib"))
3880 else if (! strcmp (argv[i], "-print-multi-directory"))
3882 else if (! strcmp (argv[i], "-ftarget-help"))
3884 else if (! strcmp (argv[i], "-fhelp"))
3886 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3888 /* Compensate for the +e options to the C++ front-end;
3889 they're there simply for cfront call-compatibility. We do
3890 some magic in default_compilers to pass them down properly.
3891 Note we deliberately start at the `+' here, to avoid passing
3892 -e0 or -e1 down into the linker. */
3893 switches[n_switches].part1 = &argv[i][0];
3894 switches[n_switches].args = 0;
3895 switches[n_switches].live_cond = SWITCH_OK;
3896 switches[n_switches].validated = 0;
3897 n_switches++;
3899 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3901 int prev, j;
3902 /* Split the argument at commas. */
3903 prev = 4;
3904 for (j = 4; argv[i][j]; j++)
3905 if (argv[i][j] == ',')
3907 infiles[n_infiles].language = "*";
3908 infiles[n_infiles++].name
3909 = save_string (argv[i] + prev, j - prev);
3910 prev = j + 1;
3912 /* Record the part after the last comma. */
3913 infiles[n_infiles].language = "*";
3914 infiles[n_infiles++].name = argv[i] + prev;
3916 else if (strcmp (argv[i], "-Xlinker") == 0)
3918 infiles[n_infiles].language = "*";
3919 infiles[n_infiles++].name = argv[++i];
3921 else if (strcmp (argv[i], "-l") == 0)
3922 { /* POSIX allows separation of -l and the lib arg;
3923 canonicalize by concatenating -l with its arg */
3924 infiles[n_infiles].language = "*";
3925 infiles[n_infiles++].name = concat ("-l", argv[++i], NULL);
3927 else if (strncmp (argv[i], "-l", 2) == 0)
3929 infiles[n_infiles].language = "*";
3930 infiles[n_infiles++].name = argv[i];
3932 else if (strcmp (argv[i], "-specs") == 0)
3933 i++;
3934 else if (strncmp (argv[i], "-specs=", 7) == 0)
3936 else if (strcmp (argv[i], "-time") == 0)
3938 else if ((save_temps_flag || report_times)
3939 && strcmp (argv[i], "-pipe") == 0)
3941 /* -save-temps overrides -pipe, so that temp files are produced */
3942 if (save_temps_flag)
3943 error ("warning: -pipe ignored because -save-temps specified");
3944 /* -time overrides -pipe because we can't get correct stats when
3945 multiple children are running at once. */
3946 else if (report_times)
3947 error ("warning: -pipe ignored because -time specified");
3949 else if (strcmp (argv[i], "-###") == 0)
3951 else if (argv[i][0] == '-' && argv[i][1] != 0)
3953 const char *p = &argv[i][1];
3954 int c = *p;
3956 if (c == 'x')
3958 if (p[1] == 0 && i + 1 == argc)
3959 fatal ("argument to `-x' is missing");
3960 if (p[1] == 0)
3961 spec_lang = argv[++i];
3962 else
3963 spec_lang = p + 1;
3964 if (! strcmp (spec_lang, "none"))
3965 /* Suppress the warning if -xnone comes after the last input
3966 file, because alternate command interfaces like g++ might
3967 find it useful to place -xnone after each input file. */
3968 spec_lang = 0;
3969 else
3970 last_language_n_infiles = n_infiles;
3971 continue;
3973 switches[n_switches].part1 = p;
3974 /* Deal with option arguments in separate argv elements. */
3975 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3976 || WORD_SWITCH_TAKES_ARG (p))
3978 int j = 0;
3979 int n_args = WORD_SWITCH_TAKES_ARG (p);
3981 if (n_args == 0)
3983 /* Count only the option arguments in separate argv elements. */
3984 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3986 if (i + n_args >= argc)
3987 fatal ("argument to `-%s' is missing", p);
3988 switches[n_switches].args
3989 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3990 while (j < n_args)
3991 switches[n_switches].args[j++] = argv[++i];
3992 /* Null-terminate the vector. */
3993 switches[n_switches].args[j] = 0;
3995 else if (strchr (switches_need_spaces, c))
3997 /* On some systems, ld cannot handle some options without
3998 a space. So split the option from its argument. */
3999 char *part1 = (char *) xmalloc (2);
4000 part1[0] = c;
4001 part1[1] = '\0';
4003 switches[n_switches].part1 = part1;
4004 switches[n_switches].args
4005 = (const char **) xmalloc (2 * sizeof (const char *));
4006 switches[n_switches].args[0] = xstrdup (p+1);
4007 switches[n_switches].args[1] = 0;
4009 else
4010 switches[n_switches].args = 0;
4012 switches[n_switches].live_cond = SWITCH_OK;
4013 switches[n_switches].validated = 0;
4014 switches[n_switches].ordering = 0;
4015 /* These are always valid, since gcc.c itself understands it. */
4016 if (!strcmp (p, "save-temps")
4017 || !strcmp (p, "static-libgcc")
4018 || !strcmp (p, "shared-libgcc"))
4019 switches[n_switches].validated = 1;
4020 else
4022 char ch = switches[n_switches].part1[0];
4023 if (ch == 'V' || ch == 'b' || ch == 'B')
4024 switches[n_switches].validated = 1;
4026 n_switches++;
4028 else
4030 #ifdef HAVE_TARGET_OBJECT_SUFFIX
4031 argv[i] = convert_filename (argv[i], 0, access (argv[i], F_OK));
4032 #endif
4034 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
4036 perror_with_name (argv[i]);
4037 error_count++;
4039 else
4041 infiles[n_infiles].language = spec_lang;
4042 infiles[n_infiles++].name = argv[i];
4047 if (n_infiles == last_language_n_infiles && spec_lang != 0)
4048 error ("warning: `-x %s' after last input file has no effect", spec_lang);
4050 /* Ensure we only invoke each subprocess once. */
4051 if (target_help_flag || print_help_list)
4053 n_infiles = 1;
4055 /* Create a dummy input file, so that we can pass --target-help on to
4056 the various sub-processes. */
4057 infiles[0].language = "c";
4058 infiles[0].name = "help-dummy";
4060 if (target_help_flag)
4062 switches[n_switches].part1 = "--target-help";
4063 switches[n_switches].args = 0;
4064 switches[n_switches].live_cond = SWITCH_OK;
4065 switches[n_switches].validated = 0;
4067 n_switches++;
4070 if (print_help_list)
4072 switches[n_switches].part1 = "--help";
4073 switches[n_switches].args = 0;
4074 switches[n_switches].live_cond = SWITCH_OK;
4075 switches[n_switches].validated = 0;
4077 n_switches++;
4081 switches[n_switches].part1 = 0;
4082 infiles[n_infiles].name = 0;
4085 /* Store switches not filtered out by %{<S} in spec in COLLECT_GCC_OPTIONS
4086 and place that in the environment. */
4088 static void
4089 set_collect_gcc_options ()
4091 int i;
4092 int first_time;
4094 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4095 the compiler. */
4096 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4097 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
4099 first_time = TRUE;
4100 for (i = 0; (int) i < n_switches; i++)
4102 const char *const *args;
4103 const char *p, *q;
4104 if (!first_time)
4105 obstack_grow (&collect_obstack, " ", 1);
4107 first_time = FALSE;
4109 /* Ignore elided switches. */
4110 if (switches[i].live_cond == SWITCH_IGNORE)
4111 continue;
4113 obstack_grow (&collect_obstack, "'-", 2);
4114 q = switches[i].part1;
4115 while ((p = strchr (q, '\'')))
4117 obstack_grow (&collect_obstack, q, p - q);
4118 obstack_grow (&collect_obstack, "'\\''", 4);
4119 q = ++p;
4121 obstack_grow (&collect_obstack, q, strlen (q));
4122 obstack_grow (&collect_obstack, "'", 1);
4124 for (args = switches[i].args; args && *args; args++)
4126 obstack_grow (&collect_obstack, " '", 2);
4127 q = *args;
4128 while ((p = strchr (q, '\'')))
4130 obstack_grow (&collect_obstack, q, p - q);
4131 obstack_grow (&collect_obstack, "'\\''", 4);
4132 q = ++p;
4134 obstack_grow (&collect_obstack, q, strlen (q));
4135 obstack_grow (&collect_obstack, "'", 1);
4138 obstack_grow (&collect_obstack, "\0", 1);
4139 putenv (obstack_finish (&collect_obstack));
4142 /* Process a spec string, accumulating and running commands. */
4144 /* These variables describe the input file name.
4145 input_file_number is the index on outfiles of this file,
4146 so that the output file name can be stored for later use by %o.
4147 input_basename is the start of the part of the input file
4148 sans all directory names, and basename_length is the number
4149 of characters starting there excluding the suffix .c or whatever. */
4151 const char *input_filename;
4152 static int input_file_number;
4153 size_t input_filename_length;
4154 static int basename_length;
4155 static int suffixed_basename_length;
4156 static const char *input_basename;
4157 static const char *input_suffix;
4158 static struct stat input_stat;
4159 static int input_stat_set;
4161 /* The compiler used to process the current input file. */
4162 static struct compiler *input_file_compiler;
4164 /* These are variables used within do_spec and do_spec_1. */
4166 /* Nonzero if an arg has been started and not yet terminated
4167 (with space, tab or newline). */
4168 static int arg_going;
4170 /* Nonzero means %d or %g has been seen; the next arg to be terminated
4171 is a temporary file name. */
4172 static int delete_this_arg;
4174 /* Nonzero means %w has been seen; the next arg to be terminated
4175 is the output file name of this compilation. */
4176 static int this_is_output_file;
4178 /* Nonzero means %s has been seen; the next arg to be terminated
4179 is the name of a library file and we should try the standard
4180 search dirs for it. */
4181 static int this_is_library_file;
4183 /* Nonzero means that the input of this command is coming from a pipe. */
4184 static int input_from_pipe;
4186 /* Nonnull means substitute this for any suffix when outputting a switches
4187 arguments. */
4188 static const char *suffix_subst;
4190 /* Process the spec SPEC and run the commands specified therein.
4191 Returns 0 if the spec is successfully processed; -1 if failed. */
4194 do_spec (spec)
4195 const char *spec;
4197 int value;
4199 clear_args ();
4200 arg_going = 0;
4201 delete_this_arg = 0;
4202 this_is_output_file = 0;
4203 this_is_library_file = 0;
4204 input_from_pipe = 0;
4205 suffix_subst = NULL;
4207 value = do_spec_1 (spec, 0, NULL);
4209 /* Force out any unfinished command.
4210 If -pipe, this forces out the last command if it ended in `|'. */
4211 if (value == 0)
4213 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4214 argbuf_index--;
4216 set_collect_gcc_options ();
4218 if (argbuf_index > 0)
4219 value = execute ();
4222 return value;
4225 /* Process the sub-spec SPEC as a portion of a larger spec.
4226 This is like processing a whole spec except that we do
4227 not initialize at the beginning and we do not supply a
4228 newline by default at the end.
4229 INSWITCH nonzero means don't process %-sequences in SPEC;
4230 in this case, % is treated as an ordinary character.
4231 This is used while substituting switches.
4232 INSWITCH nonzero also causes SPC not to terminate an argument.
4234 Value is zero unless a line was finished
4235 and the command on that line reported an error. */
4237 static int
4238 do_spec_1 (spec, inswitch, soft_matched_part)
4239 const char *spec;
4240 int inswitch;
4241 const char *soft_matched_part;
4243 const char *p = spec;
4244 int c;
4245 int i;
4246 const char *string;
4247 int value;
4249 while ((c = *p++))
4250 /* If substituting a switch, treat all chars like letters.
4251 Otherwise, NL, SPC, TAB and % are special. */
4252 switch (inswitch ? 'a' : c)
4254 case '\n':
4255 /* End of line: finish any pending argument,
4256 then run the pending command if one has been started. */
4257 if (arg_going)
4259 obstack_1grow (&obstack, 0);
4260 string = obstack_finish (&obstack);
4261 if (this_is_library_file)
4262 string = find_file (string);
4263 store_arg (string, delete_this_arg, this_is_output_file);
4264 if (this_is_output_file)
4265 outfiles[input_file_number] = string;
4267 arg_going = 0;
4269 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4271 for (i = 0; i < n_switches; i++)
4272 if (!strcmp (switches[i].part1, "pipe"))
4273 break;
4275 /* A `|' before the newline means use a pipe here,
4276 but only if -pipe was specified.
4277 Otherwise, execute now and don't pass the `|' as an arg. */
4278 if (i < n_switches)
4280 input_from_pipe = 1;
4281 switches[i].validated = 1;
4282 break;
4284 else
4285 argbuf_index--;
4288 set_collect_gcc_options ();
4290 if (argbuf_index > 0)
4292 value = execute ();
4293 if (value)
4294 return value;
4296 /* Reinitialize for a new command, and for a new argument. */
4297 clear_args ();
4298 arg_going = 0;
4299 delete_this_arg = 0;
4300 this_is_output_file = 0;
4301 this_is_library_file = 0;
4302 input_from_pipe = 0;
4303 break;
4305 case '|':
4306 /* End any pending argument. */
4307 if (arg_going)
4309 obstack_1grow (&obstack, 0);
4310 string = obstack_finish (&obstack);
4311 if (this_is_library_file)
4312 string = find_file (string);
4313 store_arg (string, delete_this_arg, this_is_output_file);
4314 if (this_is_output_file)
4315 outfiles[input_file_number] = string;
4318 /* Use pipe */
4319 obstack_1grow (&obstack, c);
4320 arg_going = 1;
4321 break;
4323 case '\t':
4324 case ' ':
4325 /* Space or tab ends an argument if one is pending. */
4326 if (arg_going)
4328 obstack_1grow (&obstack, 0);
4329 string = obstack_finish (&obstack);
4330 if (this_is_library_file)
4331 string = find_file (string);
4332 store_arg (string, delete_this_arg, this_is_output_file);
4333 if (this_is_output_file)
4334 outfiles[input_file_number] = string;
4336 /* Reinitialize for a new argument. */
4337 arg_going = 0;
4338 delete_this_arg = 0;
4339 this_is_output_file = 0;
4340 this_is_library_file = 0;
4341 break;
4343 case '%':
4344 switch (c = *p++)
4346 case 0:
4347 fatal ("invalid specification! Bug in cc");
4349 case 'b':
4350 obstack_grow (&obstack, input_basename, basename_length);
4351 arg_going = 1;
4352 break;
4354 case 'B':
4355 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4356 arg_going = 1;
4357 break;
4359 case 'd':
4360 delete_this_arg = 2;
4361 break;
4363 /* Dump out the directories specified with LIBRARY_PATH,
4364 followed by the absolute directories
4365 that we search for startfiles. */
4366 case 'D':
4368 struct prefix_list *pl = startfile_prefixes.plist;
4369 size_t bufsize = 100;
4370 char *buffer = (char *) xmalloc (bufsize);
4371 int idx;
4373 for (; pl; pl = pl->next)
4375 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4376 /* Used on systems which record the specified -L dirs
4377 and use them to search for dynamic linking. */
4378 /* Relative directories always come from -B,
4379 and it is better not to use them for searching
4380 at run time. In particular, stage1 loses. */
4381 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4382 continue;
4383 #endif
4384 /* Try subdirectory if there is one. */
4385 if (multilib_dir != NULL)
4387 if (machine_suffix)
4389 if (strlen (pl->prefix) + strlen (machine_suffix)
4390 >= bufsize)
4391 bufsize = (strlen (pl->prefix)
4392 + strlen (machine_suffix)) * 2 + 1;
4393 buffer = (char *) xrealloc (buffer, bufsize);
4394 strcpy (buffer, pl->prefix);
4395 strcat (buffer, machine_suffix);
4396 if (is_directory (buffer, multilib_dir, 1))
4398 do_spec_1 ("-L", 0, NULL);
4399 #ifdef SPACE_AFTER_L_OPTION
4400 do_spec_1 (" ", 0, NULL);
4401 #endif
4402 do_spec_1 (buffer, 1, NULL);
4403 do_spec_1 (multilib_dir, 1, NULL);
4404 /* Make this a separate argument. */
4405 do_spec_1 (" ", 0, NULL);
4408 if (!pl->require_machine_suffix)
4410 if (is_directory (pl->prefix, multilib_dir, 1))
4412 do_spec_1 ("-L", 0, NULL);
4413 #ifdef SPACE_AFTER_L_OPTION
4414 do_spec_1 (" ", 0, NULL);
4415 #endif
4416 do_spec_1 (pl->prefix, 1, NULL);
4417 do_spec_1 (multilib_dir, 1, NULL);
4418 /* Make this a separate argument. */
4419 do_spec_1 (" ", 0, NULL);
4423 if (machine_suffix)
4425 if (is_directory (pl->prefix, machine_suffix, 1))
4427 do_spec_1 ("-L", 0, NULL);
4428 #ifdef SPACE_AFTER_L_OPTION
4429 do_spec_1 (" ", 0, NULL);
4430 #endif
4431 do_spec_1 (pl->prefix, 1, NULL);
4432 /* Remove slash from machine_suffix. */
4433 if (strlen (machine_suffix) >= bufsize)
4434 bufsize = strlen (machine_suffix) * 2 + 1;
4435 buffer = (char *) xrealloc (buffer, bufsize);
4436 strcpy (buffer, machine_suffix);
4437 idx = strlen (buffer);
4438 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4439 buffer[idx - 1] = 0;
4440 do_spec_1 (buffer, 1, NULL);
4441 /* Make this a separate argument. */
4442 do_spec_1 (" ", 0, NULL);
4445 if (!pl->require_machine_suffix)
4447 if (is_directory (pl->prefix, "", 1))
4449 do_spec_1 ("-L", 0, NULL);
4450 #ifdef SPACE_AFTER_L_OPTION
4451 do_spec_1 (" ", 0, NULL);
4452 #endif
4453 /* Remove slash from pl->prefix. */
4454 if (strlen (pl->prefix) >= bufsize)
4455 bufsize = strlen (pl->prefix) * 2 + 1;
4456 buffer = (char *) xrealloc (buffer, bufsize);
4457 strcpy (buffer, pl->prefix);
4458 idx = strlen (buffer);
4459 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4460 buffer[idx - 1] = 0;
4461 do_spec_1 (buffer, 1, NULL);
4462 /* Make this a separate argument. */
4463 do_spec_1 (" ", 0, NULL);
4467 free (buffer);
4469 break;
4471 case 'e':
4472 /* %efoo means report an error with `foo' as error message
4473 and don't execute any more commands for this file. */
4475 const char *q = p;
4476 char *buf;
4477 while (*p != 0 && *p != '\n')
4478 p++;
4479 buf = (char *) alloca (p - q + 1);
4480 strncpy (buf, q, p - q);
4481 buf[p - q] = 0;
4482 error ("%s", buf);
4483 return -1;
4485 break;
4486 case 'n':
4487 /* %nfoo means report an notice with `foo' on stderr. */
4489 const char *q = p;
4490 char *buf;
4491 while (*p != 0 && *p != '\n')
4492 p++;
4493 buf = (char *) alloca (p - q + 1);
4494 strncpy (buf, q, p - q);
4495 buf[p - q] = 0;
4496 notice ("%s\n", buf);
4497 if (*p)
4498 p++;
4500 break;
4502 case 'j':
4504 struct stat st;
4506 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4507 and it is not a directory, and it is writable, use it.
4508 Otherwise, fall through and treat this like any other
4509 temporary file. */
4511 if ((!save_temps_flag)
4512 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4513 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4515 obstack_grow (&obstack, HOST_BIT_BUCKET,
4516 strlen (HOST_BIT_BUCKET));
4517 delete_this_arg = 0;
4518 arg_going = 1;
4519 break;
4522 case 'g':
4523 case 'u':
4524 case 'U':
4526 struct temp_name *t;
4527 int suffix_length;
4528 const char *suffix = p;
4529 char *saved_suffix = NULL;
4531 while (*p == '.' || ISALPHA ((unsigned char) *p))
4532 p++;
4533 suffix_length = p - suffix;
4534 if (p[0] == '%' && p[1] == 'O')
4536 p += 2;
4537 /* We don't support extra suffix characters after %O. */
4538 if (*p == '.' || ISALPHA ((unsigned char) *p))
4539 abort ();
4540 if (suffix_length == 0)
4541 suffix = TARGET_OBJECT_SUFFIX;
4542 else
4544 saved_suffix
4545 = (char *) xmalloc (suffix_length
4546 + strlen (TARGET_OBJECT_SUFFIX));
4547 strncpy (saved_suffix, suffix, suffix_length);
4548 strcpy (saved_suffix + suffix_length,
4549 TARGET_OBJECT_SUFFIX);
4551 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
4554 /* If the input_filename has the same suffix specified
4555 for the %g, %u, or %U, and -save-temps is specified,
4556 we could end up using that file as an intermediate
4557 thus clobbering the user's source file (.e.g.,
4558 gcc -save-temps foo.s would clobber foo.s with the
4559 output of cpp0). So check for this condition and
4560 generate a temp file as the intermediate. */
4562 if (save_temps_flag)
4564 temp_filename_length = basename_length + suffix_length;
4565 temp_filename = alloca (temp_filename_length + 1);
4566 strncpy ((char *) temp_filename, input_basename, basename_length);
4567 strncpy ((char *) temp_filename + basename_length, suffix,
4568 suffix_length);
4569 *((char *) temp_filename + temp_filename_length) = '\0';
4570 if (strcmp (temp_filename, input_filename) != 0)
4572 struct stat st_temp;
4574 /* Note, set_input() resets input_stat_set to 0. */
4575 if (input_stat_set == 0)
4577 input_stat_set = stat (input_filename, &input_stat);
4578 if (input_stat_set >= 0)
4579 input_stat_set = 1;
4582 /* If we have the stat for the input_filename
4583 and we can do the stat for the temp_filename
4584 then the they could still refer to the same
4585 file if st_dev/st_ino's are the same. */
4587 if (input_stat_set != 1
4588 || stat (temp_filename, &st_temp) < 0
4589 || input_stat.st_dev != st_temp.st_dev
4590 || input_stat.st_ino != st_temp.st_ino)
4592 temp_filename = save_string (temp_filename,
4593 temp_filename_length + 1);
4594 obstack_grow (&obstack, temp_filename,
4595 temp_filename_length);
4596 arg_going = 1;
4597 break;
4602 /* See if we already have an association of %g/%u/%U and
4603 suffix. */
4604 for (t = temp_names; t; t = t->next)
4605 if (t->length == suffix_length
4606 && strncmp (t->suffix, suffix, suffix_length) == 0
4607 && t->unique == (c != 'g'))
4608 break;
4610 /* Make a new association if needed. %u and %j
4611 require one. */
4612 if (t == 0 || c == 'u' || c == 'j')
4614 if (t == 0)
4616 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4617 t->next = temp_names;
4618 temp_names = t;
4620 t->length = suffix_length;
4621 if (saved_suffix)
4623 t->suffix = saved_suffix;
4624 saved_suffix = NULL;
4626 else
4627 t->suffix = save_string (suffix, suffix_length);
4628 t->unique = (c != 'g');
4629 temp_filename = make_temp_file (t->suffix);
4630 temp_filename_length = strlen (temp_filename);
4631 t->filename = temp_filename;
4632 t->filename_length = temp_filename_length;
4635 if (saved_suffix)
4636 free (saved_suffix);
4638 obstack_grow (&obstack, t->filename, t->filename_length);
4639 delete_this_arg = 1;
4641 arg_going = 1;
4642 break;
4644 case 'i':
4645 obstack_grow (&obstack, input_filename, input_filename_length);
4646 arg_going = 1;
4647 break;
4649 case 'I':
4651 struct prefix_list *pl = include_prefixes.plist;
4653 if (gcc_exec_prefix)
4655 do_spec_1 ("-iprefix", 1, NULL);
4656 /* Make this a separate argument. */
4657 do_spec_1 (" ", 0, NULL);
4658 do_spec_1 (gcc_exec_prefix, 1, NULL);
4659 do_spec_1 (" ", 0, NULL);
4662 for (; pl; pl = pl->next)
4664 do_spec_1 ("-isystem", 1, NULL);
4665 /* Make this a separate argument. */
4666 do_spec_1 (" ", 0, NULL);
4667 do_spec_1 (pl->prefix, 1, NULL);
4668 do_spec_1 (" ", 0, NULL);
4671 break;
4673 case 'o':
4675 int max = n_infiles;
4676 max += lang_specific_extra_outfiles;
4678 for (i = 0; i < max; i++)
4679 if (outfiles[i])
4680 store_arg (outfiles[i], 0, 0);
4681 break;
4684 case 'O':
4685 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
4686 arg_going = 1;
4687 break;
4689 case 's':
4690 this_is_library_file = 1;
4691 break;
4693 case 'w':
4694 this_is_output_file = 1;
4695 break;
4697 case 'W':
4699 int cur_index = argbuf_index;
4700 /* Handle the {...} following the %W. */
4701 if (*p != '{')
4702 abort ();
4703 p = handle_braces (p + 1);
4704 if (p == 0)
4705 return -1;
4706 /* If any args were output, mark the last one for deletion
4707 on failure. */
4708 if (argbuf_index != cur_index)
4709 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4710 break;
4713 /* %x{OPTION} records OPTION for %X to output. */
4714 case 'x':
4716 const char *p1 = p;
4717 char *string;
4719 /* Skip past the option value and make a copy. */
4720 if (*p != '{')
4721 abort ();
4722 while (*p++ != '}')
4724 string = save_string (p1 + 1, p - p1 - 2);
4726 /* See if we already recorded this option. */
4727 for (i = 0; i < n_linker_options; i++)
4728 if (! strcmp (string, linker_options[i]))
4730 free (string);
4731 return 0;
4734 /* This option is new; add it. */
4735 add_linker_option (string, strlen (string));
4737 break;
4739 /* Dump out the options accumulated previously using %x. */
4740 case 'X':
4741 for (i = 0; i < n_linker_options; i++)
4743 do_spec_1 (linker_options[i], 1, NULL);
4744 /* Make each accumulated option a separate argument. */
4745 do_spec_1 (" ", 0, NULL);
4747 break;
4749 /* Dump out the options accumulated previously using -Wa,. */
4750 case 'Y':
4751 for (i = 0; i < n_assembler_options; i++)
4753 do_spec_1 (assembler_options[i], 1, NULL);
4754 /* Make each accumulated option a separate argument. */
4755 do_spec_1 (" ", 0, NULL);
4757 break;
4759 /* Dump out the options accumulated previously using -Wp,. */
4760 case 'Z':
4761 for (i = 0; i < n_preprocessor_options; i++)
4763 do_spec_1 (preprocessor_options[i], 1, NULL);
4764 /* Make each accumulated option a separate argument. */
4765 do_spec_1 (" ", 0, NULL);
4767 break;
4769 /* Here are digits and numbers that just process
4770 a certain constant string as a spec. */
4772 case '1':
4773 value = do_spec_1 (cc1_spec, 0, NULL);
4774 if (value != 0)
4775 return value;
4776 break;
4778 case '2':
4779 value = do_spec_1 (cc1plus_spec, 0, NULL);
4780 if (value != 0)
4781 return value;
4782 break;
4784 case 'a':
4785 value = do_spec_1 (asm_spec, 0, NULL);
4786 if (value != 0)
4787 return value;
4788 break;
4790 case 'A':
4791 value = do_spec_1 (asm_final_spec, 0, NULL);
4792 if (value != 0)
4793 return value;
4794 break;
4796 case 'C':
4798 const char *const spec
4799 = (input_file_compiler->cpp_spec
4800 ? input_file_compiler->cpp_spec
4801 : cpp_spec);
4802 value = do_spec_1 (spec, 0, NULL);
4803 if (value != 0)
4804 return value;
4806 break;
4808 case 'E':
4809 value = do_spec_1 (endfile_spec, 0, NULL);
4810 if (value != 0)
4811 return value;
4812 break;
4814 case 'l':
4815 value = do_spec_1 (link_spec, 0, NULL);
4816 if (value != 0)
4817 return value;
4818 break;
4820 case 'L':
4821 value = do_spec_1 (lib_spec, 0, NULL);
4822 if (value != 0)
4823 return value;
4824 break;
4826 case 'G':
4827 value = do_spec_1 (libgcc_spec, 0, NULL);
4828 if (value != 0)
4829 return value;
4830 break;
4832 case 'M':
4833 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4835 char *p;
4836 const char *q;
4837 size_t len;
4839 len = strlen (multilib_dir);
4840 obstack_blank (&obstack, len + 1);
4841 p = obstack_next_free (&obstack) - (len + 1);
4843 *p++ = '_';
4844 for (q = multilib_dir; *q ; ++q, ++p)
4845 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4847 break;
4849 case 'p':
4851 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4852 char *buf = x;
4853 const char *y;
4855 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4856 y = cpp_predefines;
4857 while (*y != 0)
4859 if (! strncmp (y, "-D", 2))
4860 /* Copy the whole option. */
4861 while (*y && *y != ' ' && *y != '\t')
4862 *x++ = *y++;
4863 else if (*y == ' ' || *y == '\t')
4864 /* Copy whitespace to the result. */
4865 *x++ = *y++;
4866 /* Don't copy other options. */
4867 else
4868 y++;
4871 *x = 0;
4873 value = do_spec_1 (buf, 0, NULL);
4874 if (value != 0)
4875 return value;
4877 break;
4879 case 'P':
4881 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4882 char *buf = x;
4883 const char *y;
4885 /* Copy all of CPP_PREDEFINES into BUF,
4886 but force them all into the reserved name space if they
4887 aren't already there. The reserved name space is all
4888 identifiers beginning with two underscores or with one
4889 underscore and a capital letter. We do the forcing by
4890 adding up to two underscores to the beginning and end
4891 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4892 become __mips__. */
4893 y = cpp_predefines;
4894 while (*y != 0)
4896 if (! strncmp (y, "-D", 2))
4898 int flag = 0;
4900 *x++ = *y++;
4901 *x++ = *y++;
4903 if (*y != '_'
4904 || (*(y + 1) != '_'
4905 && ! ISUPPER ((unsigned char) *(y + 1))))
4907 /* Stick __ at front of macro name. */
4908 if (*y != '_')
4909 *x++ = '_';
4910 *x++ = '_';
4911 /* Arrange to stick __ at the end as well. */
4912 flag = 1;
4915 /* Copy the macro name. */
4916 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4917 *x++ = *y++;
4919 if (flag)
4921 if (x[-1] != '_')
4923 if (x[-2] != '_')
4924 *x++ = '_';
4925 *x++ = '_';
4929 /* Copy the value given, if any. */
4930 while (*y && *y != ' ' && *y != '\t')
4931 *x++ = *y++;
4933 else if (*y == ' ' || *y == '\t')
4934 /* Copy whitespace to the result. */
4935 *x++ = *y++;
4936 /* Don't copy -A options */
4937 else
4938 y++;
4940 *x++ = ' ';
4942 /* Copy all of CPP_PREDEFINES into BUF,
4943 but put __ after every -D. */
4944 y = cpp_predefines;
4945 while (*y != 0)
4947 if (! strncmp (y, "-D", 2))
4949 y += 2;
4951 if (*y != '_'
4952 || (*(y + 1) != '_'
4953 && ! ISUPPER ((unsigned char) *(y + 1))))
4955 /* Stick -D__ at front of macro name. */
4956 *x++ = '-';
4957 *x++ = 'D';
4958 if (*y != '_')
4959 *x++ = '_';
4960 *x++ = '_';
4962 /* Copy the macro name. */
4963 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4964 *x++ = *y++;
4966 /* Copy the value given, if any. */
4967 while (*y && *y != ' ' && *y != '\t')
4968 *x++ = *y++;
4970 else
4972 /* Do not copy this macro - we have just done it before */
4973 while (*y && *y != ' ' && *y != '\t')
4974 y++;
4977 else if (*y == ' ' || *y == '\t')
4978 /* Copy whitespace to the result. */
4979 *x++ = *y++;
4980 /* Don't copy -A options. */
4981 else
4982 y++;
4984 *x++ = ' ';
4986 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4987 y = cpp_predefines;
4988 while (*y != 0)
4990 if (! strncmp (y, "-A", 2))
4991 /* Copy the whole option. */
4992 while (*y && *y != ' ' && *y != '\t')
4993 *x++ = *y++;
4994 else if (*y == ' ' || *y == '\t')
4995 /* Copy whitespace to the result. */
4996 *x++ = *y++;
4997 /* Don't copy other options. */
4998 else
4999 y++;
5002 *x = 0;
5004 value = do_spec_1 (buf, 0, NULL);
5005 if (value != 0)
5006 return value;
5008 break;
5010 case 'S':
5011 value = do_spec_1 (startfile_spec, 0, NULL);
5012 if (value != 0)
5013 return value;
5014 break;
5016 /* Here we define characters other than letters and digits. */
5018 case '{':
5019 p = handle_braces (p);
5020 if (p == 0)
5021 return -1;
5022 break;
5024 case '%':
5025 obstack_1grow (&obstack, '%');
5026 break;
5028 case '.':
5030 unsigned len = 0;
5032 while (p[len] && p[len] != ' ' && p[len] != '%')
5033 len++;
5034 suffix_subst = save_string (p - 1, len + 1);
5035 p += len;
5037 break;
5039 case '*':
5040 if (soft_matched_part)
5042 do_spec_1 (soft_matched_part, 1, NULL);
5043 do_spec_1 (" ", 0, NULL);
5045 else
5046 /* Catch the case where a spec string contains something like
5047 '%{foo:%*}'. ie there is no * in the pattern on the left
5048 hand side of the :. */
5049 error ("spec failure: '%%*' has not been initialized by pattern match");
5050 break;
5052 /* Process a string found as the value of a spec given by name.
5053 This feature allows individual machine descriptions
5054 to add and use their own specs.
5055 %[...] modifies -D options the way %P does;
5056 %(...) uses the spec unmodified. */
5057 case '[':
5058 error ("warning: use of obsolete %%[ operator in specs");
5059 case '(':
5061 const char *name = p;
5062 struct spec_list *sl;
5063 int len;
5065 /* The string after the S/P is the name of a spec that is to be
5066 processed. */
5067 while (*p && *p != ')' && *p != ']')
5068 p++;
5070 /* See if it's in the list. */
5071 for (len = p - name, sl = specs; sl; sl = sl->next)
5072 if (sl->name_len == len && !strncmp (sl->name, name, len))
5074 name = *(sl->ptr_spec);
5075 #ifdef DEBUG_SPECS
5076 notice ("Processing spec %c%s%c, which is '%s'\n",
5077 c, sl->name, (c == '(') ? ')' : ']', name);
5078 #endif
5079 break;
5082 if (sl)
5084 if (c == '(')
5086 value = do_spec_1 (name, 0, NULL);
5087 if (value != 0)
5088 return value;
5090 else
5092 char *x = (char *) alloca (strlen (name) * 2 + 1);
5093 char *buf = x;
5094 const char *y = name;
5095 int flag = 0;
5097 /* Copy all of NAME into BUF, but put __ after
5098 every -D and at the end of each arg. */
5099 while (1)
5101 if (! strncmp (y, "-D", 2))
5103 *x++ = '-';
5104 *x++ = 'D';
5105 *x++ = '_';
5106 *x++ = '_';
5107 y += 2;
5108 flag = 1;
5109 continue;
5111 else if (flag
5112 && (*y == ' ' || *y == '\t' || *y == '='
5113 || *y == '}' || *y == 0))
5115 *x++ = '_';
5116 *x++ = '_';
5117 flag = 0;
5119 if (*y == 0)
5120 break;
5121 else
5122 *x++ = *y++;
5124 *x = 0;
5126 value = do_spec_1 (buf, 0, NULL);
5127 if (value != 0)
5128 return value;
5132 /* Discard the closing paren or bracket. */
5133 if (*p)
5134 p++;
5136 break;
5138 case 'v':
5140 int c1 = *p++; /* Select first or second version number. */
5141 const char *v = compiler_version;
5142 const char *q;
5143 static const char zeroc = '0';
5145 /* The format of the version string is
5146 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
5148 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
5149 while (! ISDIGIT (*v))
5150 v++;
5151 if (v > compiler_version && v[-1] != '-')
5152 abort ();
5154 /* If desired, advance to second version number. */
5155 if (c1 >= '2')
5157 /* Set V after the first period. */
5158 while (ISDIGIT (*v))
5159 v++;
5160 if (*v != '.')
5161 abort ();
5162 v++;
5165 /* If desired, advance to third version number.
5166 But don't complain if it's not present */
5167 if (c1 == '3')
5169 /* Set V after the second period. */
5170 while (ISDIGIT (*v))
5171 v++;
5172 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
5173 abort ();
5174 if (*v != 0)
5175 v++;
5178 /* Set Q at the next period or at the end. */
5179 q = v;
5180 while (ISDIGIT (*q))
5181 q++;
5182 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
5183 abort ();
5185 if (q > v)
5186 /* Put that part into the command. */
5187 obstack_grow (&obstack, v, q - v);
5188 else
5189 /* Default to "0" */
5190 obstack_grow (&obstack, &zeroc, 1);
5191 arg_going = 1;
5193 break;
5195 case '|':
5196 if (input_from_pipe)
5197 do_spec_1 ("-", 0, NULL);
5198 break;
5200 default:
5201 error ("spec failure: unrecognized spec option '%c'", c);
5202 break;
5204 break;
5206 case '\\':
5207 /* Backslash: treat next character as ordinary. */
5208 c = *p++;
5210 /* fall through */
5211 default:
5212 /* Ordinary character: put it into the current argument. */
5213 obstack_1grow (&obstack, c);
5214 arg_going = 1;
5217 /* End of string. */
5218 return 0;
5221 /* Return 0 if we call do_spec_1 and that returns -1. */
5223 static const char *
5224 handle_braces (p)
5225 const char *p;
5227 const char *filter, *body = NULL, *endbody = NULL;
5228 int pipe_p = 0;
5229 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
5230 int negate;
5231 int suffix;
5232 int include_blanks = 1;
5233 int elide_switch = 0;
5234 int ordered = 0;
5236 if (*p == '^')
5238 /* A '^' after the open-brace means to not give blanks before args. */
5239 include_blanks = 0;
5240 ++p;
5243 if (*p == '|')
5245 /* A `|' after the open-brace means,
5246 if the test fails, output a single minus sign rather than nothing.
5247 This is used in %{|!pipe:...}. */
5248 pipe_p = 1;
5249 ++p;
5252 if (*p == '<')
5254 /* A `<' after the open-brace means that the switch should be
5255 removed from the command-line. */
5256 elide_switch = 1;
5257 ++p;
5260 next_member:
5261 negate = suffix = 0;
5263 if (*p == '!')
5264 /* A `!' after the open-brace negates the condition:
5265 succeed if the specified switch is not present. */
5266 negate = 1, ++p;
5268 if (*p == '.')
5269 /* A `.' after the open-brace means test against the current suffix. */
5271 if (pipe_p)
5272 abort ();
5274 suffix = 1;
5275 ++p;
5278 if (elide_switch && (negate || pipe_p || suffix))
5280 /* It doesn't make sense to mix elision with other flags. We
5281 could fatal() here, but the standard seems to be to abort. */
5282 abort ();
5285 next_ampersand:
5286 filter = p;
5287 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
5288 p++;
5290 if (*p == '|' && (pipe_p || ordered))
5291 abort ();
5293 if (!body)
5295 if (*p != '}' && *p != '&')
5297 int count = 1;
5298 const char *q = p;
5300 while (*q++ != ':')
5301 continue;
5302 body = q;
5304 while (count > 0)
5306 if (*q == '{')
5307 count++;
5308 else if (*q == '}')
5309 count--;
5310 else if (*q == 0)
5311 fatal ("mismatched braces in specs");
5312 q++;
5314 endbody = q;
5316 else
5317 body = p, endbody = p + 1;
5320 if (suffix)
5322 int found = (input_suffix != 0
5323 && (long) strlen (input_suffix) == (long) (p - filter)
5324 && strncmp (input_suffix, filter, p - filter) == 0);
5326 if (body[0] == '}')
5327 abort ();
5329 if (negate != found
5330 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL) < 0)
5331 return 0;
5333 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5335 /* Substitute all matching switches as separate args. */
5336 int i;
5338 for (i = 0; i < n_switches; i++)
5339 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5340 && check_live_switch (i, p - 1 - filter))
5342 if (elide_switch)
5344 switches[i].live_cond = SWITCH_IGNORE;
5345 switches[i].validated = 1;
5347 else
5348 ordered = 1, switches[i].ordering = 1;
5351 else
5353 /* Test for presence of the specified switch. */
5354 int i;
5355 int present = 0;
5357 /* If name specified ends in *, as in {x*:...},
5358 check for %* and handle that case. */
5359 if (p[-1] == '*' && !negate)
5361 int substitution;
5362 const char *r = body;
5364 /* First see whether we have %*. */
5365 substitution = 0;
5366 while (r < endbody)
5368 if (*r == '%' && r[1] == '*')
5369 substitution = 1;
5370 r++;
5372 /* If we do, handle that case. */
5373 if (substitution)
5375 /* Substitute all matching switches as separate args.
5376 But do this by substituting for %*
5377 in the text that follows the colon. */
5379 unsigned hard_match_len = p - filter - 1;
5380 char *string = save_string (body, endbody - body - 1);
5382 for (i = 0; i < n_switches; i++)
5383 if (!strncmp (switches[i].part1, filter, hard_match_len)
5384 && check_live_switch (i, -1))
5386 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5387 /* Pass any arguments this switch has. */
5388 give_switch (i, 1, 1);
5389 suffix_subst = NULL;
5392 /* We didn't match. Try again. */
5393 if (*p++ == '|')
5394 goto next_member;
5395 return endbody;
5399 /* If name specified ends in *, as in {x*:...},
5400 check for presence of any switch name starting with x. */
5401 if (p[-1] == '*')
5403 for (i = 0; i < n_switches; i++)
5405 unsigned hard_match_len = p - filter - 1;
5407 if (!strncmp (switches[i].part1, filter, hard_match_len)
5408 && check_live_switch (i, hard_match_len))
5410 present = 1;
5411 break;
5415 /* Otherwise, check for presence of exact name specified. */
5416 else
5418 for (i = 0; i < n_switches; i++)
5420 if (!strncmp (switches[i].part1, filter, p - filter)
5421 && switches[i].part1[p - filter] == 0
5422 && check_live_switch (i, -1))
5424 present = 1;
5425 break;
5430 /* If it is as desired (present for %{s...}, absent for %{!s...})
5431 then substitute either the switch or the specified
5432 conditional text. */
5433 if (present != negate)
5435 if (elide_switch)
5437 switches[i].live_cond = SWITCH_IGNORE;
5438 switches[i].validated = 1;
5440 else if (ordered || *p == '&')
5441 ordered = 1, switches[i].ordering = 1;
5442 else if (*p == '}')
5443 give_switch (i, 0, include_blanks);
5444 else
5445 /* Even if many alternatives are matched, only output once. */
5446 true_once = 1;
5448 else if (pipe_p)
5450 /* Here if a %{|...} conditional fails: output a minus sign,
5451 which means "standard output" or "standard input". */
5452 do_spec_1 ("-", 0, NULL);
5453 return endbody;
5457 /* We didn't match; try again. */
5458 if (*p++ == '|')
5459 goto next_member;
5461 if (p[-1] == '&')
5463 body = 0;
5464 goto next_ampersand;
5467 if (ordered)
5469 int i;
5470 /* Doing this set of switches later preserves their command-line
5471 ordering. This is needed for e.g. -U, -D and -A. */
5472 for (i = 0; i < n_switches; i++)
5473 if (switches[i].ordering == 1)
5475 switches[i].ordering = 0;
5476 give_switch (i, 0, include_blanks);
5479 /* Process the spec just once, regardless of match count. */
5480 else if (true_once)
5482 if (do_spec_1 (save_string (body, endbody - body - 1),
5483 0, NULL) < 0)
5484 return 0;
5487 return endbody;
5490 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5491 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5492 spec, or -1 if either exact match or %* is used.
5494 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5495 whose value does not begin with "no-" is obsoleted by the same value
5496 with the "no-", similarly for a switch with the "no-" prefix. */
5498 static int
5499 check_live_switch (switchnum, prefix_length)
5500 int switchnum;
5501 int prefix_length;
5503 const char *name = switches[switchnum].part1;
5504 int i;
5506 /* In the common case of {<at-most-one-letter>*}, a negating
5507 switch would always match, so ignore that case. We will just
5508 send the conflicting switches to the compiler phase. */
5509 if (prefix_length >= 0 && prefix_length <= 1)
5510 return 1;
5512 /* If we already processed this switch and determined if it was
5513 live or not, return our past determination. */
5514 if (switches[switchnum].live_cond != 0)
5515 return switches[switchnum].live_cond > 0;
5517 /* Now search for duplicate in a manner that depends on the name. */
5518 switch (*name)
5520 case 'O':
5521 for (i = switchnum + 1; i < n_switches; i++)
5522 if (switches[i].part1[0] == 'O')
5524 switches[switchnum].validated = 1;
5525 switches[switchnum].live_cond = SWITCH_FALSE;
5526 return 0;
5528 break;
5530 case 'W': case 'f': case 'm':
5531 if (! strncmp (name + 1, "no-", 3))
5533 /* We have Xno-YYY, search for XYYY. */
5534 for (i = switchnum + 1; i < n_switches; i++)
5535 if (switches[i].part1[0] == name[0]
5536 && ! strcmp (&switches[i].part1[1], &name[4]))
5538 switches[switchnum].validated = 1;
5539 switches[switchnum].live_cond = SWITCH_FALSE;
5540 return 0;
5543 else
5545 /* We have XYYY, search for Xno-YYY. */
5546 for (i = switchnum + 1; i < n_switches; i++)
5547 if (switches[i].part1[0] == name[0]
5548 && switches[i].part1[1] == 'n'
5549 && switches[i].part1[2] == 'o'
5550 && switches[i].part1[3] == '-'
5551 && !strcmp (&switches[i].part1[4], &name[1]))
5553 switches[switchnum].validated = 1;
5554 switches[switchnum].live_cond = SWITCH_FALSE;
5555 return 0;
5558 break;
5561 /* Otherwise the switch is live. */
5562 switches[switchnum].live_cond = SWITCH_LIVE;
5563 return 1;
5566 /* Pass a switch to the current accumulating command
5567 in the same form that we received it.
5568 SWITCHNUM identifies the switch; it is an index into
5569 the vector of switches gcc received, which is `switches'.
5570 This cannot fail since it never finishes a command line.
5572 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5574 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5575 of the switch. */
5577 static void
5578 give_switch (switchnum, omit_first_word, include_blanks)
5579 int switchnum;
5580 int omit_first_word;
5581 int include_blanks;
5583 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5584 return;
5586 if (!omit_first_word)
5588 do_spec_1 ("-", 0, NULL);
5589 do_spec_1 (switches[switchnum].part1, 1, NULL);
5592 if (switches[switchnum].args != 0)
5594 const char **p;
5595 for (p = switches[switchnum].args; *p; p++)
5597 const char *arg = *p;
5599 if (include_blanks)
5600 do_spec_1 (" ", 0, NULL);
5601 if (suffix_subst)
5603 unsigned length = strlen (arg);
5604 int dot = 0;
5606 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5607 if (arg[length] == '.')
5609 ((char *)arg)[length] = 0;
5610 dot = 1;
5611 break;
5613 do_spec_1 (arg, 1, NULL);
5614 if (dot)
5615 ((char *)arg)[length] = '.';
5616 do_spec_1 (suffix_subst, 1, NULL);
5618 else
5619 do_spec_1 (arg, 1, NULL);
5623 do_spec_1 (" ", 0, NULL);
5624 switches[switchnum].validated = 1;
5627 /* Search for a file named NAME trying various prefixes including the
5628 user's -B prefix and some standard ones.
5629 Return the absolute file name found. If nothing is found, return NAME. */
5631 static const char *
5632 find_file (name)
5633 const char *name;
5635 char *newname;
5637 /* Try multilib_dir if it is defined. */
5638 if (multilib_dir != NULL)
5640 const char *const try = ACONCAT ((multilib_dir, dir_separator_str, name, NULL));
5642 newname = find_a_file (&startfile_prefixes, try, R_OK);
5644 /* If we don't find it in the multi library dir, then fall
5645 through and look for it in the normal places. */
5646 if (newname != NULL)
5647 return newname;
5650 newname = find_a_file (&startfile_prefixes, name, R_OK);
5651 return newname ? newname : name;
5654 /* Determine whether a directory exists. If LINKER, return 0 for
5655 certain fixed names not needed by the linker. If not LINKER, it is
5656 only important to return 0 if the host machine has a small ARG_MAX
5657 limit. */
5659 static int
5660 is_directory (path1, path2, linker)
5661 const char *path1;
5662 const char *path2;
5663 int linker;
5665 int len1 = strlen (path1);
5666 int len2 = strlen (path2);
5667 char *path = (char *) alloca (3 + len1 + len2);
5668 char *cp;
5669 struct stat st;
5671 #ifndef SMALL_ARG_MAX
5672 if (! linker)
5673 return 1;
5674 #endif
5676 /* Construct the path from the two parts. Ensure the string ends with "/.".
5677 The resulting path will be a directory even if the given path is a
5678 symbolic link. */
5679 memcpy (path, path1, len1);
5680 memcpy (path + len1, path2, len2);
5681 cp = path + len1 + len2;
5682 if (!IS_DIR_SEPARATOR (cp[-1]))
5683 *cp++ = DIR_SEPARATOR;
5684 *cp++ = '.';
5685 *cp = '\0';
5687 /* Exclude directories that the linker is known to search. */
5688 if (linker
5689 && ((cp - path == 6
5690 && strcmp (path, concat (dir_separator_str, "lib",
5691 dir_separator_str, ".", NULL)) == 0)
5692 || (cp - path == 10
5693 && strcmp (path, concat (dir_separator_str, "usr",
5694 dir_separator_str, "lib",
5695 dir_separator_str, ".", NULL)) == 0)))
5696 return 0;
5698 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5701 /* Set up the various global variables to indicate that we're processing
5702 the input file named FILENAME. */
5704 void
5705 set_input (filename)
5706 const char *filename;
5708 const char *p;
5710 input_filename = filename;
5711 input_filename_length = strlen (input_filename);
5713 input_basename = input_filename;
5714 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5715 /* Skip drive name so 'x:foo' is handled properly. */
5716 if (input_basename[1] == ':')
5717 input_basename += 2;
5718 #endif
5719 for (p = input_basename; *p; p++)
5720 if (IS_DIR_SEPARATOR (*p))
5721 input_basename = p + 1;
5723 /* Find a suffix starting with the last period,
5724 and set basename_length to exclude that suffix. */
5725 basename_length = strlen (input_basename);
5726 suffixed_basename_length = basename_length;
5727 p = input_basename + basename_length;
5728 while (p != input_basename && *p != '.')
5729 --p;
5730 if (*p == '.' && p != input_basename)
5732 basename_length = p - input_basename;
5733 input_suffix = p + 1;
5735 else
5736 input_suffix = "";
5738 /* If a spec for 'g', 'u', or 'U' is seen with -save-temps then
5739 we will need to do a stat on the input_filename. The
5740 INPUT_STAT_SET signals that the stat is needed. */
5741 input_stat_set = 0;
5744 /* On fatal signals, delete all the temporary files. */
5746 static void
5747 fatal_error (signum)
5748 int signum;
5750 signal (signum, SIG_DFL);
5751 delete_failure_queue ();
5752 delete_temp_files ();
5753 /* Get the same signal again, this time not handled,
5754 so its normal effect occurs. */
5755 kill (getpid (), signum);
5758 extern int main PARAMS ((int, const char *const *));
5761 main (argc, argv)
5762 int argc;
5763 const char *const *argv;
5765 size_t i;
5766 int value;
5767 int linker_was_run = 0;
5768 char *explicit_link_files;
5769 char *specs_file;
5770 const char *p;
5771 struct user_specs *uptr;
5773 p = argv[0] + strlen (argv[0]);
5774 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5775 --p;
5776 programname = p;
5778 xmalloc_set_program_name (programname);
5780 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5781 /* Perform host dependent initialization when needed. */
5782 GCC_DRIVER_HOST_INITIALIZATION;
5783 #endif
5785 gcc_init_libintl ();
5787 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5788 signal (SIGINT, fatal_error);
5789 #ifdef SIGHUP
5790 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5791 signal (SIGHUP, fatal_error);
5792 #endif
5793 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5794 signal (SIGTERM, fatal_error);
5795 #ifdef SIGPIPE
5796 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5797 signal (SIGPIPE, fatal_error);
5798 #endif
5799 #ifdef SIGCHLD
5800 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5801 receive the signal. A different setting is inheritable */
5802 signal (SIGCHLD, SIG_DFL);
5803 #endif
5805 argbuf_length = 10;
5806 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5808 obstack_init (&obstack);
5810 /* Build multilib_select, et. al from the separate lines that make up each
5811 multilib selection. */
5813 const char *const *q = multilib_raw;
5814 int need_space;
5816 obstack_init (&multilib_obstack);
5817 while ((p = *q++) != (char *) 0)
5818 obstack_grow (&multilib_obstack, p, strlen (p));
5820 obstack_1grow (&multilib_obstack, 0);
5821 multilib_select = obstack_finish (&multilib_obstack);
5823 q = multilib_matches_raw;
5824 while ((p = *q++) != (char *) 0)
5825 obstack_grow (&multilib_obstack, p, strlen (p));
5827 obstack_1grow (&multilib_obstack, 0);
5828 multilib_matches = obstack_finish (&multilib_obstack);
5830 q = multilib_exclusions_raw;
5831 while ((p = *q++) != (char *) 0)
5832 obstack_grow (&multilib_obstack, p, strlen (p));
5834 obstack_1grow (&multilib_obstack, 0);
5835 multilib_exclusions = obstack_finish (&multilib_obstack);
5837 need_space = FALSE;
5838 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5840 if (need_space)
5841 obstack_1grow (&multilib_obstack, ' ');
5842 obstack_grow (&multilib_obstack,
5843 multilib_defaults_raw[i],
5844 strlen (multilib_defaults_raw[i]));
5845 need_space = TRUE;
5848 obstack_1grow (&multilib_obstack, 0);
5849 multilib_defaults = obstack_finish (&multilib_obstack);
5852 /* Set up to remember the pathname of gcc and any options
5853 needed for collect. We use argv[0] instead of programname because
5854 we need the complete pathname. */
5855 obstack_init (&collect_obstack);
5856 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5857 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5858 putenv (obstack_finish (&collect_obstack));
5860 #ifdef INIT_ENVIRONMENT
5861 /* Set up any other necessary machine specific environment variables. */
5862 putenv (INIT_ENVIRONMENT);
5863 #endif
5865 /* Make a table of what switches there are (switches, n_switches).
5866 Make a table of specified input files (infiles, n_infiles).
5867 Decode switches that are handled locally. */
5869 process_command (argc, argv);
5871 /* Initialize the vector of specs to just the default.
5872 This means one element containing 0s, as a terminator. */
5874 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5875 memcpy ((char *) compilers, (char *) default_compilers,
5876 sizeof default_compilers);
5877 n_compilers = n_default_compilers;
5879 /* Read specs from a file if there is one. */
5881 machine_suffix = concat (spec_machine, dir_separator_str,
5882 spec_version, dir_separator_str, NULL);
5883 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
5885 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5886 /* Read the specs file unless it is a default one. */
5887 if (specs_file != 0 && strcmp (specs_file, "specs"))
5888 read_specs (specs_file, TRUE);
5889 else
5890 init_spec ();
5892 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5893 for any override of as, ld and libraries. */
5894 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5895 + strlen (just_machine_suffix)
5896 + sizeof ("specs"));
5898 strcpy (specs_file, standard_exec_prefix);
5899 strcat (specs_file, just_machine_suffix);
5900 strcat (specs_file, "specs");
5901 if (access (specs_file, R_OK) == 0)
5902 read_specs (specs_file, TRUE);
5904 /* If not cross-compiling, look for startfiles in the standard places. */
5905 if (*cross_compile == '0')
5907 if (*md_exec_prefix)
5909 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5910 PREFIX_PRIORITY_LAST, 0, NULL);
5911 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5912 PREFIX_PRIORITY_LAST, 0, NULL);
5915 if (*md_startfile_prefix)
5916 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5917 PREFIX_PRIORITY_LAST, 0, NULL);
5919 if (*md_startfile_prefix_1)
5920 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5921 PREFIX_PRIORITY_LAST, 0, NULL);
5923 /* If standard_startfile_prefix is relative, base it on
5924 standard_exec_prefix. This lets us move the installed tree
5925 as a unit. If GCC_EXEC_PREFIX is defined, base
5926 standard_startfile_prefix on that as well. */
5927 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5928 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5929 PREFIX_PRIORITY_LAST, 0, NULL);
5930 else
5932 if (gcc_exec_prefix)
5933 add_prefix (&startfile_prefixes,
5934 concat (gcc_exec_prefix, machine_suffix,
5935 standard_startfile_prefix, NULL),
5936 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5937 add_prefix (&startfile_prefixes,
5938 concat (standard_exec_prefix,
5939 machine_suffix,
5940 standard_startfile_prefix, NULL),
5941 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5944 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5945 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5946 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5947 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5948 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5949 add_prefix (&startfile_prefixes, "./", NULL,
5950 PREFIX_PRIORITY_LAST, 1, NULL);
5951 #endif
5953 else
5955 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5956 && gcc_exec_prefix)
5957 add_prefix (&startfile_prefixes,
5958 concat (gcc_exec_prefix, machine_suffix,
5959 standard_startfile_prefix, NULL),
5960 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5963 /* Process any user specified specs in the order given on the command
5964 line. */
5965 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5967 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5968 read_specs (filename ? filename : uptr->filename, FALSE);
5971 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5972 if (gcc_exec_prefix)
5973 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
5974 spec_version, dir_separator_str, NULL);
5976 /* Now we have the specs.
5977 Set the `valid' bits for switches that match anything in any spec. */
5979 validate_all_switches ();
5981 /* Now that we have the switches and the specs, set
5982 the subdirectory based on the options. */
5983 set_multilib_dir ();
5985 /* Warn about any switches that no pass was interested in. */
5987 for (i = 0; (int) i < n_switches; i++)
5988 if (! switches[i].validated)
5989 error ("unrecognized option `-%s'", switches[i].part1);
5991 /* Obey some of the options. */
5993 if (print_search_dirs)
5995 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5996 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5997 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5998 return (0);
6001 if (print_file_name)
6003 printf ("%s\n", find_file (print_file_name));
6004 return (0);
6007 if (print_prog_name)
6009 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
6010 printf ("%s\n", (newname ? newname : print_prog_name));
6011 return (0);
6014 if (print_multi_lib)
6016 print_multilib_info ();
6017 return (0);
6020 if (print_multi_directory)
6022 if (multilib_dir == NULL)
6023 printf (".\n");
6024 else
6025 printf ("%s\n", multilib_dir);
6026 return (0);
6029 if (target_help_flag)
6031 /* Print if any target specific options. */
6033 /* We do not exit here. Instead we have created a fake input file
6034 called 'target-dummy' which needs to be compiled, and we pass this
6035 on to the various sub-processes, along with the --target-help
6036 switch. */
6039 if (print_help_list)
6041 display_help ();
6043 if (! verbose_flag)
6045 printf (_("\nFor bug reporting instructions, please see:\n"));
6046 printf ("%s.\n", GCCBUGURL);
6048 return (0);
6051 /* We do not exit here. Instead we have created a fake input file
6052 called 'help-dummy' which needs to be compiled, and we pass this
6053 on the various sub-processes, along with the --help switch. */
6056 if (verbose_flag)
6058 int n;
6059 const char *thrmod;
6061 notice ("Configured with: %s\n", configuration_arguments);
6063 #ifdef THREAD_MODEL_SPEC
6064 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
6065 but there's no point in doing all this processing just to get
6066 thread_model back. */
6067 obstack_init (&obstack);
6068 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
6069 obstack_1grow (&obstack, '\0');
6070 thrmod = obstack_finish (&obstack);
6071 #else
6072 thrmod = thread_model;
6073 #endif
6075 notice ("Thread model: %s\n", thrmod);
6077 /* compiler_version is truncated at the first space when initialized
6078 from version string, so truncate version_string at the first space
6079 before comparing. */
6080 for (n = 0; version_string[n]; n++)
6081 if (version_string[n] == ' ')
6082 break;
6084 if (! strncmp (version_string, compiler_version, n)
6085 && compiler_version[n] == 0)
6086 notice ("gcc version %s\n", version_string);
6087 else
6088 notice ("gcc driver version %s executing gcc version %s\n",
6089 version_string, compiler_version);
6091 if (n_infiles == 0)
6092 return (0);
6095 if (n_infiles == added_libraries)
6096 fatal ("no input files");
6098 /* Make a place to record the compiler output file names
6099 that correspond to the input files. */
6101 i = n_infiles;
6102 i += lang_specific_extra_outfiles;
6103 outfiles = (const char **) xcalloc (i, sizeof (char *));
6105 /* Record which files were specified explicitly as link input. */
6107 explicit_link_files = xcalloc (1, n_infiles);
6109 for (i = 0; (int) i < n_infiles; i++)
6111 int this_file_error = 0;
6113 /* Tell do_spec what to substitute for %i. */
6115 input_file_number = i;
6116 set_input (infiles[i].name);
6118 /* Use the same thing in %o, unless cp->spec says otherwise. */
6120 outfiles[i] = input_filename;
6122 /* Figure out which compiler from the file's suffix. */
6124 input_file_compiler
6125 = lookup_compiler (infiles[i].name, input_filename_length,
6126 infiles[i].language);
6128 if (input_file_compiler)
6130 /* Ok, we found an applicable compiler. Run its spec. */
6132 if (input_file_compiler->spec[0] == '#')
6134 error ("%s: %s compiler not installed on this system",
6135 input_filename, &input_file_compiler->spec[1]);
6136 this_file_error = 1;
6138 else
6140 value = do_spec (input_file_compiler->spec);
6141 if (value < 0)
6142 this_file_error = 1;
6146 /* If this file's name does not contain a recognized suffix,
6147 record it as explicit linker input. */
6149 else
6150 explicit_link_files[i] = 1;
6152 /* Clear the delete-on-failure queue, deleting the files in it
6153 if this compilation failed. */
6155 if (this_file_error)
6157 delete_failure_queue ();
6158 error_count++;
6160 /* If this compilation succeeded, don't delete those files later. */
6161 clear_failure_queue ();
6164 /* Reset the output file name to the first input file name, for use
6165 with %b in LINK_SPEC on a target that prefers not to emit a.out
6166 by default. */
6167 if (n_infiles > 0)
6168 set_input (infiles[0].name);
6170 if (error_count == 0)
6172 /* Make sure INPUT_FILE_NUMBER points to first available open
6173 slot. */
6174 input_file_number = n_infiles;
6175 if (lang_specific_pre_link ())
6176 error_count++;
6179 /* Run ld to link all the compiler output files. */
6181 if (error_count == 0)
6183 int tmp = execution_count;
6185 /* We'll use ld if we can't find collect2. */
6186 if (! strcmp (linker_name_spec, "collect2"))
6188 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
6189 if (s == NULL)
6190 linker_name_spec = "ld";
6192 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6193 for collect. */
6194 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
6195 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
6197 value = do_spec (link_command_spec);
6198 if (value < 0)
6199 error_count = 1;
6200 linker_was_run = (tmp != execution_count);
6203 /* If options said don't run linker,
6204 complain about input files to be given to the linker. */
6206 if (! linker_was_run && error_count == 0)
6207 for (i = 0; (int) i < n_infiles; i++)
6208 if (explicit_link_files[i])
6209 error ("%s: linker input file unused because linking not done",
6210 outfiles[i]);
6212 /* Delete some or all of the temporary files we made. */
6214 if (error_count)
6215 delete_failure_queue ();
6216 delete_temp_files ();
6218 if (print_help_list)
6220 printf (("\nFor bug reporting instructions, please see:\n"));
6221 printf ("%s\n", GCCBUGURL);
6224 return (signal_count != 0 ? 2
6225 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
6226 : 0);
6229 /* Find the proper compilation spec for the file name NAME,
6230 whose length is LENGTH. LANGUAGE is the specified language,
6231 or 0 if this file is to be passed to the linker. */
6233 static struct compiler *
6234 lookup_compiler (name, length, language)
6235 const char *name;
6236 size_t length;
6237 const char *language;
6239 struct compiler *cp;
6241 /* If this was specified by the user to be a linker input, indicate that. */
6242 if (language != 0 && language[0] == '*')
6243 return 0;
6245 /* Otherwise, look for the language, if one is spec'd. */
6246 if (language != 0)
6248 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6249 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6250 return cp;
6252 error ("language %s not recognized", language);
6253 return 0;
6256 /* Look for a suffix. */
6257 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6259 if (/* The suffix `-' matches only the file name `-'. */
6260 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6261 || (strlen (cp->suffix) < length
6262 /* See if the suffix matches the end of NAME. */
6263 && !strcmp (cp->suffix,
6264 name + length - strlen (cp->suffix))
6266 break;
6269 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6270 /* look again, but case-insensitively this time. */
6271 if (cp < compilers)
6272 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6274 if (/* The suffix `-' matches only the file name `-'. */
6275 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6276 || (strlen (cp->suffix) < length
6277 /* See if the suffix matches the end of NAME. */
6278 && ((!strcmp (cp->suffix,
6279 name + length - strlen (cp->suffix))
6280 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6281 && !strcasecmp (cp->suffix,
6282 name + length - strlen (cp->suffix)))
6284 break;
6286 #endif
6288 if (cp >= compilers)
6290 if (cp->spec[0] != '@')
6291 /* A non-alias entry: return it. */
6292 return cp;
6294 /* An alias entry maps a suffix to a language.
6295 Search for the language; pass 0 for NAME and LENGTH
6296 to avoid infinite recursion if language not found. */
6297 return lookup_compiler (NULL, 0, cp->spec + 1);
6299 return 0;
6302 static char *
6303 save_string (s, len)
6304 const char *s;
6305 int len;
6307 char *result = xmalloc (len + 1);
6309 memcpy (result, s, len);
6310 result[len] = 0;
6311 return result;
6314 void
6315 pfatal_with_name (name)
6316 const char *name;
6318 perror_with_name (name);
6319 delete_temp_files ();
6320 exit (1);
6323 static void
6324 perror_with_name (name)
6325 const char *name;
6327 error ("%s: %s", name, xstrerror (errno));
6330 static void
6331 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6332 const char *errmsg_fmt;
6333 const char *errmsg_arg;
6335 if (errmsg_arg)
6337 int save_errno = errno;
6339 /* Space for trailing '\0' is in %s. */
6340 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6341 sprintf (msg, errmsg_fmt, errmsg_arg);
6342 errmsg_fmt = msg;
6344 errno = save_errno;
6347 pfatal_with_name (errmsg_fmt);
6350 /* Output an error message and exit */
6352 void
6353 fancy_abort ()
6355 fatal ("internal gcc abort");
6358 /* Output an error message and exit */
6360 void
6361 fatal VPARAMS ((const char *msgid, ...))
6363 VA_OPEN (ap, msgid);
6364 VA_FIXEDARG (ap, const char *, msgid);
6366 fprintf (stderr, "%s: ", programname);
6367 vfprintf (stderr, _(msgid), ap);
6368 VA_CLOSE (ap);
6369 fprintf (stderr, "\n");
6370 delete_temp_files ();
6371 exit (1);
6374 void
6375 error VPARAMS ((const char *msgid, ...))
6377 VA_OPEN (ap, msgid);
6378 VA_FIXEDARG (ap, const char *, msgid);
6380 fprintf (stderr, "%s: ", programname);
6381 vfprintf (stderr, _(msgid), ap);
6382 VA_CLOSE (ap);
6384 fprintf (stderr, "\n");
6387 static void
6388 notice VPARAMS ((const char *msgid, ...))
6390 VA_OPEN (ap, msgid);
6391 VA_FIXEDARG (ap, const char *, msgid);
6393 vfprintf (stderr, _(msgid), ap);
6394 VA_CLOSE (ap);
6397 static void
6398 validate_all_switches ()
6400 struct compiler *comp;
6401 const char *p;
6402 char c;
6403 struct spec_list *spec;
6405 for (comp = compilers; comp->spec; comp++)
6407 p = comp->spec;
6408 while ((c = *p++))
6409 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6410 /* We have a switch spec. */
6411 validate_switches (p + 1);
6414 /* Look through the linked list of specs read from the specs file. */
6415 for (spec = specs; spec; spec = spec->next)
6417 p = *(spec->ptr_spec);
6418 while ((c = *p++))
6419 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6420 /* We have a switch spec. */
6421 validate_switches (p + 1);
6424 p = link_command_spec;
6425 while ((c = *p++))
6426 if (c == '%' && (*p == '{' || (*p == 'W' && *++p == '{')))
6427 /* We have a switch spec. */
6428 validate_switches (p + 1);
6431 /* Look at the switch-name that comes after START
6432 and mark as valid all supplied switches that match it. */
6434 static void
6435 validate_switches (start)
6436 const char *start;
6438 const char *p = start;
6439 const char *filter;
6440 int i;
6441 int suffix;
6443 if (*p == '|')
6444 ++p;
6446 next_member:
6447 if (*p == '!')
6448 ++p;
6450 suffix = 0;
6451 if (*p == '.')
6452 suffix = 1, ++p;
6454 filter = p;
6455 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6456 p++;
6458 if (suffix)
6460 else if (p[-1] == '*')
6462 /* Mark all matching switches as valid. */
6463 for (i = 0; i < n_switches; i++)
6464 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6465 switches[i].validated = 1;
6467 else
6469 /* Mark an exact matching switch as valid. */
6470 for (i = 0; i < n_switches; i++)
6472 if (!strncmp (switches[i].part1, filter, p - filter)
6473 && switches[i].part1[p - filter] == 0)
6474 switches[i].validated = 1;
6478 if (*p++ == '|' || p[-1] == '&')
6479 goto next_member;
6482 /* Check whether a particular argument was used. The first time we
6483 canonicalize the switches to keep only the ones we care about. */
6485 static int
6486 used_arg (p, len)
6487 const char *p;
6488 int len;
6490 struct mswitchstr
6492 const char *str;
6493 const char *replace;
6494 int len;
6495 int rep_len;
6498 static struct mswitchstr *mswitches;
6499 static int n_mswitches;
6500 int i, j;
6502 if (!mswitches)
6504 struct mswitchstr *matches;
6505 const char *q;
6506 int cnt = 0;
6508 /* Break multilib_matches into the component strings of string
6509 and replacement string. */
6510 for (q = multilib_matches; *q != '\0'; q++)
6511 if (*q == ';')
6512 cnt++;
6514 matches =
6515 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6516 i = 0;
6517 q = multilib_matches;
6518 while (*q != '\0')
6520 matches[i].str = q;
6521 while (*q != ' ')
6523 if (*q == '\0')
6524 abort ();
6525 q++;
6527 matches[i].len = q - matches[i].str;
6529 matches[i].replace = ++q;
6530 while (*q != ';' && *q != '\0')
6532 if (*q == ' ')
6533 abort ();
6534 q++;
6536 matches[i].rep_len = q - matches[i].replace;
6537 i++;
6538 if (*q == ';')
6539 q++;
6542 /* Now build a list of the replacement string for switches that we care
6543 about. Make sure we allocate at least one entry. This prevents
6544 xmalloc from calling fatal, and prevents us from re-executing this
6545 block of code. */
6546 mswitches
6547 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6548 * (n_switches ? n_switches : 1));
6549 for (i = 0; i < n_switches; i++)
6551 int xlen = strlen (switches[i].part1);
6552 for (j = 0; j < cnt; j++)
6553 if (xlen == matches[j].len
6554 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6556 mswitches[n_mswitches].str = matches[j].replace;
6557 mswitches[n_mswitches].len = matches[j].rep_len;
6558 mswitches[n_mswitches].replace = (char *) 0;
6559 mswitches[n_mswitches].rep_len = 0;
6560 n_mswitches++;
6561 break;
6566 for (i = 0; i < n_mswitches; i++)
6567 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6568 return 1;
6570 return 0;
6573 static int
6574 default_arg (p, len)
6575 const char *p;
6576 int len;
6578 const char *start, *end;
6580 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6582 while (*start == ' ' || *start == '\t')
6583 start++;
6585 if (*start == '\0')
6586 break;
6588 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6591 if ((end - start) == len && strncmp (p, start, len) == 0)
6592 return 1;
6594 if (*end == '\0')
6595 break;
6598 return 0;
6601 /* Work out the subdirectory to use based on the options. The format of
6602 multilib_select is a list of elements. Each element is a subdirectory
6603 name followed by a list of options followed by a semicolon. The format
6604 of multilib_exclusions is the same, but without the preceding
6605 directory. First gcc will check the exclusions, if none of the options
6606 beginning with an exclamation point are present, and all of the other
6607 options are present, then we will ignore this completely. Passing
6608 that, gcc will consider each multilib_select in turn using the same
6609 rules for matching the options. If a match is found, that subdirectory
6610 will be used. */
6612 static void
6613 set_multilib_dir ()
6615 const char *p;
6616 unsigned int this_path_len;
6617 const char *this_path, *this_arg;
6618 int not_arg;
6619 int ok;
6621 p = multilib_exclusions;
6622 while (*p != '\0')
6624 /* Ignore newlines. */
6625 if (*p == '\n')
6627 ++p;
6628 continue;
6631 /* Check the arguments. */
6632 ok = 1;
6633 while (*p != ';')
6635 if (*p == '\0')
6636 abort ();
6638 if (! ok)
6640 ++p;
6641 continue;
6644 this_arg = p;
6645 while (*p != ' ' && *p != ';')
6647 if (*p == '\0')
6648 abort ();
6649 ++p;
6652 if (*this_arg != '!')
6653 not_arg = 0;
6654 else
6656 not_arg = 1;
6657 ++this_arg;
6660 ok = used_arg (this_arg, p - this_arg);
6661 if (not_arg)
6662 ok = ! ok;
6664 if (*p == ' ')
6665 ++p;
6668 if (ok)
6669 return;
6671 ++p;
6674 p = multilib_select;
6675 while (*p != '\0')
6677 /* Ignore newlines. */
6678 if (*p == '\n')
6680 ++p;
6681 continue;
6684 /* Get the initial path. */
6685 this_path = p;
6686 while (*p != ' ')
6688 if (*p == '\0')
6689 abort ();
6690 ++p;
6692 this_path_len = p - this_path;
6694 /* Check the arguments. */
6695 ok = 1;
6696 ++p;
6697 while (*p != ';')
6699 if (*p == '\0')
6700 abort ();
6702 if (! ok)
6704 ++p;
6705 continue;
6708 this_arg = p;
6709 while (*p != ' ' && *p != ';')
6711 if (*p == '\0')
6712 abort ();
6713 ++p;
6716 if (*this_arg != '!')
6717 not_arg = 0;
6718 else
6720 not_arg = 1;
6721 ++this_arg;
6724 /* If this is a default argument, we can just ignore it.
6725 This is true even if this_arg begins with '!'. Beginning
6726 with '!' does not mean that this argument is necessarily
6727 inappropriate for this library: it merely means that
6728 there is a more specific library which uses this
6729 argument. If this argument is a default, we need not
6730 consider that more specific library. */
6731 if (! default_arg (this_arg, p - this_arg))
6733 ok = used_arg (this_arg, p - this_arg);
6734 if (not_arg)
6735 ok = ! ok;
6738 if (*p == ' ')
6739 ++p;
6742 if (ok)
6744 if (this_path_len != 1
6745 || this_path[0] != '.')
6747 char *new_multilib_dir = xmalloc (this_path_len + 1);
6748 strncpy (new_multilib_dir, this_path, this_path_len);
6749 new_multilib_dir[this_path_len] = '\0';
6750 multilib_dir = new_multilib_dir;
6752 break;
6755 ++p;
6759 /* Print out the multiple library subdirectory selection
6760 information. This prints out a series of lines. Each line looks
6761 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6762 required. Only the desired options are printed out, the negative
6763 matches. The options are print without a leading dash. There are
6764 no spaces to make it easy to use the information in the shell.
6765 Each subdirectory is printed only once. This assumes the ordering
6766 generated by the genmultilib script. Also, we leave out ones that match
6767 the exclusions. */
6769 static void
6770 print_multilib_info ()
6772 const char *p = multilib_select;
6773 const char *last_path = 0, *this_path;
6774 int skip;
6775 unsigned int last_path_len = 0;
6777 while (*p != '\0')
6779 skip = 0;
6780 /* Ignore newlines. */
6781 if (*p == '\n')
6783 ++p;
6784 continue;
6787 /* Get the initial path. */
6788 this_path = p;
6789 while (*p != ' ')
6791 if (*p == '\0')
6792 abort ();
6793 ++p;
6796 /* Check for matches with the multilib_exclusions. We don't bother
6797 with the '!' in either list. If any of the exclusion rules match
6798 all of its options with the select rule, we skip it. */
6800 const char *e = multilib_exclusions;
6801 const char *this_arg;
6803 while (*e != '\0')
6805 int m = 1;
6806 /* Ignore newlines. */
6807 if (*e == '\n')
6809 ++e;
6810 continue;
6813 /* Check the arguments. */
6814 while (*e != ';')
6816 const char *q;
6817 int mp = 0;
6819 if (*e == '\0')
6820 abort ();
6822 if (! m)
6824 ++e;
6825 continue;
6828 this_arg = e;
6830 while (*e != ' ' && *e != ';')
6832 if (*e == '\0')
6833 abort ();
6834 ++e;
6837 q = p + 1;
6838 while (*q != ';')
6840 const char *arg;
6841 int len = e - this_arg;
6843 if (*q == '\0')
6844 abort ();
6846 arg = q;
6848 while (*q != ' ' && *q != ';')
6850 if (*q == '\0')
6851 abort ();
6852 ++q;
6855 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6856 default_arg (this_arg, e - this_arg))
6858 mp = 1;
6859 break;
6862 if (*q == ' ')
6863 ++q;
6866 if (! mp)
6867 m = 0;
6869 if (*e == ' ')
6870 ++e;
6873 if (m)
6875 skip = 1;
6876 break;
6879 if (*e != '\0')
6880 ++e;
6884 if (! skip)
6886 /* If this is a duplicate, skip it. */
6887 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6888 && ! strncmp (last_path, this_path, last_path_len));
6890 last_path = this_path;
6891 last_path_len = p - this_path;
6894 /* If this directory requires any default arguments, we can skip
6895 it. We will already have printed a directory identical to
6896 this one which does not require that default argument. */
6897 if (! skip)
6899 const char *q;
6901 q = p + 1;
6902 while (*q != ';')
6904 const char *arg;
6906 if (*q == '\0')
6907 abort ();
6909 if (*q == '!')
6910 arg = NULL;
6911 else
6912 arg = q;
6914 while (*q != ' ' && *q != ';')
6916 if (*q == '\0')
6917 abort ();
6918 ++q;
6921 if (arg != NULL
6922 && default_arg (arg, q - arg))
6924 skip = 1;
6925 break;
6928 if (*q == ' ')
6929 ++q;
6933 if (! skip)
6935 const char *p1;
6937 for (p1 = last_path; p1 < p; p1++)
6938 putchar (*p1);
6939 putchar (';');
6942 ++p;
6943 while (*p != ';')
6945 int use_arg;
6947 if (*p == '\0')
6948 abort ();
6950 if (skip)
6952 ++p;
6953 continue;
6956 use_arg = *p != '!';
6958 if (use_arg)
6959 putchar ('@');
6961 while (*p != ' ' && *p != ';')
6963 if (*p == '\0')
6964 abort ();
6965 if (use_arg)
6966 putchar (*p);
6967 ++p;
6970 if (*p == ' ')
6971 ++p;
6974 if (! skip)
6976 /* If there are extra options, print them now. */
6977 if (multilib_extra && *multilib_extra)
6979 int print_at = TRUE;
6980 const char *q;
6982 for (q = multilib_extra; *q != '\0'; q++)
6984 if (*q == ' ')
6985 print_at = TRUE;
6986 else
6988 if (print_at)
6989 putchar ('@');
6990 putchar (*q);
6991 print_at = FALSE;
6996 putchar ('\n');
6999 ++p;