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>
38 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
55 #define NULL_PTR (char *) NULL
62 /* On MSDOS, write temp files in current dir
63 because there's no place else we can expect to use. */
70 /* Test if something is a normal file. */
72 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
75 /* Test if something is a directory. */
77 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
80 /* By default there is no special suffix for executables. */
81 #ifndef EXECUTABLE_SUFFIX
82 #define EXECUTABLE_SUFFIX ""
85 /* By default, colon separates directories in a path. */
86 #ifndef PATH_SEPARATOR
87 #define PATH_SEPARATOR ':'
90 #define obstack_chunk_alloc xmalloc
91 #define obstack_chunk_free free
94 extern char *getenv ();
96 extern int errno, sys_nerr;
97 extern char *sys_errlist[];
99 extern int execv (), execvp ();
101 /* If a stage of compilation returns an exit status >= 1,
102 compilation of that file ceases. */
104 #define MIN_FATAL_STATUS 1
106 /* Flag saying to print the full filename of libgcc.a
107 as found through our usual search mechanism. */
109 static int print_libgcc_file_name;
111 /* Flag indicating whether we should print the command and arguments */
113 static int verbose_flag;
115 /* Nonzero means write "temp" files in source directory
116 and use the source file's name in them, and don't delete them. */
118 static int save_temps_flag;
120 /* The compiler version specified with -V */
122 static char *spec_version;
124 /* The target machine specified with -b. */
126 static char *spec_machine = DEFAULT_TARGET_MACHINE;
128 /* Nonzero if cross-compiling.
129 When -b is used, the value comes from the `specs' file. */
132 static int cross_compile = 1;
134 static int cross_compile = 0;
137 /* This is the obstack which we use to allocate many strings. */
139 static struct obstack obstack;
141 /* This is the obstack to build an environment variable to pass to
142 collect2 that describes all of the relevant switches of what to
143 pass the compiler in building the list of pointers to constructors
146 static struct obstack collect_obstack;
148 extern char *version_string;
150 static void set_spec ();
151 static struct compiler *lookup_compiler ();
152 static char *find_a_file ();
153 static void add_prefix ();
154 static char *skip_whitespace ();
155 static void record_temp_file ();
156 static char *handle_braces ();
157 static char *save_string ();
158 static char *concat ();
159 static int do_spec ();
160 static int do_spec_1 ();
161 static char *find_file ();
162 static int is_linker_dir ();
163 static void validate_switches ();
164 static void validate_all_switches ();
165 static void give_switch ();
166 static void pfatal_with_name ();
167 static void perror_with_name ();
168 static void perror_exec ();
169 static void fatal ();
170 static void error ();
175 /* Specs are strings containing lines, each of which (if not blank)
176 is made up of a program name, and arguments separated by spaces.
177 The program name must be exact and start from root, since no path
178 is searched and it is unreliable to depend on the current working directory.
179 Redirection of input or output is not supported; the subprograms must
180 accept filenames saying what files to read and write.
182 In addition, the specs can contain %-sequences to substitute variable text
183 or for conditional text. Here is a table of all defined %-sequences.
184 Note that spaces are not generated automatically around the results of
185 expanding these sequences; therefore, you can concatenate them together
186 or with constant text in a single argument.
188 %% substitute one % into the program name or argument.
189 %i substitute the name of the input file being processed.
190 %b substitute the basename of the input file being processed.
191 This is the substring up to (and not including) the last period
192 and not including the directory.
193 %g substitute the temporary-file-name-base. This is a string chosen
194 once per compilation. Different temporary file names are made by
195 concatenation of constant strings on the end, as in `%g.s'.
196 %g also has the same effect of %d.
197 %d marks the argument containing or following the %d as a
198 temporary file name, so that that file will be deleted if CC exits
199 successfully. Unlike %g, this contributes no text to the argument.
200 %w marks the argument containing or following the %w as the
201 "output file" of this compilation. This puts the argument
202 into the sequence of arguments that %o will substitute later.
204 like %{...} but mark last argument supplied within
205 as a file to be deleted on failure.
206 %o substitutes the names of all the output files, with spaces
207 automatically placed around them. You should write spaces
208 around the %o as well or the results are undefined.
209 %o is for use in the specs for running the linker.
210 Input files whose names have no recognized suffix are not compiled
211 at all, but they are included among the output files, so they will
213 %p substitutes the standard macro predefinitions for the
214 current target machine. Use this when running cpp.
215 %P like %p, but puts `__' before and after the name of each macro.
216 (Except macros that already have __.)
218 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
219 %s current argument is the name of a library or startup file of some sort.
220 Search for that file in a standard list of directories
221 and substitute the full name found.
222 %eSTR Print STR as an error message. STR is terminated by a newline.
223 Use this when inconsistent options are detected.
224 %x{OPTION} Accumulate an option for %X.
225 %X Output the accumulated linker options specified by compilations.
226 %a process ASM_SPEC as a spec.
227 This allows config.h to specify part of the spec for running as.
228 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
229 used here. This can be used to run a post-processor after the
230 assembler has done it's job.
231 %D Dump out a -L option for each directory in library_prefix,
232 followed by a -L option for each directory in startfile_prefix.
233 %l process LINK_SPEC as a spec.
234 %L process LIB_SPEC as a spec.
235 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
236 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
237 %c process SIGNED_CHAR_SPEC as a spec.
238 %C process CPP_SPEC as a spec. A capital C is actually used here.
239 %1 process CC1_SPEC as a spec.
240 %2 process CC1PLUS_SPEC as a spec.
241 %* substitute the variable part of a matched option. (See below.)
242 Note that each comma in the substituted string is replaced by
244 %{S} substitutes the -S switch, if that switch was given to CC.
245 If that switch was not specified, this substitutes nothing.
246 Here S is a metasyntactic variable.
247 %{S*} substitutes all the switches specified to CC whose names start
248 with -S. This is used for -o, -D, -I, etc; switches that take
249 arguments. CC considers `-o foo' as being one switch whose
250 name starts with `o'. %{o*} would substitute this text,
251 including the space; thus, two arguments would be generated.
252 %{S*:X} substitutes X if one or more switches whose names with -S are
253 specified to CC. Note that the tail part of the -S option
254 (i.e. the part matched by the `*') will be substituted for each
255 occurrence of %* within X.
256 %{S:X} substitutes X, but only if the -S switch was given to CC.
257 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
258 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
259 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
260 %{.S:X} substitutes X, but only if processing a file with suffix S.
261 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
262 %(Spec) processes a specification defined in a specs file as *Spec:
263 %[Spec] as above, but put __ around -D arguments
265 The conditional text X in a %{S:X} or %{!S:X} construct may contain
266 other nested % constructs or spaces, or even newlines. They are
267 processed as usual, as described above.
269 The character | is used to indicate that a command should be piped to
270 the following command, but only if -pipe is specified.
272 Note that it is built into CC which switches take arguments and which
273 do not. You might think it would be useful to generalize this to
274 allow each compiler's spec to say which switches take arguments. But
275 this cannot be done in a consistent fashion. CC cannot even decide
276 which input files have been specified without knowing which switches
277 take arguments, and it must know which input files to compile in order
278 to tell which compilers to run.
280 CC also knows implicitly that arguments starting in `-l' are to be
281 treated as compiler output files, and passed to the linker in their
282 proper position among the other output files. */
284 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
286 /* config.h can define ASM_SPEC to provide extra args to the assembler
287 or extra switch-translations. */
292 /* config.h can define ASM_FINAL_SPEC to run a post processor after
293 the assembler has run. */
294 #ifndef ASM_FINAL_SPEC
295 #define ASM_FINAL_SPEC ""
298 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
299 or extra switch-translations. */
304 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
305 or extra switch-translations. */
310 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
311 or extra switch-translations. */
313 #define CC1PLUS_SPEC ""
316 /* config.h can define LINK_SPEC to provide extra args to the linker
317 or extra switch-translations. */
322 /* config.h can define LIB_SPEC to override the default libraries. */
324 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
327 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
328 #ifndef STARTFILE_SPEC
329 #define STARTFILE_SPEC \
330 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
333 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
334 Make the string nonempty to require spaces there. */
335 #ifndef SWITCHES_NEED_SPACES
336 #define SWITCHES_NEED_SPACES ""
339 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
341 #define ENDFILE_SPEC ""
344 /* This spec is used for telling cpp whether char is signed or not. */
345 #ifndef SIGNED_CHAR_SPEC
346 #define SIGNED_CHAR_SPEC \
347 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
348 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
351 static char *cpp_spec = CPP_SPEC;
352 static char *cpp_predefines = CPP_PREDEFINES;
353 static char *cc1_spec = CC1_SPEC;
354 static char *cc1plus_spec = CC1PLUS_SPEC;
355 static char *signed_char_spec = SIGNED_CHAR_SPEC;
356 static char *asm_spec = ASM_SPEC;
357 static char *asm_final_spec = ASM_FINAL_SPEC;
358 static char *link_spec = LINK_SPEC;
359 static char *lib_spec = LIB_SPEC;
360 static char *endfile_spec = ENDFILE_SPEC;
361 static char *startfile_spec = STARTFILE_SPEC;
362 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
364 /* This defines which switch letters take arguments. */
366 #ifndef SWITCH_TAKES_ARG
367 #define SWITCH_TAKES_ARG(CHAR) \
368 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
369 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
370 || (CHAR) == 'I' || (CHAR) == 'm' \
371 || (CHAR) == 'L' || (CHAR) == 'A')
374 /* This defines which multi-letter switches take arguments. */
376 #ifndef WORD_SWITCH_TAKES_ARG
377 #define WORD_SWITCH_TAKES_ARG(STR) \
378 (!strcmp (STR, "Tdata") || !strcmp (STR, "include") \
379 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
382 /* Record the mapping from file suffixes for compilation specs. */
386 char *suffix; /* Use this compiler for input files
387 whose names end in this suffix. */
388 char *spec; /* To use this compiler, pass this spec
392 /* Pointer to a vector of `struct compiler' that gives the spec for
393 compiling a file, based on its suffix.
394 A file that does not end in any of these suffixes will be passed
395 unchanged to the loader and nothing else will be done to it.
397 An entry containing two 0s is used to terminate the vector.
399 If multiple entries match a file, the last matching one is used. */
401 static struct compiler *compilers;
403 /* Number of entries in `compilers', not counting the null terminator. */
405 static int n_compilers;
407 /* The default list of file name suffixes and their compilation specs. */
409 static struct compiler default_compilers[] =
413 "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
414 %{C:%{!E:%eGNU C does not support -C without using -E}}\
415 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
416 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
417 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
418 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
419 %{traditional-cpp:-traditional}\
420 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
421 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
422 %{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
423 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
424 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
425 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
427 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
428 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
429 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
430 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
431 %{!pipe:%g.s} %A\n }}}}"},
433 "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
434 %{C:%{!E:%eGNU C does not support -C without using -E}}\
435 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
436 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
437 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
438 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
439 %{traditional-cpp:-traditional}\
440 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
442 %{!E:%e-E required when input is from standard input}"},
443 {".m", "@objective-c"},
445 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
446 %{C:%{!E:%eGNU C does not support -C without using -E}}\
447 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
448 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
449 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
450 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
451 %{traditional-cpp:-traditional}\
452 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
453 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
454 %{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
455 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
456 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
457 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
458 -lang-objc %{gen-decls} \
460 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
461 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
462 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
463 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
464 %{!pipe:%g.s} %A\n }}}}"},
467 "%{!E:%eCompilation of header file requested} \
468 cpp %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
469 %{C:%{!E:%eGNU C does not support -C without using -E}}\
470 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
471 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
472 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
473 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
474 %{traditional-cpp:-traditional}\
475 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
481 "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
482 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
483 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
484 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
485 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
486 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
487 %{traditional-cpp:-traditional} %{trigraphs}\
488 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
489 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
490 %{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
491 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
492 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
493 %{v:-version} %{pg:-p} %{p} %{f*}\
495 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
496 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
497 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
498 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
499 %{!pipe:%g.s} %A\n }}}}"},
500 {".i", "@cpp-output"},
502 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
503 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
504 %{v:-version} %{pg:-p} %{p} %{f*}\
506 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
507 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
508 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
509 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s} %A\n }"},
510 {".ii", "@c++-cpp-output"},
512 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
513 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
514 %{v:-version} %{pg:-p} %{p} %{f*}\
516 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
517 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
518 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
519 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
520 %{!pipe:%g.s} %A\n }"},
521 {".s", "@assembler"},
523 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
524 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i %A\n }"},
525 {".S", "@assembler-with-cpp"},
526 {"@assembler-with-cpp",
527 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P} %I\
528 %{C:%{!E:%eGNU C does not support -C without using -E}}\
529 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
530 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
531 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
532 %{traditional-cpp:-traditional}\
533 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
534 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
535 %{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
536 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
537 %{!pipe:%g.s} %A\n }}}}"},
538 /* Mark end of table */
542 /* Number of elements in default_compilers, not counting the terminator. */
544 static int n_default_compilers
545 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
547 /* Here is the spec for running the linker, after compiling all files. */
549 #ifdef LINK_LIBGCC_SPECIAL
550 /* Have gcc do the search for libgcc.a. */
551 /* -u* was put back because both BSD and SysV seem to support it. */
552 static char *link_command_spec = "\
553 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
554 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
555 %{!A:%{!nostdlib:%S}} \
556 %{L*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
558 /* Use -L and have the linker do the search for -lgcc. */
559 static char *link_command_spec = "\
560 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
561 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
562 %{!A:%{!nostdlib:%S}} \
563 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
566 /* A vector of options to give to the linker.
567 These options are accumulated by %x
568 and substituted into the linker command with %X. */
569 static int n_linker_options;
570 static char **linker_options;
572 /* Read compilation specs from a file named FILENAME,
573 replacing the default ones.
575 A suffix which starts with `*' is a definition for
576 one of the machine-specific sub-specs. The "suffix" should be
577 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
578 The corresponding spec is stored in asm_spec, etc.,
579 rather than in the `compilers' vector.
581 Anything invalid in the file is a fatal error. */
584 read_specs (filename)
593 fprintf (stderr, "Reading specs from %s\n", filename);
595 /* Open and stat the file. */
596 desc = open (filename, 0, 0);
598 pfatal_with_name (filename);
599 if (stat (filename, &statbuf) < 0)
600 pfatal_with_name (filename);
602 /* Read contents of file into BUFFER. */
603 buffer = xmalloc (statbuf.st_size + 1);
604 read (desc, buffer, statbuf.st_size);
605 buffer[statbuf.st_size] = 0;
608 /* Scan BUFFER for specs, putting them in the vector. */
614 char *in, *out, *p1, *p2;
616 /* Advance P in BUFFER to the next nonblank nocomment line. */
617 p = skip_whitespace (p);
621 /* Find the colon that should end the suffix. */
623 while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
624 /* The colon shouldn't be missing. */
626 fatal ("specs file malformed after %d characters", p1 - buffer);
627 /* Skip back over trailing whitespace. */
629 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
630 /* Copy the suffix to a string. */
631 suffix = save_string (p, p2 - p);
632 /* Find the next line. */
633 p = skip_whitespace (p1 + 1);
635 fatal ("specs file malformed after %d characters", p - buffer);
637 /* Find next blank line. */
638 while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
639 /* Specs end at the blank line and do not include the newline. */
640 spec = save_string (p, p1 - p);
643 /* Delete backslash-newline sequences from the spec. */
648 if (in[0] == '\\' && in[1] == '\n')
650 else if (in[0] == '#')
652 while (*in && *in != '\n') in++;
659 if (suffix[0] == '*')
661 if (! strcmp (suffix, "*link_command"))
662 link_command_spec = spec;
664 set_spec (suffix + 1, spec);
668 /* Add this pair to the vector. */
670 = ((struct compiler *)
671 xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
672 compilers[n_compilers].suffix = suffix;
673 compilers[n_compilers].spec = spec;
678 link_command_spec = spec;
681 if (link_command_spec == 0)
682 fatal ("spec file has no spec for linking");
691 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
692 be considered whitespace. */
693 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
695 else if (*p == '\n' || *p == ' ' || *p == '\t')
699 while (*p != '\n') p++;
709 /* Structure to keep track of the specs that have been defined so far. These
710 are accessed using %(specname) or %[specname] in a compiler or link spec. */
714 char *name; /* Name of the spec. */
715 char *spec; /* The spec itself. */
716 struct spec_list *next; /* Next spec in linked list. */
719 /* List of specs that have been defined so far. */
721 static struct spec_list *specs = (struct spec_list *) 0;
723 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
724 removed; If the spec starts with a + then SPEC is added to the end of the
728 set_spec (name, spec)
732 struct spec_list *sl;
735 /* See if the spec already exists */
736 for (sl = specs; sl; sl = sl->next)
737 if (strcmp (sl->name, name) == 0)
742 /* Not found - make it */
743 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
744 sl->name = save_string (name, strlen (name));
745 sl->spec = save_string ("", 0);
751 if (name && spec[0] == '+' && isspace (spec[1]))
752 sl->spec = concat (old_spec, spec + 1, "");
754 sl->spec = save_string (spec, strlen (spec));
756 if (! strcmp (name, "asm"))
758 else if (! strcmp (name, "asm_final"))
759 asm_final_spec = sl->spec;
760 else if (! strcmp (name, "cc1"))
762 else if (! strcmp (name, "cc1plus"))
763 cc1plus_spec = sl->spec;
764 else if (! strcmp (name, "cpp"))
766 else if (! strcmp (name, "endfile"))
767 endfile_spec = sl->spec;
768 else if (! strcmp (name, "lib"))
770 else if (! strcmp (name, "link"))
771 link_spec = sl->spec;
772 else if (! strcmp (name, "predefines"))
773 cpp_predefines = sl->spec;
774 else if (! strcmp (name, "signed_char"))
775 signed_char_spec = sl->spec;
776 else if (! strcmp (name, "startfile"))
777 startfile_spec = sl->spec;
778 else if (! strcmp (name, "switches_need_spaces"))
779 switches_need_spaces = sl->spec;
780 else if (! strcmp (name, "cross_compile"))
781 cross_compile = atoi (sl->spec);
782 /* Free the old spec */
787 /* Accumulate a command (program name and args), and run it. */
789 /* Vector of pointers to arguments in the current line of specifications. */
791 static char **argbuf;
793 /* Number of elements allocated in argbuf. */
795 static int argbuf_length;
797 /* Number of elements in argbuf currently in use (containing args). */
799 static int argbuf_index;
801 /* Number of commands executed so far. */
803 static int execution_count;
805 /* Name with which this program was invoked. */
807 static char *programname;
809 /* Structures to keep track of prefixes to try when looking for files. */
813 char *prefix; /* String to prepend to the path. */
814 struct prefix_list *next; /* Next in linked list. */
815 int require_machine_suffix; /* Don't use without machine_suffix. */
816 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
821 struct prefix_list *plist; /* List of prefixes to try */
822 int max_len; /* Max length of a prefix in PLIST */
823 char *name; /* Name of this list (used in config stuff) */
826 /* List of prefixes to try when looking for executables. */
828 static struct path_prefix exec_prefix = { 0, 0, "exec" };
830 /* List of prefixes to try when looking for startup (crt0) files. */
832 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
834 /* List of prefixes to try when looking for libraries. */
836 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
838 /* Suffix to attach to directories searched for commands. */
840 static char *machine_suffix = 0;
842 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
844 static char *gcc_exec_prefix;
846 /* Default prefixes to attach to command names. */
848 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
849 #undef MD_EXEC_PREFIX
850 #undef MD_STARTFILE_PREFIX
851 #undef MD_STARTFILE_PREFIX_1
854 #ifndef STANDARD_EXEC_PREFIX
855 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
856 #endif /* !defined STANDARD_EXEC_PREFIX */
858 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
859 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
860 #ifdef MD_EXEC_PREFIX
861 static char *md_exec_prefix = MD_EXEC_PREFIX;
864 #ifndef STANDARD_STARTFILE_PREFIX
865 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
866 #endif /* !defined STANDARD_STARTFILE_PREFIX */
868 #ifdef MD_STARTFILE_PREFIX
869 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
871 #ifdef MD_STARTFILE_PREFIX_1
872 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
874 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
875 static char *standard_startfile_prefix_1 = "/lib/";
876 static char *standard_startfile_prefix_2 = "/usr/lib/";
878 /* Clear out the vector of arguments (after a command is executed). */
886 /* Add one argument to the vector at the end.
887 This is done when a space is seen or at the end of the line.
888 If DELETE_ALWAYS is nonzero, the arg is a filename
889 and the file should be deleted eventually.
890 If DELETE_FAILURE is nonzero, the arg is a filename
891 and the file should be deleted if this compilation fails. */
894 store_arg (arg, delete_always, delete_failure)
896 int delete_always, delete_failure;
898 if (argbuf_index + 1 == argbuf_length)
900 argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
903 argbuf[argbuf_index++] = arg;
904 argbuf[argbuf_index] = 0;
906 if (delete_always || delete_failure)
907 record_temp_file (arg, delete_always, delete_failure);
910 /* Record the names of temporary files we tell compilers to write,
911 and delete them at the end of the run. */
913 /* This is the common prefix we use to make temp file names.
914 It is chosen once for each run of this program.
915 It is substituted into a spec by %g.
916 Thus, all temp file names contain this prefix.
917 In practice, all temp file names start with this prefix.
919 This prefix comes from the envvar TMPDIR if it is defined;
920 otherwise, from the P_tmpdir macro if that is defined;
921 otherwise, in /usr/tmp or /tmp. */
923 static char *temp_filename;
925 /* Length of the prefix. */
927 static int temp_filename_length;
929 /* Define the list of temporary files to delete. */
934 struct temp_file *next;
937 /* Queue of files to delete on success or failure of compilation. */
938 static struct temp_file *always_delete_queue;
939 /* Queue of files to delete on failure of compilation. */
940 static struct temp_file *failure_delete_queue;
942 /* Record FILENAME as a file to be deleted automatically.
943 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
944 otherwise delete it in any case.
945 FAIL_DELETE nonzero means delete it if a compilation step fails;
946 otherwise delete it in any case. */
949 record_temp_file (filename, always_delete, fail_delete)
955 name = xmalloc (strlen (filename) + 1);
956 strcpy (name, filename);
960 register struct temp_file *temp;
961 for (temp = always_delete_queue; temp; temp = temp->next)
962 if (! strcmp (name, temp->name))
964 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
965 temp->next = always_delete_queue;
967 always_delete_queue = temp;
973 register struct temp_file *temp;
974 for (temp = failure_delete_queue; temp; temp = temp->next)
975 if (! strcmp (name, temp->name))
977 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
978 temp->next = failure_delete_queue;
980 failure_delete_queue = temp;
985 /* Delete all the temporary files whose names we previously recorded. */
990 register struct temp_file *temp;
992 for (temp = always_delete_queue; temp; temp = temp->next)
996 printf ("Delete %s? (y or n) ", temp->name);
1000 while (getchar () != '\n') ;
1001 if (i == 'y' || i == 'Y')
1005 if (stat (temp->name, &st) >= 0)
1007 /* Delete only ordinary files. */
1008 if (S_ISREG (st.st_mode))
1009 if (unlink (temp->name) < 0)
1011 perror_with_name (temp->name);
1016 always_delete_queue = 0;
1019 /* Delete all the files to be deleted on error. */
1022 delete_failure_queue ()
1024 register struct temp_file *temp;
1026 for (temp = failure_delete_queue; temp; temp = temp->next)
1030 printf ("Delete %s? (y or n) ", temp->name);
1034 while (getchar () != '\n') ;
1035 if (i == 'y' || i == 'Y')
1038 if (unlink (temp->name) < 0)
1040 perror_with_name (temp->name);
1046 clear_failure_queue ()
1048 failure_delete_queue = 0;
1051 /* Compute a string to use as the base of all temporary file names.
1052 It is substituted for %g. */
1057 char *base = getenv ("TMPDIR");
1060 if (base == (char *)0)
1063 if (access (P_tmpdir, R_OK | W_OK) == 0)
1066 if (base == (char *)0)
1068 if (access ("/usr/tmp", R_OK | W_OK) == 0)
1075 len = strlen (base);
1076 temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1077 strcpy (temp_filename, base);
1078 if (len > 0 && temp_filename[len-1] != '/')
1079 temp_filename[len++] = '/';
1080 strcpy (temp_filename + len, "ccXXXXXX");
1082 mktemp (temp_filename);
1083 temp_filename_length = strlen (temp_filename);
1087 /* Routine to add variables to the environment. We do this to pass
1088 the pathname of the gcc driver, and the directories search to the
1089 collect2 program, which is being run as ld. This way, we can be
1090 sure of executing the right compiler when collect2 wants to build
1091 constructors and destructors. Since the environment variables we
1092 use come from an obstack, we don't have to worry about allocating
1100 #ifndef VMS /* nor about VMS */
1102 extern char **environ;
1103 char **old_environ = environ;
1107 int str_len = strlen (str);
1111 while ((ch = *p++) != '\0' && ch != '=')
1117 /* Search for replacing an existing environment variable, and
1118 count the number of total environment variables. */
1119 for (envp = old_environ; *envp; envp++)
1122 if (!strncmp (str, *envp, name_len))
1129 /* Add a new environment variable */
1130 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1132 bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1137 #endif /* HAVE_PUTENV */
1140 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1143 putenv_from_prefixes (paths, env_var)
1144 struct path_prefix *paths;
1147 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1148 int first_time = TRUE;
1149 struct prefix_list *pprefix;
1151 obstack_grow (&collect_obstack, env_var, strlen (env_var));
1153 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1155 int len = strlen (pprefix->prefix);
1160 obstack_grow (&collect_obstack, ":", 1);
1163 obstack_grow (&collect_obstack, pprefix->prefix, len);
1164 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1167 if (!pprefix->require_machine_suffix)
1170 obstack_grow (&collect_obstack, ":", 1);
1173 obstack_grow (&collect_obstack, pprefix->prefix, len);
1176 obstack_grow (&collect_obstack, "\0", 1);
1177 putenv (obstack_finish (&collect_obstack));
1181 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1182 access to check permissions.
1183 Return 0 if not found, otherwise return its name, allocated with malloc. */
1186 find_a_file (pprefix, name, mode)
1187 struct path_prefix *pprefix;
1192 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1193 struct prefix_list *pl;
1194 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1197 len += strlen (machine_suffix);
1199 temp = xmalloc (len);
1201 /* Determine the filename to execute (special case for absolute paths). */
1205 if (access (name, mode))
1207 strcpy (temp, name);
1212 for (pl = pprefix->plist; pl; pl = pl->next)
1216 strcpy (temp, pl->prefix);
1217 strcat (temp, machine_suffix);
1218 strcat (temp, name);
1219 if (access (temp, mode) == 0)
1221 if (pl->used_flag_ptr != 0)
1222 *pl->used_flag_ptr = 1;
1225 /* Some systems have a suffix for executable files.
1226 So try appending that. */
1227 if (file_suffix[0] != 0)
1229 strcat (temp, file_suffix);
1230 if (access (temp, mode) == 0)
1232 if (pl->used_flag_ptr != 0)
1233 *pl->used_flag_ptr = 1;
1238 /* Certain prefixes can't be used without the machine suffix
1239 when the machine or version is explicitly specified. */
1240 if (!pl->require_machine_suffix)
1242 strcpy (temp, pl->prefix);
1243 strcat (temp, name);
1244 if (access (temp, mode) == 0)
1246 if (pl->used_flag_ptr != 0)
1247 *pl->used_flag_ptr = 1;
1250 /* Some systems have a suffix for executable files.
1251 So try appending that. */
1252 if (file_suffix[0] != 0)
1254 strcat (temp, file_suffix);
1255 if (access (temp, mode) == 0)
1257 if (pl->used_flag_ptr != 0)
1258 *pl->used_flag_ptr = 1;
1269 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1270 at the start of the list, otherwise it goes at the end.
1272 If WARN is nonzero, we will warn if no file is found
1273 through this prefix. WARN should point to an int
1274 which will be set to 1 if this entry is used. */
1277 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1278 struct path_prefix *pprefix;
1281 int require_machine_suffix;
1284 struct prefix_list *pl, **prev;
1287 if (!first && pprefix->plist)
1289 for (pl = pprefix->plist; pl->next; pl = pl->next)
1294 prev = &pprefix->plist;
1296 /* Keep track of the longest prefix */
1298 len = strlen (prefix);
1299 if (len > pprefix->max_len)
1300 pprefix->max_len = len;
1302 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1303 pl->prefix = save_string (prefix, len);
1304 pl->require_machine_suffix = require_machine_suffix;
1305 pl->used_flag_ptr = warn;
1312 pl->next = (struct prefix_list *) 0;
1316 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1319 unused_prefix_warnings (pprefix)
1320 struct path_prefix *pprefix;
1322 struct prefix_list *pl = pprefix->plist;
1326 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1328 error ("file path prefix `%s' never used",
1330 /* Prevent duplicate warnings. */
1331 *pl->used_flag_ptr = 1;
1337 /* Get rid of all prefixes built up so far in *PLISTP. */
1340 free_path_prefix (pprefix)
1341 struct path_prefix *pprefix;
1343 struct prefix_list *pl = pprefix->plist;
1344 struct prefix_list *temp;
1350 free (temp->prefix);
1351 free ((char *) temp);
1353 pprefix->plist = (struct prefix_list *) 0;
1356 /* stdin file number. */
1357 #define STDIN_FILE_NO 0
1359 /* stdout file number. */
1360 #define STDOUT_FILE_NO 1
1362 /* value of `pipe': port index for reading. */
1365 /* value of `pipe': port index for writing. */
1366 #define WRITE_PORT 1
1368 /* Pipe waiting from last process, to be used as input for the next one.
1369 Value is STDIN_FILE_NO if no pipe is waiting
1370 (i.e. the next command is the first of a group). */
1372 static int last_pipe_input;
1374 /* Fork one piped subcommand. FUNC is the system call to use
1375 (either execv or execvp). ARGV is the arg vector to use.
1376 NOT_LAST is nonzero if this is not the last subcommand
1377 (i.e. its output should be piped to the next one.) */
1382 /* Declare these to avoid compilation error. They won't be called. */
1383 int execv(const char *a, const char **b){}
1384 int execvp(const char *a, const char **b){}
1387 pexecute (search_flag, program, argv, not_last)
1397 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
1398 sprintf (scmd, "%s @%s.gp", program, temp_filename);
1399 argfile = fopen (scmd+strlen (program) + 2, "w");
1401 pfatal_with_name (scmd + strlen (program) + 2);
1403 for (i=1; argv[i]; i++)
1406 for (cp = argv[i]; *cp; cp++)
1408 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1409 fputc ('\\', argfile);
1410 fputc (*cp, argfile);
1412 fputc ('\n', argfile);
1418 remove (scmd + strlen (program) + 2);
1422 #else /* not __MSDOS__ */
1425 pexecute (search_flag, program, argv, not_last)
1431 int (*func)() = (search_flag ? execv : execvp);
1434 int input_desc = last_pipe_input;
1435 int output_desc = STDOUT_FILE_NO;
1436 int retries, sleep_interval;
1438 /* If this isn't the last process, make a pipe for its output,
1439 and record it as waiting to be the input to the next process. */
1443 if (pipe (pdes) < 0)
1444 pfatal_with_name ("pipe");
1445 output_desc = pdes[WRITE_PORT];
1446 last_pipe_input = pdes[READ_PORT];
1449 last_pipe_input = STDIN_FILE_NO;
1451 /* Fork a subprocess; wait and retry if it fails. */
1453 for (retries = 0; retries < 4; retries++)
1458 sleep (sleep_interval);
1459 sleep_interval *= 2;
1466 pfatal_with_name ("fork");
1468 pfatal_with_name ("vfork");
1474 /* Move the input and output pipes into place, if nec. */
1475 if (input_desc != STDIN_FILE_NO)
1477 close (STDIN_FILE_NO);
1481 if (output_desc != STDOUT_FILE_NO)
1483 close (STDOUT_FILE_NO);
1485 close (output_desc);
1488 /* Close the parent's descs that aren't wanted here. */
1489 if (last_pipe_input != STDIN_FILE_NO)
1490 close (last_pipe_input);
1492 /* Exec the program. */
1493 (*func) (program, argv);
1494 perror_exec (program);
1500 /* In the parent, after forking.
1501 Close the descriptors that we made for this child. */
1502 if (input_desc != STDIN_FILE_NO)
1504 if (output_desc != STDOUT_FILE_NO)
1505 close (output_desc);
1507 /* Return child's process number. */
1512 #endif /* not __MSDOS__ */
1516 pexecute (search_flag, program, argv, not_last)
1522 return (search_flag ? spawnv : spawnvp) (1, program, argv);
1524 #endif /* not OS2 */
1526 /* Execute the command specified by the arguments on the current line of spec.
1527 When using pipes, this includes several piped-together commands
1528 with `|' between them.
1530 Return 0 if successful, -1 if failed. */
1536 int n_commands; /* # of command. */
1540 char *prog; /* program name. */
1541 char **argv; /* vector of args. */
1542 int pid; /* pid of process for this command. */
1545 struct command *commands; /* each command buffer with above info. */
1547 /* Count # of piped commands. */
1548 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1549 if (strcmp (argbuf[i], "|") == 0)
1552 /* Get storage for each command. */
1554 = (struct command *) alloca (n_commands * sizeof (struct command));
1556 /* Split argbuf into its separate piped processes,
1557 and record info about each one.
1558 Also search for the programs that are to be run. */
1560 commands[0].prog = argbuf[0]; /* first command. */
1561 commands[0].argv = &argbuf[0];
1562 string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1564 commands[0].argv[0] = string;
1566 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1567 if (strcmp (argbuf[i], "|") == 0)
1568 { /* each command. */
1570 fatal ("-pipe not supported under MS-DOS");
1572 argbuf[i] = 0; /* termination of command args. */
1573 commands[n_commands].prog = argbuf[i + 1];
1574 commands[n_commands].argv = &argbuf[i + 1];
1575 string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1577 commands[n_commands].argv[0] = string;
1581 argbuf[argbuf_index] = 0;
1583 /* If -v, print what we are about to do, and maybe query. */
1587 /* Print each piped command as a separate line. */
1588 for (i = 0; i < n_commands ; i++)
1592 for (j = commands[i].argv; *j; j++)
1593 fprintf (stderr, " %s", *j);
1595 /* Print a pipe symbol after all but the last command. */
1596 if (i + 1 != n_commands)
1597 fprintf (stderr, " |");
1598 fprintf (stderr, "\n");
1602 fprintf (stderr, "\nGo ahead? (y or n) ");
1606 while (getchar () != '\n') ;
1607 if (i != 'y' && i != 'Y')
1612 /* Run each piped subprocess. */
1614 last_pipe_input = STDIN_FILE_NO;
1615 for (i = 0; i < n_commands; i++)
1617 char *string = commands[i].argv[0];
1619 commands[i].pid = pexecute (string != commands[i].prog,
1620 string, commands[i].argv,
1621 i + 1 < n_commands);
1623 if (string != commands[i].prog)
1629 /* Wait for all the subprocesses to finish.
1630 We don't care what order they finish in;
1631 we know that N_COMMANDS waits will get them all. */
1636 for (i = 0; i < n_commands; i++)
1643 status = pid = commands[i].pid;
1645 pid = wait (&status);
1653 for (j = 0; j < n_commands; j++)
1654 if (commands[j].pid == pid)
1655 prog = commands[j].prog;
1657 if ((status & 0x7F) != 0)
1658 fatal ("Internal compiler error: program %s got fatal signal %d",
1659 prog, (status & 0x7F));
1660 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1668 /* Find all the switches given to us
1669 and make a vector describing them.
1670 The elements of the vector are strings, one per switch given.
1671 If a switch uses following arguments, then the `part1' field
1672 is the switch itself and the `args' field
1673 is a null-terminated vector containing the following arguments.
1674 The `valid' field is nonzero if any spec has looked at this switch;
1675 if it remains zero at the end of the run, it must be meaningless. */
1684 static struct switchstr *switches;
1686 static int n_switches;
1694 /* Also a vector of input files specified. */
1696 static struct infile *infiles;
1698 static int n_infiles;
1700 /* And a vector of corresponding output files is made up later. */
1702 static char **outfiles;
1704 /* Create the vector `switches' and its contents.
1705 Store its length in `n_switches'. */
1708 process_command (argc, argv)
1714 char *spec_lang = 0;
1715 int last_language_n_infiles;
1717 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
1721 spec_version = version_string;
1723 /* Set up the default search paths. */
1725 if (gcc_exec_prefix)
1727 add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1728 add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1731 /* COMPILER_PATH and LIBRARY_PATH have values
1732 that are lists of directory names with colons. */
1734 temp = getenv ("COMPILER_PATH");
1737 char *startp, *endp;
1738 char *nstore = (char *) alloca (strlen (temp) + 3);
1740 startp = endp = temp;
1743 if (*endp == PATH_SEPARATOR || *endp == 0)
1745 strncpy (nstore, startp, endp-startp);
1748 strcpy (nstore, "./");
1750 else if (endp[-1] != '/')
1752 nstore[endp-startp] = '/';
1753 nstore[endp-startp+1] = 0;
1756 nstore[endp-startp] = 0;
1757 add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
1760 endp = startp = endp + 1;
1767 temp = getenv ("LIBRARY_PATH");
1770 char *startp, *endp;
1771 char *nstore = (char *) alloca (strlen (temp) + 3);
1773 startp = endp = temp;
1776 if (*endp == PATH_SEPARATOR || *endp == 0)
1778 strncpy (nstore, startp, endp-startp);
1781 strcpy (nstore, "./");
1783 else if (endp[-1] != '/')
1785 nstore[endp-startp] = '/';
1786 nstore[endp-startp+1] = 0;
1789 nstore[endp-startp] = 0;
1790 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1791 /* Make separate list of dirs that came from LIBRARY_PATH. */
1792 add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1795 endp = startp = endp + 1;
1802 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1803 temp = getenv ("LPATH");
1806 char *startp, *endp;
1807 char *nstore = (char *) alloca (strlen (temp) + 3);
1809 startp = endp = temp;
1812 if (*endp == PATH_SEPARATOR || *endp == 0)
1814 strncpy (nstore, startp, endp-startp);
1817 strcpy (nstore, "./");
1819 else if (endp[-1] != '/')
1821 nstore[endp-startp] = '/';
1822 nstore[endp-startp+1] = 0;
1825 nstore[endp-startp] = 0;
1826 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1827 /* Make separate list of dirs that came from LIBRARY_PATH. */
1828 add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1831 endp = startp = endp + 1;
1838 /* Scan argv twice. Here, the first time, just count how many switches
1839 there will be in their vector, and how many input files in theirs.
1840 Here we also parse the switches that cc itself uses (e.g. -v). */
1842 for (i = 1; i < argc; i++)
1844 if (! strcmp (argv[i], "-dumpspecs"))
1846 printf ("*asm:\n%s\n\n", asm_spec);
1847 printf ("*asm_final:\n%s\n\n", asm_final_spec);
1848 printf ("*cpp:\n%s\n\n", cpp_spec);
1849 printf ("*cc1:\n%s\n\n", cc1_spec);
1850 printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1851 printf ("*endfile:\n%s\n\n", endfile_spec);
1852 printf ("*link:\n%s\n\n", link_spec);
1853 printf ("*lib:\n%s\n\n", lib_spec);
1854 printf ("*startfile:\n%s\n\n", startfile_spec);
1855 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1856 printf ("*signed_char:\n%s\n\n", signed_char_spec);
1857 printf ("*predefines:\n%s\n\n", cpp_predefines);
1858 printf ("*cross_compile:\n%d\n\n", cross_compile);
1862 else if (! strcmp (argv[i], "-dumpversion"))
1864 printf ("%s\n", version_string);
1867 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
1869 print_libgcc_file_name = 1;
1871 else if (! strcmp (argv[i], "-Xlinker"))
1873 /* Pass the argument of this option to the linker when we link. */
1876 fatal ("argument to `-Xlinker' is missing");
1879 if (!linker_options)
1881 = (char **) xmalloc (n_linker_options * sizeof (char **));
1884 = (char **) xrealloc (linker_options,
1885 n_linker_options * sizeof (char **));
1887 linker_options[n_linker_options - 1] = argv[++i];
1889 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1891 register char *p = &argv[i][1];
1892 register int c = *p;
1897 if (p[1] == 0 && i + 1 == argc)
1898 fatal ("argument to `-b' is missing");
1900 spec_machine = argv[++i];
1902 spec_machine = p + 1;
1907 int *temp = (int *) xmalloc (sizeof (int));
1909 if (p[1] == 0 && i + 1 == argc)
1910 fatal ("argument to `-B' is missing");
1915 add_prefix (&exec_prefix, value, 1, 0, temp);
1916 add_prefix (&startfile_prefix, value, 1, 0, temp);
1920 case 'v': /* Print our subcommands and print versions. */
1926 if (p[1] == 0 && i + 1 == argc)
1927 fatal ("argument to `-V' is missing");
1929 spec_version = argv[++i];
1931 spec_version = p + 1;
1935 if (!strcmp (p, "save-temps"))
1937 save_temps_flag = 1;
1944 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1945 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1946 else if (WORD_SWITCH_TAKES_ARG (p))
1947 i += WORD_SWITCH_TAKES_ARG (p);
1954 /* Set up the search paths before we go looking for config files. */
1956 /* These come before the md prefixes so that we will find gcc's subcommands
1957 (such as cpp) rather than those of the host system. */
1958 add_prefix (&exec_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
1959 add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
1961 add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
1962 add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
1964 /* More prefixes are enabled in main, after we read the specs file
1965 and determine whether this is cross-compilation or not. */
1968 /* Then create the space for the vectors and scan again. */
1970 switches = ((struct switchstr *)
1971 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
1972 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
1975 last_language_n_infiles = -1;
1977 /* This, time, copy the text of each switch and store a pointer
1978 to the copy in the vector of switches.
1979 Store all the infiles in their vector. */
1981 for (i = 1; i < argc; i++)
1983 if (!strcmp (argv[i], "-Xlinker"))
1985 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
1987 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1989 register char *p = &argv[i][1];
1990 register int c = *p;
1992 if (c == 'B' || c == 'b' || c == 'V')
1994 /* Skip a separate arg, if any. */
2001 if (p[1] == 0 && i + 1 == argc)
2002 fatal ("argument to `-x' is missing");
2004 spec_lang = argv[++i];
2007 if (! strcmp (spec_lang, "none"))
2008 /* Suppress the warning if -xnone comes after the last input file,
2009 because alternate command interfaces like g++ might find it
2010 useful to place -xnone after each input file. */
2013 last_language_n_infiles = n_infiles;
2016 switches[n_switches].part1 = p;
2017 /* Deal with option arguments in separate argv elements. */
2018 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2019 || WORD_SWITCH_TAKES_ARG (p)) {
2021 int n_args = WORD_SWITCH_TAKES_ARG (p);
2024 /* Count only the option arguments in separate argv elements. */
2025 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2027 switches[n_switches].args
2028 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2030 switches[n_switches].args[j++] = argv[++i];
2031 /* Null-terminate the vector. */
2032 switches[n_switches].args[j] = 0;
2033 } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
2034 /* On some systems, ld cannot handle -o or -L without space.
2035 So split the -o or -L from its argument. */
2036 switches[n_switches].part1 = (c == 'o' ? "o" : "L");
2037 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2038 switches[n_switches].args[0] = xmalloc (strlen (p));
2039 strcpy (switches[n_switches].args[0], &p[1]);
2040 switches[n_switches].args[1] = 0;
2042 switches[n_switches].args = 0;
2043 switches[n_switches].valid = 0;
2044 /* This is always valid, since gcc.c itself understands it. */
2045 if (!strcmp (p, "save-temps"))
2046 switches[n_switches].valid = 1;
2051 infiles[n_infiles].language = spec_lang;
2052 infiles[n_infiles++].name = argv[i];
2056 if (n_infiles == last_language_n_infiles)
2057 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2059 switches[n_switches].part1 = 0;
2060 infiles[n_infiles].name = 0;
2062 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
2063 if (gcc_exec_prefix)
2065 temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
2066 + strlen (spec_machine) + 3);
2067 strcpy (temp, gcc_exec_prefix);
2068 strcat (temp, spec_machine);
2070 strcat (temp, spec_version);
2072 gcc_exec_prefix = temp;
2076 /* Process a spec string, accumulating and running commands. */
2078 /* These variables describe the input file name.
2079 input_file_number is the index on outfiles of this file,
2080 so that the output file name can be stored for later use by %o.
2081 input_basename is the start of the part of the input file
2082 sans all directory names, and basename_length is the number
2083 of characters starting there excluding the suffix .c or whatever. */
2085 static char *input_filename;
2086 static int input_file_number;
2087 static int input_filename_length;
2088 static int basename_length;
2089 static char *input_basename;
2090 static char *input_suffix;
2092 /* These are variables used within do_spec and do_spec_1. */
2094 /* Nonzero if an arg has been started and not yet terminated
2095 (with space, tab or newline). */
2096 static int arg_going;
2098 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2099 is a temporary file name. */
2100 static int delete_this_arg;
2102 /* Nonzero means %w has been seen; the next arg to be terminated
2103 is the output file name of this compilation. */
2104 static int this_is_output_file;
2106 /* Nonzero means %s has been seen; the next arg to be terminated
2107 is the name of a library file and we should try the standard
2108 search dirs for it. */
2109 static int this_is_library_file;
2111 /* Process the spec SPEC and run the commands specified therein.
2112 Returns 0 if the spec is successfully processed; -1 if failed. */
2122 delete_this_arg = 0;
2123 this_is_output_file = 0;
2124 this_is_library_file = 0;
2126 value = do_spec_1 (spec, 0, NULL_PTR);
2128 /* Force out any unfinished command.
2129 If -pipe, this forces out the last command if it ended in `|'. */
2132 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2135 if (argbuf_index > 0)
2142 /* Process the sub-spec SPEC as a portion of a larger spec.
2143 This is like processing a whole spec except that we do
2144 not initialize at the beginning and we do not supply a
2145 newline by default at the end.
2146 INSWITCH nonzero means don't process %-sequences in SPEC;
2147 in this case, % is treated as an ordinary character.
2148 This is used while substituting switches.
2149 INSWITCH nonzero also causes SPC not to terminate an argument.
2151 Value is zero unless a line was finished
2152 and the command on that line reported an error. */
2155 do_spec_1 (spec, inswitch, soft_matched_part)
2158 char *soft_matched_part;
2160 register char *p = spec;
2166 /* If substituting a switch, treat all chars like letters.
2167 Otherwise, NL, SPC, TAB and % are special. */
2168 switch (inswitch ? 'a' : c)
2171 /* End of line: finish any pending argument,
2172 then run the pending command if one has been started. */
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;
2185 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2188 for (i = 0; i < n_switches; i++)
2189 if (!strcmp (switches[i].part1, "pipe"))
2192 /* A `|' before the newline means use a pipe here,
2193 but only if -pipe was specified.
2194 Otherwise, execute now and don't pass the `|' as an arg. */
2197 switches[i].valid = 1;
2204 if (argbuf_index > 0)
2206 int value = execute ();
2210 /* Reinitialize for a new command, and for a new argument. */
2213 delete_this_arg = 0;
2214 this_is_output_file = 0;
2215 this_is_library_file = 0;
2219 /* End any pending argument. */
2222 obstack_1grow (&obstack, 0);
2223 string = obstack_finish (&obstack);
2224 if (this_is_library_file)
2225 string = find_file (string);
2226 store_arg (string, delete_this_arg, this_is_output_file);
2227 if (this_is_output_file)
2228 outfiles[input_file_number] = string;
2232 obstack_1grow (&obstack, c);
2238 /* Space or tab ends an argument if one is pending. */
2241 obstack_1grow (&obstack, 0);
2242 string = obstack_finish (&obstack);
2243 if (this_is_library_file)
2244 string = find_file (string);
2245 store_arg (string, delete_this_arg, this_is_output_file);
2246 if (this_is_output_file)
2247 outfiles[input_file_number] = string;
2249 /* Reinitialize for a new argument. */
2251 delete_this_arg = 0;
2252 this_is_output_file = 0;
2253 this_is_library_file = 0;
2260 fatal ("Invalid specification! Bug in cc.");
2263 obstack_grow (&obstack, input_basename, basename_length);
2268 delete_this_arg = 2;
2271 /* Dump out the directories specified with LIBRARY_PATH,
2272 followed by the absolute directories
2273 that we search for startfiles. */
2275 for (i = 0; i < 2; i++)
2277 struct prefix_list *pl
2278 = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2280 char *buffer = (char *) xmalloc (bufsize);
2283 for (; pl; pl = pl->next)
2285 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2286 /* Used on systems which record the specified -L dirs
2287 and use them to search for dynamic linking. */
2288 /* Relative directories always come from -B,
2289 and it is better not to use them for searching
2290 at run time. In particular, stage1 loses */
2291 if (pl->prefix[0] != '/')
2296 if (is_linker_dir (pl->prefix, machine_suffix))
2298 do_spec_1 ("-L", 0, NULL_PTR);
2299 #ifdef SPACE_AFTER_L_OPTION
2300 do_spec_1 (" ", 0, NULL_PTR);
2302 do_spec_1 (pl->prefix, 1, NULL_PTR);
2303 /* Remove slash from machine_suffix. */
2304 if (strlen (machine_suffix) >= bufsize)
2305 bufsize = strlen (machine_suffix) * 2 + 1;
2306 buffer = (char *) xrealloc (buffer, bufsize);
2307 strcpy (buffer, machine_suffix);
2308 idx = strlen (buffer);
2309 if (buffer[idx - 1] == '/')
2310 buffer[idx - 1] = 0;
2311 do_spec_1 (buffer, 1, NULL_PTR);
2312 /* Make this a separate argument. */
2313 do_spec_1 (" ", 0, NULL_PTR);
2316 if (!pl->require_machine_suffix)
2318 if (is_linker_dir (pl->prefix, ""))
2320 do_spec_1 ("-L", 0, NULL_PTR);
2321 #ifdef SPACE_AFTER_L_OPTION
2322 do_spec_1 (" ", 0, NULL_PTR);
2324 /* Remove slash from pl->prefix. */
2325 if (strlen (pl->prefix) >= bufsize)
2326 bufsize = strlen (pl->prefix) * 2 + 1;
2327 buffer = (char *) xrealloc (buffer, bufsize);
2328 strcpy (buffer, pl->prefix);
2329 idx = strlen (buffer);
2330 if (buffer[idx - 1] == '/')
2331 buffer[idx - 1] = 0;
2332 do_spec_1 (buffer, 1, NULL_PTR);
2333 /* Make this a separate argument. */
2334 do_spec_1 (" ", 0, NULL_PTR);
2343 /* {...:%efoo} means report an error with `foo' as error message
2344 and don't execute any more commands for this file. */
2348 while (*p != 0 && *p != '\n') p++;
2349 buf = (char *) alloca (p - q + 1);
2350 strncpy (buf, q, p - q);
2358 if (save_temps_flag)
2359 obstack_grow (&obstack, input_basename, basename_length);
2362 obstack_grow (&obstack, temp_filename, temp_filename_length);
2363 delete_this_arg = 1;
2369 obstack_grow (&obstack, input_filename, input_filename_length);
2374 if (gcc_exec_prefix)
2376 do_spec_1 ("-iprefix", 1, 0);
2377 /* Make this a separate argument. */
2378 do_spec_1 (" ", 0, 0);
2379 do_spec_1 (gcc_exec_prefix, 1, 0);
2380 do_spec_1 (" ", 0, 0);
2387 for (f = 0; f < n_infiles; f++)
2388 store_arg (outfiles[f], 0, 0);
2393 this_is_library_file = 1;
2397 this_is_output_file = 1;
2402 int index = argbuf_index;
2403 /* Handle the {...} following the %W. */
2406 p = handle_braces (p + 1);
2409 /* If any args were output, mark the last one for deletion
2411 if (argbuf_index != index)
2412 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2416 /* %x{OPTION} records OPTION for %X to output. */
2422 /* Skip past the option value and make a copy. */
2427 string = save_string (p1 + 1, p - p1 - 2);
2429 /* See if we already recorded this option. */
2430 for (i = 0; i < n_linker_options; i++)
2431 if (! strcmp (string, linker_options[i]))
2437 /* This option is new; add it. */
2439 if (!linker_options)
2441 = (char **) xmalloc (n_linker_options * sizeof (char **));
2444 = (char **) xrealloc (linker_options,
2445 n_linker_options * sizeof (char **));
2447 linker_options[n_linker_options - 1] = string;
2451 /* Dump out the options accumulated previously using %x. */
2453 for (i = 0; i < n_linker_options; i++)
2455 do_spec_1 (linker_options[i], 1, NULL_PTR);
2456 /* Make each accumulated option a separate argument. */
2457 do_spec_1 (" ", 0, NULL_PTR);
2461 /* Here are digits and numbers that just process
2462 a certain constant string as a spec. */
2465 do_spec_1 (cc1_spec, 0, NULL_PTR);
2469 do_spec_1 (cc1plus_spec, 0, NULL_PTR);
2473 do_spec_1 (asm_spec, 0, NULL_PTR);
2477 do_spec_1 (asm_final_spec, 0, NULL_PTR);
2481 do_spec_1 (signed_char_spec, 0, NULL_PTR);
2485 do_spec_1 (cpp_spec, 0, NULL_PTR);
2489 do_spec_1 (endfile_spec, 0, NULL_PTR);
2493 do_spec_1 (link_spec, 0, NULL_PTR);
2497 do_spec_1 (lib_spec, 0, NULL_PTR);
2502 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2506 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2510 if (! strncmp (y, "-D", 2))
2511 /* Copy the whole option. */
2512 while (*y && *y != ' ' && *y != '\t')
2514 else if (*y == ' ' || *y == '\t')
2515 /* Copy whitespace to the result. */
2517 /* Don't copy other options. */
2524 do_spec_1 (buf, 0, NULL_PTR);
2530 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2534 /* Copy all of CPP_PREDEFINES into BUF,
2535 but put __ after every -D and at the end of each arg. */
2539 if (! strncmp (y, "-D", 2))
2546 if (strncmp (y, "__", 2))
2548 /* Stick __ at front of macro name. */
2551 /* Arrange to stick __ at the end as well. */
2555 /* Copy the macro name. */
2556 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2565 /* Copy the value given, if any. */
2566 while (*y && *y != ' ' && *y != '\t')
2569 else if (*y == ' ' || *y == '\t')
2570 /* Copy whitespace to the result. */
2572 /* Don't copy -A options */
2578 /* Copy all of CPP_PREDEFINES into BUF,
2579 but put __ after every -D. */
2583 if (! strncmp (y, "-D", 2))
2588 if (strncmp (y, "__", 2))
2590 /* Stick __ at front of macro name. */
2595 /* Copy the macro name. */
2596 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2599 /* Copy the value given, if any. */
2600 while (*y && *y != ' ' && *y != '\t')
2603 else if (*y == ' ' || *y == '\t')
2604 /* Copy whitespace to the result. */
2606 /* Don't copy -A options */
2612 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2616 if (! strncmp (y, "-A", 2))
2617 /* Copy the whole option. */
2618 while (*y && *y != ' ' && *y != '\t')
2620 else if (*y == ' ' || *y == '\t')
2621 /* Copy whitespace to the result. */
2623 /* Don't copy other options. */
2630 do_spec_1 (buf, 0, NULL_PTR);
2635 do_spec_1 (startfile_spec, 0, NULL_PTR);
2638 /* Here we define characters other than letters and digits. */
2641 p = handle_braces (p);
2647 obstack_1grow (&obstack, '%');
2651 do_spec_1 (soft_matched_part, 1, NULL_PTR);
2652 do_spec_1 (" ", 0, NULL_PTR);
2655 /* Process a string found as the value of a spec given by name.
2656 This feature allows individual machine descriptions
2657 to add and use their own specs.
2658 %[...] modifies -D options the way %P does;
2659 %(...) uses the spec unmodified. */
2664 struct spec_list *sl;
2667 /* The string after the S/P is the name of a spec that is to be
2669 while (*p && *p != ')' && *p != ']')
2672 /* See if it's in the list */
2673 for (len = p - name, sl = specs; sl; sl = sl->next)
2674 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2683 do_spec_1 (name, 0, NULL_PTR);
2686 char *x = (char *) alloca (strlen (name) * 2 + 1);
2690 /* Copy all of NAME into BUF, but put __ after
2691 every -D and at the end of each arg, */
2694 if (! strncmp (y, "-D", 2))
2702 else if (*y == ' ' || *y == 0)
2716 do_spec_1 (buf, 0, NULL_PTR);
2720 /* Discard the closing paren or bracket. */
2732 /* Backslash: treat next character as ordinary. */
2737 /* Ordinary character: put it into the current argument. */
2738 obstack_1grow (&obstack, c);
2742 return 0; /* End of string */
2745 /* Return 0 if we call do_spec_1 and that returns -1. */
2758 /* A `|' after the open-brace means,
2759 if the test fails, output a single minus sign rather than nothing.
2760 This is used in %{|!pipe:...}. */
2764 /* A `!' after the open-brace negates the condition:
2765 succeed if the specified switch is not present. */
2769 /* A `.' after the open-brace means test against the current suffix. */
2779 while (*p != ':' && *p != '}') p++;
2782 register int count = 1;
2800 int found = (input_suffix != 0
2801 && strlen (input_suffix) == p - filter
2802 && strncmp (input_suffix, filter, p - filter) == 0);
2808 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2813 else if (p[-1] == '*' && p[0] == '}')
2815 /* Substitute all matching switches as separate args. */
2818 for (i = 0; i < n_switches; i++)
2819 if (!strncmp (switches[i].part1, filter, p - filter))
2824 /* Test for presence of the specified switch. */
2828 /* If name specified ends in *, as in {x*:...},
2829 check for %* and handle that case. */
2830 if (p[-1] == '*' && !negate)
2835 /* First see whether we have %*. */
2839 if (*r == '%' && r[1] == '*')
2843 /* If we do, handle that case. */
2846 /* Substitute all matching switches as separate args.
2847 But do this by substituting for %*
2848 in the text that follows the colon. */
2850 unsigned hard_match_len = p - filter - 1;
2851 char *string = save_string (p + 1, q - p - 2);
2853 for (i = 0; i < n_switches; i++)
2854 if (!strncmp (switches[i].part1, filter, hard_match_len))
2856 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
2857 /* Pass any arguments this switch has. */
2865 /* If name specified ends in *, as in {x*:...},
2866 check for presence of any switch name starting with x. */
2869 for (i = 0; i < n_switches; i++)
2871 unsigned hard_match_len = p - filter - 1;
2873 if (!strncmp (switches[i].part1, filter, hard_match_len))
2875 switches[i].valid = 1;
2880 /* Otherwise, check for presence of exact name specified. */
2883 for (i = 0; i < n_switches; i++)
2885 if (!strncmp (switches[i].part1, filter, p - filter)
2886 && switches[i].part1[p - filter] == 0)
2888 switches[i].valid = 1;
2895 /* If it is as desired (present for %{s...}, absent for %{-s...})
2896 then substitute either the switch or the specified
2897 conditional text. */
2898 if (present != negate)
2906 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2912 /* Here if a %{|...} conditional fails: output a minus sign,
2913 which means "standard output" or "standard input". */
2914 do_spec_1 ("-", 0, NULL_PTR);
2921 /* Pass a switch to the current accumulating command
2922 in the same form that we received it.
2923 SWITCHNUM identifies the switch; it is an index into
2924 the vector of switches gcc received, which is `switches'.
2925 This cannot fail since it never finishes a command line.
2927 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2930 give_switch (switchnum, omit_first_word)
2932 int omit_first_word;
2934 if (!omit_first_word)
2936 do_spec_1 ("-", 0, NULL_PTR);
2937 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
2939 do_spec_1 (" ", 0, NULL_PTR);
2940 if (switches[switchnum].args != 0)
2943 for (p = switches[switchnum].args; *p; p++)
2945 do_spec_1 (*p, 1, NULL_PTR);
2946 do_spec_1 (" ", 0, NULL_PTR);
2949 switches[switchnum].valid = 1;
2952 /* Search for a file named NAME trying various prefixes including the
2953 user's -B prefix and some standard ones.
2954 Return the absolute file name found. If nothing is found, return NAME. */
2962 newname = find_a_file (&startfile_prefix, name, R_OK);
2963 return newname ? newname : name;
2966 /* Determine whether a -L option is relevant. Not required for certain
2967 fixed names and for directories that don't exist. */
2970 is_linker_dir (path1, path2)
2974 int len1 = strlen (path1);
2975 int len2 = strlen (path2);
2976 char *path = (char *) alloca (3 + len1 + len2);
2980 /* Construct the path from the two parts. Ensure the string ends with "/.".
2981 The resulting path will be a directory even if the given path is a
2983 bcopy (path1, path, len1);
2984 bcopy (path2, path + len1, len2);
2985 cp = path + len1 + len2;
2991 /* Exclude directories that the linker is known to search. */
2992 if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
2993 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
2996 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
2999 /* On fatal signals, delete all the temporary files. */
3002 fatal_error (signum)
3005 signal (signum, SIG_DFL);
3006 delete_failure_queue ();
3007 delete_temp_files ();
3008 /* Get the same signal again, this time not handled,
3009 so its normal effect occurs. */
3010 kill (getpid (), signum);
3020 int error_count = 0;
3021 int linker_was_run = 0;
3022 char *explicit_link_files;
3025 programname = argv[0];
3027 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3028 signal (SIGINT, fatal_error);
3029 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3030 signal (SIGHUP, fatal_error);
3031 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3032 signal (SIGTERM, fatal_error);
3034 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3035 signal (SIGPIPE, fatal_error);
3039 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3041 obstack_init (&obstack);
3043 /* Set up to remember the pathname of gcc and any options
3044 needed for collect. */
3045 obstack_init (&collect_obstack);
3046 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3047 obstack_grow (&collect_obstack, programname, strlen (programname)+1);
3048 putenv (obstack_finish (&collect_obstack));
3050 /* Choose directory for temp files. */
3052 choose_temp_base ();
3054 /* Make a table of what switches there are (switches, n_switches).
3055 Make a table of specified input files (infiles, n_infiles).
3056 Decode switches that are handled locally. */
3058 process_command (argc, argv);
3060 /* Initialize the vector of specs to just the default.
3061 This means one element containing 0s, as a terminator. */
3063 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3064 bcopy (default_compilers, compilers, sizeof default_compilers);
3065 n_compilers = n_default_compilers;
3067 /* Read specs from a file if there is one. */
3069 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
3071 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3072 /* Read the specs file unless it is a default one. */
3073 if (specs_file != 0 && strcmp (specs_file, "specs"))
3074 read_specs (specs_file);
3076 /* If not cross-compiling, look for startfiles in the standard places. */
3077 /* The fact that these are done here, after reading the specs file,
3078 means that it cannot be found in these directories.
3079 But that's okay. It should never be there anyway. */
3082 #ifdef MD_EXEC_PREFIX
3083 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3084 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3087 #ifdef MD_STARTFILE_PREFIX
3088 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
3091 #ifdef MD_STARTFILE_PREFIX_1
3092 add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
3095 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3097 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3099 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3101 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3102 add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
3106 /* Now we have the specs.
3107 Set the `valid' bits for switches that match anything in any spec. */
3109 validate_all_switches ();
3111 /* Warn about any switches that no pass was interested in. */
3113 for (i = 0; i < n_switches; i++)
3114 if (! switches[i].valid)
3115 error ("unrecognized option `-%s'", switches[i].part1);
3117 if (print_libgcc_file_name)
3119 printf ("%s\n", find_file ("libgcc.a"));
3123 /* Obey some of the options. */
3127 fprintf (stderr, "gcc version %s\n", version_string);
3133 fatal ("No input files specified.");
3135 /* Make a place to record the compiler output file names
3136 that correspond to the input files. */
3138 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3139 bzero (outfiles, n_infiles * sizeof (char *));
3141 /* Record which files were specified explicitly as link input. */
3143 explicit_link_files = xmalloc (n_infiles);
3144 bzero (explicit_link_files, n_infiles);
3146 for (i = 0; i < n_infiles; i++)
3148 register struct compiler *cp = 0;
3149 int this_file_error = 0;
3151 /* Tell do_spec what to substitute for %i. */
3153 input_filename = infiles[i].name;
3154 input_filename_length = strlen (input_filename);
3155 input_file_number = i;
3157 /* Use the same thing in %o, unless cp->spec says otherwise. */
3159 outfiles[i] = input_filename;
3161 /* Figure out which compiler from the file's suffix. */
3163 cp = lookup_compiler (infiles[i].name, input_filename_length,
3164 infiles[i].language);
3168 /* Ok, we found an applicable compiler. Run its spec. */
3169 /* First say how much of input_filename to substitute for %b */
3172 input_basename = input_filename;
3173 for (p = input_filename; *p; p++)
3175 input_basename = p + 1;
3177 /* Find a suffix starting with the last period,
3178 and set basename_length to exclude that suffix. */
3179 basename_length = strlen (input_basename);
3180 p = input_basename + basename_length;
3181 while (p != input_basename && *p != '.') --p;
3182 if (*p == '.' && p != input_basename)
3184 basename_length = p - input_basename;
3185 input_suffix = p + 1;
3190 value = do_spec (cp->spec);
3192 this_file_error = 1;
3195 /* If this file's name does not contain a recognized suffix,
3196 record it as explicit linker input. */
3199 explicit_link_files[i] = 1;
3201 /* Clear the delete-on-failure queue, deleting the files in it
3202 if this compilation failed. */
3204 if (this_file_error)
3206 delete_failure_queue ();
3209 /* If this compilation succeeded, don't delete those files later. */
3210 clear_failure_queue ();
3213 /* Run ld to link all the compiler output files. */
3215 if (error_count == 0)
3217 int tmp = execution_count;
3221 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3223 putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3224 putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3226 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3228 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3229 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3232 for (i = 0; i < n_switches; i++)
3236 obstack_grow (&collect_obstack, " ", 1);
3239 obstack_grow (&collect_obstack, "-", 1);
3240 obstack_grow (&collect_obstack, switches[i].part1,
3241 strlen (switches[i].part1));
3243 for (args = switches[i].args; args && *args; args++)
3245 obstack_grow (&collect_obstack, " ", 1);
3246 obstack_grow (&collect_obstack, *args, strlen (*args));
3249 obstack_grow (&collect_obstack, "\0", 1);
3250 putenv (obstack_finish (&collect_obstack));
3252 value = do_spec (link_command_spec);
3255 linker_was_run = (tmp != execution_count);
3258 /* Warn if a -B option was specified but the prefix was never used. */
3259 unused_prefix_warnings (&exec_prefix);
3260 unused_prefix_warnings (&startfile_prefix);
3262 /* If options said don't run linker,
3263 complain about input files to be given to the linker. */
3265 if (! linker_was_run && error_count == 0)
3266 for (i = 0; i < n_infiles; i++)
3267 if (explicit_link_files[i])
3268 error ("%s: linker input file unused since linking not done",
3271 /* Delete some or all of the temporary files we made. */
3274 delete_failure_queue ();
3275 delete_temp_files ();
3282 /* Find the proper compilation spec for the file name NAME,
3283 whose length is LENGTH. LANGUAGE is the specified language,
3284 or 0 if none specified. */
3286 static struct compiler *
3287 lookup_compiler (name, length, language)
3292 struct compiler *cp;
3294 /* Look for the language, if one is spec'd. */
3297 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3301 if (cp->suffix[0] == '@'
3302 && !strcmp (cp->suffix + 1, language))
3306 error ("language %s not recognized", language);
3309 /* Look for a suffix. */
3310 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3312 if (strlen (cp->suffix) < length
3313 /* See if the suffix matches the end of NAME. */
3314 && !strcmp (cp->suffix,
3315 name + length - strlen (cp->suffix))
3316 /* The suffix `-' matches only the file name `-'. */
3317 && !(!strcmp (cp->suffix, "-") && length != 1))
3319 if (cp->spec[0] == '@')
3321 struct compiler *new;
3322 /* An alias entry maps a suffix to a language.
3323 Search for the language; pass 0 for NAME and LENGTH
3324 to avoid infinite recursion if language not found.
3325 Construct the new compiler spec. */
3326 language = cp->spec + 1;
3327 new = (struct compiler *) xmalloc (sizeof (struct compiler));
3328 new->suffix = cp->suffix;
3329 new->spec = lookup_compiler (NULL_PTR, 0, language)->spec;
3332 /* A non-alias entry: return it. */
3344 register char *value = (char *) malloc (size);
3346 fatal ("virtual memory exhausted");
3351 xrealloc (ptr, size)
3355 register char *value = (char *) realloc (ptr, size);
3357 fatal ("virtual memory exhausted");
3361 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3367 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3368 char *result = xmalloc (len1 + len2 + len3 + 1);
3370 strcpy (result, s1);
3371 strcpy (result + len1, s2);
3372 strcpy (result + len1 + len2, s3);
3373 *(result + len1 + len2 + len3) = 0;
3379 save_string (s, len)
3383 register char *result = xmalloc (len + 1);
3385 bcopy (s, result, len);
3391 pfatal_with_name (name)
3396 if (errno < sys_nerr)
3397 s = concat ("%s: ", sys_errlist[errno], "");
3399 s = "cannot open %s";
3404 perror_with_name (name)
3409 if (errno < sys_nerr)
3410 s = concat ("%s: ", sys_errlist[errno], "");
3412 s = "cannot open %s";
3422 if (errno < sys_nerr)
3423 s = concat ("installation problem, cannot exec %s: ",
3424 sys_errlist[errno], "");
3426 s = "installation problem, cannot exec %s";
3430 /* More 'friendly' abort that prints the line and file.
3431 config.h can #define abort fancy_abort if you like that sort of thing. */
3436 fatal ("Internal gcc abort.");
3441 /* Output an error message and exit */
3451 format = va_arg (ap, char *);
3452 fprintf (stderr, "%s: ", programname);
3453 vfprintf (stderr, format, ap);
3455 fprintf (stderr, "\n");
3456 delete_temp_files ();
3468 format = va_arg (ap, char *);
3469 fprintf (stderr, "%s: ", programname);
3470 vfprintf (stderr, format, ap);
3473 fprintf (stderr, "\n");
3476 #else /* not HAVE_VPRINTF */
3479 fatal (msg, arg1, arg2)
3480 char *msg, *arg1, *arg2;
3482 error (msg, arg1, arg2);
3483 delete_temp_files ();
3488 error (msg, arg1, arg2)
3489 char *msg, *arg1, *arg2;
3491 fprintf (stderr, "%s: ", programname);
3492 fprintf (stderr, msg, arg1, arg2);
3493 fprintf (stderr, "\n");
3496 #endif /* not HAVE_VPRINTF */
3500 validate_all_switches ()
3502 struct compiler *comp;
3505 struct spec_list *spec;
3507 for (comp = compilers; comp->spec; comp++)
3511 if (c == '%' && *p == '{')
3512 /* We have a switch spec. */
3513 validate_switches (p + 1);
3516 /* look through the linked list of extra specs read from the specs file */
3517 for (spec = specs ; spec ; spec = spec->next)
3521 if (c == '%' && *p == '{')
3522 /* We have a switch spec. */
3523 validate_switches (p + 1);
3526 p = link_command_spec;
3528 if (c == '%' && *p == '{')
3529 /* We have a switch spec. */
3530 validate_switches (p + 1);
3532 /* Now notice switches mentioned in the machine-specific specs. */
3536 if (c == '%' && *p == '{')
3537 /* We have a switch spec. */
3538 validate_switches (p + 1);
3542 if (c == '%' && *p == '{')
3543 /* We have a switch spec. */
3544 validate_switches (p + 1);
3548 if (c == '%' && *p == '{')
3549 /* We have a switch spec. */
3550 validate_switches (p + 1);
3552 p = signed_char_spec;
3554 if (c == '%' && *p == '{')
3555 /* We have a switch spec. */
3556 validate_switches (p + 1);
3560 if (c == '%' && *p == '{')
3561 /* We have a switch spec. */
3562 validate_switches (p + 1);
3566 if (c == '%' && *p == '{')
3567 /* We have a switch spec. */
3568 validate_switches (p + 1);
3572 if (c == '%' && *p == '{')
3573 /* We have a switch spec. */
3574 validate_switches (p + 1);
3578 if (c == '%' && *p == '{')
3579 /* We have a switch spec. */
3580 validate_switches (p + 1);
3584 if (c == '%' && *p == '{')
3585 /* We have a switch spec. */
3586 validate_switches (p + 1);
3589 /* Look at the switch-name that comes after START
3590 and mark as valid all supplied switches that match it. */
3593 validate_switches (start)
3596 register char *p = start;
3611 while (*p != ':' && *p != '}') p++;
3615 else if (p[-1] == '*')
3617 /* Mark all matching switches as valid. */
3619 for (i = 0; i < n_switches; i++)
3620 if (!strncmp (switches[i].part1, filter, p - filter))
3621 switches[i].valid = 1;
3625 /* Mark an exact matching switch as valid. */
3626 for (i = 0; i < n_switches; i++)
3628 if (!strncmp (switches[i].part1, filter, p - filter)
3629 && switches[i].part1[p - filter] == 0)
3630 switches[i].valid = 1;