* gcc.c (option_map): Added --classpath and --CLASSPATH.
[official-gcc.git] / gcc / gcc.c
blob1e95b04d81db4df2ed133dcd8b2c0ffc84f37f7c
1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 89, 92-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 This paragraph is here to try to keep Sun CC from dying.
22 The number of chars here seems crucial!!!! */
24 /* This program is the user interface to the C compiler and possibly to
25 other compilers. It is used because compilation is a complicated procedure
26 which involves running several programs and passing temporary files between
27 them, forwarding the users switches to those programs selectively,
28 and deleting the temporary files at the end.
30 CC recognizes how to compile each input file by suffixes in the file names.
31 Once it knows which kind of compilation to perform, the procedure for
32 compilation is specified by a string called a "spec". */
34 #include "config.h"
35 #include "system.h"
36 #include <signal.h>
37 #include <sys/stat.h>
39 #include "gansidecl.h"
40 #include "obstack.h"
43 /* ??? Need to find a GCC header to put these in. */
44 extern int pexecute PROTO ((const char *, char * const *, const char *,
45 const char *, char **, char **, int));
46 extern int pwait PROTO ((int, int *, int));
47 extern char *update_path PROTO((char *, char *));
48 extern void set_std_prefix PROTO((char *, int));
49 /* Flag arguments to pexecute. */
50 #define PEXECUTE_FIRST 1
51 #define PEXECUTE_LAST 2
52 #define PEXECUTE_SEARCH 4
53 #define PEXECUTE_VERBOSE 8
55 #ifndef WIFSIGNALED
56 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
57 #endif
58 #ifndef WTERMSIG
59 #define WTERMSIG(S) ((S) & 0x7f)
60 #endif
61 #ifndef WIFEXITED
62 #define WIFEXITED(S) (((S) & 0xff) == 0)
63 #endif
64 #ifndef WEXITSTATUS
65 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
66 #endif
68 #ifdef VMS
69 #define exit __posix_exit
70 #endif
72 #ifdef USG
73 #define vfork fork
74 #endif /* USG */
76 /* Test if something is a normal file. */
77 #ifndef S_ISREG
78 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
79 #endif
81 /* Test if something is a directory. */
82 #ifndef S_ISDIR
83 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
84 #endif
86 /* By default there is no special suffix for executables. */
87 #ifdef EXECUTABLE_SUFFIX
88 #define HAVE_EXECUTABLE_SUFFIX
89 #else
90 #define EXECUTABLE_SUFFIX ""
91 #endif
93 /* By default, the suffix for object files is ".o". */
94 #ifdef OBJECT_SUFFIX
95 #define HAVE_OBJECT_SUFFIX
96 #else
97 #define OBJECT_SUFFIX ".o"
98 #endif
100 /* By default, colon separates directories in a path. */
101 #ifndef PATH_SEPARATOR
102 #define PATH_SEPARATOR ':'
103 #endif
105 #ifndef DIR_SEPARATOR
106 #define DIR_SEPARATOR '/'
107 #endif
109 static char dir_separator_str[] = {DIR_SEPARATOR, 0};
111 #define obstack_chunk_alloc xmalloc
112 #define obstack_chunk_free free
114 #ifndef GET_ENV_PATH_LIST
115 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
116 #endif
118 extern char *my_strerror PROTO((int));
120 #ifndef HAVE_KILL
121 #define kill(p,s) raise(s)
122 #endif
124 /* If a stage of compilation returns an exit status >= 1,
125 compilation of that file ceases. */
127 #define MIN_FATAL_STATUS 1
129 /* Flag saying to print the directories gcc will search through looking for
130 programs, libraries, etc. */
132 static int print_search_dirs;
134 /* Flag saying to print the full filename of this file
135 as found through our usual search mechanism. */
137 static char *print_file_name = NULL;
139 /* As print_file_name, but search for executable file. */
141 static char *print_prog_name = NULL;
143 /* Flag saying to print the relative path we'd use to
144 find libgcc.a given the current compiler flags. */
146 static int print_multi_directory;
148 /* Flag saying to print the list of subdirectories and
149 compiler flags used to select them in a standard form. */
151 static int print_multi_lib;
153 /* Flag saying to print the command line options understood by gcc and its
154 sub-processes. */
156 static int print_help_list;
158 /* Flag indicating whether we should print the command and arguments */
160 static int verbose_flag;
162 /* Nonzero means write "temp" files in source directory
163 and use the source file's name in them, and don't delete them. */
165 static int save_temps_flag;
167 /* The compiler version. */
169 static char *compiler_version;
171 /* The target version specified with -V */
173 static char *spec_version = DEFAULT_TARGET_VERSION;
175 /* The target machine specified with -b. */
177 static char *spec_machine = DEFAULT_TARGET_MACHINE;
179 /* Nonzero if cross-compiling.
180 When -b is used, the value comes from the `specs' file. */
182 #ifdef CROSS_COMPILE
183 static char *cross_compile = "1";
184 #else
185 static char *cross_compile = "0";
186 #endif
188 /* The number of errors that have occurred; the link phase will not be
189 run if this is non-zero. */
190 static int error_count = 0;
192 /* This is the obstack which we use to allocate many strings. */
194 static struct obstack obstack;
196 /* This is the obstack to build an environment variable to pass to
197 collect2 that describes all of the relevant switches of what to
198 pass the compiler in building the list of pointers to constructors
199 and destructors. */
201 static struct obstack collect_obstack;
203 extern char *version_string;
205 /* Forward declaration for prototypes. */
206 struct path_prefix;
208 static void init_spec PROTO((void));
209 static void read_specs PROTO((char *, int));
210 static void set_spec PROTO((char *, char *));
211 static struct compiler *lookup_compiler PROTO((char *, size_t, char *));
212 static char *build_search_list PROTO((struct path_prefix *, char *, int));
213 static void putenv_from_prefixes PROTO((struct path_prefix *, char *));
214 static char *find_a_file PROTO((struct path_prefix *, char *, int));
215 static void add_prefix PROTO((struct path_prefix *, char *, char *,
216 int, int, int *));
217 static char *skip_whitespace PROTO((char *));
218 static void record_temp_file PROTO((char *, int, int));
219 static void delete_if_ordinary PROTO((char *));
220 static void delete_temp_files PROTO((void));
221 static void delete_failure_queue PROTO((void));
222 static void clear_failure_queue PROTO((void));
223 static int check_live_switch PROTO((int, int));
224 static char *handle_braces PROTO((char *));
225 static char *save_string PROTO((char *, int));
226 static char *concat PVPROTO((char *, ...));
227 extern int do_spec PROTO((char *));
228 static int do_spec_1 PROTO((char *, int, char *));
229 static char *find_file PROTO((char *));
230 static int is_directory PROTO((char *, char *, int));
231 static void validate_switches PROTO((char *));
232 static void validate_all_switches PROTO((void));
233 static void give_switch PROTO((int, int, int));
234 static int used_arg PROTO((char *, int));
235 static int default_arg PROTO((char *, int));
236 static void set_multilib_dir PROTO((void));
237 static void print_multilib_info PROTO((void));
238 static void pfatal_with_name PROTO((char *)) ATTRIBUTE_NORETURN;
239 static void perror_with_name PROTO((char *));
240 static void pfatal_pexecute PROTO((char *, char *)) ATTRIBUTE_NORETURN;
241 static void fatal PVPROTO((char *, ...)) ATTRIBUTE_NORETURN;
242 static void error PVPROTO((char *, ...));
243 static void display_help PROTO((void));
245 void fancy_abort PROTO((void)) ATTRIBUTE_NORETURN;
246 char *xmalloc ();
247 char *xrealloc ();
249 #ifdef LANG_SPECIFIC_DRIVER
250 /* Called before processing to change/add/remove arguments. */
251 extern void lang_specific_driver PROTO ((void (*) PVPROTO((char *, ...)), int *, char ***, int *));
253 /* Called before linking. Returns 0 on success and -1 on failure. */
254 extern int lang_specific_pre_link ();
256 /* Number of extra output files that lang_specific_pre_link may generate. */
257 extern int lang_specific_extra_outfiles;
258 #endif
260 /* Specs are strings containing lines, each of which (if not blank)
261 is made up of a program name, and arguments separated by spaces.
262 The program name must be exact and start from root, since no path
263 is searched and it is unreliable to depend on the current working directory.
264 Redirection of input or output is not supported; the subprograms must
265 accept filenames saying what files to read and write.
267 In addition, the specs can contain %-sequences to substitute variable text
268 or for conditional text. Here is a table of all defined %-sequences.
269 Note that spaces are not generated automatically around the results of
270 expanding these sequences; therefore, you can concatenate them together
271 or with constant text in a single argument.
273 %% substitute one % into the program name or argument.
274 %i substitute the name of the input file being processed.
275 %b substitute the basename of the input file being processed.
276 This is the substring up to (and not including) the last period
277 and not including the directory.
278 %gSUFFIX
279 substitute a file name that has suffix SUFFIX and is chosen
280 once per compilation, and mark the argument a la %d. To reduce
281 exposure to denial-of-service attacks, the file name is now
282 chosen in a way that is hard to predict even when previously
283 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
284 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
285 the regexp "[.A-Za-z]*" or the special string "%O", which is
286 treated exactly as if %O had been pre-processed. Previously, %g
287 was simply substituted with a file name chosen once per compilation,
288 without regard to any appended suffix (which was therefore treated
289 just like ordinary text), making such attacks more likely to succeed.
290 %uSUFFIX
291 like %g, but generates a new temporary file name even if %uSUFFIX
292 was already seen.
293 %USUFFIX
294 substitutes the last file name generated with %uSUFFIX, generating a
295 new one if there is no such last file name. In the absence of any
296 %uSUFFIX, this is just like %gSUFFIX, except they don't share
297 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
298 would involve the generation of two distinct file names, one
299 for each `%g.s' and another for each `%U.s'. Previously, %U was
300 simply substituted with a file name chosen for the previous %u,
301 without regard to any appended suffix.
302 %d marks the argument containing or following the %d as a
303 temporary file name, so that that file will be deleted if CC exits
304 successfully. Unlike %g, this contributes no text to the argument.
305 %w marks the argument containing or following the %w as the
306 "output file" of this compilation. This puts the argument
307 into the sequence of arguments that %o will substitute later.
308 %W{...}
309 like %{...} but mark last argument supplied within
310 as a file to be deleted on failure.
311 %o substitutes the names of all the output files, with spaces
312 automatically placed around them. You should write spaces
313 around the %o as well or the results are undefined.
314 %o is for use in the specs for running the linker.
315 Input files whose names have no recognized suffix are not compiled
316 at all, but they are included among the output files, so they will
317 be linked.
318 %O substitutes the suffix for object files. Note that this is
319 handled specially when it immediately follows %g, %u, or %U,
320 because of the need for those to form complete file names. The
321 handling is such that %O is treated exactly as if it had already
322 been substituted, except that %g, %u, and %U do not currently
323 support additional SUFFIX characters following %O as they would
324 following, for example, `.o'.
325 %p substitutes the standard macro predefinitions for the
326 current target machine. Use this when running cpp.
327 %P like %p, but puts `__' before and after the name of each macro.
328 (Except macros that already have __.)
329 This is for ANSI C.
330 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
331 %s current argument is the name of a library or startup file of some sort.
332 Search for that file in a standard list of directories
333 and substitute the full name found.
334 %eSTR Print STR as an error message. STR is terminated by a newline.
335 Use this when inconsistent options are detected.
336 %x{OPTION} Accumulate an option for %X.
337 %X Output the accumulated linker options specified by compilations.
338 %Y Output the accumulated assembler options specified by compilations.
339 %Z Output the accumulated preprocessor options specified by compilations.
340 %v1 Substitute the major version number of GCC.
341 (For version 2.5.n, this is 2.)
342 %v2 Substitute the minor version number of GCC.
343 (For version 2.5.n, this is 5.)
344 %a process ASM_SPEC as a spec.
345 This allows config.h to specify part of the spec for running as.
346 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
347 used here. This can be used to run a post-processor after the
348 assembler has done its job.
349 %D Dump out a -L option for each directory in startfile_prefixes.
350 If multilib_dir is set, extra entries are generated with it affixed.
351 %l process LINK_SPEC as a spec.
352 %L process LIB_SPEC as a spec.
353 %G process LIBGCC_SPEC as a spec.
354 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
355 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
356 %c process SIGNED_CHAR_SPEC as a spec.
357 %C process CPP_SPEC as a spec. A capital C is actually used here.
358 %1 process CC1_SPEC as a spec.
359 %2 process CC1PLUS_SPEC as a spec.
360 %| output "-" if the input for the current command is coming from a pipe.
361 %* substitute the variable part of a matched option. (See below.)
362 Note that each comma in the substituted string is replaced by
363 a single space.
364 %{S} substitutes the -S switch, if that switch was given to CC.
365 If that switch was not specified, this substitutes nothing.
366 Here S is a metasyntactic variable.
367 %{S*} substitutes all the switches specified to CC whose names start
368 with -S. This is used for -o, -D, -I, etc; switches that take
369 arguments. CC considers `-o foo' as being one switch whose
370 name starts with `o'. %{o*} would substitute this text,
371 including the space; thus, two arguments would be generated.
372 %{^S*} likewise, but don't put a blank between a switch and any args.
373 %{S*:X} substitutes X if one or more switches whose names start with -S are
374 specified to CC. Note that the tail part of the -S option
375 (i.e. the part matched by the `*') will be substituted for each
376 occurrence of %* within X.
377 %{S:X} substitutes X, but only if the -S switch was given to CC.
378 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
379 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
380 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
381 %{.S:X} substitutes X, but only if processing a file with suffix S.
382 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
383 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
384 combined with ! and . as above binding stronger than the OR.
385 %(Spec) processes a specification defined in a specs file as *Spec:
386 %[Spec] as above, but put __ around -D arguments
388 The conditional text X in a %{S:X} or %{!S:X} construct may contain
389 other nested % constructs or spaces, or even newlines. They are
390 processed as usual, as described above.
392 The -O, -f, -m, and -W switches are handled specifically in these
393 constructs. If another value of -O or the negated form of a -f, -m, or
394 -W switch is found later in the command line, the earlier switch
395 value is ignored, except with {S*} where S is just one letter; this
396 passes all matching options.
398 The character | at the beginning of the predicate text is used to indicate
399 that a command should be piped to the following command, but only if -pipe
400 is specified.
402 Note that it is built into CC which switches take arguments and which
403 do not. You might think it would be useful to generalize this to
404 allow each compiler's spec to say which switches take arguments. But
405 this cannot be done in a consistent fashion. CC cannot even decide
406 which input files have been specified without knowing which switches
407 take arguments, and it must know which input files to compile in order
408 to tell which compilers to run.
410 CC also knows implicitly that arguments starting in `-l' are to be
411 treated as compiler output files, and passed to the linker in their
412 proper position among the other output files. */
414 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
416 /* config.h can define ASM_SPEC to provide extra args to the assembler
417 or extra switch-translations. */
418 #ifndef ASM_SPEC
419 #define ASM_SPEC ""
420 #endif
422 /* config.h can define ASM_FINAL_SPEC to run a post processor after
423 the assembler has run. */
424 #ifndef ASM_FINAL_SPEC
425 #define ASM_FINAL_SPEC ""
426 #endif
428 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
429 or extra switch-translations. */
430 #ifndef CPP_SPEC
431 #define CPP_SPEC ""
432 #endif
434 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
435 or extra switch-translations. */
436 #ifndef CC1_SPEC
437 #define CC1_SPEC ""
438 #endif
440 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
441 or extra switch-translations. */
442 #ifndef CC1PLUS_SPEC
443 #define CC1PLUS_SPEC ""
444 #endif
446 /* config.h can define LINK_SPEC to provide extra args to the linker
447 or extra switch-translations. */
448 #ifndef LINK_SPEC
449 #define LINK_SPEC ""
450 #endif
452 /* config.h can define LIB_SPEC to override the default libraries. */
453 #ifndef LIB_SPEC
454 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
455 #endif
457 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
458 included. */
459 #ifndef LIBGCC_SPEC
460 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
461 /* Have gcc do the search for libgcc.a. */
462 #define LIBGCC_SPEC "libgcc.a%s"
463 #else
464 #define LIBGCC_SPEC "-lgcc"
465 #endif
466 #endif
468 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
469 #ifndef STARTFILE_SPEC
470 #define STARTFILE_SPEC \
471 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
472 #endif
474 /* config.h can define SWITCHES_NEED_SPACES to control which options
475 require spaces between the option and the argument. */
476 #ifndef SWITCHES_NEED_SPACES
477 #define SWITCHES_NEED_SPACES ""
478 #endif
480 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
481 #ifndef ENDFILE_SPEC
482 #define ENDFILE_SPEC ""
483 #endif
485 /* This spec is used for telling cpp whether char is signed or not. */
486 #ifndef SIGNED_CHAR_SPEC
487 /* Use #if rather than ?:
488 because MIPS C compiler rejects like ?: in initializers. */
489 #if DEFAULT_SIGNED_CHAR
490 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
491 #else
492 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
493 #endif
494 #endif
496 #ifndef LINKER_NAME
497 #define LINKER_NAME "collect2"
498 #endif
500 static char *cpp_spec = CPP_SPEC;
501 static char *cpp_predefines = CPP_PREDEFINES;
502 static char *cc1_spec = CC1_SPEC;
503 static char *cc1plus_spec = CC1PLUS_SPEC;
504 static char *signed_char_spec = SIGNED_CHAR_SPEC;
505 static char *asm_spec = ASM_SPEC;
506 static char *asm_final_spec = ASM_FINAL_SPEC;
507 static char *link_spec = LINK_SPEC;
508 static char *lib_spec = LIB_SPEC;
509 static char *libgcc_spec = LIBGCC_SPEC;
510 static char *endfile_spec = ENDFILE_SPEC;
511 static char *startfile_spec = STARTFILE_SPEC;
512 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
513 static char *linker_name_spec = LINKER_NAME;
515 /* Some compilers have limits on line lengths, and the multilib_select
516 and/or multilib_matches strings can be very long, so we build them at
517 run time. */
518 static struct obstack multilib_obstack;
519 static char *multilib_select;
520 static char *multilib_matches;
521 static char *multilib_defaults;
522 #include "multilib.h"
524 /* Check whether a particular argument is a default argument. */
526 #ifndef MULTILIB_DEFAULTS
527 #define MULTILIB_DEFAULTS { "" }
528 #endif
530 static char *multilib_defaults_raw[] = MULTILIB_DEFAULTS;
532 struct user_specs {
533 struct user_specs *next;
534 char *filename;
537 static struct user_specs *user_specs_head, *user_specs_tail;
539 /* This defines which switch letters take arguments. */
541 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
542 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
543 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
544 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
545 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
546 || (CHAR) == 'B' || (CHAR) == 'b')
548 #ifndef SWITCH_TAKES_ARG
549 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
550 #endif
552 /* This defines which multi-letter switches take arguments. */
554 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
555 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
556 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
557 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
558 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
559 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
560 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
562 #ifndef WORD_SWITCH_TAKES_ARG
563 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
564 #endif
567 #ifdef HAVE_EXECUTABLE_SUFFIX
568 /* This defines which switches stop a full compilation. */
569 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
570 ((CHAR) == 'c' || (CHAR) == 'S')
572 #ifndef SWITCH_CURTAILS_COMPILATION
573 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
574 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
575 #endif
576 #endif
578 /* Record the mapping from file suffixes for compilation specs. */
580 struct compiler
582 char *suffix; /* Use this compiler for input files
583 whose names end in this suffix. */
585 char *spec[4]; /* To use this compiler, concatenate these
586 specs and pass to do_spec. */
589 /* Pointer to a vector of `struct compiler' that gives the spec for
590 compiling a file, based on its suffix.
591 A file that does not end in any of these suffixes will be passed
592 unchanged to the loader and nothing else will be done to it.
594 An entry containing two 0s is used to terminate the vector.
596 If multiple entries match a file, the last matching one is used. */
598 static struct compiler *compilers;
600 /* Number of entries in `compilers', not counting the null terminator. */
602 static int n_compilers;
604 /* The default list of file name suffixes and their compilation specs. */
606 static struct compiler default_compilers[] =
608 /* Add lists of suffixes of known languages here. If those languages
609 were not present when we built the driver, we will hit these copies
610 and be given a more meaningful error than "file not used since
611 linking is not done". */
612 {".cc", {"#C++"}}, {".cxx", {"#C++"}}, {".cpp", {"#C++"}}, {".c++", {"#C++"}},
613 {".C", {"#C++"}}, {".ads", {"#Ada"}}, {".adb", {"#Ada"}}, {".ada", {"#Ada"}},
614 {".f", {"#Fortran"}}, {".for", {"#Fortran"}}, {".F", {"#Fortran"}},
615 {".fpp", {"#Fortran"}},
616 {".p", {"#Pascal"}}, {".pas", {"#Pascal"}},
617 /* Next come the entries for C. */
618 {".c", {"@c"}},
619 {"@c",
621 #if USE_CPPLIB
622 "%{E|M|MM:cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
623 %{C:%{!E:%eGNU C does not support -C without using -E}}\
624 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
625 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
626 %{ansi:-trigraphs -D__STRICT_ANSI__}\
627 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
628 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
629 %{traditional} %{ftraditional:-traditional}\
630 %{traditional-cpp:-traditional}\
631 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
632 %i %{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}}\n}\
633 %{!E:%{!M:%{!MM:cc1 %i %1 \
634 -lang-c%{ansi:89} %{nostdinc*} %{A*} %{I*} %I\
635 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
636 %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
637 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
638 %{ansi:-trigraphs -D__STRICT_ANSI__}\
639 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
640 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
641 %{H} %C %{D*} %{U*} %{i*} %Z\
642 %{ftraditional:-traditional}\
643 %{traditional-cpp:-traditional}\
644 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
645 %{aux-info*}\
646 %{--help:--help} \
647 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
648 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
649 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
650 %{!S:as %a %Y\
651 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
652 %{!pipe:%g.s} %A\n }}}}"
654 #else /* ! USE_CPPLIB */
655 "cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
656 %{C:%{!E:%eGNU C does not support -C without using -E}}\
657 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
658 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
659 %{ansi:-trigraphs -D__STRICT_ANSI__}\
660 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
661 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
662 %{traditional} %{ftraditional:-traditional}\
663 %{traditional-cpp:-traditional}\
664 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
665 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
666 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
667 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
668 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
669 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
670 %{aux-info*}\
671 %{--help:--help} \
672 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
673 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
674 %{!S:as %a %Y\
675 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
676 %{!pipe:%g.s} %A\n }}}}"
678 #endif /* ! USE_CPPLIB */
679 {"-",
680 {"%{E:cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
681 %{C:%{!E:%eGNU C does not support -C without using -E}}\
682 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
683 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
684 %{ansi:-trigraphs -D__STRICT_ANSI__}\
685 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
686 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
687 %{traditional} %{ftraditional:-traditional}\
688 %{traditional-cpp:-traditional}\
689 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
690 %i %W{o*}}\
691 %{!E:%e-E required when input is from standard input}"}},
692 {".m", {"@objective-c"}},
693 {"@objective-c",
694 {"cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
695 %{C:%{!E:%eGNU C does not support -C without using -E}}\
696 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
697 -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
698 %{ansi:-trigraphs -D__STRICT_ANSI__}\
699 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
700 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
701 %{traditional} %{ftraditional:-traditional}\
702 %{traditional-cpp:-traditional}\
703 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
704 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
705 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
706 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a*}\
707 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
708 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
709 -lang-objc %{gen-decls} \
710 %{aux-info*}\
711 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
712 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
713 %{!S:as %a %Y\
714 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
715 %{!pipe:%g.s} %A\n }}}}"}},
716 {".h", {"@c-header"}},
717 {"@c-header",
718 {"%{!E:%eCompilation of header file requested} \
719 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
720 %{C:%{!E:%eGNU C does not support -C without using -E}}\
721 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
722 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
723 %{ansi:-trigraphs -D__STRICT_ANSI__}\
724 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
725 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
726 %{traditional} %{ftraditional:-traditional}\
727 %{traditional-cpp:-traditional}\
728 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
729 %i %W{o*}"}},
730 {".i", {"@cpp-output"}},
731 {"@cpp-output",
732 {"%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a*}\
733 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi}\
734 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
735 %{aux-info*}\
736 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
737 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
738 %{!S:as %a %Y\
739 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
740 %{!pipe:%g.s} %A\n }}}}"}},
741 {".s", {"@assembler"}},
742 {"@assembler",
743 {"%{!M:%{!MM:%{!E:%{!S:as %a %Y\
744 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
745 %i %A\n }}}}"}},
746 {".S", {"@assembler-with-cpp"}},
747 {"@assembler-with-cpp",
748 {"cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
749 %{C:%{!E:%eGNU C does not support -C without using -E}}\
750 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
751 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
752 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
753 %{traditional} %{ftraditional:-traditional}\
754 %{traditional-cpp:-traditional}\
755 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
756 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
757 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
758 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
759 %{!pipe:%g.s} %A\n }}}}"}},
760 #include "specs.h"
761 /* Mark end of table */
762 {0, {0}}
765 /* Number of elements in default_compilers, not counting the terminator. */
767 static int n_default_compilers
768 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
770 /* Here is the spec for running the linker, after compiling all files. */
772 /* -u* was put back because both BSD and SysV seem to support it. */
773 /* %{static:} simply prevents an error message if the target machine
774 doesn't handle -static. */
775 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
776 scripts which exist in user specified directories, or in standard
777 directories. */
778 #ifdef LINK_COMMAND_SPEC
779 /* Provide option to override link_command_spec from machine specific
780 configuration files. */
781 static char *link_command_spec =
782 LINK_COMMAND_SPEC;
783 #else
784 #ifdef LINK_LIBGCC_SPECIAL
785 /* Don't generate -L options. */
786 static char *link_command_spec = "\
787 %{!fsyntax-only: \
788 %{!c:%{!M:%{!MM:%{!E:%{!S:%(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
789 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
790 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
791 %{static:} %{L*} %o\
792 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
793 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
794 %{T*}\
795 \n }}}}}}";
796 #else
797 /* Use -L. */
798 static char *link_command_spec = "\
799 %{!fsyntax-only: \
800 %{!c:%{!M:%{!MM:%{!E:%{!S:%(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
801 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
802 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
803 %{static:} %{L*} %D %o\
804 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
805 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
806 %{T*}\
807 \n }}}}}}";
808 #endif
809 #endif
811 /* A vector of options to give to the linker.
812 These options are accumulated by %x,
813 and substituted into the linker command with %X. */
814 static int n_linker_options;
815 static char **linker_options;
817 /* A vector of options to give to the assembler.
818 These options are accumulated by -Wa,
819 and substituted into the assembler command with %Y. */
820 static int n_assembler_options;
821 static char **assembler_options;
823 /* A vector of options to give to the preprocessor.
824 These options are accumulated by -Wp,
825 and substituted into the preprocessor command with %Z. */
826 static int n_preprocessor_options;
827 static char **preprocessor_options;
829 /* Define how to map long options into short ones. */
831 /* This structure describes one mapping. */
832 struct option_map
834 /* The long option's name. */
835 char *name;
836 /* The equivalent short option. */
837 char *equivalent;
838 /* Argument info. A string of flag chars; NULL equals no options.
839 a => argument required.
840 o => argument optional.
841 j => join argument to equivalent, making one word.
842 * => require other text after NAME as an argument. */
843 char *arg_info;
846 /* This is the table of mappings. Mappings are tried sequentially
847 for each option encountered; the first one that matches, wins. */
849 struct option_map option_map[] =
851 {"--all-warnings", "-Wall", 0},
852 {"--ansi", "-ansi", 0},
853 {"--assemble", "-S", 0},
854 {"--assert", "-A", "a"},
855 {"--classpath", "-fclasspath=", "aj"},
856 {"--CLASSPATH", "-fCLASSPATH=", "aj"},
857 {"--comments", "-C", 0},
858 {"--compile", "-c", 0},
859 {"--debug", "-g", "oj"},
860 {"--define-macro", "-D", "aj"},
861 {"--dependencies", "-M", 0},
862 {"--dump", "-d", "a"},
863 {"--dumpbase", "-dumpbase", "a"},
864 {"--entry", "-e", 0},
865 {"--extra-warnings", "-W", 0},
866 {"--for-assembler", "-Wa", "a"},
867 {"--for-linker", "-Xlinker", "a"},
868 {"--force-link", "-u", "a"},
869 {"--imacros", "-imacros", "a"},
870 {"--include", "-include", "a"},
871 {"--include-barrier", "-I-", 0},
872 {"--include-directory", "-I", "aj"},
873 {"--include-directory-after", "-idirafter", "a"},
874 {"--include-prefix", "-iprefix", "a"},
875 {"--include-with-prefix", "-iwithprefix", "a"},
876 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
877 {"--include-with-prefix-after", "-iwithprefix", "a"},
878 {"--language", "-x", "a"},
879 {"--library-directory", "-L", "a"},
880 {"--machine", "-m", "aj"},
881 {"--machine-", "-m", "*j"},
882 {"--no-line-commands", "-P", 0},
883 {"--no-precompiled-includes", "-noprecomp", 0},
884 {"--no-standard-includes", "-nostdinc", 0},
885 {"--no-standard-libraries", "-nostdlib", 0},
886 {"--no-warnings", "-w", 0},
887 {"--optimize", "-O", "oj"},
888 {"--output", "-o", "a"},
889 {"--pedantic", "-pedantic", 0},
890 {"--pedantic-errors", "-pedantic-errors", 0},
891 {"--pipe", "-pipe", 0},
892 {"--prefix", "-B", "a"},
893 {"--preprocess", "-E", 0},
894 {"--print-search-dirs", "-print-search-dirs", 0},
895 {"--print-file-name", "-print-file-name=", "aj"},
896 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
897 {"--print-missing-file-dependencies", "-MG", 0},
898 {"--print-multi-lib", "-print-multi-lib", 0},
899 {"--print-multi-directory", "-print-multi-directory", 0},
900 {"--print-prog-name", "-print-prog-name=", "aj"},
901 {"--profile", "-p", 0},
902 {"--profile-blocks", "-a", 0},
903 {"--quiet", "-q", 0},
904 {"--save-temps", "-save-temps", 0},
905 {"--shared", "-shared", 0},
906 {"--silent", "-q", 0},
907 {"--specs", "-specs=", "aj"},
908 {"--static", "-static", 0},
909 {"--symbolic", "-symbolic", 0},
910 {"--target", "-b", "a"},
911 {"--trace-includes", "-H", 0},
912 {"--traditional", "-traditional", 0},
913 {"--traditional-cpp", "-traditional-cpp", 0},
914 {"--trigraphs", "-trigraphs", 0},
915 {"--undefine-macro", "-U", "aj"},
916 {"--use-version", "-V", "a"},
917 {"--user-dependencies", "-MM", 0},
918 {"--verbose", "-v", 0},
919 {"--version", "-dumpversion", 0},
920 {"--warn-", "-W", "*j"},
921 {"--write-dependencies", "-MD", 0},
922 {"--write-user-dependencies", "-MMD", 0},
923 {"--", "-f", "*j"}
926 /* Translate the options described by *ARGCP and *ARGVP.
927 Make a new vector and store it back in *ARGVP,
928 and store its length in *ARGVC. */
930 static void
931 translate_options (argcp, argvp)
932 int *argcp;
933 char ***argvp;
935 int i;
936 int argc = *argcp;
937 char **argv = *argvp;
938 char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
939 int newindex = 0;
941 i = 0;
942 newv[newindex++] = argv[i++];
944 while (i < argc)
946 /* Translate -- options. */
947 if (argv[i][0] == '-' && argv[i][1] == '-')
949 size_t j;
950 /* Find a mapping that applies to this option. */
951 for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
953 size_t optlen = strlen (option_map[j].name);
954 size_t arglen = strlen (argv[i]);
955 size_t complen = arglen > optlen ? optlen : arglen;
956 char *arginfo = option_map[j].arg_info;
958 if (arginfo == 0)
959 arginfo = "";
961 if (!strncmp (argv[i], option_map[j].name, complen))
963 char *arg = 0;
965 if (arglen < optlen)
967 size_t k;
968 for (k = j + 1;
969 k < sizeof (option_map) / sizeof (option_map[0]);
970 k++)
971 if (strlen (option_map[k].name) >= arglen
972 && !strncmp (argv[i], option_map[k].name, arglen))
974 error ("Ambiguous abbreviation %s", argv[i]);
975 break;
978 if (k != sizeof (option_map) / sizeof (option_map[0]))
979 break;
982 if (arglen > optlen)
984 /* If the option has an argument, accept that. */
985 if (argv[i][optlen] == '=')
986 arg = argv[i] + optlen + 1;
988 /* If this mapping requires extra text at end of name,
989 accept that as "argument". */
990 else if (index (arginfo, '*') != 0)
991 arg = argv[i] + optlen;
993 /* Otherwise, extra text at end means mismatch.
994 Try other mappings. */
995 else
996 continue;
999 else if (index (arginfo, '*') != 0)
1001 error ("Incomplete `%s' option", option_map[j].name);
1002 break;
1005 /* Handle arguments. */
1006 if (index (arginfo, 'a') != 0)
1008 if (arg == 0)
1010 if (i + 1 == argc)
1012 error ("Missing argument to `%s' option",
1013 option_map[j].name);
1014 break;
1017 arg = argv[++i];
1020 else if (index (arginfo, '*') != 0)
1022 else if (index (arginfo, 'o') == 0)
1024 if (arg != 0)
1025 error ("Extraneous argument to `%s' option",
1026 option_map[j].name);
1027 arg = 0;
1030 /* Store the translation as one argv elt or as two. */
1031 if (arg != 0 && index (arginfo, 'j') != 0)
1032 newv[newindex++] = concat (option_map[j].equivalent, arg,
1033 NULL_PTR);
1034 else if (arg != 0)
1036 newv[newindex++] = option_map[j].equivalent;
1037 newv[newindex++] = arg;
1039 else
1040 newv[newindex++] = option_map[j].equivalent;
1042 break;
1045 i++;
1048 /* Handle old-fashioned options--just copy them through,
1049 with their arguments. */
1050 else if (argv[i][0] == '-')
1052 char *p = argv[i] + 1;
1053 int c = *p;
1054 int nskip = 1;
1056 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1057 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1058 else if (WORD_SWITCH_TAKES_ARG (p))
1059 nskip += WORD_SWITCH_TAKES_ARG (p);
1060 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1061 && p[1] == 0)
1062 nskip += 1;
1063 else if (! strcmp (p, "Xlinker"))
1064 nskip += 1;
1066 /* Watch out for an option at the end of the command line that
1067 is missing arguments, and avoid skipping past the end of the
1068 command line. */
1069 if (nskip + i > argc)
1070 nskip = argc - i;
1072 while (nskip > 0)
1074 newv[newindex++] = argv[i++];
1075 nskip--;
1078 else
1079 /* Ordinary operands, or +e options. */
1080 newv[newindex++] = argv[i++];
1083 newv[newindex] = 0;
1085 *argvp = newv;
1086 *argcp = newindex;
1089 char *
1090 my_strerror(e)
1091 int e;
1093 #ifdef HAVE_STRERROR
1095 return strerror(e);
1097 #else
1099 static char buffer[30];
1100 if (!e)
1101 return "cannot access";
1103 if (e > 0 && e < sys_nerr)
1104 return sys_errlist[e];
1106 sprintf (buffer, "Unknown error %d", e);
1107 return buffer;
1108 #endif
1111 static char *
1112 skip_whitespace (p)
1113 char *p;
1115 while (1)
1117 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1118 be considered whitespace. */
1119 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1120 return p + 1;
1121 else if (*p == '\n' || *p == ' ' || *p == '\t')
1122 p++;
1123 else if (*p == '#')
1125 while (*p != '\n') p++;
1126 p++;
1128 else
1129 break;
1132 return p;
1135 /* Structure to keep track of the specs that have been defined so far.
1136 These are accessed using %(specname) or %[specname] in a compiler
1137 or link spec. */
1139 struct spec_list
1141 /* The following 2 fields must be first */
1142 /* to allow EXTRA_SPECS to be initialized */
1143 char *name; /* name of the spec. */
1144 char *ptr; /* available ptr if no static pointer */
1146 /* The following fields are not initialized */
1147 /* by EXTRA_SPECS */
1148 char **ptr_spec; /* pointer to the spec itself. */
1149 struct spec_list *next; /* Next spec in linked list. */
1150 int name_len; /* length of the name */
1151 int alloc_p; /* whether string was allocated */
1154 #define INIT_STATIC_SPEC(NAME,PTR) \
1155 { NAME, NULL_PTR, PTR, (struct spec_list *)0, sizeof (NAME)-1, 0 }
1157 /* List of statically defined specs */
1158 static struct spec_list static_specs[] = {
1159 INIT_STATIC_SPEC ("asm", &asm_spec),
1160 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1161 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1162 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1163 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1164 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1165 INIT_STATIC_SPEC ("link", &link_spec),
1166 INIT_STATIC_SPEC ("lib", &lib_spec),
1167 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1168 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1169 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1170 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1171 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1172 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1173 INIT_STATIC_SPEC ("version", &compiler_version),
1174 INIT_STATIC_SPEC ("multilib", &multilib_select),
1175 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1176 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1177 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1178 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1181 #ifdef EXTRA_SPECS /* additional specs needed */
1182 static struct spec_list extra_specs[] = { EXTRA_SPECS };
1183 #endif
1185 /* List of dynamically allocates specs that have been defined so far. */
1187 static struct spec_list *specs = (struct spec_list *)0;
1190 /* Initialize the specs lookup routines. */
1192 static void
1193 init_spec ()
1195 struct spec_list *next = (struct spec_list *)0;
1196 struct spec_list *sl = (struct spec_list *)0;
1197 int i;
1199 if (specs)
1200 return; /* already initialized */
1202 if (verbose_flag)
1203 fprintf (stderr, "Using builtin specs.\n");
1205 #ifdef EXTRA_SPECS
1206 for (i = (sizeof (extra_specs) / sizeof (extra_specs[0])) - 1; i >= 0; i--)
1208 sl = &extra_specs[i];
1209 sl->next = next;
1210 sl->name_len = strlen (sl->name);
1211 sl->ptr_spec = &sl->ptr;
1212 next = sl;
1214 #endif
1216 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1; i >= 0; i--)
1218 sl = &static_specs[i];
1219 sl->next = next;
1220 next = sl;
1223 specs = sl;
1227 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1228 removed; If the spec starts with a + then SPEC is added to the end of the
1229 current spec. */
1231 static void
1232 set_spec (name, spec)
1233 char *name;
1234 char *spec;
1236 struct spec_list *sl;
1237 char *old_spec;
1238 int name_len = strlen (name);
1239 int i;
1241 /* If this is the first call, initialize the statically allocated specs */
1242 if (!specs)
1244 struct spec_list *next = (struct spec_list *)0;
1245 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1;
1246 i >= 0; i--)
1248 sl = &static_specs[i];
1249 sl->next = next;
1250 next = sl;
1252 specs = sl;
1255 /* See if the spec already exists */
1256 for (sl = specs; sl; sl = sl->next)
1257 if (name_len == sl->name_len && !strcmp (sl->name, name))
1258 break;
1260 if (!sl)
1262 /* Not found - make it */
1263 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1264 sl->name = save_string (name, strlen (name));
1265 sl->name_len = name_len;
1266 sl->ptr_spec = &sl->ptr;
1267 sl->alloc_p = 0;
1268 *(sl->ptr_spec) = "";
1269 sl->next = specs;
1270 specs = sl;
1273 old_spec = *(sl->ptr_spec);
1274 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1275 ? concat (old_spec, spec + 1, NULL_PTR)
1276 : save_string (spec, strlen (spec)));
1278 #ifdef DEBUG_SPECS
1279 if (verbose_flag)
1280 fprintf (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1281 #endif
1283 /* Free the old spec */
1284 if (old_spec && sl->alloc_p)
1285 free (old_spec);
1287 sl->alloc_p = 1;
1290 /* Accumulate a command (program name and args), and run it. */
1292 /* Vector of pointers to arguments in the current line of specifications. */
1294 static char **argbuf;
1296 /* Number of elements allocated in argbuf. */
1298 static int argbuf_length;
1300 /* Number of elements in argbuf currently in use (containing args). */
1302 static int argbuf_index;
1304 /* We want this on by default all the time now. */
1305 #define MKTEMP_EACH_FILE
1307 #ifdef MKTEMP_EACH_FILE
1309 extern char *make_temp_file PROTO((char *));
1311 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1312 temp file. */
1314 static struct temp_name {
1315 char *suffix; /* suffix associated with the code. */
1316 int length; /* strlen (suffix). */
1317 int unique; /* Indicates whether %g or %u/%U was used. */
1318 char *filename; /* associated filename. */
1319 int filename_length; /* strlen (filename). */
1320 struct temp_name *next;
1321 } *temp_names;
1322 #else
1323 extern char *choose_temp_base PROTO((void));
1324 #endif
1327 /* Number of commands executed so far. */
1329 static int execution_count;
1331 /* Number of commands that exited with a signal. */
1333 static int signal_count;
1335 /* Name with which this program was invoked. */
1337 static char *programname;
1339 /* Structures to keep track of prefixes to try when looking for files. */
1341 struct prefix_list
1343 char *prefix; /* String to prepend to the path. */
1344 struct prefix_list *next; /* Next in linked list. */
1345 int require_machine_suffix; /* Don't use without machine_suffix. */
1346 /* 2 means try both machine_suffix and just_machine_suffix. */
1347 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1350 struct path_prefix
1352 struct prefix_list *plist; /* List of prefixes to try */
1353 int max_len; /* Max length of a prefix in PLIST */
1354 char *name; /* Name of this list (used in config stuff) */
1357 /* List of prefixes to try when looking for executables. */
1359 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1361 /* List of prefixes to try when looking for startup (crt0) files. */
1363 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1365 /* List of prefixes to try when looking for include files. */
1367 static struct path_prefix include_prefixes = { 0, 0, "include" };
1369 /* Suffix to attach to directories searched for commands.
1370 This looks like `MACHINE/VERSION/'. */
1372 static char *machine_suffix = 0;
1374 /* Suffix to attach to directories searched for commands.
1375 This is just `MACHINE/'. */
1377 static char *just_machine_suffix = 0;
1379 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1381 static char *gcc_exec_prefix;
1383 /* Default prefixes to attach to command names. */
1385 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1386 #undef MD_EXEC_PREFIX
1387 #undef MD_STARTFILE_PREFIX
1388 #undef MD_STARTFILE_PREFIX_1
1389 #endif
1391 #ifndef STANDARD_EXEC_PREFIX
1392 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1393 #endif /* !defined STANDARD_EXEC_PREFIX */
1395 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1396 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1397 #ifdef MD_EXEC_PREFIX
1398 static char *md_exec_prefix = MD_EXEC_PREFIX;
1399 #endif
1401 #ifndef STANDARD_STARTFILE_PREFIX
1402 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1403 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1405 #ifdef MD_STARTFILE_PREFIX
1406 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1407 #endif
1408 #ifdef MD_STARTFILE_PREFIX_1
1409 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1410 #endif
1411 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1412 static char *standard_startfile_prefix_1 = "/lib/";
1413 static char *standard_startfile_prefix_2 = "/usr/lib/";
1415 #ifndef TOOLDIR_BASE_PREFIX
1416 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1417 #endif
1418 static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1419 static char *tooldir_prefix;
1421 /* Subdirectory to use for locating libraries. Set by
1422 set_multilib_dir based on the compilation options. */
1424 static char *multilib_dir;
1426 /* Clear out the vector of arguments (after a command is executed). */
1428 static void
1429 clear_args ()
1431 argbuf_index = 0;
1434 /* Add one argument to the vector at the end.
1435 This is done when a space is seen or at the end of the line.
1436 If DELETE_ALWAYS is nonzero, the arg is a filename
1437 and the file should be deleted eventually.
1438 If DELETE_FAILURE is nonzero, the arg is a filename
1439 and the file should be deleted if this compilation fails. */
1441 static void
1442 store_arg (arg, delete_always, delete_failure)
1443 char *arg;
1444 int delete_always, delete_failure;
1446 if (argbuf_index + 1 == argbuf_length)
1447 argbuf
1448 = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1450 argbuf[argbuf_index++] = arg;
1451 argbuf[argbuf_index] = 0;
1453 if (delete_always || delete_failure)
1454 record_temp_file (arg, delete_always, delete_failure);
1457 /* Read compilation specs from a file named FILENAME,
1458 replacing the default ones.
1460 A suffix which starts with `*' is a definition for
1461 one of the machine-specific sub-specs. The "suffix" should be
1462 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1463 The corresponding spec is stored in asm_spec, etc.,
1464 rather than in the `compilers' vector.
1466 Anything invalid in the file is a fatal error. */
1468 static void
1469 read_specs (filename, main_p)
1470 char *filename;
1471 int main_p;
1473 int desc;
1474 int readlen;
1475 struct stat statbuf;
1476 char *buffer;
1477 register char *p;
1479 if (verbose_flag)
1480 fprintf (stderr, "Reading specs from %s\n", filename);
1482 /* Open and stat the file. */
1483 desc = open (filename, O_RDONLY, 0);
1484 if (desc < 0)
1485 pfatal_with_name (filename);
1486 if (stat (filename, &statbuf) < 0)
1487 pfatal_with_name (filename);
1489 /* Read contents of file into BUFFER. */
1490 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1491 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1492 if (readlen < 0)
1493 pfatal_with_name (filename);
1494 buffer[readlen] = 0;
1495 close (desc);
1497 /* Scan BUFFER for specs, putting them in the vector. */
1498 p = buffer;
1499 while (1)
1501 char *suffix;
1502 char *spec;
1503 char *in, *out, *p1, *p2, *p3;
1505 /* Advance P in BUFFER to the next nonblank nocomment line. */
1506 p = skip_whitespace (p);
1507 if (*p == 0)
1508 break;
1510 /* Is this a special command that starts with '%'? */
1511 /* Don't allow this for the main specs file, since it would
1512 encourage people to overwrite it. */
1513 if (*p == '%' && !main_p)
1515 p1 = p;
1516 while (*p && *p != '\n')
1517 p++;
1519 p++; /* Skip '\n' */
1521 if (!strncmp (p1, "%include", sizeof ("%include")-1)
1522 && (p1[sizeof "%include" - 1] == ' '
1523 || p1[sizeof "%include" - 1] == '\t'))
1525 char *new_filename;
1527 p1 += sizeof ("%include");
1528 while (*p1 == ' ' || *p1 == '\t')
1529 p1++;
1531 if (*p1++ != '<' || p[-2] != '>')
1532 fatal ("specs %%include syntax malformed after %d characters",
1533 p1 - buffer + 1);
1535 p[-2] = '\0';
1536 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1537 read_specs (new_filename ? new_filename : p1, FALSE);
1538 continue;
1540 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1541 && (p1[sizeof "%include_noerr" - 1] == ' '
1542 || p1[sizeof "%include_noerr" - 1] == '\t'))
1544 char *new_filename;
1546 p1 += sizeof "%include_noerr";
1547 while (*p1 == ' ' || *p1 == '\t') p1++;
1549 if (*p1++ != '<' || p[-2] != '>')
1550 fatal ("specs %%include syntax malformed after %d characters",
1551 p1 - buffer + 1);
1553 p[-2] = '\0';
1554 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1555 if (new_filename)
1556 read_specs (new_filename, FALSE);
1557 else if (verbose_flag)
1558 fprintf (stderr, "Could not find specs file %s\n", p1);
1559 continue;
1561 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1562 && (p1[sizeof "%rename" - 1] == ' '
1563 || p1[sizeof "%rename" - 1] == '\t'))
1565 int name_len;
1566 struct spec_list *sl;
1568 /* Get original name */
1569 p1 += sizeof "%rename";
1570 while (*p1 == ' ' || *p1 == '\t')
1571 p1++;
1573 if (! ISALPHA ((unsigned char)*p1))
1574 fatal ("specs %%rename syntax malformed after %d characters",
1575 p1 - buffer);
1577 p2 = p1;
1578 while (*p2 && !ISSPACE ((unsigned char)*p2))
1579 p2++;
1581 if (*p2 != ' ' && *p2 != '\t')
1582 fatal ("specs %%rename syntax malformed after %d characters",
1583 p2 - buffer);
1585 name_len = p2 - p1;
1586 *p2++ = '\0';
1587 while (*p2 == ' ' || *p2 == '\t')
1588 p2++;
1590 if (! ISALPHA ((unsigned char)*p2))
1591 fatal ("specs %%rename syntax malformed after %d characters",
1592 p2 - buffer);
1594 /* Get new spec name */
1595 p3 = p2;
1596 while (*p3 && !ISSPACE ((unsigned char)*p3))
1597 p3++;
1599 if (p3 != p-1)
1600 fatal ("specs %%rename syntax malformed after %d characters",
1601 p3 - buffer);
1602 *p3 = '\0';
1604 for (sl = specs; sl; sl = sl->next)
1605 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1606 break;
1608 if (!sl)
1609 fatal ("specs %s spec was not found to be renamed", p1);
1611 if (strcmp (p1, p2) == 0)
1612 continue;
1614 if (verbose_flag)
1616 fprintf (stderr, "rename spec %s to %s\n", p1, p2);
1617 #ifdef DEBUG_SPECS
1618 fprintf (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
1619 #endif
1622 set_spec (p2, *(sl->ptr_spec));
1623 if (sl->alloc_p)
1624 free (*(sl->ptr_spec));
1626 *(sl->ptr_spec) = "";
1627 sl->alloc_p = 0;
1628 continue;
1630 else
1631 fatal ("specs unknown %% command after %d characters",
1632 p1 - buffer);
1635 /* Find the colon that should end the suffix. */
1636 p1 = p;
1637 while (*p1 && *p1 != ':' && *p1 != '\n')
1638 p1++;
1640 /* The colon shouldn't be missing. */
1641 if (*p1 != ':')
1642 fatal ("specs file malformed after %d characters", p1 - buffer);
1644 /* Skip back over trailing whitespace. */
1645 p2 = p1;
1646 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1647 p2--;
1649 /* Copy the suffix to a string. */
1650 suffix = save_string (p, p2 - p);
1651 /* Find the next line. */
1652 p = skip_whitespace (p1 + 1);
1653 if (p[1] == 0)
1654 fatal ("specs file malformed after %d characters", p - buffer);
1656 p1 = p;
1657 /* Find next blank line or end of string. */
1658 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1659 p1++;
1661 /* Specs end at the blank line and do not include the newline. */
1662 spec = save_string (p, p1 - p);
1663 p = p1;
1665 /* Delete backslash-newline sequences from the spec. */
1666 in = spec;
1667 out = spec;
1668 while (*in != 0)
1670 if (in[0] == '\\' && in[1] == '\n')
1671 in += 2;
1672 else if (in[0] == '#')
1673 while (*in && *in != '\n')
1674 in++;
1676 else
1677 *out++ = *in++;
1679 *out = 0;
1681 if (suffix[0] == '*')
1683 if (! strcmp (suffix, "*link_command"))
1684 link_command_spec = spec;
1685 else
1686 set_spec (suffix + 1, spec);
1688 else
1690 /* Add this pair to the vector. */
1691 compilers
1692 = ((struct compiler *)
1693 xrealloc (compilers,
1694 (n_compilers + 2) * sizeof (struct compiler)));
1696 compilers[n_compilers].suffix = suffix;
1697 bzero ((char *) compilers[n_compilers].spec,
1698 sizeof compilers[n_compilers].spec);
1699 compilers[n_compilers].spec[0] = spec;
1700 n_compilers++;
1701 bzero ((char *) &compilers[n_compilers],
1702 sizeof compilers[n_compilers]);
1705 if (*suffix == 0)
1706 link_command_spec = spec;
1709 if (link_command_spec == 0)
1710 fatal ("spec file has no spec for linking");
1713 /* Record the names of temporary files we tell compilers to write,
1714 and delete them at the end of the run. */
1716 /* This is the common prefix we use to make temp file names.
1717 It is chosen once for each run of this program.
1718 It is substituted into a spec by %g.
1719 Thus, all temp file names contain this prefix.
1720 In practice, all temp file names start with this prefix.
1722 This prefix comes from the envvar TMPDIR if it is defined;
1723 otherwise, from the P_tmpdir macro if that is defined;
1724 otherwise, in /usr/tmp or /tmp;
1725 or finally the current directory if all else fails. */
1727 static char *temp_filename;
1729 /* Length of the prefix. */
1731 static int temp_filename_length;
1733 /* Define the list of temporary files to delete. */
1735 struct temp_file
1737 char *name;
1738 struct temp_file *next;
1741 /* Queue of files to delete on success or failure of compilation. */
1742 static struct temp_file *always_delete_queue;
1743 /* Queue of files to delete on failure of compilation. */
1744 static struct temp_file *failure_delete_queue;
1746 /* Record FILENAME as a file to be deleted automatically.
1747 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1748 otherwise delete it in any case.
1749 FAIL_DELETE nonzero means delete it if a compilation step fails;
1750 otherwise delete it in any case. */
1752 static void
1753 record_temp_file (filename, always_delete, fail_delete)
1754 char *filename;
1755 int always_delete;
1756 int fail_delete;
1758 register char *name;
1759 name = xmalloc (strlen (filename) + 1);
1760 strcpy (name, filename);
1762 if (always_delete)
1764 register struct temp_file *temp;
1765 for (temp = always_delete_queue; temp; temp = temp->next)
1766 if (! strcmp (name, temp->name))
1767 goto already1;
1769 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1770 temp->next = always_delete_queue;
1771 temp->name = name;
1772 always_delete_queue = temp;
1774 already1:;
1777 if (fail_delete)
1779 register struct temp_file *temp;
1780 for (temp = failure_delete_queue; temp; temp = temp->next)
1781 if (! strcmp (name, temp->name))
1782 goto already2;
1784 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1785 temp->next = failure_delete_queue;
1786 temp->name = name;
1787 failure_delete_queue = temp;
1789 already2:;
1793 /* Delete all the temporary files whose names we previously recorded. */
1795 static void
1796 delete_if_ordinary (name)
1797 char *name;
1799 struct stat st;
1800 #ifdef DEBUG
1801 int i, c;
1803 printf ("Delete %s? (y or n) ", name);
1804 fflush (stdout);
1805 i = getchar ();
1806 if (i != '\n')
1807 while ((c = getchar ()) != '\n' && c != EOF)
1810 if (i == 'y' || i == 'Y')
1811 #endif /* DEBUG */
1812 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1813 if (unlink (name) < 0)
1814 if (verbose_flag)
1815 perror_with_name (name);
1818 static void
1819 delete_temp_files ()
1821 register struct temp_file *temp;
1823 for (temp = always_delete_queue; temp; temp = temp->next)
1824 delete_if_ordinary (temp->name);
1825 always_delete_queue = 0;
1828 /* Delete all the files to be deleted on error. */
1830 static void
1831 delete_failure_queue ()
1833 register struct temp_file *temp;
1835 for (temp = failure_delete_queue; temp; temp = temp->next)
1836 delete_if_ordinary (temp->name);
1839 static void
1840 clear_failure_queue ()
1842 failure_delete_queue = 0;
1845 /* Routine to add variables to the environment. We do this to pass
1846 the pathname of the gcc driver, and the directories search to the
1847 collect2 program, which is being run as ld. This way, we can be
1848 sure of executing the right compiler when collect2 wants to build
1849 constructors and destructors. Since the environment variables we
1850 use come from an obstack, we don't have to worry about allocating
1851 space for them. */
1853 #ifndef HAVE_PUTENV
1855 void
1856 putenv (str)
1857 char *str;
1859 #ifndef VMS /* nor about VMS */
1861 extern char **environ;
1862 char **old_environ = environ;
1863 char **envp;
1864 int num_envs = 0;
1865 int name_len = 1;
1866 int str_len = strlen (str);
1867 char *p = str;
1868 int ch;
1870 while ((ch = *p++) != '\0' && ch != '=')
1871 name_len++;
1873 if (!ch)
1874 abort ();
1876 /* Search for replacing an existing environment variable, and
1877 count the number of total environment variables. */
1878 for (envp = old_environ; *envp; envp++)
1880 num_envs++;
1881 if (!strncmp (str, *envp, name_len))
1883 *envp = str;
1884 return;
1888 /* Add a new environment variable */
1889 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1890 *environ = str;
1891 memcpy ((char *) (environ + 1), (char *) old_environ,
1892 sizeof (char *) * (num_envs+1));
1894 #endif /* VMS */
1897 #endif /* HAVE_PUTENV */
1900 /* Build a list of search directories from PATHS.
1901 PREFIX is a string to prepend to the list.
1902 If CHECK_DIR_P is non-zero we ensure the directory exists.
1903 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
1904 It is also used by the --print-search-dirs flag. */
1906 static char *
1907 build_search_list (paths, prefix, check_dir_p)
1908 struct path_prefix *paths;
1909 char *prefix;
1910 int check_dir_p;
1912 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1913 int just_suffix_len
1914 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1915 int first_time = TRUE;
1916 struct prefix_list *pprefix;
1918 obstack_grow (&collect_obstack, prefix, strlen (prefix));
1920 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1922 int len = strlen (pprefix->prefix);
1924 if (machine_suffix
1925 && (! check_dir_p
1926 || is_directory (pprefix->prefix, machine_suffix, 0)))
1928 if (!first_time)
1929 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1931 first_time = FALSE;
1932 obstack_grow (&collect_obstack, pprefix->prefix, len);
1933 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1936 if (just_machine_suffix
1937 && pprefix->require_machine_suffix == 2
1938 && (! check_dir_p
1939 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
1941 if (! first_time)
1942 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1944 first_time = FALSE;
1945 obstack_grow (&collect_obstack, pprefix->prefix, len);
1946 obstack_grow (&collect_obstack, just_machine_suffix,
1947 just_suffix_len);
1950 if (! pprefix->require_machine_suffix)
1952 if (! first_time)
1953 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1955 first_time = FALSE;
1956 obstack_grow (&collect_obstack, pprefix->prefix, len);
1960 obstack_1grow (&collect_obstack, '\0');
1961 return obstack_finish (&collect_obstack);
1964 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
1965 for collect. */
1967 static void
1968 putenv_from_prefixes (paths, env_var)
1969 struct path_prefix *paths;
1970 char *env_var;
1972 putenv (build_search_list (paths, env_var, 1));
1975 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1976 access to check permissions.
1977 Return 0 if not found, otherwise return its name, allocated with malloc. */
1979 static char *
1980 find_a_file (pprefix, name, mode)
1981 struct path_prefix *pprefix;
1982 char *name;
1983 int mode;
1985 char *temp;
1986 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1987 struct prefix_list *pl;
1988 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1990 #ifdef DEFAULT_ASSEMBLER
1991 if (! strcmp(name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0) {
1992 name = DEFAULT_ASSEMBLER;
1993 len = strlen(name)+1;
1994 temp = xmalloc (len);
1995 strcpy (temp, name);
1996 return temp;
1998 #endif
2000 #ifdef DEFAULT_LINKER
2001 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0) {
2002 name = DEFAULT_LINKER;
2003 len = strlen(name)+1;
2004 temp = xmalloc (len);
2005 strcpy (temp, name);
2006 return temp;
2008 #endif
2010 if (machine_suffix)
2011 len += strlen (machine_suffix);
2013 temp = xmalloc (len);
2015 /* Determine the filename to execute (special case for absolute paths). */
2017 if (*name == '/' || *name == DIR_SEPARATOR
2018 /* Check for disk name on MS-DOS-based systems. */
2019 || (DIR_SEPARATOR == '\\' && name[1] == ':'
2020 && (name[2] == DIR_SEPARATOR || name[2] == '/')))
2022 if (access (name, mode) == 0)
2024 strcpy (temp, name);
2025 return temp;
2028 else
2029 for (pl = pprefix->plist; pl; pl = pl->next)
2031 if (machine_suffix)
2033 /* Some systems have a suffix for executable files.
2034 So try appending that first. */
2035 if (file_suffix[0] != 0)
2037 strcpy (temp, pl->prefix);
2038 strcat (temp, machine_suffix);
2039 strcat (temp, name);
2040 strcat (temp, file_suffix);
2041 if (access (temp, mode) == 0)
2043 if (pl->used_flag_ptr != 0)
2044 *pl->used_flag_ptr = 1;
2045 return temp;
2049 /* Now try just the name. */
2050 strcpy (temp, pl->prefix);
2051 strcat (temp, machine_suffix);
2052 strcat (temp, name);
2053 if (access (temp, mode) == 0)
2055 if (pl->used_flag_ptr != 0)
2056 *pl->used_flag_ptr = 1;
2057 return temp;
2061 /* Certain prefixes are tried with just the machine type,
2062 not the version. This is used for finding as, ld, etc. */
2063 if (just_machine_suffix && pl->require_machine_suffix == 2)
2065 /* Some systems have a suffix for executable files.
2066 So try appending that first. */
2067 if (file_suffix[0] != 0)
2069 strcpy (temp, pl->prefix);
2070 strcat (temp, just_machine_suffix);
2071 strcat (temp, name);
2072 strcat (temp, file_suffix);
2073 if (access (temp, mode) == 0)
2075 if (pl->used_flag_ptr != 0)
2076 *pl->used_flag_ptr = 1;
2077 return temp;
2081 strcpy (temp, pl->prefix);
2082 strcat (temp, just_machine_suffix);
2083 strcat (temp, name);
2084 if (access (temp, mode) == 0)
2086 if (pl->used_flag_ptr != 0)
2087 *pl->used_flag_ptr = 1;
2088 return temp;
2092 /* Certain prefixes can't be used without the machine suffix
2093 when the machine or version is explicitly specified. */
2094 if (! pl->require_machine_suffix)
2096 /* Some systems have a suffix for executable files.
2097 So try appending that first. */
2098 if (file_suffix[0] != 0)
2100 strcpy (temp, pl->prefix);
2101 strcat (temp, name);
2102 strcat (temp, file_suffix);
2103 if (access (temp, mode) == 0)
2105 if (pl->used_flag_ptr != 0)
2106 *pl->used_flag_ptr = 1;
2107 return temp;
2111 strcpy (temp, pl->prefix);
2112 strcat (temp, name);
2113 if (access (temp, mode) == 0)
2115 if (pl->used_flag_ptr != 0)
2116 *pl->used_flag_ptr = 1;
2117 return temp;
2122 free (temp);
2123 return 0;
2126 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
2127 at the start of the list, otherwise it goes at the end.
2129 If WARN is nonzero, we will warn if no file is found
2130 through this prefix. WARN should point to an int
2131 which will be set to 1 if this entry is used.
2133 COMPONENT is the value to be passed to update_path.
2135 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2136 the complete value of machine_suffix.
2137 2 means try both machine_suffix and just_machine_suffix. */
2139 static void
2140 add_prefix (pprefix, prefix, component, first, require_machine_suffix, warn)
2141 struct path_prefix *pprefix;
2142 char *prefix;
2143 char *component;
2144 int first;
2145 int require_machine_suffix;
2146 int *warn;
2148 struct prefix_list *pl, **prev;
2149 int len;
2151 if (! first && pprefix->plist)
2153 for (pl = pprefix->plist; pl->next; pl = pl->next)
2155 prev = &pl->next;
2157 else
2158 prev = &pprefix->plist;
2160 /* Keep track of the longest prefix */
2162 prefix = update_path (prefix, component);
2163 len = strlen (prefix);
2164 if (len > pprefix->max_len)
2165 pprefix->max_len = len;
2167 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2168 pl->prefix = save_string (prefix, len);
2169 pl->require_machine_suffix = require_machine_suffix;
2170 pl->used_flag_ptr = warn;
2171 if (warn)
2172 *warn = 0;
2174 if (*prev)
2175 pl->next = *prev;
2176 else
2177 pl->next = (struct prefix_list *) 0;
2178 *prev = pl;
2181 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
2183 static void
2184 unused_prefix_warnings (pprefix)
2185 struct path_prefix *pprefix;
2187 struct prefix_list *pl = pprefix->plist;
2189 while (pl)
2191 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
2193 if (pl->require_machine_suffix && machine_suffix)
2194 error ("file path prefix `%s%s' never used", pl->prefix,
2195 machine_suffix);
2196 else
2197 error ("file path prefix `%s' never used", pl->prefix);
2199 /* Prevent duplicate warnings. */
2200 *pl->used_flag_ptr = 1;
2203 pl = pl->next;
2208 /* Execute the command specified by the arguments on the current line of spec.
2209 When using pipes, this includes several piped-together commands
2210 with `|' between them.
2212 Return 0 if successful, -1 if failed. */
2214 static int
2215 execute ()
2217 int i;
2218 int n_commands; /* # of command. */
2219 char *string;
2220 struct command
2222 char *prog; /* program name. */
2223 char **argv; /* vector of args. */
2224 int pid; /* pid of process for this command. */
2227 struct command *commands; /* each command buffer with above info. */
2229 /* Count # of piped commands. */
2230 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2231 if (strcmp (argbuf[i], "|") == 0)
2232 n_commands++;
2234 /* Get storage for each command. */
2235 commands
2236 = (struct command *) alloca (n_commands * sizeof (struct command));
2238 /* Split argbuf into its separate piped processes,
2239 and record info about each one.
2240 Also search for the programs that are to be run. */
2242 commands[0].prog = argbuf[0]; /* first command. */
2243 commands[0].argv = &argbuf[0];
2244 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2246 if (string)
2247 commands[0].argv[0] = string;
2249 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2250 if (strcmp (argbuf[i], "|") == 0)
2251 { /* each command. */
2252 #if defined (__MSDOS__) || (defined (_WIN32) && defined (__CYGWIN32_)) || defined (OS2) || defined (VMS)
2253 fatal ("-pipe not supported");
2254 #endif
2255 argbuf[i] = 0; /* termination of command args. */
2256 commands[n_commands].prog = argbuf[i + 1];
2257 commands[n_commands].argv = &argbuf[i + 1];
2258 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2259 if (string)
2260 commands[n_commands].argv[0] = string;
2261 n_commands++;
2264 argbuf[argbuf_index] = 0;
2266 /* If -v, print what we are about to do, and maybe query. */
2268 if (verbose_flag)
2270 /* For help listings, put a blank line between sub-processes. */
2271 if (print_help_list)
2272 fputc ('\n', stderr);
2274 /* Print each piped command as a separate line. */
2275 for (i = 0; i < n_commands ; i++)
2277 char **j;
2279 for (j = commands[i].argv; *j; j++)
2280 fprintf (stderr, " %s", *j);
2282 /* Print a pipe symbol after all but the last command. */
2283 if (i + 1 != n_commands)
2284 fprintf (stderr, " |");
2285 fprintf (stderr, "\n");
2287 fflush (stderr);
2288 #ifdef DEBUG
2289 fprintf (stderr, "\nGo ahead? (y or n) ");
2290 fflush (stderr);
2291 i = getchar ();
2292 if (i != '\n')
2293 while (getchar () != '\n')
2296 if (i != 'y' && i != 'Y')
2297 return 0;
2298 #endif /* DEBUG */
2301 /* Run each piped subprocess. */
2303 for (i = 0; i < n_commands; i++)
2305 char *errmsg_fmt, *errmsg_arg;
2306 char *string = commands[i].argv[0];
2308 commands[i].pid = pexecute (string, commands[i].argv,
2309 programname, temp_filename,
2310 &errmsg_fmt, &errmsg_arg,
2311 ((i == 0 ? PEXECUTE_FIRST : 0)
2312 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2313 | (string == commands[i].prog
2314 ? PEXECUTE_SEARCH : 0)
2315 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2317 if (commands[i].pid == -1)
2318 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2320 if (string != commands[i].prog)
2321 free (string);
2324 execution_count++;
2326 /* Wait for all the subprocesses to finish.
2327 We don't care what order they finish in;
2328 we know that N_COMMANDS waits will get them all.
2329 Ignore subprocesses that we don't know about,
2330 since they can be spawned by the process that exec'ed us. */
2333 int ret_code = 0;
2335 for (i = 0; i < n_commands; )
2337 int j;
2338 int status;
2339 int pid;
2341 pid = pwait (commands[i].pid, &status, 0);
2342 if (pid < 0)
2343 abort ();
2345 for (j = 0; j < n_commands; j++)
2346 if (commands[j].pid == pid)
2348 i++;
2349 if (status != 0)
2351 if (WIFSIGNALED (status))
2353 fatal ("Internal compiler error: program %s got fatal signal %d",
2354 commands[j].prog, WTERMSIG (status));
2355 signal_count++;
2356 ret_code = -1;
2358 else if (WIFEXITED (status)
2359 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2360 ret_code = -1;
2362 break;
2365 return ret_code;
2369 /* Find all the switches given to us
2370 and make a vector describing them.
2371 The elements of the vector are strings, one per switch given.
2372 If a switch uses following arguments, then the `part1' field
2373 is the switch itself and the `args' field
2374 is a null-terminated vector containing the following arguments.
2375 The `live_cond' field is 1 if the switch is true in a conditional spec,
2376 -1 if false (overridden by a later switch), and is initialized to zero.
2377 The `valid' field is nonzero if any spec has looked at this switch;
2378 if it remains zero at the end of the run, it must be meaningless. */
2380 struct switchstr
2382 char *part1;
2383 char **args;
2384 int live_cond;
2385 int valid;
2388 static struct switchstr *switches;
2390 static int n_switches;
2392 struct infile
2394 char *name;
2395 char *language;
2398 /* Also a vector of input files specified. */
2400 static struct infile *infiles;
2402 static int n_infiles;
2404 /* This counts the number of libraries added by LANG_SPECIFIC_DRIVER, so that
2405 we can tell if there were any user supplied any files or libraries. */
2407 static int added_libraries;
2409 /* And a vector of corresponding output files is made up later. */
2411 static char **outfiles;
2413 /* Used to track if none of the -B paths are used. */
2414 static int warn_B;
2416 /* Used to track if standard path isn't used and -b or -V is specified. */
2417 static int warn_std;
2419 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2420 static int *warn_std_ptr = 0;
2423 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2425 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2426 is true if we should look for an executable suffix as well. */
2428 static char *
2429 convert_filename (name, do_exe)
2430 char *name;
2431 int do_exe;
2433 int i;
2434 int len = strlen (name);
2436 #ifdef HAVE_OBJECT_SUFFIX
2437 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2438 if (len > 2
2439 && name[len - 2] == '.'
2440 && name[len - 1] == 'o')
2442 obstack_grow (&obstack, name, len - 2);
2443 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2444 name = obstack_finish (&obstack);
2446 #endif
2448 #ifdef HAVE_EXECUTABLE_SUFFIX
2449 /* If there is no filetype, make it the executable suffix (which includes
2450 the "."). But don't get confused if we have just "-o". */
2451 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2452 return name;
2454 for (i = len - 1; i >= 0; i--)
2455 if (name[i] == '/' || name[i] == DIR_SEPARATOR)
2456 break;
2458 for (i++; i < len; i++)
2459 if (name[i] == '.')
2460 return name;
2462 obstack_grow (&obstack, name, len);
2463 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2464 name = obstack_finish (&obstack);
2465 #endif
2467 return name;
2469 #endif
2471 /* Display the command line switches accepted by gcc. */
2472 static void
2473 display_help ()
2475 printf ("Usage: %s [options] file...\n", programname);
2476 printf ("Options:\n");
2478 printf (" --help Display this information\n");
2479 if (! verbose_flag)
2480 printf (" (Use '-v --help' to display command line options of sub-processes)\n");
2481 printf (" -dumpspecs Display all of the built in spec strings\n");
2482 printf (" -dumpversion Display the version of the compiler\n");
2483 printf (" -dumpmachine Display the compiler's target processor\n");
2484 printf (" -print-search-dirs Display the directories in the compiler's search path\n");
2485 printf (" -print-libgcc-file-name Display the name of the compiler's companion library\n");
2486 printf (" -print-file-name=<lib> Display the full path to library <lib>\n");
2487 printf (" -print-prog-name=<prog> Display the full path to compiler component <prog>\n");
2488 printf (" -print-multi-directory Display the root directory for versions of libgcc\n");
2489 printf (" -print-multi-lib Display the mapping between command line options and\n");
2490 printf (" multiple library search directories\n");
2491 printf (" -Wa,<options> Pass comma-separated <options> on to the assembler\n");
2492 printf (" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n");
2493 printf (" -Wl,<options> Pass comma-separated <options> on to the linker\n");
2494 printf (" -Xlinker <arg> Pass <arg> on to the linker\n");
2495 printf (" -save-temps Do not delete intermediate files\n");
2496 printf (" -pipe Use pipes rather than intermediate files\n");
2497 printf (" -specs=<file> Override builtin specs with the contents of <file>\n");
2498 printf (" -B <directory> Add <directory> to the compiler's search paths\n");
2499 printf (" -b <machine> Run gcc for target <machine>, if installed\n");
2500 printf (" -V <version> Run gcc version number <version>, if installed\n");
2501 printf (" -v Display the programs invoked by the compiler\n");
2502 printf (" -E Preprocess only; do not compile, assemble or link\n");
2503 printf (" -S Compile only; do not assemble or link\n");
2504 printf (" -c Compile and assemble, but do not link\n");
2505 printf (" -o <file> Place the output into <file>\n");
2506 printf (" -x <language> Specify the language of the following input files\n");
2507 printf (" Permissable languages include: c c++ assembler none\n");
2508 printf (" 'none' means revert to the default behaviour of\n");
2509 printf (" guessing the language based on the file's extension\n");
2511 printf ("\nOptions starting with -g, -f, -m, -O or -W are automatically passed on to\n");
2512 printf ("the various sub-processes invoked by %s. In order to pass other options\n",
2513 programname);
2514 printf ("on to these processes the -W<letter> options must be used.\n");
2516 /* The rest of the options are displayed by invocations of the various
2517 sub-processes. */
2520 static void
2521 add_preprocessor_option (option, len)
2522 char * option;
2523 int len;
2525 n_preprocessor_options++;
2527 if (! preprocessor_options)
2528 preprocessor_options
2529 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
2530 else
2531 preprocessor_options
2532 = (char **) xrealloc (preprocessor_options,
2533 n_preprocessor_options * sizeof (char *));
2535 preprocessor_options [n_preprocessor_options - 1] = save_string (option, len);
2538 static void
2539 add_assembler_option (option, len)
2540 char * option;
2541 int len;
2543 n_assembler_options++;
2545 if (! assembler_options)
2546 assembler_options
2547 = (char **) xmalloc (n_assembler_options * sizeof (char *));
2548 else
2549 assembler_options
2550 = (char **) xrealloc (assembler_options,
2551 n_assembler_options * sizeof (char *));
2553 assembler_options [n_assembler_options - 1] = save_string (option, len);
2556 static void
2557 add_linker_option (option, len)
2558 char * option;
2559 int len;
2561 n_linker_options++;
2563 if (! linker_options)
2564 linker_options
2565 = (char **) xmalloc (n_linker_options * sizeof (char *));
2566 else
2567 linker_options
2568 = (char **) xrealloc (linker_options,
2569 n_linker_options * sizeof (char *));
2571 linker_options [n_linker_options - 1] = save_string (option, len);
2574 /* Create the vector `switches' and its contents.
2575 Store its length in `n_switches'. */
2577 static void
2578 process_command (argc, argv)
2579 int argc;
2580 char **argv;
2582 register int i;
2583 char *temp;
2584 char *spec_lang = 0;
2585 int last_language_n_infiles;
2586 int have_c = 0;
2587 int have_o = 0;
2588 int lang_n_infiles = 0;
2590 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
2592 n_switches = 0;
2593 n_infiles = 0;
2594 added_libraries = 0;
2596 /* Figure compiler version from version string. */
2598 compiler_version = save_string (version_string, strlen (version_string));
2599 for (temp = compiler_version; *temp; ++temp)
2601 if (*temp == ' ')
2603 *temp = '\0';
2604 break;
2608 /* Set up the default search paths. */
2610 if (gcc_exec_prefix)
2612 int len = strlen (gcc_exec_prefix);
2613 if (len > (int) sizeof ("/lib/gcc-lib/")-1
2614 && (gcc_exec_prefix[len-1] == '/'
2615 || gcc_exec_prefix[len-1] == DIR_SEPARATOR))
2617 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
2618 if ((*temp == '/' || *temp == DIR_SEPARATOR)
2619 && strncmp (temp+1, "lib", 3) == 0
2620 && (temp[4] == '/' || temp[4] == DIR_SEPARATOR)
2621 && strncmp (temp+5, "gcc-lib", 7) == 0)
2622 len -= sizeof ("/lib/gcc-lib/") - 1;
2625 set_std_prefix (gcc_exec_prefix, len);
2626 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2627 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2630 /* COMPILER_PATH and LIBRARY_PATH have values
2631 that are lists of directory names with colons. */
2633 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
2634 if (temp)
2636 char *startp, *endp;
2637 char *nstore = (char *) alloca (strlen (temp) + 3);
2639 startp = endp = temp;
2640 while (1)
2642 if (*endp == PATH_SEPARATOR || *endp == 0)
2644 strncpy (nstore, startp, endp-startp);
2645 if (endp == startp)
2646 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2647 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2649 nstore[endp-startp] = DIR_SEPARATOR;
2650 nstore[endp-startp+1] = 0;
2652 else
2653 nstore[endp-startp] = 0;
2654 add_prefix (&exec_prefixes, nstore, 0, 0, 0, NULL_PTR);
2655 add_prefix (&include_prefixes,
2656 concat (nstore, "include", NULL_PTR),
2657 0, 0, 0, NULL_PTR);
2658 if (*endp == 0)
2659 break;
2660 endp = startp = endp + 1;
2662 else
2663 endp++;
2667 GET_ENV_PATH_LIST (temp, "LIBRARY_PATH");
2668 if (temp && *cross_compile == '0')
2670 char *startp, *endp;
2671 char *nstore = (char *) alloca (strlen (temp) + 3);
2673 startp = endp = temp;
2674 while (1)
2676 if (*endp == PATH_SEPARATOR || *endp == 0)
2678 strncpy (nstore, startp, endp-startp);
2679 if (endp == startp)
2680 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2681 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2683 nstore[endp-startp] = DIR_SEPARATOR;
2684 nstore[endp-startp+1] = 0;
2686 else
2687 nstore[endp-startp] = 0;
2688 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2689 0, 0, NULL_PTR);
2690 if (*endp == 0)
2691 break;
2692 endp = startp = endp + 1;
2694 else
2695 endp++;
2699 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2700 GET_ENV_PATH_LIST (temp, "LPATH");
2701 if (temp && *cross_compile == '0')
2703 char *startp, *endp;
2704 char *nstore = (char *) alloca (strlen (temp) + 3);
2706 startp = endp = temp;
2707 while (1)
2709 if (*endp == PATH_SEPARATOR || *endp == 0)
2711 strncpy (nstore, startp, endp-startp);
2712 if (endp == startp)
2713 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2714 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2716 nstore[endp-startp] = DIR_SEPARATOR;
2717 nstore[endp-startp+1] = 0;
2719 else
2720 nstore[endp-startp] = 0;
2721 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2722 0, 0, NULL_PTR);
2723 if (*endp == 0)
2724 break;
2725 endp = startp = endp + 1;
2727 else
2728 endp++;
2732 /* Convert new-style -- options to old-style. */
2733 translate_options (&argc, &argv);
2735 #ifdef LANG_SPECIFIC_DRIVER
2736 /* Do language-specific adjustment/addition of flags. */
2737 lang_specific_driver (fatal, &argc, &argv, &added_libraries);
2738 #endif
2740 /* Scan argv twice. Here, the first time, just count how many switches
2741 there will be in their vector, and how many input files in theirs.
2742 Here we also parse the switches that cc itself uses (e.g. -v). */
2744 for (i = 1; i < argc; i++)
2746 if (! strcmp (argv[i], "-dumpspecs"))
2748 struct spec_list *sl;
2749 init_spec ();
2750 for (sl = specs; sl; sl = sl->next)
2751 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
2752 exit (0);
2754 else if (! strcmp (argv[i], "-dumpversion"))
2756 printf ("%s\n", spec_version);
2757 exit (0);
2759 else if (! strcmp (argv[i], "-dumpmachine"))
2761 printf ("%s\n", spec_machine);
2762 exit (0);
2764 else if (strcmp (argv[i], "-fhelp") == 0)
2766 /* translate_options () has turned --help into -fhelp. */
2767 print_help_list = 1;
2769 /* We will be passing a dummy file on to the sub-processes. */
2770 n_infiles++;
2771 n_switches++;
2773 add_preprocessor_option ("--help", 6);
2774 add_assembler_option ("--help", 6);
2775 add_linker_option ("--help", 6);
2777 else if (! strcmp (argv[i], "-print-search-dirs"))
2778 print_search_dirs = 1;
2779 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2780 print_file_name = "libgcc.a";
2781 else if (! strncmp (argv[i], "-print-file-name=", 17))
2782 print_file_name = argv[i] + 17;
2783 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2784 print_prog_name = argv[i] + 17;
2785 else if (! strcmp (argv[i], "-print-multi-lib"))
2786 print_multi_lib = 1;
2787 else if (! strcmp (argv[i], "-print-multi-directory"))
2788 print_multi_directory = 1;
2789 else if (! strncmp (argv[i], "-Wa,", 4))
2791 int prev, j;
2792 /* Pass the rest of this option to the assembler. */
2794 /* Split the argument at commas. */
2795 prev = 4;
2796 for (j = 4; argv[i][j]; j++)
2797 if (argv[i][j] == ',')
2799 add_assembler_option (argv[i] + prev, j - prev);
2800 prev = j + 1;
2803 /* Record the part after the last comma. */
2804 add_assembler_option (argv[i] + prev, j - prev);
2806 else if (! strncmp (argv[i], "-Wp,", 4))
2808 int prev, j;
2809 /* Pass the rest of this option to the preprocessor. */
2811 /* Split the argument at commas. */
2812 prev = 4;
2813 for (j = 4; argv[i][j]; j++)
2814 if (argv[i][j] == ',')
2816 add_preprocessor_option (argv[i] + prev, j - prev);
2817 prev = j + 1;
2820 /* Record the part after the last comma. */
2821 add_preprocessor_option (argv[i] + prev, j - prev);
2823 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2824 /* The +e options to the C++ front-end. */
2825 n_switches++;
2826 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2828 int j;
2829 /* Split the argument at commas. */
2830 for (j = 3; argv[i][j]; j++)
2831 n_infiles += (argv[i][j] == ',');
2833 else if (strcmp (argv[i], "-Xlinker") == 0)
2835 if (i + 1 == argc)
2836 fatal ("argument to `-Xlinker' is missing");
2838 n_infiles++;
2839 i++;
2841 else if (strncmp (argv[i], "-l", 2) == 0)
2842 n_infiles++;
2843 else if (strcmp (argv[i], "-save-temps") == 0)
2845 save_temps_flag = 1;
2846 n_switches++;
2848 else if (strcmp (argv[i], "-specs") == 0)
2850 struct user_specs *user = (struct user_specs *)
2851 xmalloc (sizeof (struct user_specs));
2852 if (++i >= argc)
2853 fatal ("argument to `-specs' is missing");
2855 user->next = (struct user_specs *)0;
2856 user->filename = argv[i];
2857 if (user_specs_tail)
2858 user_specs_tail->next = user;
2859 else
2860 user_specs_head = user;
2861 user_specs_tail = user;
2863 else if (strncmp (argv[i], "-specs=", 7) == 0)
2865 struct user_specs *user = (struct user_specs *)
2866 xmalloc (sizeof (struct user_specs));
2867 if (strlen (argv[i]) == 7)
2868 fatal ("argument to `-specs=' is missing");
2870 user->next = (struct user_specs *)0;
2871 user->filename = argv[i]+7;
2872 if (user_specs_tail)
2873 user_specs_tail->next = user;
2874 else
2875 user_specs_head = user;
2876 user_specs_tail = user;
2878 else if (argv[i][0] == '-' && argv[i][1] != 0)
2880 register char *p = &argv[i][1];
2881 register int c = *p;
2883 switch (c)
2885 case 'b':
2886 n_switches++;
2887 if (p[1] == 0 && i + 1 == argc)
2888 fatal ("argument to `-b' is missing");
2889 if (p[1] == 0)
2890 spec_machine = argv[++i];
2891 else
2892 spec_machine = p + 1;
2894 warn_std_ptr = &warn_std;
2895 break;
2897 case 'B':
2899 char *value;
2900 if (p[1] == 0 && i + 1 == argc)
2901 fatal ("argument to `-B' is missing");
2902 if (p[1] == 0)
2903 value = argv[++i];
2904 else
2905 value = p + 1;
2906 add_prefix (&exec_prefixes, value, NULL_PTR, 1, 0, &warn_B);
2907 add_prefix (&startfile_prefixes, value, NULL_PTR,
2908 1, 0, &warn_B);
2909 add_prefix (&include_prefixes, concat (value, "include",
2910 NULL_PTR),
2911 NULL_PTR, 1, 0, NULL_PTR);
2913 /* As a kludge, if the arg is "[foo/]stageN/", just add
2914 "[foo/]include" to the include prefix. */
2916 int len = strlen (value);
2917 if ((len == 7
2918 || (len > 7
2919 && (value[len - 8] == '/'
2920 || value[len - 8] == DIR_SEPARATOR)))
2921 && strncmp (value + len - 7, "stage", 5) == 0
2922 && ISDIGIT (value[len - 2])
2923 && (value[len - 1] == '/'
2924 || value[len - 1] == DIR_SEPARATOR))
2926 if (len == 7)
2927 add_prefix (&include_prefixes, "include", NULL_PTR,
2928 1, 0, NULL_PTR);
2929 else
2931 char *string = xmalloc (len + 1);
2932 strncpy (string, value, len-7);
2933 strcpy (string+len-7, "include");
2934 add_prefix (&include_prefixes, string, NULL_PTR,
2935 1, 0, NULL_PTR);
2939 n_switches++;
2941 break;
2943 case 'v': /* Print our subcommands and print versions. */
2944 n_switches++;
2945 /* If they do anything other than exactly `-v', don't set
2946 verbose_flag; rather, continue on to give the error. */
2947 if (p[1] != 0)
2948 break;
2949 verbose_flag++;
2950 break;
2952 case 'V':
2953 n_switches++;
2954 if (p[1] == 0 && i + 1 == argc)
2955 fatal ("argument to `-V' is missing");
2956 if (p[1] == 0)
2957 spec_version = argv[++i];
2958 else
2959 spec_version = p + 1;
2960 compiler_version = spec_version;
2961 warn_std_ptr = &warn_std;
2963 /* Validate the version number. Use the same checks
2964 done when inserting it into a spec.
2966 The format of the version string is
2967 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
2969 char *v = compiler_version;
2971 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
2972 while (! ISDIGIT (*v))
2973 v++;
2975 if (v > compiler_version && v[-1] != '-')
2976 fatal ("invalid version number format");
2978 /* Set V after the first period. */
2979 while (ISDIGIT (*v))
2980 v++;
2982 if (*v != '.')
2983 fatal ("invalid version number format");
2985 v++;
2986 while (ISDIGIT (*v))
2987 v++;
2989 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
2990 fatal ("invalid version number format");
2992 break;
2994 case 'S':
2995 case 'c':
2996 if (p[1] == 0)
2998 have_c = 1;
2999 n_switches++;
3000 break;
3002 goto normal_switch;
3004 case 'o':
3005 have_o = 1;
3006 #if defined(HAVE_EXECUTABLE_SUFFIX)
3007 if (! have_c)
3009 int skip;
3011 /* Forward scan, just in case -S or -c is specified
3012 after -o. */
3013 int j = i + 1;
3014 if (p[1] == 0)
3015 ++j;
3016 while (j < argc)
3018 if (argv[j][0] == '-')
3020 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3021 && argv[j][2] == 0)
3023 have_c = 1;
3024 break;
3026 else if (skip = SWITCH_TAKES_ARG (argv[j][1]))
3027 j += skip - (argv[j][2] != 0);
3028 else if (skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1))
3029 j += skip;
3031 j++;
3034 #endif
3035 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
3036 if (p[1] == 0)
3037 argv[i+1] = convert_filename (argv[i+1], ! have_c);
3038 else
3039 argv[i] = convert_filename (argv[i], ! have_c);
3040 #endif
3041 goto normal_switch;
3043 default:
3044 normal_switch:
3045 n_switches++;
3047 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3048 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3049 else if (WORD_SWITCH_TAKES_ARG (p))
3050 i += WORD_SWITCH_TAKES_ARG (p);
3053 else
3055 n_infiles++;
3056 lang_n_infiles++;
3060 if (have_c && have_o && lang_n_infiles > 1)
3061 fatal ("cannot specify -o with -c or -S and multiple compilations");
3063 /* Set up the search paths before we go looking for config files. */
3065 /* These come before the md prefixes so that we will find gcc's subcommands
3066 (such as cpp) rather than those of the host system. */
3067 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3068 as well as trying the machine and the version. */
3069 #ifndef OS2
3070 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3071 0, 2, warn_std_ptr);
3072 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3073 0, 2, warn_std_ptr);
3074 #endif
3076 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3077 0, 1, warn_std_ptr);
3078 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3079 0, 1, warn_std_ptr);
3081 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3082 dir_separator_str, NULL_PTR);
3084 /* If tooldir is relative, base it on exec_prefixes. A relative
3085 tooldir lets us move the installed tree as a unit.
3087 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3088 directories, so that we can search both the user specified directory
3089 and the standard place. */
3091 if (*tooldir_prefix != '/' && *tooldir_prefix != DIR_SEPARATOR)
3093 if (gcc_exec_prefix)
3095 char *gcc_exec_tooldir_prefix
3096 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3097 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
3099 add_prefix (&exec_prefixes,
3100 concat (gcc_exec_tooldir_prefix, "bin",
3101 dir_separator_str, NULL_PTR),
3102 NULL_PTR, 0, 0, NULL_PTR);
3103 add_prefix (&startfile_prefixes,
3104 concat (gcc_exec_tooldir_prefix, "lib",
3105 dir_separator_str, NULL_PTR),
3106 NULL_PTR, 0, 0, NULL_PTR);
3109 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3110 dir_separator_str, spec_version,
3111 dir_separator_str, tooldir_prefix, NULL_PTR);
3114 add_prefix (&exec_prefixes,
3115 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
3116 "BINUTILS", 0, 0, NULL_PTR);
3117 add_prefix (&startfile_prefixes,
3118 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
3119 "BINUTILS", 0, 0, NULL_PTR);
3121 /* More prefixes are enabled in main, after we read the specs file
3122 and determine whether this is cross-compilation or not. */
3125 /* Then create the space for the vectors and scan again. */
3127 switches = ((struct switchstr *)
3128 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3129 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3130 n_switches = 0;
3131 n_infiles = 0;
3132 last_language_n_infiles = -1;
3134 /* This, time, copy the text of each switch and store a pointer
3135 to the copy in the vector of switches.
3136 Store all the infiles in their vector. */
3138 for (i = 1; i < argc; i++)
3140 /* Just skip the switches that were handled by the preceding loop. */
3141 if (! strncmp (argv[i], "-Wa,", 4))
3143 else if (! strncmp (argv[i], "-Wp,", 4))
3145 else if (! strcmp (argv[i], "-print-search-dirs"))
3147 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3149 else if (! strncmp (argv[i], "-print-file-name=", 17))
3151 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3153 else if (! strcmp (argv[i], "-print-multi-lib"))
3155 else if (! strcmp (argv[i], "-print-multi-directory"))
3157 else if (strcmp (argv[i], "-fhelp") == 0)
3159 if (verbose_flag)
3161 /* Create a dummy input file, so that we can pass --help on to
3162 the various sub-processes. */
3163 infiles[n_infiles].language = "c";
3164 infiles[n_infiles++].name = "help-dummy";
3166 /* Preserve the --help switch so that it can be caught by the
3167 cc1 spec string. */
3168 switches[n_switches].part1 = "--help";
3169 switches[n_switches].args = 0;
3170 switches[n_switches].live_cond = 0;
3171 switches[n_switches].valid = 0;
3173 n_switches++;
3176 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3178 /* Compensate for the +e options to the C++ front-end;
3179 they're there simply for cfront call-compatibility. We do
3180 some magic in default_compilers to pass them down properly.
3181 Note we deliberately start at the `+' here, to avoid passing
3182 -e0 or -e1 down into the linker. */
3183 switches[n_switches].part1 = &argv[i][0];
3184 switches[n_switches].args = 0;
3185 switches[n_switches].live_cond = 0;
3186 switches[n_switches].valid = 0;
3187 n_switches++;
3189 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3191 int prev, j;
3192 /* Split the argument at commas. */
3193 prev = 4;
3194 for (j = 4; argv[i][j]; j++)
3195 if (argv[i][j] == ',')
3197 infiles[n_infiles].language = "*";
3198 infiles[n_infiles++].name
3199 = save_string (argv[i] + prev, j - prev);
3200 prev = j + 1;
3202 /* Record the part after the last comma. */
3203 infiles[n_infiles].language = "*";
3204 infiles[n_infiles++].name = argv[i] + prev;
3206 else if (strcmp (argv[i], "-Xlinker") == 0)
3208 infiles[n_infiles].language = "*";
3209 infiles[n_infiles++].name = argv[++i];
3211 else if (strncmp (argv[i], "-l", 2) == 0)
3213 infiles[n_infiles].language = "*";
3214 infiles[n_infiles++].name = argv[i];
3216 else if (strcmp (argv[i], "-specs") == 0)
3217 i++;
3218 else if (strncmp (argv[i], "-specs=", 7) == 0)
3220 /* -save-temps overrides -pipe, so that temp files are produced */
3221 else if (save_temps_flag && strcmp (argv[i], "-pipe") == 0)
3222 error ("Warning: -pipe ignored since -save-temps specified");
3223 else if (argv[i][0] == '-' && argv[i][1] != 0)
3225 register char *p = &argv[i][1];
3226 register int c = *p;
3228 if (c == 'x')
3230 if (p[1] == 0 && i + 1 == argc)
3231 fatal ("argument to `-x' is missing");
3232 if (p[1] == 0)
3233 spec_lang = argv[++i];
3234 else
3235 spec_lang = p + 1;
3236 if (! strcmp (spec_lang, "none"))
3237 /* Suppress the warning if -xnone comes after the last input
3238 file, because alternate command interfaces like g++ might
3239 find it useful to place -xnone after each input file. */
3240 spec_lang = 0;
3241 else
3242 last_language_n_infiles = n_infiles;
3243 continue;
3245 switches[n_switches].part1 = p;
3246 /* Deal with option arguments in separate argv elements. */
3247 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3248 || WORD_SWITCH_TAKES_ARG (p))
3250 int j = 0;
3251 int n_args = WORD_SWITCH_TAKES_ARG (p);
3253 if (n_args == 0)
3255 /* Count only the option arguments in separate argv elements. */
3256 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3258 if (i + n_args >= argc)
3259 fatal ("argument to `-%s' is missing", p);
3260 switches[n_switches].args
3261 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
3262 while (j < n_args)
3263 switches[n_switches].args[j++] = argv[++i];
3264 /* Null-terminate the vector. */
3265 switches[n_switches].args[j] = 0;
3267 else if (index (switches_need_spaces, c))
3269 /* On some systems, ld cannot handle some options without
3270 a space. So split the option from its argument. */
3271 char *part1 = (char *) xmalloc (2);
3272 part1[0] = c;
3273 part1[1] = '\0';
3275 switches[n_switches].part1 = part1;
3276 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
3277 switches[n_switches].args[0] = xmalloc (strlen (p));
3278 strcpy (switches[n_switches].args[0], &p[1]);
3279 switches[n_switches].args[1] = 0;
3281 else
3282 switches[n_switches].args = 0;
3284 switches[n_switches].live_cond = 0;
3285 switches[n_switches].valid = 0;
3286 /* This is always valid, since gcc.c itself understands it. */
3287 if (!strcmp (p, "save-temps"))
3288 switches[n_switches].valid = 1;
3289 else
3291 char ch = switches[n_switches].part1[0];
3292 if (ch == 'V' || ch == 'b' || ch == 'B')
3293 switches[n_switches].valid = 1;
3295 n_switches++;
3297 else
3299 #ifdef HAVE_OBJECT_SUFFIX
3300 argv[i] = convert_filename (argv[i], 0);
3301 #endif
3303 if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
3305 perror_with_name (argv[i]);
3306 error_count++;
3308 else
3310 infiles[n_infiles].language = spec_lang;
3311 infiles[n_infiles++].name = argv[i];
3316 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3317 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3319 switches[n_switches].part1 = 0;
3320 infiles[n_infiles].name = 0;
3323 /* Process a spec string, accumulating and running commands. */
3325 /* These variables describe the input file name.
3326 input_file_number is the index on outfiles of this file,
3327 so that the output file name can be stored for later use by %o.
3328 input_basename is the start of the part of the input file
3329 sans all directory names, and basename_length is the number
3330 of characters starting there excluding the suffix .c or whatever. */
3332 char *input_filename;
3333 static int input_file_number;
3334 size_t input_filename_length;
3335 static int basename_length;
3336 static char *input_basename;
3337 static char *input_suffix;
3339 /* These are variables used within do_spec and do_spec_1. */
3341 /* Nonzero if an arg has been started and not yet terminated
3342 (with space, tab or newline). */
3343 static int arg_going;
3345 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3346 is a temporary file name. */
3347 static int delete_this_arg;
3349 /* Nonzero means %w has been seen; the next arg to be terminated
3350 is the output file name of this compilation. */
3351 static int this_is_output_file;
3353 /* Nonzero means %s has been seen; the next arg to be terminated
3354 is the name of a library file and we should try the standard
3355 search dirs for it. */
3356 static int this_is_library_file;
3358 /* Nonzero means that the input of this command is coming from a pipe. */
3359 static int input_from_pipe;
3361 /* Process the spec SPEC and run the commands specified therein.
3362 Returns 0 if the spec is successfully processed; -1 if failed. */
3365 do_spec (spec)
3366 char *spec;
3368 int value;
3370 clear_args ();
3371 arg_going = 0;
3372 delete_this_arg = 0;
3373 this_is_output_file = 0;
3374 this_is_library_file = 0;
3375 input_from_pipe = 0;
3377 value = do_spec_1 (spec, 0, NULL_PTR);
3379 /* Force out any unfinished command.
3380 If -pipe, this forces out the last command if it ended in `|'. */
3381 if (value == 0)
3383 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3384 argbuf_index--;
3386 if (argbuf_index > 0)
3387 value = execute ();
3390 return value;
3393 /* Process the sub-spec SPEC as a portion of a larger spec.
3394 This is like processing a whole spec except that we do
3395 not initialize at the beginning and we do not supply a
3396 newline by default at the end.
3397 INSWITCH nonzero means don't process %-sequences in SPEC;
3398 in this case, % is treated as an ordinary character.
3399 This is used while substituting switches.
3400 INSWITCH nonzero also causes SPC not to terminate an argument.
3402 Value is zero unless a line was finished
3403 and the command on that line reported an error. */
3405 static int
3406 do_spec_1 (spec, inswitch, soft_matched_part)
3407 char *spec;
3408 int inswitch;
3409 char *soft_matched_part;
3411 register char *p = spec;
3412 register int c;
3413 int i;
3414 char *string;
3415 int value;
3417 while ((c = *p++))
3418 /* If substituting a switch, treat all chars like letters.
3419 Otherwise, NL, SPC, TAB and % are special. */
3420 switch (inswitch ? 'a' : c)
3422 case '\n':
3423 /* End of line: finish any pending argument,
3424 then run the pending command if one has been started. */
3425 if (arg_going)
3427 obstack_1grow (&obstack, 0);
3428 string = obstack_finish (&obstack);
3429 if (this_is_library_file)
3430 string = find_file (string);
3431 store_arg (string, delete_this_arg, this_is_output_file);
3432 if (this_is_output_file)
3433 outfiles[input_file_number] = string;
3435 arg_going = 0;
3437 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3439 for (i = 0; i < n_switches; i++)
3440 if (!strcmp (switches[i].part1, "pipe"))
3441 break;
3443 /* A `|' before the newline means use a pipe here,
3444 but only if -pipe was specified.
3445 Otherwise, execute now and don't pass the `|' as an arg. */
3446 if (i < n_switches)
3448 input_from_pipe = 1;
3449 switches[i].valid = 1;
3450 break;
3452 else
3453 argbuf_index--;
3456 if (argbuf_index > 0)
3458 value = execute ();
3459 if (value)
3460 return value;
3462 /* Reinitialize for a new command, and for a new argument. */
3463 clear_args ();
3464 arg_going = 0;
3465 delete_this_arg = 0;
3466 this_is_output_file = 0;
3467 this_is_library_file = 0;
3468 input_from_pipe = 0;
3469 break;
3471 case '|':
3472 /* End any pending argument. */
3473 if (arg_going)
3475 obstack_1grow (&obstack, 0);
3476 string = obstack_finish (&obstack);
3477 if (this_is_library_file)
3478 string = find_file (string);
3479 store_arg (string, delete_this_arg, this_is_output_file);
3480 if (this_is_output_file)
3481 outfiles[input_file_number] = string;
3484 /* Use pipe */
3485 obstack_1grow (&obstack, c);
3486 arg_going = 1;
3487 break;
3489 case '\t':
3490 case ' ':
3491 /* Space or tab ends an argument if one is pending. */
3492 if (arg_going)
3494 obstack_1grow (&obstack, 0);
3495 string = obstack_finish (&obstack);
3496 if (this_is_library_file)
3497 string = find_file (string);
3498 store_arg (string, delete_this_arg, this_is_output_file);
3499 if (this_is_output_file)
3500 outfiles[input_file_number] = string;
3502 /* Reinitialize for a new argument. */
3503 arg_going = 0;
3504 delete_this_arg = 0;
3505 this_is_output_file = 0;
3506 this_is_library_file = 0;
3507 break;
3509 case '%':
3510 switch (c = *p++)
3512 case 0:
3513 fatal ("Invalid specification! Bug in cc.");
3515 case 'b':
3516 obstack_grow (&obstack, input_basename, basename_length);
3517 arg_going = 1;
3518 break;
3520 case 'd':
3521 delete_this_arg = 2;
3522 break;
3524 /* Dump out the directories specified with LIBRARY_PATH,
3525 followed by the absolute directories
3526 that we search for startfiles. */
3527 case 'D':
3529 struct prefix_list *pl = startfile_prefixes.plist;
3530 size_t bufsize = 100;
3531 char *buffer = (char *) xmalloc (bufsize);
3532 int idx;
3534 for (; pl; pl = pl->next)
3536 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
3537 /* Used on systems which record the specified -L dirs
3538 and use them to search for dynamic linking. */
3539 /* Relative directories always come from -B,
3540 and it is better not to use them for searching
3541 at run time. In particular, stage1 loses */
3542 if (pl->prefix[0] != '/' && pl->prefix[0] != DIR_SEPARATOR)
3543 continue;
3544 #endif
3545 /* Try subdirectory if there is one. */
3546 if (multilib_dir != NULL)
3548 if (machine_suffix)
3550 if (strlen (pl->prefix) + strlen (machine_suffix)
3551 >= bufsize)
3552 bufsize = (strlen (pl->prefix)
3553 + strlen (machine_suffix)) * 2 + 1;
3554 buffer = (char *) xrealloc (buffer, bufsize);
3555 strcpy (buffer, pl->prefix);
3556 strcat (buffer, machine_suffix);
3557 if (is_directory (buffer, multilib_dir, 1))
3559 do_spec_1 ("-L", 0, NULL_PTR);
3560 #ifdef SPACE_AFTER_L_OPTION
3561 do_spec_1 (" ", 0, NULL_PTR);
3562 #endif
3563 do_spec_1 (buffer, 1, NULL_PTR);
3564 do_spec_1 (multilib_dir, 1, NULL_PTR);
3565 /* Make this a separate argument. */
3566 do_spec_1 (" ", 0, NULL_PTR);
3569 if (!pl->require_machine_suffix)
3571 if (is_directory (pl->prefix, multilib_dir, 1))
3573 do_spec_1 ("-L", 0, NULL_PTR);
3574 #ifdef SPACE_AFTER_L_OPTION
3575 do_spec_1 (" ", 0, NULL_PTR);
3576 #endif
3577 do_spec_1 (pl->prefix, 1, NULL_PTR);
3578 do_spec_1 (multilib_dir, 1, NULL_PTR);
3579 /* Make this a separate argument. */
3580 do_spec_1 (" ", 0, NULL_PTR);
3584 if (machine_suffix)
3586 if (is_directory (pl->prefix, machine_suffix, 1))
3588 do_spec_1 ("-L", 0, NULL_PTR);
3589 #ifdef SPACE_AFTER_L_OPTION
3590 do_spec_1 (" ", 0, NULL_PTR);
3591 #endif
3592 do_spec_1 (pl->prefix, 1, NULL_PTR);
3593 /* Remove slash from machine_suffix. */
3594 if (strlen (machine_suffix) >= bufsize)
3595 bufsize = strlen (machine_suffix) * 2 + 1;
3596 buffer = (char *) xrealloc (buffer, bufsize);
3597 strcpy (buffer, machine_suffix);
3598 idx = strlen (buffer);
3599 if (buffer[idx - 1] == '/'
3600 || buffer[idx - 1] == DIR_SEPARATOR)
3601 buffer[idx - 1] = 0;
3602 do_spec_1 (buffer, 1, NULL_PTR);
3603 /* Make this a separate argument. */
3604 do_spec_1 (" ", 0, NULL_PTR);
3607 if (!pl->require_machine_suffix)
3609 if (is_directory (pl->prefix, "", 1))
3611 do_spec_1 ("-L", 0, NULL_PTR);
3612 #ifdef SPACE_AFTER_L_OPTION
3613 do_spec_1 (" ", 0, NULL_PTR);
3614 #endif
3615 /* Remove slash from pl->prefix. */
3616 if (strlen (pl->prefix) >= bufsize)
3617 bufsize = strlen (pl->prefix) * 2 + 1;
3618 buffer = (char *) xrealloc (buffer, bufsize);
3619 strcpy (buffer, pl->prefix);
3620 idx = strlen (buffer);
3621 if (buffer[idx - 1] == '/'
3622 || buffer[idx - 1] == DIR_SEPARATOR)
3623 buffer[idx - 1] = 0;
3624 do_spec_1 (buffer, 1, NULL_PTR);
3625 /* Make this a separate argument. */
3626 do_spec_1 (" ", 0, NULL_PTR);
3630 free (buffer);
3632 break;
3634 case 'e':
3635 /* {...:%efoo} means report an error with `foo' as error message
3636 and don't execute any more commands for this file. */
3638 char *q = p;
3639 char *buf;
3640 while (*p != 0 && *p != '\n') p++;
3641 buf = (char *) alloca (p - q + 1);
3642 strncpy (buf, q, p - q);
3643 buf[p - q] = 0;
3644 error ("%s", buf);
3645 return -1;
3647 break;
3649 case 'g':
3650 case 'u':
3651 case 'U':
3652 if (save_temps_flag)
3654 obstack_grow (&obstack, input_basename, basename_length);
3655 delete_this_arg = 0;
3657 else
3659 #ifdef MKTEMP_EACH_FILE
3660 /* ??? This has a problem: the total number of
3661 values mktemp can return is limited.
3662 That matters for the names of object files.
3663 In 2.4, do something about that. */
3664 struct temp_name *t;
3665 int suffix_length;
3666 char *suffix = p;
3668 if (p[0] == '%' && p[1] == 'O')
3670 /* We don't support extra suffix characters after %O. */
3671 if (*p == '.' || ISALPHA ((unsigned char)*p))
3672 abort ();
3673 suffix = OBJECT_SUFFIX;
3674 suffix_length = strlen (OBJECT_SUFFIX);
3675 p += 2;
3677 else
3679 while (*p == '.' || ISALPHA ((unsigned char)*p))
3680 p++;
3681 suffix_length = p - suffix;
3684 /* See if we already have an association of %g/%u/%U and
3685 suffix. */
3686 for (t = temp_names; t; t = t->next)
3687 if (t->length == suffix_length
3688 && strncmp (t->suffix, suffix, suffix_length) == 0
3689 && t->unique == (c != 'g'))
3690 break;
3692 /* Make a new association if needed. %u requires one. */
3693 if (t == 0 || c == 'u')
3695 if (t == 0)
3697 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
3698 t->next = temp_names;
3699 temp_names = t;
3701 t->length = suffix_length;
3702 t->suffix = save_string (suffix, suffix_length);
3703 t->unique = (c != 'g');
3704 temp_filename = make_temp_file (t->suffix);
3705 temp_filename_length = strlen (temp_filename);
3706 t->filename = temp_filename;
3707 t->filename_length = temp_filename_length;
3710 obstack_grow (&obstack, t->filename, t->filename_length);
3711 delete_this_arg = 1;
3712 #else
3713 obstack_grow (&obstack, temp_filename, temp_filename_length);
3714 if (c == 'u' || c == 'U')
3716 static int unique;
3717 char buff[9];
3718 if (c == 'u')
3719 unique++;
3720 sprintf (buff, "%d", unique);
3721 obstack_grow (&obstack, buff, strlen (buff));
3723 #endif
3724 delete_this_arg = 1;
3726 arg_going = 1;
3727 break;
3729 case 'i':
3730 obstack_grow (&obstack, input_filename, input_filename_length);
3731 arg_going = 1;
3732 break;
3734 case 'I':
3736 struct prefix_list *pl = include_prefixes.plist;
3738 if (gcc_exec_prefix)
3740 do_spec_1 ("-iprefix", 1, NULL_PTR);
3741 /* Make this a separate argument. */
3742 do_spec_1 (" ", 0, NULL_PTR);
3743 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
3744 do_spec_1 (" ", 0, NULL_PTR);
3747 for (; pl; pl = pl->next)
3749 do_spec_1 ("-isystem", 1, NULL_PTR);
3750 /* Make this a separate argument. */
3751 do_spec_1 (" ", 0, NULL_PTR);
3752 do_spec_1 (pl->prefix, 1, NULL_PTR);
3753 do_spec_1 (" ", 0, NULL_PTR);
3756 break;
3758 case 'o':
3760 int max = n_infiles;
3761 #ifdef LANG_SPECIFIC_DRIVER
3762 max += lang_specific_extra_outfiles;
3763 #endif
3764 for (i = 0; i < max; i++)
3765 if (outfiles[i])
3766 store_arg (outfiles[i], 0, 0);
3767 break;
3770 case 'O':
3771 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
3772 arg_going = 1;
3773 break;
3775 case 's':
3776 this_is_library_file = 1;
3777 break;
3779 case 'w':
3780 this_is_output_file = 1;
3781 break;
3783 case 'W':
3785 int cur_index = argbuf_index;
3786 /* Handle the {...} following the %W. */
3787 if (*p != '{')
3788 abort ();
3789 p = handle_braces (p + 1);
3790 if (p == 0)
3791 return -1;
3792 /* If any args were output, mark the last one for deletion
3793 on failure. */
3794 if (argbuf_index != cur_index)
3795 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3796 break;
3799 /* %x{OPTION} records OPTION for %X to output. */
3800 case 'x':
3802 char *p1 = p;
3803 char *string;
3805 /* Skip past the option value and make a copy. */
3806 if (*p != '{')
3807 abort ();
3808 while (*p++ != '}')
3810 string = save_string (p1 + 1, p - p1 - 2);
3812 /* See if we already recorded this option. */
3813 for (i = 0; i < n_linker_options; i++)
3814 if (! strcmp (string, linker_options[i]))
3816 free (string);
3817 return 0;
3820 /* This option is new; add it. */
3821 add_linker_option (string, strlen (string));
3823 break;
3825 /* Dump out the options accumulated previously using %x. */
3826 case 'X':
3827 for (i = 0; i < n_linker_options; i++)
3829 do_spec_1 (linker_options[i], 1, NULL_PTR);
3830 /* Make each accumulated option a separate argument. */
3831 do_spec_1 (" ", 0, NULL_PTR);
3833 break;
3835 /* Dump out the options accumulated previously using -Wa,. */
3836 case 'Y':
3837 for (i = 0; i < n_assembler_options; i++)
3839 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3840 /* Make each accumulated option a separate argument. */
3841 do_spec_1 (" ", 0, NULL_PTR);
3843 break;
3845 /* Dump out the options accumulated previously using -Wp,. */
3846 case 'Z':
3847 for (i = 0; i < n_preprocessor_options; i++)
3849 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
3850 /* Make each accumulated option a separate argument. */
3851 do_spec_1 (" ", 0, NULL_PTR);
3853 break;
3855 /* Here are digits and numbers that just process
3856 a certain constant string as a spec. */
3858 case '1':
3859 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3860 if (value != 0)
3861 return value;
3862 break;
3864 case '2':
3865 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3866 if (value != 0)
3867 return value;
3868 break;
3870 case 'a':
3871 value = do_spec_1 (asm_spec, 0, NULL_PTR);
3872 if (value != 0)
3873 return value;
3874 break;
3876 case 'A':
3877 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3878 if (value != 0)
3879 return value;
3880 break;
3882 case 'c':
3883 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3884 if (value != 0)
3885 return value;
3886 break;
3888 case 'C':
3889 value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3890 if (value != 0)
3891 return value;
3892 break;
3894 case 'E':
3895 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
3896 if (value != 0)
3897 return value;
3898 break;
3900 case 'l':
3901 value = do_spec_1 (link_spec, 0, NULL_PTR);
3902 if (value != 0)
3903 return value;
3904 break;
3906 case 'L':
3907 value = do_spec_1 (lib_spec, 0, NULL_PTR);
3908 if (value != 0)
3909 return value;
3910 break;
3912 case 'G':
3913 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
3914 if (value != 0)
3915 return value;
3916 break;
3918 case 'p':
3920 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
3921 char *buf = x;
3922 char *y;
3924 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
3925 y = cpp_predefines;
3926 while (*y != 0)
3928 if (! strncmp (y, "-D", 2))
3929 /* Copy the whole option. */
3930 while (*y && *y != ' ' && *y != '\t')
3931 *x++ = *y++;
3932 else if (*y == ' ' || *y == '\t')
3933 /* Copy whitespace to the result. */
3934 *x++ = *y++;
3935 /* Don't copy other options. */
3936 else
3937 y++;
3940 *x = 0;
3942 value = do_spec_1 (buf, 0, NULL_PTR);
3943 if (value != 0)
3944 return value;
3946 break;
3948 case 'P':
3950 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
3951 char *buf = x;
3952 char *y;
3954 /* Copy all of CPP_PREDEFINES into BUF,
3955 but put __ after every -D and at the end of each arg. */
3956 y = cpp_predefines;
3957 while (*y != 0)
3959 if (! strncmp (y, "-D", 2))
3961 int flag = 0;
3963 *x++ = *y++;
3964 *x++ = *y++;
3966 if (*y != '_'
3967 || (*(y+1) != '_'
3968 && ! ISUPPER ((unsigned char)*(y+1))))
3970 /* Stick __ at front of macro name. */
3971 *x++ = '_';
3972 *x++ = '_';
3973 /* Arrange to stick __ at the end as well. */
3974 flag = 1;
3977 /* Copy the macro name. */
3978 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3979 *x++ = *y++;
3981 if (flag)
3983 *x++ = '_';
3984 *x++ = '_';
3987 /* Copy the value given, if any. */
3988 while (*y && *y != ' ' && *y != '\t')
3989 *x++ = *y++;
3991 else if (*y == ' ' || *y == '\t')
3992 /* Copy whitespace to the result. */
3993 *x++ = *y++;
3994 /* Don't copy -A options */
3995 else
3996 y++;
3998 *x++ = ' ';
4000 /* Copy all of CPP_PREDEFINES into BUF,
4001 but put __ after every -D. */
4002 y = cpp_predefines;
4003 while (*y != 0)
4005 if (! strncmp (y, "-D", 2))
4007 y += 2;
4009 if (*y != '_'
4010 || (*(y+1) != '_'
4011 && ! ISUPPER ((unsigned char)*(y+1))))
4013 /* Stick -D__ at front of macro name. */
4014 *x++ = '-';
4015 *x++ = 'D';
4016 *x++ = '_';
4017 *x++ = '_';
4019 /* Copy the macro name. */
4020 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4021 *x++ = *y++;
4023 /* Copy the value given, if any. */
4024 while (*y && *y != ' ' && *y != '\t')
4025 *x++ = *y++;
4027 else
4029 /* Do not copy this macro - we have just done it before */
4030 while (*y && *y != ' ' && *y != '\t')
4031 y++;
4034 else if (*y == ' ' || *y == '\t')
4035 /* Copy whitespace to the result. */
4036 *x++ = *y++;
4037 /* Don't copy -A options */
4038 else
4039 y++;
4041 *x++ = ' ';
4043 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4044 y = cpp_predefines;
4045 while (*y != 0)
4047 if (! strncmp (y, "-A", 2))
4048 /* Copy the whole option. */
4049 while (*y && *y != ' ' && *y != '\t')
4050 *x++ = *y++;
4051 else if (*y == ' ' || *y == '\t')
4052 /* Copy whitespace to the result. */
4053 *x++ = *y++;
4054 /* Don't copy other options. */
4055 else
4056 y++;
4059 *x = 0;
4061 value = do_spec_1 (buf, 0, NULL_PTR);
4062 if (value != 0)
4063 return value;
4065 break;
4067 case 'S':
4068 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
4069 if (value != 0)
4070 return value;
4071 break;
4073 /* Here we define characters other than letters and digits. */
4075 case '{':
4076 p = handle_braces (p);
4077 if (p == 0)
4078 return -1;
4079 break;
4081 case '%':
4082 obstack_1grow (&obstack, '%');
4083 break;
4085 case '*':
4086 do_spec_1 (soft_matched_part, 1, NULL_PTR);
4087 do_spec_1 (" ", 0, NULL_PTR);
4088 break;
4090 /* Process a string found as the value of a spec given by name.
4091 This feature allows individual machine descriptions
4092 to add and use their own specs.
4093 %[...] modifies -D options the way %P does;
4094 %(...) uses the spec unmodified. */
4095 case '[':
4096 error ("Warning: use of obsolete %%[ operator in specs");
4097 case '(':
4099 char *name = p;
4100 struct spec_list *sl;
4101 int len;
4103 /* The string after the S/P is the name of a spec that is to be
4104 processed. */
4105 while (*p && *p != ')' && *p != ']')
4106 p++;
4108 /* See if it's in the list */
4109 for (len = p - name, sl = specs; sl; sl = sl->next)
4110 if (sl->name_len == len && !strncmp (sl->name, name, len))
4112 name = *(sl->ptr_spec);
4113 #ifdef DEBUG_SPECS
4114 fprintf (stderr, "Processing spec %c%s%c, which is '%s'\n",
4115 c, sl->name, (c == '(') ? ')' : ']', name);
4116 #endif
4117 break;
4120 if (sl)
4122 if (c == '(')
4124 value = do_spec_1 (name, 0, NULL_PTR);
4125 if (value != 0)
4126 return value;
4128 else
4130 char *x = (char *) alloca (strlen (name) * 2 + 1);
4131 char *buf = x;
4132 char *y = name;
4133 int flag = 0;
4135 /* Copy all of NAME into BUF, but put __ after
4136 every -D and at the end of each arg, */
4137 while (1)
4139 if (! strncmp (y, "-D", 2))
4141 *x++ = '-';
4142 *x++ = 'D';
4143 *x++ = '_';
4144 *x++ = '_';
4145 y += 2;
4146 flag = 1;
4147 continue;
4149 else if (flag && (*y == ' ' || *y == '\t' || *y == '='
4150 || *y == '}' || *y == 0))
4152 *x++ = '_';
4153 *x++ = '_';
4154 flag = 0;
4156 if (*y == 0)
4157 break;
4158 else
4159 *x++ = *y++;
4161 *x = 0;
4163 value = do_spec_1 (buf, 0, NULL_PTR);
4164 if (value != 0)
4165 return value;
4169 /* Discard the closing paren or bracket. */
4170 if (*p)
4171 p++;
4173 break;
4175 case 'v':
4177 int c1 = *p++; /* Select first or second version number. */
4178 char *v = compiler_version;
4179 char *q;
4181 /* The format of the version string is
4182 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
4184 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
4185 while (! ISDIGIT (*v))
4186 v++;
4187 if (v > compiler_version && v[-1] != '-')
4188 abort ();
4190 /* If desired, advance to second version number. */
4191 if (c1 == '2')
4193 /* Set V after the first period. */
4194 while (ISDIGIT (*v))
4195 v++;
4196 if (*v != '.')
4197 abort ();
4198 v++;
4201 /* Set Q at the next period or at the end. */
4202 q = v;
4203 while (ISDIGIT (*q))
4204 q++;
4205 if (*q != 0 && *q != ' ' && *q != '.' && *q != '-')
4206 abort ();
4208 /* Put that part into the command. */
4209 obstack_grow (&obstack, v, q - v);
4210 arg_going = 1;
4212 break;
4214 case '|':
4215 if (input_from_pipe)
4216 do_spec_1 ("-", 0, NULL_PTR);
4217 break;
4219 default:
4220 abort ();
4222 break;
4224 case '\\':
4225 /* Backslash: treat next character as ordinary. */
4226 c = *p++;
4228 /* fall through */
4229 default:
4230 /* Ordinary character: put it into the current argument. */
4231 obstack_1grow (&obstack, c);
4232 arg_going = 1;
4235 return 0; /* End of string */
4238 /* Return 0 if we call do_spec_1 and that returns -1. */
4240 static char *
4241 handle_braces (p)
4242 register char *p;
4244 char *filter, *body = NULL, *endbody;
4245 int pipe_p = 0;
4246 int negate;
4247 int suffix;
4248 int include_blanks = 1;
4250 if (*p == '^')
4251 /* A '^' after the open-brace means to not give blanks before args. */
4252 include_blanks = 0, ++p;
4254 if (*p == '|')
4255 /* A `|' after the open-brace means,
4256 if the test fails, output a single minus sign rather than nothing.
4257 This is used in %{|!pipe:...}. */
4258 pipe_p = 1, ++p;
4260 next_member:
4261 negate = suffix = 0;
4263 if (*p == '!')
4264 /* A `!' after the open-brace negates the condition:
4265 succeed if the specified switch is not present. */
4266 negate = 1, ++p;
4268 if (*p == '.')
4269 /* A `.' after the open-brace means test against the current suffix. */
4271 if (pipe_p)
4272 abort ();
4274 suffix = 1;
4275 ++p;
4278 filter = p;
4279 while (*p != ':' && *p != '}' && *p != '|') p++;
4281 if (*p == '|' && pipe_p)
4282 abort ();
4284 if (!body)
4286 if (*p != '}')
4288 register int count = 1;
4289 register char *q = p;
4291 while (*q++ != ':') continue;
4292 body = q;
4294 while (count > 0)
4296 if (*q == '{')
4297 count++;
4298 else if (*q == '}')
4299 count--;
4300 else if (*q == 0)
4301 abort ();
4302 q++;
4304 endbody = q;
4306 else
4307 body = p, endbody = p+1;
4310 if (suffix)
4312 int found = (input_suffix != 0
4313 && (long) strlen (input_suffix) == (long)(p - filter)
4314 && strncmp (input_suffix, filter, p - filter) == 0);
4316 if (body[0] == '}')
4317 abort ();
4319 if (negate != found
4320 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL_PTR) < 0)
4321 return 0;
4323 else if (p[-1] == '*' && p[0] == '}')
4325 /* Substitute all matching switches as separate args. */
4326 register int i;
4327 --p;
4328 for (i = 0; i < n_switches; i++)
4329 if (!strncmp (switches[i].part1, filter, p - filter)
4330 && check_live_switch (i, p - filter))
4331 give_switch (i, 0, include_blanks);
4333 else
4335 /* Test for presence of the specified switch. */
4336 register int i;
4337 int present = 0;
4339 /* If name specified ends in *, as in {x*:...},
4340 check for %* and handle that case. */
4341 if (p[-1] == '*' && !negate)
4343 int substitution;
4344 char *r = body;
4346 /* First see whether we have %*. */
4347 substitution = 0;
4348 while (r < endbody)
4350 if (*r == '%' && r[1] == '*')
4351 substitution = 1;
4352 r++;
4354 /* If we do, handle that case. */
4355 if (substitution)
4357 /* Substitute all matching switches as separate args.
4358 But do this by substituting for %*
4359 in the text that follows the colon. */
4361 unsigned hard_match_len = p - filter - 1;
4362 char *string = save_string (body, endbody - body - 1);
4364 for (i = 0; i < n_switches; i++)
4365 if (!strncmp (switches[i].part1, filter, hard_match_len)
4366 && check_live_switch (i, -1))
4368 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
4369 /* Pass any arguments this switch has. */
4370 give_switch (i, 1, 1);
4373 /* We didn't match. Try again. */
4374 if (*p++ == '|')
4375 goto next_member;
4376 return endbody;
4380 /* If name specified ends in *, as in {x*:...},
4381 check for presence of any switch name starting with x. */
4382 if (p[-1] == '*')
4384 for (i = 0; i < n_switches; i++)
4386 unsigned hard_match_len = p - filter - 1;
4388 if (!strncmp (switches[i].part1, filter, hard_match_len)
4389 && check_live_switch (i, hard_match_len))
4391 present = 1;
4395 /* Otherwise, check for presence of exact name specified. */
4396 else
4398 for (i = 0; i < n_switches; i++)
4400 if (!strncmp (switches[i].part1, filter, p - filter)
4401 && switches[i].part1[p - filter] == 0
4402 && check_live_switch (i, -1))
4404 present = 1;
4405 break;
4410 /* If it is as desired (present for %{s...}, absent for %{!s...})
4411 then substitute either the switch or the specified
4412 conditional text. */
4413 if (present != negate)
4415 if (*p == '}')
4417 give_switch (i, 0, include_blanks);
4419 else
4421 if (do_spec_1 (save_string (body, endbody - body - 1),
4422 0, NULL_PTR) < 0)
4423 return 0;
4426 else if (pipe_p)
4428 /* Here if a %{|...} conditional fails: output a minus sign,
4429 which means "standard output" or "standard input". */
4430 do_spec_1 ("-", 0, NULL_PTR);
4431 return endbody;
4435 /* We didn't match; try again. */
4436 if (*p++ == '|')
4437 goto next_member;
4439 return endbody;
4442 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
4443 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
4444 spec, or -1 if either exact match or %* is used.
4446 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
4447 whose value does not begin with "no-" is obsoleted by the same value
4448 with the "no-", similarly for a switch with the "no-" prefix. */
4450 static int
4451 check_live_switch (switchnum, prefix_length)
4452 int switchnum;
4453 int prefix_length;
4455 char *name = switches[switchnum].part1;
4456 int i;
4458 /* In the common case of {<at-most-one-letter>*}, a negating
4459 switch would always match, so ignore that case. We will just
4460 send the conflicting switches to the compiler phase. */
4461 if (prefix_length >= 0 && prefix_length <= 1)
4462 return 1;
4464 /* If we already processed this switch and determined if it was
4465 live or not, return our past determination. */
4466 if (switches[switchnum].live_cond != 0)
4467 return switches[switchnum].live_cond > 0;
4469 /* Now search for duplicate in a manner that depends on the name. */
4470 switch (*name)
4472 case 'O':
4473 for (i = switchnum + 1; i < n_switches; i++)
4474 if (switches[i].part1[0] == 'O')
4476 switches[switchnum].valid = 1;
4477 switches[switchnum].live_cond = -1;
4478 return 0;
4480 break;
4482 case 'W': case 'f': case 'm':
4483 if (! strncmp (name + 1, "no-", 3))
4485 /* We have Xno-YYY, search for XYYY. */
4486 for (i = switchnum + 1; i < n_switches; i++)
4487 if (switches[i].part1[0] == name[0]
4488 && ! strcmp (&switches[i].part1[1], &name[4]))
4490 switches[switchnum].valid = 1;
4491 switches[switchnum].live_cond = -1;
4492 return 0;
4495 else
4497 /* We have XYYY, search for Xno-YYY. */
4498 for (i = switchnum + 1; i < n_switches; i++)
4499 if (switches[i].part1[0] == name[0]
4500 && switches[i].part1[1] == 'n'
4501 && switches[i].part1[2] == 'o'
4502 && switches[i].part1[3] == '-'
4503 && !strcmp (&switches[i].part1[4], &name[1]))
4505 switches[switchnum].valid = 1;
4506 switches[switchnum].live_cond = -1;
4507 return 0;
4510 break;
4513 /* Otherwise the switch is live. */
4514 switches[switchnum].live_cond = 1;
4515 return 1;
4518 /* Pass a switch to the current accumulating command
4519 in the same form that we received it.
4520 SWITCHNUM identifies the switch; it is an index into
4521 the vector of switches gcc received, which is `switches'.
4522 This cannot fail since it never finishes a command line.
4524 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
4526 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
4527 of the switch. */
4529 static void
4530 give_switch (switchnum, omit_first_word, include_blanks)
4531 int switchnum;
4532 int omit_first_word;
4533 int include_blanks;
4535 if (!omit_first_word)
4537 do_spec_1 ("-", 0, NULL_PTR);
4538 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
4541 if (switches[switchnum].args != 0)
4543 char **p;
4544 for (p = switches[switchnum].args; *p; p++)
4546 if (include_blanks)
4547 do_spec_1 (" ", 0, NULL_PTR);
4548 do_spec_1 (*p, 1, NULL_PTR);
4552 do_spec_1 (" ", 0, NULL_PTR);
4553 switches[switchnum].valid = 1;
4556 /* Search for a file named NAME trying various prefixes including the
4557 user's -B prefix and some standard ones.
4558 Return the absolute file name found. If nothing is found, return NAME. */
4560 static char *
4561 find_file (name)
4562 char *name;
4564 char *newname;
4566 /* Try multilib_dir if it is defined. */
4567 if (multilib_dir != NULL)
4569 char *try;
4571 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
4572 strcpy (try, multilib_dir);
4573 strcat (try, dir_separator_str);
4574 strcat (try, name);
4576 newname = find_a_file (&startfile_prefixes, try, R_OK);
4578 /* If we don't find it in the multi library dir, then fall
4579 through and look for it in the normal places. */
4580 if (newname != NULL)
4581 return newname;
4584 newname = find_a_file (&startfile_prefixes, name, R_OK);
4585 return newname ? newname : name;
4588 /* Determine whether a directory exists. If LINKER, return 0 for
4589 certain fixed names not needed by the linker. If not LINKER, it is
4590 only important to return 0 if the host machine has a small ARG_MAX
4591 limit. */
4593 static int
4594 is_directory (path1, path2, linker)
4595 char *path1;
4596 char *path2;
4597 int linker;
4599 int len1 = strlen (path1);
4600 int len2 = strlen (path2);
4601 char *path = (char *) alloca (3 + len1 + len2);
4602 char *cp;
4603 struct stat st;
4605 #ifndef SMALL_ARG_MAX
4606 if (! linker)
4607 return 1;
4608 #endif
4610 /* Construct the path from the two parts. Ensure the string ends with "/.".
4611 The resulting path will be a directory even if the given path is a
4612 symbolic link. */
4613 memcpy (path, path1, len1);
4614 memcpy (path + len1, path2, len2);
4615 cp = path + len1 + len2;
4616 if (cp[-1] != '/' && cp[-1] != DIR_SEPARATOR)
4617 *cp++ = DIR_SEPARATOR;
4618 *cp++ = '.';
4619 *cp = '\0';
4621 /* Exclude directories that the linker is known to search. */
4622 if (linker
4623 && ((cp - path == 6
4624 && strcmp (path, concat (dir_separator_str, "lib",
4625 dir_separator_str, ".", NULL_PTR)) == 0)
4626 || (cp - path == 10
4627 && strcmp (path, concat (dir_separator_str, "usr",
4628 dir_separator_str, "lib",
4629 dir_separator_str, ".", NULL_PTR)) == 0)))
4630 return 0;
4632 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
4635 /* On fatal signals, delete all the temporary files. */
4637 static void
4638 fatal_error (signum)
4639 int signum;
4641 signal (signum, SIG_DFL);
4642 delete_failure_queue ();
4643 delete_temp_files ();
4644 /* Get the same signal again, this time not handled,
4645 so its normal effect occurs. */
4646 kill (getpid (), signum);
4650 main (argc, argv)
4651 int argc;
4652 char **argv;
4654 register size_t i;
4655 size_t j;
4656 int value;
4657 int linker_was_run = 0;
4658 char *explicit_link_files;
4659 char *specs_file;
4660 char *p;
4661 struct user_specs *uptr;
4663 p = argv[0] + strlen (argv[0]);
4664 while (p != argv[0] && p[-1] != '/' && p[-1] != DIR_SEPARATOR) --p;
4665 programname = p;
4667 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
4668 signal (SIGINT, fatal_error);
4669 #ifdef SIGHUP
4670 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
4671 signal (SIGHUP, fatal_error);
4672 #endif
4673 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
4674 signal (SIGTERM, fatal_error);
4675 #ifdef SIGPIPE
4676 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
4677 signal (SIGPIPE, fatal_error);
4678 #endif
4680 argbuf_length = 10;
4681 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
4683 obstack_init (&obstack);
4685 /* Build multilib_select, et. al from the separate lines that make up each
4686 multilib selection. */
4688 char **q = multilib_raw;
4689 int need_space;
4691 obstack_init (&multilib_obstack);
4692 while ((p = *q++) != (char *) 0)
4693 obstack_grow (&multilib_obstack, p, strlen (p));
4695 obstack_1grow (&multilib_obstack, 0);
4696 multilib_select = obstack_finish (&multilib_obstack);
4698 q = multilib_matches_raw;
4699 while ((p = *q++) != (char *) 0)
4700 obstack_grow (&multilib_obstack, p, strlen (p));
4702 obstack_1grow (&multilib_obstack, 0);
4703 multilib_matches = obstack_finish (&multilib_obstack);
4705 need_space = FALSE;
4706 for (i = 0;
4707 i < sizeof (multilib_defaults_raw) / sizeof (multilib_defaults_raw[0]);
4708 i++)
4710 if (need_space)
4711 obstack_1grow (&multilib_obstack, ' ');
4712 obstack_grow (&multilib_obstack,
4713 multilib_defaults_raw[i],
4714 strlen (multilib_defaults_raw[i]));
4715 need_space = TRUE;
4718 obstack_1grow (&multilib_obstack, 0);
4719 multilib_defaults = obstack_finish (&multilib_obstack);
4722 /* Set up to remember the pathname of gcc and any options
4723 needed for collect. We use argv[0] instead of programname because
4724 we need the complete pathname. */
4725 obstack_init (&collect_obstack);
4726 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
4727 obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
4728 putenv (obstack_finish (&collect_obstack));
4730 #ifdef INIT_ENVIRONMENT
4731 /* Set up any other necessary machine specific environment variables. */
4732 putenv (INIT_ENVIRONMENT);
4733 #endif
4735 /* Choose directory for temp files. */
4737 #ifndef MKTEMP_EACH_FILE
4738 temp_filename = choose_temp_base ();
4739 temp_filename_length = strlen (temp_filename);
4740 #endif
4742 /* Make a table of what switches there are (switches, n_switches).
4743 Make a table of specified input files (infiles, n_infiles).
4744 Decode switches that are handled locally. */
4746 process_command (argc, argv);
4749 int first_time;
4751 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4752 the compiler. */
4753 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4754 sizeof ("COLLECT_GCC_OPTIONS=")-1);
4756 first_time = TRUE;
4757 for (i = 0; (int)i < n_switches; i++)
4759 char **args;
4760 char *p, *q;
4761 if (!first_time)
4762 obstack_grow (&collect_obstack, " ", 1);
4764 first_time = FALSE;
4765 obstack_grow (&collect_obstack, "'-", 2);
4766 q = switches[i].part1;
4767 while ((p = index (q,'\'')))
4769 obstack_grow (&collect_obstack, q, p-q);
4770 obstack_grow (&collect_obstack, "'\\''", 4);
4771 q = ++p;
4773 obstack_grow (&collect_obstack, q, strlen (q));
4774 obstack_grow (&collect_obstack, "'", 1);
4776 for (args = switches[i].args; args && *args; args++)
4778 obstack_grow (&collect_obstack, " '", 2);
4779 q = *args;
4780 while ((p = index (q,'\'')))
4782 obstack_grow (&collect_obstack, q, p-q);
4783 obstack_grow (&collect_obstack, "'\\''", 4);
4784 q = ++p;
4786 obstack_grow (&collect_obstack, q, strlen (q));
4787 obstack_grow (&collect_obstack, "'", 1);
4790 obstack_grow (&collect_obstack, "\0", 1);
4791 putenv (obstack_finish (&collect_obstack));
4794 /* Initialize the vector of specs to just the default.
4795 This means one element containing 0s, as a terminator. */
4797 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
4798 bcopy ((char *) default_compilers, (char *) compilers,
4799 sizeof default_compilers);
4800 n_compilers = n_default_compilers;
4802 /* Read specs from a file if there is one. */
4804 machine_suffix = concat (spec_machine, dir_separator_str,
4805 spec_version, dir_separator_str, NULL_PTR);
4806 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
4808 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
4809 /* Read the specs file unless it is a default one. */
4810 if (specs_file != 0 && strcmp (specs_file, "specs"))
4811 read_specs (specs_file, TRUE);
4812 else
4813 init_spec ();
4815 /* We need to check standard_exec_prefix/just_machine_suffix/specs
4816 for any override of as, ld and libraries. */
4817 specs_file = (char *) alloca (strlen (standard_exec_prefix)
4818 + strlen (just_machine_suffix)
4819 + sizeof ("specs"));
4821 strcpy (specs_file, standard_exec_prefix);
4822 strcat (specs_file, just_machine_suffix);
4823 strcat (specs_file, "specs");
4824 if (access (specs_file, R_OK) == 0)
4825 read_specs (specs_file, TRUE);
4827 /* Process any user specified specs in the order given on the command
4828 line. */
4829 for (uptr = user_specs_head; uptr; uptr = uptr->next)
4831 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
4832 read_specs (filename ? filename : uptr->filename, FALSE);
4835 /* If not cross-compiling, look for startfiles in the standard places. */
4836 /* The fact that these are done here, after reading the specs file,
4837 means that it cannot be found in these directories.
4838 But that's okay. It should never be there anyway. */
4839 if (*cross_compile == '0')
4841 #ifdef MD_EXEC_PREFIX
4842 add_prefix (&exec_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4843 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4844 #endif
4846 #ifdef MD_STARTFILE_PREFIX
4847 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
4848 0, 0, NULL_PTR);
4849 #endif
4851 #ifdef MD_STARTFILE_PREFIX_1
4852 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
4853 0, 0, NULL_PTR);
4854 #endif
4856 /* If standard_startfile_prefix is relative, base it on
4857 standard_exec_prefix. This lets us move the installed tree
4858 as a unit. If GCC_EXEC_PREFIX is defined, base
4859 standard_startfile_prefix on that as well. */
4860 if (*standard_startfile_prefix == '/'
4861 || *standard_startfile_prefix == DIR_SEPARATOR
4862 || *standard_startfile_prefix == '$'
4863 #ifdef __MSDOS__
4864 /* Check for disk name on MS-DOS-based systems. */
4865 || (standard_startfile_prefix[1] == ':'
4866 && (standard_startfile_prefix[2] == DIR_SEPARATOR
4867 || standard_startfile_prefix[2] == '/'))
4868 #endif
4870 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
4871 0, 0, NULL_PTR);
4872 else
4874 if (gcc_exec_prefix)
4875 add_prefix (&startfile_prefixes,
4876 concat (gcc_exec_prefix, machine_suffix,
4877 standard_startfile_prefix, NULL_PTR),
4878 NULL_PTR, 0, 0, NULL_PTR);
4879 add_prefix (&startfile_prefixes,
4880 concat (standard_exec_prefix,
4881 machine_suffix,
4882 standard_startfile_prefix, NULL_PTR),
4883 NULL_PTR, 0, 0, NULL_PTR);
4886 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
4887 "BINUTILS", 0, 0, NULL_PTR);
4888 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
4889 "BINUTILS", 0, 0, NULL_PTR);
4890 #if 0 /* Can cause surprises, and one can use -B./ instead. */
4891 add_prefix (&startfile_prefixes, "./", NULL_PTR, 0, 1, NULL_PTR);
4892 #endif
4894 else
4896 if (*standard_startfile_prefix != DIR_SEPARATOR && gcc_exec_prefix)
4897 add_prefix (&startfile_prefixes,
4898 concat (gcc_exec_prefix, machine_suffix,
4899 standard_startfile_prefix, NULL_PTR),
4900 "BINUTILS", 0, 0, NULL_PTR);
4903 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
4904 if (gcc_exec_prefix)
4906 char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
4907 + strlen (spec_version)
4908 + strlen (spec_machine) + 3);
4909 strcpy (temp, gcc_exec_prefix);
4910 strcat (temp, spec_machine);
4911 strcat (temp, dir_separator_str);
4912 strcat (temp, spec_version);
4913 strcat (temp, dir_separator_str);
4914 gcc_exec_prefix = temp;
4917 /* Now we have the specs.
4918 Set the `valid' bits for switches that match anything in any spec. */
4920 validate_all_switches ();
4922 /* Now that we have the switches and the specs, set
4923 the subdirectory based on the options. */
4924 set_multilib_dir ();
4926 /* Warn about any switches that no pass was interested in. */
4928 for (i = 0; (int)i < n_switches; i++)
4929 if (! switches[i].valid)
4930 error ("unrecognized option `-%s'", switches[i].part1);
4932 /* Obey some of the options. */
4934 if (print_search_dirs)
4936 printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
4937 printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
4938 printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
4939 exit (0);
4942 if (print_file_name)
4944 printf ("%s\n", find_file (print_file_name));
4945 exit (0);
4948 if (print_prog_name)
4950 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
4951 printf ("%s\n", (newname ? newname : print_prog_name));
4952 exit (0);
4955 if (print_multi_lib)
4957 print_multilib_info ();
4958 exit (0);
4961 if (print_multi_directory)
4963 if (multilib_dir == NULL)
4964 printf (".\n");
4965 else
4966 printf ("%s\n", multilib_dir);
4967 exit (0);
4970 if (print_help_list)
4972 display_help ();
4974 if (! verbose_flag)
4976 printf ("\nReport bugs to egcs-bugs@cygnus.com.\n");
4977 printf ("Please see the file BUGS (included with the sources) first.\n");
4979 exit (0);
4982 /* We do not exit here. Instead we have created a fake input file
4983 called 'help-dummy' which needs to be compiled, and we pass this
4984 on the the various sub-processes, along with the --help switch. */
4987 if (verbose_flag)
4989 int n;
4991 /* compiler_version is truncated at the first space when initialized
4992 from version string, so truncate version_string at the first space
4993 before comparing. */
4994 for (n = 0; version_string[n]; n++)
4995 if (version_string[n] == ' ')
4996 break;
4998 if (! strncmp (version_string, compiler_version, n)
4999 && compiler_version[n] == 0)
5000 fprintf (stderr, "gcc version %s\n", version_string);
5001 else
5002 fprintf (stderr, "gcc driver version %s executing gcc version %s\n",
5003 version_string, compiler_version);
5005 if (n_infiles == 0)
5006 exit (0);
5009 if (n_infiles == added_libraries)
5010 fatal ("No input files");
5012 /* Make a place to record the compiler output file names
5013 that correspond to the input files. */
5015 i = n_infiles;
5016 #ifdef LANG_SPECIFIC_DRIVER
5017 i += lang_specific_extra_outfiles;
5018 #endif
5019 outfiles = (char **) xmalloc (i * sizeof (char *));
5020 bzero ((char *) outfiles, i * sizeof (char *));
5022 /* Record which files were specified explicitly as link input. */
5024 explicit_link_files = xmalloc (n_infiles);
5025 bzero (explicit_link_files, n_infiles);
5027 for (i = 0; (int)i < n_infiles; i++)
5029 register struct compiler *cp = 0;
5030 int this_file_error = 0;
5032 /* Tell do_spec what to substitute for %i. */
5034 input_filename = infiles[i].name;
5035 input_filename_length = strlen (input_filename);
5036 input_file_number = i;
5038 /* Use the same thing in %o, unless cp->spec says otherwise. */
5040 outfiles[i] = input_filename;
5042 /* Figure out which compiler from the file's suffix. */
5044 cp = lookup_compiler (infiles[i].name, input_filename_length,
5045 infiles[i].language);
5047 if (cp)
5049 /* Ok, we found an applicable compiler. Run its spec. */
5050 /* First say how much of input_filename to substitute for %b */
5051 register char *p;
5052 int len;
5054 if (cp->spec[0][0] == '#')
5055 error ("%s: %s compiler not installed on this system",
5056 input_filename, &cp->spec[0][1]);
5058 input_basename = input_filename;
5059 for (p = input_filename; *p; p++)
5060 if (*p == '/' || *p == DIR_SEPARATOR)
5061 input_basename = p + 1;
5063 /* Find a suffix starting with the last period,
5064 and set basename_length to exclude that suffix. */
5065 basename_length = strlen (input_basename);
5066 p = input_basename + basename_length;
5067 while (p != input_basename && *p != '.') --p;
5068 if (*p == '.' && p != input_basename)
5070 basename_length = p - input_basename;
5071 input_suffix = p + 1;
5073 else
5074 input_suffix = "";
5076 len = 0;
5077 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
5078 if (cp->spec[j])
5079 len += strlen (cp->spec[j]);
5081 p = (char *) xmalloc (len + 1);
5083 len = 0;
5084 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
5085 if (cp->spec[j])
5087 strcpy (p + len, cp->spec[j]);
5088 len += strlen (cp->spec[j]);
5091 value = do_spec (p);
5092 free (p);
5093 if (value < 0)
5094 this_file_error = 1;
5097 /* If this file's name does not contain a recognized suffix,
5098 record it as explicit linker input. */
5100 else
5101 explicit_link_files[i] = 1;
5103 /* Clear the delete-on-failure queue, deleting the files in it
5104 if this compilation failed. */
5106 if (this_file_error)
5108 delete_failure_queue ();
5109 error_count++;
5111 /* If this compilation succeeded, don't delete those files later. */
5112 clear_failure_queue ();
5115 #ifdef LANG_SPECIFIC_DRIVER
5116 if (error_count == 0)
5118 /* Make sure INPUT_FILE_NUMBER points to first available open
5119 slot. */
5120 input_file_number = n_infiles;
5121 if (lang_specific_pre_link ())
5122 error_count++;
5124 #endif
5126 /* Run ld to link all the compiler output files. */
5128 if (error_count == 0)
5130 int tmp = execution_count;
5132 /* We'll use ld if we can't find collect2. */
5133 if (! strcmp (linker_name_spec, "collect2"))
5135 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
5136 if (s == NULL)
5137 linker_name_spec = "ld";
5139 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
5140 for collect. */
5141 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
5142 putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
5144 value = do_spec (link_command_spec);
5145 if (value < 0)
5146 error_count = 1;
5147 linker_was_run = (tmp != execution_count);
5150 /* Warn if a -B option was specified but the prefix was never used. */
5151 unused_prefix_warnings (&exec_prefixes);
5152 unused_prefix_warnings (&startfile_prefixes);
5154 /* If options said don't run linker,
5155 complain about input files to be given to the linker. */
5157 if (! linker_was_run && error_count == 0)
5158 for (i = 0; (int)i < n_infiles; i++)
5159 if (explicit_link_files[i])
5160 error ("%s: linker input file unused since linking not done",
5161 outfiles[i]);
5163 /* Delete some or all of the temporary files we made. */
5165 if (error_count)
5166 delete_failure_queue ();
5167 delete_temp_files ();
5169 if (print_help_list)
5171 printf ("\nReport bugs to egcs-bugs@cygnus.com.\n");
5172 printf ("Please see the file BUGS (included with the sources) first.\n");
5175 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
5176 /* NOTREACHED */
5177 return 0;
5180 /* Find the proper compilation spec for the file name NAME,
5181 whose length is LENGTH. LANGUAGE is the specified language,
5182 or 0 if this file is to be passed to the linker. */
5184 static struct compiler *
5185 lookup_compiler (name, length, language)
5186 char *name;
5187 size_t length;
5188 char *language;
5190 struct compiler *cp;
5192 /* If this was specified by the user to be a linker input, indicate that. */
5193 if (language != 0 && language[0] == '*')
5194 return 0;
5196 /* Otherwise, look for the language, if one is spec'd. */
5197 if (language != 0)
5199 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5200 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
5201 return cp;
5203 error ("language %s not recognized", language);
5204 return 0;
5207 /* Look for a suffix. */
5208 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5210 if (/* The suffix `-' matches only the file name `-'. */
5211 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
5212 || (strlen (cp->suffix) < length
5213 /* See if the suffix matches the end of NAME. */
5214 #ifdef OS2
5215 && ((!strcmp (cp->suffix,
5216 name + length - strlen (cp->suffix))
5217 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
5218 && !strcasecmp (cp->suffix,
5219 name + length - strlen (cp->suffix)))
5220 #else
5221 && !strcmp (cp->suffix,
5222 name + length - strlen (cp->suffix))
5223 #endif
5226 if (cp->spec[0][0] == '@')
5228 struct compiler *new;
5230 /* An alias entry maps a suffix to a language.
5231 Search for the language; pass 0 for NAME and LENGTH
5232 to avoid infinite recursion if language not found.
5233 Construct the new compiler spec. */
5234 language = cp->spec[0] + 1;
5235 new = (struct compiler *) xmalloc (sizeof (struct compiler));
5236 new->suffix = cp->suffix;
5237 bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
5238 (char *) new->spec, sizeof new->spec);
5239 return new;
5242 /* A non-alias entry: return it. */
5243 return cp;
5247 return 0;
5250 char *
5251 xmalloc (size)
5252 unsigned size;
5254 register char *value = (char *) malloc (size);
5255 if (value == 0)
5256 fatal ("virtual memory exhausted");
5257 return value;
5260 char *
5261 xrealloc (ptr, size)
5262 char *ptr;
5263 unsigned size;
5265 register char *value = (char *) realloc (ptr, size);
5266 if (value == 0)
5267 fatal ("virtual memory exhausted");
5268 return value;
5271 /* This function is based on the one in libiberty. */
5273 static char *
5274 concat VPROTO((char *first, ...))
5276 register int length;
5277 register char *newstr;
5278 register char *end;
5279 register char *arg;
5280 va_list args;
5281 #ifndef __STDC__
5282 char *first;
5283 #endif
5285 /* First compute the size of the result and get sufficient memory. */
5287 VA_START (args, first);
5288 #ifndef __STDC__
5289 first = va_arg (args, char *);
5290 #endif
5292 arg = first;
5293 length = 0;
5295 while (arg != 0)
5297 length += strlen (arg);
5298 arg = va_arg (args, char *);
5301 newstr = (char *) xmalloc (length + 1);
5302 va_end (args);
5304 /* Now copy the individual pieces to the result string. */
5306 VA_START (args, first);
5307 #ifndef __STDC__
5308 first = va_arg (args, char *);
5309 #endif
5311 end = newstr;
5312 arg = first;
5313 while (arg != 0)
5315 while (*arg)
5316 *end++ = *arg++;
5317 arg = va_arg (args, char *);
5319 *end = '\000';
5320 va_end (args);
5322 return (newstr);
5325 static char *
5326 save_string (s, len)
5327 char *s;
5328 int len;
5330 register char *result = xmalloc (len + 1);
5332 bcopy (s, result, len);
5333 result[len] = 0;
5334 return result;
5337 static void
5338 pfatal_with_name (name)
5339 char *name;
5341 fatal ("%s: %s", name, my_strerror (errno));
5344 static void
5345 perror_with_name (name)
5346 char *name;
5348 error ("%s: %s", name, my_strerror (errno));
5351 static void
5352 pfatal_pexecute (errmsg_fmt, errmsg_arg)
5353 char *errmsg_fmt;
5354 char *errmsg_arg;
5356 int save_errno = errno;
5358 if (errmsg_arg)
5360 /* Space for trailing '\0' is in %s. */
5361 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
5362 sprintf (msg, errmsg_fmt, errmsg_arg);
5363 errmsg_fmt = msg;
5366 fatal ("%s: %s", errmsg_fmt, my_strerror (save_errno));
5369 /* More 'friendly' abort that prints the line and file.
5370 config.h can #define abort fancy_abort if you like that sort of thing. */
5372 void
5373 fancy_abort ()
5375 fatal ("Internal gcc abort.");
5378 /* Output an error message and exit */
5380 static void
5381 fatal VPROTO((char *format, ...))
5383 #ifndef __STDC__
5384 char *format;
5385 #endif
5386 va_list ap;
5388 VA_START (ap, format);
5390 #ifndef __STDC__
5391 format = va_arg (ap, char *);
5392 #endif
5394 fprintf (stderr, "%s: ", programname);
5395 vfprintf (stderr, format, ap);
5396 va_end (ap);
5397 fprintf (stderr, "\n");
5398 delete_temp_files ();
5399 exit (1);
5402 static void
5403 error VPROTO((char *format, ...))
5405 #ifndef __STDC__
5406 char *format;
5407 #endif
5408 va_list ap;
5410 VA_START (ap, format);
5412 #ifndef __STDC__
5413 format = va_arg (ap, char *);
5414 #endif
5416 fprintf (stderr, "%s: ", programname);
5417 vfprintf (stderr, format, ap);
5418 va_end (ap);
5420 fprintf (stderr, "\n");
5423 static void
5424 validate_all_switches ()
5426 struct compiler *comp;
5427 register char *p;
5428 register char c;
5429 struct spec_list *spec;
5431 for (comp = compilers; comp->spec[0]; comp++)
5433 size_t i;
5434 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
5436 p = comp->spec[i];
5437 while ((c = *p++))
5438 if (c == '%' && *p == '{')
5439 /* We have a switch spec. */
5440 validate_switches (p + 1);
5444 /* look through the linked list of specs read from the specs file */
5445 for (spec = specs; spec ; spec = spec->next)
5447 p = *(spec->ptr_spec);
5448 while ((c = *p++))
5449 if (c == '%' && *p == '{')
5450 /* We have a switch spec. */
5451 validate_switches (p + 1);
5454 p = link_command_spec;
5455 while ((c = *p++))
5456 if (c == '%' && *p == '{')
5457 /* We have a switch spec. */
5458 validate_switches (p + 1);
5461 /* Look at the switch-name that comes after START
5462 and mark as valid all supplied switches that match it. */
5464 static void
5465 validate_switches (start)
5466 char *start;
5468 register char *p = start;
5469 char *filter;
5470 register int i;
5471 int suffix = 0;
5473 if (*p == '|')
5474 ++p;
5476 if (*p == '!')
5477 ++p;
5479 if (*p == '.')
5480 suffix = 1, ++p;
5482 filter = p;
5483 while (*p != ':' && *p != '}') p++;
5485 if (suffix)
5487 else if (p[-1] == '*')
5489 /* Mark all matching switches as valid. */
5490 --p;
5491 for (i = 0; i < n_switches; i++)
5492 if (!strncmp (switches[i].part1, filter, p - filter))
5493 switches[i].valid = 1;
5495 else
5497 /* Mark an exact matching switch as valid. */
5498 for (i = 0; i < n_switches; i++)
5500 if (!strncmp (switches[i].part1, filter, p - filter)
5501 && switches[i].part1[p - filter] == 0)
5502 switches[i].valid = 1;
5507 /* Check whether a particular argument was used. The first time we
5508 canonicalize the switches to keep only the ones we care about. */
5510 static int
5511 used_arg (p, len)
5512 char *p;
5513 int len;
5515 struct mswitchstr {
5516 char *str;
5517 char *replace;
5518 int len;
5519 int rep_len;
5522 static struct mswitchstr *mswitches;
5523 static int n_mswitches;
5524 int i, j;
5526 if (!mswitches)
5528 struct mswitchstr *matches;
5529 char *q;
5530 int cnt = 0;
5532 /* Break multilib_matches into the component strings of string and replacement
5533 string */
5534 for (q = multilib_matches; *q != '\0'; q++)
5535 if (*q == ';')
5536 cnt++;
5538 matches = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
5539 i = 0;
5540 q = multilib_matches;
5541 while (*q != '\0')
5543 matches[i].str = q;
5544 while (*q != ' ')
5546 if (*q == '\0')
5547 abort ();
5548 q++;
5550 *q = '\0';
5551 matches[i].len = q - matches[i].str;
5553 matches[i].replace = ++q;
5554 while (*q != ';' && *q != '\0')
5556 if (*q == ' ')
5557 abort ();
5558 q++;
5560 matches[i].rep_len = q - matches[i].replace;
5561 i++;
5562 if (*q == ';')
5563 *q++ = '\0';
5564 else
5565 break;
5568 /* Now build a list of the replacement string for switches that we care
5569 about. Make sure we allocate at least one entry. This prevents
5570 xmalloc from calling fatal, and prevents us from re-executing this
5571 block of code. */
5572 mswitches
5573 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
5574 * (n_switches ? n_switches : 1));
5575 for (i = 0; i < n_switches; i++)
5577 int xlen = strlen (switches[i].part1);
5578 for (j = 0; j < cnt; j++)
5579 if (xlen == matches[j].len && ! strcmp (switches[i].part1, matches[j].str))
5581 mswitches[n_mswitches].str = matches[j].replace;
5582 mswitches[n_mswitches].len = matches[j].rep_len;
5583 mswitches[n_mswitches].replace = (char *)0;
5584 mswitches[n_mswitches].rep_len = 0;
5585 n_mswitches++;
5586 break;
5591 for (i = 0; i < n_mswitches; i++)
5592 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
5593 return 1;
5595 return 0;
5598 static int
5599 default_arg (p, len)
5600 char *p;
5601 int len;
5603 char *start, *end;
5605 for (start = multilib_defaults; *start != '\0'; start = end+1)
5607 while (*start == ' ' || *start == '\t')
5608 start++;
5610 if (*start == '\0')
5611 break;
5613 for (end = start+1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
5616 if ((end - start) == len && strncmp (p, start, len) == 0)
5617 return 1;
5619 if (*end == '\0')
5620 break;
5623 return 0;
5626 /* Work out the subdirectory to use based on the
5627 options. The format of multilib_select is a list of elements.
5628 Each element is a subdirectory name followed by a list of options
5629 followed by a semicolon. gcc will consider each line in turn. If
5630 none of the options beginning with an exclamation point are
5631 present, and all of the other options are present, that
5632 subdirectory will be used. */
5634 static void
5635 set_multilib_dir ()
5637 char *p = multilib_select;
5638 int this_path_len;
5639 char *this_path, *this_arg;
5640 int not_arg;
5641 int ok;
5643 while (*p != '\0')
5645 /* Ignore newlines. */
5646 if (*p == '\n')
5648 ++p;
5649 continue;
5652 /* Get the initial path. */
5653 this_path = p;
5654 while (*p != ' ')
5656 if (*p == '\0')
5657 abort ();
5658 ++p;
5660 this_path_len = p - this_path;
5662 /* Check the arguments. */
5663 ok = 1;
5664 ++p;
5665 while (*p != ';')
5667 if (*p == '\0')
5668 abort ();
5670 if (! ok)
5672 ++p;
5673 continue;
5676 this_arg = p;
5677 while (*p != ' ' && *p != ';')
5679 if (*p == '\0')
5680 abort ();
5681 ++p;
5684 if (*this_arg != '!')
5685 not_arg = 0;
5686 else
5688 not_arg = 1;
5689 ++this_arg;
5692 /* If this is a default argument, we can just ignore it.
5693 This is true even if this_arg begins with '!'. Beginning
5694 with '!' does not mean that this argument is necessarily
5695 inappropriate for this library: it merely means that
5696 there is a more specific library which uses this
5697 argument. If this argument is a default, we need not
5698 consider that more specific library. */
5699 if (! default_arg (this_arg, p - this_arg))
5701 ok = used_arg (this_arg, p - this_arg);
5702 if (not_arg)
5703 ok = ! ok;
5706 if (*p == ' ')
5707 ++p;
5710 if (ok)
5712 if (this_path_len != 1
5713 || this_path[0] != '.')
5715 multilib_dir = xmalloc (this_path_len + 1);
5716 strncpy (multilib_dir, this_path, this_path_len);
5717 multilib_dir[this_path_len] = '\0';
5719 break;
5722 ++p;
5726 /* Print out the multiple library subdirectory selection
5727 information. This prints out a series of lines. Each line looks
5728 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
5729 required. Only the desired options are printed out, the negative
5730 matches. The options are print without a leading dash. There are
5731 no spaces to make it easy to use the information in the shell.
5732 Each subdirectory is printed only once. This assumes the ordering
5733 generated by the genmultilib script. */
5735 static void
5736 print_multilib_info ()
5738 char *p = multilib_select;
5739 char *last_path = 0, *this_path;
5740 int skip;
5741 int last_path_len = 0;
5743 while (*p != '\0')
5745 /* Ignore newlines. */
5746 if (*p == '\n')
5748 ++p;
5749 continue;
5752 /* Get the initial path. */
5753 this_path = p;
5754 while (*p != ' ')
5756 if (*p == '\0')
5757 abort ();
5758 ++p;
5761 /* If this is a duplicate, skip it. */
5762 skip = (last_path != 0 && p - this_path == last_path_len
5763 && ! strncmp (last_path, this_path, last_path_len));
5765 last_path = this_path;
5766 last_path_len = p - this_path;
5768 /* If this directory requires any default arguments, we can skip
5769 it. We will already have printed a directory identical to
5770 this one which does not require that default argument. */
5771 if (! skip)
5773 char *q;
5775 q = p + 1;
5776 while (*q != ';')
5778 char *arg;
5780 if (*q == '\0')
5781 abort ();
5783 if (*q == '!')
5784 arg = NULL;
5785 else
5786 arg = q;
5788 while (*q != ' ' && *q != ';')
5790 if (*q == '\0')
5791 abort ();
5792 ++q;
5795 if (arg != NULL
5796 && default_arg (arg, q - arg))
5798 skip = 1;
5799 break;
5802 if (*q == ' ')
5803 ++q;
5807 if (! skip)
5809 char *p1;
5811 for (p1 = last_path; p1 < p; p1++)
5812 putchar (*p1);
5813 putchar (';');
5816 ++p;
5817 while (*p != ';')
5819 int use_arg;
5821 if (*p == '\0')
5822 abort ();
5824 if (skip)
5826 ++p;
5827 continue;
5830 use_arg = *p != '!';
5832 if (use_arg)
5833 putchar ('@');
5835 while (*p != ' ' && *p != ';')
5837 if (*p == '\0')
5838 abort ();
5839 if (use_arg)
5840 putchar (*p);
5841 ++p;
5844 if (*p == ' ')
5845 ++p;
5848 if (! skip)
5850 /* If there are extra options, print them now */
5851 if (multilib_extra && *multilib_extra)
5853 int print_at = TRUE;
5854 char *q;
5856 for (q = multilib_extra; *q != '\0'; q++)
5858 if (*q == ' ')
5859 print_at = TRUE;
5860 else
5862 if (print_at)
5863 putchar ('@');
5864 putchar (*q);
5865 print_at = FALSE;
5869 putchar ('\n');
5872 ++p;