PR c++/3637
[official-gcc.git] / gcc / gcc.c
blobfc9ee62a02cb223cb63d13e18a9b572a244a7ddc
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 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"
84 #ifdef HAVE_SYS_RESOURCE_H
85 #include <sys/resource.h>
86 #endif
87 #if defined (HAVE_DECL_GETRUSAGE) && !HAVE_DECL_GETRUSAGE
88 extern int getrusage PARAMS ((int, struct rusage *));
89 #endif
91 /* By default there is no special suffix for target executables. */
92 /* FIXME: when autoconf is fixed, remove the host check - dj */
93 #if defined(TARGET_EXECUTABLE_SUFFIX) && defined(HOST_EXECUTABLE_SUFFIX)
94 #define HAVE_TARGET_EXECUTABLE_SUFFIX
95 #else
96 #undef TARGET_EXECUTABLE_SUFFIX
97 #define TARGET_EXECUTABLE_SUFFIX ""
98 #endif
100 /* By default there is no special suffix for host executables. */
101 #ifdef HOST_EXECUTABLE_SUFFIX
102 #define HAVE_HOST_EXECUTABLE_SUFFIX
103 #else
104 #define HOST_EXECUTABLE_SUFFIX ""
105 #endif
107 /* By default, the suffix for target object files is ".o". */
108 #ifdef TARGET_OBJECT_SUFFIX
109 #define HAVE_TARGET_OBJECT_SUFFIX
110 #else
111 #define TARGET_OBJECT_SUFFIX ".o"
112 #endif
114 #ifndef VMS
115 /* FIXME: the location independence code for VMS is hairier than this,
116 and hasn't been written. */
117 #ifndef DIR_UP
118 #define DIR_UP ".."
119 #endif /* DIR_UP */
120 #endif /* VMS */
122 static const char dir_separator_str[] = { DIR_SEPARATOR, 0 };
124 #define obstack_chunk_alloc xmalloc
125 #define obstack_chunk_free free
127 #ifndef GET_ENV_PATH_LIST
128 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
129 #endif
131 /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
132 #ifndef LIBRARY_PATH_ENV
133 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
134 #endif
136 #ifndef HAVE_KILL
137 #define kill(p,s) raise(s)
138 #endif
140 /* If a stage of compilation returns an exit status >= 1,
141 compilation of that file ceases. */
143 #define MIN_FATAL_STATUS 1
145 /* Flag saying to pass the greatest exit code returned by a sub-process
146 to the calling program. */
147 static int pass_exit_codes;
149 /* Definition of string containing the arguments given to configure. */
150 #include "configargs.h"
152 /* Flag saying to print the directories gcc will search through looking for
153 programs, libraries, etc. */
155 static int print_search_dirs;
157 /* Flag saying to print the full filename of this file
158 as found through our usual search mechanism. */
160 static const char *print_file_name = NULL;
162 /* As print_file_name, but search for executable file. */
164 static const char *print_prog_name = NULL;
166 /* Flag saying to print the relative path we'd use to
167 find libgcc.a given the current compiler flags. */
169 static int print_multi_directory;
171 /* Flag saying to print the list of subdirectories and
172 compiler flags used to select them in a standard form. */
174 static int print_multi_lib;
176 /* Flag saying to print the command line options understood by gcc and its
177 sub-processes. */
179 static int print_help_list;
181 /* Flag indicating whether we should print the command and arguments */
183 static int verbose_flag;
185 /* Flag indicating to print target specific command line options. */
187 static int target_help_flag;
189 /* Flag indicating whether we should report subprocess execution times
190 (if this is supported by the system - see pexecute.c). */
192 static int report_times;
194 /* Nonzero means write "temp" files in source directory
195 and use the source file's name in them, and don't delete them. */
197 static int save_temps_flag;
199 /* The compiler version. */
201 static const char *compiler_version;
203 /* The target version specified with -V */
205 static const char *spec_version = DEFAULT_TARGET_VERSION;
207 /* The target machine specified with -b. */
209 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
211 /* Nonzero if cross-compiling.
212 When -b is used, the value comes from the `specs' file. */
214 #ifdef CROSS_COMPILE
215 static const char *cross_compile = "1";
216 #else
217 static const char *cross_compile = "0";
218 #endif
220 #ifdef MODIFY_TARGET_NAME
222 /* Information on how to alter the target name based on a command-line
223 switch. The only case we support now is simply appending or deleting a
224 string to or from the end of the first part of the configuration name. */
226 const struct modify_target
228 const char *const sw;
229 const enum add_del {ADD, DELETE} add_del;
230 const char *const str;
232 modify_target[] = MODIFY_TARGET_NAME;
233 #endif
235 /* The number of errors that have occurred; the link phase will not be
236 run if this is non-zero. */
237 static int error_count = 0;
239 /* Greatest exit code of sub-processes that has been encountered up to
240 now. */
241 static int greatest_status = 1;
243 /* This is the obstack which we use to allocate many strings. */
245 static struct obstack obstack;
247 /* This is the obstack to build an environment variable to pass to
248 collect2 that describes all of the relevant switches of what to
249 pass the compiler in building the list of pointers to constructors
250 and destructors. */
252 static struct obstack collect_obstack;
254 /* These structs are used to collect resource usage information for
255 subprocesses. */
256 #ifdef HAVE_GETRUSAGE
257 static struct rusage rus, prus;
258 #endif
260 /* Forward declaration for prototypes. */
261 struct path_prefix;
263 static void init_spec PARAMS ((void));
264 #ifndef VMS
265 static char **split_directories PARAMS ((const char *, int *));
266 static void free_split_directories PARAMS ((char **));
267 static char *make_relative_prefix PARAMS ((const char *, const char *, const char *));
268 #endif /* VMS */
269 static void store_arg PARAMS ((const char *, int, int));
270 static char *load_specs PARAMS ((const char *));
271 static void read_specs PARAMS ((const char *, int));
272 static void set_spec PARAMS ((const char *, const char *));
273 static struct compiler *lookup_compiler PARAMS ((const char *, size_t, const char *));
274 static char *build_search_list PARAMS ((struct path_prefix *, const char *, int));
275 static void putenv_from_prefixes PARAMS ((struct path_prefix *, const char *));
276 static int access_check PARAMS ((const char *, int));
277 static char *find_a_file PARAMS ((struct path_prefix *, const char *, int));
278 static void add_prefix PARAMS ((struct path_prefix *, const char *,
279 const char *, int, int, int *));
280 static void translate_options PARAMS ((int *, const char *const **));
281 static char *skip_whitespace PARAMS ((char *));
282 static void delete_if_ordinary PARAMS ((const char *));
283 static void delete_temp_files PARAMS ((void));
284 static void delete_failure_queue PARAMS ((void));
285 static void clear_failure_queue PARAMS ((void));
286 static int check_live_switch PARAMS ((int, int));
287 static const char *handle_braces PARAMS ((const char *));
288 static char *save_string PARAMS ((const char *, int));
289 static int do_spec_1 PARAMS ((const char *, int, const char *));
290 static const char *find_file PARAMS ((const char *));
291 static int is_directory PARAMS ((const char *, const char *, int));
292 static void validate_switches PARAMS ((const char *));
293 static void validate_all_switches PARAMS ((void));
294 static void give_switch PARAMS ((int, int, int));
295 static int used_arg PARAMS ((const char *, int));
296 static int default_arg PARAMS ((const char *, int));
297 static void set_multilib_dir PARAMS ((void));
298 static void print_multilib_info PARAMS ((void));
299 static void perror_with_name PARAMS ((const char *));
300 static void pfatal_pexecute PARAMS ((const char *, const char *))
301 ATTRIBUTE_NORETURN;
302 static void notice PARAMS ((const char *, ...))
303 ATTRIBUTE_PRINTF_1;
304 static void display_help PARAMS ((void));
305 static void add_preprocessor_option PARAMS ((const char *, int));
306 static void add_assembler_option PARAMS ((const char *, int));
307 static void add_linker_option PARAMS ((const char *, int));
308 static void process_command PARAMS ((int, const char *const *));
309 static int execute PARAMS ((void));
310 static void clear_args PARAMS ((void));
311 static void fatal_error PARAMS ((int));
312 static void set_input PARAMS ((const char *));
313 #ifdef ENABLE_SHARED_LIBGCC
314 static void init_gcc_specs PARAMS ((struct obstack *,
315 const char *,
316 const char *));
317 #endif
318 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
319 static const char *convert_filename PARAMS ((const char *, int));
320 #endif
322 /* The Specs Language
324 Specs are strings containing lines, each of which (if not blank)
325 is made up of a program name, and arguments separated by spaces.
326 The program name must be exact and start from root, since no path
327 is searched and it is unreliable to depend on the current working directory.
328 Redirection of input or output is not supported; the subprograms must
329 accept filenames saying what files to read and write.
331 In addition, the specs can contain %-sequences to substitute variable text
332 or for conditional text. Here is a table of all defined %-sequences.
333 Note that spaces are not generated automatically around the results of
334 expanding these sequences; therefore, you can concatenate them together
335 or with constant text in a single argument.
337 %% substitute one % into the program name or argument.
338 %i substitute the name of the input file being processed.
339 %b substitute the basename of the input file being processed.
340 This is the substring up to (and not including) the last period
341 and not including the directory.
342 %B same as %b, but include the file suffix (text after the last period).
343 %gSUFFIX
344 substitute a file name that has suffix SUFFIX and is chosen
345 once per compilation, and mark the argument a la %d. To reduce
346 exposure to denial-of-service attacks, the file name is now
347 chosen in a way that is hard to predict even when previously
348 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
349 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
350 the regexp "[.A-Za-z]*%O"; "%O" is treated exactly as if it
351 had been pre-processed. Previously, %g was simply substituted
352 with a file name chosen once per compilation, without regard
353 to any appended suffix (which was therefore treated just like
354 ordinary text), making such attacks more likely to succeed.
355 %uSUFFIX
356 like %g, but generates a new temporary file name even if %uSUFFIX
357 was already seen.
358 %USUFFIX
359 substitutes the last file name generated with %uSUFFIX, generating a
360 new one if there is no such last file name. In the absence of any
361 %uSUFFIX, this is just like %gSUFFIX, except they don't share
362 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
363 would involve the generation of two distinct file names, one
364 for each `%g.s' and another for each `%U.s'. Previously, %U was
365 simply substituted with a file name chosen for the previous %u,
366 without regard to any appended suffix.
367 %jSUFFIX
368 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
369 writable, and if save-temps is off; otherwise, substitute the name
370 of a temporary file, just like %u. This temporary file is not
371 meant for communication between processes, but rather as a junk
372 disposal mechanism.
373 %.SUFFIX
374 substitutes .SUFFIX for the suffixes of a matched switch's args when
375 it is subsequently output with %*. SUFFIX is terminated by the next
376 space or %.
377 %d marks the argument containing or following the %d as a
378 temporary file name, so that that file will be deleted if CC exits
379 successfully. Unlike %g, this contributes no text to the argument.
380 %w marks the argument containing or following the %w as the
381 "output file" of this compilation. This puts the argument
382 into the sequence of arguments that %o will substitute later.
383 %W{...}
384 like %{...} but mark last argument supplied within
385 as a file to be deleted on failure.
386 %o substitutes the names of all the output files, with spaces
387 automatically placed around them. You should write spaces
388 around the %o as well or the results are undefined.
389 %o is for use in the specs for running the linker.
390 Input files whose names have no recognized suffix are not compiled
391 at all, but they are included among the output files, so they will
392 be linked.
393 %O substitutes the suffix for object files. Note that this is
394 handled specially when it immediately follows %g, %u, or %U
395 (with or without a suffix argument) because of the need for
396 those to form complete file names. The handling is such that
397 %O is treated exactly as if it had already been substituted,
398 except that %g, %u, and %U do not currently support additional
399 SUFFIX characters following %O as they would following, for
400 example, `.o'.
401 %p substitutes the standard macro predefinitions for the
402 current target machine. Use this when running cpp.
403 %P like %p, but puts `__' before and after the name of each macro.
404 (Except macros that already have __.)
405 This is for ANSI C.
406 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
407 %s current argument is the name of a library or startup file of some sort.
408 Search for that file in a standard list of directories
409 and substitute the full name found.
410 %eSTR Print STR as an error message. STR is terminated by a newline.
411 Use this when inconsistent options are detected.
412 %nSTR Print STR as an notice. STR is terminated by a newline.
413 %x{OPTION} Accumulate an option for %X.
414 %X Output the accumulated linker options specified by compilations.
415 %Y Output the accumulated assembler options specified by compilations.
416 %Z Output the accumulated preprocessor options specified by compilations.
417 %v1 Substitute the major version number of GCC.
418 (For version 2.5.3, this is 2.)
419 %v2 Substitute the minor version number of GCC.
420 (For version 2.5.3, this is 5.)
421 %v3 Substitute the patch level number of GCC.
422 (For version 2.5.3, this is 3.)
423 %a process ASM_SPEC as a spec.
424 This allows config.h to specify part of the spec for running as.
425 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
426 used here. This can be used to run a post-processor after the
427 assembler has done its job.
428 %D Dump out a -L option for each directory in startfile_prefixes.
429 If multilib_dir is set, extra entries are generated with it affixed.
430 %l process LINK_SPEC as a spec.
431 %L process LIB_SPEC as a spec.
432 %G process LIBGCC_SPEC as a spec.
433 %M output multilib_dir with directory separators replaced with "_";
434 if multilib_dir is not set or is ".", output "".
435 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
436 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
437 %c process SIGNED_CHAR_SPEC as a spec.
438 %C process CPP_SPEC as a spec.
439 %1 process CC1_SPEC as a spec.
440 %2 process CC1PLUS_SPEC as a spec.
441 %| output "-" if the input for the current command is coming from a pipe.
442 %* substitute the variable part of a matched option. (See below.)
443 Note that each comma in the substituted string is replaced by
444 a single space.
445 %{S} substitutes the -S switch, if that switch was given to CC.
446 If that switch was not specified, this substitutes nothing.
447 Here S is a metasyntactic variable.
448 %{S*} substitutes all the switches specified to CC whose names start
449 with -S. This is used for -o, -I, etc; switches that take
450 arguments. CC considers `-o foo' as being one switch whose
451 name starts with `o'. %{o*} would substitute this text,
452 including the space; thus, two arguments would be generated.
453 %{^S*} likewise, but don't put a blank between a switch and any args.
454 %{S*&T*} likewise, but preserve order of S and T options (the order
455 of S and T in the spec is not significant). Can be any number
456 of ampersand-separated variables; for each the wild card is
457 optional. Useful for CPP as %{D*&U*&A*}.
458 %{S*:X} substitutes X if one or more switches whose names start with -S are
459 specified to CC. Note that the tail part of the -S option
460 (i.e. the part matched by the `*') will be substituted for each
461 occurrence of %* within X.
462 %{<S} remove all occurrences of -S from the command line.
463 Note - this option is position dependent. % commands in the
464 spec string before this option will see -S, % commands in the
465 spec string after this option will not.
466 %{S:X} substitutes X, but only if the -S switch was given to CC.
467 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
468 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
469 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
470 %{.S:X} substitutes X, but only if processing a file with suffix S.
471 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
472 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
473 combined with ! and . as above binding stronger than the OR.
474 %(Spec) processes a specification defined in a specs file as *Spec:
475 %[Spec] as above, but put __ around -D arguments
477 The conditional text X in a %{S:X} or %{!S:X} construct may contain
478 other nested % constructs or spaces, or even newlines. They are
479 processed as usual, as described above.
481 The -O, -f, -m, and -W switches are handled specifically in these
482 constructs. If another value of -O or the negated form of a -f, -m, or
483 -W switch is found later in the command line, the earlier switch
484 value is ignored, except with {S*} where S is just one letter; this
485 passes all matching options.
487 The character | at the beginning of the predicate text is used to indicate
488 that a command should be piped to the following command, but only if -pipe
489 is specified.
491 Note that it is built into CC which switches take arguments and which
492 do not. You might think it would be useful to generalize this to
493 allow each compiler's spec to say which switches take arguments. But
494 this cannot be done in a consistent fashion. CC cannot even decide
495 which input files have been specified without knowing which switches
496 take arguments, and it must know which input files to compile in order
497 to tell which compilers to run.
499 CC also knows implicitly that arguments starting in `-l' are to be
500 treated as compiler output files, and passed to the linker in their
501 proper position among the other output files. */
503 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
505 /* config.h can define ASM_SPEC to provide extra args to the assembler
506 or extra switch-translations. */
507 #ifndef ASM_SPEC
508 #define ASM_SPEC ""
509 #endif
511 /* config.h can define ASM_FINAL_SPEC to run a post processor after
512 the assembler has run. */
513 #ifndef ASM_FINAL_SPEC
514 #define ASM_FINAL_SPEC ""
515 #endif
517 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
518 or extra switch-translations. */
519 #ifndef CPP_SPEC
520 #define CPP_SPEC ""
521 #endif
523 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
524 or extra switch-translations. */
525 #ifndef CC1_SPEC
526 #define CC1_SPEC ""
527 #endif
529 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
530 or extra switch-translations. */
531 #ifndef CC1PLUS_SPEC
532 #define CC1PLUS_SPEC ""
533 #endif
535 /* config.h can define LINK_SPEC to provide extra args to the linker
536 or extra switch-translations. */
537 #ifndef LINK_SPEC
538 #define LINK_SPEC ""
539 #endif
541 /* config.h can define LIB_SPEC to override the default libraries. */
542 #ifndef LIB_SPEC
543 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
544 #endif
546 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
547 included. */
548 #ifndef LIBGCC_SPEC
549 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
550 /* Have gcc do the search for libgcc.a. */
551 #define LIBGCC_SPEC "libgcc.a%s"
552 #else
553 #define LIBGCC_SPEC "-lgcc"
554 #endif
555 #endif
557 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
558 #ifndef STARTFILE_SPEC
559 #define STARTFILE_SPEC \
560 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
561 #endif
563 /* config.h can define SWITCHES_NEED_SPACES to control which options
564 require spaces between the option and the argument. */
565 #ifndef SWITCHES_NEED_SPACES
566 #define SWITCHES_NEED_SPACES ""
567 #endif
569 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
570 #ifndef ENDFILE_SPEC
571 #define ENDFILE_SPEC ""
572 #endif
574 /* This spec is used for telling cpp whether char is signed or not. */
575 #ifndef SIGNED_CHAR_SPEC
576 /* Use #if rather than ?:
577 because MIPS C compiler rejects like ?: in initializers. */
578 #if DEFAULT_SIGNED_CHAR
579 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
580 #else
581 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
582 #endif
583 #endif
585 #ifndef LINKER_NAME
586 #define LINKER_NAME "collect2"
587 #endif
589 /* Here is the spec for running the linker, after compiling all files. */
591 /* -u* was put back because both BSD and SysV seem to support it. */
592 /* %{static:} simply prevents an error message if the target machine
593 doesn't handle -static. */
594 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
595 scripts which exist in user specified directories, or in standard
596 directories. */
597 #ifndef LINK_COMMAND_SPEC
598 #define LINK_COMMAND_SPEC "\
599 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
600 %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\
601 %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
602 %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
603 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
604 #endif
606 #ifndef LINK_LIBGCC_SPEC
607 # ifdef LINK_LIBGCC_SPECIAL
608 /* Don't generate -L options for startfile prefix list. */
609 # define LINK_LIBGCC_SPEC ""
610 # else
611 /* Do generate them. */
612 # define LINK_LIBGCC_SPEC "%D"
613 # endif
614 #endif
616 static const char *cpp_spec = CPP_SPEC;
617 static const char *cpp_predefines = CPP_PREDEFINES;
618 static const char *cc1_spec = CC1_SPEC;
619 static const char *cc1plus_spec = CC1PLUS_SPEC;
620 static const char *signed_char_spec = SIGNED_CHAR_SPEC;
621 static const char *asm_spec = ASM_SPEC;
622 static const char *asm_final_spec = ASM_FINAL_SPEC;
623 static const char *link_spec = LINK_SPEC;
624 static const char *lib_spec = LIB_SPEC;
625 static const char *libgcc_spec = LIBGCC_SPEC;
626 static const char *endfile_spec = ENDFILE_SPEC;
627 static const char *startfile_spec = STARTFILE_SPEC;
628 static const char *switches_need_spaces = SWITCHES_NEED_SPACES;
629 static const char *linker_name_spec = LINKER_NAME;
630 static const char *link_command_spec = LINK_COMMAND_SPEC;
631 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
633 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
634 There should be no need to override these in target dependent files,
635 but we need to copy them to the specs file so that newer versions
636 of the GCC driver can correctly drive older tool chains with the
637 appropriate -B options. */
639 static const char *trad_capable_cpp =
640 "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
642 static const char *cpp_options =
643 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
644 %{std*} %{nostdinc*}\
645 %{C} %{v} %{I*} %{P} %{$} %I\
646 %{MD:-M -MF %W{!o: %b.d}%W{o*:%.d%*}}\
647 %{MMD:-MM -MF %W{!o: %b.d}%W{o*:%.d%*}}\
648 %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*} %{M|MD|MM|MMD:%{o*:-MQ %*}}\
649 %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
650 %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
651 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
652 %{fno-inline|O0|!O*:-D__NO_INLINE__} %{ffast-math:-D__FAST_MATH__}\
653 %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
654 %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
655 %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}}\
656 %{fshow-column} %{fno-show-column}\
657 %{fleading-underscore} %{fno-leading-underscore}\
658 %{fno-operator-names} %{ftabstop=*} %{remap}\
659 %{g3:-dD} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*&U*&A*} %{i*} %Z %i\
660 %{E:%{!M*:%W{o*}}}";
662 /* NB: This is shared amongst all front-ends. */
663 static const char *cc1_options =
664 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
665 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
666 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*} %{ansi}\
667 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
668 %{Qn:-fno-ident} %{--help:--help}\
669 %{--target-help:--target-help}\
670 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
671 %{fsyntax-only:-o %j} %{-param*}";
673 static const char *asm_options =
674 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
676 static const char *invoke_as =
677 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
679 /* Some compilers have limits on line lengths, and the multilib_select
680 and/or multilib_matches strings can be very long, so we build them at
681 run time. */
682 static struct obstack multilib_obstack;
683 static const char *multilib_select;
684 static const char *multilib_matches;
685 static const char *multilib_defaults;
686 static const char *multilib_exclusions;
687 #include "multilib.h"
689 /* Check whether a particular argument is a default argument. */
691 #ifndef MULTILIB_DEFAULTS
692 #define MULTILIB_DEFAULTS { "" }
693 #endif
695 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
697 struct user_specs
699 struct user_specs *next;
700 const char *filename;
703 static struct user_specs *user_specs_head, *user_specs_tail;
705 /* This defines which switch letters take arguments. */
707 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
708 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
709 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
710 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
711 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
712 || (CHAR) == 'B' || (CHAR) == 'b')
714 #ifndef SWITCH_TAKES_ARG
715 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
716 #endif
718 /* This defines which multi-letter switches take arguments. */
720 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
721 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
722 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
723 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
724 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
725 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
726 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
727 || !strcmp (STR, "specs") \
728 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
730 #ifndef WORD_SWITCH_TAKES_ARG
731 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
732 #endif
734 #ifdef HAVE_TARGET_EXECUTABLE_SUFFIX
735 /* This defines which switches stop a full compilation. */
736 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
737 ((CHAR) == 'c' || (CHAR) == 'S')
739 #ifndef SWITCH_CURTAILS_COMPILATION
740 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
741 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
742 #endif
743 #endif
745 /* Record the mapping from file suffixes for compilation specs. */
747 struct compiler
749 const char *suffix; /* Use this compiler for input files
750 whose names end in this suffix. */
752 const char *spec; /* To use this compiler, run this spec. */
754 const char *cpp_spec; /* If non-NULL, substitute this spec
755 for `%C', rather than the usual
756 cpp_spec. */
759 /* Pointer to a vector of `struct compiler' that gives the spec for
760 compiling a file, based on its suffix.
761 A file that does not end in any of these suffixes will be passed
762 unchanged to the loader and nothing else will be done to it.
764 An entry containing two 0s is used to terminate the vector.
766 If multiple entries match a file, the last matching one is used. */
768 static struct compiler *compilers;
770 /* Number of entries in `compilers', not counting the null terminator. */
772 static int n_compilers;
774 /* The default list of file name suffixes and their compilation specs. */
776 static struct compiler default_compilers[] =
778 /* Add lists of suffixes of known languages here. If those languages
779 were not present when we built the driver, we will hit these copies
780 and be given a more meaningful error than "file not used since
781 linking is not done". */
782 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
783 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
784 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
785 {".ii", "#C++", 0},
786 {".ads", "#Ada", 0}, {".adb", "#Ada", 0},
787 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
788 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
789 {".r", "#Ratfor", 0},
790 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
791 {".ch", "#Chill", 0}, {".chi", "#Chill", 0},
792 {".java", "#Java", 0}, {".class", "#Java", 0},
793 {".zip", "#Java", 0}, {".jar", "#Java", 0},
794 /* Next come the entries for C. */
795 {".c", "@c", 0},
796 {"@c",
797 /* cc1 has an integrated ISO C preprocessor. We should invoke the
798 external preprocessor if -save-temps or -traditional is given. */
799 "%{E|M|MM:%(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)}\
800 %{!E:%{!M:%{!MM:\
801 %{save-temps:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
802 %(cpp_options) %b.i \n\
803 cc1 -fpreprocessed %b.i %(cc1_options)}\
804 %{!save-temps:\
805 %{traditional|ftraditional|traditional-cpp:\
806 tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
807 cc1 -fpreprocessed %{!pipe:%g.i} %(cc1_options)}\
808 %{!traditional:%{!ftraditional:%{!traditional-cpp:\
809 cc1 -lang-c %{ansi:-std=c89} %(cpp_options) %(cc1_options)}}}}\
810 %{!fsyntax-only:%(invoke_as)}}}}", 0},
811 {"-",
812 "%{!E:%e-E required when input is from standard input}\
813 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
814 {".h", "@c-header", 0},
815 {"@c-header",
816 "%{!E:%eCompilation of header file requested} \
817 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
818 {".i", "@cpp-output", 0},
819 {"@cpp-output",
820 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
821 {".s", "@assembler", 0},
822 {"@assembler",
823 "%{!M:%{!MM:%{!E:%{!S:as %(asm_options) %i %A }}}}", 0},
824 {".S", "@assembler-with-cpp", 0},
825 {"@assembler-with-cpp",
826 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
827 %{!M:%{!MM:%{!E:%(invoke_as)}}}", 0},
828 #include "specs.h"
829 /* Mark end of table */
830 {0, 0, 0}
833 /* Number of elements in default_compilers, not counting the terminator. */
835 static int n_default_compilers
836 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
838 /* A vector of options to give to the linker.
839 These options are accumulated by %x,
840 and substituted into the linker command with %X. */
841 static int n_linker_options;
842 static char **linker_options;
844 /* A vector of options to give to the assembler.
845 These options are accumulated by -Wa,
846 and substituted into the assembler command with %Y. */
847 static int n_assembler_options;
848 static char **assembler_options;
850 /* A vector of options to give to the preprocessor.
851 These options are accumulated by -Wp,
852 and substituted into the preprocessor command with %Z. */
853 static int n_preprocessor_options;
854 static char **preprocessor_options;
856 /* Define how to map long options into short ones. */
858 /* This structure describes one mapping. */
859 struct option_map
861 /* The long option's name. */
862 const char *const name;
863 /* The equivalent short option. */
864 const char *const equivalent;
865 /* Argument info. A string of flag chars; NULL equals no options.
866 a => argument required.
867 o => argument optional.
868 j => join argument to equivalent, making one word.
869 * => require other text after NAME as an argument. */
870 const char *const arg_info;
873 /* This is the table of mappings. Mappings are tried sequentially
874 for each option encountered; the first one that matches, wins. */
876 static const struct option_map option_map[] =
878 {"--all-warnings", "-Wall", 0},
879 {"--ansi", "-ansi", 0},
880 {"--assemble", "-S", 0},
881 {"--assert", "-A", "a"},
882 {"--classpath", "-fclasspath=", "aj"},
883 {"--CLASSPATH", "-fCLASSPATH=", "aj"},
884 {"--comments", "-C", 0},
885 {"--compile", "-c", 0},
886 {"--debug", "-g", "oj"},
887 {"--define-macro", "-D", "aj"},
888 {"--dependencies", "-M", 0},
889 {"--dump", "-d", "a"},
890 {"--dumpbase", "-dumpbase", "a"},
891 {"--entry", "-e", 0},
892 {"--extra-warnings", "-W", 0},
893 {"--for-assembler", "-Wa", "a"},
894 {"--for-linker", "-Xlinker", "a"},
895 {"--force-link", "-u", "a"},
896 {"--imacros", "-imacros", "a"},
897 {"--include", "-include", "a"},
898 {"--include-barrier", "-I-", 0},
899 {"--include-directory", "-I", "aj"},
900 {"--include-directory-after", "-idirafter", "a"},
901 {"--include-prefix", "-iprefix", "a"},
902 {"--include-with-prefix", "-iwithprefix", "a"},
903 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
904 {"--include-with-prefix-after", "-iwithprefix", "a"},
905 {"--language", "-x", "a"},
906 {"--library-directory", "-L", "a"},
907 {"--machine", "-m", "aj"},
908 {"--machine-", "-m", "*j"},
909 {"--no-line-commands", "-P", 0},
910 {"--no-precompiled-includes", "-noprecomp", 0},
911 {"--no-standard-includes", "-nostdinc", 0},
912 {"--no-standard-libraries", "-nostdlib", 0},
913 {"--no-warnings", "-w", 0},
914 {"--optimize", "-O", "oj"},
915 {"--output", "-o", "a"},
916 {"--output-class-directory", "-foutput-class-dir=", "ja"},
917 {"--param", "--param", "a"},
918 {"--pedantic", "-pedantic", 0},
919 {"--pedantic-errors", "-pedantic-errors", 0},
920 {"--pipe", "-pipe", 0},
921 {"--prefix", "-B", "a"},
922 {"--preprocess", "-E", 0},
923 {"--print-search-dirs", "-print-search-dirs", 0},
924 {"--print-file-name", "-print-file-name=", "aj"},
925 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
926 {"--print-missing-file-dependencies", "-MG", 0},
927 {"--print-multi-lib", "-print-multi-lib", 0},
928 {"--print-multi-directory", "-print-multi-directory", 0},
929 {"--print-prog-name", "-print-prog-name=", "aj"},
930 {"--profile", "-p", 0},
931 {"--profile-blocks", "-a", 0},
932 {"--quiet", "-q", 0},
933 {"--save-temps", "-save-temps", 0},
934 {"--shared", "-shared", 0},
935 {"--silent", "-q", 0},
936 {"--specs", "-specs=", "aj"},
937 {"--static", "-static", 0},
938 {"--std", "-std=", "aj"},
939 {"--symbolic", "-symbolic", 0},
940 {"--target", "-b", "a"},
941 {"--time", "-time", 0},
942 {"--trace-includes", "-H", 0},
943 {"--traditional", "-traditional", 0},
944 {"--traditional-cpp", "-traditional-cpp", 0},
945 {"--trigraphs", "-trigraphs", 0},
946 {"--undefine-macro", "-U", "aj"},
947 {"--use-version", "-V", "a"},
948 {"--user-dependencies", "-MM", 0},
949 {"--verbose", "-v", 0},
950 {"--version", "-dumpversion", 0},
951 {"--warn-", "-W", "*j"},
952 {"--write-dependencies", "-MD", 0},
953 {"--write-user-dependencies", "-MMD", 0},
954 {"--", "-f", "*j"}
958 #ifdef TARGET_OPTION_TRANSLATE_TABLE
959 static const struct {
960 const char *const option_found;
961 const char *const replacements;
962 } target_option_translations[] =
964 TARGET_OPTION_TRANSLATE_TABLE,
965 { 0, 0 }
967 #endif
969 /* Translate the options described by *ARGCP and *ARGVP.
970 Make a new vector and store it back in *ARGVP,
971 and store its length in *ARGVC. */
973 static void
974 translate_options (argcp, argvp)
975 int *argcp;
976 const char *const **argvp;
978 int i;
979 int argc = *argcp;
980 const char *const *argv = *argvp;
981 int newvsize = (argc + 2) * 2 * sizeof (const char *);
982 const char **newv =
983 (const char **) xmalloc (newvsize);
984 int newindex = 0;
986 i = 0;
987 newv[newindex++] = argv[i++];
989 while (i < argc)
991 #ifdef TARGET_OPTION_TRANSLATE_TABLE
992 int tott_idx;
994 for (tott_idx = 0;
995 target_option_translations[tott_idx].option_found;
996 tott_idx++)
998 if (strcmp (target_option_translations[tott_idx].option_found,
999 argv[i]) == 0)
1001 int spaces = 1;
1002 const char *sp;
1003 char *np;
1005 for (sp = target_option_translations[tott_idx].replacements;
1006 *sp; sp++)
1008 if (*sp == ' ')
1009 spaces ++;
1012 newvsize += spaces * sizeof (const char *);
1013 newv = (const char **) xrealloc (newv, newvsize);
1015 sp = target_option_translations[tott_idx].replacements;
1016 np = xstrdup (sp);
1018 while (1)
1020 while (*np == ' ')
1021 np++;
1022 if (*np == 0)
1023 break;
1024 newv[newindex++] = np;
1025 while (*np != ' ' && *np)
1026 np++;
1027 if (*np == 0)
1028 break;
1029 *np++ = 0;
1032 i ++;
1033 break;
1036 if (target_option_translations[tott_idx].option_found)
1037 continue;
1038 #endif
1040 /* Translate -- options. */
1041 if (argv[i][0] == '-' && argv[i][1] == '-')
1043 size_t j;
1044 /* Find a mapping that applies to this option. */
1045 for (j = 0; j < ARRAY_SIZE (option_map); j++)
1047 size_t optlen = strlen (option_map[j].name);
1048 size_t arglen = strlen (argv[i]);
1049 size_t complen = arglen > optlen ? optlen : arglen;
1050 const char *arginfo = option_map[j].arg_info;
1052 if (arginfo == 0)
1053 arginfo = "";
1055 if (!strncmp (argv[i], option_map[j].name, complen))
1057 const char *arg = 0;
1059 if (arglen < optlen)
1061 size_t k;
1062 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
1063 if (strlen (option_map[k].name) >= arglen
1064 && !strncmp (argv[i], option_map[k].name, arglen))
1066 error ("Ambiguous abbreviation %s", argv[i]);
1067 break;
1070 if (k != ARRAY_SIZE (option_map))
1071 break;
1074 if (arglen > optlen)
1076 /* If the option has an argument, accept that. */
1077 if (argv[i][optlen] == '=')
1078 arg = argv[i] + optlen + 1;
1080 /* If this mapping requires extra text at end of name,
1081 accept that as "argument". */
1082 else if (strchr (arginfo, '*') != 0)
1083 arg = argv[i] + optlen;
1085 /* Otherwise, extra text at end means mismatch.
1086 Try other mappings. */
1087 else
1088 continue;
1091 else if (strchr (arginfo, '*') != 0)
1093 error ("Incomplete `%s' option", option_map[j].name);
1094 break;
1097 /* Handle arguments. */
1098 if (strchr (arginfo, 'a') != 0)
1100 if (arg == 0)
1102 if (i + 1 == argc)
1104 error ("Missing argument to `%s' option",
1105 option_map[j].name);
1106 break;
1109 arg = argv[++i];
1112 else if (strchr (arginfo, '*') != 0)
1114 else if (strchr (arginfo, 'o') == 0)
1116 if (arg != 0)
1117 error ("Extraneous argument to `%s' option",
1118 option_map[j].name);
1119 arg = 0;
1122 /* Store the translation as one argv elt or as two. */
1123 if (arg != 0 && strchr (arginfo, 'j') != 0)
1124 newv[newindex++] = concat (option_map[j].equivalent, arg,
1125 NULL);
1126 else if (arg != 0)
1128 newv[newindex++] = option_map[j].equivalent;
1129 newv[newindex++] = arg;
1131 else
1132 newv[newindex++] = option_map[j].equivalent;
1134 break;
1137 i++;
1140 /* Handle old-fashioned options--just copy them through,
1141 with their arguments. */
1142 else if (argv[i][0] == '-')
1144 const char *p = argv[i] + 1;
1145 int c = *p;
1146 int nskip = 1;
1148 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1149 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1150 else if (WORD_SWITCH_TAKES_ARG (p))
1151 nskip += WORD_SWITCH_TAKES_ARG (p);
1152 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1153 && p[1] == 0)
1154 nskip += 1;
1155 else if (! strcmp (p, "Xlinker"))
1156 nskip += 1;
1158 /* Watch out for an option at the end of the command line that
1159 is missing arguments, and avoid skipping past the end of the
1160 command line. */
1161 if (nskip + i > argc)
1162 nskip = argc - i;
1164 while (nskip > 0)
1166 newv[newindex++] = argv[i++];
1167 nskip--;
1170 else
1171 /* Ordinary operands, or +e options. */
1172 newv[newindex++] = argv[i++];
1175 newv[newindex] = 0;
1177 *argvp = newv;
1178 *argcp = newindex;
1181 static char *
1182 skip_whitespace (p)
1183 char *p;
1185 while (1)
1187 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1188 be considered whitespace. */
1189 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1190 return p + 1;
1191 else if (*p == '\n' || *p == ' ' || *p == '\t')
1192 p++;
1193 else if (*p == '#')
1195 while (*p != '\n')
1196 p++;
1197 p++;
1199 else
1200 break;
1203 return p;
1205 /* Structures to keep track of prefixes to try when looking for files. */
1207 struct prefix_list
1209 const char *prefix; /* String to prepend to the path. */
1210 struct prefix_list *next; /* Next in linked list. */
1211 int require_machine_suffix; /* Don't use without machine_suffix. */
1212 /* 2 means try both machine_suffix and just_machine_suffix. */
1213 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1214 int priority; /* Sort key - priority within list */
1217 struct path_prefix
1219 struct prefix_list *plist; /* List of prefixes to try */
1220 int max_len; /* Max length of a prefix in PLIST */
1221 const char *name; /* Name of this list (used in config stuff) */
1224 /* List of prefixes to try when looking for executables. */
1226 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1228 /* List of prefixes to try when looking for startup (crt0) files. */
1230 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1232 /* List of prefixes to try when looking for include files. */
1234 static struct path_prefix include_prefixes = { 0, 0, "include" };
1236 /* Suffix to attach to directories searched for commands.
1237 This looks like `MACHINE/VERSION/'. */
1239 static const char *machine_suffix = 0;
1241 /* Suffix to attach to directories searched for commands.
1242 This is just `MACHINE/'. */
1244 static const char *just_machine_suffix = 0;
1246 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1248 static const char *gcc_exec_prefix;
1250 /* Default prefixes to attach to command names. */
1252 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1253 #undef MD_EXEC_PREFIX
1254 #undef MD_STARTFILE_PREFIX
1255 #undef MD_STARTFILE_PREFIX_1
1256 #endif
1258 /* If no prefixes defined, use the null string, which will disable them. */
1259 #ifndef MD_EXEC_PREFIX
1260 #define MD_EXEC_PREFIX ""
1261 #endif
1262 #ifndef MD_STARTFILE_PREFIX
1263 #define MD_STARTFILE_PREFIX ""
1264 #endif
1265 #ifndef MD_STARTFILE_PREFIX_1
1266 #define MD_STARTFILE_PREFIX_1 ""
1267 #endif
1269 /* Supply defaults for the standard prefixes. */
1271 #ifndef STANDARD_EXEC_PREFIX
1272 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1273 #endif
1274 #ifndef STANDARD_STARTFILE_PREFIX
1275 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1276 #endif
1277 #ifndef TOOLDIR_BASE_PREFIX
1278 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1279 #endif
1280 #ifndef STANDARD_BINDIR_PREFIX
1281 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1282 #endif
1284 static const char *const standard_exec_prefix = STANDARD_EXEC_PREFIX;
1285 static const char *const standard_exec_prefix_1 = "/usr/lib/gcc/";
1286 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1288 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1289 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1290 static const char *const standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1291 static const char *const standard_startfile_prefix_1 = "/lib/";
1292 static const char *const standard_startfile_prefix_2 = "/usr/lib/";
1294 static const char *const tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1295 static const char *tooldir_prefix;
1297 static const char *const standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1299 /* Subdirectory to use for locating libraries. Set by
1300 set_multilib_dir based on the compilation options. */
1302 static const char *multilib_dir;
1304 /* Structure to keep track of the specs that have been defined so far.
1305 These are accessed using %(specname) or %[specname] in a compiler
1306 or link spec. */
1308 struct spec_list
1310 /* The following 2 fields must be first */
1311 /* to allow EXTRA_SPECS to be initialized */
1312 const char *name; /* name of the spec. */
1313 const char *ptr; /* available ptr if no static pointer */
1315 /* The following fields are not initialized */
1316 /* by EXTRA_SPECS */
1317 const char **ptr_spec; /* pointer to the spec itself. */
1318 struct spec_list *next; /* Next spec in linked list. */
1319 int name_len; /* length of the name */
1320 int alloc_p; /* whether string was allocated */
1323 #define INIT_STATIC_SPEC(NAME,PTR) \
1324 { NAME, NULL, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1326 /* List of statically defined specs. */
1327 static struct spec_list static_specs[] =
1329 INIT_STATIC_SPEC ("asm", &asm_spec),
1330 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1331 INIT_STATIC_SPEC ("asm_options", &asm_options),
1332 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1333 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1334 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1335 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1336 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1337 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1338 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1339 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1340 INIT_STATIC_SPEC ("link", &link_spec),
1341 INIT_STATIC_SPEC ("lib", &lib_spec),
1342 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1343 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1344 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1345 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1346 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1347 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1348 INIT_STATIC_SPEC ("version", &compiler_version),
1349 INIT_STATIC_SPEC ("multilib", &multilib_select),
1350 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1351 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1352 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1353 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1354 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1355 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1356 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1357 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1358 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1361 #ifdef EXTRA_SPECS /* additional specs needed */
1362 /* Structure to keep track of just the first two args of a spec_list.
1363 That is all that the EXTRA_SPECS macro gives us. */
1364 struct spec_list_1
1366 const char *const name;
1367 const char *const ptr;
1370 static const struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1371 static struct spec_list *extra_specs = (struct spec_list *) 0;
1372 #endif
1374 /* List of dynamically allocates specs that have been defined so far. */
1376 static struct spec_list *specs = (struct spec_list *) 0;
1378 /* Add appropriate libgcc specs to OBSTACK, taking into account
1379 various permutations of -shared-libgcc, -shared, and such. */
1381 #ifdef ENABLE_SHARED_LIBGCC
1382 static void
1383 init_gcc_specs (obstack, shared_name, static_name)
1384 struct obstack *obstack;
1385 const char *shared_name;
1386 const char *static_name;
1388 char buffer[128];
1390 /* If we see -shared-libgcc, then use the shared version. */
1391 sprintf (buffer, "%%{shared-libgcc:%s %s}", shared_name, static_name);
1392 obstack_grow (obstack, buffer, strlen (buffer));
1393 /* If we see -static-libgcc, then use the static version. */
1394 sprintf (buffer, "%%{static-libgcc:%s}", static_name);
1395 obstack_grow (obstack, buffer, strlen (buffer));
1396 /* Otherwise, if we see -shared, then use the shared version. */
1397 sprintf (buffer,
1398 "%%{!shared-libgcc:%%{!static-libgcc:%%{shared:%s %s}}}",
1399 shared_name, static_name);
1400 obstack_grow (obstack, buffer, strlen (buffer));
1401 /* Otherwise, use the static version. */
1402 sprintf (buffer,
1403 "%%{!shared-libgcc:%%{!static-libgcc:%%{!shared:%s}}}",
1404 static_name);
1405 obstack_grow (obstack, buffer, strlen (buffer));
1407 #endif /* ENABLE_SHARED_LIBGCC */
1409 /* Initialize the specs lookup routines. */
1411 static void
1412 init_spec ()
1414 struct spec_list *next = (struct spec_list *) 0;
1415 struct spec_list *sl = (struct spec_list *) 0;
1416 int i;
1418 if (specs)
1419 return; /* Already initialized. */
1421 if (verbose_flag)
1422 notice ("Using builtin specs.\n");
1424 #ifdef EXTRA_SPECS
1425 extra_specs = (struct spec_list *)
1426 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1428 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1430 sl = &extra_specs[i];
1431 sl->name = extra_specs_1[i].name;
1432 sl->ptr = extra_specs_1[i].ptr;
1433 sl->next = next;
1434 sl->name_len = strlen (sl->name);
1435 sl->ptr_spec = &sl->ptr;
1436 next = sl;
1438 #endif
1440 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1442 sl = &static_specs[i];
1443 sl->next = next;
1444 next = sl;
1447 #ifdef ENABLE_SHARED_LIBGCC
1448 /* ??? If neither -shared-libgcc nor --static-libgcc was
1449 seen, then we should be making an educated guess. Some proposed
1450 heuristics for ELF include:
1452 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1453 program will be doing dynamic loading, which will likely
1454 need the shared libgcc.
1456 (2) If "-ldl", then it's also a fair bet that we're doing
1457 dynamic loading.
1459 (3) For each ET_DYN we're linking against (either through -lfoo
1460 or /some/path/foo.so), check to see whether it or one of
1461 its dependencies depends on a shared libgcc.
1463 (4) If "-shared"
1465 If the runtime is fixed to look for program headers instead
1466 of calling __register_frame_info at all, for each object,
1467 use the shared libgcc if any EH symbol referenced.
1469 If crtstuff is fixed to not invoke __register_frame_info
1470 automatically, for each object, use the shared libgcc if
1471 any non-empty unwind section found.
1473 Doing any of this probably requires invoking an external program to
1474 do the actual object file scanning. */
1476 const char *p = libgcc_spec;
1477 int in_sep = 1;
1479 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1480 when given the proper command line arguments. */
1481 while (*p)
1483 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1485 init_gcc_specs (&obstack,
1486 #ifdef NO_SHARED_LIBGCC_MULTILIB
1487 "-lgcc_s"
1488 #else
1489 "-lgcc_s%M"
1490 #endif
1492 "-lgcc");
1493 p += 5;
1494 in_sep = 0;
1496 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1498 /* Ug. We don't know shared library extensions. Hope that
1499 systems that use this form don't do shared libraries. */
1500 init_gcc_specs (&obstack,
1501 #ifdef NO_SHARED_LIBGCC_MULTILIB
1502 "-lgcc_s"
1503 #else
1504 "-lgcc_s%M"
1505 #endif
1507 "libgcc.a%s");
1508 p += 10;
1509 in_sep = 0;
1511 else
1513 obstack_1grow (&obstack, *p);
1514 in_sep = (*p == ' ');
1515 p += 1;
1519 obstack_1grow (&obstack, '\0');
1520 libgcc_spec = obstack_finish (&obstack);
1522 #endif
1523 #ifdef USE_AS_TRADITIONAL_FORMAT
1524 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1526 static const char tf[] = "--traditional-format ";
1527 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1528 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1529 asm_spec = obstack_finish (&obstack);
1531 #endif
1533 specs = sl;
1536 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1537 removed; If the spec starts with a + then SPEC is added to the end of the
1538 current spec. */
1540 static void
1541 set_spec (name, spec)
1542 const char *name;
1543 const char *spec;
1545 struct spec_list *sl;
1546 const char *old_spec;
1547 int name_len = strlen (name);
1548 int i;
1550 /* If this is the first call, initialize the statically allocated specs. */
1551 if (!specs)
1553 struct spec_list *next = (struct spec_list *) 0;
1554 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1556 sl = &static_specs[i];
1557 sl->next = next;
1558 next = sl;
1560 specs = sl;
1563 /* See if the spec already exists. */
1564 for (sl = specs; sl; sl = sl->next)
1565 if (name_len == sl->name_len && !strcmp (sl->name, name))
1566 break;
1568 if (!sl)
1570 /* Not found - make it. */
1571 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1572 sl->name = xstrdup (name);
1573 sl->name_len = name_len;
1574 sl->ptr_spec = &sl->ptr;
1575 sl->alloc_p = 0;
1576 *(sl->ptr_spec) = "";
1577 sl->next = specs;
1578 specs = sl;
1581 old_spec = *(sl->ptr_spec);
1582 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1583 ? concat (old_spec, spec + 1, NULL)
1584 : xstrdup (spec));
1586 #ifdef DEBUG_SPECS
1587 if (verbose_flag)
1588 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1589 #endif
1591 /* Free the old spec. */
1592 if (old_spec && sl->alloc_p)
1593 free ((PTR) old_spec);
1595 sl->alloc_p = 1;
1598 /* Accumulate a command (program name and args), and run it. */
1600 /* Vector of pointers to arguments in the current line of specifications. */
1602 static const char **argbuf;
1604 /* Number of elements allocated in argbuf. */
1606 static int argbuf_length;
1608 /* Number of elements in argbuf currently in use (containing args). */
1610 static int argbuf_index;
1612 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1613 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1614 it here. */
1616 static struct temp_name {
1617 const char *suffix; /* suffix associated with the code. */
1618 int length; /* strlen (suffix). */
1619 int unique; /* Indicates whether %g or %u/%U was used. */
1620 const char *filename; /* associated filename. */
1621 int filename_length; /* strlen (filename). */
1622 struct temp_name *next;
1623 } *temp_names;
1625 /* Number of commands executed so far. */
1627 static int execution_count;
1629 /* Number of commands that exited with a signal. */
1631 static int signal_count;
1633 /* Name with which this program was invoked. */
1635 static const char *programname;
1637 /* Clear out the vector of arguments (after a command is executed). */
1639 static void
1640 clear_args ()
1642 argbuf_index = 0;
1645 /* Add one argument to the vector at the end.
1646 This is done when a space is seen or at the end of the line.
1647 If DELETE_ALWAYS is nonzero, the arg is a filename
1648 and the file should be deleted eventually.
1649 If DELETE_FAILURE is nonzero, the arg is a filename
1650 and the file should be deleted if this compilation fails. */
1652 static void
1653 store_arg (arg, delete_always, delete_failure)
1654 const char *arg;
1655 int delete_always, delete_failure;
1657 if (argbuf_index + 1 == argbuf_length)
1658 argbuf
1659 = (const char **) xrealloc (argbuf,
1660 (argbuf_length *= 2) * sizeof (const char *));
1662 argbuf[argbuf_index++] = arg;
1663 argbuf[argbuf_index] = 0;
1665 if (delete_always || delete_failure)
1666 record_temp_file (arg, delete_always, delete_failure);
1669 /* Load specs from a file name named FILENAME, replacing occurrences of
1670 various different types of line-endings, \r\n, \n\r and just \r, with
1671 a single \n. */
1673 static char *
1674 load_specs (filename)
1675 const char *filename;
1677 int desc;
1678 int readlen;
1679 struct stat statbuf;
1680 char *buffer;
1681 char *buffer_p;
1682 char *specs;
1683 char *specs_p;
1685 if (verbose_flag)
1686 notice ("Reading specs from %s\n", filename);
1688 /* Open and stat the file. */
1689 desc = open (filename, O_RDONLY, 0);
1690 if (desc < 0)
1691 pfatal_with_name (filename);
1692 if (stat (filename, &statbuf) < 0)
1693 pfatal_with_name (filename);
1695 /* Read contents of file into BUFFER. */
1696 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1697 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1698 if (readlen < 0)
1699 pfatal_with_name (filename);
1700 buffer[readlen] = 0;
1701 close (desc);
1703 specs = xmalloc (readlen + 1);
1704 specs_p = specs;
1705 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1707 int skip = 0;
1708 char c = *buffer_p;
1709 if (c == '\r')
1711 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1712 skip = 1;
1713 else if (*(buffer_p + 1) == '\n') /* \r\n */
1714 skip = 1;
1715 else /* \r */
1716 c = '\n';
1718 if (! skip)
1719 *specs_p++ = c;
1721 *specs_p = '\0';
1723 free (buffer);
1724 return (specs);
1727 /* Read compilation specs from a file named FILENAME,
1728 replacing the default ones.
1730 A suffix which starts with `*' is a definition for
1731 one of the machine-specific sub-specs. The "suffix" should be
1732 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1733 The corresponding spec is stored in asm_spec, etc.,
1734 rather than in the `compilers' vector.
1736 Anything invalid in the file is a fatal error. */
1738 static void
1739 read_specs (filename, main_p)
1740 const char *filename;
1741 int main_p;
1743 char *buffer;
1744 char *p;
1746 buffer = load_specs (filename);
1748 /* Scan BUFFER for specs, putting them in the vector. */
1749 p = buffer;
1750 while (1)
1752 char *suffix;
1753 char *spec;
1754 char *in, *out, *p1, *p2, *p3;
1756 /* Advance P in BUFFER to the next nonblank nocomment line. */
1757 p = skip_whitespace (p);
1758 if (*p == 0)
1759 break;
1761 /* Is this a special command that starts with '%'? */
1762 /* Don't allow this for the main specs file, since it would
1763 encourage people to overwrite it. */
1764 if (*p == '%' && !main_p)
1766 p1 = p;
1767 while (*p && *p != '\n')
1768 p++;
1770 /* Skip '\n'. */
1771 p++;
1773 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1774 && (p1[sizeof "%include" - 1] == ' '
1775 || p1[sizeof "%include" - 1] == '\t'))
1777 char *new_filename;
1779 p1 += sizeof ("%include");
1780 while (*p1 == ' ' || *p1 == '\t')
1781 p1++;
1783 if (*p1++ != '<' || p[-2] != '>')
1784 fatal ("specs %%include syntax malformed after %ld characters",
1785 (long) (p1 - buffer + 1));
1787 p[-2] = '\0';
1788 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1789 read_specs (new_filename ? new_filename : p1, FALSE);
1790 continue;
1792 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1793 && (p1[sizeof "%include_noerr" - 1] == ' '
1794 || p1[sizeof "%include_noerr" - 1] == '\t'))
1796 char *new_filename;
1798 p1 += sizeof "%include_noerr";
1799 while (*p1 == ' ' || *p1 == '\t')
1800 p1++;
1802 if (*p1++ != '<' || p[-2] != '>')
1803 fatal ("specs %%include syntax malformed after %ld characters",
1804 (long) (p1 - buffer + 1));
1806 p[-2] = '\0';
1807 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1808 if (new_filename)
1809 read_specs (new_filename, FALSE);
1810 else if (verbose_flag)
1811 notice ("Could not find specs file %s\n", p1);
1812 continue;
1814 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1815 && (p1[sizeof "%rename" - 1] == ' '
1816 || p1[sizeof "%rename" - 1] == '\t'))
1818 int name_len;
1819 struct spec_list *sl;
1821 /* Get original name */
1822 p1 += sizeof "%rename";
1823 while (*p1 == ' ' || *p1 == '\t')
1824 p1++;
1826 if (! ISALPHA ((unsigned char) *p1))
1827 fatal ("specs %%rename syntax malformed after %ld characters",
1828 (long) (p1 - buffer));
1830 p2 = p1;
1831 while (*p2 && !ISSPACE ((unsigned char) *p2))
1832 p2++;
1834 if (*p2 != ' ' && *p2 != '\t')
1835 fatal ("specs %%rename syntax malformed after %ld characters",
1836 (long) (p2 - buffer));
1838 name_len = p2 - p1;
1839 *p2++ = '\0';
1840 while (*p2 == ' ' || *p2 == '\t')
1841 p2++;
1843 if (! ISALPHA ((unsigned char) *p2))
1844 fatal ("specs %%rename syntax malformed after %ld characters",
1845 (long) (p2 - buffer));
1847 /* Get new spec name. */
1848 p3 = p2;
1849 while (*p3 && !ISSPACE ((unsigned char) *p3))
1850 p3++;
1852 if (p3 != p - 1)
1853 fatal ("specs %%rename syntax malformed after %ld characters",
1854 (long) (p3 - buffer));
1855 *p3 = '\0';
1857 for (sl = specs; sl; sl = sl->next)
1858 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1859 break;
1861 if (!sl)
1862 fatal ("specs %s spec was not found to be renamed", p1);
1864 if (strcmp (p1, p2) == 0)
1865 continue;
1867 if (verbose_flag)
1869 notice ("rename spec %s to %s\n", p1, p2);
1870 #ifdef DEBUG_SPECS
1871 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1872 #endif
1875 set_spec (p2, *(sl->ptr_spec));
1876 if (sl->alloc_p)
1877 free ((PTR) *(sl->ptr_spec));
1879 *(sl->ptr_spec) = "";
1880 sl->alloc_p = 0;
1881 continue;
1883 else
1884 fatal ("specs unknown %% command after %ld characters",
1885 (long) (p1 - buffer));
1888 /* Find the colon that should end the suffix. */
1889 p1 = p;
1890 while (*p1 && *p1 != ':' && *p1 != '\n')
1891 p1++;
1893 /* The colon shouldn't be missing. */
1894 if (*p1 != ':')
1895 fatal ("specs file malformed after %ld characters",
1896 (long) (p1 - buffer));
1898 /* Skip back over trailing whitespace. */
1899 p2 = p1;
1900 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1901 p2--;
1903 /* Copy the suffix to a string. */
1904 suffix = save_string (p, p2 - p);
1905 /* Find the next line. */
1906 p = skip_whitespace (p1 + 1);
1907 if (p[1] == 0)
1908 fatal ("specs file malformed after %ld characters",
1909 (long) (p - buffer));
1911 p1 = p;
1912 /* Find next blank line or end of string. */
1913 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1914 p1++;
1916 /* Specs end at the blank line and do not include the newline. */
1917 spec = save_string (p, p1 - p);
1918 p = p1;
1920 /* Delete backslash-newline sequences from the spec. */
1921 in = spec;
1922 out = spec;
1923 while (*in != 0)
1925 if (in[0] == '\\' && in[1] == '\n')
1926 in += 2;
1927 else if (in[0] == '#')
1928 while (*in && *in != '\n')
1929 in++;
1931 else
1932 *out++ = *in++;
1934 *out = 0;
1936 if (suffix[0] == '*')
1938 if (! strcmp (suffix, "*link_command"))
1939 link_command_spec = spec;
1940 else
1941 set_spec (suffix + 1, spec);
1943 else
1945 /* Add this pair to the vector. */
1946 compilers
1947 = ((struct compiler *)
1948 xrealloc (compilers,
1949 (n_compilers + 2) * sizeof (struct compiler)));
1951 compilers[n_compilers].suffix = suffix;
1952 compilers[n_compilers].spec = spec;
1953 n_compilers++;
1954 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
1957 if (*suffix == 0)
1958 link_command_spec = spec;
1961 if (link_command_spec == 0)
1962 fatal ("spec file has no spec for linking");
1965 /* Record the names of temporary files we tell compilers to write,
1966 and delete them at the end of the run. */
1968 /* This is the common prefix we use to make temp file names.
1969 It is chosen once for each run of this program.
1970 It is substituted into a spec by %g or %j.
1971 Thus, all temp file names contain this prefix.
1972 In practice, all temp file names start with this prefix.
1974 This prefix comes from the envvar TMPDIR if it is defined;
1975 otherwise, from the P_tmpdir macro if that is defined;
1976 otherwise, in /usr/tmp or /tmp;
1977 or finally the current directory if all else fails. */
1979 static const char *temp_filename;
1981 /* Length of the prefix. */
1983 static int temp_filename_length;
1985 /* Define the list of temporary files to delete. */
1987 struct temp_file
1989 const char *name;
1990 struct temp_file *next;
1993 /* Queue of files to delete on success or failure of compilation. */
1994 static struct temp_file *always_delete_queue;
1995 /* Queue of files to delete on failure of compilation. */
1996 static struct temp_file *failure_delete_queue;
1998 /* Record FILENAME as a file to be deleted automatically.
1999 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
2000 otherwise delete it in any case.
2001 FAIL_DELETE nonzero means delete it if a compilation step fails;
2002 otherwise delete it in any case. */
2004 void
2005 record_temp_file (filename, always_delete, fail_delete)
2006 const char *filename;
2007 int always_delete;
2008 int fail_delete;
2010 char *const name = xstrdup (filename);
2012 if (always_delete)
2014 struct temp_file *temp;
2015 for (temp = always_delete_queue; temp; temp = temp->next)
2016 if (! strcmp (name, temp->name))
2017 goto already1;
2019 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2020 temp->next = always_delete_queue;
2021 temp->name = name;
2022 always_delete_queue = temp;
2024 already1:;
2027 if (fail_delete)
2029 struct temp_file *temp;
2030 for (temp = failure_delete_queue; temp; temp = temp->next)
2031 if (! strcmp (name, temp->name))
2032 goto already2;
2034 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
2035 temp->next = failure_delete_queue;
2036 temp->name = name;
2037 failure_delete_queue = temp;
2039 already2:;
2043 /* Delete all the temporary files whose names we previously recorded. */
2045 static void
2046 delete_if_ordinary (name)
2047 const char *name;
2049 struct stat st;
2050 #ifdef DEBUG
2051 int i, c;
2053 printf ("Delete %s? (y or n) ", name);
2054 fflush (stdout);
2055 i = getchar ();
2056 if (i != '\n')
2057 while ((c = getchar ()) != '\n' && c != EOF)
2060 if (i == 'y' || i == 'Y')
2061 #endif /* DEBUG */
2062 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
2063 if (unlink (name) < 0)
2064 if (verbose_flag)
2065 perror_with_name (name);
2068 static void
2069 delete_temp_files ()
2071 struct temp_file *temp;
2073 for (temp = always_delete_queue; temp; temp = temp->next)
2074 delete_if_ordinary (temp->name);
2075 always_delete_queue = 0;
2078 /* Delete all the files to be deleted on error. */
2080 static void
2081 delete_failure_queue ()
2083 struct temp_file *temp;
2085 for (temp = failure_delete_queue; temp; temp = temp->next)
2086 delete_if_ordinary (temp->name);
2089 static void
2090 clear_failure_queue ()
2092 failure_delete_queue = 0;
2095 /* Build a list of search directories from PATHS.
2096 PREFIX is a string to prepend to the list.
2097 If CHECK_DIR_P is non-zero we ensure the directory exists.
2098 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2099 It is also used by the --print-search-dirs flag. */
2101 static char *
2102 build_search_list (paths, prefix, check_dir_p)
2103 struct path_prefix *paths;
2104 const char *prefix;
2105 int check_dir_p;
2107 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2108 int just_suffix_len
2109 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2110 int first_time = TRUE;
2111 struct prefix_list *pprefix;
2113 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2114 obstack_1grow (&collect_obstack, '=');
2116 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2118 int len = strlen (pprefix->prefix);
2120 if (machine_suffix
2121 && (! check_dir_p
2122 || is_directory (pprefix->prefix, machine_suffix, 0)))
2124 if (!first_time)
2125 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2127 first_time = FALSE;
2128 obstack_grow (&collect_obstack, pprefix->prefix, len);
2129 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2132 if (just_machine_suffix
2133 && pprefix->require_machine_suffix == 2
2134 && (! check_dir_p
2135 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2137 if (! first_time)
2138 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2140 first_time = FALSE;
2141 obstack_grow (&collect_obstack, pprefix->prefix, len);
2142 obstack_grow (&collect_obstack, just_machine_suffix,
2143 just_suffix_len);
2146 if (! pprefix->require_machine_suffix)
2148 if (! first_time)
2149 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2151 first_time = FALSE;
2152 obstack_grow (&collect_obstack, pprefix->prefix, len);
2156 obstack_1grow (&collect_obstack, '\0');
2157 return obstack_finish (&collect_obstack);
2160 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2161 for collect. */
2163 static void
2164 putenv_from_prefixes (paths, env_var)
2165 struct path_prefix *paths;
2166 const char *env_var;
2168 putenv (build_search_list (paths, env_var, 1));
2171 #ifndef VMS
2173 /* FIXME: the location independence code for VMS is hairier than this,
2174 and hasn't been written. */
2176 /* Split a filename into component directories. */
2178 static char **
2179 split_directories (name, ptr_num_dirs)
2180 const char *name;
2181 int *ptr_num_dirs;
2183 int num_dirs = 0;
2184 char **dirs;
2185 const char *p, *q;
2186 int ch;
2188 /* Count the number of directories. Special case MSDOS disk names as part
2189 of the initial directory. */
2190 p = name;
2191 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2192 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2194 p += 3;
2195 num_dirs++;
2197 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2199 while ((ch = *p++) != '\0')
2201 if (IS_DIR_SEPARATOR (ch))
2203 num_dirs++;
2204 while (IS_DIR_SEPARATOR (*p))
2205 p++;
2209 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2211 /* Now copy the directory parts. */
2212 num_dirs = 0;
2213 p = name;
2214 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2215 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2217 dirs[num_dirs++] = save_string (p, 3);
2218 p += 3;
2220 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2222 q = p;
2223 while ((ch = *p++) != '\0')
2225 if (IS_DIR_SEPARATOR (ch))
2227 while (IS_DIR_SEPARATOR (*p))
2228 p++;
2230 dirs[num_dirs++] = save_string (q, p - q);
2231 q = p;
2235 if (p - 1 - q > 0)
2236 dirs[num_dirs++] = save_string (q, p - 1 - q);
2238 dirs[num_dirs] = NULL;
2239 if (ptr_num_dirs)
2240 *ptr_num_dirs = num_dirs;
2242 return dirs;
2245 /* Release storage held by split directories. */
2247 static void
2248 free_split_directories (dirs)
2249 char **dirs;
2251 int i = 0;
2253 while (dirs[i] != NULL)
2254 free (dirs[i++]);
2256 free ((char *) dirs);
2259 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2260 to PREFIX starting with the directory portion of PROGNAME and a relative
2261 pathname of the difference between BIN_PREFIX and PREFIX.
2263 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2264 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2265 function will return /red/green/blue/../omega.
2267 If no relative prefix can be found, return NULL. */
2269 static char *
2270 make_relative_prefix (progname, bin_prefix, prefix)
2271 const char *progname;
2272 const char *bin_prefix;
2273 const char *prefix;
2275 char **prog_dirs, **bin_dirs, **prefix_dirs;
2276 int prog_num, bin_num, prefix_num, std_loc_p;
2277 int i, n, common;
2279 prog_dirs = split_directories (progname, &prog_num);
2280 bin_dirs = split_directories (bin_prefix, &bin_num);
2282 /* If there is no full pathname, try to find the program by checking in each
2283 of the directories specified in the PATH environment variable. */
2284 if (prog_num == 1)
2286 char *temp;
2288 GET_ENV_PATH_LIST (temp, "PATH");
2289 if (temp)
2291 char *startp, *endp, *nstore;
2292 size_t prefixlen = strlen (temp) + 1;
2293 if (prefixlen < 2)
2294 prefixlen = 2;
2296 nstore = (char *) alloca (prefixlen + strlen (progname) + 1);
2298 startp = endp = temp;
2299 while (1)
2301 if (*endp == PATH_SEPARATOR || *endp == 0)
2303 if (endp == startp)
2305 nstore[0] = '.';
2306 nstore[1] = DIR_SEPARATOR;
2307 nstore[2] = '\0';
2309 else
2311 strncpy (nstore, startp, endp - startp);
2312 if (! IS_DIR_SEPARATOR (endp[-1]))
2314 nstore[endp - startp] = DIR_SEPARATOR;
2315 nstore[endp - startp + 1] = 0;
2317 else
2318 nstore[endp - startp] = 0;
2320 strcat (nstore, progname);
2321 if (! access (nstore, X_OK)
2322 #ifdef HAVE_HOST_EXECUTABLE_SUFFIX
2323 || ! access (strcat (nstore, HOST_EXECUTABLE_SUFFIX), X_OK)
2324 #endif
2327 free_split_directories (prog_dirs);
2328 progname = nstore;
2329 prog_dirs = split_directories (progname, &prog_num);
2330 break;
2333 if (*endp == 0)
2334 break;
2335 endp = startp = endp + 1;
2337 else
2338 endp++;
2343 /* Remove the program name from comparison of directory names. */
2344 prog_num--;
2346 /* Determine if the compiler is installed in the standard location, and if
2347 so, we don't need to specify relative directories. Also, if argv[0]
2348 doesn't contain any directory specifiers, there is not much we can do. */
2349 std_loc_p = 0;
2350 if (prog_num == bin_num)
2352 for (i = 0; i < bin_num; i++)
2354 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2355 break;
2358 if (prog_num <= 0 || i == bin_num)
2360 std_loc_p = 1;
2361 free_split_directories (prog_dirs);
2362 free_split_directories (bin_dirs);
2363 prog_dirs = bin_dirs = (char **) 0;
2364 return NULL;
2368 prefix_dirs = split_directories (prefix, &prefix_num);
2370 /* Find how many directories are in common between bin_prefix & prefix. */
2371 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2372 for (common = 0; common < n; common++)
2374 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2375 break;
2378 /* If there are no common directories, there can be no relative prefix. */
2379 if (common == 0)
2381 free_split_directories (prog_dirs);
2382 free_split_directories (bin_dirs);
2383 free_split_directories (prefix_dirs);
2384 return NULL;
2387 /* Build up the pathnames in argv[0]. */
2388 for (i = 0; i < prog_num; i++)
2389 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2391 /* Now build up the ..'s. */
2392 for (i = common; i < n; i++)
2394 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2395 obstack_1grow (&obstack, DIR_SEPARATOR);
2398 /* Put in directories to move over to prefix. */
2399 for (i = common; i < prefix_num; i++)
2400 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2402 free_split_directories (prog_dirs);
2403 free_split_directories (bin_dirs);
2404 free_split_directories (prefix_dirs);
2406 obstack_1grow (&obstack, '\0');
2407 return obstack_finish (&obstack);
2409 #endif /* VMS */
2411 /* Check whether NAME can be accessed in MODE. This is like access,
2412 except that it never considers directories to be executable. */
2414 static int
2415 access_check (name, mode)
2416 const char *name;
2417 int mode;
2419 if (mode == X_OK)
2421 struct stat st;
2423 if (stat (name, &st) < 0
2424 || S_ISDIR (st.st_mode))
2425 return -1;
2428 return access (name, mode);
2431 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2432 access to check permissions.
2433 Return 0 if not found, otherwise return its name, allocated with malloc. */
2435 static char *
2436 find_a_file (pprefix, name, mode)
2437 struct path_prefix *pprefix;
2438 const char *name;
2439 int mode;
2441 char *temp;
2442 const char *const file_suffix =
2443 ((mode & X_OK) != 0 ? HOST_EXECUTABLE_SUFFIX : "");
2444 struct prefix_list *pl;
2445 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2447 #ifdef DEFAULT_ASSEMBLER
2448 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2449 return xstrdup (DEFAULT_ASSEMBLER);
2450 #endif
2452 #ifdef DEFAULT_LINKER
2453 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2454 return xstrdup (DEFAULT_LINKER);
2455 #endif
2457 if (machine_suffix)
2458 len += strlen (machine_suffix);
2460 temp = xmalloc (len);
2462 /* Determine the filename to execute (special case for absolute paths). */
2464 if (IS_ABSOLUTE_PATHNAME (name))
2466 if (access (name, mode) == 0)
2468 strcpy (temp, name);
2469 return temp;
2472 else
2473 for (pl = pprefix->plist; pl; pl = pl->next)
2475 if (machine_suffix)
2477 /* Some systems have a suffix for executable files.
2478 So try appending that first. */
2479 if (file_suffix[0] != 0)
2481 strcpy (temp, pl->prefix);
2482 strcat (temp, machine_suffix);
2483 strcat (temp, name);
2484 strcat (temp, file_suffix);
2485 if (access_check (temp, mode) == 0)
2487 if (pl->used_flag_ptr != 0)
2488 *pl->used_flag_ptr = 1;
2489 return temp;
2493 /* Now try just the name. */
2494 strcpy (temp, pl->prefix);
2495 strcat (temp, machine_suffix);
2496 strcat (temp, name);
2497 if (access_check (temp, mode) == 0)
2499 if (pl->used_flag_ptr != 0)
2500 *pl->used_flag_ptr = 1;
2501 return temp;
2505 /* Certain prefixes are tried with just the machine type,
2506 not the version. This is used for finding as, ld, etc. */
2507 if (just_machine_suffix && pl->require_machine_suffix == 2)
2509 /* Some systems have a suffix for executable files.
2510 So try appending that first. */
2511 if (file_suffix[0] != 0)
2513 strcpy (temp, pl->prefix);
2514 strcat (temp, just_machine_suffix);
2515 strcat (temp, name);
2516 strcat (temp, file_suffix);
2517 if (access_check (temp, mode) == 0)
2519 if (pl->used_flag_ptr != 0)
2520 *pl->used_flag_ptr = 1;
2521 return temp;
2525 strcpy (temp, pl->prefix);
2526 strcat (temp, just_machine_suffix);
2527 strcat (temp, name);
2528 if (access_check (temp, mode) == 0)
2530 if (pl->used_flag_ptr != 0)
2531 *pl->used_flag_ptr = 1;
2532 return temp;
2536 /* Certain prefixes can't be used without the machine suffix
2537 when the machine or version is explicitly specified. */
2538 if (! pl->require_machine_suffix)
2540 /* Some systems have a suffix for executable files.
2541 So try appending that first. */
2542 if (file_suffix[0] != 0)
2544 strcpy (temp, pl->prefix);
2545 strcat (temp, name);
2546 strcat (temp, file_suffix);
2547 if (access_check (temp, mode) == 0)
2549 if (pl->used_flag_ptr != 0)
2550 *pl->used_flag_ptr = 1;
2551 return temp;
2555 strcpy (temp, pl->prefix);
2556 strcat (temp, name);
2557 if (access_check (temp, mode) == 0)
2559 if (pl->used_flag_ptr != 0)
2560 *pl->used_flag_ptr = 1;
2561 return temp;
2566 free (temp);
2567 return 0;
2570 /* Ranking of prefixes in the sort list. -B prefixes are put before
2571 all others. */
2573 enum path_prefix_priority
2575 PREFIX_PRIORITY_B_OPT,
2576 PREFIX_PRIORITY_LAST
2579 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2580 order according to PRIORITY. Within each PRIORITY, new entries are
2581 appended.
2583 If WARN is nonzero, we will warn if no file is found
2584 through this prefix. WARN should point to an int
2585 which will be set to 1 if this entry is used.
2587 COMPONENT is the value to be passed to update_path.
2589 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2590 the complete value of machine_suffix.
2591 2 means try both machine_suffix and just_machine_suffix. */
2593 static void
2594 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2595 struct path_prefix *pprefix;
2596 const char *prefix;
2597 const char *component;
2598 /* enum prefix_priority */ int priority;
2599 int require_machine_suffix;
2600 int *warn;
2602 struct prefix_list *pl, **prev;
2603 int len;
2605 for (prev = &pprefix->plist;
2606 (*prev) != NULL && (*prev)->priority <= priority;
2607 prev = &(*prev)->next)
2610 /* Keep track of the longest prefix */
2612 prefix = update_path (prefix, component);
2613 len = strlen (prefix);
2614 if (len > pprefix->max_len)
2615 pprefix->max_len = len;
2617 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2618 pl->prefix = prefix;
2619 pl->require_machine_suffix = require_machine_suffix;
2620 pl->used_flag_ptr = warn;
2621 pl->priority = priority;
2622 if (warn)
2623 *warn = 0;
2625 /* Insert after PREV */
2626 pl->next = (*prev);
2627 (*prev) = pl;
2630 /* Execute the command specified by the arguments on the current line of spec.
2631 When using pipes, this includes several piped-together commands
2632 with `|' between them.
2634 Return 0 if successful, -1 if failed. */
2636 static int
2637 execute ()
2639 int i;
2640 int n_commands; /* # of command. */
2641 char *string;
2642 struct command
2644 const char *prog; /* program name. */
2645 const char **argv; /* vector of args. */
2646 int pid; /* pid of process for this command. */
2649 struct command *commands; /* each command buffer with above info. */
2651 /* Count # of piped commands. */
2652 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2653 if (strcmp (argbuf[i], "|") == 0)
2654 n_commands++;
2656 /* Get storage for each command. */
2657 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2659 /* Split argbuf into its separate piped processes,
2660 and record info about each one.
2661 Also search for the programs that are to be run. */
2663 commands[0].prog = argbuf[0]; /* first command. */
2664 commands[0].argv = &argbuf[0];
2665 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2667 if (string)
2668 commands[0].argv[0] = string;
2670 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2671 if (strcmp (argbuf[i], "|") == 0)
2672 { /* each command. */
2673 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2674 fatal ("-pipe not supported");
2675 #endif
2676 argbuf[i] = 0; /* termination of command args. */
2677 commands[n_commands].prog = argbuf[i + 1];
2678 commands[n_commands].argv = &argbuf[i + 1];
2679 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2680 if (string)
2681 commands[n_commands].argv[0] = string;
2682 n_commands++;
2685 argbuf[argbuf_index] = 0;
2687 /* If -v, print what we are about to do, and maybe query. */
2689 if (verbose_flag)
2691 /* For help listings, put a blank line between sub-processes. */
2692 if (print_help_list)
2693 fputc ('\n', stderr);
2695 /* Print each piped command as a separate line. */
2696 for (i = 0; i < n_commands; i++)
2698 const char *const *j;
2700 for (j = commands[i].argv; *j; j++)
2701 fprintf (stderr, " %s", *j);
2703 /* Print a pipe symbol after all but the last command. */
2704 if (i + 1 != n_commands)
2705 fprintf (stderr, " |");
2706 fprintf (stderr, "\n");
2708 fflush (stderr);
2709 #ifdef DEBUG
2710 notice ("\nGo ahead? (y or n) ");
2711 fflush (stderr);
2712 i = getchar ();
2713 if (i != '\n')
2714 while (getchar () != '\n')
2717 if (i != 'y' && i != 'Y')
2718 return 0;
2719 #endif /* DEBUG */
2722 /* Run each piped subprocess. */
2724 for (i = 0; i < n_commands; i++)
2726 char *errmsg_fmt, *errmsg_arg;
2727 const char *string = commands[i].argv[0];
2729 /* For some bizarre reason, the second argument of execvp() is
2730 char *const *, not const char *const *. */
2731 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2732 programname, temp_filename,
2733 &errmsg_fmt, &errmsg_arg,
2734 ((i == 0 ? PEXECUTE_FIRST : 0)
2735 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2736 | (string == commands[i].prog
2737 ? PEXECUTE_SEARCH : 0)
2738 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2740 if (commands[i].pid == -1)
2741 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2743 if (string != commands[i].prog)
2744 free ((PTR) string);
2747 execution_count++;
2749 /* Wait for all the subprocesses to finish.
2750 We don't care what order they finish in;
2751 we know that N_COMMANDS waits will get them all.
2752 Ignore subprocesses that we don't know about,
2753 since they can be spawned by the process that exec'ed us. */
2756 int ret_code = 0;
2757 #ifdef HAVE_GETRUSAGE
2758 struct timeval d;
2759 double ut = 0.0, st = 0.0;
2760 #endif
2762 for (i = 0; i < n_commands;)
2764 int j;
2765 int status;
2766 int pid;
2768 pid = pwait (commands[i].pid, &status, 0);
2769 if (pid < 0)
2770 abort ();
2772 #ifdef HAVE_GETRUSAGE
2773 if (report_times)
2775 /* getrusage returns the total resource usage of all children
2776 up to now. Copy the previous values into prus, get the
2777 current statistics, then take the difference. */
2779 prus = rus;
2780 getrusage (RUSAGE_CHILDREN, &rus);
2781 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2782 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2783 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2785 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2786 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2787 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2789 #endif
2791 for (j = 0; j < n_commands; j++)
2792 if (commands[j].pid == pid)
2794 i++;
2795 if (WIFSIGNALED (status))
2797 #ifdef SIGPIPE
2798 /* SIGPIPE is a special case. It happens in -pipe mode
2799 when the compiler dies before the preprocessor is
2800 done, or the assembler dies before the compiler is
2801 done. There's generally been an error already, and
2802 this is just fallout. So don't generate another error
2803 unless we would otherwise have succeeded. */
2804 if (WTERMSIG (status) == SIGPIPE
2805 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2807 else
2808 #endif
2809 fatal ("\
2810 Internal error: %s (program %s)\n\
2811 Please submit a full bug report.\n\
2812 See %s for instructions.",
2813 strsignal (WTERMSIG (status)), commands[j].prog,
2814 GCCBUGURL);
2815 signal_count++;
2816 ret_code = -1;
2818 else if (WIFEXITED (status)
2819 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2821 if (WEXITSTATUS (status) > greatest_status)
2822 greatest_status = WEXITSTATUS (status);
2823 ret_code = -1;
2825 #ifdef HAVE_GETRUSAGE
2826 if (report_times && ut + st != 0)
2827 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2828 #endif
2829 break;
2832 return ret_code;
2836 /* Find all the switches given to us
2837 and make a vector describing them.
2838 The elements of the vector are strings, one per switch given.
2839 If a switch uses following arguments, then the `part1' field
2840 is the switch itself and the `args' field
2841 is a null-terminated vector containing the following arguments.
2842 The `live_cond' field is:
2843 0 when initialized
2844 1 if the switch is true in a conditional spec,
2845 -1 if false (overridden by a later switch)
2846 -2 if this switch should be ignored (used in %{<S})
2847 The `validated' field is nonzero if any spec has looked at this switch;
2848 if it remains zero at the end of the run, it must be meaningless. */
2850 #define SWITCH_OK 0
2851 #define SWITCH_FALSE -1
2852 #define SWITCH_IGNORE -2
2853 #define SWITCH_LIVE 1
2855 struct switchstr
2857 const char *part1;
2858 const char **args;
2859 int live_cond;
2860 unsigned char validated;
2861 unsigned char ordering;
2864 static struct switchstr *switches;
2866 static int n_switches;
2868 struct infile
2870 const char *name;
2871 const char *language;
2874 /* Also a vector of input files specified. */
2876 static struct infile *infiles;
2878 int n_infiles;
2880 /* This counts the number of libraries added by lang_specific_driver, so that
2881 we can tell if there were any user supplied any files or libraries. */
2883 static int added_libraries;
2885 /* And a vector of corresponding output files is made up later. */
2887 const char **outfiles;
2889 /* Used to track if none of the -B paths are used. */
2890 static int warn_B;
2892 /* Used to track if standard path isn't used and -b or -V is specified. */
2893 static int warn_std;
2895 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2896 static int *warn_std_ptr = 0;
2898 #if defined(HAVE_TARGET_OBJECT_SUFFIX) || defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2900 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2901 is true if we should look for an executable suffix as well. */
2903 static const char *
2904 convert_filename (name, do_exe)
2905 const char *name;
2906 int do_exe ATTRIBUTE_UNUSED;
2908 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2909 int i;
2910 #endif
2911 int len;
2913 if (name == NULL)
2914 return NULL;
2916 len = strlen (name);
2918 #ifdef HAVE_TARGET_OBJECT_SUFFIX
2919 /* Convert x.o to x.obj if TARGET_OBJECT_SUFFIX is ".obj". */
2920 if (len > 2
2921 && name[len - 2] == '.'
2922 && name[len - 1] == 'o')
2924 obstack_grow (&obstack, name, len - 2);
2925 obstack_grow0 (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
2926 name = obstack_finish (&obstack);
2928 #endif
2930 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
2931 /* If there is no filetype, make it the executable suffix (which includes
2932 the "."). But don't get confused if we have just "-o". */
2933 if (! do_exe || TARGET_EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2934 return name;
2936 for (i = len - 1; i >= 0; i--)
2937 if (IS_DIR_SEPARATOR (name[i]))
2938 break;
2940 for (i++; i < len; i++)
2941 if (name[i] == '.')
2942 return name;
2944 obstack_grow (&obstack, name, len);
2945 obstack_grow0 (&obstack, TARGET_EXECUTABLE_SUFFIX,
2946 strlen (TARGET_EXECUTABLE_SUFFIX));
2947 name = obstack_finish (&obstack);
2948 #endif
2950 return name;
2952 #endif
2954 /* Display the command line switches accepted by gcc. */
2955 static void
2956 display_help ()
2958 printf (_("Usage: %s [options] file...\n"), programname);
2959 fputs (_("Options:\n"), stdout);
2961 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2962 fputs (_(" --help Display this information\n"), stdout);
2963 fputs (_(" --target-help Display target specific command line options\n"), stdout);
2964 if (! verbose_flag)
2965 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
2966 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2967 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2968 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2969 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2970 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2971 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2972 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2973 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2974 fputs (_("\
2975 -print-multi-lib Display the mapping between command line options and\n\
2976 multiple library search directories\n"), stdout);
2977 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2978 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2979 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
2980 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2981 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
2982 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2983 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
2984 fputs (_(" -specs=<file> Override builtin specs with the contents of <file>\n"), stdout);
2985 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
2986 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
2987 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
2988 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
2989 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
2990 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2991 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2992 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2993 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2994 fputs (_("\
2995 -x <language> Specify the language of the following input files\n\
2996 Permissable languages include: c c++ assembler none\n\
2997 'none' means revert to the default behaviour of\n\
2998 guessing the language based on the file's extension\n\
2999 "), stdout);
3001 printf (_("\
3002 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
3003 passed on to the various sub-processes invoked by %s. In order to pass\n\
3004 other options on to these processes the -W<letter> options must be used.\n\
3005 "), programname);
3007 /* The rest of the options are displayed by invocations of the various
3008 sub-processes. */
3011 static void
3012 add_preprocessor_option (option, len)
3013 const char *option;
3014 int len;
3016 n_preprocessor_options++;
3018 if (! preprocessor_options)
3019 preprocessor_options
3020 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
3021 else
3022 preprocessor_options
3023 = (char **) xrealloc (preprocessor_options,
3024 n_preprocessor_options * sizeof (char *));
3026 preprocessor_options [n_preprocessor_options - 1] =
3027 save_string (option, len);
3030 static void
3031 add_assembler_option (option, len)
3032 const char *option;
3033 int len;
3035 n_assembler_options++;
3037 if (! assembler_options)
3038 assembler_options
3039 = (char **) xmalloc (n_assembler_options * sizeof (char *));
3040 else
3041 assembler_options
3042 = (char **) xrealloc (assembler_options,
3043 n_assembler_options * sizeof (char *));
3045 assembler_options [n_assembler_options - 1] = save_string (option, len);
3048 static void
3049 add_linker_option (option, len)
3050 const char *option;
3051 int len;
3053 n_linker_options++;
3055 if (! linker_options)
3056 linker_options
3057 = (char **) xmalloc (n_linker_options * sizeof (char *));
3058 else
3059 linker_options
3060 = (char **) xrealloc (linker_options,
3061 n_linker_options * sizeof (char *));
3063 linker_options [n_linker_options - 1] = save_string (option, len);
3066 /* Create the vector `switches' and its contents.
3067 Store its length in `n_switches'. */
3069 static void
3070 process_command (argc, argv)
3071 int argc;
3072 const char *const *argv;
3074 int i;
3075 const char *temp;
3076 char *temp1;
3077 const char *spec_lang = 0;
3078 int last_language_n_infiles;
3079 int have_c = 0;
3080 int have_o = 0;
3081 int lang_n_infiles = 0;
3082 #ifdef MODIFY_TARGET_NAME
3083 int is_modify_target_name;
3084 int j;
3085 #endif
3087 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3089 n_switches = 0;
3090 n_infiles = 0;
3091 added_libraries = 0;
3093 /* Figure compiler version from version string. */
3095 compiler_version = temp1 = xstrdup (version_string);
3097 for (; *temp1; ++temp1)
3099 if (*temp1 == ' ')
3101 *temp1 = '\0';
3102 break;
3106 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3107 see if we can create it from the pathname specified in argv[0]. */
3109 #ifndef VMS
3110 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3111 if (!gcc_exec_prefix)
3113 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3114 standard_exec_prefix);
3115 if (gcc_exec_prefix)
3116 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL));
3118 #endif
3120 if (gcc_exec_prefix)
3122 int len = strlen (gcc_exec_prefix);
3124 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3125 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3127 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3128 if (IS_DIR_SEPARATOR (*temp)
3129 && strncmp (temp + 1, "lib", 3) == 0
3130 && IS_DIR_SEPARATOR (temp[4])
3131 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3132 len -= sizeof ("/lib/gcc-lib/") - 1;
3135 set_std_prefix (gcc_exec_prefix, len);
3136 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3137 PREFIX_PRIORITY_LAST, 0, NULL);
3138 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3139 PREFIX_PRIORITY_LAST, 0, NULL);
3142 /* COMPILER_PATH and LIBRARY_PATH have values
3143 that are lists of directory names with colons. */
3145 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3146 if (temp)
3148 const char *startp, *endp;
3149 char *nstore = (char *) alloca (strlen (temp) + 3);
3151 startp = endp = temp;
3152 while (1)
3154 if (*endp == PATH_SEPARATOR || *endp == 0)
3156 strncpy (nstore, startp, endp - startp);
3157 if (endp == startp)
3158 strcpy (nstore, concat (".", dir_separator_str, NULL));
3159 else if (!IS_DIR_SEPARATOR (endp[-1]))
3161 nstore[endp - startp] = DIR_SEPARATOR;
3162 nstore[endp - startp + 1] = 0;
3164 else
3165 nstore[endp - startp] = 0;
3166 add_prefix (&exec_prefixes, nstore, 0,
3167 PREFIX_PRIORITY_LAST, 0, NULL);
3168 add_prefix (&include_prefixes,
3169 concat (nstore, "include", NULL),
3170 0, PREFIX_PRIORITY_LAST, 0, NULL);
3171 if (*endp == 0)
3172 break;
3173 endp = startp = endp + 1;
3175 else
3176 endp++;
3180 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3181 if (temp && *cross_compile == '0')
3183 const char *startp, *endp;
3184 char *nstore = (char *) alloca (strlen (temp) + 3);
3186 startp = endp = temp;
3187 while (1)
3189 if (*endp == PATH_SEPARATOR || *endp == 0)
3191 strncpy (nstore, startp, endp - startp);
3192 if (endp == startp)
3193 strcpy (nstore, concat (".", dir_separator_str, NULL));
3194 else if (!IS_DIR_SEPARATOR (endp[-1]))
3196 nstore[endp - startp] = DIR_SEPARATOR;
3197 nstore[endp - startp + 1] = 0;
3199 else
3200 nstore[endp - startp] = 0;
3201 add_prefix (&startfile_prefixes, nstore, NULL,
3202 PREFIX_PRIORITY_LAST, 0, NULL);
3203 if (*endp == 0)
3204 break;
3205 endp = startp = endp + 1;
3207 else
3208 endp++;
3212 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3213 GET_ENV_PATH_LIST (temp, "LPATH");
3214 if (temp && *cross_compile == '0')
3216 const char *startp, *endp;
3217 char *nstore = (char *) alloca (strlen (temp) + 3);
3219 startp = endp = temp;
3220 while (1)
3222 if (*endp == PATH_SEPARATOR || *endp == 0)
3224 strncpy (nstore, startp, endp - startp);
3225 if (endp == startp)
3226 strcpy (nstore, concat (".", dir_separator_str, NULL));
3227 else if (!IS_DIR_SEPARATOR (endp[-1]))
3229 nstore[endp - startp] = DIR_SEPARATOR;
3230 nstore[endp - startp + 1] = 0;
3232 else
3233 nstore[endp - startp] = 0;
3234 add_prefix (&startfile_prefixes, nstore, NULL,
3235 PREFIX_PRIORITY_LAST, 0, NULL);
3236 if (*endp == 0)
3237 break;
3238 endp = startp = endp + 1;
3240 else
3241 endp++;
3245 /* Convert new-style -- options to old-style. */
3246 translate_options (&argc, &argv);
3248 /* Do language-specific adjustment/addition of flags. */
3249 lang_specific_driver (&argc, &argv, &added_libraries);
3251 /* Scan argv twice. Here, the first time, just count how many switches
3252 there will be in their vector, and how many input files in theirs.
3253 Also parse any switches that determine the configuration name, such as -b.
3254 Here we also parse the switches that cc itself uses (e.g. -v). */
3256 for (i = 1; i < argc; i++)
3258 if (! strcmp (argv[i], "-dumpspecs"))
3260 struct spec_list *sl;
3261 init_spec ();
3262 for (sl = specs; sl; sl = sl->next)
3263 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3264 if (link_command_spec)
3265 printf ("*link_command:\n%s\n\n", link_command_spec);
3266 exit (0);
3268 else if (! strcmp (argv[i], "-dumpversion"))
3270 printf ("%s\n", spec_version);
3271 exit (0);
3273 else if (! strcmp (argv[i], "-dumpmachine"))
3275 printf ("%s\n", spec_machine);
3276 exit (0);
3278 else if (strcmp (argv[i], "-fhelp") == 0)
3280 /* translate_options () has turned --help into -fhelp. */
3281 print_help_list = 1;
3283 /* We will be passing a dummy file on to the sub-processes. */
3284 n_infiles++;
3285 n_switches++;
3287 add_preprocessor_option ("--help", 6);
3288 add_assembler_option ("--help", 6);
3289 add_linker_option ("--help", 6);
3291 else if (strcmp (argv[i], "-ftarget-help") == 0)
3293 /* translate_options() has turned --target-help into -ftarget-help. */
3294 target_help_flag = 1;
3296 /* We will be passing a dummy file on to the sub-processes. */
3297 n_infiles++;
3298 n_switches++;
3300 add_preprocessor_option ("--target-help", 13);
3301 add_assembler_option ("--target-help", 13);
3302 add_linker_option ("--target-help", 13);
3304 else if (! strcmp (argv[i], "-pass-exit-codes"))
3306 pass_exit_codes = 1;
3307 n_switches++;
3309 else if (! strcmp (argv[i], "-print-search-dirs"))
3310 print_search_dirs = 1;
3311 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3312 print_file_name = "libgcc.a";
3313 else if (! strncmp (argv[i], "-print-file-name=", 17))
3314 print_file_name = argv[i] + 17;
3315 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3316 print_prog_name = argv[i] + 17;
3317 else if (! strcmp (argv[i], "-print-multi-lib"))
3318 print_multi_lib = 1;
3319 else if (! strcmp (argv[i], "-print-multi-directory"))
3320 print_multi_directory = 1;
3321 else if (! strncmp (argv[i], "-Wa,", 4))
3323 int prev, j;
3324 /* Pass the rest of this option to the assembler. */
3326 /* Split the argument at commas. */
3327 prev = 4;
3328 for (j = 4; argv[i][j]; j++)
3329 if (argv[i][j] == ',')
3331 add_assembler_option (argv[i] + prev, j - prev);
3332 prev = j + 1;
3335 /* Record the part after the last comma. */
3336 add_assembler_option (argv[i] + prev, j - prev);
3338 else if (! strncmp (argv[i], "-Wp,", 4))
3340 int prev, j;
3341 /* Pass the rest of this option to the preprocessor. */
3343 /* Split the argument at commas. */
3344 prev = 4;
3345 for (j = 4; argv[i][j]; j++)
3346 if (argv[i][j] == ',')
3348 add_preprocessor_option (argv[i] + prev, j - prev);
3349 prev = j + 1;
3352 /* Record the part after the last comma. */
3353 add_preprocessor_option (argv[i] + prev, j - prev);
3355 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3356 /* The +e options to the C++ front-end. */
3357 n_switches++;
3358 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3360 int j;
3361 /* Split the argument at commas. */
3362 for (j = 3; argv[i][j]; j++)
3363 n_infiles += (argv[i][j] == ',');
3365 else if (strcmp (argv[i], "-Xlinker") == 0)
3367 if (i + 1 == argc)
3368 fatal ("argument to `-Xlinker' is missing");
3370 n_infiles++;
3371 i++;
3373 else if (strcmp (argv[i], "-l") == 0)
3375 if (i + 1 == argc)
3376 fatal ("argument to `-l' is missing");
3378 n_infiles++;
3379 i++;
3381 else if (strncmp (argv[i], "-l", 2) == 0)
3382 n_infiles++;
3383 else if (strcmp (argv[i], "-save-temps") == 0)
3385 save_temps_flag = 1;
3386 n_switches++;
3388 else if (strcmp (argv[i], "-specs") == 0)
3390 struct user_specs *user = (struct user_specs *)
3391 xmalloc (sizeof (struct user_specs));
3392 if (++i >= argc)
3393 fatal ("argument to `-specs' is missing");
3395 user->next = (struct user_specs *) 0;
3396 user->filename = argv[i];
3397 if (user_specs_tail)
3398 user_specs_tail->next = user;
3399 else
3400 user_specs_head = user;
3401 user_specs_tail = user;
3403 else if (strncmp (argv[i], "-specs=", 7) == 0)
3405 struct user_specs *user = (struct user_specs *)
3406 xmalloc (sizeof (struct user_specs));
3407 if (strlen (argv[i]) == 7)
3408 fatal ("argument to `-specs=' is missing");
3410 user->next = (struct user_specs *) 0;
3411 user->filename = argv[i] + 7;
3412 if (user_specs_tail)
3413 user_specs_tail->next = user;
3414 else
3415 user_specs_head = user;
3416 user_specs_tail = user;
3418 else if (strcmp (argv[i], "-time") == 0)
3419 report_times = 1;
3420 else if (argv[i][0] == '-' && argv[i][1] != 0)
3422 const char *p = &argv[i][1];
3423 int c = *p;
3425 switch (c)
3427 case 'b':
3428 n_switches++;
3429 if (p[1] == 0 && i + 1 == argc)
3430 fatal ("argument to `-b' is missing");
3431 if (p[1] == 0)
3432 spec_machine = argv[++i];
3433 else
3434 spec_machine = p + 1;
3436 warn_std_ptr = &warn_std;
3437 break;
3439 case 'B':
3441 const char *value;
3442 int len;
3444 if (p[1] == 0 && i + 1 == argc)
3445 fatal ("argument to `-B' is missing");
3446 if (p[1] == 0)
3447 value = argv[++i];
3448 else
3449 value = p + 1;
3451 len = strlen (value);
3453 /* Catch the case where the user has forgotten to append a
3454 directory separator to the path. Note, they may be using
3455 -B to add an executable name prefix, eg "i386-elf-", in
3456 order to distinguish between multiple installations of
3457 GCC in the same directory. Hence we must check to see
3458 if appending a directory separator actually makes a
3459 valid directory name. */
3460 if (! IS_DIR_SEPARATOR (value [len - 1])
3461 && is_directory (value, "", 0))
3463 char *tmp = xmalloc (len + 2);
3464 strcpy (tmp, value);
3465 tmp[len] = DIR_SEPARATOR;
3466 tmp[++ len] = 0;
3467 value = tmp;
3470 /* As a kludge, if the arg is "[foo/]stageN/", just
3471 add "[foo/]include" to the include prefix. */
3472 if ((len == 7
3473 || (len > 7
3474 && (IS_DIR_SEPARATOR (value[len - 8]))))
3475 && strncmp (value + len - 7, "stage", 5) == 0
3476 && ISDIGIT (value[len - 2])
3477 && (IS_DIR_SEPARATOR (value[len - 1])))
3479 if (len == 7)
3480 add_prefix (&include_prefixes, "include", NULL,
3481 PREFIX_PRIORITY_B_OPT, 0, NULL);
3482 else
3484 char * string = xmalloc (len + 1);
3486 strncpy (string, value, len - 7);
3487 strcpy (string + len - 7, "include");
3488 add_prefix (&include_prefixes, string, NULL,
3489 PREFIX_PRIORITY_B_OPT, 0, NULL);
3493 add_prefix (&exec_prefixes, value, NULL,
3494 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3495 add_prefix (&startfile_prefixes, value, NULL,
3496 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3497 add_prefix (&include_prefixes, concat (value, "include", NULL),
3498 NULL, PREFIX_PRIORITY_B_OPT, 0, NULL);
3499 n_switches++;
3501 break;
3503 case 'v': /* Print our subcommands and print versions. */
3504 n_switches++;
3505 /* If they do anything other than exactly `-v', don't set
3506 verbose_flag; rather, continue on to give the error. */
3507 if (p[1] != 0)
3508 break;
3509 verbose_flag++;
3510 break;
3512 case 'V':
3513 n_switches++;
3514 if (p[1] == 0 && i + 1 == argc)
3515 fatal ("argument to `-V' is missing");
3516 if (p[1] == 0)
3517 spec_version = argv[++i];
3518 else
3519 spec_version = p + 1;
3520 compiler_version = spec_version;
3521 warn_std_ptr = &warn_std;
3523 /* Validate the version number. Use the same checks
3524 done when inserting it into a spec.
3526 The format of the version string is
3527 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3529 const char *v = compiler_version;
3531 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3532 while (! ISDIGIT (*v))
3533 v++;
3535 if (v > compiler_version && v[-1] != '-')
3536 fatal ("invalid version number format");
3538 /* Set V after the first period. */
3539 while (ISDIGIT (*v))
3540 v++;
3542 if (*v != '.')
3543 fatal ("invalid version number format");
3545 v++;
3546 while (ISDIGIT (*v))
3547 v++;
3549 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
3550 fatal ("invalid version number format");
3552 break;
3554 case 'S':
3555 case 'c':
3556 if (p[1] == 0)
3558 have_c = 1;
3559 n_switches++;
3560 break;
3562 goto normal_switch;
3564 case 'o':
3565 have_o = 1;
3566 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX)
3567 if (! have_c)
3569 int skip;
3571 /* Forward scan, just in case -S or -c is specified
3572 after -o. */
3573 int j = i + 1;
3574 if (p[1] == 0)
3575 ++j;
3576 while (j < argc)
3578 if (argv[j][0] == '-')
3580 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3581 && argv[j][2] == 0)
3583 have_c = 1;
3584 break;
3586 else if ((skip = SWITCH_TAKES_ARG (argv[j][1])))
3587 j += skip - (argv[j][2] != 0);
3588 else if ((skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1)))
3589 j += skip;
3591 j++;
3594 #endif
3595 #if defined(HAVE_TARGET_EXECUTABLE_SUFFIX) || defined(HAVE_TARGET_OBJECT_SUFFIX)
3596 if (p[1] == 0)
3597 argv[i + 1] = convert_filename (argv[i + 1], ! have_c);
3598 else
3599 argv[i] = convert_filename (argv[i], ! have_c);
3600 #endif
3601 goto normal_switch;
3603 default:
3604 normal_switch:
3606 #ifdef MODIFY_TARGET_NAME
3607 is_modify_target_name = 0;
3609 for (j = 0;
3610 j < sizeof modify_target / sizeof modify_target[0]; j++)
3611 if (! strcmp (argv[i], modify_target[j].sw))
3613 char *new_name
3614 = (char *) xmalloc (strlen (modify_target[j].str)
3615 + strlen (spec_machine));
3616 const char *p, *r;
3617 char *q;
3618 int made_addition = 0;
3620 is_modify_target_name = 1;
3621 for (p = spec_machine, q = new_name; *p != 0; )
3623 if (modify_target[j].add_del == DELETE
3624 && (! strncmp (q, modify_target[j].str,
3625 strlen (modify_target[j].str))))
3626 p += strlen (modify_target[j].str);
3627 else if (modify_target[j].add_del == ADD
3628 && ! made_addition && *p == '-')
3630 for (r = modify_target[j].str; *r != 0; )
3631 *q++ = *r++;
3632 made_addition = 1;
3635 *q++ = *p++;
3638 spec_machine = new_name;
3641 if (is_modify_target_name)
3642 break;
3643 #endif
3645 n_switches++;
3647 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3648 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3649 else if (WORD_SWITCH_TAKES_ARG (p))
3650 i += WORD_SWITCH_TAKES_ARG (p);
3653 else
3655 n_infiles++;
3656 lang_n_infiles++;
3660 if (have_c && have_o && lang_n_infiles > 1)
3661 fatal ("cannot specify -o with -c or -S and multiple compilations");
3663 /* Set up the search paths before we go looking for config files. */
3665 /* These come before the md prefixes so that we will find gcc's subcommands
3666 (such as cpp) rather than those of the host system. */
3667 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3668 as well as trying the machine and the version. */
3669 #ifndef OS2
3670 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3671 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3672 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3673 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3674 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3675 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3676 #endif
3678 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3679 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3680 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3681 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3683 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3684 dir_separator_str, NULL);
3686 /* If tooldir is relative, base it on exec_prefixes. A relative
3687 tooldir lets us move the installed tree as a unit.
3689 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3690 directories, so that we can search both the user specified directory
3691 and the standard place. */
3693 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3695 if (gcc_exec_prefix)
3697 char *gcc_exec_tooldir_prefix
3698 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3699 spec_version, dir_separator_str, tooldir_prefix, NULL);
3701 add_prefix (&exec_prefixes,
3702 concat (gcc_exec_tooldir_prefix, "bin",
3703 dir_separator_str, NULL),
3704 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3705 add_prefix (&startfile_prefixes,
3706 concat (gcc_exec_tooldir_prefix, "lib",
3707 dir_separator_str, NULL),
3708 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
3711 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3712 dir_separator_str, spec_version,
3713 dir_separator_str, tooldir_prefix, NULL);
3716 add_prefix (&exec_prefixes,
3717 concat (tooldir_prefix, "bin", dir_separator_str, NULL),
3718 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3719 add_prefix (&startfile_prefixes,
3720 concat (tooldir_prefix, "lib", dir_separator_str, NULL),
3721 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
3723 /* More prefixes are enabled in main, after we read the specs file
3724 and determine whether this is cross-compilation or not. */
3726 /* Then create the space for the vectors and scan again. */
3728 switches = ((struct switchstr *)
3729 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3730 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3731 n_switches = 0;
3732 n_infiles = 0;
3733 last_language_n_infiles = -1;
3735 /* This, time, copy the text of each switch and store a pointer
3736 to the copy in the vector of switches.
3737 Store all the infiles in their vector. */
3739 for (i = 1; i < argc; i++)
3741 /* Just skip the switches that were handled by the preceding loop. */
3742 #ifdef MODIFY_TARGET_NAME
3743 is_modify_target_name = 0;
3745 for (j = 0; j < sizeof modify_target / sizeof modify_target[0]; j++)
3746 if (! strcmp (argv[i], modify_target[j].sw))
3747 is_modify_target_name = 1;
3749 if (is_modify_target_name)
3751 else
3752 #endif
3753 if (! strncmp (argv[i], "-Wa,", 4))
3755 else if (! strncmp (argv[i], "-Wp,", 4))
3757 else if (! strcmp (argv[i], "-pass-exit-codes"))
3759 else if (! strcmp (argv[i], "-print-search-dirs"))
3761 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3763 else if (! strncmp (argv[i], "-print-file-name=", 17))
3765 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3767 else if (! strcmp (argv[i], "-print-multi-lib"))
3769 else if (! strcmp (argv[i], "-print-multi-directory"))
3771 else if (strcmp (argv[i], "-ftarget-help") == 0)
3773 /* Create a dummy input file, so that we can pass --target-help on to
3774 the various sub-processes. */
3775 infiles[n_infiles].language = "c";
3776 infiles[n_infiles++].name = "target-dummy";
3778 /* Preserve the --target-help switch so that it can be caught by
3779 the cc1 spec string. */
3780 switches[n_switches].part1 = "--target-help";
3781 switches[n_switches].args = 0;
3782 switches[n_switches].live_cond = SWITCH_OK;
3783 switches[n_switches].validated = 0;
3785 n_switches++;
3787 else if (strcmp (argv[i], "-fhelp") == 0)
3789 if (verbose_flag)
3791 /* Create a dummy input file, so that we can pass --help on to
3792 the various sub-processes. */
3793 infiles[n_infiles].language = "c";
3794 infiles[n_infiles++].name = "help-dummy";
3796 /* Preserve the --help switch so that it can be caught by the
3797 cc1 spec string. */
3798 switches[n_switches].part1 = "--help";
3799 switches[n_switches].args = 0;
3800 switches[n_switches].live_cond = SWITCH_OK;
3801 switches[n_switches].validated = 0;
3803 n_switches++;
3806 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3808 /* Compensate for the +e options to the C++ front-end;
3809 they're there simply for cfront call-compatibility. We do
3810 some magic in default_compilers to pass them down properly.
3811 Note we deliberately start at the `+' here, to avoid passing
3812 -e0 or -e1 down into the linker. */
3813 switches[n_switches].part1 = &argv[i][0];
3814 switches[n_switches].args = 0;
3815 switches[n_switches].live_cond = SWITCH_OK;
3816 switches[n_switches].validated = 0;
3817 n_switches++;
3819 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3821 int prev, j;
3822 /* Split the argument at commas. */
3823 prev = 4;
3824 for (j = 4; argv[i][j]; j++)
3825 if (argv[i][j] == ',')
3827 infiles[n_infiles].language = "*";
3828 infiles[n_infiles++].name
3829 = save_string (argv[i] + prev, j - prev);
3830 prev = j + 1;
3832 /* Record the part after the last comma. */
3833 infiles[n_infiles].language = "*";
3834 infiles[n_infiles++].name = argv[i] + prev;
3836 else if (strcmp (argv[i], "-Xlinker") == 0)
3838 infiles[n_infiles].language = "*";
3839 infiles[n_infiles++].name = argv[++i];
3841 else if (strcmp (argv[i], "-l") == 0)
3842 { /* POSIX allows separation of -l and the lib arg;
3843 canonicalize by concatenating -l with its arg */
3844 infiles[n_infiles].language = "*";
3845 infiles[n_infiles++].name = concat ("-l", argv[++i], NULL);
3847 else if (strncmp (argv[i], "-l", 2) == 0)
3849 infiles[n_infiles].language = "*";
3850 infiles[n_infiles++].name = argv[i];
3852 else if (strcmp (argv[i], "-specs") == 0)
3853 i++;
3854 else if (strncmp (argv[i], "-specs=", 7) == 0)
3856 else if (strcmp (argv[i], "-time") == 0)
3858 else if ((save_temps_flag || report_times)
3859 && strcmp (argv[i], "-pipe") == 0)
3861 /* -save-temps overrides -pipe, so that temp files are produced */
3862 if (save_temps_flag)
3863 error ("Warning: -pipe ignored because -save-temps specified");
3864 /* -time overrides -pipe because we can't get correct stats when
3865 multiple children are running at once. */
3866 else if (report_times)
3867 error ("Warning: -pipe ignored because -time specified");
3869 else if (argv[i][0] == '-' && argv[i][1] != 0)
3871 const char *p = &argv[i][1];
3872 int c = *p;
3874 if (c == 'x')
3876 if (p[1] == 0 && i + 1 == argc)
3877 fatal ("argument to `-x' is missing");
3878 if (p[1] == 0)
3879 spec_lang = argv[++i];
3880 else
3881 spec_lang = p + 1;
3882 if (! strcmp (spec_lang, "none"))
3883 /* Suppress the warning if -xnone comes after the last input
3884 file, because alternate command interfaces like g++ might
3885 find it useful to place -xnone after each input file. */
3886 spec_lang = 0;
3887 else
3888 last_language_n_infiles = n_infiles;
3889 continue;
3891 switches[n_switches].part1 = p;
3892 /* Deal with option arguments in separate argv elements. */
3893 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3894 || WORD_SWITCH_TAKES_ARG (p))
3896 int j = 0;
3897 int n_args = WORD_SWITCH_TAKES_ARG (p);
3899 if (n_args == 0)
3901 /* Count only the option arguments in separate argv elements. */
3902 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3904 if (i + n_args >= argc)
3905 fatal ("argument to `-%s' is missing", p);
3906 switches[n_switches].args
3907 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3908 while (j < n_args)
3909 switches[n_switches].args[j++] = argv[++i];
3910 /* Null-terminate the vector. */
3911 switches[n_switches].args[j] = 0;
3913 else if (strchr (switches_need_spaces, c))
3915 /* On some systems, ld cannot handle some options without
3916 a space. So split the option from its argument. */
3917 char *part1 = (char *) xmalloc (2);
3918 part1[0] = c;
3919 part1[1] = '\0';
3921 switches[n_switches].part1 = part1;
3922 switches[n_switches].args
3923 = (const char **) xmalloc (2 * sizeof (const char *));
3924 switches[n_switches].args[0] = xstrdup (p+1);
3925 switches[n_switches].args[1] = 0;
3927 else
3928 switches[n_switches].args = 0;
3930 switches[n_switches].live_cond = SWITCH_OK;
3931 switches[n_switches].validated = 0;
3932 switches[n_switches].ordering = 0;
3933 /* These are always valid, since gcc.c itself understands it. */
3934 if (!strcmp (p, "save-temps")
3935 || !strcmp (p, "static-libgcc")
3936 || !strcmp (p, "shared-libgcc"))
3937 switches[n_switches].validated = 1;
3938 else
3940 char ch = switches[n_switches].part1[0];
3941 if (ch == 'V' || ch == 'b' || ch == 'B')
3942 switches[n_switches].validated = 1;
3944 n_switches++;
3946 else
3948 #ifdef HAVE_TARGET_OBJECT_SUFFIX
3949 argv[i] = convert_filename (argv[i], 0);
3950 #endif
3952 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
3954 perror_with_name (argv[i]);
3955 error_count++;
3957 else
3959 infiles[n_infiles].language = spec_lang;
3960 infiles[n_infiles++].name = argv[i];
3965 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3966 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3968 switches[n_switches].part1 = 0;
3969 infiles[n_infiles].name = 0;
3972 /* Process a spec string, accumulating and running commands. */
3974 /* These variables describe the input file name.
3975 input_file_number is the index on outfiles of this file,
3976 so that the output file name can be stored for later use by %o.
3977 input_basename is the start of the part of the input file
3978 sans all directory names, and basename_length is the number
3979 of characters starting there excluding the suffix .c or whatever. */
3981 const char *input_filename;
3982 static int input_file_number;
3983 size_t input_filename_length;
3984 static int basename_length;
3985 static int suffixed_basename_length;
3986 static const char *input_basename;
3987 static const char *input_suffix;
3989 /* The compiler used to process the current input file. */
3990 static struct compiler *input_file_compiler;
3992 /* These are variables used within do_spec and do_spec_1. */
3994 /* Nonzero if an arg has been started and not yet terminated
3995 (with space, tab or newline). */
3996 static int arg_going;
3998 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3999 is a temporary file name. */
4000 static int delete_this_arg;
4002 /* Nonzero means %w has been seen; the next arg to be terminated
4003 is the output file name of this compilation. */
4004 static int this_is_output_file;
4006 /* Nonzero means %s has been seen; the next arg to be terminated
4007 is the name of a library file and we should try the standard
4008 search dirs for it. */
4009 static int this_is_library_file;
4011 /* Nonzero means that the input of this command is coming from a pipe. */
4012 static int input_from_pipe;
4014 /* Nonnull means substitute this for any suffix when outputting a switches
4015 arguments. */
4016 static const char *suffix_subst;
4018 /* Process the spec SPEC and run the commands specified therein.
4019 Returns 0 if the spec is successfully processed; -1 if failed. */
4022 do_spec (spec)
4023 const char *spec;
4025 int value;
4027 clear_args ();
4028 arg_going = 0;
4029 delete_this_arg = 0;
4030 this_is_output_file = 0;
4031 this_is_library_file = 0;
4032 input_from_pipe = 0;
4033 suffix_subst = NULL;
4035 value = do_spec_1 (spec, 0, NULL);
4037 /* Force out any unfinished command.
4038 If -pipe, this forces out the last command if it ended in `|'. */
4039 if (value == 0)
4041 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4042 argbuf_index--;
4044 if (argbuf_index > 0)
4045 value = execute ();
4048 return value;
4051 /* Process the sub-spec SPEC as a portion of a larger spec.
4052 This is like processing a whole spec except that we do
4053 not initialize at the beginning and we do not supply a
4054 newline by default at the end.
4055 INSWITCH nonzero means don't process %-sequences in SPEC;
4056 in this case, % is treated as an ordinary character.
4057 This is used while substituting switches.
4058 INSWITCH nonzero also causes SPC not to terminate an argument.
4060 Value is zero unless a line was finished
4061 and the command on that line reported an error. */
4063 static int
4064 do_spec_1 (spec, inswitch, soft_matched_part)
4065 const char *spec;
4066 int inswitch;
4067 const char *soft_matched_part;
4069 const char *p = spec;
4070 int c;
4071 int i;
4072 const char *string;
4073 int value;
4075 while ((c = *p++))
4076 /* If substituting a switch, treat all chars like letters.
4077 Otherwise, NL, SPC, TAB and % are special. */
4078 switch (inswitch ? 'a' : c)
4080 case '\n':
4081 /* End of line: finish any pending argument,
4082 then run the pending command if one has been started. */
4083 if (arg_going)
4085 obstack_1grow (&obstack, 0);
4086 string = obstack_finish (&obstack);
4087 if (this_is_library_file)
4088 string = find_file (string);
4089 store_arg (string, delete_this_arg, this_is_output_file);
4090 if (this_is_output_file)
4091 outfiles[input_file_number] = string;
4093 arg_going = 0;
4095 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
4097 for (i = 0; i < n_switches; i++)
4098 if (!strcmp (switches[i].part1, "pipe"))
4099 break;
4101 /* A `|' before the newline means use a pipe here,
4102 but only if -pipe was specified.
4103 Otherwise, execute now and don't pass the `|' as an arg. */
4104 if (i < n_switches)
4106 input_from_pipe = 1;
4107 switches[i].validated = 1;
4108 break;
4110 else
4111 argbuf_index--;
4114 if (argbuf_index > 0)
4116 value = execute ();
4117 if (value)
4118 return value;
4120 /* Reinitialize for a new command, and for a new argument. */
4121 clear_args ();
4122 arg_going = 0;
4123 delete_this_arg = 0;
4124 this_is_output_file = 0;
4125 this_is_library_file = 0;
4126 input_from_pipe = 0;
4127 break;
4129 case '|':
4130 /* End any pending argument. */
4131 if (arg_going)
4133 obstack_1grow (&obstack, 0);
4134 string = obstack_finish (&obstack);
4135 if (this_is_library_file)
4136 string = find_file (string);
4137 store_arg (string, delete_this_arg, this_is_output_file);
4138 if (this_is_output_file)
4139 outfiles[input_file_number] = string;
4142 /* Use pipe */
4143 obstack_1grow (&obstack, c);
4144 arg_going = 1;
4145 break;
4147 case '\t':
4148 case ' ':
4149 /* Space or tab ends an argument if one is pending. */
4150 if (arg_going)
4152 obstack_1grow (&obstack, 0);
4153 string = obstack_finish (&obstack);
4154 if (this_is_library_file)
4155 string = find_file (string);
4156 store_arg (string, delete_this_arg, this_is_output_file);
4157 if (this_is_output_file)
4158 outfiles[input_file_number] = string;
4160 /* Reinitialize for a new argument. */
4161 arg_going = 0;
4162 delete_this_arg = 0;
4163 this_is_output_file = 0;
4164 this_is_library_file = 0;
4165 break;
4167 case '%':
4168 switch (c = *p++)
4170 case 0:
4171 fatal ("Invalid specification! Bug in cc.");
4173 case 'b':
4174 obstack_grow (&obstack, input_basename, basename_length);
4175 arg_going = 1;
4176 break;
4178 case 'B':
4179 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4180 arg_going = 1;
4181 break;
4183 case 'd':
4184 delete_this_arg = 2;
4185 break;
4187 /* Dump out the directories specified with LIBRARY_PATH,
4188 followed by the absolute directories
4189 that we search for startfiles. */
4190 case 'D':
4192 struct prefix_list *pl = startfile_prefixes.plist;
4193 size_t bufsize = 100;
4194 char *buffer = (char *) xmalloc (bufsize);
4195 int idx;
4197 for (; pl; pl = pl->next)
4199 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4200 /* Used on systems which record the specified -L dirs
4201 and use them to search for dynamic linking. */
4202 /* Relative directories always come from -B,
4203 and it is better not to use them for searching
4204 at run time. In particular, stage1 loses. */
4205 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4206 continue;
4207 #endif
4208 /* Try subdirectory if there is one. */
4209 if (multilib_dir != NULL)
4211 if (machine_suffix)
4213 if (strlen (pl->prefix) + strlen (machine_suffix)
4214 >= bufsize)
4215 bufsize = (strlen (pl->prefix)
4216 + strlen (machine_suffix)) * 2 + 1;
4217 buffer = (char *) xrealloc (buffer, bufsize);
4218 strcpy (buffer, pl->prefix);
4219 strcat (buffer, machine_suffix);
4220 if (is_directory (buffer, multilib_dir, 1))
4222 do_spec_1 ("-L", 0, NULL);
4223 #ifdef SPACE_AFTER_L_OPTION
4224 do_spec_1 (" ", 0, NULL);
4225 #endif
4226 do_spec_1 (buffer, 1, NULL);
4227 do_spec_1 (multilib_dir, 1, NULL);
4228 /* Make this a separate argument. */
4229 do_spec_1 (" ", 0, NULL);
4232 if (!pl->require_machine_suffix)
4234 if (is_directory (pl->prefix, multilib_dir, 1))
4236 do_spec_1 ("-L", 0, NULL);
4237 #ifdef SPACE_AFTER_L_OPTION
4238 do_spec_1 (" ", 0, NULL);
4239 #endif
4240 do_spec_1 (pl->prefix, 1, NULL);
4241 do_spec_1 (multilib_dir, 1, NULL);
4242 /* Make this a separate argument. */
4243 do_spec_1 (" ", 0, NULL);
4247 if (machine_suffix)
4249 if (is_directory (pl->prefix, machine_suffix, 1))
4251 do_spec_1 ("-L", 0, NULL);
4252 #ifdef SPACE_AFTER_L_OPTION
4253 do_spec_1 (" ", 0, NULL);
4254 #endif
4255 do_spec_1 (pl->prefix, 1, NULL);
4256 /* Remove slash from machine_suffix. */
4257 if (strlen (machine_suffix) >= bufsize)
4258 bufsize = strlen (machine_suffix) * 2 + 1;
4259 buffer = (char *) xrealloc (buffer, bufsize);
4260 strcpy (buffer, machine_suffix);
4261 idx = strlen (buffer);
4262 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4263 buffer[idx - 1] = 0;
4264 do_spec_1 (buffer, 1, NULL);
4265 /* Make this a separate argument. */
4266 do_spec_1 (" ", 0, NULL);
4269 if (!pl->require_machine_suffix)
4271 if (is_directory (pl->prefix, "", 1))
4273 do_spec_1 ("-L", 0, NULL);
4274 #ifdef SPACE_AFTER_L_OPTION
4275 do_spec_1 (" ", 0, NULL);
4276 #endif
4277 /* Remove slash from pl->prefix. */
4278 if (strlen (pl->prefix) >= bufsize)
4279 bufsize = strlen (pl->prefix) * 2 + 1;
4280 buffer = (char *) xrealloc (buffer, bufsize);
4281 strcpy (buffer, pl->prefix);
4282 idx = strlen (buffer);
4283 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4284 buffer[idx - 1] = 0;
4285 do_spec_1 (buffer, 1, NULL);
4286 /* Make this a separate argument. */
4287 do_spec_1 (" ", 0, NULL);
4291 free (buffer);
4293 break;
4295 case 'e':
4296 /* %efoo means report an error with `foo' as error message
4297 and don't execute any more commands for this file. */
4299 const char *q = p;
4300 char *buf;
4301 while (*p != 0 && *p != '\n')
4302 p++;
4303 buf = (char *) alloca (p - q + 1);
4304 strncpy (buf, q, p - q);
4305 buf[p - q] = 0;
4306 error ("%s", buf);
4307 return -1;
4309 break;
4310 case 'n':
4311 /* %nfoo means report an notice with `foo' on stderr. */
4313 const char *q = p;
4314 char *buf;
4315 while (*p != 0 && *p != '\n')
4316 p++;
4317 buf = (char *) alloca (p - q + 1);
4318 strncpy (buf, q, p - q);
4319 buf[p - q] = 0;
4320 notice ("%s\n", buf);
4321 if (*p)
4322 p++;
4324 break;
4326 case 'j':
4328 struct stat st;
4330 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4331 and it is not a directory, and it is writable, use it.
4332 Otherwise, fall through and treat this like any other
4333 temporary file. */
4335 if ((!save_temps_flag)
4336 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4337 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4339 obstack_grow (&obstack, HOST_BIT_BUCKET,
4340 strlen (HOST_BIT_BUCKET));
4341 delete_this_arg = 0;
4342 arg_going = 1;
4343 break;
4346 case 'g':
4347 case 'u':
4348 case 'U':
4349 if (save_temps_flag)
4351 obstack_grow (&obstack, input_basename, basename_length);
4352 delete_this_arg = 0;
4354 else
4356 struct temp_name *t;
4357 int suffix_length;
4358 const char *suffix = p;
4359 char *saved_suffix = NULL;
4361 while (*p == '.' || ISALPHA ((unsigned char) *p))
4362 p++;
4363 suffix_length = p - suffix;
4364 if (p[0] == '%' && p[1] == 'O')
4366 p += 2;
4367 /* We don't support extra suffix characters after %O. */
4368 if (*p == '.' || ISALPHA ((unsigned char) *p))
4369 abort ();
4370 if (suffix_length == 0)
4371 suffix = TARGET_OBJECT_SUFFIX;
4372 else
4374 saved_suffix
4375 = (char *) xmalloc (suffix_length
4376 + strlen (TARGET_OBJECT_SUFFIX));
4377 strncpy (saved_suffix, suffix, suffix_length);
4378 strcpy (saved_suffix + suffix_length,
4379 TARGET_OBJECT_SUFFIX);
4381 suffix_length += strlen (TARGET_OBJECT_SUFFIX);
4384 /* See if we already have an association of %g/%u/%U and
4385 suffix. */
4386 for (t = temp_names; t; t = t->next)
4387 if (t->length == suffix_length
4388 && strncmp (t->suffix, suffix, suffix_length) == 0
4389 && t->unique == (c != 'g'))
4390 break;
4392 /* Make a new association if needed. %u and %j
4393 require one. */
4394 if (t == 0 || c == 'u' || c == 'j')
4396 if (t == 0)
4398 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4399 t->next = temp_names;
4400 temp_names = t;
4402 t->length = suffix_length;
4403 if (saved_suffix)
4405 t->suffix = saved_suffix;
4406 saved_suffix = NULL;
4408 else
4409 t->suffix = save_string (suffix, suffix_length);
4410 t->unique = (c != 'g');
4411 temp_filename = make_temp_file (t->suffix);
4412 temp_filename_length = strlen (temp_filename);
4413 t->filename = temp_filename;
4414 t->filename_length = temp_filename_length;
4417 if (saved_suffix)
4418 free (saved_suffix);
4420 obstack_grow (&obstack, t->filename, t->filename_length);
4421 delete_this_arg = 1;
4423 arg_going = 1;
4424 break;
4426 case 'i':
4427 obstack_grow (&obstack, input_filename, input_filename_length);
4428 arg_going = 1;
4429 break;
4431 case 'I':
4433 struct prefix_list *pl = include_prefixes.plist;
4435 if (gcc_exec_prefix)
4437 do_spec_1 ("-iprefix", 1, NULL);
4438 /* Make this a separate argument. */
4439 do_spec_1 (" ", 0, NULL);
4440 do_spec_1 (gcc_exec_prefix, 1, NULL);
4441 do_spec_1 (" ", 0, NULL);
4444 for (; pl; pl = pl->next)
4446 do_spec_1 ("-isystem", 1, NULL);
4447 /* Make this a separate argument. */
4448 do_spec_1 (" ", 0, NULL);
4449 do_spec_1 (pl->prefix, 1, NULL);
4450 do_spec_1 (" ", 0, NULL);
4453 break;
4455 case 'o':
4457 int max = n_infiles;
4458 max += lang_specific_extra_outfiles;
4460 for (i = 0; i < max; i++)
4461 if (outfiles[i])
4462 store_arg (outfiles[i], 0, 0);
4463 break;
4466 case 'O':
4467 obstack_grow (&obstack, TARGET_OBJECT_SUFFIX, strlen (TARGET_OBJECT_SUFFIX));
4468 arg_going = 1;
4469 break;
4471 case 's':
4472 this_is_library_file = 1;
4473 break;
4475 case 'w':
4476 this_is_output_file = 1;
4477 break;
4479 case 'W':
4481 int cur_index = argbuf_index;
4482 /* Handle the {...} following the %W. */
4483 if (*p != '{')
4484 abort ();
4485 p = handle_braces (p + 1);
4486 if (p == 0)
4487 return -1;
4488 /* If any args were output, mark the last one for deletion
4489 on failure. */
4490 if (argbuf_index != cur_index)
4491 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4492 break;
4495 /* %x{OPTION} records OPTION for %X to output. */
4496 case 'x':
4498 const char *p1 = p;
4499 char *string;
4501 /* Skip past the option value and make a copy. */
4502 if (*p != '{')
4503 abort ();
4504 while (*p++ != '}')
4506 string = save_string (p1 + 1, p - p1 - 2);
4508 /* See if we already recorded this option. */
4509 for (i = 0; i < n_linker_options; i++)
4510 if (! strcmp (string, linker_options[i]))
4512 free (string);
4513 return 0;
4516 /* This option is new; add it. */
4517 add_linker_option (string, strlen (string));
4519 break;
4521 /* Dump out the options accumulated previously using %x. */
4522 case 'X':
4523 for (i = 0; i < n_linker_options; i++)
4525 do_spec_1 (linker_options[i], 1, NULL);
4526 /* Make each accumulated option a separate argument. */
4527 do_spec_1 (" ", 0, NULL);
4529 break;
4531 /* Dump out the options accumulated previously using -Wa,. */
4532 case 'Y':
4533 for (i = 0; i < n_assembler_options; i++)
4535 do_spec_1 (assembler_options[i], 1, NULL);
4536 /* Make each accumulated option a separate argument. */
4537 do_spec_1 (" ", 0, NULL);
4539 break;
4541 /* Dump out the options accumulated previously using -Wp,. */
4542 case 'Z':
4543 for (i = 0; i < n_preprocessor_options; i++)
4545 do_spec_1 (preprocessor_options[i], 1, NULL);
4546 /* Make each accumulated option a separate argument. */
4547 do_spec_1 (" ", 0, NULL);
4549 break;
4551 /* Here are digits and numbers that just process
4552 a certain constant string as a spec. */
4554 case '1':
4555 value = do_spec_1 (cc1_spec, 0, NULL);
4556 if (value != 0)
4557 return value;
4558 break;
4560 case '2':
4561 value = do_spec_1 (cc1plus_spec, 0, NULL);
4562 if (value != 0)
4563 return value;
4564 break;
4566 case 'a':
4567 value = do_spec_1 (asm_spec, 0, NULL);
4568 if (value != 0)
4569 return value;
4570 break;
4572 case 'A':
4573 value = do_spec_1 (asm_final_spec, 0, NULL);
4574 if (value != 0)
4575 return value;
4576 break;
4578 case 'c':
4579 value = do_spec_1 (signed_char_spec, 0, NULL);
4580 if (value != 0)
4581 return value;
4582 break;
4584 case 'C':
4586 const char *const spec
4587 = (input_file_compiler->cpp_spec
4588 ? input_file_compiler->cpp_spec
4589 : cpp_spec);
4590 value = do_spec_1 (spec, 0, NULL);
4591 if (value != 0)
4592 return value;
4594 break;
4596 case 'E':
4597 value = do_spec_1 (endfile_spec, 0, NULL);
4598 if (value != 0)
4599 return value;
4600 break;
4602 case 'l':
4603 value = do_spec_1 (link_spec, 0, NULL);
4604 if (value != 0)
4605 return value;
4606 break;
4608 case 'L':
4609 value = do_spec_1 (lib_spec, 0, NULL);
4610 if (value != 0)
4611 return value;
4612 break;
4614 case 'G':
4615 value = do_spec_1 (libgcc_spec, 0, NULL);
4616 if (value != 0)
4617 return value;
4618 break;
4620 case 'M':
4621 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4623 char *p;
4624 const char *q;
4625 size_t len;
4627 len = strlen (multilib_dir);
4628 obstack_blank (&obstack, len + 1);
4629 p = obstack_next_free (&obstack) - (len + 1);
4631 *p++ = '_';
4632 for (q = multilib_dir; *q ; ++q, ++p)
4633 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4635 break;
4637 case 'p':
4639 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4640 char *buf = x;
4641 const char *y;
4643 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4644 y = cpp_predefines;
4645 while (*y != 0)
4647 if (! strncmp (y, "-D", 2))
4648 /* Copy the whole option. */
4649 while (*y && *y != ' ' && *y != '\t')
4650 *x++ = *y++;
4651 else if (*y == ' ' || *y == '\t')
4652 /* Copy whitespace to the result. */
4653 *x++ = *y++;
4654 /* Don't copy other options. */
4655 else
4656 y++;
4659 *x = 0;
4661 value = do_spec_1 (buf, 0, NULL);
4662 if (value != 0)
4663 return value;
4665 break;
4667 case 'P':
4669 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4670 char *buf = x;
4671 const char *y;
4673 /* Copy all of CPP_PREDEFINES into BUF,
4674 but force them all into the reserved name space if they
4675 aren't already there. The reserved name space is all
4676 identifiers beginning with two underscores or with one
4677 underscore and a capital letter. We do the forcing by
4678 adding up to two underscores to the beginning and end
4679 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4680 become __mips__. */
4681 y = cpp_predefines;
4682 while (*y != 0)
4684 if (! strncmp (y, "-D", 2))
4686 int flag = 0;
4688 *x++ = *y++;
4689 *x++ = *y++;
4691 if (*y != '_'
4692 || (*(y + 1) != '_'
4693 && ! ISUPPER ((unsigned char) *(y + 1))))
4695 /* Stick __ at front of macro name. */
4696 if (*y != '_')
4697 *x++ = '_';
4698 *x++ = '_';
4699 /* Arrange to stick __ at the end as well. */
4700 flag = 1;
4703 /* Copy the macro name. */
4704 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4705 *x++ = *y++;
4707 if (flag)
4709 if (x[-1] != '_')
4711 if (x[-2] != '_')
4712 *x++ = '_';
4713 *x++ = '_';
4717 /* Copy the value given, if any. */
4718 while (*y && *y != ' ' && *y != '\t')
4719 *x++ = *y++;
4721 else if (*y == ' ' || *y == '\t')
4722 /* Copy whitespace to the result. */
4723 *x++ = *y++;
4724 /* Don't copy -A options */
4725 else
4726 y++;
4728 *x++ = ' ';
4730 /* Copy all of CPP_PREDEFINES into BUF,
4731 but put __ after every -D. */
4732 y = cpp_predefines;
4733 while (*y != 0)
4735 if (! strncmp (y, "-D", 2))
4737 y += 2;
4739 if (*y != '_'
4740 || (*(y + 1) != '_'
4741 && ! ISUPPER ((unsigned char) *(y + 1))))
4743 /* Stick -D__ at front of macro name. */
4744 *x++ = '-';
4745 *x++ = 'D';
4746 if (*y != '_')
4747 *x++ = '_';
4748 *x++ = '_';
4750 /* Copy the macro name. */
4751 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4752 *x++ = *y++;
4754 /* Copy the value given, if any. */
4755 while (*y && *y != ' ' && *y != '\t')
4756 *x++ = *y++;
4758 else
4760 /* Do not copy this macro - we have just done it before */
4761 while (*y && *y != ' ' && *y != '\t')
4762 y++;
4765 else if (*y == ' ' || *y == '\t')
4766 /* Copy whitespace to the result. */
4767 *x++ = *y++;
4768 /* Don't copy -A options. */
4769 else
4770 y++;
4772 *x++ = ' ';
4774 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4775 y = cpp_predefines;
4776 while (*y != 0)
4778 if (! strncmp (y, "-A", 2))
4779 /* Copy the whole option. */
4780 while (*y && *y != ' ' && *y != '\t')
4781 *x++ = *y++;
4782 else if (*y == ' ' || *y == '\t')
4783 /* Copy whitespace to the result. */
4784 *x++ = *y++;
4785 /* Don't copy other options. */
4786 else
4787 y++;
4790 *x = 0;
4792 value = do_spec_1 (buf, 0, NULL);
4793 if (value != 0)
4794 return value;
4796 break;
4798 case 'S':
4799 value = do_spec_1 (startfile_spec, 0, NULL);
4800 if (value != 0)
4801 return value;
4802 break;
4804 /* Here we define characters other than letters and digits. */
4806 case '{':
4807 p = handle_braces (p);
4808 if (p == 0)
4809 return -1;
4810 break;
4812 case '%':
4813 obstack_1grow (&obstack, '%');
4814 break;
4816 case '.':
4818 unsigned len = 0;
4820 while (p[len] && p[len] != ' ' && p[len] != '%')
4821 len++;
4822 suffix_subst = save_string (p - 1, len + 1);
4823 p += len;
4825 break;
4827 case '*':
4828 if (soft_matched_part)
4830 do_spec_1 (soft_matched_part, 1, NULL);
4831 do_spec_1 (" ", 0, NULL);
4833 else
4834 /* Catch the case where a spec string contains something like
4835 '%{foo:%*}'. ie there is no * in the pattern on the left
4836 hand side of the :. */
4837 error ("Spec failure: '%%*' has not been initialised by pattern match");
4838 break;
4840 /* Process a string found as the value of a spec given by name.
4841 This feature allows individual machine descriptions
4842 to add and use their own specs.
4843 %[...] modifies -D options the way %P does;
4844 %(...) uses the spec unmodified. */
4845 case '[':
4846 error ("Warning: use of obsolete %%[ operator in specs");
4847 case '(':
4849 const char *name = p;
4850 struct spec_list *sl;
4851 int len;
4853 /* The string after the S/P is the name of a spec that is to be
4854 processed. */
4855 while (*p && *p != ')' && *p != ']')
4856 p++;
4858 /* See if it's in the list. */
4859 for (len = p - name, sl = specs; sl; sl = sl->next)
4860 if (sl->name_len == len && !strncmp (sl->name, name, len))
4862 name = *(sl->ptr_spec);
4863 #ifdef DEBUG_SPECS
4864 notice ("Processing spec %c%s%c, which is '%s'\n",
4865 c, sl->name, (c == '(') ? ')' : ']', name);
4866 #endif
4867 break;
4870 if (sl)
4872 if (c == '(')
4874 value = do_spec_1 (name, 0, NULL);
4875 if (value != 0)
4876 return value;
4878 else
4880 char *x = (char *) alloca (strlen (name) * 2 + 1);
4881 char *buf = x;
4882 const char *y = name;
4883 int flag = 0;
4885 /* Copy all of NAME into BUF, but put __ after
4886 every -D and at the end of each arg. */
4887 while (1)
4889 if (! strncmp (y, "-D", 2))
4891 *x++ = '-';
4892 *x++ = 'D';
4893 *x++ = '_';
4894 *x++ = '_';
4895 y += 2;
4896 flag = 1;
4897 continue;
4899 else if (flag
4900 && (*y == ' ' || *y == '\t' || *y == '='
4901 || *y == '}' || *y == 0))
4903 *x++ = '_';
4904 *x++ = '_';
4905 flag = 0;
4907 if (*y == 0)
4908 break;
4909 else
4910 *x++ = *y++;
4912 *x = 0;
4914 value = do_spec_1 (buf, 0, NULL);
4915 if (value != 0)
4916 return value;
4920 /* Discard the closing paren or bracket. */
4921 if (*p)
4922 p++;
4924 break;
4926 case 'v':
4928 int c1 = *p++; /* Select first or second version number. */
4929 const char *v = compiler_version;
4930 const char *q;
4931 static const char zeroc = '0';
4933 /* The format of the version string is
4934 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
4936 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
4937 while (! ISDIGIT (*v))
4938 v++;
4939 if (v > compiler_version && v[-1] != '-')
4940 abort ();
4942 /* If desired, advance to second version number. */
4943 if (c1 >= '2')
4945 /* Set V after the first period. */
4946 while (ISDIGIT (*v))
4947 v++;
4948 if (*v != '.')
4949 abort ();
4950 v++;
4953 /* If desired, advance to third version number.
4954 But don't complain if it's not present */
4955 if (c1 == '3')
4957 /* Set V after the second period. */
4958 while (ISDIGIT (*v))
4959 v++;
4960 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
4961 abort ();
4962 if (*v != 0)
4963 v++;
4966 /* Set Q at the next period or at the end. */
4967 q = v;
4968 while (ISDIGIT (*q))
4969 q++;
4970 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
4971 abort ();
4973 if (q > v)
4974 /* Put that part into the command. */
4975 obstack_grow (&obstack, v, q - v);
4976 else
4977 /* Default to "0" */
4978 obstack_grow (&obstack, &zeroc, 1);
4979 arg_going = 1;
4981 break;
4983 case '|':
4984 if (input_from_pipe)
4985 do_spec_1 ("-", 0, NULL);
4986 break;
4988 default:
4989 error ("Spec failure: Unrecognised spec option '%c'", c);
4990 break;
4992 break;
4994 case '\\':
4995 /* Backslash: treat next character as ordinary. */
4996 c = *p++;
4998 /* fall through */
4999 default:
5000 /* Ordinary character: put it into the current argument. */
5001 obstack_1grow (&obstack, c);
5002 arg_going = 1;
5005 /* End of string. */
5006 return 0;
5009 /* Return 0 if we call do_spec_1 and that returns -1. */
5011 static const char *
5012 handle_braces (p)
5013 const char *p;
5015 const char *filter, *body = NULL, *endbody = NULL;
5016 int pipe_p = 0;
5017 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
5018 int negate;
5019 int suffix;
5020 int include_blanks = 1;
5021 int elide_switch = 0;
5022 int ordered = 0;
5024 if (*p == '^')
5026 /* A '^' after the open-brace means to not give blanks before args. */
5027 include_blanks = 0;
5028 ++p;
5031 if (*p == '|')
5033 /* A `|' after the open-brace means,
5034 if the test fails, output a single minus sign rather than nothing.
5035 This is used in %{|!pipe:...}. */
5036 pipe_p = 1;
5037 ++p;
5040 if (*p == '<')
5042 /* A `<' after the open-brace means that the switch should be
5043 removed from the command-line. */
5044 elide_switch = 1;
5045 ++p;
5048 next_member:
5049 negate = suffix = 0;
5051 if (*p == '!')
5052 /* A `!' after the open-brace negates the condition:
5053 succeed if the specified switch is not present. */
5054 negate = 1, ++p;
5056 if (*p == '.')
5057 /* A `.' after the open-brace means test against the current suffix. */
5059 if (pipe_p)
5060 abort ();
5062 suffix = 1;
5063 ++p;
5066 if (elide_switch && (negate || pipe_p || suffix))
5068 /* It doesn't make sense to mix elision with other flags. We
5069 could fatal() here, but the standard seems to be to abort. */
5070 abort ();
5073 next_ampersand:
5074 filter = p;
5075 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
5076 p++;
5078 if (*p == '|' && (pipe_p || ordered))
5079 abort ();
5081 if (!body)
5083 if (*p != '}' && *p != '&')
5085 int count = 1;
5086 const char *q = p;
5088 while (*q++ != ':')
5089 continue;
5090 body = q;
5092 while (count > 0)
5094 if (*q == '{')
5095 count++;
5096 else if (*q == '}')
5097 count--;
5098 else if (*q == 0)
5099 fatal ("Mismatched braces in specs");
5100 q++;
5102 endbody = q;
5104 else
5105 body = p, endbody = p + 1;
5108 if (suffix)
5110 int found = (input_suffix != 0
5111 && (long) strlen (input_suffix) == (long) (p - filter)
5112 && strncmp (input_suffix, filter, p - filter) == 0);
5114 if (body[0] == '}')
5115 abort ();
5117 if (negate != found
5118 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL) < 0)
5119 return 0;
5121 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5123 /* Substitute all matching switches as separate args. */
5124 int i;
5126 for (i = 0; i < n_switches; i++)
5127 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5128 && check_live_switch (i, p - 1 - filter))
5130 if (elide_switch)
5132 switches[i].live_cond = SWITCH_IGNORE;
5133 switches[i].validated = 1;
5135 else
5136 ordered = 1, switches[i].ordering = 1;
5139 else
5141 /* Test for presence of the specified switch. */
5142 int i;
5143 int present = 0;
5145 /* If name specified ends in *, as in {x*:...},
5146 check for %* and handle that case. */
5147 if (p[-1] == '*' && !negate)
5149 int substitution;
5150 const char *r = body;
5152 /* First see whether we have %*. */
5153 substitution = 0;
5154 while (r < endbody)
5156 if (*r == '%' && r[1] == '*')
5157 substitution = 1;
5158 r++;
5160 /* If we do, handle that case. */
5161 if (substitution)
5163 /* Substitute all matching switches as separate args.
5164 But do this by substituting for %*
5165 in the text that follows the colon. */
5167 unsigned hard_match_len = p - filter - 1;
5168 char *string = save_string (body, endbody - body - 1);
5170 for (i = 0; i < n_switches; i++)
5171 if (!strncmp (switches[i].part1, filter, hard_match_len)
5172 && check_live_switch (i, -1))
5174 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5175 /* Pass any arguments this switch has. */
5176 give_switch (i, 1, 1);
5177 suffix_subst = NULL;
5180 /* We didn't match. Try again. */
5181 if (*p++ == '|')
5182 goto next_member;
5183 return endbody;
5187 /* If name specified ends in *, as in {x*:...},
5188 check for presence of any switch name starting with x. */
5189 if (p[-1] == '*')
5191 for (i = 0; i < n_switches; i++)
5193 unsigned hard_match_len = p - filter - 1;
5195 if (!strncmp (switches[i].part1, filter, hard_match_len)
5196 && check_live_switch (i, hard_match_len))
5198 present = 1;
5199 break;
5203 /* Otherwise, check for presence of exact name specified. */
5204 else
5206 for (i = 0; i < n_switches; i++)
5208 if (!strncmp (switches[i].part1, filter, p - filter)
5209 && switches[i].part1[p - filter] == 0
5210 && check_live_switch (i, -1))
5212 present = 1;
5213 break;
5218 /* If it is as desired (present for %{s...}, absent for %{!s...})
5219 then substitute either the switch or the specified
5220 conditional text. */
5221 if (present != negate)
5223 if (elide_switch)
5225 switches[i].live_cond = SWITCH_IGNORE;
5226 switches[i].validated = 1;
5228 else if (ordered || *p == '&')
5229 ordered = 1, switches[i].ordering = 1;
5230 else if (*p == '}')
5231 give_switch (i, 0, include_blanks);
5232 else
5233 /* Even if many alternatives are matched, only output once. */
5234 true_once = 1;
5236 else if (pipe_p)
5238 /* Here if a %{|...} conditional fails: output a minus sign,
5239 which means "standard output" or "standard input". */
5240 do_spec_1 ("-", 0, NULL);
5241 return endbody;
5245 /* We didn't match; try again. */
5246 if (*p++ == '|')
5247 goto next_member;
5249 if (p[-1] == '&')
5251 body = 0;
5252 goto next_ampersand;
5255 if (ordered)
5257 int i;
5258 /* Doing this set of switches later preserves their command-line
5259 ordering. This is needed for e.g. -U, -D and -A. */
5260 for (i = 0; i < n_switches; i++)
5261 if (switches[i].ordering == 1)
5263 switches[i].ordering = 0;
5264 give_switch (i, 0, include_blanks);
5267 /* Process the spec just once, regardless of match count. */
5268 else if (true_once)
5270 if (do_spec_1 (save_string (body, endbody - body - 1),
5271 0, NULL) < 0)
5272 return 0;
5275 return endbody;
5278 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5279 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5280 spec, or -1 if either exact match or %* is used.
5282 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5283 whose value does not begin with "no-" is obsoleted by the same value
5284 with the "no-", similarly for a switch with the "no-" prefix. */
5286 static int
5287 check_live_switch (switchnum, prefix_length)
5288 int switchnum;
5289 int prefix_length;
5291 const char *name = switches[switchnum].part1;
5292 int i;
5294 /* In the common case of {<at-most-one-letter>*}, a negating
5295 switch would always match, so ignore that case. We will just
5296 send the conflicting switches to the compiler phase. */
5297 if (prefix_length >= 0 && prefix_length <= 1)
5298 return 1;
5300 /* If we already processed this switch and determined if it was
5301 live or not, return our past determination. */
5302 if (switches[switchnum].live_cond != 0)
5303 return switches[switchnum].live_cond > 0;
5305 /* Now search for duplicate in a manner that depends on the name. */
5306 switch (*name)
5308 case 'O':
5309 for (i = switchnum + 1; i < n_switches; i++)
5310 if (switches[i].part1[0] == 'O')
5312 switches[switchnum].validated = 1;
5313 switches[switchnum].live_cond = SWITCH_FALSE;
5314 return 0;
5316 break;
5318 case 'W': case 'f': case 'm':
5319 if (! strncmp (name + 1, "no-", 3))
5321 /* We have Xno-YYY, search for XYYY. */
5322 for (i = switchnum + 1; i < n_switches; i++)
5323 if (switches[i].part1[0] == name[0]
5324 && ! strcmp (&switches[i].part1[1], &name[4]))
5326 switches[switchnum].validated = 1;
5327 switches[switchnum].live_cond = SWITCH_FALSE;
5328 return 0;
5331 else
5333 /* We have XYYY, search for Xno-YYY. */
5334 for (i = switchnum + 1; i < n_switches; i++)
5335 if (switches[i].part1[0] == name[0]
5336 && switches[i].part1[1] == 'n'
5337 && switches[i].part1[2] == 'o'
5338 && switches[i].part1[3] == '-'
5339 && !strcmp (&switches[i].part1[4], &name[1]))
5341 switches[switchnum].validated = 1;
5342 switches[switchnum].live_cond = SWITCH_FALSE;
5343 return 0;
5346 break;
5349 /* Otherwise the switch is live. */
5350 switches[switchnum].live_cond = SWITCH_LIVE;
5351 return 1;
5354 /* Pass a switch to the current accumulating command
5355 in the same form that we received it.
5356 SWITCHNUM identifies the switch; it is an index into
5357 the vector of switches gcc received, which is `switches'.
5358 This cannot fail since it never finishes a command line.
5360 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5362 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5363 of the switch. */
5365 static void
5366 give_switch (switchnum, omit_first_word, include_blanks)
5367 int switchnum;
5368 int omit_first_word;
5369 int include_blanks;
5371 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5372 return;
5374 if (!omit_first_word)
5376 do_spec_1 ("-", 0, NULL);
5377 do_spec_1 (switches[switchnum].part1, 1, NULL);
5380 if (switches[switchnum].args != 0)
5382 const char **p;
5383 for (p = switches[switchnum].args; *p; p++)
5385 const char *arg = *p;
5387 if (include_blanks)
5388 do_spec_1 (" ", 0, NULL);
5389 if (suffix_subst)
5391 unsigned length = strlen (arg);
5392 int dot = 0;
5394 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5395 if (arg[length] == '.')
5397 ((char *)arg)[length] = 0;
5398 dot = 1;
5399 break;
5401 do_spec_1 (arg, 1, NULL);
5402 if (dot)
5403 ((char *)arg)[length] = '.';
5404 do_spec_1 (suffix_subst, 1, NULL);
5406 else
5407 do_spec_1 (arg, 1, NULL);
5411 do_spec_1 (" ", 0, NULL);
5412 switches[switchnum].validated = 1;
5415 /* Search for a file named NAME trying various prefixes including the
5416 user's -B prefix and some standard ones.
5417 Return the absolute file name found. If nothing is found, return NAME. */
5419 static const char *
5420 find_file (name)
5421 const char *name;
5423 char *newname;
5425 /* Try multilib_dir if it is defined. */
5426 if (multilib_dir != NULL)
5428 const char *const try = ACONCAT ((multilib_dir, dir_separator_str, name, NULL));
5430 newname = find_a_file (&startfile_prefixes, try, R_OK);
5432 /* If we don't find it in the multi library dir, then fall
5433 through and look for it in the normal places. */
5434 if (newname != NULL)
5435 return newname;
5438 newname = find_a_file (&startfile_prefixes, name, R_OK);
5439 return newname ? newname : name;
5442 /* Determine whether a directory exists. If LINKER, return 0 for
5443 certain fixed names not needed by the linker. If not LINKER, it is
5444 only important to return 0 if the host machine has a small ARG_MAX
5445 limit. */
5447 static int
5448 is_directory (path1, path2, linker)
5449 const char *path1;
5450 const char *path2;
5451 int linker;
5453 int len1 = strlen (path1);
5454 int len2 = strlen (path2);
5455 char *path = (char *) alloca (3 + len1 + len2);
5456 char *cp;
5457 struct stat st;
5459 #ifndef SMALL_ARG_MAX
5460 if (! linker)
5461 return 1;
5462 #endif
5464 /* Construct the path from the two parts. Ensure the string ends with "/.".
5465 The resulting path will be a directory even if the given path is a
5466 symbolic link. */
5467 memcpy (path, path1, len1);
5468 memcpy (path + len1, path2, len2);
5469 cp = path + len1 + len2;
5470 if (!IS_DIR_SEPARATOR (cp[-1]))
5471 *cp++ = DIR_SEPARATOR;
5472 *cp++ = '.';
5473 *cp = '\0';
5475 /* Exclude directories that the linker is known to search. */
5476 if (linker
5477 && ((cp - path == 6
5478 && strcmp (path, concat (dir_separator_str, "lib",
5479 dir_separator_str, ".", NULL)) == 0)
5480 || (cp - path == 10
5481 && strcmp (path, concat (dir_separator_str, "usr",
5482 dir_separator_str, "lib",
5483 dir_separator_str, ".", NULL)) == 0)))
5484 return 0;
5486 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5489 /* Set up the various global variables to indicate that we're processing
5490 the input file named FILENAME. */
5492 static void
5493 set_input (filename)
5494 const char *filename;
5496 const char *p;
5498 input_filename = filename;
5499 input_filename_length = strlen (input_filename);
5501 input_basename = input_filename;
5502 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5503 /* Skip drive name so 'x:foo' is handled properly. */
5504 if (input_basename[1] == ':')
5505 input_basename += 2;
5506 #endif
5507 for (p = input_basename; *p; p++)
5508 if (IS_DIR_SEPARATOR (*p))
5509 input_basename = p + 1;
5511 /* Find a suffix starting with the last period,
5512 and set basename_length to exclude that suffix. */
5513 basename_length = strlen (input_basename);
5514 suffixed_basename_length = basename_length;
5515 p = input_basename + basename_length;
5516 while (p != input_basename && *p != '.')
5517 --p;
5518 if (*p == '.' && p != input_basename)
5520 basename_length = p - input_basename;
5521 input_suffix = p + 1;
5523 else
5524 input_suffix = "";
5527 /* On fatal signals, delete all the temporary files. */
5529 static void
5530 fatal_error (signum)
5531 int signum;
5533 signal (signum, SIG_DFL);
5534 delete_failure_queue ();
5535 delete_temp_files ();
5536 /* Get the same signal again, this time not handled,
5537 so its normal effect occurs. */
5538 kill (getpid (), signum);
5541 extern int main PARAMS ((int, const char *const *));
5544 main (argc, argv)
5545 int argc;
5546 const char *const *argv;
5548 size_t i;
5549 int value;
5550 int linker_was_run = 0;
5551 char *explicit_link_files;
5552 char *specs_file;
5553 const char *p;
5554 struct user_specs *uptr;
5556 p = argv[0] + strlen (argv[0]);
5557 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5558 --p;
5559 programname = p;
5561 xmalloc_set_program_name (programname);
5563 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5564 /* Perform host dependent initialization when needed. */
5565 GCC_DRIVER_HOST_INITIALIZATION;
5566 #endif
5568 gcc_init_libintl ();
5570 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5571 signal (SIGINT, fatal_error);
5572 #ifdef SIGHUP
5573 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5574 signal (SIGHUP, fatal_error);
5575 #endif
5576 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5577 signal (SIGTERM, fatal_error);
5578 #ifdef SIGPIPE
5579 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5580 signal (SIGPIPE, fatal_error);
5581 #endif
5582 #ifdef SIGCHLD
5583 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5584 receive the signal. A different setting is inheritable */
5585 signal (SIGCHLD, SIG_DFL);
5586 #endif
5588 argbuf_length = 10;
5589 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5591 obstack_init (&obstack);
5593 /* Build multilib_select, et. al from the separate lines that make up each
5594 multilib selection. */
5596 const char *const *q = multilib_raw;
5597 int need_space;
5599 obstack_init (&multilib_obstack);
5600 while ((p = *q++) != (char *) 0)
5601 obstack_grow (&multilib_obstack, p, strlen (p));
5603 obstack_1grow (&multilib_obstack, 0);
5604 multilib_select = obstack_finish (&multilib_obstack);
5606 q = multilib_matches_raw;
5607 while ((p = *q++) != (char *) 0)
5608 obstack_grow (&multilib_obstack, p, strlen (p));
5610 obstack_1grow (&multilib_obstack, 0);
5611 multilib_matches = obstack_finish (&multilib_obstack);
5613 q = multilib_exclusions_raw;
5614 while ((p = *q++) != (char *) 0)
5615 obstack_grow (&multilib_obstack, p, strlen (p));
5617 obstack_1grow (&multilib_obstack, 0);
5618 multilib_exclusions = obstack_finish (&multilib_obstack);
5620 need_space = FALSE;
5621 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5623 if (need_space)
5624 obstack_1grow (&multilib_obstack, ' ');
5625 obstack_grow (&multilib_obstack,
5626 multilib_defaults_raw[i],
5627 strlen (multilib_defaults_raw[i]));
5628 need_space = TRUE;
5631 obstack_1grow (&multilib_obstack, 0);
5632 multilib_defaults = obstack_finish (&multilib_obstack);
5635 /* Set up to remember the pathname of gcc and any options
5636 needed for collect. We use argv[0] instead of programname because
5637 we need the complete pathname. */
5638 obstack_init (&collect_obstack);
5639 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5640 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5641 putenv (obstack_finish (&collect_obstack));
5643 #ifdef INIT_ENVIRONMENT
5644 /* Set up any other necessary machine specific environment variables. */
5645 putenv (INIT_ENVIRONMENT);
5646 #endif
5648 /* Make a table of what switches there are (switches, n_switches).
5649 Make a table of specified input files (infiles, n_infiles).
5650 Decode switches that are handled locally. */
5652 process_command (argc, argv);
5655 int first_time;
5657 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
5658 the compiler. */
5659 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
5660 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
5662 first_time = TRUE;
5663 for (i = 0; (int) i < n_switches; i++)
5665 const char *const *args;
5666 const char *p, *q;
5667 if (!first_time)
5668 obstack_grow (&collect_obstack, " ", 1);
5670 first_time = FALSE;
5671 obstack_grow (&collect_obstack, "'-", 2);
5672 q = switches[i].part1;
5673 while ((p = strchr (q, '\'')))
5675 obstack_grow (&collect_obstack, q, p - q);
5676 obstack_grow (&collect_obstack, "'\\''", 4);
5677 q = ++p;
5679 obstack_grow (&collect_obstack, q, strlen (q));
5680 obstack_grow (&collect_obstack, "'", 1);
5682 for (args = switches[i].args; args && *args; args++)
5684 obstack_grow (&collect_obstack, " '", 2);
5685 q = *args;
5686 while ((p = strchr (q, '\'')))
5688 obstack_grow (&collect_obstack, q, p - q);
5689 obstack_grow (&collect_obstack, "'\\''", 4);
5690 q = ++p;
5692 obstack_grow (&collect_obstack, q, strlen (q));
5693 obstack_grow (&collect_obstack, "'", 1);
5696 obstack_grow (&collect_obstack, "\0", 1);
5697 putenv (obstack_finish (&collect_obstack));
5700 /* Initialize the vector of specs to just the default.
5701 This means one element containing 0s, as a terminator. */
5703 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5704 memcpy ((char *) compilers, (char *) default_compilers,
5705 sizeof default_compilers);
5706 n_compilers = n_default_compilers;
5708 /* Read specs from a file if there is one. */
5710 machine_suffix = concat (spec_machine, dir_separator_str,
5711 spec_version, dir_separator_str, NULL);
5712 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL);
5714 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5715 /* Read the specs file unless it is a default one. */
5716 if (specs_file != 0 && strcmp (specs_file, "specs"))
5717 read_specs (specs_file, TRUE);
5718 else
5719 init_spec ();
5721 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5722 for any override of as, ld and libraries. */
5723 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5724 + strlen (just_machine_suffix)
5725 + sizeof ("specs"));
5727 strcpy (specs_file, standard_exec_prefix);
5728 strcat (specs_file, just_machine_suffix);
5729 strcat (specs_file, "specs");
5730 if (access (specs_file, R_OK) == 0)
5731 read_specs (specs_file, TRUE);
5733 /* If not cross-compiling, look for startfiles in the standard places. */
5734 if (*cross_compile == '0')
5736 if (*md_exec_prefix)
5738 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5739 PREFIX_PRIORITY_LAST, 0, NULL);
5740 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5741 PREFIX_PRIORITY_LAST, 0, NULL);
5744 if (*md_startfile_prefix)
5745 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5746 PREFIX_PRIORITY_LAST, 0, NULL);
5748 if (*md_startfile_prefix_1)
5749 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5750 PREFIX_PRIORITY_LAST, 0, NULL);
5752 /* If standard_startfile_prefix is relative, base it on
5753 standard_exec_prefix. This lets us move the installed tree
5754 as a unit. If GCC_EXEC_PREFIX is defined, base
5755 standard_startfile_prefix on that as well. */
5756 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5757 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5758 PREFIX_PRIORITY_LAST, 0, NULL);
5759 else
5761 if (gcc_exec_prefix)
5762 add_prefix (&startfile_prefixes,
5763 concat (gcc_exec_prefix, machine_suffix,
5764 standard_startfile_prefix, NULL),
5765 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5766 add_prefix (&startfile_prefixes,
5767 concat (standard_exec_prefix,
5768 machine_suffix,
5769 standard_startfile_prefix, NULL),
5770 NULL, PREFIX_PRIORITY_LAST, 0, NULL);
5773 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5774 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5775 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5776 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5777 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5778 add_prefix (&startfile_prefixes, "./", NULL,
5779 PREFIX_PRIORITY_LAST, 1, NULL);
5780 #endif
5782 else
5784 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5785 && gcc_exec_prefix)
5786 add_prefix (&startfile_prefixes,
5787 concat (gcc_exec_prefix, machine_suffix,
5788 standard_startfile_prefix, NULL),
5789 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL);
5792 /* Process any user specified specs in the order given on the command
5793 line. */
5794 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5796 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5797 read_specs (filename ? filename : uptr->filename, FALSE);
5800 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5801 if (gcc_exec_prefix)
5802 gcc_exec_prefix = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
5803 spec_version, dir_separator_str, NULL);
5805 /* Now we have the specs.
5806 Set the `valid' bits for switches that match anything in any spec. */
5808 validate_all_switches ();
5810 /* Now that we have the switches and the specs, set
5811 the subdirectory based on the options. */
5812 set_multilib_dir ();
5814 /* Warn about any switches that no pass was interested in. */
5816 for (i = 0; (int) i < n_switches; i++)
5817 if (! switches[i].validated)
5818 error ("unrecognized option `-%s'", switches[i].part1);
5820 /* Obey some of the options. */
5822 if (print_search_dirs)
5824 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5825 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5826 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5827 return (0);
5830 if (print_file_name)
5832 printf ("%s\n", find_file (print_file_name));
5833 return (0);
5836 if (print_prog_name)
5838 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5839 printf ("%s\n", (newname ? newname : print_prog_name));
5840 return (0);
5843 if (print_multi_lib)
5845 print_multilib_info ();
5846 return (0);
5849 if (print_multi_directory)
5851 if (multilib_dir == NULL)
5852 printf (".\n");
5853 else
5854 printf ("%s\n", multilib_dir);
5855 return (0);
5858 if (target_help_flag)
5860 /* Print if any target specific options.*/
5862 /* We do not exit here. Instead we have created a fake input file
5863 called 'target-dummy' which needs to be compiled, and we pass this
5864 on to the various sub-processes, along with the --target-help
5865 switch. */
5868 if (print_help_list)
5870 display_help ();
5872 if (! verbose_flag)
5874 printf (_("\nFor bug reporting instructions, please see:\n"));
5875 printf ("%s.\n", GCCBUGURL);
5877 return (0);
5880 /* We do not exit here. Instead we have created a fake input file
5881 called 'help-dummy' which needs to be compiled, and we pass this
5882 on the various sub-processes, along with the --help switch. */
5885 if (verbose_flag)
5887 int n;
5888 const char *thrmod;
5890 notice ("Configured with: %s\n", configuration_arguments);
5892 #ifdef THREAD_MODEL_SPEC
5893 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
5894 but there's no point in doing all this processing just to get
5895 thread_model back. */
5896 obstack_init (&obstack);
5897 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
5898 obstack_1grow (&obstack, '\0');
5899 thrmod = obstack_finish (&obstack);
5900 #else
5901 thrmod = thread_model;
5902 #endif
5904 notice ("Thread model: %s\n", thrmod);
5906 /* compiler_version is truncated at the first space when initialized
5907 from version string, so truncate version_string at the first space
5908 before comparing. */
5909 for (n = 0; version_string[n]; n++)
5910 if (version_string[n] == ' ')
5911 break;
5913 if (! strncmp (version_string, compiler_version, n)
5914 && compiler_version[n] == 0)
5915 notice ("gcc version %s\n", version_string);
5916 else
5917 notice ("gcc driver version %s executing gcc version %s\n",
5918 version_string, compiler_version);
5920 if (n_infiles == 0)
5921 return (0);
5924 if (n_infiles == added_libraries)
5925 fatal ("No input files");
5927 /* Make a place to record the compiler output file names
5928 that correspond to the input files. */
5930 i = n_infiles;
5931 i += lang_specific_extra_outfiles;
5932 outfiles = (const char **) xcalloc (i, sizeof (char *));
5934 /* Record which files were specified explicitly as link input. */
5936 explicit_link_files = xcalloc (1, n_infiles);
5938 for (i = 0; (int) i < n_infiles; i++)
5940 int this_file_error = 0;
5942 /* Tell do_spec what to substitute for %i. */
5944 input_file_number = i;
5945 set_input (infiles[i].name);
5947 /* Use the same thing in %o, unless cp->spec says otherwise. */
5949 outfiles[i] = input_filename;
5951 /* Figure out which compiler from the file's suffix. */
5953 input_file_compiler
5954 = lookup_compiler (infiles[i].name, input_filename_length,
5955 infiles[i].language);
5957 if (input_file_compiler)
5959 /* Ok, we found an applicable compiler. Run its spec. */
5961 if (input_file_compiler->spec[0] == '#')
5963 error ("%s: %s compiler not installed on this system",
5964 input_filename, &input_file_compiler->spec[1]);
5965 this_file_error = 1;
5967 else
5969 value = do_spec (input_file_compiler->spec);
5970 if (value < 0)
5971 this_file_error = 1;
5975 /* If this file's name does not contain a recognized suffix,
5976 record it as explicit linker input. */
5978 else
5979 explicit_link_files[i] = 1;
5981 /* Clear the delete-on-failure queue, deleting the files in it
5982 if this compilation failed. */
5984 if (this_file_error)
5986 delete_failure_queue ();
5987 error_count++;
5989 /* If this compilation succeeded, don't delete those files later. */
5990 clear_failure_queue ();
5993 /* Reset the output file name to the first input file name, for use
5994 with %b in LINK_SPEC on a target that prefers not to emit a.out
5995 by default. */
5996 if (n_infiles > 0)
5997 set_input (infiles[0].name);
5999 if (error_count == 0)
6001 /* Make sure INPUT_FILE_NUMBER points to first available open
6002 slot. */
6003 input_file_number = n_infiles;
6004 if (lang_specific_pre_link ())
6005 error_count++;
6008 /* Run ld to link all the compiler output files. */
6010 if (error_count == 0)
6012 int tmp = execution_count;
6014 /* We'll use ld if we can't find collect2. */
6015 if (! strcmp (linker_name_spec, "collect2"))
6017 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
6018 if (s == NULL)
6019 linker_name_spec = "ld";
6021 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
6022 for collect. */
6023 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
6024 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
6026 value = do_spec (link_command_spec);
6027 if (value < 0)
6028 error_count = 1;
6029 linker_was_run = (tmp != execution_count);
6032 /* If options said don't run linker,
6033 complain about input files to be given to the linker. */
6035 if (! linker_was_run && error_count == 0)
6036 for (i = 0; (int) i < n_infiles; i++)
6037 if (explicit_link_files[i])
6038 error ("%s: linker input file unused because linking not done",
6039 outfiles[i]);
6041 /* Delete some or all of the temporary files we made. */
6043 if (error_count)
6044 delete_failure_queue ();
6045 delete_temp_files ();
6047 if (print_help_list)
6049 printf (("\nFor bug reporting instructions, please see:\n"));
6050 printf ("%s\n", GCCBUGURL);
6053 return (signal_count != 0 ? 2
6054 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
6055 : 0);
6058 /* Find the proper compilation spec for the file name NAME,
6059 whose length is LENGTH. LANGUAGE is the specified language,
6060 or 0 if this file is to be passed to the linker. */
6062 static struct compiler *
6063 lookup_compiler (name, length, language)
6064 const char *name;
6065 size_t length;
6066 const char *language;
6068 struct compiler *cp;
6070 /* If this was specified by the user to be a linker input, indicate that. */
6071 if (language != 0 && language[0] == '*')
6072 return 0;
6074 /* Otherwise, look for the language, if one is spec'd. */
6075 if (language != 0)
6077 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6078 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
6079 return cp;
6081 error ("language %s not recognized", language);
6082 return 0;
6085 /* Look for a suffix. */
6086 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6088 if (/* The suffix `-' matches only the file name `-'. */
6089 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6090 || (strlen (cp->suffix) < length
6091 /* See if the suffix matches the end of NAME. */
6092 && !strcmp (cp->suffix,
6093 name + length - strlen (cp->suffix))
6095 break;
6098 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6099 /* look again, but case-insensitively this time. */
6100 if (cp < compilers)
6101 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6103 if (/* The suffix `-' matches only the file name `-'. */
6104 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6105 || (strlen (cp->suffix) < length
6106 /* See if the suffix matches the end of NAME. */
6107 && ((!strcmp (cp->suffix,
6108 name + length - strlen (cp->suffix))
6109 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6110 && !strcasecmp (cp->suffix,
6111 name + length - strlen (cp->suffix)))
6113 break;
6115 #endif
6117 if (cp >= compilers)
6119 if (cp->spec[0] != '@')
6120 /* A non-alias entry: return it. */
6121 return cp;
6123 /* An alias entry maps a suffix to a language.
6124 Search for the language; pass 0 for NAME and LENGTH
6125 to avoid infinite recursion if language not found. */
6126 return lookup_compiler (NULL, 0, cp->spec + 1);
6128 return 0;
6131 static char *
6132 save_string (s, len)
6133 const char *s;
6134 int len;
6136 char *result = xmalloc (len + 1);
6138 memcpy (result, s, len);
6139 result[len] = 0;
6140 return result;
6143 void
6144 pfatal_with_name (name)
6145 const char *name;
6147 perror_with_name (name);
6148 delete_temp_files ();
6149 exit (1);
6152 static void
6153 perror_with_name (name)
6154 const char *name;
6156 error ("%s: %s", name, xstrerror (errno));
6159 static void
6160 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6161 const char *errmsg_fmt;
6162 const char *errmsg_arg;
6164 if (errmsg_arg)
6166 int save_errno = errno;
6168 /* Space for trailing '\0' is in %s. */
6169 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6170 sprintf (msg, errmsg_fmt, errmsg_arg);
6171 errmsg_fmt = msg;
6173 errno = save_errno;
6176 pfatal_with_name (errmsg_fmt);
6179 /* Output an error message and exit */
6181 void
6182 fancy_abort ()
6184 fatal ("Internal gcc abort.");
6187 /* Output an error message and exit */
6189 void
6190 fatal VPARAMS ((const char *msgid, ...))
6192 VA_OPEN (ap, msgid);
6193 VA_FIXEDARG (ap, const char *, msgid);
6195 fprintf (stderr, "%s: ", programname);
6196 vfprintf (stderr, _(msgid), ap);
6197 VA_CLOSE (ap);
6198 fprintf (stderr, "\n");
6199 delete_temp_files ();
6200 exit (1);
6203 void
6204 error VPARAMS ((const char *msgid, ...))
6206 VA_OPEN (ap, msgid);
6207 VA_FIXEDARG (ap, const char *, msgid);
6209 fprintf (stderr, "%s: ", programname);
6210 vfprintf (stderr, _(msgid), ap);
6211 VA_CLOSE (ap);
6213 fprintf (stderr, "\n");
6216 static void
6217 notice VPARAMS ((const char *msgid, ...))
6219 VA_OPEN (ap, msgid);
6220 VA_FIXEDARG (ap, const char *, msgid);
6222 vfprintf (stderr, _(msgid), ap);
6223 VA_CLOSE (ap);
6226 static void
6227 validate_all_switches ()
6229 struct compiler *comp;
6230 const char *p;
6231 char c;
6232 struct spec_list *spec;
6234 for (comp = compilers; comp->spec; comp++)
6236 p = comp->spec;
6237 while ((c = *p++))
6238 if (c == '%' && *p == '{')
6239 /* We have a switch spec. */
6240 validate_switches (p + 1);
6243 /* Look through the linked list of specs read from the specs file. */
6244 for (spec = specs; spec; spec = spec->next)
6246 p = *(spec->ptr_spec);
6247 while ((c = *p++))
6248 if (c == '%' && *p == '{')
6249 /* We have a switch spec. */
6250 validate_switches (p + 1);
6253 p = link_command_spec;
6254 while ((c = *p++))
6255 if (c == '%' && *p == '{')
6256 /* We have a switch spec. */
6257 validate_switches (p + 1);
6260 /* Look at the switch-name that comes after START
6261 and mark as valid all supplied switches that match it. */
6263 static void
6264 validate_switches (start)
6265 const char *start;
6267 const char *p = start;
6268 const char *filter;
6269 int i;
6270 int suffix;
6272 if (*p == '|')
6273 ++p;
6275 next_member:
6276 if (*p == '!')
6277 ++p;
6279 suffix = 0;
6280 if (*p == '.')
6281 suffix = 1, ++p;
6283 filter = p;
6284 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6285 p++;
6287 if (suffix)
6289 else if (p[-1] == '*')
6291 /* Mark all matching switches as valid. */
6292 for (i = 0; i < n_switches; i++)
6293 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6294 switches[i].validated = 1;
6296 else
6298 /* Mark an exact matching switch as valid. */
6299 for (i = 0; i < n_switches; i++)
6301 if (!strncmp (switches[i].part1, filter, p - filter)
6302 && switches[i].part1[p - filter] == 0)
6303 switches[i].validated = 1;
6307 if (*p++ == '|' || p[-1] == '&')
6308 goto next_member;
6311 /* Check whether a particular argument was used. The first time we
6312 canonicalize the switches to keep only the ones we care about. */
6314 static int
6315 used_arg (p, len)
6316 const char *p;
6317 int len;
6319 struct mswitchstr
6321 const char *str;
6322 const char *replace;
6323 int len;
6324 int rep_len;
6327 static struct mswitchstr *mswitches;
6328 static int n_mswitches;
6329 int i, j;
6331 if (!mswitches)
6333 struct mswitchstr *matches;
6334 const char *q;
6335 int cnt = 0;
6337 /* Break multilib_matches into the component strings of string
6338 and replacement string. */
6339 for (q = multilib_matches; *q != '\0'; q++)
6340 if (*q == ';')
6341 cnt++;
6343 matches =
6344 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6345 i = 0;
6346 q = multilib_matches;
6347 while (*q != '\0')
6349 matches[i].str = q;
6350 while (*q != ' ')
6352 if (*q == '\0')
6353 abort ();
6354 q++;
6356 matches[i].len = q - matches[i].str;
6358 matches[i].replace = ++q;
6359 while (*q != ';' && *q != '\0')
6361 if (*q == ' ')
6362 abort ();
6363 q++;
6365 matches[i].rep_len = q - matches[i].replace;
6366 i++;
6367 if (*q == ';')
6368 q++;
6371 /* Now build a list of the replacement string for switches that we care
6372 about. Make sure we allocate at least one entry. This prevents
6373 xmalloc from calling fatal, and prevents us from re-executing this
6374 block of code. */
6375 mswitches
6376 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6377 * (n_switches ? n_switches : 1));
6378 for (i = 0; i < n_switches; i++)
6380 int xlen = strlen (switches[i].part1);
6381 for (j = 0; j < cnt; j++)
6382 if (xlen == matches[j].len
6383 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6385 mswitches[n_mswitches].str = matches[j].replace;
6386 mswitches[n_mswitches].len = matches[j].rep_len;
6387 mswitches[n_mswitches].replace = (char *) 0;
6388 mswitches[n_mswitches].rep_len = 0;
6389 n_mswitches++;
6390 break;
6395 for (i = 0; i < n_mswitches; i++)
6396 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6397 return 1;
6399 return 0;
6402 static int
6403 default_arg (p, len)
6404 const char *p;
6405 int len;
6407 const char *start, *end;
6409 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6411 while (*start == ' ' || *start == '\t')
6412 start++;
6414 if (*start == '\0')
6415 break;
6417 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6420 if ((end - start) == len && strncmp (p, start, len) == 0)
6421 return 1;
6423 if (*end == '\0')
6424 break;
6427 return 0;
6430 /* Work out the subdirectory to use based on the options. The format of
6431 multilib_select is a list of elements. Each element is a subdirectory
6432 name followed by a list of options followed by a semicolon. The format
6433 of multilib_exclusions is the same, but without the preceding
6434 directory. First gcc will check the exclusions, if none of the options
6435 beginning with an exclamation point are present, and all of the other
6436 options are present, then we will ignore this completely. Passing
6437 that, gcc will consider each multilib_select in turn using the same
6438 rules for matching the options. If a match is found, that subdirectory
6439 will be used. */
6441 static void
6442 set_multilib_dir ()
6444 const char *p;
6445 unsigned int this_path_len;
6446 const char *this_path, *this_arg;
6447 int not_arg;
6448 int ok;
6450 p = multilib_exclusions;
6451 while (*p != '\0')
6453 /* Ignore newlines. */
6454 if (*p == '\n')
6456 ++p;
6457 continue;
6460 /* Check the arguments. */
6461 ok = 1;
6462 while (*p != ';')
6464 if (*p == '\0')
6465 abort ();
6467 if (! ok)
6469 ++p;
6470 continue;
6473 this_arg = p;
6474 while (*p != ' ' && *p != ';')
6476 if (*p == '\0')
6477 abort ();
6478 ++p;
6481 if (*this_arg != '!')
6482 not_arg = 0;
6483 else
6485 not_arg = 1;
6486 ++this_arg;
6489 ok = used_arg (this_arg, p - this_arg);
6490 if (not_arg)
6491 ok = ! ok;
6493 if (*p == ' ')
6494 ++p;
6497 if (ok)
6498 return;
6500 ++p;
6503 p = multilib_select;
6504 while (*p != '\0')
6506 /* Ignore newlines. */
6507 if (*p == '\n')
6509 ++p;
6510 continue;
6513 /* Get the initial path. */
6514 this_path = p;
6515 while (*p != ' ')
6517 if (*p == '\0')
6518 abort ();
6519 ++p;
6521 this_path_len = p - this_path;
6523 /* Check the arguments. */
6524 ok = 1;
6525 ++p;
6526 while (*p != ';')
6528 if (*p == '\0')
6529 abort ();
6531 if (! ok)
6533 ++p;
6534 continue;
6537 this_arg = p;
6538 while (*p != ' ' && *p != ';')
6540 if (*p == '\0')
6541 abort ();
6542 ++p;
6545 if (*this_arg != '!')
6546 not_arg = 0;
6547 else
6549 not_arg = 1;
6550 ++this_arg;
6553 /* If this is a default argument, we can just ignore it.
6554 This is true even if this_arg begins with '!'. Beginning
6555 with '!' does not mean that this argument is necessarily
6556 inappropriate for this library: it merely means that
6557 there is a more specific library which uses this
6558 argument. If this argument is a default, we need not
6559 consider that more specific library. */
6560 if (! default_arg (this_arg, p - this_arg))
6562 ok = used_arg (this_arg, p - this_arg);
6563 if (not_arg)
6564 ok = ! ok;
6567 if (*p == ' ')
6568 ++p;
6571 if (ok)
6573 if (this_path_len != 1
6574 || this_path[0] != '.')
6576 char *new_multilib_dir = xmalloc (this_path_len + 1);
6577 strncpy (new_multilib_dir, this_path, this_path_len);
6578 new_multilib_dir[this_path_len] = '\0';
6579 multilib_dir = new_multilib_dir;
6581 break;
6584 ++p;
6588 /* Print out the multiple library subdirectory selection
6589 information. This prints out a series of lines. Each line looks
6590 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6591 required. Only the desired options are printed out, the negative
6592 matches. The options are print without a leading dash. There are
6593 no spaces to make it easy to use the information in the shell.
6594 Each subdirectory is printed only once. This assumes the ordering
6595 generated by the genmultilib script. Also, we leave out ones that match
6596 the exclusions. */
6598 static void
6599 print_multilib_info ()
6601 const char *p = multilib_select;
6602 const char *last_path = 0, *this_path;
6603 int skip;
6604 unsigned int last_path_len = 0;
6606 while (*p != '\0')
6608 skip = 0;
6609 /* Ignore newlines. */
6610 if (*p == '\n')
6612 ++p;
6613 continue;
6616 /* Get the initial path. */
6617 this_path = p;
6618 while (*p != ' ')
6620 if (*p == '\0')
6621 abort ();
6622 ++p;
6625 /* Check for matches with the multilib_exclusions. We don't bother
6626 with the '!' in either list. If any of the exclusion rules match
6627 all of its options with the select rule, we skip it. */
6629 const char *e = multilib_exclusions;
6630 const char *this_arg;
6632 while (*e != '\0')
6634 int m = 1;
6635 /* Ignore newlines. */
6636 if (*e == '\n')
6638 ++e;
6639 continue;
6642 /* Check the arguments. */
6643 while (*e != ';')
6645 const char *q;
6646 int mp = 0;
6648 if (*e == '\0')
6649 abort ();
6651 if (! m)
6653 ++e;
6654 continue;
6657 this_arg = e;
6659 while (*e != ' ' && *e != ';')
6661 if (*e == '\0')
6662 abort ();
6663 ++e;
6666 q = p + 1;
6667 while (*q != ';')
6669 const char *arg;
6670 int len = e - this_arg;
6672 if (*q == '\0')
6673 abort ();
6675 arg = q;
6677 while (*q != ' ' && *q != ';')
6679 if (*q == '\0')
6680 abort ();
6681 ++q;
6684 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6685 default_arg (this_arg, e - this_arg))
6687 mp = 1;
6688 break;
6691 if (*q == ' ')
6692 ++q;
6695 if (! mp)
6696 m = 0;
6698 if (*e == ' ')
6699 ++e;
6702 if (m)
6704 skip = 1;
6705 break;
6708 if (*e != '\0')
6709 ++e;
6713 if (! skip)
6715 /* If this is a duplicate, skip it. */
6716 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6717 && ! strncmp (last_path, this_path, last_path_len));
6719 last_path = this_path;
6720 last_path_len = p - this_path;
6723 /* If this directory requires any default arguments, we can skip
6724 it. We will already have printed a directory identical to
6725 this one which does not require that default argument. */
6726 if (! skip)
6728 const char *q;
6730 q = p + 1;
6731 while (*q != ';')
6733 const char *arg;
6735 if (*q == '\0')
6736 abort ();
6738 if (*q == '!')
6739 arg = NULL;
6740 else
6741 arg = q;
6743 while (*q != ' ' && *q != ';')
6745 if (*q == '\0')
6746 abort ();
6747 ++q;
6750 if (arg != NULL
6751 && default_arg (arg, q - arg))
6753 skip = 1;
6754 break;
6757 if (*q == ' ')
6758 ++q;
6762 if (! skip)
6764 const char *p1;
6766 for (p1 = last_path; p1 < p; p1++)
6767 putchar (*p1);
6768 putchar (';');
6771 ++p;
6772 while (*p != ';')
6774 int use_arg;
6776 if (*p == '\0')
6777 abort ();
6779 if (skip)
6781 ++p;
6782 continue;
6785 use_arg = *p != '!';
6787 if (use_arg)
6788 putchar ('@');
6790 while (*p != ' ' && *p != ';')
6792 if (*p == '\0')
6793 abort ();
6794 if (use_arg)
6795 putchar (*p);
6796 ++p;
6799 if (*p == ' ')
6800 ++p;
6803 if (! skip)
6805 /* If there are extra options, print them now. */
6806 if (multilib_extra && *multilib_extra)
6808 int print_at = TRUE;
6809 const char *q;
6811 for (q = multilib_extra; *q != '\0'; q++)
6813 if (*q == ' ')
6814 print_at = TRUE;
6815 else
6817 if (print_at)
6818 putchar ('@');
6819 putchar (*q);
6820 print_at = FALSE;
6825 putchar ('\n');
6828 ++p;