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