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 /* 2 means try both machine_suffix and just_machine_suffix. */
817 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
822 struct prefix_list *plist; /* List of prefixes to try */
823 int max_len; /* Max length of a prefix in PLIST */
824 char *name; /* Name of this list (used in config stuff) */
827 /* List of prefixes to try when looking for executables. */
829 static struct path_prefix exec_prefix = { 0, 0, "exec" };
831 /* List of prefixes to try when looking for startup (crt0) files. */
833 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
835 /* List of prefixes to try when looking for libraries. */
837 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
839 /* Suffix to attach to directories searched for commands.
840 This looks like `MACHINE/VERSION/'. */
842 static char *machine_suffix = 0;
844 /* Suffix to attach to directories searched for commands.
845 This is just `MACHINE/'. */
847 static char *just_machine_suffix = 0;
849 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
851 static char *gcc_exec_prefix;
853 /* Default prefixes to attach to command names. */
855 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
856 #undef MD_EXEC_PREFIX
857 #undef MD_STARTFILE_PREFIX
858 #undef MD_STARTFILE_PREFIX_1
861 #ifndef STANDARD_EXEC_PREFIX
862 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
863 #endif /* !defined STANDARD_EXEC_PREFIX */
865 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
866 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
867 #ifdef MD_EXEC_PREFIX
868 static char *md_exec_prefix = MD_EXEC_PREFIX;
871 #ifndef STANDARD_STARTFILE_PREFIX
872 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
873 #endif /* !defined STANDARD_STARTFILE_PREFIX */
875 #ifdef MD_STARTFILE_PREFIX
876 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
878 #ifdef MD_STARTFILE_PREFIX_1
879 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
881 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
882 static char *standard_startfile_prefix_1 = "/lib/";
883 static char *standard_startfile_prefix_2 = "/usr/lib/";
885 /* Clear out the vector of arguments (after a command is executed). */
893 /* Add one argument to the vector at the end.
894 This is done when a space is seen or at the end of the line.
895 If DELETE_ALWAYS is nonzero, the arg is a filename
896 and the file should be deleted eventually.
897 If DELETE_FAILURE is nonzero, the arg is a filename
898 and the file should be deleted if this compilation fails. */
901 store_arg (arg, delete_always, delete_failure)
903 int delete_always, delete_failure;
905 if (argbuf_index + 1 == argbuf_length)
907 argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
910 argbuf[argbuf_index++] = arg;
911 argbuf[argbuf_index] = 0;
913 if (delete_always || delete_failure)
914 record_temp_file (arg, delete_always, delete_failure);
917 /* Record the names of temporary files we tell compilers to write,
918 and delete them at the end of the run. */
920 /* This is the common prefix we use to make temp file names.
921 It is chosen once for each run of this program.
922 It is substituted into a spec by %g.
923 Thus, all temp file names contain this prefix.
924 In practice, all temp file names start with this prefix.
926 This prefix comes from the envvar TMPDIR if it is defined;
927 otherwise, from the P_tmpdir macro if that is defined;
928 otherwise, in /usr/tmp or /tmp. */
930 static char *temp_filename;
932 /* Length of the prefix. */
934 static int temp_filename_length;
936 /* Define the list of temporary files to delete. */
941 struct temp_file *next;
944 /* Queue of files to delete on success or failure of compilation. */
945 static struct temp_file *always_delete_queue;
946 /* Queue of files to delete on failure of compilation. */
947 static struct temp_file *failure_delete_queue;
949 /* Record FILENAME as a file to be deleted automatically.
950 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
951 otherwise delete it in any case.
952 FAIL_DELETE nonzero means delete it if a compilation step fails;
953 otherwise delete it in any case. */
956 record_temp_file (filename, always_delete, fail_delete)
962 name = xmalloc (strlen (filename) + 1);
963 strcpy (name, filename);
967 register struct temp_file *temp;
968 for (temp = always_delete_queue; temp; temp = temp->next)
969 if (! strcmp (name, temp->name))
971 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
972 temp->next = always_delete_queue;
974 always_delete_queue = temp;
980 register struct temp_file *temp;
981 for (temp = failure_delete_queue; temp; temp = temp->next)
982 if (! strcmp (name, temp->name))
984 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
985 temp->next = failure_delete_queue;
987 failure_delete_queue = temp;
992 /* Delete all the temporary files whose names we previously recorded. */
997 register struct temp_file *temp;
999 for (temp = always_delete_queue; temp; temp = temp->next)
1003 printf ("Delete %s? (y or n) ", temp->name);
1007 while (getchar () != '\n') ;
1008 if (i == 'y' || i == 'Y')
1012 if (stat (temp->name, &st) >= 0)
1014 /* Delete only ordinary files. */
1015 if (S_ISREG (st.st_mode))
1016 if (unlink (temp->name) < 0)
1018 perror_with_name (temp->name);
1023 always_delete_queue = 0;
1026 /* Delete all the files to be deleted on error. */
1029 delete_failure_queue ()
1031 register struct temp_file *temp;
1033 for (temp = failure_delete_queue; temp; temp = temp->next)
1037 printf ("Delete %s? (y or n) ", temp->name);
1041 while (getchar () != '\n') ;
1042 if (i == 'y' || i == 'Y')
1045 if (unlink (temp->name) < 0)
1047 perror_with_name (temp->name);
1053 clear_failure_queue ()
1055 failure_delete_queue = 0;
1058 /* Compute a string to use as the base of all temporary file names.
1059 It is substituted for %g. */
1064 char *base = getenv ("TMPDIR");
1067 if (base == (char *)0)
1070 if (access (P_tmpdir, R_OK | W_OK) == 0)
1073 if (base == (char *)0)
1075 if (access ("/usr/tmp", R_OK | W_OK) == 0)
1082 len = strlen (base);
1083 temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1084 strcpy (temp_filename, base);
1085 if (len > 0 && temp_filename[len-1] != '/')
1086 temp_filename[len++] = '/';
1087 strcpy (temp_filename + len, "ccXXXXXX");
1089 mktemp (temp_filename);
1090 temp_filename_length = strlen (temp_filename);
1094 /* Routine to add variables to the environment. We do this to pass
1095 the pathname of the gcc driver, and the directories search to the
1096 collect2 program, which is being run as ld. This way, we can be
1097 sure of executing the right compiler when collect2 wants to build
1098 constructors and destructors. Since the environment variables we
1099 use come from an obstack, we don't have to worry about allocating
1107 #ifndef VMS /* nor about VMS */
1109 extern char **environ;
1110 char **old_environ = environ;
1114 int str_len = strlen (str);
1118 while ((ch = *p++) != '\0' && ch != '=')
1124 /* Search for replacing an existing environment variable, and
1125 count the number of total environment variables. */
1126 for (envp = old_environ; *envp; envp++)
1129 if (!strncmp (str, *envp, name_len))
1136 /* Add a new environment variable */
1137 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1139 bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1144 #endif /* HAVE_PUTENV */
1147 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1150 putenv_from_prefixes (paths, env_var)
1151 struct path_prefix *paths;
1154 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1155 int first_time = TRUE;
1156 struct prefix_list *pprefix;
1158 obstack_grow (&collect_obstack, env_var, strlen (env_var));
1160 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1162 int len = strlen (pprefix->prefix);
1167 obstack_grow (&collect_obstack, ":", 1);
1170 obstack_grow (&collect_obstack, pprefix->prefix, len);
1171 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1174 if (just_machine_suffix && pprefix->require_machine_suffix == 2)
1177 obstack_grow (&collect_obstack, ":", 1);
1180 obstack_grow (&collect_obstack, pprefix->prefix, len);
1181 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1184 if (!pprefix->require_machine_suffix)
1187 obstack_grow (&collect_obstack, ":", 1);
1190 obstack_grow (&collect_obstack, pprefix->prefix, len);
1193 obstack_grow (&collect_obstack, "\0", 1);
1194 putenv (obstack_finish (&collect_obstack));
1198 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1199 access to check permissions.
1200 Return 0 if not found, otherwise return its name, allocated with malloc. */
1203 find_a_file (pprefix, name, mode)
1204 struct path_prefix *pprefix;
1209 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1210 struct prefix_list *pl;
1211 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1214 len += strlen (machine_suffix);
1216 temp = xmalloc (len);
1218 /* Determine the filename to execute (special case for absolute paths). */
1222 if (access (name, mode))
1224 strcpy (temp, name);
1229 for (pl = pprefix->plist; pl; pl = pl->next)
1233 strcpy (temp, pl->prefix);
1234 strcat (temp, machine_suffix);
1235 strcat (temp, name);
1236 if (access (temp, mode) == 0)
1238 if (pl->used_flag_ptr != 0)
1239 *pl->used_flag_ptr = 1;
1242 /* Some systems have a suffix for executable files.
1243 So try appending that. */
1244 if (file_suffix[0] != 0)
1246 strcat (temp, file_suffix);
1247 if (access (temp, mode) == 0)
1249 if (pl->used_flag_ptr != 0)
1250 *pl->used_flag_ptr = 1;
1255 /* Certain prefixes are tried with just the machine type,
1256 not the version. This is used for finding as, ld, etc. */
1257 if (just_machine_suffix && pl->require_machine_suffix == 2)
1259 strcpy (temp, pl->prefix);
1260 strcat (temp, just_machine_suffix);
1261 strcat (temp, name);
1262 if (access (temp, mode) == 0)
1264 if (pl->used_flag_ptr != 0)
1265 *pl->used_flag_ptr = 1;
1268 /* Some systems have a suffix for executable files.
1269 So try appending that. */
1270 if (file_suffix[0] != 0)
1272 strcat (temp, file_suffix);
1273 if (access (temp, mode) == 0)
1275 if (pl->used_flag_ptr != 0)
1276 *pl->used_flag_ptr = 1;
1281 /* Certain prefixes can't be used without the machine suffix
1282 when the machine or version is explicitly specified. */
1283 if (!pl->require_machine_suffix)
1285 strcpy (temp, pl->prefix);
1286 strcat (temp, name);
1287 if (access (temp, mode) == 0)
1289 if (pl->used_flag_ptr != 0)
1290 *pl->used_flag_ptr = 1;
1293 /* Some systems have a suffix for executable files.
1294 So try appending that. */
1295 if (file_suffix[0] != 0)
1297 strcat (temp, file_suffix);
1298 if (access (temp, mode) == 0)
1300 if (pl->used_flag_ptr != 0)
1301 *pl->used_flag_ptr = 1;
1312 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1313 at the start of the list, otherwise it goes at the end.
1315 If WARN is nonzero, we will warn if no file is found
1316 through this prefix. WARN should point to an int
1317 which will be set to 1 if this entry is used.
1319 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1320 the complete value of machine_suffix.
1321 2 means try both machine_suffix and just_machine_suffix. */
1324 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1325 struct path_prefix *pprefix;
1328 int require_machine_suffix;
1331 struct prefix_list *pl, **prev;
1334 if (!first && pprefix->plist)
1336 for (pl = pprefix->plist; pl->next; pl = pl->next)
1341 prev = &pprefix->plist;
1343 /* Keep track of the longest prefix */
1345 len = strlen (prefix);
1346 if (len > pprefix->max_len)
1347 pprefix->max_len = len;
1349 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1350 pl->prefix = save_string (prefix, len);
1351 pl->require_machine_suffix = require_machine_suffix;
1352 pl->used_flag_ptr = warn;
1359 pl->next = (struct prefix_list *) 0;
1363 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1366 unused_prefix_warnings (pprefix)
1367 struct path_prefix *pprefix;
1369 struct prefix_list *pl = pprefix->plist;
1373 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1375 error ("file path prefix `%s' never used",
1377 /* Prevent duplicate warnings. */
1378 *pl->used_flag_ptr = 1;
1384 /* Get rid of all prefixes built up so far in *PLISTP. */
1387 free_path_prefix (pprefix)
1388 struct path_prefix *pprefix;
1390 struct prefix_list *pl = pprefix->plist;
1391 struct prefix_list *temp;
1397 free (temp->prefix);
1398 free ((char *) temp);
1400 pprefix->plist = (struct prefix_list *) 0;
1403 /* stdin file number. */
1404 #define STDIN_FILE_NO 0
1406 /* stdout file number. */
1407 #define STDOUT_FILE_NO 1
1409 /* value of `pipe': port index for reading. */
1412 /* value of `pipe': port index for writing. */
1413 #define WRITE_PORT 1
1415 /* Pipe waiting from last process, to be used as input for the next one.
1416 Value is STDIN_FILE_NO if no pipe is waiting
1417 (i.e. the next command is the first of a group). */
1419 static int last_pipe_input;
1421 /* Fork one piped subcommand. FUNC is the system call to use
1422 (either execv or execvp). ARGV is the arg vector to use.
1423 NOT_LAST is nonzero if this is not the last subcommand
1424 (i.e. its output should be piped to the next one.) */
1429 /* Declare these to avoid compilation error. They won't be called. */
1430 int execv(const char *a, const char **b){}
1431 int execvp(const char *a, const char **b){}
1434 pexecute (search_flag, program, argv, not_last)
1444 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6);
1445 sprintf (scmd, "%s @%s.gp", program, temp_filename);
1446 argfile = fopen (scmd+strlen (program) + 2, "w");
1448 pfatal_with_name (scmd + strlen (program) + 2);
1450 for (i=1; argv[i]; i++)
1453 for (cp = argv[i]; *cp; cp++)
1455 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1456 fputc ('\\', argfile);
1457 fputc (*cp, argfile);
1459 fputc ('\n', argfile);
1465 remove (scmd + strlen (program) + 2);
1469 #else /* not __MSDOS__ */
1472 pexecute (search_flag, program, argv, not_last)
1478 int (*func)() = (search_flag ? execv : execvp);
1481 int input_desc = last_pipe_input;
1482 int output_desc = STDOUT_FILE_NO;
1483 int retries, sleep_interval;
1485 /* If this isn't the last process, make a pipe for its output,
1486 and record it as waiting to be the input to the next process. */
1490 if (pipe (pdes) < 0)
1491 pfatal_with_name ("pipe");
1492 output_desc = pdes[WRITE_PORT];
1493 last_pipe_input = pdes[READ_PORT];
1496 last_pipe_input = STDIN_FILE_NO;
1498 /* Fork a subprocess; wait and retry if it fails. */
1500 for (retries = 0; retries < 4; retries++)
1505 sleep (sleep_interval);
1506 sleep_interval *= 2;
1513 pfatal_with_name ("fork");
1515 pfatal_with_name ("vfork");
1521 /* Move the input and output pipes into place, if nec. */
1522 if (input_desc != STDIN_FILE_NO)
1524 close (STDIN_FILE_NO);
1528 if (output_desc != STDOUT_FILE_NO)
1530 close (STDOUT_FILE_NO);
1532 close (output_desc);
1535 /* Close the parent's descs that aren't wanted here. */
1536 if (last_pipe_input != STDIN_FILE_NO)
1537 close (last_pipe_input);
1539 /* Exec the program. */
1540 (*func) (program, argv);
1541 perror_exec (program);
1547 /* In the parent, after forking.
1548 Close the descriptors that we made for this child. */
1549 if (input_desc != STDIN_FILE_NO)
1551 if (output_desc != STDOUT_FILE_NO)
1552 close (output_desc);
1554 /* Return child's process number. */
1559 #endif /* not __MSDOS__ */
1563 pexecute (search_flag, program, argv, not_last)
1569 return (search_flag ? spawnv : spawnvp) (1, program, argv);
1571 #endif /* not OS2 */
1573 /* Execute the command specified by the arguments on the current line of spec.
1574 When using pipes, this includes several piped-together commands
1575 with `|' between them.
1577 Return 0 if successful, -1 if failed. */
1583 int n_commands; /* # of command. */
1587 char *prog; /* program name. */
1588 char **argv; /* vector of args. */
1589 int pid; /* pid of process for this command. */
1592 struct command *commands; /* each command buffer with above info. */
1594 /* Count # of piped commands. */
1595 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1596 if (strcmp (argbuf[i], "|") == 0)
1599 /* Get storage for each command. */
1601 = (struct command *) alloca (n_commands * sizeof (struct command));
1603 /* Split argbuf into its separate piped processes,
1604 and record info about each one.
1605 Also search for the programs that are to be run. */
1607 commands[0].prog = argbuf[0]; /* first command. */
1608 commands[0].argv = &argbuf[0];
1609 string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1611 commands[0].argv[0] = string;
1613 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1614 if (strcmp (argbuf[i], "|") == 0)
1615 { /* each command. */
1617 fatal ("-pipe not supported under MS-DOS");
1619 argbuf[i] = 0; /* termination of command args. */
1620 commands[n_commands].prog = argbuf[i + 1];
1621 commands[n_commands].argv = &argbuf[i + 1];
1622 string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1624 commands[n_commands].argv[0] = string;
1628 argbuf[argbuf_index] = 0;
1630 /* If -v, print what we are about to do, and maybe query. */
1634 /* Print each piped command as a separate line. */
1635 for (i = 0; i < n_commands ; i++)
1639 for (j = commands[i].argv; *j; j++)
1640 fprintf (stderr, " %s", *j);
1642 /* Print a pipe symbol after all but the last command. */
1643 if (i + 1 != n_commands)
1644 fprintf (stderr, " |");
1645 fprintf (stderr, "\n");
1649 fprintf (stderr, "\nGo ahead? (y or n) ");
1653 while (getchar () != '\n') ;
1654 if (i != 'y' && i != 'Y')
1659 /* Run each piped subprocess. */
1661 last_pipe_input = STDIN_FILE_NO;
1662 for (i = 0; i < n_commands; i++)
1664 char *string = commands[i].argv[0];
1666 commands[i].pid = pexecute (string != commands[i].prog,
1667 string, commands[i].argv,
1668 i + 1 < n_commands);
1670 if (string != commands[i].prog)
1676 /* Wait for all the subprocesses to finish.
1677 We don't care what order they finish in;
1678 we know that N_COMMANDS waits will get them all. */
1683 for (i = 0; i < n_commands; i++)
1690 status = pid = commands[i].pid;
1692 pid = wait (&status);
1700 for (j = 0; j < n_commands; j++)
1701 if (commands[j].pid == pid)
1702 prog = commands[j].prog;
1704 if ((status & 0x7F) != 0)
1705 fatal ("Internal compiler error: program %s got fatal signal %d",
1706 prog, (status & 0x7F));
1707 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1715 /* Find all the switches given to us
1716 and make a vector describing them.
1717 The elements of the vector are strings, one per switch given.
1718 If a switch uses following arguments, then the `part1' field
1719 is the switch itself and the `args' field
1720 is a null-terminated vector containing the following arguments.
1721 The `valid' field is nonzero if any spec has looked at this switch;
1722 if it remains zero at the end of the run, it must be meaningless. */
1731 static struct switchstr *switches;
1733 static int n_switches;
1741 /* Also a vector of input files specified. */
1743 static struct infile *infiles;
1745 static int n_infiles;
1747 /* And a vector of corresponding output files is made up later. */
1749 static char **outfiles;
1751 /* Create the vector `switches' and its contents.
1752 Store its length in `n_switches'. */
1755 process_command (argc, argv)
1761 char *spec_lang = 0;
1762 int last_language_n_infiles;
1764 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
1768 spec_version = version_string;
1770 /* Set up the default search paths. */
1772 if (gcc_exec_prefix)
1774 add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1775 add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
1778 /* COMPILER_PATH and LIBRARY_PATH have values
1779 that are lists of directory names with colons. */
1781 temp = getenv ("COMPILER_PATH");
1784 char *startp, *endp;
1785 char *nstore = (char *) alloca (strlen (temp) + 3);
1787 startp = endp = temp;
1790 if (*endp == PATH_SEPARATOR || *endp == 0)
1792 strncpy (nstore, startp, endp-startp);
1795 strcpy (nstore, "./");
1797 else if (endp[-1] != '/')
1799 nstore[endp-startp] = '/';
1800 nstore[endp-startp+1] = 0;
1803 nstore[endp-startp] = 0;
1804 add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
1807 endp = startp = endp + 1;
1814 temp = getenv ("LIBRARY_PATH");
1817 char *startp, *endp;
1818 char *nstore = (char *) alloca (strlen (temp) + 3);
1820 startp = endp = temp;
1823 if (*endp == PATH_SEPARATOR || *endp == 0)
1825 strncpy (nstore, startp, endp-startp);
1828 strcpy (nstore, "./");
1830 else if (endp[-1] != '/')
1832 nstore[endp-startp] = '/';
1833 nstore[endp-startp+1] = 0;
1836 nstore[endp-startp] = 0;
1837 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1838 /* Make separate list of dirs that came from LIBRARY_PATH. */
1839 add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1842 endp = startp = endp + 1;
1849 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1850 temp = getenv ("LPATH");
1853 char *startp, *endp;
1854 char *nstore = (char *) alloca (strlen (temp) + 3);
1856 startp = endp = temp;
1859 if (*endp == PATH_SEPARATOR || *endp == 0)
1861 strncpy (nstore, startp, endp-startp);
1864 strcpy (nstore, "./");
1866 else if (endp[-1] != '/')
1868 nstore[endp-startp] = '/';
1869 nstore[endp-startp+1] = 0;
1872 nstore[endp-startp] = 0;
1873 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
1874 /* Make separate list of dirs that came from LIBRARY_PATH. */
1875 add_prefix (&library_prefix, nstore, 0, 0, NULL_PTR);
1878 endp = startp = endp + 1;
1885 /* Scan argv twice. Here, the first time, just count how many switches
1886 there will be in their vector, and how many input files in theirs.
1887 Here we also parse the switches that cc itself uses (e.g. -v). */
1889 for (i = 1; i < argc; i++)
1891 if (! strcmp (argv[i], "-dumpspecs"))
1893 printf ("*asm:\n%s\n\n", asm_spec);
1894 printf ("*asm_final:\n%s\n\n", asm_final_spec);
1895 printf ("*cpp:\n%s\n\n", cpp_spec);
1896 printf ("*cc1:\n%s\n\n", cc1_spec);
1897 printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1898 printf ("*endfile:\n%s\n\n", endfile_spec);
1899 printf ("*link:\n%s\n\n", link_spec);
1900 printf ("*lib:\n%s\n\n", lib_spec);
1901 printf ("*startfile:\n%s\n\n", startfile_spec);
1902 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1903 printf ("*signed_char:\n%s\n\n", signed_char_spec);
1904 printf ("*predefines:\n%s\n\n", cpp_predefines);
1905 printf ("*cross_compile:\n%d\n\n", cross_compile);
1909 else if (! strcmp (argv[i], "-dumpversion"))
1911 printf ("%s\n", version_string);
1914 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
1916 print_libgcc_file_name = 1;
1918 else if (! strcmp (argv[i], "-Xlinker"))
1920 /* Pass the argument of this option to the linker when we link. */
1923 fatal ("argument to `-Xlinker' is missing");
1926 if (!linker_options)
1928 = (char **) xmalloc (n_linker_options * sizeof (char **));
1931 = (char **) xrealloc (linker_options,
1932 n_linker_options * sizeof (char **));
1934 linker_options[n_linker_options - 1] = argv[++i];
1936 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1938 register char *p = &argv[i][1];
1939 register int c = *p;
1944 if (p[1] == 0 && i + 1 == argc)
1945 fatal ("argument to `-b' is missing");
1947 spec_machine = argv[++i];
1949 spec_machine = p + 1;
1954 int *temp = (int *) xmalloc (sizeof (int));
1956 if (p[1] == 0 && i + 1 == argc)
1957 fatal ("argument to `-B' is missing");
1962 add_prefix (&exec_prefix, value, 1, 0, temp);
1963 add_prefix (&startfile_prefix, value, 1, 0, temp);
1967 case 'v': /* Print our subcommands and print versions. */
1973 if (p[1] == 0 && i + 1 == argc)
1974 fatal ("argument to `-V' is missing");
1976 spec_version = argv[++i];
1978 spec_version = p + 1;
1982 if (!strcmp (p, "save-temps"))
1984 save_temps_flag = 1;
1991 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1992 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1993 else if (WORD_SWITCH_TAKES_ARG (p))
1994 i += WORD_SWITCH_TAKES_ARG (p);
2001 /* Set up the search paths before we go looking for config files. */
2003 /* These come before the md prefixes so that we will find gcc's subcommands
2004 (such as cpp) rather than those of the host system. */
2005 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2006 as well as trying the machine and the version. */
2007 add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
2008 add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
2010 add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
2011 add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
2013 /* More prefixes are enabled in main, after we read the specs file
2014 and determine whether this is cross-compilation or not. */
2017 /* Then create the space for the vectors and scan again. */
2019 switches = ((struct switchstr *)
2020 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2021 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2024 last_language_n_infiles = -1;
2026 /* This, time, copy the text of each switch and store a pointer
2027 to the copy in the vector of switches.
2028 Store all the infiles in their vector. */
2030 for (i = 1; i < argc; i++)
2032 if (!strcmp (argv[i], "-Xlinker"))
2034 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2036 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2038 register char *p = &argv[i][1];
2039 register int c = *p;
2041 if (c == 'B' || c == 'b' || c == 'V')
2043 /* Skip a separate arg, if any. */
2050 if (p[1] == 0 && i + 1 == argc)
2051 fatal ("argument to `-x' is missing");
2053 spec_lang = argv[++i];
2056 if (! strcmp (spec_lang, "none"))
2057 /* Suppress the warning if -xnone comes after the last input file,
2058 because alternate command interfaces like g++ might find it
2059 useful to place -xnone after each input file. */
2062 last_language_n_infiles = n_infiles;
2065 switches[n_switches].part1 = p;
2066 /* Deal with option arguments in separate argv elements. */
2067 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2068 || WORD_SWITCH_TAKES_ARG (p)) {
2070 int n_args = WORD_SWITCH_TAKES_ARG (p);
2073 /* Count only the option arguments in separate argv elements. */
2074 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2076 switches[n_switches].args
2077 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2079 switches[n_switches].args[j++] = argv[++i];
2080 /* Null-terminate the vector. */
2081 switches[n_switches].args[j] = 0;
2082 } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
2083 /* On some systems, ld cannot handle -o or -L without space.
2084 So split the -o or -L from its argument. */
2085 switches[n_switches].part1 = (c == 'o' ? "o" : "L");
2086 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2087 switches[n_switches].args[0] = xmalloc (strlen (p));
2088 strcpy (switches[n_switches].args[0], &p[1]);
2089 switches[n_switches].args[1] = 0;
2091 switches[n_switches].args = 0;
2092 switches[n_switches].valid = 0;
2093 /* This is always valid, since gcc.c itself understands it. */
2094 if (!strcmp (p, "save-temps"))
2095 switches[n_switches].valid = 1;
2100 infiles[n_infiles].language = spec_lang;
2101 infiles[n_infiles++].name = argv[i];
2105 if (n_infiles == last_language_n_infiles)
2106 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2108 switches[n_switches].part1 = 0;
2109 infiles[n_infiles].name = 0;
2111 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
2112 if (gcc_exec_prefix)
2114 temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
2115 + strlen (spec_machine) + 3);
2116 strcpy (temp, gcc_exec_prefix);
2117 strcat (temp, spec_machine);
2119 strcat (temp, spec_version);
2121 gcc_exec_prefix = temp;
2125 /* Process a spec string, accumulating and running commands. */
2127 /* These variables describe the input file name.
2128 input_file_number is the index on outfiles of this file,
2129 so that the output file name can be stored for later use by %o.
2130 input_basename is the start of the part of the input file
2131 sans all directory names, and basename_length is the number
2132 of characters starting there excluding the suffix .c or whatever. */
2134 static char *input_filename;
2135 static int input_file_number;
2136 static int input_filename_length;
2137 static int basename_length;
2138 static char *input_basename;
2139 static char *input_suffix;
2141 /* These are variables used within do_spec and do_spec_1. */
2143 /* Nonzero if an arg has been started and not yet terminated
2144 (with space, tab or newline). */
2145 static int arg_going;
2147 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2148 is a temporary file name. */
2149 static int delete_this_arg;
2151 /* Nonzero means %w has been seen; the next arg to be terminated
2152 is the output file name of this compilation. */
2153 static int this_is_output_file;
2155 /* Nonzero means %s has been seen; the next arg to be terminated
2156 is the name of a library file and we should try the standard
2157 search dirs for it. */
2158 static int this_is_library_file;
2160 /* Process the spec SPEC and run the commands specified therein.
2161 Returns 0 if the spec is successfully processed; -1 if failed. */
2171 delete_this_arg = 0;
2172 this_is_output_file = 0;
2173 this_is_library_file = 0;
2175 value = do_spec_1 (spec, 0, NULL_PTR);
2177 /* Force out any unfinished command.
2178 If -pipe, this forces out the last command if it ended in `|'. */
2181 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2184 if (argbuf_index > 0)
2191 /* Process the sub-spec SPEC as a portion of a larger spec.
2192 This is like processing a whole spec except that we do
2193 not initialize at the beginning and we do not supply a
2194 newline by default at the end.
2195 INSWITCH nonzero means don't process %-sequences in SPEC;
2196 in this case, % is treated as an ordinary character.
2197 This is used while substituting switches.
2198 INSWITCH nonzero also causes SPC not to terminate an argument.
2200 Value is zero unless a line was finished
2201 and the command on that line reported an error. */
2204 do_spec_1 (spec, inswitch, soft_matched_part)
2207 char *soft_matched_part;
2209 register char *p = spec;
2215 /* If substituting a switch, treat all chars like letters.
2216 Otherwise, NL, SPC, TAB and % are special. */
2217 switch (inswitch ? 'a' : c)
2220 /* End of line: finish any pending argument,
2221 then run the pending command if one has been started. */
2224 obstack_1grow (&obstack, 0);
2225 string = obstack_finish (&obstack);
2226 if (this_is_library_file)
2227 string = find_file (string);
2228 store_arg (string, delete_this_arg, this_is_output_file);
2229 if (this_is_output_file)
2230 outfiles[input_file_number] = string;
2234 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2237 for (i = 0; i < n_switches; i++)
2238 if (!strcmp (switches[i].part1, "pipe"))
2241 /* A `|' before the newline means use a pipe here,
2242 but only if -pipe was specified.
2243 Otherwise, execute now and don't pass the `|' as an arg. */
2246 switches[i].valid = 1;
2253 if (argbuf_index > 0)
2255 int value = execute ();
2259 /* Reinitialize for a new command, and for a new argument. */
2262 delete_this_arg = 0;
2263 this_is_output_file = 0;
2264 this_is_library_file = 0;
2268 /* End any pending argument. */
2271 obstack_1grow (&obstack, 0);
2272 string = obstack_finish (&obstack);
2273 if (this_is_library_file)
2274 string = find_file (string);
2275 store_arg (string, delete_this_arg, this_is_output_file);
2276 if (this_is_output_file)
2277 outfiles[input_file_number] = string;
2281 obstack_1grow (&obstack, c);
2287 /* Space or tab ends an argument if one is pending. */
2290 obstack_1grow (&obstack, 0);
2291 string = obstack_finish (&obstack);
2292 if (this_is_library_file)
2293 string = find_file (string);
2294 store_arg (string, delete_this_arg, this_is_output_file);
2295 if (this_is_output_file)
2296 outfiles[input_file_number] = string;
2298 /* Reinitialize for a new argument. */
2300 delete_this_arg = 0;
2301 this_is_output_file = 0;
2302 this_is_library_file = 0;
2309 fatal ("Invalid specification! Bug in cc.");
2312 obstack_grow (&obstack, input_basename, basename_length);
2317 delete_this_arg = 2;
2320 /* Dump out the directories specified with LIBRARY_PATH,
2321 followed by the absolute directories
2322 that we search for startfiles. */
2324 for (i = 0; i < 2; i++)
2326 struct prefix_list *pl
2327 = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2329 char *buffer = (char *) xmalloc (bufsize);
2332 for (; pl; pl = pl->next)
2334 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2335 /* Used on systems which record the specified -L dirs
2336 and use them to search for dynamic linking. */
2337 /* Relative directories always come from -B,
2338 and it is better not to use them for searching
2339 at run time. In particular, stage1 loses */
2340 if (pl->prefix[0] != '/')
2345 if (is_linker_dir (pl->prefix, machine_suffix))
2347 do_spec_1 ("-L", 0, NULL_PTR);
2348 #ifdef SPACE_AFTER_L_OPTION
2349 do_spec_1 (" ", 0, NULL_PTR);
2351 do_spec_1 (pl->prefix, 1, NULL_PTR);
2352 /* Remove slash from machine_suffix. */
2353 if (strlen (machine_suffix) >= bufsize)
2354 bufsize = strlen (machine_suffix) * 2 + 1;
2355 buffer = (char *) xrealloc (buffer, bufsize);
2356 strcpy (buffer, machine_suffix);
2357 idx = strlen (buffer);
2358 if (buffer[idx - 1] == '/')
2359 buffer[idx - 1] = 0;
2360 do_spec_1 (buffer, 1, NULL_PTR);
2361 /* Make this a separate argument. */
2362 do_spec_1 (" ", 0, NULL_PTR);
2365 if (!pl->require_machine_suffix)
2367 if (is_linker_dir (pl->prefix, ""))
2369 do_spec_1 ("-L", 0, NULL_PTR);
2370 #ifdef SPACE_AFTER_L_OPTION
2371 do_spec_1 (" ", 0, NULL_PTR);
2373 /* Remove slash from pl->prefix. */
2374 if (strlen (pl->prefix) >= bufsize)
2375 bufsize = strlen (pl->prefix) * 2 + 1;
2376 buffer = (char *) xrealloc (buffer, bufsize);
2377 strcpy (buffer, pl->prefix);
2378 idx = strlen (buffer);
2379 if (buffer[idx - 1] == '/')
2380 buffer[idx - 1] = 0;
2381 do_spec_1 (buffer, 1, NULL_PTR);
2382 /* Make this a separate argument. */
2383 do_spec_1 (" ", 0, NULL_PTR);
2392 /* {...:%efoo} means report an error with `foo' as error message
2393 and don't execute any more commands for this file. */
2397 while (*p != 0 && *p != '\n') p++;
2398 buf = (char *) alloca (p - q + 1);
2399 strncpy (buf, q, p - q);
2407 if (save_temps_flag)
2408 obstack_grow (&obstack, input_basename, basename_length);
2411 obstack_grow (&obstack, temp_filename, temp_filename_length);
2412 delete_this_arg = 1;
2418 obstack_grow (&obstack, input_filename, input_filename_length);
2423 if (gcc_exec_prefix)
2425 do_spec_1 ("-iprefix", 1, NULL_PTR);
2426 /* Make this a separate argument. */
2427 do_spec_1 (" ", 0, NULL_PTR);
2428 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
2429 do_spec_1 (" ", 0, NULL_PTR);
2436 for (f = 0; f < n_infiles; f++)
2437 store_arg (outfiles[f], 0, 0);
2442 this_is_library_file = 1;
2446 this_is_output_file = 1;
2451 int index = argbuf_index;
2452 /* Handle the {...} following the %W. */
2455 p = handle_braces (p + 1);
2458 /* If any args were output, mark the last one for deletion
2460 if (argbuf_index != index)
2461 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2465 /* %x{OPTION} records OPTION for %X to output. */
2471 /* Skip past the option value and make a copy. */
2476 string = save_string (p1 + 1, p - p1 - 2);
2478 /* See if we already recorded this option. */
2479 for (i = 0; i < n_linker_options; i++)
2480 if (! strcmp (string, linker_options[i]))
2486 /* This option is new; add it. */
2488 if (!linker_options)
2490 = (char **) xmalloc (n_linker_options * sizeof (char **));
2493 = (char **) xrealloc (linker_options,
2494 n_linker_options * sizeof (char **));
2496 linker_options[n_linker_options - 1] = string;
2500 /* Dump out the options accumulated previously using %x. */
2502 for (i = 0; i < n_linker_options; i++)
2504 do_spec_1 (linker_options[i], 1, NULL_PTR);
2505 /* Make each accumulated option a separate argument. */
2506 do_spec_1 (" ", 0, NULL_PTR);
2510 /* Here are digits and numbers that just process
2511 a certain constant string as a spec. */
2514 do_spec_1 (cc1_spec, 0, NULL_PTR);
2518 do_spec_1 (cc1plus_spec, 0, NULL_PTR);
2522 do_spec_1 (asm_spec, 0, NULL_PTR);
2526 do_spec_1 (asm_final_spec, 0, NULL_PTR);
2530 do_spec_1 (signed_char_spec, 0, NULL_PTR);
2534 do_spec_1 (cpp_spec, 0, NULL_PTR);
2538 do_spec_1 (endfile_spec, 0, NULL_PTR);
2542 do_spec_1 (link_spec, 0, NULL_PTR);
2546 do_spec_1 (lib_spec, 0, NULL_PTR);
2551 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2555 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2559 if (! strncmp (y, "-D", 2))
2560 /* Copy the whole option. */
2561 while (*y && *y != ' ' && *y != '\t')
2563 else if (*y == ' ' || *y == '\t')
2564 /* Copy whitespace to the result. */
2566 /* Don't copy other options. */
2573 do_spec_1 (buf, 0, NULL_PTR);
2579 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2583 /* Copy all of CPP_PREDEFINES into BUF,
2584 but put __ after every -D and at the end of each arg. */
2588 if (! strncmp (y, "-D", 2))
2595 if (strncmp (y, "__", 2))
2597 /* Stick __ at front of macro name. */
2600 /* Arrange to stick __ at the end as well. */
2604 /* Copy the macro name. */
2605 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2614 /* Copy the value given, if any. */
2615 while (*y && *y != ' ' && *y != '\t')
2618 else if (*y == ' ' || *y == '\t')
2619 /* Copy whitespace to the result. */
2621 /* Don't copy -A options */
2627 /* Copy all of CPP_PREDEFINES into BUF,
2628 but put __ after every -D. */
2632 if (! strncmp (y, "-D", 2))
2637 if (strncmp (y, "__", 2))
2639 /* Stick __ at front of macro name. */
2644 /* Copy the macro name. */
2645 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2648 /* Copy the value given, if any. */
2649 while (*y && *y != ' ' && *y != '\t')
2652 else if (*y == ' ' || *y == '\t')
2653 /* Copy whitespace to the result. */
2655 /* Don't copy -A options */
2661 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2665 if (! strncmp (y, "-A", 2))
2666 /* Copy the whole option. */
2667 while (*y && *y != ' ' && *y != '\t')
2669 else if (*y == ' ' || *y == '\t')
2670 /* Copy whitespace to the result. */
2672 /* Don't copy other options. */
2679 do_spec_1 (buf, 0, NULL_PTR);
2684 do_spec_1 (startfile_spec, 0, NULL_PTR);
2687 /* Here we define characters other than letters and digits. */
2690 p = handle_braces (p);
2696 obstack_1grow (&obstack, '%');
2700 do_spec_1 (soft_matched_part, 1, NULL_PTR);
2701 do_spec_1 (" ", 0, NULL_PTR);
2704 /* Process a string found as the value of a spec given by name.
2705 This feature allows individual machine descriptions
2706 to add and use their own specs.
2707 %[...] modifies -D options the way %P does;
2708 %(...) uses the spec unmodified. */
2713 struct spec_list *sl;
2716 /* The string after the S/P is the name of a spec that is to be
2718 while (*p && *p != ')' && *p != ']')
2721 /* See if it's in the list */
2722 for (len = p - name, sl = specs; sl; sl = sl->next)
2723 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2732 do_spec_1 (name, 0, NULL_PTR);
2735 char *x = (char *) alloca (strlen (name) * 2 + 1);
2739 /* Copy all of NAME into BUF, but put __ after
2740 every -D and at the end of each arg, */
2743 if (! strncmp (y, "-D", 2))
2751 else if (*y == ' ' || *y == 0)
2765 do_spec_1 (buf, 0, NULL_PTR);
2769 /* Discard the closing paren or bracket. */
2781 /* Backslash: treat next character as ordinary. */
2786 /* Ordinary character: put it into the current argument. */
2787 obstack_1grow (&obstack, c);
2791 return 0; /* End of string */
2794 /* Return 0 if we call do_spec_1 and that returns -1. */
2807 /* A `|' after the open-brace means,
2808 if the test fails, output a single minus sign rather than nothing.
2809 This is used in %{|!pipe:...}. */
2813 /* A `!' after the open-brace negates the condition:
2814 succeed if the specified switch is not present. */
2818 /* A `.' after the open-brace means test against the current suffix. */
2828 while (*p != ':' && *p != '}') p++;
2831 register int count = 1;
2849 int found = (input_suffix != 0
2850 && strlen (input_suffix) == p - filter
2851 && strncmp (input_suffix, filter, p - filter) == 0);
2857 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2862 else if (p[-1] == '*' && p[0] == '}')
2864 /* Substitute all matching switches as separate args. */
2867 for (i = 0; i < n_switches; i++)
2868 if (!strncmp (switches[i].part1, filter, p - filter))
2873 /* Test for presence of the specified switch. */
2877 /* If name specified ends in *, as in {x*:...},
2878 check for %* and handle that case. */
2879 if (p[-1] == '*' && !negate)
2884 /* First see whether we have %*. */
2888 if (*r == '%' && r[1] == '*')
2892 /* If we do, handle that case. */
2895 /* Substitute all matching switches as separate args.
2896 But do this by substituting for %*
2897 in the text that follows the colon. */
2899 unsigned hard_match_len = p - filter - 1;
2900 char *string = save_string (p + 1, q - p - 2);
2902 for (i = 0; i < n_switches; i++)
2903 if (!strncmp (switches[i].part1, filter, hard_match_len))
2905 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
2906 /* Pass any arguments this switch has. */
2914 /* If name specified ends in *, as in {x*:...},
2915 check for presence of any switch name starting with x. */
2918 for (i = 0; i < n_switches; i++)
2920 unsigned hard_match_len = p - filter - 1;
2922 if (!strncmp (switches[i].part1, filter, hard_match_len))
2924 switches[i].valid = 1;
2929 /* Otherwise, check for presence of exact name specified. */
2932 for (i = 0; i < n_switches; i++)
2934 if (!strncmp (switches[i].part1, filter, p - filter)
2935 && switches[i].part1[p - filter] == 0)
2937 switches[i].valid = 1;
2944 /* If it is as desired (present for %{s...}, absent for %{-s...})
2945 then substitute either the switch or the specified
2946 conditional text. */
2947 if (present != negate)
2955 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
2961 /* Here if a %{|...} conditional fails: output a minus sign,
2962 which means "standard output" or "standard input". */
2963 do_spec_1 ("-", 0, NULL_PTR);
2970 /* Pass a switch to the current accumulating command
2971 in the same form that we received it.
2972 SWITCHNUM identifies the switch; it is an index into
2973 the vector of switches gcc received, which is `switches'.
2974 This cannot fail since it never finishes a command line.
2976 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2979 give_switch (switchnum, omit_first_word)
2981 int omit_first_word;
2983 if (!omit_first_word)
2985 do_spec_1 ("-", 0, NULL_PTR);
2986 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
2988 do_spec_1 (" ", 0, NULL_PTR);
2989 if (switches[switchnum].args != 0)
2992 for (p = switches[switchnum].args; *p; p++)
2994 do_spec_1 (*p, 1, NULL_PTR);
2995 do_spec_1 (" ", 0, NULL_PTR);
2998 switches[switchnum].valid = 1;
3001 /* Search for a file named NAME trying various prefixes including the
3002 user's -B prefix and some standard ones.
3003 Return the absolute file name found. If nothing is found, return NAME. */
3011 newname = find_a_file (&startfile_prefix, name, R_OK);
3012 return newname ? newname : name;
3015 /* Determine whether a -L option is relevant. Not required for certain
3016 fixed names and for directories that don't exist. */
3019 is_linker_dir (path1, path2)
3023 int len1 = strlen (path1);
3024 int len2 = strlen (path2);
3025 char *path = (char *) alloca (3 + len1 + len2);
3029 /* Construct the path from the two parts. Ensure the string ends with "/.".
3030 The resulting path will be a directory even if the given path is a
3032 bcopy (path1, path, len1);
3033 bcopy (path2, path + len1, len2);
3034 cp = path + len1 + len2;
3040 /* Exclude directories that the linker is known to search. */
3041 if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
3042 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
3045 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
3048 /* On fatal signals, delete all the temporary files. */
3051 fatal_error (signum)
3054 signal (signum, SIG_DFL);
3055 delete_failure_queue ();
3056 delete_temp_files ();
3057 /* Get the same signal again, this time not handled,
3058 so its normal effect occurs. */
3059 kill (getpid (), signum);
3069 int error_count = 0;
3070 int linker_was_run = 0;
3071 char *explicit_link_files;
3074 programname = argv[0];
3076 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3077 signal (SIGINT, fatal_error);
3078 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3079 signal (SIGHUP, fatal_error);
3080 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3081 signal (SIGTERM, fatal_error);
3083 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3084 signal (SIGPIPE, fatal_error);
3088 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3090 obstack_init (&obstack);
3092 /* Set up to remember the pathname of gcc and any options
3093 needed for collect. */
3094 obstack_init (&collect_obstack);
3095 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3096 obstack_grow (&collect_obstack, programname, strlen (programname)+1);
3097 putenv (obstack_finish (&collect_obstack));
3099 /* Choose directory for temp files. */
3101 choose_temp_base ();
3103 /* Make a table of what switches there are (switches, n_switches).
3104 Make a table of specified input files (infiles, n_infiles).
3105 Decode switches that are handled locally. */
3107 process_command (argc, argv);
3109 /* Initialize the vector of specs to just the default.
3110 This means one element containing 0s, as a terminator. */
3112 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3113 bcopy (default_compilers, compilers, sizeof default_compilers);
3114 n_compilers = n_default_compilers;
3116 /* Read specs from a file if there is one. */
3118 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
3119 just_machine_suffix = concat (spec_machine, "/", "");
3121 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3122 /* Read the specs file unless it is a default one. */
3123 if (specs_file != 0 && strcmp (specs_file, "specs"))
3124 read_specs (specs_file);
3126 /* If not cross-compiling, look for startfiles in the standard places. */
3127 /* The fact that these are done here, after reading the specs file,
3128 means that it cannot be found in these directories.
3129 But that's okay. It should never be there anyway. */
3132 #ifdef MD_EXEC_PREFIX
3133 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3134 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3137 #ifdef MD_STARTFILE_PREFIX
3138 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
3141 #ifdef MD_STARTFILE_PREFIX_1
3142 add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
3145 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3147 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3149 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3151 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3152 add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
3156 /* Now we have the specs.
3157 Set the `valid' bits for switches that match anything in any spec. */
3159 validate_all_switches ();
3161 /* Warn about any switches that no pass was interested in. */
3163 for (i = 0; i < n_switches; i++)
3164 if (! switches[i].valid)
3165 error ("unrecognized option `-%s'", switches[i].part1);
3167 if (print_libgcc_file_name)
3169 printf ("%s\n", find_file ("libgcc.a"));
3173 /* Obey some of the options. */
3177 fprintf (stderr, "gcc version %s\n", version_string);
3183 fatal ("No input files specified.");
3185 /* Make a place to record the compiler output file names
3186 that correspond to the input files. */
3188 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3189 bzero (outfiles, n_infiles * sizeof (char *));
3191 /* Record which files were specified explicitly as link input. */
3193 explicit_link_files = xmalloc (n_infiles);
3194 bzero (explicit_link_files, n_infiles);
3196 for (i = 0; i < n_infiles; i++)
3198 register struct compiler *cp = 0;
3199 int this_file_error = 0;
3201 /* Tell do_spec what to substitute for %i. */
3203 input_filename = infiles[i].name;
3204 input_filename_length = strlen (input_filename);
3205 input_file_number = i;
3207 /* Use the same thing in %o, unless cp->spec says otherwise. */
3209 outfiles[i] = input_filename;
3211 /* Figure out which compiler from the file's suffix. */
3213 cp = lookup_compiler (infiles[i].name, input_filename_length,
3214 infiles[i].language);
3218 /* Ok, we found an applicable compiler. Run its spec. */
3219 /* First say how much of input_filename to substitute for %b */
3222 input_basename = input_filename;
3223 for (p = input_filename; *p; p++)
3225 input_basename = p + 1;
3227 /* Find a suffix starting with the last period,
3228 and set basename_length to exclude that suffix. */
3229 basename_length = strlen (input_basename);
3230 p = input_basename + basename_length;
3231 while (p != input_basename && *p != '.') --p;
3232 if (*p == '.' && p != input_basename)
3234 basename_length = p - input_basename;
3235 input_suffix = p + 1;
3240 value = do_spec (cp->spec);
3242 this_file_error = 1;
3245 /* If this file's name does not contain a recognized suffix,
3246 record it as explicit linker input. */
3249 explicit_link_files[i] = 1;
3251 /* Clear the delete-on-failure queue, deleting the files in it
3252 if this compilation failed. */
3254 if (this_file_error)
3256 delete_failure_queue ();
3259 /* If this compilation succeeded, don't delete those files later. */
3260 clear_failure_queue ();
3263 /* Run ld to link all the compiler output files. */
3265 if (error_count == 0)
3267 int tmp = execution_count;
3271 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3273 putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3274 putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3276 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3278 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3279 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3282 for (i = 0; i < n_switches; i++)
3286 obstack_grow (&collect_obstack, " ", 1);
3289 obstack_grow (&collect_obstack, "-", 1);
3290 obstack_grow (&collect_obstack, switches[i].part1,
3291 strlen (switches[i].part1));
3293 for (args = switches[i].args; args && *args; args++)
3295 obstack_grow (&collect_obstack, " ", 1);
3296 obstack_grow (&collect_obstack, *args, strlen (*args));
3299 obstack_grow (&collect_obstack, "\0", 1);
3300 putenv (obstack_finish (&collect_obstack));
3302 value = do_spec (link_command_spec);
3305 linker_was_run = (tmp != execution_count);
3308 /* Warn if a -B option was specified but the prefix was never used. */
3309 unused_prefix_warnings (&exec_prefix);
3310 unused_prefix_warnings (&startfile_prefix);
3312 /* If options said don't run linker,
3313 complain about input files to be given to the linker. */
3315 if (! linker_was_run && error_count == 0)
3316 for (i = 0; i < n_infiles; i++)
3317 if (explicit_link_files[i])
3318 error ("%s: linker input file unused since linking not done",
3321 /* Delete some or all of the temporary files we made. */
3324 delete_failure_queue ();
3325 delete_temp_files ();
3332 /* Find the proper compilation spec for the file name NAME,
3333 whose length is LENGTH. LANGUAGE is the specified language,
3334 or 0 if none specified. */
3336 static struct compiler *
3337 lookup_compiler (name, length, language)
3342 struct compiler *cp;
3344 /* Look for the language, if one is spec'd. */
3347 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3351 if (cp->suffix[0] == '@'
3352 && !strcmp (cp->suffix + 1, language))
3356 error ("language %s not recognized", language);
3359 /* Look for a suffix. */
3360 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3362 if (strlen (cp->suffix) < length
3363 /* See if the suffix matches the end of NAME. */
3364 && !strcmp (cp->suffix,
3365 name + length - strlen (cp->suffix))
3366 /* The suffix `-' matches only the file name `-'. */
3367 && !(!strcmp (cp->suffix, "-") && length != 1))
3369 if (cp->spec[0] == '@')
3371 struct compiler *new;
3372 /* An alias entry maps a suffix to a language.
3373 Search for the language; pass 0 for NAME and LENGTH
3374 to avoid infinite recursion if language not found.
3375 Construct the new compiler spec. */
3376 language = cp->spec + 1;
3377 new = (struct compiler *) xmalloc (sizeof (struct compiler));
3378 new->suffix = cp->suffix;
3379 new->spec = lookup_compiler (NULL_PTR, 0, language)->spec;
3382 /* A non-alias entry: return it. */
3394 register char *value = (char *) malloc (size);
3396 fatal ("virtual memory exhausted");
3401 xrealloc (ptr, size)
3405 register char *value = (char *) realloc (ptr, size);
3407 fatal ("virtual memory exhausted");
3411 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3417 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3418 char *result = xmalloc (len1 + len2 + len3 + 1);
3420 strcpy (result, s1);
3421 strcpy (result + len1, s2);
3422 strcpy (result + len1 + len2, s3);
3423 *(result + len1 + len2 + len3) = 0;
3429 save_string (s, len)
3433 register char *result = xmalloc (len + 1);
3435 bcopy (s, result, len);
3441 pfatal_with_name (name)
3446 if (errno < sys_nerr)
3447 s = concat ("%s: ", sys_errlist[errno], "");
3449 s = "cannot open %s";
3454 perror_with_name (name)
3459 if (errno < sys_nerr)
3460 s = concat ("%s: ", sys_errlist[errno], "");
3462 s = "cannot open %s";
3472 if (errno < sys_nerr)
3473 s = concat ("installation problem, cannot exec %s: ",
3474 sys_errlist[errno], "");
3476 s = "installation problem, cannot exec %s";
3480 /* More 'friendly' abort that prints the line and file.
3481 config.h can #define abort fancy_abort if you like that sort of thing. */
3486 fatal ("Internal gcc abort.");
3491 /* Output an error message and exit */
3501 format = va_arg (ap, char *);
3502 fprintf (stderr, "%s: ", programname);
3503 vfprintf (stderr, format, ap);
3505 fprintf (stderr, "\n");
3506 delete_temp_files ();
3518 format = va_arg (ap, char *);
3519 fprintf (stderr, "%s: ", programname);
3520 vfprintf (stderr, format, ap);
3523 fprintf (stderr, "\n");
3526 #else /* not HAVE_VPRINTF */
3529 fatal (msg, arg1, arg2)
3530 char *msg, *arg1, *arg2;
3532 error (msg, arg1, arg2);
3533 delete_temp_files ();
3538 error (msg, arg1, arg2)
3539 char *msg, *arg1, *arg2;
3541 fprintf (stderr, "%s: ", programname);
3542 fprintf (stderr, msg, arg1, arg2);
3543 fprintf (stderr, "\n");
3546 #endif /* not HAVE_VPRINTF */
3550 validate_all_switches ()
3552 struct compiler *comp;
3555 struct spec_list *spec;
3557 for (comp = compilers; comp->spec; comp++)
3561 if (c == '%' && *p == '{')
3562 /* We have a switch spec. */
3563 validate_switches (p + 1);
3566 /* look through the linked list of extra specs read from the specs file */
3567 for (spec = specs ; spec ; spec = spec->next)
3571 if (c == '%' && *p == '{')
3572 /* We have a switch spec. */
3573 validate_switches (p + 1);
3576 p = link_command_spec;
3578 if (c == '%' && *p == '{')
3579 /* We have a switch spec. */
3580 validate_switches (p + 1);
3582 /* Now notice switches mentioned in the machine-specific specs. */
3586 if (c == '%' && *p == '{')
3587 /* We have a switch spec. */
3588 validate_switches (p + 1);
3592 if (c == '%' && *p == '{')
3593 /* We have a switch spec. */
3594 validate_switches (p + 1);
3598 if (c == '%' && *p == '{')
3599 /* We have a switch spec. */
3600 validate_switches (p + 1);
3602 p = signed_char_spec;
3604 if (c == '%' && *p == '{')
3605 /* We have a switch spec. */
3606 validate_switches (p + 1);
3610 if (c == '%' && *p == '{')
3611 /* We have a switch spec. */
3612 validate_switches (p + 1);
3616 if (c == '%' && *p == '{')
3617 /* We have a switch spec. */
3618 validate_switches (p + 1);
3622 if (c == '%' && *p == '{')
3623 /* We have a switch spec. */
3624 validate_switches (p + 1);
3628 if (c == '%' && *p == '{')
3629 /* We have a switch spec. */
3630 validate_switches (p + 1);
3634 if (c == '%' && *p == '{')
3635 /* We have a switch spec. */
3636 validate_switches (p + 1);
3639 /* Look at the switch-name that comes after START
3640 and mark as valid all supplied switches that match it. */
3643 validate_switches (start)
3646 register char *p = start;
3661 while (*p != ':' && *p != '}') p++;
3665 else if (p[-1] == '*')
3667 /* Mark all matching switches as valid. */
3669 for (i = 0; i < n_switches; i++)
3670 if (!strncmp (switches[i].part1, filter, p - filter))
3671 switches[i].valid = 1;
3675 /* Mark an exact matching switch as valid. */
3676 for (i = 0; i < n_switches; i++)
3678 if (!strncmp (switches[i].part1, filter, p - filter)
3679 && switches[i].part1[p - filter] == 0)
3680 switches[i].valid = 1;