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