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