* decl.c (grokdeclarator): Remove const and volatile from type after
[official-gcc.git] / gcc / gcc.c
blobab376553a6e49d4e36cd41dd69fe5a5edb651525
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"
36 #ifdef __STDC__
37 #include <stdarg.h>
38 #else
39 #include <varargs.h>
40 #endif
41 #include "system.h"
42 #include <signal.h>
43 #include <sys/stat.h>
45 #include "gansidecl.h"
46 #include "obstack.h"
49 /* ??? Need to find a GCC header to put these in. */
50 extern int pexecute PROTO ((const char *, char * const *, const char *,
51 const char *, char **, char **, int));
52 extern int pwait PROTO ((int, int *, int));
53 extern char *update_path PROTO((char *, char *));
54 /* Flag arguments to pexecute. */
55 #define PEXECUTE_FIRST 1
56 #define PEXECUTE_LAST 2
57 #define PEXECUTE_SEARCH 4
58 #define PEXECUTE_VERBOSE 8
60 #ifndef WIFSIGNALED
61 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
62 #endif
63 #ifndef WTERMSIG
64 #define WTERMSIG(S) ((S) & 0x7f)
65 #endif
66 #ifndef WIFEXITED
67 #define WIFEXITED(S) (((S) & 0xff) == 0)
68 #endif
69 #ifndef WEXITSTATUS
70 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
71 #endif
73 #ifdef VMS
74 #define exit __posix_exit
75 #endif
77 /* Define O_RDONLY if the system hasn't defined it for us. */
78 #ifndef O_RDONLY
79 #define O_RDONLY 0
80 #endif
82 #ifdef USG
83 #define vfork fork
84 #endif /* USG */
86 /* Test if something is a normal file. */
87 #ifndef S_ISREG
88 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
89 #endif
91 /* Test if something is a directory. */
92 #ifndef S_ISDIR
93 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
94 #endif
96 /* By default there is no special suffix for executables. */
97 #ifdef EXECUTABLE_SUFFIX
98 #define HAVE_EXECUTABLE_SUFFIX
99 #else
100 #define EXECUTABLE_SUFFIX ""
101 #endif
103 /* By default, the suffix for object files is ".o". */
104 #ifdef OBJECT_SUFFIX
105 #define HAVE_OBJECT_SUFFIX
106 #else
107 #define OBJECT_SUFFIX ".o"
108 #endif
110 /* By default, colon separates directories in a path. */
111 #ifndef PATH_SEPARATOR
112 #define PATH_SEPARATOR ':'
113 #endif
115 #ifndef DIR_SEPARATOR
116 #define DIR_SEPARATOR '/'
117 #endif
119 static char dir_separator_str[] = {DIR_SEPARATOR, 0};
121 #define obstack_chunk_alloc xmalloc
122 #define obstack_chunk_free free
124 extern char *choose_temp_base PROTO((void));
126 #ifndef HAVE_STRERROR
127 extern int sys_nerr;
128 extern char *sys_errlist[];
129 #else
130 extern char *strerror();
131 #endif
133 #ifndef HAVE_KILL
134 #define kill(p,s) raise(s)
135 #endif
137 /* If a stage of compilation returns an exit status >= 1,
138 compilation of that file ceases. */
140 #define MIN_FATAL_STATUS 1
142 /* Flag saying to print the directories gcc will search through looking for
143 programs, libraries, etc. */
145 static int print_search_dirs;
147 /* Flag saying to print the full filename of this file
148 as found through our usual search mechanism. */
150 static char *print_file_name = NULL;
152 /* As print_file_name, but search for executable file. */
154 static char *print_prog_name = NULL;
156 /* Flag saying to print the relative path we'd use to
157 find libgcc.a given the current compiler flags. */
159 static int print_multi_directory;
161 /* Flag saying to print the list of subdirectories and
162 compiler flags used to select them in a standard form. */
164 static int print_multi_lib;
166 /* Flag indicating whether we should print the command and arguments */
168 static int verbose_flag;
170 /* Nonzero means write "temp" files in source directory
171 and use the source file's name in them, and don't delete them. */
173 static int save_temps_flag;
175 /* The compiler version. */
177 static char *compiler_version;
179 /* The target version specified with -V */
181 static char *spec_version = DEFAULT_TARGET_VERSION;
183 /* The target machine specified with -b. */
185 static char *spec_machine = DEFAULT_TARGET_MACHINE;
187 /* Nonzero if cross-compiling.
188 When -b is used, the value comes from the `specs' file. */
190 #ifdef CROSS_COMPILE
191 static char *cross_compile = "1";
192 #else
193 static char *cross_compile = "0";
194 #endif
196 /* The number of errors that have occurred; the link phase will not be
197 run if this is non-zero. */
198 static int error_count = 0;
200 /* This is the obstack which we use to allocate many strings. */
202 static struct obstack obstack;
204 /* This is the obstack to build an environment variable to pass to
205 collect2 that describes all of the relevant switches of what to
206 pass the compiler in building the list of pointers to constructors
207 and destructors. */
209 static struct obstack collect_obstack;
211 extern char *version_string;
213 /* Forward declaration for prototypes. */
214 struct path_prefix;
216 static void init_spec PROTO((void));
217 static void read_specs PROTO((char *, int));
218 static void set_spec PROTO((char *, char *));
219 static struct compiler *lookup_compiler PROTO((char *, size_t, char *));
220 static char *build_search_list PROTO((struct path_prefix *, char *, int));
221 static void putenv_from_prefixes PROTO((struct path_prefix *, char *));
222 static char *find_a_file PROTO((struct path_prefix *, char *, int));
223 static void add_prefix PROTO((struct path_prefix *, char *, char *,
224 int, int, int *));
225 static char *skip_whitespace PROTO((char *));
226 static void record_temp_file PROTO((char *, int, int));
227 static void delete_if_ordinary PROTO((char *));
228 static void delete_temp_files PROTO((void));
229 static void delete_failure_queue PROTO((void));
230 static void clear_failure_queue PROTO((void));
231 static int check_live_switch PROTO((int, int));
232 static char *handle_braces PROTO((char *));
233 static char *save_string PROTO((char *, int));
234 static char *concat PVPROTO((char *, ...));
235 static int do_spec PROTO((char *));
236 static int do_spec_1 PROTO((char *, int, char *));
237 static char *find_file PROTO((char *));
238 static int is_directory PROTO((char *, char *, int));
239 static void validate_switches PROTO((char *));
240 static void validate_all_switches PROTO((void));
241 static void give_switch PROTO((int, int, int));
242 static int used_arg PROTO((char *, int));
243 static int default_arg PROTO((char *, int));
244 static void set_multilib_dir PROTO((void));
245 static void print_multilib_info PROTO((void));
246 static void pfatal_with_name PROTO((char *));
247 static void perror_with_name PROTO((char *));
248 static void pfatal_pexecute PROTO((char *, char *));
249 #ifdef HAVE_VPRINTF
250 static void fatal PVPROTO((char *, ...));
251 static void error PVPROTO((char *, ...));
252 #else
253 /* We must not provide any prototype here, even if ANSI C. */
254 static void fatal PROTO(());
255 static void error PROTO(());
256 #endif
258 void fancy_abort ();
259 char *xmalloc ();
260 char *xrealloc ();
262 #ifdef LANG_SPECIFIC_DRIVER
263 extern void lang_specific_driver PROTO ((void (*) (), int *, char ***));
264 #endif
266 /* Specs are strings containing lines, each of which (if not blank)
267 is made up of a program name, and arguments separated by spaces.
268 The program name must be exact and start from root, since no path
269 is searched and it is unreliable to depend on the current working directory.
270 Redirection of input or output is not supported; the subprograms must
271 accept filenames saying what files to read and write.
273 In addition, the specs can contain %-sequences to substitute variable text
274 or for conditional text. Here is a table of all defined %-sequences.
275 Note that spaces are not generated automatically around the results of
276 expanding these sequences; therefore, you can concatenate them together
277 or with constant text in a single argument.
279 %% substitute one % into the program name or argument.
280 %i substitute the name of the input file being processed.
281 %b substitute the basename of the input file being processed.
282 This is the substring up to (and not including) the last period
283 and not including the directory.
284 %g substitute the temporary-file-name-base. This is a string chosen
285 once per compilation. Different temporary file names are made by
286 concatenation of constant strings on the end, as in `%g.s'.
287 %g also has the same effect of %d.
288 %u like %g, but make the temporary file name unique.
289 %U returns the last file name generated with %u.
290 %d marks the argument containing or following the %d as a
291 temporary file name, so that that file will be deleted if CC exits
292 successfully. Unlike %g, this contributes no text to the argument.
293 %w marks the argument containing or following the %w as the
294 "output file" of this compilation. This puts the argument
295 into the sequence of arguments that %o will substitute later.
296 %W{...}
297 like %{...} but mark last argument supplied within
298 as a file to be deleted on failure.
299 %o substitutes the names of all the output files, with spaces
300 automatically placed around them. You should write spaces
301 around the %o as well or the results are undefined.
302 %o is for use in the specs for running the linker.
303 Input files whose names have no recognized suffix are not compiled
304 at all, but they are included among the output files, so they will
305 be linked.
306 %O substitutes the suffix for object files.
307 %p substitutes the standard macro predefinitions for the
308 current target machine. Use this when running cpp.
309 %P like %p, but puts `__' before and after the name of each macro.
310 (Except macros that already have __.)
311 This is for ANSI C.
312 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
313 %s current argument is the name of a library or startup file of some sort.
314 Search for that file in a standard list of directories
315 and substitute the full name found.
316 %eSTR Print STR as an error message. STR is terminated by a newline.
317 Use this when inconsistent options are detected.
318 %x{OPTION} Accumulate an option for %X.
319 %X Output the accumulated linker options specified by compilations.
320 %Y Output the accumulated assembler options specified by compilations.
321 %Z Output the accumulated preprocessor options specified by compilations.
322 %v1 Substitute the major version number of GCC.
323 (For version 2.5.n, this is 2.)
324 %v2 Substitute the minor version number of GCC.
325 (For version 2.5.n, this is 5.)
326 %a process ASM_SPEC as a spec.
327 This allows config.h to specify part of the spec for running as.
328 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
329 used here. This can be used to run a post-processor after the
330 assembler has done it's job.
331 %D Dump out a -L option for each directory in startfile_prefixes.
332 If multilib_dir is set, extra entries are generated with it affixed.
333 %l process LINK_SPEC as a spec.
334 %L process LIB_SPEC as a spec.
335 %G process LIBGCC_SPEC as a spec.
336 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
337 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
338 %c process SIGNED_CHAR_SPEC as a spec.
339 %C process CPP_SPEC as a spec. A capital C is actually used here.
340 %1 process CC1_SPEC as a spec.
341 %2 process CC1PLUS_SPEC as a spec.
342 %| output "-" if the input for the current command is coming from a pipe.
343 %* substitute the variable part of a matched option. (See below.)
344 Note that each comma in the substituted string is replaced by
345 a single space.
346 %{S} substitutes the -S switch, if that switch was given to CC.
347 If that switch was not specified, this substitutes nothing.
348 Here S is a metasyntactic variable.
349 %{S*} substitutes all the switches specified to CC whose names start
350 with -S. This is used for -o, -D, -I, etc; switches that take
351 arguments. CC considers `-o foo' as being one switch whose
352 name starts with `o'. %{o*} would substitute this text,
353 including the space; thus, two arguments would be generated.
354 %{^S*} likewise, but don't put a blank between a switch and any args.
355 %{S*:X} substitutes X if one or more switches whose names start with -S are
356 specified to CC. Note that the tail part of the -S option
357 (i.e. the part matched by the `*') will be substituted for each
358 occurrence of %* within X.
359 %{S:X} substitutes X, but only if the -S switch was given to CC.
360 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
361 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
362 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
363 %{.S:X} substitutes X, but only if processing a file with suffix S.
364 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
365 %(Spec) processes a specification defined in a specs file as *Spec:
366 %[Spec] as above, but put __ around -D arguments
368 The conditional text X in a %{S:X} or %{!S:X} construct may contain
369 other nested % constructs or spaces, or even newlines. They are
370 processed as usual, as described above.
372 The -O, -f, -m, and -W switches are handled specifically in these
373 constructs. If another value of -O or the negated form of a -f, -m, or
374 -W switch is found later in the command line, the earlier switch
375 value is ignored, except with {S*} where S is just one letter; this
376 passes all matching options.
378 The character | is used to indicate that a command should be piped to
379 the following command, but only if -pipe is specified.
381 Note that it is built into CC which switches take arguments and which
382 do not. You might think it would be useful to generalize this to
383 allow each compiler's spec to say which switches take arguments. But
384 this cannot be done in a consistent fashion. CC cannot even decide
385 which input files have been specified without knowing which switches
386 take arguments, and it must know which input files to compile in order
387 to tell which compilers to run.
389 CC also knows implicitly that arguments starting in `-l' are to be
390 treated as compiler output files, and passed to the linker in their
391 proper position among the other output files. */
393 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
395 /* config.h can define ASM_SPEC to provide extra args to the assembler
396 or extra switch-translations. */
397 #ifndef ASM_SPEC
398 #define ASM_SPEC ""
399 #endif
401 /* config.h can define ASM_FINAL_SPEC to run a post processor after
402 the assembler has run. */
403 #ifndef ASM_FINAL_SPEC
404 #define ASM_FINAL_SPEC ""
405 #endif
407 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
408 or extra switch-translations. */
409 #ifndef CPP_SPEC
410 #define CPP_SPEC ""
411 #endif
413 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
414 or extra switch-translations. */
415 #ifndef CC1_SPEC
416 #define CC1_SPEC ""
417 #endif
419 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
420 or extra switch-translations. */
421 #ifndef CC1PLUS_SPEC
422 #define CC1PLUS_SPEC ""
423 #endif
425 /* config.h can define LINK_SPEC to provide extra args to the linker
426 or extra switch-translations. */
427 #ifndef LINK_SPEC
428 #define LINK_SPEC ""
429 #endif
431 /* config.h can define LIB_SPEC to override the default libraries. */
432 #ifndef LIB_SPEC
433 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
434 #endif
436 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
437 included. */
438 #ifndef LIBGCC_SPEC
439 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
440 /* Have gcc do the search for libgcc.a. */
441 #define LIBGCC_SPEC "libgcc.a%s"
442 #else
443 #define LIBGCC_SPEC "-lgcc"
444 #endif
445 #endif
447 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
448 #ifndef STARTFILE_SPEC
449 #define STARTFILE_SPEC \
450 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
451 #endif
453 /* config.h can define SWITCHES_NEED_SPACES to control which options
454 require spaces between the option and the argument. */
455 #ifndef SWITCHES_NEED_SPACES
456 #define SWITCHES_NEED_SPACES ""
457 #endif
459 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
460 #ifndef ENDFILE_SPEC
461 #define ENDFILE_SPEC ""
462 #endif
464 /* This spec is used for telling cpp whether char is signed or not. */
465 #ifndef SIGNED_CHAR_SPEC
466 /* Use #if rather than ?:
467 because MIPS C compiler rejects like ?: in initializers. */
468 #if DEFAULT_SIGNED_CHAR
469 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
470 #else
471 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
472 #endif
473 #endif
475 static char *cpp_spec = CPP_SPEC;
476 static char *cpp_predefines = CPP_PREDEFINES;
477 static char *cc1_spec = CC1_SPEC;
478 static char *cc1plus_spec = CC1PLUS_SPEC;
479 static char *signed_char_spec = SIGNED_CHAR_SPEC;
480 static char *asm_spec = ASM_SPEC;
481 static char *asm_final_spec = ASM_FINAL_SPEC;
482 static char *link_spec = LINK_SPEC;
483 static char *lib_spec = LIB_SPEC;
484 static char *libgcc_spec = LIBGCC_SPEC;
485 static char *endfile_spec = ENDFILE_SPEC;
486 static char *startfile_spec = STARTFILE_SPEC;
487 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
489 /* Some compilers have limits on line lengths, and the multilib_select
490 and/or multilib_matches strings can be very long, so we build them at
491 run time. */
492 static struct obstack multilib_obstack;
493 static char *multilib_select;
494 static char *multilib_matches;
495 static char *multilib_defaults;
496 #include "multilib.h"
498 /* Check whether a particular argument is a default argument. */
500 #ifndef MULTILIB_DEFAULTS
501 #define MULTILIB_DEFAULTS { "" }
502 #endif
504 static char *multilib_defaults_raw[] = MULTILIB_DEFAULTS;
506 struct user_specs {
507 struct user_specs *next;
508 char *filename;
511 static struct user_specs *user_specs_head, *user_specs_tail;
513 /* This defines which switch letters take arguments. */
515 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
516 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
517 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
518 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
519 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
520 || (CHAR) == 'B' || (CHAR) == 'b')
522 #ifndef SWITCH_TAKES_ARG
523 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
524 #endif
526 /* This defines which multi-letter switches take arguments. */
528 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
529 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
530 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
531 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
532 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
533 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
534 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
536 #ifndef WORD_SWITCH_TAKES_ARG
537 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
538 #endif
540 /* Record the mapping from file suffixes for compilation specs. */
542 struct compiler
544 char *suffix; /* Use this compiler for input files
545 whose names end in this suffix. */
547 char *spec[4]; /* To use this compiler, concatenate these
548 specs and pass to do_spec. */
551 /* Pointer to a vector of `struct compiler' that gives the spec for
552 compiling a file, based on its suffix.
553 A file that does not end in any of these suffixes will be passed
554 unchanged to the loader and nothing else will be done to it.
556 An entry containing two 0s is used to terminate the vector.
558 If multiple entries match a file, the last matching one is used. */
560 static struct compiler *compilers;
562 /* Number of entries in `compilers', not counting the null terminator. */
564 static int n_compilers;
566 /* The default list of file name suffixes and their compilation specs. */
568 static struct compiler default_compilers[] =
570 /* Add lists of suffixes of known languages here. If those languages
571 were not present when we built the driver, we will hit these copies
572 and be given a more meaningful error than "file not used since
573 linking is not done". */
574 {".cc", {"#C++"}}, {".cxx", {"#C++"}}, {".cpp", {"#C++"}}, {".c++", {"#C++"}},
575 {".C", {"#C++"}}, {".ads", {"#Ada"}}, {".adb", {"#Ada"}}, {".ada", {"#Ada"}},
576 {".f", {"#Fortran"}}, {".for", {"#Fortran"}}, {".F", {"#Fortran"}},
577 {".fpp", {"#Fortran"}},
578 {".p", {"#Pascal"}}, {".pas", {"#Pascal"}},
579 /* Next come the entries for C. */
580 {".c", {"@c"}},
581 {"@c",
582 {"cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
583 %{C:%{!E:%eGNU C does not support -C without using -E}}\
584 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
585 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
586 %{ansi:-trigraphs -D__STRICT_ANSI__}\
587 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
588 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
589 %{traditional} %{ftraditional:-traditional}\
590 %{traditional-cpp:-traditional}\
591 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
592 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
593 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
594 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
595 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
596 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
597 %{aux-info*}\
598 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
599 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
600 %{!S:as %a %Y\
601 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
602 %{!pipe:%g.s} %A\n }}}}"}},
603 {"-",
604 {"%{E:cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
605 %{C:%{!E:%eGNU C does not support -C without using -E}}\
606 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
607 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
608 %{ansi:-trigraphs -D__STRICT_ANSI__}\
609 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
610 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
611 %{traditional} %{ftraditional:-traditional}\
612 %{traditional-cpp:-traditional}\
613 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
614 %i %W{o*}}\
615 %{!E:%e-E required when input is from standard input}"}},
616 {".m", {"@objective-c"}},
617 {"@objective-c",
618 {"cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
619 %{C:%{!E:%eGNU C does not support -C without using -E}}\
620 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
621 -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
622 %{ansi:-trigraphs -D__STRICT_ANSI__}\
623 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
624 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
625 %{traditional} %{ftraditional:-traditional}\
626 %{traditional-cpp:-traditional}\
627 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
628 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
629 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
630 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a*}\
631 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
632 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
633 -lang-objc %{gen-decls} \
634 %{aux-info*}\
635 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
636 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
637 %{!S:as %a %Y\
638 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
639 %{!pipe:%g.s} %A\n }}}}"}},
640 {".h", {"@c-header"}},
641 {"@c-header",
642 {"%{!E:%eCompilation of header file requested} \
643 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
644 %{C:%{!E:%eGNU C does not support -C without using -E}}\
645 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
646 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
647 %{ansi:-trigraphs -D__STRICT_ANSI__}\
648 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
649 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
650 %{traditional} %{ftraditional:-traditional}\
651 %{traditional-cpp:-traditional}\
652 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
653 %i %W{o*}"}},
654 {".i", {"@cpp-output"}},
655 {"@cpp-output",
656 {"%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a*}\
657 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi}\
658 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
659 %{aux-info*}\
660 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
661 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
662 %{!S:as %a %Y\
663 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
664 %{!pipe:%g.s} %A\n }}}}"}},
665 {".s", {"@assembler"}},
666 {"@assembler",
667 {"%{!M:%{!MM:%{!E:%{!S:as %a %Y\
668 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
669 %i %A\n }}}}"}},
670 {".S", {"@assembler-with-cpp"}},
671 {"@assembler-with-cpp",
672 {"cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
673 %{C:%{!E:%eGNU C does not support -C without using -E}}\
674 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
675 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
676 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
677 %{traditional} %{ftraditional:-traditional}\
678 %{traditional-cpp:-traditional}\
679 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
680 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
681 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
682 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
683 %{!pipe:%g.s} %A\n }}}}"}},
684 #include "specs.h"
685 /* Mark end of table */
686 {0, {0}}
689 /* Number of elements in default_compilers, not counting the terminator. */
691 static int n_default_compilers
692 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
694 /* Here is the spec for running the linker, after compiling all files. */
696 /* -u* was put back because both BSD and SysV seem to support it. */
697 /* %{static:} simply prevents an error message if the target machine
698 doesn't handle -static. */
699 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
700 scripts which exist in user specified directories, or in standard
701 directories. */
702 #ifdef LINK_LIBGCC_SPECIAL
703 /* Don't generate -L options. */
704 static char *link_command_spec = "\
705 %{!fsyntax-only: \
706 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
707 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
708 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
709 %{static:} %{L*} %o\
710 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
711 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
712 %{T*}\
713 \n }}}}}}";
714 #else
715 /* Use -L. */
716 static char *link_command_spec = "\
717 %{!fsyntax-only: \
718 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
719 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
720 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
721 %{static:} %{L*} %D %o\
722 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
723 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
724 %{T*}\
725 \n }}}}}}";
726 #endif
728 /* A vector of options to give to the linker.
729 These options are accumulated by %x,
730 and substituted into the linker command with %X. */
731 static int n_linker_options;
732 static char **linker_options;
734 /* A vector of options to give to the assembler.
735 These options are accumulated by -Wa,
736 and substituted into the assembler command with %Y. */
737 static int n_assembler_options;
738 static char **assembler_options;
740 /* A vector of options to give to the preprocessor.
741 These options are accumulated by -Wp,
742 and substituted into the preprocessor command with %Z. */
743 static int n_preprocessor_options;
744 static char **preprocessor_options;
746 /* Define how to map long options into short ones. */
748 /* This structure describes one mapping. */
749 struct option_map
751 /* The long option's name. */
752 char *name;
753 /* The equivalent short option. */
754 char *equivalent;
755 /* Argument info. A string of flag chars; NULL equals no options.
756 a => argument required.
757 o => argument optional.
758 j => join argument to equivalent, making one word.
759 * => require other text after NAME as an argument. */
760 char *arg_info;
763 /* This is the table of mappings. Mappings are tried sequentially
764 for each option encountered; the first one that matches, wins. */
766 struct option_map option_map[] =
768 {"--all-warnings", "-Wall", 0},
769 {"--ansi", "-ansi", 0},
770 {"--assemble", "-S", 0},
771 {"--assert", "-A", "a"},
772 {"--comments", "-C", 0},
773 {"--compile", "-c", 0},
774 {"--debug", "-g", "oj"},
775 {"--define-macro", "-D", "aj"},
776 {"--dependencies", "-M", 0},
777 {"--dump", "-d", "a"},
778 {"--dumpbase", "-dumpbase", "a"},
779 {"--entry", "-e", 0},
780 {"--extra-warnings", "-W", 0},
781 {"--for-assembler", "-Wa", "a"},
782 {"--for-linker", "-Xlinker", "a"},
783 {"--force-link", "-u", "a"},
784 {"--imacros", "-imacros", "a"},
785 {"--include", "-include", "a"},
786 {"--include-barrier", "-I-", 0},
787 {"--include-directory", "-I", "aj"},
788 {"--include-directory-after", "-idirafter", "a"},
789 {"--include-prefix", "-iprefix", "a"},
790 {"--include-with-prefix", "-iwithprefix", "a"},
791 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
792 {"--include-with-prefix-after", "-iwithprefix", "a"},
793 {"--language", "-x", "a"},
794 {"--library-directory", "-L", "a"},
795 {"--machine", "-m", "aj"},
796 {"--machine-", "-m", "*j"},
797 {"--no-line-commands", "-P", 0},
798 {"--no-precompiled-includes", "-noprecomp", 0},
799 {"--no-standard-includes", "-nostdinc", 0},
800 {"--no-standard-libraries", "-nostdlib", 0},
801 {"--no-warnings", "-w", 0},
802 {"--optimize", "-O", "oj"},
803 {"--output", "-o", "a"},
804 {"--pedantic", "-pedantic", 0},
805 {"--pedantic-errors", "-pedantic-errors", 0},
806 {"--pipe", "-pipe", 0},
807 {"--prefix", "-B", "a"},
808 {"--preprocess", "-E", 0},
809 {"--print-search-dirs", "-print-search-dirs", 0},
810 {"--print-file-name", "-print-file-name=", "aj"},
811 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
812 {"--print-missing-file-dependencies", "-MG", 0},
813 {"--print-multi-lib", "-print-multi-lib", 0},
814 {"--print-multi-directory", "-print-multi-directory", 0},
815 {"--print-prog-name", "-print-prog-name=", "aj"},
816 {"--profile", "-p", 0},
817 {"--profile-blocks", "-a", 0},
818 {"--quiet", "-q", 0},
819 {"--save-temps", "-save-temps", 0},
820 {"--shared", "-shared", 0},
821 {"--silent", "-q", 0},
822 {"--specs", "-specs=", "aj"},
823 {"--static", "-static", 0},
824 {"--symbolic", "-symbolic", 0},
825 {"--target", "-b", "a"},
826 {"--trace-includes", "-H", 0},
827 {"--traditional", "-traditional", 0},
828 {"--traditional-cpp", "-traditional-cpp", 0},
829 {"--trigraphs", "-trigraphs", 0},
830 {"--undefine-macro", "-U", "aj"},
831 {"--use-version", "-V", "a"},
832 {"--user-dependencies", "-MM", 0},
833 {"--verbose", "-v", 0},
834 {"--version", "-dumpversion", 0},
835 {"--warn-", "-W", "*j"},
836 {"--write-dependencies", "-MD", 0},
837 {"--write-user-dependencies", "-MMD", 0},
838 {"--", "-f", "*j"}
841 /* Translate the options described by *ARGCP and *ARGVP.
842 Make a new vector and store it back in *ARGVP,
843 and store its length in *ARGVC. */
845 static void
846 translate_options (argcp, argvp)
847 int *argcp;
848 char ***argvp;
850 int i, j, k;
851 int argc = *argcp;
852 char **argv = *argvp;
853 char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
854 int newindex = 0;
856 i = 0;
857 newv[newindex++] = argv[i++];
859 while (i < argc)
861 /* Translate -- options. */
862 if (argv[i][0] == '-' && argv[i][1] == '-')
864 /* Find a mapping that applies to this option. */
865 for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
867 size_t optlen = strlen (option_map[j].name);
868 size_t arglen = strlen (argv[i]);
869 size_t complen = arglen > optlen ? optlen : arglen;
870 char *arginfo = option_map[j].arg_info;
872 if (arginfo == 0)
873 arginfo = "";
875 if (!strncmp (argv[i], option_map[j].name, complen))
877 char *arg = 0;
879 if (arglen < optlen)
881 for (k = j + 1;
882 k < sizeof (option_map) / sizeof (option_map[0]);
883 k++)
884 if (strlen (option_map[k].name) >= arglen
885 && !strncmp (argv[i], option_map[k].name, arglen))
887 error ("Ambiguous abbreviation %s", argv[i]);
888 break;
891 if (k != sizeof (option_map) / sizeof (option_map[0]))
892 break;
895 if (arglen > optlen)
897 /* If the option has an argument, accept that. */
898 if (argv[i][optlen] == '=')
899 arg = argv[i] + optlen + 1;
901 /* If this mapping requires extra text at end of name,
902 accept that as "argument". */
903 else if (index (arginfo, '*') != 0)
904 arg = argv[i] + optlen;
906 /* Otherwise, extra text at end means mismatch.
907 Try other mappings. */
908 else
909 continue;
912 else if (index (arginfo, '*') != 0)
914 error ("Incomplete `%s' option", option_map[j].name);
915 break;
918 /* Handle arguments. */
919 if (index (arginfo, 'a') != 0)
921 if (arg == 0)
923 if (i + 1 == argc)
925 error ("Missing argument to `%s' option",
926 option_map[j].name);
927 break;
930 arg = argv[++i];
933 else if (index (arginfo, '*') != 0)
935 else if (index (arginfo, 'o') == 0)
937 if (arg != 0)
938 error ("Extraneous argument to `%s' option",
939 option_map[j].name);
940 arg = 0;
943 /* Store the translation as one argv elt or as two. */
944 if (arg != 0 && index (arginfo, 'j') != 0)
945 newv[newindex++] = concat (option_map[j].equivalent, arg,
946 NULL_PTR);
947 else if (arg != 0)
949 newv[newindex++] = option_map[j].equivalent;
950 newv[newindex++] = arg;
952 else
953 newv[newindex++] = option_map[j].equivalent;
955 break;
958 i++;
961 /* Handle old-fashioned options--just copy them through,
962 with their arguments. */
963 else if (argv[i][0] == '-')
965 char *p = argv[i] + 1;
966 int c = *p;
967 int nskip = 1;
969 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
970 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
971 else if (WORD_SWITCH_TAKES_ARG (p))
972 nskip += WORD_SWITCH_TAKES_ARG (p);
973 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
974 && p[1] == 0)
975 nskip += 1;
976 else if (! strcmp (p, "Xlinker"))
977 nskip += 1;
979 /* Watch out for an option at the end of the command line that
980 is missing arguments, and avoid skipping past the end of the
981 command line. */
982 if (nskip + i > argc)
983 nskip = argc - i;
985 while (nskip > 0)
987 newv[newindex++] = argv[i++];
988 nskip--;
991 else
992 /* Ordinary operands, or +e options. */
993 newv[newindex++] = argv[i++];
996 newv[newindex] = 0;
998 *argvp = newv;
999 *argcp = newindex;
1002 char *
1003 my_strerror(e)
1004 int e;
1006 #ifdef HAVE_STRERROR
1008 return strerror(e);
1010 #else
1012 static char buffer[30];
1013 if (!e)
1014 return "cannot access";
1016 if (e > 0 && e < sys_nerr)
1017 return sys_errlist[e];
1019 sprintf (buffer, "Unknown error %d", e);
1020 return buffer;
1021 #endif
1024 static char *
1025 skip_whitespace (p)
1026 char *p;
1028 while (1)
1030 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1031 be considered whitespace. */
1032 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1033 return p + 1;
1034 else if (*p == '\n' || *p == ' ' || *p == '\t')
1035 p++;
1036 else if (*p == '#')
1038 while (*p != '\n') p++;
1039 p++;
1041 else
1042 break;
1045 return p;
1048 /* Structure to keep track of the specs that have been defined so far.
1049 These are accessed using %(specname) or %[specname] in a compiler
1050 or link spec. */
1052 struct spec_list
1054 /* The following 2 fields must be first */
1055 /* to allow EXTRA_SPECS to be initialized */
1056 char *name; /* name of the spec. */
1057 char *ptr; /* available ptr if no static pointer */
1059 /* The following fields are not initialized */
1060 /* by EXTRA_SPECS */
1061 char **ptr_spec; /* pointer to the spec itself. */
1062 struct spec_list *next; /* Next spec in linked list. */
1063 int name_len; /* length of the name */
1064 int alloc_p; /* whether string was allocated */
1067 #define INIT_STATIC_SPEC(NAME,PTR) \
1068 { NAME, NULL_PTR, PTR, (struct spec_list *)0, sizeof (NAME)-1, 0 }
1070 /* List of statically defined specs */
1071 static struct spec_list static_specs[] = {
1072 INIT_STATIC_SPEC ("asm", &asm_spec),
1073 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1074 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1075 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1076 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1077 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1078 INIT_STATIC_SPEC ("link", &link_spec),
1079 INIT_STATIC_SPEC ("lib", &lib_spec),
1080 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1081 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1082 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1083 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1084 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1085 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1086 INIT_STATIC_SPEC ("version", &compiler_version),
1087 INIT_STATIC_SPEC ("multilib", &multilib_select),
1088 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1089 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1090 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1093 #ifdef EXTRA_SPECS /* additional specs needed */
1094 static struct spec_list extra_specs[] = { EXTRA_SPECS };
1095 #endif
1097 /* List of dynamically allocates specs that have been defined so far. */
1099 static struct spec_list *specs = (struct spec_list *)0;
1102 /* Initialize the specs lookup routines. */
1104 static void
1105 init_spec ()
1107 struct spec_list *next = (struct spec_list *)0;
1108 struct spec_list *sl = (struct spec_list *)0;
1109 int i;
1111 if (specs)
1112 return; /* already initialized */
1114 if (verbose_flag)
1115 fprintf (stderr, "Using builtin specs.\n");
1117 #ifdef EXTRA_SPECS
1118 for (i = (sizeof (extra_specs) / sizeof (extra_specs[0])) - 1; i >= 0; i--)
1120 sl = &extra_specs[i];
1121 sl->next = next;
1122 sl->name_len = strlen (sl->name);
1123 sl->ptr_spec = &sl->ptr;
1124 next = sl;
1126 #endif
1128 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1; i >= 0; i--)
1130 sl = &static_specs[i];
1131 sl->next = next;
1132 next = sl;
1135 specs = sl;
1139 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1140 removed; If the spec starts with a + then SPEC is added to the end of the
1141 current spec. */
1143 static void
1144 set_spec (name, spec)
1145 char *name;
1146 char *spec;
1148 struct spec_list *sl;
1149 char *old_spec;
1150 int name_len = strlen (name);
1151 int i;
1153 /* If this is the first call, initialize the statically allocated specs */
1154 if (!specs)
1156 struct spec_list *next = (struct spec_list *)0;
1157 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1;
1158 i >= 0; i--)
1160 sl = &static_specs[i];
1161 sl->next = next;
1162 next = sl;
1164 specs = sl;
1167 /* See if the spec already exists */
1168 for (sl = specs; sl; sl = sl->next)
1169 if (name_len == sl->name_len && !strcmp (sl->name, name))
1170 break;
1172 if (!sl)
1174 /* Not found - make it */
1175 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1176 sl->name = save_string (name, strlen (name));
1177 sl->name_len = name_len;
1178 sl->ptr_spec = &sl->ptr;
1179 sl->alloc_p = 0;
1180 *(sl->ptr_spec) = "";
1181 sl->next = specs;
1182 specs = sl;
1185 old_spec = *(sl->ptr_spec);
1186 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE (spec[1]))
1187 ? concat (old_spec, spec + 1, NULL_PTR)
1188 : save_string (spec, strlen (spec)));
1190 #ifdef DEBUG_SPECS
1191 if (verbose_flag)
1192 fprintf (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1193 #endif
1195 /* Free the old spec */
1196 if (old_spec && sl->alloc_p)
1197 free (old_spec);
1199 sl->alloc_p = 1;
1202 /* Accumulate a command (program name and args), and run it. */
1204 /* Vector of pointers to arguments in the current line of specifications. */
1206 static char **argbuf;
1208 /* Number of elements allocated in argbuf. */
1210 static int argbuf_length;
1212 /* Number of elements in argbuf currently in use (containing args). */
1214 static int argbuf_index;
1216 #ifdef MKTEMP_EACH_FILE
1217 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1218 temp file. */
1220 static struct temp_name {
1221 char *suffix; /* suffix associated with the code. */
1222 int length; /* strlen (suffix). */
1223 int unique; /* Indicates whether %g or %u/%U was used. */
1224 char *filename; /* associated filename. */
1225 int filename_length; /* strlen (filename). */
1226 struct temp_name *next;
1227 } *temp_names;
1228 #endif
1230 /* Number of commands executed so far. */
1232 static int execution_count;
1234 /* Number of commands that exited with a signal. */
1236 static int signal_count;
1238 /* Name with which this program was invoked. */
1240 static char *programname;
1242 /* Structures to keep track of prefixes to try when looking for files. */
1244 struct prefix_list
1246 char *prefix; /* String to prepend to the path. */
1247 struct prefix_list *next; /* Next in linked list. */
1248 int require_machine_suffix; /* Don't use without machine_suffix. */
1249 /* 2 means try both machine_suffix and just_machine_suffix. */
1250 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1253 struct path_prefix
1255 struct prefix_list *plist; /* List of prefixes to try */
1256 int max_len; /* Max length of a prefix in PLIST */
1257 char *name; /* Name of this list (used in config stuff) */
1260 /* List of prefixes to try when looking for executables. */
1262 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1264 /* List of prefixes to try when looking for startup (crt0) files. */
1266 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1268 /* List of prefixes to try when looking for include files. */
1270 static struct path_prefix include_prefixes = { 0, 0, "include" };
1272 /* Suffix to attach to directories searched for commands.
1273 This looks like `MACHINE/VERSION/'. */
1275 static char *machine_suffix = 0;
1277 /* Suffix to attach to directories searched for commands.
1278 This is just `MACHINE/'. */
1280 static char *just_machine_suffix = 0;
1282 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1284 static char *gcc_exec_prefix;
1286 /* Default prefixes to attach to command names. */
1288 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1289 #undef MD_EXEC_PREFIX
1290 #undef MD_STARTFILE_PREFIX
1291 #undef MD_STARTFILE_PREFIX_1
1292 #endif
1294 #ifndef STANDARD_EXEC_PREFIX
1295 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1296 #endif /* !defined STANDARD_EXEC_PREFIX */
1298 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1299 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1300 #ifdef MD_EXEC_PREFIX
1301 static char *md_exec_prefix = MD_EXEC_PREFIX;
1302 #endif
1304 #ifndef STANDARD_STARTFILE_PREFIX
1305 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1306 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1308 #ifdef MD_STARTFILE_PREFIX
1309 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1310 #endif
1311 #ifdef MD_STARTFILE_PREFIX_1
1312 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1313 #endif
1314 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1315 static char *standard_startfile_prefix_1 = "/lib/";
1316 static char *standard_startfile_prefix_2 = "/usr/lib/";
1318 #ifndef TOOLDIR_BASE_PREFIX
1319 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1320 #endif
1321 static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1322 static char *tooldir_prefix;
1324 /* Subdirectory to use for locating libraries. Set by
1325 set_multilib_dir based on the compilation options. */
1327 static char *multilib_dir;
1329 /* Clear out the vector of arguments (after a command is executed). */
1331 static void
1332 clear_args ()
1334 argbuf_index = 0;
1337 /* Add one argument to the vector at the end.
1338 This is done when a space is seen or at the end of the line.
1339 If DELETE_ALWAYS is nonzero, the arg is a filename
1340 and the file should be deleted eventually.
1341 If DELETE_FAILURE is nonzero, the arg is a filename
1342 and the file should be deleted if this compilation fails. */
1344 static void
1345 store_arg (arg, delete_always, delete_failure)
1346 char *arg;
1347 int delete_always, delete_failure;
1349 if (argbuf_index + 1 == argbuf_length)
1350 argbuf
1351 = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1353 argbuf[argbuf_index++] = arg;
1354 argbuf[argbuf_index] = 0;
1356 if (delete_always || delete_failure)
1357 record_temp_file (arg, delete_always, delete_failure);
1360 /* Read compilation specs from a file named FILENAME,
1361 replacing the default ones.
1363 A suffix which starts with `*' is a definition for
1364 one of the machine-specific sub-specs. The "suffix" should be
1365 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1366 The corresponding spec is stored in asm_spec, etc.,
1367 rather than in the `compilers' vector.
1369 Anything invalid in the file is a fatal error. */
1371 static void
1372 read_specs (filename, main_p)
1373 char *filename;
1374 int main_p;
1376 int desc;
1377 int readlen;
1378 struct stat statbuf;
1379 char *buffer;
1380 register char *p;
1382 if (verbose_flag)
1383 fprintf (stderr, "Reading specs from %s\n", filename);
1385 /* Open and stat the file. */
1386 desc = open (filename, O_RDONLY, 0);
1387 if (desc < 0)
1388 pfatal_with_name (filename);
1389 if (stat (filename, &statbuf) < 0)
1390 pfatal_with_name (filename);
1392 /* Read contents of file into BUFFER. */
1393 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1394 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1395 if (readlen < 0)
1396 pfatal_with_name (filename);
1397 buffer[readlen] = 0;
1398 close (desc);
1400 /* Scan BUFFER for specs, putting them in the vector. */
1401 p = buffer;
1402 while (1)
1404 char *suffix;
1405 char *spec;
1406 char *in, *out, *p1, *p2, *p3;
1408 /* Advance P in BUFFER to the next nonblank nocomment line. */
1409 p = skip_whitespace (p);
1410 if (*p == 0)
1411 break;
1413 /* Is this a special command that starts with '%'? */
1414 /* Don't allow this for the main specs file, since it would
1415 encourage people to overwrite it. */
1416 if (*p == '%' && !main_p)
1418 p1 = p;
1419 while (*p && *p != '\n')
1420 p++;
1422 p++; /* Skip '\n' */
1424 if (!strncmp (p1, "%include", sizeof ("%include")-1)
1425 && (p1[sizeof "%include" - 1] == ' '
1426 || p1[sizeof "%include" - 1] == '\t'))
1428 char *new_filename;
1430 p1 += sizeof ("%include");
1431 while (*p1 == ' ' || *p1 == '\t')
1432 p1++;
1434 if (*p1++ != '<' || p[-2] != '>')
1435 fatal ("specs %%include syntax malformed after %d characters",
1436 p1 - buffer + 1);
1438 p[-2] = '\0';
1439 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1440 read_specs (new_filename ? new_filename : p1, FALSE);
1441 continue;
1443 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1444 && (p1[sizeof "%include_noerr" - 1] == ' '
1445 || p1[sizeof "%include_noerr" - 1] == '\t'))
1447 char *new_filename;
1449 p1 += sizeof "%include_noerr";
1450 while (*p1 == ' ' || *p1 == '\t') p1++;
1452 if (*p1++ != '<' || p[-2] != '>')
1453 fatal ("specs %%include syntax malformed after %d characters",
1454 p1 - buffer + 1);
1456 p[-2] = '\0';
1457 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1458 if (new_filename)
1459 read_specs (new_filename, FALSE);
1460 else if (verbose_flag)
1461 fprintf (stderr, "Could not find specs file %s\n", p1);
1462 continue;
1464 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1465 && (p1[sizeof "%rename" - 1] == ' '
1466 || p1[sizeof "%rename" - 1] == '\t'))
1468 int name_len;
1469 struct spec_list *sl;
1471 /* Get original name */
1472 p1 += sizeof "%rename";
1473 while (*p1 == ' ' || *p1 == '\t')
1474 p1++;
1476 if (! ISALPHA (*p1))
1477 fatal ("specs %%rename syntax malformed after %d characters",
1478 p1 - buffer);
1480 p2 = p1;
1481 while (*p2 && !ISSPACE (*p2))
1482 p2++;
1484 if (*p2 != ' ' && *p2 != '\t')
1485 fatal ("specs %%rename syntax malformed after %d characters",
1486 p2 - buffer);
1488 name_len = p2 - p1;
1489 *p2++ = '\0';
1490 while (*p2 == ' ' || *p2 == '\t')
1491 p2++;
1493 if (! ISALPHA (*p2))
1494 fatal ("specs %%rename syntax malformed after %d characters",
1495 p2 - buffer);
1497 /* Get new spec name */
1498 p3 = p2;
1499 while (*p3 && !ISSPACE (*p3))
1500 p3++;
1502 if (p3 != p-1)
1503 fatal ("specs %%rename syntax malformed after %d characters",
1504 p3 - buffer);
1505 *p3 = '\0';
1507 for (sl = specs; sl; sl = sl->next)
1508 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1509 break;
1511 if (!sl)
1512 fatal ("specs %s spec was not found to be renamed", p1);
1514 if (strcmp (p1, p2) == 0)
1515 continue;
1517 if (verbose_flag)
1519 fprintf (stderr, "rename spec %s to %s\n", p1, p2);
1520 #ifdef DEBUG_SPECS
1521 fprintf (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
1522 #endif
1525 set_spec (p2, *(sl->ptr_spec));
1526 if (sl->alloc_p)
1527 free (*(sl->ptr_spec));
1529 *(sl->ptr_spec) = "";
1530 sl->alloc_p = 0;
1531 continue;
1533 else
1534 fatal ("specs unknown %% command after %d characters",
1535 p1 - buffer);
1538 /* Find the colon that should end the suffix. */
1539 p1 = p;
1540 while (*p1 && *p1 != ':' && *p1 != '\n')
1541 p1++;
1543 /* The colon shouldn't be missing. */
1544 if (*p1 != ':')
1545 fatal ("specs file malformed after %d characters", p1 - buffer);
1547 /* Skip back over trailing whitespace. */
1548 p2 = p1;
1549 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1550 p2--;
1552 /* Copy the suffix to a string. */
1553 suffix = save_string (p, p2 - p);
1554 /* Find the next line. */
1555 p = skip_whitespace (p1 + 1);
1556 if (p[1] == 0)
1557 fatal ("specs file malformed after %d characters", p - buffer);
1559 p1 = p;
1560 /* Find next blank line. */
1561 while (*p1 && !(*p1 == '\n' && p1[1] == '\n'))
1562 p1++;
1564 /* Specs end at the blank line and do not include the newline. */
1565 spec = save_string (p, p1 - p);
1566 p = p1;
1568 /* Delete backslash-newline sequences from the spec. */
1569 in = spec;
1570 out = spec;
1571 while (*in != 0)
1573 if (in[0] == '\\' && in[1] == '\n')
1574 in += 2;
1575 else if (in[0] == '#')
1576 while (*in && *in != '\n')
1577 in++;
1579 else
1580 *out++ = *in++;
1582 *out = 0;
1584 if (suffix[0] == '*')
1586 if (! strcmp (suffix, "*link_command"))
1587 link_command_spec = spec;
1588 else
1589 set_spec (suffix + 1, spec);
1591 else
1593 /* Add this pair to the vector. */
1594 compilers
1595 = ((struct compiler *)
1596 xrealloc (compilers,
1597 (n_compilers + 2) * sizeof (struct compiler)));
1599 compilers[n_compilers].suffix = suffix;
1600 bzero ((char *) compilers[n_compilers].spec,
1601 sizeof compilers[n_compilers].spec);
1602 compilers[n_compilers].spec[0] = spec;
1603 n_compilers++;
1604 bzero ((char *) &compilers[n_compilers],
1605 sizeof compilers[n_compilers]);
1608 if (*suffix == 0)
1609 link_command_spec = spec;
1612 if (link_command_spec == 0)
1613 fatal ("spec file has no spec for linking");
1616 /* Record the names of temporary files we tell compilers to write,
1617 and delete them at the end of the run. */
1619 /* This is the common prefix we use to make temp file names.
1620 It is chosen once for each run of this program.
1621 It is substituted into a spec by %g.
1622 Thus, all temp file names contain this prefix.
1623 In practice, all temp file names start with this prefix.
1625 This prefix comes from the envvar TMPDIR if it is defined;
1626 otherwise, from the P_tmpdir macro if that is defined;
1627 otherwise, in /usr/tmp or /tmp;
1628 or finally the current directory if all else fails. */
1630 static char *temp_filename;
1632 /* Length of the prefix. */
1634 static int temp_filename_length;
1636 /* Define the list of temporary files to delete. */
1638 struct temp_file
1640 char *name;
1641 struct temp_file *next;
1644 /* Queue of files to delete on success or failure of compilation. */
1645 static struct temp_file *always_delete_queue;
1646 /* Queue of files to delete on failure of compilation. */
1647 static struct temp_file *failure_delete_queue;
1649 /* Record FILENAME as a file to be deleted automatically.
1650 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1651 otherwise delete it in any case.
1652 FAIL_DELETE nonzero means delete it if a compilation step fails;
1653 otherwise delete it in any case. */
1655 static void
1656 record_temp_file (filename, always_delete, fail_delete)
1657 char *filename;
1658 int always_delete;
1659 int fail_delete;
1661 register char *name;
1662 name = xmalloc (strlen (filename) + 1);
1663 strcpy (name, filename);
1665 if (always_delete)
1667 register struct temp_file *temp;
1668 for (temp = always_delete_queue; temp; temp = temp->next)
1669 if (! strcmp (name, temp->name))
1670 goto already1;
1672 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1673 temp->next = always_delete_queue;
1674 temp->name = name;
1675 always_delete_queue = temp;
1677 already1:;
1680 if (fail_delete)
1682 register struct temp_file *temp;
1683 for (temp = failure_delete_queue; temp; temp = temp->next)
1684 if (! strcmp (name, temp->name))
1685 goto already2;
1687 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1688 temp->next = failure_delete_queue;
1689 temp->name = name;
1690 failure_delete_queue = temp;
1692 already2:;
1696 /* Delete all the temporary files whose names we previously recorded. */
1698 static void
1699 delete_if_ordinary (name)
1700 char *name;
1702 struct stat st;
1703 #ifdef DEBUG
1704 int i, c;
1706 printf ("Delete %s? (y or n) ", name);
1707 fflush (stdout);
1708 i = getchar ();
1709 if (i != '\n')
1710 while ((c = getchar ()) != '\n' && c != EOF)
1713 if (i == 'y' || i == 'Y')
1714 #endif /* DEBUG */
1715 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1716 if (unlink (name) < 0)
1717 if (verbose_flag)
1718 perror_with_name (name);
1721 static void
1722 delete_temp_files ()
1724 register struct temp_file *temp;
1726 for (temp = always_delete_queue; temp; temp = temp->next)
1727 delete_if_ordinary (temp->name);
1728 always_delete_queue = 0;
1731 /* Delete all the files to be deleted on error. */
1733 static void
1734 delete_failure_queue ()
1736 register struct temp_file *temp;
1738 for (temp = failure_delete_queue; temp; temp = temp->next)
1739 delete_if_ordinary (temp->name);
1742 static void
1743 clear_failure_queue ()
1745 failure_delete_queue = 0;
1748 /* Routine to add variables to the environment. We do this to pass
1749 the pathname of the gcc driver, and the directories search to the
1750 collect2 program, which is being run as ld. This way, we can be
1751 sure of executing the right compiler when collect2 wants to build
1752 constructors and destructors. Since the environment variables we
1753 use come from an obstack, we don't have to worry about allocating
1754 space for them. */
1756 #ifndef HAVE_PUTENV
1758 void
1759 putenv (str)
1760 char *str;
1762 #ifndef VMS /* nor about VMS */
1764 extern char **environ;
1765 char **old_environ = environ;
1766 char **envp;
1767 int num_envs = 0;
1768 int name_len = 1;
1769 int str_len = strlen (str);
1770 char *p = str;
1771 int ch;
1773 while ((ch = *p++) != '\0' && ch != '=')
1774 name_len++;
1776 if (!ch)
1777 abort ();
1779 /* Search for replacing an existing environment variable, and
1780 count the number of total environment variables. */
1781 for (envp = old_environ; *envp; envp++)
1783 num_envs++;
1784 if (!strncmp (str, *envp, name_len))
1786 *envp = str;
1787 return;
1791 /* Add a new environment variable */
1792 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1793 *environ = str;
1794 bcopy ((char *) old_environ, (char *) (environ + 1),
1795 sizeof (char *) * (num_envs+1));
1797 #endif /* VMS */
1800 #endif /* HAVE_PUTENV */
1803 /* Build a list of search directories from PATHS.
1804 PREFIX is a string to prepend to the list.
1805 If CHECK_DIR_P is non-zero we ensure the directory exists.
1806 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
1807 It is also used by the --print-search-dirs flag. */
1809 static char *
1810 build_search_list (paths, prefix, check_dir_p)
1811 struct path_prefix *paths;
1812 char *prefix;
1813 int check_dir_p;
1815 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1816 int just_suffix_len
1817 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1818 int first_time = TRUE;
1819 struct prefix_list *pprefix;
1821 obstack_grow (&collect_obstack, prefix, strlen (prefix));
1823 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1825 int len = strlen (pprefix->prefix);
1827 if (machine_suffix
1828 && (! check_dir_p
1829 || is_directory (pprefix->prefix, machine_suffix, 0)))
1831 if (!first_time)
1832 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1834 first_time = FALSE;
1835 obstack_grow (&collect_obstack, pprefix->prefix, len);
1836 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1839 if (just_machine_suffix
1840 && pprefix->require_machine_suffix == 2
1841 && (! check_dir_p
1842 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
1844 if (! first_time)
1845 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1847 first_time = FALSE;
1848 obstack_grow (&collect_obstack, pprefix->prefix, len);
1849 obstack_grow (&collect_obstack, just_machine_suffix,
1850 just_suffix_len);
1853 if (! pprefix->require_machine_suffix)
1855 if (! first_time)
1856 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1858 first_time = FALSE;
1859 obstack_grow (&collect_obstack, pprefix->prefix, len);
1863 obstack_1grow (&collect_obstack, '\0');
1864 return obstack_finish (&collect_obstack);
1867 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
1868 for collect. */
1870 static void
1871 putenv_from_prefixes (paths, env_var)
1872 struct path_prefix *paths;
1873 char *env_var;
1875 putenv (build_search_list (paths, env_var, 1));
1878 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1879 access to check permissions.
1880 Return 0 if not found, otherwise return its name, allocated with malloc. */
1882 static char *
1883 find_a_file (pprefix, name, mode)
1884 struct path_prefix *pprefix;
1885 char *name;
1886 int mode;
1888 char *temp;
1889 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1890 struct prefix_list *pl;
1891 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1893 if (machine_suffix)
1894 len += strlen (machine_suffix);
1896 temp = xmalloc (len);
1898 /* Determine the filename to execute (special case for absolute paths). */
1900 if (*name == '/' || *name == DIR_SEPARATOR
1901 /* Check for disk name on MS-DOS-based systems. */
1902 || (DIR_SEPARATOR == '\\' && name[1] == ':'
1903 && (name[2] == DIR_SEPARATOR || name[2] == '/')))
1905 if (access (name, mode))
1907 strcpy (temp, name);
1908 return temp;
1911 else
1912 for (pl = pprefix->plist; pl; pl = pl->next)
1914 if (machine_suffix)
1916 /* Some systems have a suffix for executable files.
1917 So try appending that first. */
1918 if (file_suffix[0] != 0)
1920 strcpy (temp, pl->prefix);
1921 strcat (temp, machine_suffix);
1922 strcat (temp, name);
1923 strcat (temp, file_suffix);
1924 if (access (temp, mode) == 0)
1926 if (pl->used_flag_ptr != 0)
1927 *pl->used_flag_ptr = 1;
1928 return temp;
1932 /* Now try just the name. */
1933 strcpy (temp, pl->prefix);
1934 strcat (temp, machine_suffix);
1935 strcat (temp, name);
1936 if (access (temp, mode) == 0)
1938 if (pl->used_flag_ptr != 0)
1939 *pl->used_flag_ptr = 1;
1940 return temp;
1944 /* Certain prefixes are tried with just the machine type,
1945 not the version. This is used for finding as, ld, etc. */
1946 if (just_machine_suffix && pl->require_machine_suffix == 2)
1948 /* Some systems have a suffix for executable files.
1949 So try appending that first. */
1950 if (file_suffix[0] != 0)
1952 strcpy (temp, pl->prefix);
1953 strcat (temp, just_machine_suffix);
1954 strcat (temp, name);
1955 strcat (temp, file_suffix);
1956 if (access (temp, mode) == 0)
1958 if (pl->used_flag_ptr != 0)
1959 *pl->used_flag_ptr = 1;
1960 return temp;
1964 strcpy (temp, pl->prefix);
1965 strcat (temp, just_machine_suffix);
1966 strcat (temp, name);
1967 if (access (temp, mode) == 0)
1969 if (pl->used_flag_ptr != 0)
1970 *pl->used_flag_ptr = 1;
1971 return temp;
1975 /* Certain prefixes can't be used without the machine suffix
1976 when the machine or version is explicitly specified. */
1977 if (! pl->require_machine_suffix)
1979 /* Some systems have a suffix for executable files.
1980 So try appending that first. */
1981 if (file_suffix[0] != 0)
1983 strcpy (temp, pl->prefix);
1984 strcat (temp, name);
1985 strcat (temp, file_suffix);
1986 if (access (temp, mode) == 0)
1988 if (pl->used_flag_ptr != 0)
1989 *pl->used_flag_ptr = 1;
1990 return temp;
1994 strcpy (temp, pl->prefix);
1995 strcat (temp, name);
1996 if (access (temp, mode) == 0)
1998 if (pl->used_flag_ptr != 0)
1999 *pl->used_flag_ptr = 1;
2000 return temp;
2005 free (temp);
2006 return 0;
2009 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
2010 at the start of the list, otherwise it goes at the end.
2012 If WARN is nonzero, we will warn if no file is found
2013 through this prefix. WARN should point to an int
2014 which will be set to 1 if this entry is used.
2016 COMPONENT is the value to be passed to update_path.
2018 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2019 the complete value of machine_suffix.
2020 2 means try both machine_suffix and just_machine_suffix. */
2022 static void
2023 add_prefix (pprefix, prefix, component, first, require_machine_suffix, warn)
2024 struct path_prefix *pprefix;
2025 char *prefix;
2026 char *component;
2027 int first;
2028 int require_machine_suffix;
2029 int *warn;
2031 struct prefix_list *pl, **prev;
2032 int len;
2034 if (! first && pprefix->plist)
2036 for (pl = pprefix->plist; pl->next; pl = pl->next)
2038 prev = &pl->next;
2040 else
2041 prev = &pprefix->plist;
2043 /* Keep track of the longest prefix */
2045 prefix = update_path (prefix, component);
2046 len = strlen (prefix);
2047 if (len > pprefix->max_len)
2048 pprefix->max_len = len;
2050 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2051 pl->prefix = save_string (prefix, len);
2052 pl->require_machine_suffix = require_machine_suffix;
2053 pl->used_flag_ptr = warn;
2054 if (warn)
2055 *warn = 0;
2057 if (*prev)
2058 pl->next = *prev;
2059 else
2060 pl->next = (struct prefix_list *) 0;
2061 *prev = pl;
2064 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
2066 static void
2067 unused_prefix_warnings (pprefix)
2068 struct path_prefix *pprefix;
2070 struct prefix_list *pl = pprefix->plist;
2072 while (pl)
2074 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
2076 if (pl->require_machine_suffix && machine_suffix)
2077 error ("file path prefix `%s%s' never used", pl->prefix,
2078 machine_suffix);
2079 else
2080 error ("file path prefix `%s' never used", pl->prefix);
2082 /* Prevent duplicate warnings. */
2083 *pl->used_flag_ptr = 1;
2086 pl = pl->next;
2091 /* Execute the command specified by the arguments on the current line of spec.
2092 When using pipes, this includes several piped-together commands
2093 with `|' between them.
2095 Return 0 if successful, -1 if failed. */
2097 static int
2098 execute ()
2100 int i;
2101 int n_commands; /* # of command. */
2102 char *string;
2103 struct command
2105 char *prog; /* program name. */
2106 char **argv; /* vector of args. */
2107 int pid; /* pid of process for this command. */
2110 struct command *commands; /* each command buffer with above info. */
2112 /* Count # of piped commands. */
2113 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2114 if (strcmp (argbuf[i], "|") == 0)
2115 n_commands++;
2117 /* Get storage for each command. */
2118 commands
2119 = (struct command *) alloca (n_commands * sizeof (struct command));
2121 /* Split argbuf into its separate piped processes,
2122 and record info about each one.
2123 Also search for the programs that are to be run. */
2125 commands[0].prog = argbuf[0]; /* first command. */
2126 commands[0].argv = &argbuf[0];
2127 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2128 if (string)
2129 commands[0].argv[0] = string;
2131 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2132 if (strcmp (argbuf[i], "|") == 0)
2133 { /* each command. */
2134 #if defined (__MSDOS__) || (defined (_WIN32) && defined (__CYGWIN32_)) || defined (OS2) || defined (VMS)
2135 fatal ("-pipe not supported");
2136 #endif
2137 argbuf[i] = 0; /* termination of command args. */
2138 commands[n_commands].prog = argbuf[i + 1];
2139 commands[n_commands].argv = &argbuf[i + 1];
2140 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2141 if (string)
2142 commands[n_commands].argv[0] = string;
2143 n_commands++;
2146 argbuf[argbuf_index] = 0;
2148 /* If -v, print what we are about to do, and maybe query. */
2150 if (verbose_flag)
2152 /* Print each piped command as a separate line. */
2153 for (i = 0; i < n_commands ; i++)
2155 char **j;
2157 for (j = commands[i].argv; *j; j++)
2158 fprintf (stderr, " %s", *j);
2160 /* Print a pipe symbol after all but the last command. */
2161 if (i + 1 != n_commands)
2162 fprintf (stderr, " |");
2163 fprintf (stderr, "\n");
2165 fflush (stderr);
2166 #ifdef DEBUG
2167 fprintf (stderr, "\nGo ahead? (y or n) ");
2168 fflush (stderr);
2169 i = getchar ();
2170 if (i != '\n')
2171 while (getchar () != '\n')
2174 if (i != 'y' && i != 'Y')
2175 return 0;
2176 #endif /* DEBUG */
2179 /* Run each piped subprocess. */
2181 for (i = 0; i < n_commands; i++)
2183 char *errmsg_fmt, *errmsg_arg;
2184 char *string = commands[i].argv[0];
2186 commands[i].pid = pexecute (string, commands[i].argv,
2187 programname, temp_filename,
2188 &errmsg_fmt, &errmsg_arg,
2189 ((i == 0 ? PEXECUTE_FIRST : 0)
2190 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2191 | (string == commands[i].prog
2192 ? PEXECUTE_SEARCH : 0)
2193 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2195 if (commands[i].pid == -1)
2196 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2198 if (string != commands[i].prog)
2199 free (string);
2202 execution_count++;
2204 /* Wait for all the subprocesses to finish.
2205 We don't care what order they finish in;
2206 we know that N_COMMANDS waits will get them all.
2207 Ignore subprocesses that we don't know about,
2208 since they can be spawned by the process that exec'ed us. */
2211 int ret_code = 0;
2213 for (i = 0; i < n_commands; )
2215 int j;
2216 int status;
2217 int pid;
2219 pid = pwait (commands[i].pid, &status, 0);
2220 if (pid < 0)
2221 abort ();
2223 for (j = 0; j < n_commands; j++)
2224 if (commands[j].pid == pid)
2226 i++;
2227 if (status != 0)
2229 if (WIFSIGNALED (status))
2231 fatal ("Internal compiler error: program %s got fatal signal %d",
2232 commands[j].prog, WTERMSIG (status));
2233 signal_count++;
2234 ret_code = -1;
2236 else if (WIFEXITED (status)
2237 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2238 ret_code = -1;
2240 break;
2243 return ret_code;
2247 /* Find all the switches given to us
2248 and make a vector describing them.
2249 The elements of the vector are strings, one per switch given.
2250 If a switch uses following arguments, then the `part1' field
2251 is the switch itself and the `args' field
2252 is a null-terminated vector containing the following arguments.
2253 The `live_cond' field is 1 if the switch is true in a conditional spec,
2254 -1 if false (overridden by a later switch), and is initialized to zero.
2255 The `valid' field is nonzero if any spec has looked at this switch;
2256 if it remains zero at the end of the run, it must be meaningless. */
2258 struct switchstr
2260 char *part1;
2261 char **args;
2262 int live_cond;
2263 int valid;
2266 static struct switchstr *switches;
2268 static int n_switches;
2270 struct infile
2272 char *name;
2273 char *language;
2276 /* Also a vector of input files specified. */
2278 static struct infile *infiles;
2280 static int n_infiles;
2282 /* And a vector of corresponding output files is made up later. */
2284 static char **outfiles;
2286 /* Used to track if none of the -B paths are used. */
2287 static int warn_B;
2289 /* Used to track if standard path isn't used and -b or -V is specified. */
2290 static int warn_std;
2292 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2293 static int *warn_std_ptr = 0;
2296 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2298 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2299 is true if we should look for an executable suffix as well. */
2301 static char *
2302 convert_filename (name, do_exe)
2303 char *name;
2304 int do_exe;
2306 int i;
2307 int len = strlen (name);
2309 #ifdef HAVE_OBJECT_SUFFIX
2310 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2311 if (len > 2
2312 && name[len - 2] == '.'
2313 && name[len - 1] == 'o')
2315 obstack_grow (&obstack, name, len - 2);
2316 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2317 name = obstack_finish (&obstack);
2319 #endif
2321 #ifdef HAVE_EXECUTABLE_SUFFIX
2322 /* If there is no filetype, make it the executable suffix (which includes
2323 the "."). But don't get confused if we have just "-o". */
2324 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2325 return name;
2327 for (i = len - 1; i >= 0; i--)
2328 if (name[i] == '/' || name[i] == DIR_SEPARATOR)
2329 break;
2331 for (i++; i < len; i++)
2332 if (name[i] == '.')
2333 return name;
2335 obstack_grow (&obstack, name, len);
2336 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2337 name = obstack_finish (&obstack);
2338 #endif
2340 return name;
2342 #endif
2344 /* Create the vector `switches' and its contents.
2345 Store its length in `n_switches'. */
2347 static void
2348 process_command (argc, argv)
2349 int argc;
2350 char **argv;
2352 register int i;
2353 char *temp;
2354 char *spec_lang = 0;
2355 int last_language_n_infiles;
2356 int have_c = 0;
2357 int have_o = 0;
2358 int lang_n_infiles = 0;
2360 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
2362 n_switches = 0;
2363 n_infiles = 0;
2365 /* Figure compiler version from version string. */
2367 compiler_version = save_string (version_string, strlen (version_string));
2368 for (temp = compiler_version; *temp; ++temp)
2370 if (*temp == ' ')
2372 *temp = '\0';
2373 break;
2377 /* Set up the default search paths. */
2379 if (gcc_exec_prefix)
2381 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2382 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2385 /* COMPILER_PATH and LIBRARY_PATH have values
2386 that are lists of directory names with colons. */
2388 temp = getenv ("COMPILER_PATH");
2389 if (temp)
2391 char *startp, *endp;
2392 char *nstore = (char *) alloca (strlen (temp) + 3);
2394 startp = endp = temp;
2395 while (1)
2397 if (*endp == PATH_SEPARATOR || *endp == 0)
2399 strncpy (nstore, startp, endp-startp);
2400 if (endp == startp)
2401 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2402 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2404 nstore[endp-startp] = DIR_SEPARATOR;
2405 nstore[endp-startp+1] = 0;
2407 else
2408 nstore[endp-startp] = 0;
2409 add_prefix (&exec_prefixes, nstore, 0, 0, 0, NULL_PTR);
2410 add_prefix (&include_prefixes,
2411 concat (nstore, "include", NULL_PTR),
2412 0, 0, 0, NULL_PTR);
2413 if (*endp == 0)
2414 break;
2415 endp = startp = endp + 1;
2417 else
2418 endp++;
2422 temp = getenv ("LIBRARY_PATH");
2423 if (temp && *cross_compile == '0')
2425 char *startp, *endp;
2426 char *nstore = (char *) alloca (strlen (temp) + 3);
2428 startp = endp = temp;
2429 while (1)
2431 if (*endp == PATH_SEPARATOR || *endp == 0)
2433 strncpy (nstore, startp, endp-startp);
2434 if (endp == startp)
2435 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2436 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2438 nstore[endp-startp] = DIR_SEPARATOR;
2439 nstore[endp-startp+1] = 0;
2441 else
2442 nstore[endp-startp] = 0;
2443 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2444 0, 0, NULL_PTR);
2445 if (*endp == 0)
2446 break;
2447 endp = startp = endp + 1;
2449 else
2450 endp++;
2454 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2455 temp = getenv ("LPATH");
2456 if (temp && *cross_compile == '0')
2458 char *startp, *endp;
2459 char *nstore = (char *) alloca (strlen (temp) + 3);
2461 startp = endp = temp;
2462 while (1)
2464 if (*endp == PATH_SEPARATOR || *endp == 0)
2466 strncpy (nstore, startp, endp-startp);
2467 if (endp == startp)
2468 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2469 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2471 nstore[endp-startp] = DIR_SEPARATOR;
2472 nstore[endp-startp+1] = 0;
2474 else
2475 nstore[endp-startp] = 0;
2476 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2477 0, 0, NULL_PTR);
2478 if (*endp == 0)
2479 break;
2480 endp = startp = endp + 1;
2482 else
2483 endp++;
2487 /* Convert new-style -- options to old-style. */
2488 translate_options (&argc, &argv);
2490 #ifdef LANG_SPECIFIC_DRIVER
2491 /* Do language-specific adjustment/addition of flags. */
2492 lang_specific_driver (fatal, &argc, &argv);
2493 #endif
2495 /* Scan argv twice. Here, the first time, just count how many switches
2496 there will be in their vector, and how many input files in theirs.
2497 Here we also parse the switches that cc itself uses (e.g. -v). */
2499 for (i = 1; i < argc; i++)
2501 if (! strcmp (argv[i], "-dumpspecs"))
2503 struct spec_list *sl;
2504 init_spec ();
2505 for (sl = specs; sl; sl = sl->next)
2506 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
2507 exit (0);
2509 else if (! strcmp (argv[i], "-dumpversion"))
2511 printf ("%s\n", version_string);
2512 exit (0);
2514 else if (! strcmp (argv[i], "-dumpmachine"))
2516 printf ("%s\n", spec_machine);
2517 exit (0);
2519 else if (! strcmp (argv[i], "-print-search-dirs"))
2520 print_search_dirs = 1;
2521 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2522 print_file_name = "libgcc.a";
2523 else if (! strncmp (argv[i], "-print-file-name=", 17))
2524 print_file_name = argv[i] + 17;
2525 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2526 print_prog_name = argv[i] + 17;
2527 else if (! strcmp (argv[i], "-print-multi-lib"))
2528 print_multi_lib = 1;
2529 else if (! strcmp (argv[i], "-print-multi-directory"))
2530 print_multi_directory = 1;
2531 else if (! strncmp (argv[i], "-Wa,", 4))
2533 int prev, j;
2534 /* Pass the rest of this option to the assembler. */
2536 n_assembler_options++;
2537 if (!assembler_options)
2538 assembler_options
2539 = (char **) xmalloc (n_assembler_options * sizeof (char **));
2540 else
2541 assembler_options
2542 = (char **) xrealloc (assembler_options,
2543 n_assembler_options * sizeof (char **));
2545 /* Split the argument at commas. */
2546 prev = 4;
2547 for (j = 4; argv[i][j]; j++)
2548 if (argv[i][j] == ',')
2550 assembler_options[n_assembler_options - 1]
2551 = save_string (argv[i] + prev, j - prev);
2552 n_assembler_options++;
2553 assembler_options
2554 = (char **) xrealloc (assembler_options,
2555 n_assembler_options * sizeof (char **));
2556 prev = j + 1;
2558 /* Record the part after the last comma. */
2559 assembler_options[n_assembler_options - 1] = argv[i] + prev;
2561 else if (! strncmp (argv[i], "-Wp,", 4))
2563 int prev, j;
2564 /* Pass the rest of this option to the preprocessor. */
2566 n_preprocessor_options++;
2567 if (!preprocessor_options)
2568 preprocessor_options
2569 = (char **) xmalloc (n_preprocessor_options * sizeof (char **));
2570 else
2571 preprocessor_options
2572 = (char **) xrealloc (preprocessor_options,
2573 n_preprocessor_options * sizeof (char **));
2575 /* Split the argument at commas. */
2576 prev = 4;
2577 for (j = 4; argv[i][j]; j++)
2578 if (argv[i][j] == ',')
2580 preprocessor_options[n_preprocessor_options - 1]
2581 = save_string (argv[i] + prev, j - prev);
2582 n_preprocessor_options++;
2583 preprocessor_options
2584 = (char **) xrealloc (preprocessor_options,
2585 n_preprocessor_options * sizeof (char **));
2586 prev = j + 1;
2588 /* Record the part after the last comma. */
2589 preprocessor_options[n_preprocessor_options - 1] = argv[i] + prev;
2591 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2592 /* The +e options to the C++ front-end. */
2593 n_switches++;
2594 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2596 int j;
2597 /* Split the argument at commas. */
2598 for (j = 3; argv[i][j]; j++)
2599 n_infiles += (argv[i][j] == ',');
2601 else if (strcmp (argv[i], "-Xlinker") == 0)
2603 if (i + 1 == argc)
2604 fatal ("argument to `-Xlinker' is missing");
2606 n_infiles++;
2607 i++;
2609 else if (strncmp (argv[i], "-l", 2) == 0)
2610 n_infiles++;
2611 else if (strcmp (argv[i], "-save-temps") == 0)
2613 save_temps_flag = 1;
2614 n_switches++;
2616 else if (strcmp (argv[i], "-specs") == 0)
2618 struct user_specs *user = (struct user_specs *)
2619 xmalloc (sizeof (struct user_specs));
2620 if (++i >= argc)
2621 fatal ("argument to `-specs' is missing");
2623 user->next = (struct user_specs *)0;
2624 user->filename = argv[i];
2625 if (user_specs_tail)
2626 user_specs_tail->next = user;
2627 else
2628 user_specs_head = user;
2629 user_specs_tail = user;
2631 else if (strncmp (argv[i], "-specs=", 7) == 0)
2633 struct user_specs *user = (struct user_specs *)
2634 xmalloc (sizeof (struct user_specs));
2635 if (strlen (argv[i]) == 7)
2636 fatal ("argument to `-specs=' is missing");
2638 user->next = (struct user_specs *)0;
2639 user->filename = argv[i]+7;
2640 if (user_specs_tail)
2641 user_specs_tail->next = user;
2642 else
2643 user_specs_head = user;
2644 user_specs_tail = user;
2646 else if (argv[i][0] == '-' && argv[i][1] != 0)
2648 register char *p = &argv[i][1];
2649 register int c = *p;
2651 switch (c)
2653 case 'b':
2654 n_switches++;
2655 if (p[1] == 0 && i + 1 == argc)
2656 fatal ("argument to `-b' is missing");
2657 if (p[1] == 0)
2658 spec_machine = argv[++i];
2659 else
2660 spec_machine = p + 1;
2662 warn_std_ptr = &warn_std;
2663 break;
2665 case 'B':
2667 char *value;
2668 if (p[1] == 0 && i + 1 == argc)
2669 fatal ("argument to `-B' is missing");
2670 if (p[1] == 0)
2671 value = argv[++i];
2672 else
2673 value = p + 1;
2674 add_prefix (&exec_prefixes, value, NULL_PTR, 1, 0, &warn_B);
2675 add_prefix (&startfile_prefixes, value, NULL_PTR,
2676 1, 0, &warn_B);
2677 add_prefix (&include_prefixes, concat (value, "include",
2678 NULL_PTR),
2679 NULL_PTR, 1, 0, NULL_PTR);
2681 /* As a kludge, if the arg is "[foo/]stageN/", just add
2682 "[foo/]include" to the include prefix. */
2684 int len = strlen (value);
2685 if ((len == 7
2686 || (len > 7
2687 && (value[len - 8] == '/'
2688 || value[len - 8] == DIR_SEPARATOR)))
2689 && strncmp (value + len - 7, "stage", 5) == 0
2690 && ISDIGIT (value[len - 2])
2691 && (value[len - 1] == '/'
2692 || value[len - 1] == DIR_SEPARATOR))
2694 if (len == 7)
2695 add_prefix (&include_prefixes, "include", NULL_PTR,
2696 1, 0, NULL_PTR);
2697 else
2699 char *string = xmalloc (len + 1);
2700 strncpy (string, value, len-7);
2701 strcpy (string+len-7, "include");
2702 add_prefix (&include_prefixes, string, NULL_PTR,
2703 1, 0, NULL_PTR);
2707 n_switches++;
2709 break;
2711 case 'v': /* Print our subcommands and print versions. */
2712 n_switches++;
2713 /* If they do anything other than exactly `-v', don't set
2714 verbose_flag; rather, continue on to give the error. */
2715 if (p[1] != 0)
2716 break;
2717 verbose_flag++;
2718 break;
2720 case 'V':
2721 n_switches++;
2722 if (p[1] == 0 && i + 1 == argc)
2723 fatal ("argument to `-V' is missing");
2724 if (p[1] == 0)
2725 spec_version = argv[++i];
2726 else
2727 spec_version = p + 1;
2728 compiler_version = spec_version;
2729 warn_std_ptr = &warn_std;
2730 break;
2732 case 'c':
2733 if (p[1] == 0)
2735 have_c = 1;
2736 n_switches++;
2737 break;
2739 goto normal_switch;
2741 case 'o':
2742 have_o = 1;
2743 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
2744 argv[i] = convert_filename (argv[i], 1);
2745 if (p[1] == 0)
2746 argv[i+1] = convert_filename (argv[i+1], 1);
2747 #endif
2748 goto normal_switch;
2750 default:
2751 normal_switch:
2752 n_switches++;
2754 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
2755 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
2756 else if (WORD_SWITCH_TAKES_ARG (p))
2757 i += WORD_SWITCH_TAKES_ARG (p);
2760 else
2762 n_infiles++;
2763 lang_n_infiles++;
2767 if (have_c && have_o && lang_n_infiles > 1)
2768 fatal ("cannot specify -o with -c and multiple compilations");
2770 /* Set up the search paths before we go looking for config files. */
2772 /* These come before the md prefixes so that we will find gcc's subcommands
2773 (such as cpp) rather than those of the host system. */
2774 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2775 as well as trying the machine and the version. */
2776 #ifndef OS2
2777 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
2778 0, 2, warn_std_ptr);
2779 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
2780 0, 2, warn_std_ptr);
2781 #endif
2783 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
2784 0, 1, warn_std_ptr);
2785 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
2786 0, 1, warn_std_ptr);
2788 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
2789 dir_separator_str, NULL_PTR);
2791 /* If tooldir is relative, base it on exec_prefixes. A relative
2792 tooldir lets us move the installed tree as a unit.
2794 If GCC_EXEC_PREFIX is defined, then we want to add two relative
2795 directories, so that we can search both the user specified directory
2796 and the standard place. */
2798 if (*tooldir_prefix != '/' && *tooldir_prefix != DIR_SEPARATOR)
2800 if (gcc_exec_prefix)
2802 char *gcc_exec_tooldir_prefix
2803 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
2804 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
2806 add_prefix (&exec_prefixes,
2807 concat (gcc_exec_tooldir_prefix, "bin",
2808 dir_separator_str, NULL_PTR),
2809 NULL_PTR, 0, 0, NULL_PTR);
2810 add_prefix (&startfile_prefixes,
2811 concat (gcc_exec_tooldir_prefix, "lib",
2812 dir_separator_str, NULL_PTR),
2813 NULL_PTR, 0, 0, NULL_PTR);
2816 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
2817 dir_separator_str, spec_version,
2818 dir_separator_str, tooldir_prefix, NULL_PTR);
2821 add_prefix (&exec_prefixes,
2822 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
2823 "BINUTILS", 0, 0, NULL_PTR);
2824 add_prefix (&startfile_prefixes,
2825 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
2826 "BINUTILS", 0, 0, NULL_PTR);
2828 /* More prefixes are enabled in main, after we read the specs file
2829 and determine whether this is cross-compilation or not. */
2832 /* Then create the space for the vectors and scan again. */
2834 switches = ((struct switchstr *)
2835 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2836 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2837 n_switches = 0;
2838 n_infiles = 0;
2839 last_language_n_infiles = -1;
2841 /* This, time, copy the text of each switch and store a pointer
2842 to the copy in the vector of switches.
2843 Store all the infiles in their vector. */
2845 for (i = 1; i < argc; i++)
2847 /* Just skip the switches that were handled by the preceding loop. */
2848 if (! strncmp (argv[i], "-Wa,", 4))
2850 else if (! strncmp (argv[i], "-Wp,", 4))
2852 else if (! strcmp (argv[i], "-print-search-dirs"))
2854 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2856 else if (! strncmp (argv[i], "-print-file-name=", 17))
2858 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2860 else if (! strcmp (argv[i], "-print-multi-lib"))
2862 else if (! strcmp (argv[i], "-print-multi-directory"))
2864 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2866 /* Compensate for the +e options to the C++ front-end;
2867 they're there simply for cfront call-compatibility. We do
2868 some magic in default_compilers to pass them down properly.
2869 Note we deliberately start at the `+' here, to avoid passing
2870 -e0 or -e1 down into the linker. */
2871 switches[n_switches].part1 = &argv[i][0];
2872 switches[n_switches].args = 0;
2873 switches[n_switches].live_cond = 0;
2874 switches[n_switches].valid = 0;
2875 n_switches++;
2877 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2879 int prev, j;
2880 /* Split the argument at commas. */
2881 prev = 4;
2882 for (j = 4; argv[i][j]; j++)
2883 if (argv[i][j] == ',')
2885 infiles[n_infiles].language = 0;
2886 infiles[n_infiles++].name
2887 = save_string (argv[i] + prev, j - prev);
2888 prev = j + 1;
2890 /* Record the part after the last comma. */
2891 infiles[n_infiles].language = 0;
2892 infiles[n_infiles++].name = argv[i] + prev;
2894 else if (strcmp (argv[i], "-Xlinker") == 0)
2896 infiles[n_infiles].language = 0;
2897 infiles[n_infiles++].name = argv[++i];
2899 else if (strncmp (argv[i], "-l", 2) == 0)
2901 infiles[n_infiles].language = 0;
2902 infiles[n_infiles++].name = argv[i];
2904 else if (strcmp (argv[i], "-specs") == 0)
2905 i++;
2906 else if (strncmp (argv[i], "-specs=", 7) == 0)
2908 /* -save-temps overrides -pipe, so that temp files are produced */
2909 else if (save_temps_flag && strcmp (argv[i], "-pipe") == 0)
2910 error ("Warning: -pipe ignored since -save-temps specified");
2911 else if (argv[i][0] == '-' && argv[i][1] != 0)
2913 register char *p = &argv[i][1];
2914 register int c = *p;
2916 if (c == 'x')
2918 if (p[1] == 0 && i + 1 == argc)
2919 fatal ("argument to `-x' is missing");
2920 if (p[1] == 0)
2921 spec_lang = argv[++i];
2922 else
2923 spec_lang = p + 1;
2924 if (! strcmp (spec_lang, "none"))
2925 /* Suppress the warning if -xnone comes after the last input
2926 file, because alternate command interfaces like g++ might
2927 find it useful to place -xnone after each input file. */
2928 spec_lang = 0;
2929 else
2930 last_language_n_infiles = n_infiles;
2931 continue;
2933 switches[n_switches].part1 = p;
2934 /* Deal with option arguments in separate argv elements. */
2935 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2936 || WORD_SWITCH_TAKES_ARG (p))
2938 int j = 0;
2939 int n_args = WORD_SWITCH_TAKES_ARG (p);
2941 if (n_args == 0)
2943 /* Count only the option arguments in separate argv elements. */
2944 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2946 if (i + n_args >= argc)
2947 fatal ("argument to `-%s' is missing", p);
2948 switches[n_switches].args
2949 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2950 while (j < n_args)
2951 switches[n_switches].args[j++] = argv[++i];
2952 /* Null-terminate the vector. */
2953 switches[n_switches].args[j] = 0;
2955 else if (index (switches_need_spaces, c))
2957 /* On some systems, ld cannot handle some options without
2958 a space. So split the option from its argument. */
2959 char *part1 = (char *) xmalloc (2);
2960 part1[0] = c;
2961 part1[1] = '\0';
2963 switches[n_switches].part1 = part1;
2964 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2965 switches[n_switches].args[0] = xmalloc (strlen (p));
2966 strcpy (switches[n_switches].args[0], &p[1]);
2967 switches[n_switches].args[1] = 0;
2969 else
2970 switches[n_switches].args = 0;
2972 switches[n_switches].live_cond = 0;
2973 switches[n_switches].valid = 0;
2974 /* This is always valid, since gcc.c itself understands it. */
2975 if (!strcmp (p, "save-temps"))
2976 switches[n_switches].valid = 1;
2977 else
2979 char ch = switches[n_switches].part1[0];
2980 if (ch == 'V' || ch == 'b' || ch == 'B')
2981 switches[n_switches].valid = 1;
2983 n_switches++;
2985 else
2987 #ifdef HAVE_OBJECT_SUFFIX
2988 argv[i] = convert_filename (argv[i], 0);
2989 #endif
2991 if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
2993 perror_with_name (argv[i]);
2994 error_count++;
2996 else
2998 infiles[n_infiles].language = spec_lang;
2999 infiles[n_infiles++].name = argv[i];
3004 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3005 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3007 switches[n_switches].part1 = 0;
3008 infiles[n_infiles].name = 0;
3011 /* Process a spec string, accumulating and running commands. */
3013 /* These variables describe the input file name.
3014 input_file_number is the index on outfiles of this file,
3015 so that the output file name can be stored for later use by %o.
3016 input_basename is the start of the part of the input file
3017 sans all directory names, and basename_length is the number
3018 of characters starting there excluding the suffix .c or whatever. */
3020 static char *input_filename;
3021 static int input_file_number;
3022 static size_t input_filename_length;
3023 static int basename_length;
3024 static char *input_basename;
3025 static char *input_suffix;
3027 /* These are variables used within do_spec and do_spec_1. */
3029 /* Nonzero if an arg has been started and not yet terminated
3030 (with space, tab or newline). */
3031 static int arg_going;
3033 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3034 is a temporary file name. */
3035 static int delete_this_arg;
3037 /* Nonzero means %w has been seen; the next arg to be terminated
3038 is the output file name of this compilation. */
3039 static int this_is_output_file;
3041 /* Nonzero means %s has been seen; the next arg to be terminated
3042 is the name of a library file and we should try the standard
3043 search dirs for it. */
3044 static int this_is_library_file;
3046 /* Nonzero means that the input of this command is coming from a pipe. */
3047 static int input_from_pipe;
3049 /* Process the spec SPEC and run the commands specified therein.
3050 Returns 0 if the spec is successfully processed; -1 if failed. */
3052 static int
3053 do_spec (spec)
3054 char *spec;
3056 int value;
3058 clear_args ();
3059 arg_going = 0;
3060 delete_this_arg = 0;
3061 this_is_output_file = 0;
3062 this_is_library_file = 0;
3063 input_from_pipe = 0;
3065 value = do_spec_1 (spec, 0, NULL_PTR);
3067 /* Force out any unfinished command.
3068 If -pipe, this forces out the last command if it ended in `|'. */
3069 if (value == 0)
3071 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3072 argbuf_index--;
3074 if (argbuf_index > 0)
3075 value = execute ();
3078 return value;
3081 /* Process the sub-spec SPEC as a portion of a larger spec.
3082 This is like processing a whole spec except that we do
3083 not initialize at the beginning and we do not supply a
3084 newline by default at the end.
3085 INSWITCH nonzero means don't process %-sequences in SPEC;
3086 in this case, % is treated as an ordinary character.
3087 This is used while substituting switches.
3088 INSWITCH nonzero also causes SPC not to terminate an argument.
3090 Value is zero unless a line was finished
3091 and the command on that line reported an error. */
3093 static int
3094 do_spec_1 (spec, inswitch, soft_matched_part)
3095 char *spec;
3096 int inswitch;
3097 char *soft_matched_part;
3099 register char *p = spec;
3100 register int c;
3101 int i;
3102 char *string;
3103 int value;
3105 while ((c = *p++))
3106 /* If substituting a switch, treat all chars like letters.
3107 Otherwise, NL, SPC, TAB and % are special. */
3108 switch (inswitch ? 'a' : c)
3110 case '\n':
3111 /* End of line: finish any pending argument,
3112 then run the pending command if one has been started. */
3113 if (arg_going)
3115 obstack_1grow (&obstack, 0);
3116 string = obstack_finish (&obstack);
3117 if (this_is_library_file)
3118 string = find_file (string);
3119 store_arg (string, delete_this_arg, this_is_output_file);
3120 if (this_is_output_file)
3121 outfiles[input_file_number] = string;
3123 arg_going = 0;
3125 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3127 for (i = 0; i < n_switches; i++)
3128 if (!strcmp (switches[i].part1, "pipe"))
3129 break;
3131 /* A `|' before the newline means use a pipe here,
3132 but only if -pipe was specified.
3133 Otherwise, execute now and don't pass the `|' as an arg. */
3134 if (i < n_switches)
3136 input_from_pipe = 1;
3137 switches[i].valid = 1;
3138 break;
3140 else
3141 argbuf_index--;
3144 if (argbuf_index > 0)
3146 value = execute ();
3147 if (value)
3148 return value;
3150 /* Reinitialize for a new command, and for a new argument. */
3151 clear_args ();
3152 arg_going = 0;
3153 delete_this_arg = 0;
3154 this_is_output_file = 0;
3155 this_is_library_file = 0;
3156 input_from_pipe = 0;
3157 break;
3159 case '|':
3160 /* End any pending argument. */
3161 if (arg_going)
3163 obstack_1grow (&obstack, 0);
3164 string = obstack_finish (&obstack);
3165 if (this_is_library_file)
3166 string = find_file (string);
3167 store_arg (string, delete_this_arg, this_is_output_file);
3168 if (this_is_output_file)
3169 outfiles[input_file_number] = string;
3172 /* Use pipe */
3173 obstack_1grow (&obstack, c);
3174 arg_going = 1;
3175 break;
3177 case '\t':
3178 case ' ':
3179 /* Space or tab ends an argument if one is pending. */
3180 if (arg_going)
3182 obstack_1grow (&obstack, 0);
3183 string = obstack_finish (&obstack);
3184 if (this_is_library_file)
3185 string = find_file (string);
3186 store_arg (string, delete_this_arg, this_is_output_file);
3187 if (this_is_output_file)
3188 outfiles[input_file_number] = string;
3190 /* Reinitialize for a new argument. */
3191 arg_going = 0;
3192 delete_this_arg = 0;
3193 this_is_output_file = 0;
3194 this_is_library_file = 0;
3195 break;
3197 case '%':
3198 switch (c = *p++)
3200 case 0:
3201 fatal ("Invalid specification! Bug in cc.");
3203 case 'b':
3204 obstack_grow (&obstack, input_basename, basename_length);
3205 arg_going = 1;
3206 break;
3208 case 'd':
3209 delete_this_arg = 2;
3210 break;
3212 /* Dump out the directories specified with LIBRARY_PATH,
3213 followed by the absolute directories
3214 that we search for startfiles. */
3215 case 'D':
3217 struct prefix_list *pl = startfile_prefixes.plist;
3218 size_t bufsize = 100;
3219 char *buffer = (char *) xmalloc (bufsize);
3220 int idx;
3222 for (; pl; pl = pl->next)
3224 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
3225 /* Used on systems which record the specified -L dirs
3226 and use them to search for dynamic linking. */
3227 /* Relative directories always come from -B,
3228 and it is better not to use them for searching
3229 at run time. In particular, stage1 loses */
3230 if (pl->prefix[0] != '/' && pl->prefix[0] != DIR_SEPARATOR)
3231 continue;
3232 #endif
3233 /* Try subdirectory if there is one. */
3234 if (multilib_dir != NULL)
3236 if (machine_suffix)
3238 if (strlen (pl->prefix) + strlen (machine_suffix)
3239 >= bufsize)
3240 bufsize = (strlen (pl->prefix)
3241 + strlen (machine_suffix)) * 2 + 1;
3242 buffer = (char *) xrealloc (buffer, bufsize);
3243 strcpy (buffer, pl->prefix);
3244 strcat (buffer, machine_suffix);
3245 if (is_directory (buffer, multilib_dir, 1))
3247 do_spec_1 ("-L", 0, NULL_PTR);
3248 #ifdef SPACE_AFTER_L_OPTION
3249 do_spec_1 (" ", 0, NULL_PTR);
3250 #endif
3251 do_spec_1 (buffer, 1, NULL_PTR);
3252 do_spec_1 (multilib_dir, 1, NULL_PTR);
3253 /* Make this a separate argument. */
3254 do_spec_1 (" ", 0, NULL_PTR);
3257 if (!pl->require_machine_suffix)
3259 if (is_directory (pl->prefix, multilib_dir, 1))
3261 do_spec_1 ("-L", 0, NULL_PTR);
3262 #ifdef SPACE_AFTER_L_OPTION
3263 do_spec_1 (" ", 0, NULL_PTR);
3264 #endif
3265 do_spec_1 (pl->prefix, 1, NULL_PTR);
3266 do_spec_1 (multilib_dir, 1, NULL_PTR);
3267 /* Make this a separate argument. */
3268 do_spec_1 (" ", 0, NULL_PTR);
3272 if (machine_suffix)
3274 if (is_directory (pl->prefix, machine_suffix, 1))
3276 do_spec_1 ("-L", 0, NULL_PTR);
3277 #ifdef SPACE_AFTER_L_OPTION
3278 do_spec_1 (" ", 0, NULL_PTR);
3279 #endif
3280 do_spec_1 (pl->prefix, 1, NULL_PTR);
3281 /* Remove slash from machine_suffix. */
3282 if (strlen (machine_suffix) >= bufsize)
3283 bufsize = strlen (machine_suffix) * 2 + 1;
3284 buffer = (char *) xrealloc (buffer, bufsize);
3285 strcpy (buffer, machine_suffix);
3286 idx = strlen (buffer);
3287 if (buffer[idx - 1] == '/'
3288 || buffer[idx - 1] == DIR_SEPARATOR)
3289 buffer[idx - 1] = 0;
3290 do_spec_1 (buffer, 1, NULL_PTR);
3291 /* Make this a separate argument. */
3292 do_spec_1 (" ", 0, NULL_PTR);
3295 if (!pl->require_machine_suffix)
3297 if (is_directory (pl->prefix, "", 1))
3299 do_spec_1 ("-L", 0, NULL_PTR);
3300 #ifdef SPACE_AFTER_L_OPTION
3301 do_spec_1 (" ", 0, NULL_PTR);
3302 #endif
3303 /* Remove slash from pl->prefix. */
3304 if (strlen (pl->prefix) >= bufsize)
3305 bufsize = strlen (pl->prefix) * 2 + 1;
3306 buffer = (char *) xrealloc (buffer, bufsize);
3307 strcpy (buffer, pl->prefix);
3308 idx = strlen (buffer);
3309 if (buffer[idx - 1] == '/'
3310 || buffer[idx - 1] == DIR_SEPARATOR)
3311 buffer[idx - 1] = 0;
3312 do_spec_1 (buffer, 1, NULL_PTR);
3313 /* Make this a separate argument. */
3314 do_spec_1 (" ", 0, NULL_PTR);
3318 free (buffer);
3320 break;
3322 case 'e':
3323 /* {...:%efoo} means report an error with `foo' as error message
3324 and don't execute any more commands for this file. */
3326 char *q = p;
3327 char *buf;
3328 while (*p != 0 && *p != '\n') p++;
3329 buf = (char *) alloca (p - q + 1);
3330 strncpy (buf, q, p - q);
3331 buf[p - q] = 0;
3332 error ("%s", buf);
3333 return -1;
3335 break;
3337 case 'g':
3338 case 'u':
3339 case 'U':
3340 if (save_temps_flag)
3342 obstack_grow (&obstack, input_basename, basename_length);
3343 delete_this_arg = 0;
3345 else
3347 #ifdef MKTEMP_EACH_FILE
3348 /* ??? This has a problem: the total number of
3349 values mktemp can return is limited.
3350 That matters for the names of object files.
3351 In 2.4, do something about that. */
3352 struct temp_name *t;
3353 char *suffix = p;
3354 while (*p == '.' || ISALPHA (*p)
3355 || (p[0] == '%' && p[1] == 'O'))
3356 p++;
3358 /* See if we already have an association of %g/%u/%U and
3359 suffix. */
3360 for (t = temp_names; t; t = t->next)
3361 if (t->length == p - suffix
3362 && strncmp (t->suffix, suffix, p - suffix) == 0
3363 && t->unique == (c != 'g'))
3364 break;
3366 /* Make a new association if needed. %u requires one. */
3367 if (t == 0 || c == 'u')
3369 if (t == 0)
3371 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
3372 t->next = temp_names;
3373 temp_names = t;
3375 t->length = p - suffix;
3376 t->suffix = save_string (suffix, p - suffix);
3377 t->unique = (c != 'g');
3378 temp_filename = choose_temp_base ();
3379 temp_filename_length = strlen (temp_filename);
3380 t->filename = temp_filename;
3381 t->filename_length = temp_filename_length;
3384 obstack_grow (&obstack, t->filename, t->filename_length);
3385 delete_this_arg = 1;
3386 #else
3387 obstack_grow (&obstack, temp_filename, temp_filename_length);
3388 if (c == 'u' || c == 'U')
3390 static int unique;
3391 char buff[9];
3392 if (c == 'u')
3393 unique++;
3394 sprintf (buff, "%d", unique);
3395 obstack_grow (&obstack, buff, strlen (buff));
3397 #endif
3398 delete_this_arg = 1;
3400 arg_going = 1;
3401 break;
3403 case 'i':
3404 obstack_grow (&obstack, input_filename, input_filename_length);
3405 arg_going = 1;
3406 break;
3408 case 'I':
3410 struct prefix_list *pl = include_prefixes.plist;
3412 if (gcc_exec_prefix)
3414 do_spec_1 ("-iprefix", 1, NULL_PTR);
3415 /* Make this a separate argument. */
3416 do_spec_1 (" ", 0, NULL_PTR);
3417 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
3418 do_spec_1 (" ", 0, NULL_PTR);
3421 for (; pl; pl = pl->next)
3423 do_spec_1 ("-isystem", 1, NULL_PTR);
3424 /* Make this a separate argument. */
3425 do_spec_1 (" ", 0, NULL_PTR);
3426 do_spec_1 (pl->prefix, 1, NULL_PTR);
3427 do_spec_1 (" ", 0, NULL_PTR);
3430 break;
3432 case 'o':
3433 for (i = 0; i < n_infiles; i++)
3434 store_arg (outfiles[i], 0, 0);
3435 break;
3437 case 'O':
3438 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
3439 arg_going = 1;
3440 break;
3442 case 's':
3443 this_is_library_file = 1;
3444 break;
3446 case 'w':
3447 this_is_output_file = 1;
3448 break;
3450 case 'W':
3452 int cur_index = argbuf_index;
3453 /* Handle the {...} following the %W. */
3454 if (*p != '{')
3455 abort ();
3456 p = handle_braces (p + 1);
3457 if (p == 0)
3458 return -1;
3459 /* If any args were output, mark the last one for deletion
3460 on failure. */
3461 if (argbuf_index != cur_index)
3462 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3463 break;
3466 /* %x{OPTION} records OPTION for %X to output. */
3467 case 'x':
3469 char *p1 = p;
3470 char *string;
3472 /* Skip past the option value and make a copy. */
3473 if (*p != '{')
3474 abort ();
3475 while (*p++ != '}')
3477 string = save_string (p1 + 1, p - p1 - 2);
3479 /* See if we already recorded this option. */
3480 for (i = 0; i < n_linker_options; i++)
3481 if (! strcmp (string, linker_options[i]))
3483 free (string);
3484 return 0;
3487 /* This option is new; add it. */
3488 n_linker_options++;
3489 if (!linker_options)
3490 linker_options
3491 = (char **) xmalloc (n_linker_options * sizeof (char **));
3492 else
3493 linker_options
3494 = (char **) xrealloc (linker_options,
3495 n_linker_options * sizeof (char **));
3497 linker_options[n_linker_options - 1] = string;
3499 break;
3501 /* Dump out the options accumulated previously using %x. */
3502 case 'X':
3503 for (i = 0; i < n_linker_options; i++)
3505 do_spec_1 (linker_options[i], 1, NULL_PTR);
3506 /* Make each accumulated option a separate argument. */
3507 do_spec_1 (" ", 0, NULL_PTR);
3509 break;
3511 /* Dump out the options accumulated previously using -Wa,. */
3512 case 'Y':
3513 for (i = 0; i < n_assembler_options; i++)
3515 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3516 /* Make each accumulated option a separate argument. */
3517 do_spec_1 (" ", 0, NULL_PTR);
3519 break;
3521 /* Dump out the options accumulated previously using -Wp,. */
3522 case 'Z':
3523 for (i = 0; i < n_preprocessor_options; i++)
3525 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
3526 /* Make each accumulated option a separate argument. */
3527 do_spec_1 (" ", 0, NULL_PTR);
3529 break;
3531 /* Here are digits and numbers that just process
3532 a certain constant string as a spec. */
3534 case '1':
3535 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3536 if (value != 0)
3537 return value;
3538 break;
3540 case '2':
3541 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3542 if (value != 0)
3543 return value;
3544 break;
3546 case 'a':
3547 value = do_spec_1 (asm_spec, 0, NULL_PTR);
3548 if (value != 0)
3549 return value;
3550 break;
3552 case 'A':
3553 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3554 if (value != 0)
3555 return value;
3556 break;
3558 case 'c':
3559 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3560 if (value != 0)
3561 return value;
3562 break;
3564 case 'C':
3565 value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3566 if (value != 0)
3567 return value;
3568 break;
3570 case 'E':
3571 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
3572 if (value != 0)
3573 return value;
3574 break;
3576 case 'l':
3577 value = do_spec_1 (link_spec, 0, NULL_PTR);
3578 if (value != 0)
3579 return value;
3580 break;
3582 case 'L':
3583 value = do_spec_1 (lib_spec, 0, NULL_PTR);
3584 if (value != 0)
3585 return value;
3586 break;
3588 case 'G':
3589 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
3590 if (value != 0)
3591 return value;
3592 break;
3594 case 'p':
3596 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
3597 char *buf = x;
3598 char *y;
3600 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
3601 y = cpp_predefines;
3602 while (*y != 0)
3604 if (! strncmp (y, "-D", 2))
3605 /* Copy the whole option. */
3606 while (*y && *y != ' ' && *y != '\t')
3607 *x++ = *y++;
3608 else if (*y == ' ' || *y == '\t')
3609 /* Copy whitespace to the result. */
3610 *x++ = *y++;
3611 /* Don't copy other options. */
3612 else
3613 y++;
3616 *x = 0;
3618 value = do_spec_1 (buf, 0, NULL_PTR);
3619 if (value != 0)
3620 return value;
3622 break;
3624 case 'P':
3626 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
3627 char *buf = x;
3628 char *y;
3630 /* Copy all of CPP_PREDEFINES into BUF,
3631 but put __ after every -D and at the end of each arg. */
3632 y = cpp_predefines;
3633 while (*y != 0)
3635 if (! strncmp (y, "-D", 2))
3637 int flag = 0;
3639 *x++ = *y++;
3640 *x++ = *y++;
3642 if (*y != '_'
3643 || (*(y+1) != '_' && ! ISUPPER (*(y+1))))
3645 /* Stick __ at front of macro name. */
3646 *x++ = '_';
3647 *x++ = '_';
3648 /* Arrange to stick __ at the end as well. */
3649 flag = 1;
3652 /* Copy the macro name. */
3653 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3654 *x++ = *y++;
3656 if (flag)
3658 *x++ = '_';
3659 *x++ = '_';
3662 /* Copy the value given, if any. */
3663 while (*y && *y != ' ' && *y != '\t')
3664 *x++ = *y++;
3666 else if (*y == ' ' || *y == '\t')
3667 /* Copy whitespace to the result. */
3668 *x++ = *y++;
3669 /* Don't copy -A options */
3670 else
3671 y++;
3673 *x++ = ' ';
3675 /* Copy all of CPP_PREDEFINES into BUF,
3676 but put __ after every -D. */
3677 y = cpp_predefines;
3678 while (*y != 0)
3680 if (! strncmp (y, "-D", 2))
3682 y += 2;
3684 if (*y != '_'
3685 || (*(y+1) != '_' && ! ISUPPER (*(y+1))))
3687 /* Stick -D__ at front of macro name. */
3688 *x++ = '-';
3689 *x++ = 'D';
3690 *x++ = '_';
3691 *x++ = '_';
3693 /* Copy the macro name. */
3694 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3695 *x++ = *y++;
3697 /* Copy the value given, if any. */
3698 while (*y && *y != ' ' && *y != '\t')
3699 *x++ = *y++;
3701 else
3703 /* Do not copy this macro - we have just done it before */
3704 while (*y && *y != ' ' && *y != '\t')
3705 y++;
3708 else if (*y == ' ' || *y == '\t')
3709 /* Copy whitespace to the result. */
3710 *x++ = *y++;
3711 /* Don't copy -A options */
3712 else
3713 y++;
3715 *x++ = ' ';
3717 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
3718 y = cpp_predefines;
3719 while (*y != 0)
3721 if (! strncmp (y, "-A", 2))
3722 /* Copy the whole option. */
3723 while (*y && *y != ' ' && *y != '\t')
3724 *x++ = *y++;
3725 else if (*y == ' ' || *y == '\t')
3726 /* Copy whitespace to the result. */
3727 *x++ = *y++;
3728 /* Don't copy other options. */
3729 else
3730 y++;
3733 *x = 0;
3735 value = do_spec_1 (buf, 0, NULL_PTR);
3736 if (value != 0)
3737 return value;
3739 break;
3741 case 'S':
3742 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
3743 if (value != 0)
3744 return value;
3745 break;
3747 /* Here we define characters other than letters and digits. */
3749 case '{':
3750 p = handle_braces (p);
3751 if (p == 0)
3752 return -1;
3753 break;
3755 case '%':
3756 obstack_1grow (&obstack, '%');
3757 break;
3759 case '*':
3760 do_spec_1 (soft_matched_part, 1, NULL_PTR);
3761 do_spec_1 (" ", 0, NULL_PTR);
3762 break;
3764 /* Process a string found as the value of a spec given by name.
3765 This feature allows individual machine descriptions
3766 to add and use their own specs.
3767 %[...] modifies -D options the way %P does;
3768 %(...) uses the spec unmodified. */
3769 case '(':
3770 case '[':
3772 char *name = p;
3773 struct spec_list *sl;
3774 int len;
3776 /* The string after the S/P is the name of a spec that is to be
3777 processed. */
3778 while (*p && *p != ')' && *p != ']')
3779 p++;
3781 /* See if it's in the list */
3782 for (len = p - name, sl = specs; sl; sl = sl->next)
3783 if (sl->name_len == len && !strncmp (sl->name, name, len))
3785 name = *(sl->ptr_spec);
3786 #ifdef DEBUG_SPECS
3787 fprintf (stderr, "Processing spec %c%s%c, which is '%s'\n",
3788 c, sl->name, (c == '(') ? ')' : ']', name);
3789 #endif
3790 break;
3793 if (sl)
3795 if (c == '(')
3797 value = do_spec_1 (name, 0, NULL_PTR);
3798 if (value != 0)
3799 return value;
3801 else
3803 char *x = (char *) alloca (strlen (name) * 2 + 1);
3804 char *buf = x;
3805 char *y = name;
3807 /* Copy all of NAME into BUF, but put __ after
3808 every -D and at the end of each arg, */
3809 while (1)
3811 int flag;
3813 if (! strncmp (y, "-D", 2))
3815 *x++ = '-';
3816 *x++ = 'D';
3817 *x++ = '_';
3818 *x++ = '_';
3819 y += 2;
3820 flag = 1;
3821 continue;
3823 else if (flag && (*y == ' ' || *y == '\t' || *y == '='
3824 || *y == '}' || *y == 0))
3826 *x++ = '_';
3827 *x++ = '_';
3828 flag = 0;
3830 if (*y == 0)
3831 break;
3832 else
3833 *x++ = *y++;
3835 *x = 0;
3837 value = do_spec_1 (buf, 0, NULL_PTR);
3838 if (value != 0)
3839 return value;
3843 /* Discard the closing paren or bracket. */
3844 if (*p)
3845 p++;
3847 break;
3849 case 'v':
3851 int c1 = *p++; /* Select first or second version number. */
3852 char *v = compiler_version;
3853 char *q;
3855 /* The format of the version string is
3856 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3858 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3859 while (! ISDIGIT (*v))
3860 v++;
3861 if (v > compiler_version && v[-1] != '-')
3862 abort ();
3864 /* If desired, advance to second version number. */
3865 if (c1 == '2')
3867 /* Set V after the first period. */
3868 while (ISDIGIT (*v))
3869 v++;
3870 if (*v != '.')
3871 abort ();
3872 v++;
3875 /* Set Q at the next period or at the end. */
3876 q = v;
3877 while (ISDIGIT (*q))
3878 q++;
3879 if (*q != 0 && *q != ' ' && *q != '.' && *q != '-')
3880 abort ();
3882 /* Put that part into the command. */
3883 obstack_grow (&obstack, v, q - v);
3884 arg_going = 1;
3886 break;
3888 case '|':
3889 if (input_from_pipe)
3890 do_spec_1 ("-", 0, NULL_PTR);
3891 break;
3893 default:
3894 abort ();
3896 break;
3898 case '\\':
3899 /* Backslash: treat next character as ordinary. */
3900 c = *p++;
3902 /* fall through */
3903 default:
3904 /* Ordinary character: put it into the current argument. */
3905 obstack_1grow (&obstack, c);
3906 arg_going = 1;
3909 return 0; /* End of string */
3912 /* Return 0 if we call do_spec_1 and that returns -1. */
3914 static char *
3915 handle_braces (p)
3916 register char *p;
3918 register char *q;
3919 char *filter;
3920 int pipe_p = 0;
3921 int negate = 0;
3922 int suffix = 0;
3923 int include_blanks = 1;
3925 if (*p == '^')
3926 /* A '^' after the open-brace means to not give blanks before args. */
3927 include_blanks = 0, ++p;
3929 if (*p == '|')
3930 /* A `|' after the open-brace means,
3931 if the test fails, output a single minus sign rather than nothing.
3932 This is used in %{|!pipe:...}. */
3933 pipe_p = 1, ++p;
3935 if (*p == '!')
3936 /* A `!' after the open-brace negates the condition:
3937 succeed if the specified switch is not present. */
3938 negate = 1, ++p;
3940 if (*p == '.')
3941 /* A `.' after the open-brace means test against the current suffix. */
3943 if (pipe_p)
3944 abort ();
3946 suffix = 1;
3947 ++p;
3950 filter = p;
3951 while (*p != ':' && *p != '}') p++;
3952 if (*p != '}')
3954 register int count = 1;
3955 q = p + 1;
3956 while (count > 0)
3958 if (*q == '{')
3959 count++;
3960 else if (*q == '}')
3961 count--;
3962 else if (*q == 0)
3963 abort ();
3964 q++;
3967 else
3968 q = p + 1;
3970 if (suffix)
3972 int found = (input_suffix != 0
3973 && strlen (input_suffix) == p - filter
3974 && strncmp (input_suffix, filter, p - filter) == 0);
3976 if (p[0] == '}')
3977 abort ();
3979 if (negate != found
3980 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3981 return 0;
3983 return q;
3985 else if (p[-1] == '*' && p[0] == '}')
3987 /* Substitute all matching switches as separate args. */
3988 register int i;
3989 --p;
3990 for (i = 0; i < n_switches; i++)
3991 if (!strncmp (switches[i].part1, filter, p - filter)
3992 && check_live_switch (i, p - filter))
3993 give_switch (i, 0, include_blanks);
3995 else
3997 /* Test for presence of the specified switch. */
3998 register int i;
3999 int present = 0;
4001 /* If name specified ends in *, as in {x*:...},
4002 check for %* and handle that case. */
4003 if (p[-1] == '*' && !negate)
4005 int substitution;
4006 char *r = p;
4008 /* First see whether we have %*. */
4009 substitution = 0;
4010 while (r < q)
4012 if (*r == '%' && r[1] == '*')
4013 substitution = 1;
4014 r++;
4016 /* If we do, handle that case. */
4017 if (substitution)
4019 /* Substitute all matching switches as separate args.
4020 But do this by substituting for %*
4021 in the text that follows the colon. */
4023 unsigned hard_match_len = p - filter - 1;
4024 char *string = save_string (p + 1, q - p - 2);
4026 for (i = 0; i < n_switches; i++)
4027 if (!strncmp (switches[i].part1, filter, hard_match_len)
4028 && check_live_switch (i, -1))
4030 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
4031 /* Pass any arguments this switch has. */
4032 give_switch (i, 1, 1);
4035 return q;
4039 /* If name specified ends in *, as in {x*:...},
4040 check for presence of any switch name starting with x. */
4041 if (p[-1] == '*')
4043 for (i = 0; i < n_switches; i++)
4045 unsigned hard_match_len = p - filter - 1;
4047 if (!strncmp (switches[i].part1, filter, hard_match_len)
4048 && check_live_switch (i, hard_match_len))
4050 present = 1;
4054 /* Otherwise, check for presence of exact name specified. */
4055 else
4057 for (i = 0; i < n_switches; i++)
4059 if (!strncmp (switches[i].part1, filter, p - filter)
4060 && switches[i].part1[p - filter] == 0
4061 && check_live_switch (i, -1))
4063 present = 1;
4064 break;
4069 /* If it is as desired (present for %{s...}, absent for %{-s...})
4070 then substitute either the switch or the specified
4071 conditional text. */
4072 if (present != negate)
4074 if (*p == '}')
4076 give_switch (i, 0, include_blanks);
4078 else
4080 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
4081 return 0;
4084 else if (pipe_p)
4086 /* Here if a %{|...} conditional fails: output a minus sign,
4087 which means "standard output" or "standard input". */
4088 do_spec_1 ("-", 0, NULL_PTR);
4092 return q;
4095 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
4096 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
4097 spec, or -1 if either exact match or %* is used.
4099 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
4100 whose value does not begin with "no-" is obsoleted by the same value
4101 with the "no-", similarly for a switch with the "no-" prefix. */
4103 static int
4104 check_live_switch (switchnum, prefix_length)
4105 int switchnum;
4106 int prefix_length;
4108 char *name = switches[switchnum].part1;
4109 int i;
4111 /* In the common case of {<at-most-one-letter>*}, a negating
4112 switch would always match, so ignore that case. We will just
4113 send the conflicting switches to the compiler phase. */
4114 if (prefix_length >= 0 && prefix_length <= 1)
4115 return 1;
4117 /* If we already processed this switch and determined if it was
4118 live or not, return our past determination. */
4119 if (switches[switchnum].live_cond != 0)
4120 return switches[switchnum].live_cond > 0;
4122 /* Now search for duplicate in a manner that depends on the name. */
4123 switch (*name)
4125 case 'O':
4126 for (i = switchnum + 1; i < n_switches; i++)
4127 if (switches[i].part1[0] == 'O')
4129 switches[switchnum].valid = 1;
4130 switches[switchnum].live_cond = -1;
4131 return 0;
4133 break;
4135 case 'W': case 'f': case 'm':
4136 if (! strncmp (name + 1, "no-", 3))
4138 /* We have Xno-YYY, search for XYYY. */
4139 for (i = switchnum + 1; i < n_switches; i++)
4140 if (switches[i].part1[0] == name[0]
4141 && ! strcmp (&switches[i].part1[1], &name[4]))
4143 switches[switchnum].valid = 1;
4144 switches[switchnum].live_cond = -1;
4145 return 0;
4148 else
4150 /* We have XYYY, search for Xno-YYY. */
4151 for (i = switchnum + 1; i < n_switches; i++)
4152 if (switches[i].part1[0] == name[0]
4153 && switches[i].part1[1] == 'n'
4154 && switches[i].part1[2] == 'o'
4155 && switches[i].part1[3] == '-'
4156 && !strcmp (&switches[i].part1[4], &name[1]))
4158 switches[switchnum].valid = 1;
4159 switches[switchnum].live_cond = -1;
4160 return 0;
4163 break;
4166 /* Otherwise the switch is live. */
4167 switches[switchnum].live_cond = 1;
4168 return 1;
4171 /* Pass a switch to the current accumulating command
4172 in the same form that we received it.
4173 SWITCHNUM identifies the switch; it is an index into
4174 the vector of switches gcc received, which is `switches'.
4175 This cannot fail since it never finishes a command line.
4177 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
4179 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
4180 of the switch. */
4182 static void
4183 give_switch (switchnum, omit_first_word, include_blanks)
4184 int switchnum;
4185 int omit_first_word;
4186 int include_blanks;
4188 if (!omit_first_word)
4190 do_spec_1 ("-", 0, NULL_PTR);
4191 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
4194 if (switches[switchnum].args != 0)
4196 char **p;
4197 for (p = switches[switchnum].args; *p; p++)
4199 if (include_blanks)
4200 do_spec_1 (" ", 0, NULL_PTR);
4201 do_spec_1 (*p, 1, NULL_PTR);
4205 do_spec_1 (" ", 0, NULL_PTR);
4206 switches[switchnum].valid = 1;
4209 /* Search for a file named NAME trying various prefixes including the
4210 user's -B prefix and some standard ones.
4211 Return the absolute file name found. If nothing is found, return NAME. */
4213 static char *
4214 find_file (name)
4215 char *name;
4217 char *newname;
4219 /* Try multilib_dir if it is defined. */
4220 if (multilib_dir != NULL)
4222 char *try;
4224 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
4225 strcpy (try, multilib_dir);
4226 strcat (try, dir_separator_str);
4227 strcat (try, name);
4229 newname = find_a_file (&startfile_prefixes, try, R_OK);
4231 /* If we don't find it in the multi library dir, then fall
4232 through and look for it in the normal places. */
4233 if (newname != NULL)
4234 return newname;
4237 newname = find_a_file (&startfile_prefixes, name, R_OK);
4238 return newname ? newname : name;
4241 /* Determine whether a directory exists. If LINKER, return 0 for
4242 certain fixed names not needed by the linker. If not LINKER, it is
4243 only important to return 0 if the host machine has a small ARG_MAX
4244 limit. */
4246 static int
4247 is_directory (path1, path2, linker)
4248 char *path1;
4249 char *path2;
4250 int linker;
4252 int len1 = strlen (path1);
4253 int len2 = strlen (path2);
4254 char *path = (char *) alloca (3 + len1 + len2);
4255 char *cp;
4256 struct stat st;
4258 #ifndef SMALL_ARG_MAX
4259 if (! linker)
4260 return 1;
4261 #endif
4263 /* Construct the path from the two parts. Ensure the string ends with "/.".
4264 The resulting path will be a directory even if the given path is a
4265 symbolic link. */
4266 bcopy (path1, path, len1);
4267 bcopy (path2, path + len1, len2);
4268 cp = path + len1 + len2;
4269 if (cp[-1] != '/' && cp[-1] != DIR_SEPARATOR)
4270 *cp++ = DIR_SEPARATOR;
4271 *cp++ = '.';
4272 *cp = '\0';
4274 /* Exclude directories that the linker is known to search. */
4275 if (linker
4276 && ((cp - path == 6
4277 && strcmp (path, concat (dir_separator_str, "lib",
4278 dir_separator_str, ".", NULL_PTR)) == 0)
4279 || (cp - path == 10
4280 && strcmp (path, concat (dir_separator_str, "usr",
4281 dir_separator_str, "lib",
4282 dir_separator_str, ".", NULL_PTR)) == 0)))
4283 return 0;
4285 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
4288 /* On fatal signals, delete all the temporary files. */
4290 static void
4291 fatal_error (signum)
4292 int signum;
4294 signal (signum, SIG_DFL);
4295 delete_failure_queue ();
4296 delete_temp_files ();
4297 /* Get the same signal again, this time not handled,
4298 so its normal effect occurs. */
4299 kill (getpid (), signum);
4303 main (argc, argv)
4304 int argc;
4305 char **argv;
4307 register size_t i;
4308 size_t j;
4309 int value;
4310 int linker_was_run = 0;
4311 char *explicit_link_files;
4312 char *specs_file;
4313 char *p;
4314 struct user_specs *uptr;
4316 p = argv[0] + strlen (argv[0]);
4317 while (p != argv[0] && p[-1] != '/' && p[-1] != DIR_SEPARATOR) --p;
4318 programname = p;
4320 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
4321 signal (SIGINT, fatal_error);
4322 #ifdef SIGHUP
4323 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
4324 signal (SIGHUP, fatal_error);
4325 #endif
4326 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
4327 signal (SIGTERM, fatal_error);
4328 #ifdef SIGPIPE
4329 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
4330 signal (SIGPIPE, fatal_error);
4331 #endif
4333 argbuf_length = 10;
4334 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
4336 obstack_init (&obstack);
4338 /* Build multilib_select, et. al from the separate lines that make up each
4339 multilib selection. */
4341 char **q = multilib_raw;
4342 int need_space;
4344 obstack_init (&multilib_obstack);
4345 while ((p = *q++) != (char *) 0)
4346 obstack_grow (&multilib_obstack, p, strlen (p));
4348 obstack_1grow (&multilib_obstack, 0);
4349 multilib_select = obstack_finish (&multilib_obstack);
4351 q = multilib_matches_raw;
4352 while ((p = *q++) != (char *) 0)
4353 obstack_grow (&multilib_obstack, p, strlen (p));
4355 obstack_1grow (&multilib_obstack, 0);
4356 multilib_matches = obstack_finish (&multilib_obstack);
4358 need_space = FALSE;
4359 for (i = 0;
4360 i < sizeof (multilib_defaults_raw) / sizeof (multilib_defaults_raw[0]);
4361 i++)
4363 if (need_space)
4364 obstack_1grow (&multilib_obstack, ' ');
4365 obstack_grow (&multilib_obstack,
4366 multilib_defaults_raw[i],
4367 strlen (multilib_defaults_raw[i]));
4368 need_space = TRUE;
4371 obstack_1grow (&multilib_obstack, 0);
4372 multilib_defaults = obstack_finish (&multilib_obstack);
4375 /* Set up to remember the pathname of gcc and any options
4376 needed for collect. We use argv[0] instead of programname because
4377 we need the complete pathname. */
4378 obstack_init (&collect_obstack);
4379 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
4380 obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
4381 putenv (obstack_finish (&collect_obstack));
4383 #ifdef INIT_ENVIRONMENT
4384 /* Set up any other necessary machine specific environment variables. */
4385 putenv (INIT_ENVIRONMENT);
4386 #endif
4388 /* Choose directory for temp files. */
4390 temp_filename = choose_temp_base ();
4391 temp_filename_length = strlen (temp_filename);
4393 /* Make a table of what switches there are (switches, n_switches).
4394 Make a table of specified input files (infiles, n_infiles).
4395 Decode switches that are handled locally. */
4397 process_command (argc, argv);
4400 int first_time;
4402 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4403 the compiler. */
4404 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4405 sizeof ("COLLECT_GCC_OPTIONS=")-1);
4407 first_time = TRUE;
4408 for (i = 0; i < n_switches; i++)
4410 char **args;
4411 char *p, *q;
4412 if (!first_time)
4413 obstack_grow (&collect_obstack, " ", 1);
4415 first_time = FALSE;
4416 obstack_grow (&collect_obstack, "'-", 2);
4417 q = switches[i].part1;
4418 while ((p = index (q,'\'')))
4420 obstack_grow (&collect_obstack, q, p-q);
4421 obstack_grow (&collect_obstack, "'\\''", 4);
4422 q = ++p;
4424 obstack_grow (&collect_obstack, q, strlen (q));
4425 obstack_grow (&collect_obstack, "'", 1);
4427 for (args = switches[i].args; args && *args; args++)
4429 obstack_grow (&collect_obstack, " '", 2);
4430 q = *args;
4431 while ((p = index (q,'\'')))
4433 obstack_grow (&collect_obstack, q, p-q);
4434 obstack_grow (&collect_obstack, "'\\''", 4);
4435 q = ++p;
4437 obstack_grow (&collect_obstack, q, strlen (q));
4438 obstack_grow (&collect_obstack, "'", 1);
4441 obstack_grow (&collect_obstack, "\0", 1);
4442 putenv (obstack_finish (&collect_obstack));
4445 /* Initialize the vector of specs to just the default.
4446 This means one element containing 0s, as a terminator. */
4448 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
4449 bcopy ((char *) default_compilers, (char *) compilers,
4450 sizeof default_compilers);
4451 n_compilers = n_default_compilers;
4453 /* Read specs from a file if there is one. */
4455 machine_suffix = concat (spec_machine, dir_separator_str,
4456 spec_version, dir_separator_str, NULL_PTR);
4457 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
4459 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
4460 /* Read the specs file unless it is a default one. */
4461 if (specs_file != 0 && strcmp (specs_file, "specs"))
4462 read_specs (specs_file, TRUE);
4463 else
4464 init_spec ();
4466 /* We need to check standard_exec_prefix/just_machine_suffix/specs
4467 for any override of as, ld and libraries. */
4468 specs_file = (char *) alloca (strlen (standard_exec_prefix)
4469 + strlen (just_machine_suffix)
4470 + sizeof ("specs"));
4472 strcpy (specs_file, standard_exec_prefix);
4473 strcat (specs_file, just_machine_suffix);
4474 strcat (specs_file, "specs");
4475 if (access (specs_file, R_OK) == 0)
4476 read_specs (specs_file, TRUE);
4478 /* Process any user specified specs in the order given on the command
4479 line. */
4480 for (uptr = user_specs_head; uptr; uptr = uptr->next)
4482 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
4483 read_specs (filename ? filename : uptr->filename, FALSE);
4486 /* If not cross-compiling, look for startfiles in the standard places. */
4487 /* The fact that these are done here, after reading the specs file,
4488 means that it cannot be found in these directories.
4489 But that's okay. It should never be there anyway. */
4490 if (*cross_compile == '0')
4492 #ifdef MD_EXEC_PREFIX
4493 add_prefix (&exec_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4494 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4495 #endif
4497 #ifdef MD_STARTFILE_PREFIX
4498 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
4499 0, 0, NULL_PTR);
4500 #endif
4502 #ifdef MD_STARTFILE_PREFIX_1
4503 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
4504 0, 0, NULL_PTR);
4505 #endif
4507 /* If standard_startfile_prefix is relative, base it on
4508 standard_exec_prefix. This lets us move the installed tree
4509 as a unit. If GCC_EXEC_PREFIX is defined, base
4510 standard_startfile_prefix on that as well. */
4511 if (*standard_startfile_prefix == '/'
4512 || *standard_startfile_prefix == DIR_SEPARATOR)
4513 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
4514 0, 0, NULL_PTR);
4515 else
4517 if (gcc_exec_prefix)
4518 add_prefix (&startfile_prefixes,
4519 concat (gcc_exec_prefix, machine_suffix,
4520 standard_startfile_prefix, NULL_PTR),
4521 NULL_PTR, 0, 0, NULL_PTR);
4522 add_prefix (&startfile_prefixes,
4523 concat (standard_exec_prefix,
4524 machine_suffix,
4525 standard_startfile_prefix, NULL_PTR),
4526 NULL_PTR, 0, 0, NULL_PTR);
4529 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
4530 "BINUTILS", 0, 0, NULL_PTR);
4531 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
4532 "BINUTILS", 0, 0, NULL_PTR);
4533 #if 0 /* Can cause surprises, and one can use -B./ instead. */
4534 add_prefix (&startfile_prefixes, "./", NULL_PTR, 0, 1, NULL_PTR);
4535 #endif
4537 else
4539 if (*standard_startfile_prefix != DIR_SEPARATOR && gcc_exec_prefix)
4540 add_prefix (&startfile_prefixes,
4541 concat (gcc_exec_prefix, machine_suffix,
4542 standard_startfile_prefix, NULL_PTR),
4543 "BINUTILS", 0, 0, NULL_PTR);
4546 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
4547 if (gcc_exec_prefix)
4549 char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
4550 + strlen (spec_version)
4551 + strlen (spec_machine) + 3);
4552 strcpy (temp, gcc_exec_prefix);
4553 strcat (temp, spec_machine);
4554 strcat (temp, dir_separator_str);
4555 strcat (temp, spec_version);
4556 strcat (temp, dir_separator_str);
4557 gcc_exec_prefix = temp;
4560 /* Now we have the specs.
4561 Set the `valid' bits for switches that match anything in any spec. */
4563 validate_all_switches ();
4565 /* Now that we have the switches and the specs, set
4566 the subdirectory based on the options. */
4567 set_multilib_dir ();
4569 /* Warn about any switches that no pass was interested in. */
4571 for (i = 0; i < n_switches; i++)
4572 if (! switches[i].valid)
4573 error ("unrecognized option `-%s'", switches[i].part1);
4575 /* Obey some of the options. */
4577 if (print_search_dirs)
4579 printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
4580 printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
4581 printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
4582 exit (0);
4585 if (print_file_name)
4587 printf ("%s\n", find_file (print_file_name));
4588 exit (0);
4591 if (print_prog_name)
4593 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
4594 printf ("%s\n", (newname ? newname : print_prog_name));
4595 exit (0);
4598 if (print_multi_lib)
4600 print_multilib_info ();
4601 exit (0);
4604 if (print_multi_directory)
4606 if (multilib_dir == NULL)
4607 printf (".\n");
4608 else
4609 printf ("%s\n", multilib_dir);
4610 exit (0);
4613 if (verbose_flag)
4615 int n;
4617 /* compiler_version is truncated at the first space when initialized
4618 from version string, so truncate version_string at the first space
4619 before comparing. */
4620 for (n = 0; version_string[n]; n++)
4621 if (version_string[n] == ' ')
4622 break;
4624 if (! strncmp (version_string, compiler_version, n)
4625 && compiler_version[n] == 0)
4626 fprintf (stderr, "gcc version %s\n", version_string);
4627 else
4628 fprintf (stderr, "gcc driver version %s executing gcc version %s\n",
4629 version_string, compiler_version);
4631 if (n_infiles == 0)
4632 exit (0);
4635 if (n_infiles == 0)
4636 fatal ("No input files");
4638 /* Make a place to record the compiler output file names
4639 that correspond to the input files. */
4641 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
4642 bzero ((char *) outfiles, n_infiles * sizeof (char *));
4644 /* Record which files were specified explicitly as link input. */
4646 explicit_link_files = xmalloc (n_infiles);
4647 bzero (explicit_link_files, n_infiles);
4649 for (i = 0; i < n_infiles; i++)
4651 register struct compiler *cp = 0;
4652 int this_file_error = 0;
4654 /* Tell do_spec what to substitute for %i. */
4656 input_filename = infiles[i].name;
4657 input_filename_length = strlen (input_filename);
4658 input_file_number = i;
4660 /* Use the same thing in %o, unless cp->spec says otherwise. */
4662 outfiles[i] = input_filename;
4664 /* Figure out which compiler from the file's suffix. */
4666 cp = lookup_compiler (infiles[i].name, input_filename_length,
4667 infiles[i].language);
4669 if (cp)
4671 /* Ok, we found an applicable compiler. Run its spec. */
4672 /* First say how much of input_filename to substitute for %b */
4673 register char *p;
4674 int len;
4676 if (cp->spec[0][0] == '#')
4677 error ("%s: %s compiler not installed on this system",
4678 input_filename, &cp->spec[0][1]);
4680 input_basename = input_filename;
4681 for (p = input_filename; *p; p++)
4682 if (*p == '/' || *p == DIR_SEPARATOR)
4683 input_basename = p + 1;
4685 /* Find a suffix starting with the last period,
4686 and set basename_length to exclude that suffix. */
4687 basename_length = strlen (input_basename);
4688 p = input_basename + basename_length;
4689 while (p != input_basename && *p != '.') --p;
4690 if (*p == '.' && p != input_basename)
4692 basename_length = p - input_basename;
4693 input_suffix = p + 1;
4695 else
4696 input_suffix = "";
4698 len = 0;
4699 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4700 if (cp->spec[j])
4701 len += strlen (cp->spec[j]);
4703 p = (char *) xmalloc (len + 1);
4705 len = 0;
4706 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4707 if (cp->spec[j])
4709 strcpy (p + len, cp->spec[j]);
4710 len += strlen (cp->spec[j]);
4713 value = do_spec (p);
4714 free (p);
4715 if (value < 0)
4716 this_file_error = 1;
4719 /* If this file's name does not contain a recognized suffix,
4720 record it as explicit linker input. */
4722 else
4723 explicit_link_files[i] = 1;
4725 /* Clear the delete-on-failure queue, deleting the files in it
4726 if this compilation failed. */
4728 if (this_file_error)
4730 delete_failure_queue ();
4731 error_count++;
4733 /* If this compilation succeeded, don't delete those files later. */
4734 clear_failure_queue ();
4737 /* Run ld to link all the compiler output files. */
4739 if (error_count == 0)
4741 int tmp = execution_count;
4743 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
4744 for collect. */
4745 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
4746 putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
4748 value = do_spec (link_command_spec);
4749 if (value < 0)
4750 error_count = 1;
4751 linker_was_run = (tmp != execution_count);
4754 /* Warn if a -B option was specified but the prefix was never used. */
4755 unused_prefix_warnings (&exec_prefixes);
4756 unused_prefix_warnings (&startfile_prefixes);
4758 /* If options said don't run linker,
4759 complain about input files to be given to the linker. */
4761 if (! linker_was_run && error_count == 0)
4762 for (i = 0; i < n_infiles; i++)
4763 if (explicit_link_files[i])
4764 error ("%s: linker input file unused since linking not done",
4765 outfiles[i]);
4767 /* Delete some or all of the temporary files we made. */
4769 if (error_count)
4770 delete_failure_queue ();
4771 delete_temp_files ();
4773 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
4774 /* NOTREACHED */
4775 return 0;
4778 /* Find the proper compilation spec for the file name NAME,
4779 whose length is LENGTH. LANGUAGE is the specified language,
4780 or 0 if none specified. */
4782 static struct compiler *
4783 lookup_compiler (name, length, language)
4784 char *name;
4785 size_t length;
4786 char *language;
4788 struct compiler *cp;
4790 /* Look for the language, if one is spec'd. */
4791 if (language != 0)
4793 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4795 if (language != 0)
4797 if (cp->suffix[0] == '@'
4798 && !strcmp (cp->suffix + 1, language))
4799 return cp;
4802 error ("language %s not recognized", language);
4805 /* Look for a suffix. */
4806 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4808 if (/* The suffix `-' matches only the file name `-'. */
4809 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
4811 (strlen (cp->suffix) < length
4812 /* See if the suffix matches the end of NAME. */
4813 #ifdef OS2
4814 && (!strcmp (cp->suffix,
4815 name + length - strlen (cp->suffix))
4816 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
4817 && !strcasecmp (cp->suffix,
4818 name + length - strlen (cp->suffix)))))
4819 #else
4820 && !strcmp (cp->suffix,
4821 name + length - strlen (cp->suffix))))
4822 #endif
4824 if (cp->spec[0][0] == '@')
4826 struct compiler *new;
4827 /* An alias entry maps a suffix to a language.
4828 Search for the language; pass 0 for NAME and LENGTH
4829 to avoid infinite recursion if language not found.
4830 Construct the new compiler spec. */
4831 language = cp->spec[0] + 1;
4832 new = (struct compiler *) xmalloc (sizeof (struct compiler));
4833 new->suffix = cp->suffix;
4834 bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
4835 (char *) new->spec, sizeof new->spec);
4836 return new;
4838 /* A non-alias entry: return it. */
4839 return cp;
4843 return 0;
4846 char *
4847 xmalloc (size)
4848 unsigned size;
4850 register char *value = (char *) malloc (size);
4851 if (value == 0)
4852 fatal ("virtual memory exhausted");
4853 return value;
4856 char *
4857 xrealloc (ptr, size)
4858 char *ptr;
4859 unsigned size;
4861 register char *value = (char *) realloc (ptr, size);
4862 if (value == 0)
4863 fatal ("virtual memory exhausted");
4864 return value;
4867 /* This function is based on the one in libiberty. */
4869 static char *
4870 concat VPROTO((char *first, ...))
4872 register int length;
4873 register char *newstr;
4874 register char *end;
4875 register char *arg;
4876 va_list args;
4877 #ifndef __STDC__
4878 char *first;
4879 #endif
4881 /* First compute the size of the result and get sufficient memory. */
4883 VA_START (args, first);
4884 #ifndef __STDC__
4885 first = va_arg (args, char *);
4886 #endif
4888 arg = first;
4889 length = 0;
4891 while (arg != 0)
4893 length += strlen (arg);
4894 arg = va_arg (args, char *);
4897 newstr = (char *) xmalloc (length + 1);
4898 va_end (args);
4900 /* Now copy the individual pieces to the result string. */
4902 VA_START (args, first);
4903 #ifndef __STDC__
4904 first = va_arg (args, char *);
4905 #endif
4907 end = newstr;
4908 arg = first;
4909 while (arg != 0)
4911 while (*arg)
4912 *end++ = *arg++;
4913 arg = va_arg (args, char *);
4915 *end = '\000';
4916 va_end (args);
4918 return (newstr);
4921 static char *
4922 save_string (s, len)
4923 char *s;
4924 int len;
4926 register char *result = xmalloc (len + 1);
4928 bcopy (s, result, len);
4929 result[len] = 0;
4930 return result;
4933 static void
4934 pfatal_with_name (name)
4935 char *name;
4937 fatal ("%s: %s", name, my_strerror (errno));
4940 static void
4941 perror_with_name (name)
4942 char *name;
4944 error ("%s: %s", name, my_strerror (errno));
4947 static void
4948 pfatal_pexecute (errmsg_fmt, errmsg_arg)
4949 char *errmsg_fmt;
4950 char *errmsg_arg;
4952 if (errmsg_arg)
4954 /* Space for trailing '\0' is in %s. */
4955 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
4956 sprintf (msg, errmsg_fmt, errmsg_arg);
4957 errmsg_fmt = msg;
4960 fatal ("%s: %s", errmsg_fmt, my_strerror (errno));
4963 /* More 'friendly' abort that prints the line and file.
4964 config.h can #define abort fancy_abort if you like that sort of thing. */
4966 void
4967 fancy_abort ()
4969 fatal ("Internal gcc abort.");
4972 #ifdef HAVE_VPRINTF
4974 /* Output an error message and exit */
4976 static void
4977 fatal VPROTO((char *format, ...))
4979 #ifndef __STDC__
4980 char *format;
4981 #endif
4982 va_list ap;
4984 VA_START (ap, format);
4986 #ifndef __STDC__
4987 format = va_arg (ap, char *);
4988 #endif
4990 fprintf (stderr, "%s: ", programname);
4991 vfprintf (stderr, format, ap);
4992 va_end (ap);
4993 fprintf (stderr, "\n");
4994 delete_temp_files ();
4995 exit (1);
4998 static void
4999 error VPROTO((char *format, ...))
5001 #ifndef __STDC__
5002 char *format;
5003 #endif
5004 va_list ap;
5006 VA_START (ap, format);
5008 #ifndef __STDC__
5009 format = va_arg (ap, char *);
5010 #endif
5012 fprintf (stderr, "%s: ", programname);
5013 vfprintf (stderr, format, ap);
5014 va_end (ap);
5016 fprintf (stderr, "\n");
5019 #else /* not HAVE_VPRINTF */
5021 static void
5022 fatal (msg, arg1, arg2)
5023 char *msg, *arg1, *arg2;
5025 error (msg, arg1, arg2);
5026 delete_temp_files ();
5027 exit (1);
5030 static void
5031 error (msg, arg1, arg2)
5032 char *msg, *arg1, *arg2;
5034 fprintf (stderr, "%s: ", programname);
5035 fprintf (stderr, msg, arg1, arg2);
5036 fprintf (stderr, "\n");
5039 #endif /* not HAVE_VPRINTF */
5042 static void
5043 validate_all_switches ()
5045 struct compiler *comp;
5046 register char *p;
5047 register char c;
5048 struct spec_list *spec;
5050 for (comp = compilers; comp->spec[0]; comp++)
5052 size_t i;
5053 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
5055 p = comp->spec[i];
5056 while ((c = *p++))
5057 if (c == '%' && *p == '{')
5058 /* We have a switch spec. */
5059 validate_switches (p + 1);
5063 /* look through the linked list of specs read from the specs file */
5064 for (spec = specs; spec ; spec = spec->next)
5066 p = *(spec->ptr_spec);
5067 while ((c = *p++))
5068 if (c == '%' && *p == '{')
5069 /* We have a switch spec. */
5070 validate_switches (p + 1);
5073 p = link_command_spec;
5074 while ((c = *p++))
5075 if (c == '%' && *p == '{')
5076 /* We have a switch spec. */
5077 validate_switches (p + 1);
5080 /* Look at the switch-name that comes after START
5081 and mark as valid all supplied switches that match it. */
5083 static void
5084 validate_switches (start)
5085 char *start;
5087 register char *p = start;
5088 char *filter;
5089 register int i;
5090 int suffix = 0;
5092 if (*p == '|')
5093 ++p;
5095 if (*p == '!')
5096 ++p;
5098 if (*p == '.')
5099 suffix = 1, ++p;
5101 filter = p;
5102 while (*p != ':' && *p != '}') p++;
5104 if (suffix)
5106 else if (p[-1] == '*')
5108 /* Mark all matching switches as valid. */
5109 --p;
5110 for (i = 0; i < n_switches; i++)
5111 if (!strncmp (switches[i].part1, filter, p - filter))
5112 switches[i].valid = 1;
5114 else
5116 /* Mark an exact matching switch as valid. */
5117 for (i = 0; i < n_switches; i++)
5119 if (!strncmp (switches[i].part1, filter, p - filter)
5120 && switches[i].part1[p - filter] == 0)
5121 switches[i].valid = 1;
5126 /* Check whether a particular argument was used. The first time we
5127 canonicalize the switches to keep only the ones we care about. */
5129 static int
5130 used_arg (p, len)
5131 char *p;
5132 int len;
5134 struct mswitchstr {
5135 char *str;
5136 char *replace;
5137 int len;
5138 int rep_len;
5141 static struct mswitchstr *mswitches;
5142 static int n_mswitches;
5143 int i, j;
5145 if (!mswitches)
5147 struct mswitchstr *matches;
5148 char *q;
5149 int cnt = 0;
5151 /* Break multilib_matches into the component strings of string and replacement
5152 string */
5153 for (q = multilib_matches; *q != '\0'; q++)
5154 if (*q == ';')
5155 cnt++;
5157 matches = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
5158 i = 0;
5159 q = multilib_matches;
5160 while (*q != '\0')
5162 matches[i].str = q;
5163 while (*q != ' ')
5165 if (*q == '\0')
5166 abort ();
5167 q++;
5169 *q = '\0';
5170 matches[i].len = q - matches[i].str;
5172 matches[i].replace = ++q;
5173 while (*q != ';' && *q != '\0')
5175 if (*q == ' ')
5176 abort ();
5177 q++;
5179 matches[i].rep_len = q - matches[i].replace;
5180 i++;
5181 if (*q == ';')
5182 *q++ = '\0';
5183 else
5184 break;
5187 /* Now build a list of the replacement string for switches that we care
5188 about. Make sure we allocate at least one entry. This prevents
5189 xmalloc from calling fatal, and prevents us from re-executing this
5190 block of code. */
5191 mswitches
5192 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
5193 * (n_switches ? n_switches : 1));
5194 for (i = 0; i < n_switches; i++)
5196 int xlen = strlen (switches[i].part1);
5197 for (j = 0; j < cnt; j++)
5198 if (xlen == matches[j].len && ! strcmp (switches[i].part1, matches[j].str))
5200 mswitches[n_mswitches].str = matches[j].replace;
5201 mswitches[n_mswitches].len = matches[j].rep_len;
5202 mswitches[n_mswitches].replace = (char *)0;
5203 mswitches[n_mswitches].rep_len = 0;
5204 n_mswitches++;
5205 break;
5210 for (i = 0; i < n_mswitches; i++)
5211 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
5212 return 1;
5214 return 0;
5217 static int
5218 default_arg (p, len)
5219 char *p;
5220 int len;
5222 char *start, *end;
5224 for (start = multilib_defaults; *start != '\0'; start = end+1)
5226 while (*start == ' ' || *start == '\t')
5227 start++;
5229 if (*start == '\0')
5230 break;
5232 for (end = start+1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
5235 if ((end - start) == len && strncmp (p, start, len) == 0)
5236 return 1;
5238 if (*end == '\0')
5239 break;
5242 return 0;
5245 /* Work out the subdirectory to use based on the
5246 options. The format of multilib_select is a list of elements.
5247 Each element is a subdirectory name followed by a list of options
5248 followed by a semicolon. gcc will consider each line in turn. If
5249 none of the options beginning with an exclamation point are
5250 present, and all of the other options are present, that
5251 subdirectory will be used. */
5253 static void
5254 set_multilib_dir ()
5256 char *p = multilib_select;
5257 int this_path_len;
5258 char *this_path, *this_arg;
5259 int not_arg;
5260 int ok;
5262 while (*p != '\0')
5264 /* Ignore newlines. */
5265 if (*p == '\n')
5267 ++p;
5268 continue;
5271 /* Get the initial path. */
5272 this_path = p;
5273 while (*p != ' ')
5275 if (*p == '\0')
5276 abort ();
5277 ++p;
5279 this_path_len = p - this_path;
5281 /* Check the arguments. */
5282 ok = 1;
5283 ++p;
5284 while (*p != ';')
5286 if (*p == '\0')
5287 abort ();
5289 if (! ok)
5291 ++p;
5292 continue;
5295 this_arg = p;
5296 while (*p != ' ' && *p != ';')
5298 if (*p == '\0')
5299 abort ();
5300 ++p;
5303 if (*this_arg != '!')
5304 not_arg = 0;
5305 else
5307 not_arg = 1;
5308 ++this_arg;
5311 /* If this is a default argument, we can just ignore it.
5312 This is true even if this_arg begins with '!'. Beginning
5313 with '!' does not mean that this argument is necessarily
5314 inappropriate for this library: it merely means that
5315 there is a more specific library which uses this
5316 argument. If this argument is a default, we need not
5317 consider that more specific library. */
5318 if (! default_arg (this_arg, p - this_arg))
5320 ok = used_arg (this_arg, p - this_arg);
5321 if (not_arg)
5322 ok = ! ok;
5325 if (*p == ' ')
5326 ++p;
5329 if (ok)
5331 if (this_path_len != 1
5332 || this_path[0] != '.')
5334 multilib_dir = xmalloc (this_path_len + 1);
5335 strncpy (multilib_dir, this_path, this_path_len);
5336 multilib_dir[this_path_len] = '\0';
5338 break;
5341 ++p;
5345 /* Print out the multiple library subdirectory selection
5346 information. This prints out a series of lines. Each line looks
5347 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
5348 required. Only the desired options are printed out, the negative
5349 matches. The options are print without a leading dash. There are
5350 no spaces to make it easy to use the information in the shell.
5351 Each subdirectory is printed only once. This assumes the ordering
5352 generated by the genmultilib script. */
5354 static void
5355 print_multilib_info ()
5357 char *p = multilib_select;
5358 char *last_path = 0, *this_path;
5359 int skip;
5360 int last_path_len = 0;
5362 while (*p != '\0')
5364 /* Ignore newlines. */
5365 if (*p == '\n')
5367 ++p;
5368 continue;
5371 /* Get the initial path. */
5372 this_path = p;
5373 while (*p != ' ')
5375 if (*p == '\0')
5376 abort ();
5377 ++p;
5380 /* If this is a duplicate, skip it. */
5381 skip = (last_path != 0 && p - this_path == last_path_len
5382 && ! strncmp (last_path, this_path, last_path_len));
5384 last_path = this_path;
5385 last_path_len = p - this_path;
5387 /* If this directory requires any default arguments, we can skip
5388 it. We will already have printed a directory identical to
5389 this one which does not require that default argument. */
5390 if (! skip)
5392 char *q;
5394 q = p + 1;
5395 while (*q != ';')
5397 char *arg;
5399 if (*q == '\0')
5400 abort ();
5402 if (*q == '!')
5403 arg = NULL;
5404 else
5405 arg = q;
5407 while (*q != ' ' && *q != ';')
5409 if (*q == '\0')
5410 abort ();
5411 ++q;
5414 if (arg != NULL
5415 && default_arg (arg, q - arg))
5417 skip = 1;
5418 break;
5421 if (*q == ' ')
5422 ++q;
5426 if (! skip)
5428 char *p1;
5430 for (p1 = last_path; p1 < p; p1++)
5431 putchar (*p1);
5432 putchar (';');
5435 ++p;
5436 while (*p != ';')
5438 int use_arg;
5440 if (*p == '\0')
5441 abort ();
5443 if (skip)
5445 ++p;
5446 continue;
5449 use_arg = *p != '!';
5451 if (use_arg)
5452 putchar ('@');
5454 while (*p != ' ' && *p != ';')
5456 if (*p == '\0')
5457 abort ();
5458 if (use_arg)
5459 putchar (*p);
5460 ++p;
5463 if (*p == ' ')
5464 ++p;
5467 if (! skip)
5469 /* If there are extra options, print them now */
5470 if (multilib_extra && *multilib_extra)
5472 int print_at = TRUE;
5473 char *q;
5475 for (q = multilib_extra; *q != '\0'; q++)
5477 if (*q == ' ')
5478 print_at = TRUE;
5479 else
5481 if (print_at)
5482 putchar ('@');
5483 putchar (*q);
5484 print_at = FALSE;
5488 putchar ('\n');
5491 ++p;