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