]> gcc.gnu.org Git - gcc.git/blame - gcc/gcc.c
(gmon.o, crt*.o): Change config to $(srcdir)/config.
[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;
2367
2368 while (c = *p++)
2369 /* If substituting a switch, treat all chars like letters.
2370 Otherwise, NL, SPC, TAB and % are special. */
2371 switch (inswitch ? 'a' : c)
2372 {
2373 case '\n':
2374 /* End of line: finish any pending argument,
2375 then run the pending command if one has been started. */
2376 if (arg_going)
2377 {
2378 obstack_1grow (&obstack, 0);
2379 string = obstack_finish (&obstack);
2380 if (this_is_library_file)
2381 string = find_file (string);
2382 store_arg (string, delete_this_arg, this_is_output_file);
2383 if (this_is_output_file)
2384 outfiles[input_file_number] = string;
2385 }
2386 arg_going = 0;
2387
2388 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2389 {
2390 int i;
2391 for (i = 0; i < n_switches; i++)
2392 if (!strcmp (switches[i].part1, "pipe"))
2393 break;
2394
2395 /* A `|' before the newline means use a pipe here,
2396 but only if -pipe was specified.
2397 Otherwise, execute now and don't pass the `|' as an arg. */
2398 if (i < n_switches)
2399 {
a99bf70c 2400 input_from_pipe = 1;
ed1f651b
RS
2401 switches[i].valid = 1;
2402 break;
2403 }
2404 else
2405 argbuf_index--;
2406 }
2407
2408 if (argbuf_index > 0)
2409 {
2410 int value = execute ();
2411 if (value)
2412 return value;
2413 }
2414 /* Reinitialize for a new command, and for a new argument. */
2415 clear_args ();
2416 arg_going = 0;
2417 delete_this_arg = 0;
2418 this_is_output_file = 0;
2419 this_is_library_file = 0;
a99bf70c 2420 input_from_pipe = 0;
ed1f651b
RS
2421 break;
2422
2423 case '|':
2424 /* End any pending argument. */
2425 if (arg_going)
2426 {
2427 obstack_1grow (&obstack, 0);
2428 string = obstack_finish (&obstack);
2429 if (this_is_library_file)
2430 string = find_file (string);
2431 store_arg (string, delete_this_arg, this_is_output_file);
2432 if (this_is_output_file)
2433 outfiles[input_file_number] = string;
2434 }
2435
2436 /* Use pipe */
2437 obstack_1grow (&obstack, c);
2438 arg_going = 1;
2439 break;
2440
2441 case '\t':
2442 case ' ':
2443 /* Space or tab ends an argument if one is pending. */
2444 if (arg_going)
2445 {
2446 obstack_1grow (&obstack, 0);
2447 string = obstack_finish (&obstack);
2448 if (this_is_library_file)
2449 string = find_file (string);
2450 store_arg (string, delete_this_arg, this_is_output_file);
2451 if (this_is_output_file)
2452 outfiles[input_file_number] = string;
2453 }
2454 /* Reinitialize for a new argument. */
2455 arg_going = 0;
2456 delete_this_arg = 0;
2457 this_is_output_file = 0;
2458 this_is_library_file = 0;
2459 break;
2460
2461 case '%':
2462 switch (c = *p++)
2463 {
2464 case 0:
2465 fatal ("Invalid specification! Bug in cc.");
2466
2467 case 'b':
2468 obstack_grow (&obstack, input_basename, basename_length);
2469 arg_going = 1;
2470 break;
2471
2472 case 'd':
2473 delete_this_arg = 2;
2474 break;
2475
2476 /* Dump out the directories specified with LIBRARY_PATH,
004fd4d5
RS
2477 followed by the absolute directories
2478 that we search for startfiles. */
ed1f651b
RS
2479 case 'D':
2480 for (i = 0; i < 2; i++)
2481 {
2482 struct prefix_list *pl
2483 = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
59014d0a
RS
2484 int bufsize = 100;
2485 char *buffer = (char *) xmalloc (bufsize);
2486 int idx;
2487
ed1f651b
RS
2488 for (; pl; pl = pl->next)
2489 {
004fd4d5
RS
2490#ifdef RELATIVE_PREFIX_NOT_LINKDIR
2491 /* Used on systems which record the specified -L dirs
2492 and use them to search for dynamic linking. */
2493 /* Relative directories always come from -B,
2494 and it is better not to use them for searching
2495 at run time. In particular, stage1 loses */
2496 if (pl->prefix[0] != '/')
2497 continue;
2498#endif
ed1f651b
RS
2499 if (machine_suffix)
2500 {
2501 if (is_linker_dir (pl->prefix, machine_suffix))
2502 {
906c4e36 2503 do_spec_1 ("-L", 0, NULL_PTR);
004fd4d5 2504#ifdef SPACE_AFTER_L_OPTION
906c4e36 2505 do_spec_1 (" ", 0, NULL_PTR);
004fd4d5 2506#endif
906c4e36 2507 do_spec_1 (pl->prefix, 1, NULL_PTR);
59014d0a
RS
2508 /* Remove slash from machine_suffix. */
2509 if (strlen (machine_suffix) >= bufsize)
2510 bufsize = strlen (machine_suffix) * 2 + 1;
2511 buffer = (char *) xrealloc (buffer, bufsize);
2512 strcpy (buffer, machine_suffix);
2513 idx = strlen (buffer);
2514 if (buffer[idx - 1] == '/')
2515 buffer[idx - 1] = 0;
906c4e36 2516 do_spec_1 (buffer, 1, NULL_PTR);
ed1f651b 2517 /* Make this a separate argument. */
906c4e36 2518 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
2519 }
2520 }
004fd4d5 2521 if (!pl->require_machine_suffix)
ed1f651b
RS
2522 {
2523 if (is_linker_dir (pl->prefix, ""))
2524 {
906c4e36 2525 do_spec_1 ("-L", 0, NULL_PTR);
004fd4d5 2526#ifdef SPACE_AFTER_L_OPTION
906c4e36 2527 do_spec_1 (" ", 0, NULL_PTR);
004fd4d5 2528#endif
59014d0a
RS
2529 /* Remove slash from pl->prefix. */
2530 if (strlen (pl->prefix) >= bufsize)
2531 bufsize = strlen (pl->prefix) * 2 + 1;
2532 buffer = (char *) xrealloc (buffer, bufsize);
2533 strcpy (buffer, pl->prefix);
2534 idx = strlen (buffer);
2535 if (buffer[idx - 1] == '/')
2536 buffer[idx - 1] = 0;
906c4e36 2537 do_spec_1 (buffer, 1, NULL_PTR);
ed1f651b 2538 /* Make this a separate argument. */
906c4e36 2539 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
2540 }
2541 }
2542 }
59014d0a 2543 free (buffer);
ed1f651b
RS
2544 }
2545 break;
2546
2547 case 'e':
2548 /* {...:%efoo} means report an error with `foo' as error message
2549 and don't execute any more commands for this file. */
2550 {
2551 char *q = p;
2552 char *buf;
2553 while (*p != 0 && *p != '\n') p++;
2554 buf = (char *) alloca (p - q + 1);
2555 strncpy (buf, q, p - q);
2556 buf[p - q] = 0;
2557 error ("%s", buf);
2558 return -1;
2559 }
2560 break;
2561
2562 case 'g':
d887e808 2563 case 'u':
4401b31c 2564 case 'U':
ed1f651b
RS
2565 if (save_temps_flag)
2566 obstack_grow (&obstack, input_basename, basename_length);
2567 else
2568 {
fb266030
TW
2569#ifdef MKTEMP_EACH_FILE
2570 /* ??? This has a problem: the total number of
2571 values mktemp can return is limited.
2572 That matters for the names of object files.
2573 In 2.4, do something about that. */
2574 struct temp_name *t;
2575 char *suffix = p;
b9490a6e
RS
2576 while (*p == '.' || isalpha (*p))
2577 p++;
fb266030
TW
2578
2579 /* See if we already have an association of %g/%u/%U and
2580 suffix. */
2581 for (t = temp_names; t; t = t->next)
2582 if (t->length == p - suffix
2583 && strncmp (t->suffix, suffix, p - suffix) == 0
2584 && t->unique == (c != 'g'))
2585 break;
2586
2587 /* Make a new association if needed. %u requires one. */
2588 if (t == 0 || c == 'u')
2589 {
2590 if (t == 0)
2591 {
2592 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
2593 t->next = temp_names;
2594 temp_names = t;
2595 }
2596 t->length = p - suffix;
2597 t->suffix = save_string (suffix, p - suffix);
2598 t->unique = (c != 'g');
2599 choose_temp_base ();
2600 t->filename = temp_filename;
2601 t->filename_length = temp_filename_length;
2602 }
2603
2604 obstack_grow (&obstack, t->filename, t->filename_length);
b9490a6e
RS
2605 delete_this_arg = 1;
2606#else
ed1f651b 2607 obstack_grow (&obstack, temp_filename, temp_filename_length);
4401b31c 2608 if (c == 'u' || c == 'U')
d887e808
TW
2609 {
2610 static int unique;
2611 char buff[9];
4401b31c
MM
2612 if (c == 'u')
2613 unique++;
2614 sprintf (buff, "%d", unique);
d887e808
TW
2615 obstack_grow (&obstack, buff, strlen (buff));
2616 }
b9490a6e 2617#endif
ed1f651b
RS
2618 delete_this_arg = 1;
2619 }
2620 arg_going = 1;
2621 break;
2622
2623 case 'i':
2624 obstack_grow (&obstack, input_filename, input_filename_length);
2625 arg_going = 1;
2626 break;
2627
8eebb258
RS
2628 case 'I':
2629 if (gcc_exec_prefix)
2630 {
7471ffd8 2631 do_spec_1 ("-iprefix", 1, NULL_PTR);
8eebb258 2632 /* Make this a separate argument. */
7471ffd8
RK
2633 do_spec_1 (" ", 0, NULL_PTR);
2634 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
2635 do_spec_1 (" ", 0, NULL_PTR);
8eebb258
RS
2636 }
2637 break;
2638
ed1f651b
RS
2639 case 'o':
2640 {
2641 register int f;
2642 for (f = 0; f < n_infiles; f++)
2643 store_arg (outfiles[f], 0, 0);
2644 }
2645 break;
2646
2647 case 's':
2648 this_is_library_file = 1;
2649 break;
2650
2651 case 'w':
2652 this_is_output_file = 1;
2653 break;
2654
2655 case 'W':
2656 {
2657 int index = argbuf_index;
2658 /* Handle the {...} following the %W. */
2659 if (*p != '{')
2660 abort ();
2661 p = handle_braces (p + 1);
2662 if (p == 0)
2663 return -1;
2664 /* If any args were output, mark the last one for deletion
2665 on failure. */
2666 if (argbuf_index != index)
2667 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2668 break;
2669 }
2670
2671 /* %x{OPTION} records OPTION for %X to output. */
2672 case 'x':
2673 {
2674 char *p1 = p;
2675 char *string;
2676
2677 /* Skip past the option value and make a copy. */
2678 if (*p != '{')
2679 abort ();
2680 while (*p++ != '}')
2681 ;
2682 string = save_string (p1 + 1, p - p1 - 2);
2683
2684 /* See if we already recorded this option. */
2685 for (i = 0; i < n_linker_options; i++)
2686 if (! strcmp (string, linker_options[i]))
2687 {
2688 free (string);
2689 return 0;
2690 }
2691
2692 /* This option is new; add it. */
2693 n_linker_options++;
2694 if (!linker_options)
2695 linker_options
2696 = (char **) xmalloc (n_linker_options * sizeof (char **));
2697 else
2698 linker_options
2699 = (char **) xrealloc (linker_options,
2700 n_linker_options * sizeof (char **));
2701
2702 linker_options[n_linker_options - 1] = string;
2703 }
2704 break;
2705
c9ebacb8
RS
2706 /* Dump out the options accumulated previously using %x,
2707 -Xlinker and -Wl,. */
ed1f651b
RS
2708 case 'X':
2709 for (i = 0; i < n_linker_options; i++)
2710 {
906c4e36 2711 do_spec_1 (linker_options[i], 1, NULL_PTR);
ed1f651b 2712 /* Make each accumulated option a separate argument. */
906c4e36 2713 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
2714 }
2715 break;
2716
c9ebacb8
RS
2717 /* Dump out the options accumulated previously using -Wa,. */
2718 case 'Y':
2719 for (i = 0; i < n_assembler_options; i++)
2720 {
2721 do_spec_1 (assembler_options[i], 1, NULL_PTR);
2722 /* Make each accumulated option a separate argument. */
2723 do_spec_1 (" ", 0, NULL_PTR);
2724 }
2725 break;
2726
2727 /* Here are digits and numbers that just process
2728 a certain constant string as a spec.
ed1f651b
RS
2729 /* Here are digits and numbers that just process
2730 a certain constant string as a spec. */
2731
2732 case '1':
906c4e36 2733 do_spec_1 (cc1_spec, 0, NULL_PTR);
ed1f651b
RS
2734 break;
2735
2736 case '2':
906c4e36 2737 do_spec_1 (cc1plus_spec, 0, NULL_PTR);
ed1f651b
RS
2738 break;
2739
2740 case 'a':
906c4e36 2741 do_spec_1 (asm_spec, 0, NULL_PTR);
ed1f651b
RS
2742 break;
2743
2744 case 'A':
906c4e36 2745 do_spec_1 (asm_final_spec, 0, NULL_PTR);
ed1f651b
RS
2746 break;
2747
2748 case 'c':
906c4e36 2749 do_spec_1 (signed_char_spec, 0, NULL_PTR);
ed1f651b
RS
2750 break;
2751
2752 case 'C':
906c4e36 2753 do_spec_1 (cpp_spec, 0, NULL_PTR);
ed1f651b
RS
2754 break;
2755
2756 case 'E':
906c4e36 2757 do_spec_1 (endfile_spec, 0, NULL_PTR);
ed1f651b
RS
2758 break;
2759
2760 case 'l':
906c4e36 2761 do_spec_1 (link_spec, 0, NULL_PTR);
ed1f651b
RS
2762 break;
2763
2764 case 'L':
906c4e36 2765 do_spec_1 (lib_spec, 0, NULL_PTR);
ed1f651b
RS
2766 break;
2767
2768 case 'p':
2769 {
2770 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2771 char *buf = x;
2772 char *y;
2773
2774 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2775 y = cpp_predefines;
2776 while (*y != 0)
2777 {
2778 if (! strncmp (y, "-D", 2))
2779 /* Copy the whole option. */
2780 while (*y && *y != ' ' && *y != '\t')
2781 *x++ = *y++;
2782 else if (*y == ' ' || *y == '\t')
2783 /* Copy whitespace to the result. */
2784 *x++ = *y++;
2785 /* Don't copy other options. */
2786 else
2787 y++;
2788 }
2789
2790 *x = 0;
2791
906c4e36 2792 do_spec_1 (buf, 0, NULL_PTR);
ed1f651b
RS
2793 }
2794 break;
2795
2796 case 'P':
2797 {
2798 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2799 char *buf = x;
2800 char *y;
2801
2802 /* Copy all of CPP_PREDEFINES into BUF,
2803 but put __ after every -D and at the end of each arg. */
2804 y = cpp_predefines;
2805 while (*y != 0)
2806 {
2807 if (! strncmp (y, "-D", 2))
2808 {
2809 int flag = 0;
2810
2811 *x++ = *y++;
2812 *x++ = *y++;
2813
2814 if (strncmp (y, "__", 2))
2815 {
2816 /* Stick __ at front of macro name. */
2817 *x++ = '_';
2818 *x++ = '_';
2819 /* Arrange to stick __ at the end as well. */
2820 flag = 1;
2821 }
2822
2823 /* Copy the macro name. */
2824 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2825 *x++ = *y++;
2826
2827 if (flag)
2828 {
2829 *x++ = '_';
2830 *x++ = '_';
2831 }
2832
2833 /* Copy the value given, if any. */
2834 while (*y && *y != ' ' && *y != '\t')
2835 *x++ = *y++;
2836 }
2837 else if (*y == ' ' || *y == '\t')
2838 /* Copy whitespace to the result. */
2839 *x++ = *y++;
2840 /* Don't copy -A options */
2841 else
2842 y++;
2843 }
2844 *x++ = ' ';
2845
2846 /* Copy all of CPP_PREDEFINES into BUF,
2847 but put __ after every -D. */
2848 y = cpp_predefines;
2849 while (*y != 0)
2850 {
2851 if (! strncmp (y, "-D", 2))
2852 {
2853 *x++ = *y++;
2854 *x++ = *y++;
2855
2856 if (strncmp (y, "__", 2))
2857 {
2858 /* Stick __ at front of macro name. */
2859 *x++ = '_';
2860 *x++ = '_';
2861 }
2862
2863 /* Copy the macro name. */
2864 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2865 *x++ = *y++;
2866
2867 /* Copy the value given, if any. */
2868 while (*y && *y != ' ' && *y != '\t')
2869 *x++ = *y++;
2870 }
2871 else if (*y == ' ' || *y == '\t')
2872 /* Copy whitespace to the result. */
2873 *x++ = *y++;
2874 /* Don't copy -A options */
2875 else
2876 y++;
2877 }
2878 *x++ = ' ';
2879
2880 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2881 y = cpp_predefines;
2882 while (*y != 0)
2883 {
2884 if (! strncmp (y, "-A", 2))
2885 /* Copy the whole option. */
2886 while (*y && *y != ' ' && *y != '\t')
2887 *x++ = *y++;
2888 else if (*y == ' ' || *y == '\t')
2889 /* Copy whitespace to the result. */
2890 *x++ = *y++;
2891 /* Don't copy other options. */
2892 else
2893 y++;
2894 }
2895
2896 *x = 0;
2897
906c4e36 2898 do_spec_1 (buf, 0, NULL_PTR);
ed1f651b
RS
2899 }
2900 break;
2901
2902 case 'S':
906c4e36 2903 do_spec_1 (startfile_spec, 0, NULL_PTR);
ed1f651b
RS
2904 break;
2905
2906 /* Here we define characters other than letters and digits. */
2907
2908 case '{':
2909 p = handle_braces (p);
2910 if (p == 0)
2911 return -1;
2912 break;
2913
2914 case '%':
2915 obstack_1grow (&obstack, '%');
2916 break;
2917
2918 case '*':
906c4e36
RK
2919 do_spec_1 (soft_matched_part, 1, NULL_PTR);
2920 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
2921 break;
2922
2923 /* Process a string found as the value of a spec given by name.
2924 This feature allows individual machine descriptions
2925 to add and use their own specs.
2926 %[...] modifies -D options the way %P does;
2927 %(...) uses the spec unmodified. */
2928 case '(':
2929 case '[':
2930 {
2931 char *name = p;
2932 struct spec_list *sl;
2933 int len;
2934
2935 /* The string after the S/P is the name of a spec that is to be
2936 processed. */
2937 while (*p && *p != ')' && *p != ']')
2938 p++;
2939
2940 /* See if it's in the list */
2941 for (len = p - name, sl = specs; sl; sl = sl->next)
2942 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2943 {
2944 name = sl->spec;
2945 break;
2946 }
2947
2948 if (sl)
2949 {
2950 if (c == '(')
906c4e36 2951 do_spec_1 (name, 0, NULL_PTR);
ed1f651b
RS
2952 else
2953 {
2954 char *x = (char *) alloca (strlen (name) * 2 + 1);
2955 char *buf = x;
2956 char *y = name;
2957
2958 /* Copy all of NAME into BUF, but put __ after
2959 every -D and at the end of each arg, */
2960 while (1)
2961 {
2962 if (! strncmp (y, "-D", 2))
2963 {
2964 *x++ = '-';
2965 *x++ = 'D';
2966 *x++ = '_';
2967 *x++ = '_';
2968 y += 2;
2969 }
2970 else if (*y == ' ' || *y == 0)
2971 {
2972 *x++ = '_';
2973 *x++ = '_';
2974 if (*y == 0)
2975 break;
2976 else
2977 *x++ = *y++;
2978 }
2979 else
2980 *x++ = *y++;
2981 }
2982 *x = 0;
2983
906c4e36 2984 do_spec_1 (buf, 0, NULL_PTR);
ed1f651b
RS
2985 }
2986 }
b3865ca9
RS
2987
2988 /* Discard the closing paren or bracket. */
2989 if (*p)
2990 p++;
ed1f651b
RS
2991 }
2992 break;
2993
a99bf70c
JW
2994 case '|':
2995 if (input_from_pipe)
2996 do_spec_1 ("-", 0, NULL_PTR);
2997 break;
2998
ed1f651b
RS
2999 default:
3000 abort ();
3001 }
3002 break;
3003
3004 case '\\':
3005 /* Backslash: treat next character as ordinary. */
3006 c = *p++;
3007
3008 /* fall through */
3009 default:
3010 /* Ordinary character: put it into the current argument. */
3011 obstack_1grow (&obstack, c);
3012 arg_going = 1;
3013 }
3014
3015 return 0; /* End of string */
3016}
3017
3018/* Return 0 if we call do_spec_1 and that returns -1. */
3019
3020static char *
3021handle_braces (p)
3022 register char *p;
3023{
3024 register char *q;
3025 char *filter;
3026 int pipe = 0;
3027 int negate = 0;
3028 int suffix = 0;
3029
3030 if (*p == '|')
3031 /* A `|' after the open-brace means,
3032 if the test fails, output a single minus sign rather than nothing.
3033 This is used in %{|!pipe:...}. */
3034 pipe = 1, ++p;
3035
3036 if (*p == '!')
3037 /* A `!' after the open-brace negates the condition:
3038 succeed if the specified switch is not present. */
3039 negate = 1, ++p;
3040
3041 if (*p == '.')
3042 /* A `.' after the open-brace means test against the current suffix. */
3043 {
3044 if (pipe)
3045 abort ();
3046
3047 suffix = 1;
3048 ++p;
3049 }
3050
3051 filter = p;
3052 while (*p != ':' && *p != '}') p++;
3053 if (*p != '}')
3054 {
3055 register int count = 1;
3056 q = p + 1;
3057 while (count > 0)
3058 {
3059 if (*q == '{')
3060 count++;
3061 else if (*q == '}')
3062 count--;
3063 else if (*q == 0)
3064 abort ();
3065 q++;
3066 }
3067 }
3068 else
3069 q = p + 1;
3070
3071 if (suffix)
3072 {
3073 int found = (input_suffix != 0
004fd4d5 3074 && strlen (input_suffix) == p - filter
ed1f651b
RS
3075 && strncmp (input_suffix, filter, p - filter) == 0);
3076
3077 if (p[0] == '}')
3078 abort ();
3079
3080 if (negate != found
906c4e36 3081 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
ed1f651b
RS
3082 return 0;
3083
3084 return q;
3085 }
3086 else if (p[-1] == '*' && p[0] == '}')
3087 {
3088 /* Substitute all matching switches as separate args. */
3089 register int i;
3090 --p;
3091 for (i = 0; i < n_switches; i++)
3092 if (!strncmp (switches[i].part1, filter, p - filter))
3093 give_switch (i, 0);
3094 }
3095 else
3096 {
3097 /* Test for presence of the specified switch. */
3098 register int i;
3099 int present = 0;
3100
3101 /* If name specified ends in *, as in {x*:...},
3102 check for %* and handle that case. */
3103 if (p[-1] == '*' && !negate)
3104 {
3105 int substitution;
3106 char *r = p;
3107
3108 /* First see whether we have %*. */
3109 substitution = 0;
b3865ca9 3110 while (r < q)
ed1f651b
RS
3111 {
3112 if (*r == '%' && r[1] == '*')
3113 substitution = 1;
3114 r++;
3115 }
3116 /* If we do, handle that case. */
3117 if (substitution)
3118 {
3119 /* Substitute all matching switches as separate args.
3120 But do this by substituting for %*
3121 in the text that follows the colon. */
3122
3123 unsigned hard_match_len = p - filter - 1;
3124 char *string = save_string (p + 1, q - p - 2);
3125
3126 for (i = 0; i < n_switches; i++)
3127 if (!strncmp (switches[i].part1, filter, hard_match_len))
3128 {
3129 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
3130 /* Pass any arguments this switch has. */
3131 give_switch (i, 1);
3132 }
3133
3134 return q;
3135 }
3136 }
3137
3138 /* If name specified ends in *, as in {x*:...},
3139 check for presence of any switch name starting with x. */
3140 if (p[-1] == '*')
3141 {
3142 for (i = 0; i < n_switches; i++)
3143 {
3144 unsigned hard_match_len = p - filter - 1;
3145
3146 if (!strncmp (switches[i].part1, filter, hard_match_len))
3147 {
3148 switches[i].valid = 1;
3149 present = 1;
3150 }
3151 }
3152 }
3153 /* Otherwise, check for presence of exact name specified. */
3154 else
3155 {
3156 for (i = 0; i < n_switches; i++)
3157 {
3158 if (!strncmp (switches[i].part1, filter, p - filter)
3159 && switches[i].part1[p - filter] == 0)
3160 {
3161 switches[i].valid = 1;
3162 present = 1;
3163 break;
3164 }
3165 }
3166 }
3167
3168 /* If it is as desired (present for %{s...}, absent for %{-s...})
3169 then substitute either the switch or the specified
3170 conditional text. */
3171 if (present != negate)
3172 {
3173 if (*p == '}')
3174 {
3175 give_switch (i, 0);
3176 }
3177 else
3178 {
906c4e36 3179 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
ed1f651b
RS
3180 return 0;
3181 }
3182 }
3183 else if (pipe)
3184 {
3185 /* Here if a %{|...} conditional fails: output a minus sign,
3186 which means "standard output" or "standard input". */
906c4e36 3187 do_spec_1 ("-", 0, NULL_PTR);
ed1f651b
RS
3188 }
3189 }
3190
3191 return q;
3192}
3193
3194/* Pass a switch to the current accumulating command
3195 in the same form that we received it.
3196 SWITCHNUM identifies the switch; it is an index into
3197 the vector of switches gcc received, which is `switches'.
3198 This cannot fail since it never finishes a command line.
3199
3200 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
3201
3202static void
3203give_switch (switchnum, omit_first_word)
3204 int switchnum;
3205 int omit_first_word;
3206{
3207 if (!omit_first_word)
3208 {
906c4e36
RK
3209 do_spec_1 ("-", 0, NULL_PTR);
3210 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
ed1f651b 3211 }
906c4e36 3212 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
3213 if (switches[switchnum].args != 0)
3214 {
3215 char **p;
3216 for (p = switches[switchnum].args; *p; p++)
3217 {
906c4e36
RK
3218 do_spec_1 (*p, 1, NULL_PTR);
3219 do_spec_1 (" ", 0, NULL_PTR);
ed1f651b
RS
3220 }
3221 }
3222 switches[switchnum].valid = 1;
3223}
3224\f
3225/* Search for a file named NAME trying various prefixes including the
3226 user's -B prefix and some standard ones.
3227 Return the absolute file name found. If nothing is found, return NAME. */
3228
3229static char *
3230find_file (name)
3231 char *name;
3232{
3233 char *newname;
3234
3235 newname = find_a_file (&startfile_prefix, name, R_OK);
3236 return newname ? newname : name;
3237}
3238
3239/* Determine whether a -L option is relevant. Not required for certain
3240 fixed names and for directories that don't exist. */
3241
3242static int
3243is_linker_dir (path1, path2)
3244 char *path1;
3245 char *path2;
3246{
3247 int len1 = strlen (path1);
3248 int len2 = strlen (path2);
3249 char *path = (char *) alloca (3 + len1 + len2);
3250 char *cp;
3251 struct stat st;
3252
3253 /* Construct the path from the two parts. Ensure the string ends with "/.".
3254 The resulting path will be a directory even if the given path is a
3255 symbolic link. */
3256 bcopy (path1, path, len1);
3257 bcopy (path2, path + len1, len2);
3258 cp = path + len1 + len2;
3259 if (cp[-1] != '/')
3260 *cp++ = '/';
3261 *cp++ = '.';
3262 *cp = '\0';
3263
3264 /* Exclude directories that the linker is known to search. */
3265 if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
3266 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
3267 return 0;
3268
3269 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
3270}
ed1f651b
RS
3271\f
3272/* On fatal signals, delete all the temporary files. */
3273
3274static void
3275fatal_error (signum)
3276 int signum;
3277{
3278 signal (signum, SIG_DFL);
3279 delete_failure_queue ();
3280 delete_temp_files ();
3281 /* Get the same signal again, this time not handled,
3282 so its normal effect occurs. */
3283 kill (getpid (), signum);
3284}
3285
3286int
3287main (argc, argv)
3288 int argc;
3289 char **argv;
3290{
3291 register int i;
058d8521 3292 int j;
ed1f651b
RS
3293 int value;
3294 int error_count = 0;
3295 int linker_was_run = 0;
3296 char *explicit_link_files;
3297 char *specs_file;
3298
3299 programname = argv[0];
3300
3301 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3302 signal (SIGINT, fatal_error);
3303 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3304 signal (SIGHUP, fatal_error);
3305 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3306 signal (SIGTERM, fatal_error);
3307#ifdef SIGPIPE
3308 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3309 signal (SIGPIPE, fatal_error);
3310#endif
3311
3312 argbuf_length = 10;
3313 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3314
3315 obstack_init (&obstack);
3316
b3865ca9
RS
3317 /* Set up to remember the pathname of gcc and any options
3318 needed for collect. */
3319 obstack_init (&collect_obstack);
3320 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3321 obstack_grow (&collect_obstack, programname, strlen (programname)+1);
3322 putenv (obstack_finish (&collect_obstack));
3323
ed1f651b
RS
3324 /* Choose directory for temp files. */
3325
3326 choose_temp_base ();
3327
3328 /* Make a table of what switches there are (switches, n_switches).
3329 Make a table of specified input files (infiles, n_infiles).
3330 Decode switches that are handled locally. */
3331
3332 process_command (argc, argv);
3333
3334 /* Initialize the vector of specs to just the default.
3335 This means one element containing 0s, as a terminator. */
3336
3337 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3338 bcopy (default_compilers, compilers, sizeof default_compilers);
3339 n_compilers = n_default_compilers;
3340
3341 /* Read specs from a file if there is one. */
3342
3343 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
ae04227b 3344 just_machine_suffix = concat (spec_machine, "/", "");
ed1f651b
RS
3345
3346 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3347 /* Read the specs file unless it is a default one. */
3348 if (specs_file != 0 && strcmp (specs_file, "specs"))
3349 read_specs (specs_file);
3350
004fd4d5
RS
3351 /* If not cross-compiling, look for startfiles in the standard places. */
3352 /* The fact that these are done here, after reading the specs file,
3353 means that it cannot be found in these directories.
3354 But that's okay. It should never be there anyway. */
3355 if (!cross_compile)
3356 {
3357#ifdef MD_EXEC_PREFIX
906c4e36
RK
3358 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3359 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
004fd4d5
RS
3360#endif
3361
3362#ifdef MD_STARTFILE_PREFIX
906c4e36 3363 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
004fd4d5
RS
3364#endif
3365
607a4f7d 3366#ifdef MD_STARTFILE_PREFIX_1
906c4e36 3367 add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
607a4f7d
RS
3368#endif
3369
906c4e36
RK
3370 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3371 NULL_PTR);
3372 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3373 NULL_PTR);
3374 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3375 NULL_PTR);
004fd4d5 3376#if 0 /* Can cause surprises, and one can use -B./ instead. */
906c4e36 3377 add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
004fd4d5
RS
3378#endif
3379 }
3380
ed1f651b
RS
3381 /* Now we have the specs.
3382 Set the `valid' bits for switches that match anything in any spec. */
3383
3384 validate_all_switches ();
3385
3386 /* Warn about any switches that no pass was interested in. */
3387
3388 for (i = 0; i < n_switches; i++)
3389 if (! switches[i].valid)
3390 error ("unrecognized option `-%s'", switches[i].part1);
3391
2dcb563f
RS
3392 if (print_libgcc_file_name)
3393 {
3394 printf ("%s\n", find_file ("libgcc.a"));
3395 exit (0);
3396 }
3397
ed1f651b
RS
3398 /* Obey some of the options. */
3399
3400 if (verbose_flag)
3401 {
3402 fprintf (stderr, "gcc version %s\n", version_string);
3403 if (n_infiles == 0)
3404 exit (0);
3405 }
3406
3407 if (n_infiles == 0)
3408 fatal ("No input files specified.");
3409
3410 /* Make a place to record the compiler output file names
3411 that correspond to the input files. */
3412
3413 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3414 bzero (outfiles, n_infiles * sizeof (char *));
3415
3416 /* Record which files were specified explicitly as link input. */
3417
3418 explicit_link_files = xmalloc (n_infiles);
3419 bzero (explicit_link_files, n_infiles);
3420
3421 for (i = 0; i < n_infiles; i++)
3422 {
3423 register struct compiler *cp = 0;
3424 int this_file_error = 0;
3425
3426 /* Tell do_spec what to substitute for %i. */
3427
3428 input_filename = infiles[i].name;
3429 input_filename_length = strlen (input_filename);
3430 input_file_number = i;
3431
3432 /* Use the same thing in %o, unless cp->spec says otherwise. */
3433
3434 outfiles[i] = input_filename;
3435
3436 /* Figure out which compiler from the file's suffix. */
3437
3438 cp = lookup_compiler (infiles[i].name, input_filename_length,
3439 infiles[i].language);
3440
3441 if (cp)
3442 {
3443 /* Ok, we found an applicable compiler. Run its spec. */
3444 /* First say how much of input_filename to substitute for %b */
3445 register char *p;
ec32609a 3446 int len;
ed1f651b
RS
3447
3448 input_basename = input_filename;
3449 for (p = input_filename; *p; p++)
3450 if (*p == '/')
3451 input_basename = p + 1;
3452
3453 /* Find a suffix starting with the last period,
3454 and set basename_length to exclude that suffix. */
3455 basename_length = strlen (input_basename);
3456 p = input_basename + basename_length;
3457 while (p != input_basename && *p != '.') --p;
3458 if (*p == '.' && p != input_basename)
3459 {
3460 basename_length = p - input_basename;
3461 input_suffix = p + 1;
3462 }
3463 else
3464 input_suffix = "";
3465
ec32609a 3466 len = 0;
058d8521
RS
3467 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3468 if (cp->spec[j])
3469 len += strlen (cp->spec[j]);
ec32609a
RS
3470
3471 p = (char *) xmalloc (len + 1);
3472
3473 len = 0;
058d8521
RS
3474 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3475 if (cp->spec[j])
3476 {
3477 strcpy (p + len, cp->spec[j]);
3478 len += strlen (cp->spec[j]);
3479 }
ec32609a
RS
3480
3481 value = do_spec (p);
3482 free (p);
ed1f651b
RS
3483 if (value < 0)
3484 this_file_error = 1;
3485 }
3486
3487 /* If this file's name does not contain a recognized suffix,
3488 record it as explicit linker input. */
3489
3490 else
3491 explicit_link_files[i] = 1;
3492
3493 /* Clear the delete-on-failure queue, deleting the files in it
3494 if this compilation failed. */
3495
3496 if (this_file_error)
3497 {
3498 delete_failure_queue ();
3499 error_count++;
3500 }
3501 /* If this compilation succeeded, don't delete those files later. */
3502 clear_failure_queue ();
3503 }
3504
3505 /* Run ld to link all the compiler output files. */
3506
3507 if (error_count == 0)
3508 {
3509 int tmp = execution_count;
b3865ca9
RS
3510 int i;
3511 int first_time;
3512
3513 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3514 for collect. */
3515 putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3516 putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3517
3518 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3519 the compiler. */
3520 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3521 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3522
3523 first_time = TRUE;
3524 for (i = 0; i < n_switches; i++)
3525 {
3526 char **args;
3527 if (!first_time)
3528 obstack_grow (&collect_obstack, " ", 1);
3529
3530 first_time = FALSE;
3531 obstack_grow (&collect_obstack, "-", 1);
3532 obstack_grow (&collect_obstack, switches[i].part1,
3533 strlen (switches[i].part1));
3534
3535 for (args = switches[i].args; args && *args; args++)
3536 {
3537 obstack_grow (&collect_obstack, " ", 1);
3538 obstack_grow (&collect_obstack, *args, strlen (*args));
3539 }
3540 }
3541 obstack_grow (&collect_obstack, "\0", 1);
3542 putenv (obstack_finish (&collect_obstack));
3543
ed1f651b
RS
3544 value = do_spec (link_command_spec);
3545 if (value < 0)
3546 error_count = 1;
3547 linker_was_run = (tmp != execution_count);
3548 }
3549
3550 /* Warn if a -B option was specified but the prefix was never used. */
3551 unused_prefix_warnings (&exec_prefix);
3552 unused_prefix_warnings (&startfile_prefix);
3553
3554 /* If options said don't run linker,
3555 complain about input files to be given to the linker. */
3556
3557 if (! linker_was_run && error_count == 0)
3558 for (i = 0; i < n_infiles; i++)
3559 if (explicit_link_files[i])
3560 error ("%s: linker input file unused since linking not done",
3561 outfiles[i]);
3562
3563 /* Delete some or all of the temporary files we made. */
3564
3565 if (error_count)
3566 delete_failure_queue ();
3567 delete_temp_files ();
3568
3b9b4d3f 3569 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
ed1f651b
RS
3570 /* NOTREACHED */
3571 return 0;
3572}
3573
3574/* Find the proper compilation spec for the file name NAME,
004fd4d5 3575 whose length is LENGTH. LANGUAGE is the specified language,
ed1f651b
RS
3576 or 0 if none specified. */
3577
3578static struct compiler *
3579lookup_compiler (name, length, language)
3580 char *name;
3581 int length;
3582 char *language;
3583{
3584 struct compiler *cp;
3585
3586 /* Look for the language, if one is spec'd. */
3587 if (language != 0)
3588 {
3589 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3590 {
3591 if (language != 0)
3592 {
3593 if (cp->suffix[0] == '@'
3594 && !strcmp (cp->suffix + 1, language))
3595 return cp;
3596 }
3597 }
3598 error ("language %s not recognized", language);
3599 }
3600
3601 /* Look for a suffix. */
3602 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3603 {
3604 if (strlen (cp->suffix) < length
3605 /* See if the suffix matches the end of NAME. */
3606 && !strcmp (cp->suffix,
3607 name + length - strlen (cp->suffix))
3608 /* The suffix `-' matches only the file name `-'. */
3609 && !(!strcmp (cp->suffix, "-") && length != 1))
3610 {
ec32609a 3611 if (cp->spec[0][0] == '@')
ed1f651b
RS
3612 {
3613 struct compiler *new;
3614 /* An alias entry maps a suffix to a language.
3615 Search for the language; pass 0 for NAME and LENGTH
3616 to avoid infinite recursion if language not found.
3617 Construct the new compiler spec. */
ec32609a 3618 language = cp->spec[0] + 1;
ed1f651b
RS
3619 new = (struct compiler *) xmalloc (sizeof (struct compiler));
3620 new->suffix = cp->suffix;
ec32609a
RS
3621 bcopy (lookup_compiler (NULL_PTR, 0, language)->spec,
3622 new->spec, sizeof new->spec);
ed1f651b
RS
3623 return new;
3624 }
3625 /* A non-alias entry: return it. */
3626 return cp;
3627 }
3628 }
3629
3630 return 0;
3631}
3632\f
3633char *
3634xmalloc (size)
3635 unsigned size;
3636{
3637 register char *value = (char *) malloc (size);
3638 if (value == 0)
3639 fatal ("virtual memory exhausted");
3640 return value;
3641}
3642
3643char *
3644xrealloc (ptr, size)
3645 char *ptr;
3646 unsigned size;
3647{
3648 register char *value = (char *) realloc (ptr, size);
3649 if (value == 0)
3650 fatal ("virtual memory exhausted");
3651 return value;
3652}
3653
3654/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3655
3656static char *
3657concat (s1, s2, s3)
3658 char *s1, *s2, *s3;
3659{
3660 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3661 char *result = xmalloc (len1 + len2 + len3 + 1);
3662
3663 strcpy (result, s1);
3664 strcpy (result + len1, s2);
3665 strcpy (result + len1 + len2, s3);
3666 *(result + len1 + len2 + len3) = 0;
3667
3668 return result;
3669}
3670
3671static char *
3672save_string (s, len)
3673 char *s;
3674 int len;
3675{
3676 register char *result = xmalloc (len + 1);
3677
3678 bcopy (s, result, len);
3679 result[len] = 0;
3680 return result;
3681}
3682
3683static void
3684pfatal_with_name (name)
3685 char *name;
3686{
3687 char *s;
3688
3689 if (errno < sys_nerr)
3690 s = concat ("%s: ", sys_errlist[errno], "");
3691 else
3692 s = "cannot open %s";
3693 fatal (s, name);
3694}
3695
3696static void
3697perror_with_name (name)
3698 char *name;
3699{
3700 char *s;
3701
3702 if (errno < sys_nerr)
3703 s = concat ("%s: ", sys_errlist[errno], "");
3704 else
3705 s = "cannot open %s";
3706 error (s, name);
3707}
3708
3709static void
3710perror_exec (name)
3711 char *name;
3712{
3713 char *s;
3714
3715 if (errno < sys_nerr)
3716 s = concat ("installation problem, cannot exec %s: ",
3717 sys_errlist[errno], "");
3718 else
3719 s = "installation problem, cannot exec %s";
3720 error (s, name);
3721}
3722
3723/* More 'friendly' abort that prints the line and file.
3724 config.h can #define abort fancy_abort if you like that sort of thing. */
3725
3726void
3727fancy_abort ()
3728{
3729 fatal ("Internal gcc abort.");
3730}
3731\f
3732#ifdef HAVE_VPRINTF
3733
3734/* Output an error message and exit */
3735
3736static void
3737fatal (va_alist)
3738 va_dcl
3739{
3740 va_list ap;
3741 char *format;
3742
3743 va_start (ap);
3744 format = va_arg (ap, char *);
3745 fprintf (stderr, "%s: ", programname);
3746 vfprintf (stderr, format, ap);
3747 va_end (ap);
3748 fprintf (stderr, "\n");
3749 delete_temp_files ();
3750 exit (1);
3751}
3752
3753static void
3754error (va_alist)
3755 va_dcl
3756{
3757 va_list ap;
3758 char *format;
3759
3760 va_start (ap);
3761 format = va_arg (ap, char *);
3762 fprintf (stderr, "%s: ", programname);
3763 vfprintf (stderr, format, ap);
3764 va_end (ap);
3765
3766 fprintf (stderr, "\n");
3767}
3768
3769#else /* not HAVE_VPRINTF */
3770
3771static void
3772fatal (msg, arg1, arg2)
3773 char *msg, *arg1, *arg2;
3774{
3775 error (msg, arg1, arg2);
3776 delete_temp_files ();
3777 exit (1);
3778}
3779
3780static void
3781error (msg, arg1, arg2)
3782 char *msg, *arg1, *arg2;
3783{
3784 fprintf (stderr, "%s: ", programname);
3785 fprintf (stderr, msg, arg1, arg2);
3786 fprintf (stderr, "\n");
3787}
3788
3789#endif /* not HAVE_VPRINTF */
3790
3791\f
3792static void
3793validate_all_switches ()
3794{
3795 struct compiler *comp;
3796 register char *p;
3797 register char c;
b3865ca9 3798 struct spec_list *spec;
ed1f651b 3799
ec32609a 3800 for (comp = compilers; comp->spec[0]; comp++)
ed1f651b 3801 {
ec32609a 3802 int i;
20eec2c2 3803 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
ec32609a
RS
3804 {
3805 p = comp->spec[i];
3806 while (c = *p++)
3807 if (c == '%' && *p == '{')
3808 /* We have a switch spec. */
3809 validate_switches (p + 1);
3810 }
ed1f651b
RS
3811 }
3812
b3865ca9 3813 /* look through the linked list of extra specs read from the specs file */
ec32609a 3814 for (spec = specs; spec ; spec = spec->next)
b3865ca9
RS
3815 {
3816 p = spec->spec;
3817 while (c = *p++)
3818 if (c == '%' && *p == '{')
3819 /* We have a switch spec. */
3820 validate_switches (p + 1);
3821 }
3822
ed1f651b
RS
3823 p = link_command_spec;
3824 while (c = *p++)
3825 if (c == '%' && *p == '{')
3826 /* We have a switch spec. */
3827 validate_switches (p + 1);
3828
3829 /* Now notice switches mentioned in the machine-specific specs. */
3830
3831 p = asm_spec;
3832 while (c = *p++)
3833 if (c == '%' && *p == '{')
3834 /* We have a switch spec. */
3835 validate_switches (p + 1);
3836
3837 p = asm_final_spec;
3838 while (c = *p++)
3839 if (c == '%' && *p == '{')
3840 /* We have a switch spec. */
3841 validate_switches (p + 1);
3842
3843 p = cpp_spec;
3844 while (c = *p++)
3845 if (c == '%' && *p == '{')
3846 /* We have a switch spec. */
3847 validate_switches (p + 1);
3848
3849 p = signed_char_spec;
3850 while (c = *p++)
3851 if (c == '%' && *p == '{')
3852 /* We have a switch spec. */
3853 validate_switches (p + 1);
3854
3855 p = cc1_spec;
3856 while (c = *p++)
3857 if (c == '%' && *p == '{')
3858 /* We have a switch spec. */
3859 validate_switches (p + 1);
3860
3861 p = cc1plus_spec;
3862 while (c = *p++)
3863 if (c == '%' && *p == '{')
3864 /* We have a switch spec. */
3865 validate_switches (p + 1);
3866
3867 p = link_spec;
3868 while (c = *p++)
3869 if (c == '%' && *p == '{')
3870 /* We have a switch spec. */
3871 validate_switches (p + 1);
3872
3873 p = lib_spec;
3874 while (c = *p++)
3875 if (c == '%' && *p == '{')
3876 /* We have a switch spec. */
3877 validate_switches (p + 1);
3878
3879 p = startfile_spec;
3880 while (c = *p++)
3881 if (c == '%' && *p == '{')
3882 /* We have a switch spec. */
3883 validate_switches (p + 1);
3884}
3885
3886/* Look at the switch-name that comes after START
3887 and mark as valid all supplied switches that match it. */
3888
3889static void
3890validate_switches (start)
3891 char *start;
3892{
3893 register char *p = start;
3894 char *filter;
3895 register int i;
3896 int suffix = 0;
3897
3898 if (*p == '|')
3899 ++p;
3900
3901 if (*p == '!')
3902 ++p;
3903
3904 if (*p == '.')
3905 suffix = 1, ++p;
3906
3907 filter = p;
3908 while (*p != ':' && *p != '}') p++;
3909
3910 if (suffix)
3911 ;
3912 else if (p[-1] == '*')
3913 {
3914 /* Mark all matching switches as valid. */
3915 --p;
3916 for (i = 0; i < n_switches; i++)
3917 if (!strncmp (switches[i].part1, filter, p - filter))
3918 switches[i].valid = 1;
3919 }
3920 else
3921 {
3922 /* Mark an exact matching switch as valid. */
3923 for (i = 0; i < n_switches; i++)
3924 {
3925 if (!strncmp (switches[i].part1, filter, p - filter)
3926 && switches[i].part1[p - filter] == 0)
3927 switches[i].valid = 1;
3928 }
3929 }
3930}
This page took 0.503358 seconds and 5 git commands to generate.