1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 This paragraph is here to try to keep Sun CC from dying.
21 The number of chars here seems crucial!!!! */
23 /* This program is the user interface to the C compiler and possibly to
24 other compilers. It is used because compilation is a complicated procedure
25 which involves running several programs and passing temporary files between
26 them, forwarding the users switches to those programs selectively,
27 and deleting the temporary files at the end.
29 CC recognizes how to compile each input file by suffixes in the file names.
30 Once it knows which kind of compilation to perform, the procedure for
31 compilation is specified by a string called a "spec". */
34 #include <sys/types.h>
53 /* On MSDOS, write temp files in current dir
54 because there's no place else we can expect to use. */
61 /* Test if something is a normal file. */
63 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
66 /* Test if something is a directory. */
68 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
71 /* By default there is no special suffix for executables. */
72 #ifndef EXECUTABLE_SUFFIX
73 #define EXECUTABLE_SUFFIX ""
76 /* By default, colon separates directories in a path. */
77 #ifndef PATH_SEPARATOR
78 #define PATH_SEPARATOR ':'
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
85 extern char *getenv ();
87 extern int errno
, sys_nerr
;
88 extern char *sys_errlist
[];
90 extern int execv (), execvp ();
92 /* If a stage of compilation returns an exit status >= 1,
93 compilation of that file ceases. */
95 #define MIN_FATAL_STATUS 1
97 /* Flag indicating whether we should print the command and arguments */
99 static int verbose_flag
;
101 /* Nonzero means write "temp" files in source directory
102 and use the source file's name in them, and don't delete them. */
104 static int save_temps_flag
;
106 /* The compiler version specified with -V */
108 static char *spec_version
;
110 /* The target machine specified with -b. */
112 static char *spec_machine
= DEFAULT_TARGET_MACHINE
;
114 /* Nonzero if cross-compiling.
115 When -b is used, the value comes from the `specs' file. */
118 static int cross_compile
= 1;
120 static int cross_compile
= 0;
123 /* This is the obstack which we use to allocate many strings. */
125 static struct obstack obstack
;
127 /* This is the obstack to build an environment variable to pass to
128 collect2 that describes all of the relevant switches of what to
129 pass the compiler in building the list of pointers to constructors
132 static struct obstack collect_obstack
;
134 extern char *version_string
;
136 static void set_spec ();
137 static struct compiler
*lookup_compiler ();
138 static char *find_a_file ();
139 static void add_prefix ();
140 static char *skip_whitespace ();
141 static void record_temp_file ();
142 static char *handle_braces ();
143 static char *save_string ();
144 static char *concat ();
145 static int do_spec ();
146 static int do_spec_1 ();
147 static char *find_file ();
148 static int is_linker_dir ();
149 static void validate_switches ();
150 static void validate_all_switches ();
151 static void give_switch ();
152 static void pfatal_with_name ();
153 static void perror_with_name ();
154 static void perror_exec ();
155 static void fatal ();
156 static void error ();
161 /* Specs are strings containing lines, each of which (if not blank)
162 is made up of a program name, and arguments separated by spaces.
163 The program name must be exact and start from root, since no path
164 is searched and it is unreliable to depend on the current working directory.
165 Redirection of input or output is not supported; the subprograms must
166 accept filenames saying what files to read and write.
168 In addition, the specs can contain %-sequences to substitute variable text
169 or for conditional text. Here is a table of all defined %-sequences.
170 Note that spaces are not generated automatically around the results of
171 expanding these sequences; therefore, you can concatenate them together
172 or with constant text in a single argument.
174 %% substitute one % into the program name or argument.
175 %i substitute the name of the input file being processed.
176 %b substitute the basename of the input file being processed.
177 This is the substring up to (and not including) the last period
178 and not including the directory.
179 %g substitute the temporary-file-name-base. This is a string chosen
180 once per compilation. Different temporary file names are made by
181 concatenation of constant strings on the end, as in `%g.s'.
182 %g also has the same effect of %d.
183 %d marks the argument containing or following the %d as a
184 temporary file name, so that that file will be deleted if CC exits
185 successfully. Unlike %g, this contributes no text to the argument.
186 %w marks the argument containing or following the %w as the
187 "output file" of this compilation. This puts the argument
188 into the sequence of arguments that %o will substitute later.
190 like %{...} but mark last argument supplied within
191 as a file to be deleted on failure.
192 %o substitutes the names of all the output files, with spaces
193 automatically placed around them. You should write spaces
194 around the %o as well or the results are undefined.
195 %o is for use in the specs for running the linker.
196 Input files whose names have no recognized suffix are not compiled
197 at all, but they are included among the output files, so they will
199 %p substitutes the standard macro predefinitions for the
200 current target machine. Use this when running cpp.
201 %P like %p, but puts `__' before and after the name of each macro.
202 (Except macros that already have __.)
204 %s current argument is the name of a library or startup file of some sort.
205 Search for that file in a standard list of directories
206 and substitute the full name found.
207 %eSTR Print STR as an error message. STR is terminated by a newline.
208 Use this when inconsistent options are detected.
209 %x{OPTION} Accumulate an option for %X.
210 %X Output the accumulated linker options specified by compilations.
211 %a process ASM_SPEC as a spec.
212 This allows config.h to specify part of the spec for running as.
213 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
214 used here. This can be used to run a post-processor after the
215 assembler has done it's job.
216 %D Dump out a -L option for each directory in library_prefix,
217 followed by a -L option for each directory in startfile_prefix.
218 %l process LINK_SPEC as a spec.
219 %L process LIB_SPEC as a spec.
220 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
221 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
222 %c process SIGNED_CHAR_SPEC as a spec.
223 %C process CPP_SPEC as a spec. A capital C is actually used here.
224 %1 process CC1_SPEC as a spec.
225 %2 process CC1PLUS_SPEC as a spec.
226 %* substitute the variable part of a matched option. (See below.)
227 Note that each comma in the substituted string is replaced by
229 %{S} substitutes the -S switch, if that switch was given to CC.
230 If that switch was not specified, this substitutes nothing.
231 Here S is a metasyntactic variable.
232 %{S*} substitutes all the switches specified to CC whose names start
233 with -S. This is used for -o, -D, -I, etc; switches that take
234 arguments. CC considers `-o foo' as being one switch whose
235 name starts with `o'. %{o*} would substitute this text,
236 including the space; thus, two arguments would be generated.
237 %{S*:X} substitutes X if one or more switches whose names with -S are
238 specified to CC. Note that the tail part of the -S option
239 (i.e. the part matched by the `*') will be substituted for each
240 occurrence of %* within X.
241 %{S:X} substitutes X, but only if the -S switch was given to CC.
242 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
243 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
244 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
245 %{.S:X} substitutes X, but only if processing a file with suffix S.
246 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
247 %(Spec) processes a specification defined in a specs file as *Spec:
248 %[Spec] as above, but put __ around -D arguments
250 The conditional text X in a %{S:X} or %{!S:X} construct may contain
251 other nested % constructs or spaces, or even newlines. They are
252 processed as usual, as described above.
254 The character | is used to indicate that a command should be piped to
255 the following command, but only if -pipe is specified.
257 Note that it is built into CC which switches take arguments and which
258 do not. You might think it would be useful to generalize this to
259 allow each compiler's spec to say which switches take arguments. But
260 this cannot be done in a consistent fashion. CC cannot even decide
261 which input files have been specified without knowing which switches
262 take arguments, and it must know which input files to compile in order
263 to tell which compilers to run.
265 CC also knows implicitly that arguments starting in `-l' are to be
266 treated as compiler output files, and passed to the linker in their
267 proper position among the other output files. */
269 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
271 /* config.h can define ASM_SPEC to provide extra args to the assembler
272 or extra switch-translations. */
277 /* config.h can define ASM_FINAL_SPEC to run a post processor after
278 the assembler has run. */
279 #ifndef ASM_FINAL_SPEC
280 #define ASM_FINAL_SPEC ""
283 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
284 or extra switch-translations. */
289 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
290 or extra switch-translations. */
295 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
296 or extra switch-translations. */
298 #define CC1PLUS_SPEC ""
301 /* config.h can define LINK_SPEC to provide extra args to the linker
302 or extra switch-translations. */
307 /* config.h can define LIB_SPEC to override the default libraries. */
309 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
312 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
313 #ifndef STARTFILE_SPEC
314 #define STARTFILE_SPEC \
315 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
318 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
319 Make the string nonempty to require spaces there. */
320 #ifndef SWITCHES_NEED_SPACES
321 #define SWITCHES_NEED_SPACES ""
324 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
326 #define ENDFILE_SPEC ""
329 /* This spec is used for telling cpp whether char is signed or not. */
330 #ifndef SIGNED_CHAR_SPEC
331 #define SIGNED_CHAR_SPEC \
332 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
333 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
336 static char *cpp_spec
= CPP_SPEC
;
337 static char *cpp_predefines
= CPP_PREDEFINES
;
338 static char *cc1_spec
= CC1_SPEC
;
339 static char *cc1plus_spec
= CC1PLUS_SPEC
;
340 static char *signed_char_spec
= SIGNED_CHAR_SPEC
;
341 static char *asm_spec
= ASM_SPEC
;
342 static char *asm_final_spec
= ASM_FINAL_SPEC
;
343 static char *link_spec
= LINK_SPEC
;
344 static char *lib_spec
= LIB_SPEC
;
345 static char *endfile_spec
= ENDFILE_SPEC
;
346 static char *startfile_spec
= STARTFILE_SPEC
;
347 static char *switches_need_spaces
= SWITCHES_NEED_SPACES
;
349 /* This defines which switch letters take arguments. */
351 #ifndef SWITCH_TAKES_ARG
352 #define SWITCH_TAKES_ARG(CHAR) \
353 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
354 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
355 || (CHAR) == 'I' || (CHAR) == 'm' \
356 || (CHAR) == 'L' || (CHAR) == 'A')
359 /* This defines which multi-letter switches take arguments. */
361 #ifndef WORD_SWITCH_TAKES_ARG
362 #define WORD_SWITCH_TAKES_ARG(STR) \
363 (!strcmp (STR, "Tdata") || !strcmp (STR, "include") \
364 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
367 /* Record the mapping from file suffixes for compilation specs. */
371 char *suffix
; /* Use this compiler for input files
372 whose names end in this suffix. */
373 char *spec
; /* To use this compiler, pass this spec
377 /* Pointer to a vector of `struct compiler' that gives the spec for
378 compiling a file, based on its suffix.
379 A file that does not end in any of these suffixes will be passed
380 unchanged to the loader and nothing else will be done to it.
382 An entry containing two 0s is used to terminate the vector.
384 If multiple entries match a file, the last matching one is used. */
386 static struct compiler
*compilers
;
388 /* Number of entries in `compilers', not counting the null terminator. */
390 static int n_compilers
;
392 /* The default list of file name suffixes and their compilation specs. */
394 static struct compiler default_compilers
[] =
398 "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
399 %{C:%{!E:%eGNU C does not support -C without using -E}}\
400 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
401 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
402 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
403 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
404 %{traditional-cpp:-traditional}\
405 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
406 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
407 %{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
408 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
409 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
410 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
412 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
413 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
414 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
415 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
416 %{!pipe:%g.s} %A\n }}}}"},
418 "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
419 %{C:%{!E:%eGNU C does not support -C without using -E}}\
420 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
421 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
422 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
423 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
424 %{traditional-cpp:-traditional}\
425 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
427 %{!E:%e-E required when input is from standard input}"},
428 {".m", "@objective-c"},
430 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
431 %{C:%{!E:%eGNU C does not support -C without using -E}}\
432 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
433 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
434 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
435 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
436 %{traditional-cpp:-traditional}\
437 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
438 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
439 %{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
440 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
441 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
442 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
443 -lang-objc %{gen-decls} \
445 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
446 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
447 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
448 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
449 %{!pipe:%g.s} %A\n }}}}"},
452 "%{!E:%eCompilation of header file requested} \
453 cpp %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
454 %{C:%{!E:%eGNU C does not support -C without using -E}}\
455 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
456 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
457 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
458 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
459 %{traditional-cpp:-traditional}\
460 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
466 "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
467 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
468 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
469 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
470 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
471 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
472 %{traditional-cpp:-traditional} %{trigraphs}\
473 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
474 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
475 %{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
476 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
477 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
478 %{v:-version} %{pg:-p} %{p} %{f*}\
480 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
481 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
482 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
483 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
484 %{!pipe:%g.s} %A\n }}}}"},
485 {".i", "@cpp-output"},
487 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
488 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
489 %{v:-version} %{pg:-p} %{p} %{f*}\
491 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
492 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
493 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
494 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s} %A\n }"},
495 {".ii", "@c++-cpp-output"},
497 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
498 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
499 %{v:-version} %{pg:-p} %{p} %{f*}\
501 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
502 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
503 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
504 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
505 %{!pipe:%g.s} %A\n }"},
506 {".s", "@assembler"},
508 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
509 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i %A\n }"},
510 {".S", "@assembler-with-cpp"},
511 {"@assembler-with-cpp",
512 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
513 %{C:%{!E:%eGNU C does not support -C without using -E}}\
514 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
515 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
516 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
517 %{traditional-cpp:-traditional}\
518 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
519 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
520 %{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
521 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
522 %{!pipe:%g.s} %A\n }}}}"},
523 /* Mark end of table */
527 /* Number of elements in default_compilers, not counting the terminator. */
529 static int n_default_compilers
530 = (sizeof default_compilers
/ sizeof (struct compiler
)) - 1;
532 /* Here is the spec for running the linker, after compiling all files. */
534 #ifdef LINK_LIBGCC_SPECIAL
535 /* Have gcc do the search. */
536 /* -u* was put back because both BSD and SysV seem to support it. */
537 static char *link_command_spec
= "\
538 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
539 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
540 %{!A:%{!nostdlib:%S}} \
541 %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
543 /* Use -l and have the linker do the search. */
544 static char *link_command_spec
= "\
545 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
546 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
547 %{!A:%{!nostdlib:%S}} \
548 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
551 /* A vector of options to give to the linker.
552 These options are accumulated by %x
553 and substituted into the linker command with %X. */
554 static int n_linker_options
;
555 static char **linker_options
;
557 /* Read compilation specs from a file named FILENAME,
558 replacing the default ones.
560 A suffix which starts with `*' is a definition for
561 one of the machine-specific sub-specs. The "suffix" should be
562 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
563 The corresponding spec is stored in asm_spec, etc.,
564 rather than in the `compilers' vector.
566 Anything invalid in the file is a fatal error. */
569 read_specs (filename
)
578 fprintf (stderr
, "Reading specs from %s\n", filename
);
580 /* Open and stat the file. */
581 desc
= open (filename
, 0, 0);
583 pfatal_with_name (filename
);
584 if (stat (filename
, &statbuf
) < 0)
585 pfatal_with_name (filename
);
587 /* Read contents of file into BUFFER. */
588 buffer
= xmalloc (statbuf
.st_size
+ 1);
589 read (desc
, buffer
, statbuf
.st_size
);
590 buffer
[statbuf
.st_size
] = 0;
593 /* Scan BUFFER for specs, putting them in the vector. */
599 char *in
, *out
, *p1
, *p2
;
601 /* Advance P in BUFFER to the next nonblank nocomment line. */
602 p
= skip_whitespace (p
);
606 /* Find the colon that should end the suffix. */
608 while (*p1
&& *p1
!= ':' && *p1
!= '\n') p1
++;
609 /* The colon shouldn't be missing. */
611 fatal ("specs file malformed after %d characters", p1
- buffer
);
612 /* Skip back over trailing whitespace. */
614 while (p2
> buffer
&& (p2
[-1] == ' ' || p2
[-1] == '\t')) p2
--;
615 /* Copy the suffix to a string. */
616 suffix
= save_string (p
, p2
- p
);
617 /* Find the next line. */
618 p
= skip_whitespace (p1
+ 1);
620 fatal ("specs file malformed after %d characters", p
- buffer
);
622 /* Find next blank line. */
623 while (*p1
&& !(*p1
== '\n' && p1
[1] == '\n')) p1
++;
624 /* Specs end at the blank line and do not include the newline. */
625 spec
= save_string (p
, p1
- p
);
628 /* Delete backslash-newline sequences from the spec. */
633 if (in
[0] == '\\' && in
[1] == '\n')
635 else if (in
[0] == '#')
637 while (*in
&& *in
!= '\n') in
++;
644 if (suffix
[0] == '*')
646 if (! strcmp (suffix
, "*link_command"))
647 link_command_spec
= spec
;
649 set_spec (suffix
+ 1, spec
);
653 /* Add this pair to the vector. */
655 = ((struct compiler
*)
656 xrealloc (compilers
, (n_compilers
+ 2) * sizeof (struct compiler
)));
657 compilers
[n_compilers
].suffix
= suffix
;
658 compilers
[n_compilers
].spec
= spec
;
663 link_command_spec
= spec
;
666 if (link_command_spec
== 0)
667 fatal ("spec file has no spec for linking");
676 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
677 be considered whitespace. */
678 if (p
[0] == '\n' && p
[1] == '\n' && p
[2] == '\n')
680 else if (*p
== '\n' || *p
== ' ' || *p
== '\t')
684 while (*p
!= '\n') p
++;
694 /* Structure to keep track of the specs that have been defined so far. These
695 are accessed using %(specname) or %[specname] in a compiler or link spec. */
699 char *name
; /* Name of the spec. */
700 char *spec
; /* The spec itself. */
701 struct spec_list
*next
; /* Next spec in linked list. */
704 /* List of specs that have been defined so far. */
706 static struct spec_list
*specs
= (struct spec_list
*) 0;
708 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
709 removed; If the spec starts with a + then SPEC is added to the end of the
713 set_spec (name
, spec
)
717 struct spec_list
*sl
;
720 /* See if the spec already exists */
721 for (sl
= specs
; sl
; sl
= sl
->next
)
722 if (strcmp (sl
->name
, name
) == 0)
727 /* Not found - make it */
728 sl
= (struct spec_list
*) xmalloc (sizeof (struct spec_list
));
729 sl
->name
= save_string (name
, strlen (name
));
730 sl
->spec
= save_string ("", 0);
736 if (name
&& spec
[0] == '+' && isspace (spec
[1]))
737 sl
->spec
= concat (name
, spec
+ 1, "");
739 sl
->spec
= save_string (spec
, strlen (spec
));
741 if (! strcmp (name
, "asm"))
743 else if (! strcmp (name
, "asm_final"))
744 asm_final_spec
= sl
->spec
;
745 else if (! strcmp (name
, "cc1"))
747 else if (! strcmp (name
, "cc1plus"))
748 cc1plus_spec
= sl
->spec
;
749 else if (! strcmp (name
, "cpp"))
751 else if (! strcmp (name
, "endfile"))
752 endfile_spec
= sl
->spec
;
753 else if (! strcmp (name
, "lib"))
755 else if (! strcmp (name
, "link"))
756 link_spec
= sl
->spec
;
757 else if (! strcmp (name
, "predefines"))
758 cpp_predefines
= sl
->spec
;
759 else if (! strcmp (name
, "signed_char"))
760 signed_char_spec
= sl
->spec
;
761 else if (! strcmp (name
, "startfile"))
762 startfile_spec
= sl
->spec
;
763 else if (! strcmp (name
, "switches_need_spaces"))
764 switches_need_spaces
= sl
->spec
;
765 else if (! strcmp (name
, "cross_compile"))
766 cross_compile
= atoi (sl
->spec
);
767 /* Free the old spec */
772 /* Accumulate a command (program name and args), and run it. */
774 /* Vector of pointers to arguments in the current line of specifications. */
776 static char **argbuf
;
778 /* Number of elements allocated in argbuf. */
780 static int argbuf_length
;
782 /* Number of elements in argbuf currently in use (containing args). */
784 static int argbuf_index
;
786 /* Number of commands executed so far. */
788 static int execution_count
;
790 /* Name with which this program was invoked. */
792 static char *programname
;
794 /* Structures to keep track of prefixes to try when looking for files. */
798 char *prefix
; /* String to prepend to the path. */
799 struct prefix_list
*next
; /* Next in linked list. */
800 int require_machine_suffix
; /* Don't use without machine_suffix. */
801 int *used_flag_ptr
; /* 1 if a file was found with this prefix. */
806 struct prefix_list
*plist
; /* List of prefixes to try */
807 int max_len
; /* Max length of a prefix in PLIST */
808 char *name
; /* Name of this list (used in config stuff) */
811 /* List of prefixes to try when looking for executables. */
813 static struct path_prefix exec_prefix
= { 0, 0, "exec" };
815 /* List of prefixes to try when looking for startup (crt0) files. */
817 static struct path_prefix startfile_prefix
= { 0, 0, "startfile" };
819 /* List of prefixes to try when looking for libraries. */
821 static struct path_prefix library_prefix
= { 0, 0, "libraryfile" };
823 /* Suffix to attach to directories searched for commands. */
825 static char *machine_suffix
= 0;
827 /* Default prefixes to attach to command names. */
829 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
830 #undef MD_EXEC_PREFIX
831 #undef MD_STARTFILE_PREFIX
834 #ifndef STANDARD_EXEC_PREFIX
835 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
836 #endif /* !defined STANDARD_EXEC_PREFIX */
838 static char *standard_exec_prefix
= STANDARD_EXEC_PREFIX
;
839 static char *standard_exec_prefix_1
= "/usr/lib/gcc/";
840 #ifdef MD_EXEC_PREFIX
841 static char *md_exec_prefix
= MD_EXEC_PREFIX
;
844 #ifndef STANDARD_STARTFILE_PREFIX
845 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
846 #endif /* !defined STANDARD_STARTFILE_PREFIX */
848 #ifdef MD_STARTFILE_PREFIX
849 static char *md_startfile_prefix
= MD_STARTFILE_PREFIX
;
851 static char *standard_startfile_prefix
= STANDARD_STARTFILE_PREFIX
;
852 static char *standard_startfile_prefix_1
= "/lib/";
853 static char *standard_startfile_prefix_2
= "/usr/lib/";
855 /* Clear out the vector of arguments (after a command is executed). */
863 /* Add one argument to the vector at the end.
864 This is done when a space is seen or at the end of the line.
865 If DELETE_ALWAYS is nonzero, the arg is a filename
866 and the file should be deleted eventually.
867 If DELETE_FAILURE is nonzero, the arg is a filename
868 and the file should be deleted if this compilation fails. */
871 store_arg (arg
, delete_always
, delete_failure
)
873 int delete_always
, delete_failure
;
875 if (argbuf_index
+ 1 == argbuf_length
)
877 argbuf
= (char **) xrealloc (argbuf
, (argbuf_length
*= 2) * sizeof (char *));
880 argbuf
[argbuf_index
++] = arg
;
881 argbuf
[argbuf_index
] = 0;
883 if (delete_always
|| delete_failure
)
884 record_temp_file (arg
, delete_always
, delete_failure
);
887 /* Record the names of temporary files we tell compilers to write,
888 and delete them at the end of the run. */
890 /* This is the common prefix we use to make temp file names.
891 It is chosen once for each run of this program.
892 It is substituted into a spec by %g.
893 Thus, all temp file names contain this prefix.
894 In practice, all temp file names start with this prefix.
896 This prefix comes from the envvar TMPDIR if it is defined;
897 otherwise, from the P_tmpdir macro if that is defined;
898 otherwise, in /usr/tmp or /tmp. */
900 static char *temp_filename
;
902 /* Length of the prefix. */
904 static int temp_filename_length
;
906 /* Define the list of temporary files to delete. */
911 struct temp_file
*next
;
914 /* Queue of files to delete on success or failure of compilation. */
915 static struct temp_file
*always_delete_queue
;
916 /* Queue of files to delete on failure of compilation. */
917 static struct temp_file
*failure_delete_queue
;
919 /* Record FILENAME as a file to be deleted automatically.
920 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
921 otherwise delete it in any case.
922 FAIL_DELETE nonzero means delete it if a compilation step fails;
923 otherwise delete it in any case. */
926 record_temp_file (filename
, always_delete
, fail_delete
)
932 name
= xmalloc (strlen (filename
) + 1);
933 strcpy (name
, filename
);
937 register struct temp_file
*temp
;
938 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
939 if (! strcmp (name
, temp
->name
))
941 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
942 temp
->next
= always_delete_queue
;
944 always_delete_queue
= temp
;
950 register struct temp_file
*temp
;
951 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
952 if (! strcmp (name
, temp
->name
))
954 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
955 temp
->next
= failure_delete_queue
;
957 failure_delete_queue
= temp
;
962 /* Delete all the temporary files whose names we previously recorded. */
967 register struct temp_file
*temp
;
969 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
973 printf ("Delete %s? (y or n) ", temp
->name
);
977 while (getchar () != '\n') ;
978 if (i
== 'y' || i
== 'Y')
982 if (stat (temp
->name
, &st
) >= 0)
984 /* Delete only ordinary files. */
985 if (S_ISREG (st
.st_mode
))
986 if (unlink (temp
->name
) < 0)
988 perror_with_name (temp
->name
);
993 always_delete_queue
= 0;
996 /* Delete all the files to be deleted on error. */
999 delete_failure_queue ()
1001 register struct temp_file
*temp
;
1003 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
1007 printf ("Delete %s? (y or n) ", temp
->name
);
1011 while (getchar () != '\n') ;
1012 if (i
== 'y' || i
== 'Y')
1015 if (unlink (temp
->name
) < 0)
1017 perror_with_name (temp
->name
);
1023 clear_failure_queue ()
1025 failure_delete_queue
= 0;
1028 /* Compute a string to use as the base of all temporary file names.
1029 It is substituted for %g. */
1034 char *base
= getenv ("TMPDIR");
1037 if (base
== (char *)0)
1040 if (access (P_tmpdir
, R_OK
| W_OK
) == 0)
1043 if (base
== (char *)0)
1045 if (access ("/usr/tmp", R_OK
| W_OK
) == 0)
1052 len
= strlen (base
);
1053 temp_filename
= xmalloc (len
+ sizeof("/ccXXXXXX"));
1054 strcpy (temp_filename
, base
);
1055 if (len
> 0 && temp_filename
[len
-1] != '/')
1056 temp_filename
[len
++] = '/';
1057 strcpy (temp_filename
+ len
, "ccXXXXXX");
1059 mktemp (temp_filename
);
1060 temp_filename_length
= strlen (temp_filename
);
1064 /* Routine to add variables to the environment. We do this to pass
1065 the pathname of the gcc driver, and the directories search to the
1066 collect2 program, which is being run as ld. This way, we can be
1067 sure of executing the right compiler when collect2 wants to build
1068 constructors and destructors. Since the environment variables we
1069 use come from an obstack, we don't have to worry about allocating
1077 #ifndef VMS /* nor about VMS */
1079 extern char **environ
;
1080 char **old_environ
= environ
;
1084 int str_len
= strlen (str
);
1088 while ((ch
= *p
++) != '\0' && ch
!= '=')
1094 /* Search for replacing an existing environment variable, and
1095 count the number of total environment variables. */
1096 for (envp
= old_environ
; *envp
; envp
++)
1099 if (!strncmp (str
, *envp
, name_len
))
1106 /* Add a new environment variable */
1107 environ
= (char **) xmalloc (sizeof (char *) * (num_envs
+2));
1109 bcopy (old_environ
, environ
+1, sizeof (char *) * (num_envs
+1));
1114 #endif /* HAVE_PUTENV */
1117 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1120 putenv_from_prefixes (paths
, env_var
)
1121 struct path_prefix
*paths
;
1124 int suffix_len
= (machine_suffix
) ? strlen (machine_suffix
) : 0;
1125 int first_time
= TRUE
;
1126 struct prefix_list
*pprefix
;
1128 obstack_grow (&collect_obstack
, env_var
, strlen (env_var
));
1130 for (pprefix
= paths
->plist
; pprefix
!= 0; pprefix
= pprefix
->next
)
1132 int len
= strlen (pprefix
->prefix
);
1137 obstack_grow (&collect_obstack
, ":", 1);
1140 obstack_grow (&collect_obstack
, pprefix
->prefix
, len
);
1141 obstack_grow (&collect_obstack
, machine_suffix
, suffix_len
);
1144 if (!pprefix
->require_machine_suffix
)
1147 obstack_grow (&collect_obstack
, ":", 1);
1150 obstack_grow (&collect_obstack
, pprefix
->prefix
, len
);
1153 obstack_grow (&collect_obstack
, "\0", 1);
1154 putenv (obstack_finish (&collect_obstack
));
1158 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1159 access to check permissions.
1160 Return 0 if not found, otherwise return its name, allocated with malloc. */
1163 find_a_file (pprefix
, name
, mode
)
1164 struct path_prefix
*pprefix
;
1169 char *file_suffix
= ((mode
& X_OK
) != 0 ? EXECUTABLE_SUFFIX
: "");
1170 struct prefix_list
*pl
;
1171 int len
= pprefix
->max_len
+ strlen (name
) + strlen (file_suffix
) + 1;
1174 len
+= strlen (machine_suffix
);
1176 temp
= xmalloc (len
);
1178 /* Determine the filename to execute (special case for absolute paths). */
1182 if (access (name
, mode
))
1184 strcpy (temp
, name
);
1189 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
1193 strcpy (temp
, pl
->prefix
);
1194 strcat (temp
, machine_suffix
);
1195 strcat (temp
, name
);
1196 if (access (temp
, mode
) == 0)
1198 if (pl
->used_flag_ptr
!= 0)
1199 *pl
->used_flag_ptr
= 1;
1202 /* Some systems have a suffix for executable files.
1203 So try appending that. */
1204 if (file_suffix
[0] != 0)
1206 strcat (temp
, file_suffix
);
1207 if (access (temp
, mode
) == 0)
1209 if (pl
->used_flag_ptr
!= 0)
1210 *pl
->used_flag_ptr
= 1;
1215 /* Certain prefixes can't be used without the machine suffix
1216 when the machine or version is explicitly specified. */
1217 if (!pl
->require_machine_suffix
)
1219 strcpy (temp
, pl
->prefix
);
1220 strcat (temp
, name
);
1221 if (access (temp
, mode
) == 0)
1223 if (pl
->used_flag_ptr
!= 0)
1224 *pl
->used_flag_ptr
= 1;
1227 /* Some systems have a suffix for executable files.
1228 So try appending that. */
1229 if (file_suffix
[0] != 0)
1231 strcat (temp
, file_suffix
);
1232 if (access (temp
, mode
) == 0)
1234 if (pl
->used_flag_ptr
!= 0)
1235 *pl
->used_flag_ptr
= 1;
1246 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1247 at the start of the list, otherwise it goes at the end.
1249 If WARN is nonzero, we will warn if no file is found
1250 through this prefix. WARN should point to an int
1251 which will be set to 1 if this entry is used. */
1254 add_prefix (pprefix
, prefix
, first
, require_machine_suffix
, warn
)
1255 struct path_prefix
*pprefix
;
1258 int require_machine_suffix
;
1261 struct prefix_list
*pl
, **prev
;
1264 if (!first
&& pprefix
->plist
)
1266 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
1271 prev
= &pprefix
->plist
;
1273 /* Keep track of the longest prefix */
1275 len
= strlen (prefix
);
1276 if (len
> pprefix
->max_len
)
1277 pprefix
->max_len
= len
;
1279 pl
= (struct prefix_list
*) xmalloc (sizeof (struct prefix_list
));
1280 pl
->prefix
= save_string (prefix
, len
);
1281 pl
->require_machine_suffix
= require_machine_suffix
;
1282 pl
->used_flag_ptr
= warn
;
1289 pl
->next
= (struct prefix_list
*) 0;
1293 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1296 unused_prefix_warnings (pprefix
)
1297 struct path_prefix
*pprefix
;
1299 struct prefix_list
*pl
= pprefix
->plist
;
1303 if (pl
->used_flag_ptr
!= 0 && !*pl
->used_flag_ptr
)
1305 error ("file path prefix `%s' never used",
1307 /* Prevent duplicate warnings. */
1308 *pl
->used_flag_ptr
= 1;
1314 /* Get rid of all prefixes built up so far in *PLISTP. */
1317 free_path_prefix (pprefix
)
1318 struct path_prefix
*pprefix
;
1320 struct prefix_list
*pl
= pprefix
->plist
;
1321 struct prefix_list
*temp
;
1327 free (temp
->prefix
);
1328 free ((char *) temp
);
1330 pprefix
->plist
= (struct prefix_list
*) 0;
1333 /* stdin file number. */
1334 #define STDIN_FILE_NO 0
1336 /* stdout file number. */
1337 #define STDOUT_FILE_NO 1
1339 /* value of `pipe': port index for reading. */
1342 /* value of `pipe': port index for writing. */
1343 #define WRITE_PORT 1
1345 /* Pipe waiting from last process, to be used as input for the next one.
1346 Value is STDIN_FILE_NO if no pipe is waiting
1347 (i.e. the next command is the first of a group). */
1349 static int last_pipe_input
;
1351 /* Fork one piped subcommand. FUNC is the system call to use
1352 (either execv or execvp). ARGV is the arg vector to use.
1353 NOT_LAST is nonzero if this is not the last subcommand
1354 (i.e. its output should be piped to the next one.) */
1358 /* Declare these to avoid compilation error. They won't be called. */
1359 int execv(const char *a
, const char **b
){}
1360 int execvp(const char *a
, const char **b
){}
1363 pexecute (func
, program
, argv
, not_last
)
1373 scmd
= (char *)malloc (strlen (program
) + strlen (temp_filename
) + 6);
1374 sprintf (scmd
, "%s @%s.gp", program
, temp_filename
);
1375 argfile
= fopen (scmd
+strlen (program
) + 2, "w");
1377 pfatal_with_name (scmd
+ strlen (program
) + 2);
1379 for (i
=1; argv
[i
]; i
++)
1382 for (cp
= argv
[i
]; *cp
; cp
++)
1384 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || isspace (*cp
))
1385 fputc ('\\', argfile
);
1386 fputc (*cp
, argfile
);
1388 fputc ('\n', argfile
);
1394 remove (scmd
+ strlen (program
) + 2);
1398 #else /* not __MSDOS__ */
1401 pexecute (func
, program
, argv
, not_last
)
1409 int input_desc
= last_pipe_input
;
1410 int output_desc
= STDOUT_FILE_NO
;
1411 int retries
, sleep_interval
;
1413 /* If this isn't the last process, make a pipe for its output,
1414 and record it as waiting to be the input to the next process. */
1418 if (pipe (pdes
) < 0)
1419 pfatal_with_name ("pipe");
1420 output_desc
= pdes
[WRITE_PORT
];
1421 last_pipe_input
= pdes
[READ_PORT
];
1424 last_pipe_input
= STDIN_FILE_NO
;
1426 /* Fork a subprocess; wait and retry if it fails. */
1428 for (retries
= 0; retries
< 4; retries
++)
1433 sleep (sleep_interval
);
1434 sleep_interval
*= 2;
1441 pfatal_with_name ("fork");
1443 pfatal_with_name ("vfork");
1449 /* Move the input and output pipes into place, if nec. */
1450 if (input_desc
!= STDIN_FILE_NO
)
1452 close (STDIN_FILE_NO
);
1456 if (output_desc
!= STDOUT_FILE_NO
)
1458 close (STDOUT_FILE_NO
);
1460 close (output_desc
);
1463 /* Close the parent's descs that aren't wanted here. */
1464 if (last_pipe_input
!= STDIN_FILE_NO
)
1465 close (last_pipe_input
);
1467 /* Exec the program. */
1468 (*func
) (program
, argv
);
1469 perror_exec (program
);
1475 /* In the parent, after forking.
1476 Close the descriptors that we made for this child. */
1477 if (input_desc
!= STDIN_FILE_NO
)
1479 if (output_desc
!= STDOUT_FILE_NO
)
1480 close (output_desc
);
1482 /* Return child's process number. */
1487 #endif /* not __MSDOS__ */
1489 /* Execute the command specified by the arguments on the current line of spec.
1490 When using pipes, this includes several piped-together commands
1491 with `|' between them.
1493 Return 0 if successful, -1 if failed. */
1499 int n_commands
; /* # of command. */
1503 char *prog
; /* program name. */
1504 char **argv
; /* vector of args. */
1505 int pid
; /* pid of process for this command. */
1508 struct command
*commands
; /* each command buffer with above info. */
1510 /* Count # of piped commands. */
1511 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1512 if (strcmp (argbuf
[i
], "|") == 0)
1515 /* Get storage for each command. */
1517 = (struct command
*) alloca (n_commands
* sizeof (struct command
));
1519 /* Split argbuf into its separate piped processes,
1520 and record info about each one.
1521 Also search for the programs that are to be run. */
1523 commands
[0].prog
= argbuf
[0]; /* first command. */
1524 commands
[0].argv
= &argbuf
[0];
1525 string
= find_a_file (&exec_prefix
, commands
[0].prog
, X_OK
);
1527 commands
[0].argv
[0] = string
;
1529 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1530 if (strcmp (argbuf
[i
], "|") == 0)
1531 { /* each command. */
1533 fatal ("-pipe not supported under MS-DOS");
1535 argbuf
[i
] = 0; /* termination of command args. */
1536 commands
[n_commands
].prog
= argbuf
[i
+ 1];
1537 commands
[n_commands
].argv
= &argbuf
[i
+ 1];
1538 string
= find_a_file (&exec_prefix
, commands
[n_commands
].prog
, X_OK
);
1540 commands
[n_commands
].argv
[0] = string
;
1544 argbuf
[argbuf_index
] = 0;
1546 /* If -v, print what we are about to do, and maybe query. */
1550 /* Print each piped command as a separate line. */
1551 for (i
= 0; i
< n_commands
; i
++)
1555 for (j
= commands
[i
].argv
; *j
; j
++)
1556 fprintf (stderr
, " %s", *j
);
1558 /* Print a pipe symbol after all but the last command. */
1559 if (i
+ 1 != n_commands
)
1560 fprintf (stderr
, " |");
1561 fprintf (stderr
, "\n");
1565 fprintf (stderr
, "\nGo ahead? (y or n) ");
1569 while (getchar () != '\n') ;
1570 if (i
!= 'y' && i
!= 'Y')
1575 /* Run each piped subprocess. */
1577 last_pipe_input
= STDIN_FILE_NO
;
1578 for (i
= 0; i
< n_commands
; i
++)
1580 char *string
= commands
[i
].argv
[0];
1582 commands
[i
].pid
= pexecute ((string
!= commands
[i
].prog
? execv
: execvp
),
1583 string
, commands
[i
].argv
,
1584 i
+ 1 < n_commands
);
1586 if (string
!= commands
[i
].prog
)
1592 /* Wait for all the subprocesses to finish.
1593 We don't care what order they finish in;
1594 we know that N_COMMANDS waits will get them all. */
1599 for (i
= 0; i
< n_commands
; i
++)
1606 status
= pid
= commands
[i
].pid
;
1608 pid
= wait (&status
);
1616 for (j
= 0; j
< n_commands
; j
++)
1617 if (commands
[j
].pid
== pid
)
1618 prog
= commands
[j
].prog
;
1620 if ((status
& 0x7F) != 0)
1621 fatal ("Internal compiler error: program %s got fatal signal %d",
1622 prog
, (status
& 0x7F));
1623 if (((status
& 0xFF00) >> 8) >= MIN_FATAL_STATUS
)
1631 /* Find all the switches given to us
1632 and make a vector describing them.
1633 The elements of the vector are strings, one per switch given.
1634 If a switch uses following arguments, then the `part1' field
1635 is the switch itself and the `args' field
1636 is a null-terminated vector containing the following arguments.
1637 The `valid' field is nonzero if any spec has looked at this switch;
1638 if it remains zero at the end of the run, it must be meaningless. */
1647 static struct switchstr
*switches
;
1649 static int n_switches
;
1657 /* Also a vector of input files specified. */
1659 static struct infile
*infiles
;
1661 static int n_infiles
;
1663 /* And a vector of corresponding output files is made up later. */
1665 static char **outfiles
;
1667 /* Create the vector `switches' and its contents.
1668 Store its length in `n_switches'. */
1671 process_command (argc
, argv
)
1677 char *spec_lang
= 0;
1678 int last_language_n_infiles
;
1682 spec_version
= version_string
;
1684 /* Set up the default search paths. */
1686 temp
= getenv ("GCC_EXEC_PREFIX");
1689 add_prefix (&exec_prefix
, temp
, 0, 0, 0);
1690 add_prefix (&startfile_prefix
, temp
, 0, 0, 0);
1693 /* COMPILER_PATH and LIBRARY_PATH have values
1694 that are lists of directory names with colons. */
1696 temp
= getenv ("COMPILER_PATH");
1699 char *startp
, *endp
;
1700 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1702 startp
= endp
= temp
;
1705 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1707 strncpy (nstore
, startp
, endp
-startp
);
1710 strcpy (nstore
, "./");
1712 else if (endp
[-1] != '/')
1714 nstore
[endp
-startp
] = '/';
1715 nstore
[endp
-startp
+1] = 0;
1718 nstore
[endp
-startp
] = 0;
1719 add_prefix (&exec_prefix
, nstore
, 0, 0, 0);
1722 endp
= startp
= endp
+ 1;
1729 temp
= getenv ("LIBRARY_PATH");
1732 char *startp
, *endp
;
1733 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1735 startp
= endp
= temp
;
1738 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1740 strncpy (nstore
, startp
, endp
-startp
);
1743 strcpy (nstore
, "./");
1745 else if (endp
[-1] != '/')
1747 nstore
[endp
-startp
] = '/';
1748 nstore
[endp
-startp
+1] = 0;
1751 nstore
[endp
-startp
] = 0;
1752 add_prefix (&startfile_prefix
, nstore
, 0, 0, 0);
1753 /* Make separate list of dirs that came from LIBRARY_PATH. */
1754 add_prefix (&library_prefix
, nstore
, 0, 0, 0);
1757 endp
= startp
= endp
+ 1;
1764 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1765 temp
= getenv ("LPATH");
1768 char *startp
, *endp
;
1769 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1771 startp
= endp
= temp
;
1774 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1776 strncpy (nstore
, startp
, endp
-startp
);
1779 strcpy (nstore
, "./");
1781 else if (endp
[-1] != '/')
1783 nstore
[endp
-startp
] = '/';
1784 nstore
[endp
-startp
+1] = 0;
1787 nstore
[endp
-startp
] = 0;
1788 add_prefix (&startfile_prefix
, nstore
, 0, 0, 0);
1789 /* Make separate list of dirs that came from LIBRARY_PATH. */
1790 add_prefix (&library_prefix
, nstore
, 0, 0, 0);
1793 endp
= startp
= endp
+ 1;
1800 /* Scan argv twice. Here, the first time, just count how many switches
1801 there will be in their vector, and how many input files in theirs.
1802 Here we also parse the switches that cc itself uses (e.g. -v). */
1804 for (i
= 1; i
< argc
; i
++)
1806 if (! strcmp (argv
[i
], "-dumpspecs"))
1808 printf ("*asm:\n%s\n\n", asm_spec
);
1809 printf ("*asm_final:\n%s\n\n", asm_final_spec
);
1810 printf ("*cpp:\n%s\n\n", cpp_spec
);
1811 printf ("*cc1:\n%s\n\n", cc1_spec
);
1812 printf ("*cc1plus:\n%s\n\n", cc1plus_spec
);
1813 printf ("*endfile:\n%s\n\n", endfile_spec
);
1814 printf ("*link:\n%s\n\n", link_spec
);
1815 printf ("*lib:\n%s\n\n", lib_spec
);
1816 printf ("*startfile:\n%s\n\n", startfile_spec
);
1817 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces
);
1818 printf ("*signed_char:\n%s\n\n", signed_char_spec
);
1819 printf ("*predefines:\n%s\n\n", cpp_predefines
);
1820 printf ("*cross_compile:\n%d\n\n", cross_compile
);
1824 else if (! strcmp (argv
[i
], "-dumpversion"))
1826 printf ("%s\n", version_string
);
1829 else if (! strcmp (argv
[i
], "-Xlinker"))
1831 /* Pass the argument of this option to the linker when we link. */
1834 fatal ("argument to `-Xlinker' is missing");
1837 if (!linker_options
)
1839 = (char **) xmalloc (n_linker_options
* sizeof (char **));
1842 = (char **) xrealloc (linker_options
,
1843 n_linker_options
* sizeof (char **));
1845 linker_options
[n_linker_options
- 1] = argv
[++i
];
1847 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
1849 register char *p
= &argv
[i
][1];
1850 register int c
= *p
;
1855 if (p
[1] == 0 && i
+ 1 == argc
)
1856 fatal ("argument to `-b' is missing");
1858 spec_machine
= argv
[++i
];
1860 spec_machine
= p
+ 1;
1865 int *temp
= (int *) xmalloc (sizeof (int));
1867 if (p
[1] == 0 && i
+ 1 == argc
)
1868 fatal ("argument to `-B' is missing");
1873 add_prefix (&exec_prefix
, value
, 1, 0, temp
);
1874 add_prefix (&startfile_prefix
, value
, 1, 0, temp
);
1878 case 'v': /* Print our subcommands and print versions. */
1884 if (p
[1] == 0 && i
+ 1 == argc
)
1885 fatal ("argument to `-V' is missing");
1887 spec_version
= argv
[++i
];
1889 spec_version
= p
+ 1;
1893 if (!strcmp (p
, "save-temps"))
1895 save_temps_flag
= 1;
1901 if (SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
1902 i
+= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
1903 else if (WORD_SWITCH_TAKES_ARG (p
))
1904 i
+= WORD_SWITCH_TAKES_ARG (p
);
1911 /* Set up the search paths before we go looking for config files. */
1913 /* These come before the md prefixes so that we will find gcc's subcommands
1914 (such as cpp) rather than those of the host system. */
1915 add_prefix (&exec_prefix
, standard_exec_prefix
, 0, 1, 0);
1916 add_prefix (&exec_prefix
, standard_exec_prefix_1
, 0, 1, 0);
1918 add_prefix (&startfile_prefix
, standard_exec_prefix
, 0, 1, 0);
1919 add_prefix (&startfile_prefix
, standard_exec_prefix_1
, 0, 1, 0);
1921 /* More prefixes are enabled in main, after we read the specs file
1922 and determine whether this is cross-compilation or not. */
1925 /* Then create the space for the vectors and scan again. */
1927 switches
= ((struct switchstr
*)
1928 xmalloc ((n_switches
+ 1) * sizeof (struct switchstr
)));
1929 infiles
= (struct infile
*) xmalloc ((n_infiles
+ 1) * sizeof (struct infile
));
1932 last_language_n_infiles
= -1;
1934 /* This, time, copy the text of each switch and store a pointer
1935 to the copy in the vector of switches.
1936 Store all the infiles in their vector. */
1938 for (i
= 1; i
< argc
; i
++)
1940 if (!strcmp (argv
[i
], "-Xlinker"))
1942 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
1944 register char *p
= &argv
[i
][1];
1945 register int c
= *p
;
1947 if (c
== 'B' || c
== 'b' || c
== 'V')
1949 /* Skip a separate arg, if any. */
1956 if (p
[1] == 0 && i
+ 1 == argc
)
1957 fatal ("argument to `-x' is missing");
1959 spec_lang
= argv
[++i
];
1962 if (! strcmp (spec_lang
, "none"))
1963 /* Suppress the warning if -xnone comes after the last input file,
1964 because alternate command interfaces like g++ might find it
1965 useful to place -xnone after each input file. */
1968 last_language_n_infiles
= n_infiles
;
1971 switches
[n_switches
].part1
= p
;
1972 /* Deal with option arguments in separate argv elements. */
1973 if ((SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
1974 || WORD_SWITCH_TAKES_ARG (p
)) {
1976 int n_args
= WORD_SWITCH_TAKES_ARG (p
);
1979 /* Count only the option arguments in separate argv elements. */
1980 n_args
= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
1982 switches
[n_switches
].args
1983 = (char **) xmalloc ((n_args
+ 1) * sizeof (char *));
1985 switches
[n_switches
].args
[j
++] = argv
[++i
];
1986 /* Null-terminate the vector. */
1987 switches
[n_switches
].args
[j
] = 0;
1988 } else if (*switches_need_spaces
!= 0 && (c
== 'o' || c
== 'L')) {
1989 /* On some systems, ld cannot handle -o or -L without space.
1990 So split the -o or -L from its argument. */
1991 switches
[n_switches
].part1
= (c
== 'o' ? "o" : "L");
1992 switches
[n_switches
].args
= (char **) xmalloc (2 * sizeof (char *));
1993 switches
[n_switches
].args
[0] = xmalloc (strlen (p
));
1994 strcpy (switches
[n_switches
].args
[0], &p
[1]);
1995 switches
[n_switches
].args
[1] = 0;
1997 switches
[n_switches
].args
= 0;
1998 switches
[n_switches
].valid
= 0;
1999 /* This is always valid, since gcc.c itself understands it. */
2000 if (!strcmp (p
, "save-temps"))
2001 switches
[n_switches
].valid
= 1;
2006 infiles
[n_infiles
].language
= spec_lang
;
2007 infiles
[n_infiles
++].name
= argv
[i
];
2011 if (n_infiles
== last_language_n_infiles
)
2012 error ("Warning: `-x %s' after last input file has no effect", spec_lang
);
2014 switches
[n_switches
].part1
= 0;
2015 infiles
[n_infiles
].name
= 0;
2018 /* Process a spec string, accumulating and running commands. */
2020 /* These variables describe the input file name.
2021 input_file_number is the index on outfiles of this file,
2022 so that the output file name can be stored for later use by %o.
2023 input_basename is the start of the part of the input file
2024 sans all directory names, and basename_length is the number
2025 of characters starting there excluding the suffix .c or whatever. */
2027 static char *input_filename
;
2028 static int input_file_number
;
2029 static int input_filename_length
;
2030 static int basename_length
;
2031 static char *input_basename
;
2032 static char *input_suffix
;
2034 /* These are variables used within do_spec and do_spec_1. */
2036 /* Nonzero if an arg has been started and not yet terminated
2037 (with space, tab or newline). */
2038 static int arg_going
;
2040 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2041 is a temporary file name. */
2042 static int delete_this_arg
;
2044 /* Nonzero means %w has been seen; the next arg to be terminated
2045 is the output file name of this compilation. */
2046 static int this_is_output_file
;
2048 /* Nonzero means %s has been seen; the next arg to be terminated
2049 is the name of a library file and we should try the standard
2050 search dirs for it. */
2051 static int this_is_library_file
;
2053 /* Process the spec SPEC and run the commands specified therein.
2054 Returns 0 if the spec is successfully processed; -1 if failed. */
2064 delete_this_arg
= 0;
2065 this_is_output_file
= 0;
2066 this_is_library_file
= 0;
2068 value
= do_spec_1 (spec
, 0, NULL
);
2070 /* Force out any unfinished command.
2071 If -pipe, this forces out the last command if it ended in `|'. */
2074 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
2077 if (argbuf_index
> 0)
2084 /* Process the sub-spec SPEC as a portion of a larger spec.
2085 This is like processing a whole spec except that we do
2086 not initialize at the beginning and we do not supply a
2087 newline by default at the end.
2088 INSWITCH nonzero means don't process %-sequences in SPEC;
2089 in this case, % is treated as an ordinary character.
2090 This is used while substituting switches.
2091 INSWITCH nonzero also causes SPC not to terminate an argument.
2093 Value is zero unless a line was finished
2094 and the command on that line reported an error. */
2097 do_spec_1 (spec
, inswitch
, soft_matched_part
)
2100 char *soft_matched_part
;
2102 register char *p
= spec
;
2108 /* If substituting a switch, treat all chars like letters.
2109 Otherwise, NL, SPC, TAB and % are special. */
2110 switch (inswitch
? 'a' : c
)
2113 /* End of line: finish any pending argument,
2114 then run the pending command if one has been started. */
2117 obstack_1grow (&obstack
, 0);
2118 string
= obstack_finish (&obstack
);
2119 if (this_is_library_file
)
2120 string
= find_file (string
);
2121 store_arg (string
, delete_this_arg
, this_is_output_file
);
2122 if (this_is_output_file
)
2123 outfiles
[input_file_number
] = string
;
2127 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
2130 for (i
= 0; i
< n_switches
; i
++)
2131 if (!strcmp (switches
[i
].part1
, "pipe"))
2134 /* A `|' before the newline means use a pipe here,
2135 but only if -pipe was specified.
2136 Otherwise, execute now and don't pass the `|' as an arg. */
2139 switches
[i
].valid
= 1;
2146 if (argbuf_index
> 0)
2148 int value
= execute ();
2152 /* Reinitialize for a new command, and for a new argument. */
2155 delete_this_arg
= 0;
2156 this_is_output_file
= 0;
2157 this_is_library_file
= 0;
2161 /* End any pending argument. */
2164 obstack_1grow (&obstack
, 0);
2165 string
= obstack_finish (&obstack
);
2166 if (this_is_library_file
)
2167 string
= find_file (string
);
2168 store_arg (string
, delete_this_arg
, this_is_output_file
);
2169 if (this_is_output_file
)
2170 outfiles
[input_file_number
] = string
;
2174 obstack_1grow (&obstack
, c
);
2180 /* Space or tab ends an argument if one is pending. */
2183 obstack_1grow (&obstack
, 0);
2184 string
= obstack_finish (&obstack
);
2185 if (this_is_library_file
)
2186 string
= find_file (string
);
2187 store_arg (string
, delete_this_arg
, this_is_output_file
);
2188 if (this_is_output_file
)
2189 outfiles
[input_file_number
] = string
;
2191 /* Reinitialize for a new argument. */
2193 delete_this_arg
= 0;
2194 this_is_output_file
= 0;
2195 this_is_library_file
= 0;
2202 fatal ("Invalid specification! Bug in cc.");
2205 obstack_grow (&obstack
, input_basename
, basename_length
);
2210 delete_this_arg
= 2;
2213 /* Dump out the directories specified with LIBRARY_PATH,
2214 followed by the absolute directories
2215 that we search for startfiles. */
2217 for (i
= 0; i
< 2; i
++)
2219 struct prefix_list
*pl
2220 = (i
== 0 ? library_prefix
.plist
: startfile_prefix
.plist
);
2222 char *buffer
= (char *) xmalloc (bufsize
);
2225 for (; pl
; pl
= pl
->next
)
2227 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2228 /* Used on systems which record the specified -L dirs
2229 and use them to search for dynamic linking. */
2230 /* Relative directories always come from -B,
2231 and it is better not to use them for searching
2232 at run time. In particular, stage1 loses */
2233 if (pl
->prefix
[0] != '/')
2238 if (is_linker_dir (pl
->prefix
, machine_suffix
))
2240 do_spec_1 ("-L", 0, 0);
2241 #ifdef SPACE_AFTER_L_OPTION
2242 do_spec_1 (" ", 0, 0);
2244 do_spec_1 (pl
->prefix
, 1, 0);
2245 /* Remove slash from machine_suffix. */
2246 if (strlen (machine_suffix
) >= bufsize
)
2247 bufsize
= strlen (machine_suffix
) * 2 + 1;
2248 buffer
= (char *) xrealloc (buffer
, bufsize
);
2249 strcpy (buffer
, machine_suffix
);
2250 idx
= strlen (buffer
);
2251 if (buffer
[idx
- 1] == '/')
2252 buffer
[idx
- 1] = 0;
2253 do_spec_1 (buffer
, 1, 0);
2254 /* Make this a separate argument. */
2255 do_spec_1 (" ", 0, 0);
2258 if (!pl
->require_machine_suffix
)
2260 if (is_linker_dir (pl
->prefix
, ""))
2262 do_spec_1 ("-L", 0, 0);
2263 #ifdef SPACE_AFTER_L_OPTION
2264 do_spec_1 (" ", 0, 0);
2266 /* Remove slash from pl->prefix. */
2267 if (strlen (pl
->prefix
) >= bufsize
)
2268 bufsize
= strlen (pl
->prefix
) * 2 + 1;
2269 buffer
= (char *) xrealloc (buffer
, bufsize
);
2270 strcpy (buffer
, pl
->prefix
);
2271 idx
= strlen (buffer
);
2272 if (buffer
[idx
- 1] == '/')
2273 buffer
[idx
- 1] = 0;
2274 do_spec_1 (buffer
, 1, 0);
2275 /* Make this a separate argument. */
2276 do_spec_1 (" ", 0, 0);
2285 /* {...:%efoo} means report an error with `foo' as error message
2286 and don't execute any more commands for this file. */
2290 while (*p
!= 0 && *p
!= '\n') p
++;
2291 buf
= (char *) alloca (p
- q
+ 1);
2292 strncpy (buf
, q
, p
- q
);
2300 if (save_temps_flag
)
2301 obstack_grow (&obstack
, input_basename
, basename_length
);
2304 obstack_grow (&obstack
, temp_filename
, temp_filename_length
);
2305 delete_this_arg
= 1;
2311 obstack_grow (&obstack
, input_filename
, input_filename_length
);
2318 for (f
= 0; f
< n_infiles
; f
++)
2319 store_arg (outfiles
[f
], 0, 0);
2324 this_is_library_file
= 1;
2328 this_is_output_file
= 1;
2333 int index
= argbuf_index
;
2334 /* Handle the {...} following the %W. */
2337 p
= handle_braces (p
+ 1);
2340 /* If any args were output, mark the last one for deletion
2342 if (argbuf_index
!= index
)
2343 record_temp_file (argbuf
[argbuf_index
- 1], 0, 1);
2347 /* %x{OPTION} records OPTION for %X to output. */
2353 /* Skip past the option value and make a copy. */
2358 string
= save_string (p1
+ 1, p
- p1
- 2);
2360 /* See if we already recorded this option. */
2361 for (i
= 0; i
< n_linker_options
; i
++)
2362 if (! strcmp (string
, linker_options
[i
]))
2368 /* This option is new; add it. */
2370 if (!linker_options
)
2372 = (char **) xmalloc (n_linker_options
* sizeof (char **));
2375 = (char **) xrealloc (linker_options
,
2376 n_linker_options
* sizeof (char **));
2378 linker_options
[n_linker_options
- 1] = string
;
2382 /* Dump out the options accumulated previously using %x. */
2384 for (i
= 0; i
< n_linker_options
; i
++)
2386 do_spec_1 (linker_options
[i
], 1, NULL
);
2387 /* Make each accumulated option a separate argument. */
2388 do_spec_1 (" ", 0, NULL
);
2392 /* Here are digits and numbers that just process
2393 a certain constant string as a spec. */
2396 do_spec_1 (cc1_spec
, 0, NULL
);
2400 do_spec_1 (cc1plus_spec
, 0, NULL
);
2404 do_spec_1 (asm_spec
, 0, NULL
);
2408 do_spec_1 (asm_final_spec
, 0, NULL
);
2412 do_spec_1 (signed_char_spec
, 0, NULL
);
2416 do_spec_1 (cpp_spec
, 0, NULL
);
2420 do_spec_1 (endfile_spec
, 0, NULL
);
2424 do_spec_1 (link_spec
, 0, NULL
);
2428 do_spec_1 (lib_spec
, 0, NULL
);
2433 char *x
= (char *) alloca (strlen (cpp_predefines
) + 1);
2437 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2441 if (! strncmp (y
, "-D", 2))
2442 /* Copy the whole option. */
2443 while (*y
&& *y
!= ' ' && *y
!= '\t')
2445 else if (*y
== ' ' || *y
== '\t')
2446 /* Copy whitespace to the result. */
2448 /* Don't copy other options. */
2455 do_spec_1 (buf
, 0, NULL
);
2461 char *x
= (char *) alloca (strlen (cpp_predefines
) * 4 + 1);
2465 /* Copy all of CPP_PREDEFINES into BUF,
2466 but put __ after every -D and at the end of each arg. */
2470 if (! strncmp (y
, "-D", 2))
2477 if (strncmp (y
, "__", 2))
2479 /* Stick __ at front of macro name. */
2482 /* Arrange to stick __ at the end as well. */
2486 /* Copy the macro name. */
2487 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2496 /* Copy the value given, if any. */
2497 while (*y
&& *y
!= ' ' && *y
!= '\t')
2500 else if (*y
== ' ' || *y
== '\t')
2501 /* Copy whitespace to the result. */
2503 /* Don't copy -A options */
2509 /* Copy all of CPP_PREDEFINES into BUF,
2510 but put __ after every -D. */
2514 if (! strncmp (y
, "-D", 2))
2519 if (strncmp (y
, "__", 2))
2521 /* Stick __ at front of macro name. */
2526 /* Copy the macro name. */
2527 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2530 /* Copy the value given, if any. */
2531 while (*y
&& *y
!= ' ' && *y
!= '\t')
2534 else if (*y
== ' ' || *y
== '\t')
2535 /* Copy whitespace to the result. */
2537 /* Don't copy -A options */
2543 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2547 if (! strncmp (y
, "-A", 2))
2548 /* Copy the whole option. */
2549 while (*y
&& *y
!= ' ' && *y
!= '\t')
2551 else if (*y
== ' ' || *y
== '\t')
2552 /* Copy whitespace to the result. */
2554 /* Don't copy other options. */
2561 do_spec_1 (buf
, 0, NULL
);
2566 do_spec_1 (startfile_spec
, 0, NULL
);
2569 /* Here we define characters other than letters and digits. */
2572 p
= handle_braces (p
);
2578 obstack_1grow (&obstack
, '%');
2582 do_spec_1 (soft_matched_part
, 1, NULL
);
2583 do_spec_1 (" ", 0, NULL
);
2586 /* Process a string found as the value of a spec given by name.
2587 This feature allows individual machine descriptions
2588 to add and use their own specs.
2589 %[...] modifies -D options the way %P does;
2590 %(...) uses the spec unmodified. */
2595 struct spec_list
*sl
;
2598 /* The string after the S/P is the name of a spec that is to be
2600 while (*p
&& *p
!= ')' && *p
!= ']')
2603 /* See if it's in the list */
2604 for (len
= p
- name
, sl
= specs
; sl
; sl
= sl
->next
)
2605 if (strncmp (sl
->name
, name
, len
) == 0 && !sl
->name
[len
])
2614 do_spec_1 (name
, 0, NULL
);
2617 char *x
= (char *) alloca (strlen (name
) * 2 + 1);
2621 /* Copy all of NAME into BUF, but put __ after
2622 every -D and at the end of each arg, */
2625 if (! strncmp (y
, "-D", 2))
2633 else if (*y
== ' ' || *y
== 0)
2647 do_spec_1 (buf
, 0, NULL
);
2651 /* Discard the closing paren or bracket. */
2663 /* Backslash: treat next character as ordinary. */
2668 /* Ordinary character: put it into the current argument. */
2669 obstack_1grow (&obstack
, c
);
2673 return 0; /* End of string */
2676 /* Return 0 if we call do_spec_1 and that returns -1. */
2689 /* A `|' after the open-brace means,
2690 if the test fails, output a single minus sign rather than nothing.
2691 This is used in %{|!pipe:...}. */
2695 /* A `!' after the open-brace negates the condition:
2696 succeed if the specified switch is not present. */
2700 /* A `.' after the open-brace means test against the current suffix. */
2710 while (*p
!= ':' && *p
!= '}') p
++;
2713 register int count
= 1;
2731 int found
= (input_suffix
!= 0
2732 && strlen (input_suffix
) == p
- filter
2733 && strncmp (input_suffix
, filter
, p
- filter
) == 0);
2739 && do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL
) < 0)
2744 else if (p
[-1] == '*' && p
[0] == '}')
2746 /* Substitute all matching switches as separate args. */
2749 for (i
= 0; i
< n_switches
; i
++)
2750 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
2755 /* Test for presence of the specified switch. */
2759 /* If name specified ends in *, as in {x*:...},
2760 check for %* and handle that case. */
2761 if (p
[-1] == '*' && !negate
)
2766 /* First see whether we have %*. */
2770 if (*r
== '%' && r
[1] == '*')
2774 /* If we do, handle that case. */
2777 /* Substitute all matching switches as separate args.
2778 But do this by substituting for %*
2779 in the text that follows the colon. */
2781 unsigned hard_match_len
= p
- filter
- 1;
2782 char *string
= save_string (p
+ 1, q
- p
- 2);
2784 for (i
= 0; i
< n_switches
; i
++)
2785 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
2787 do_spec_1 (string
, 0, &switches
[i
].part1
[hard_match_len
]);
2788 /* Pass any arguments this switch has. */
2796 /* If name specified ends in *, as in {x*:...},
2797 check for presence of any switch name starting with x. */
2800 for (i
= 0; i
< n_switches
; i
++)
2802 unsigned hard_match_len
= p
- filter
- 1;
2804 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
2806 switches
[i
].valid
= 1;
2811 /* Otherwise, check for presence of exact name specified. */
2814 for (i
= 0; i
< n_switches
; i
++)
2816 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
2817 && switches
[i
].part1
[p
- filter
] == 0)
2819 switches
[i
].valid
= 1;
2826 /* If it is as desired (present for %{s...}, absent for %{-s...})
2827 then substitute either the switch or the specified
2828 conditional text. */
2829 if (present
!= negate
)
2837 if (do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL
) < 0)
2843 /* Here if a %{|...} conditional fails: output a minus sign,
2844 which means "standard output" or "standard input". */
2845 do_spec_1 ("-", 0, NULL
);
2852 /* Pass a switch to the current accumulating command
2853 in the same form that we received it.
2854 SWITCHNUM identifies the switch; it is an index into
2855 the vector of switches gcc received, which is `switches'.
2856 This cannot fail since it never finishes a command line.
2858 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2861 give_switch (switchnum
, omit_first_word
)
2863 int omit_first_word
;
2865 if (!omit_first_word
)
2867 do_spec_1 ("-", 0, NULL
);
2868 do_spec_1 (switches
[switchnum
].part1
, 1, NULL
);
2870 do_spec_1 (" ", 0, NULL
);
2871 if (switches
[switchnum
].args
!= 0)
2874 for (p
= switches
[switchnum
].args
; *p
; p
++)
2876 do_spec_1 (*p
, 1, NULL
);
2877 do_spec_1 (" ", 0, NULL
);
2880 switches
[switchnum
].valid
= 1;
2883 /* Search for a file named NAME trying various prefixes including the
2884 user's -B prefix and some standard ones.
2885 Return the absolute file name found. If nothing is found, return NAME. */
2893 newname
= find_a_file (&startfile_prefix
, name
, R_OK
);
2894 return newname
? newname
: name
;
2897 /* Determine whether a -L option is relevant. Not required for certain
2898 fixed names and for directories that don't exist. */
2901 is_linker_dir (path1
, path2
)
2905 int len1
= strlen (path1
);
2906 int len2
= strlen (path2
);
2907 char *path
= (char *) alloca (3 + len1
+ len2
);
2911 /* Construct the path from the two parts. Ensure the string ends with "/.".
2912 The resulting path will be a directory even if the given path is a
2914 bcopy (path1
, path
, len1
);
2915 bcopy (path2
, path
+ len1
, len2
);
2916 cp
= path
+ len1
+ len2
;
2922 /* Exclude directories that the linker is known to search. */
2923 if ((cp
- path
== 6 && strcmp (path
, "/lib/.") == 0)
2924 || (cp
- path
== 10 && strcmp (path
, "/usr/lib/.") == 0))
2927 return (stat (path
, &st
) >= 0 && S_ISDIR (st
.st_mode
));
2930 /* On fatal signals, delete all the temporary files. */
2933 fatal_error (signum
)
2936 signal (signum
, SIG_DFL
);
2937 delete_failure_queue ();
2938 delete_temp_files ();
2939 /* Get the same signal again, this time not handled,
2940 so its normal effect occurs. */
2941 kill (getpid (), signum
);
2951 int error_count
= 0;
2952 int linker_was_run
= 0;
2953 char *explicit_link_files
;
2956 programname
= argv
[0];
2958 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
2959 signal (SIGINT
, fatal_error
);
2960 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
2961 signal (SIGHUP
, fatal_error
);
2962 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
2963 signal (SIGTERM
, fatal_error
);
2965 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
2966 signal (SIGPIPE
, fatal_error
);
2970 argbuf
= (char **) xmalloc (argbuf_length
* sizeof (char *));
2972 obstack_init (&obstack
);
2974 /* Set up to remember the pathname of gcc and any options
2975 needed for collect. */
2976 obstack_init (&collect_obstack
);
2977 obstack_grow (&collect_obstack
, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
2978 obstack_grow (&collect_obstack
, programname
, strlen (programname
)+1);
2979 putenv (obstack_finish (&collect_obstack
));
2981 /* Choose directory for temp files. */
2983 choose_temp_base ();
2985 /* Make a table of what switches there are (switches, n_switches).
2986 Make a table of specified input files (infiles, n_infiles).
2987 Decode switches that are handled locally. */
2989 process_command (argc
, argv
);
2991 /* Initialize the vector of specs to just the default.
2992 This means one element containing 0s, as a terminator. */
2994 compilers
= (struct compiler
*) xmalloc (sizeof default_compilers
);
2995 bcopy (default_compilers
, compilers
, sizeof default_compilers
);
2996 n_compilers
= n_default_compilers
;
2998 /* Read specs from a file if there is one. */
3000 machine_suffix
= concat (spec_machine
, "/", concat (spec_version
, "/", ""));
3002 specs_file
= find_a_file (&startfile_prefix
, "specs", R_OK
);
3003 /* Read the specs file unless it is a default one. */
3004 if (specs_file
!= 0 && strcmp (specs_file
, "specs"))
3005 read_specs (specs_file
);
3007 /* If not cross-compiling, look for startfiles in the standard places. */
3008 /* The fact that these are done here, after reading the specs file,
3009 means that it cannot be found in these directories.
3010 But that's okay. It should never be there anyway. */
3013 #ifdef MD_EXEC_PREFIX
3014 add_prefix (&exec_prefix
, md_exec_prefix
, 0, 0, 0);
3015 add_prefix (&startfile_prefix
, md_exec_prefix
, 0, 0, 0);
3018 #ifdef MD_STARTFILE_PREFIX
3019 add_prefix (&startfile_prefix
, md_startfile_prefix
, 0, 0, 0);
3022 add_prefix (&startfile_prefix
, standard_startfile_prefix
, 0, 0, 0);
3023 add_prefix (&startfile_prefix
, standard_startfile_prefix_1
, 0, 0, 0);
3024 add_prefix (&startfile_prefix
, standard_startfile_prefix_2
, 0, 0, 0);
3025 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3026 add_prefix (&startfile_prefix
, "./", 0, 1, 0);
3030 /* Now we have the specs.
3031 Set the `valid' bits for switches that match anything in any spec. */
3033 validate_all_switches ();
3035 /* Warn about any switches that no pass was interested in. */
3037 for (i
= 0; i
< n_switches
; i
++)
3038 if (! switches
[i
].valid
)
3039 error ("unrecognized option `-%s'", switches
[i
].part1
);
3041 /* Obey some of the options. */
3045 fprintf (stderr
, "gcc version %s\n", version_string
);
3051 fatal ("No input files specified.");
3053 /* Make a place to record the compiler output file names
3054 that correspond to the input files. */
3056 outfiles
= (char **) xmalloc (n_infiles
* sizeof (char *));
3057 bzero (outfiles
, n_infiles
* sizeof (char *));
3059 /* Record which files were specified explicitly as link input. */
3061 explicit_link_files
= xmalloc (n_infiles
);
3062 bzero (explicit_link_files
, n_infiles
);
3064 for (i
= 0; i
< n_infiles
; i
++)
3066 register struct compiler
*cp
= 0;
3067 int this_file_error
= 0;
3069 /* Tell do_spec what to substitute for %i. */
3071 input_filename
= infiles
[i
].name
;
3072 input_filename_length
= strlen (input_filename
);
3073 input_file_number
= i
;
3075 /* Use the same thing in %o, unless cp->spec says otherwise. */
3077 outfiles
[i
] = input_filename
;
3079 /* Figure out which compiler from the file's suffix. */
3081 cp
= lookup_compiler (infiles
[i
].name
, input_filename_length
,
3082 infiles
[i
].language
);
3086 /* Ok, we found an applicable compiler. Run its spec. */
3087 /* First say how much of input_filename to substitute for %b */
3090 input_basename
= input_filename
;
3091 for (p
= input_filename
; *p
; p
++)
3093 input_basename
= p
+ 1;
3095 /* Find a suffix starting with the last period,
3096 and set basename_length to exclude that suffix. */
3097 basename_length
= strlen (input_basename
);
3098 p
= input_basename
+ basename_length
;
3099 while (p
!= input_basename
&& *p
!= '.') --p
;
3100 if (*p
== '.' && p
!= input_basename
)
3102 basename_length
= p
- input_basename
;
3103 input_suffix
= p
+ 1;
3108 value
= do_spec (cp
->spec
);
3110 this_file_error
= 1;
3113 /* If this file's name does not contain a recognized suffix,
3114 record it as explicit linker input. */
3117 explicit_link_files
[i
] = 1;
3119 /* Clear the delete-on-failure queue, deleting the files in it
3120 if this compilation failed. */
3122 if (this_file_error
)
3124 delete_failure_queue ();
3127 /* If this compilation succeeded, don't delete those files later. */
3128 clear_failure_queue ();
3131 /* Run ld to link all the compiler output files. */
3133 if (error_count
== 0)
3135 int tmp
= execution_count
;
3139 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3141 putenv_from_prefixes (&exec_prefix
, "COMPILER_PATH=");
3142 putenv_from_prefixes (&startfile_prefix
, "LIBRARY_PATH=");
3144 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3146 obstack_grow (&collect_obstack
, "COLLECT_GCC_OPTIONS=",
3147 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3150 for (i
= 0; i
< n_switches
; i
++)
3154 obstack_grow (&collect_obstack
, " ", 1);
3157 obstack_grow (&collect_obstack
, "-", 1);
3158 obstack_grow (&collect_obstack
, switches
[i
].part1
,
3159 strlen (switches
[i
].part1
));
3161 for (args
= switches
[i
].args
; args
&& *args
; args
++)
3163 obstack_grow (&collect_obstack
, " ", 1);
3164 obstack_grow (&collect_obstack
, *args
, strlen (*args
));
3167 obstack_grow (&collect_obstack
, "\0", 1);
3168 putenv (obstack_finish (&collect_obstack
));
3170 value
= do_spec (link_command_spec
);
3173 linker_was_run
= (tmp
!= execution_count
);
3176 /* Warn if a -B option was specified but the prefix was never used. */
3177 unused_prefix_warnings (&exec_prefix
);
3178 unused_prefix_warnings (&startfile_prefix
);
3180 /* If options said don't run linker,
3181 complain about input files to be given to the linker. */
3183 if (! linker_was_run
&& error_count
== 0)
3184 for (i
= 0; i
< n_infiles
; i
++)
3185 if (explicit_link_files
[i
])
3186 error ("%s: linker input file unused since linking not done",
3189 /* Delete some or all of the temporary files we made. */
3192 delete_failure_queue ();
3193 delete_temp_files ();
3200 /* Find the proper compilation spec for the file name NAME,
3201 whose length is LENGTH. LANGUAGE is the specified language,
3202 or 0 if none specified. */
3204 static struct compiler
*
3205 lookup_compiler (name
, length
, language
)
3210 struct compiler
*cp
;
3212 /* Look for the language, if one is spec'd. */
3215 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3219 if (cp
->suffix
[0] == '@'
3220 && !strcmp (cp
->suffix
+ 1, language
))
3224 error ("language %s not recognized", language
);
3227 /* Look for a suffix. */
3228 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3230 if (strlen (cp
->suffix
) < length
3231 /* See if the suffix matches the end of NAME. */
3232 && !strcmp (cp
->suffix
,
3233 name
+ length
- strlen (cp
->suffix
))
3234 /* The suffix `-' matches only the file name `-'. */
3235 && !(!strcmp (cp
->suffix
, "-") && length
!= 1))
3237 if (cp
->spec
[0] == '@')
3239 struct compiler
*new;
3240 /* An alias entry maps a suffix to a language.
3241 Search for the language; pass 0 for NAME and LENGTH
3242 to avoid infinite recursion if language not found.
3243 Construct the new compiler spec. */
3244 language
= cp
->spec
+ 1;
3245 new = (struct compiler
*) xmalloc (sizeof (struct compiler
));
3246 new->suffix
= cp
->suffix
;
3247 new->spec
= lookup_compiler (0, 0, language
)->spec
;
3250 /* A non-alias entry: return it. */
3262 register char *value
= (char *) malloc (size
);
3264 fatal ("virtual memory exhausted");
3269 xrealloc (ptr
, size
)
3273 register char *value
= (char *) realloc (ptr
, size
);
3275 fatal ("virtual memory exhausted");
3279 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3285 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3286 char *result
= xmalloc (len1
+ len2
+ len3
+ 1);
3288 strcpy (result
, s1
);
3289 strcpy (result
+ len1
, s2
);
3290 strcpy (result
+ len1
+ len2
, s3
);
3291 *(result
+ len1
+ len2
+ len3
) = 0;
3297 save_string (s
, len
)
3301 register char *result
= xmalloc (len
+ 1);
3303 bcopy (s
, result
, len
);
3309 pfatal_with_name (name
)
3314 if (errno
< sys_nerr
)
3315 s
= concat ("%s: ", sys_errlist
[errno
], "");
3317 s
= "cannot open %s";
3322 perror_with_name (name
)
3327 if (errno
< sys_nerr
)
3328 s
= concat ("%s: ", sys_errlist
[errno
], "");
3330 s
= "cannot open %s";
3340 if (errno
< sys_nerr
)
3341 s
= concat ("installation problem, cannot exec %s: ",
3342 sys_errlist
[errno
], "");
3344 s
= "installation problem, cannot exec %s";
3348 /* More 'friendly' abort that prints the line and file.
3349 config.h can #define abort fancy_abort if you like that sort of thing. */
3354 fatal ("Internal gcc abort.");
3359 /* Output an error message and exit */
3369 format
= va_arg (ap
, char *);
3370 fprintf (stderr
, "%s: ", programname
);
3371 vfprintf (stderr
, format
, ap
);
3373 fprintf (stderr
, "\n");
3374 delete_temp_files ();
3386 format
= va_arg (ap
, char *);
3387 fprintf (stderr
, "%s: ", programname
);
3388 vfprintf (stderr
, format
, ap
);
3391 fprintf (stderr
, "\n");
3394 #else /* not HAVE_VPRINTF */
3397 fatal (msg
, arg1
, arg2
)
3398 char *msg
, *arg1
, *arg2
;
3400 error (msg
, arg1
, arg2
);
3401 delete_temp_files ();
3406 error (msg
, arg1
, arg2
)
3407 char *msg
, *arg1
, *arg2
;
3409 fprintf (stderr
, "%s: ", programname
);
3410 fprintf (stderr
, msg
, arg1
, arg2
);
3411 fprintf (stderr
, "\n");
3414 #endif /* not HAVE_VPRINTF */
3418 validate_all_switches ()
3420 struct compiler
*comp
;
3423 struct spec_list
*spec
;
3425 for (comp
= compilers
; comp
->spec
; comp
++)
3429 if (c
== '%' && *p
== '{')
3430 /* We have a switch spec. */
3431 validate_switches (p
+ 1);
3434 /* look through the linked list of extra specs read from the specs file */
3435 for (spec
= specs
; spec
; spec
= spec
->next
)
3439 if (c
== '%' && *p
== '{')
3440 /* We have a switch spec. */
3441 validate_switches (p
+ 1);
3444 p
= link_command_spec
;
3446 if (c
== '%' && *p
== '{')
3447 /* We have a switch spec. */
3448 validate_switches (p
+ 1);
3450 /* Now notice switches mentioned in the machine-specific specs. */
3454 if (c
== '%' && *p
== '{')
3455 /* We have a switch spec. */
3456 validate_switches (p
+ 1);
3460 if (c
== '%' && *p
== '{')
3461 /* We have a switch spec. */
3462 validate_switches (p
+ 1);
3466 if (c
== '%' && *p
== '{')
3467 /* We have a switch spec. */
3468 validate_switches (p
+ 1);
3470 p
= signed_char_spec
;
3472 if (c
== '%' && *p
== '{')
3473 /* We have a switch spec. */
3474 validate_switches (p
+ 1);
3478 if (c
== '%' && *p
== '{')
3479 /* We have a switch spec. */
3480 validate_switches (p
+ 1);
3484 if (c
== '%' && *p
== '{')
3485 /* We have a switch spec. */
3486 validate_switches (p
+ 1);
3490 if (c
== '%' && *p
== '{')
3491 /* We have a switch spec. */
3492 validate_switches (p
+ 1);
3496 if (c
== '%' && *p
== '{')
3497 /* We have a switch spec. */
3498 validate_switches (p
+ 1);
3502 if (c
== '%' && *p
== '{')
3503 /* We have a switch spec. */
3504 validate_switches (p
+ 1);
3507 /* Look at the switch-name that comes after START
3508 and mark as valid all supplied switches that match it. */
3511 validate_switches (start
)
3514 register char *p
= start
;
3529 while (*p
!= ':' && *p
!= '}') p
++;
3533 else if (p
[-1] == '*')
3535 /* Mark all matching switches as valid. */
3537 for (i
= 0; i
< n_switches
; i
++)
3538 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
3539 switches
[i
].valid
= 1;
3543 /* Mark an exact matching switch as valid. */
3544 for (i
= 0; i
< n_switches
; i
++)
3546 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
3547 && switches
[i
].part1
[p
- filter
] == 0)
3548 switches
[i
].valid
= 1;