* gcc_release: New script.
[official-gcc.git] / gcc / gcc.c
blobaf7e7064018b19cd213f0b9c34821fa69d32de16
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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 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 #include "config.h"
36 #include "system.h"
37 #include <signal.h>
38 #if ! defined( SIGCHLD ) && defined( SIGCLD )
39 # define SIGCHLD SIGCLD
40 #endif
41 #include "obstack.h"
42 #include "intl.h"
43 #include "prefix.h"
44 #include "gcc.h"
46 #ifdef VMS
47 #define exit __posix_exit
48 #endif
50 #ifdef HAVE_SYS_RESOURCE_H
51 #include <sys/resource.h>
52 #endif
53 #if defined (HAVE_DECL_GETRUSAGE) && !HAVE_DECL_GETRUSAGE
54 extern int getrusage PARAMS ((int, struct rusage *));
55 #endif
57 /* By default there is no special suffix for executables. */
58 #ifdef EXECUTABLE_SUFFIX
59 #define HAVE_EXECUTABLE_SUFFIX
60 #else
61 #define EXECUTABLE_SUFFIX ""
62 #endif
64 /* By default, the suffix for object files is ".o". */
65 #ifdef OBJECT_SUFFIX
66 #define HAVE_OBJECT_SUFFIX
67 #else
68 #define OBJECT_SUFFIX ".o"
69 #endif
71 #ifndef VMS
72 /* FIXME: the location independence code for VMS is hairier than this,
73 and hasn't been written. */
74 #ifndef DIR_UP
75 #define DIR_UP ".."
76 #endif /* DIR_UP */
77 #endif /* VMS */
79 static char dir_separator_str[] = { DIR_SEPARATOR, 0 };
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 #ifndef GET_ENV_PATH_LIST
85 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
86 #endif
88 /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
89 #ifndef LIBRARY_PATH_ENV
90 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
91 #endif
93 #ifndef HAVE_KILL
94 #define kill(p,s) raise(s)
95 #endif
97 /* If a stage of compilation returns an exit status >= 1,
98 compilation of that file ceases. */
100 #define MIN_FATAL_STATUS 1
102 /* Flag saying to pass the greatest exit code returned by a sub-process
103 to the calling program. */
104 static int pass_exit_codes;
106 /* Definition of string containing the arguments given to configure. */
107 #include "configargs.h"
109 /* Flag saying to print the directories gcc will search through looking for
110 programs, libraries, etc. */
112 static int print_search_dirs;
114 /* Flag saying to print the full filename of this file
115 as found through our usual search mechanism. */
117 static const char *print_file_name = NULL;
119 /* As print_file_name, but search for executable file. */
121 static const char *print_prog_name = NULL;
123 /* Flag saying to print the relative path we'd use to
124 find libgcc.a given the current compiler flags. */
126 static int print_multi_directory;
128 /* Flag saying to print the list of subdirectories and
129 compiler flags used to select them in a standard form. */
131 static int print_multi_lib;
133 /* Flag saying to print the command line options understood by gcc and its
134 sub-processes. */
136 static int print_help_list;
138 /* Flag indicating whether we should print the command and arguments */
140 static int verbose_flag;
142 /* Flag indicating to print target specific command line options. */
144 static int target_help_flag;
146 /* Flag indicating whether we should report subprocess execution times
147 (if this is supported by the system - see pexecute.c). */
149 static int report_times;
151 /* Nonzero means write "temp" files in source directory
152 and use the source file's name in them, and don't delete them. */
154 static int save_temps_flag;
156 /* The compiler version. */
158 static const char *compiler_version;
160 /* The target version specified with -V */
162 static const char *spec_version = DEFAULT_TARGET_VERSION;
164 /* The target machine specified with -b. */
166 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
168 /* Nonzero if cross-compiling.
169 When -b is used, the value comes from the `specs' file. */
171 #ifdef CROSS_COMPILE
172 static const char *cross_compile = "1";
173 #else
174 static const char *cross_compile = "0";
175 #endif
177 #ifdef MODIFY_TARGET_NAME
179 /* Information on how to alter the target name based on a command-line
180 switch. The only case we support now is simply appending or deleting a
181 string to or from the end of the first part of the configuration name. */
183 struct modify_target
185 const char *sw;
186 enum add_del {ADD, DELETE} add_del;
187 const char *str;
189 modify_target[] = MODIFY_TARGET_NAME;
190 #endif
192 /* The number of errors that have occurred; the link phase will not be
193 run if this is non-zero. */
194 static int error_count = 0;
196 /* Greatest exit code of sub-processes that has been encountered up to
197 now. */
198 static int greatest_status = 1;
200 /* This is the obstack which we use to allocate many strings. */
202 static struct obstack obstack;
204 /* This is the obstack to build an environment variable to pass to
205 collect2 that describes all of the relevant switches of what to
206 pass the compiler in building the list of pointers to constructors
207 and destructors. */
209 static struct obstack collect_obstack;
211 /* These structs are used to collect resource usage information for
212 subprocesses. */
213 #ifdef HAVE_GETRUSAGE
214 static struct rusage rus, prus;
215 #endif
217 /* Forward declaration for prototypes. */
218 struct path_prefix;
220 static void init_spec PARAMS ((void));
221 #ifndef VMS
222 static char **split_directories PARAMS ((const char *, int *));
223 static void free_split_directories PARAMS ((char **));
224 static char *make_relative_prefix PARAMS ((const char *, const char *, const char *));
225 #endif /* VMS */
226 static void store_arg PARAMS ((const char *, int, int));
227 static char *load_specs PARAMS ((const char *));
228 static void read_specs PARAMS ((const char *, int));
229 static void set_spec PARAMS ((const char *, const char *));
230 static struct compiler *lookup_compiler PARAMS ((const char *, size_t, const char *));
231 static char *build_search_list PARAMS ((struct path_prefix *, const char *, int));
232 static void putenv_from_prefixes PARAMS ((struct path_prefix *, const char *));
233 static int access_check PARAMS ((const char *, int));
234 static char *find_a_file PARAMS ((struct path_prefix *, const char *, int));
235 static void add_prefix PARAMS ((struct path_prefix *, const char *,
236 const char *, int, int, int *));
237 static void translate_options PARAMS ((int *, const char *const **));
238 static char *skip_whitespace PARAMS ((char *));
239 static void delete_if_ordinary PARAMS ((const char *));
240 static void delete_temp_files PARAMS ((void));
241 static void delete_failure_queue PARAMS ((void));
242 static void clear_failure_queue PARAMS ((void));
243 static int check_live_switch PARAMS ((int, int));
244 static const char *handle_braces PARAMS ((const char *));
245 static char *save_string PARAMS ((const char *, int));
246 static int do_spec_1 PARAMS ((const char *, int, const char *));
247 static const char *find_file PARAMS ((const char *));
248 static int is_directory PARAMS ((const char *, const char *, int));
249 static void validate_switches PARAMS ((const char *));
250 static void validate_all_switches PARAMS ((void));
251 static void give_switch PARAMS ((int, int, int));
252 static int used_arg PARAMS ((const char *, int));
253 static int default_arg PARAMS ((const char *, int));
254 static void set_multilib_dir PARAMS ((void));
255 static void print_multilib_info PARAMS ((void));
256 static void perror_with_name PARAMS ((const char *));
257 static void pfatal_pexecute PARAMS ((const char *, const char *))
258 ATTRIBUTE_NORETURN;
259 static void notice PARAMS ((const char *, ...))
260 ATTRIBUTE_PRINTF_1;
261 static void display_help PARAMS ((void));
262 static void add_preprocessor_option PARAMS ((const char *, int));
263 static void add_assembler_option PARAMS ((const char *, int));
264 static void add_linker_option PARAMS ((const char *, int));
265 static void process_command PARAMS ((int, const char *const *));
266 static int execute PARAMS ((void));
267 static void clear_args PARAMS ((void));
268 static void fatal_error PARAMS ((int));
269 static void set_input PARAMS ((const char *));
270 static void init_gcc_specs PARAMS ((struct obstack *,
271 const char *,
272 const char *));
274 /* Specs are strings containing lines, each of which (if not blank)
275 is made up of a program name, and arguments separated by spaces.
276 The program name must be exact and start from root, since no path
277 is searched and it is unreliable to depend on the current working directory.
278 Redirection of input or output is not supported; the subprograms must
279 accept filenames saying what files to read and write.
281 In addition, the specs can contain %-sequences to substitute variable text
282 or for conditional text. Here is a table of all defined %-sequences.
283 Note that spaces are not generated automatically around the results of
284 expanding these sequences; therefore, you can concatenate them together
285 or with constant text in a single argument.
287 %% substitute one % into the program name or argument.
288 %i substitute the name of the input file being processed.
289 %b substitute the basename of the input file being processed.
290 This is the substring up to (and not including) the last period
291 and not including the directory.
292 %B same as %b, but include the file suffix (text after the last period).
293 %gSUFFIX
294 substitute a file name that has suffix SUFFIX and is chosen
295 once per compilation, and mark the argument a la %d. To reduce
296 exposure to denial-of-service attacks, the file name is now
297 chosen in a way that is hard to predict even when previously
298 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
299 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
300 the regexp "[.A-Za-z]*%O"; "%O" is treated exactly as if it
301 had been pre-processed. Previously, %g was simply substituted
302 with a file name chosen once per compilation, without regard
303 to any appended suffix (which was therefore treated just like
304 ordinary text), making such attacks more likely to succeed.
305 %uSUFFIX
306 like %g, but generates a new temporary file name even if %uSUFFIX
307 was already seen.
308 %USUFFIX
309 substitutes the last file name generated with %uSUFFIX, generating a
310 new one if there is no such last file name. In the absence of any
311 %uSUFFIX, this is just like %gSUFFIX, except they don't share
312 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
313 would involve the generation of two distinct file names, one
314 for each `%g.s' and another for each `%U.s'. Previously, %U was
315 simply substituted with a file name chosen for the previous %u,
316 without regard to any appended suffix.
317 %jSUFFIX
318 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
319 writable, and if save-temps is off; otherwise, substitute the name
320 of a temporary file, just like %u. This temporary file is not
321 meant for communication between processes, but rather as a junk
322 disposal mechanism.
323 %.SUFFIX
324 substitutes .SUFFIX for the suffixes of a matched switch's args when
325 it is subsequently output with %*. SUFFIX is terminated by the next
326 space or %.
327 %d marks the argument containing or following the %d as a
328 temporary file name, so that that file will be deleted if CC exits
329 successfully. Unlike %g, this contributes no text to the argument.
330 %w marks the argument containing or following the %w as the
331 "output file" of this compilation. This puts the argument
332 into the sequence of arguments that %o will substitute later.
333 %W{...}
334 like %{...} but mark last argument supplied within
335 as a file to be deleted on failure.
336 %o substitutes the names of all the output files, with spaces
337 automatically placed around them. You should write spaces
338 around the %o as well or the results are undefined.
339 %o is for use in the specs for running the linker.
340 Input files whose names have no recognized suffix are not compiled
341 at all, but they are included among the output files, so they will
342 be linked.
343 %O substitutes the suffix for object files. Note that this is
344 handled specially when it immediately follows %g, %u, or %U
345 (with or without a suffix argument) because of the need for
346 those to form complete file names. The handling is such that
347 %O is treated exactly as if it had already been substituted,
348 except that %g, %u, and %U do not currently support additional
349 SUFFIX characters following %O as they would following, for
350 example, `.o'.
351 %p substitutes the standard macro predefinitions for the
352 current target machine. Use this when running cpp.
353 %P like %p, but puts `__' before and after the name of each macro.
354 (Except macros that already have __.)
355 This is for ANSI C.
356 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
357 %s current argument is the name of a library or startup file of some sort.
358 Search for that file in a standard list of directories
359 and substitute the full name found.
360 %eSTR Print STR as an error message. STR is terminated by a newline.
361 Use this when inconsistent options are detected.
362 %nSTR Print STR as an notice. STR is terminated by a newline.
363 %x{OPTION} Accumulate an option for %X.
364 %X Output the accumulated linker options specified by compilations.
365 %Y Output the accumulated assembler options specified by compilations.
366 %Z Output the accumulated preprocessor options specified by compilations.
367 %v1 Substitute the major version number of GCC.
368 (For version 2.5.3, this is 2.)
369 %v2 Substitute the minor version number of GCC.
370 (For version 2.5.3, this is 5.)
371 %v3 Substitute the patch level number of GCC.
372 (For version 2.5.3, this is 3.)
373 %a process ASM_SPEC as a spec.
374 This allows config.h to specify part of the spec for running as.
375 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
376 used here. This can be used to run a post-processor after the
377 assembler has done its job.
378 %D Dump out a -L option for each directory in startfile_prefixes.
379 If multilib_dir is set, extra entries are generated with it affixed.
380 %l process LINK_SPEC as a spec.
381 %L process LIB_SPEC as a spec.
382 %G process LIBGCC_SPEC as a spec.
383 %M output multilib_dir with directory separators replaced with "_";
384 if multilib_dir is not set or is ".", output "".
385 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
386 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
387 %c process SIGNED_CHAR_SPEC as a spec.
388 %C process CPP_SPEC as a spec.
389 %1 process CC1_SPEC as a spec.
390 %2 process CC1PLUS_SPEC as a spec.
391 %| output "-" if the input for the current command is coming from a pipe.
392 %* substitute the variable part of a matched option. (See below.)
393 Note that each comma in the substituted string is replaced by
394 a single space.
395 %{S} substitutes the -S switch, if that switch was given to CC.
396 If that switch was not specified, this substitutes nothing.
397 Here S is a metasyntactic variable.
398 %{S*} substitutes all the switches specified to CC whose names start
399 with -S. This is used for -o, -I, etc; switches that take
400 arguments. CC considers `-o foo' as being one switch whose
401 name starts with `o'. %{o*} would substitute this text,
402 including the space; thus, two arguments would be generated.
403 %{^S*} likewise, but don't put a blank between a switch and any args.
404 %{S*&T*} likewise, but preserve order of S and T options (the order
405 of S and T in the spec is not significant). Can be any number
406 of ampersand-separated variables; for each the wild card is
407 optional. Useful for CPP as %{D*&U*&A*}.
408 %{S*:X} substitutes X if one or more switches whose names start with -S are
409 specified to CC. Note that the tail part of the -S option
410 (i.e. the part matched by the `*') will be substituted for each
411 occurrence of %* within X.
412 %{<S} remove all occurences of -S from the command line.
413 Note - this option is position dependent. % commands in the
414 spec string before this option will see -S, % commands in the
415 spec string after this option will not.
416 %{S:X} substitutes X, but only if the -S switch was given to CC.
417 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
418 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
419 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
420 %{.S:X} substitutes X, but only if processing a file with suffix S.
421 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
422 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
423 combined with ! and . as above binding stronger than the OR.
424 %(Spec) processes a specification defined in a specs file as *Spec:
425 %[Spec] as above, but put __ around -D arguments
427 The conditional text X in a %{S:X} or %{!S:X} construct may contain
428 other nested % constructs or spaces, or even newlines. They are
429 processed as usual, as described above.
431 The -O, -f, -m, and -W switches are handled specifically in these
432 constructs. If another value of -O or the negated form of a -f, -m, or
433 -W switch is found later in the command line, the earlier switch
434 value is ignored, except with {S*} where S is just one letter; this
435 passes all matching options.
437 The character | at the beginning of the predicate text is used to indicate
438 that a command should be piped to the following command, but only if -pipe
439 is specified.
441 Note that it is built into CC which switches take arguments and which
442 do not. You might think it would be useful to generalize this to
443 allow each compiler's spec to say which switches take arguments. But
444 this cannot be done in a consistent fashion. CC cannot even decide
445 which input files have been specified without knowing which switches
446 take arguments, and it must know which input files to compile in order
447 to tell which compilers to run.
449 CC also knows implicitly that arguments starting in `-l' are to be
450 treated as compiler output files, and passed to the linker in their
451 proper position among the other output files. */
453 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
455 /* config.h can define ASM_SPEC to provide extra args to the assembler
456 or extra switch-translations. */
457 #ifndef ASM_SPEC
458 #define ASM_SPEC ""
459 #endif
461 /* config.h can define ASM_FINAL_SPEC to run a post processor after
462 the assembler has run. */
463 #ifndef ASM_FINAL_SPEC
464 #define ASM_FINAL_SPEC ""
465 #endif
467 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
468 or extra switch-translations. */
469 #ifndef CPP_SPEC
470 #define CPP_SPEC ""
471 #endif
473 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
474 or extra switch-translations. */
475 #ifndef CC1_SPEC
476 #define CC1_SPEC ""
477 #endif
479 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
480 or extra switch-translations. */
481 #ifndef CC1PLUS_SPEC
482 #define CC1PLUS_SPEC ""
483 #endif
485 /* config.h can define LINK_SPEC to provide extra args to the linker
486 or extra switch-translations. */
487 #ifndef LINK_SPEC
488 #define LINK_SPEC ""
489 #endif
491 /* config.h can define LIB_SPEC to override the default libraries. */
492 #ifndef LIB_SPEC
493 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
494 #endif
496 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
497 included. */
498 #ifndef LIBGCC_SPEC
499 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
500 /* Have gcc do the search for libgcc.a. */
501 #define LIBGCC_SPEC "libgcc.a%s"
502 #else
503 #define LIBGCC_SPEC "-lgcc"
504 #endif
505 #endif
507 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
508 #ifndef STARTFILE_SPEC
509 #define STARTFILE_SPEC \
510 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
511 #endif
513 /* config.h can define SWITCHES_NEED_SPACES to control which options
514 require spaces between the option and the argument. */
515 #ifndef SWITCHES_NEED_SPACES
516 #define SWITCHES_NEED_SPACES ""
517 #endif
519 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
520 #ifndef ENDFILE_SPEC
521 #define ENDFILE_SPEC ""
522 #endif
524 /* This spec is used for telling cpp whether char is signed or not. */
525 #ifndef SIGNED_CHAR_SPEC
526 /* Use #if rather than ?:
527 because MIPS C compiler rejects like ?: in initializers. */
528 #if DEFAULT_SIGNED_CHAR
529 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
530 #else
531 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
532 #endif
533 #endif
535 #ifndef LINKER_NAME
536 #define LINKER_NAME "collect2"
537 #endif
539 /* Here is the spec for running the linker, after compiling all files. */
541 /* -u* was put back because both BSD and SysV seem to support it. */
542 /* %{static:} simply prevents an error message if the target machine
543 doesn't handle -static. */
544 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
545 scripts which exist in user specified directories, or in standard
546 directories. */
547 #ifndef LINK_COMMAND_SPEC
548 #define LINK_COMMAND_SPEC "\
549 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
550 %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\
551 %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
552 %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
553 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
554 #endif
556 #ifndef LINK_LIBGCC_SPEC
557 # ifdef LINK_LIBGCC_SPECIAL
558 /* Don't generate -L options for startfile prefix list. */
559 # define LINK_LIBGCC_SPEC ""
560 # else
561 /* Do generate them. */
562 # define LINK_LIBGCC_SPEC "%D"
563 # endif
564 #endif
566 static const char *cpp_spec = CPP_SPEC;
567 static const char *cpp_predefines = CPP_PREDEFINES;
568 static const char *cc1_spec = CC1_SPEC;
569 static const char *cc1plus_spec = CC1PLUS_SPEC;
570 static const char *signed_char_spec = SIGNED_CHAR_SPEC;
571 static const char *asm_spec = ASM_SPEC;
572 static const char *asm_final_spec = ASM_FINAL_SPEC;
573 static const char *link_spec = LINK_SPEC;
574 static const char *lib_spec = LIB_SPEC;
575 static const char *libgcc_spec = LIBGCC_SPEC;
576 static const char *endfile_spec = ENDFILE_SPEC;
577 static const char *startfile_spec = STARTFILE_SPEC;
578 static const char *switches_need_spaces = SWITCHES_NEED_SPACES;
579 static const char *linker_name_spec = LINKER_NAME;
580 static const char *link_command_spec = LINK_COMMAND_SPEC;
581 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
583 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
584 There should be no need to override these in target dependent files,
585 but we need to copy them to the specs file so that newer versions
586 of the GCC driver can correctly drive older tool chains with the
587 appropriate -B options. */
589 static const char *trad_capable_cpp =
590 "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
592 static const char *cpp_options =
593 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
594 %{std*} %{nostdinc*}\
595 %{C} %{v} %{I*} %{P} %{$} %I\
596 %{MD:-M -MF %W{!o: %b.d}%W{o*:%.d%*}}\
597 %{MMD:-MM -MF %W{!o: %b.d}%W{o*:%.d%*}}\
598 %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*} %{M|MD|MM|MMD:%{o*:-MQ %*}}\
599 %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
600 %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
601 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
602 %{ffast-math:-D__FAST_MATH__}\
603 %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
604 %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
605 %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}}\
606 %{fshow-column} %{fno-show-column}\
607 %{fleading-underscore} %{fno-leading-underscore}\
608 %{fno-operator-names} %{ftabstop=*} %{remap}\
609 %{g3:-dD} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*&U*&A*} %{i*} %Z %i\
610 %{E:%{!M*:%W{o*}}}";
612 /* NB: This is shared amongst all front-ends. */
613 static const char *cc1_options =
614 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
615 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
616 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*} %{ansi}\
617 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
618 %{aux-info*} %{Qn:-fno-ident} %{--help:--help}\
619 %{--target-help:--target-help}\
620 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
621 %{fsyntax-only:-o %j} %{-param*}";
623 static const char *asm_options =
624 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
626 static const char *invoke_as =
627 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
629 /* Some compilers have limits on line lengths, and the multilib_select
630 and/or multilib_matches strings can be very long, so we build them at
631 run time. */
632 static struct obstack multilib_obstack;
633 static const char *multilib_select;
634 static const char *multilib_matches;
635 static const char *multilib_defaults;
636 static const char *multilib_exclusions;
637 #include "multilib.h"
639 /* Check whether a particular argument is a default argument. */
641 #ifndef MULTILIB_DEFAULTS
642 #define MULTILIB_DEFAULTS { "" }
643 #endif
645 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
647 struct user_specs
649 struct user_specs *next;
650 const char *filename;
653 static struct user_specs *user_specs_head, *user_specs_tail;
655 /* This defines which switch letters take arguments. */
657 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
658 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
659 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
660 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
661 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
662 || (CHAR) == 'B' || (CHAR) == 'b')
664 #ifndef SWITCH_TAKES_ARG
665 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
666 #endif
668 /* This defines which multi-letter switches take arguments. */
670 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
671 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
672 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
673 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
674 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
675 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
676 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
677 || !strcmp (STR, "specs") \
678 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
680 #ifndef WORD_SWITCH_TAKES_ARG
681 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
682 #endif
684 #ifdef HAVE_EXECUTABLE_SUFFIX
685 /* This defines which switches stop a full compilation. */
686 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
687 ((CHAR) == 'c' || (CHAR) == 'S')
689 #ifndef SWITCH_CURTAILS_COMPILATION
690 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
691 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
692 #endif
693 #endif
695 /* Record the mapping from file suffixes for compilation specs. */
697 struct compiler
699 const char *suffix; /* Use this compiler for input files
700 whose names end in this suffix. */
702 const char *spec; /* To use this compiler, run this spec. */
704 const char *cpp_spec; /* If non-NULL, substitute this spec
705 for `%C', rather than the usual
706 cpp_spec. */
709 /* Pointer to a vector of `struct compiler' that gives the spec for
710 compiling a file, based on its suffix.
711 A file that does not end in any of these suffixes will be passed
712 unchanged to the loader and nothing else will be done to it.
714 An entry containing two 0s is used to terminate the vector.
716 If multiple entries match a file, the last matching one is used. */
718 static struct compiler *compilers;
720 /* Number of entries in `compilers', not counting the null terminator. */
722 static int n_compilers;
724 /* The default list of file name suffixes and their compilation specs. */
726 static struct compiler default_compilers[] =
728 /* Add lists of suffixes of known languages here. If those languages
729 were not present when we built the driver, we will hit these copies
730 and be given a more meaningful error than "file not used since
731 linking is not done". */
732 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
733 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
734 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
735 {".ii", "#C++", 0},
736 {".ads", "#Ada", 0}, {".adb", "#Ada", 0}, {".ada", "#Ada", 0},
737 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
738 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
739 {".r", "#Ratfor", 0},
740 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
741 {".ch", "#Chill", 0}, {".chi", "#Chill", 0},
742 {".java", "#Java", 0}, {".class", "#Java", 0},
743 {".zip", "#Java", 0}, {".jar", "#Java", 0},
744 /* Next come the entries for C. */
745 {".c", "@c", 0},
746 {"@c",
747 /* cc1 has an integrated ISO C preprocessor. We should invoke the
748 external preprocessor if -save-temps or -traditional is given. */
749 "%{E|M|MM:%(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)}\
750 %{!E:%{!M:%{!MM:\
751 %{save-temps:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
752 %(cpp_options) %b.i \n\
753 cc1 -fpreprocessed %b.i %(cc1_options)}\
754 %{!save-temps:\
755 %{traditional|ftraditional|traditional-cpp:\
756 tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
757 cc1 -fpreprocessed %{!pipe:%g.i} %(cc1_options)}\
758 %{!traditional:%{!ftraditional:%{!traditional-cpp:\
759 cc1 -lang-c %{ansi:-std=c89} %(cpp_options) %(cc1_options)}}}}\
760 %{!fsyntax-only:%(invoke_as)}}}}", 0},
761 {"-",
762 "%{!E:%e-E required when input is from standard input}\
763 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
764 {".h", "@c-header", 0},
765 {"@c-header",
766 "%{!E:%eCompilation of header file requested} \
767 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
768 {".i", "@cpp-output", 0},
769 {"@cpp-output",
770 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
771 {".s", "@assembler", 0},
772 {"@assembler",
773 "%{!M:%{!MM:%{!E:%{!S:as %(asm_options) %i %A }}}}", 0},
774 {".S", "@assembler-with-cpp", 0},
775 {"@assembler-with-cpp",
776 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
777 %{!M:%{!MM:%{!E:%(invoke_as)}}}", 0},
778 #include "specs.h"
779 /* Mark end of table */
780 {0, 0, 0}
783 /* Number of elements in default_compilers, not counting the terminator. */
785 static int n_default_compilers
786 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
788 /* A vector of options to give to the linker.
789 These options are accumulated by %x,
790 and substituted into the linker command with %X. */
791 static int n_linker_options;
792 static char **linker_options;
794 /* A vector of options to give to the assembler.
795 These options are accumulated by -Wa,
796 and substituted into the assembler command with %Y. */
797 static int n_assembler_options;
798 static char **assembler_options;
800 /* A vector of options to give to the preprocessor.
801 These options are accumulated by -Wp,
802 and substituted into the preprocessor command with %Z. */
803 static int n_preprocessor_options;
804 static char **preprocessor_options;
806 /* Define how to map long options into short ones. */
808 /* This structure describes one mapping. */
809 struct option_map
811 /* The long option's name. */
812 const char *name;
813 /* The equivalent short option. */
814 const char *equivalent;
815 /* Argument info. A string of flag chars; NULL equals no options.
816 a => argument required.
817 o => argument optional.
818 j => join argument to equivalent, making one word.
819 * => require other text after NAME as an argument. */
820 const char *arg_info;
823 /* This is the table of mappings. Mappings are tried sequentially
824 for each option encountered; the first one that matches, wins. */
826 struct option_map option_map[] =
828 {"--all-warnings", "-Wall", 0},
829 {"--ansi", "-ansi", 0},
830 {"--assemble", "-S", 0},
831 {"--assert", "-A", "a"},
832 {"--classpath", "-fclasspath=", "aj"},
833 {"--CLASSPATH", "-fCLASSPATH=", "aj"},
834 {"--comments", "-C", 0},
835 {"--compile", "-c", 0},
836 {"--debug", "-g", "oj"},
837 {"--define-macro", "-D", "aj"},
838 {"--dependencies", "-M", 0},
839 {"--dump", "-d", "a"},
840 {"--dumpbase", "-dumpbase", "a"},
841 {"--entry", "-e", 0},
842 {"--extra-warnings", "-W", 0},
843 {"--for-assembler", "-Wa", "a"},
844 {"--for-linker", "-Xlinker", "a"},
845 {"--force-link", "-u", "a"},
846 {"--imacros", "-imacros", "a"},
847 {"--include", "-include", "a"},
848 {"--include-barrier", "-I-", 0},
849 {"--include-directory", "-I", "aj"},
850 {"--include-directory-after", "-idirafter", "a"},
851 {"--include-prefix", "-iprefix", "a"},
852 {"--include-with-prefix", "-iwithprefix", "a"},
853 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
854 {"--include-with-prefix-after", "-iwithprefix", "a"},
855 {"--language", "-x", "a"},
856 {"--library-directory", "-L", "a"},
857 {"--machine", "-m", "aj"},
858 {"--machine-", "-m", "*j"},
859 {"--no-line-commands", "-P", 0},
860 {"--no-precompiled-includes", "-noprecomp", 0},
861 {"--no-standard-includes", "-nostdinc", 0},
862 {"--no-standard-libraries", "-nostdlib", 0},
863 {"--no-warnings", "-w", 0},
864 {"--optimize", "-O", "oj"},
865 {"--output", "-o", "a"},
866 {"--output-class-directory", "-foutput-class-dir=", "ja"},
867 {"--param", "--param", "a"},
868 {"--pedantic", "-pedantic", 0},
869 {"--pedantic-errors", "-pedantic-errors", 0},
870 {"--pipe", "-pipe", 0},
871 {"--prefix", "-B", "a"},
872 {"--preprocess", "-E", 0},
873 {"--print-search-dirs", "-print-search-dirs", 0},
874 {"--print-file-name", "-print-file-name=", "aj"},
875 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
876 {"--print-missing-file-dependencies", "-MG", 0},
877 {"--print-multi-lib", "-print-multi-lib", 0},
878 {"--print-multi-directory", "-print-multi-directory", 0},
879 {"--print-prog-name", "-print-prog-name=", "aj"},
880 {"--profile", "-p", 0},
881 {"--profile-blocks", "-a", 0},
882 {"--quiet", "-q", 0},
883 {"--save-temps", "-save-temps", 0},
884 {"--shared", "-shared", 0},
885 {"--silent", "-q", 0},
886 {"--specs", "-specs=", "aj"},
887 {"--static", "-static", 0},
888 {"--std", "-std=", "aj"},
889 {"--symbolic", "-symbolic", 0},
890 {"--target", "-b", "a"},
891 {"--time", "-time", 0},
892 {"--trace-includes", "-H", 0},
893 {"--traditional", "-traditional", 0},
894 {"--traditional-cpp", "-traditional-cpp", 0},
895 {"--trigraphs", "-trigraphs", 0},
896 {"--undefine-macro", "-U", "aj"},
897 {"--use-version", "-V", "a"},
898 {"--user-dependencies", "-MM", 0},
899 {"--verbose", "-v", 0},
900 {"--version", "-dumpversion", 0},
901 {"--warn-", "-W", "*j"},
902 {"--write-dependencies", "-MD", 0},
903 {"--write-user-dependencies", "-MMD", 0},
904 {"--", "-f", "*j"}
907 /* Translate the options described by *ARGCP and *ARGVP.
908 Make a new vector and store it back in *ARGVP,
909 and store its length in *ARGVC. */
911 static void
912 translate_options (argcp, argvp)
913 int *argcp;
914 const char *const **argvp;
916 int i;
917 int argc = *argcp;
918 const char *const *argv = *argvp;
919 const char **newv =
920 (const char **) xmalloc ((argc + 2) * 2 * sizeof (const char *));
921 int newindex = 0;
923 i = 0;
924 newv[newindex++] = argv[i++];
926 while (i < argc)
928 /* Translate -- options. */
929 if (argv[i][0] == '-' && argv[i][1] == '-')
931 size_t j;
932 /* Find a mapping that applies to this option. */
933 for (j = 0; j < ARRAY_SIZE (option_map); j++)
935 size_t optlen = strlen (option_map[j].name);
936 size_t arglen = strlen (argv[i]);
937 size_t complen = arglen > optlen ? optlen : arglen;
938 const char *arginfo = option_map[j].arg_info;
940 if (arginfo == 0)
941 arginfo = "";
943 if (!strncmp (argv[i], option_map[j].name, complen))
945 const char *arg = 0;
947 if (arglen < optlen)
949 size_t k;
950 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
951 if (strlen (option_map[k].name) >= arglen
952 && !strncmp (argv[i], option_map[k].name, arglen))
954 error ("Ambiguous abbreviation %s", argv[i]);
955 break;
958 if (k != ARRAY_SIZE (option_map))
959 break;
962 if (arglen > optlen)
964 /* If the option has an argument, accept that. */
965 if (argv[i][optlen] == '=')
966 arg = argv[i] + optlen + 1;
968 /* If this mapping requires extra text at end of name,
969 accept that as "argument". */
970 else if (strchr (arginfo, '*') != 0)
971 arg = argv[i] + optlen;
973 /* Otherwise, extra text at end means mismatch.
974 Try other mappings. */
975 else
976 continue;
979 else if (strchr (arginfo, '*') != 0)
981 error ("Incomplete `%s' option", option_map[j].name);
982 break;
985 /* Handle arguments. */
986 if (strchr (arginfo, 'a') != 0)
988 if (arg == 0)
990 if (i + 1 == argc)
992 error ("Missing argument to `%s' option",
993 option_map[j].name);
994 break;
997 arg = argv[++i];
1000 else if (strchr (arginfo, '*') != 0)
1002 else if (strchr (arginfo, 'o') == 0)
1004 if (arg != 0)
1005 error ("Extraneous argument to `%s' option",
1006 option_map[j].name);
1007 arg = 0;
1010 /* Store the translation as one argv elt or as two. */
1011 if (arg != 0 && strchr (arginfo, 'j') != 0)
1012 newv[newindex++] = concat (option_map[j].equivalent, arg,
1013 NULL_PTR);
1014 else if (arg != 0)
1016 newv[newindex++] = option_map[j].equivalent;
1017 newv[newindex++] = arg;
1019 else
1020 newv[newindex++] = option_map[j].equivalent;
1022 break;
1025 i++;
1028 /* Handle old-fashioned options--just copy them through,
1029 with their arguments. */
1030 else if (argv[i][0] == '-')
1032 const char *p = argv[i] + 1;
1033 int c = *p;
1034 int nskip = 1;
1036 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1037 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1038 else if (WORD_SWITCH_TAKES_ARG (p))
1039 nskip += WORD_SWITCH_TAKES_ARG (p);
1040 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1041 && p[1] == 0)
1042 nskip += 1;
1043 else if (! strcmp (p, "Xlinker"))
1044 nskip += 1;
1046 /* Watch out for an option at the end of the command line that
1047 is missing arguments, and avoid skipping past the end of the
1048 command line. */
1049 if (nskip + i > argc)
1050 nskip = argc - i;
1052 while (nskip > 0)
1054 newv[newindex++] = argv[i++];
1055 nskip--;
1058 else
1059 /* Ordinary operands, or +e options. */
1060 newv[newindex++] = argv[i++];
1063 newv[newindex] = 0;
1065 *argvp = newv;
1066 *argcp = newindex;
1069 static char *
1070 skip_whitespace (p)
1071 char *p;
1073 while (1)
1075 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1076 be considered whitespace. */
1077 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1078 return p + 1;
1079 else if (*p == '\n' || *p == ' ' || *p == '\t')
1080 p++;
1081 else if (*p == '#')
1083 while (*p != '\n')
1084 p++;
1085 p++;
1087 else
1088 break;
1091 return p;
1093 /* Structures to keep track of prefixes to try when looking for files. */
1095 struct prefix_list
1097 char *prefix; /* String to prepend to the path. */
1098 struct prefix_list *next; /* Next in linked list. */
1099 int require_machine_suffix; /* Don't use without machine_suffix. */
1100 /* 2 means try both machine_suffix and just_machine_suffix. */
1101 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1102 int priority; /* Sort key - priority within list */
1105 struct path_prefix
1107 struct prefix_list *plist; /* List of prefixes to try */
1108 int max_len; /* Max length of a prefix in PLIST */
1109 const char *name; /* Name of this list (used in config stuff) */
1112 /* List of prefixes to try when looking for executables. */
1114 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1116 /* List of prefixes to try when looking for startup (crt0) files. */
1118 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1120 /* List of prefixes to try when looking for include files. */
1122 static struct path_prefix include_prefixes = { 0, 0, "include" };
1124 /* Suffix to attach to directories searched for commands.
1125 This looks like `MACHINE/VERSION/'. */
1127 static const char *machine_suffix = 0;
1129 /* Suffix to attach to directories searched for commands.
1130 This is just `MACHINE/'. */
1132 static const char *just_machine_suffix = 0;
1134 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1136 static const char *gcc_exec_prefix;
1138 /* Default prefixes to attach to command names. */
1140 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1141 #undef MD_EXEC_PREFIX
1142 #undef MD_STARTFILE_PREFIX
1143 #undef MD_STARTFILE_PREFIX_1
1144 #endif
1146 /* If no prefixes defined, use the null string, which will disable them. */
1147 #ifndef MD_EXEC_PREFIX
1148 #define MD_EXEC_PREFIX ""
1149 #endif
1150 #ifndef MD_STARTFILE_PREFIX
1151 #define MD_STARTFILE_PREFIX ""
1152 #endif
1153 #ifndef MD_STARTFILE_PREFIX_1
1154 #define MD_STARTFILE_PREFIX_1 ""
1155 #endif
1157 /* Supply defaults for the standard prefixes. */
1159 #ifndef STANDARD_EXEC_PREFIX
1160 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1161 #endif
1162 #ifndef STANDARD_STARTFILE_PREFIX
1163 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1164 #endif
1165 #ifndef TOOLDIR_BASE_PREFIX
1166 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1167 #endif
1168 #ifndef STANDARD_BINDIR_PREFIX
1169 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1170 #endif
1172 static const char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1173 static const char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1174 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1176 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1177 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1178 static const char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1179 static const char *standard_startfile_prefix_1 = "/lib/";
1180 static const char *standard_startfile_prefix_2 = "/usr/lib/";
1182 static const char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1183 static const char *tooldir_prefix;
1185 static const char *standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1187 /* Subdirectory to use for locating libraries. Set by
1188 set_multilib_dir based on the compilation options. */
1190 static const char *multilib_dir;
1192 /* Structure to keep track of the specs that have been defined so far.
1193 These are accessed using %(specname) or %[specname] in a compiler
1194 or link spec. */
1196 struct spec_list
1198 /* The following 2 fields must be first */
1199 /* to allow EXTRA_SPECS to be initialized */
1200 const char *name; /* name of the spec. */
1201 const char *ptr; /* available ptr if no static pointer */
1203 /* The following fields are not initialized */
1204 /* by EXTRA_SPECS */
1205 const char **ptr_spec; /* pointer to the spec itself. */
1206 struct spec_list *next; /* Next spec in linked list. */
1207 int name_len; /* length of the name */
1208 int alloc_p; /* whether string was allocated */
1211 #define INIT_STATIC_SPEC(NAME,PTR) \
1212 { NAME, NULL_PTR, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1214 /* List of statically defined specs. */
1215 static struct spec_list static_specs[] =
1217 INIT_STATIC_SPEC ("asm", &asm_spec),
1218 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1219 INIT_STATIC_SPEC ("asm_options", &asm_options),
1220 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1221 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1222 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1223 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1224 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1225 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1226 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1227 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1228 INIT_STATIC_SPEC ("link", &link_spec),
1229 INIT_STATIC_SPEC ("lib", &lib_spec),
1230 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1231 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1232 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1233 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1234 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1235 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1236 INIT_STATIC_SPEC ("version", &compiler_version),
1237 INIT_STATIC_SPEC ("multilib", &multilib_select),
1238 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1239 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1240 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1241 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1242 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1243 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1244 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1245 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1246 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1249 #ifdef EXTRA_SPECS /* additional specs needed */
1250 /* Structure to keep track of just the first two args of a spec_list.
1251 That is all that the EXTRA_SPECS macro gives us. */
1252 struct spec_list_1
1254 const char *name;
1255 const char *ptr;
1258 static struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1259 static struct spec_list *extra_specs = (struct spec_list *) 0;
1260 #endif
1262 /* List of dynamically allocates specs that have been defined so far. */
1264 static struct spec_list *specs = (struct spec_list *) 0;
1266 /* Add appropriate libgcc specs to OBSTACK, taking into account
1267 various permutations of -shared-libgcc, -shared, and such. */
1269 static void
1270 init_gcc_specs (obstack, shared_name, static_name)
1271 struct obstack *obstack;
1272 const char *shared_name;
1273 const char *static_name;
1275 char buffer[128];
1277 /* If we see -shared-libgcc, then use the shared version. */
1278 sprintf (buffer, "%%{shared-libgcc:%s}", shared_name);
1279 obstack_grow (obstack, buffer, strlen (buffer));
1280 /* If we see -static-libgcc, then use the static version. */
1281 sprintf (buffer, "%%{static-libgcc:%s}", static_name);
1282 obstack_grow (obstack, buffer, strlen (buffer));
1283 /* Otherwise, if we see -shared, then use the shared version. */
1284 sprintf (buffer,
1285 "%%{!shared-libgcc:%%{!static-libgcc:%%{shared:%s}}}",
1286 shared_name);
1287 obstack_grow (obstack, buffer, strlen (buffer));
1288 /* Otherwise, use the static version. */
1289 sprintf (buffer,
1290 "%%{!shared-libgcc:%%{!static-libgcc:%%{!shared:%s}}}",
1291 static_name);
1292 obstack_grow (obstack, buffer, strlen (buffer));
1295 /* Initialize the specs lookup routines. */
1297 static void
1298 init_spec ()
1300 struct spec_list *next = (struct spec_list *) 0;
1301 struct spec_list *sl = (struct spec_list *) 0;
1302 int i;
1304 if (specs)
1305 return; /* Already initialized. */
1307 if (verbose_flag)
1308 notice ("Using builtin specs.\n");
1310 #ifdef EXTRA_SPECS
1311 extra_specs = (struct spec_list *)
1312 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1314 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1316 sl = &extra_specs[i];
1317 sl->name = extra_specs_1[i].name;
1318 sl->ptr = extra_specs_1[i].ptr;
1319 sl->next = next;
1320 sl->name_len = strlen (sl->name);
1321 sl->ptr_spec = &sl->ptr;
1322 next = sl;
1324 #endif
1326 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1328 sl = &static_specs[i];
1329 sl->next = next;
1330 next = sl;
1333 #ifdef ENABLE_SHARED_LIBGCC
1334 /* ??? If neither -shared-libgcc nor --static-libgcc was
1335 seen, then we should be making an educated guess. Some proposed
1336 heuristics for ELF include:
1338 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1339 program will be doing dynamic loading, which will likely
1340 need the shared libgcc.
1342 (2) If "-ldl", then it's also a fair bet that we're doing
1343 dynamic loading.
1345 (3) For each ET_DYN we're linking against (either through -lfoo
1346 or /some/path/foo.so), check to see whether it or one of
1347 its dependancies depends on a shared libgcc.
1349 (4) If "-shared"
1351 If the runtime is fixed to look for program headers instead
1352 of calling __register_frame_info at all, for each object,
1353 use the shared libgcc if any EH symbol referenced.
1355 If crtstuff is fixed to not invoke __register_frame_info
1356 automatically, for each object, use the shared libgcc if
1357 any non-empty unwind section found.
1359 Doing any of this probably requires invoking an external program to
1360 do the actual object file scanning. */
1362 const char *p = libgcc_spec;
1363 int in_sep = 1;
1365 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1366 when given the proper command line arguments. */
1367 while (*p)
1369 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1371 init_gcc_specs (&obstack,
1372 #ifdef NO_SHARED_LIBGCC_MULTILIB
1373 "-lgcc_s"
1374 #else
1375 "-lgcc_s%M"
1376 #endif
1378 "-lgcc");
1379 p += 5;
1380 in_sep = 0;
1382 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1384 /* Ug. We don't know shared library extensions. Hope that
1385 systems that use this form don't do shared libraries. */
1386 init_gcc_specs (&obstack,
1387 #ifdef NO_SHARED_LIBGCC_MULTILIB
1388 "-lgcc_s"
1389 #else
1390 "-lgcc_s%M"
1391 #endif
1393 "libgcc.a%s");
1394 p += 10;
1395 in_sep = 0;
1397 else
1399 obstack_1grow (&obstack, *p);
1400 in_sep = (*p == ' ');
1401 p += 1;
1405 obstack_1grow (&obstack, '\0');
1406 libgcc_spec = obstack_finish (&obstack);
1408 #endif
1409 #ifdef USE_AS_TRADITIONAL_FORMAT
1410 /* Prepend "--traditional-format" to whatever asm_spec we had before. */
1412 static char tf[] = "--traditional-format ";
1413 obstack_grow (&obstack, tf, sizeof(tf) - 1);
1414 obstack_grow0 (&obstack, asm_spec, strlen (asm_spec));
1415 asm_spec = obstack_finish (&obstack);
1417 #endif
1419 specs = sl;
1422 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1423 removed; If the spec starts with a + then SPEC is added to the end of the
1424 current spec. */
1426 static void
1427 set_spec (name, spec)
1428 const char *name;
1429 const char *spec;
1431 struct spec_list *sl;
1432 const char *old_spec;
1433 int name_len = strlen (name);
1434 int i;
1436 /* If this is the first call, initialize the statically allocated specs. */
1437 if (!specs)
1439 struct spec_list *next = (struct spec_list *) 0;
1440 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1442 sl = &static_specs[i];
1443 sl->next = next;
1444 next = sl;
1446 specs = sl;
1449 /* See if the spec already exists. */
1450 for (sl = specs; sl; sl = sl->next)
1451 if (name_len == sl->name_len && !strcmp (sl->name, name))
1452 break;
1454 if (!sl)
1456 /* Not found - make it. */
1457 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1458 sl->name = xstrdup (name);
1459 sl->name_len = name_len;
1460 sl->ptr_spec = &sl->ptr;
1461 sl->alloc_p = 0;
1462 *(sl->ptr_spec) = "";
1463 sl->next = specs;
1464 specs = sl;
1467 old_spec = *(sl->ptr_spec);
1468 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1469 ? concat (old_spec, spec + 1, NULL_PTR)
1470 : xstrdup (spec));
1472 #ifdef DEBUG_SPECS
1473 if (verbose_flag)
1474 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1475 #endif
1477 /* Free the old spec. */
1478 if (old_spec && sl->alloc_p)
1479 free ((PTR) old_spec);
1481 sl->alloc_p = 1;
1484 /* Accumulate a command (program name and args), and run it. */
1486 /* Vector of pointers to arguments in the current line of specifications. */
1488 static const char **argbuf;
1490 /* Number of elements allocated in argbuf. */
1492 static int argbuf_length;
1494 /* Number of elements in argbuf currently in use (containing args). */
1496 static int argbuf_index;
1498 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1499 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1500 it here. */
1502 static struct temp_name {
1503 const char *suffix; /* suffix associated with the code. */
1504 int length; /* strlen (suffix). */
1505 int unique; /* Indicates whether %g or %u/%U was used. */
1506 const char *filename; /* associated filename. */
1507 int filename_length; /* strlen (filename). */
1508 struct temp_name *next;
1509 } *temp_names;
1511 /* Number of commands executed so far. */
1513 static int execution_count;
1515 /* Number of commands that exited with a signal. */
1517 static int signal_count;
1519 /* Name with which this program was invoked. */
1521 static const char *programname;
1523 /* Clear out the vector of arguments (after a command is executed). */
1525 static void
1526 clear_args ()
1528 argbuf_index = 0;
1531 /* Add one argument to the vector at the end.
1532 This is done when a space is seen or at the end of the line.
1533 If DELETE_ALWAYS is nonzero, the arg is a filename
1534 and the file should be deleted eventually.
1535 If DELETE_FAILURE is nonzero, the arg is a filename
1536 and the file should be deleted if this compilation fails. */
1538 static void
1539 store_arg (arg, delete_always, delete_failure)
1540 const char *arg;
1541 int delete_always, delete_failure;
1543 if (argbuf_index + 1 == argbuf_length)
1544 argbuf
1545 = (const char **) xrealloc (argbuf,
1546 (argbuf_length *= 2) * sizeof (const char *));
1548 argbuf[argbuf_index++] = arg;
1549 argbuf[argbuf_index] = 0;
1551 if (delete_always || delete_failure)
1552 record_temp_file (arg, delete_always, delete_failure);
1555 /* Load specs from a file name named FILENAME, replacing occurances of
1556 various different types of line-endings, \r\n, \n\r and just \r, with
1557 a single \n. */
1559 static char *
1560 load_specs (filename)
1561 const char *filename;
1563 int desc;
1564 int readlen;
1565 struct stat statbuf;
1566 char *buffer;
1567 char *buffer_p;
1568 char *specs;
1569 char *specs_p;
1571 if (verbose_flag)
1572 notice ("Reading specs from %s\n", filename);
1574 /* Open and stat the file. */
1575 desc = open (filename, O_RDONLY, 0);
1576 if (desc < 0)
1577 pfatal_with_name (filename);
1578 if (stat (filename, &statbuf) < 0)
1579 pfatal_with_name (filename);
1581 /* Read contents of file into BUFFER. */
1582 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1583 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1584 if (readlen < 0)
1585 pfatal_with_name (filename);
1586 buffer[readlen] = 0;
1587 close (desc);
1589 specs = xmalloc (readlen + 1);
1590 specs_p = specs;
1591 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1593 int skip = 0;
1594 char c = *buffer_p;
1595 if (c == '\r')
1597 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1598 skip = 1;
1599 else if (*(buffer_p + 1) == '\n') /* \r\n */
1600 skip = 1;
1601 else /* \r */
1602 c = '\n';
1604 if (! skip)
1605 *specs_p++ = c;
1607 *specs_p = '\0';
1609 free (buffer);
1610 return (specs);
1613 /* Read compilation specs from a file named FILENAME,
1614 replacing the default ones.
1616 A suffix which starts with `*' is a definition for
1617 one of the machine-specific sub-specs. The "suffix" should be
1618 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1619 The corresponding spec is stored in asm_spec, etc.,
1620 rather than in the `compilers' vector.
1622 Anything invalid in the file is a fatal error. */
1624 static void
1625 read_specs (filename, main_p)
1626 const char *filename;
1627 int main_p;
1629 char *buffer;
1630 register char *p;
1632 buffer = load_specs (filename);
1634 /* Scan BUFFER for specs, putting them in the vector. */
1635 p = buffer;
1636 while (1)
1638 char *suffix;
1639 char *spec;
1640 char *in, *out, *p1, *p2, *p3;
1642 /* Advance P in BUFFER to the next nonblank nocomment line. */
1643 p = skip_whitespace (p);
1644 if (*p == 0)
1645 break;
1647 /* Is this a special command that starts with '%'? */
1648 /* Don't allow this for the main specs file, since it would
1649 encourage people to overwrite it. */
1650 if (*p == '%' && !main_p)
1652 p1 = p;
1653 while (*p && *p != '\n')
1654 p++;
1656 /* Skip '\n'. */
1657 p++;
1659 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1660 && (p1[sizeof "%include" - 1] == ' '
1661 || p1[sizeof "%include" - 1] == '\t'))
1663 char *new_filename;
1665 p1 += sizeof ("%include");
1666 while (*p1 == ' ' || *p1 == '\t')
1667 p1++;
1669 if (*p1++ != '<' || p[-2] != '>')
1670 fatal ("specs %%include syntax malformed after %ld characters",
1671 (long) (p1 - buffer + 1));
1673 p[-2] = '\0';
1674 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1675 read_specs (new_filename ? new_filename : p1, FALSE);
1676 continue;
1678 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1679 && (p1[sizeof "%include_noerr" - 1] == ' '
1680 || p1[sizeof "%include_noerr" - 1] == '\t'))
1682 char *new_filename;
1684 p1 += sizeof "%include_noerr";
1685 while (*p1 == ' ' || *p1 == '\t')
1686 p1++;
1688 if (*p1++ != '<' || p[-2] != '>')
1689 fatal ("specs %%include syntax malformed after %ld characters",
1690 (long) (p1 - buffer + 1));
1692 p[-2] = '\0';
1693 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1694 if (new_filename)
1695 read_specs (new_filename, FALSE);
1696 else if (verbose_flag)
1697 notice ("Could not find specs file %s\n", p1);
1698 continue;
1700 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1701 && (p1[sizeof "%rename" - 1] == ' '
1702 || p1[sizeof "%rename" - 1] == '\t'))
1704 int name_len;
1705 struct spec_list *sl;
1707 /* Get original name */
1708 p1 += sizeof "%rename";
1709 while (*p1 == ' ' || *p1 == '\t')
1710 p1++;
1712 if (! ISALPHA ((unsigned char) *p1))
1713 fatal ("specs %%rename syntax malformed after %ld characters",
1714 (long) (p1 - buffer));
1716 p2 = p1;
1717 while (*p2 && !ISSPACE ((unsigned char) *p2))
1718 p2++;
1720 if (*p2 != ' ' && *p2 != '\t')
1721 fatal ("specs %%rename syntax malformed after %ld characters",
1722 (long) (p2 - buffer));
1724 name_len = p2 - p1;
1725 *p2++ = '\0';
1726 while (*p2 == ' ' || *p2 == '\t')
1727 p2++;
1729 if (! ISALPHA ((unsigned char) *p2))
1730 fatal ("specs %%rename syntax malformed after %ld characters",
1731 (long) (p2 - buffer));
1733 /* Get new spec name. */
1734 p3 = p2;
1735 while (*p3 && !ISSPACE ((unsigned char) *p3))
1736 p3++;
1738 if (p3 != p - 1)
1739 fatal ("specs %%rename syntax malformed after %ld characters",
1740 (long) (p3 - buffer));
1741 *p3 = '\0';
1743 for (sl = specs; sl; sl = sl->next)
1744 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1745 break;
1747 if (!sl)
1748 fatal ("specs %s spec was not found to be renamed", p1);
1750 if (strcmp (p1, p2) == 0)
1751 continue;
1753 if (verbose_flag)
1755 notice ("rename spec %s to %s\n", p1, p2);
1756 #ifdef DEBUG_SPECS
1757 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1758 #endif
1761 set_spec (p2, *(sl->ptr_spec));
1762 if (sl->alloc_p)
1763 free ((PTR) *(sl->ptr_spec));
1765 *(sl->ptr_spec) = "";
1766 sl->alloc_p = 0;
1767 continue;
1769 else
1770 fatal ("specs unknown %% command after %ld characters",
1771 (long) (p1 - buffer));
1774 /* Find the colon that should end the suffix. */
1775 p1 = p;
1776 while (*p1 && *p1 != ':' && *p1 != '\n')
1777 p1++;
1779 /* The colon shouldn't be missing. */
1780 if (*p1 != ':')
1781 fatal ("specs file malformed after %ld characters",
1782 (long) (p1 - buffer));
1784 /* Skip back over trailing whitespace. */
1785 p2 = p1;
1786 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1787 p2--;
1789 /* Copy the suffix to a string. */
1790 suffix = save_string (p, p2 - p);
1791 /* Find the next line. */
1792 p = skip_whitespace (p1 + 1);
1793 if (p[1] == 0)
1794 fatal ("specs file malformed after %ld characters",
1795 (long) (p - buffer));
1797 p1 = p;
1798 /* Find next blank line or end of string. */
1799 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1800 p1++;
1802 /* Specs end at the blank line and do not include the newline. */
1803 spec = save_string (p, p1 - p);
1804 p = p1;
1806 /* Delete backslash-newline sequences from the spec. */
1807 in = spec;
1808 out = spec;
1809 while (*in != 0)
1811 if (in[0] == '\\' && in[1] == '\n')
1812 in += 2;
1813 else if (in[0] == '#')
1814 while (*in && *in != '\n')
1815 in++;
1817 else
1818 *out++ = *in++;
1820 *out = 0;
1822 if (suffix[0] == '*')
1824 if (! strcmp (suffix, "*link_command"))
1825 link_command_spec = spec;
1826 else
1827 set_spec (suffix + 1, spec);
1829 else
1831 /* Add this pair to the vector. */
1832 compilers
1833 = ((struct compiler *)
1834 xrealloc (compilers,
1835 (n_compilers + 2) * sizeof (struct compiler)));
1837 compilers[n_compilers].suffix = suffix;
1838 compilers[n_compilers].spec = spec;
1839 n_compilers++;
1840 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
1843 if (*suffix == 0)
1844 link_command_spec = spec;
1847 if (link_command_spec == 0)
1848 fatal ("spec file has no spec for linking");
1851 /* Record the names of temporary files we tell compilers to write,
1852 and delete them at the end of the run. */
1854 /* This is the common prefix we use to make temp file names.
1855 It is chosen once for each run of this program.
1856 It is substituted into a spec by %g or %j.
1857 Thus, all temp file names contain this prefix.
1858 In practice, all temp file names start with this prefix.
1860 This prefix comes from the envvar TMPDIR if it is defined;
1861 otherwise, from the P_tmpdir macro if that is defined;
1862 otherwise, in /usr/tmp or /tmp;
1863 or finally the current directory if all else fails. */
1865 static const char *temp_filename;
1867 /* Length of the prefix. */
1869 static int temp_filename_length;
1871 /* Define the list of temporary files to delete. */
1873 struct temp_file
1875 const char *name;
1876 struct temp_file *next;
1879 /* Queue of files to delete on success or failure of compilation. */
1880 static struct temp_file *always_delete_queue;
1881 /* Queue of files to delete on failure of compilation. */
1882 static struct temp_file *failure_delete_queue;
1884 /* Record FILENAME as a file to be deleted automatically.
1885 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1886 otherwise delete it in any case.
1887 FAIL_DELETE nonzero means delete it if a compilation step fails;
1888 otherwise delete it in any case. */
1890 void
1891 record_temp_file (filename, always_delete, fail_delete)
1892 const char *filename;
1893 int always_delete;
1894 int fail_delete;
1896 register char *const name = xstrdup (filename);
1898 if (always_delete)
1900 register struct temp_file *temp;
1901 for (temp = always_delete_queue; temp; temp = temp->next)
1902 if (! strcmp (name, temp->name))
1903 goto already1;
1905 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1906 temp->next = always_delete_queue;
1907 temp->name = name;
1908 always_delete_queue = temp;
1910 already1:;
1913 if (fail_delete)
1915 register struct temp_file *temp;
1916 for (temp = failure_delete_queue; temp; temp = temp->next)
1917 if (! strcmp (name, temp->name))
1918 goto already2;
1920 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1921 temp->next = failure_delete_queue;
1922 temp->name = name;
1923 failure_delete_queue = temp;
1925 already2:;
1929 /* Delete all the temporary files whose names we previously recorded. */
1931 static void
1932 delete_if_ordinary (name)
1933 const char *name;
1935 struct stat st;
1936 #ifdef DEBUG
1937 int i, c;
1939 printf ("Delete %s? (y or n) ", name);
1940 fflush (stdout);
1941 i = getchar ();
1942 if (i != '\n')
1943 while ((c = getchar ()) != '\n' && c != EOF)
1946 if (i == 'y' || i == 'Y')
1947 #endif /* DEBUG */
1948 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1949 if (unlink (name) < 0)
1950 if (verbose_flag)
1951 perror_with_name (name);
1954 static void
1955 delete_temp_files ()
1957 register struct temp_file *temp;
1959 for (temp = always_delete_queue; temp; temp = temp->next)
1960 delete_if_ordinary (temp->name);
1961 always_delete_queue = 0;
1964 /* Delete all the files to be deleted on error. */
1966 static void
1967 delete_failure_queue ()
1969 register struct temp_file *temp;
1971 for (temp = failure_delete_queue; temp; temp = temp->next)
1972 delete_if_ordinary (temp->name);
1975 static void
1976 clear_failure_queue ()
1978 failure_delete_queue = 0;
1981 /* Routine to add variables to the environment. We do this to pass
1982 the pathname of the gcc driver, and the directories search to the
1983 collect2 program, which is being run as ld. This way, we can be
1984 sure of executing the right compiler when collect2 wants to build
1985 constructors and destructors. Since the environment variables we
1986 use come from an obstack, we don't have to worry about allocating
1987 space for them. */
1989 #ifndef HAVE_PUTENV
1991 void
1992 putenv (str)
1993 char *str;
1995 #ifndef VMS /* nor about VMS */
1997 extern char **environ;
1998 char **old_environ = environ;
1999 char **envp;
2000 int num_envs = 0;
2001 int name_len = 1;
2002 int str_len = strlen (str);
2003 char *p = str;
2004 int ch;
2006 while ((ch = *p++) != '\0' && ch != '=')
2007 name_len++;
2009 if (!ch)
2010 abort ();
2012 /* Search for replacing an existing environment variable, and
2013 count the number of total environment variables. */
2014 for (envp = old_environ; *envp; envp++)
2016 num_envs++;
2017 if (!strncmp (str, *envp, name_len))
2019 *envp = str;
2020 return;
2024 /* Add a new environment variable */
2025 environ = (char **) xmalloc (sizeof (char *) * (num_envs + 2));
2026 *environ = str;
2027 memcpy ((char *) (environ + 1), (char *) old_environ,
2028 sizeof (char *) * (num_envs + 1));
2030 #endif /* VMS */
2033 #endif /* HAVE_PUTENV */
2035 /* Build a list of search directories from PATHS.
2036 PREFIX is a string to prepend to the list.
2037 If CHECK_DIR_P is non-zero we ensure the directory exists.
2038 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2039 It is also used by the --print-search-dirs flag. */
2041 static char *
2042 build_search_list (paths, prefix, check_dir_p)
2043 struct path_prefix *paths;
2044 const char *prefix;
2045 int check_dir_p;
2047 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2048 int just_suffix_len
2049 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2050 int first_time = TRUE;
2051 struct prefix_list *pprefix;
2053 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2054 obstack_1grow (&collect_obstack, '=');
2056 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2058 int len = strlen (pprefix->prefix);
2060 if (machine_suffix
2061 && (! check_dir_p
2062 || is_directory (pprefix->prefix, machine_suffix, 0)))
2064 if (!first_time)
2065 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2067 first_time = FALSE;
2068 obstack_grow (&collect_obstack, pprefix->prefix, len);
2069 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2072 if (just_machine_suffix
2073 && pprefix->require_machine_suffix == 2
2074 && (! check_dir_p
2075 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2077 if (! first_time)
2078 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2080 first_time = FALSE;
2081 obstack_grow (&collect_obstack, pprefix->prefix, len);
2082 obstack_grow (&collect_obstack, just_machine_suffix,
2083 just_suffix_len);
2086 if (! pprefix->require_machine_suffix)
2088 if (! first_time)
2089 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2091 first_time = FALSE;
2092 obstack_grow (&collect_obstack, pprefix->prefix, len);
2096 obstack_1grow (&collect_obstack, '\0');
2097 return obstack_finish (&collect_obstack);
2100 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2101 for collect. */
2103 static void
2104 putenv_from_prefixes (paths, env_var)
2105 struct path_prefix *paths;
2106 const char *env_var;
2108 putenv (build_search_list (paths, env_var, 1));
2111 #ifndef VMS
2113 /* FIXME: the location independence code for VMS is hairier than this,
2114 and hasn't been written. */
2116 /* Split a filename into component directories. */
2118 static char **
2119 split_directories (name, ptr_num_dirs)
2120 const char *name;
2121 int *ptr_num_dirs;
2123 int num_dirs = 0;
2124 char **dirs;
2125 const char *p, *q;
2126 int ch;
2128 /* Count the number of directories. Special case MSDOS disk names as part
2129 of the initial directory. */
2130 p = name;
2131 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2132 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2134 p += 3;
2135 num_dirs++;
2137 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2139 while ((ch = *p++) != '\0')
2141 if (IS_DIR_SEPARATOR (ch))
2143 num_dirs++;
2144 while (IS_DIR_SEPARATOR (*p))
2145 p++;
2149 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2151 /* Now copy the directory parts. */
2152 num_dirs = 0;
2153 p = name;
2154 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2155 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2157 dirs[num_dirs++] = save_string (p, 3);
2158 p += 3;
2160 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2162 q = p;
2163 while ((ch = *p++) != '\0')
2165 if (IS_DIR_SEPARATOR (ch))
2167 while (IS_DIR_SEPARATOR (*p))
2168 p++;
2170 dirs[num_dirs++] = save_string (q, p - q);
2171 q = p;
2175 if (p - 1 - q > 0)
2176 dirs[num_dirs++] = save_string (q, p - 1 - q);
2178 dirs[num_dirs] = NULL_PTR;
2179 if (ptr_num_dirs)
2180 *ptr_num_dirs = num_dirs;
2182 return dirs;
2185 /* Release storage held by split directories. */
2187 static void
2188 free_split_directories (dirs)
2189 char **dirs;
2191 int i = 0;
2193 while (dirs[i] != NULL_PTR)
2194 free (dirs[i++]);
2196 free ((char *) dirs);
2199 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2200 to PREFIX starting with the directory portion of PROGNAME and a relative
2201 pathname of the difference between BIN_PREFIX and PREFIX.
2203 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2204 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2205 function will return /red/green/blue/../omega.
2207 If no relative prefix can be found, return NULL. */
2209 static char *
2210 make_relative_prefix (progname, bin_prefix, prefix)
2211 const char *progname;
2212 const char *bin_prefix;
2213 const char *prefix;
2215 char **prog_dirs, **bin_dirs, **prefix_dirs;
2216 int prog_num, bin_num, prefix_num, std_loc_p;
2217 int i, n, common;
2219 prog_dirs = split_directories (progname, &prog_num);
2220 bin_dirs = split_directories (bin_prefix, &bin_num);
2222 /* If there is no full pathname, try to find the program by checking in each
2223 of the directories specified in the PATH environment variable. */
2224 if (prog_num == 1)
2226 char *temp;
2228 GET_ENV_PATH_LIST (temp, "PATH");
2229 if (temp)
2231 char *startp, *endp;
2232 char *nstore = (char *) alloca (strlen (temp) + strlen (progname) + 1);
2234 startp = endp = temp;
2235 while (1)
2237 if (*endp == PATH_SEPARATOR || *endp == 0)
2239 if (endp == startp)
2241 nstore[0] = '.';
2242 nstore[1] = DIR_SEPARATOR;
2243 nstore[2] = '\0';
2245 else
2247 strncpy (nstore, startp, endp - startp);
2248 if (! IS_DIR_SEPARATOR (endp[-1]))
2250 nstore[endp - startp] = DIR_SEPARATOR;
2251 nstore[endp - startp + 1] = 0;
2253 else
2254 nstore[endp - startp] = 0;
2256 strcat (nstore, progname);
2257 if (! access (nstore, X_OK)
2258 #ifdef HAVE_EXECUTABLE_SUFFIX
2259 || ! access (strcat (nstore, EXECUTABLE_SUFFIX), X_OK)
2260 #endif
2263 free_split_directories (prog_dirs);
2264 progname = nstore;
2265 prog_dirs = split_directories (progname, &prog_num);
2266 break;
2269 if (*endp == 0)
2270 break;
2271 endp = startp = endp + 1;
2273 else
2274 endp++;
2279 /* Remove the program name from comparison of directory names. */
2280 prog_num--;
2282 /* Determine if the compiler is installed in the standard location, and if
2283 so, we don't need to specify relative directories. Also, if argv[0]
2284 doesn't contain any directory specifiers, there is not much we can do. */
2285 std_loc_p = 0;
2286 if (prog_num == bin_num)
2288 for (i = 0; i < bin_num; i++)
2290 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2291 break;
2294 if (prog_num <= 0 || i == bin_num)
2296 std_loc_p = 1;
2297 free_split_directories (prog_dirs);
2298 free_split_directories (bin_dirs);
2299 prog_dirs = bin_dirs = (char **) 0;
2300 return NULL_PTR;
2304 prefix_dirs = split_directories (prefix, &prefix_num);
2306 /* Find how many directories are in common between bin_prefix & prefix. */
2307 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2308 for (common = 0; common < n; common++)
2310 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2311 break;
2314 /* If there are no common directories, there can be no relative prefix. */
2315 if (common == 0)
2317 free_split_directories (prog_dirs);
2318 free_split_directories (bin_dirs);
2319 free_split_directories (prefix_dirs);
2320 return NULL_PTR;
2323 /* Build up the pathnames in argv[0]. */
2324 for (i = 0; i < prog_num; i++)
2325 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2327 /* Now build up the ..'s. */
2328 for (i = common; i < n; i++)
2330 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2331 obstack_1grow (&obstack, DIR_SEPARATOR);
2334 /* Put in directories to move over to prefix. */
2335 for (i = common; i < prefix_num; i++)
2336 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2338 free_split_directories (prog_dirs);
2339 free_split_directories (bin_dirs);
2340 free_split_directories (prefix_dirs);
2342 obstack_1grow (&obstack, '\0');
2343 return obstack_finish (&obstack);
2345 #endif /* VMS */
2347 /* Check whether NAME can be accessed in MODE. This is like access,
2348 except that it never considers directories to be executable. */
2350 static int
2351 access_check (name, mode)
2352 const char *name;
2353 int mode;
2355 if (mode == X_OK)
2357 struct stat st;
2359 if (stat (name, &st) < 0
2360 || S_ISDIR (st.st_mode))
2361 return -1;
2364 return access (name, mode);
2367 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2368 access to check permissions.
2369 Return 0 if not found, otherwise return its name, allocated with malloc. */
2371 static char *
2372 find_a_file (pprefix, name, mode)
2373 struct path_prefix *pprefix;
2374 const char *name;
2375 int mode;
2377 char *temp;
2378 const char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
2379 struct prefix_list *pl;
2380 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2382 #ifdef DEFAULT_ASSEMBLER
2383 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2384 return xstrdup (DEFAULT_ASSEMBLER);
2385 #endif
2387 #ifdef DEFAULT_LINKER
2388 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2389 return xstrdup (DEFAULT_LINKER);
2390 #endif
2392 if (machine_suffix)
2393 len += strlen (machine_suffix);
2395 temp = xmalloc (len);
2397 /* Determine the filename to execute (special case for absolute paths). */
2399 if (IS_ABSOLUTE_PATHNAME (name))
2401 if (access (name, mode) == 0)
2403 strcpy (temp, name);
2404 return temp;
2407 else
2408 for (pl = pprefix->plist; pl; pl = pl->next)
2410 if (machine_suffix)
2412 /* Some systems have a suffix for executable files.
2413 So try appending that first. */
2414 if (file_suffix[0] != 0)
2416 strcpy (temp, pl->prefix);
2417 strcat (temp, machine_suffix);
2418 strcat (temp, name);
2419 strcat (temp, file_suffix);
2420 if (access_check (temp, mode) == 0)
2422 if (pl->used_flag_ptr != 0)
2423 *pl->used_flag_ptr = 1;
2424 return temp;
2428 /* Now try just the name. */
2429 strcpy (temp, pl->prefix);
2430 strcat (temp, machine_suffix);
2431 strcat (temp, name);
2432 if (access_check (temp, mode) == 0)
2434 if (pl->used_flag_ptr != 0)
2435 *pl->used_flag_ptr = 1;
2436 return temp;
2440 /* Certain prefixes are tried with just the machine type,
2441 not the version. This is used for finding as, ld, etc. */
2442 if (just_machine_suffix && pl->require_machine_suffix == 2)
2444 /* Some systems have a suffix for executable files.
2445 So try appending that first. */
2446 if (file_suffix[0] != 0)
2448 strcpy (temp, pl->prefix);
2449 strcat (temp, just_machine_suffix);
2450 strcat (temp, name);
2451 strcat (temp, file_suffix);
2452 if (access_check (temp, mode) == 0)
2454 if (pl->used_flag_ptr != 0)
2455 *pl->used_flag_ptr = 1;
2456 return temp;
2460 strcpy (temp, pl->prefix);
2461 strcat (temp, just_machine_suffix);
2462 strcat (temp, name);
2463 if (access_check (temp, mode) == 0)
2465 if (pl->used_flag_ptr != 0)
2466 *pl->used_flag_ptr = 1;
2467 return temp;
2471 /* Certain prefixes can't be used without the machine suffix
2472 when the machine or version is explicitly specified. */
2473 if (! pl->require_machine_suffix)
2475 /* Some systems have a suffix for executable files.
2476 So try appending that first. */
2477 if (file_suffix[0] != 0)
2479 strcpy (temp, pl->prefix);
2480 strcat (temp, name);
2481 strcat (temp, file_suffix);
2482 if (access_check (temp, mode) == 0)
2484 if (pl->used_flag_ptr != 0)
2485 *pl->used_flag_ptr = 1;
2486 return temp;
2490 strcpy (temp, pl->prefix);
2491 strcat (temp, name);
2492 if (access_check (temp, mode) == 0)
2494 if (pl->used_flag_ptr != 0)
2495 *pl->used_flag_ptr = 1;
2496 return temp;
2501 free (temp);
2502 return 0;
2505 /* Ranking of prefixes in the sort list. -B prefixes are put before
2506 all others. */
2508 enum path_prefix_priority
2510 PREFIX_PRIORITY_B_OPT,
2511 PREFIX_PRIORITY_LAST
2514 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2515 order according to PRIORITY. Within each PRIORITY, new entries are
2516 appended.
2518 If WARN is nonzero, we will warn if no file is found
2519 through this prefix. WARN should point to an int
2520 which will be set to 1 if this entry is used.
2522 COMPONENT is the value to be passed to update_path.
2524 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2525 the complete value of machine_suffix.
2526 2 means try both machine_suffix and just_machine_suffix. */
2528 static void
2529 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2530 struct path_prefix *pprefix;
2531 const char *prefix;
2532 const char *component;
2533 /* enum prefix_priority */ int priority;
2534 int require_machine_suffix;
2535 int *warn;
2537 struct prefix_list *pl, **prev;
2538 int len;
2540 for (prev = &pprefix->plist;
2541 (*prev) != NULL && (*prev)->priority <= priority;
2542 prev = &(*prev)->next)
2545 /* Keep track of the longest prefix */
2547 prefix = update_path (prefix, component);
2548 len = strlen (prefix);
2549 if (len > pprefix->max_len)
2550 pprefix->max_len = len;
2552 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2553 pl->prefix = save_string (prefix, len);
2554 pl->require_machine_suffix = require_machine_suffix;
2555 pl->used_flag_ptr = warn;
2556 pl->priority = priority;
2557 if (warn)
2558 *warn = 0;
2560 /* Insert after PREV */
2561 pl->next = (*prev);
2562 (*prev) = pl;
2565 /* Execute the command specified by the arguments on the current line of spec.
2566 When using pipes, this includes several piped-together commands
2567 with `|' between them.
2569 Return 0 if successful, -1 if failed. */
2571 static int
2572 execute ()
2574 int i;
2575 int n_commands; /* # of command. */
2576 char *string;
2577 struct command
2579 const char *prog; /* program name. */
2580 const char **argv; /* vector of args. */
2581 int pid; /* pid of process for this command. */
2584 struct command *commands; /* each command buffer with above info. */
2586 /* Count # of piped commands. */
2587 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2588 if (strcmp (argbuf[i], "|") == 0)
2589 n_commands++;
2591 /* Get storage for each command. */
2592 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2594 /* Split argbuf into its separate piped processes,
2595 and record info about each one.
2596 Also search for the programs that are to be run. */
2598 commands[0].prog = argbuf[0]; /* first command. */
2599 commands[0].argv = &argbuf[0];
2600 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2602 if (string)
2603 commands[0].argv[0] = string;
2605 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2606 if (strcmp (argbuf[i], "|") == 0)
2607 { /* each command. */
2608 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2609 fatal ("-pipe not supported");
2610 #endif
2611 argbuf[i] = 0; /* termination of command args. */
2612 commands[n_commands].prog = argbuf[i + 1];
2613 commands[n_commands].argv = &argbuf[i + 1];
2614 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2615 if (string)
2616 commands[n_commands].argv[0] = string;
2617 n_commands++;
2620 argbuf[argbuf_index] = 0;
2622 /* If -v, print what we are about to do, and maybe query. */
2624 if (verbose_flag)
2626 /* For help listings, put a blank line between sub-processes. */
2627 if (print_help_list)
2628 fputc ('\n', stderr);
2630 /* Print each piped command as a separate line. */
2631 for (i = 0; i < n_commands; i++)
2633 const char *const *j;
2635 for (j = commands[i].argv; *j; j++)
2636 fprintf (stderr, " %s", *j);
2638 /* Print a pipe symbol after all but the last command. */
2639 if (i + 1 != n_commands)
2640 fprintf (stderr, " |");
2641 fprintf (stderr, "\n");
2643 fflush (stderr);
2644 #ifdef DEBUG
2645 notice ("\nGo ahead? (y or n) ");
2646 fflush (stderr);
2647 i = getchar ();
2648 if (i != '\n')
2649 while (getchar () != '\n')
2652 if (i != 'y' && i != 'Y')
2653 return 0;
2654 #endif /* DEBUG */
2657 /* Run each piped subprocess. */
2659 for (i = 0; i < n_commands; i++)
2661 char *errmsg_fmt, *errmsg_arg;
2662 const char *string = commands[i].argv[0];
2664 /* For some bizarre reason, the second argument of execvp() is
2665 char *const *, not const char *const *. */
2666 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2667 programname, temp_filename,
2668 &errmsg_fmt, &errmsg_arg,
2669 ((i == 0 ? PEXECUTE_FIRST : 0)
2670 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2671 | (string == commands[i].prog
2672 ? PEXECUTE_SEARCH : 0)
2673 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2675 if (commands[i].pid == -1)
2676 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2678 if (string != commands[i].prog)
2679 free ((PTR) string);
2682 execution_count++;
2684 /* Wait for all the subprocesses to finish.
2685 We don't care what order they finish in;
2686 we know that N_COMMANDS waits will get them all.
2687 Ignore subprocesses that we don't know about,
2688 since they can be spawned by the process that exec'ed us. */
2691 int ret_code = 0;
2692 #ifdef HAVE_GETRUSAGE
2693 struct timeval d;
2694 double ut = 0.0, st = 0.0;
2695 #endif
2697 for (i = 0; i < n_commands;)
2699 int j;
2700 int status;
2701 int pid;
2703 pid = pwait (commands[i].pid, &status, 0);
2704 if (pid < 0)
2705 abort ();
2707 #ifdef HAVE_GETRUSAGE
2708 if (report_times)
2710 /* getrusage returns the total resource usage of all children
2711 up to now. Copy the previous values into prus, get the
2712 current statistics, then take the difference. */
2714 prus = rus;
2715 getrusage (RUSAGE_CHILDREN, &rus);
2716 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2717 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2718 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2720 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2721 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2722 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2724 #endif
2726 for (j = 0; j < n_commands; j++)
2727 if (commands[j].pid == pid)
2729 i++;
2730 if (WIFSIGNALED (status))
2732 #ifdef SIGPIPE
2733 /* SIGPIPE is a special case. It happens in -pipe mode
2734 when the compiler dies before the preprocessor is
2735 done, or the assembler dies before the compiler is
2736 done. There's generally been an error already, and
2737 this is just fallout. So don't generate another error
2738 unless we would otherwise have succeeded. */
2739 if (WTERMSIG (status) == SIGPIPE
2740 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2742 else
2743 #endif
2744 fatal ("\
2745 Internal error: %s (program %s)\n\
2746 Please submit a full bug report.\n\
2747 See %s for instructions.",
2748 strsignal (WTERMSIG (status)), commands[j].prog,
2749 GCCBUGURL);
2750 signal_count++;
2751 ret_code = -1;
2753 else if (WIFEXITED (status)
2754 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2756 if (WEXITSTATUS (status) > greatest_status)
2757 greatest_status = WEXITSTATUS (status);
2758 ret_code = -1;
2760 #ifdef HAVE_GETRUSAGE
2761 if (report_times && ut + st != 0)
2762 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2763 #endif
2764 break;
2767 return ret_code;
2771 /* Find all the switches given to us
2772 and make a vector describing them.
2773 The elements of the vector are strings, one per switch given.
2774 If a switch uses following arguments, then the `part1' field
2775 is the switch itself and the `args' field
2776 is a null-terminated vector containing the following arguments.
2777 The `live_cond' field is:
2778 0 when initialized
2779 1 if the switch is true in a conditional spec,
2780 -1 if false (overridden by a later switch)
2781 -2 if this switch should be ignored (used in %{<S})
2782 The `validated' field is nonzero if any spec has looked at this switch;
2783 if it remains zero at the end of the run, it must be meaningless. */
2785 #define SWITCH_OK 0
2786 #define SWITCH_FALSE -1
2787 #define SWITCH_IGNORE -2
2788 #define SWITCH_LIVE 1
2790 struct switchstr
2792 const char *part1;
2793 const char **args;
2794 int live_cond;
2795 unsigned char validated;
2796 unsigned char ordering;
2799 static struct switchstr *switches;
2801 static int n_switches;
2803 struct infile
2805 const char *name;
2806 const char *language;
2809 /* Also a vector of input files specified. */
2811 static struct infile *infiles;
2813 int n_infiles;
2815 /* This counts the number of libraries added by lang_specific_driver, so that
2816 we can tell if there were any user supplied any files or libraries. */
2818 static int added_libraries;
2820 /* And a vector of corresponding output files is made up later. */
2822 const char **outfiles;
2824 /* Used to track if none of the -B paths are used. */
2825 static int warn_B;
2827 /* Used to track if standard path isn't used and -b or -V is specified. */
2828 static int warn_std;
2830 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2831 static int *warn_std_ptr = 0;
2833 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2835 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2836 is true if we should look for an executable suffix as well. */
2838 static char *
2839 convert_filename (name, do_exe)
2840 char *name;
2841 int do_exe;
2843 int i;
2844 int len;
2846 if (name == NULL)
2847 return NULL;
2849 len = strlen (name);
2851 #ifdef HAVE_OBJECT_SUFFIX
2852 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2853 if (len > 2
2854 && name[len - 2] == '.'
2855 && name[len - 1] == 'o')
2857 obstack_grow (&obstack, name, len - 2);
2858 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2859 name = obstack_finish (&obstack);
2861 #endif
2863 #if defined(HAVE_EXECUTABLE_SUFFIX) && !defined(NO_AUTO_EXE_SUFFIX)
2864 /* If there is no filetype, make it the executable suffix (which includes
2865 the "."). But don't get confused if we have just "-o". */
2866 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2867 return name;
2869 for (i = len - 1; i >= 0; i--)
2870 if (IS_DIR_SEPARATOR (name[i]))
2871 break;
2873 for (i++; i < len; i++)
2874 if (name[i] == '.')
2875 return name;
2877 obstack_grow (&obstack, name, len);
2878 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2879 name = obstack_finish (&obstack);
2880 #endif
2882 return name;
2884 #endif
2886 /* Display the command line switches accepted by gcc. */
2887 static void
2888 display_help ()
2890 printf (_("Usage: %s [options] file...\n"), programname);
2891 fputs (_("Options:\n"), stdout);
2893 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2894 fputs (_(" --help Display this information\n"), stdout);
2895 fputs (_(" --target-help Display target specific command line options\n"), stdout);
2896 if (! verbose_flag)
2897 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
2898 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2899 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2900 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2901 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2902 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2903 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2904 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2905 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2906 fputs (_("\
2907 -print-multi-lib Display the mapping between command line options and\n\
2908 multiple library search directories\n"), stdout);
2909 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2910 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2911 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
2912 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2913 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
2914 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2915 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
2916 fputs (_(" -specs=<file> Override builtin specs with the contents of <file>\n"), stdout);
2917 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
2918 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
2919 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
2920 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
2921 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
2922 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2923 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2924 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2925 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2926 fputs (_("\
2927 -x <language> Specify the language of the following input files\n\
2928 Permissable languages include: c c++ assembler none\n\
2929 'none' means revert to the default behaviour of\n\
2930 guessing the language based on the file's extension\n\
2931 "), stdout);
2933 printf (_("\
2934 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
2935 passed on to the various sub-processes invoked by %s. In order to pass\n\
2936 other options on to these processes the -W<letter> options must be used.\n\
2937 "), programname);
2939 /* The rest of the options are displayed by invocations of the various
2940 sub-processes. */
2943 static void
2944 add_preprocessor_option (option, len)
2945 const char *option;
2946 int len;
2948 n_preprocessor_options++;
2950 if (! preprocessor_options)
2951 preprocessor_options
2952 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
2953 else
2954 preprocessor_options
2955 = (char **) xrealloc (preprocessor_options,
2956 n_preprocessor_options * sizeof (char *));
2958 preprocessor_options [n_preprocessor_options - 1] =
2959 save_string (option, len);
2962 static void
2963 add_assembler_option (option, len)
2964 const char *option;
2965 int len;
2967 n_assembler_options++;
2969 if (! assembler_options)
2970 assembler_options
2971 = (char **) xmalloc (n_assembler_options * sizeof (char *));
2972 else
2973 assembler_options
2974 = (char **) xrealloc (assembler_options,
2975 n_assembler_options * sizeof (char *));
2977 assembler_options [n_assembler_options - 1] = save_string (option, len);
2980 static void
2981 add_linker_option (option, len)
2982 const char *option;
2983 int len;
2985 n_linker_options++;
2987 if (! linker_options)
2988 linker_options
2989 = (char **) xmalloc (n_linker_options * sizeof (char *));
2990 else
2991 linker_options
2992 = (char **) xrealloc (linker_options,
2993 n_linker_options * sizeof (char *));
2995 linker_options [n_linker_options - 1] = save_string (option, len);
2998 /* Create the vector `switches' and its contents.
2999 Store its length in `n_switches'. */
3001 static void
3002 process_command (argc, argv)
3003 int argc;
3004 const char *const *argv;
3006 register int i;
3007 const char *temp;
3008 char *temp1;
3009 const char *spec_lang = 0;
3010 int last_language_n_infiles;
3011 int have_c = 0;
3012 int have_o = 0;
3013 int lang_n_infiles = 0;
3014 #ifdef MODIFY_TARGET_NAME
3015 int is_modify_target_name;
3016 int j;
3017 #endif
3019 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3021 n_switches = 0;
3022 n_infiles = 0;
3023 added_libraries = 0;
3025 /* Figure compiler version from version string. */
3027 compiler_version = temp1 = xstrdup (version_string);
3029 for (; *temp1; ++temp1)
3031 if (*temp1 == ' ')
3033 *temp1 = '\0';
3034 break;
3038 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3039 see if we can create it from the pathname specified in argv[0]. */
3041 #ifndef VMS
3042 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3043 if (!gcc_exec_prefix)
3045 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3046 standard_exec_prefix);
3047 if (gcc_exec_prefix)
3048 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL_PTR));
3050 #endif
3052 if (gcc_exec_prefix)
3054 int len = strlen (gcc_exec_prefix);
3056 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3057 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3059 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3060 if (IS_DIR_SEPARATOR (*temp)
3061 && strncmp (temp + 1, "lib", 3) == 0
3062 && IS_DIR_SEPARATOR (temp[4])
3063 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3064 len -= sizeof ("/lib/gcc-lib/") - 1;
3067 set_std_prefix (gcc_exec_prefix, len);
3068 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3069 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3070 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3071 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3074 /* COMPILER_PATH and LIBRARY_PATH have values
3075 that are lists of directory names with colons. */
3077 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3078 if (temp)
3080 const char *startp, *endp;
3081 char *nstore = (char *) alloca (strlen (temp) + 3);
3083 startp = endp = temp;
3084 while (1)
3086 if (*endp == PATH_SEPARATOR || *endp == 0)
3088 strncpy (nstore, startp, endp - startp);
3089 if (endp == startp)
3090 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
3091 else if (!IS_DIR_SEPARATOR (endp[-1]))
3093 nstore[endp - startp] = DIR_SEPARATOR;
3094 nstore[endp - startp + 1] = 0;
3096 else
3097 nstore[endp - startp] = 0;
3098 add_prefix (&exec_prefixes, nstore, 0,
3099 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3100 add_prefix (&include_prefixes,
3101 concat (nstore, "include", NULL_PTR),
3102 0, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3103 if (*endp == 0)
3104 break;
3105 endp = startp = endp + 1;
3107 else
3108 endp++;
3112 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3113 if (temp && *cross_compile == '0')
3115 const char *startp, *endp;
3116 char *nstore = (char *) alloca (strlen (temp) + 3);
3118 startp = endp = temp;
3119 while (1)
3121 if (*endp == PATH_SEPARATOR || *endp == 0)
3123 strncpy (nstore, startp, endp - startp);
3124 if (endp == startp)
3125 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
3126 else if (!IS_DIR_SEPARATOR (endp[-1]))
3128 nstore[endp - startp] = DIR_SEPARATOR;
3129 nstore[endp - startp + 1] = 0;
3131 else
3132 nstore[endp - startp] = 0;
3133 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
3134 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3135 if (*endp == 0)
3136 break;
3137 endp = startp = endp + 1;
3139 else
3140 endp++;
3144 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3145 GET_ENV_PATH_LIST (temp, "LPATH");
3146 if (temp && *cross_compile == '0')
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_PTR));
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 (&startfile_prefixes, nstore, NULL_PTR,
3167 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3168 if (*endp == 0)
3169 break;
3170 endp = startp = endp + 1;
3172 else
3173 endp++;
3177 /* Convert new-style -- options to old-style. */
3178 translate_options (&argc, &argv);
3180 /* Do language-specific adjustment/addition of flags. */
3181 lang_specific_driver (&argc, &argv, &added_libraries);
3183 /* Scan argv twice. Here, the first time, just count how many switches
3184 there will be in their vector, and how many input files in theirs.
3185 Also parse any switches that determine the configuration name, such as -b.
3186 Here we also parse the switches that cc itself uses (e.g. -v). */
3188 for (i = 1; i < argc; i++)
3190 if (! strcmp (argv[i], "-dumpspecs"))
3192 struct spec_list *sl;
3193 init_spec ();
3194 for (sl = specs; sl; sl = sl->next)
3195 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3196 if (link_command_spec)
3197 printf ("*link_command:\n%s\n\n", link_command_spec);
3198 exit (0);
3200 else if (! strcmp (argv[i], "-dumpversion"))
3202 printf ("%s\n", spec_version);
3203 exit (0);
3205 else if (! strcmp (argv[i], "-dumpmachine"))
3207 printf ("%s\n", spec_machine);
3208 exit (0);
3210 else if (strcmp (argv[i], "-fhelp") == 0)
3212 /* translate_options () has turned --help into -fhelp. */
3213 print_help_list = 1;
3215 /* We will be passing a dummy file on to the sub-processes. */
3216 n_infiles++;
3217 n_switches++;
3219 add_preprocessor_option ("--help", 6);
3220 add_assembler_option ("--help", 6);
3221 add_linker_option ("--help", 6);
3223 else if (strcmp (argv[i], "-ftarget-help") == 0)
3225 /* translate_options() has turned --target-help into -ftarget-help. */
3226 target_help_flag = 1;
3228 /* We will be passing a dummy file on to the sub-processes. */
3229 n_infiles++;
3230 n_switches++;
3232 add_preprocessor_option ("--target-help", 13);
3233 add_assembler_option ("--target-help", 13);
3234 add_linker_option ("--target-help", 13);
3236 else if (! strcmp (argv[i], "-pass-exit-codes"))
3238 pass_exit_codes = 1;
3239 n_switches++;
3241 else if (! strcmp (argv[i], "-print-search-dirs"))
3242 print_search_dirs = 1;
3243 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3244 print_file_name = "libgcc.a";
3245 else if (! strncmp (argv[i], "-print-file-name=", 17))
3246 print_file_name = argv[i] + 17;
3247 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3248 print_prog_name = argv[i] + 17;
3249 else if (! strcmp (argv[i], "-print-multi-lib"))
3250 print_multi_lib = 1;
3251 else if (! strcmp (argv[i], "-print-multi-directory"))
3252 print_multi_directory = 1;
3253 else if (! strncmp (argv[i], "-Wa,", 4))
3255 int prev, j;
3256 /* Pass the rest of this option to the assembler. */
3258 /* Split the argument at commas. */
3259 prev = 4;
3260 for (j = 4; argv[i][j]; j++)
3261 if (argv[i][j] == ',')
3263 add_assembler_option (argv[i] + prev, j - prev);
3264 prev = j + 1;
3267 /* Record the part after the last comma. */
3268 add_assembler_option (argv[i] + prev, j - prev);
3270 else if (! strncmp (argv[i], "-Wp,", 4))
3272 int prev, j;
3273 /* Pass the rest of this option to the preprocessor. */
3275 /* Split the argument at commas. */
3276 prev = 4;
3277 for (j = 4; argv[i][j]; j++)
3278 if (argv[i][j] == ',')
3280 add_preprocessor_option (argv[i] + prev, j - prev);
3281 prev = j + 1;
3284 /* Record the part after the last comma. */
3285 add_preprocessor_option (argv[i] + prev, j - prev);
3287 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3288 /* The +e options to the C++ front-end. */
3289 n_switches++;
3290 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3292 int j;
3293 /* Split the argument at commas. */
3294 for (j = 3; argv[i][j]; j++)
3295 n_infiles += (argv[i][j] == ',');
3297 else if (strcmp (argv[i], "-Xlinker") == 0)
3299 if (i + 1 == argc)
3300 fatal ("argument to `-Xlinker' is missing");
3302 n_infiles++;
3303 i++;
3305 else if (strncmp (argv[i], "-l", 2) == 0)
3306 n_infiles++;
3307 else if (strcmp (argv[i], "-save-temps") == 0)
3309 save_temps_flag = 1;
3310 n_switches++;
3312 else if (strcmp (argv[i], "-specs") == 0)
3314 struct user_specs *user = (struct user_specs *)
3315 xmalloc (sizeof (struct user_specs));
3316 if (++i >= argc)
3317 fatal ("argument to `-specs' is missing");
3319 user->next = (struct user_specs *) 0;
3320 user->filename = argv[i];
3321 if (user_specs_tail)
3322 user_specs_tail->next = user;
3323 else
3324 user_specs_head = user;
3325 user_specs_tail = user;
3327 else if (strncmp (argv[i], "-specs=", 7) == 0)
3329 struct user_specs *user = (struct user_specs *)
3330 xmalloc (sizeof (struct user_specs));
3331 if (strlen (argv[i]) == 7)
3332 fatal ("argument to `-specs=' is missing");
3334 user->next = (struct user_specs *) 0;
3335 user->filename = argv[i] + 7;
3336 if (user_specs_tail)
3337 user_specs_tail->next = user;
3338 else
3339 user_specs_head = user;
3340 user_specs_tail = user;
3342 else if (strcmp (argv[i], "-time") == 0)
3343 report_times = 1;
3344 else if (argv[i][0] == '-' && argv[i][1] != 0)
3346 register const char *p = &argv[i][1];
3347 register int c = *p;
3349 switch (c)
3351 case 'b':
3352 n_switches++;
3353 if (p[1] == 0 && i + 1 == argc)
3354 fatal ("argument to `-b' is missing");
3355 if (p[1] == 0)
3356 spec_machine = argv[++i];
3357 else
3358 spec_machine = p + 1;
3360 warn_std_ptr = &warn_std;
3361 break;
3363 case 'B':
3365 const char *value;
3366 if (p[1] == 0 && i + 1 == argc)
3367 fatal ("argument to `-B' is missing");
3368 if (p[1] == 0)
3369 value = argv[++i];
3370 else
3371 value = p + 1;
3373 /* As a kludge, if the arg is "[foo/]stageN/", just
3374 add "[foo/]include" to the include prefix. */
3375 int len = strlen (value);
3376 if ((len == 7
3377 || (len > 7
3378 && (IS_DIR_SEPARATOR (value[len - 8]))))
3379 && strncmp (value + len - 7, "stage", 5) == 0
3380 && ISDIGIT (value[len - 2])
3381 && (IS_DIR_SEPARATOR (value[len - 1])))
3383 if (len == 7)
3384 add_prefix (&include_prefixes, "include", NULL_PTR,
3385 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3386 else
3388 char *string = xmalloc (len + 1);
3389 strncpy (string, value, len-7);
3390 strcpy (string+len-7, "include");
3391 add_prefix (&include_prefixes, string, NULL_PTR,
3392 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3396 add_prefix (&exec_prefixes, value, NULL_PTR,
3397 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3398 add_prefix (&startfile_prefixes, value, NULL_PTR,
3399 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3400 add_prefix (&include_prefixes, concat (value, "include",
3401 NULL_PTR),
3402 NULL_PTR,
3403 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3404 n_switches++;
3406 break;
3408 case 'v': /* Print our subcommands and print versions. */
3409 n_switches++;
3410 /* If they do anything other than exactly `-v', don't set
3411 verbose_flag; rather, continue on to give the error. */
3412 if (p[1] != 0)
3413 break;
3414 verbose_flag++;
3415 break;
3417 case 'V':
3418 n_switches++;
3419 if (p[1] == 0 && i + 1 == argc)
3420 fatal ("argument to `-V' is missing");
3421 if (p[1] == 0)
3422 spec_version = argv[++i];
3423 else
3424 spec_version = p + 1;
3425 compiler_version = spec_version;
3426 warn_std_ptr = &warn_std;
3428 /* Validate the version number. Use the same checks
3429 done when inserting it into a spec.
3431 The format of the version string is
3432 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3434 const char *v = compiler_version;
3436 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3437 while (! ISDIGIT (*v))
3438 v++;
3440 if (v > compiler_version && v[-1] != '-')
3441 fatal ("invalid version number format");
3443 /* Set V after the first period. */
3444 while (ISDIGIT (*v))
3445 v++;
3447 if (*v != '.')
3448 fatal ("invalid version number format");
3450 v++;
3451 while (ISDIGIT (*v))
3452 v++;
3454 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
3455 fatal ("invalid version number format");
3457 break;
3459 case 'S':
3460 case 'c':
3461 if (p[1] == 0)
3463 have_c = 1;
3464 n_switches++;
3465 break;
3467 goto normal_switch;
3469 case 'o':
3470 have_o = 1;
3471 #if defined(HAVE_EXECUTABLE_SUFFIX)
3472 if (! have_c)
3474 int skip;
3476 /* Forward scan, just in case -S or -c is specified
3477 after -o. */
3478 int j = i + 1;
3479 if (p[1] == 0)
3480 ++j;
3481 while (j < argc)
3483 if (argv[j][0] == '-')
3485 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3486 && argv[j][2] == 0)
3488 have_c = 1;
3489 break;
3491 else if (skip = SWITCH_TAKES_ARG (argv[j][1]))
3492 j += skip - (argv[j][2] != 0);
3493 else if (skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1))
3494 j += skip;
3496 j++;
3499 #endif
3500 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
3501 if (p[1] == 0)
3502 argv[i + 1] = convert_filename (argv[i + 1], ! have_c);
3503 else
3504 argv[i] = convert_filename (argv[i], ! have_c);
3505 #endif
3506 goto normal_switch;
3508 default:
3509 normal_switch:
3511 #ifdef MODIFY_TARGET_NAME
3512 is_modify_target_name = 0;
3514 for (j = 0;
3515 j < sizeof modify_target / sizeof modify_target[0]; j++)
3516 if (! strcmp (argv[i], modify_target[j].sw))
3518 char *new_name
3519 = (char *) xmalloc (strlen (modify_target[j].str)
3520 + strlen (spec_machine));
3521 const char *p, *r;
3522 char *q;
3523 int made_addition = 0;
3525 is_modify_target_name = 1;
3526 for (p = spec_machine, q = new_name; *p != 0; )
3528 if (modify_target[j].add_del == DELETE
3529 && (! strncmp (q, modify_target[j].str,
3530 strlen (modify_target[j].str))))
3531 p += strlen (modify_target[j].str);
3532 else if (modify_target[j].add_del == ADD
3533 && ! made_addition && *p == '-')
3535 for (r = modify_target[j].str; *r != 0; )
3536 *q++ = *r++;
3537 made_addition = 1;
3540 *q++ = *p++;
3543 spec_machine = new_name;
3546 if (is_modify_target_name)
3547 break;
3548 #endif
3550 n_switches++;
3552 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3553 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3554 else if (WORD_SWITCH_TAKES_ARG (p))
3555 i += WORD_SWITCH_TAKES_ARG (p);
3558 else
3560 n_infiles++;
3561 lang_n_infiles++;
3565 if (have_c && have_o && lang_n_infiles > 1)
3566 fatal ("cannot specify -o with -c or -S and multiple compilations");
3568 /* Set up the search paths before we go looking for config files. */
3570 /* These come before the md prefixes so that we will find gcc's subcommands
3571 (such as cpp) rather than those of the host system. */
3572 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3573 as well as trying the machine and the version. */
3574 #ifndef OS2
3575 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3576 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3577 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3578 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3579 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3580 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3581 #endif
3583 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3584 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3585 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3586 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3588 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3589 dir_separator_str, NULL_PTR);
3591 /* If tooldir is relative, base it on exec_prefixes. A relative
3592 tooldir lets us move the installed tree as a unit.
3594 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3595 directories, so that we can search both the user specified directory
3596 and the standard place. */
3598 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3600 if (gcc_exec_prefix)
3602 char *gcc_exec_tooldir_prefix
3603 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3604 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
3606 add_prefix (&exec_prefixes,
3607 concat (gcc_exec_tooldir_prefix, "bin",
3608 dir_separator_str, NULL_PTR),
3609 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3610 add_prefix (&startfile_prefixes,
3611 concat (gcc_exec_tooldir_prefix, "lib",
3612 dir_separator_str, NULL_PTR),
3613 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3616 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3617 dir_separator_str, spec_version,
3618 dir_separator_str, tooldir_prefix, NULL_PTR);
3621 add_prefix (&exec_prefixes,
3622 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
3623 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3624 add_prefix (&startfile_prefixes,
3625 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
3626 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3628 /* More prefixes are enabled in main, after we read the specs file
3629 and determine whether this is cross-compilation or not. */
3631 /* Then create the space for the vectors and scan again. */
3633 switches = ((struct switchstr *)
3634 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3635 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3636 n_switches = 0;
3637 n_infiles = 0;
3638 last_language_n_infiles = -1;
3640 /* This, time, copy the text of each switch and store a pointer
3641 to the copy in the vector of switches.
3642 Store all the infiles in their vector. */
3644 for (i = 1; i < argc; i++)
3646 /* Just skip the switches that were handled by the preceding loop. */
3647 #ifdef MODIFY_TARGET_NAME
3648 is_modify_target_name = 0;
3650 for (j = 0; j < sizeof modify_target / sizeof modify_target[0]; j++)
3651 if (! strcmp (argv[i], modify_target[j].sw))
3652 is_modify_target_name = 1;
3654 if (is_modify_target_name)
3656 else
3657 #endif
3658 if (! strncmp (argv[i], "-Wa,", 4))
3660 else if (! strncmp (argv[i], "-Wp,", 4))
3662 else if (! strcmp (argv[i], "-pass-exit-codes"))
3664 else if (! strcmp (argv[i], "-print-search-dirs"))
3666 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3668 else if (! strncmp (argv[i], "-print-file-name=", 17))
3670 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3672 else if (! strcmp (argv[i], "-print-multi-lib"))
3674 else if (! strcmp (argv[i], "-print-multi-directory"))
3676 else if (strcmp (argv[i], "-ftarget-help") == 0)
3678 /* Create a dummy input file, so that we can pass --target-help on to
3679 the various sub-processes. */
3680 infiles[n_infiles].language = "c";
3681 infiles[n_infiles++].name = "target-dummy";
3683 /* Preserve the --target-help switch so that it can be caught by
3684 the cc1 spec string. */
3685 switches[n_switches].part1 = "--target-help";
3686 switches[n_switches].args = 0;
3687 switches[n_switches].live_cond = SWITCH_OK;
3688 switches[n_switches].validated = 0;
3690 n_switches++;
3692 else if (strcmp (argv[i], "-fhelp") == 0)
3694 if (verbose_flag)
3696 /* Create a dummy input file, so that we can pass --help on to
3697 the various sub-processes. */
3698 infiles[n_infiles].language = "c";
3699 infiles[n_infiles++].name = "help-dummy";
3701 /* Preserve the --help switch so that it can be caught by the
3702 cc1 spec string. */
3703 switches[n_switches].part1 = "--help";
3704 switches[n_switches].args = 0;
3705 switches[n_switches].live_cond = SWITCH_OK;
3706 switches[n_switches].validated = 0;
3708 n_switches++;
3711 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3713 /* Compensate for the +e options to the C++ front-end;
3714 they're there simply for cfront call-compatibility. We do
3715 some magic in default_compilers to pass them down properly.
3716 Note we deliberately start at the `+' here, to avoid passing
3717 -e0 or -e1 down into the linker. */
3718 switches[n_switches].part1 = &argv[i][0];
3719 switches[n_switches].args = 0;
3720 switches[n_switches].live_cond = SWITCH_OK;
3721 switches[n_switches].validated = 0;
3722 n_switches++;
3724 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3726 int prev, j;
3727 /* Split the argument at commas. */
3728 prev = 4;
3729 for (j = 4; argv[i][j]; j++)
3730 if (argv[i][j] == ',')
3732 infiles[n_infiles].language = "*";
3733 infiles[n_infiles++].name
3734 = save_string (argv[i] + prev, j - prev);
3735 prev = j + 1;
3737 /* Record the part after the last comma. */
3738 infiles[n_infiles].language = "*";
3739 infiles[n_infiles++].name = argv[i] + prev;
3741 else if (strcmp (argv[i], "-Xlinker") == 0)
3743 infiles[n_infiles].language = "*";
3744 infiles[n_infiles++].name = argv[++i];
3746 else if (strncmp (argv[i], "-l", 2) == 0)
3748 infiles[n_infiles].language = "*";
3749 infiles[n_infiles++].name = argv[i];
3751 else if (strcmp (argv[i], "-specs") == 0)
3752 i++;
3753 else if (strncmp (argv[i], "-specs=", 7) == 0)
3755 else if (strcmp (argv[i], "-time") == 0)
3757 else if ((save_temps_flag || report_times)
3758 && strcmp (argv[i], "-pipe") == 0)
3760 /* -save-temps overrides -pipe, so that temp files are produced */
3761 if (save_temps_flag)
3762 error ("Warning: -pipe ignored because -save-temps specified");
3763 /* -time overrides -pipe because we can't get correct stats when
3764 multiple children are running at once. */
3765 else if (report_times)
3766 error ("Warning: -pipe ignored because -time specified");
3768 else if (argv[i][0] == '-' && argv[i][1] != 0)
3770 const char *p = &argv[i][1];
3771 int c = *p;
3773 if (c == 'x')
3775 if (p[1] == 0 && i + 1 == argc)
3776 fatal ("argument to `-x' is missing");
3777 if (p[1] == 0)
3778 spec_lang = argv[++i];
3779 else
3780 spec_lang = p + 1;
3781 if (! strcmp (spec_lang, "none"))
3782 /* Suppress the warning if -xnone comes after the last input
3783 file, because alternate command interfaces like g++ might
3784 find it useful to place -xnone after each input file. */
3785 spec_lang = 0;
3786 else
3787 last_language_n_infiles = n_infiles;
3788 continue;
3790 switches[n_switches].part1 = p;
3791 /* Deal with option arguments in separate argv elements. */
3792 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3793 || WORD_SWITCH_TAKES_ARG (p))
3795 int j = 0;
3796 int n_args = WORD_SWITCH_TAKES_ARG (p);
3798 if (n_args == 0)
3800 /* Count only the option arguments in separate argv elements. */
3801 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3803 if (i + n_args >= argc)
3804 fatal ("argument to `-%s' is missing", p);
3805 switches[n_switches].args
3806 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3807 while (j < n_args)
3808 switches[n_switches].args[j++] = argv[++i];
3809 /* Null-terminate the vector. */
3810 switches[n_switches].args[j] = 0;
3812 else if (strchr (switches_need_spaces, c))
3814 /* On some systems, ld cannot handle some options without
3815 a space. So split the option from its argument. */
3816 char *part1 = (char *) xmalloc (2);
3817 char *tmp;
3818 part1[0] = c;
3819 part1[1] = '\0';
3821 switches[n_switches].part1 = part1;
3822 switches[n_switches].args
3823 = (const char **) xmalloc (2 * sizeof (const char *));
3824 switches[n_switches].args[0] = tmp = xmalloc (strlen (p));
3825 strcpy (tmp, &p[1]);
3826 switches[n_switches].args[1] = 0;
3828 else
3829 switches[n_switches].args = 0;
3831 switches[n_switches].live_cond = SWITCH_OK;
3832 switches[n_switches].validated = 0;
3833 switches[n_switches].ordering = 0;
3834 /* These are always valid, since gcc.c itself understands it. */
3835 if (!strcmp (p, "save-temps")
3836 || !strcmp (p, "static-libgcc")
3837 || !strcmp (p, "shared-libgcc"))
3838 switches[n_switches].validated = 1;
3839 else
3841 char ch = switches[n_switches].part1[0];
3842 if (ch == 'V' || ch == 'b' || ch == 'B')
3843 switches[n_switches].validated = 1;
3845 n_switches++;
3847 else
3849 #ifdef HAVE_OBJECT_SUFFIX
3850 argv[i] = convert_filename (argv[i], 0);
3851 #endif
3853 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
3855 perror_with_name (argv[i]);
3856 error_count++;
3858 else
3860 infiles[n_infiles].language = spec_lang;
3861 infiles[n_infiles++].name = argv[i];
3866 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3867 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3869 switches[n_switches].part1 = 0;
3870 infiles[n_infiles].name = 0;
3873 /* Process a spec string, accumulating and running commands. */
3875 /* These variables describe the input file name.
3876 input_file_number is the index on outfiles of this file,
3877 so that the output file name can be stored for later use by %o.
3878 input_basename is the start of the part of the input file
3879 sans all directory names, and basename_length is the number
3880 of characters starting there excluding the suffix .c or whatever. */
3882 const char *input_filename;
3883 static int input_file_number;
3884 size_t input_filename_length;
3885 static int basename_length;
3886 static int suffixed_basename_length;
3887 static const char *input_basename;
3888 static const char *input_suffix;
3890 /* The compiler used to process the current input file. */
3891 static struct compiler *input_file_compiler;
3893 /* These are variables used within do_spec and do_spec_1. */
3895 /* Nonzero if an arg has been started and not yet terminated
3896 (with space, tab or newline). */
3897 static int arg_going;
3899 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3900 is a temporary file name. */
3901 static int delete_this_arg;
3903 /* Nonzero means %w has been seen; the next arg to be terminated
3904 is the output file name of this compilation. */
3905 static int this_is_output_file;
3907 /* Nonzero means %s has been seen; the next arg to be terminated
3908 is the name of a library file and we should try the standard
3909 search dirs for it. */
3910 static int this_is_library_file;
3912 /* Nonzero means that the input of this command is coming from a pipe. */
3913 static int input_from_pipe;
3915 /* Nonnull means substitute this for any suffix when outputting a switches
3916 arguments. */
3917 static const char *suffix_subst;
3919 /* Process the spec SPEC and run the commands specified therein.
3920 Returns 0 if the spec is successfully processed; -1 if failed. */
3923 do_spec (spec)
3924 const char *spec;
3926 int value;
3928 clear_args ();
3929 arg_going = 0;
3930 delete_this_arg = 0;
3931 this_is_output_file = 0;
3932 this_is_library_file = 0;
3933 input_from_pipe = 0;
3934 suffix_subst = NULL;
3936 value = do_spec_1 (spec, 0, NULL_PTR);
3938 /* Force out any unfinished command.
3939 If -pipe, this forces out the last command if it ended in `|'. */
3940 if (value == 0)
3942 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3943 argbuf_index--;
3945 if (argbuf_index > 0)
3946 value = execute ();
3949 return value;
3952 /* Process the sub-spec SPEC as a portion of a larger spec.
3953 This is like processing a whole spec except that we do
3954 not initialize at the beginning and we do not supply a
3955 newline by default at the end.
3956 INSWITCH nonzero means don't process %-sequences in SPEC;
3957 in this case, % is treated as an ordinary character.
3958 This is used while substituting switches.
3959 INSWITCH nonzero also causes SPC not to terminate an argument.
3961 Value is zero unless a line was finished
3962 and the command on that line reported an error. */
3964 static int
3965 do_spec_1 (spec, inswitch, soft_matched_part)
3966 const char *spec;
3967 int inswitch;
3968 const char *soft_matched_part;
3970 register const char *p = spec;
3971 register int c;
3972 int i;
3973 const char *string;
3974 int value;
3976 while ((c = *p++))
3977 /* If substituting a switch, treat all chars like letters.
3978 Otherwise, NL, SPC, TAB and % are special. */
3979 switch (inswitch ? 'a' : c)
3981 case '\n':
3982 /* End of line: finish any pending argument,
3983 then run the pending command if one has been started. */
3984 if (arg_going)
3986 obstack_1grow (&obstack, 0);
3987 string = obstack_finish (&obstack);
3988 if (this_is_library_file)
3989 string = find_file (string);
3990 store_arg (string, delete_this_arg, this_is_output_file);
3991 if (this_is_output_file)
3992 outfiles[input_file_number] = string;
3994 arg_going = 0;
3996 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3998 for (i = 0; i < n_switches; i++)
3999 if (!strcmp (switches[i].part1, "pipe"))
4000 break;
4002 /* A `|' before the newline means use a pipe here,
4003 but only if -pipe was specified.
4004 Otherwise, execute now and don't pass the `|' as an arg. */
4005 if (i < n_switches)
4007 input_from_pipe = 1;
4008 switches[i].validated = 1;
4009 break;
4011 else
4012 argbuf_index--;
4015 if (argbuf_index > 0)
4017 value = execute ();
4018 if (value)
4019 return value;
4021 /* Reinitialize for a new command, and for a new argument. */
4022 clear_args ();
4023 arg_going = 0;
4024 delete_this_arg = 0;
4025 this_is_output_file = 0;
4026 this_is_library_file = 0;
4027 input_from_pipe = 0;
4028 break;
4030 case '|':
4031 /* End any pending argument. */
4032 if (arg_going)
4034 obstack_1grow (&obstack, 0);
4035 string = obstack_finish (&obstack);
4036 if (this_is_library_file)
4037 string = find_file (string);
4038 store_arg (string, delete_this_arg, this_is_output_file);
4039 if (this_is_output_file)
4040 outfiles[input_file_number] = string;
4043 /* Use pipe */
4044 obstack_1grow (&obstack, c);
4045 arg_going = 1;
4046 break;
4048 case '\t':
4049 case ' ':
4050 /* Space or tab ends an argument if one is pending. */
4051 if (arg_going)
4053 obstack_1grow (&obstack, 0);
4054 string = obstack_finish (&obstack);
4055 if (this_is_library_file)
4056 string = find_file (string);
4057 store_arg (string, delete_this_arg, this_is_output_file);
4058 if (this_is_output_file)
4059 outfiles[input_file_number] = string;
4061 /* Reinitialize for a new argument. */
4062 arg_going = 0;
4063 delete_this_arg = 0;
4064 this_is_output_file = 0;
4065 this_is_library_file = 0;
4066 break;
4068 case '%':
4069 switch (c = *p++)
4071 case 0:
4072 fatal ("Invalid specification! Bug in cc.");
4074 case 'b':
4075 obstack_grow (&obstack, input_basename, basename_length);
4076 arg_going = 1;
4077 break;
4079 case 'B':
4080 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4081 arg_going = 1;
4082 break;
4084 case 'd':
4085 delete_this_arg = 2;
4086 break;
4088 /* Dump out the directories specified with LIBRARY_PATH,
4089 followed by the absolute directories
4090 that we search for startfiles. */
4091 case 'D':
4093 struct prefix_list *pl = startfile_prefixes.plist;
4094 size_t bufsize = 100;
4095 char *buffer = (char *) xmalloc (bufsize);
4096 int idx;
4098 for (; pl; pl = pl->next)
4100 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4101 /* Used on systems which record the specified -L dirs
4102 and use them to search for dynamic linking. */
4103 /* Relative directories always come from -B,
4104 and it is better not to use them for searching
4105 at run time. In particular, stage1 loses. */
4106 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4107 continue;
4108 #endif
4109 /* Try subdirectory if there is one. */
4110 if (multilib_dir != NULL)
4112 if (machine_suffix)
4114 if (strlen (pl->prefix) + strlen (machine_suffix)
4115 >= bufsize)
4116 bufsize = (strlen (pl->prefix)
4117 + strlen (machine_suffix)) * 2 + 1;
4118 buffer = (char *) xrealloc (buffer, bufsize);
4119 strcpy (buffer, pl->prefix);
4120 strcat (buffer, machine_suffix);
4121 if (is_directory (buffer, multilib_dir, 1))
4123 do_spec_1 ("-L", 0, NULL_PTR);
4124 #ifdef SPACE_AFTER_L_OPTION
4125 do_spec_1 (" ", 0, NULL_PTR);
4126 #endif
4127 do_spec_1 (buffer, 1, NULL_PTR);
4128 do_spec_1 (multilib_dir, 1, NULL_PTR);
4129 /* Make this a separate argument. */
4130 do_spec_1 (" ", 0, NULL_PTR);
4133 if (!pl->require_machine_suffix)
4135 if (is_directory (pl->prefix, multilib_dir, 1))
4137 do_spec_1 ("-L", 0, NULL_PTR);
4138 #ifdef SPACE_AFTER_L_OPTION
4139 do_spec_1 (" ", 0, NULL_PTR);
4140 #endif
4141 do_spec_1 (pl->prefix, 1, NULL_PTR);
4142 do_spec_1 (multilib_dir, 1, NULL_PTR);
4143 /* Make this a separate argument. */
4144 do_spec_1 (" ", 0, NULL_PTR);
4148 if (machine_suffix)
4150 if (is_directory (pl->prefix, machine_suffix, 1))
4152 do_spec_1 ("-L", 0, NULL_PTR);
4153 #ifdef SPACE_AFTER_L_OPTION
4154 do_spec_1 (" ", 0, NULL_PTR);
4155 #endif
4156 do_spec_1 (pl->prefix, 1, NULL_PTR);
4157 /* Remove slash from machine_suffix. */
4158 if (strlen (machine_suffix) >= bufsize)
4159 bufsize = strlen (machine_suffix) * 2 + 1;
4160 buffer = (char *) xrealloc (buffer, bufsize);
4161 strcpy (buffer, machine_suffix);
4162 idx = strlen (buffer);
4163 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4164 buffer[idx - 1] = 0;
4165 do_spec_1 (buffer, 1, NULL_PTR);
4166 /* Make this a separate argument. */
4167 do_spec_1 (" ", 0, NULL_PTR);
4170 if (!pl->require_machine_suffix)
4172 if (is_directory (pl->prefix, "", 1))
4174 do_spec_1 ("-L", 0, NULL_PTR);
4175 #ifdef SPACE_AFTER_L_OPTION
4176 do_spec_1 (" ", 0, NULL_PTR);
4177 #endif
4178 /* Remove slash from pl->prefix. */
4179 if (strlen (pl->prefix) >= bufsize)
4180 bufsize = strlen (pl->prefix) * 2 + 1;
4181 buffer = (char *) xrealloc (buffer, bufsize);
4182 strcpy (buffer, pl->prefix);
4183 idx = strlen (buffer);
4184 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4185 buffer[idx - 1] = 0;
4186 do_spec_1 (buffer, 1, NULL_PTR);
4187 /* Make this a separate argument. */
4188 do_spec_1 (" ", 0, NULL_PTR);
4192 free (buffer);
4194 break;
4196 case 'e':
4197 /* %efoo means report an error with `foo' as error message
4198 and don't execute any more commands for this file. */
4200 const char *q = p;
4201 char *buf;
4202 while (*p != 0 && *p != '\n')
4203 p++;
4204 buf = (char *) alloca (p - q + 1);
4205 strncpy (buf, q, p - q);
4206 buf[p - q] = 0;
4207 error ("%s", buf);
4208 return -1;
4210 break;
4211 case 'n':
4212 /* %nfoo means report an notice with `foo' on stderr. */
4214 const char *q = p;
4215 char *buf;
4216 while (*p != 0 && *p != '\n')
4217 p++;
4218 buf = (char *) alloca (p - q + 1);
4219 strncpy (buf, q, p - q);
4220 buf[p - q] = 0;
4221 notice ("%s\n", buf);
4222 if (*p)
4223 p++;
4225 break;
4227 case 'j':
4229 struct stat st;
4231 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4232 and it is not a directory, and it is writable, use it.
4233 Otherwise, fall through and treat this like any other
4234 temporary file. */
4236 if ((!save_temps_flag)
4237 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4238 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4240 obstack_grow (&obstack, HOST_BIT_BUCKET,
4241 strlen (HOST_BIT_BUCKET));
4242 delete_this_arg = 0;
4243 arg_going = 1;
4244 break;
4247 case 'g':
4248 case 'u':
4249 case 'U':
4250 if (save_temps_flag)
4252 obstack_grow (&obstack, input_basename, basename_length);
4253 delete_this_arg = 0;
4255 else
4257 struct temp_name *t;
4258 int suffix_length;
4259 const char *suffix = p;
4260 char *saved_suffix = NULL;
4262 while (*p == '.' || ISALPHA ((unsigned char) *p))
4263 p++;
4264 suffix_length = p - suffix;
4265 if (p[0] == '%' && p[1] == 'O')
4267 p += 2;
4268 /* We don't support extra suffix characters after %O. */
4269 if (*p == '.' || ISALPHA ((unsigned char) *p))
4270 abort ();
4271 if (suffix_length == 0)
4272 suffix = OBJECT_SUFFIX;
4273 else
4275 saved_suffix
4276 = (char *) xmalloc (suffix_length
4277 + strlen (OBJECT_SUFFIX));
4278 strncpy (saved_suffix, suffix, suffix_length);
4279 strcpy (saved_suffix + suffix_length,
4280 OBJECT_SUFFIX);
4282 suffix_length += strlen (OBJECT_SUFFIX);
4285 /* See if we already have an association of %g/%u/%U and
4286 suffix. */
4287 for (t = temp_names; t; t = t->next)
4288 if (t->length == suffix_length
4289 && strncmp (t->suffix, suffix, suffix_length) == 0
4290 && t->unique == (c != 'g'))
4291 break;
4293 /* Make a new association if needed. %u and %j require one. */
4294 if (t == 0 || c == 'u' || c == 'j')
4296 if (t == 0)
4298 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4299 t->next = temp_names;
4300 temp_names = t;
4302 t->length = suffix_length;
4303 t->suffix = save_string (suffix, suffix_length);
4304 t->unique = (c != 'g');
4305 temp_filename = make_temp_file (t->suffix);
4306 temp_filename_length = strlen (temp_filename);
4307 t->filename = temp_filename;
4308 t->filename_length = temp_filename_length;
4311 if (saved_suffix)
4312 free (saved_suffix);
4314 obstack_grow (&obstack, t->filename, t->filename_length);
4315 delete_this_arg = 1;
4317 arg_going = 1;
4318 break;
4320 case 'i':
4321 obstack_grow (&obstack, input_filename, input_filename_length);
4322 arg_going = 1;
4323 break;
4325 case 'I':
4327 struct prefix_list *pl = include_prefixes.plist;
4329 if (gcc_exec_prefix)
4331 do_spec_1 ("-iprefix", 1, NULL_PTR);
4332 /* Make this a separate argument. */
4333 do_spec_1 (" ", 0, NULL_PTR);
4334 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
4335 do_spec_1 (" ", 0, NULL_PTR);
4338 for (; pl; pl = pl->next)
4340 do_spec_1 ("-isystem", 1, NULL_PTR);
4341 /* Make this a separate argument. */
4342 do_spec_1 (" ", 0, NULL_PTR);
4343 do_spec_1 (pl->prefix, 1, NULL_PTR);
4344 do_spec_1 (" ", 0, NULL_PTR);
4347 break;
4349 case 'o':
4351 int max = n_infiles;
4352 max += lang_specific_extra_outfiles;
4354 for (i = 0; i < max; i++)
4355 if (outfiles[i])
4356 store_arg (outfiles[i], 0, 0);
4357 break;
4360 case 'O':
4361 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
4362 arg_going = 1;
4363 break;
4365 case 's':
4366 this_is_library_file = 1;
4367 break;
4369 case 'w':
4370 this_is_output_file = 1;
4371 break;
4373 case 'W':
4375 int cur_index = argbuf_index;
4376 /* Handle the {...} following the %W. */
4377 if (*p != '{')
4378 abort ();
4379 p = handle_braces (p + 1);
4380 if (p == 0)
4381 return -1;
4382 /* If any args were output, mark the last one for deletion
4383 on failure. */
4384 if (argbuf_index != cur_index)
4385 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4386 break;
4389 /* %x{OPTION} records OPTION for %X to output. */
4390 case 'x':
4392 const char *p1 = p;
4393 char *string;
4395 /* Skip past the option value and make a copy. */
4396 if (*p != '{')
4397 abort ();
4398 while (*p++ != '}')
4400 string = save_string (p1 + 1, p - p1 - 2);
4402 /* See if we already recorded this option. */
4403 for (i = 0; i < n_linker_options; i++)
4404 if (! strcmp (string, linker_options[i]))
4406 free (string);
4407 return 0;
4410 /* This option is new; add it. */
4411 add_linker_option (string, strlen (string));
4413 break;
4415 /* Dump out the options accumulated previously using %x. */
4416 case 'X':
4417 for (i = 0; i < n_linker_options; i++)
4419 do_spec_1 (linker_options[i], 1, NULL_PTR);
4420 /* Make each accumulated option a separate argument. */
4421 do_spec_1 (" ", 0, NULL_PTR);
4423 break;
4425 /* Dump out the options accumulated previously using -Wa,. */
4426 case 'Y':
4427 for (i = 0; i < n_assembler_options; i++)
4429 do_spec_1 (assembler_options[i], 1, NULL_PTR);
4430 /* Make each accumulated option a separate argument. */
4431 do_spec_1 (" ", 0, NULL_PTR);
4433 break;
4435 /* Dump out the options accumulated previously using -Wp,. */
4436 case 'Z':
4437 for (i = 0; i < n_preprocessor_options; i++)
4439 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
4440 /* Make each accumulated option a separate argument. */
4441 do_spec_1 (" ", 0, NULL_PTR);
4443 break;
4445 /* Here are digits and numbers that just process
4446 a certain constant string as a spec. */
4448 case '1':
4449 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
4450 if (value != 0)
4451 return value;
4452 break;
4454 case '2':
4455 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
4456 if (value != 0)
4457 return value;
4458 break;
4460 case 'a':
4461 value = do_spec_1 (asm_spec, 0, NULL_PTR);
4462 if (value != 0)
4463 return value;
4464 break;
4466 case 'A':
4467 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
4468 if (value != 0)
4469 return value;
4470 break;
4472 case 'c':
4473 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
4474 if (value != 0)
4475 return value;
4476 break;
4478 case 'C':
4480 const char* spec
4481 = (input_file_compiler->cpp_spec
4482 ? input_file_compiler->cpp_spec
4483 : cpp_spec);
4484 value = do_spec_1 (spec, 0, NULL_PTR);
4485 if (value != 0)
4486 return value;
4488 break;
4490 case 'E':
4491 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
4492 if (value != 0)
4493 return value;
4494 break;
4496 case 'l':
4497 value = do_spec_1 (link_spec, 0, NULL_PTR);
4498 if (value != 0)
4499 return value;
4500 break;
4502 case 'L':
4503 value = do_spec_1 (lib_spec, 0, NULL_PTR);
4504 if (value != 0)
4505 return value;
4506 break;
4508 case 'G':
4509 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
4510 if (value != 0)
4511 return value;
4512 break;
4514 case 'M':
4515 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4517 char *p;
4518 const char *q;
4519 size_t len;
4521 len = strlen (multilib_dir);
4522 obstack_blank (&obstack, len + 1);
4523 p = obstack_next_free (&obstack) - (len + 1);
4525 *p++ = '_';
4526 for (q = multilib_dir; *q ; ++q, ++p)
4527 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4529 break;
4531 case 'p':
4533 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4534 char *buf = x;
4535 const char *y;
4537 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4538 y = cpp_predefines;
4539 while (*y != 0)
4541 if (! strncmp (y, "-D", 2))
4542 /* Copy the whole option. */
4543 while (*y && *y != ' ' && *y != '\t')
4544 *x++ = *y++;
4545 else if (*y == ' ' || *y == '\t')
4546 /* Copy whitespace to the result. */
4547 *x++ = *y++;
4548 /* Don't copy other options. */
4549 else
4550 y++;
4553 *x = 0;
4555 value = do_spec_1 (buf, 0, NULL_PTR);
4556 if (value != 0)
4557 return value;
4559 break;
4561 case 'P':
4563 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4564 char *buf = x;
4565 const char *y;
4567 /* Copy all of CPP_PREDEFINES into BUF,
4568 but force them all into the reserved name space if they
4569 aren't already there. The reserved name space is all
4570 identifiers beginning with two underscores or with one
4571 underscore and a capital letter. We do the forcing by
4572 adding up to two underscores to the beginning and end
4573 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4574 become __mips__. */
4575 y = cpp_predefines;
4576 while (*y != 0)
4578 if (! strncmp (y, "-D", 2))
4580 int flag = 0;
4582 *x++ = *y++;
4583 *x++ = *y++;
4585 if (*y != '_'
4586 || (*(y + 1) != '_'
4587 && ! ISUPPER ((unsigned char) *(y + 1))))
4589 /* Stick __ at front of macro name. */
4590 if (*y != '_')
4591 *x++ = '_';
4592 *x++ = '_';
4593 /* Arrange to stick __ at the end as well. */
4594 flag = 1;
4597 /* Copy the macro name. */
4598 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4599 *x++ = *y++;
4601 if (flag)
4603 if (x[-1] != '_')
4605 if (x[-2] != '_')
4606 *x++ = '_';
4607 *x++ = '_';
4611 /* Copy the value given, if any. */
4612 while (*y && *y != ' ' && *y != '\t')
4613 *x++ = *y++;
4615 else if (*y == ' ' || *y == '\t')
4616 /* Copy whitespace to the result. */
4617 *x++ = *y++;
4618 /* Don't copy -A options */
4619 else
4620 y++;
4622 *x++ = ' ';
4624 /* Copy all of CPP_PREDEFINES into BUF,
4625 but put __ after every -D. */
4626 y = cpp_predefines;
4627 while (*y != 0)
4629 if (! strncmp (y, "-D", 2))
4631 y += 2;
4633 if (*y != '_'
4634 || (*(y + 1) != '_'
4635 && ! ISUPPER ((unsigned char) *(y + 1))))
4637 /* Stick -D__ at front of macro name. */
4638 *x++ = '-';
4639 *x++ = 'D';
4640 if (*y != '_')
4641 *x++ = '_';
4642 *x++ = '_';
4644 /* Copy the macro name. */
4645 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4646 *x++ = *y++;
4648 /* Copy the value given, if any. */
4649 while (*y && *y != ' ' && *y != '\t')
4650 *x++ = *y++;
4652 else
4654 /* Do not copy this macro - we have just done it before */
4655 while (*y && *y != ' ' && *y != '\t')
4656 y++;
4659 else if (*y == ' ' || *y == '\t')
4660 /* Copy whitespace to the result. */
4661 *x++ = *y++;
4662 /* Don't copy -A options. */
4663 else
4664 y++;
4666 *x++ = ' ';
4668 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4669 y = cpp_predefines;
4670 while (*y != 0)
4672 if (! strncmp (y, "-A", 2))
4673 /* Copy the whole option. */
4674 while (*y && *y != ' ' && *y != '\t')
4675 *x++ = *y++;
4676 else if (*y == ' ' || *y == '\t')
4677 /* Copy whitespace to the result. */
4678 *x++ = *y++;
4679 /* Don't copy other options. */
4680 else
4681 y++;
4684 *x = 0;
4686 value = do_spec_1 (buf, 0, NULL_PTR);
4687 if (value != 0)
4688 return value;
4690 break;
4692 case 'S':
4693 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
4694 if (value != 0)
4695 return value;
4696 break;
4698 /* Here we define characters other than letters and digits. */
4700 case '{':
4701 p = handle_braces (p);
4702 if (p == 0)
4703 return -1;
4704 break;
4706 case '%':
4707 obstack_1grow (&obstack, '%');
4708 break;
4710 case '.':
4712 unsigned len = 0;
4714 while (p[len] && p[len] != ' ' && p[len] != '%')
4715 len++;
4716 suffix_subst = save_string (p - 1, len + 1);
4717 p += len;
4719 break;
4721 case '*':
4722 if (soft_matched_part)
4724 do_spec_1 (soft_matched_part, 1, NULL_PTR);
4725 do_spec_1 (" ", 0, NULL_PTR);
4727 else
4728 /* Catch the case where a spec string contains something like
4729 '%{foo:%*}'. ie there is no * in the pattern on the left
4730 hand side of the :. */
4731 error ("Spec failure: '%%*' has not been initialised by pattern match");
4732 break;
4734 /* Process a string found as the value of a spec given by name.
4735 This feature allows individual machine descriptions
4736 to add and use their own specs.
4737 %[...] modifies -D options the way %P does;
4738 %(...) uses the spec unmodified. */
4739 case '[':
4740 error ("Warning: use of obsolete %%[ operator in specs");
4741 case '(':
4743 const char *name = p;
4744 struct spec_list *sl;
4745 int len;
4747 /* The string after the S/P is the name of a spec that is to be
4748 processed. */
4749 while (*p && *p != ')' && *p != ']')
4750 p++;
4752 /* See if it's in the list. */
4753 for (len = p - name, sl = specs; sl; sl = sl->next)
4754 if (sl->name_len == len && !strncmp (sl->name, name, len))
4756 name = *(sl->ptr_spec);
4757 #ifdef DEBUG_SPECS
4758 notice ("Processing spec %c%s%c, which is '%s'\n",
4759 c, sl->name, (c == '(') ? ')' : ']', name);
4760 #endif
4761 break;
4764 if (sl)
4766 if (c == '(')
4768 value = do_spec_1 (name, 0, NULL_PTR);
4769 if (value != 0)
4770 return value;
4772 else
4774 char *x = (char *) alloca (strlen (name) * 2 + 1);
4775 char *buf = x;
4776 const char *y = name;
4777 int flag = 0;
4779 /* Copy all of NAME into BUF, but put __ after
4780 every -D and at the end of each arg. */
4781 while (1)
4783 if (! strncmp (y, "-D", 2))
4785 *x++ = '-';
4786 *x++ = 'D';
4787 *x++ = '_';
4788 *x++ = '_';
4789 y += 2;
4790 flag = 1;
4791 continue;
4793 else if (flag
4794 && (*y == ' ' || *y == '\t' || *y == '='
4795 || *y == '}' || *y == 0))
4797 *x++ = '_';
4798 *x++ = '_';
4799 flag = 0;
4801 if (*y == 0)
4802 break;
4803 else
4804 *x++ = *y++;
4806 *x = 0;
4808 value = do_spec_1 (buf, 0, NULL_PTR);
4809 if (value != 0)
4810 return value;
4814 /* Discard the closing paren or bracket. */
4815 if (*p)
4816 p++;
4818 break;
4820 case 'v':
4822 int c1 = *p++; /* Select first or second version number. */
4823 const char *v = compiler_version;
4824 const char *q;
4825 static const char zeroc = '0';
4827 /* The format of the version string is
4828 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
4830 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
4831 while (! ISDIGIT (*v))
4832 v++;
4833 if (v > compiler_version && v[-1] != '-')
4834 abort ();
4836 /* If desired, advance to second version number. */
4837 if (c1 >= '2')
4839 /* Set V after the first period. */
4840 while (ISDIGIT (*v))
4841 v++;
4842 if (*v != '.')
4843 abort ();
4844 v++;
4847 /* If desired, advance to third version number.
4848 But don't complain if it's not present */
4849 if (c1 == '3')
4851 /* Set V after the second period. */
4852 while (ISDIGIT (*v))
4853 v++;
4854 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
4855 abort ();
4856 if (*v != 0)
4857 v++;
4860 /* Set Q at the next period or at the end. */
4861 q = v;
4862 while (ISDIGIT (*q))
4863 q++;
4864 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
4865 abort ();
4867 if (q > v)
4868 /* Put that part into the command. */
4869 obstack_grow (&obstack, v, q - v);
4870 else
4871 /* Default to "0" */
4872 obstack_grow (&obstack, &zeroc, 1);
4873 arg_going = 1;
4875 break;
4877 case '|':
4878 if (input_from_pipe)
4879 do_spec_1 ("-", 0, NULL_PTR);
4880 break;
4882 default:
4883 error ("Spec failure: Unrecognised spec option '%c'", c);
4884 break;
4886 break;
4888 case '\\':
4889 /* Backslash: treat next character as ordinary. */
4890 c = *p++;
4892 /* fall through */
4893 default:
4894 /* Ordinary character: put it into the current argument. */
4895 obstack_1grow (&obstack, c);
4896 arg_going = 1;
4899 /* End of string. */
4900 return 0;
4903 /* Return 0 if we call do_spec_1 and that returns -1. */
4905 static const char *
4906 handle_braces (p)
4907 register const char *p;
4909 const char *filter, *body = NULL, *endbody = NULL;
4910 int pipe_p = 0;
4911 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
4912 int negate;
4913 int suffix;
4914 int include_blanks = 1;
4915 int elide_switch = 0;
4916 int ordered = 0;
4918 if (*p == '^')
4920 /* A '^' after the open-brace means to not give blanks before args. */
4921 include_blanks = 0;
4922 ++p;
4925 if (*p == '|')
4927 /* A `|' after the open-brace means,
4928 if the test fails, output a single minus sign rather than nothing.
4929 This is used in %{|!pipe:...}. */
4930 pipe_p = 1;
4931 ++p;
4934 if (*p == '<')
4936 /* A `<' after the open-brace means that the switch should be
4937 removed from the command-line. */
4938 elide_switch = 1;
4939 ++p;
4942 next_member:
4943 negate = suffix = 0;
4945 if (*p == '!')
4946 /* A `!' after the open-brace negates the condition:
4947 succeed if the specified switch is not present. */
4948 negate = 1, ++p;
4950 if (*p == '.')
4951 /* A `.' after the open-brace means test against the current suffix. */
4953 if (pipe_p)
4954 abort ();
4956 suffix = 1;
4957 ++p;
4960 if (elide_switch && (negate || pipe_p || suffix))
4962 /* It doesn't make sense to mix elision with other flags. We
4963 could fatal() here, but the standard seems to be to abort. */
4964 abort ();
4967 next_ampersand:
4968 filter = p;
4969 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
4970 p++;
4972 if (*p == '|' && (pipe_p || ordered))
4973 abort ();
4975 if (!body)
4977 if (*p != '}' && *p != '&')
4979 register int count = 1;
4980 register const char *q = p;
4982 while (*q++ != ':')
4983 continue;
4984 body = q;
4986 while (count > 0)
4988 if (*q == '{')
4989 count++;
4990 else if (*q == '}')
4991 count--;
4992 else if (*q == 0)
4993 abort ();
4994 q++;
4996 endbody = q;
4998 else
4999 body = p, endbody = p + 1;
5002 if (suffix)
5004 int found = (input_suffix != 0
5005 && (long) strlen (input_suffix) == (long) (p - filter)
5006 && strncmp (input_suffix, filter, p - filter) == 0);
5008 if (body[0] == '}')
5009 abort ();
5011 if (negate != found
5012 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL_PTR) < 0)
5013 return 0;
5015 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5017 /* Substitute all matching switches as separate args. */
5018 register int i;
5020 for (i = 0; i < n_switches; i++)
5021 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5022 && check_live_switch (i, p - 1 - filter))
5024 if (elide_switch)
5026 switches[i].live_cond = SWITCH_IGNORE;
5027 switches[i].validated = 1;
5029 else
5030 ordered = 1, switches[i].ordering = 1;
5033 else
5035 /* Test for presence of the specified switch. */
5036 register int i;
5037 int present = 0;
5039 /* If name specified ends in *, as in {x*:...},
5040 check for %* and handle that case. */
5041 if (p[-1] == '*' && !negate)
5043 int substitution;
5044 const char *r = body;
5046 /* First see whether we have %*. */
5047 substitution = 0;
5048 while (r < endbody)
5050 if (*r == '%' && r[1] == '*')
5051 substitution = 1;
5052 r++;
5054 /* If we do, handle that case. */
5055 if (substitution)
5057 /* Substitute all matching switches as separate args.
5058 But do this by substituting for %*
5059 in the text that follows the colon. */
5061 unsigned hard_match_len = p - filter - 1;
5062 char *string = save_string (body, endbody - body - 1);
5064 for (i = 0; i < n_switches; i++)
5065 if (!strncmp (switches[i].part1, filter, hard_match_len)
5066 && check_live_switch (i, -1))
5068 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5069 /* Pass any arguments this switch has. */
5070 give_switch (i, 1, 1);
5071 suffix_subst = NULL;
5074 /* We didn't match. Try again. */
5075 if (*p++ == '|')
5076 goto next_member;
5077 return endbody;
5081 /* If name specified ends in *, as in {x*:...},
5082 check for presence of any switch name starting with x. */
5083 if (p[-1] == '*')
5085 for (i = 0; i < n_switches; i++)
5087 unsigned hard_match_len = p - filter - 1;
5089 if (!strncmp (switches[i].part1, filter, hard_match_len)
5090 && check_live_switch (i, hard_match_len))
5092 present = 1;
5093 break;
5097 /* Otherwise, check for presence of exact name specified. */
5098 else
5100 for (i = 0; i < n_switches; i++)
5102 if (!strncmp (switches[i].part1, filter, p - filter)
5103 && switches[i].part1[p - filter] == 0
5104 && check_live_switch (i, -1))
5106 present = 1;
5107 break;
5112 /* If it is as desired (present for %{s...}, absent for %{!s...})
5113 then substitute either the switch or the specified
5114 conditional text. */
5115 if (present != negate)
5117 if (elide_switch)
5119 switches[i].live_cond = SWITCH_IGNORE;
5120 switches[i].validated = 1;
5122 else if (ordered || *p == '&')
5123 ordered = 1, switches[i].ordering = 1;
5124 else if (*p == '}')
5125 give_switch (i, 0, include_blanks);
5126 else
5127 /* Even if many alternatives are matched, only output once. */
5128 true_once = 1;
5130 else if (pipe_p)
5132 /* Here if a %{|...} conditional fails: output a minus sign,
5133 which means "standard output" or "standard input". */
5134 do_spec_1 ("-", 0, NULL_PTR);
5135 return endbody;
5139 /* We didn't match; try again. */
5140 if (*p++ == '|')
5141 goto next_member;
5143 if (p[-1] == '&')
5145 body = 0;
5146 goto next_ampersand;
5149 if (ordered)
5151 int i;
5152 /* Doing this set of switches later preserves their command-line
5153 ordering. This is needed for e.g. -U, -D and -A. */
5154 for (i = 0; i < n_switches; i++)
5155 if (switches[i].ordering == 1)
5157 switches[i].ordering = 0;
5158 give_switch (i, 0, include_blanks);
5161 /* Process the spec just once, regardless of match count. */
5162 else if (true_once)
5164 if (do_spec_1 (save_string (body, endbody - body - 1),
5165 0, NULL_PTR) < 0)
5166 return 0;
5169 return endbody;
5172 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5173 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5174 spec, or -1 if either exact match or %* is used.
5176 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5177 whose value does not begin with "no-" is obsoleted by the same value
5178 with the "no-", similarly for a switch with the "no-" prefix. */
5180 static int
5181 check_live_switch (switchnum, prefix_length)
5182 int switchnum;
5183 int prefix_length;
5185 const char *name = switches[switchnum].part1;
5186 int i;
5188 /* In the common case of {<at-most-one-letter>*}, a negating
5189 switch would always match, so ignore that case. We will just
5190 send the conflicting switches to the compiler phase. */
5191 if (prefix_length >= 0 && prefix_length <= 1)
5192 return 1;
5194 /* If we already processed this switch and determined if it was
5195 live or not, return our past determination. */
5196 if (switches[switchnum].live_cond != 0)
5197 return switches[switchnum].live_cond > 0;
5199 /* Now search for duplicate in a manner that depends on the name. */
5200 switch (*name)
5202 case 'O':
5203 for (i = switchnum + 1; i < n_switches; i++)
5204 if (switches[i].part1[0] == 'O')
5206 switches[switchnum].validated = 1;
5207 switches[switchnum].live_cond = SWITCH_FALSE;
5208 return 0;
5210 break;
5212 case 'W': case 'f': case 'm':
5213 if (! strncmp (name + 1, "no-", 3))
5215 /* We have Xno-YYY, search for XYYY. */
5216 for (i = switchnum + 1; i < n_switches; i++)
5217 if (switches[i].part1[0] == name[0]
5218 && ! strcmp (&switches[i].part1[1], &name[4]))
5220 switches[switchnum].validated = 1;
5221 switches[switchnum].live_cond = SWITCH_FALSE;
5222 return 0;
5225 else
5227 /* We have XYYY, search for Xno-YYY. */
5228 for (i = switchnum + 1; i < n_switches; i++)
5229 if (switches[i].part1[0] == name[0]
5230 && switches[i].part1[1] == 'n'
5231 && switches[i].part1[2] == 'o'
5232 && switches[i].part1[3] == '-'
5233 && !strcmp (&switches[i].part1[4], &name[1]))
5235 switches[switchnum].validated = 1;
5236 switches[switchnum].live_cond = SWITCH_FALSE;
5237 return 0;
5240 break;
5243 /* Otherwise the switch is live. */
5244 switches[switchnum].live_cond = SWITCH_LIVE;
5245 return 1;
5248 /* Pass a switch to the current accumulating command
5249 in the same form that we received it.
5250 SWITCHNUM identifies the switch; it is an index into
5251 the vector of switches gcc received, which is `switches'.
5252 This cannot fail since it never finishes a command line.
5254 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5256 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5257 of the switch. */
5259 static void
5260 give_switch (switchnum, omit_first_word, include_blanks)
5261 int switchnum;
5262 int omit_first_word;
5263 int include_blanks;
5265 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5266 return;
5268 if (!omit_first_word)
5270 do_spec_1 ("-", 0, NULL_PTR);
5271 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
5274 if (switches[switchnum].args != 0)
5276 const char **p;
5277 for (p = switches[switchnum].args; *p; p++)
5279 const char *arg = *p;
5281 if (include_blanks)
5282 do_spec_1 (" ", 0, NULL_PTR);
5283 if (suffix_subst)
5285 unsigned length = strlen (arg);
5286 int dot = 0;
5288 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5289 if (arg[length] == '.')
5291 ((char *)arg)[length] = 0;
5292 dot = 1;
5293 break;
5295 do_spec_1 (arg, 1, NULL_PTR);
5296 if (dot)
5297 ((char *)arg)[length] = '.';
5298 do_spec_1 (suffix_subst, 1, NULL_PTR);
5300 else
5301 do_spec_1 (arg, 1, NULL_PTR);
5305 do_spec_1 (" ", 0, NULL_PTR);
5306 switches[switchnum].validated = 1;
5309 /* Search for a file named NAME trying various prefixes including the
5310 user's -B prefix and some standard ones.
5311 Return the absolute file name found. If nothing is found, return NAME. */
5313 static const char *
5314 find_file (name)
5315 const char *name;
5317 char *newname;
5319 /* Try multilib_dir if it is defined. */
5320 if (multilib_dir != NULL)
5322 char *try;
5324 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
5325 strcpy (try, multilib_dir);
5326 strcat (try, dir_separator_str);
5327 strcat (try, name);
5329 newname = find_a_file (&startfile_prefixes, try, R_OK);
5331 /* If we don't find it in the multi library dir, then fall
5332 through and look for it in the normal places. */
5333 if (newname != NULL)
5334 return newname;
5337 newname = find_a_file (&startfile_prefixes, name, R_OK);
5338 return newname ? newname : name;
5341 /* Determine whether a directory exists. If LINKER, return 0 for
5342 certain fixed names not needed by the linker. If not LINKER, it is
5343 only important to return 0 if the host machine has a small ARG_MAX
5344 limit. */
5346 static int
5347 is_directory (path1, path2, linker)
5348 const char *path1;
5349 const char *path2;
5350 int linker;
5352 int len1 = strlen (path1);
5353 int len2 = strlen (path2);
5354 char *path = (char *) alloca (3 + len1 + len2);
5355 char *cp;
5356 struct stat st;
5358 #ifndef SMALL_ARG_MAX
5359 if (! linker)
5360 return 1;
5361 #endif
5363 /* Construct the path from the two parts. Ensure the string ends with "/.".
5364 The resulting path will be a directory even if the given path is a
5365 symbolic link. */
5366 memcpy (path, path1, len1);
5367 memcpy (path + len1, path2, len2);
5368 cp = path + len1 + len2;
5369 if (!IS_DIR_SEPARATOR (cp[-1]))
5370 *cp++ = DIR_SEPARATOR;
5371 *cp++ = '.';
5372 *cp = '\0';
5374 /* Exclude directories that the linker is known to search. */
5375 if (linker
5376 && ((cp - path == 6
5377 && strcmp (path, concat (dir_separator_str, "lib",
5378 dir_separator_str, ".", NULL_PTR)) == 0)
5379 || (cp - path == 10
5380 && strcmp (path, concat (dir_separator_str, "usr",
5381 dir_separator_str, "lib",
5382 dir_separator_str, ".", NULL_PTR)) == 0)))
5383 return 0;
5385 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5388 /* Set up the various global variables to indicate that we're processing
5389 the input file named FILENAME. */
5391 static void
5392 set_input (filename)
5393 const char *filename;
5395 register const char *p;
5397 input_filename = filename;
5398 input_filename_length = strlen (input_filename);
5400 input_basename = input_filename;
5401 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5402 /* Skip drive name so 'x:foo' is handled properly. */
5403 if (input_basename[1] == ':')
5404 input_basename += 2;
5405 #endif
5406 for (p = input_basename; *p; p++)
5407 if (IS_DIR_SEPARATOR (*p))
5408 input_basename = p + 1;
5410 /* Find a suffix starting with the last period,
5411 and set basename_length to exclude that suffix. */
5412 basename_length = strlen (input_basename);
5413 suffixed_basename_length = basename_length;
5414 p = input_basename + basename_length;
5415 while (p != input_basename && *p != '.')
5416 --p;
5417 if (*p == '.' && p != input_basename)
5419 basename_length = p - input_basename;
5420 input_suffix = p + 1;
5422 else
5423 input_suffix = "";
5426 /* On fatal signals, delete all the temporary files. */
5428 static void
5429 fatal_error (signum)
5430 int signum;
5432 signal (signum, SIG_DFL);
5433 delete_failure_queue ();
5434 delete_temp_files ();
5435 /* Get the same signal again, this time not handled,
5436 so its normal effect occurs. */
5437 kill (getpid (), signum);
5440 extern int main PARAMS ((int, const char *const *));
5443 main (argc, argv)
5444 int argc;
5445 const char *const *argv;
5447 size_t i;
5448 int value;
5449 int linker_was_run = 0;
5450 char *explicit_link_files;
5451 char *specs_file;
5452 const char *p;
5453 struct user_specs *uptr;
5455 p = argv[0] + strlen (argv[0]);
5456 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5457 --p;
5458 programname = p;
5460 xmalloc_set_program_name (programname);
5462 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5463 /* Perform host dependant initialization when needed. */
5464 GCC_DRIVER_HOST_INITIALIZATION;
5465 #endif
5467 /* LC_CTYPE determines the character set used by the terminal so it has be set
5468 to output messages correctly. */
5470 #ifdef HAVE_LC_MESSAGES
5471 setlocale (LC_CTYPE, "");
5472 setlocale (LC_MESSAGES, "");
5473 #else
5474 setlocale (LC_ALL, "");
5475 #endif
5477 (void) bindtextdomain (PACKAGE, localedir);
5478 (void) textdomain (PACKAGE);
5480 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5481 signal (SIGINT, fatal_error);
5482 #ifdef SIGHUP
5483 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5484 signal (SIGHUP, fatal_error);
5485 #endif
5486 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5487 signal (SIGTERM, fatal_error);
5488 #ifdef SIGPIPE
5489 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5490 signal (SIGPIPE, fatal_error);
5491 #endif
5492 #ifdef SIGCHLD
5493 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5494 receive the signal. A different setting is inheritable */
5495 signal (SIGCHLD, SIG_DFL);
5496 #endif
5498 argbuf_length = 10;
5499 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5501 obstack_init (&obstack);
5503 /* Build multilib_select, et. al from the separate lines that make up each
5504 multilib selection. */
5506 const char *const *q = multilib_raw;
5507 int need_space;
5509 obstack_init (&multilib_obstack);
5510 while ((p = *q++) != (char *) 0)
5511 obstack_grow (&multilib_obstack, p, strlen (p));
5513 obstack_1grow (&multilib_obstack, 0);
5514 multilib_select = obstack_finish (&multilib_obstack);
5516 q = multilib_matches_raw;
5517 while ((p = *q++) != (char *) 0)
5518 obstack_grow (&multilib_obstack, p, strlen (p));
5520 obstack_1grow (&multilib_obstack, 0);
5521 multilib_matches = obstack_finish (&multilib_obstack);
5523 q = multilib_exclusions_raw;
5524 while ((p = *q++) != (char *) 0)
5525 obstack_grow (&multilib_obstack, p, strlen (p));
5527 obstack_1grow (&multilib_obstack, 0);
5528 multilib_exclusions = obstack_finish (&multilib_obstack);
5530 need_space = FALSE;
5531 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5533 if (need_space)
5534 obstack_1grow (&multilib_obstack, ' ');
5535 obstack_grow (&multilib_obstack,
5536 multilib_defaults_raw[i],
5537 strlen (multilib_defaults_raw[i]));
5538 need_space = TRUE;
5541 obstack_1grow (&multilib_obstack, 0);
5542 multilib_defaults = obstack_finish (&multilib_obstack);
5545 /* Set up to remember the pathname of gcc and any options
5546 needed for collect. We use argv[0] instead of programname because
5547 we need the complete pathname. */
5548 obstack_init (&collect_obstack);
5549 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5550 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5551 putenv (obstack_finish (&collect_obstack));
5553 #ifdef INIT_ENVIRONMENT
5554 /* Set up any other necessary machine specific environment variables. */
5555 putenv (INIT_ENVIRONMENT);
5556 #endif
5558 /* Make a table of what switches there are (switches, n_switches).
5559 Make a table of specified input files (infiles, n_infiles).
5560 Decode switches that are handled locally. */
5562 process_command (argc, argv);
5565 int first_time;
5567 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
5568 the compiler. */
5569 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
5570 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
5572 first_time = TRUE;
5573 for (i = 0; (int) i < n_switches; i++)
5575 const char *const *args;
5576 const char *p, *q;
5577 if (!first_time)
5578 obstack_grow (&collect_obstack, " ", 1);
5580 first_time = FALSE;
5581 obstack_grow (&collect_obstack, "'-", 2);
5582 q = switches[i].part1;
5583 while ((p = strchr (q, '\'')))
5585 obstack_grow (&collect_obstack, q, p - q);
5586 obstack_grow (&collect_obstack, "'\\''", 4);
5587 q = ++p;
5589 obstack_grow (&collect_obstack, q, strlen (q));
5590 obstack_grow (&collect_obstack, "'", 1);
5592 for (args = switches[i].args; args && *args; args++)
5594 obstack_grow (&collect_obstack, " '", 2);
5595 q = *args;
5596 while ((p = strchr (q, '\'')))
5598 obstack_grow (&collect_obstack, q, p - q);
5599 obstack_grow (&collect_obstack, "'\\''", 4);
5600 q = ++p;
5602 obstack_grow (&collect_obstack, q, strlen (q));
5603 obstack_grow (&collect_obstack, "'", 1);
5606 obstack_grow (&collect_obstack, "\0", 1);
5607 putenv (obstack_finish (&collect_obstack));
5610 /* Initialize the vector of specs to just the default.
5611 This means one element containing 0s, as a terminator. */
5613 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5614 memcpy ((char *) compilers, (char *) default_compilers,
5615 sizeof default_compilers);
5616 n_compilers = n_default_compilers;
5618 /* Read specs from a file if there is one. */
5620 machine_suffix = concat (spec_machine, dir_separator_str,
5621 spec_version, dir_separator_str, NULL_PTR);
5622 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
5624 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5625 /* Read the specs file unless it is a default one. */
5626 if (specs_file != 0 && strcmp (specs_file, "specs"))
5627 read_specs (specs_file, TRUE);
5628 else
5629 init_spec ();
5631 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5632 for any override of as, ld and libraries. */
5633 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5634 + strlen (just_machine_suffix)
5635 + sizeof ("specs"));
5637 strcpy (specs_file, standard_exec_prefix);
5638 strcat (specs_file, just_machine_suffix);
5639 strcat (specs_file, "specs");
5640 if (access (specs_file, R_OK) == 0)
5641 read_specs (specs_file, TRUE);
5643 /* If not cross-compiling, look for startfiles in the standard places. */
5644 if (*cross_compile == '0')
5646 if (*md_exec_prefix)
5648 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5649 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5650 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5651 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5654 if (*md_startfile_prefix)
5655 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5656 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5658 if (*md_startfile_prefix_1)
5659 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5660 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5662 /* If standard_startfile_prefix is relative, base it on
5663 standard_exec_prefix. This lets us move the installed tree
5664 as a unit. If GCC_EXEC_PREFIX is defined, base
5665 standard_startfile_prefix on that as well. */
5666 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5667 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5668 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5669 else
5671 if (gcc_exec_prefix)
5672 add_prefix (&startfile_prefixes,
5673 concat (gcc_exec_prefix, machine_suffix,
5674 standard_startfile_prefix, NULL_PTR),
5675 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5676 add_prefix (&startfile_prefixes,
5677 concat (standard_exec_prefix,
5678 machine_suffix,
5679 standard_startfile_prefix, NULL_PTR),
5680 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5683 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5684 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5685 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5686 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5687 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5688 add_prefix (&startfile_prefixes, "./", NULL_PTR,
5689 PREFIX_PRIORITY_LAST, 1, NULL_PTR);
5690 #endif
5692 else
5694 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5695 && gcc_exec_prefix)
5696 add_prefix (&startfile_prefixes,
5697 concat (gcc_exec_prefix, machine_suffix,
5698 standard_startfile_prefix, NULL_PTR),
5699 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5702 /* Process any user specified specs in the order given on the command
5703 line. */
5704 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5706 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5707 read_specs (filename ? filename : uptr->filename, FALSE);
5710 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5711 if (gcc_exec_prefix)
5713 char *temp = (char *) xmalloc (strlen (gcc_exec_prefix)
5714 + strlen (spec_version)
5715 + strlen (spec_machine) + 3);
5716 strcpy (temp, gcc_exec_prefix);
5717 strcat (temp, spec_machine);
5718 strcat (temp, dir_separator_str);
5719 strcat (temp, spec_version);
5720 strcat (temp, dir_separator_str);
5721 gcc_exec_prefix = temp;
5724 /* Now we have the specs.
5725 Set the `valid' bits for switches that match anything in any spec. */
5727 validate_all_switches ();
5729 /* Now that we have the switches and the specs, set
5730 the subdirectory based on the options. */
5731 set_multilib_dir ();
5733 /* Warn about any switches that no pass was interested in. */
5735 for (i = 0; (int) i < n_switches; i++)
5736 if (! switches[i].validated)
5737 error ("unrecognized option `-%s'", switches[i].part1);
5739 /* Obey some of the options. */
5741 if (print_search_dirs)
5743 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5744 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5745 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5746 return (0);
5749 if (print_file_name)
5751 printf ("%s\n", find_file (print_file_name));
5752 return (0);
5755 if (print_prog_name)
5757 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5758 printf ("%s\n", (newname ? newname : print_prog_name));
5759 return (0);
5762 if (print_multi_lib)
5764 print_multilib_info ();
5765 return (0);
5768 if (print_multi_directory)
5770 if (multilib_dir == NULL)
5771 printf (".\n");
5772 else
5773 printf ("%s\n", multilib_dir);
5774 return (0);
5777 if (target_help_flag)
5779 /* Print if any target specific options.*/
5781 /* We do not exit here. Instead we have created a fake input file
5782 called 'target-dummy' which needs to be compiled, and we pass this
5783 on to the various sub-processes, along with the --target-help
5784 switch. */
5787 if (print_help_list)
5789 display_help ();
5791 if (! verbose_flag)
5793 printf (_("\nFor bug reporting instructions, please see:\n"));
5794 printf ("%s.\n", GCCBUGURL);
5796 return (0);
5799 /* We do not exit here. Instead we have created a fake input file
5800 called 'help-dummy' which needs to be compiled, and we pass this
5801 on the the various sub-processes, along with the --help switch. */
5804 if (verbose_flag)
5806 int n;
5807 const char *thrmod;
5809 notice ("Configured with: %s\n", configuration_arguments);
5811 #ifdef THREAD_MODEL_SPEC
5812 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
5813 but there's no point in doing all this processing just to get
5814 thread_model back. */
5815 obstack_init (&obstack);
5816 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
5817 obstack_1grow (&obstack, '\0');
5818 thrmod = obstack_finish (&obstack);
5819 #else
5820 thrmod = thread_model;
5821 #endif
5823 notice ("Thread model: %s\n", thrmod);
5825 /* compiler_version is truncated at the first space when initialized
5826 from version string, so truncate version_string at the first space
5827 before comparing. */
5828 for (n = 0; version_string[n]; n++)
5829 if (version_string[n] == ' ')
5830 break;
5832 if (! strncmp (version_string, compiler_version, n)
5833 && compiler_version[n] == 0)
5834 notice ("gcc version %s\n", version_string);
5835 else
5836 notice ("gcc driver version %s executing gcc version %s\n",
5837 version_string, compiler_version);
5839 if (n_infiles == 0)
5840 return (0);
5843 if (n_infiles == added_libraries)
5844 fatal ("No input files");
5846 /* Make a place to record the compiler output file names
5847 that correspond to the input files. */
5849 i = n_infiles;
5850 i += lang_specific_extra_outfiles;
5851 outfiles = (const char **) xcalloc (i, sizeof (char *));
5853 /* Record which files were specified explicitly as link input. */
5855 explicit_link_files = xcalloc (1, n_infiles);
5857 for (i = 0; (int) i < n_infiles; i++)
5859 int this_file_error = 0;
5861 /* Tell do_spec what to substitute for %i. */
5863 input_file_number = i;
5864 set_input (infiles[i].name);
5866 /* Use the same thing in %o, unless cp->spec says otherwise. */
5868 outfiles[i] = input_filename;
5870 /* Figure out which compiler from the file's suffix. */
5872 input_file_compiler
5873 = lookup_compiler (infiles[i].name, input_filename_length,
5874 infiles[i].language);
5876 if (input_file_compiler)
5878 /* Ok, we found an applicable compiler. Run its spec. */
5880 if (input_file_compiler->spec[0] == '#')
5881 error ("%s: %s compiler not installed on this system",
5882 input_filename, &input_file_compiler->spec[1]);
5883 value = do_spec (input_file_compiler->spec);
5884 if (value < 0)
5885 this_file_error = 1;
5888 /* If this file's name does not contain a recognized suffix,
5889 record it as explicit linker input. */
5891 else
5892 explicit_link_files[i] = 1;
5894 /* Clear the delete-on-failure queue, deleting the files in it
5895 if this compilation failed. */
5897 if (this_file_error)
5899 delete_failure_queue ();
5900 error_count++;
5902 /* If this compilation succeeded, don't delete those files later. */
5903 clear_failure_queue ();
5906 /* Reset the output file name to the first input file name, for use
5907 with %b in LINK_SPEC on a target that prefers not to emit a.out
5908 by default. */
5909 if (n_infiles > 0)
5910 set_input (infiles[0].name);
5912 if (error_count == 0)
5914 /* Make sure INPUT_FILE_NUMBER points to first available open
5915 slot. */
5916 input_file_number = n_infiles;
5917 if (lang_specific_pre_link ())
5918 error_count++;
5921 /* Run ld to link all the compiler output files. */
5923 if (error_count == 0)
5925 int tmp = execution_count;
5927 /* We'll use ld if we can't find collect2. */
5928 if (! strcmp (linker_name_spec, "collect2"))
5930 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
5931 if (s == NULL)
5932 linker_name_spec = "ld";
5934 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
5935 for collect. */
5936 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
5937 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
5939 value = do_spec (link_command_spec);
5940 if (value < 0)
5941 error_count = 1;
5942 linker_was_run = (tmp != execution_count);
5945 /* If options said don't run linker,
5946 complain about input files to be given to the linker. */
5948 if (! linker_was_run && error_count == 0)
5949 for (i = 0; (int) i < n_infiles; i++)
5950 if (explicit_link_files[i])
5951 error ("%s: linker input file unused because linking not done",
5952 outfiles[i]);
5954 /* Delete some or all of the temporary files we made. */
5956 if (error_count)
5957 delete_failure_queue ();
5958 delete_temp_files ();
5960 if (print_help_list)
5962 printf (("\nFor bug reporting instructions, please see:\n"));
5963 printf ("%s\n", GCCBUGURL);
5966 return (signal_count != 0 ? 2
5967 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
5968 : 0);
5971 /* Find the proper compilation spec for the file name NAME,
5972 whose length is LENGTH. LANGUAGE is the specified language,
5973 or 0 if this file is to be passed to the linker. */
5975 static struct compiler *
5976 lookup_compiler (name, length, language)
5977 const char *name;
5978 size_t length;
5979 const char *language;
5981 struct compiler *cp;
5983 /* If this was specified by the user to be a linker input, indicate that. */
5984 if (language != 0 && language[0] == '*')
5985 return 0;
5987 /* Otherwise, look for the language, if one is spec'd. */
5988 if (language != 0)
5990 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5991 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
5992 return cp;
5994 error ("language %s not recognized", language);
5995 return 0;
5998 /* Look for a suffix. */
5999 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6001 if (/* The suffix `-' matches only the file name `-'. */
6002 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6003 || (strlen (cp->suffix) < length
6004 /* See if the suffix matches the end of NAME. */
6005 && !strcmp (cp->suffix,
6006 name + length - strlen (cp->suffix))
6008 break;
6011 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6012 /* look again, but case-insensitively this time. */
6013 if (cp < compilers)
6014 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6016 if (/* The suffix `-' matches only the file name `-'. */
6017 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6018 || (strlen (cp->suffix) < length
6019 /* See if the suffix matches the end of NAME. */
6020 && ((!strcmp (cp->suffix,
6021 name + length - strlen (cp->suffix))
6022 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6023 && !strcasecmp (cp->suffix,
6024 name + length - strlen (cp->suffix)))
6026 break;
6028 #endif
6030 if (cp >= compilers)
6032 if (cp->spec[0] != '@')
6033 /* A non-alias entry: return it. */
6034 return cp;
6036 /* An alias entry maps a suffix to a language.
6037 Search for the language; pass 0 for NAME and LENGTH
6038 to avoid infinite recursion if language not found. */
6039 return lookup_compiler (NULL_PTR, 0, cp->spec + 1);
6041 return 0;
6044 static char *
6045 save_string (s, len)
6046 const char *s;
6047 int len;
6049 register char *result = xmalloc (len + 1);
6051 memcpy (result, s, len);
6052 result[len] = 0;
6053 return result;
6056 void
6057 pfatal_with_name (name)
6058 const char *name;
6060 perror_with_name (name);
6061 delete_temp_files ();
6062 exit (1);
6065 static void
6066 perror_with_name (name)
6067 const char *name;
6069 error ("%s: %s", name, xstrerror (errno));
6072 static void
6073 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6074 const char *errmsg_fmt;
6075 const char *errmsg_arg;
6077 if (errmsg_arg)
6079 int save_errno = errno;
6081 /* Space for trailing '\0' is in %s. */
6082 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6083 sprintf (msg, errmsg_fmt, errmsg_arg);
6084 errmsg_fmt = msg;
6086 errno = save_errno;
6089 pfatal_with_name (errmsg_fmt);
6092 /* Output an error message and exit */
6094 void
6095 fancy_abort ()
6097 fatal ("Internal gcc abort.");
6100 /* Output an error message and exit */
6102 void
6103 fatal VPARAMS ((const char *msgid, ...))
6105 #ifndef ANSI_PROTOTYPES
6106 const char *msgid;
6107 #endif
6108 va_list ap;
6110 VA_START (ap, msgid);
6112 #ifndef ANSI_PROTOTYPES
6113 msgid = va_arg (ap, const char *);
6114 #endif
6116 fprintf (stderr, "%s: ", programname);
6117 vfprintf (stderr, _(msgid), ap);
6118 va_end (ap);
6119 fprintf (stderr, "\n");
6120 delete_temp_files ();
6121 exit (1);
6124 void
6125 error VPARAMS ((const char *msgid, ...))
6127 #ifndef ANSI_PROTOTYPES
6128 const char *msgid;
6129 #endif
6130 va_list ap;
6132 VA_START (ap, msgid);
6134 #ifndef ANSI_PROTOTYPES
6135 msgid = va_arg (ap, const char *);
6136 #endif
6138 fprintf (stderr, "%s: ", programname);
6139 vfprintf (stderr, _(msgid), ap);
6140 va_end (ap);
6142 fprintf (stderr, "\n");
6145 static void
6146 notice VPARAMS ((const char *msgid, ...))
6148 #ifndef ANSI_PROTOTYPES
6149 const char *msgid;
6150 #endif
6151 va_list ap;
6153 VA_START (ap, msgid);
6155 #ifndef ANSI_PROTOTYPES
6156 msgid = va_arg (ap, const char *);
6157 #endif
6159 vfprintf (stderr, _(msgid), ap);
6160 va_end (ap);
6163 static void
6164 validate_all_switches ()
6166 struct compiler *comp;
6167 register const char *p;
6168 register char c;
6169 struct spec_list *spec;
6171 for (comp = compilers; comp->spec; comp++)
6173 p = comp->spec;
6174 while ((c = *p++))
6175 if (c == '%' && *p == '{')
6176 /* We have a switch spec. */
6177 validate_switches (p + 1);
6180 /* Look through the linked list of specs read from the specs file. */
6181 for (spec = specs; spec; spec = spec->next)
6183 p = *(spec->ptr_spec);
6184 while ((c = *p++))
6185 if (c == '%' && *p == '{')
6186 /* We have a switch spec. */
6187 validate_switches (p + 1);
6190 p = link_command_spec;
6191 while ((c = *p++))
6192 if (c == '%' && *p == '{')
6193 /* We have a switch spec. */
6194 validate_switches (p + 1);
6197 /* Look at the switch-name that comes after START
6198 and mark as valid all supplied switches that match it. */
6200 static void
6201 validate_switches (start)
6202 const char *start;
6204 register const char *p = start;
6205 const char *filter;
6206 register int i;
6207 int suffix;
6209 if (*p == '|')
6210 ++p;
6212 next_member:
6213 if (*p == '!')
6214 ++p;
6216 suffix = 0;
6217 if (*p == '.')
6218 suffix = 1, ++p;
6220 filter = p;
6221 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6222 p++;
6224 if (suffix)
6226 else if (p[-1] == '*')
6228 /* Mark all matching switches as valid. */
6229 for (i = 0; i < n_switches; i++)
6230 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6231 switches[i].validated = 1;
6233 else
6235 /* Mark an exact matching switch as valid. */
6236 for (i = 0; i < n_switches; i++)
6238 if (!strncmp (switches[i].part1, filter, p - filter)
6239 && switches[i].part1[p - filter] == 0)
6240 switches[i].validated = 1;
6244 if (*p++ == '|' || p[-1] == '&')
6245 goto next_member;
6248 /* Check whether a particular argument was used. The first time we
6249 canonicalize the switches to keep only the ones we care about. */
6251 static int
6252 used_arg (p, len)
6253 const char *p;
6254 int len;
6256 struct mswitchstr
6258 const char *str;
6259 const char *replace;
6260 int len;
6261 int rep_len;
6264 static struct mswitchstr *mswitches;
6265 static int n_mswitches;
6266 int i, j;
6268 if (!mswitches)
6270 struct mswitchstr *matches;
6271 const char *q;
6272 int cnt = 0;
6274 /* Break multilib_matches into the component strings of string
6275 and replacement string. */
6276 for (q = multilib_matches; *q != '\0'; q++)
6277 if (*q == ';')
6278 cnt++;
6280 matches =
6281 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6282 i = 0;
6283 q = multilib_matches;
6284 while (*q != '\0')
6286 matches[i].str = q;
6287 while (*q != ' ')
6289 if (*q == '\0')
6290 abort ();
6291 q++;
6293 matches[i].len = q - matches[i].str;
6295 matches[i].replace = ++q;
6296 while (*q != ';' && *q != '\0')
6298 if (*q == ' ')
6299 abort ();
6300 q++;
6302 matches[i].rep_len = q - matches[i].replace;
6303 i++;
6304 if (*q == ';')
6305 q++;
6308 /* Now build a list of the replacement string for switches that we care
6309 about. Make sure we allocate at least one entry. This prevents
6310 xmalloc from calling fatal, and prevents us from re-executing this
6311 block of code. */
6312 mswitches
6313 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6314 * (n_switches ? n_switches : 1));
6315 for (i = 0; i < n_switches; i++)
6317 int xlen = strlen (switches[i].part1);
6318 for (j = 0; j < cnt; j++)
6319 if (xlen == matches[j].len
6320 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6322 mswitches[n_mswitches].str = matches[j].replace;
6323 mswitches[n_mswitches].len = matches[j].rep_len;
6324 mswitches[n_mswitches].replace = (char *) 0;
6325 mswitches[n_mswitches].rep_len = 0;
6326 n_mswitches++;
6327 break;
6332 for (i = 0; i < n_mswitches; i++)
6333 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6334 return 1;
6336 return 0;
6339 static int
6340 default_arg (p, len)
6341 const char *p;
6342 int len;
6344 const char *start, *end;
6346 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6348 while (*start == ' ' || *start == '\t')
6349 start++;
6351 if (*start == '\0')
6352 break;
6354 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6357 if ((end - start) == len && strncmp (p, start, len) == 0)
6358 return 1;
6360 if (*end == '\0')
6361 break;
6364 return 0;
6367 /* Work out the subdirectory to use based on the options. The format of
6368 multilib_select is a list of elements. Each element is a subdirectory
6369 name followed by a list of options followed by a semicolon. The format
6370 of multilib_exclusions is the same, but without the preceding
6371 directory. First gcc will check the exclusions, if none of the options
6372 beginning with an exclamation point are present, and all of the other
6373 options are present, then we will ignore this completely. Passing
6374 that, gcc will consider each multilib_select in turn using the same
6375 rules for matching the options. If a match is found, that subdirectory
6376 will be used. */
6378 static void
6379 set_multilib_dir ()
6381 const char *p;
6382 unsigned int this_path_len;
6383 const char *this_path, *this_arg;
6384 int not_arg;
6385 int ok;
6387 p = multilib_exclusions;
6388 while (*p != '\0')
6390 /* Ignore newlines. */
6391 if (*p == '\n')
6393 ++p;
6394 continue;
6397 /* Check the arguments. */
6398 ok = 1;
6399 while (*p != ';')
6401 if (*p == '\0')
6402 abort ();
6404 if (! ok)
6406 ++p;
6407 continue;
6410 this_arg = p;
6411 while (*p != ' ' && *p != ';')
6413 if (*p == '\0')
6414 abort ();
6415 ++p;
6418 if (*this_arg != '!')
6419 not_arg = 0;
6420 else
6422 not_arg = 1;
6423 ++this_arg;
6426 ok = used_arg (this_arg, p - this_arg);
6427 if (not_arg)
6428 ok = ! ok;
6430 if (*p == ' ')
6431 ++p;
6434 if (ok)
6435 return;
6437 ++p;
6440 p = multilib_select;
6441 while (*p != '\0')
6443 /* Ignore newlines. */
6444 if (*p == '\n')
6446 ++p;
6447 continue;
6450 /* Get the initial path. */
6451 this_path = p;
6452 while (*p != ' ')
6454 if (*p == '\0')
6455 abort ();
6456 ++p;
6458 this_path_len = p - this_path;
6460 /* Check the arguments. */
6461 ok = 1;
6462 ++p;
6463 while (*p != ';')
6465 if (*p == '\0')
6466 abort ();
6468 if (! ok)
6470 ++p;
6471 continue;
6474 this_arg = p;
6475 while (*p != ' ' && *p != ';')
6477 if (*p == '\0')
6478 abort ();
6479 ++p;
6482 if (*this_arg != '!')
6483 not_arg = 0;
6484 else
6486 not_arg = 1;
6487 ++this_arg;
6490 /* If this is a default argument, we can just ignore it.
6491 This is true even if this_arg begins with '!'. Beginning
6492 with '!' does not mean that this argument is necessarily
6493 inappropriate for this library: it merely means that
6494 there is a more specific library which uses this
6495 argument. If this argument is a default, we need not
6496 consider that more specific library. */
6497 if (! default_arg (this_arg, p - this_arg))
6499 ok = used_arg (this_arg, p - this_arg);
6500 if (not_arg)
6501 ok = ! ok;
6504 if (*p == ' ')
6505 ++p;
6508 if (ok)
6510 if (this_path_len != 1
6511 || this_path[0] != '.')
6513 char *new_multilib_dir = xmalloc (this_path_len + 1);
6514 strncpy (new_multilib_dir, this_path, this_path_len);
6515 new_multilib_dir[this_path_len] = '\0';
6516 multilib_dir = new_multilib_dir;
6518 break;
6521 ++p;
6525 /* Print out the multiple library subdirectory selection
6526 information. This prints out a series of lines. Each line looks
6527 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6528 required. Only the desired options are printed out, the negative
6529 matches. The options are print without a leading dash. There are
6530 no spaces to make it easy to use the information in the shell.
6531 Each subdirectory is printed only once. This assumes the ordering
6532 generated by the genmultilib script. Also, we leave out ones that match
6533 the exclusions. */
6535 static void
6536 print_multilib_info ()
6538 const char *p = multilib_select;
6539 const char *last_path = 0, *this_path;
6540 int skip;
6541 unsigned int last_path_len = 0;
6543 while (*p != '\0')
6545 skip = 0;
6546 /* Ignore newlines. */
6547 if (*p == '\n')
6549 ++p;
6550 continue;
6553 /* Get the initial path. */
6554 this_path = p;
6555 while (*p != ' ')
6557 if (*p == '\0')
6558 abort ();
6559 ++p;
6562 /* Check for matches with the multilib_exclusions. We don't bother
6563 with the '!' in either list. If any of the exclusion rules match
6564 all of its options with the select rule, we skip it. */
6566 const char *e = multilib_exclusions;
6567 const char *this_arg;
6569 while (*e != '\0')
6571 int m = 1;
6572 /* Ignore newlines. */
6573 if (*e == '\n')
6575 ++e;
6576 continue;
6579 /* Check the arguments. */
6580 while (*e != ';')
6582 const char *q;
6583 int mp = 0;
6585 if (*e == '\0')
6586 abort ();
6588 if (! m)
6590 ++e;
6591 continue;
6594 this_arg = e;
6596 while (*e != ' ' && *e != ';')
6598 if (*e == '\0')
6599 abort ();
6600 ++e;
6603 q = p + 1;
6604 while (*q != ';')
6606 const char *arg;
6607 int len = e - this_arg;
6609 if (*q == '\0')
6610 abort ();
6612 arg = q;
6614 while (*q != ' ' && *q != ';')
6616 if (*q == '\0')
6617 abort ();
6618 ++q;
6621 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6622 default_arg (this_arg, e - this_arg))
6624 mp = 1;
6625 break;
6628 if (*q == ' ')
6629 ++q;
6632 if (! mp)
6633 m = 0;
6635 if (*e == ' ')
6636 ++e;
6639 if (m)
6641 skip = 1;
6642 break;
6645 if (*e != '\0')
6646 ++e;
6650 if (! skip)
6652 /* If this is a duplicate, skip it. */
6653 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6654 && ! strncmp (last_path, this_path, last_path_len));
6656 last_path = this_path;
6657 last_path_len = p - this_path;
6660 /* If this directory requires any default arguments, we can skip
6661 it. We will already have printed a directory identical to
6662 this one which does not require that default argument. */
6663 if (! skip)
6665 const char *q;
6667 q = p + 1;
6668 while (*q != ';')
6670 const char *arg;
6672 if (*q == '\0')
6673 abort ();
6675 if (*q == '!')
6676 arg = NULL;
6677 else
6678 arg = q;
6680 while (*q != ' ' && *q != ';')
6682 if (*q == '\0')
6683 abort ();
6684 ++q;
6687 if (arg != NULL
6688 && default_arg (arg, q - arg))
6690 skip = 1;
6691 break;
6694 if (*q == ' ')
6695 ++q;
6699 if (! skip)
6701 const char *p1;
6703 for (p1 = last_path; p1 < p; p1++)
6704 putchar (*p1);
6705 putchar (';');
6708 ++p;
6709 while (*p != ';')
6711 int use_arg;
6713 if (*p == '\0')
6714 abort ();
6716 if (skip)
6718 ++p;
6719 continue;
6722 use_arg = *p != '!';
6724 if (use_arg)
6725 putchar ('@');
6727 while (*p != ' ' && *p != ';')
6729 if (*p == '\0')
6730 abort ();
6731 if (use_arg)
6732 putchar (*p);
6733 ++p;
6736 if (*p == ' ')
6737 ++p;
6740 if (! skip)
6742 /* If there are extra options, print them now. */
6743 if (multilib_extra && *multilib_extra)
6745 int print_at = TRUE;
6746 const char *q;
6748 for (q = multilib_extra; *q != '\0'; q++)
6750 if (*q == ' ')
6751 print_at = TRUE;
6752 else
6754 if (print_at)
6755 putchar ('@');
6756 putchar (*q);
6757 print_at = FALSE;
6762 putchar ('\n');
6765 ++p;