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