* expr.c (expand_expr, case ARRAY_REF): Do not replace
[official-gcc.git] / gcc / gcc.c
blobe61595476980406fba7794c27eec99b6a252f755
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 %{fno-inline|O0|!O*:-D__NO_INLINE__} %{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 %s}", shared_name, static_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 %s}}}",
1286 shared_name, static_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
4294 require one. */
4295 if (t == 0 || c == 'u' || c == 'j')
4297 if (t == 0)
4299 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4300 t->next = temp_names;
4301 temp_names = t;
4303 t->length = suffix_length;
4304 if (saved_suffix)
4306 t->suffix = saved_suffix;
4307 saved_suffix = NULL;
4309 else
4310 t->suffix = save_string (suffix, suffix_length);
4311 t->unique = (c != 'g');
4312 temp_filename = make_temp_file (t->suffix);
4313 temp_filename_length = strlen (temp_filename);
4314 t->filename = temp_filename;
4315 t->filename_length = temp_filename_length;
4318 if (saved_suffix)
4319 free (saved_suffix);
4321 obstack_grow (&obstack, t->filename, t->filename_length);
4322 delete_this_arg = 1;
4324 arg_going = 1;
4325 break;
4327 case 'i':
4328 obstack_grow (&obstack, input_filename, input_filename_length);
4329 arg_going = 1;
4330 break;
4332 case 'I':
4334 struct prefix_list *pl = include_prefixes.plist;
4336 if (gcc_exec_prefix)
4338 do_spec_1 ("-iprefix", 1, NULL_PTR);
4339 /* Make this a separate argument. */
4340 do_spec_1 (" ", 0, NULL_PTR);
4341 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
4342 do_spec_1 (" ", 0, NULL_PTR);
4345 for (; pl; pl = pl->next)
4347 do_spec_1 ("-isystem", 1, NULL_PTR);
4348 /* Make this a separate argument. */
4349 do_spec_1 (" ", 0, NULL_PTR);
4350 do_spec_1 (pl->prefix, 1, NULL_PTR);
4351 do_spec_1 (" ", 0, NULL_PTR);
4354 break;
4356 case 'o':
4358 int max = n_infiles;
4359 max += lang_specific_extra_outfiles;
4361 for (i = 0; i < max; i++)
4362 if (outfiles[i])
4363 store_arg (outfiles[i], 0, 0);
4364 break;
4367 case 'O':
4368 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
4369 arg_going = 1;
4370 break;
4372 case 's':
4373 this_is_library_file = 1;
4374 break;
4376 case 'w':
4377 this_is_output_file = 1;
4378 break;
4380 case 'W':
4382 int cur_index = argbuf_index;
4383 /* Handle the {...} following the %W. */
4384 if (*p != '{')
4385 abort ();
4386 p = handle_braces (p + 1);
4387 if (p == 0)
4388 return -1;
4389 /* If any args were output, mark the last one for deletion
4390 on failure. */
4391 if (argbuf_index != cur_index)
4392 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4393 break;
4396 /* %x{OPTION} records OPTION for %X to output. */
4397 case 'x':
4399 const char *p1 = p;
4400 char *string;
4402 /* Skip past the option value and make a copy. */
4403 if (*p != '{')
4404 abort ();
4405 while (*p++ != '}')
4407 string = save_string (p1 + 1, p - p1 - 2);
4409 /* See if we already recorded this option. */
4410 for (i = 0; i < n_linker_options; i++)
4411 if (! strcmp (string, linker_options[i]))
4413 free (string);
4414 return 0;
4417 /* This option is new; add it. */
4418 add_linker_option (string, strlen (string));
4420 break;
4422 /* Dump out the options accumulated previously using %x. */
4423 case 'X':
4424 for (i = 0; i < n_linker_options; i++)
4426 do_spec_1 (linker_options[i], 1, NULL_PTR);
4427 /* Make each accumulated option a separate argument. */
4428 do_spec_1 (" ", 0, NULL_PTR);
4430 break;
4432 /* Dump out the options accumulated previously using -Wa,. */
4433 case 'Y':
4434 for (i = 0; i < n_assembler_options; i++)
4436 do_spec_1 (assembler_options[i], 1, NULL_PTR);
4437 /* Make each accumulated option a separate argument. */
4438 do_spec_1 (" ", 0, NULL_PTR);
4440 break;
4442 /* Dump out the options accumulated previously using -Wp,. */
4443 case 'Z':
4444 for (i = 0; i < n_preprocessor_options; i++)
4446 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
4447 /* Make each accumulated option a separate argument. */
4448 do_spec_1 (" ", 0, NULL_PTR);
4450 break;
4452 /* Here are digits and numbers that just process
4453 a certain constant string as a spec. */
4455 case '1':
4456 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
4457 if (value != 0)
4458 return value;
4459 break;
4461 case '2':
4462 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
4463 if (value != 0)
4464 return value;
4465 break;
4467 case 'a':
4468 value = do_spec_1 (asm_spec, 0, NULL_PTR);
4469 if (value != 0)
4470 return value;
4471 break;
4473 case 'A':
4474 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
4475 if (value != 0)
4476 return value;
4477 break;
4479 case 'c':
4480 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
4481 if (value != 0)
4482 return value;
4483 break;
4485 case 'C':
4487 const char* spec
4488 = (input_file_compiler->cpp_spec
4489 ? input_file_compiler->cpp_spec
4490 : cpp_spec);
4491 value = do_spec_1 (spec, 0, NULL_PTR);
4492 if (value != 0)
4493 return value;
4495 break;
4497 case 'E':
4498 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
4499 if (value != 0)
4500 return value;
4501 break;
4503 case 'l':
4504 value = do_spec_1 (link_spec, 0, NULL_PTR);
4505 if (value != 0)
4506 return value;
4507 break;
4509 case 'L':
4510 value = do_spec_1 (lib_spec, 0, NULL_PTR);
4511 if (value != 0)
4512 return value;
4513 break;
4515 case 'G':
4516 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
4517 if (value != 0)
4518 return value;
4519 break;
4521 case 'M':
4522 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4524 char *p;
4525 const char *q;
4526 size_t len;
4528 len = strlen (multilib_dir);
4529 obstack_blank (&obstack, len + 1);
4530 p = obstack_next_free (&obstack) - (len + 1);
4532 *p++ = '_';
4533 for (q = multilib_dir; *q ; ++q, ++p)
4534 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4536 break;
4538 case 'p':
4540 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4541 char *buf = x;
4542 const char *y;
4544 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4545 y = cpp_predefines;
4546 while (*y != 0)
4548 if (! strncmp (y, "-D", 2))
4549 /* Copy the whole option. */
4550 while (*y && *y != ' ' && *y != '\t')
4551 *x++ = *y++;
4552 else if (*y == ' ' || *y == '\t')
4553 /* Copy whitespace to the result. */
4554 *x++ = *y++;
4555 /* Don't copy other options. */
4556 else
4557 y++;
4560 *x = 0;
4562 value = do_spec_1 (buf, 0, NULL_PTR);
4563 if (value != 0)
4564 return value;
4566 break;
4568 case 'P':
4570 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4571 char *buf = x;
4572 const char *y;
4574 /* Copy all of CPP_PREDEFINES into BUF,
4575 but force them all into the reserved name space if they
4576 aren't already there. The reserved name space is all
4577 identifiers beginning with two underscores or with one
4578 underscore and a capital letter. We do the forcing by
4579 adding up to two underscores to the beginning and end
4580 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4581 become __mips__. */
4582 y = cpp_predefines;
4583 while (*y != 0)
4585 if (! strncmp (y, "-D", 2))
4587 int flag = 0;
4589 *x++ = *y++;
4590 *x++ = *y++;
4592 if (*y != '_'
4593 || (*(y + 1) != '_'
4594 && ! ISUPPER ((unsigned char) *(y + 1))))
4596 /* Stick __ at front of macro name. */
4597 if (*y != '_')
4598 *x++ = '_';
4599 *x++ = '_';
4600 /* Arrange to stick __ at the end as well. */
4601 flag = 1;
4604 /* Copy the macro name. */
4605 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4606 *x++ = *y++;
4608 if (flag)
4610 if (x[-1] != '_')
4612 if (x[-2] != '_')
4613 *x++ = '_';
4614 *x++ = '_';
4618 /* Copy the value given, if any. */
4619 while (*y && *y != ' ' && *y != '\t')
4620 *x++ = *y++;
4622 else if (*y == ' ' || *y == '\t')
4623 /* Copy whitespace to the result. */
4624 *x++ = *y++;
4625 /* Don't copy -A options */
4626 else
4627 y++;
4629 *x++ = ' ';
4631 /* Copy all of CPP_PREDEFINES into BUF,
4632 but put __ after every -D. */
4633 y = cpp_predefines;
4634 while (*y != 0)
4636 if (! strncmp (y, "-D", 2))
4638 y += 2;
4640 if (*y != '_'
4641 || (*(y + 1) != '_'
4642 && ! ISUPPER ((unsigned char) *(y + 1))))
4644 /* Stick -D__ at front of macro name. */
4645 *x++ = '-';
4646 *x++ = 'D';
4647 if (*y != '_')
4648 *x++ = '_';
4649 *x++ = '_';
4651 /* Copy the macro name. */
4652 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4653 *x++ = *y++;
4655 /* Copy the value given, if any. */
4656 while (*y && *y != ' ' && *y != '\t')
4657 *x++ = *y++;
4659 else
4661 /* Do not copy this macro - we have just done it before */
4662 while (*y && *y != ' ' && *y != '\t')
4663 y++;
4666 else if (*y == ' ' || *y == '\t')
4667 /* Copy whitespace to the result. */
4668 *x++ = *y++;
4669 /* Don't copy -A options. */
4670 else
4671 y++;
4673 *x++ = ' ';
4675 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4676 y = cpp_predefines;
4677 while (*y != 0)
4679 if (! strncmp (y, "-A", 2))
4680 /* Copy the whole option. */
4681 while (*y && *y != ' ' && *y != '\t')
4682 *x++ = *y++;
4683 else if (*y == ' ' || *y == '\t')
4684 /* Copy whitespace to the result. */
4685 *x++ = *y++;
4686 /* Don't copy other options. */
4687 else
4688 y++;
4691 *x = 0;
4693 value = do_spec_1 (buf, 0, NULL_PTR);
4694 if (value != 0)
4695 return value;
4697 break;
4699 case 'S':
4700 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
4701 if (value != 0)
4702 return value;
4703 break;
4705 /* Here we define characters other than letters and digits. */
4707 case '{':
4708 p = handle_braces (p);
4709 if (p == 0)
4710 return -1;
4711 break;
4713 case '%':
4714 obstack_1grow (&obstack, '%');
4715 break;
4717 case '.':
4719 unsigned len = 0;
4721 while (p[len] && p[len] != ' ' && p[len] != '%')
4722 len++;
4723 suffix_subst = save_string (p - 1, len + 1);
4724 p += len;
4726 break;
4728 case '*':
4729 if (soft_matched_part)
4731 do_spec_1 (soft_matched_part, 1, NULL_PTR);
4732 do_spec_1 (" ", 0, NULL_PTR);
4734 else
4735 /* Catch the case where a spec string contains something like
4736 '%{foo:%*}'. ie there is no * in the pattern on the left
4737 hand side of the :. */
4738 error ("Spec failure: '%%*' has not been initialised by pattern match");
4739 break;
4741 /* Process a string found as the value of a spec given by name.
4742 This feature allows individual machine descriptions
4743 to add and use their own specs.
4744 %[...] modifies -D options the way %P does;
4745 %(...) uses the spec unmodified. */
4746 case '[':
4747 error ("Warning: use of obsolete %%[ operator in specs");
4748 case '(':
4750 const char *name = p;
4751 struct spec_list *sl;
4752 int len;
4754 /* The string after the S/P is the name of a spec that is to be
4755 processed. */
4756 while (*p && *p != ')' && *p != ']')
4757 p++;
4759 /* See if it's in the list. */
4760 for (len = p - name, sl = specs; sl; sl = sl->next)
4761 if (sl->name_len == len && !strncmp (sl->name, name, len))
4763 name = *(sl->ptr_spec);
4764 #ifdef DEBUG_SPECS
4765 notice ("Processing spec %c%s%c, which is '%s'\n",
4766 c, sl->name, (c == '(') ? ')' : ']', name);
4767 #endif
4768 break;
4771 if (sl)
4773 if (c == '(')
4775 value = do_spec_1 (name, 0, NULL_PTR);
4776 if (value != 0)
4777 return value;
4779 else
4781 char *x = (char *) alloca (strlen (name) * 2 + 1);
4782 char *buf = x;
4783 const char *y = name;
4784 int flag = 0;
4786 /* Copy all of NAME into BUF, but put __ after
4787 every -D and at the end of each arg. */
4788 while (1)
4790 if (! strncmp (y, "-D", 2))
4792 *x++ = '-';
4793 *x++ = 'D';
4794 *x++ = '_';
4795 *x++ = '_';
4796 y += 2;
4797 flag = 1;
4798 continue;
4800 else if (flag
4801 && (*y == ' ' || *y == '\t' || *y == '='
4802 || *y == '}' || *y == 0))
4804 *x++ = '_';
4805 *x++ = '_';
4806 flag = 0;
4808 if (*y == 0)
4809 break;
4810 else
4811 *x++ = *y++;
4813 *x = 0;
4815 value = do_spec_1 (buf, 0, NULL_PTR);
4816 if (value != 0)
4817 return value;
4821 /* Discard the closing paren or bracket. */
4822 if (*p)
4823 p++;
4825 break;
4827 case 'v':
4829 int c1 = *p++; /* Select first or second version number. */
4830 const char *v = compiler_version;
4831 const char *q;
4832 static const char zeroc = '0';
4834 /* The format of the version string is
4835 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
4837 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
4838 while (! ISDIGIT (*v))
4839 v++;
4840 if (v > compiler_version && v[-1] != '-')
4841 abort ();
4843 /* If desired, advance to second version number. */
4844 if (c1 >= '2')
4846 /* Set V after the first period. */
4847 while (ISDIGIT (*v))
4848 v++;
4849 if (*v != '.')
4850 abort ();
4851 v++;
4854 /* If desired, advance to third version number.
4855 But don't complain if it's not present */
4856 if (c1 == '3')
4858 /* Set V after the second period. */
4859 while (ISDIGIT (*v))
4860 v++;
4861 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
4862 abort ();
4863 if (*v != 0)
4864 v++;
4867 /* Set Q at the next period or at the end. */
4868 q = v;
4869 while (ISDIGIT (*q))
4870 q++;
4871 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
4872 abort ();
4874 if (q > v)
4875 /* Put that part into the command. */
4876 obstack_grow (&obstack, v, q - v);
4877 else
4878 /* Default to "0" */
4879 obstack_grow (&obstack, &zeroc, 1);
4880 arg_going = 1;
4882 break;
4884 case '|':
4885 if (input_from_pipe)
4886 do_spec_1 ("-", 0, NULL_PTR);
4887 break;
4889 default:
4890 error ("Spec failure: Unrecognised spec option '%c'", c);
4891 break;
4893 break;
4895 case '\\':
4896 /* Backslash: treat next character as ordinary. */
4897 c = *p++;
4899 /* fall through */
4900 default:
4901 /* Ordinary character: put it into the current argument. */
4902 obstack_1grow (&obstack, c);
4903 arg_going = 1;
4906 /* End of string. */
4907 return 0;
4910 /* Return 0 if we call do_spec_1 and that returns -1. */
4912 static const char *
4913 handle_braces (p)
4914 register const char *p;
4916 const char *filter, *body = NULL, *endbody = NULL;
4917 int pipe_p = 0;
4918 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
4919 int negate;
4920 int suffix;
4921 int include_blanks = 1;
4922 int elide_switch = 0;
4923 int ordered = 0;
4925 if (*p == '^')
4927 /* A '^' after the open-brace means to not give blanks before args. */
4928 include_blanks = 0;
4929 ++p;
4932 if (*p == '|')
4934 /* A `|' after the open-brace means,
4935 if the test fails, output a single minus sign rather than nothing.
4936 This is used in %{|!pipe:...}. */
4937 pipe_p = 1;
4938 ++p;
4941 if (*p == '<')
4943 /* A `<' after the open-brace means that the switch should be
4944 removed from the command-line. */
4945 elide_switch = 1;
4946 ++p;
4949 next_member:
4950 negate = suffix = 0;
4952 if (*p == '!')
4953 /* A `!' after the open-brace negates the condition:
4954 succeed if the specified switch is not present. */
4955 negate = 1, ++p;
4957 if (*p == '.')
4958 /* A `.' after the open-brace means test against the current suffix. */
4960 if (pipe_p)
4961 abort ();
4963 suffix = 1;
4964 ++p;
4967 if (elide_switch && (negate || pipe_p || suffix))
4969 /* It doesn't make sense to mix elision with other flags. We
4970 could fatal() here, but the standard seems to be to abort. */
4971 abort ();
4974 next_ampersand:
4975 filter = p;
4976 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
4977 p++;
4979 if (*p == '|' && (pipe_p || ordered))
4980 abort ();
4982 if (!body)
4984 if (*p != '}' && *p != '&')
4986 register int count = 1;
4987 register const char *q = p;
4989 while (*q++ != ':')
4990 continue;
4991 body = q;
4993 while (count > 0)
4995 if (*q == '{')
4996 count++;
4997 else if (*q == '}')
4998 count--;
4999 else if (*q == 0)
5000 abort ();
5001 q++;
5003 endbody = q;
5005 else
5006 body = p, endbody = p + 1;
5009 if (suffix)
5011 int found = (input_suffix != 0
5012 && (long) strlen (input_suffix) == (long) (p - filter)
5013 && strncmp (input_suffix, filter, p - filter) == 0);
5015 if (body[0] == '}')
5016 abort ();
5018 if (negate != found
5019 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL_PTR) < 0)
5020 return 0;
5022 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5024 /* Substitute all matching switches as separate args. */
5025 register int i;
5027 for (i = 0; i < n_switches; i++)
5028 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5029 && check_live_switch (i, p - 1 - filter))
5031 if (elide_switch)
5033 switches[i].live_cond = SWITCH_IGNORE;
5034 switches[i].validated = 1;
5036 else
5037 ordered = 1, switches[i].ordering = 1;
5040 else
5042 /* Test for presence of the specified switch. */
5043 register int i;
5044 int present = 0;
5046 /* If name specified ends in *, as in {x*:...},
5047 check for %* and handle that case. */
5048 if (p[-1] == '*' && !negate)
5050 int substitution;
5051 const char *r = body;
5053 /* First see whether we have %*. */
5054 substitution = 0;
5055 while (r < endbody)
5057 if (*r == '%' && r[1] == '*')
5058 substitution = 1;
5059 r++;
5061 /* If we do, handle that case. */
5062 if (substitution)
5064 /* Substitute all matching switches as separate args.
5065 But do this by substituting for %*
5066 in the text that follows the colon. */
5068 unsigned hard_match_len = p - filter - 1;
5069 char *string = save_string (body, endbody - body - 1);
5071 for (i = 0; i < n_switches; i++)
5072 if (!strncmp (switches[i].part1, filter, hard_match_len)
5073 && check_live_switch (i, -1))
5075 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5076 /* Pass any arguments this switch has. */
5077 give_switch (i, 1, 1);
5078 suffix_subst = NULL;
5081 /* We didn't match. Try again. */
5082 if (*p++ == '|')
5083 goto next_member;
5084 return endbody;
5088 /* If name specified ends in *, as in {x*:...},
5089 check for presence of any switch name starting with x. */
5090 if (p[-1] == '*')
5092 for (i = 0; i < n_switches; i++)
5094 unsigned hard_match_len = p - filter - 1;
5096 if (!strncmp (switches[i].part1, filter, hard_match_len)
5097 && check_live_switch (i, hard_match_len))
5099 present = 1;
5100 break;
5104 /* Otherwise, check for presence of exact name specified. */
5105 else
5107 for (i = 0; i < n_switches; i++)
5109 if (!strncmp (switches[i].part1, filter, p - filter)
5110 && switches[i].part1[p - filter] == 0
5111 && check_live_switch (i, -1))
5113 present = 1;
5114 break;
5119 /* If it is as desired (present for %{s...}, absent for %{!s...})
5120 then substitute either the switch or the specified
5121 conditional text. */
5122 if (present != negate)
5124 if (elide_switch)
5126 switches[i].live_cond = SWITCH_IGNORE;
5127 switches[i].validated = 1;
5129 else if (ordered || *p == '&')
5130 ordered = 1, switches[i].ordering = 1;
5131 else if (*p == '}')
5132 give_switch (i, 0, include_blanks);
5133 else
5134 /* Even if many alternatives are matched, only output once. */
5135 true_once = 1;
5137 else if (pipe_p)
5139 /* Here if a %{|...} conditional fails: output a minus sign,
5140 which means "standard output" or "standard input". */
5141 do_spec_1 ("-", 0, NULL_PTR);
5142 return endbody;
5146 /* We didn't match; try again. */
5147 if (*p++ == '|')
5148 goto next_member;
5150 if (p[-1] == '&')
5152 body = 0;
5153 goto next_ampersand;
5156 if (ordered)
5158 int i;
5159 /* Doing this set of switches later preserves their command-line
5160 ordering. This is needed for e.g. -U, -D and -A. */
5161 for (i = 0; i < n_switches; i++)
5162 if (switches[i].ordering == 1)
5164 switches[i].ordering = 0;
5165 give_switch (i, 0, include_blanks);
5168 /* Process the spec just once, regardless of match count. */
5169 else if (true_once)
5171 if (do_spec_1 (save_string (body, endbody - body - 1),
5172 0, NULL_PTR) < 0)
5173 return 0;
5176 return endbody;
5179 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5180 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5181 spec, or -1 if either exact match or %* is used.
5183 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5184 whose value does not begin with "no-" is obsoleted by the same value
5185 with the "no-", similarly for a switch with the "no-" prefix. */
5187 static int
5188 check_live_switch (switchnum, prefix_length)
5189 int switchnum;
5190 int prefix_length;
5192 const char *name = switches[switchnum].part1;
5193 int i;
5195 /* In the common case of {<at-most-one-letter>*}, a negating
5196 switch would always match, so ignore that case. We will just
5197 send the conflicting switches to the compiler phase. */
5198 if (prefix_length >= 0 && prefix_length <= 1)
5199 return 1;
5201 /* If we already processed this switch and determined if it was
5202 live or not, return our past determination. */
5203 if (switches[switchnum].live_cond != 0)
5204 return switches[switchnum].live_cond > 0;
5206 /* Now search for duplicate in a manner that depends on the name. */
5207 switch (*name)
5209 case 'O':
5210 for (i = switchnum + 1; i < n_switches; i++)
5211 if (switches[i].part1[0] == 'O')
5213 switches[switchnum].validated = 1;
5214 switches[switchnum].live_cond = SWITCH_FALSE;
5215 return 0;
5217 break;
5219 case 'W': case 'f': case 'm':
5220 if (! strncmp (name + 1, "no-", 3))
5222 /* We have Xno-YYY, search for XYYY. */
5223 for (i = switchnum + 1; i < n_switches; i++)
5224 if (switches[i].part1[0] == name[0]
5225 && ! strcmp (&switches[i].part1[1], &name[4]))
5227 switches[switchnum].validated = 1;
5228 switches[switchnum].live_cond = SWITCH_FALSE;
5229 return 0;
5232 else
5234 /* We have XYYY, search for Xno-YYY. */
5235 for (i = switchnum + 1; i < n_switches; i++)
5236 if (switches[i].part1[0] == name[0]
5237 && switches[i].part1[1] == 'n'
5238 && switches[i].part1[2] == 'o'
5239 && switches[i].part1[3] == '-'
5240 && !strcmp (&switches[i].part1[4], &name[1]))
5242 switches[switchnum].validated = 1;
5243 switches[switchnum].live_cond = SWITCH_FALSE;
5244 return 0;
5247 break;
5250 /* Otherwise the switch is live. */
5251 switches[switchnum].live_cond = SWITCH_LIVE;
5252 return 1;
5255 /* Pass a switch to the current accumulating command
5256 in the same form that we received it.
5257 SWITCHNUM identifies the switch; it is an index into
5258 the vector of switches gcc received, which is `switches'.
5259 This cannot fail since it never finishes a command line.
5261 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5263 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5264 of the switch. */
5266 static void
5267 give_switch (switchnum, omit_first_word, include_blanks)
5268 int switchnum;
5269 int omit_first_word;
5270 int include_blanks;
5272 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5273 return;
5275 if (!omit_first_word)
5277 do_spec_1 ("-", 0, NULL_PTR);
5278 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
5281 if (switches[switchnum].args != 0)
5283 const char **p;
5284 for (p = switches[switchnum].args; *p; p++)
5286 const char *arg = *p;
5288 if (include_blanks)
5289 do_spec_1 (" ", 0, NULL_PTR);
5290 if (suffix_subst)
5292 unsigned length = strlen (arg);
5293 int dot = 0;
5295 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5296 if (arg[length] == '.')
5298 ((char *)arg)[length] = 0;
5299 dot = 1;
5300 break;
5302 do_spec_1 (arg, 1, NULL_PTR);
5303 if (dot)
5304 ((char *)arg)[length] = '.';
5305 do_spec_1 (suffix_subst, 1, NULL_PTR);
5307 else
5308 do_spec_1 (arg, 1, NULL_PTR);
5312 do_spec_1 (" ", 0, NULL_PTR);
5313 switches[switchnum].validated = 1;
5316 /* Search for a file named NAME trying various prefixes including the
5317 user's -B prefix and some standard ones.
5318 Return the absolute file name found. If nothing is found, return NAME. */
5320 static const char *
5321 find_file (name)
5322 const char *name;
5324 char *newname;
5326 /* Try multilib_dir if it is defined. */
5327 if (multilib_dir != NULL)
5329 char *try;
5331 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
5332 strcpy (try, multilib_dir);
5333 strcat (try, dir_separator_str);
5334 strcat (try, name);
5336 newname = find_a_file (&startfile_prefixes, try, R_OK);
5338 /* If we don't find it in the multi library dir, then fall
5339 through and look for it in the normal places. */
5340 if (newname != NULL)
5341 return newname;
5344 newname = find_a_file (&startfile_prefixes, name, R_OK);
5345 return newname ? newname : name;
5348 /* Determine whether a directory exists. If LINKER, return 0 for
5349 certain fixed names not needed by the linker. If not LINKER, it is
5350 only important to return 0 if the host machine has a small ARG_MAX
5351 limit. */
5353 static int
5354 is_directory (path1, path2, linker)
5355 const char *path1;
5356 const char *path2;
5357 int linker;
5359 int len1 = strlen (path1);
5360 int len2 = strlen (path2);
5361 char *path = (char *) alloca (3 + len1 + len2);
5362 char *cp;
5363 struct stat st;
5365 #ifndef SMALL_ARG_MAX
5366 if (! linker)
5367 return 1;
5368 #endif
5370 /* Construct the path from the two parts. Ensure the string ends with "/.".
5371 The resulting path will be a directory even if the given path is a
5372 symbolic link. */
5373 memcpy (path, path1, len1);
5374 memcpy (path + len1, path2, len2);
5375 cp = path + len1 + len2;
5376 if (!IS_DIR_SEPARATOR (cp[-1]))
5377 *cp++ = DIR_SEPARATOR;
5378 *cp++ = '.';
5379 *cp = '\0';
5381 /* Exclude directories that the linker is known to search. */
5382 if (linker
5383 && ((cp - path == 6
5384 && strcmp (path, concat (dir_separator_str, "lib",
5385 dir_separator_str, ".", NULL_PTR)) == 0)
5386 || (cp - path == 10
5387 && strcmp (path, concat (dir_separator_str, "usr",
5388 dir_separator_str, "lib",
5389 dir_separator_str, ".", NULL_PTR)) == 0)))
5390 return 0;
5392 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5395 /* Set up the various global variables to indicate that we're processing
5396 the input file named FILENAME. */
5398 static void
5399 set_input (filename)
5400 const char *filename;
5402 register const char *p;
5404 input_filename = filename;
5405 input_filename_length = strlen (input_filename);
5407 input_basename = input_filename;
5408 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5409 /* Skip drive name so 'x:foo' is handled properly. */
5410 if (input_basename[1] == ':')
5411 input_basename += 2;
5412 #endif
5413 for (p = input_basename; *p; p++)
5414 if (IS_DIR_SEPARATOR (*p))
5415 input_basename = p + 1;
5417 /* Find a suffix starting with the last period,
5418 and set basename_length to exclude that suffix. */
5419 basename_length = strlen (input_basename);
5420 suffixed_basename_length = basename_length;
5421 p = input_basename + basename_length;
5422 while (p != input_basename && *p != '.')
5423 --p;
5424 if (*p == '.' && p != input_basename)
5426 basename_length = p - input_basename;
5427 input_suffix = p + 1;
5429 else
5430 input_suffix = "";
5433 /* On fatal signals, delete all the temporary files. */
5435 static void
5436 fatal_error (signum)
5437 int signum;
5439 signal (signum, SIG_DFL);
5440 delete_failure_queue ();
5441 delete_temp_files ();
5442 /* Get the same signal again, this time not handled,
5443 so its normal effect occurs. */
5444 kill (getpid (), signum);
5447 extern int main PARAMS ((int, const char *const *));
5450 main (argc, argv)
5451 int argc;
5452 const char *const *argv;
5454 size_t i;
5455 int value;
5456 int linker_was_run = 0;
5457 char *explicit_link_files;
5458 char *specs_file;
5459 const char *p;
5460 struct user_specs *uptr;
5462 p = argv[0] + strlen (argv[0]);
5463 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5464 --p;
5465 programname = p;
5467 xmalloc_set_program_name (programname);
5469 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5470 /* Perform host dependant initialization when needed. */
5471 GCC_DRIVER_HOST_INITIALIZATION;
5472 #endif
5474 /* LC_CTYPE determines the character set used by the terminal so it has be set
5475 to output messages correctly. */
5477 #ifdef HAVE_LC_MESSAGES
5478 setlocale (LC_CTYPE, "");
5479 setlocale (LC_MESSAGES, "");
5480 #else
5481 setlocale (LC_ALL, "");
5482 #endif
5484 (void) bindtextdomain (PACKAGE, localedir);
5485 (void) textdomain (PACKAGE);
5487 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5488 signal (SIGINT, fatal_error);
5489 #ifdef SIGHUP
5490 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5491 signal (SIGHUP, fatal_error);
5492 #endif
5493 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5494 signal (SIGTERM, fatal_error);
5495 #ifdef SIGPIPE
5496 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5497 signal (SIGPIPE, fatal_error);
5498 #endif
5499 #ifdef SIGCHLD
5500 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5501 receive the signal. A different setting is inheritable */
5502 signal (SIGCHLD, SIG_DFL);
5503 #endif
5505 argbuf_length = 10;
5506 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5508 obstack_init (&obstack);
5510 /* Build multilib_select, et. al from the separate lines that make up each
5511 multilib selection. */
5513 const char *const *q = multilib_raw;
5514 int need_space;
5516 obstack_init (&multilib_obstack);
5517 while ((p = *q++) != (char *) 0)
5518 obstack_grow (&multilib_obstack, p, strlen (p));
5520 obstack_1grow (&multilib_obstack, 0);
5521 multilib_select = obstack_finish (&multilib_obstack);
5523 q = multilib_matches_raw;
5524 while ((p = *q++) != (char *) 0)
5525 obstack_grow (&multilib_obstack, p, strlen (p));
5527 obstack_1grow (&multilib_obstack, 0);
5528 multilib_matches = obstack_finish (&multilib_obstack);
5530 q = multilib_exclusions_raw;
5531 while ((p = *q++) != (char *) 0)
5532 obstack_grow (&multilib_obstack, p, strlen (p));
5534 obstack_1grow (&multilib_obstack, 0);
5535 multilib_exclusions = obstack_finish (&multilib_obstack);
5537 need_space = FALSE;
5538 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5540 if (need_space)
5541 obstack_1grow (&multilib_obstack, ' ');
5542 obstack_grow (&multilib_obstack,
5543 multilib_defaults_raw[i],
5544 strlen (multilib_defaults_raw[i]));
5545 need_space = TRUE;
5548 obstack_1grow (&multilib_obstack, 0);
5549 multilib_defaults = obstack_finish (&multilib_obstack);
5552 /* Set up to remember the pathname of gcc and any options
5553 needed for collect. We use argv[0] instead of programname because
5554 we need the complete pathname. */
5555 obstack_init (&collect_obstack);
5556 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5557 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5558 putenv (obstack_finish (&collect_obstack));
5560 #ifdef INIT_ENVIRONMENT
5561 /* Set up any other necessary machine specific environment variables. */
5562 putenv (INIT_ENVIRONMENT);
5563 #endif
5565 /* Make a table of what switches there are (switches, n_switches).
5566 Make a table of specified input files (infiles, n_infiles).
5567 Decode switches that are handled locally. */
5569 process_command (argc, argv);
5572 int first_time;
5574 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
5575 the compiler. */
5576 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
5577 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
5579 first_time = TRUE;
5580 for (i = 0; (int) i < n_switches; i++)
5582 const char *const *args;
5583 const char *p, *q;
5584 if (!first_time)
5585 obstack_grow (&collect_obstack, " ", 1);
5587 first_time = FALSE;
5588 obstack_grow (&collect_obstack, "'-", 2);
5589 q = switches[i].part1;
5590 while ((p = strchr (q, '\'')))
5592 obstack_grow (&collect_obstack, q, p - q);
5593 obstack_grow (&collect_obstack, "'\\''", 4);
5594 q = ++p;
5596 obstack_grow (&collect_obstack, q, strlen (q));
5597 obstack_grow (&collect_obstack, "'", 1);
5599 for (args = switches[i].args; args && *args; args++)
5601 obstack_grow (&collect_obstack, " '", 2);
5602 q = *args;
5603 while ((p = strchr (q, '\'')))
5605 obstack_grow (&collect_obstack, q, p - q);
5606 obstack_grow (&collect_obstack, "'\\''", 4);
5607 q = ++p;
5609 obstack_grow (&collect_obstack, q, strlen (q));
5610 obstack_grow (&collect_obstack, "'", 1);
5613 obstack_grow (&collect_obstack, "\0", 1);
5614 putenv (obstack_finish (&collect_obstack));
5617 /* Initialize the vector of specs to just the default.
5618 This means one element containing 0s, as a terminator. */
5620 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5621 memcpy ((char *) compilers, (char *) default_compilers,
5622 sizeof default_compilers);
5623 n_compilers = n_default_compilers;
5625 /* Read specs from a file if there is one. */
5627 machine_suffix = concat (spec_machine, dir_separator_str,
5628 spec_version, dir_separator_str, NULL_PTR);
5629 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
5631 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5632 /* Read the specs file unless it is a default one. */
5633 if (specs_file != 0 && strcmp (specs_file, "specs"))
5634 read_specs (specs_file, TRUE);
5635 else
5636 init_spec ();
5638 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5639 for any override of as, ld and libraries. */
5640 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5641 + strlen (just_machine_suffix)
5642 + sizeof ("specs"));
5644 strcpy (specs_file, standard_exec_prefix);
5645 strcat (specs_file, just_machine_suffix);
5646 strcat (specs_file, "specs");
5647 if (access (specs_file, R_OK) == 0)
5648 read_specs (specs_file, TRUE);
5650 /* If not cross-compiling, look for startfiles in the standard places. */
5651 if (*cross_compile == '0')
5653 if (*md_exec_prefix)
5655 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5656 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5657 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5658 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5661 if (*md_startfile_prefix)
5662 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5663 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5665 if (*md_startfile_prefix_1)
5666 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5667 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5669 /* If standard_startfile_prefix is relative, base it on
5670 standard_exec_prefix. This lets us move the installed tree
5671 as a unit. If GCC_EXEC_PREFIX is defined, base
5672 standard_startfile_prefix on that as well. */
5673 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5674 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5675 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5676 else
5678 if (gcc_exec_prefix)
5679 add_prefix (&startfile_prefixes,
5680 concat (gcc_exec_prefix, machine_suffix,
5681 standard_startfile_prefix, NULL_PTR),
5682 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5683 add_prefix (&startfile_prefixes,
5684 concat (standard_exec_prefix,
5685 machine_suffix,
5686 standard_startfile_prefix, NULL_PTR),
5687 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5690 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5691 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5692 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5693 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5694 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5695 add_prefix (&startfile_prefixes, "./", NULL_PTR,
5696 PREFIX_PRIORITY_LAST, 1, NULL_PTR);
5697 #endif
5699 else
5701 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5702 && gcc_exec_prefix)
5703 add_prefix (&startfile_prefixes,
5704 concat (gcc_exec_prefix, machine_suffix,
5705 standard_startfile_prefix, NULL_PTR),
5706 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5709 /* Process any user specified specs in the order given on the command
5710 line. */
5711 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5713 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5714 read_specs (filename ? filename : uptr->filename, FALSE);
5717 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5718 if (gcc_exec_prefix)
5720 char *temp = (char *) xmalloc (strlen (gcc_exec_prefix)
5721 + strlen (spec_version)
5722 + strlen (spec_machine) + 3);
5723 strcpy (temp, gcc_exec_prefix);
5724 strcat (temp, spec_machine);
5725 strcat (temp, dir_separator_str);
5726 strcat (temp, spec_version);
5727 strcat (temp, dir_separator_str);
5728 gcc_exec_prefix = temp;
5731 /* Now we have the specs.
5732 Set the `valid' bits for switches that match anything in any spec. */
5734 validate_all_switches ();
5736 /* Now that we have the switches and the specs, set
5737 the subdirectory based on the options. */
5738 set_multilib_dir ();
5740 /* Warn about any switches that no pass was interested in. */
5742 for (i = 0; (int) i < n_switches; i++)
5743 if (! switches[i].validated)
5744 error ("unrecognized option `-%s'", switches[i].part1);
5746 /* Obey some of the options. */
5748 if (print_search_dirs)
5750 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5751 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5752 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5753 return (0);
5756 if (print_file_name)
5758 printf ("%s\n", find_file (print_file_name));
5759 return (0);
5762 if (print_prog_name)
5764 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5765 printf ("%s\n", (newname ? newname : print_prog_name));
5766 return (0);
5769 if (print_multi_lib)
5771 print_multilib_info ();
5772 return (0);
5775 if (print_multi_directory)
5777 if (multilib_dir == NULL)
5778 printf (".\n");
5779 else
5780 printf ("%s\n", multilib_dir);
5781 return (0);
5784 if (target_help_flag)
5786 /* Print if any target specific options.*/
5788 /* We do not exit here. Instead we have created a fake input file
5789 called 'target-dummy' which needs to be compiled, and we pass this
5790 on to the various sub-processes, along with the --target-help
5791 switch. */
5794 if (print_help_list)
5796 display_help ();
5798 if (! verbose_flag)
5800 printf (_("\nFor bug reporting instructions, please see:\n"));
5801 printf ("%s.\n", GCCBUGURL);
5803 return (0);
5806 /* We do not exit here. Instead we have created a fake input file
5807 called 'help-dummy' which needs to be compiled, and we pass this
5808 on the the various sub-processes, along with the --help switch. */
5811 if (verbose_flag)
5813 int n;
5814 const char *thrmod;
5816 notice ("Configured with: %s\n", configuration_arguments);
5818 #ifdef THREAD_MODEL_SPEC
5819 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
5820 but there's no point in doing all this processing just to get
5821 thread_model back. */
5822 obstack_init (&obstack);
5823 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
5824 obstack_1grow (&obstack, '\0');
5825 thrmod = obstack_finish (&obstack);
5826 #else
5827 thrmod = thread_model;
5828 #endif
5830 notice ("Thread model: %s\n", thrmod);
5832 /* compiler_version is truncated at the first space when initialized
5833 from version string, so truncate version_string at the first space
5834 before comparing. */
5835 for (n = 0; version_string[n]; n++)
5836 if (version_string[n] == ' ')
5837 break;
5839 if (! strncmp (version_string, compiler_version, n)
5840 && compiler_version[n] == 0)
5841 notice ("gcc version %s\n", version_string);
5842 else
5843 notice ("gcc driver version %s executing gcc version %s\n",
5844 version_string, compiler_version);
5846 if (n_infiles == 0)
5847 return (0);
5850 if (n_infiles == added_libraries)
5851 fatal ("No input files");
5853 /* Make a place to record the compiler output file names
5854 that correspond to the input files. */
5856 i = n_infiles;
5857 i += lang_specific_extra_outfiles;
5858 outfiles = (const char **) xcalloc (i, sizeof (char *));
5860 /* Record which files were specified explicitly as link input. */
5862 explicit_link_files = xcalloc (1, n_infiles);
5864 for (i = 0; (int) i < n_infiles; i++)
5866 int this_file_error = 0;
5868 /* Tell do_spec what to substitute for %i. */
5870 input_file_number = i;
5871 set_input (infiles[i].name);
5873 /* Use the same thing in %o, unless cp->spec says otherwise. */
5875 outfiles[i] = input_filename;
5877 /* Figure out which compiler from the file's suffix. */
5879 input_file_compiler
5880 = lookup_compiler (infiles[i].name, input_filename_length,
5881 infiles[i].language);
5883 if (input_file_compiler)
5885 /* Ok, we found an applicable compiler. Run its spec. */
5887 if (input_file_compiler->spec[0] == '#')
5888 error ("%s: %s compiler not installed on this system",
5889 input_filename, &input_file_compiler->spec[1]);
5890 value = do_spec (input_file_compiler->spec);
5891 if (value < 0)
5892 this_file_error = 1;
5895 /* If this file's name does not contain a recognized suffix,
5896 record it as explicit linker input. */
5898 else
5899 explicit_link_files[i] = 1;
5901 /* Clear the delete-on-failure queue, deleting the files in it
5902 if this compilation failed. */
5904 if (this_file_error)
5906 delete_failure_queue ();
5907 error_count++;
5909 /* If this compilation succeeded, don't delete those files later. */
5910 clear_failure_queue ();
5913 /* Reset the output file name to the first input file name, for use
5914 with %b in LINK_SPEC on a target that prefers not to emit a.out
5915 by default. */
5916 if (n_infiles > 0)
5917 set_input (infiles[0].name);
5919 if (error_count == 0)
5921 /* Make sure INPUT_FILE_NUMBER points to first available open
5922 slot. */
5923 input_file_number = n_infiles;
5924 if (lang_specific_pre_link ())
5925 error_count++;
5928 /* Run ld to link all the compiler output files. */
5930 if (error_count == 0)
5932 int tmp = execution_count;
5934 /* We'll use ld if we can't find collect2. */
5935 if (! strcmp (linker_name_spec, "collect2"))
5937 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
5938 if (s == NULL)
5939 linker_name_spec = "ld";
5941 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
5942 for collect. */
5943 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
5944 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
5946 value = do_spec (link_command_spec);
5947 if (value < 0)
5948 error_count = 1;
5949 linker_was_run = (tmp != execution_count);
5952 /* If options said don't run linker,
5953 complain about input files to be given to the linker. */
5955 if (! linker_was_run && error_count == 0)
5956 for (i = 0; (int) i < n_infiles; i++)
5957 if (explicit_link_files[i])
5958 error ("%s: linker input file unused because linking not done",
5959 outfiles[i]);
5961 /* Delete some or all of the temporary files we made. */
5963 if (error_count)
5964 delete_failure_queue ();
5965 delete_temp_files ();
5967 if (print_help_list)
5969 printf (("\nFor bug reporting instructions, please see:\n"));
5970 printf ("%s\n", GCCBUGURL);
5973 return (signal_count != 0 ? 2
5974 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
5975 : 0);
5978 /* Find the proper compilation spec for the file name NAME,
5979 whose length is LENGTH. LANGUAGE is the specified language,
5980 or 0 if this file is to be passed to the linker. */
5982 static struct compiler *
5983 lookup_compiler (name, length, language)
5984 const char *name;
5985 size_t length;
5986 const char *language;
5988 struct compiler *cp;
5990 /* If this was specified by the user to be a linker input, indicate that. */
5991 if (language != 0 && language[0] == '*')
5992 return 0;
5994 /* Otherwise, look for the language, if one is spec'd. */
5995 if (language != 0)
5997 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5998 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
5999 return cp;
6001 error ("language %s not recognized", language);
6002 return 0;
6005 /* Look for a suffix. */
6006 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6008 if (/* The suffix `-' matches only the file name `-'. */
6009 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6010 || (strlen (cp->suffix) < length
6011 /* See if the suffix matches the end of NAME. */
6012 && !strcmp (cp->suffix,
6013 name + length - strlen (cp->suffix))
6015 break;
6018 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6019 /* look again, but case-insensitively this time. */
6020 if (cp < compilers)
6021 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6023 if (/* The suffix `-' matches only the file name `-'. */
6024 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6025 || (strlen (cp->suffix) < length
6026 /* See if the suffix matches the end of NAME. */
6027 && ((!strcmp (cp->suffix,
6028 name + length - strlen (cp->suffix))
6029 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6030 && !strcasecmp (cp->suffix,
6031 name + length - strlen (cp->suffix)))
6033 break;
6035 #endif
6037 if (cp >= compilers)
6039 if (cp->spec[0] != '@')
6040 /* A non-alias entry: return it. */
6041 return cp;
6043 /* An alias entry maps a suffix to a language.
6044 Search for the language; pass 0 for NAME and LENGTH
6045 to avoid infinite recursion if language not found. */
6046 return lookup_compiler (NULL_PTR, 0, cp->spec + 1);
6048 return 0;
6051 static char *
6052 save_string (s, len)
6053 const char *s;
6054 int len;
6056 register char *result = xmalloc (len + 1);
6058 memcpy (result, s, len);
6059 result[len] = 0;
6060 return result;
6063 void
6064 pfatal_with_name (name)
6065 const char *name;
6067 perror_with_name (name);
6068 delete_temp_files ();
6069 exit (1);
6072 static void
6073 perror_with_name (name)
6074 const char *name;
6076 error ("%s: %s", name, xstrerror (errno));
6079 static void
6080 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6081 const char *errmsg_fmt;
6082 const char *errmsg_arg;
6084 if (errmsg_arg)
6086 int save_errno = errno;
6088 /* Space for trailing '\0' is in %s. */
6089 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6090 sprintf (msg, errmsg_fmt, errmsg_arg);
6091 errmsg_fmt = msg;
6093 errno = save_errno;
6096 pfatal_with_name (errmsg_fmt);
6099 /* Output an error message and exit */
6101 void
6102 fancy_abort ()
6104 fatal ("Internal gcc abort.");
6107 /* Output an error message and exit */
6109 void
6110 fatal VPARAMS ((const char *msgid, ...))
6112 #ifndef ANSI_PROTOTYPES
6113 const char *msgid;
6114 #endif
6115 va_list ap;
6117 VA_START (ap, msgid);
6119 #ifndef ANSI_PROTOTYPES
6120 msgid = va_arg (ap, const char *);
6121 #endif
6123 fprintf (stderr, "%s: ", programname);
6124 vfprintf (stderr, _(msgid), ap);
6125 va_end (ap);
6126 fprintf (stderr, "\n");
6127 delete_temp_files ();
6128 exit (1);
6131 void
6132 error VPARAMS ((const char *msgid, ...))
6134 #ifndef ANSI_PROTOTYPES
6135 const char *msgid;
6136 #endif
6137 va_list ap;
6139 VA_START (ap, msgid);
6141 #ifndef ANSI_PROTOTYPES
6142 msgid = va_arg (ap, const char *);
6143 #endif
6145 fprintf (stderr, "%s: ", programname);
6146 vfprintf (stderr, _(msgid), ap);
6147 va_end (ap);
6149 fprintf (stderr, "\n");
6152 static void
6153 notice VPARAMS ((const char *msgid, ...))
6155 #ifndef ANSI_PROTOTYPES
6156 const char *msgid;
6157 #endif
6158 va_list ap;
6160 VA_START (ap, msgid);
6162 #ifndef ANSI_PROTOTYPES
6163 msgid = va_arg (ap, const char *);
6164 #endif
6166 vfprintf (stderr, _(msgid), ap);
6167 va_end (ap);
6170 static void
6171 validate_all_switches ()
6173 struct compiler *comp;
6174 register const char *p;
6175 register char c;
6176 struct spec_list *spec;
6178 for (comp = compilers; comp->spec; comp++)
6180 p = comp->spec;
6181 while ((c = *p++))
6182 if (c == '%' && *p == '{')
6183 /* We have a switch spec. */
6184 validate_switches (p + 1);
6187 /* Look through the linked list of specs read from the specs file. */
6188 for (spec = specs; spec; spec = spec->next)
6190 p = *(spec->ptr_spec);
6191 while ((c = *p++))
6192 if (c == '%' && *p == '{')
6193 /* We have a switch spec. */
6194 validate_switches (p + 1);
6197 p = link_command_spec;
6198 while ((c = *p++))
6199 if (c == '%' && *p == '{')
6200 /* We have a switch spec. */
6201 validate_switches (p + 1);
6204 /* Look at the switch-name that comes after START
6205 and mark as valid all supplied switches that match it. */
6207 static void
6208 validate_switches (start)
6209 const char *start;
6211 register const char *p = start;
6212 const char *filter;
6213 register int i;
6214 int suffix;
6216 if (*p == '|')
6217 ++p;
6219 next_member:
6220 if (*p == '!')
6221 ++p;
6223 suffix = 0;
6224 if (*p == '.')
6225 suffix = 1, ++p;
6227 filter = p;
6228 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6229 p++;
6231 if (suffix)
6233 else if (p[-1] == '*')
6235 /* Mark all matching switches as valid. */
6236 for (i = 0; i < n_switches; i++)
6237 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6238 switches[i].validated = 1;
6240 else
6242 /* Mark an exact matching switch as valid. */
6243 for (i = 0; i < n_switches; i++)
6245 if (!strncmp (switches[i].part1, filter, p - filter)
6246 && switches[i].part1[p - filter] == 0)
6247 switches[i].validated = 1;
6251 if (*p++ == '|' || p[-1] == '&')
6252 goto next_member;
6255 /* Check whether a particular argument was used. The first time we
6256 canonicalize the switches to keep only the ones we care about. */
6258 static int
6259 used_arg (p, len)
6260 const char *p;
6261 int len;
6263 struct mswitchstr
6265 const char *str;
6266 const char *replace;
6267 int len;
6268 int rep_len;
6271 static struct mswitchstr *mswitches;
6272 static int n_mswitches;
6273 int i, j;
6275 if (!mswitches)
6277 struct mswitchstr *matches;
6278 const char *q;
6279 int cnt = 0;
6281 /* Break multilib_matches into the component strings of string
6282 and replacement string. */
6283 for (q = multilib_matches; *q != '\0'; q++)
6284 if (*q == ';')
6285 cnt++;
6287 matches =
6288 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6289 i = 0;
6290 q = multilib_matches;
6291 while (*q != '\0')
6293 matches[i].str = q;
6294 while (*q != ' ')
6296 if (*q == '\0')
6297 abort ();
6298 q++;
6300 matches[i].len = q - matches[i].str;
6302 matches[i].replace = ++q;
6303 while (*q != ';' && *q != '\0')
6305 if (*q == ' ')
6306 abort ();
6307 q++;
6309 matches[i].rep_len = q - matches[i].replace;
6310 i++;
6311 if (*q == ';')
6312 q++;
6315 /* Now build a list of the replacement string for switches that we care
6316 about. Make sure we allocate at least one entry. This prevents
6317 xmalloc from calling fatal, and prevents us from re-executing this
6318 block of code. */
6319 mswitches
6320 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6321 * (n_switches ? n_switches : 1));
6322 for (i = 0; i < n_switches; i++)
6324 int xlen = strlen (switches[i].part1);
6325 for (j = 0; j < cnt; j++)
6326 if (xlen == matches[j].len
6327 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6329 mswitches[n_mswitches].str = matches[j].replace;
6330 mswitches[n_mswitches].len = matches[j].rep_len;
6331 mswitches[n_mswitches].replace = (char *) 0;
6332 mswitches[n_mswitches].rep_len = 0;
6333 n_mswitches++;
6334 break;
6339 for (i = 0; i < n_mswitches; i++)
6340 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6341 return 1;
6343 return 0;
6346 static int
6347 default_arg (p, len)
6348 const char *p;
6349 int len;
6351 const char *start, *end;
6353 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6355 while (*start == ' ' || *start == '\t')
6356 start++;
6358 if (*start == '\0')
6359 break;
6361 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6364 if ((end - start) == len && strncmp (p, start, len) == 0)
6365 return 1;
6367 if (*end == '\0')
6368 break;
6371 return 0;
6374 /* Work out the subdirectory to use based on the options. The format of
6375 multilib_select is a list of elements. Each element is a subdirectory
6376 name followed by a list of options followed by a semicolon. The format
6377 of multilib_exclusions is the same, but without the preceding
6378 directory. First gcc will check the exclusions, if none of the options
6379 beginning with an exclamation point are present, and all of the other
6380 options are present, then we will ignore this completely. Passing
6381 that, gcc will consider each multilib_select in turn using the same
6382 rules for matching the options. If a match is found, that subdirectory
6383 will be used. */
6385 static void
6386 set_multilib_dir ()
6388 const char *p;
6389 unsigned int this_path_len;
6390 const char *this_path, *this_arg;
6391 int not_arg;
6392 int ok;
6394 p = multilib_exclusions;
6395 while (*p != '\0')
6397 /* Ignore newlines. */
6398 if (*p == '\n')
6400 ++p;
6401 continue;
6404 /* Check the arguments. */
6405 ok = 1;
6406 while (*p != ';')
6408 if (*p == '\0')
6409 abort ();
6411 if (! ok)
6413 ++p;
6414 continue;
6417 this_arg = p;
6418 while (*p != ' ' && *p != ';')
6420 if (*p == '\0')
6421 abort ();
6422 ++p;
6425 if (*this_arg != '!')
6426 not_arg = 0;
6427 else
6429 not_arg = 1;
6430 ++this_arg;
6433 ok = used_arg (this_arg, p - this_arg);
6434 if (not_arg)
6435 ok = ! ok;
6437 if (*p == ' ')
6438 ++p;
6441 if (ok)
6442 return;
6444 ++p;
6447 p = multilib_select;
6448 while (*p != '\0')
6450 /* Ignore newlines. */
6451 if (*p == '\n')
6453 ++p;
6454 continue;
6457 /* Get the initial path. */
6458 this_path = p;
6459 while (*p != ' ')
6461 if (*p == '\0')
6462 abort ();
6463 ++p;
6465 this_path_len = p - this_path;
6467 /* Check the arguments. */
6468 ok = 1;
6469 ++p;
6470 while (*p != ';')
6472 if (*p == '\0')
6473 abort ();
6475 if (! ok)
6477 ++p;
6478 continue;
6481 this_arg = p;
6482 while (*p != ' ' && *p != ';')
6484 if (*p == '\0')
6485 abort ();
6486 ++p;
6489 if (*this_arg != '!')
6490 not_arg = 0;
6491 else
6493 not_arg = 1;
6494 ++this_arg;
6497 /* If this is a default argument, we can just ignore it.
6498 This is true even if this_arg begins with '!'. Beginning
6499 with '!' does not mean that this argument is necessarily
6500 inappropriate for this library: it merely means that
6501 there is a more specific library which uses this
6502 argument. If this argument is a default, we need not
6503 consider that more specific library. */
6504 if (! default_arg (this_arg, p - this_arg))
6506 ok = used_arg (this_arg, p - this_arg);
6507 if (not_arg)
6508 ok = ! ok;
6511 if (*p == ' ')
6512 ++p;
6515 if (ok)
6517 if (this_path_len != 1
6518 || this_path[0] != '.')
6520 char *new_multilib_dir = xmalloc (this_path_len + 1);
6521 strncpy (new_multilib_dir, this_path, this_path_len);
6522 new_multilib_dir[this_path_len] = '\0';
6523 multilib_dir = new_multilib_dir;
6525 break;
6528 ++p;
6532 /* Print out the multiple library subdirectory selection
6533 information. This prints out a series of lines. Each line looks
6534 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6535 required. Only the desired options are printed out, the negative
6536 matches. The options are print without a leading dash. There are
6537 no spaces to make it easy to use the information in the shell.
6538 Each subdirectory is printed only once. This assumes the ordering
6539 generated by the genmultilib script. Also, we leave out ones that match
6540 the exclusions. */
6542 static void
6543 print_multilib_info ()
6545 const char *p = multilib_select;
6546 const char *last_path = 0, *this_path;
6547 int skip;
6548 unsigned int last_path_len = 0;
6550 while (*p != '\0')
6552 skip = 0;
6553 /* Ignore newlines. */
6554 if (*p == '\n')
6556 ++p;
6557 continue;
6560 /* Get the initial path. */
6561 this_path = p;
6562 while (*p != ' ')
6564 if (*p == '\0')
6565 abort ();
6566 ++p;
6569 /* Check for matches with the multilib_exclusions. We don't bother
6570 with the '!' in either list. If any of the exclusion rules match
6571 all of its options with the select rule, we skip it. */
6573 const char *e = multilib_exclusions;
6574 const char *this_arg;
6576 while (*e != '\0')
6578 int m = 1;
6579 /* Ignore newlines. */
6580 if (*e == '\n')
6582 ++e;
6583 continue;
6586 /* Check the arguments. */
6587 while (*e != ';')
6589 const char *q;
6590 int mp = 0;
6592 if (*e == '\0')
6593 abort ();
6595 if (! m)
6597 ++e;
6598 continue;
6601 this_arg = e;
6603 while (*e != ' ' && *e != ';')
6605 if (*e == '\0')
6606 abort ();
6607 ++e;
6610 q = p + 1;
6611 while (*q != ';')
6613 const char *arg;
6614 int len = e - this_arg;
6616 if (*q == '\0')
6617 abort ();
6619 arg = q;
6621 while (*q != ' ' && *q != ';')
6623 if (*q == '\0')
6624 abort ();
6625 ++q;
6628 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6629 default_arg (this_arg, e - this_arg))
6631 mp = 1;
6632 break;
6635 if (*q == ' ')
6636 ++q;
6639 if (! mp)
6640 m = 0;
6642 if (*e == ' ')
6643 ++e;
6646 if (m)
6648 skip = 1;
6649 break;
6652 if (*e != '\0')
6653 ++e;
6657 if (! skip)
6659 /* If this is a duplicate, skip it. */
6660 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6661 && ! strncmp (last_path, this_path, last_path_len));
6663 last_path = this_path;
6664 last_path_len = p - this_path;
6667 /* If this directory requires any default arguments, we can skip
6668 it. We will already have printed a directory identical to
6669 this one which does not require that default argument. */
6670 if (! skip)
6672 const char *q;
6674 q = p + 1;
6675 while (*q != ';')
6677 const char *arg;
6679 if (*q == '\0')
6680 abort ();
6682 if (*q == '!')
6683 arg = NULL;
6684 else
6685 arg = q;
6687 while (*q != ' ' && *q != ';')
6689 if (*q == '\0')
6690 abort ();
6691 ++q;
6694 if (arg != NULL
6695 && default_arg (arg, q - arg))
6697 skip = 1;
6698 break;
6701 if (*q == ' ')
6702 ++q;
6706 if (! skip)
6708 const char *p1;
6710 for (p1 = last_path; p1 < p; p1++)
6711 putchar (*p1);
6712 putchar (';');
6715 ++p;
6716 while (*p != ';')
6718 int use_arg;
6720 if (*p == '\0')
6721 abort ();
6723 if (skip)
6725 ++p;
6726 continue;
6729 use_arg = *p != '!';
6731 if (use_arg)
6732 putchar ('@');
6734 while (*p != ' ' && *p != ';')
6736 if (*p == '\0')
6737 abort ();
6738 if (use_arg)
6739 putchar (*p);
6740 ++p;
6743 if (*p == ' ')
6744 ++p;
6747 if (! skip)
6749 /* If there are extra options, print them now. */
6750 if (multilib_extra && *multilib_extra)
6752 int print_at = TRUE;
6753 const char *q;
6755 for (q = multilib_extra; *q != '\0'; q++)
6757 if (*q == ' ')
6758 print_at = TRUE;
6759 else
6761 if (print_at)
6762 putchar ('@');
6763 putchar (*q);
6764 print_at = FALSE;
6769 putchar ('\n');
6772 ++p;