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>
54 /* On MSDOS, write temp files in current dir
55 because there's no place else we can expect to use. */
62 /* Test if something is a normal file. */
64 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
67 /* Test if something is a directory. */
69 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
72 /* By default there is no special suffix for executables. */
73 #ifndef EXECUTABLE_SUFFIX
75 #define EXECUTABLE_SUFFIX ".exe"
77 #define EXECUTABLE_SUFFIX ""
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 extern char *version_string
;
129 static void set_spec ();
130 static struct compiler
*lookup_compiler ();
131 static char *find_a_file ();
132 static void add_prefix ();
133 static char *skip_whitespace ();
134 static void record_temp_file ();
135 static char *handle_braces ();
136 static char *save_string ();
137 static char *concat ();
138 static int do_spec ();
139 static int do_spec_1 ();
140 static char *find_file ();
141 static int is_linker_dir ();
142 static void validate_switches ();
143 static void validate_all_switches ();
144 static void give_switch ();
145 static void pfatal_with_name ();
146 static void perror_with_name ();
147 static void perror_exec ();
148 static void fatal ();
149 static void error ();
154 /* Specs are strings containing lines, each of which (if not blank)
155 is made up of a program name, and arguments separated by spaces.
156 The program name must be exact and start from root, since no path
157 is searched and it is unreliable to depend on the current working directory.
158 Redirection of input or output is not supported; the subprograms must
159 accept filenames saying what files to read and write.
161 In addition, the specs can contain %-sequences to substitute variable text
162 or for conditional text. Here is a table of all defined %-sequences.
163 Note that spaces are not generated automatically around the results of
164 expanding these sequences; therefore, you can concatenate them together
165 or with constant text in a single argument.
167 %% substitute one % into the program name or argument.
168 %i substitute the name of the input file being processed.
169 %b substitute the basename of the input file being processed.
170 This is the substring up to (and not including) the last period
171 and not including the directory.
172 %g substitute the temporary-file-name-base. This is a string chosen
173 once per compilation. Different temporary file names are made by
174 concatenation of constant strings on the end, as in `%g.s'.
175 %g also has the same effect of %d.
176 %d marks the argument containing or following the %d as a
177 temporary file name, so that that file will be deleted if CC exits
178 successfully. Unlike %g, this contributes no text to the argument.
179 %w marks the argument containing or following the %w as the
180 "output file" of this compilation. This puts the argument
181 into the sequence of arguments that %o will substitute later.
183 like %{...} but mark last argument supplied within
184 as a file to be deleted on failure.
185 %o substitutes the names of all the output files, with spaces
186 automatically placed around them. You should write spaces
187 around the %o as well or the results are undefined.
188 %o is for use in the specs for running the linker.
189 Input files whose names have no recognized suffix are not compiled
190 at all, but they are included among the output files, so they will
192 %p substitutes the standard macro predefinitions for the
193 current target machine. Use this when running cpp.
194 %P like %p, but puts `__' before and after the name of each macro.
195 (Except macros that already have __.)
197 %s current argument is the name of a library or startup file of some sort.
198 Search for that file in a standard list of directories
199 and substitute the full name found.
200 %eSTR Print STR as an error message. STR is terminated by a newline.
201 Use this when inconsistent options are detected.
202 %x{OPTION} Accumulate an option for %X.
203 %X Output the accumulated linker options specified by compilations.
204 %a process ASM_SPEC as a spec.
205 This allows config.h to specify part of the spec for running as.
206 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
207 used here. This can be used to run a post-processor after the
208 assembler has done it's job.
209 %D Dump out a -L option for each directory in library_prefix,
210 followed by a -L option for each directory in startfile_prefix.
211 %l process LINK_SPEC as a spec.
212 %L process LIB_SPEC as a spec.
213 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
214 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
215 %c process SIGNED_CHAR_SPEC as a spec.
216 %C process CPP_SPEC as a spec. A capital C is actually used here.
217 %1 process CC1_SPEC as a spec.
218 %2 process CC1PLUS_SPEC as a spec.
219 %* substitute the variable part of a matched option. (See below.)
220 Note that each comma in the substituted string is replaced by
222 %{S} substitutes the -S switch, if that switch was given to CC.
223 If that switch was not specified, this substitutes nothing.
224 Here S is a metasyntactic variable.
225 %{S*} substitutes all the switches specified to CC whose names start
226 with -S. This is used for -o, -D, -I, etc; switches that take
227 arguments. CC considers `-o foo' as being one switch whose
228 name starts with `o'. %{o*} would substitute this text,
229 including the space; thus, two arguments would be generated.
230 %{S*:X} substitutes X if one or more switches whose names with -S are
231 specified to CC. Note that the tail part of the -S option
232 (i.e. the part matched by the `*') will be substituted for each
233 occurance of %* within X.
234 %{S:X} substitutes X, but only if the -S switch was given to CC.
235 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
236 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
237 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
238 %{.S:X} substitutes X, but only if processing a file with suffix S.
239 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
241 The conditional text X in a %{S:X} or %{!S:X} construct may contain
242 other nested % constructs or spaces, or even newlines. They are
243 processed as usual, as described above.
245 The character | is used to indicate that a command should be piped to
246 the following command, but only if -pipe is specified.
248 Note that it is built into CC which switches take arguments and which
249 do not. You might think it would be useful to generalize this to
250 allow each compiler's spec to say which switches take arguments. But
251 this cannot be done in a consistent fashion. CC cannot even decide
252 which input files have been specified without knowing which switches
253 take arguments, and it must know which input files to compile in order
254 to tell which compilers to run.
256 CC also knows implicitly that arguments starting in `-l' are to be
257 treated as compiler output files, and passed to the linker in their
258 proper position among the other output files. */
260 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
262 /* config.h can define ASM_SPEC to provide extra args to the assembler
263 or extra switch-translations. */
268 /* config.h can define ASM_FINAL_SPEC to run a post processor after
269 the assembler has run. */
270 #ifndef ASM_FINAL_SPEC
271 #define ASM_FINAL_SPEC ""
274 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
275 or extra switch-translations. */
280 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
281 or extra switch-translations. */
286 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
287 or extra switch-translations. */
289 #define CC1PLUS_SPEC ""
292 /* config.h can define LINK_SPEC to provide extra args to the linker
293 or extra switch-translations. */
298 /* config.h can define LIB_SPEC to override the default libraries. */
300 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
303 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
304 #ifndef STARTFILE_SPEC
305 #define STARTFILE_SPEC \
306 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
309 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
310 Make the string nonempty to require spaces there. */
311 #ifndef SWITCHES_NEED_SPACES
312 #define SWITCHES_NEED_SPACES ""
315 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
317 #define ENDFILE_SPEC ""
320 /* This spec is used for telling cpp whether char is signed or not. */
321 #ifndef SIGNED_CHAR_SPEC
322 #define SIGNED_CHAR_SPEC \
323 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
324 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
327 static char *cpp_spec
= CPP_SPEC
;
328 static char *cpp_predefines
= CPP_PREDEFINES
;
329 static char *cc1_spec
= CC1_SPEC
;
330 static char *cc1plus_spec
= CC1PLUS_SPEC
;
331 static char *signed_char_spec
= SIGNED_CHAR_SPEC
;
332 static char *asm_spec
= ASM_SPEC
;
333 static char *asm_final_spec
= ASM_FINAL_SPEC
;
334 static char *link_spec
= LINK_SPEC
;
335 static char *lib_spec
= LIB_SPEC
;
336 static char *endfile_spec
= ENDFILE_SPEC
;
337 static char *startfile_spec
= STARTFILE_SPEC
;
338 static char *switches_need_spaces
= SWITCHES_NEED_SPACES
;
340 /* This defines which switch letters take arguments. */
342 #ifndef SWITCH_TAKES_ARG
343 #define SWITCH_TAKES_ARG(CHAR) \
344 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
345 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
346 || (CHAR) == 'I' || (CHAR) == 'm' \
347 || (CHAR) == 'L' || (CHAR) == 'A')
350 /* This defines which multi-letter switches take arguments. */
352 #ifndef WORD_SWITCH_TAKES_ARG
353 #define WORD_SWITCH_TAKES_ARG(STR) \
354 (!strcmp (STR, "Tdata") || !strcmp (STR, "include") \
355 || !strcmp (STR, "imacros"))
358 /* Record the mapping from file suffixes for compilation specs. */
362 char *suffix
; /* Use this compiler for input files
363 whose names end in this suffix. */
364 char *spec
; /* To use this compiler, pass this spec
368 /* Pointer to a vector of `struct compiler' that gives the spec for
369 compiling a file, based on its suffix.
370 A file that does not end in any of these suffixes will be passed
371 unchanged to the loader and nothing else will be done to it.
373 An entry containing two 0s is used to terminate the vector.
375 If multiple entries match a file, the last matching one is used. */
377 static struct compiler
*compilers
;
379 /* Number of entries in `compilers', not counting the null terminator. */
381 static int n_compilers
;
383 /* The default list of file name suffixes and their compilation specs. */
385 static struct compiler default_compilers
[] =
389 "cpp -lang-c %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
390 %{C:%{!E:%eGNU C does not support -C without using -E}}\
391 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
392 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
393 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
394 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
395 %{traditional-cpp:-traditional}\
396 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
397 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
398 %{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
399 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
400 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
401 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
402 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
403 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
404 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
405 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
406 %{!pipe:%g.s} %A\n }}}}"},
408 "%{E:cpp -lang-c %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
409 %{C:%{!E:%eGNU C does not support -C without using -E}}\
410 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
411 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
412 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
413 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
414 %{traditional-cpp:-traditional}\
415 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
417 %{!E:%e-E required when input is from standard input}"},
418 {".m", "@objective-c"},
420 "cpp -lang-objc %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
421 %{C:%{!E:%eGNU C does not support -C without using -E}}\
422 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
423 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
424 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
425 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
426 %{traditional-cpp:-traditional}\
427 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
428 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
429 %{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
430 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
431 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
432 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
433 -lang-objc %{gen-decls} \
434 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
435 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
436 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
437 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
438 %{!pipe:%g.s} %A\n }}}}"},
441 "%{!E:%eCompilation of header file requested} \
442 cpp %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
443 %{C:%{!E:%eGNU C does not support -C without using -E}}\
444 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
445 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
446 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
447 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
448 %{traditional-cpp:-traditional}\
449 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
455 "cpp -lang-c++ %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
456 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
457 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
458 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
459 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
460 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
461 %{traditional-cpp:-traditional} %{trigraphs}\
462 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
463 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
464 %{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
465 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
466 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
467 %{v:-version} %{pg:-p} %{p} %{f*}\
468 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
469 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
470 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
471 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
472 %{!pipe:%g.s} %A\n }}}}"},
473 {".i", "@cpp-output"},
475 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
476 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
477 %{v:-version} %{pg:-p} %{p} %{f*}\
478 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
479 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
480 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
481 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s} %A\n }"},
482 {".ii", "@c++-cpp-output"},
484 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
485 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
486 %{v:-version} %{pg:-p} %{p} %{f*}\
487 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
488 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
489 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
490 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
491 %{!pipe:%g.s} %A\n }"},
492 {".s", "@assembler"},
494 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
495 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i %A\n }"},
496 {".S", "@assembler-with-cpp"},
497 {"@assembler-with-cpp",
498 "cpp -lang-asm %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
499 %{C:%{!E:%eGNU C does not support -C without using -E}}\
500 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
501 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
502 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
503 %{traditional-cpp:-traditional}\
504 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
505 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
506 %{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
507 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
508 %{!pipe:%g.s} %A\n }}}}"},
509 /* Mark end of table */
513 /* Number of elements in default_compilers, not counting the terminator. */
515 static int n_default_compilers
516 = (sizeof default_compilers
/ sizeof (struct compiler
)) - 1;
518 /* Here is the spec for running the linker, after compiling all files. */
520 #ifdef LINK_LIBGCC_SPECIAL
521 /* Have gcc do the search. */
522 static char *link_command_spec
= "\
523 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %X %l %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
524 %{r} %{s} %{T*} %{t} %{x} %{z}\
525 %{!A:%{!nostdlib:%S}} \
526 %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
528 /* Use -l and have the linker do the search. */
529 static char *link_command_spec
= "\
530 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %X %l %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
531 %{r} %{T*} %{t} %{x} %{z}\
532 %{!A:%{!nostdlib:%S}} \
533 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
536 /* A vector of options to give to the linker.
537 These options are accumlated by %x
538 and substituted into the linker command with %X. */
539 static int n_linker_options
;
540 static char **linker_options
;
542 /* Read compilation specs from a file named FILENAME,
543 replacing the default ones.
545 A suffix which starts with `*' is a definition for
546 one of the machine-specific sub-specs. The "suffix" should be
547 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
548 The corresponding spec is stored in asm_spec, etc.,
549 rather than in the `compilers' vector.
551 Anything invalid in the file is a fatal error. */
554 read_specs (filename
)
563 fprintf (stderr
, "Reading specs from %s\n", filename
);
565 /* Open and stat the file. */
566 desc
= open (filename
, 0, 0);
568 pfatal_with_name (filename
);
569 if (stat (filename
, &statbuf
) < 0)
570 pfatal_with_name (filename
);
572 /* Read contents of file into BUFFER. */
573 buffer
= xmalloc (statbuf
.st_size
+ 1);
574 read (desc
, buffer
, statbuf
.st_size
);
575 buffer
[statbuf
.st_size
] = 0;
578 /* Scan BUFFER for specs, putting them in the vector. */
584 char *in
, *out
, *p1
, *p2
;
586 /* Advance P in BUFFER to the next nonblank nocomment line. */
587 p
= skip_whitespace (p
);
591 /* Find the colon that should end the suffix. */
593 while (*p1
&& *p1
!= ':' && *p1
!= '\n') p1
++;
594 /* The colon shouldn't be missing. */
596 fatal ("specs file malformed after %d characters", p1
- buffer
);
597 /* Skip back over trailing whitespace. */
599 while (p2
> buffer
&& (p2
[-1] == ' ' || p2
[-1] == '\t')) p2
--;
600 /* Copy the suffix to a string. */
601 suffix
= save_string (p
, p2
- p
);
602 /* Find the next line. */
603 p
= skip_whitespace (p1
+ 1);
605 fatal ("specs file malformed after %d characters", p
- buffer
);
607 /* Find next blank line. */
608 while (*p1
&& !(*p1
== '\n' && p1
[1] == '\n')) p1
++;
609 /* Specs end at the blank line and do not include the newline. */
610 spec
= save_string (p
, p1
- p
);
613 /* Delete backslash-newline sequences from the spec. */
618 if (in
[0] == '\\' && in
[1] == '\n')
620 else if (in
[0] == '#')
622 while (*in
&& *in
!= '\n') in
++;
630 if (suffix
[0] == '*')
632 if (! strcmp (suffix
, "*link_command"))
633 link_command_spec
= spec
;
635 set_spec (suffix
+ 1, spec
);
639 /* Add this pair to the vector. */
641 = ((struct compiler
*)
642 xrealloc (compilers
, (n_compilers
+ 2) * sizeof (struct compiler
)));
643 compilers
[n_compilers
].suffix
= suffix
;
644 compilers
[n_compilers
].spec
= spec
;
649 link_command_spec
= spec
;
652 if (link_command_spec
== 0)
653 fatal ("spec file has no spec for linking");
662 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
663 be considered whitespace. */
664 if (p
[0] == '\n' && p
[1] == '\n' && p
[2] == '\n')
666 else if (*p
== '\n' || *p
== ' ' || *p
== '\t')
670 while (*p
!= '\n') p
++;
680 /* Structure to keep track of the specs that have been defined so far. These
681 are accessed using %Sspecname in a compiler or link spec. */
685 char *name
; /* Name of the spec. */
686 char *spec
; /* The spec itself. */
687 struct spec_list
*next
; /* Next spec in linked list. */
690 /* List of specs that have been defined so far. */
692 static struct spec_list
*specs
= (struct spec_list
*) 0;
694 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
695 removed; If the spec starts with a + then SPEC is added to the end of the
699 set_spec (name
, spec
)
703 struct spec_list
*sl
;
706 /* See if the spec already exists */
707 for (sl
= specs
; sl
; sl
= sl
->next
)
708 if (strcmp (sl
->name
, name
) == 0)
713 /* Not found - make it */
714 sl
= (struct spec_list
*) xmalloc (sizeof (struct spec_list
));
715 sl
->name
= save_string (name
, strlen (name
));
716 sl
->spec
= save_string ("", 0);
722 if (name
&& spec
[0] == '+' && isspace (spec
[1]))
723 sl
->spec
= concat (name
, spec
+ 1, "");
725 sl
->spec
= save_string (spec
, strlen (spec
));
727 if (! strcmp (name
, "asm"))
729 else if (! strcmp (name
, "asm_final"))
730 asm_final_spec
= sl
->spec
;
731 else if (! strcmp (name
, "cc1"))
733 else if (! strcmp (name
, "cc1plus"))
734 cc1plus_spec
= sl
->spec
;
735 else if (! strcmp (name
, "cpp"))
737 else if (! strcmp (name
, "endfile"))
738 endfile_spec
= sl
->spec
;
739 else if (! strcmp (name
, "lib"))
741 else if (! strcmp (name
, "link"))
742 link_spec
= sl
->spec
;
743 else if (! strcmp (name
, "predefines"))
744 cpp_predefines
= sl
->spec
;
745 else if (! strcmp (name
, "signed_char"))
746 signed_char_spec
= sl
->spec
;
747 else if (! strcmp (name
, "startfile"))
748 startfile_spec
= sl
->spec
;
749 else if (! strcmp (name
, "switches_need_spaces"))
750 switches_need_spaces
= sl
->spec
;
751 else if (! strcmp (name
, "cross_compile"))
752 cross_compile
= atoi (sl
->spec
);
753 /* Free the old spec */
758 /* Accumulate a command (program name and args), and run it. */
760 /* Vector of pointers to arguments in the current line of specifications. */
762 static char **argbuf
;
764 /* Number of elements allocated in argbuf. */
766 static int argbuf_length
;
768 /* Number of elements in argbuf currently in use (containing args). */
770 static int argbuf_index
;
772 /* Number of commands executed so far. */
774 static int execution_count
;
776 /* Name with which this program was invoked. */
778 static char *programname
;
780 /* Structures to keep track of prefixes to try when looking for files. */
784 char *prefix
; /* String to prepend to the path. */
785 struct prefix_list
*next
; /* Next in linked list. */
786 int require_machine_suffix
; /* Don't use without machine_suffix. */
787 int *used_flag_ptr
; /* 1 if a file was found with this prefix. */
792 struct prefix_list
*plist
; /* List of prefixes to try */
793 int max_len
; /* Max length of a prefix in PLIST */
794 char *name
; /* Name of this list (used in config stuff) */
797 /* List of prefixes to try when looking for executables. */
799 static struct path_prefix exec_prefix
= { 0, 0, "exec" };
801 /* List of prefixes to try when looking for startup (crt0) files. */
803 static struct path_prefix startfile_prefix
= { 0, 0, "startfile" };
805 /* List of prefixes to try when looking for libraries. */
807 static struct path_prefix library_prefix
= { 0, 0, "libraryfile" };
809 /* Suffix to attach to directories searched for commands. */
811 static char *machine_suffix
= 0;
813 /* Default prefixes to attach to command names. */
815 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
816 #undef MD_EXEC_PREFIX
817 #undef MD_STARTFILE_PREFIX
820 #ifndef STANDARD_EXEC_PREFIX
821 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
822 #endif /* !defined STANDARD_EXEC_PREFIX */
824 static char *standard_exec_prefix
= STANDARD_EXEC_PREFIX
;
825 static char *standard_exec_prefix_1
= "/usr/lib/gcc/";
826 #ifdef MD_EXEC_PREFIX
827 static char *md_exec_prefix
= MD_EXEC_PREFIX
;
830 #ifndef STANDARD_STARTFILE_PREFIX
831 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
832 #endif /* !defined STANDARD_STARTFILE_PREFIX */
834 #ifdef MD_STARTFILE_PREFIX
835 static char *md_startfile_prefix
= MD_STARTFILE_PREFIX
;
837 static char *standard_startfile_prefix
= STANDARD_STARTFILE_PREFIX
;
838 static char *standard_startfile_prefix_1
= "/lib/";
839 static char *standard_startfile_prefix_2
= "/usr/lib/";
841 /* Clear out the vector of arguments (after a command is executed). */
849 /* Add one argument to the vector at the end.
850 This is done when a space is seen or at the end of the line.
851 If DELETE_ALWAYS is nonzero, the arg is a filename
852 and the file should be deleted eventually.
853 If DELETE_FAILURE is nonzero, the arg is a filename
854 and the file should be deleted if this compilation fails. */
857 store_arg (arg
, delete_always
, delete_failure
)
859 int delete_always
, delete_failure
;
861 if (argbuf_index
+ 1 == argbuf_length
)
863 argbuf
= (char **) xrealloc (argbuf
, (argbuf_length
*= 2) * sizeof (char *));
866 argbuf
[argbuf_index
++] = arg
;
867 argbuf
[argbuf_index
] = 0;
869 if (delete_always
|| delete_failure
)
870 record_temp_file (arg
, delete_always
, delete_failure
);
873 /* Record the names of temporary files we tell compilers to write,
874 and delete them at the end of the run. */
876 /* This is the common prefix we use to make temp file names.
877 It is chosen once for each run of this program.
878 It is substituted into a spec by %g.
879 Thus, all temp file names contain this prefix.
880 In practice, all temp file names start with this prefix.
882 This prefix comes from the envvar TMPDIR if it is defined;
883 otherwise, from the P_tmpdir macro if that is defined;
884 otherwise, in /usr/tmp or /tmp. */
886 static char *temp_filename
;
888 /* Length of the prefix. */
890 static int temp_filename_length
;
892 /* Define the list of temporary files to delete. */
897 struct temp_file
*next
;
900 /* Queue of files to delete on success or failure of compilation. */
901 static struct temp_file
*always_delete_queue
;
902 /* Queue of files to delete on failure of compilation. */
903 static struct temp_file
*failure_delete_queue
;
905 /* Record FILENAME as a file to be deleted automatically.
906 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
907 otherwise delete it in any case.
908 FAIL_DELETE nonzero means delete it if a compilation step fails;
909 otherwise delete it in any case. */
912 record_temp_file (filename
, always_delete
, fail_delete
)
918 name
= xmalloc (strlen (filename
) + 1);
919 strcpy (name
, filename
);
923 register struct temp_file
*temp
;
924 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
925 if (! strcmp (name
, temp
->name
))
927 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
928 temp
->next
= always_delete_queue
;
930 always_delete_queue
= temp
;
936 register struct temp_file
*temp
;
937 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
938 if (! strcmp (name
, temp
->name
))
940 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
941 temp
->next
= failure_delete_queue
;
943 failure_delete_queue
= temp
;
948 /* Delete all the temporary files whose names we previously recorded. */
953 register struct temp_file
*temp
;
955 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
959 printf ("Delete %s? (y or n) ", temp
->name
);
963 while (getchar () != '\n') ;
964 if (i
== 'y' || i
== 'Y')
968 if (stat (temp
->name
, &st
) >= 0)
970 /* Delete only ordinary files. */
971 if (S_ISREG (st
.st_mode
))
972 if (unlink (temp
->name
) < 0)
974 perror_with_name (temp
->name
);
979 always_delete_queue
= 0;
982 /* Delete all the files to be deleted on error. */
985 delete_failure_queue ()
987 register struct temp_file
*temp
;
989 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
993 printf ("Delete %s? (y or n) ", temp
->name
);
997 while (getchar () != '\n') ;
998 if (i
== 'y' || i
== 'Y')
1001 if (unlink (temp
->name
) < 0)
1003 perror_with_name (temp
->name
);
1009 clear_failure_queue ()
1011 failure_delete_queue
= 0;
1014 /* Compute a string to use as the base of all temporary file names.
1015 It is substituted for %g. */
1020 char *base
= getenv ("TMPDIR");
1023 if (base
== (char *)0)
1026 if (access (P_tmpdir
, R_OK
| W_OK
) == 0)
1029 if (base
== (char *)0)
1031 if (access ("/usr/tmp", R_OK
| W_OK
) == 0)
1038 len
= strlen (base
);
1039 temp_filename
= xmalloc (len
+ sizeof("/ccXXXXXX"));
1040 strcpy (temp_filename
, base
);
1041 if (len
> 0 && temp_filename
[len
-1] != '/')
1042 temp_filename
[len
++] = '/';
1043 strcpy (temp_filename
+ len
, "ccXXXXXX");
1045 mktemp (temp_filename
);
1046 temp_filename_length
= strlen (temp_filename
);
1049 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1050 access to check permissions.
1051 Return 0 if not found, otherwise return its name, allocated with malloc. */
1054 find_a_file (pprefix
, name
, mode
)
1055 struct path_prefix
*pprefix
;
1060 char *file_suffix
= ((mode
& X_OK
) != 0 ? EXECUTABLE_SUFFIX
: "");
1061 struct prefix_list
*pl
;
1062 int len
= pprefix
->max_len
+ strlen (name
) + strlen (file_suffix
) + 1;
1065 len
+= strlen (machine_suffix
);
1067 temp
= xmalloc (len
);
1069 /* Determine the filename to execute (special case for absolute paths). */
1073 if (access (name
, mode
))
1075 strcpy (temp
, name
);
1080 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
1084 strcpy (temp
, pl
->prefix
);
1085 strcat (temp
, machine_suffix
);
1086 strcat (temp
, name
);
1087 if (access (temp
, mode
) == 0)
1089 if (pl
->used_flag_ptr
!= 0)
1090 *pl
->used_flag_ptr
= 1;
1093 /* Some systems have a suffix for executable files.
1094 So try appending that. */
1095 if (file_suffix
[0] != 0)
1097 strcat (temp
, file_suffix
);
1098 if (access (temp
, mode
) == 0)
1100 if (pl
->used_flag_ptr
!= 0)
1101 *pl
->used_flag_ptr
= 1;
1106 /* Certain prefixes can't be used without the machine suffix
1107 when the machine or version is explicitly specified. */
1108 if (!pl
->require_machine_suffix
)
1110 strcpy (temp
, pl
->prefix
);
1111 strcat (temp
, name
);
1112 if (access (temp
, mode
) == 0)
1114 if (pl
->used_flag_ptr
!= 0)
1115 *pl
->used_flag_ptr
= 1;
1118 /* Some systems have a suffix for executable files.
1119 So try appending that. */
1120 if (file_suffix
[0] != 0)
1122 strcat (temp
, file_suffix
);
1123 if (access (temp
, mode
) == 0)
1125 if (pl
->used_flag_ptr
!= 0)
1126 *pl
->used_flag_ptr
= 1;
1137 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1138 at the start of the list, otherwise it goes at the end.
1140 If WARN is nonzero, we will warn if no file is found
1141 through this prefix. WARN should point to an int
1142 which will be set to 1 if this entry is used. */
1145 add_prefix (pprefix
, prefix
, first
, require_machine_suffix
, warn
)
1146 struct path_prefix
*pprefix
;
1149 int require_machine_suffix
;
1152 struct prefix_list
*pl
, **prev
;
1155 if (!first
&& pprefix
->plist
)
1157 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
1162 prev
= &pprefix
->plist
;
1164 /* Keep track of the longest prefix */
1166 len
= strlen (prefix
);
1167 if (len
> pprefix
->max_len
)
1168 pprefix
->max_len
= len
;
1170 pl
= (struct prefix_list
*) xmalloc (sizeof (struct prefix_list
));
1171 pl
->prefix
= save_string (prefix
, len
);
1172 pl
->require_machine_suffix
= require_machine_suffix
;
1173 pl
->used_flag_ptr
= warn
;
1180 pl
->next
= (struct prefix_list
*) 0;
1184 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1187 unused_prefix_warnings (pprefix
)
1188 struct path_prefix
*pprefix
;
1190 struct prefix_list
*pl
= pprefix
->plist
;
1194 if (pl
->used_flag_ptr
!= 0 && !*pl
->used_flag_ptr
)
1196 error ("file path prefix `%s' never used",
1198 /* Prevent duplicate warnings. */
1199 *pl
->used_flag_ptr
= 1;
1205 /* Get rid of all prefixes built up so far in *PLISTP. */
1208 free_path_prefix (pprefix
)
1209 struct path_prefix
*pprefix
;
1211 struct prefix_list
*pl
= pprefix
->plist
;
1212 struct prefix_list
*temp
;
1218 free (temp
->prefix
);
1219 free ((char *) temp
);
1221 pprefix
->plist
= (struct prefix_list
*) 0;
1224 /* stdin file number. */
1225 #define STDIN_FILE_NO 0
1227 /* stdout file number. */
1228 #define STDOUT_FILE_NO 1
1230 /* value of `pipe': port index for reading. */
1233 /* value of `pipe': port index for writing. */
1234 #define WRITE_PORT 1
1236 /* Pipe waiting from last process, to be used as input for the next one.
1237 Value is STDIN_FILE_NO if no pipe is waiting
1238 (i.e. the next command is the first of a group). */
1240 static int last_pipe_input
;
1242 /* Fork one piped subcommand. FUNC is the system call to use
1243 (either execv or execvp). ARGV is the arg vector to use.
1244 NOT_LAST is nonzero if this is not the last subcommand
1245 (i.e. its output should be piped to the next one.) */
1249 /* Declare these to avoid compilation error. They won't be called. */
1250 int execv(const char *a
, const char **b
){}
1251 int execvp(const char *a
, const char **b
){}
1254 pexecute (func
, program
, argv
, not_last
)
1264 scmd
= (char *)malloc(strlen(program
) + strlen(temp_filename
) + 6);
1265 sprintf(scmd
, "%s @%s.gp", program
, temp_filename
);
1266 argfile
= fopen(scmd
+strlen(program
)+2, "w");
1268 pfatal_with_name(scmd
+strlen(program
)+2);
1270 for (i
=1; argv
[i
]; i
++)
1271 fprintf(argfile
, "%s\n", argv
[i
]);
1276 remove(scmd
+strlen(program
)+2);
1280 #else /* not __MSDOS__ */
1283 pexecute (func
, program
, argv
, not_last
)
1291 int input_desc
= last_pipe_input
;
1292 int output_desc
= STDOUT_FILE_NO
;
1293 int retries
, sleep_interval
;
1295 /* If this isn't the last process, make a pipe for its output,
1296 and record it as waiting to be the input to the next process. */
1300 if (pipe (pdes
) < 0)
1301 pfatal_with_name ("pipe");
1302 output_desc
= pdes
[WRITE_PORT
];
1303 last_pipe_input
= pdes
[READ_PORT
];
1306 last_pipe_input
= STDIN_FILE_NO
;
1308 /* Fork a subprocess; wait and retry if it fails. */
1310 for (retries
= 0; retries
< 4; retries
++)
1315 sleep (sleep_interval
);
1316 sleep_interval
*= 2;
1323 pfatal_with_name ("fork");
1325 pfatal_with_name ("vfork");
1331 /* Move the input and output pipes into place, if nec. */
1332 if (input_desc
!= STDIN_FILE_NO
)
1334 close (STDIN_FILE_NO
);
1338 if (output_desc
!= STDOUT_FILE_NO
)
1340 close (STDOUT_FILE_NO
);
1342 close (output_desc
);
1345 /* Close the parent's descs that aren't wanted here. */
1346 if (last_pipe_input
!= STDIN_FILE_NO
)
1347 close (last_pipe_input
);
1349 /* Exec the program. */
1350 (*func
) (program
, argv
);
1351 perror_exec (program
);
1357 /* In the parent, after forking.
1358 Close the descriptors that we made for this child. */
1359 if (input_desc
!= STDIN_FILE_NO
)
1361 if (output_desc
!= STDOUT_FILE_NO
)
1362 close (output_desc
);
1364 /* Return child's process number. */
1369 #endif /* not __MSDOS__ */
1371 /* Execute the command specified by the arguments on the current line of spec.
1372 When using pipes, this includes several piped-together commands
1373 with `|' between them.
1375 Return 0 if successful, -1 if failed. */
1381 int n_commands
; /* # of command. */
1385 char *prog
; /* program name. */
1386 char **argv
; /* vector of args. */
1387 int pid
; /* pid of process for this command. */
1390 struct command
*commands
; /* each command buffer with above info. */
1392 /* Count # of piped commands. */
1393 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1394 if (strcmp (argbuf
[i
], "|") == 0)
1397 /* Get storage for each command. */
1399 = (struct command
*) alloca (n_commands
* sizeof (struct command
));
1401 /* Split argbuf into its separate piped processes,
1402 and record info about each one.
1403 Also search for the programs that are to be run. */
1405 commands
[0].prog
= argbuf
[0]; /* first command. */
1406 commands
[0].argv
= &argbuf
[0];
1407 string
= find_a_file (&exec_prefix
, commands
[0].prog
, X_OK
);
1409 commands
[0].argv
[0] = string
;
1411 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1412 if (strcmp (argbuf
[i
], "|") == 0)
1413 { /* each command. */
1415 fatal ("-pipe not supported under MS-DOS");
1417 argbuf
[i
] = 0; /* termination of command args. */
1418 commands
[n_commands
].prog
= argbuf
[i
+ 1];
1419 commands
[n_commands
].argv
= &argbuf
[i
+ 1];
1420 string
= find_a_file (&exec_prefix
, commands
[n_commands
].prog
, X_OK
);
1422 commands
[n_commands
].argv
[0] = string
;
1426 argbuf
[argbuf_index
] = 0;
1428 /* If -v, print what we are about to do, and maybe query. */
1430 if (verbose_flag
|| save_temps_flag
)
1432 /* Print each piped command as a separate line. */
1433 for (i
= 0; i
< n_commands
; i
++)
1437 for (j
= commands
[i
].argv
; *j
; j
++)
1438 fprintf (stderr
, " %s", *j
);
1440 /* Print a pipe symbol after all but the last command. */
1441 if (i
+ 1 != n_commands
)
1442 fprintf (stderr
, " |");
1443 fprintf (stderr
, "\n");
1447 fprintf (stderr
, "\nGo ahead? (y or n) ");
1451 while (getchar () != '\n') ;
1452 if (i
!= 'y' && i
!= 'Y')
1457 /* Run each piped subprocess. */
1459 last_pipe_input
= STDIN_FILE_NO
;
1460 for (i
= 0; i
< n_commands
; i
++)
1462 char *string
= commands
[i
].argv
[0];
1464 commands
[i
].pid
= pexecute ((string
!= commands
[i
].prog
? execv
: execvp
),
1465 string
, commands
[i
].argv
,
1466 i
+ 1 < n_commands
);
1468 if (string
!= commands
[i
].prog
)
1474 /* Wait for all the subprocesses to finish.
1475 We don't care what order they finish in;
1476 we know that N_COMMANDS waits will get them all. */
1481 for (i
= 0; i
< n_commands
; i
++)
1488 status
= pid
= commands
[i
].pid
;
1490 pid
= wait (&status
);
1498 for (j
= 0; j
< n_commands
; j
++)
1499 if (commands
[j
].pid
== pid
)
1500 prog
= commands
[j
].prog
;
1502 if ((status
& 0x7F) != 0)
1503 fatal ("Internal compiler error: program %s got fatal signal %d",
1504 prog
, (status
& 0x7F));
1505 if (((status
& 0xFF00) >> 8) >= MIN_FATAL_STATUS
)
1513 /* Find all the switches given to us
1514 and make a vector describing them.
1515 The elements of the vector are strings, one per switch given.
1516 If a switch uses following arguments, then the `part1' field
1517 is the switch itself and the `args' field
1518 is a null-terminated vector containing the following arguments.
1519 The `valid' field is nonzero if any spec has looked at this switch;
1520 if it remains zero at the end of the run, it must be meaningless. */
1529 static struct switchstr
*switches
;
1531 static int n_switches
;
1539 /* Also a vector of input files specified. */
1541 static struct infile
*infiles
;
1543 static int n_infiles
;
1545 /* And a vector of corresponding output files is made up later. */
1547 static char **outfiles
;
1549 /* Create the vector `switches' and its contents.
1550 Store its length in `n_switches'. */
1553 process_command (argc
, argv
)
1559 char *spec_lang
= 0;
1560 int last_language_n_infiles
;
1564 spec_version
= version_string
;
1566 /* Set up the default search paths. */
1568 temp
= getenv ("GCC_EXEC_PREFIX");
1571 add_prefix (&exec_prefix
, temp
, 0, 0, 0);
1572 add_prefix (&startfile_prefix
, temp
, 0, 0, 0);
1575 /* COMPILER_PATH and LIBRARY_PATH have values
1576 that are lists of directory names with colons. */
1578 temp
= getenv ("COMPILER_PATH");
1581 char *startp
, *endp
;
1582 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1584 startp
= endp
= temp
;
1587 if ((*endp
== ':') || (*endp
== 0))
1589 strncpy (nstore
, startp
, endp
-startp
);
1592 strcpy (nstore
, "./");
1594 else if (endp
[-1] != '/')
1596 nstore
[endp
-startp
] = '/';
1597 nstore
[endp
-startp
+1] = 0;
1600 nstore
[endp
-startp
] = 0;
1601 add_prefix (&exec_prefix
, nstore
, 0, 0, 0);
1604 endp
= startp
= endp
+ 1;
1611 temp
= getenv ("LIBRARY_PATH");
1614 char *startp
, *endp
;
1615 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1617 startp
= endp
= temp
;
1620 if ((*endp
== ':') || (*endp
== 0))
1622 strncpy (nstore
, startp
, endp
-startp
);
1625 strcpy (nstore
, "./");
1627 else if (endp
[-1] != '/')
1629 nstore
[endp
-startp
] = '/';
1630 nstore
[endp
-startp
+1] = 0;
1633 nstore
[endp
-startp
] = 0;
1634 add_prefix (&startfile_prefix
, nstore
, 0, 0, 0);
1635 /* Make separate list of dirs that came from LIBRARY_PATH. */
1636 add_prefix (&library_prefix
, nstore
, 0, 0, 0);
1639 endp
= startp
= endp
+ 1;
1646 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1647 temp
= getenv ("LPATH");
1650 char *startp
, *endp
;
1651 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1653 startp
= endp
= temp
;
1656 if ((*endp
== ':') || (*endp
== 0))
1658 strncpy (nstore
, startp
, endp
-startp
);
1661 strcpy (nstore
, "./");
1663 else if (endp
[-1] != '/')
1665 nstore
[endp
-startp
] = '/';
1666 nstore
[endp
-startp
+1] = 0;
1669 nstore
[endp
-startp
] = 0;
1670 add_prefix (&startfile_prefix
, nstore
, 0, 0, 0);
1671 /* Make separate list of dirs that came from LIBRARY_PATH. */
1672 add_prefix (&library_prefix
, nstore
, 0, 0, 0);
1675 endp
= startp
= endp
+ 1;
1682 /* Scan argv twice. Here, the first time, just count how many switches
1683 there will be in their vector, and how many input files in theirs.
1684 Here we also parse the switches that cc itself uses (e.g. -v). */
1686 for (i
= 1; i
< argc
; i
++)
1688 if (! strcmp (argv
[i
], "-dumpspecs"))
1690 printf ("*asm:\n%s\n\n", asm_spec
);
1691 printf ("*asm_final:\n%s\n\n", asm_final_spec
);
1692 printf ("*cpp:\n%s\n\n", cpp_spec
);
1693 printf ("*cc1:\n%s\n\n", cc1_spec
);
1694 printf ("*cc1plus:\n%s\n\n", cc1plus_spec
);
1695 printf ("*endfile:\n%s\n\n", endfile_spec
);
1696 printf ("*link:\n%s\n\n", link_spec
);
1697 printf ("*lib:\n%s\n\n", lib_spec
);
1698 printf ("*startfile:\n%s\n\n", startfile_spec
);
1699 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces
);
1700 printf ("*signed_char:\n%s\n\n", signed_char_spec
);
1701 printf ("*predefines:\n%s\n\n", cpp_predefines
);
1702 printf ("*cross_compile:\n%d\n\n", cross_compile
);
1706 else if (! strcmp (argv
[i
], "-dumpversion"))
1708 printf ("%s\n", version_string
);
1711 else if (! strcmp (argv
[i
], "-Xlinker"))
1713 /* Pass the argument of this option to the linker when we link. */
1716 fatal ("argument to `-Xlinker' is missing");
1719 if (!linker_options
)
1721 = (char **) xmalloc (n_linker_options
* sizeof (char **));
1724 = (char **) xrealloc (linker_options
,
1725 n_linker_options
* sizeof (char **));
1727 linker_options
[n_linker_options
- 1] = argv
[++i
];
1729 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
1731 register char *p
= &argv
[i
][1];
1732 register int c
= *p
;
1737 if (p
[1] == 0 && i
+ 1 == argc
)
1738 fatal ("argument to `-b' is missing");
1740 spec_machine
= argv
[++i
];
1742 spec_machine
= p
+ 1;
1747 int *temp
= (int *) xmalloc (sizeof (int));
1749 if (p
[1] == 0 && i
+ 1 == argc
)
1750 fatal ("argument to `-B' is missing");
1755 add_prefix (&exec_prefix
, value
, 1, 0, temp
);
1756 add_prefix (&startfile_prefix
, value
, 1, 0, temp
);
1760 case 'v': /* Print our subcommands and print versions. */
1766 if (p
[1] == 0 && i
+ 1 == argc
)
1767 fatal ("argument to `-V' is missing");
1769 spec_version
= argv
[++i
];
1771 spec_version
= p
+ 1;
1775 if (!strcmp (p
, "save-temps"))
1777 save_temps_flag
= 1;
1783 if (SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
1784 i
+= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
1785 else if (WORD_SWITCH_TAKES_ARG (p
))
1786 i
+= WORD_SWITCH_TAKES_ARG (p
);
1793 /* Set up the search paths before we go looking for config files. */
1795 /* These come before the md prefixes so that we will find gcc's subcommands
1796 (such as cpp) rather than those of the host system. */
1797 add_prefix (&exec_prefix
, standard_exec_prefix
, 0, 1, 0);
1798 add_prefix (&exec_prefix
, standard_exec_prefix_1
, 0, 1, 0);
1800 add_prefix (&startfile_prefix
, standard_exec_prefix
, 0, 1, 0);
1801 add_prefix (&startfile_prefix
, standard_exec_prefix_1
, 0, 1, 0);
1803 /* More prefixes are enabled in main, after we read the specs file
1804 and determine whether this is cross-compilation or not. */
1807 /* Then create the space for the vectors and scan again. */
1809 switches
= ((struct switchstr
*)
1810 xmalloc ((n_switches
+ 1) * sizeof (struct switchstr
)));
1811 infiles
= (struct infile
*) xmalloc ((n_infiles
+ 1) * sizeof (struct infile
));
1814 last_language_n_infiles
= -1;
1816 /* This, time, copy the text of each switch and store a pointer
1817 to the copy in the vector of switches.
1818 Store all the infiles in their vector. */
1820 for (i
= 1; i
< argc
; i
++)
1822 if (!strcmp (argv
[i
], "-Xlinker"))
1824 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
1826 register char *p
= &argv
[i
][1];
1827 register int c
= *p
;
1829 if (c
== 'B' || c
== 'b' || c
== 'V')
1831 /* Skip a separate arg, if any. */
1838 if (p
[1] == 0 && i
+ 1 == argc
)
1839 fatal ("argument to `-x' is missing");
1841 spec_lang
= argv
[++i
];
1844 if (! strcmp (spec_lang
, "none"))
1845 /* Suppress the warning if -xnone comes after the last input file,
1846 because alternate command interfaces like g++ might find it
1847 useful to place -xnone after each input file. */
1850 last_language_n_infiles
= n_infiles
;
1853 switches
[n_switches
].part1
= p
;
1854 /* Deal with option arguments in separate argv elements. */
1855 if ((SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
1856 || WORD_SWITCH_TAKES_ARG (p
)) {
1858 int n_args
= WORD_SWITCH_TAKES_ARG (p
);
1861 /* Count only the option arguments in separate argv elements. */
1862 n_args
= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
1864 switches
[n_switches
].args
1865 = (char **) xmalloc ((n_args
+ 1) * sizeof (char *));
1867 switches
[n_switches
].args
[j
++] = argv
[++i
];
1868 /* Null-terminate the vector. */
1869 switches
[n_switches
].args
[j
] = 0;
1870 } else if (*switches_need_spaces
!= 0 && (c
== 'o' || c
== 'L')) {
1871 /* On some systems, ld cannot handle -o or -L without space.
1872 So split the -o or -L from its argument. */
1873 switches
[n_switches
].part1
= (c
== 'o' ? "o" : "L");
1874 switches
[n_switches
].args
= (char **) xmalloc (2 * sizeof (char *));
1875 switches
[n_switches
].args
[0] = xmalloc (strlen (p
));
1876 strcpy (switches
[n_switches
].args
[0], &p
[1]);
1877 switches
[n_switches
].args
[1] = 0;
1879 switches
[n_switches
].args
= 0;
1880 switches
[n_switches
].valid
= 0;
1881 /* This is always valid, since gcc.c itself understands it. */
1882 if (!strcmp (p
, "save-temps"))
1883 switches
[n_switches
].valid
= 1;
1888 infiles
[n_infiles
].language
= spec_lang
;
1889 infiles
[n_infiles
++].name
= argv
[i
];
1893 if (n_infiles
== last_language_n_infiles
)
1894 error ("Warning: `-x %s' after last input file has no effect", spec_lang
);
1896 switches
[n_switches
].part1
= 0;
1897 infiles
[n_infiles
].name
= 0;
1900 /* Process a spec string, accumulating and running commands. */
1902 /* These variables describe the input file name.
1903 input_file_number is the index on outfiles of this file,
1904 so that the output file name can be stored for later use by %o.
1905 input_basename is the start of the part of the input file
1906 sans all directory names, and basename_length is the number
1907 of characters starting there excluding the suffix .c or whatever. */
1909 static char *input_filename
;
1910 static int input_file_number
;
1911 static int input_filename_length
;
1912 static int basename_length
;
1913 static char *input_basename
;
1914 static char *input_suffix
;
1916 /* These are variables used within do_spec and do_spec_1. */
1918 /* Nonzero if an arg has been started and not yet terminated
1919 (with space, tab or newline). */
1920 static int arg_going
;
1922 /* Nonzero means %d or %g has been seen; the next arg to be terminated
1923 is a temporary file name. */
1924 static int delete_this_arg
;
1926 /* Nonzero means %w has been seen; the next arg to be terminated
1927 is the output file name of this compilation. */
1928 static int this_is_output_file
;
1930 /* Nonzero means %s has been seen; the next arg to be terminated
1931 is the name of a library file and we should try the standard
1932 search dirs for it. */
1933 static int this_is_library_file
;
1935 /* Process the spec SPEC and run the commands specified therein.
1936 Returns 0 if the spec is successfully processed; -1 if failed. */
1946 delete_this_arg
= 0;
1947 this_is_output_file
= 0;
1948 this_is_library_file
= 0;
1950 value
= do_spec_1 (spec
, 0, NULL
);
1952 /* Force out any unfinished command.
1953 If -pipe, this forces out the last command if it ended in `|'. */
1956 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
1959 if (argbuf_index
> 0)
1966 /* Process the sub-spec SPEC as a portion of a larger spec.
1967 This is like processing a whole spec except that we do
1968 not initialize at the beginning and we do not supply a
1969 newline by default at the end.
1970 INSWITCH nonzero means don't process %-sequences in SPEC;
1971 in this case, % is treated as an ordinary character.
1972 This is used while substituting switches.
1973 INSWITCH nonzero also causes SPC not to terminate an argument.
1975 Value is zero unless a line was finished
1976 and the command on that line reported an error. */
1979 do_spec_1 (spec
, inswitch
, soft_matched_part
)
1982 char *soft_matched_part
;
1984 register char *p
= spec
;
1990 /* If substituting a switch, treat all chars like letters.
1991 Otherwise, NL, SPC, TAB and % are special. */
1992 switch (inswitch
? 'a' : c
)
1995 /* End of line: finish any pending argument,
1996 then run the pending command if one has been started. */
1999 obstack_1grow (&obstack
, 0);
2000 string
= obstack_finish (&obstack
);
2001 if (this_is_library_file
)
2002 string
= find_file (string
);
2003 store_arg (string
, delete_this_arg
, this_is_output_file
);
2004 if (this_is_output_file
)
2005 outfiles
[input_file_number
] = string
;
2009 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
2012 for (i
= 0; i
< n_switches
; i
++)
2013 if (!strcmp (switches
[i
].part1
, "pipe"))
2016 /* A `|' before the newline means use a pipe here,
2017 but only if -pipe was specified.
2018 Otherwise, execute now and don't pass the `|' as an arg. */
2021 switches
[i
].valid
= 1;
2028 if (argbuf_index
> 0)
2030 int value
= execute ();
2034 /* Reinitialize for a new command, and for a new argument. */
2037 delete_this_arg
= 0;
2038 this_is_output_file
= 0;
2039 this_is_library_file
= 0;
2043 /* End any pending argument. */
2046 obstack_1grow (&obstack
, 0);
2047 string
= obstack_finish (&obstack
);
2048 if (this_is_library_file
)
2049 string
= find_file (string
);
2050 store_arg (string
, delete_this_arg
, this_is_output_file
);
2051 if (this_is_output_file
)
2052 outfiles
[input_file_number
] = string
;
2056 obstack_1grow (&obstack
, c
);
2062 /* Space or tab ends an argument if one is pending. */
2065 obstack_1grow (&obstack
, 0);
2066 string
= obstack_finish (&obstack
);
2067 if (this_is_library_file
)
2068 string
= find_file (string
);
2069 store_arg (string
, delete_this_arg
, this_is_output_file
);
2070 if (this_is_output_file
)
2071 outfiles
[input_file_number
] = string
;
2073 /* Reinitialize for a new argument. */
2075 delete_this_arg
= 0;
2076 this_is_output_file
= 0;
2077 this_is_library_file
= 0;
2084 fatal ("Invalid specification! Bug in cc.");
2087 obstack_grow (&obstack
, input_basename
, basename_length
);
2092 delete_this_arg
= 2;
2095 /* Dump out the directories specified with LIBRARY_PATH,
2096 followed by the absolute directories
2097 that we search for startfiles. */
2099 for (i
= 0; i
< 2; i
++)
2101 struct prefix_list
*pl
2102 = (i
== 0 ? library_prefix
.plist
: startfile_prefix
.plist
);
2103 for (; pl
; pl
= pl
->next
)
2105 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2106 /* Used on systems which record the specified -L dirs
2107 and use them to search for dynamic linking. */
2108 /* Relative directories always come from -B,
2109 and it is better not to use them for searching
2110 at run time. In particular, stage1 loses */
2111 if (pl
->prefix
[0] != '/')
2116 if (is_linker_dir (pl
->prefix
, machine_suffix
))
2118 do_spec_1 ("-L", 0, 0);
2119 #ifdef SPACE_AFTER_L_OPTION
2120 do_spec_1 (" ", 0, 0);
2122 do_spec_1 (pl
->prefix
, 1, 0);
2123 do_spec_1 (machine_suffix
, 1, 0);
2124 /* Make this a separate argument. */
2125 do_spec_1 (" ", 0, 0);
2128 if (!pl
->require_machine_suffix
)
2130 if (is_linker_dir (pl
->prefix
, ""))
2132 do_spec_1 ("-L", 0, 0);
2133 #ifdef SPACE_AFTER_L_OPTION
2134 do_spec_1 (" ", 0, 0);
2136 do_spec_1 (pl
->prefix
, 1, 0);
2137 /* Make this a separate argument. */
2138 do_spec_1 (" ", 0, 0);
2146 /* {...:%efoo} means report an error with `foo' as error message
2147 and don't execute any more commands for this file. */
2151 while (*p
!= 0 && *p
!= '\n') p
++;
2152 buf
= (char *) alloca (p
- q
+ 1);
2153 strncpy (buf
, q
, p
- q
);
2161 if (save_temps_flag
)
2162 obstack_grow (&obstack
, input_basename
, basename_length
);
2165 obstack_grow (&obstack
, temp_filename
, temp_filename_length
);
2166 delete_this_arg
= 1;
2172 obstack_grow (&obstack
, input_filename
, input_filename_length
);
2179 for (f
= 0; f
< n_infiles
; f
++)
2180 store_arg (outfiles
[f
], 0, 0);
2185 this_is_library_file
= 1;
2189 this_is_output_file
= 1;
2194 int index
= argbuf_index
;
2195 /* Handle the {...} following the %W. */
2198 p
= handle_braces (p
+ 1);
2201 /* If any args were output, mark the last one for deletion
2203 if (argbuf_index
!= index
)
2204 record_temp_file (argbuf
[argbuf_index
- 1], 0, 1);
2208 /* %x{OPTION} records OPTION for %X to output. */
2214 /* Skip past the option value and make a copy. */
2219 string
= save_string (p1
+ 1, p
- p1
- 2);
2221 /* See if we already recorded this option. */
2222 for (i
= 0; i
< n_linker_options
; i
++)
2223 if (! strcmp (string
, linker_options
[i
]))
2229 /* This option is new; add it. */
2231 if (!linker_options
)
2233 = (char **) xmalloc (n_linker_options
* sizeof (char **));
2236 = (char **) xrealloc (linker_options
,
2237 n_linker_options
* sizeof (char **));
2239 linker_options
[n_linker_options
- 1] = string
;
2243 /* Dump out the options accumulated previously using %x. */
2245 for (i
= 0; i
< n_linker_options
; i
++)
2247 do_spec_1 (linker_options
[i
], 1, NULL
);
2248 /* Make each accumulated option a separate argument. */
2249 do_spec_1 (" ", 0, NULL
);
2253 /* Here are digits and numbers that just process
2254 a certain constant string as a spec. */
2257 do_spec_1 (cc1_spec
, 0, NULL
);
2261 do_spec_1 (cc1plus_spec
, 0, NULL
);
2265 do_spec_1 (asm_spec
, 0, NULL
);
2269 do_spec_1 (asm_final_spec
, 0, NULL
);
2273 do_spec_1 (signed_char_spec
, 0, NULL
);
2277 do_spec_1 (cpp_spec
, 0, NULL
);
2281 do_spec_1 (endfile_spec
, 0, NULL
);
2285 do_spec_1 (link_spec
, 0, NULL
);
2289 do_spec_1 (lib_spec
, 0, NULL
);
2294 char *x
= (char *) alloca (strlen (cpp_predefines
) + 1);
2298 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2302 if (! strncmp (y
, "-D", 2))
2303 /* Copy the whole option. */
2304 while (*y
&& *y
!= ' ' && *y
!= '\t')
2306 else if (*y
== ' ' || *y
== '\t')
2307 /* Copy whitespace to the result. */
2309 /* Don't copy other options. */
2316 do_spec_1 (buf
, 0, NULL
);
2322 char *x
= (char *) alloca (strlen (cpp_predefines
) * 4 + 1);
2326 /* Copy all of CPP_PREDEFINES into BUF,
2327 but put __ after every -D and at the end of each arg. */
2331 if (! strncmp (y
, "-D", 2))
2338 if (strncmp (y
, "__", 2))
2340 /* Stick __ at front of macro name. */
2343 /* Arrange to stick __ at the end as well. */
2347 /* Copy the macro name. */
2348 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2357 /* Copy the value given, if any. */
2358 while (*y
&& *y
!= ' ' && *y
!= '\t')
2361 else if (*y
== ' ' || *y
== '\t')
2362 /* Copy whitespace to the result. */
2364 /* Don't copy -A options */
2370 /* Copy all of CPP_PREDEFINES into BUF,
2371 but put __ after every -D. */
2375 if (! strncmp (y
, "-D", 2))
2380 if (strncmp (y
, "__", 2))
2382 /* Stick __ at front of macro name. */
2387 /* Copy the macro name. */
2388 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2391 /* Copy the value given, if any. */
2392 while (*y
&& *y
!= ' ' && *y
!= '\t')
2395 else if (*y
== ' ' || *y
== '\t')
2396 /* Copy whitespace to the result. */
2398 /* Don't copy -A options */
2404 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2408 if (! strncmp (y
, "-A", 2))
2409 /* Copy the whole option. */
2410 while (*y
&& *y
!= ' ' && *y
!= '\t')
2412 else if (*y
== ' ' || *y
== '\t')
2413 /* Copy whitespace to the result. */
2415 /* Don't copy other options. */
2422 do_spec_1 (buf
, 0, NULL
);
2427 do_spec_1 (startfile_spec
, 0, NULL
);
2430 /* Here we define characters other than letters and digits. */
2433 p
= handle_braces (p
);
2439 obstack_1grow (&obstack
, '%');
2443 do_spec_1 (soft_matched_part
, 1, NULL
);
2444 do_spec_1 (" ", 0, NULL
);
2447 /* Process a string found as the value of a spec given by name.
2448 This feature allows individual machine descriptions
2449 to add and use their own specs.
2450 %[...] modifies -D options the way %P does;
2451 %(...) uses the spec unmodified. */
2456 struct spec_list
*sl
;
2459 /* The string after the S/P is the name of a spec that is to be
2461 while (*p
&& *p
!= ')' && *p
!= ']')
2464 /* See if it's in the list */
2465 for (len
= p
- name
, sl
= specs
; sl
; sl
= sl
->next
)
2466 if (strncmp (sl
->name
, name
, len
) == 0 && !sl
->name
[len
])
2475 do_spec_1 (name
, 0, NULL
);
2478 char *x
= (char *) alloca (strlen (name
) * 2 + 1);
2482 /* Copy all of NAME into BUF, but put __ after
2483 every -D and at the end of each arg, */
2486 if (! strncmp (y
, "-D", 2))
2494 else if (*y
== ' ' || *y
== 0)
2508 do_spec_1 (buf
, 0, NULL
);
2520 /* Backslash: treat next character as ordinary. */
2525 /* Ordinary character: put it into the current argument. */
2526 obstack_1grow (&obstack
, c
);
2530 return 0; /* End of string */
2533 /* Return 0 if we call do_spec_1 and that returns -1. */
2546 /* A `|' after the open-brace means,
2547 if the test fails, output a single minus sign rather than nothing.
2548 This is used in %{|!pipe:...}. */
2552 /* A `!' after the open-brace negates the condition:
2553 succeed if the specified switch is not present. */
2557 /* A `.' after the open-brace means test against the current suffix. */
2567 while (*p
!= ':' && *p
!= '}') p
++;
2570 register int count
= 1;
2588 int found
= (input_suffix
!= 0
2589 && strlen (input_suffix
) == p
- filter
2590 && strncmp (input_suffix
, filter
, p
- filter
) == 0);
2596 && do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL
) < 0)
2601 else if (p
[-1] == '*' && p
[0] == '}')
2603 /* Substitute all matching switches as separate args. */
2606 for (i
= 0; i
< n_switches
; i
++)
2607 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
2612 /* Test for presence of the specified switch. */
2616 /* If name specified ends in *, as in {x*:...},
2617 check for %* and handle that case. */
2618 if (p
[-1] == '*' && !negate
)
2623 /* First see whether we have %*. */
2625 while (*r
&& *r
== '}')
2627 if (*r
== '%' && r
[1] == '*')
2631 /* If we do, handle that case. */
2634 /* Substitute all matching switches as separate args.
2635 But do this by substituting for %*
2636 in the text that follows the colon. */
2638 unsigned hard_match_len
= p
- filter
- 1;
2639 char *string
= save_string (p
+ 1, q
- p
- 2);
2641 for (i
= 0; i
< n_switches
; i
++)
2642 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
2644 do_spec_1 (string
, 0, &switches
[i
].part1
[hard_match_len
]);
2645 /* Pass any arguments this switch has. */
2653 /* If name specified ends in *, as in {x*:...},
2654 check for presence of any switch name starting with x. */
2657 for (i
= 0; i
< n_switches
; i
++)
2659 unsigned hard_match_len
= p
- filter
- 1;
2661 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
2663 switches
[i
].valid
= 1;
2668 /* Otherwise, check for presence of exact name specified. */
2671 for (i
= 0; i
< n_switches
; i
++)
2673 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
2674 && switches
[i
].part1
[p
- filter
] == 0)
2676 switches
[i
].valid
= 1;
2683 /* If it is as desired (present for %{s...}, absent for %{-s...})
2684 then substitute either the switch or the specified
2685 conditional text. */
2686 if (present
!= negate
)
2694 if (do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL
) < 0)
2700 /* Here if a %{|...} conditional fails: output a minus sign,
2701 which means "standard output" or "standard input". */
2702 do_spec_1 ("-", 0, NULL
);
2709 /* Pass a switch to the current accumulating command
2710 in the same form that we received it.
2711 SWITCHNUM identifies the switch; it is an index into
2712 the vector of switches gcc received, which is `switches'.
2713 This cannot fail since it never finishes a command line.
2715 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2718 give_switch (switchnum
, omit_first_word
)
2720 int omit_first_word
;
2722 if (!omit_first_word
)
2724 do_spec_1 ("-", 0, NULL
);
2725 do_spec_1 (switches
[switchnum
].part1
, 1, NULL
);
2727 do_spec_1 (" ", 0, NULL
);
2728 if (switches
[switchnum
].args
!= 0)
2731 for (p
= switches
[switchnum
].args
; *p
; p
++)
2733 do_spec_1 (*p
, 1, NULL
);
2734 do_spec_1 (" ", 0, NULL
);
2737 switches
[switchnum
].valid
= 1;
2740 /* Search for a file named NAME trying various prefixes including the
2741 user's -B prefix and some standard ones.
2742 Return the absolute file name found. If nothing is found, return NAME. */
2750 newname
= find_a_file (&startfile_prefix
, name
, R_OK
);
2751 return newname
? newname
: name
;
2754 /* Determine whether a -L option is relevant. Not required for certain
2755 fixed names and for directories that don't exist. */
2758 is_linker_dir (path1
, path2
)
2762 int len1
= strlen (path1
);
2763 int len2
= strlen (path2
);
2764 char *path
= (char *) alloca (3 + len1
+ len2
);
2768 /* Construct the path from the two parts. Ensure the string ends with "/.".
2769 The resulting path will be a directory even if the given path is a
2771 bcopy (path1
, path
, len1
);
2772 bcopy (path2
, path
+ len1
, len2
);
2773 cp
= path
+ len1
+ len2
;
2779 /* Exclude directories that the linker is known to search. */
2780 if ((cp
- path
== 6 && strcmp (path
, "/lib/.") == 0)
2781 || (cp
- path
== 10 && strcmp (path
, "/usr/lib/.") == 0))
2784 return (stat (path
, &st
) >= 0 && S_ISDIR (st
.st_mode
));
2787 /* On fatal signals, delete all the temporary files. */
2790 fatal_error (signum
)
2793 signal (signum
, SIG_DFL
);
2794 delete_failure_queue ();
2795 delete_temp_files ();
2796 /* Get the same signal again, this time not handled,
2797 so its normal effect occurs. */
2798 kill (getpid (), signum
);
2808 int error_count
= 0;
2809 int linker_was_run
= 0;
2810 char *explicit_link_files
;
2813 programname
= argv
[0];
2815 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
2816 signal (SIGINT
, fatal_error
);
2817 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
2818 signal (SIGHUP
, fatal_error
);
2819 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
2820 signal (SIGTERM
, fatal_error
);
2822 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
2823 signal (SIGPIPE
, fatal_error
);
2827 argbuf
= (char **) xmalloc (argbuf_length
* sizeof (char *));
2829 obstack_init (&obstack
);
2831 /* Choose directory for temp files. */
2833 choose_temp_base ();
2835 /* Make a table of what switches there are (switches, n_switches).
2836 Make a table of specified input files (infiles, n_infiles).
2837 Decode switches that are handled locally. */
2839 process_command (argc
, argv
);
2841 /* Initialize the vector of specs to just the default.
2842 This means one element containing 0s, as a terminator. */
2844 compilers
= (struct compiler
*) xmalloc (sizeof default_compilers
);
2845 bcopy (default_compilers
, compilers
, sizeof default_compilers
);
2846 n_compilers
= n_default_compilers
;
2848 /* Read specs from a file if there is one. */
2850 machine_suffix
= concat (spec_machine
, "/", concat (spec_version
, "/", ""));
2852 specs_file
= find_a_file (&startfile_prefix
, "specs", R_OK
);
2853 /* Read the specs file unless it is a default one. */
2854 if (specs_file
!= 0 && strcmp (specs_file
, "specs"))
2855 read_specs (specs_file
);
2857 /* If not cross-compiling, look for startfiles in the standard places. */
2858 /* The fact that these are done here, after reading the specs file,
2859 means that it cannot be found in these directories.
2860 But that's okay. It should never be there anyway. */
2863 #ifdef MD_EXEC_PREFIX
2864 add_prefix (&exec_prefix
, md_exec_prefix
, 0, 0, 0);
2865 add_prefix (&startfile_prefix
, md_exec_prefix
, 0, 0, 0);
2868 #ifdef MD_STARTFILE_PREFIX
2869 add_prefix (&startfile_prefix
, md_startfile_prefix
, 0, 0, 0);
2872 add_prefix (&startfile_prefix
, standard_startfile_prefix
, 0, 0, 0);
2873 add_prefix (&startfile_prefix
, standard_startfile_prefix_1
, 0, 0, 0);
2874 add_prefix (&startfile_prefix
, standard_startfile_prefix_2
, 0, 0, 0);
2875 #if 0 /* Can cause surprises, and one can use -B./ instead. */
2876 add_prefix (&startfile_prefix
, "./", 0, 1, 0);
2880 /* Now we have the specs.
2881 Set the `valid' bits for switches that match anything in any spec. */
2883 validate_all_switches ();
2885 /* Warn about any switches that no pass was interested in. */
2887 for (i
= 0; i
< n_switches
; i
++)
2888 if (! switches
[i
].valid
)
2889 error ("unrecognized option `-%s'", switches
[i
].part1
);
2891 /* Obey some of the options. */
2895 fprintf (stderr
, "gcc version %s\n", version_string
);
2901 fatal ("No input files specified.");
2903 /* Make a place to record the compiler output file names
2904 that correspond to the input files. */
2906 outfiles
= (char **) xmalloc (n_infiles
* sizeof (char *));
2907 bzero (outfiles
, n_infiles
* sizeof (char *));
2909 /* Record which files were specified explicitly as link input. */
2911 explicit_link_files
= xmalloc (n_infiles
);
2912 bzero (explicit_link_files
, n_infiles
);
2914 for (i
= 0; i
< n_infiles
; i
++)
2916 register struct compiler
*cp
= 0;
2917 int this_file_error
= 0;
2919 /* Tell do_spec what to substitute for %i. */
2921 input_filename
= infiles
[i
].name
;
2922 input_filename_length
= strlen (input_filename
);
2923 input_file_number
= i
;
2925 /* Use the same thing in %o, unless cp->spec says otherwise. */
2927 outfiles
[i
] = input_filename
;
2929 /* Figure out which compiler from the file's suffix. */
2931 cp
= lookup_compiler (infiles
[i
].name
, input_filename_length
,
2932 infiles
[i
].language
);
2936 /* Ok, we found an applicable compiler. Run its spec. */
2937 /* First say how much of input_filename to substitute for %b */
2940 input_basename
= input_filename
;
2941 for (p
= input_filename
; *p
; p
++)
2943 input_basename
= p
+ 1;
2945 /* Find a suffix starting with the last period,
2946 and set basename_length to exclude that suffix. */
2947 basename_length
= strlen (input_basename
);
2948 p
= input_basename
+ basename_length
;
2949 while (p
!= input_basename
&& *p
!= '.') --p
;
2950 if (*p
== '.' && p
!= input_basename
)
2952 basename_length
= p
- input_basename
;
2953 input_suffix
= p
+ 1;
2958 value
= do_spec (cp
->spec
);
2960 this_file_error
= 1;
2963 /* If this file's name does not contain a recognized suffix,
2964 record it as explicit linker input. */
2967 explicit_link_files
[i
] = 1;
2969 /* Clear the delete-on-failure queue, deleting the files in it
2970 if this compilation failed. */
2972 if (this_file_error
)
2974 delete_failure_queue ();
2977 /* If this compilation succeeded, don't delete those files later. */
2978 clear_failure_queue ();
2981 /* Run ld to link all the compiler output files. */
2983 if (error_count
== 0)
2985 int tmp
= execution_count
;
2986 value
= do_spec (link_command_spec
);
2989 linker_was_run
= (tmp
!= execution_count
);
2992 /* Warn if a -B option was specified but the prefix was never used. */
2993 unused_prefix_warnings (&exec_prefix
);
2994 unused_prefix_warnings (&startfile_prefix
);
2996 /* If options said don't run linker,
2997 complain about input files to be given to the linker. */
2999 if (! linker_was_run
&& error_count
== 0)
3000 for (i
= 0; i
< n_infiles
; i
++)
3001 if (explicit_link_files
[i
])
3002 error ("%s: linker input file unused since linking not done",
3005 /* Delete some or all of the temporary files we made. */
3008 delete_failure_queue ();
3009 delete_temp_files ();
3016 /* Find the proper compilation spec for the file name NAME,
3017 whose length is LENGTH. LANGUAGE is the specified language,
3018 or 0 if none specified. */
3020 static struct compiler
*
3021 lookup_compiler (name
, length
, language
)
3026 struct compiler
*cp
;
3028 /* Look for the language, if one is spec'd. */
3031 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3035 if (cp
->suffix
[0] == '@'
3036 && !strcmp (cp
->suffix
+ 1, language
))
3040 error ("language %s not recognized", language
);
3043 /* Look for a suffix. */
3044 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3046 if (strlen (cp
->suffix
) < length
3047 /* See if the suffix matches the end of NAME. */
3048 && !strcmp (cp
->suffix
,
3049 name
+ length
- strlen (cp
->suffix
))
3050 /* The suffix `-' matches only the file name `-'. */
3051 && !(!strcmp (cp
->suffix
, "-") && length
!= 1))
3053 if (cp
->spec
[0] == '@')
3055 struct compiler
*new;
3056 /* An alias entry maps a suffix to a language.
3057 Search for the language; pass 0 for NAME and LENGTH
3058 to avoid infinite recursion if language not found.
3059 Construct the new compiler spec. */
3060 language
= cp
->spec
+ 1;
3061 new = (struct compiler
*) xmalloc (sizeof (struct compiler
));
3062 new->suffix
= cp
->suffix
;
3063 new->spec
= lookup_compiler (0, 0, language
)->spec
;
3066 /* A non-alias entry: return it. */
3078 register char *value
= (char *) malloc (size
);
3080 fatal ("virtual memory exhausted");
3085 xrealloc (ptr
, size
)
3089 register char *value
= (char *) realloc (ptr
, size
);
3091 fatal ("virtual memory exhausted");
3095 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3101 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3102 char *result
= xmalloc (len1
+ len2
+ len3
+ 1);
3104 strcpy (result
, s1
);
3105 strcpy (result
+ len1
, s2
);
3106 strcpy (result
+ len1
+ len2
, s3
);
3107 *(result
+ len1
+ len2
+ len3
) = 0;
3113 save_string (s
, len
)
3117 register char *result
= xmalloc (len
+ 1);
3119 bcopy (s
, result
, len
);
3125 pfatal_with_name (name
)
3130 if (errno
< sys_nerr
)
3131 s
= concat ("%s: ", sys_errlist
[errno
], "");
3133 s
= "cannot open %s";
3138 perror_with_name (name
)
3143 if (errno
< sys_nerr
)
3144 s
= concat ("%s: ", sys_errlist
[errno
], "");
3146 s
= "cannot open %s";
3156 if (errno
< sys_nerr
)
3157 s
= concat ("installation problem, cannot exec %s: ",
3158 sys_errlist
[errno
], "");
3160 s
= "installation problem, cannot exec %s";
3164 /* More 'friendly' abort that prints the line and file.
3165 config.h can #define abort fancy_abort if you like that sort of thing. */
3170 fatal ("Internal gcc abort.");
3175 /* Output an error message and exit */
3185 format
= va_arg (ap
, char *);
3186 fprintf (stderr
, "%s: ", programname
);
3187 vfprintf (stderr
, format
, ap
);
3189 fprintf (stderr
, "\n");
3190 delete_temp_files ();
3202 format
= va_arg (ap
, char *);
3203 fprintf (stderr
, "%s: ", programname
);
3204 vfprintf (stderr
, format
, ap
);
3207 fprintf (stderr
, "\n");
3210 #else /* not HAVE_VPRINTF */
3213 fatal (msg
, arg1
, arg2
)
3214 char *msg
, *arg1
, *arg2
;
3216 error (msg
, arg1
, arg2
);
3217 delete_temp_files ();
3222 error (msg
, arg1
, arg2
)
3223 char *msg
, *arg1
, *arg2
;
3225 fprintf (stderr
, "%s: ", programname
);
3226 fprintf (stderr
, msg
, arg1
, arg2
);
3227 fprintf (stderr
, "\n");
3230 #endif /* not HAVE_VPRINTF */
3234 validate_all_switches ()
3236 struct compiler
*comp
;
3240 for (comp
= compilers
; comp
->spec
; comp
++)
3244 if (c
== '%' && *p
== '{')
3245 /* We have a switch spec. */
3246 validate_switches (p
+ 1);
3249 p
= link_command_spec
;
3251 if (c
== '%' && *p
== '{')
3252 /* We have a switch spec. */
3253 validate_switches (p
+ 1);
3255 /* Now notice switches mentioned in the machine-specific specs. */
3259 if (c
== '%' && *p
== '{')
3260 /* We have a switch spec. */
3261 validate_switches (p
+ 1);
3265 if (c
== '%' && *p
== '{')
3266 /* We have a switch spec. */
3267 validate_switches (p
+ 1);
3271 if (c
== '%' && *p
== '{')
3272 /* We have a switch spec. */
3273 validate_switches (p
+ 1);
3275 p
= signed_char_spec
;
3277 if (c
== '%' && *p
== '{')
3278 /* We have a switch spec. */
3279 validate_switches (p
+ 1);
3283 if (c
== '%' && *p
== '{')
3284 /* We have a switch spec. */
3285 validate_switches (p
+ 1);
3289 if (c
== '%' && *p
== '{')
3290 /* We have a switch spec. */
3291 validate_switches (p
+ 1);
3295 if (c
== '%' && *p
== '{')
3296 /* We have a switch spec. */
3297 validate_switches (p
+ 1);
3301 if (c
== '%' && *p
== '{')
3302 /* We have a switch spec. */
3303 validate_switches (p
+ 1);
3307 if (c
== '%' && *p
== '{')
3308 /* We have a switch spec. */
3309 validate_switches (p
+ 1);
3312 /* Look at the switch-name that comes after START
3313 and mark as valid all supplied switches that match it. */
3316 validate_switches (start
)
3319 register char *p
= start
;
3334 while (*p
!= ':' && *p
!= '}') p
++;
3338 else if (p
[-1] == '*')
3340 /* Mark all matching switches as valid. */
3342 for (i
= 0; i
< n_switches
; i
++)
3343 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
3344 switches
[i
].valid
= 1;
3348 /* Mark an exact matching switch as valid. */
3349 for (i
= 0; i
< n_switches
; i
++)
3351 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
3352 && switches
[i
].part1
[p
- filter
] == 0)
3353 switches
[i
].valid
= 1;