1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 This paragraph is here to try to keep Sun CC from dying.
21 The number of chars here seems crucial!!!! */
23 /* This program is the user interface to the C compiler and possibly to
24 other compilers. It is used because compilation is a complicated procedure
25 which involves running several programs and passing temporary files between
26 them, forwarding the users switches to those programs selectively,
27 and deleting the temporary files at the end.
29 CC recognizes how to compile each input file by suffixes in the file names.
30 Once it knows which kind of compilation to perform, the procedure for
31 compilation is specified by a string called a "spec". */
34 #include <sys/types.h>
53 /* On MSDOS, write temp files in current dir
54 because there's no place else we can expect to use. */
61 /* Test if something is a normal file. */
63 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
66 /* Test if something is a directory. */
68 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
71 /* By default there is no special suffix for executables. */
72 #ifndef EXECUTABLE_SUFFIX
74 #define EXECUTABLE_SUFFIX ".exe"
76 #define EXECUTABLE_SUFFIX ""
80 #define obstack_chunk_alloc xmalloc
81 #define obstack_chunk_free free
84 extern char *getenv ();
86 extern int errno, sys_nerr;
87 extern char *sys_errlist[];
89 extern int execv (), execvp ();
91 /* If a stage of compilation returns an exit status >= 1,
92 compilation of that file ceases. */
94 #define MIN_FATAL_STATUS 1
96 /* Flag indicating whether we should print the command and arguments */
98 static int verbose_flag;
100 /* Nonzero means write "temp" files in source directory
101 and use the source file's name in them, and don't delete them. */
103 static int save_temps_flag;
105 /* The compiler version specified with -V */
107 static char *spec_version;
109 /* The target machine specified with -b. */
111 static char *spec_machine = DEFAULT_TARGET_MACHINE;
113 /* Nonzero if cross-compiling.
114 When -b is used, the value comes from the `specs' file. */
117 static int cross_compile = 1;
119 static int cross_compile = 0;
122 /* This is the obstack which we use to allocate many strings. */
124 static struct obstack obstack;
126 /* This is the obstack to build an environment variable to pass to
127 collect2 that describes all of the relavant switches of what to
128 pass the compiler in building the list of pointers to constructors
131 static struct obstack collect_obstack;
133 extern char *version_string;
135 static void set_spec ();
136 static struct compiler *lookup_compiler ();
137 static char *find_a_file ();
138 static void add_prefix ();
139 static char *skip_whitespace ();
140 static void record_temp_file ();
141 static char *handle_braces ();
142 static char *save_string ();
143 static char *concat ();
144 static int do_spec ();
145 static int do_spec_1 ();
146 static char *find_file ();
147 static int is_linker_dir ();
148 static void validate_switches ();
149 static void validate_all_switches ();
150 static void give_switch ();
151 static void pfatal_with_name ();
152 static void perror_with_name ();
153 static void perror_exec ();
154 static void fatal ();
155 static void error ();
160 /* Specs are strings containing lines, each of which (if not blank)
161 is made up of a program name, and arguments separated by spaces.
162 The program name must be exact and start from root, since no path
163 is searched and it is unreliable to depend on the current working directory.
164 Redirection of input or output is not supported; the subprograms must
165 accept filenames saying what files to read and write.
167 In addition, the specs can contain %-sequences to substitute variable text
168 or for conditional text. Here is a table of all defined %-sequences.
169 Note that spaces are not generated automatically around the results of
170 expanding these sequences; therefore, you can concatenate them together
171 or with constant text in a single argument.
173 %% substitute one % into the program name or argument.
174 %i substitute the name of the input file being processed.
175 %b substitute the basename of the input file being processed.
176 This is the substring up to (and not including) the last period
177 and not including the directory.
178 %g substitute the temporary-file-name-base. This is a string chosen
179 once per compilation. Different temporary file names are made by
180 concatenation of constant strings on the end, as in `%g.s'.
181 %g also has the same effect of %d.
182 %d marks the argument containing or following the %d as a
183 temporary file name, so that that file will be deleted if CC exits
184 successfully. Unlike %g, this contributes no text to the argument.
185 %w marks the argument containing or following the %w as the
186 "output file" of this compilation. This puts the argument
187 into the sequence of arguments that %o will substitute later.
189 like %{...} but mark last argument supplied within
190 as a file to be deleted on failure.
191 %o substitutes the names of all the output files, with spaces
192 automatically placed around them. You should write spaces
193 around the %o as well or the results are undefined.
194 %o is for use in the specs for running the linker.
195 Input files whose names have no recognized suffix are not compiled
196 at all, but they are included among the output files, so they will
198 %p substitutes the standard macro predefinitions for the
199 current target machine. Use this when running cpp.
200 %P like %p, but puts `__' before and after the name of each macro.
201 (Except macros that already have __.)
203 %s current argument is the name of a library or startup file of some sort.
204 Search for that file in a standard list of directories
205 and substitute the full name found.
206 %eSTR Print STR as an error message. STR is terminated by a newline.
207 Use this when inconsistent options are detected.
208 %x{OPTION} Accumulate an option for %X.
209 %X Output the accumulated linker options specified by compilations.
210 %a process ASM_SPEC as a spec.
211 This allows config.h to specify part of the spec for running as.
212 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
213 used here. This can be used to run a post-processor after the
214 assembler has done it's job.
215 %D Dump out a -L option for each directory in library_prefix,
216 followed by a -L option for each directory in startfile_prefix.
217 %l process LINK_SPEC as a spec.
218 %L process LIB_SPEC as a spec.
219 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
220 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
221 %c process SIGNED_CHAR_SPEC as a spec.
222 %C process CPP_SPEC as a spec. A capital C is actually used here.
223 %1 process CC1_SPEC as a spec.
224 %2 process CC1PLUS_SPEC as a spec.
225 %* substitute the variable part of a matched option. (See below.)
226 Note that each comma in the substituted string is replaced by
228 %{S} substitutes the -S switch, if that switch was given to CC.
229 If that switch was not specified, this substitutes nothing.
230 Here S is a metasyntactic variable.
231 %{S*} substitutes all the switches specified to CC whose names start
232 with -S. This is used for -o, -D, -I, etc; switches that take
233 arguments. CC considers `-o foo' as being one switch whose
234 name starts with `o'. %{o*} would substitute this text,
235 including the space; thus, two arguments would be generated.
236 %{S*:X} substitutes X if one or more switches whose names with -S are
237 specified to CC. Note that the tail part of the -S option
238 (i.e. the part matched by the `*') will be substituted for each
239 occurance of %* within X.
240 %{S:X} substitutes X, but only if the -S switch was given to CC.
241 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
242 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
243 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
244 %{.S:X} substitutes X, but only if processing a file with suffix S.
245 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
246 %(Spec) processes a specification defined in a specs file as *Spec:
247 %[Spec] as above, but put __ around -D arguments
249 The conditional text X in a %{S:X} or %{!S:X} construct may contain
250 other nested % constructs or spaces, or even newlines. They are
251 processed as usual, as described above.
253 The character | is used to indicate that a command should be piped to
254 the following command, but only if -pipe is specified.
256 Note that it is built into CC which switches take arguments and which
257 do not. You might think it would be useful to generalize this to
258 allow each compiler's spec to say which switches take arguments. But
259 this cannot be done in a consistent fashion. CC cannot even decide
260 which input files have been specified without knowing which switches
261 take arguments, and it must know which input files to compile in order
262 to tell which compilers to run.
264 CC also knows implicitly that arguments starting in `-l' are to be
265 treated as compiler output files, and passed to the linker in their
266 proper position among the other output files. */
268 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
270 /* config.h can define ASM_SPEC to provide extra args to the assembler
271 or extra switch-translations. */
276 /* config.h can define ASM_FINAL_SPEC to run a post processor after
277 the assembler has run. */
278 #ifndef ASM_FINAL_SPEC
279 #define ASM_FINAL_SPEC ""
282 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
283 or extra switch-translations. */
288 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
289 or extra switch-translations. */
294 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
295 or extra switch-translations. */
297 #define CC1PLUS_SPEC ""
300 /* config.h can define LINK_SPEC to provide extra args to the linker
301 or extra switch-translations. */
306 /* config.h can define LIB_SPEC to override the default libraries. */
308 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
311 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
312 #ifndef STARTFILE_SPEC
313 #define STARTFILE_SPEC \
314 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
317 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
318 Make the string nonempty to require spaces there. */
319 #ifndef SWITCHES_NEED_SPACES
320 #define SWITCHES_NEED_SPACES ""
323 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
325 #define ENDFILE_SPEC ""
328 /* This spec is used for telling cpp whether char is signed or not. */
329 #ifndef SIGNED_CHAR_SPEC
330 #define SIGNED_CHAR_SPEC \
331 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
332 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
335 static char *cpp_spec = CPP_SPEC;
336 static char *cpp_predefines = CPP_PREDEFINES;
337 static char *cc1_spec = CC1_SPEC;
338 static char *cc1plus_spec = CC1PLUS_SPEC;
339 static char *signed_char_spec = SIGNED_CHAR_SPEC;
340 static char *asm_spec = ASM_SPEC;
341 static char *asm_final_spec = ASM_FINAL_SPEC;
342 static char *link_spec = LINK_SPEC;
343 static char *lib_spec = LIB_SPEC;
344 static char *endfile_spec = ENDFILE_SPEC;
345 static char *startfile_spec = STARTFILE_SPEC;
346 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
348 /* This defines which switch letters take arguments. */
350 #ifndef SWITCH_TAKES_ARG
351 #define SWITCH_TAKES_ARG(CHAR) \
352 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
353 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
354 || (CHAR) == 'I' || (CHAR) == 'm' \
355 || (CHAR) == 'L' || (CHAR) == 'A')
358 /* This defines which multi-letter switches take arguments. */
360 #ifndef WORD_SWITCH_TAKES_ARG
361 #define WORD_SWITCH_TAKES_ARG(STR) \
362 (!strcmp (STR, "Tdata") || !strcmp (STR, "include") \
363 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
366 /* Record the mapping from file suffixes for compilation specs. */
370 char *suffix; /* Use this compiler for input files
371 whose names end in this suffix. */
372 char *spec; /* To use this compiler, pass this spec
376 /* Pointer to a vector of `struct compiler' that gives the spec for
377 compiling a file, based on its suffix.
378 A file that does not end in any of these suffixes will be passed
379 unchanged to the loader and nothing else will be done to it.
381 An entry containing two 0s is used to terminate the vector.
383 If multiple entries match a file, the last matching one is used. */
385 static struct compiler *compilers;
387 /* Number of entries in `compilers', not counting the null terminator. */
389 static int n_compilers;
391 /* The default list of file name suffixes and their compilation specs. */
393 static struct compiler default_compilers[] =
397 "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
398 %{C:%{!E:%eGNU C does not support -C without using -E}}\
399 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
400 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
401 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
402 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
403 %{traditional-cpp:-traditional}\
404 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
405 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
406 %{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
407 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
408 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
409 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
411 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
412 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
413 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
414 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
415 %{!pipe:%g.s} %A\n }}}}"},
417 "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
418 %{C:%{!E:%eGNU C does not support -C without using -E}}\
419 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
420 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
421 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
422 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
423 %{traditional-cpp:-traditional}\
424 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
426 %{!E:%e-E required when input is from standard input}"},
427 {".m", "@objective-c"},
429 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
430 %{C:%{!E:%eGNU C does not support -C without using -E}}\
431 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
432 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
433 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
434 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
435 %{traditional-cpp:-traditional}\
436 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
437 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
438 %{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
439 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
440 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
441 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
442 -lang-objc %{gen-decls} \
444 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
445 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
446 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
447 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
448 %{!pipe:%g.s} %A\n }}}}"},
451 "%{!E:%eCompilation of header file requested} \
452 cpp %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
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\
465 "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
466 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
467 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
468 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
469 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
470 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
471 %{traditional-cpp:-traditional} %{trigraphs}\
472 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
473 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
474 %{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
475 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
476 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
477 %{v:-version} %{pg:-p} %{p} %{f*}\
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 \
482 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
483 %{!pipe:%g.s} %A\n }}}}"},
484 {".i", "@cpp-output"},
486 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
487 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
488 %{v:-version} %{pg:-p} %{p} %{f*}\
490 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
491 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
492 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
493 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s} %A\n }"},
494 {".ii", "@c++-cpp-output"},
496 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
497 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
498 %{v:-version} %{pg:-p} %{p} %{f*}\
500 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
501 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
502 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
503 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
504 %{!pipe:%g.s} %A\n }"},
505 {".s", "@assembler"},
507 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
508 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i %A\n }"},
509 {".S", "@assembler-with-cpp"},
510 {"@assembler-with-cpp",
511 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
512 %{C:%{!E:%eGNU C does not support -C without using -E}}\
513 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
514 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
515 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
516 %{traditional-cpp:-traditional}\
517 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
518 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
519 %{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
520 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
521 %{!pipe:%g.s} %A\n }}}}"},
522 /* Mark end of table */
526 /* Number of elements in default_compilers, not counting the terminator. */
528 static int n_default_compilers
529 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
531 /* Here is the spec for running the linker, after compiling all files. */
533 #ifdef LINK_LIBGCC_SPECIAL
534 /* Have gcc do the search. */
535 /* -u* was put back because both BSD and SysV seem to support it. */
536 static char *link_command_spec = "\
537 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
538 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
539 %{!A:%{!nostdlib:%S}} \
540 %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
542 /* Use -l and have the linker do the search. */
543 static char *link_command_spec = "\
544 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
545 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
546 %{!A:%{!nostdlib:%S}} \
547 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
550 /* A vector of options to give to the linker.
551 These options are accumlated by %x
552 and substituted into the linker command with %X. */
553 static int n_linker_options;
554 static char **linker_options;
556 /* Read compilation specs from a file named FILENAME,
557 replacing the default ones.
559 A suffix which starts with `*' is a definition for
560 one of the machine-specific sub-specs. The "suffix" should be
561 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
562 The corresponding spec is stored in asm_spec, etc.,
563 rather than in the `compilers' vector.
565 Anything invalid in the file is a fatal error. */
568 read_specs (filename)
577 fprintf (stderr, "Reading specs from %s\n", filename);
579 /* Open and stat the file. */
580 desc = open (filename, 0, 0);
582 pfatal_with_name (filename);
583 if (stat (filename, &statbuf) < 0)
584 pfatal_with_name (filename);
586 /* Read contents of file into BUFFER. */
587 buffer = xmalloc (statbuf.st_size + 1);
588 read (desc, buffer, statbuf.st_size);
589 buffer[statbuf.st_size] = 0;
592 /* Scan BUFFER for specs, putting them in the vector. */
598 char *in, *out, *p1, *p2;
600 /* Advance P in BUFFER to the next nonblank nocomment line. */
601 p = skip_whitespace (p);
605 /* Find the colon that should end the suffix. */
607 while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
608 /* The colon shouldn't be missing. */
610 fatal ("specs file malformed after %d characters", p1 - buffer);
611 /* Skip back over trailing whitespace. */
613 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
614 /* Copy the suffix to a string. */
615 suffix = save_string (p, p2 - p);
616 /* Find the next line. */
617 p = skip_whitespace (p1 + 1);
619 fatal ("specs file malformed after %d characters", p - buffer);
621 /* Find next blank line. */
622 while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
623 /* Specs end at the blank line and do not include the newline. */
624 spec = save_string (p, p1 - p);
627 /* Delete backslash-newline sequences from the spec. */
632 if (in[0] == '\\' && in[1] == '\n')
634 else if (in[0] == '#')
636 while (*in && *in != '\n') in++;
643 if (suffix[0] == '*')
645 if (! strcmp (suffix, "*link_command"))
646 link_command_spec = spec;
648 set_spec (suffix + 1, spec);
652 /* Add this pair to the vector. */
654 = ((struct compiler *)
655 xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
656 compilers[n_compilers].suffix = suffix;
657 compilers[n_compilers].spec = spec;
662 link_command_spec = spec;
665 if (link_command_spec == 0)
666 fatal ("spec file has no spec for linking");
675 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
676 be considered whitespace. */
677 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
679 else if (*p == '\n' || *p == ' ' || *p == '\t')
683 while (*p != '\n') p++;
693 /* Structure to keep track of the specs that have been defined so far. These
694 are accessed using %(specname) or %[specname] in a compiler or link spec. */
698 char *name; /* Name of the spec. */
699 char *spec; /* The spec itself. */
700 struct spec_list *next; /* Next spec in linked list. */
703 /* List of specs that have been defined so far. */
705 static struct spec_list *specs = (struct spec_list *) 0;
707 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
708 removed; If the spec starts with a + then SPEC is added to the end of the
712 set_spec (name, spec)
716 struct spec_list *sl;
719 /* See if the spec already exists */
720 for (sl = specs; sl; sl = sl->next)
721 if (strcmp (sl->name, name) == 0)
726 /* Not found - make it */
727 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
728 sl->name = save_string (name, strlen (name));
729 sl->spec = save_string ("", 0);
735 if (name && spec[0] == '+' && isspace (spec[1]))
736 sl->spec = concat (name, spec + 1, "");
738 sl->spec = save_string (spec, strlen (spec));
740 if (! strcmp (name, "asm"))
742 else if (! strcmp (name, "asm_final"))
743 asm_final_spec = sl->spec;
744 else if (! strcmp (name, "cc1"))
746 else if (! strcmp (name, "cc1plus"))
747 cc1plus_spec = sl->spec;
748 else if (! strcmp (name, "cpp"))
750 else if (! strcmp (name, "endfile"))
751 endfile_spec = sl->spec;
752 else if (! strcmp (name, "lib"))
754 else if (! strcmp (name, "link"))
755 link_spec = sl->spec;
756 else if (! strcmp (name, "predefines"))
757 cpp_predefines = sl->spec;
758 else if (! strcmp (name, "signed_char"))
759 signed_char_spec = sl->spec;
760 else if (! strcmp (name, "startfile"))
761 startfile_spec = sl->spec;
762 else if (! strcmp (name, "switches_need_spaces"))
763 switches_need_spaces = sl->spec;
764 else if (! strcmp (name, "cross_compile"))
765 cross_compile = atoi (sl->spec);
766 /* Free the old spec */
771 /* Accumulate a command (program name and args), and run it. */
773 /* Vector of pointers to arguments in the current line of specifications. */
775 static char **argbuf;
777 /* Number of elements allocated in argbuf. */
779 static int argbuf_length;
781 /* Number of elements in argbuf currently in use (containing args). */
783 static int argbuf_index;
785 /* Number of commands executed so far. */
787 static int execution_count;
789 /* Name with which this program was invoked. */
791 static char *programname;
793 /* Structures to keep track of prefixes to try when looking for files. */
797 char *prefix; /* String to prepend to the path. */
798 struct prefix_list *next; /* Next in linked list. */
799 int require_machine_suffix; /* Don't use without machine_suffix. */
800 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
805 struct prefix_list *plist; /* List of prefixes to try */
806 int max_len; /* Max length of a prefix in PLIST */
807 char *name; /* Name of this list (used in config stuff) */
810 /* List of prefixes to try when looking for executables. */
812 static struct path_prefix exec_prefix = { 0, 0, "exec" };
814 /* List of prefixes to try when looking for startup (crt0) files. */
816 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
818 /* List of prefixes to try when looking for libraries. */
820 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
822 /* Suffix to attach to directories searched for commands. */
824 static char *machine_suffix = 0;
826 /* Default prefixes to attach to command names. */
828 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
829 #undef MD_EXEC_PREFIX
830 #undef MD_STARTFILE_PREFIX
833 #ifndef STANDARD_EXEC_PREFIX
834 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
835 #endif /* !defined STANDARD_EXEC_PREFIX */
837 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
838 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
839 #ifdef MD_EXEC_PREFIX
840 static char *md_exec_prefix = MD_EXEC_PREFIX;
843 #ifndef STANDARD_STARTFILE_PREFIX
844 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
845 #endif /* !defined STANDARD_STARTFILE_PREFIX */
847 #ifdef MD_STARTFILE_PREFIX
848 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
850 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
851 static char *standard_startfile_prefix_1 = "/lib/";
852 static char *standard_startfile_prefix_2 = "/usr/lib/";
854 /* Clear out the vector of arguments (after a command is executed). */
862 /* Add one argument to the vector at the end.
863 This is done when a space is seen or at the end of the line.
864 If DELETE_ALWAYS is nonzero, the arg is a filename
865 and the file should be deleted eventually.
866 If DELETE_FAILURE is nonzero, the arg is a filename
867 and the file should be deleted if this compilation fails. */
870 store_arg (arg, delete_always, delete_failure)
872 int delete_always, delete_failure;
874 if (argbuf_index + 1 == argbuf_length)
876 argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
879 argbuf[argbuf_index++] = arg;
880 argbuf[argbuf_index] = 0;
882 if (delete_always || delete_failure)
883 record_temp_file (arg, delete_always, delete_failure);
886 /* Record the names of temporary files we tell compilers to write,
887 and delete them at the end of the run. */
889 /* This is the common prefix we use to make temp file names.
890 It is chosen once for each run of this program.
891 It is substituted into a spec by %g.
892 Thus, all temp file names contain this prefix.
893 In practice, all temp file names start with this prefix.
895 This prefix comes from the envvar TMPDIR if it is defined;
896 otherwise, from the P_tmpdir macro if that is defined;
897 otherwise, in /usr/tmp or /tmp. */
899 static char *temp_filename;
901 /* Length of the prefix. */
903 static int temp_filename_length;
905 /* Define the list of temporary files to delete. */
910 struct temp_file *next;
913 /* Queue of files to delete on success or failure of compilation. */
914 static struct temp_file *always_delete_queue;
915 /* Queue of files to delete on failure of compilation. */
916 static struct temp_file *failure_delete_queue;
918 /* Record FILENAME as a file to be deleted automatically.
919 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
920 otherwise delete it in any case.
921 FAIL_DELETE nonzero means delete it if a compilation step fails;
922 otherwise delete it in any case. */
925 record_temp_file (filename, always_delete, fail_delete)
931 name = xmalloc (strlen (filename) + 1);
932 strcpy (name, filename);
936 register struct temp_file *temp;
937 for (temp = always_delete_queue; temp; temp = temp->next)
938 if (! strcmp (name, temp->name))
940 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
941 temp->next = always_delete_queue;
943 always_delete_queue = temp;
949 register struct temp_file *temp;
950 for (temp = failure_delete_queue; temp; temp = temp->next)
951 if (! strcmp (name, temp->name))
953 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
954 temp->next = failure_delete_queue;
956 failure_delete_queue = temp;
961 /* Delete all the temporary files whose names we previously recorded. */
966 register struct temp_file *temp;
968 for (temp = always_delete_queue; temp; temp = temp->next)
972 printf ("Delete %s? (y or n) ", temp->name);
976 while (getchar () != '\n') ;
977 if (i == 'y' || i == 'Y')
981 if (stat (temp->name, &st) >= 0)
983 /* Delete only ordinary files. */
984 if (S_ISREG (st.st_mode))
985 if (unlink (temp->name) < 0)
987 perror_with_name (temp->name);
992 always_delete_queue = 0;
995 /* Delete all the files to be deleted on error. */
998 delete_failure_queue ()
1000 register struct temp_file *temp;
1002 for (temp = failure_delete_queue; temp; temp = temp->next)
1006 printf ("Delete %s? (y or n) ", temp->name);
1010 while (getchar () != '\n') ;
1011 if (i == 'y' || i == 'Y')
1014 if (unlink (temp->name) < 0)
1016 perror_with_name (temp->name);
1022 clear_failure_queue ()
1024 failure_delete_queue = 0;
1027 /* Compute a string to use as the base of all temporary file names.
1028 It is substituted for %g. */
1033 char *base = getenv ("TMPDIR");
1036 if (base == (char *)0)
1039 if (access (P_tmpdir, R_OK | W_OK) == 0)
1042 if (base == (char *)0)
1044 if (access ("/usr/tmp", R_OK | W_OK) == 0)
1051 len = strlen (base);
1052 temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1053 strcpy (temp_filename, base);
1054 if (len > 0 && temp_filename[len-1] != '/')
1055 temp_filename[len++] = '/';
1056 strcpy (temp_filename + len, "ccXXXXXX");
1058 mktemp (temp_filename);
1059 temp_filename_length = strlen (temp_filename);
1063 /* Routine to add variables to the environment. We do this to pass
1064 the pathname of the gcc driver, and the directories search to the
1065 collect2 program, which is being run as ld. This way, we can be
1066 sure of executing the right compiler when collect2 wants to build
1067 constructors and destructors. Since the environment variables we
1068 use come from an obstack, we don't have to worry about allocating
1076 #ifndef __MSDOS__ /* not sure about MS/DOS */
1077 #ifndef VMS /* nor about VMS */
1079 extern char **environ;
1080 char **old_environ = environ;
1084 int str_len = strlen (str);
1088 while ((ch = *p++) != '\0' && ch != '=')
1094 /* Search for replacing an existing environment variable, and
1095 count the number of total environment variables. */
1096 for (envp = old_environ; *envp; envp++)
1099 if (!strncmp (str, *envp, name_len))
1106 /* Add a new environment variable */
1107 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1109 bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1112 #endif /* __MSDOS__ */
1115 #endif /* HAVE_PUTENV */
1118 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1121 putenv_from_prefixes (paths, env_var)
1122 struct path_prefix *paths;
1125 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1126 int first_time = TRUE;
1127 struct prefix_list *pprefix;
1129 obstack_grow (&collect_obstack, env_var, strlen (env_var));
1131 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1133 int len = strlen (pprefix->prefix);
1138 obstack_grow (&collect_obstack, ":", 1);
1141 obstack_grow (&collect_obstack, pprefix->prefix, len);
1142 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1145 if (!pprefix->require_machine_suffix)
1148 obstack_grow (&collect_obstack, ":", 1);
1151 obstack_grow (&collect_obstack, pprefix->prefix, len);
1154 obstack_grow (&collect_obstack, "\0", 1);
1155 putenv (obstack_finish (&collect_obstack));
1159 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1160 access to check permissions.
1161 Return 0 if not found, otherwise return its name, allocated with malloc. */
1164 find_a_file (pprefix, name, mode)
1165 struct path_prefix *pprefix;
1170 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1171 struct prefix_list *pl;
1172 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1175 len += strlen (machine_suffix);
1177 temp = xmalloc (len);
1179 /* Determine the filename to execute (special case for absolute paths). */
1183 if (access (name, mode))
1185 strcpy (temp, name);
1190 for (pl = pprefix->plist; pl; pl = pl->next)
1194 strcpy (temp, pl->prefix);
1195 strcat (temp, machine_suffix);
1196 strcat (temp, name);
1197 if (access (temp, mode) == 0)
1199 if (pl->used_flag_ptr != 0)
1200 *pl->used_flag_ptr = 1;
1203 /* Some systems have a suffix for executable files.
1204 So try appending that. */
1205 if (file_suffix[0] != 0)
1207 strcat (temp, file_suffix);
1208 if (access (temp, mode) == 0)
1210 if (pl->used_flag_ptr != 0)
1211 *pl->used_flag_ptr = 1;
1216 /* Certain prefixes can't be used without the machine suffix
1217 when the machine or version is explicitly specified. */
1218 if (!pl->require_machine_suffix)
1220 strcpy (temp, pl->prefix);
1221 strcat (temp, name);
1222 if (access (temp, mode) == 0)
1224 if (pl->used_flag_ptr != 0)
1225 *pl->used_flag_ptr = 1;
1228 /* Some systems have a suffix for executable files.
1229 So try appending that. */
1230 if (file_suffix[0] != 0)
1232 strcat (temp, file_suffix);
1233 if (access (temp, mode) == 0)
1235 if (pl->used_flag_ptr != 0)
1236 *pl->used_flag_ptr = 1;
1247 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1248 at the start of the list, otherwise it goes at the end.
1250 If WARN is nonzero, we will warn if no file is found
1251 through this prefix. WARN should point to an int
1252 which will be set to 1 if this entry is used. */
1255 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1256 struct path_prefix *pprefix;
1259 int require_machine_suffix;
1262 struct prefix_list *pl, **prev;
1265 if (!first && pprefix->plist)
1267 for (pl = pprefix->plist; pl->next; pl = pl->next)
1272 prev = &pprefix->plist;
1274 /* Keep track of the longest prefix */
1276 len = strlen (prefix);
1277 if (len > pprefix->max_len)
1278 pprefix->max_len = len;
1280 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1281 pl->prefix = save_string (prefix, len);
1282 pl->require_machine_suffix = require_machine_suffix;
1283 pl->used_flag_ptr = warn;
1290 pl->next = (struct prefix_list *) 0;
1294 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1297 unused_prefix_warnings (pprefix)
1298 struct path_prefix *pprefix;
1300 struct prefix_list *pl = pprefix->plist;
1304 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1306 error ("file path prefix `%s' never used",
1308 /* Prevent duplicate warnings. */
1309 *pl->used_flag_ptr = 1;
1315 /* Get rid of all prefixes built up so far in *PLISTP. */
1318 free_path_prefix (pprefix)
1319 struct path_prefix *pprefix;
1321 struct prefix_list *pl = pprefix->plist;
1322 struct prefix_list *temp;
1328 free (temp->prefix);
1329 free ((char *) temp);
1331 pprefix->plist = (struct prefix_list *) 0;
1334 /* stdin file number. */
1335 #define STDIN_FILE_NO 0
1337 /* stdout file number. */
1338 #define STDOUT_FILE_NO 1
1340 /* value of `pipe': port index for reading. */
1343 /* value of `pipe': port index for writing. */
1344 #define WRITE_PORT 1
1346 /* Pipe waiting from last process, to be used as input for the next one.
1347 Value is STDIN_FILE_NO if no pipe is waiting
1348 (i.e. the next command is the first of a group). */
1350 static int last_pipe_input;
1352 /* Fork one piped subcommand. FUNC is the system call to use
1353 (either execv or execvp). ARGV is the arg vector to use.
1354 NOT_LAST is nonzero if this is not the last subcommand
1355 (i.e. its output should be piped to the next one.) */
1359 /* Declare these to avoid compilation error. They won't be called. */
1360 int execv(const char *a, const char **b){}
1361 int execvp(const char *a, const char **b){}
1364 pexecute (func, program, argv, not_last)
1374 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
1375 sprintf (scmd, "%s @%s.gp", program, temp_filename);
1376 argfile = fopen (scmd+strlen (program) + 2, "w");
1378 pfatal_with_name (scmd + strlen (program) + 2);
1380 for (i=1; argv[i]; i++)
1381 fprintf (argfile, "%s\n", argv[i]);
1386 remove (scmd + strlen (program) + 2);
1390 #else /* not __MSDOS__ */
1393 pexecute (func, program, argv, not_last)
1401 int input_desc = last_pipe_input;
1402 int output_desc = STDOUT_FILE_NO;
1403 int retries, sleep_interval;
1405 /* If this isn't the last process, make a pipe for its output,
1406 and record it as waiting to be the input to the next process. */
1410 if (pipe (pdes) < 0)
1411 pfatal_with_name ("pipe");
1412 output_desc = pdes[WRITE_PORT];
1413 last_pipe_input = pdes[READ_PORT];
1416 last_pipe_input = STDIN_FILE_NO;
1418 /* Fork a subprocess; wait and retry if it fails. */
1420 for (retries = 0; retries < 4; retries++)
1425 sleep (sleep_interval);
1426 sleep_interval *= 2;
1433 pfatal_with_name ("fork");
1435 pfatal_with_name ("vfork");
1441 /* Move the input and output pipes into place, if nec. */
1442 if (input_desc != STDIN_FILE_NO)
1444 close (STDIN_FILE_NO);
1448 if (output_desc != STDOUT_FILE_NO)
1450 close (STDOUT_FILE_NO);
1452 close (output_desc);
1455 /* Close the parent's descs that aren't wanted here. */
1456 if (last_pipe_input != STDIN_FILE_NO)
1457 close (last_pipe_input);
1459 /* Exec the program. */
1460 (*func) (program, argv);
1461 perror_exec (program);
1467 /* In the parent, after forking.
1468 Close the descriptors that we made for this child. */
1469 if (input_desc != STDIN_FILE_NO)
1471 if (output_desc != STDOUT_FILE_NO)
1472 close (output_desc);
1474 /* Return child's process number. */
1479 #endif /* not __MSDOS__ */
1481 /* Execute the command specified by the arguments on the current line of spec.
1482 When using pipes, this includes several piped-together commands
1483 with `|' between them.
1485 Return 0 if successful, -1 if failed. */
1491 int n_commands; /* # of command. */
1495 char *prog; /* program name. */
1496 char **argv; /* vector of args. */
1497 int pid; /* pid of process for this command. */
1500 struct command *commands; /* each command buffer with above info. */
1502 /* Count # of piped commands. */
1503 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1504 if (strcmp (argbuf[i], "|") == 0)
1507 /* Get storage for each command. */
1509 = (struct command *) alloca (n_commands * sizeof (struct command));
1511 /* Split argbuf into its separate piped processes,
1512 and record info about each one.
1513 Also search for the programs that are to be run. */
1515 commands[0].prog = argbuf[0]; /* first command. */
1516 commands[0].argv = &argbuf[0];
1517 string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1519 commands[0].argv[0] = string;
1521 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1522 if (strcmp (argbuf[i], "|") == 0)
1523 { /* each command. */
1525 fatal ("-pipe not supported under MS-DOS");
1527 argbuf[i] = 0; /* termination of command args. */
1528 commands[n_commands].prog = argbuf[i + 1];
1529 commands[n_commands].argv = &argbuf[i + 1];
1530 string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1532 commands[n_commands].argv[0] = string;
1536 argbuf[argbuf_index] = 0;
1538 /* If -v, print what we are about to do, and maybe query. */
1542 /* Print each piped command as a separate line. */
1543 for (i = 0; i < n_commands ; i++)
1547 for (j = commands[i].argv; *j; j++)
1548 fprintf (stderr, " %s", *j);
1550 /* Print a pipe symbol after all but the last command. */
1551 if (i + 1 != n_commands)
1552 fprintf (stderr, " |");
1553 fprintf (stderr, "\n");
1557 fprintf (stderr, "\nGo ahead? (y or n) ");
1561 while (getchar () != '\n') ;
1562 if (i != 'y' && i != 'Y')
1567 /* Run each piped subprocess. */
1569 last_pipe_input = STDIN_FILE_NO;
1570 for (i = 0; i < n_commands; i++)
1572 char *string = commands[i].argv[0];
1574 commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
1575 string, commands[i].argv,
1576 i + 1 < n_commands);
1578 if (string != commands[i].prog)
1584 /* Wait for all the subprocesses to finish.
1585 We don't care what order they finish in;
1586 we know that N_COMMANDS waits will get them all. */
1591 for (i = 0; i < n_commands; i++)
1598 status = pid = commands[i].pid;
1600 pid = wait (&status);
1608 for (j = 0; j < n_commands; j++)
1609 if (commands[j].pid == pid)
1610 prog = commands[j].prog;
1612 if ((status & 0x7F) != 0)
1613 fatal ("Internal compiler error: program %s got fatal signal %d",
1614 prog, (status & 0x7F));
1615 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1623 /* Find all the switches given to us
1624 and make a vector describing them.
1625 The elements of the vector are strings, one per switch given.
1626 If a switch uses following arguments, then the `part1' field
1627 is the switch itself and the `args' field
1628 is a null-terminated vector containing the following arguments.
1629 The `valid' field is nonzero if any spec has looked at this switch;
1630 if it remains zero at the end of the run, it must be meaningless. */
1639 static struct switchstr *switches;
1641 static int n_switches;
1649 /* Also a vector of input files specified. */
1651 static struct infile *infiles;
1653 static int n_infiles;
1655 /* And a vector of corresponding output files is made up later. */
1657 static char **outfiles;
1659 /* Create the vector `switches' and its contents.
1660 Store its length in `n_switches'. */
1663 process_command (argc, argv)
1669 char *spec_lang = 0;
1670 int last_language_n_infiles;
1674 spec_version = version_string;
1676 /* Set up the default search paths. */
1678 temp = getenv ("GCC_EXEC_PREFIX");
1681 add_prefix (&exec_prefix, temp, 0, 0, 0);
1682 add_prefix (&startfile_prefix, temp, 0, 0, 0);
1685 /* COMPILER_PATH and LIBRARY_PATH have values
1686 that are lists of directory names with colons. */
1688 temp = getenv ("COMPILER_PATH");
1691 char *startp, *endp;
1692 char *nstore = (char *) alloca (strlen (temp) + 3);
1694 startp = endp = temp;
1697 if ((*endp == ':') || (*endp == 0))
1699 strncpy (nstore, startp, endp-startp);
1702 strcpy (nstore, "./");
1704 else if (endp[-1] != '/')
1706 nstore[endp-startp] = '/';
1707 nstore[endp-startp+1] = 0;
1710 nstore[endp-startp] = 0;
1711 add_prefix (&exec_prefix, nstore, 0, 0, 0);
1714 endp = startp = endp + 1;
1721 temp = getenv ("LIBRARY_PATH");
1724 char *startp, *endp;
1725 char *nstore = (char *) alloca (strlen (temp) + 3);
1727 startp = endp = temp;
1730 if ((*endp == ':') || (*endp == 0))
1732 strncpy (nstore, startp, endp-startp);
1735 strcpy (nstore, "./");
1737 else if (endp[-1] != '/')
1739 nstore[endp-startp] = '/';
1740 nstore[endp-startp+1] = 0;
1743 nstore[endp-startp] = 0;
1744 add_prefix (&startfile_prefix, nstore, 0, 0, 0);
1745 /* Make separate list of dirs that came from LIBRARY_PATH. */
1746 add_prefix (&library_prefix, nstore, 0, 0, 0);
1749 endp = startp = endp + 1;
1756 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1757 temp = getenv ("LPATH");
1760 char *startp, *endp;
1761 char *nstore = (char *) alloca (strlen (temp) + 3);
1763 startp = endp = temp;
1766 if ((*endp == ':') || (*endp == 0))
1768 strncpy (nstore, startp, endp-startp);
1771 strcpy (nstore, "./");
1773 else if (endp[-1] != '/')
1775 nstore[endp-startp] = '/';
1776 nstore[endp-startp+1] = 0;
1779 nstore[endp-startp] = 0;
1780 add_prefix (&startfile_prefix, nstore, 0, 0, 0);
1781 /* Make separate list of dirs that came from LIBRARY_PATH. */
1782 add_prefix (&library_prefix, nstore, 0, 0, 0);
1785 endp = startp = endp + 1;
1792 /* Scan argv twice. Here, the first time, just count how many switches
1793 there will be in their vector, and how many input files in theirs.
1794 Here we also parse the switches that cc itself uses (e.g. -v). */
1796 for (i = 1; i < argc; i++)
1798 if (! strcmp (argv[i], "-dumpspecs"))
1800 printf ("*asm:\n%s\n\n", asm_spec);
1801 printf ("*asm_final:\n%s\n\n", asm_final_spec);
1802 printf ("*cpp:\n%s\n\n", cpp_spec);
1803 printf ("*cc1:\n%s\n\n", cc1_spec);
1804 printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1805 printf ("*endfile:\n%s\n\n", endfile_spec);
1806 printf ("*link:\n%s\n\n", link_spec);
1807 printf ("*lib:\n%s\n\n", lib_spec);
1808 printf ("*startfile:\n%s\n\n", startfile_spec);
1809 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1810 printf ("*signed_char:\n%s\n\n", signed_char_spec);
1811 printf ("*predefines:\n%s\n\n", cpp_predefines);
1812 printf ("*cross_compile:\n%d\n\n", cross_compile);
1816 else if (! strcmp (argv[i], "-dumpversion"))
1818 printf ("%s\n", version_string);
1821 else if (! strcmp (argv[i], "-Xlinker"))
1823 /* Pass the argument of this option to the linker when we link. */
1826 fatal ("argument to `-Xlinker' is missing");
1829 if (!linker_options)
1831 = (char **) xmalloc (n_linker_options * sizeof (char **));
1834 = (char **) xrealloc (linker_options,
1835 n_linker_options * sizeof (char **));
1837 linker_options[n_linker_options - 1] = argv[++i];
1839 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1841 register char *p = &argv[i][1];
1842 register int c = *p;
1847 if (p[1] == 0 && i + 1 == argc)
1848 fatal ("argument to `-b' is missing");
1850 spec_machine = argv[++i];
1852 spec_machine = p + 1;
1857 int *temp = (int *) xmalloc (sizeof (int));
1859 if (p[1] == 0 && i + 1 == argc)
1860 fatal ("argument to `-B' is missing");
1865 add_prefix (&exec_prefix, value, 1, 0, temp);
1866 add_prefix (&startfile_prefix, value, 1, 0, temp);
1870 case 'v': /* Print our subcommands and print versions. */
1876 if (p[1] == 0 && i + 1 == argc)
1877 fatal ("argument to `-V' is missing");
1879 spec_version = argv[++i];
1881 spec_version = p + 1;
1885 if (!strcmp (p, "save-temps"))
1887 save_temps_flag = 1;
1893 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1894 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1895 else if (WORD_SWITCH_TAKES_ARG (p))
1896 i += WORD_SWITCH_TAKES_ARG (p);
1903 /* Set up the search paths before we go looking for config files. */
1905 /* These come before the md prefixes so that we will find gcc's subcommands
1906 (such as cpp) rather than those of the host system. */
1907 add_prefix (&exec_prefix, standard_exec_prefix, 0, 1, 0);
1908 add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 1, 0);
1910 add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, 0);
1911 add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, 0);
1913 /* More prefixes are enabled in main, after we read the specs file
1914 and determine whether this is cross-compilation or not. */
1917 /* Then create the space for the vectors and scan again. */
1919 switches = ((struct switchstr *)
1920 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
1921 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
1924 last_language_n_infiles = -1;
1926 /* This, time, copy the text of each switch and store a pointer
1927 to the copy in the vector of switches.
1928 Store all the infiles in their vector. */
1930 for (i = 1; i < argc; i++)
1932 if (!strcmp (argv[i], "-Xlinker"))
1934 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1936 register char *p = &argv[i][1];
1937 register int c = *p;
1939 if (c == 'B' || c == 'b' || c == 'V')
1941 /* Skip a separate arg, if any. */
1948 if (p[1] == 0 && i + 1 == argc)
1949 fatal ("argument to `-x' is missing");
1951 spec_lang = argv[++i];
1954 if (! strcmp (spec_lang, "none"))
1955 /* Suppress the warning if -xnone comes after the last input file,
1956 because alternate command interfaces like g++ might find it
1957 useful to place -xnone after each input file. */
1960 last_language_n_infiles = n_infiles;
1963 switches[n_switches].part1 = p;
1964 /* Deal with option arguments in separate argv elements. */
1965 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
1966 || WORD_SWITCH_TAKES_ARG (p)) {
1968 int n_args = WORD_SWITCH_TAKES_ARG (p);
1971 /* Count only the option arguments in separate argv elements. */
1972 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
1974 switches[n_switches].args
1975 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
1977 switches[n_switches].args[j++] = argv[++i];
1978 /* Null-terminate the vector. */
1979 switches[n_switches].args[j] = 0;
1980 } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
1981 /* On some systems, ld cannot handle -o or -L without space.
1982 So split the -o or -L from its argument. */
1983 switches[n_switches].part1 = (c == 'o' ? "o" : "L");
1984 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
1985 switches[n_switches].args[0] = xmalloc (strlen (p));
1986 strcpy (switches[n_switches].args[0], &p[1]);
1987 switches[n_switches].args[1] = 0;
1989 switches[n_switches].args = 0;
1990 switches[n_switches].valid = 0;
1991 /* This is always valid, since gcc.c itself understands it. */
1992 if (!strcmp (p, "save-temps"))
1993 switches[n_switches].valid = 1;
1998 infiles[n_infiles].language = spec_lang;
1999 infiles[n_infiles++].name = argv[i];
2003 if (n_infiles == last_language_n_infiles)
2004 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2006 switches[n_switches].part1 = 0;
2007 infiles[n_infiles].name = 0;
2010 /* Process a spec string, accumulating and running commands. */
2012 /* These variables describe the input file name.
2013 input_file_number is the index on outfiles of this file,
2014 so that the output file name can be stored for later use by %o.
2015 input_basename is the start of the part of the input file
2016 sans all directory names, and basename_length is the number
2017 of characters starting there excluding the suffix .c or whatever. */
2019 static char *input_filename;
2020 static int input_file_number;
2021 static int input_filename_length;
2022 static int basename_length;
2023 static char *input_basename;
2024 static char *input_suffix;
2026 /* These are variables used within do_spec and do_spec_1. */
2028 /* Nonzero if an arg has been started and not yet terminated
2029 (with space, tab or newline). */
2030 static int arg_going;
2032 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2033 is a temporary file name. */
2034 static int delete_this_arg;
2036 /* Nonzero means %w has been seen; the next arg to be terminated
2037 is the output file name of this compilation. */
2038 static int this_is_output_file;
2040 /* Nonzero means %s has been seen; the next arg to be terminated
2041 is the name of a library file and we should try the standard
2042 search dirs for it. */
2043 static int this_is_library_file;
2045 /* Process the spec SPEC and run the commands specified therein.
2046 Returns 0 if the spec is successfully processed; -1 if failed. */
2056 delete_this_arg = 0;
2057 this_is_output_file = 0;
2058 this_is_library_file = 0;
2060 value = do_spec_1 (spec, 0, NULL);
2062 /* Force out any unfinished command.
2063 If -pipe, this forces out the last command if it ended in `|'. */
2066 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2069 if (argbuf_index > 0)
2076 /* Process the sub-spec SPEC as a portion of a larger spec.
2077 This is like processing a whole spec except that we do
2078 not initialize at the beginning and we do not supply a
2079 newline by default at the end.
2080 INSWITCH nonzero means don't process %-sequences in SPEC;
2081 in this case, % is treated as an ordinary character.
2082 This is used while substituting switches.
2083 INSWITCH nonzero also causes SPC not to terminate an argument.
2085 Value is zero unless a line was finished
2086 and the command on that line reported an error. */
2089 do_spec_1 (spec, inswitch, soft_matched_part)
2092 char *soft_matched_part;
2094 register char *p = spec;
2100 /* If substituting a switch, treat all chars like letters.
2101 Otherwise, NL, SPC, TAB and % are special. */
2102 switch (inswitch ? 'a' : c)
2105 /* End of line: finish any pending argument,
2106 then run the pending command if one has been started. */
2109 obstack_1grow (&obstack, 0);
2110 string = obstack_finish (&obstack);
2111 if (this_is_library_file)
2112 string = find_file (string);
2113 store_arg (string, delete_this_arg, this_is_output_file);
2114 if (this_is_output_file)
2115 outfiles[input_file_number] = string;
2119 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2122 for (i = 0; i < n_switches; i++)
2123 if (!strcmp (switches[i].part1, "pipe"))
2126 /* A `|' before the newline means use a pipe here,
2127 but only if -pipe was specified.
2128 Otherwise, execute now and don't pass the `|' as an arg. */
2131 switches[i].valid = 1;
2138 if (argbuf_index > 0)
2140 int value = execute ();
2144 /* Reinitialize for a new command, and for a new argument. */
2147 delete_this_arg = 0;
2148 this_is_output_file = 0;
2149 this_is_library_file = 0;
2153 /* End any pending argument. */
2156 obstack_1grow (&obstack, 0);
2157 string = obstack_finish (&obstack);
2158 if (this_is_library_file)
2159 string = find_file (string);
2160 store_arg (string, delete_this_arg, this_is_output_file);
2161 if (this_is_output_file)
2162 outfiles[input_file_number] = string;
2166 obstack_1grow (&obstack, c);
2172 /* Space or tab ends an argument if one is pending. */
2175 obstack_1grow (&obstack, 0);
2176 string = obstack_finish (&obstack);
2177 if (this_is_library_file)
2178 string = find_file (string);
2179 store_arg (string, delete_this_arg, this_is_output_file);
2180 if (this_is_output_file)
2181 outfiles[input_file_number] = string;
2183 /* Reinitialize for a new argument. */
2185 delete_this_arg = 0;
2186 this_is_output_file = 0;
2187 this_is_library_file = 0;
2194 fatal ("Invalid specification! Bug in cc.");
2197 obstack_grow (&obstack, input_basename, basename_length);
2202 delete_this_arg = 2;
2205 /* Dump out the directories specified with LIBRARY_PATH,
2206 followed by the absolute directories
2207 that we search for startfiles. */
2209 for (i = 0; i < 2; i++)
2211 struct prefix_list *pl
2212 = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2214 char *buffer = (char *) xmalloc (bufsize);
2217 for (; pl; pl = pl->next)
2219 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2220 /* Used on systems which record the specified -L dirs
2221 and use them to search for dynamic linking. */
2222 /* Relative directories always come from -B,
2223 and it is better not to use them for searching
2224 at run time. In particular, stage1 loses */
2225 if (pl->prefix[0] != '/')
2230 if (is_linker_dir (pl->prefix, machine_suffix))
2232 do_spec_1 ("-L", 0, 0);
2233 #ifdef SPACE_AFTER_L_OPTION
2234 do_spec_1 (" ", 0, 0);
2236 do_spec_1 (pl->prefix, 1, 0);
2237 /* Remove slash from machine_suffix. */
2238 if (strlen (machine_suffix) >= bufsize)
2239 bufsize = strlen (machine_suffix) * 2 + 1;
2240 buffer = (char *) xrealloc (buffer, bufsize);
2241 strcpy (buffer, machine_suffix);
2242 idx = strlen (buffer);
2243 if (buffer[idx - 1] == '/')
2244 buffer[idx - 1] = 0;
2245 do_spec_1 (buffer, 1, 0);
2246 /* Make this a separate argument. */
2247 do_spec_1 (" ", 0, 0);
2250 if (!pl->require_machine_suffix)
2252 if (is_linker_dir (pl->prefix, ""))
2254 do_spec_1 ("-L", 0, 0);
2255 #ifdef SPACE_AFTER_L_OPTION
2256 do_spec_1 (" ", 0, 0);
2258 /* Remove slash from pl->prefix. */
2259 if (strlen (pl->prefix) >= bufsize)
2260 bufsize = strlen (pl->prefix) * 2 + 1;
2261 buffer = (char *) xrealloc (buffer, bufsize);
2262 strcpy (buffer, pl->prefix);
2263 idx = strlen (buffer);
2264 if (buffer[idx - 1] == '/')
2265 buffer[idx - 1] = 0;
2266 do_spec_1 (buffer, 1, 0);
2267 /* Make this a separate argument. */
2268 do_spec_1 (" ", 0, 0);
2277 /* {...:%efoo} means report an error with `foo' as error message
2278 and don't execute any more commands for this file. */
2282 while (*p != 0 && *p != '\n') p++;
2283 buf = (char *) alloca (p - q + 1);
2284 strncpy (buf, q, p - q);
2292 if (save_temps_flag)
2293 obstack_grow (&obstack, input_basename, basename_length);
2296 obstack_grow (&obstack, temp_filename, temp_filename_length);
2297 delete_this_arg = 1;
2303 obstack_grow (&obstack, input_filename, input_filename_length);
2310 for (f = 0; f < n_infiles; f++)
2311 store_arg (outfiles[f], 0, 0);
2316 this_is_library_file = 1;
2320 this_is_output_file = 1;
2325 int index = argbuf_index;
2326 /* Handle the {...} following the %W. */
2329 p = handle_braces (p + 1);
2332 /* If any args were output, mark the last one for deletion
2334 if (argbuf_index != index)
2335 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2339 /* %x{OPTION} records OPTION for %X to output. */
2345 /* Skip past the option value and make a copy. */
2350 string = save_string (p1 + 1, p - p1 - 2);
2352 /* See if we already recorded this option. */
2353 for (i = 0; i < n_linker_options; i++)
2354 if (! strcmp (string, linker_options[i]))
2360 /* This option is new; add it. */
2362 if (!linker_options)
2364 = (char **) xmalloc (n_linker_options * sizeof (char **));
2367 = (char **) xrealloc (linker_options,
2368 n_linker_options * sizeof (char **));
2370 linker_options[n_linker_options - 1] = string;
2374 /* Dump out the options accumulated previously using %x. */
2376 for (i = 0; i < n_linker_options; i++)
2378 do_spec_1 (linker_options[i], 1, NULL);
2379 /* Make each accumulated option a separate argument. */
2380 do_spec_1 (" ", 0, NULL);
2384 /* Here are digits and numbers that just process
2385 a certain constant string as a spec. */
2388 do_spec_1 (cc1_spec, 0, NULL);
2392 do_spec_1 (cc1plus_spec, 0, NULL);
2396 do_spec_1 (asm_spec, 0, NULL);
2400 do_spec_1 (asm_final_spec, 0, NULL);
2404 do_spec_1 (signed_char_spec, 0, NULL);
2408 do_spec_1 (cpp_spec, 0, NULL);
2412 do_spec_1 (endfile_spec, 0, NULL);
2416 do_spec_1 (link_spec, 0, NULL);
2420 do_spec_1 (lib_spec, 0, NULL);
2425 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2429 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2433 if (! strncmp (y, "-D", 2))
2434 /* Copy the whole option. */
2435 while (*y && *y != ' ' && *y != '\t')
2437 else if (*y == ' ' || *y == '\t')
2438 /* Copy whitespace to the result. */
2440 /* Don't copy other options. */
2447 do_spec_1 (buf, 0, NULL);
2453 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2457 /* Copy all of CPP_PREDEFINES into BUF,
2458 but put __ after every -D and at the end of each arg. */
2462 if (! strncmp (y, "-D", 2))
2469 if (strncmp (y, "__", 2))
2471 /* Stick __ at front of macro name. */
2474 /* Arrange to stick __ at the end as well. */
2478 /* Copy the macro name. */
2479 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2488 /* Copy the value given, if any. */
2489 while (*y && *y != ' ' && *y != '\t')
2492 else if (*y == ' ' || *y == '\t')
2493 /* Copy whitespace to the result. */
2495 /* Don't copy -A options */
2501 /* Copy all of CPP_PREDEFINES into BUF,
2502 but put __ after every -D. */
2506 if (! strncmp (y, "-D", 2))
2511 if (strncmp (y, "__", 2))
2513 /* Stick __ at front of macro name. */
2518 /* Copy the macro name. */
2519 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2522 /* Copy the value given, if any. */
2523 while (*y && *y != ' ' && *y != '\t')
2526 else if (*y == ' ' || *y == '\t')
2527 /* Copy whitespace to the result. */
2529 /* Don't copy -A options */
2535 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2539 if (! strncmp (y, "-A", 2))
2540 /* Copy the whole option. */
2541 while (*y && *y != ' ' && *y != '\t')
2543 else if (*y == ' ' || *y == '\t')
2544 /* Copy whitespace to the result. */
2546 /* Don't copy other options. */
2553 do_spec_1 (buf, 0, NULL);
2558 do_spec_1 (startfile_spec, 0, NULL);
2561 /* Here we define characters other than letters and digits. */
2564 p = handle_braces (p);
2570 obstack_1grow (&obstack, '%');
2574 do_spec_1 (soft_matched_part, 1, NULL);
2575 do_spec_1 (" ", 0, NULL);
2578 /* Process a string found as the value of a spec given by name.
2579 This feature allows individual machine descriptions
2580 to add and use their own specs.
2581 %[...] modifies -D options the way %P does;
2582 %(...) uses the spec unmodified. */
2587 struct spec_list *sl;
2590 /* The string after the S/P is the name of a spec that is to be
2592 while (*p && *p != ')' && *p != ']')
2595 /* See if it's in the list */
2596 for (len = p - name, sl = specs; sl; sl = sl->next)
2597 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2606 do_spec_1 (name, 0, NULL);
2609 char *x = (char *) alloca (strlen (name) * 2 + 1);
2613 /* Copy all of NAME into BUF, but put __ after
2614 every -D and at the end of each arg, */
2617 if (! strncmp (y, "-D", 2))
2625 else if (*y == ' ' || *y == 0)
2639 do_spec_1 (buf, 0, NULL);
2643 /* Discard the closing paren or bracket. */
2655 /* Backslash: treat next character as ordinary. */
2660 /* Ordinary character: put it into the current argument. */
2661 obstack_1grow (&obstack, c);
2665 return 0; /* End of string */
2668 /* Return 0 if we call do_spec_1 and that returns -1. */
2681 /* A `|' after the open-brace means,
2682 if the test fails, output a single minus sign rather than nothing.
2683 This is used in %{|!pipe:...}. */
2687 /* A `!' after the open-brace negates the condition:
2688 succeed if the specified switch is not present. */
2692 /* A `.' after the open-brace means test against the current suffix. */
2702 while (*p != ':' && *p != '}') p++;
2705 register int count = 1;
2723 int found = (input_suffix != 0
2724 && strlen (input_suffix) == p - filter
2725 && strncmp (input_suffix, filter, p - filter) == 0);
2731 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL) < 0)
2736 else if (p[-1] == '*' && p[0] == '}')
2738 /* Substitute all matching switches as separate args. */
2741 for (i = 0; i < n_switches; i++)
2742 if (!strncmp (switches[i].part1, filter, p - filter))
2747 /* Test for presence of the specified switch. */
2751 /* If name specified ends in *, as in {x*:...},
2752 check for %* and handle that case. */
2753 if (p[-1] == '*' && !negate)
2758 /* First see whether we have %*. */
2762 if (*r == '%' && r[1] == '*')
2766 /* If we do, handle that case. */
2769 /* Substitute all matching switches as separate args.
2770 But do this by substituting for %*
2771 in the text that follows the colon. */
2773 unsigned hard_match_len = p - filter - 1;
2774 char *string = save_string (p + 1, q - p - 2);
2776 for (i = 0; i < n_switches; i++)
2777 if (!strncmp (switches[i].part1, filter, hard_match_len))
2779 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
2780 /* Pass any arguments this switch has. */
2788 /* If name specified ends in *, as in {x*:...},
2789 check for presence of any switch name starting with x. */
2792 for (i = 0; i < n_switches; i++)
2794 unsigned hard_match_len = p - filter - 1;
2796 if (!strncmp (switches[i].part1, filter, hard_match_len))
2798 switches[i].valid = 1;
2803 /* Otherwise, check for presence of exact name specified. */
2806 for (i = 0; i < n_switches; i++)
2808 if (!strncmp (switches[i].part1, filter, p - filter)
2809 && switches[i].part1[p - filter] == 0)
2811 switches[i].valid = 1;
2818 /* If it is as desired (present for %{s...}, absent for %{-s...})
2819 then substitute either the switch or the specified
2820 conditional text. */
2821 if (present != negate)
2829 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL) < 0)
2835 /* Here if a %{|...} conditional fails: output a minus sign,
2836 which means "standard output" or "standard input". */
2837 do_spec_1 ("-", 0, NULL);
2844 /* Pass a switch to the current accumulating command
2845 in the same form that we received it.
2846 SWITCHNUM identifies the switch; it is an index into
2847 the vector of switches gcc received, which is `switches'.
2848 This cannot fail since it never finishes a command line.
2850 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2853 give_switch (switchnum, omit_first_word)
2855 int omit_first_word;
2857 if (!omit_first_word)
2859 do_spec_1 ("-", 0, NULL);
2860 do_spec_1 (switches[switchnum].part1, 1, NULL);
2862 do_spec_1 (" ", 0, NULL);
2863 if (switches[switchnum].args != 0)
2866 for (p = switches[switchnum].args; *p; p++)
2868 do_spec_1 (*p, 1, NULL);
2869 do_spec_1 (" ", 0, NULL);
2872 switches[switchnum].valid = 1;
2875 /* Search for a file named NAME trying various prefixes including the
2876 user's -B prefix and some standard ones.
2877 Return the absolute file name found. If nothing is found, return NAME. */
2885 newname = find_a_file (&startfile_prefix, name, R_OK);
2886 return newname ? newname : name;
2889 /* Determine whether a -L option is relevant. Not required for certain
2890 fixed names and for directories that don't exist. */
2893 is_linker_dir (path1, path2)
2897 int len1 = strlen (path1);
2898 int len2 = strlen (path2);
2899 char *path = (char *) alloca (3 + len1 + len2);
2903 /* Construct the path from the two parts. Ensure the string ends with "/.".
2904 The resulting path will be a directory even if the given path is a
2906 bcopy (path1, path, len1);
2907 bcopy (path2, path + len1, len2);
2908 cp = path + len1 + len2;
2914 /* Exclude directories that the linker is known to search. */
2915 if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
2916 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
2919 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
2922 /* On fatal signals, delete all the temporary files. */
2925 fatal_error (signum)
2928 signal (signum, SIG_DFL);
2929 delete_failure_queue ();
2930 delete_temp_files ();
2931 /* Get the same signal again, this time not handled,
2932 so its normal effect occurs. */
2933 kill (getpid (), signum);
2943 int error_count = 0;
2944 int linker_was_run = 0;
2945 char *explicit_link_files;
2948 programname = argv[0];
2950 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
2951 signal (SIGINT, fatal_error);
2952 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
2953 signal (SIGHUP, fatal_error);
2954 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
2955 signal (SIGTERM, fatal_error);
2957 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
2958 signal (SIGPIPE, fatal_error);
2962 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
2964 obstack_init (&obstack);
2966 /* Set up to remember the pathname of gcc and any options
2967 needed for collect. */
2968 obstack_init (&collect_obstack);
2969 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
2970 obstack_grow (&collect_obstack, programname, strlen (programname)+1);
2971 putenv (obstack_finish (&collect_obstack));
2973 /* Choose directory for temp files. */
2975 choose_temp_base ();
2977 /* Make a table of what switches there are (switches, n_switches).
2978 Make a table of specified input files (infiles, n_infiles).
2979 Decode switches that are handled locally. */
2981 process_command (argc, argv);
2983 /* Initialize the vector of specs to just the default.
2984 This means one element containing 0s, as a terminator. */
2986 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
2987 bcopy (default_compilers, compilers, sizeof default_compilers);
2988 n_compilers = n_default_compilers;
2990 /* Read specs from a file if there is one. */
2992 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
2994 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
2995 /* Read the specs file unless it is a default one. */
2996 if (specs_file != 0 && strcmp (specs_file, "specs"))
2997 read_specs (specs_file);
2999 /* If not cross-compiling, look for startfiles in the standard places. */
3000 /* The fact that these are done here, after reading the specs file,
3001 means that it cannot be found in these directories.
3002 But that's okay. It should never be there anyway. */
3005 #ifdef MD_EXEC_PREFIX
3006 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, 0);
3007 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, 0);
3010 #ifdef MD_STARTFILE_PREFIX
3011 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, 0);
3014 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0, 0);
3015 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0, 0);
3016 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0, 0);
3017 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3018 add_prefix (&startfile_prefix, "./", 0, 1, 0);
3022 /* Now we have the specs.
3023 Set the `valid' bits for switches that match anything in any spec. */
3025 validate_all_switches ();
3027 /* Warn about any switches that no pass was interested in. */
3029 for (i = 0; i < n_switches; i++)
3030 if (! switches[i].valid)
3031 error ("unrecognized option `-%s'", switches[i].part1);
3033 /* Obey some of the options. */
3037 fprintf (stderr, "gcc version %s\n", version_string);
3043 fatal ("No input files specified.");
3045 /* Make a place to record the compiler output file names
3046 that correspond to the input files. */
3048 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3049 bzero (outfiles, n_infiles * sizeof (char *));
3051 /* Record which files were specified explicitly as link input. */
3053 explicit_link_files = xmalloc (n_infiles);
3054 bzero (explicit_link_files, n_infiles);
3056 for (i = 0; i < n_infiles; i++)
3058 register struct compiler *cp = 0;
3059 int this_file_error = 0;
3061 /* Tell do_spec what to substitute for %i. */
3063 input_filename = infiles[i].name;
3064 input_filename_length = strlen (input_filename);
3065 input_file_number = i;
3067 /* Use the same thing in %o, unless cp->spec says otherwise. */
3069 outfiles[i] = input_filename;
3071 /* Figure out which compiler from the file's suffix. */
3073 cp = lookup_compiler (infiles[i].name, input_filename_length,
3074 infiles[i].language);
3078 /* Ok, we found an applicable compiler. Run its spec. */
3079 /* First say how much of input_filename to substitute for %b */
3082 input_basename = input_filename;
3083 for (p = input_filename; *p; p++)
3085 input_basename = p + 1;
3087 /* Find a suffix starting with the last period,
3088 and set basename_length to exclude that suffix. */
3089 basename_length = strlen (input_basename);
3090 p = input_basename + basename_length;
3091 while (p != input_basename && *p != '.') --p;
3092 if (*p == '.' && p != input_basename)
3094 basename_length = p - input_basename;
3095 input_suffix = p + 1;
3100 value = do_spec (cp->spec);
3102 this_file_error = 1;
3105 /* If this file's name does not contain a recognized suffix,
3106 record it as explicit linker input. */
3109 explicit_link_files[i] = 1;
3111 /* Clear the delete-on-failure queue, deleting the files in it
3112 if this compilation failed. */
3114 if (this_file_error)
3116 delete_failure_queue ();
3119 /* If this compilation succeeded, don't delete those files later. */
3120 clear_failure_queue ();
3123 /* Run ld to link all the compiler output files. */
3125 if (error_count == 0)
3127 int tmp = execution_count;
3131 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3133 putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3134 putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3136 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3138 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3139 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3142 for (i = 0; i < n_switches; i++)
3146 obstack_grow (&collect_obstack, " ", 1);
3149 obstack_grow (&collect_obstack, "-", 1);
3150 obstack_grow (&collect_obstack, switches[i].part1,
3151 strlen (switches[i].part1));
3153 for (args = switches[i].args; args && *args; args++)
3155 obstack_grow (&collect_obstack, " ", 1);
3156 obstack_grow (&collect_obstack, *args, strlen (*args));
3159 obstack_grow (&collect_obstack, "\0", 1);
3160 putenv (obstack_finish (&collect_obstack));
3162 value = do_spec (link_command_spec);
3165 linker_was_run = (tmp != execution_count);
3168 /* Warn if a -B option was specified but the prefix was never used. */
3169 unused_prefix_warnings (&exec_prefix);
3170 unused_prefix_warnings (&startfile_prefix);
3172 /* If options said don't run linker,
3173 complain about input files to be given to the linker. */
3175 if (! linker_was_run && error_count == 0)
3176 for (i = 0; i < n_infiles; i++)
3177 if (explicit_link_files[i])
3178 error ("%s: linker input file unused since linking not done",
3181 /* Delete some or all of the temporary files we made. */
3184 delete_failure_queue ();
3185 delete_temp_files ();
3192 /* Find the proper compilation spec for the file name NAME,
3193 whose length is LENGTH. LANGUAGE is the specified language,
3194 or 0 if none specified. */
3196 static struct compiler *
3197 lookup_compiler (name, length, language)
3202 struct compiler *cp;
3204 /* Look for the language, if one is spec'd. */
3207 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3211 if (cp->suffix[0] == '@'
3212 && !strcmp (cp->suffix + 1, language))
3216 error ("language %s not recognized", language);
3219 /* Look for a suffix. */
3220 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3222 if (strlen (cp->suffix) < length
3223 /* See if the suffix matches the end of NAME. */
3224 && !strcmp (cp->suffix,
3225 name + length - strlen (cp->suffix))
3226 /* The suffix `-' matches only the file name `-'. */
3227 && !(!strcmp (cp->suffix, "-") && length != 1))
3229 if (cp->spec[0] == '@')
3231 struct compiler *new;
3232 /* An alias entry maps a suffix to a language.
3233 Search for the language; pass 0 for NAME and LENGTH
3234 to avoid infinite recursion if language not found.
3235 Construct the new compiler spec. */
3236 language = cp->spec + 1;
3237 new = (struct compiler *) xmalloc (sizeof (struct compiler));
3238 new->suffix = cp->suffix;
3239 new->spec = lookup_compiler (0, 0, language)->spec;
3242 /* A non-alias entry: return it. */
3254 register char *value = (char *) malloc (size);
3256 fatal ("virtual memory exhausted");
3261 xrealloc (ptr, size)
3265 register char *value = (char *) realloc (ptr, size);
3267 fatal ("virtual memory exhausted");
3271 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3277 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3278 char *result = xmalloc (len1 + len2 + len3 + 1);
3280 strcpy (result, s1);
3281 strcpy (result + len1, s2);
3282 strcpy (result + len1 + len2, s3);
3283 *(result + len1 + len2 + len3) = 0;
3289 save_string (s, len)
3293 register char *result = xmalloc (len + 1);
3295 bcopy (s, result, len);
3301 pfatal_with_name (name)
3306 if (errno < sys_nerr)
3307 s = concat ("%s: ", sys_errlist[errno], "");
3309 s = "cannot open %s";
3314 perror_with_name (name)
3319 if (errno < sys_nerr)
3320 s = concat ("%s: ", sys_errlist[errno], "");
3322 s = "cannot open %s";
3332 if (errno < sys_nerr)
3333 s = concat ("installation problem, cannot exec %s: ",
3334 sys_errlist[errno], "");
3336 s = "installation problem, cannot exec %s";
3340 /* More 'friendly' abort that prints the line and file.
3341 config.h can #define abort fancy_abort if you like that sort of thing. */
3346 fatal ("Internal gcc abort.");
3351 /* Output an error message and exit */
3361 format = va_arg (ap, char *);
3362 fprintf (stderr, "%s: ", programname);
3363 vfprintf (stderr, format, ap);
3365 fprintf (stderr, "\n");
3366 delete_temp_files ();
3378 format = va_arg (ap, char *);
3379 fprintf (stderr, "%s: ", programname);
3380 vfprintf (stderr, format, ap);
3383 fprintf (stderr, "\n");
3386 #else /* not HAVE_VPRINTF */
3389 fatal (msg, arg1, arg2)
3390 char *msg, *arg1, *arg2;
3392 error (msg, arg1, arg2);
3393 delete_temp_files ();
3398 error (msg, arg1, arg2)
3399 char *msg, *arg1, *arg2;
3401 fprintf (stderr, "%s: ", programname);
3402 fprintf (stderr, msg, arg1, arg2);
3403 fprintf (stderr, "\n");
3406 #endif /* not HAVE_VPRINTF */
3410 validate_all_switches ()
3412 struct compiler *comp;
3415 struct spec_list *spec;
3417 for (comp = compilers; comp->spec; comp++)
3421 if (c == '%' && *p == '{')
3422 /* We have a switch spec. */
3423 validate_switches (p + 1);
3426 /* look through the linked list of extra specs read from the specs file */
3427 for (spec = specs ; spec ; spec = spec->next)
3431 if (c == '%' && *p == '{')
3432 /* We have a switch spec. */
3433 validate_switches (p + 1);
3436 p = link_command_spec;
3438 if (c == '%' && *p == '{')
3439 /* We have a switch spec. */
3440 validate_switches (p + 1);
3442 /* Now notice switches mentioned in the machine-specific specs. */
3446 if (c == '%' && *p == '{')
3447 /* We have a switch spec. */
3448 validate_switches (p + 1);
3452 if (c == '%' && *p == '{')
3453 /* We have a switch spec. */
3454 validate_switches (p + 1);
3458 if (c == '%' && *p == '{')
3459 /* We have a switch spec. */
3460 validate_switches (p + 1);
3462 p = signed_char_spec;
3464 if (c == '%' && *p == '{')
3465 /* We have a switch spec. */
3466 validate_switches (p + 1);
3470 if (c == '%' && *p == '{')
3471 /* We have a switch spec. */
3472 validate_switches (p + 1);
3476 if (c == '%' && *p == '{')
3477 /* We have a switch spec. */
3478 validate_switches (p + 1);
3482 if (c == '%' && *p == '{')
3483 /* We have a switch spec. */
3484 validate_switches (p + 1);
3488 if (c == '%' && *p == '{')
3489 /* We have a switch spec. */
3490 validate_switches (p + 1);
3494 if (c == '%' && *p == '{')
3495 /* We have a switch spec. */
3496 validate_switches (p + 1);
3499 /* Look at the switch-name that comes after START
3500 and mark as valid all supplied switches that match it. */
3503 validate_switches (start)
3506 register char *p = start;
3521 while (*p != ':' && *p != '}') p++;
3525 else if (p[-1] == '*')
3527 /* Mark all matching switches as valid. */
3529 for (i = 0; i < n_switches; i++)
3530 if (!strncmp (switches[i].part1, filter, p - filter))
3531 switches[i].valid = 1;
3535 /* Mark an exact matching switch as valid. */
3536 for (i = 0; i < n_switches; i++)
3538 if (!strncmp (switches[i].part1, filter, p - filter)
3539 && switches[i].part1[p - filter] == 0)
3540 switches[i].valid = 1;