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