OSDN Git Service

(default_compilers): Pass -i* after -U*.
[pf3gnuchains/gcc-fork.git] / gcc / gcc.c
1 /* Compiler driver program that can handle many languages.
2    Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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, 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 This paragraph is here to try to keep Sun CC from dying.
21 The number of chars here seems crucial!!!!  */
22
23 /* This program is the user interface to the C compiler and possibly to
24 other compilers.  It is used because compilation is a complicated procedure
25 which involves running several programs and passing temporary files between
26 them, forwarding the users switches to those programs selectively,
27 and deleting the temporary files at the end.
28
29 CC recognizes how to compile each input file by suffixes in the file names.
30 Once it knows which kind of compilation to perform, the procedure for
31 compilation is specified by a string called a "spec".  */
32 \f
33 #include <sys/types.h>
34 #include <ctype.h>
35 #include <signal.h>
36 #include <sys/stat.h>
37 #include <sys/file.h>   /* May get R_OK, etc. on some systems.  */
38
39 #include "config.h"
40 #include "obstack.h"
41 #include "gvarargs.h"
42 #include <stdio.h>
43
44 #ifndef R_OK
45 #define R_OK 4
46 #define W_OK 2
47 #define X_OK 1
48 #endif
49
50 /* Define a generic NULL if one hasn't already been defined.  */
51
52 #ifndef NULL
53 #define NULL 0
54 #endif
55
56 #ifndef GENERIC_PTR
57 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
58 #define GENERIC_PTR void *
59 #else
60 #define GENERIC_PTR char *
61 #endif
62 #endif
63
64 #ifndef NULL_PTR
65 #define NULL_PTR ((GENERIC_PTR)0)
66 #endif
67
68 #ifdef USG
69 #define vfork fork
70 #endif /* USG */
71
72 /* On MSDOS, write temp files in current dir
73    because there's no place else we can expect to use.  */
74 #if __MSDOS__
75 #ifndef P_tmpdir
76 #define P_tmpdir "./"
77 #endif
78 #endif
79
80 /* Test if something is a normal file.  */
81 #ifndef S_ISREG
82 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
83 #endif
84
85 /* Test if something is a directory.  */
86 #ifndef S_ISDIR
87 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
88 #endif
89
90 /* By default there is no special suffix for executables.  */
91 #ifndef EXECUTABLE_SUFFIX
92 #define EXECUTABLE_SUFFIX ""
93 #endif
94
95 /* By default, colon separates directories in a path.  */
96 #ifndef PATH_SEPARATOR
97 #define PATH_SEPARATOR ':'
98 #endif
99
100 #define obstack_chunk_alloc xmalloc
101 #define obstack_chunk_free free
102
103 extern void free ();
104 extern char *getenv ();
105
106 extern int errno, sys_nerr;
107 extern char *sys_errlist[];
108
109 extern int execv (), execvp ();
110
111 /* If a stage of compilation returns an exit status >= 1,
112    compilation of that file ceases.  */
113
114 #define MIN_FATAL_STATUS 1
115
116 /* Flag saying to print the full filename of libgcc.a
117    as found through our usual search mechanism.  */
118
119 static int print_libgcc_file_name;
120
121 /* Flag indicating whether we should print the command and arguments */
122
123 static int verbose_flag;
124
125 /* Nonzero means write "temp" files in source directory
126    and use the source file's name in them, and don't delete them.  */
127
128 static int save_temps_flag;
129
130 /* The compiler version specified with -V */
131
132 static char *spec_version;
133
134 /* The target machine specified with -b.  */
135
136 static char *spec_machine = DEFAULT_TARGET_MACHINE;
137
138 /* Nonzero if cross-compiling.
139    When -b is used, the value comes from the `specs' file.  */
140
141 #ifdef CROSS_COMPILE
142 static int cross_compile = 1;
143 #else
144 static int cross_compile = 0;
145 #endif
146
147 /* This is the obstack which we use to allocate many strings.  */
148
149 static struct obstack obstack;
150
151 /* This is the obstack to build an environment variable to pass to
152    collect2 that describes all of the relevant switches of what to
153    pass the compiler in building the list of pointers to constructors
154    and destructors.  */
155
156 static struct obstack collect_obstack;
157
158 extern char *version_string;
159
160 static void set_spec ();
161 static struct compiler *lookup_compiler ();
162 static char *find_a_file ();
163 static void add_prefix ();
164 static char *skip_whitespace ();
165 static void record_temp_file ();
166 static char *handle_braces ();
167 static char *save_string ();
168 static char *concat ();
169 static int do_spec ();
170 static int do_spec_1 ();
171 static char *find_file ();
172 static int is_linker_dir ();
173 static void validate_switches ();
174 static void validate_all_switches ();
175 static void give_switch ();
176 static void pfatal_with_name ();
177 static void perror_with_name ();
178 static void perror_exec ();
179 static void fatal ();
180 static void error ();
181 void fancy_abort ();
182 char *xmalloc ();
183 char *xrealloc ();
184 \f
185 /* Specs are strings containing lines, each of which (if not blank)
186 is made up of a program name, and arguments separated by spaces.
187 The program name must be exact and start from root, since no path
188 is searched and it is unreliable to depend on the current working directory.
189 Redirection of input or output is not supported; the subprograms must
190 accept filenames saying what files to read and write.
191
192 In addition, the specs can contain %-sequences to substitute variable text
193 or for conditional text.  Here is a table of all defined %-sequences.
194 Note that spaces are not generated automatically around the results of
195 expanding these sequences; therefore, you can concatenate them together
196 or with constant text in a single argument.
197
198  %%     substitute one % into the program name or argument.
199  %i     substitute the name of the input file being processed.
200  %b     substitute the basename of the input file being processed.
201         This is the substring up to (and not including) the last period
202         and not including the directory.
203  %g     substitute the temporary-file-name-base.  This is a string chosen
204         once per compilation.  Different temporary file names are made by
205         concatenation of constant strings on the end, as in `%g.s'.
206         %g also has the same effect of %d.
207  %u     like %g, but make the temporary file name unique.
208  %U     returns the last file name generated with %u.
209  %d     marks the argument containing or following the %d as a
210         temporary file name, so that that file will be deleted if CC exits
211         successfully.  Unlike %g, this contributes no text to the argument.
212  %w     marks the argument containing or following the %w as the
213         "output file" of this compilation.  This puts the argument
214         into the sequence of arguments that %o will substitute later.
215  %W{...}
216         like %{...} but mark last argument supplied within
217         as a file to be deleted on failure.
218  %o     substitutes the names of all the output files, with spaces
219         automatically placed around them.  You should write spaces
220         around the %o as well or the results are undefined.
221         %o is for use in the specs for running the linker.
222         Input files whose names have no recognized suffix are not compiled
223         at all, but they are included among the output files, so they will
224         be linked.
225  %p     substitutes the standard macro predefinitions for the
226         current target machine.  Use this when running cpp.
227  %P     like %p, but puts `__' before and after the name of each macro.
228         (Except macros that already have __.)
229         This is for ANSI C.
230  %I     Substitute a -iprefix option made from GCC_EXEC_PREFIX.
231  %s     current argument is the name of a library or startup file of some sort.
232         Search for that file in a standard list of directories
233         and substitute the full name found.
234  %eSTR  Print STR as an error message.  STR is terminated by a newline.
235         Use this when inconsistent options are detected.
236  %x{OPTION}     Accumulate an option for %X.
237  %X     Output the accumulated linker options specified by compilations.
238  %Y     Output the accumulated assembler options specified by compilations.
239  %a     process ASM_SPEC as a spec.
240         This allows config.h to specify part of the spec for running as.
241  %A     process ASM_FINAL_SPEC as a spec.  A capital A is actually
242         used here.  This can be used to run a post-processor after the
243         assembler has done it's job.
244  %D     Dump out a -L option for each directory in library_prefix,
245         followed by a -L option for each directory in startfile_prefix.
246  %l     process LINK_SPEC as a spec.
247  %L     process LIB_SPEC as a spec.
248  %S     process STARTFILE_SPEC as a spec.  A capital S is actually used here.
249  %E     process ENDFILE_SPEC as a spec.  A capital E is actually used here.
250  %c     process SIGNED_CHAR_SPEC as a spec.
251  %C     process CPP_SPEC as a spec.  A capital C is actually used here.
252  %1     process CC1_SPEC as a spec.
253  %2     process CC1PLUS_SPEC as a spec.
254  %*     substitute the variable part of a matched option.  (See below.)
255         Note that each comma in the substituted string is replaced by
256         a single space.
257  %{S}   substitutes the -S switch, if that switch was given to CC.
258         If that switch was not specified, this substitutes nothing.
259         Here S is a metasyntactic variable.
260  %{S*}  substitutes all the switches specified to CC whose names start
261         with -S.  This is used for -o, -D, -I, etc; switches that take
262         arguments.  CC considers `-o foo' as being one switch whose
263         name starts with `o'.  %{o*} would substitute this text,
264         including the space; thus, two arguments would be generated.
265  %{S*:X} substitutes X if one or more switches whose names start with -S are
266         specified to CC.  Note that the tail part of the -S option
267         (i.e. the part matched by the `*') will be substituted for each
268         occurrence of %* within X.
269  %{S:X} substitutes X, but only if the -S switch was given to CC.
270  %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
271  %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
272  %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
273  %{.S:X} substitutes X, but only if processing a file with suffix S.
274  %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
275  %(Spec) processes a specification defined in a specs file as *Spec:
276  %[Spec] as above, but put __ around -D arguments
277
278 The conditional text X in a %{S:X} or %{!S:X} construct may contain
279 other nested % constructs or spaces, or even newlines.  They are
280 processed as usual, as described above.
281
282 The character | is used to indicate that a command should be piped to
283 the following command, but only if -pipe is specified.
284
285 Note that it is built into CC which switches take arguments and which
286 do not.  You might think it would be useful to generalize this to
287 allow each compiler's spec to say which switches take arguments.  But
288 this cannot be done in a consistent fashion.  CC cannot even decide
289 which input files have been specified without knowing which switches
290 take arguments, and it must know which input files to compile in order
291 to tell which compilers to run.
292
293 CC also knows implicitly that arguments starting in `-l' are to be
294 treated as compiler output files, and passed to the linker in their
295 proper position among the other output files.  */
296 \f
297 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1.  */
298
299 /* config.h can define ASM_SPEC to provide extra args to the assembler
300    or extra switch-translations.  */
301 #ifndef ASM_SPEC
302 #define ASM_SPEC ""
303 #endif
304
305 /* config.h can define ASM_FINAL_SPEC to run a post processor after
306    the assembler has run.  */
307 #ifndef ASM_FINAL_SPEC
308 #define ASM_FINAL_SPEC ""
309 #endif
310
311 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
312    or extra switch-translations.  */
313 #ifndef CPP_SPEC
314 #define CPP_SPEC ""
315 #endif
316
317 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
318    or extra switch-translations.  */
319 #ifndef CC1_SPEC
320 #define CC1_SPEC ""
321 #endif
322
323 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
324    or extra switch-translations.  */
325 #ifndef CC1PLUS_SPEC
326 #define CC1PLUS_SPEC ""
327 #endif
328
329 /* config.h can define LINK_SPEC to provide extra args to the linker
330    or extra switch-translations.  */
331 #ifndef LINK_SPEC
332 #define LINK_SPEC ""
333 #endif
334
335 /* config.h can define LIB_SPEC to override the default libraries.  */
336 #ifndef LIB_SPEC
337 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
338 #endif
339
340 /* config.h can define STARTFILE_SPEC to override the default crt0 files.  */
341 #ifndef STARTFILE_SPEC
342 #define STARTFILE_SPEC  \
343   "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
344 #endif
345
346 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
347    Make the string nonempty to require spaces there.  */
348 #ifndef SWITCHES_NEED_SPACES
349 #define SWITCHES_NEED_SPACES ""
350 #endif
351
352 /* config.h can define ENDFILE_SPEC to override the default crtn files.  */
353 #ifndef ENDFILE_SPEC
354 #define ENDFILE_SPEC ""
355 #endif
356
357 /* This spec is used for telling cpp whether char is signed or not.  */
358 #ifndef SIGNED_CHAR_SPEC
359 /* Use #if rather than ?:
360    because MIPS C compiler rejects like ?: in initializers.  */
361 #if DEFAULT_SIGNED_CHAR
362 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
363 #else
364 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
365 #endif
366 #endif
367
368 static char *cpp_spec = CPP_SPEC;
369 static char *cpp_predefines = CPP_PREDEFINES;
370 static char *cc1_spec = CC1_SPEC;
371 static char *cc1plus_spec = CC1PLUS_SPEC;
372 static char *signed_char_spec = SIGNED_CHAR_SPEC;
373 static char *asm_spec = ASM_SPEC;
374 static char *asm_final_spec = ASM_FINAL_SPEC;
375 static char *link_spec = LINK_SPEC;
376 static char *lib_spec = LIB_SPEC;
377 static char *endfile_spec = ENDFILE_SPEC;
378 static char *startfile_spec = STARTFILE_SPEC;
379 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
380
381 /* This defines which switch letters take arguments.  */
382
383 #ifndef SWITCH_TAKES_ARG
384 #define SWITCH_TAKES_ARG(CHAR)      \
385   ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
386    || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
387    || (CHAR) == 'I' || (CHAR) == 'm' \
388    || (CHAR) == 'L' || (CHAR) == 'A')
389 #endif
390
391 /* This defines which multi-letter switches take arguments.  */
392
393 #ifndef WORD_SWITCH_TAKES_ARG
394 #define WORD_SWITCH_TAKES_ARG(STR)                      \
395  (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext")      \
396   || !strcmp (STR, "Tbss") || !strcmp (STR, "include")  \
397   || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
398 #endif
399 \f
400 /* Record the mapping from file suffixes for compilation specs.  */
401
402 struct compiler
403 {
404   char *suffix;                 /* Use this compiler for input files
405                                    whose names end in this suffix.  */
406
407   char *spec[4];                /* To use this compiler, concatenate these
408                                    specs and pass to do_spec.  */
409 };
410
411 /* Pointer to a vector of `struct compiler' that gives the spec for
412    compiling a file, based on its suffix.
413    A file that does not end in any of these suffixes will be passed
414    unchanged to the loader and nothing else will be done to it.
415
416    An entry containing two 0s is used to terminate the vector.
417
418    If multiple entries match a file, the last matching one is used.  */
419
420 static struct compiler *compilers;
421
422 /* Number of entries in `compilers', not counting the null terminator.  */
423
424 static int n_compilers;
425
426 /* The default list of file name suffixes and their compilation specs.  */
427
428 static struct compiler default_compilers[] =
429 {
430   {".c", "@c"},
431   {"@c",
432    "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
433         %{C:%{!E:%eGNU C does not support -C without using -E}}\
434         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
435         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
436         %{!undef:%{!ansi:%p} %P} %{trigraphs} \
437         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
438         %{traditional-cpp:-traditional}\
439         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
440         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
441    "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
442                    %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
443                    %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
444                    %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
445                    %{aux-info*}\
446                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
447                    %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
448               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
449                       %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
450                       %{!pipe:%g.s} %A\n }}}}"},
451   {"-",
452    "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
453         %{C:%{!E:%eGNU C does not support -C without using -E}}\
454         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
455         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
456         %{!undef:%{!ansi:%p} %P} %{trigraphs}\
457         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
458         %{traditional-cpp:-traditional}\
459         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
460         %i %W{o*}}\
461     %{!E:%e-E required when input is from standard input}"},
462   {".m", "@objective-c"},
463   {"@objective-c",
464    "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
465         %{C:%{!E:%eGNU C does not support -C without using -E}}\
466         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
467         -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
468         %{!undef:%{!ansi:%p} %P} %{trigraphs}\
469         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
470         %{traditional-cpp:-traditional}\
471         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
472         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
473    "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
474                    %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
475                    %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
476                    %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
477                    -lang-objc %{gen-decls} \
478                    %{aux-info*}\
479                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
480                    %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
481               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
482                       %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
483                       %{!pipe:%g.s} %A\n }}}}"},
484   {".h", "@c-header"},
485   {"@c-header",
486    "%{!E:%eCompilation of header file requested} \
487     cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
488         %{C:%{!E:%eGNU C does not support -C without using -E}}\
489          %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
490         -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
491         %{!undef:%{!ansi:%p} %P} %{trigraphs}\
492         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
493         %{traditional-cpp:-traditional}\
494         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
495         %i %W{o*}"},
496   {".cc", "@c++"},
497   {".cxx", "@c++"},
498   {".C", "@c++"},
499   {"@c++",
500    "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
501         %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
502         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
503         -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
504         %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
505         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
506         %{traditional-cpp:-traditional} %{trigraphs}\
507         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
508         %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
509    "%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
510                    %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
511                    %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
512                    %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
513                    %{aux-info*}\
514                    %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
515                    %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
516               %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
517                       %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
518                       %{!pipe:%g.s} %A\n }}}}"},
519   {".i", "@cpp-output"},
520   {"@cpp-output",
521    "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
522         %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
523         %{v:-version} %{pg:-p} %{p} %{f*}\
524         %{aux-info*}\
525         %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
526         %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
527     %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
528             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %{!pipe:%g.s} %A\n }"},
529   {".ii", "@c++-cpp-output"},
530   {"@c++-cpp-output",
531    "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
532             %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
533             %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
534             %{aux-info*}\
535             %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
536             %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
537        %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
538                %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
539                %{!pipe:%g.s} %A\n }"},
540   {".s", "@assembler"},
541   {"@assembler",
542    "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
543             %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %i %A\n }"},
544   {".S", "@assembler-with-cpp"},
545   {"@assembler-with-cpp",
546    "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
547         %{C:%{!E:%eGNU C does not support -C without using -E}}\
548         %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
549         -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
550         %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
551         %{traditional-cpp:-traditional}\
552         %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
553         %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
554    "%{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
555                     %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
556                     %{!pipe:%g.s} %A\n }}}}"},
557   /* Mark end of table */
558   {0, 0}
559 };
560
561 /* Number of elements in default_compilers, not counting the terminator.  */
562
563 static int n_default_compilers
564   = (sizeof default_compilers / sizeof (struct compiler)) - 1;
565
566 /* Here is the spec for running the linker, after compiling all files.  */
567
568 #ifdef LINK_LIBGCC_SPECIAL
569 /* Have gcc do the search for libgcc.a.  */
570 /* -u* was put back because both BSD and SysV seem to support it.  */
571 /* %{static:} simply prevents an error message if the target machine
572    doesn't handle -static.  */
573 static char *link_command_spec = "\
574 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
575                         %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
576                         %{!A:%{!nostdlib:%S}} %{static:}\
577                         %{L*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
578 #else
579 /* Use -L and have the linker do the search for -lgcc.  */
580 static char *link_command_spec = "\
581 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
582                         %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
583                         %{!A:%{!nostdlib:%S}} %{static:}\
584                         %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
585 #endif
586
587 /* A vector of options to give to the linker.
588    These options are accumulated by -Xlinker and -Wl,
589    and substituted into the linker command with %X.  */
590 static int n_linker_options;
591 static char **linker_options;
592
593 /* A vector of options to give to the assembler.
594    These options are accumulated by -Wa,
595    and substituted into the assembler command with %X.  */
596 static int n_assembler_options;
597 static char **assembler_options;
598 \f
599 /* Read compilation specs from a file named FILENAME,
600    replacing the default ones.
601
602    A suffix which starts with `*' is a definition for
603    one of the machine-specific sub-specs.  The "suffix" should be
604    *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
605    The corresponding spec is stored in asm_spec, etc.,
606    rather than in the `compilers' vector.
607
608    Anything invalid in the file is a fatal error.  */
609
610 static void
611 read_specs (filename)
612      char *filename;
613 {
614   int desc;
615   struct stat statbuf;
616   char *buffer;
617   register char *p;
618
619   if (verbose_flag)
620     fprintf (stderr, "Reading specs from %s\n", filename);
621
622   /* Open and stat the file.  */
623   desc = open (filename, 0, 0);
624   if (desc < 0)
625     pfatal_with_name (filename);
626   if (stat (filename, &statbuf) < 0)
627     pfatal_with_name (filename);
628
629   /* Read contents of file into BUFFER.  */
630   buffer = xmalloc ((unsigned) statbuf.st_size + 1);
631   read (desc, buffer, (unsigned) statbuf.st_size);
632   buffer[statbuf.st_size] = 0;
633   close (desc);
634
635   /* Scan BUFFER for specs, putting them in the vector.  */
636   p = buffer;
637   while (1)
638     {
639       char *suffix;
640       char *spec;
641       char *in, *out, *p1, *p2;
642
643       /* Advance P in BUFFER to the next nonblank nocomment line.  */
644       p = skip_whitespace (p);
645       if (*p == 0)
646         break;
647
648       /* Find the colon that should end the suffix.  */
649       p1 = p;
650       while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
651       /* The colon shouldn't be missing.  */
652       if (*p1 != ':')
653         fatal ("specs file malformed after %d characters", p1 - buffer);
654       /* Skip back over trailing whitespace.  */
655       p2 = p1;
656       while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
657       /* Copy the suffix to a string.  */
658       suffix = save_string (p, p2 - p);
659       /* Find the next line.  */
660       p = skip_whitespace (p1 + 1);
661       if (p[1] == 0)
662         fatal ("specs file malformed after %d characters", p - buffer);
663       p1 = p;
664       /* Find next blank line.  */
665       while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
666       /* Specs end at the blank line and do not include the newline.  */
667       spec = save_string (p, p1 - p);
668       p = p1;
669
670       /* Delete backslash-newline sequences from the spec.  */
671       in = spec;
672       out = spec;
673       while (*in != 0)
674         {
675           if (in[0] == '\\' && in[1] == '\n')
676             in += 2;
677           else if (in[0] == '#')
678             {
679               while (*in && *in != '\n') in++;
680             }
681           else
682             *out++ = *in++;
683         }
684       *out = 0;
685
686       if (suffix[0] == '*')
687         {
688           if (! strcmp (suffix, "*link_command"))
689             link_command_spec = spec;
690           else
691             set_spec (suffix + 1, spec);
692         }
693       else
694         {
695           /* Add this pair to the vector.  */
696           compilers
697             = ((struct compiler *)
698                xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
699           compilers[n_compilers].suffix = suffix;
700           bzero (compilers[n_compilers].spec,
701                  sizeof compilers[n_compilers].spec);
702           compilers[n_compilers].spec[0] = spec;
703           n_compilers++;
704         }
705
706       if (*suffix == 0)
707         link_command_spec = spec;
708     }
709
710   if (link_command_spec == 0)
711     fatal ("spec file has no spec for linking");
712 }
713
714 static char *
715 skip_whitespace (p)
716      char *p;
717 {
718   while (1)
719     {
720       /* A fully-blank line is a delimiter in the SPEC file and shouldn't
721          be considered whitespace.  */
722       if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
723         return p + 1;
724       else if (*p == '\n' || *p == ' ' || *p == '\t')
725         p++;
726       else if (*p == '#')
727         {
728           while (*p != '\n') p++;
729           p++;
730         }
731       else
732         break;
733     }
734
735   return p;
736 }
737 \f
738 /* Structure to keep track of the specs that have been defined so far.  These
739    are accessed using %(specname) or %[specname] in a compiler or link spec. */
740
741 struct spec_list
742 {
743   char *name;                 /* Name of the spec. */
744   char *spec;                 /* The spec itself. */
745   struct spec_list *next;     /* Next spec in linked list. */
746 };
747
748 /* List of specs that have been defined so far. */
749
750 static struct spec_list *specs = (struct spec_list *) 0;
751 \f
752 /* Change the value of spec NAME to SPEC.  If SPEC is empty, then the spec is
753    removed; If the spec starts with a + then SPEC is added to the end of the
754    current spec. */
755
756 static void
757 set_spec (name, spec)
758      char *name;
759      char *spec;
760 {
761   struct spec_list *sl;
762   char *old_spec;
763
764   /* See if the spec already exists */
765   for (sl = specs; sl; sl = sl->next)
766     if (strcmp (sl->name, name) == 0)
767       break;
768
769   if (!sl)
770     {
771       /* Not found - make it */
772       sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
773       sl->name = save_string (name, strlen (name));
774       sl->spec = save_string ("", 0);
775       sl->next = specs;
776       specs = sl;
777     }
778
779   old_spec = sl->spec;
780   if (name && spec[0] == '+' && isspace (spec[1]))
781     sl->spec = concat (old_spec, spec + 1, "");
782   else
783     sl->spec = save_string (spec, strlen (spec));
784
785   if (! strcmp (name, "asm"))
786     asm_spec = sl->spec;
787   else if (! strcmp (name, "asm_final"))
788     asm_final_spec = sl->spec;
789   else if (! strcmp (name, "cc1"))
790     cc1_spec = sl->spec;
791   else if (! strcmp (name, "cc1plus"))
792     cc1plus_spec = sl->spec;
793   else if (! strcmp (name, "cpp"))
794     cpp_spec = sl->spec;
795   else if (! strcmp (name, "endfile"))
796     endfile_spec = sl->spec;
797   else if (! strcmp (name, "lib"))
798     lib_spec = sl->spec;
799   else if (! strcmp (name, "link"))
800     link_spec = sl->spec;
801   else if (! strcmp (name, "predefines"))
802     cpp_predefines = sl->spec;
803   else if (! strcmp (name, "signed_char"))
804     signed_char_spec = sl->spec;
805   else if (! strcmp (name, "startfile"))
806     startfile_spec = sl->spec;
807   else if (! strcmp (name, "switches_need_spaces"))
808     switches_need_spaces = sl->spec;
809   else if (! strcmp (name, "cross_compile"))
810     cross_compile = atoi (sl->spec);
811   /* Free the old spec */
812   if (old_spec)
813     free (old_spec);
814 }
815 \f
816 /* Accumulate a command (program name and args), and run it.  */
817
818 /* Vector of pointers to arguments in the current line of specifications.  */
819
820 static char **argbuf;
821
822 /* Number of elements allocated in argbuf.  */
823
824 static int argbuf_length;
825
826 /* Number of elements in argbuf currently in use (containing args).  */
827
828 static int argbuf_index;
829
830 /* Number of commands executed so far.  */
831
832 static int execution_count;
833
834 /* Number of commands that exited with a signal.  */
835
836 static int signal_count;
837
838 /* Name with which this program was invoked.  */
839
840 static char *programname;
841 \f
842 /* Structures to keep track of prefixes to try when looking for files. */
843
844 struct prefix_list
845 {
846   char *prefix;               /* String to prepend to the path. */
847   struct prefix_list *next;   /* Next in linked list. */
848   int require_machine_suffix; /* Don't use without machine_suffix.  */
849   /* 2 means try both machine_suffix and just_machine_suffix.  */
850   int *used_flag_ptr;         /* 1 if a file was found with this prefix.  */
851 };
852
853 struct path_prefix
854 {
855   struct prefix_list *plist;  /* List of prefixes to try */
856   int max_len;                /* Max length of a prefix in PLIST */
857   char *name;                 /* Name of this list (used in config stuff) */
858 };
859
860 /* List of prefixes to try when looking for executables. */
861
862 static struct path_prefix exec_prefix = { 0, 0, "exec" };
863
864 /* List of prefixes to try when looking for startup (crt0) files. */
865
866 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
867
868 /* List of prefixes to try when looking for libraries. */
869
870 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
871
872 /* Suffix to attach to directories searched for commands.
873    This looks like `MACHINE/VERSION/'.  */
874
875 static char *machine_suffix = 0;
876
877 /* Suffix to attach to directories searched for commands.
878    This is just `MACHINE/'.  */
879
880 static char *just_machine_suffix = 0;
881
882 /* Adjusted value of GCC_EXEC_PREFIX envvar.  */
883
884 static char *gcc_exec_prefix;
885
886 /* Default prefixes to attach to command names.  */
887
888 #ifdef CROSS_COMPILE  /* Don't use these prefixes for a cross compiler.  */
889 #undef MD_EXEC_PREFIX
890 #undef MD_STARTFILE_PREFIX
891 #undef MD_STARTFILE_PREFIX_1
892 #endif
893
894 #ifndef STANDARD_EXEC_PREFIX
895 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
896 #endif /* !defined STANDARD_EXEC_PREFIX */
897
898 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
899 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
900 #ifdef MD_EXEC_PREFIX
901 static char *md_exec_prefix = MD_EXEC_PREFIX;
902 #endif
903
904 #ifndef STANDARD_STARTFILE_PREFIX
905 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
906 #endif /* !defined STANDARD_STARTFILE_PREFIX */
907
908 #ifdef MD_STARTFILE_PREFIX
909 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
910 #endif
911 #ifdef MD_STARTFILE_PREFIX_1
912 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
913 #endif
914 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
915 static char *standard_startfile_prefix_1 = "/lib/";
916 static char *standard_startfile_prefix_2 = "/usr/lib/";
917
918 /* Clear out the vector of arguments (after a command is executed).  */
919
920 static void
921 clear_args ()
922 {
923   argbuf_index = 0;
924 }
925
926 /* Add one argument to the vector at the end.
927    This is done when a space is seen or at the end of the line.
928    If DELETE_ALWAYS is nonzero, the arg is a filename
929     and the file should be deleted eventually.
930    If DELETE_FAILURE is nonzero, the arg is a filename
931     and the file should be deleted if this compilation fails.  */
932
933 static void
934 store_arg (arg, delete_always, delete_failure)
935      char *arg;
936      int delete_always, delete_failure;
937 {
938   if (argbuf_index + 1 == argbuf_length)
939     {
940       argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
941     }
942
943   argbuf[argbuf_index++] = arg;
944   argbuf[argbuf_index] = 0;
945
946   if (delete_always || delete_failure)
947     record_temp_file (arg, delete_always, delete_failure);
948 }
949 \f
950 /* Record the names of temporary files we tell compilers to write,
951    and delete them at the end of the run.  */
952
953 /* This is the common prefix we use to make temp file names.
954    It is chosen once for each run of this program.
955    It is substituted into a spec by %g.
956    Thus, all temp file names contain this prefix.
957    In practice, all temp file names start with this prefix.
958
959    This prefix comes from the envvar TMPDIR if it is defined;
960    otherwise, from the P_tmpdir macro if that is defined;
961    otherwise, in /usr/tmp or /tmp.  */
962
963 static char *temp_filename;
964
965 /* Length of the prefix.  */
966
967 static int temp_filename_length;
968
969 /* Define the list of temporary files to delete.  */
970
971 struct temp_file
972 {
973   char *name;
974   struct temp_file *next;
975 };
976
977 /* Queue of files to delete on success or failure of compilation.  */
978 static struct temp_file *always_delete_queue;
979 /* Queue of files to delete on failure of compilation.  */
980 static struct temp_file *failure_delete_queue;
981
982 /* Record FILENAME as a file to be deleted automatically.
983    ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
984    otherwise delete it in any case.
985    FAIL_DELETE nonzero means delete it if a compilation step fails;
986    otherwise delete it in any case.  */
987
988 static void
989 record_temp_file (filename, always_delete, fail_delete)
990      char *filename;
991      int always_delete;
992      int fail_delete;
993 {
994   register char *name;
995   name = xmalloc (strlen (filename) + 1);
996   strcpy (name, filename);
997
998   if (always_delete)
999     {
1000       register struct temp_file *temp;
1001       for (temp = always_delete_queue; temp; temp = temp->next)
1002         if (! strcmp (name, temp->name))
1003           goto already1;
1004       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1005       temp->next = always_delete_queue;
1006       temp->name = name;
1007       always_delete_queue = temp;
1008     already1:;
1009     }
1010
1011   if (fail_delete)
1012     {
1013       register struct temp_file *temp;
1014       for (temp = failure_delete_queue; temp; temp = temp->next)
1015         if (! strcmp (name, temp->name))
1016           goto already2;
1017       temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1018       temp->next = failure_delete_queue;
1019       temp->name = name;
1020       failure_delete_queue = temp;
1021     already2:;
1022     }
1023 }
1024
1025 /* Delete all the temporary files whose names we previously recorded.  */
1026
1027 static void
1028 delete_temp_files ()
1029 {
1030   register struct temp_file *temp;
1031
1032   for (temp = always_delete_queue; temp; temp = temp->next)
1033     {
1034 #ifdef DEBUG
1035       int i;
1036       printf ("Delete %s? (y or n) ", temp->name);
1037       fflush (stdout);
1038       i = getchar ();
1039       if (i != '\n')
1040         while (getchar () != '\n') ;
1041       if (i == 'y' || i == 'Y')
1042 #endif /* DEBUG */
1043         {
1044           struct stat st;
1045           if (stat (temp->name, &st) >= 0)
1046             {
1047               /* Delete only ordinary files.  */
1048               if (S_ISREG (st.st_mode))
1049                 if (unlink (temp->name) < 0)
1050                   if (verbose_flag)
1051                     perror_with_name (temp->name);
1052             }
1053         }
1054     }
1055
1056   always_delete_queue = 0;
1057 }
1058
1059 /* Delete all the files to be deleted on error.  */
1060
1061 static void
1062 delete_failure_queue ()
1063 {
1064   register struct temp_file *temp;
1065
1066   for (temp = failure_delete_queue; temp; temp = temp->next)
1067     {
1068 #ifdef DEBUG
1069       int i;
1070       printf ("Delete %s? (y or n) ", temp->name);
1071       fflush (stdout);
1072       i = getchar ();
1073       if (i != '\n')
1074         while (getchar () != '\n') ;
1075       if (i == 'y' || i == 'Y')
1076 #endif /* DEBUG */
1077         {
1078           if (unlink (temp->name) < 0)
1079             if (verbose_flag)
1080               perror_with_name (temp->name);
1081         }
1082     }
1083 }
1084
1085 static void
1086 clear_failure_queue ()
1087 {
1088   failure_delete_queue = 0;
1089 }
1090
1091 /* Compute a string to use as the base of all temporary file names.
1092    It is substituted for %g.  */
1093
1094 static void
1095 choose_temp_base ()
1096 {
1097   char *base = getenv ("TMPDIR");
1098   int len;
1099
1100   if (base == (char *)0)
1101     {
1102 #ifdef P_tmpdir
1103       if (access (P_tmpdir, R_OK | W_OK) == 0)
1104         base = P_tmpdir;
1105 #endif
1106       if (base == (char *)0)
1107         {
1108           if (access ("/usr/tmp", R_OK | W_OK) == 0)
1109             base = "/usr/tmp/";
1110           else
1111             base = "/tmp/";
1112         }
1113     }
1114
1115   len = strlen (base);
1116   temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1117   strcpy (temp_filename, base);
1118   if (len > 0 && temp_filename[len-1] != '/')
1119     temp_filename[len++] = '/';
1120   strcpy (temp_filename + len, "ccXXXXXX");
1121
1122   mktemp (temp_filename);
1123   temp_filename_length = strlen (temp_filename);
1124 }
1125 \f
1126
1127 /* Routine to add variables to the environment.  We do this to pass
1128    the pathname of the gcc driver, and the directories search to the
1129    collect2 program, which is being run as ld.  This way, we can be
1130    sure of executing the right compiler when collect2 wants to build
1131    constructors and destructors.  Since the environment variables we
1132    use come from an obstack, we don't have to worry about allocating
1133    space for them.  */
1134
1135 #ifndef HAVE_PUTENV
1136
1137 putenv (str)
1138      char *str;
1139 {
1140 #ifndef VMS                     /* nor about VMS */
1141
1142   extern char **environ;
1143   char **old_environ = environ;
1144   char **envp;
1145   int num_envs = 0;
1146   int name_len = 1;
1147   int str_len = strlen (str);
1148   char *p = str;
1149   int ch;
1150
1151   while ((ch = *p++) != '\0' && ch != '=')
1152     name_len++;
1153
1154   if (!ch)
1155     abort ();
1156
1157   /* Search for replacing an existing environment variable, and
1158      count the number of total environment variables.  */
1159   for (envp = old_environ; *envp; envp++)
1160     {
1161       num_envs++;
1162       if (!strncmp (str, *envp, name_len))
1163         {
1164           *envp = str;
1165           return;
1166         }
1167     }
1168
1169   /* Add a new environment variable */
1170   environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1171   *environ = str;
1172   bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1173
1174 #endif  /* VMS */
1175 }
1176
1177 #endif  /* HAVE_PUTENV */
1178
1179 \f
1180 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect.  */
1181
1182 static void
1183 putenv_from_prefixes (paths, env_var)
1184      struct path_prefix *paths;
1185      char *env_var;
1186 {
1187   int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1188   int first_time = TRUE;
1189   struct prefix_list *pprefix;
1190
1191   obstack_grow (&collect_obstack, env_var, strlen (env_var));
1192
1193   for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1194     {
1195       int len = strlen (pprefix->prefix);
1196
1197       if (machine_suffix)
1198         {
1199           if (!first_time)
1200             obstack_grow (&collect_obstack, ":", 1);
1201             
1202           first_time = FALSE;
1203           obstack_grow (&collect_obstack, pprefix->prefix, len);
1204           obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1205         }
1206
1207       if (just_machine_suffix && pprefix->require_machine_suffix == 2)
1208         {
1209           if (!first_time)
1210             obstack_grow (&collect_obstack, ":", 1);
1211             
1212           first_time = FALSE;
1213           obstack_grow (&collect_obstack, pprefix->prefix, len);
1214           obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1215         }
1216
1217       if (!pprefix->require_machine_suffix)
1218         {
1219           if (!first_time)
1220             obstack_grow (&collect_obstack, ":", 1);
1221
1222           first_time = FALSE;
1223           obstack_grow (&collect_obstack, pprefix->prefix, len);
1224         }
1225     }
1226   obstack_grow (&collect_obstack, "\0", 1);
1227   putenv (obstack_finish (&collect_obstack));
1228 }
1229
1230 \f
1231 /* Search for NAME using the prefix list PREFIXES.  MODE is passed to
1232    access to check permissions.
1233    Return 0 if not found, otherwise return its name, allocated with malloc. */
1234
1235 static char *
1236 find_a_file (pprefix, name, mode)
1237      struct path_prefix *pprefix;
1238      char *name;
1239      int mode;
1240 {
1241   char *temp;
1242   char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1243   struct prefix_list *pl;
1244   int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1245
1246   if (machine_suffix)
1247     len += strlen (machine_suffix);
1248
1249   temp = xmalloc (len);
1250
1251   /* Determine the filename to execute (special case for absolute paths).  */
1252
1253   if (*name == '/')
1254     {
1255       if (access (name, mode))
1256         {
1257           strcpy (temp, name);
1258           return temp;
1259         }
1260     }
1261   else
1262     for (pl = pprefix->plist; pl; pl = pl->next)
1263       {
1264         if (machine_suffix)
1265           {
1266             strcpy (temp, pl->prefix);
1267             strcat (temp, machine_suffix);
1268             strcat (temp, name);
1269             if (access (temp, mode) == 0)
1270               {
1271                 if (pl->used_flag_ptr != 0)
1272                   *pl->used_flag_ptr = 1;
1273                 return temp;
1274               }
1275             /* Some systems have a suffix for executable files.
1276                So try appending that.  */
1277             if (file_suffix[0] != 0)
1278               {
1279                 strcat (temp, file_suffix);
1280                 if (access (temp, mode) == 0)
1281                   {
1282                     if (pl->used_flag_ptr != 0)
1283                       *pl->used_flag_ptr = 1;
1284                     return temp;
1285                   }
1286               }
1287           }
1288         /* Certain prefixes are tried with just the machine type,
1289            not the version.  This is used for finding as, ld, etc.  */
1290         if (just_machine_suffix && pl->require_machine_suffix == 2)
1291           {
1292             strcpy (temp, pl->prefix);
1293             strcat (temp, just_machine_suffix);
1294             strcat (temp, name);
1295             if (access (temp, mode) == 0)
1296               {
1297                 if (pl->used_flag_ptr != 0)
1298                   *pl->used_flag_ptr = 1;
1299                 return temp;
1300               }
1301             /* Some systems have a suffix for executable files.
1302                So try appending that.  */
1303             if (file_suffix[0] != 0)
1304               {
1305                 strcat (temp, file_suffix);
1306                 if (access (temp, mode) == 0)
1307                   {
1308                     if (pl->used_flag_ptr != 0)
1309                       *pl->used_flag_ptr = 1;
1310                     return temp;
1311                   }
1312               }
1313           }
1314         /* Certain prefixes can't be used without the machine suffix
1315            when the machine or version is explicitly specified.  */
1316         if (!pl->require_machine_suffix)
1317           {
1318             strcpy (temp, pl->prefix);
1319             strcat (temp, name);
1320             if (access (temp, mode) == 0)
1321               {
1322                 if (pl->used_flag_ptr != 0)
1323                   *pl->used_flag_ptr = 1;
1324                 return temp;
1325               }
1326             /* Some systems have a suffix for executable files.
1327                So try appending that.  */
1328             if (file_suffix[0] != 0)
1329               {
1330                 strcat (temp, file_suffix);
1331                 if (access (temp, mode) == 0)
1332                   {
1333                     if (pl->used_flag_ptr != 0)
1334                       *pl->used_flag_ptr = 1;
1335                     return temp;
1336                   }
1337               }
1338           }
1339       }
1340
1341   free (temp);
1342   return 0;
1343 }
1344
1345 /* Add an entry for PREFIX in PLIST.  If FIRST is set, it goes
1346    at the start of the list, otherwise it goes at the end.
1347
1348    If WARN is nonzero, we will warn if no file is found
1349    through this prefix.  WARN should point to an int
1350    which will be set to 1 if this entry is used.
1351
1352    REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1353    the complete value of machine_suffix.
1354    2 means try both machine_suffix and just_machine_suffix.  */
1355
1356 static void
1357 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1358      struct path_prefix *pprefix;
1359      char *prefix;
1360      int first;
1361      int require_machine_suffix;
1362      int *warn;
1363 {
1364   struct prefix_list *pl, **prev;
1365   int len;
1366
1367   if (!first && pprefix->plist)
1368     {
1369       for (pl = pprefix->plist; pl->next; pl = pl->next)
1370         ;
1371       prev = &pl->next;
1372     }
1373   else
1374     prev = &pprefix->plist;
1375
1376   /* Keep track of the longest prefix */
1377
1378   len = strlen (prefix);
1379   if (len > pprefix->max_len)
1380     pprefix->max_len = len;
1381
1382   pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1383   pl->prefix = save_string (prefix, len);
1384   pl->require_machine_suffix = require_machine_suffix;
1385   pl->used_flag_ptr = warn;
1386   if (warn)
1387     *warn = 0;
1388
1389   if (*prev)
1390     pl->next = *prev;
1391   else
1392     pl->next = (struct prefix_list *) 0;
1393   *prev = pl;
1394 }
1395
1396 /* Print warnings for any prefixes in the list PPREFIX that were not used.  */
1397
1398 static void
1399 unused_prefix_warnings (pprefix)
1400      struct path_prefix *pprefix;
1401 {
1402   struct prefix_list *pl = pprefix->plist;
1403
1404   while (pl)
1405     {
1406       if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1407         {
1408           error ("file path prefix `%s' never used",
1409                  pl->prefix);
1410           /* Prevent duplicate warnings.  */
1411           *pl->used_flag_ptr = 1;
1412         }
1413       pl = pl->next;
1414     }
1415 }
1416
1417 /* Get rid of all prefixes built up so far in *PLISTP. */
1418
1419 static void
1420 free_path_prefix (pprefix)
1421      struct path_prefix *pprefix;
1422 {
1423   struct prefix_list *pl = pprefix->plist;
1424   struct prefix_list *temp;
1425
1426   while (pl)
1427     {
1428       temp = pl;
1429       pl = pl->next;
1430       free (temp->prefix);
1431       free ((char *) temp);
1432     }
1433   pprefix->plist = (struct prefix_list *) 0;
1434 }
1435 \f
1436 /* stdin file number.  */
1437 #define STDIN_FILE_NO 0
1438
1439 /* stdout file number.  */
1440 #define STDOUT_FILE_NO 1
1441
1442 /* value of `pipe': port index for reading.  */
1443 #define READ_PORT 0
1444
1445 /* value of `pipe': port index for writing.  */
1446 #define WRITE_PORT 1
1447
1448 /* Pipe waiting from last process, to be used as input for the next one.
1449    Value is STDIN_FILE_NO if no pipe is waiting
1450    (i.e. the next command is the first of a group).  */
1451
1452 static int last_pipe_input;
1453
1454 /* Fork one piped subcommand.  FUNC is the system call to use
1455    (either execv or execvp).  ARGV is the arg vector to use.
1456    NOT_LAST is nonzero if this is not the last subcommand
1457    (i.e. its output should be piped to the next one.)  */
1458
1459 #ifndef OS2
1460 #ifdef __MSDOS__
1461
1462 /* Declare these to avoid compilation error.  They won't be called.  */
1463 int execv(const char *a, const char **b){}
1464 int execvp(const char *a, const char **b){}
1465
1466 static int
1467 pexecute (search_flag, program, argv, not_last)
1468      int search_flag;
1469      char *program;
1470      char *argv[];
1471      int not_last;
1472 {
1473   char *scmd;
1474   FILE *argfile;
1475   int i;
1476
1477   scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
1478   sprintf (scmd, "%s @%s.gp", program, temp_filename);
1479   argfile = fopen (scmd+strlen (program) + 2, "w");
1480   if (argfile == 0)
1481     pfatal_with_name (scmd + strlen (program) + 2);
1482
1483   for (i=1; argv[i]; i++)
1484   {
1485     char *cp;
1486     for (cp = argv[i]; *cp; cp++)
1487       {
1488         if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1489           fputc ('\\', argfile);
1490         fputc (*cp, argfile);
1491       }
1492     fputc ('\n', argfile);
1493   }
1494   fclose (argfile);
1495
1496   i = system (scmd);
1497
1498   remove (scmd + strlen (program) + 2);
1499   return i << 8;
1500 }
1501
1502 #else /* not __MSDOS__ */
1503
1504 static int
1505 pexecute (search_flag, program, argv, not_last)
1506      int search_flag;
1507      char *program;
1508      char *argv[];
1509      int not_last;
1510 {
1511   int (*func)() = (search_flag ? execv : execvp);
1512   int pid;
1513   int pdes[2];
1514   int input_desc = last_pipe_input;
1515   int output_desc = STDOUT_FILE_NO;
1516   int retries, sleep_interval;
1517
1518   /* If this isn't the last process, make a pipe for its output,
1519      and record it as waiting to be the input to the next process.  */
1520
1521   if (not_last)
1522     {
1523       if (pipe (pdes) < 0)
1524         pfatal_with_name ("pipe");
1525       output_desc = pdes[WRITE_PORT];
1526       last_pipe_input = pdes[READ_PORT];
1527     }
1528   else
1529     last_pipe_input = STDIN_FILE_NO;
1530
1531   /* Fork a subprocess; wait and retry if it fails.  */
1532   sleep_interval = 1;
1533   for (retries = 0; retries < 4; retries++)
1534     {
1535       pid = vfork ();
1536       if (pid >= 0)
1537         break;
1538       sleep (sleep_interval);
1539       sleep_interval *= 2;
1540     }
1541
1542   switch (pid)
1543     {
1544     case -1:
1545 #ifdef vfork
1546       pfatal_with_name ("fork");
1547 #else
1548       pfatal_with_name ("vfork");
1549 #endif
1550       /* NOTREACHED */
1551       return 0;
1552
1553     case 0: /* child */
1554       /* Move the input and output pipes into place, if nec.  */
1555       if (input_desc != STDIN_FILE_NO)
1556         {
1557           close (STDIN_FILE_NO);
1558           dup (input_desc);
1559           close (input_desc);
1560         }
1561       if (output_desc != STDOUT_FILE_NO)
1562         {
1563           close (STDOUT_FILE_NO);
1564           dup (output_desc);
1565           close (output_desc);
1566         }
1567
1568       /* Close the parent's descs that aren't wanted here.  */
1569       if (last_pipe_input != STDIN_FILE_NO)
1570         close (last_pipe_input);
1571
1572       /* Exec the program.  */
1573       (*func) (program, argv);
1574       perror_exec (program);
1575       exit (-1);
1576       /* NOTREACHED */
1577       return 0;
1578
1579     default:
1580       /* In the parent, after forking.
1581          Close the descriptors that we made for this child.  */
1582       if (input_desc != STDIN_FILE_NO)
1583         close (input_desc);
1584       if (output_desc != STDOUT_FILE_NO)
1585         close (output_desc);
1586
1587       /* Return child's process number.  */
1588       return pid;
1589     }
1590 }
1591
1592 #endif /* not __MSDOS__ */
1593 #else /* not OS2 */
1594
1595 static int
1596 pexecute (search_flag, program, argv, not_last)
1597      int search_flag;
1598      char *program;
1599      char *argv[];
1600      int not_last;
1601 {
1602   return (search_flag ? spawnv : spawnvp) (1, program, argv);
1603 }
1604 #endif /* not OS2 */
1605 \f
1606 /* Execute the command specified by the arguments on the current line of spec.
1607    When using pipes, this includes several piped-together commands
1608    with `|' between them.
1609
1610    Return 0 if successful, -1 if failed.  */
1611
1612 static int
1613 execute ()
1614 {
1615   int i;
1616   int n_commands;               /* # of command.  */
1617   char *string;
1618   struct command
1619     {
1620       char *prog;               /* program name.  */
1621       char **argv;              /* vector of args.  */
1622       int pid;                  /* pid of process for this command.  */
1623     };
1624
1625   struct command *commands;     /* each command buffer with above info.  */
1626
1627   /* Count # of piped commands.  */
1628   for (n_commands = 1, i = 0; i < argbuf_index; i++)
1629     if (strcmp (argbuf[i], "|") == 0)
1630       n_commands++;
1631
1632   /* Get storage for each command.  */
1633   commands
1634     = (struct command *) alloca (n_commands * sizeof (struct command));
1635
1636   /* Split argbuf into its separate piped processes,
1637      and record info about each one.
1638      Also search for the programs that are to be run.  */
1639
1640   commands[0].prog = argbuf[0]; /* first command.  */
1641   commands[0].argv = &argbuf[0];
1642   string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1643   if (string)
1644     commands[0].argv[0] = string;
1645
1646   for (n_commands = 1, i = 0; i < argbuf_index; i++)
1647     if (strcmp (argbuf[i], "|") == 0)
1648       {                         /* each command.  */
1649 #ifdef __MSDOS__
1650         fatal ("-pipe not supported under MS-DOS");
1651 #endif
1652         argbuf[i] = 0;  /* termination of command args.  */
1653         commands[n_commands].prog = argbuf[i + 1];
1654         commands[n_commands].argv = &argbuf[i + 1];
1655         string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1656         if (string)
1657           commands[n_commands].argv[0] = string;
1658         n_commands++;
1659       }
1660
1661   argbuf[argbuf_index] = 0;
1662
1663   /* If -v, print what we are about to do, and maybe query.  */
1664
1665   if (verbose_flag)
1666     {
1667       /* Print each piped command as a separate line.  */
1668       for (i = 0; i < n_commands ; i++)
1669         {
1670           char **j;
1671
1672           for (j = commands[i].argv; *j; j++)
1673             fprintf (stderr, " %s", *j);
1674
1675           /* Print a pipe symbol after all but the last command.  */
1676           if (i + 1 != n_commands)
1677             fprintf (stderr, " |");
1678           fprintf (stderr, "\n");
1679         }
1680       fflush (stderr);
1681 #ifdef DEBUG
1682       fprintf (stderr, "\nGo ahead? (y or n) ");
1683       fflush (stderr);
1684       i = getchar ();
1685       if (i != '\n')
1686         while (getchar () != '\n') ;
1687       if (i != 'y' && i != 'Y')
1688         return 0;
1689 #endif /* DEBUG */
1690     }
1691
1692   /* Run each piped subprocess.  */
1693
1694   last_pipe_input = STDIN_FILE_NO;
1695   for (i = 0; i < n_commands; i++)
1696     {
1697       char *string = commands[i].argv[0];
1698
1699       commands[i].pid = pexecute (string != commands[i].prog,
1700                                   string, commands[i].argv,
1701                                   i + 1 < n_commands);
1702
1703       if (string != commands[i].prog)
1704         free (string);
1705     }
1706
1707   execution_count++;
1708
1709   /* Wait for all the subprocesses to finish.
1710      We don't care what order they finish in;
1711      we know that N_COMMANDS waits will get them all.  */
1712
1713   {
1714     int ret_code = 0;
1715
1716     for (i = 0; i < n_commands; i++)
1717       {
1718         int status;
1719         int pid;
1720         char *prog;
1721
1722 #ifdef __MSDOS__
1723         status = pid = commands[i].pid;
1724 #else
1725         pid = wait (&status);
1726 #endif
1727         if (pid < 0)
1728           abort ();
1729
1730         if (status != 0)
1731           {
1732             int j;
1733             for (j = 0; j < n_commands; j++)
1734               if (commands[j].pid == pid)
1735                 prog = commands[j].prog;
1736
1737             if ((status & 0x7F) != 0)
1738               {
1739                 fatal ("Internal compiler error: program %s got fatal signal %d",
1740                        prog, (status & 0x7F));
1741                 signal_count++;
1742               }
1743             if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1744               ret_code = -1;
1745           }
1746       }
1747     return ret_code;
1748   }
1749 }
1750 \f
1751 /* Find all the switches given to us
1752    and make a vector describing them.
1753    The elements of the vector are strings, one per switch given.
1754    If a switch uses following arguments, then the `part1' field
1755    is the switch itself and the `args' field
1756    is a null-terminated vector containing the following arguments.
1757    The `valid' field is nonzero if any spec has looked at this switch;
1758    if it remains zero at the end of the run, it must be meaningless.  */
1759
1760 struct switchstr
1761 {
1762   char *part1;
1763   char **args;
1764   int valid;
1765 };
1766
1767 static struct switchstr *switches;
1768
1769 static int n_switches;
1770
1771 struct infile
1772 {
1773   char *name;
1774   char *language;
1775 };
1776
1777 /* Also a vector of input files specified.  */
1778
1779 static struct infile *infiles;
1780
1781 static int n_infiles;
1782
1783 /* And a vector of corresponding output files is made up later.  */
1784
1785 static char **outfiles;
1786
1787 /* Create the vector `switches' and its contents.
1788    Store its length in `n_switches'.  */
1789
1790 static void
1791 process_command (argc, argv)
1792      int argc;
1793      char **argv;
1794 {
1795   register int i;
1796   char *temp;
1797   char *spec_lang = 0;
1798   int last_language_n_infiles;
1799
1800   gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
1801
1802   n_switches = 0;
1803   n_infiles = 0;
1804
1805   /* Default for -V is our version number, ending at first space.  */
1806   spec_version = save_string (version_string, strlen (version_string));
1807   for (temp = spec_version; *temp && *temp != ' '; temp++);
1808   if (*temp) *temp = '\0';
1809
1810   /* Set up the default search paths.  */
1811
1812   if (gcc_exec_prefix)
1813     {
1814       add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1815       add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1816     }
1817
1818   /* COMPILER_PATH and LIBRARY_PATH have values
1819      that are lists of directory names with colons.  */
1820
1821   temp = getenv ("COMPILER_PATH");
1822   if (temp)
1823     {
1824       char *startp, *endp;
1825       char *nstore = (char *) alloca (strlen (temp) + 3);
1826
1827       startp = endp = temp;
1828       while (1)
1829         {
1830           if (*endp == PATH_SEPARATOR || *endp == 0)
1831             {
1832               strncpy (nstore, startp, endp-startp);
1833               if (endp == startp)
1834                 {
1835                   strcpy (nstore, "./");
1836                 }
1837               else if (endp[-1] != '/')
1838                 {
1839                   nstore[endp-startp] = '/';
1840                   nstore[endp-startp+1] = 0;
1841                 }
1842               else
1843                 nstore[endp-startp] = 0;
1844               add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
1845               if (*endp == 0)
1846                 break;
1847               endp = startp = endp + 1;
1848             }
1849           else
1850             endp++;
1851         }
1852     }
1853
1854   temp = getenv ("LIBRARY_PATH");
1855   if (temp)
1856     {
1857       char *startp, *endp;
1858       char *nstore = (char *) alloca (strlen (temp) + 3);
1859
1860       startp = endp = temp;
1861       while (1)
1862         {
1863           if (*endp == PATH_SEPARATOR || *endp == 0)
1864             {
1865               strncpy (nstore, startp, endp-startp);
1866               if (endp == startp)
1867                 {
1868                   strcpy (nstore, "./");
1869                 }
1870               else if (endp[-1] != '/')
1871                 {
1872                   nstore[endp-startp] = '/';
1873                   nstore[endp-startp+1] = 0;
1874                 }
1875               else
1876                 nstore[endp-startp] = 0;
1877               add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1878               /* Make separate list of dirs that came from LIBRARY_PATH.  */
1879               add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1880               if (*endp == 0)
1881                 break;
1882               endp = startp = endp + 1;
1883             }
1884           else
1885             endp++;
1886         }
1887     }
1888
1889   /* Use LPATH like LIBRARY_PATH (for the CMU build program).  */
1890   temp = getenv ("LPATH");
1891   if (temp)
1892     {
1893       char *startp, *endp;
1894       char *nstore = (char *) alloca (strlen (temp) + 3);
1895
1896       startp = endp = temp;
1897       while (1)
1898         {
1899           if (*endp == PATH_SEPARATOR || *endp == 0)
1900             {
1901               strncpy (nstore, startp, endp-startp);
1902               if (endp == startp)
1903                 {
1904                   strcpy (nstore, "./");
1905                 }
1906               else if (endp[-1] != '/')
1907                 {
1908                   nstore[endp-startp] = '/';
1909                   nstore[endp-startp+1] = 0;
1910                 }
1911               else
1912                 nstore[endp-startp] = 0;
1913               add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1914               /* Make separate list of dirs that came from LIBRARY_PATH.  */
1915               add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1916               if (*endp == 0)
1917                 break;
1918               endp = startp = endp + 1;
1919             }
1920           else
1921             endp++;
1922         }
1923     }
1924
1925   /* Scan argv twice.  Here, the first time, just count how many switches
1926      there will be in their vector, and how many input files in theirs.
1927      Here we also parse the switches that cc itself uses (e.g. -v).  */
1928
1929   for (i = 1; i < argc; i++)
1930     {
1931       if (! strcmp (argv[i], "-dumpspecs"))
1932         {
1933           printf ("*asm:\n%s\n\n", asm_spec);
1934           printf ("*asm_final:\n%s\n\n", asm_final_spec);
1935           printf ("*cpp:\n%s\n\n", cpp_spec);
1936           printf ("*cc1:\n%s\n\n", cc1_spec);
1937           printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1938           printf ("*endfile:\n%s\n\n", endfile_spec);
1939           printf ("*link:\n%s\n\n", link_spec);
1940           printf ("*lib:\n%s\n\n", lib_spec);
1941           printf ("*startfile:\n%s\n\n", startfile_spec);
1942           printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1943           printf ("*signed_char:\n%s\n\n", signed_char_spec);
1944           printf ("*predefines:\n%s\n\n", cpp_predefines);
1945           printf ("*cross_compile:\n%d\n\n", cross_compile);
1946
1947           exit (0);
1948         }
1949       else if (! strcmp (argv[i], "-dumpversion"))
1950         {
1951           printf ("%s\n", version_string);
1952           exit (0);
1953         }
1954       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
1955         {
1956           print_libgcc_file_name = 1;
1957         }
1958       else if (! strcmp (argv[i], "-Xlinker"))
1959         {
1960           /* Pass the argument of this option to the linker when we link.  */
1961
1962           if (i + 1 == argc)
1963             fatal ("argument to `-Xlinker' is missing");
1964
1965           n_linker_options++;
1966           if (!linker_options)
1967             linker_options
1968               = (char **) xmalloc (n_linker_options * sizeof (char **));
1969           else
1970             linker_options
1971               = (char **) xrealloc (linker_options,
1972                                     n_linker_options * sizeof (char **));
1973
1974           linker_options[n_linker_options - 1] = argv[++i];
1975         }
1976       else if (! strncmp (argv[i], "-Wl,", 4))
1977         {
1978           int prev, j;
1979           /* Pass the rest of this option to the linker when we link.  */
1980
1981           n_linker_options++;
1982           if (!linker_options)
1983             linker_options
1984               = (char **) xmalloc (n_linker_options * sizeof (char **));
1985           else
1986             linker_options
1987               = (char **) xrealloc (linker_options,
1988                                     n_linker_options * sizeof (char **));
1989
1990           /* Split the argument at commas.  */
1991           prev = 4;
1992           for (j = 4; argv[i][j]; j++)
1993             if (argv[i][j] == ',')
1994               {
1995                 linker_options[n_linker_options - 1]
1996                   = save_string (argv[i] + prev, j - prev);
1997                 n_linker_options++;
1998                 linker_options
1999                   = (char **) xrealloc (linker_options,
2000                                         n_linker_options * sizeof (char **));
2001                 prev = j + 1;
2002               }
2003           /* Record the part after the last comma.  */
2004           linker_options[n_linker_options - 1] = argv[i] + prev;
2005         }
2006       else if (! strncmp (argv[i], "-Wa,", 4))
2007         {
2008           int prev, j;
2009           /* Pass the rest of this option to the assembler.  */
2010
2011           n_assembler_options++;
2012           if (!assembler_options)
2013             assembler_options
2014               = (char **) xmalloc (n_assembler_options * sizeof (char **));
2015           else
2016             assembler_options
2017               = (char **) xrealloc (assembler_options,
2018                                     n_assembler_options * sizeof (char **));
2019
2020           /* Split the argument at commas.  */
2021           prev = 4;
2022           for (j = 4; argv[i][j]; j++)
2023             if (argv[i][j] == ',')
2024               {
2025                 assembler_options[n_assembler_options - 1]
2026                   = save_string (argv[i] + prev, j - prev);
2027                 n_assembler_options++;
2028                 assembler_options
2029                   = (char **) xrealloc (assembler_options,
2030                                         n_assembler_options * sizeof (char **));
2031                 prev = j + 1;
2032               }
2033           /* Record the part after the last comma.  */
2034           assembler_options[n_assembler_options - 1] = argv[i] + prev;
2035         }
2036       else if (argv[i][0] == '+' && argv[i][1] == 'e')
2037         /* Compensate for the +e options to the C++ front-end.  */
2038         n_switches++;
2039       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2040         {
2041           register char *p = &argv[i][1];
2042           register int c = *p;
2043
2044           switch (c)
2045             {
2046             case 'b':
2047               if (p[1] == 0 && i + 1 == argc)
2048                 fatal ("argument to `-b' is missing");
2049               if (p[1] == 0)
2050                 spec_machine = argv[++i];
2051               else
2052                 spec_machine = p + 1;
2053               break;
2054
2055             case 'B':
2056               {
2057                 int *temp = (int *) xmalloc (sizeof (int));
2058                 char *value;
2059                 if (p[1] == 0 && i + 1 == argc)
2060                   fatal ("argument to `-B' is missing");
2061                 if (p[1] == 0)
2062                   value = argv[++i];
2063                 else
2064                   value = p + 1;
2065                 add_prefix (&exec_prefix, value, 1, 0, temp);
2066                 add_prefix (&startfile_prefix, value, 1, 0, temp);
2067               }
2068               break;
2069
2070             case 'v':   /* Print our subcommands and print versions.  */
2071               n_switches++;
2072               /* If they do anything other than exactly `-v', don't set
2073                  verbose_flag; rather, continue on to give the error.  */
2074               if (p[1] != 0)
2075                 break;
2076               verbose_flag++;
2077               break;
2078
2079             case 'V':
2080               if (p[1] == 0 && i + 1 == argc)
2081                 fatal ("argument to `-V' is missing");
2082               if (p[1] == 0)
2083                 spec_version = argv[++i];
2084               else
2085                 spec_version = p + 1;
2086               break;
2087
2088             case 's':
2089               if (!strcmp (p, "save-temps"))
2090                 {
2091                   save_temps_flag = 1;
2092                   n_switches++;
2093                   break;
2094                 }
2095             default:
2096               n_switches++;
2097
2098               if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
2099                 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
2100               else if (WORD_SWITCH_TAKES_ARG (p))
2101                 i += WORD_SWITCH_TAKES_ARG (p);
2102             }
2103         }
2104       else
2105         n_infiles++;
2106     }
2107
2108   /* Set up the search paths before we go looking for config files.  */
2109
2110   /* These come before the md prefixes so that we will find gcc's subcommands
2111      (such as cpp) rather than those of the host system.  */
2112   /* Use 2 as fourth arg meaning try just the machine as a suffix,
2113      as well as trying the machine and the version.  */
2114   add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
2115   add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
2116
2117   add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
2118   add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
2119
2120   /* More prefixes are enabled in main, after we read the specs file
2121      and determine whether this is cross-compilation or not.  */
2122
2123
2124   /* Then create the space for the vectors and scan again.  */
2125
2126   switches = ((struct switchstr *)
2127               xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2128   infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2129   n_switches = 0;
2130   n_infiles = 0;
2131   last_language_n_infiles = -1;
2132
2133   /* This, time, copy the text of each switch and store a pointer
2134      to the copy in the vector of switches.
2135      Store all the infiles in their vector.  */
2136
2137   for (i = 1; i < argc; i++)
2138     {
2139       /* Just skip the switches that were handled by the preceding loop.  */
2140       if (!strcmp (argv[i], "-Xlinker"))
2141         i++;
2142       else if (! strncmp (argv[i], "-Wl,", 4))
2143         ;
2144       else if (! strncmp (argv[i], "-Wa,", 4))
2145         ;
2146       else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2147         ;
2148       else if (argv[i][0] == '+' && argv[i][1] == 'e')
2149         {
2150           /* Compensate for the +e options to the C++ front-end;
2151              they're there simply for cfront call-compatability.  We do
2152              some magic in default_compilers to pass them down properly.
2153              Note we deliberately start at the `+' here, to avoid passing
2154              -e0 or -e1 down into the linker.  */
2155           switches[n_switches].part1 = &argv[i][0];
2156           switches[n_switches].args = 0;
2157           switches[n_switches].valid = 0;
2158           n_switches++;
2159         }
2160       else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2161         {
2162           register char *p = &argv[i][1];
2163           register int c = *p;
2164
2165           if (c == 'B' || c == 'b' || c == 'V')
2166             {
2167               /* Skip a separate arg, if any.  */
2168               if (p[1] == 0)
2169                 i++;
2170               continue;
2171             }
2172           if (c == 'x')
2173             {
2174               if (p[1] == 0 && i + 1 == argc)
2175                 fatal ("argument to `-x' is missing");
2176               if (p[1] == 0)
2177                 spec_lang = argv[++i];
2178               else
2179                 spec_lang = p + 1;
2180               if (! strcmp (spec_lang, "none"))
2181                 /* Suppress the warning if -xnone comes after the last input file,
2182                    because alternate command interfaces like g++ might find it
2183                    useful to place -xnone after each input file.  */
2184                 spec_lang = 0;
2185               else
2186                 last_language_n_infiles = n_infiles;
2187               continue;
2188             }
2189           switches[n_switches].part1 = p;
2190           /* Deal with option arguments in separate argv elements.  */
2191           if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2192               || WORD_SWITCH_TAKES_ARG (p)) {
2193             int j = 0;
2194             int n_args = WORD_SWITCH_TAKES_ARG (p);
2195
2196             if (n_args == 0) {
2197               /* Count only the option arguments in separate argv elements.  */
2198               n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2199             }
2200             if (i + n_args >= argc)
2201               fatal ("argument to `-%s' is missing", p);
2202             switches[n_switches].args
2203               = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2204             while (j < n_args)
2205               switches[n_switches].args[j++] = argv[++i];
2206             /* Null-terminate the vector.  */
2207             switches[n_switches].args[j] = 0;
2208           } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
2209             /* On some systems, ld cannot handle -o or -L without space.
2210                So split the -o or -L from its argument.  */
2211             switches[n_switches].part1 = (c == 'o' ? "o" : "L");
2212             switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2213             switches[n_switches].args[0] = xmalloc (strlen (p));
2214             strcpy (switches[n_switches].args[0], &p[1]);
2215             switches[n_switches].args[1] = 0;
2216           } else
2217             switches[n_switches].args = 0;
2218           switches[n_switches].valid = 0;
2219           /* This is always valid, since gcc.c itself understands it.  */
2220           if (!strcmp (p, "save-temps"))
2221             switches[n_switches].valid = 1;
2222           n_switches++;
2223         }
2224       else
2225         {
2226           infiles[n_infiles].language = spec_lang;
2227           infiles[n_infiles++].name = argv[i];
2228         }
2229     }
2230
2231   if (n_infiles == last_language_n_infiles)
2232     error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2233
2234   switches[n_switches].part1 = 0;
2235   infiles[n_infiles].name = 0;
2236
2237   /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake.  */
2238   if (gcc_exec_prefix)
2239     {
2240       temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
2241                                + strlen (spec_machine) + 3);
2242       strcpy (temp, gcc_exec_prefix);
2243       strcat (temp, spec_machine);
2244       strcat (temp, "/");
2245       strcat (temp, spec_version);
2246       strcat (temp, "/");
2247       gcc_exec_prefix = temp;
2248     }
2249 }
2250 \f
2251 /* Process a spec string, accumulating and running commands.  */
2252
2253 /* These variables describe the input file name.
2254    input_file_number is the index on outfiles of this file,
2255    so that the output file name can be stored for later use by %o.
2256    input_basename is the start of the part of the input file
2257    sans all directory names, and basename_length is the number
2258    of characters starting there excluding the suffix .c or whatever.  */
2259
2260 static char *input_filename;
2261 static int input_file_number;
2262 static int input_filename_length;
2263 static int basename_length;
2264 static char *input_basename;
2265 static char *input_suffix;
2266
2267 /* These are variables used within do_spec and do_spec_1.  */
2268
2269 /* Nonzero if an arg has been started and not yet terminated
2270    (with space, tab or newline).  */
2271 static int arg_going;
2272
2273 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2274    is a temporary file name.  */
2275 static int delete_this_arg;
2276
2277 /* Nonzero means %w has been seen; the next arg to be terminated
2278    is the output file name of this compilation.  */
2279 static int this_is_output_file;
2280
2281 /* Nonzero means %s has been seen; the next arg to be terminated
2282    is the name of a library file and we should try the standard
2283    search dirs for it.  */
2284 static int this_is_library_file;
2285
2286 /* Process the spec SPEC and run the commands specified therein.
2287    Returns 0 if the spec is successfully processed; -1 if failed.  */
2288
2289 static int
2290 do_spec (spec)
2291      char *spec;
2292 {
2293   int value;
2294
2295   clear_args ();
2296   arg_going = 0;
2297   delete_this_arg = 0;
2298   this_is_output_file = 0;
2299   this_is_library_file = 0;
2300
2301   value = do_spec_1 (spec, 0, NULL_PTR);
2302
2303   /* Force out any unfinished command.
2304      If -pipe, this forces out the last command if it ended in `|'.  */
2305   if (value == 0)
2306     {
2307       if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2308         argbuf_index--;
2309
2310       if (argbuf_index > 0)
2311         value = execute ();
2312     }
2313
2314   return value;
2315 }
2316
2317 /* Process the sub-spec SPEC as a portion of a larger spec.
2318    This is like processing a whole spec except that we do
2319    not initialize at the beginning and we do not supply a
2320    newline by default at the end.
2321    INSWITCH nonzero means don't process %-sequences in SPEC;
2322    in this case, % is treated as an ordinary character.
2323    This is used while substituting switches.
2324    INSWITCH nonzero also causes SPC not to terminate an argument.
2325
2326    Value is zero unless a line was finished
2327    and the command on that line reported an error.  */
2328
2329 static int
2330 do_spec_1 (spec, inswitch, soft_matched_part)
2331      char *spec;
2332      int inswitch;
2333      char *soft_matched_part;
2334 {
2335   register char *p = spec;
2336   register int c;
2337   int i;
2338   char *string;
2339
2340   while (c = *p++)
2341     /* If substituting a switch, treat all chars like letters.
2342        Otherwise, NL, SPC, TAB and % are special.  */
2343     switch (inswitch ? 'a' : c)
2344       {
2345       case '\n':
2346         /* End of line: finish any pending argument,
2347            then run the pending command if one has been started.  */
2348         if (arg_going)
2349           {
2350             obstack_1grow (&obstack, 0);
2351             string = obstack_finish (&obstack);
2352             if (this_is_library_file)
2353               string = find_file (string);
2354             store_arg (string, delete_this_arg, this_is_output_file);
2355             if (this_is_output_file)
2356               outfiles[input_file_number] = string;
2357           }
2358         arg_going = 0;
2359
2360         if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2361           {
2362             int i;
2363             for (i = 0; i < n_switches; i++)
2364               if (!strcmp (switches[i].part1, "pipe"))
2365                 break;
2366
2367             /* A `|' before the newline means use a pipe here,
2368                but only if -pipe was specified.
2369                Otherwise, execute now and don't pass the `|' as an arg.  */
2370             if (i < n_switches)
2371               {
2372                 switches[i].valid = 1;
2373                 break;
2374               }
2375             else
2376               argbuf_index--;
2377           }
2378
2379         if (argbuf_index > 0)
2380           {
2381             int value = execute ();
2382             if (value)
2383               return value;
2384           }
2385         /* Reinitialize for a new command, and for a new argument.  */
2386         clear_args ();
2387         arg_going = 0;
2388         delete_this_arg = 0;
2389         this_is_output_file = 0;
2390         this_is_library_file = 0;
2391         break;
2392
2393       case '|':
2394         /* End any pending argument.  */
2395         if (arg_going)
2396           {
2397             obstack_1grow (&obstack, 0);
2398             string = obstack_finish (&obstack);
2399             if (this_is_library_file)
2400               string = find_file (string);
2401             store_arg (string, delete_this_arg, this_is_output_file);
2402             if (this_is_output_file)
2403               outfiles[input_file_number] = string;
2404           }
2405
2406         /* Use pipe */
2407         obstack_1grow (&obstack, c);
2408         arg_going = 1;
2409         break;
2410
2411       case '\t':
2412       case ' ':
2413         /* Space or tab ends an argument if one is pending.  */
2414         if (arg_going)
2415           {
2416             obstack_1grow (&obstack, 0);
2417             string = obstack_finish (&obstack);
2418             if (this_is_library_file)
2419               string = find_file (string);
2420             store_arg (string, delete_this_arg, this_is_output_file);
2421             if (this_is_output_file)
2422               outfiles[input_file_number] = string;
2423           }
2424         /* Reinitialize for a new argument.  */
2425         arg_going = 0;
2426         delete_this_arg = 0;
2427         this_is_output_file = 0;
2428         this_is_library_file = 0;
2429         break;
2430
2431       case '%':
2432         switch (c = *p++)
2433           {
2434           case 0:
2435             fatal ("Invalid specification!  Bug in cc.");
2436
2437           case 'b':
2438             obstack_grow (&obstack, input_basename, basename_length);
2439             arg_going = 1;
2440             break;
2441
2442           case 'd':
2443             delete_this_arg = 2;
2444             break;
2445
2446           /* Dump out the directories specified with LIBRARY_PATH,
2447              followed by the absolute directories
2448              that we search for startfiles.  */
2449           case 'D':
2450             for (i = 0; i < 2; i++)
2451               {
2452                 struct prefix_list *pl
2453                   = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2454                 int bufsize = 100;
2455                 char *buffer = (char *) xmalloc (bufsize);
2456                 int idx;
2457
2458                 for (; pl; pl = pl->next)
2459                   {
2460 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2461                     /* Used on systems which record the specified -L dirs
2462                        and use them to search for dynamic linking.  */
2463                     /* Relative directories always come from -B,
2464                        and it is better not to use them for searching
2465                        at run time.  In particular, stage1 loses  */
2466                     if (pl->prefix[0] != '/')
2467                       continue;
2468 #endif
2469                     if (machine_suffix)
2470                       {
2471                         if (is_linker_dir (pl->prefix, machine_suffix))
2472                           {
2473                             do_spec_1 ("-L", 0, NULL_PTR);
2474 #ifdef SPACE_AFTER_L_OPTION
2475                             do_spec_1 (" ", 0, NULL_PTR);
2476 #endif
2477                             do_spec_1 (pl->prefix, 1, NULL_PTR);
2478                             /* Remove slash from machine_suffix.  */
2479                             if (strlen (machine_suffix) >= bufsize)
2480                               bufsize = strlen (machine_suffix) * 2 + 1;
2481                             buffer = (char *) xrealloc (buffer, bufsize);
2482                             strcpy (buffer, machine_suffix);
2483                             idx = strlen (buffer);
2484                             if (buffer[idx - 1] == '/')
2485                               buffer[idx - 1] = 0;
2486                             do_spec_1 (buffer, 1, NULL_PTR);
2487                             /* Make this a separate argument.  */
2488                             do_spec_1 (" ", 0, NULL_PTR);
2489                           }
2490                       }
2491                     if (!pl->require_machine_suffix)
2492                       {
2493                         if (is_linker_dir (pl->prefix, ""))
2494                           {
2495                             do_spec_1 ("-L", 0, NULL_PTR);
2496 #ifdef SPACE_AFTER_L_OPTION
2497                             do_spec_1 (" ", 0, NULL_PTR);
2498 #endif
2499                             /* Remove slash from pl->prefix.  */
2500                             if (strlen (pl->prefix) >= bufsize)
2501                               bufsize = strlen (pl->prefix) * 2 + 1;
2502                             buffer = (char *) xrealloc (buffer, bufsize);
2503                             strcpy (buffer, pl->prefix);
2504                             idx = strlen (buffer);
2505                             if (buffer[idx - 1] == '/')
2506                               buffer[idx - 1] = 0;
2507                             do_spec_1 (buffer, 1, NULL_PTR);
2508                             /* Make this a separate argument.  */
2509                             do_spec_1 (" ", 0, NULL_PTR);
2510                           }
2511                       }
2512                   }
2513                 free (buffer);
2514               }
2515             break;
2516
2517           case 'e':
2518             /* {...:%efoo} means report an error with `foo' as error message
2519                and don't execute any more commands for this file.  */
2520             {
2521               char *q = p;
2522               char *buf;
2523               while (*p != 0 && *p != '\n') p++;
2524               buf = (char *) alloca (p - q + 1);
2525               strncpy (buf, q, p - q);
2526               buf[p - q] = 0;
2527               error ("%s", buf);
2528               return -1;
2529             }
2530             break;
2531
2532           case 'g':
2533           case 'u':
2534           case 'U':
2535             if (save_temps_flag)
2536               obstack_grow (&obstack, input_basename, basename_length);
2537             else
2538               {
2539 #ifdef MKTEMP_EACH_FILE /* ??? This has a problem: the total number of
2540                            values mktemp can return is limited.
2541                            That matters for the names of object files.
2542                            In 2.4, do something about that.  */
2543                 /* Choose a new temp_filename, but get rid of the
2544                    suffix that the spec wants to add to it.  */
2545                 choose_temp_base ();
2546                 while (*p == '.' || isalpha (*p))
2547                   p++;
2548                 obstack_grow (&obstack, temp_filename, temp_filename_length);
2549                 delete_this_arg = 1;
2550 #else
2551                 obstack_grow (&obstack, temp_filename, temp_filename_length);
2552                 if (c == 'u' || c == 'U')
2553                   {
2554                     static int unique;
2555                     char buff[9];
2556                     if (c == 'u')
2557                       unique++;
2558                     sprintf (buff, "%d", unique);
2559                     obstack_grow (&obstack, buff, strlen (buff));
2560                   }
2561 #endif
2562                 delete_this_arg = 1;
2563               }
2564             arg_going = 1;
2565             break;
2566
2567           case 'i':
2568             obstack_grow (&obstack, input_filename, input_filename_length);
2569             arg_going = 1;
2570             break;
2571
2572           case 'I':
2573             if (gcc_exec_prefix)
2574               {
2575                 do_spec_1 ("-iprefix", 1, NULL_PTR);
2576                 /* Make this a separate argument.  */
2577                 do_spec_1 (" ", 0, NULL_PTR);
2578                 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
2579                 do_spec_1 (" ", 0, NULL_PTR);
2580               }
2581             break;
2582
2583           case 'o':
2584             {
2585               register int f;
2586               for (f = 0; f < n_infiles; f++)
2587                 store_arg (outfiles[f], 0, 0);
2588             }
2589             break;
2590
2591           case 's':
2592             this_is_library_file = 1;
2593             break;
2594
2595           case 'w':
2596             this_is_output_file = 1;
2597             break;
2598
2599           case 'W':
2600             {
2601               int index = argbuf_index;
2602               /* Handle the {...} following the %W.  */
2603               if (*p != '{')
2604                 abort ();
2605               p = handle_braces (p + 1);
2606               if (p == 0)
2607                 return -1;
2608               /* If any args were output, mark the last one for deletion
2609                  on failure.  */
2610               if (argbuf_index != index)
2611                 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2612               break;
2613             }
2614
2615           /* %x{OPTION} records OPTION for %X to output.  */
2616           case 'x':
2617             {
2618               char *p1 = p;
2619               char *string;
2620
2621               /* Skip past the option value and make a copy.  */
2622               if (*p != '{')
2623                 abort ();
2624               while (*p++ != '}')
2625                 ;
2626               string = save_string (p1 + 1, p - p1 - 2);
2627
2628               /* See if we already recorded this option.  */
2629               for (i = 0; i < n_linker_options; i++)
2630                 if (! strcmp (string, linker_options[i]))
2631                   {
2632                     free (string);
2633                     return 0;
2634                   }
2635
2636               /* This option is new; add it.  */
2637               n_linker_options++;
2638               if (!linker_options)
2639                 linker_options
2640                   = (char **) xmalloc (n_linker_options * sizeof (char **));
2641               else
2642                 linker_options
2643                   = (char **) xrealloc (linker_options,
2644                                         n_linker_options * sizeof (char **));
2645
2646               linker_options[n_linker_options - 1] = string;
2647             }
2648             break;
2649
2650           /* Dump out the options accumulated previously using %x,
2651              -Xlinker and -Wl,.  */
2652           case 'X':
2653             for (i = 0; i < n_linker_options; i++)
2654               {
2655                 do_spec_1 (linker_options[i], 1, NULL_PTR);
2656                 /* Make each accumulated option a separate argument.  */
2657                 do_spec_1 (" ", 0, NULL_PTR);
2658               }
2659             break;
2660
2661           /* Dump out the options accumulated previously using -Wa,.  */
2662           case 'Y':
2663             for (i = 0; i < n_assembler_options; i++)
2664               {
2665                 do_spec_1 (assembler_options[i], 1, NULL_PTR);
2666                 /* Make each accumulated option a separate argument.  */
2667                 do_spec_1 (" ", 0, NULL_PTR);
2668               }
2669             break;
2670
2671             /* Here are digits and numbers that just process
2672                a certain constant string as a spec.
2673             /* Here are digits and numbers that just process
2674                a certain constant string as a spec.  */
2675
2676           case '1':
2677             do_spec_1 (cc1_spec, 0, NULL_PTR);
2678             break;
2679
2680           case '2':
2681             do_spec_1 (cc1plus_spec, 0, NULL_PTR);
2682             break;
2683
2684           case 'a':
2685             do_spec_1 (asm_spec, 0, NULL_PTR);
2686             break;
2687
2688           case 'A':
2689             do_spec_1 (asm_final_spec, 0, NULL_PTR);
2690             break;
2691
2692           case 'c':
2693             do_spec_1 (signed_char_spec, 0, NULL_PTR);
2694             break;
2695
2696           case 'C':
2697             do_spec_1 (cpp_spec, 0, NULL_PTR);
2698             break;
2699
2700           case 'E':
2701             do_spec_1 (endfile_spec, 0, NULL_PTR);
2702             break;
2703
2704           case 'l':
2705             do_spec_1 (link_spec, 0, NULL_PTR);
2706             break;
2707
2708           case 'L':
2709             do_spec_1 (lib_spec, 0, NULL_PTR);
2710             break;
2711
2712           case 'p':
2713             {
2714               char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2715               char *buf = x;
2716               char *y;
2717
2718               /* Copy all of the -D options in CPP_PREDEFINES into BUF.  */
2719               y = cpp_predefines;
2720               while (*y != 0)
2721                 {
2722                   if (! strncmp (y, "-D", 2))
2723                     /* Copy the whole option.  */
2724                     while (*y && *y != ' ' && *y != '\t')
2725                       *x++ = *y++;
2726                   else if (*y == ' ' || *y == '\t')
2727                     /* Copy whitespace to the result.  */
2728                     *x++ = *y++;
2729                   /* Don't copy other options.  */
2730                   else
2731                     y++;
2732                 }
2733
2734               *x = 0;
2735
2736               do_spec_1 (buf, 0, NULL_PTR);
2737             }
2738             break;
2739
2740           case 'P':
2741             {
2742               char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2743               char *buf = x;
2744               char *y;
2745
2746               /* Copy all of CPP_PREDEFINES into BUF,
2747                  but put __ after every -D and at the end of each arg.  */
2748               y = cpp_predefines;
2749               while (*y != 0)
2750                 {
2751                   if (! strncmp (y, "-D", 2))
2752                     {
2753                       int flag = 0;
2754
2755                       *x++ = *y++;
2756                       *x++ = *y++;
2757
2758                       if (strncmp (y, "__", 2))
2759                         {
2760                           /* Stick __ at front of macro name.  */
2761                           *x++ = '_';
2762                           *x++ = '_';
2763                           /* Arrange to stick __ at the end as well.  */
2764                           flag = 1;
2765                         }
2766
2767                       /* Copy the macro name.  */
2768                       while (*y && *y != '=' && *y != ' ' && *y != '\t')
2769                         *x++ = *y++;
2770
2771                       if (flag)
2772                         {
2773                           *x++ = '_';
2774                           *x++ = '_';
2775                         }
2776
2777                       /* Copy the value given, if any.  */
2778                       while (*y && *y != ' ' && *y != '\t')
2779                         *x++ = *y++;
2780                     }
2781                   else if (*y == ' ' || *y == '\t')
2782                     /* Copy whitespace to the result.  */
2783                     *x++ = *y++;
2784                   /* Don't copy -A options  */
2785                   else
2786                     y++;
2787                 }
2788               *x++ = ' ';
2789
2790               /* Copy all of CPP_PREDEFINES into BUF,
2791                  but put __ after every -D.  */
2792               y = cpp_predefines;
2793               while (*y != 0)
2794                 {
2795                   if (! strncmp (y, "-D", 2))
2796                     {
2797                       *x++ = *y++;
2798                       *x++ = *y++;
2799
2800                       if (strncmp (y, "__", 2))
2801                         {
2802                           /* Stick __ at front of macro name.  */
2803                           *x++ = '_';
2804                           *x++ = '_';
2805                         }
2806
2807                       /* Copy the macro name.  */
2808                       while (*y && *y != '=' && *y != ' ' && *y != '\t')
2809                         *x++ = *y++;
2810
2811                       /* Copy the value given, if any.  */
2812                       while (*y && *y != ' ' && *y != '\t')
2813                         *x++ = *y++;
2814                     }
2815                   else if (*y == ' ' || *y == '\t')
2816                     /* Copy whitespace to the result.  */
2817                     *x++ = *y++;
2818                   /* Don't copy -A options  */
2819                   else
2820                     y++;
2821                 }
2822               *x++ = ' ';
2823
2824               /* Copy all of the -A options in CPP_PREDEFINES into BUF.  */
2825               y = cpp_predefines;
2826               while (*y != 0)
2827                 {
2828                   if (! strncmp (y, "-A", 2))
2829                     /* Copy the whole option.  */
2830                     while (*y && *y != ' ' && *y != '\t')
2831                       *x++ = *y++;
2832                   else if (*y == ' ' || *y == '\t')
2833                     /* Copy whitespace to the result.  */
2834                     *x++ = *y++;
2835                   /* Don't copy other options.  */
2836                   else
2837                     y++;
2838                 }
2839
2840               *x = 0;
2841
2842               do_spec_1 (buf, 0, NULL_PTR);
2843             }
2844             break;
2845
2846           case 'S':
2847             do_spec_1 (startfile_spec, 0, NULL_PTR);
2848             break;
2849
2850             /* Here we define characters other than letters and digits.  */
2851
2852           case '{':
2853             p = handle_braces (p);
2854             if (p == 0)
2855               return -1;
2856             break;
2857
2858           case '%':
2859             obstack_1grow (&obstack, '%');
2860             break;
2861
2862           case '*':
2863             do_spec_1 (soft_matched_part, 1, NULL_PTR);
2864             do_spec_1 (" ", 0, NULL_PTR);
2865             break;
2866
2867             /* Process a string found as the value of a spec given by name.
2868                This feature allows individual machine descriptions
2869                to add and use their own specs.
2870                %[...] modifies -D options the way %P does;
2871                %(...) uses the spec unmodified.  */
2872           case '(':
2873           case '[':
2874             {
2875               char *name = p;
2876               struct spec_list *sl;
2877               int len;
2878
2879               /* The string after the S/P is the name of a spec that is to be
2880                  processed. */
2881               while (*p && *p != ')' && *p != ']')
2882                 p++;
2883
2884               /* See if it's in the list */
2885               for (len = p - name, sl = specs; sl; sl = sl->next)
2886                 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2887                   {
2888                     name = sl->spec;
2889                     break;
2890                   }
2891
2892               if (sl)
2893                 {
2894                   if (c == '(')
2895                     do_spec_1 (name, 0, NULL_PTR);
2896                   else
2897                     {
2898                       char *x = (char *) alloca (strlen (name) * 2 + 1);
2899                       char *buf = x;
2900                       char *y = name;
2901
2902                       /* Copy all of NAME into BUF, but put __ after
2903                          every -D and at the end of each arg,  */
2904                       while (1)
2905                         {
2906                           if (! strncmp (y, "-D", 2))
2907                             {
2908                               *x++ = '-';
2909                               *x++ = 'D';
2910                               *x++ = '_';
2911                               *x++ = '_';
2912                               y += 2;
2913                             }
2914                           else if (*y == ' ' || *y == 0)
2915                             {
2916                               *x++ = '_';
2917                               *x++ = '_';
2918                               if (*y == 0)
2919                                 break;
2920                               else
2921                                 *x++ = *y++;
2922                             }
2923                           else
2924                             *x++ = *y++;
2925                         }
2926                       *x = 0;
2927
2928                       do_spec_1 (buf, 0, NULL_PTR);
2929                     }
2930                 }
2931
2932               /* Discard the closing paren or bracket.  */
2933               if (*p)
2934                 p++;
2935             }
2936             break;
2937
2938           default:
2939             abort ();
2940           }
2941         break;
2942
2943       case '\\':
2944         /* Backslash: treat next character as ordinary.  */
2945         c = *p++;
2946
2947         /* fall through */
2948       default:
2949         /* Ordinary character: put it into the current argument.  */
2950         obstack_1grow (&obstack, c);
2951         arg_going = 1;
2952       }
2953
2954   return 0;             /* End of string */
2955 }
2956
2957 /* Return 0 if we call do_spec_1 and that returns -1.  */
2958
2959 static char *
2960 handle_braces (p)
2961      register char *p;
2962 {
2963   register char *q;
2964   char *filter;
2965   int pipe = 0;
2966   int negate = 0;
2967   int suffix = 0;
2968
2969   if (*p == '|')
2970     /* A `|' after the open-brace means,
2971        if the test fails, output a single minus sign rather than nothing.
2972        This is used in %{|!pipe:...}.  */
2973     pipe = 1, ++p;
2974
2975   if (*p == '!')
2976     /* A `!' after the open-brace negates the condition:
2977        succeed if the specified switch is not present.  */
2978     negate = 1, ++p;
2979
2980   if (*p == '.')
2981     /* A `.' after the open-brace means test against the current suffix.  */
2982     {
2983       if (pipe)
2984         abort ();
2985
2986       suffix = 1;
2987       ++p;
2988     }
2989
2990   filter = p;
2991   while (*p != ':' && *p != '}') p++;
2992   if (*p != '}')
2993     {
2994       register int count = 1;
2995       q = p + 1;
2996       while (count > 0)
2997         {
2998           if (*q == '{')
2999             count++;
3000           else if (*q == '}')
3001             count--;
3002           else if (*q == 0)
3003             abort ();
3004           q++;
3005         }
3006     }
3007   else
3008     q = p + 1;
3009
3010   if (suffix)
3011     {
3012       int found = (input_suffix != 0
3013                    && strlen (input_suffix) == p - filter
3014                    && strncmp (input_suffix, filter, p - filter) == 0);
3015
3016       if (p[0] == '}')
3017         abort ();
3018
3019       if (negate != found
3020           && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3021         return 0;
3022
3023       return q;
3024     }
3025   else if (p[-1] == '*' && p[0] == '}')
3026     {
3027       /* Substitute all matching switches as separate args.  */
3028       register int i;
3029       --p;
3030       for (i = 0; i < n_switches; i++)
3031         if (!strncmp (switches[i].part1, filter, p - filter))
3032           give_switch (i, 0);
3033     }
3034   else
3035     {
3036       /* Test for presence of the specified switch.  */
3037       register int i;
3038       int present = 0;
3039
3040       /* If name specified ends in *, as in {x*:...},
3041          check for %* and handle that case.  */
3042       if (p[-1] == '*' && !negate)
3043         {
3044           int substitution;
3045           char *r = p;
3046
3047           /* First see whether we have %*.  */
3048           substitution = 0;
3049           while (r < q)
3050             {
3051               if (*r == '%' && r[1] == '*')
3052                 substitution = 1;
3053               r++;
3054             }
3055           /* If we do, handle that case.  */
3056           if (substitution)
3057             {
3058               /* Substitute all matching switches as separate args.
3059                  But do this by substituting for %*
3060                  in the text that follows the colon.  */
3061
3062               unsigned hard_match_len = p - filter - 1;
3063               char *string = save_string (p + 1, q - p - 2);
3064
3065               for (i = 0; i < n_switches; i++)
3066                 if (!strncmp (switches[i].part1, filter, hard_match_len))
3067                   {
3068                     do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
3069                     /* Pass any arguments this switch has.  */
3070                     give_switch (i, 1);
3071                   }
3072
3073               return q;
3074             }
3075         }
3076
3077       /* If name specified ends in *, as in {x*:...},
3078          check for presence of any switch name starting with x.  */
3079       if (p[-1] == '*')
3080         {
3081           for (i = 0; i < n_switches; i++)
3082             {
3083               unsigned hard_match_len = p - filter - 1;
3084
3085               if (!strncmp (switches[i].part1, filter, hard_match_len))
3086                 {
3087                   switches[i].valid = 1;
3088                   present = 1;
3089                 }
3090             }
3091         }
3092       /* Otherwise, check for presence of exact name specified.  */
3093       else
3094         {
3095           for (i = 0; i < n_switches; i++)
3096             {
3097               if (!strncmp (switches[i].part1, filter, p - filter)
3098                   && switches[i].part1[p - filter] == 0)
3099                 {
3100                   switches[i].valid = 1;
3101                   present = 1;
3102                   break;
3103                 }
3104             }
3105         }
3106
3107       /* If it is as desired (present for %{s...}, absent for %{-s...})
3108          then substitute either the switch or the specified
3109          conditional text.  */
3110       if (present != negate)
3111         {
3112           if (*p == '}')
3113             {
3114               give_switch (i, 0);
3115             }
3116           else
3117             {
3118               if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3119                 return 0;
3120             }
3121         }
3122       else if (pipe)
3123         {
3124           /* Here if a %{|...} conditional fails: output a minus sign,
3125              which means "standard output" or "standard input".  */
3126           do_spec_1 ("-", 0, NULL_PTR);
3127         }
3128     }
3129
3130   return q;
3131 }
3132
3133 /* Pass a switch to the current accumulating command
3134    in the same form that we received it.
3135    SWITCHNUM identifies the switch; it is an index into
3136    the vector of switches gcc received, which is `switches'.
3137    This cannot fail since it never finishes a command line.
3138
3139    If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.  */
3140
3141 static void
3142 give_switch (switchnum, omit_first_word)
3143      int switchnum;
3144      int omit_first_word;
3145 {
3146   if (!omit_first_word)
3147     {
3148       do_spec_1 ("-", 0, NULL_PTR);
3149       do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
3150     }
3151   do_spec_1 (" ", 0, NULL_PTR);
3152   if (switches[switchnum].args != 0)
3153     {
3154       char **p;
3155       for (p = switches[switchnum].args; *p; p++)
3156         {
3157           do_spec_1 (*p, 1, NULL_PTR);
3158           do_spec_1 (" ", 0, NULL_PTR);
3159         }
3160     }
3161   switches[switchnum].valid = 1;
3162 }
3163 \f
3164 /* Search for a file named NAME trying various prefixes including the
3165    user's -B prefix and some standard ones.
3166    Return the absolute file name found.  If nothing is found, return NAME.  */
3167
3168 static char *
3169 find_file (name)
3170      char *name;
3171 {
3172   char *newname;
3173
3174   newname = find_a_file (&startfile_prefix, name, R_OK);
3175   return newname ? newname : name;
3176 }
3177
3178 /* Determine whether a -L option is relevant.  Not required for certain
3179    fixed names and for directories that don't exist.  */
3180
3181 static int
3182 is_linker_dir (path1, path2)
3183      char *path1;
3184      char *path2;
3185 {
3186   int len1 = strlen (path1);
3187   int len2 = strlen (path2);
3188   char *path = (char *) alloca (3 + len1 + len2);
3189   char *cp;
3190   struct stat st;
3191
3192   /* Construct the path from the two parts.  Ensure the string ends with "/.".
3193      The resulting path will be a directory even if the given path is a
3194      symbolic link.  */
3195   bcopy (path1, path, len1);
3196   bcopy (path2, path + len1, len2);
3197   cp = path + len1 + len2;
3198   if (cp[-1] != '/')
3199     *cp++ = '/';
3200   *cp++ = '.';
3201   *cp = '\0';
3202
3203   /* Exclude directories that the linker is known to search.  */
3204   if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
3205       || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
3206     return 0;
3207
3208   return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
3209 }
3210 \f
3211 /* On fatal signals, delete all the temporary files.  */
3212
3213 static void
3214 fatal_error (signum)
3215      int signum;
3216 {
3217   signal (signum, SIG_DFL);
3218   delete_failure_queue ();
3219   delete_temp_files ();
3220   /* Get the same signal again, this time not handled,
3221      so its normal effect occurs.  */
3222   kill (getpid (), signum);
3223 }
3224
3225 int
3226 main (argc, argv)
3227      int argc;
3228      char **argv;
3229 {
3230   register int i;
3231   int j;
3232   int value;
3233   int error_count = 0;
3234   int linker_was_run = 0;
3235   char *explicit_link_files;
3236   char *specs_file;
3237
3238   programname = argv[0];
3239
3240   if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3241     signal (SIGINT, fatal_error);
3242   if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3243     signal (SIGHUP, fatal_error);
3244   if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3245     signal (SIGTERM, fatal_error);
3246 #ifdef SIGPIPE
3247   if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3248     signal (SIGPIPE, fatal_error);
3249 #endif
3250
3251   argbuf_length = 10;
3252   argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3253
3254   obstack_init (&obstack);
3255
3256   /* Set up to remember the pathname of gcc and any options
3257      needed for collect.  */
3258   obstack_init (&collect_obstack);
3259   obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3260   obstack_grow (&collect_obstack, programname, strlen (programname)+1);
3261   putenv (obstack_finish (&collect_obstack));
3262
3263   /* Choose directory for temp files.  */
3264
3265   choose_temp_base ();
3266
3267   /* Make a table of what switches there are (switches, n_switches).
3268      Make a table of specified input files (infiles, n_infiles).
3269      Decode switches that are handled locally.  */
3270
3271   process_command (argc, argv);
3272
3273   /* Initialize the vector of specs to just the default.
3274      This means one element containing 0s, as a terminator.  */
3275
3276   compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3277   bcopy (default_compilers, compilers, sizeof default_compilers);
3278   n_compilers = n_default_compilers;
3279
3280   /* Read specs from a file if there is one.  */
3281
3282   machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
3283   just_machine_suffix = concat (spec_machine, "/", "");
3284
3285   specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3286   /* Read the specs file unless it is a default one.  */
3287   if (specs_file != 0 && strcmp (specs_file, "specs"))
3288     read_specs (specs_file);
3289
3290   /* If not cross-compiling, look for startfiles in the standard places.  */
3291   /* The fact that these are done here, after reading the specs file,
3292      means that it cannot be found in these directories.
3293      But that's okay.  It should never be there anyway.  */
3294   if (!cross_compile)
3295     {
3296 #ifdef MD_EXEC_PREFIX
3297       add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3298       add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3299 #endif
3300
3301 #ifdef MD_STARTFILE_PREFIX
3302       add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
3303 #endif
3304
3305 #ifdef MD_STARTFILE_PREFIX_1
3306       add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
3307 #endif
3308
3309       add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3310                   NULL_PTR);
3311       add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3312                   NULL_PTR);
3313       add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3314                   NULL_PTR);
3315 #if 0 /* Can cause surprises, and one can use -B./ instead.  */
3316       add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
3317 #endif
3318     }
3319
3320   /* Now we have the specs.
3321      Set the `valid' bits for switches that match anything in any spec.  */
3322
3323   validate_all_switches ();
3324
3325   /* Warn about any switches that no pass was interested in.  */
3326
3327   for (i = 0; i < n_switches; i++)
3328     if (! switches[i].valid)
3329       error ("unrecognized option `-%s'", switches[i].part1);
3330
3331   if (print_libgcc_file_name)
3332     {
3333       printf ("%s\n", find_file ("libgcc.a"));
3334       exit (0);
3335     }
3336
3337   /* Obey some of the options.  */
3338
3339   if (verbose_flag)
3340     {
3341       fprintf (stderr, "gcc version %s\n", version_string);
3342       if (n_infiles == 0)
3343         exit (0);
3344     }
3345
3346   if (n_infiles == 0)
3347     fatal ("No input files specified.");
3348
3349   /* Make a place to record the compiler output file names
3350      that correspond to the input files.  */
3351
3352   outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3353   bzero (outfiles, n_infiles * sizeof (char *));
3354
3355   /* Record which files were specified explicitly as link input.  */
3356
3357   explicit_link_files = xmalloc (n_infiles);
3358   bzero (explicit_link_files, n_infiles);
3359
3360   for (i = 0; i < n_infiles; i++)
3361     {
3362       register struct compiler *cp = 0;
3363       int this_file_error = 0;
3364
3365       /* Tell do_spec what to substitute for %i.  */
3366
3367       input_filename = infiles[i].name;
3368       input_filename_length = strlen (input_filename);
3369       input_file_number = i;
3370
3371       /* Use the same thing in %o, unless cp->spec says otherwise.  */
3372
3373       outfiles[i] = input_filename;
3374
3375       /* Figure out which compiler from the file's suffix.  */
3376
3377       cp = lookup_compiler (infiles[i].name, input_filename_length,
3378                             infiles[i].language);
3379
3380       if (cp)
3381         {
3382           /* Ok, we found an applicable compiler.  Run its spec.  */
3383           /* First say how much of input_filename to substitute for %b  */
3384           register char *p;
3385           int len;
3386
3387           input_basename = input_filename;
3388           for (p = input_filename; *p; p++)
3389             if (*p == '/')
3390               input_basename = p + 1;
3391
3392           /* Find a suffix starting with the last period,
3393              and set basename_length to exclude that suffix.  */
3394           basename_length = strlen (input_basename);
3395           p = input_basename + basename_length;
3396           while (p != input_basename && *p != '.') --p;
3397           if (*p == '.' && p != input_basename)
3398             {
3399               basename_length = p - input_basename;
3400               input_suffix = p + 1;
3401             }
3402           else
3403             input_suffix = "";
3404
3405           len = 0;
3406           for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3407             if (cp->spec[j])
3408               len += strlen (cp->spec[j]);
3409
3410           p = (char *) xmalloc (len + 1);
3411
3412           len = 0;
3413           for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3414             if (cp->spec[j])
3415               {
3416                 strcpy (p + len, cp->spec[j]);
3417                 len += strlen (cp->spec[j]);
3418               }
3419
3420           value = do_spec (p);
3421           free (p);
3422           if (value < 0)
3423             this_file_error = 1;
3424         }
3425
3426       /* If this file's name does not contain a recognized suffix,
3427          record it as explicit linker input.  */
3428
3429       else
3430         explicit_link_files[i] = 1;
3431
3432       /* Clear the delete-on-failure queue, deleting the files in it
3433          if this compilation failed.  */
3434
3435       if (this_file_error)
3436         {
3437           delete_failure_queue ();
3438           error_count++;
3439         }
3440       /* If this compilation succeeded, don't delete those files later.  */
3441       clear_failure_queue ();
3442     }
3443
3444   /* Run ld to link all the compiler output files.  */
3445
3446   if (error_count == 0)
3447     {
3448       int tmp = execution_count;
3449       int i;
3450       int first_time;
3451
3452       /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3453          for collect.  */
3454       putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3455       putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3456
3457       /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3458          the compiler.  */
3459       obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3460                     sizeof ("COLLECT_GCC_OPTIONS=")-1);
3461
3462       first_time = TRUE;
3463       for (i = 0; i < n_switches; i++)
3464         {
3465           char **args;
3466           if (!first_time)
3467             obstack_grow (&collect_obstack, " ", 1);
3468
3469           first_time = FALSE;
3470           obstack_grow (&collect_obstack, "-", 1);
3471           obstack_grow (&collect_obstack, switches[i].part1,
3472                         strlen (switches[i].part1));
3473
3474           for (args = switches[i].args; args && *args; args++)
3475             {
3476               obstack_grow (&collect_obstack, " ", 1);
3477               obstack_grow (&collect_obstack, *args, strlen (*args));
3478             }
3479         }
3480       obstack_grow (&collect_obstack, "\0", 1);
3481       putenv (obstack_finish (&collect_obstack));
3482
3483       value = do_spec (link_command_spec);
3484       if (value < 0)
3485         error_count = 1;
3486       linker_was_run = (tmp != execution_count);
3487     }
3488
3489   /* Warn if a -B option was specified but the prefix was never used.  */
3490   unused_prefix_warnings (&exec_prefix);
3491   unused_prefix_warnings (&startfile_prefix);
3492
3493   /* If options said don't run linker,
3494      complain about input files to be given to the linker.  */
3495
3496   if (! linker_was_run && error_count == 0)
3497     for (i = 0; i < n_infiles; i++)
3498       if (explicit_link_files[i])
3499         error ("%s: linker input file unused since linking not done",
3500                outfiles[i]);
3501
3502   /* Delete some or all of the temporary files we made.  */
3503
3504   if (error_count)
3505     delete_failure_queue ();
3506   delete_temp_files ();
3507
3508   exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
3509   /* NOTREACHED */
3510   return 0;
3511 }
3512
3513 /* Find the proper compilation spec for the file name NAME,
3514    whose length is LENGTH.  LANGUAGE is the specified language,
3515    or 0 if none specified.  */
3516
3517 static struct compiler *
3518 lookup_compiler (name, length, language)
3519      char *name;
3520      int length;
3521      char *language;
3522 {
3523   struct compiler *cp;
3524
3525   /* Look for the language, if one is spec'd.  */
3526   if (language != 0)
3527     {
3528       for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3529         {
3530           if (language != 0)
3531             {
3532               if (cp->suffix[0] == '@'
3533                   && !strcmp (cp->suffix + 1, language))
3534                 return cp;
3535             }
3536         }
3537       error ("language %s not recognized", language);
3538     }
3539
3540   /* Look for a suffix.  */
3541   for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3542     {
3543       if (strlen (cp->suffix) < length
3544                /* See if the suffix matches the end of NAME.  */
3545                && !strcmp (cp->suffix,
3546                            name + length - strlen (cp->suffix))
3547                /* The suffix `-' matches only the file name `-'.  */
3548                && !(!strcmp (cp->suffix, "-") && length != 1))
3549         {
3550           if (cp->spec[0][0] == '@')
3551             {
3552               struct compiler *new;
3553               /* An alias entry maps a suffix to a language.
3554                  Search for the language; pass 0 for NAME and LENGTH
3555                  to avoid infinite recursion if language not found.
3556                  Construct the new compiler spec.  */
3557               language = cp->spec[0] + 1;
3558               new = (struct compiler *) xmalloc (sizeof (struct compiler));
3559               new->suffix = cp->suffix;
3560               bcopy (lookup_compiler (NULL_PTR, 0, language)->spec,
3561                      new->spec, sizeof new->spec);
3562               return new;
3563             }
3564           /* A non-alias entry: return it.  */
3565           return cp;
3566         }
3567     }
3568
3569   return 0;
3570 }
3571 \f
3572 char *
3573 xmalloc (size)
3574      unsigned size;
3575 {
3576   register char *value = (char *) malloc (size);
3577   if (value == 0)
3578     fatal ("virtual memory exhausted");
3579   return value;
3580 }
3581
3582 char *
3583 xrealloc (ptr, size)
3584      char *ptr;
3585      unsigned size;
3586 {
3587   register char *value = (char *) realloc (ptr, size);
3588   if (value == 0)
3589     fatal ("virtual memory exhausted");
3590   return value;
3591 }
3592
3593 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3.  */
3594
3595 static char *
3596 concat (s1, s2, s3)
3597      char *s1, *s2, *s3;
3598 {
3599   int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3600   char *result = xmalloc (len1 + len2 + len3 + 1);
3601
3602   strcpy (result, s1);
3603   strcpy (result + len1, s2);
3604   strcpy (result + len1 + len2, s3);
3605   *(result + len1 + len2 + len3) = 0;
3606
3607   return result;
3608 }
3609
3610 static char *
3611 save_string (s, len)
3612      char *s;
3613      int len;
3614 {
3615   register char *result = xmalloc (len + 1);
3616
3617   bcopy (s, result, len);
3618   result[len] = 0;
3619   return result;
3620 }
3621
3622 static void
3623 pfatal_with_name (name)
3624      char *name;
3625 {
3626   char *s;
3627
3628   if (errno < sys_nerr)
3629     s = concat ("%s: ", sys_errlist[errno], "");
3630   else
3631     s = "cannot open %s";
3632   fatal (s, name);
3633 }
3634
3635 static void
3636 perror_with_name (name)
3637      char *name;
3638 {
3639   char *s;
3640
3641   if (errno < sys_nerr)
3642     s = concat ("%s: ", sys_errlist[errno], "");
3643   else
3644     s = "cannot open %s";
3645   error (s, name);
3646 }
3647
3648 static void
3649 perror_exec (name)
3650      char *name;
3651 {
3652   char *s;
3653
3654   if (errno < sys_nerr)
3655     s = concat ("installation problem, cannot exec %s: ",
3656                 sys_errlist[errno], "");
3657   else
3658     s = "installation problem, cannot exec %s";
3659   error (s, name);
3660 }
3661
3662 /* More 'friendly' abort that prints the line and file.
3663    config.h can #define abort fancy_abort if you like that sort of thing.  */
3664
3665 void
3666 fancy_abort ()
3667 {
3668   fatal ("Internal gcc abort.");
3669 }
3670 \f
3671 #ifdef HAVE_VPRINTF
3672
3673 /* Output an error message and exit */
3674
3675 static void
3676 fatal (va_alist)
3677      va_dcl
3678 {
3679   va_list ap;
3680   char *format;
3681
3682   va_start (ap);
3683   format = va_arg (ap, char *);
3684   fprintf (stderr, "%s: ", programname);
3685   vfprintf (stderr, format, ap);
3686   va_end (ap);
3687   fprintf (stderr, "\n");
3688   delete_temp_files ();
3689   exit (1);
3690 }
3691
3692 static void
3693 error (va_alist)
3694      va_dcl
3695 {
3696   va_list ap;
3697   char *format;
3698
3699   va_start (ap);
3700   format = va_arg (ap, char *);
3701   fprintf (stderr, "%s: ", programname);
3702   vfprintf (stderr, format, ap);
3703   va_end (ap);
3704
3705   fprintf (stderr, "\n");
3706 }
3707
3708 #else /* not HAVE_VPRINTF */
3709
3710 static void
3711 fatal (msg, arg1, arg2)
3712      char *msg, *arg1, *arg2;
3713 {
3714   error (msg, arg1, arg2);
3715   delete_temp_files ();
3716   exit (1);
3717 }
3718
3719 static void
3720 error (msg, arg1, arg2)
3721      char *msg, *arg1, *arg2;
3722 {
3723   fprintf (stderr, "%s: ", programname);
3724   fprintf (stderr, msg, arg1, arg2);
3725   fprintf (stderr, "\n");
3726 }
3727
3728 #endif /* not HAVE_VPRINTF */
3729
3730 \f
3731 static void
3732 validate_all_switches ()
3733 {
3734   struct compiler *comp;
3735   register char *p;
3736   register char c;
3737   struct spec_list *spec;
3738
3739   for (comp = compilers; comp->spec[0]; comp++)
3740     {
3741       int i;
3742       for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
3743         {
3744           p = comp->spec[i];
3745           while (c = *p++)
3746             if (c == '%' && *p == '{')
3747               /* We have a switch spec.  */
3748               validate_switches (p + 1);
3749         }
3750     }
3751
3752   /* look through the linked list of extra specs read from the specs file */
3753   for (spec = specs; spec ; spec = spec->next)
3754     {
3755       p = spec->spec;
3756       while (c = *p++)
3757         if (c == '%' && *p == '{')
3758           /* We have a switch spec.  */
3759           validate_switches (p + 1);
3760     }
3761
3762   p = link_command_spec;
3763   while (c = *p++)
3764     if (c == '%' && *p == '{')
3765       /* We have a switch spec.  */
3766       validate_switches (p + 1);
3767
3768   /* Now notice switches mentioned in the machine-specific specs.  */
3769
3770   p = asm_spec;
3771   while (c = *p++)
3772     if (c == '%' && *p == '{')
3773       /* We have a switch spec.  */
3774       validate_switches (p + 1);
3775
3776   p = asm_final_spec;
3777   while (c = *p++)
3778     if (c == '%' && *p == '{')
3779       /* We have a switch spec.  */
3780       validate_switches (p + 1);
3781
3782   p = cpp_spec;
3783   while (c = *p++)
3784     if (c == '%' && *p == '{')
3785       /* We have a switch spec.  */
3786       validate_switches (p + 1);
3787
3788   p = signed_char_spec;
3789   while (c = *p++)
3790     if (c == '%' && *p == '{')
3791       /* We have a switch spec.  */
3792       validate_switches (p + 1);
3793
3794   p = cc1_spec;
3795   while (c = *p++)
3796     if (c == '%' && *p == '{')
3797       /* We have a switch spec.  */
3798       validate_switches (p + 1);
3799
3800   p = cc1plus_spec;
3801   while (c = *p++)
3802     if (c == '%' && *p == '{')
3803       /* We have a switch spec.  */
3804       validate_switches (p + 1);
3805
3806   p = link_spec;
3807   while (c = *p++)
3808     if (c == '%' && *p == '{')
3809       /* We have a switch spec.  */
3810       validate_switches (p + 1);
3811
3812   p = lib_spec;
3813   while (c = *p++)
3814     if (c == '%' && *p == '{')
3815       /* We have a switch spec.  */
3816       validate_switches (p + 1);
3817
3818   p = startfile_spec;
3819   while (c = *p++)
3820     if (c == '%' && *p == '{')
3821       /* We have a switch spec.  */
3822       validate_switches (p + 1);
3823 }
3824
3825 /* Look at the switch-name that comes after START
3826    and mark as valid all supplied switches that match it.  */
3827
3828 static void
3829 validate_switches (start)
3830      char *start;
3831 {
3832   register char *p = start;
3833   char *filter;
3834   register int i;
3835   int suffix = 0;
3836
3837   if (*p == '|')
3838     ++p;
3839
3840   if (*p == '!')
3841     ++p;
3842
3843   if (*p == '.')
3844     suffix = 1, ++p;
3845
3846   filter = p;
3847   while (*p != ':' && *p != '}') p++;
3848
3849   if (suffix)
3850     ;
3851   else if (p[-1] == '*')
3852     {
3853       /* Mark all matching switches as valid.  */
3854       --p;
3855       for (i = 0; i < n_switches; i++)
3856         if (!strncmp (switches[i].part1, filter, p - filter))
3857           switches[i].valid = 1;
3858     }
3859   else
3860     {
3861       /* Mark an exact matching switch as valid.  */
3862       for (i = 0; i < n_switches; i++)
3863         {
3864           if (!strncmp (switches[i].part1, filter, p - filter)
3865               && switches[i].part1[p - filter] == 0)
3866             switches[i].valid = 1;
3867         }
3868     }
3869 }