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