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". */
33 #include <sys/types.h>
37 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
50 /* Define a generic NULL if one hasn't already been defined. */
57 #if defined (USE_PROTOTYPES) ? USE_PROTOTYPES : defined (__STDC__)
58 #define GENERIC_PTR void *
60 #define GENERIC_PTR char *
65 #define NULL_PTR ((GENERIC_PTR)0)
72 /* On MSDOS, write temp files in current dir
73 because there's no place else we can expect to use. */
80 /* Test if something is a normal file. */
82 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
85 /* Test if something is a directory. */
87 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
90 /* By default there is no special suffix for executables. */
91 #ifndef EXECUTABLE_SUFFIX
92 #define EXECUTABLE_SUFFIX ""
95 /* By default, colon separates directories in a path. */
96 #ifndef PATH_SEPARATOR
97 #define PATH_SEPARATOR ':'
100 #define obstack_chunk_alloc xmalloc
101 #define obstack_chunk_free free
104 extern char *getenv ();
106 extern int errno
, sys_nerr
;
107 extern char *sys_errlist
[];
109 extern int execv (), execvp ();
111 /* If a stage of compilation returns an exit status >= 1,
112 compilation of that file ceases. */
114 #define MIN_FATAL_STATUS 1
116 /* Flag saying to print the full filename of libgcc.a
117 as found through our usual search mechanism. */
119 static int print_libgcc_file_name
;
121 /* Flag indicating whether we should print the command and arguments */
123 static int verbose_flag
;
125 /* Nonzero means write "temp" files in source directory
126 and use the source file's name in them, and don't delete them. */
128 static int save_temps_flag
;
130 /* The compiler version specified with -V */
132 static char *spec_version
;
134 /* The target machine specified with -b. */
136 static char *spec_machine
= DEFAULT_TARGET_MACHINE
;
138 /* Nonzero if cross-compiling.
139 When -b is used, the value comes from the `specs' file. */
142 static int cross_compile
= 1;
144 static int cross_compile
= 0;
147 /* This is the obstack which we use to allocate many strings. */
149 static struct obstack obstack
;
151 /* This is the obstack to build an environment variable to pass to
152 collect2 that describes all of the relevant switches of what to
153 pass the compiler in building the list of pointers to constructors
156 static struct obstack collect_obstack
;
158 extern char *version_string
;
160 static void set_spec ();
161 static struct compiler
*lookup_compiler ();
162 static char *find_a_file ();
163 static void add_prefix ();
164 static char *skip_whitespace ();
165 static void record_temp_file ();
166 static char *handle_braces ();
167 static char *save_string ();
168 static char *concat ();
169 static int do_spec ();
170 static int do_spec_1 ();
171 static char *find_file ();
172 static int is_linker_dir ();
173 static void validate_switches ();
174 static void validate_all_switches ();
175 static void give_switch ();
176 static void pfatal_with_name ();
177 static void perror_with_name ();
178 static void perror_exec ();
179 static void fatal ();
180 static void error ();
185 /* Specs are strings containing lines, each of which (if not blank)
186 is made up of a program name, and arguments separated by spaces.
187 The program name must be exact and start from root, since no path
188 is searched and it is unreliable to depend on the current working directory.
189 Redirection of input or output is not supported; the subprograms must
190 accept filenames saying what files to read and write.
192 In addition, the specs can contain %-sequences to substitute variable text
193 or for conditional text. Here is a table of all defined %-sequences.
194 Note that spaces are not generated automatically around the results of
195 expanding these sequences; therefore, you can concatenate them together
196 or with constant text in a single argument.
198 %% substitute one % into the program name or argument.
199 %i substitute the name of the input file being processed.
200 %b substitute the basename of the input file being processed.
201 This is the substring up to (and not including) the last period
202 and not including the directory.
203 %g substitute the temporary-file-name-base. This is a string chosen
204 once per compilation. Different temporary file names are made by
205 concatenation of constant strings on the end, as in `%g.s'.
206 %g also has the same effect of %d.
207 %u like %g, but make the temporary file name unique.
208 %U returns the last file name generated with %u.
209 %d marks the argument containing or following the %d as a
210 temporary file name, so that that file will be deleted if CC exits
211 successfully. Unlike %g, this contributes no text to the argument.
212 %w marks the argument containing or following the %w as the
213 "output file" of this compilation. This puts the argument
214 into the sequence of arguments that %o will substitute later.
216 like %{...} but mark last argument supplied within
217 as a file to be deleted on failure.
218 %o substitutes the names of all the output files, with spaces
219 automatically placed around them. You should write spaces
220 around the %o as well or the results are undefined.
221 %o is for use in the specs for running the linker.
222 Input files whose names have no recognized suffix are not compiled
223 at all, but they are included among the output files, so they will
225 %p substitutes the standard macro predefinitions for the
226 current target machine. Use this when running cpp.
227 %P like %p, but puts `__' before and after the name of each macro.
228 (Except macros that already have __.)
230 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
231 %s current argument is the name of a library or startup file of some sort.
232 Search for that file in a standard list of directories
233 and substitute the full name found.
234 %eSTR Print STR as an error message. STR is terminated by a newline.
235 Use this when inconsistent options are detected.
236 %x{OPTION} Accumulate an option for %X.
237 %X Output the accumulated linker options specified by compilations.
238 %Y Output the accumulated assembler options specified by compilations.
239 %a process ASM_SPEC as a spec.
240 This allows config.h to specify part of the spec for running as.
241 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
242 used here. This can be used to run a post-processor after the
243 assembler has done it's job.
244 %D Dump out a -L option for each directory in library_prefix,
245 followed by a -L option for each directory in startfile_prefix.
246 %l process LINK_SPEC as a spec.
247 %L process LIB_SPEC as a spec.
248 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
249 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
250 %c process SIGNED_CHAR_SPEC as a spec.
251 %C process CPP_SPEC as a spec. A capital C is actually used here.
252 %1 process CC1_SPEC as a spec.
253 %2 process CC1PLUS_SPEC as a spec.
254 %| output "-" if the input for the current command is coming from a pipe.
255 %* substitute the variable part of a matched option. (See below.)
256 Note that each comma in the substituted string is replaced by
258 %{S} substitutes the -S switch, if that switch was given to CC.
259 If that switch was not specified, this substitutes nothing.
260 Here S is a metasyntactic variable.
261 %{S*} substitutes all the switches specified to CC whose names start
262 with -S. This is used for -o, -D, -I, etc; switches that take
263 arguments. CC considers `-o foo' as being one switch whose
264 name starts with `o'. %{o*} would substitute this text,
265 including the space; thus, two arguments would be generated.
266 %{S*:X} substitutes X if one or more switches whose names start with -S are
267 specified to CC. Note that the tail part of the -S option
268 (i.e. the part matched by the `*') will be substituted for each
269 occurrence of %* within X.
270 %{S:X} substitutes X, but only if the -S switch was given to CC.
271 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
272 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
273 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
274 %{.S:X} substitutes X, but only if processing a file with suffix S.
275 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
276 %(Spec) processes a specification defined in a specs file as *Spec:
277 %[Spec] as above, but put __ around -D arguments
279 The conditional text X in a %{S:X} or %{!S:X} construct may contain
280 other nested % constructs or spaces, or even newlines. They are
281 processed as usual, as described above.
283 The character | is used to indicate that a command should be piped to
284 the following command, but only if -pipe is specified.
286 Note that it is built into CC which switches take arguments and which
287 do not. You might think it would be useful to generalize this to
288 allow each compiler's spec to say which switches take arguments. But
289 this cannot be done in a consistent fashion. CC cannot even decide
290 which input files have been specified without knowing which switches
291 take arguments, and it must know which input files to compile in order
292 to tell which compilers to run.
294 CC also knows implicitly that arguments starting in `-l' are to be
295 treated as compiler output files, and passed to the linker in their
296 proper position among the other output files. */
298 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
300 /* config.h can define ASM_SPEC to provide extra args to the assembler
301 or extra switch-translations. */
306 /* config.h can define ASM_FINAL_SPEC to run a post processor after
307 the assembler has run. */
308 #ifndef ASM_FINAL_SPEC
309 #define ASM_FINAL_SPEC ""
312 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
313 or extra switch-translations. */
318 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
319 or extra switch-translations. */
324 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
325 or extra switch-translations. */
327 #define CC1PLUS_SPEC ""
330 /* config.h can define LINK_SPEC to provide extra args to the linker
331 or extra switch-translations. */
336 /* config.h can define LIB_SPEC to override the default libraries. */
338 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
341 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
342 #ifndef STARTFILE_SPEC
343 #define STARTFILE_SPEC \
344 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
347 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
348 Make the string nonempty to require spaces there. */
349 #ifndef SWITCHES_NEED_SPACES
350 #define SWITCHES_NEED_SPACES ""
353 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
355 #define ENDFILE_SPEC ""
358 /* This spec is used for telling cpp whether char is signed or not. */
359 #ifndef SIGNED_CHAR_SPEC
360 /* Use #if rather than ?:
361 because MIPS C compiler rejects like ?: in initializers. */
362 #if DEFAULT_SIGNED_CHAR
363 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
365 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
369 static char *cpp_spec
= CPP_SPEC
;
370 static char *cpp_predefines
= CPP_PREDEFINES
;
371 static char *cc1_spec
= CC1_SPEC
;
372 static char *cc1plus_spec
= CC1PLUS_SPEC
;
373 static char *signed_char_spec
= SIGNED_CHAR_SPEC
;
374 static char *asm_spec
= ASM_SPEC
;
375 static char *asm_final_spec
= ASM_FINAL_SPEC
;
376 static char *link_spec
= LINK_SPEC
;
377 static char *lib_spec
= LIB_SPEC
;
378 static char *endfile_spec
= ENDFILE_SPEC
;
379 static char *startfile_spec
= STARTFILE_SPEC
;
380 static char *switches_need_spaces
= SWITCHES_NEED_SPACES
;
382 /* This defines which switch letters take arguments. */
384 #ifndef SWITCH_TAKES_ARG
385 #define SWITCH_TAKES_ARG(CHAR) \
386 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
387 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
388 || (CHAR) == 'I' || (CHAR) == 'm' \
389 || (CHAR) == 'L' || (CHAR) == 'A')
392 /* This defines which multi-letter switches take arguments. */
394 #ifndef WORD_SWITCH_TAKES_ARG
395 #define WORD_SWITCH_TAKES_ARG(STR) \
396 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
397 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
398 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info"))
401 /* Record the mapping from file suffixes for compilation specs. */
405 char *suffix
; /* Use this compiler for input files
406 whose names end in this suffix. */
408 char *spec
[4]; /* To use this compiler, concatenate these
409 specs and pass to do_spec. */
412 /* Pointer to a vector of `struct compiler' that gives the spec for
413 compiling a file, based on its suffix.
414 A file that does not end in any of these suffixes will be passed
415 unchanged to the loader and nothing else will be done to it.
417 An entry containing two 0s is used to terminate the vector.
419 If multiple entries match a file, the last matching one is used. */
421 static struct compiler
*compilers
;
423 /* Number of entries in `compilers', not counting the null terminator. */
425 static int n_compilers
;
427 /* The default list of file name suffixes and their compilation specs. */
429 static struct compiler default_compilers
[] =
433 "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
434 %{C:%{!E:%eGNU C does not support -C without using -E}}\
435 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
436 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
437 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
438 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
439 %{traditional-cpp:-traditional}\
440 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
441 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
442 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
443 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
444 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
445 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
447 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
448 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
449 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
450 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
451 %{!pipe:%g.s} %A\n }}}}"},
453 "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
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 %{D*} %{U*} %{i*}\
462 %{!E:%e-E required when input is from standard input}"},
463 {".m", "@objective-c"},
465 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
466 %{C:%{!E:%eGNU C does not support -C without using -E}}\
467 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
468 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
469 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
470 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
471 %{traditional-cpp:-traditional}\
472 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
473 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
474 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
475 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
476 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
477 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
478 -lang-objc %{gen-decls} \
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 %Y\
483 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
484 %{!pipe:%g.s} %A\n }}}}"},
487 "%{!E:%eCompilation of header file requested} \
488 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
489 %{C:%{!E:%eGNU C does not support -C without using -E}}\
490 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
491 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
492 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
493 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
494 %{traditional-cpp:-traditional}\
495 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
501 "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
502 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
503 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
504 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
505 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
506 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
507 %{traditional-cpp:-traditional} %{trigraphs}\
508 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
509 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
510 "%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
511 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
512 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
513 %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
515 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
516 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
517 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
518 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
519 %{!pipe:%g.s} %A\n }}}}"},
520 {".i", "@cpp-output"},
522 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
523 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
524 %{v:-version} %{pg:-p} %{p} %{f*}\
526 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
527 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
528 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
529 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %{!pipe:%g.s} %A\n }"},
530 {".ii", "@c++-cpp-output"},
532 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
533 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
534 %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
536 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
537 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
538 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
539 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
540 %{!pipe:%g.s} %A\n }"},
541 {".s", "@assembler"},
543 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
544 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %i %A\n }"},
545 {".S", "@assembler-with-cpp"},
546 {"@assembler-with-cpp",
547 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
548 %{C:%{!E:%eGNU C does not support -C without using -E}}\
549 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
550 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
551 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
552 %{traditional-cpp:-traditional}\
553 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
554 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
555 "%{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
556 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
557 %{!pipe:%g.s} %A\n }}}}"},
558 /* Mark end of table */
562 /* Number of elements in default_compilers, not counting the terminator. */
564 static int n_default_compilers
565 = (sizeof default_compilers
/ sizeof (struct compiler
)) - 1;
567 /* Here is the spec for running the linker, after compiling all files. */
569 /* -u* was put back because both BSD and SysV seem to support it. */
570 /* %{static:} simply prevents an error message if the target machine
571 doesn't handle -static. */
572 #ifdef LINK_LIBGCC_SPECIAL_1
573 /* Have gcc do the search for libgcc.a, but generate -L options as usual. */
574 static char *link_command_spec
= "\
575 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
576 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
577 %{!A:%{!nostdlib:%S}} %{static:}\
578 %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
580 #ifdef LINK_LIBGCC_SPECIAL
581 /* Have gcc do the search for libgcc.a, and don't generate -L options. */
582 static char *link_command_spec
= "\
583 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
584 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
585 %{!A:%{!nostdlib:%S}} %{static:}\
586 %{L*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
588 /* Use -L and have the linker do the search for -lgcc. */
589 static char *link_command_spec
= "\
590 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
591 %{r} %{s} %{T*} %{t} %{u*} %{x} %{z}\
592 %{!A:%{!nostdlib:%S}} %{static:}\
593 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
597 /* A vector of options to give to the linker.
598 These options are accumulated by -Xlinker and -Wl,
599 and substituted into the linker command with %X. */
600 static int n_linker_options
;
601 static char **linker_options
;
603 /* A vector of options to give to the assembler.
604 These options are accumulated by -Wa,
605 and substituted into the assembler command with %X. */
606 static int n_assembler_options
;
607 static char **assembler_options
;
609 /* Read compilation specs from a file named FILENAME,
610 replacing the default ones.
612 A suffix which starts with `*' is a definition for
613 one of the machine-specific sub-specs. The "suffix" should be
614 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
615 The corresponding spec is stored in asm_spec, etc.,
616 rather than in the `compilers' vector.
618 Anything invalid in the file is a fatal error. */
621 read_specs (filename
)
630 fprintf (stderr
, "Reading specs from %s\n", filename
);
632 /* Open and stat the file. */
633 desc
= open (filename
, 0, 0);
635 pfatal_with_name (filename
);
636 if (stat (filename
, &statbuf
) < 0)
637 pfatal_with_name (filename
);
639 /* Read contents of file into BUFFER. */
640 buffer
= xmalloc ((unsigned) statbuf
.st_size
+ 1);
641 read (desc
, buffer
, (unsigned) statbuf
.st_size
);
642 buffer
[statbuf
.st_size
] = 0;
645 /* Scan BUFFER for specs, putting them in the vector. */
651 char *in
, *out
, *p1
, *p2
;
653 /* Advance P in BUFFER to the next nonblank nocomment line. */
654 p
= skip_whitespace (p
);
658 /* Find the colon that should end the suffix. */
660 while (*p1
&& *p1
!= ':' && *p1
!= '\n') p1
++;
661 /* The colon shouldn't be missing. */
663 fatal ("specs file malformed after %d characters", p1
- buffer
);
664 /* Skip back over trailing whitespace. */
666 while (p2
> buffer
&& (p2
[-1] == ' ' || p2
[-1] == '\t')) p2
--;
667 /* Copy the suffix to a string. */
668 suffix
= save_string (p
, p2
- p
);
669 /* Find the next line. */
670 p
= skip_whitespace (p1
+ 1);
672 fatal ("specs file malformed after %d characters", p
- buffer
);
674 /* Find next blank line. */
675 while (*p1
&& !(*p1
== '\n' && p1
[1] == '\n')) p1
++;
676 /* Specs end at the blank line and do not include the newline. */
677 spec
= save_string (p
, p1
- p
);
680 /* Delete backslash-newline sequences from the spec. */
685 if (in
[0] == '\\' && in
[1] == '\n')
687 else if (in
[0] == '#')
689 while (*in
&& *in
!= '\n') in
++;
696 if (suffix
[0] == '*')
698 if (! strcmp (suffix
, "*link_command"))
699 link_command_spec
= spec
;
701 set_spec (suffix
+ 1, spec
);
705 /* Add this pair to the vector. */
707 = ((struct compiler
*)
708 xrealloc (compilers
, (n_compilers
+ 2) * sizeof (struct compiler
)));
709 compilers
[n_compilers
].suffix
= suffix
;
710 bzero (compilers
[n_compilers
].spec
,
711 sizeof compilers
[n_compilers
].spec
);
712 compilers
[n_compilers
].spec
[0] = spec
;
717 link_command_spec
= spec
;
720 if (link_command_spec
== 0)
721 fatal ("spec file has no spec for linking");
730 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
731 be considered whitespace. */
732 if (p
[0] == '\n' && p
[1] == '\n' && p
[2] == '\n')
734 else if (*p
== '\n' || *p
== ' ' || *p
== '\t')
738 while (*p
!= '\n') p
++;
748 /* Structure to keep track of the specs that have been defined so far. These
749 are accessed using %(specname) or %[specname] in a compiler or link spec. */
753 char *name
; /* Name of the spec. */
754 char *spec
; /* The spec itself. */
755 struct spec_list
*next
; /* Next spec in linked list. */
758 /* List of specs that have been defined so far. */
760 static struct spec_list
*specs
= (struct spec_list
*) 0;
762 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
763 removed; If the spec starts with a + then SPEC is added to the end of the
767 set_spec (name
, spec
)
771 struct spec_list
*sl
;
774 /* See if the spec already exists */
775 for (sl
= specs
; sl
; sl
= sl
->next
)
776 if (strcmp (sl
->name
, name
) == 0)
781 /* Not found - make it */
782 sl
= (struct spec_list
*) xmalloc (sizeof (struct spec_list
));
783 sl
->name
= save_string (name
, strlen (name
));
784 sl
->spec
= save_string ("", 0);
790 if (name
&& spec
[0] == '+' && isspace (spec
[1]))
791 sl
->spec
= concat (old_spec
, spec
+ 1, "");
793 sl
->spec
= save_string (spec
, strlen (spec
));
795 if (! strcmp (name
, "asm"))
797 else if (! strcmp (name
, "asm_final"))
798 asm_final_spec
= sl
->spec
;
799 else if (! strcmp (name
, "cc1"))
801 else if (! strcmp (name
, "cc1plus"))
802 cc1plus_spec
= sl
->spec
;
803 else if (! strcmp (name
, "cpp"))
805 else if (! strcmp (name
, "endfile"))
806 endfile_spec
= sl
->spec
;
807 else if (! strcmp (name
, "lib"))
809 else if (! strcmp (name
, "link"))
810 link_spec
= sl
->spec
;
811 else if (! strcmp (name
, "predefines"))
812 cpp_predefines
= sl
->spec
;
813 else if (! strcmp (name
, "signed_char"))
814 signed_char_spec
= sl
->spec
;
815 else if (! strcmp (name
, "startfile"))
816 startfile_spec
= sl
->spec
;
817 else if (! strcmp (name
, "switches_need_spaces"))
818 switches_need_spaces
= sl
->spec
;
819 else if (! strcmp (name
, "cross_compile"))
820 cross_compile
= atoi (sl
->spec
);
821 /* Free the old spec */
826 /* Accumulate a command (program name and args), and run it. */
828 /* Vector of pointers to arguments in the current line of specifications. */
830 static char **argbuf
;
832 /* Number of elements allocated in argbuf. */
834 static int argbuf_length
;
836 /* Number of elements in argbuf currently in use (containing args). */
838 static int argbuf_index
;
840 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
841 temp file. Used only if MKTEMP_EACH_FILE. */
843 static struct temp_name
{
844 char *suffix
; /* suffix associated with the code. */
845 int length
; /* strlen (suffix). */
846 int unique
; /* Indicates whether %g or %u/%U was used. */
847 char *filename
; /* associated filename. */
848 int filename_length
; /* strlen (filename). */
849 struct temp_name
*next
;
852 /* Number of commands executed so far. */
854 static int execution_count
;
856 /* Number of commands that exited with a signal. */
858 static int signal_count
;
860 /* Name with which this program was invoked. */
862 static char *programname
;
864 /* Structures to keep track of prefixes to try when looking for files. */
868 char *prefix
; /* String to prepend to the path. */
869 struct prefix_list
*next
; /* Next in linked list. */
870 int require_machine_suffix
; /* Don't use without machine_suffix. */
871 /* 2 means try both machine_suffix and just_machine_suffix. */
872 int *used_flag_ptr
; /* 1 if a file was found with this prefix. */
877 struct prefix_list
*plist
; /* List of prefixes to try */
878 int max_len
; /* Max length of a prefix in PLIST */
879 char *name
; /* Name of this list (used in config stuff) */
882 /* List of prefixes to try when looking for executables. */
884 static struct path_prefix exec_prefix
= { 0, 0, "exec" };
886 /* List of prefixes to try when looking for startup (crt0) files. */
888 static struct path_prefix startfile_prefix
= { 0, 0, "startfile" };
890 /* List of prefixes to try when looking for libraries. */
892 static struct path_prefix library_prefix
= { 0, 0, "libraryfile" };
894 /* Suffix to attach to directories searched for commands.
895 This looks like `MACHINE/VERSION/'. */
897 static char *machine_suffix
= 0;
899 /* Suffix to attach to directories searched for commands.
900 This is just `MACHINE/'. */
902 static char *just_machine_suffix
= 0;
904 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
906 static char *gcc_exec_prefix
;
908 /* Default prefixes to attach to command names. */
910 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
911 #undef MD_EXEC_PREFIX
912 #undef MD_STARTFILE_PREFIX
913 #undef MD_STARTFILE_PREFIX_1
916 #ifndef STANDARD_EXEC_PREFIX
917 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
918 #endif /* !defined STANDARD_EXEC_PREFIX */
920 static char *standard_exec_prefix
= STANDARD_EXEC_PREFIX
;
921 static char *standard_exec_prefix_1
= "/usr/lib/gcc/";
922 #ifdef MD_EXEC_PREFIX
923 static char *md_exec_prefix
= MD_EXEC_PREFIX
;
926 #ifndef STANDARD_STARTFILE_PREFIX
927 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
928 #endif /* !defined STANDARD_STARTFILE_PREFIX */
930 #ifdef MD_STARTFILE_PREFIX
931 static char *md_startfile_prefix
= MD_STARTFILE_PREFIX
;
933 #ifdef MD_STARTFILE_PREFIX_1
934 static char *md_startfile_prefix_1
= MD_STARTFILE_PREFIX_1
;
936 static char *standard_startfile_prefix
= STANDARD_STARTFILE_PREFIX
;
937 static char *standard_startfile_prefix_1
= "/lib/";
938 static char *standard_startfile_prefix_2
= "/usr/lib/";
940 #ifndef TOOLDIR_PREFIX
941 #define TOOLDIR_PREFIX "/usr/local/"
943 static char *tooldir_base_prefix
= TOOLDIR_PREFIX
;
944 static char *tooldir_prefix
;
946 /* Clear out the vector of arguments (after a command is executed). */
954 /* Add one argument to the vector at the end.
955 This is done when a space is seen or at the end of the line.
956 If DELETE_ALWAYS is nonzero, the arg is a filename
957 and the file should be deleted eventually.
958 If DELETE_FAILURE is nonzero, the arg is a filename
959 and the file should be deleted if this compilation fails. */
962 store_arg (arg
, delete_always
, delete_failure
)
964 int delete_always
, delete_failure
;
966 if (argbuf_index
+ 1 == argbuf_length
)
968 argbuf
= (char **) xrealloc (argbuf
, (argbuf_length
*= 2) * sizeof (char *));
971 argbuf
[argbuf_index
++] = arg
;
972 argbuf
[argbuf_index
] = 0;
974 if (delete_always
|| delete_failure
)
975 record_temp_file (arg
, delete_always
, delete_failure
);
978 /* Record the names of temporary files we tell compilers to write,
979 and delete them at the end of the run. */
981 /* This is the common prefix we use to make temp file names.
982 It is chosen once for each run of this program.
983 It is substituted into a spec by %g.
984 Thus, all temp file names contain this prefix.
985 In practice, all temp file names start with this prefix.
987 This prefix comes from the envvar TMPDIR if it is defined;
988 otherwise, from the P_tmpdir macro if that is defined;
989 otherwise, in /usr/tmp or /tmp. */
991 static char *temp_filename
;
993 /* Length of the prefix. */
995 static int temp_filename_length
;
997 /* Define the list of temporary files to delete. */
1002 struct temp_file
*next
;
1005 /* Queue of files to delete on success or failure of compilation. */
1006 static struct temp_file
*always_delete_queue
;
1007 /* Queue of files to delete on failure of compilation. */
1008 static struct temp_file
*failure_delete_queue
;
1010 /* Record FILENAME as a file to be deleted automatically.
1011 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1012 otherwise delete it in any case.
1013 FAIL_DELETE nonzero means delete it if a compilation step fails;
1014 otherwise delete it in any case. */
1017 record_temp_file (filename
, always_delete
, fail_delete
)
1022 register char *name
;
1023 name
= xmalloc (strlen (filename
) + 1);
1024 strcpy (name
, filename
);
1028 register struct temp_file
*temp
;
1029 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
1030 if (! strcmp (name
, temp
->name
))
1032 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
1033 temp
->next
= always_delete_queue
;
1035 always_delete_queue
= temp
;
1041 register struct temp_file
*temp
;
1042 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
1043 if (! strcmp (name
, temp
->name
))
1045 temp
= (struct temp_file
*) xmalloc (sizeof (struct temp_file
));
1046 temp
->next
= failure_delete_queue
;
1048 failure_delete_queue
= temp
;
1053 /* Delete all the temporary files whose names we previously recorded. */
1056 delete_temp_files ()
1058 register struct temp_file
*temp
;
1060 for (temp
= always_delete_queue
; temp
; temp
= temp
->next
)
1064 printf ("Delete %s? (y or n) ", temp
->name
);
1068 while (getchar () != '\n') ;
1069 if (i
== 'y' || i
== 'Y')
1073 if (stat (temp
->name
, &st
) >= 0)
1075 /* Delete only ordinary files. */
1076 if (S_ISREG (st
.st_mode
))
1077 if (unlink (temp
->name
) < 0)
1079 perror_with_name (temp
->name
);
1084 always_delete_queue
= 0;
1087 /* Delete all the files to be deleted on error. */
1090 delete_failure_queue ()
1092 register struct temp_file
*temp
;
1094 for (temp
= failure_delete_queue
; temp
; temp
= temp
->next
)
1098 printf ("Delete %s? (y or n) ", temp
->name
);
1102 while (getchar () != '\n') ;
1103 if (i
== 'y' || i
== 'Y')
1106 if (unlink (temp
->name
) < 0)
1108 perror_with_name (temp
->name
);
1114 clear_failure_queue ()
1116 failure_delete_queue
= 0;
1119 /* Compute a string to use as the base of all temporary file names.
1120 It is substituted for %g. */
1125 char *base
= getenv ("TMPDIR");
1128 if (base
== (char *)0)
1131 if (access (P_tmpdir
, R_OK
| W_OK
) == 0)
1134 if (base
== (char *)0)
1136 if (access ("/usr/tmp", R_OK
| W_OK
) == 0)
1143 len
= strlen (base
);
1144 temp_filename
= xmalloc (len
+ sizeof("/ccXXXXXX"));
1145 strcpy (temp_filename
, base
);
1146 if (len
> 0 && temp_filename
[len
-1] != '/')
1147 temp_filename
[len
++] = '/';
1148 strcpy (temp_filename
+ len
, "ccXXXXXX");
1150 mktemp (temp_filename
);
1151 temp_filename_length
= strlen (temp_filename
);
1152 if (temp_filename_length
== 0)
1157 /* Routine to add variables to the environment. We do this to pass
1158 the pathname of the gcc driver, and the directories search to the
1159 collect2 program, which is being run as ld. This way, we can be
1160 sure of executing the right compiler when collect2 wants to build
1161 constructors and destructors. Since the environment variables we
1162 use come from an obstack, we don't have to worry about allocating
1170 #ifndef VMS /* nor about VMS */
1172 extern char **environ
;
1173 char **old_environ
= environ
;
1177 int str_len
= strlen (str
);
1181 while ((ch
= *p
++) != '\0' && ch
!= '=')
1187 /* Search for replacing an existing environment variable, and
1188 count the number of total environment variables. */
1189 for (envp
= old_environ
; *envp
; envp
++)
1192 if (!strncmp (str
, *envp
, name_len
))
1199 /* Add a new environment variable */
1200 environ
= (char **) xmalloc (sizeof (char *) * (num_envs
+2));
1202 bcopy (old_environ
, environ
+1, sizeof (char *) * (num_envs
+1));
1207 #endif /* HAVE_PUTENV */
1210 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1213 putenv_from_prefixes (paths
, env_var
)
1214 struct path_prefix
*paths
;
1217 int suffix_len
= (machine_suffix
) ? strlen (machine_suffix
) : 0;
1219 = (just_machine_suffix
) ? strlen (just_machine_suffix
) : 0;
1220 int first_time
= TRUE
;
1221 struct prefix_list
*pprefix
;
1223 obstack_grow (&collect_obstack
, env_var
, strlen (env_var
));
1225 for (pprefix
= paths
->plist
; pprefix
!= 0; pprefix
= pprefix
->next
)
1227 int len
= strlen (pprefix
->prefix
);
1232 obstack_1grow (&collect_obstack
, PATH_SEPARATOR
);
1235 obstack_grow (&collect_obstack
, pprefix
->prefix
, len
);
1236 obstack_grow (&collect_obstack
, machine_suffix
, suffix_len
);
1239 if (just_machine_suffix
&& pprefix
->require_machine_suffix
== 2)
1242 obstack_1grow (&collect_obstack
, PATH_SEPARATOR
);
1245 obstack_grow (&collect_obstack
, pprefix
->prefix
, len
);
1246 obstack_grow (&collect_obstack
, just_machine_suffix
,
1250 if (!pprefix
->require_machine_suffix
)
1253 obstack_1grow (&collect_obstack
, PATH_SEPARATOR
);
1256 obstack_grow (&collect_obstack
, pprefix
->prefix
, len
);
1259 obstack_1grow (&collect_obstack
, '\0');
1260 putenv (obstack_finish (&collect_obstack
));
1264 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1265 access to check permissions.
1266 Return 0 if not found, otherwise return its name, allocated with malloc. */
1269 find_a_file (pprefix
, name
, mode
)
1270 struct path_prefix
*pprefix
;
1275 char *file_suffix
= ((mode
& X_OK
) != 0 ? EXECUTABLE_SUFFIX
: "");
1276 struct prefix_list
*pl
;
1277 int len
= pprefix
->max_len
+ strlen (name
) + strlen (file_suffix
) + 1;
1280 len
+= strlen (machine_suffix
);
1282 temp
= xmalloc (len
);
1284 /* Determine the filename to execute (special case for absolute paths). */
1288 if (access (name
, mode
))
1290 strcpy (temp
, name
);
1295 for (pl
= pprefix
->plist
; pl
; pl
= pl
->next
)
1299 strcpy (temp
, pl
->prefix
);
1300 strcat (temp
, machine_suffix
);
1301 strcat (temp
, name
);
1302 if (access (temp
, mode
) == 0)
1304 if (pl
->used_flag_ptr
!= 0)
1305 *pl
->used_flag_ptr
= 1;
1308 /* Some systems have a suffix for executable files.
1309 So try appending that. */
1310 if (file_suffix
[0] != 0)
1312 strcat (temp
, file_suffix
);
1313 if (access (temp
, mode
) == 0)
1315 if (pl
->used_flag_ptr
!= 0)
1316 *pl
->used_flag_ptr
= 1;
1321 /* Certain prefixes are tried with just the machine type,
1322 not the version. This is used for finding as, ld, etc. */
1323 if (just_machine_suffix
&& pl
->require_machine_suffix
== 2)
1325 strcpy (temp
, pl
->prefix
);
1326 strcat (temp
, just_machine_suffix
);
1327 strcat (temp
, name
);
1328 if (access (temp
, mode
) == 0)
1330 if (pl
->used_flag_ptr
!= 0)
1331 *pl
->used_flag_ptr
= 1;
1334 /* Some systems have a suffix for executable files.
1335 So try appending that. */
1336 if (file_suffix
[0] != 0)
1338 strcat (temp
, file_suffix
);
1339 if (access (temp
, mode
) == 0)
1341 if (pl
->used_flag_ptr
!= 0)
1342 *pl
->used_flag_ptr
= 1;
1347 /* Certain prefixes can't be used without the machine suffix
1348 when the machine or version is explicitly specified. */
1349 if (!pl
->require_machine_suffix
)
1351 strcpy (temp
, pl
->prefix
);
1352 strcat (temp
, name
);
1353 if (access (temp
, mode
) == 0)
1355 if (pl
->used_flag_ptr
!= 0)
1356 *pl
->used_flag_ptr
= 1;
1359 /* Some systems have a suffix for executable files.
1360 So try appending that. */
1361 if (file_suffix
[0] != 0)
1363 strcat (temp
, file_suffix
);
1364 if (access (temp
, mode
) == 0)
1366 if (pl
->used_flag_ptr
!= 0)
1367 *pl
->used_flag_ptr
= 1;
1378 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1379 at the start of the list, otherwise it goes at the end.
1381 If WARN is nonzero, we will warn if no file is found
1382 through this prefix. WARN should point to an int
1383 which will be set to 1 if this entry is used.
1385 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1386 the complete value of machine_suffix.
1387 2 means try both machine_suffix and just_machine_suffix. */
1390 add_prefix (pprefix
, prefix
, first
, require_machine_suffix
, warn
)
1391 struct path_prefix
*pprefix
;
1394 int require_machine_suffix
;
1397 struct prefix_list
*pl
, **prev
;
1400 if (!first
&& pprefix
->plist
)
1402 for (pl
= pprefix
->plist
; pl
->next
; pl
= pl
->next
)
1407 prev
= &pprefix
->plist
;
1409 /* Keep track of the longest prefix */
1411 len
= strlen (prefix
);
1412 if (len
> pprefix
->max_len
)
1413 pprefix
->max_len
= len
;
1415 pl
= (struct prefix_list
*) xmalloc (sizeof (struct prefix_list
));
1416 pl
->prefix
= save_string (prefix
, len
);
1417 pl
->require_machine_suffix
= require_machine_suffix
;
1418 pl
->used_flag_ptr
= warn
;
1425 pl
->next
= (struct prefix_list
*) 0;
1429 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1432 unused_prefix_warnings (pprefix
)
1433 struct path_prefix
*pprefix
;
1435 struct prefix_list
*pl
= pprefix
->plist
;
1439 if (pl
->used_flag_ptr
!= 0 && !*pl
->used_flag_ptr
)
1441 error ("file path prefix `%s' never used",
1443 /* Prevent duplicate warnings. */
1444 *pl
->used_flag_ptr
= 1;
1450 /* Get rid of all prefixes built up so far in *PLISTP. */
1453 free_path_prefix (pprefix
)
1454 struct path_prefix
*pprefix
;
1456 struct prefix_list
*pl
= pprefix
->plist
;
1457 struct prefix_list
*temp
;
1463 free (temp
->prefix
);
1464 free ((char *) temp
);
1466 pprefix
->plist
= (struct prefix_list
*) 0;
1469 /* stdin file number. */
1470 #define STDIN_FILE_NO 0
1472 /* stdout file number. */
1473 #define STDOUT_FILE_NO 1
1475 /* value of `pipe': port index for reading. */
1478 /* value of `pipe': port index for writing. */
1479 #define WRITE_PORT 1
1481 /* Pipe waiting from last process, to be used as input for the next one.
1482 Value is STDIN_FILE_NO if no pipe is waiting
1483 (i.e. the next command is the first of a group). */
1485 static int last_pipe_input
;
1487 /* Fork one piped subcommand. FUNC is the system call to use
1488 (either execv or execvp). ARGV is the arg vector to use.
1489 NOT_LAST is nonzero if this is not the last subcommand
1490 (i.e. its output should be piped to the next one.) */
1495 /* Declare these to avoid compilation error. They won't be called. */
1496 int execv(const char *a
, const char **b
){}
1497 int execvp(const char *a
, const char **b
){}
1500 pexecute (search_flag
, program
, argv
, not_last
)
1510 scmd
= (char *)malloc (strlen (program
) + strlen (temp_filename
) + 6);
1511 sprintf (scmd
, "%s @%s.gp", program
, temp_filename
);
1512 argfile
= fopen (scmd
+strlen (program
) + 2, "w");
1514 pfatal_with_name (scmd
+ strlen (program
) + 2);
1516 for (i
=1; argv
[i
]; i
++)
1519 for (cp
= argv
[i
]; *cp
; cp
++)
1521 if (*cp
== '"' || *cp
== '\'' || *cp
== '\\' || isspace (*cp
))
1522 fputc ('\\', argfile
);
1523 fputc (*cp
, argfile
);
1525 fputc ('\n', argfile
);
1531 remove (scmd
+ strlen (program
) + 2);
1535 #else /* not __MSDOS__ */
1538 pexecute (search_flag
, program
, argv
, not_last
)
1544 int (*func
)() = (search_flag
? execv
: execvp
);
1547 int input_desc
= last_pipe_input
;
1548 int output_desc
= STDOUT_FILE_NO
;
1549 int retries
, sleep_interval
;
1551 /* If this isn't the last process, make a pipe for its output,
1552 and record it as waiting to be the input to the next process. */
1556 if (pipe (pdes
) < 0)
1557 pfatal_with_name ("pipe");
1558 output_desc
= pdes
[WRITE_PORT
];
1559 last_pipe_input
= pdes
[READ_PORT
];
1562 last_pipe_input
= STDIN_FILE_NO
;
1564 /* Fork a subprocess; wait and retry if it fails. */
1566 for (retries
= 0; retries
< 4; retries
++)
1571 sleep (sleep_interval
);
1572 sleep_interval
*= 2;
1579 pfatal_with_name ("fork");
1581 pfatal_with_name ("vfork");
1587 /* Move the input and output pipes into place, if nec. */
1588 if (input_desc
!= STDIN_FILE_NO
)
1590 close (STDIN_FILE_NO
);
1594 if (output_desc
!= STDOUT_FILE_NO
)
1596 close (STDOUT_FILE_NO
);
1598 close (output_desc
);
1601 /* Close the parent's descs that aren't wanted here. */
1602 if (last_pipe_input
!= STDIN_FILE_NO
)
1603 close (last_pipe_input
);
1605 /* Exec the program. */
1606 (*func
) (program
, argv
);
1607 perror_exec (program
);
1613 /* In the parent, after forking.
1614 Close the descriptors that we made for this child. */
1615 if (input_desc
!= STDIN_FILE_NO
)
1617 if (output_desc
!= STDOUT_FILE_NO
)
1618 close (output_desc
);
1620 /* Return child's process number. */
1625 #endif /* not __MSDOS__ */
1629 pexecute (search_flag
, program
, argv
, not_last
)
1635 return (search_flag
? spawnv
: spawnvp
) (1, program
, argv
);
1637 #endif /* not OS2 */
1639 /* Execute the command specified by the arguments on the current line of spec.
1640 When using pipes, this includes several piped-together commands
1641 with `|' between them.
1643 Return 0 if successful, -1 if failed. */
1649 int n_commands
; /* # of command. */
1653 char *prog
; /* program name. */
1654 char **argv
; /* vector of args. */
1655 int pid
; /* pid of process for this command. */
1658 struct command
*commands
; /* each command buffer with above info. */
1660 /* Count # of piped commands. */
1661 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1662 if (strcmp (argbuf
[i
], "|") == 0)
1665 /* Get storage for each command. */
1667 = (struct command
*) alloca (n_commands
* sizeof (struct command
));
1669 /* Split argbuf into its separate piped processes,
1670 and record info about each one.
1671 Also search for the programs that are to be run. */
1673 commands
[0].prog
= argbuf
[0]; /* first command. */
1674 commands
[0].argv
= &argbuf
[0];
1675 string
= find_a_file (&exec_prefix
, commands
[0].prog
, X_OK
);
1677 commands
[0].argv
[0] = string
;
1679 for (n_commands
= 1, i
= 0; i
< argbuf_index
; i
++)
1680 if (strcmp (argbuf
[i
], "|") == 0)
1681 { /* each command. */
1683 fatal ("-pipe not supported under MS-DOS");
1685 argbuf
[i
] = 0; /* termination of command args. */
1686 commands
[n_commands
].prog
= argbuf
[i
+ 1];
1687 commands
[n_commands
].argv
= &argbuf
[i
+ 1];
1688 string
= find_a_file (&exec_prefix
, commands
[n_commands
].prog
, X_OK
);
1690 commands
[n_commands
].argv
[0] = string
;
1694 argbuf
[argbuf_index
] = 0;
1696 /* If -v, print what we are about to do, and maybe query. */
1700 /* Print each piped command as a separate line. */
1701 for (i
= 0; i
< n_commands
; i
++)
1705 for (j
= commands
[i
].argv
; *j
; j
++)
1706 fprintf (stderr
, " %s", *j
);
1708 /* Print a pipe symbol after all but the last command. */
1709 if (i
+ 1 != n_commands
)
1710 fprintf (stderr
, " |");
1711 fprintf (stderr
, "\n");
1715 fprintf (stderr
, "\nGo ahead? (y or n) ");
1719 while (getchar () != '\n') ;
1720 if (i
!= 'y' && i
!= 'Y')
1725 /* Run each piped subprocess. */
1727 last_pipe_input
= STDIN_FILE_NO
;
1728 for (i
= 0; i
< n_commands
; i
++)
1730 char *string
= commands
[i
].argv
[0];
1732 commands
[i
].pid
= pexecute (string
!= commands
[i
].prog
,
1733 string
, commands
[i
].argv
,
1734 i
+ 1 < n_commands
);
1736 if (string
!= commands
[i
].prog
)
1742 /* Wait for all the subprocesses to finish.
1743 We don't care what order they finish in;
1744 we know that N_COMMANDS waits will get them all. */
1749 for (i
= 0; i
< n_commands
; i
++)
1756 status
= pid
= commands
[i
].pid
;
1758 pid
= wait (&status
);
1766 for (j
= 0; j
< n_commands
; j
++)
1767 if (commands
[j
].pid
== pid
)
1768 prog
= commands
[j
].prog
;
1770 if ((status
& 0x7F) != 0)
1772 fatal ("Internal compiler error: program %s got fatal signal %d",
1773 prog
, (status
& 0x7F));
1776 if (((status
& 0xFF00) >> 8) >= MIN_FATAL_STATUS
)
1784 /* Find all the switches given to us
1785 and make a vector describing them.
1786 The elements of the vector are strings, one per switch given.
1787 If a switch uses following arguments, then the `part1' field
1788 is the switch itself and the `args' field
1789 is a null-terminated vector containing the following arguments.
1790 The `valid' field is nonzero if any spec has looked at this switch;
1791 if it remains zero at the end of the run, it must be meaningless. */
1800 static struct switchstr
*switches
;
1802 static int n_switches
;
1810 /* Also a vector of input files specified. */
1812 static struct infile
*infiles
;
1814 static int n_infiles
;
1816 /* And a vector of corresponding output files is made up later. */
1818 static char **outfiles
;
1820 /* Create the vector `switches' and its contents.
1821 Store its length in `n_switches'. */
1824 process_command (argc
, argv
)
1830 char *spec_lang
= 0;
1831 int last_language_n_infiles
;
1833 gcc_exec_prefix
= getenv ("GCC_EXEC_PREFIX");
1838 /* Default for -V is our version number, ending at first space. */
1839 spec_version
= save_string (version_string
, strlen (version_string
));
1840 for (temp
= spec_version
; *temp
&& *temp
!= ' '; temp
++);
1841 if (*temp
) *temp
= '\0';
1843 /* Set up the default search paths. */
1845 if (gcc_exec_prefix
)
1847 add_prefix (&exec_prefix
, gcc_exec_prefix
, 0, 0, NULL_PTR
);
1848 add_prefix (&startfile_prefix
, gcc_exec_prefix
, 0, 0, NULL_PTR
);
1851 /* COMPILER_PATH and LIBRARY_PATH have values
1852 that are lists of directory names with colons. */
1854 temp
= getenv ("COMPILER_PATH");
1857 char *startp
, *endp
;
1858 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1860 startp
= endp
= temp
;
1863 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1865 strncpy (nstore
, startp
, endp
-startp
);
1868 strcpy (nstore
, "./");
1870 else if (endp
[-1] != '/')
1872 nstore
[endp
-startp
] = '/';
1873 nstore
[endp
-startp
+1] = 0;
1876 nstore
[endp
-startp
] = 0;
1877 add_prefix (&exec_prefix
, nstore
, 0, 0, NULL_PTR
);
1880 endp
= startp
= endp
+ 1;
1887 temp
= getenv ("LIBRARY_PATH");
1890 char *startp
, *endp
;
1891 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1893 startp
= endp
= temp
;
1896 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1898 strncpy (nstore
, startp
, endp
-startp
);
1901 strcpy (nstore
, "./");
1903 else if (endp
[-1] != '/')
1905 nstore
[endp
-startp
] = '/';
1906 nstore
[endp
-startp
+1] = 0;
1909 nstore
[endp
-startp
] = 0;
1910 add_prefix (&startfile_prefix
, nstore
, 0, 0, NULL_PTR
);
1911 /* Make separate list of dirs that came from LIBRARY_PATH. */
1912 add_prefix (&library_prefix
, nstore
, 0, 0, NULL_PTR
);
1915 endp
= startp
= endp
+ 1;
1922 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1923 temp
= getenv ("LPATH");
1926 char *startp
, *endp
;
1927 char *nstore
= (char *) alloca (strlen (temp
) + 3);
1929 startp
= endp
= temp
;
1932 if (*endp
== PATH_SEPARATOR
|| *endp
== 0)
1934 strncpy (nstore
, startp
, endp
-startp
);
1937 strcpy (nstore
, "./");
1939 else if (endp
[-1] != '/')
1941 nstore
[endp
-startp
] = '/';
1942 nstore
[endp
-startp
+1] = 0;
1945 nstore
[endp
-startp
] = 0;
1946 add_prefix (&startfile_prefix
, nstore
, 0, 0, NULL_PTR
);
1947 /* Make separate list of dirs that came from LIBRARY_PATH. */
1948 add_prefix (&library_prefix
, nstore
, 0, 0, NULL_PTR
);
1951 endp
= startp
= endp
+ 1;
1958 /* Scan argv twice. Here, the first time, just count how many switches
1959 there will be in their vector, and how many input files in theirs.
1960 Here we also parse the switches that cc itself uses (e.g. -v). */
1962 for (i
= 1; i
< argc
; i
++)
1964 if (! strcmp (argv
[i
], "-dumpspecs"))
1966 printf ("*asm:\n%s\n\n", asm_spec
);
1967 printf ("*asm_final:\n%s\n\n", asm_final_spec
);
1968 printf ("*cpp:\n%s\n\n", cpp_spec
);
1969 printf ("*cc1:\n%s\n\n", cc1_spec
);
1970 printf ("*cc1plus:\n%s\n\n", cc1plus_spec
);
1971 printf ("*endfile:\n%s\n\n", endfile_spec
);
1972 printf ("*link:\n%s\n\n", link_spec
);
1973 printf ("*lib:\n%s\n\n", lib_spec
);
1974 printf ("*startfile:\n%s\n\n", startfile_spec
);
1975 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces
);
1976 printf ("*signed_char:\n%s\n\n", signed_char_spec
);
1977 printf ("*predefines:\n%s\n\n", cpp_predefines
);
1978 printf ("*cross_compile:\n%d\n\n", cross_compile
);
1982 else if (! strcmp (argv
[i
], "-dumpversion"))
1984 printf ("%s\n", version_string
);
1987 else if (! strcmp (argv
[i
], "-print-libgcc-file-name"))
1989 print_libgcc_file_name
= 1;
1991 else if (! strcmp (argv
[i
], "-Xlinker"))
1993 /* Pass the argument of this option to the linker when we link. */
1996 fatal ("argument to `-Xlinker' is missing");
1999 if (!linker_options
)
2001 = (char **) xmalloc (n_linker_options
* sizeof (char **));
2004 = (char **) xrealloc (linker_options
,
2005 n_linker_options
* sizeof (char **));
2007 linker_options
[n_linker_options
- 1] = argv
[++i
];
2009 else if (! strncmp (argv
[i
], "-Wl,", 4))
2012 /* Pass the rest of this option to the linker when we link. */
2015 if (!linker_options
)
2017 = (char **) xmalloc (n_linker_options
* sizeof (char **));
2020 = (char **) xrealloc (linker_options
,
2021 n_linker_options
* sizeof (char **));
2023 /* Split the argument at commas. */
2025 for (j
= 4; argv
[i
][j
]; j
++)
2026 if (argv
[i
][j
] == ',')
2028 linker_options
[n_linker_options
- 1]
2029 = save_string (argv
[i
] + prev
, j
- prev
);
2032 = (char **) xrealloc (linker_options
,
2033 n_linker_options
* sizeof (char **));
2036 /* Record the part after the last comma. */
2037 linker_options
[n_linker_options
- 1] = argv
[i
] + prev
;
2039 else if (! strncmp (argv
[i
], "-Wa,", 4))
2042 /* Pass the rest of this option to the assembler. */
2044 n_assembler_options
++;
2045 if (!assembler_options
)
2047 = (char **) xmalloc (n_assembler_options
* sizeof (char **));
2050 = (char **) xrealloc (assembler_options
,
2051 n_assembler_options
* sizeof (char **));
2053 /* Split the argument at commas. */
2055 for (j
= 4; argv
[i
][j
]; j
++)
2056 if (argv
[i
][j
] == ',')
2058 assembler_options
[n_assembler_options
- 1]
2059 = save_string (argv
[i
] + prev
, j
- prev
);
2060 n_assembler_options
++;
2062 = (char **) xrealloc (assembler_options
,
2063 n_assembler_options
* sizeof (char **));
2066 /* Record the part after the last comma. */
2067 assembler_options
[n_assembler_options
- 1] = argv
[i
] + prev
;
2069 else if (argv
[i
][0] == '+' && argv
[i
][1] == 'e')
2070 /* Compensate for the +e options to the C++ front-end. */
2072 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
2074 register char *p
= &argv
[i
][1];
2075 register int c
= *p
;
2080 if (p
[1] == 0 && i
+ 1 == argc
)
2081 fatal ("argument to `-b' is missing");
2083 spec_machine
= argv
[++i
];
2085 spec_machine
= p
+ 1;
2090 int *temp
= (int *) xmalloc (sizeof (int));
2092 if (p
[1] == 0 && i
+ 1 == argc
)
2093 fatal ("argument to `-B' is missing");
2098 add_prefix (&exec_prefix
, value
, 1, 0, temp
);
2099 add_prefix (&startfile_prefix
, value
, 1, 0, temp
);
2103 case 'v': /* Print our subcommands and print versions. */
2105 /* If they do anything other than exactly `-v', don't set
2106 verbose_flag; rather, continue on to give the error. */
2113 if (p
[1] == 0 && i
+ 1 == argc
)
2114 fatal ("argument to `-V' is missing");
2116 spec_version
= argv
[++i
];
2118 spec_version
= p
+ 1;
2122 if (!strcmp (p
, "save-temps"))
2124 save_temps_flag
= 1;
2131 if (SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
2132 i
+= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
2133 else if (WORD_SWITCH_TAKES_ARG (p
))
2134 i
+= WORD_SWITCH_TAKES_ARG (p
);
2141 tooldir_prefix
= concat (tooldir_base_prefix
, spec_machine
, "/");
2143 /* If tooldir is relative, base it on exec_prefix. A relative
2144 tooldir lets us move the installed tree as a unit. */
2146 if (*tooldir_prefix
!= '/')
2148 if (gcc_exec_prefix
)
2149 tooldir_prefix
= concat (concat (gcc_exec_prefix
, spec_machine
, "/"),
2150 concat (spec_version
, "/", tooldir_prefix
),
2153 tooldir_prefix
= concat (concat (standard_exec_prefix
, spec_machine
, "/"),
2154 concat (spec_version
, "/", tooldir_prefix
),
2159 /* Set up the search paths before we go looking for config files. */
2161 /* These come before the md prefixes so that we will find gcc's subcommands
2162 (such as cpp) rather than those of the host system. */
2163 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2164 as well as trying the machine and the version. */
2165 add_prefix (&exec_prefix
, standard_exec_prefix
, 0, 2, NULL_PTR
);
2166 add_prefix (&exec_prefix
, standard_exec_prefix_1
, 0, 2, NULL_PTR
);
2168 add_prefix (&startfile_prefix
, standard_exec_prefix
, 0, 1, NULL_PTR
);
2169 add_prefix (&startfile_prefix
, standard_exec_prefix_1
, 0, 1, NULL_PTR
);
2171 add_prefix (&exec_prefix
, concat (tooldir_prefix
, "bin", "/"),
2173 add_prefix (&startfile_prefix
, concat (tooldir_prefix
, "lib", "/"),
2176 /* More prefixes are enabled in main, after we read the specs file
2177 and determine whether this is cross-compilation or not. */
2180 /* Then create the space for the vectors and scan again. */
2182 switches
= ((struct switchstr
*)
2183 xmalloc ((n_switches
+ 1) * sizeof (struct switchstr
)));
2184 infiles
= (struct infile
*) xmalloc ((n_infiles
+ 1) * sizeof (struct infile
));
2187 last_language_n_infiles
= -1;
2189 /* This, time, copy the text of each switch and store a pointer
2190 to the copy in the vector of switches.
2191 Store all the infiles in their vector. */
2193 for (i
= 1; i
< argc
; i
++)
2195 /* Just skip the switches that were handled by the preceding loop. */
2196 if (!strcmp (argv
[i
], "-Xlinker"))
2198 else if (! strncmp (argv
[i
], "-Wl,", 4))
2200 else if (! strncmp (argv
[i
], "-Wa,", 4))
2202 else if (! strcmp (argv
[i
], "-print-libgcc-file-name"))
2204 else if (argv
[i
][0] == '+' && argv
[i
][1] == 'e')
2206 /* Compensate for the +e options to the C++ front-end;
2207 they're there simply for cfront call-compatability. We do
2208 some magic in default_compilers to pass them down properly.
2209 Note we deliberately start at the `+' here, to avoid passing
2210 -e0 or -e1 down into the linker. */
2211 switches
[n_switches
].part1
= &argv
[i
][0];
2212 switches
[n_switches
].args
= 0;
2213 switches
[n_switches
].valid
= 0;
2216 else if (argv
[i
][0] == '-' && argv
[i
][1] != 0 && argv
[i
][1] != 'l')
2218 register char *p
= &argv
[i
][1];
2219 register int c
= *p
;
2221 if (c
== 'B' || c
== 'b' || c
== 'V')
2223 /* Skip a separate arg, if any. */
2230 if (p
[1] == 0 && i
+ 1 == argc
)
2231 fatal ("argument to `-x' is missing");
2233 spec_lang
= argv
[++i
];
2236 if (! strcmp (spec_lang
, "none"))
2237 /* Suppress the warning if -xnone comes after the last input file,
2238 because alternate command interfaces like g++ might find it
2239 useful to place -xnone after each input file. */
2242 last_language_n_infiles
= n_infiles
;
2245 switches
[n_switches
].part1
= p
;
2246 /* Deal with option arguments in separate argv elements. */
2247 if ((SWITCH_TAKES_ARG (c
) > (p
[1] != 0))
2248 || WORD_SWITCH_TAKES_ARG (p
)) {
2250 int n_args
= WORD_SWITCH_TAKES_ARG (p
);
2253 /* Count only the option arguments in separate argv elements. */
2254 n_args
= SWITCH_TAKES_ARG (c
) - (p
[1] != 0);
2256 if (i
+ n_args
>= argc
)
2257 fatal ("argument to `-%s' is missing", p
);
2258 switches
[n_switches
].args
2259 = (char **) xmalloc ((n_args
+ 1) * sizeof (char *));
2261 switches
[n_switches
].args
[j
++] = argv
[++i
];
2262 /* Null-terminate the vector. */
2263 switches
[n_switches
].args
[j
] = 0;
2264 } else if (*switches_need_spaces
!= 0 && (c
== 'o' || c
== 'L')) {
2265 /* On some systems, ld cannot handle -o or -L without space.
2266 So split the -o or -L from its argument. */
2267 switches
[n_switches
].part1
= (c
== 'o' ? "o" : "L");
2268 switches
[n_switches
].args
= (char **) xmalloc (2 * sizeof (char *));
2269 switches
[n_switches
].args
[0] = xmalloc (strlen (p
));
2270 strcpy (switches
[n_switches
].args
[0], &p
[1]);
2271 switches
[n_switches
].args
[1] = 0;
2273 switches
[n_switches
].args
= 0;
2274 switches
[n_switches
].valid
= 0;
2275 /* This is always valid, since gcc.c itself understands it. */
2276 if (!strcmp (p
, "save-temps"))
2277 switches
[n_switches
].valid
= 1;
2282 infiles
[n_infiles
].language
= spec_lang
;
2283 infiles
[n_infiles
++].name
= argv
[i
];
2287 if (n_infiles
== last_language_n_infiles
)
2288 error ("Warning: `-x %s' after last input file has no effect", spec_lang
);
2290 switches
[n_switches
].part1
= 0;
2291 infiles
[n_infiles
].name
= 0;
2293 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
2294 if (gcc_exec_prefix
)
2296 temp
= (char *) xmalloc (strlen (gcc_exec_prefix
) + strlen (spec_version
)
2297 + strlen (spec_machine
) + 3);
2298 strcpy (temp
, gcc_exec_prefix
);
2299 strcat (temp
, spec_machine
);
2301 strcat (temp
, spec_version
);
2303 gcc_exec_prefix
= temp
;
2307 /* Process a spec string, accumulating and running commands. */
2309 /* These variables describe the input file name.
2310 input_file_number is the index on outfiles of this file,
2311 so that the output file name can be stored for later use by %o.
2312 input_basename is the start of the part of the input file
2313 sans all directory names, and basename_length is the number
2314 of characters starting there excluding the suffix .c or whatever. */
2316 static char *input_filename
;
2317 static int input_file_number
;
2318 static int input_filename_length
;
2319 static int basename_length
;
2320 static char *input_basename
;
2321 static char *input_suffix
;
2323 /* These are variables used within do_spec and do_spec_1. */
2325 /* Nonzero if an arg has been started and not yet terminated
2326 (with space, tab or newline). */
2327 static int arg_going
;
2329 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2330 is a temporary file name. */
2331 static int delete_this_arg
;
2333 /* Nonzero means %w has been seen; the next arg to be terminated
2334 is the output file name of this compilation. */
2335 static int this_is_output_file
;
2337 /* Nonzero means %s has been seen; the next arg to be terminated
2338 is the name of a library file and we should try the standard
2339 search dirs for it. */
2340 static int this_is_library_file
;
2342 /* Nonzero means that the input of this command is coming from a pipe. */
2343 static int input_from_pipe
;
2345 /* Process the spec SPEC and run the commands specified therein.
2346 Returns 0 if the spec is successfully processed; -1 if failed. */
2356 delete_this_arg
= 0;
2357 this_is_output_file
= 0;
2358 this_is_library_file
= 0;
2359 input_from_pipe
= 0;
2361 value
= do_spec_1 (spec
, 0, NULL_PTR
);
2363 /* Force out any unfinished command.
2364 If -pipe, this forces out the last command if it ended in `|'. */
2367 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
2370 if (argbuf_index
> 0)
2377 /* Process the sub-spec SPEC as a portion of a larger spec.
2378 This is like processing a whole spec except that we do
2379 not initialize at the beginning and we do not supply a
2380 newline by default at the end.
2381 INSWITCH nonzero means don't process %-sequences in SPEC;
2382 in this case, % is treated as an ordinary character.
2383 This is used while substituting switches.
2384 INSWITCH nonzero also causes SPC not to terminate an argument.
2386 Value is zero unless a line was finished
2387 and the command on that line reported an error. */
2390 do_spec_1 (spec
, inswitch
, soft_matched_part
)
2393 char *soft_matched_part
;
2395 register char *p
= spec
;
2402 /* If substituting a switch, treat all chars like letters.
2403 Otherwise, NL, SPC, TAB and % are special. */
2404 switch (inswitch
? 'a' : c
)
2407 /* End of line: finish any pending argument,
2408 then run the pending command if one has been started. */
2411 obstack_1grow (&obstack
, 0);
2412 string
= obstack_finish (&obstack
);
2413 if (this_is_library_file
)
2414 string
= find_file (string
);
2415 store_arg (string
, delete_this_arg
, this_is_output_file
);
2416 if (this_is_output_file
)
2417 outfiles
[input_file_number
] = string
;
2421 if (argbuf_index
> 0 && !strcmp (argbuf
[argbuf_index
- 1], "|"))
2424 for (i
= 0; i
< n_switches
; i
++)
2425 if (!strcmp (switches
[i
].part1
, "pipe"))
2428 /* A `|' before the newline means use a pipe here,
2429 but only if -pipe was specified.
2430 Otherwise, execute now and don't pass the `|' as an arg. */
2433 input_from_pipe
= 1;
2434 switches
[i
].valid
= 1;
2441 if (argbuf_index
> 0)
2447 /* Reinitialize for a new command, and for a new argument. */
2450 delete_this_arg
= 0;
2451 this_is_output_file
= 0;
2452 this_is_library_file
= 0;
2453 input_from_pipe
= 0;
2457 /* End any pending argument. */
2460 obstack_1grow (&obstack
, 0);
2461 string
= obstack_finish (&obstack
);
2462 if (this_is_library_file
)
2463 string
= find_file (string
);
2464 store_arg (string
, delete_this_arg
, this_is_output_file
);
2465 if (this_is_output_file
)
2466 outfiles
[input_file_number
] = string
;
2470 obstack_1grow (&obstack
, c
);
2476 /* Space or tab ends an argument if one is pending. */
2479 obstack_1grow (&obstack
, 0);
2480 string
= obstack_finish (&obstack
);
2481 if (this_is_library_file
)
2482 string
= find_file (string
);
2483 store_arg (string
, delete_this_arg
, this_is_output_file
);
2484 if (this_is_output_file
)
2485 outfiles
[input_file_number
] = string
;
2487 /* Reinitialize for a new argument. */
2489 delete_this_arg
= 0;
2490 this_is_output_file
= 0;
2491 this_is_library_file
= 0;
2498 fatal ("Invalid specification! Bug in cc.");
2501 obstack_grow (&obstack
, input_basename
, basename_length
);
2506 delete_this_arg
= 2;
2509 /* Dump out the directories specified with LIBRARY_PATH,
2510 followed by the absolute directories
2511 that we search for startfiles. */
2513 for (i
= 0; i
< 2; i
++)
2515 struct prefix_list
*pl
2516 = (i
== 0 ? library_prefix
.plist
: startfile_prefix
.plist
);
2518 char *buffer
= (char *) xmalloc (bufsize
);
2521 for (; pl
; pl
= pl
->next
)
2523 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2524 /* Used on systems which record the specified -L dirs
2525 and use them to search for dynamic linking. */
2526 /* Relative directories always come from -B,
2527 and it is better not to use them for searching
2528 at run time. In particular, stage1 loses */
2529 if (pl
->prefix
[0] != '/')
2534 if (is_linker_dir (pl
->prefix
, machine_suffix
))
2536 do_spec_1 ("-L", 0, NULL_PTR
);
2537 #ifdef SPACE_AFTER_L_OPTION
2538 do_spec_1 (" ", 0, NULL_PTR
);
2540 do_spec_1 (pl
->prefix
, 1, NULL_PTR
);
2541 /* Remove slash from machine_suffix. */
2542 if (strlen (machine_suffix
) >= bufsize
)
2543 bufsize
= strlen (machine_suffix
) * 2 + 1;
2544 buffer
= (char *) xrealloc (buffer
, bufsize
);
2545 strcpy (buffer
, machine_suffix
);
2546 idx
= strlen (buffer
);
2547 if (buffer
[idx
- 1] == '/')
2548 buffer
[idx
- 1] = 0;
2549 do_spec_1 (buffer
, 1, NULL_PTR
);
2550 /* Make this a separate argument. */
2551 do_spec_1 (" ", 0, NULL_PTR
);
2554 if (!pl
->require_machine_suffix
)
2556 if (is_linker_dir (pl
->prefix
, ""))
2558 do_spec_1 ("-L", 0, NULL_PTR
);
2559 #ifdef SPACE_AFTER_L_OPTION
2560 do_spec_1 (" ", 0, NULL_PTR
);
2562 /* Remove slash from pl->prefix. */
2563 if (strlen (pl
->prefix
) >= bufsize
)
2564 bufsize
= strlen (pl
->prefix
) * 2 + 1;
2565 buffer
= (char *) xrealloc (buffer
, bufsize
);
2566 strcpy (buffer
, pl
->prefix
);
2567 idx
= strlen (buffer
);
2568 if (buffer
[idx
- 1] == '/')
2569 buffer
[idx
- 1] = 0;
2570 do_spec_1 (buffer
, 1, NULL_PTR
);
2571 /* Make this a separate argument. */
2572 do_spec_1 (" ", 0, NULL_PTR
);
2581 /* {...:%efoo} means report an error with `foo' as error message
2582 and don't execute any more commands for this file. */
2586 while (*p
!= 0 && *p
!= '\n') p
++;
2587 buf
= (char *) alloca (p
- q
+ 1);
2588 strncpy (buf
, q
, p
- q
);
2598 if (save_temps_flag
)
2599 obstack_grow (&obstack
, input_basename
, basename_length
);
2602 #ifdef MKTEMP_EACH_FILE
2603 /* ??? This has a problem: the total number of
2604 values mktemp can return is limited.
2605 That matters for the names of object files.
2606 In 2.4, do something about that. */
2607 struct temp_name
*t
;
2609 while (*p
== '.' || isalpha (*p
))
2612 /* See if we already have an association of %g/%u/%U and
2614 for (t
= temp_names
; t
; t
= t
->next
)
2615 if (t
->length
== p
- suffix
2616 && strncmp (t
->suffix
, suffix
, p
- suffix
) == 0
2617 && t
->unique
== (c
!= 'g'))
2620 /* Make a new association if needed. %u requires one. */
2621 if (t
== 0 || c
== 'u')
2625 t
= (struct temp_name
*) xmalloc (sizeof (struct temp_name
));
2626 t
->next
= temp_names
;
2629 t
->length
= p
- suffix
;
2630 t
->suffix
= save_string (suffix
, p
- suffix
);
2631 t
->unique
= (c
!= 'g');
2632 choose_temp_base ();
2633 t
->filename
= temp_filename
;
2634 t
->filename_length
= temp_filename_length
;
2637 obstack_grow (&obstack
, t
->filename
, t
->filename_length
);
2638 delete_this_arg
= 1;
2640 obstack_grow (&obstack
, temp_filename
, temp_filename_length
);
2641 if (c
== 'u' || c
== 'U')
2647 sprintf (buff
, "%d", unique
);
2648 obstack_grow (&obstack
, buff
, strlen (buff
));
2651 delete_this_arg
= 1;
2657 obstack_grow (&obstack
, input_filename
, input_filename_length
);
2662 if (gcc_exec_prefix
)
2664 do_spec_1 ("-iprefix", 1, NULL_PTR
);
2665 /* Make this a separate argument. */
2666 do_spec_1 (" ", 0, NULL_PTR
);
2667 do_spec_1 (gcc_exec_prefix
, 1, NULL_PTR
);
2668 do_spec_1 (" ", 0, NULL_PTR
);
2675 for (f
= 0; f
< n_infiles
; f
++)
2676 store_arg (outfiles
[f
], 0, 0);
2681 this_is_library_file
= 1;
2685 this_is_output_file
= 1;
2690 int index
= argbuf_index
;
2691 /* Handle the {...} following the %W. */
2694 p
= handle_braces (p
+ 1);
2697 /* If any args were output, mark the last one for deletion
2699 if (argbuf_index
!= index
)
2700 record_temp_file (argbuf
[argbuf_index
- 1], 0, 1);
2704 /* %x{OPTION} records OPTION for %X to output. */
2710 /* Skip past the option value and make a copy. */
2715 string
= save_string (p1
+ 1, p
- p1
- 2);
2717 /* See if we already recorded this option. */
2718 for (i
= 0; i
< n_linker_options
; i
++)
2719 if (! strcmp (string
, linker_options
[i
]))
2725 /* This option is new; add it. */
2727 if (!linker_options
)
2729 = (char **) xmalloc (n_linker_options
* sizeof (char **));
2732 = (char **) xrealloc (linker_options
,
2733 n_linker_options
* sizeof (char **));
2735 linker_options
[n_linker_options
- 1] = string
;
2739 /* Dump out the options accumulated previously using %x,
2740 -Xlinker and -Wl,. */
2742 for (i
= 0; i
< n_linker_options
; i
++)
2744 do_spec_1 (linker_options
[i
], 1, NULL_PTR
);
2745 /* Make each accumulated option a separate argument. */
2746 do_spec_1 (" ", 0, NULL_PTR
);
2750 /* Dump out the options accumulated previously using -Wa,. */
2752 for (i
= 0; i
< n_assembler_options
; i
++)
2754 do_spec_1 (assembler_options
[i
], 1, NULL_PTR
);
2755 /* Make each accumulated option a separate argument. */
2756 do_spec_1 (" ", 0, NULL_PTR
);
2760 /* Here are digits and numbers that just process
2761 a certain constant string as a spec.
2762 /* Here are digits and numbers that just process
2763 a certain constant string as a spec. */
2766 value
= do_spec_1 (cc1_spec
, 0, NULL_PTR
);
2772 value
= do_spec_1 (cc1plus_spec
, 0, NULL_PTR
);
2778 value
= do_spec_1 (asm_spec
, 0, NULL_PTR
);
2784 value
= do_spec_1 (asm_final_spec
, 0, NULL_PTR
);
2790 value
= do_spec_1 (signed_char_spec
, 0, NULL_PTR
);
2796 value
= do_spec_1 (cpp_spec
, 0, NULL_PTR
);
2802 value
= do_spec_1 (endfile_spec
, 0, NULL_PTR
);
2808 value
= do_spec_1 (link_spec
, 0, NULL_PTR
);
2814 value
= do_spec_1 (lib_spec
, 0, NULL_PTR
);
2821 char *x
= (char *) alloca (strlen (cpp_predefines
) + 1);
2825 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2829 if (! strncmp (y
, "-D", 2))
2830 /* Copy the whole option. */
2831 while (*y
&& *y
!= ' ' && *y
!= '\t')
2833 else if (*y
== ' ' || *y
== '\t')
2834 /* Copy whitespace to the result. */
2836 /* Don't copy other options. */
2843 value
= do_spec_1 (buf
, 0, NULL_PTR
);
2851 char *x
= (char *) alloca (strlen (cpp_predefines
) * 4 + 1);
2855 /* Copy all of CPP_PREDEFINES into BUF,
2856 but put __ after every -D and at the end of each arg. */
2860 if (! strncmp (y
, "-D", 2))
2867 if (strncmp (y
, "__", 2))
2869 /* Stick __ at front of macro name. */
2872 /* Arrange to stick __ at the end as well. */
2876 /* Copy the macro name. */
2877 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2886 /* Copy the value given, if any. */
2887 while (*y
&& *y
!= ' ' && *y
!= '\t')
2890 else if (*y
== ' ' || *y
== '\t')
2891 /* Copy whitespace to the result. */
2893 /* Don't copy -A options */
2899 /* Copy all of CPP_PREDEFINES into BUF,
2900 but put __ after every -D. */
2904 if (! strncmp (y
, "-D", 2))
2909 if (strncmp (y
, "__", 2))
2911 /* Stick __ at front of macro name. */
2916 /* Copy the macro name. */
2917 while (*y
&& *y
!= '=' && *y
!= ' ' && *y
!= '\t')
2920 /* Copy the value given, if any. */
2921 while (*y
&& *y
!= ' ' && *y
!= '\t')
2924 else if (*y
== ' ' || *y
== '\t')
2925 /* Copy whitespace to the result. */
2927 /* Don't copy -A options */
2933 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2937 if (! strncmp (y
, "-A", 2))
2938 /* Copy the whole option. */
2939 while (*y
&& *y
!= ' ' && *y
!= '\t')
2941 else if (*y
== ' ' || *y
== '\t')
2942 /* Copy whitespace to the result. */
2944 /* Don't copy other options. */
2951 value
= do_spec_1 (buf
, 0, NULL_PTR
);
2958 value
= do_spec_1 (startfile_spec
, 0, NULL_PTR
);
2963 /* Here we define characters other than letters and digits. */
2966 p
= handle_braces (p
);
2972 obstack_1grow (&obstack
, '%');
2976 do_spec_1 (soft_matched_part
, 1, NULL_PTR
);
2977 do_spec_1 (" ", 0, NULL_PTR
);
2980 /* Process a string found as the value of a spec given by name.
2981 This feature allows individual machine descriptions
2982 to add and use their own specs.
2983 %[...] modifies -D options the way %P does;
2984 %(...) uses the spec unmodified. */
2989 struct spec_list
*sl
;
2992 /* The string after the S/P is the name of a spec that is to be
2994 while (*p
&& *p
!= ')' && *p
!= ']')
2997 /* See if it's in the list */
2998 for (len
= p
- name
, sl
= specs
; sl
; sl
= sl
->next
)
2999 if (strncmp (sl
->name
, name
, len
) == 0 && !sl
->name
[len
])
3009 value
= do_spec_1 (name
, 0, NULL_PTR
);
3015 char *x
= (char *) alloca (strlen (name
) * 2 + 1);
3019 /* Copy all of NAME into BUF, but put __ after
3020 every -D and at the end of each arg, */
3023 if (! strncmp (y
, "-D", 2))
3031 else if (*y
== ' ' || *y
== 0)
3045 value
= do_spec_1 (buf
, 0, NULL_PTR
);
3051 /* Discard the closing paren or bracket. */
3058 if (input_from_pipe
)
3059 do_spec_1 ("-", 0, NULL_PTR
);
3068 /* Backslash: treat next character as ordinary. */
3073 /* Ordinary character: put it into the current argument. */
3074 obstack_1grow (&obstack
, c
);
3078 return 0; /* End of string */
3081 /* Return 0 if we call do_spec_1 and that returns -1. */
3094 /* A `|' after the open-brace means,
3095 if the test fails, output a single minus sign rather than nothing.
3096 This is used in %{|!pipe:...}. */
3100 /* A `!' after the open-brace negates the condition:
3101 succeed if the specified switch is not present. */
3105 /* A `.' after the open-brace means test against the current suffix. */
3115 while (*p
!= ':' && *p
!= '}') p
++;
3118 register int count
= 1;
3136 int found
= (input_suffix
!= 0
3137 && strlen (input_suffix
) == p
- filter
3138 && strncmp (input_suffix
, filter
, p
- filter
) == 0);
3144 && do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL_PTR
) < 0)
3149 else if (p
[-1] == '*' && p
[0] == '}')
3151 /* Substitute all matching switches as separate args. */
3154 for (i
= 0; i
< n_switches
; i
++)
3155 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
3160 /* Test for presence of the specified switch. */
3164 /* If name specified ends in *, as in {x*:...},
3165 check for %* and handle that case. */
3166 if (p
[-1] == '*' && !negate
)
3171 /* First see whether we have %*. */
3175 if (*r
== '%' && r
[1] == '*')
3179 /* If we do, handle that case. */
3182 /* Substitute all matching switches as separate args.
3183 But do this by substituting for %*
3184 in the text that follows the colon. */
3186 unsigned hard_match_len
= p
- filter
- 1;
3187 char *string
= save_string (p
+ 1, q
- p
- 2);
3189 for (i
= 0; i
< n_switches
; i
++)
3190 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
3192 do_spec_1 (string
, 0, &switches
[i
].part1
[hard_match_len
]);
3193 /* Pass any arguments this switch has. */
3201 /* If name specified ends in *, as in {x*:...},
3202 check for presence of any switch name starting with x. */
3205 for (i
= 0; i
< n_switches
; i
++)
3207 unsigned hard_match_len
= p
- filter
- 1;
3209 if (!strncmp (switches
[i
].part1
, filter
, hard_match_len
))
3211 switches
[i
].valid
= 1;
3216 /* Otherwise, check for presence of exact name specified. */
3219 for (i
= 0; i
< n_switches
; i
++)
3221 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
3222 && switches
[i
].part1
[p
- filter
] == 0)
3224 switches
[i
].valid
= 1;
3231 /* If it is as desired (present for %{s...}, absent for %{-s...})
3232 then substitute either the switch or the specified
3233 conditional text. */
3234 if (present
!= negate
)
3242 if (do_spec_1 (save_string (p
+ 1, q
- p
- 2), 0, NULL_PTR
) < 0)
3248 /* Here if a %{|...} conditional fails: output a minus sign,
3249 which means "standard output" or "standard input". */
3250 do_spec_1 ("-", 0, NULL_PTR
);
3257 /* Pass a switch to the current accumulating command
3258 in the same form that we received it.
3259 SWITCHNUM identifies the switch; it is an index into
3260 the vector of switches gcc received, which is `switches'.
3261 This cannot fail since it never finishes a command line.
3263 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
3266 give_switch (switchnum
, omit_first_word
)
3268 int omit_first_word
;
3270 if (!omit_first_word
)
3272 do_spec_1 ("-", 0, NULL_PTR
);
3273 do_spec_1 (switches
[switchnum
].part1
, 1, NULL_PTR
);
3275 do_spec_1 (" ", 0, NULL_PTR
);
3276 if (switches
[switchnum
].args
!= 0)
3279 for (p
= switches
[switchnum
].args
; *p
; p
++)
3281 do_spec_1 (*p
, 1, NULL_PTR
);
3282 do_spec_1 (" ", 0, NULL_PTR
);
3285 switches
[switchnum
].valid
= 1;
3288 /* Search for a file named NAME trying various prefixes including the
3289 user's -B prefix and some standard ones.
3290 Return the absolute file name found. If nothing is found, return NAME. */
3298 newname
= find_a_file (&startfile_prefix
, name
, R_OK
);
3299 return newname
? newname
: name
;
3302 /* Determine whether a -L option is relevant. Not required for certain
3303 fixed names and for directories that don't exist. */
3306 is_linker_dir (path1
, path2
)
3310 int len1
= strlen (path1
);
3311 int len2
= strlen (path2
);
3312 char *path
= (char *) alloca (3 + len1
+ len2
);
3316 /* Construct the path from the two parts. Ensure the string ends with "/.".
3317 The resulting path will be a directory even if the given path is a
3319 bcopy (path1
, path
, len1
);
3320 bcopy (path2
, path
+ len1
, len2
);
3321 cp
= path
+ len1
+ len2
;
3327 /* Exclude directories that the linker is known to search. */
3328 if ((cp
- path
== 6 && strcmp (path
, "/lib/.") == 0)
3329 || (cp
- path
== 10 && strcmp (path
, "/usr/lib/.") == 0))
3332 return (stat (path
, &st
) >= 0 && S_ISDIR (st
.st_mode
));
3335 /* On fatal signals, delete all the temporary files. */
3338 fatal_error (signum
)
3341 signal (signum
, SIG_DFL
);
3342 delete_failure_queue ();
3343 delete_temp_files ();
3344 /* Get the same signal again, this time not handled,
3345 so its normal effect occurs. */
3346 kill (getpid (), signum
);
3357 int error_count
= 0;
3358 int linker_was_run
= 0;
3359 char *explicit_link_files
;
3362 programname
= argv
[0];
3364 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
3365 signal (SIGINT
, fatal_error
);
3366 if (signal (SIGHUP
, SIG_IGN
) != SIG_IGN
)
3367 signal (SIGHUP
, fatal_error
);
3368 if (signal (SIGTERM
, SIG_IGN
) != SIG_IGN
)
3369 signal (SIGTERM
, fatal_error
);
3371 if (signal (SIGPIPE
, SIG_IGN
) != SIG_IGN
)
3372 signal (SIGPIPE
, fatal_error
);
3376 argbuf
= (char **) xmalloc (argbuf_length
* sizeof (char *));
3378 obstack_init (&obstack
);
3380 /* Set up to remember the pathname of gcc and any options
3381 needed for collect. */
3382 obstack_init (&collect_obstack
);
3383 obstack_grow (&collect_obstack
, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3384 obstack_grow (&collect_obstack
, programname
, strlen (programname
)+1);
3385 putenv (obstack_finish (&collect_obstack
));
3387 /* Choose directory for temp files. */
3389 choose_temp_base ();
3391 /* Make a table of what switches there are (switches, n_switches).
3392 Make a table of specified input files (infiles, n_infiles).
3393 Decode switches that are handled locally. */
3395 process_command (argc
, argv
);
3397 /* Initialize the vector of specs to just the default.
3398 This means one element containing 0s, as a terminator. */
3400 compilers
= (struct compiler
*) xmalloc (sizeof default_compilers
);
3401 bcopy (default_compilers
, compilers
, sizeof default_compilers
);
3402 n_compilers
= n_default_compilers
;
3404 /* Read specs from a file if there is one. */
3406 machine_suffix
= concat (spec_machine
, "/", concat (spec_version
, "/", ""));
3407 just_machine_suffix
= concat (spec_machine
, "/", "");
3409 specs_file
= find_a_file (&startfile_prefix
, "specs", R_OK
);
3410 /* Read the specs file unless it is a default one. */
3411 if (specs_file
!= 0 && strcmp (specs_file
, "specs"))
3412 read_specs (specs_file
);
3414 /* If not cross-compiling, look for startfiles in the standard places. */
3415 /* The fact that these are done here, after reading the specs file,
3416 means that it cannot be found in these directories.
3417 But that's okay. It should never be there anyway. */
3420 #ifdef MD_EXEC_PREFIX
3421 add_prefix (&exec_prefix
, md_exec_prefix
, 0, 0, NULL_PTR
);
3422 add_prefix (&startfile_prefix
, md_exec_prefix
, 0, 0, NULL_PTR
);
3425 #ifdef MD_STARTFILE_PREFIX
3426 add_prefix (&startfile_prefix
, md_startfile_prefix
, 0, 0, NULL_PTR
);
3429 #ifdef MD_STARTFILE_PREFIX_1
3430 add_prefix (&startfile_prefix
, md_startfile_prefix_1
, 0, 0, NULL_PTR
);
3433 add_prefix (&startfile_prefix
, standard_startfile_prefix
, 0, 0,
3435 add_prefix (&startfile_prefix
, standard_startfile_prefix_1
, 0, 0,
3437 add_prefix (&startfile_prefix
, standard_startfile_prefix_2
, 0, 0,
3439 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3440 add_prefix (&startfile_prefix
, "./", 0, 1, NULL_PTR
);
3444 /* Now we have the specs.
3445 Set the `valid' bits for switches that match anything in any spec. */
3447 validate_all_switches ();
3449 /* Warn about any switches that no pass was interested in. */
3451 for (i
= 0; i
< n_switches
; i
++)
3452 if (! switches
[i
].valid
)
3453 error ("unrecognized option `-%s'", switches
[i
].part1
);
3455 if (print_libgcc_file_name
)
3457 printf ("%s\n", find_file ("libgcc.a"));
3461 /* Obey some of the options. */
3465 fprintf (stderr
, "gcc version %s\n", version_string
);
3471 fatal ("No input files specified.");
3473 /* Make a place to record the compiler output file names
3474 that correspond to the input files. */
3476 outfiles
= (char **) xmalloc (n_infiles
* sizeof (char *));
3477 bzero (outfiles
, n_infiles
* sizeof (char *));
3479 /* Record which files were specified explicitly as link input. */
3481 explicit_link_files
= xmalloc (n_infiles
);
3482 bzero (explicit_link_files
, n_infiles
);
3484 for (i
= 0; i
< n_infiles
; i
++)
3486 register struct compiler
*cp
= 0;
3487 int this_file_error
= 0;
3489 /* Tell do_spec what to substitute for %i. */
3491 input_filename
= infiles
[i
].name
;
3492 input_filename_length
= strlen (input_filename
);
3493 input_file_number
= i
;
3495 /* Use the same thing in %o, unless cp->spec says otherwise. */
3497 outfiles
[i
] = input_filename
;
3499 /* Figure out which compiler from the file's suffix. */
3501 cp
= lookup_compiler (infiles
[i
].name
, input_filename_length
,
3502 infiles
[i
].language
);
3506 /* Ok, we found an applicable compiler. Run its spec. */
3507 /* First say how much of input_filename to substitute for %b */
3511 input_basename
= input_filename
;
3512 for (p
= input_filename
; *p
; p
++)
3514 input_basename
= p
+ 1;
3516 /* Find a suffix starting with the last period,
3517 and set basename_length to exclude that suffix. */
3518 basename_length
= strlen (input_basename
);
3519 p
= input_basename
+ basename_length
;
3520 while (p
!= input_basename
&& *p
!= '.') --p
;
3521 if (*p
== '.' && p
!= input_basename
)
3523 basename_length
= p
- input_basename
;
3524 input_suffix
= p
+ 1;
3530 for (j
= 0; j
< sizeof cp
->spec
/ sizeof cp
->spec
[0]; j
++)
3532 len
+= strlen (cp
->spec
[j
]);
3534 p
= (char *) xmalloc (len
+ 1);
3537 for (j
= 0; j
< sizeof cp
->spec
/ sizeof cp
->spec
[0]; j
++)
3540 strcpy (p
+ len
, cp
->spec
[j
]);
3541 len
+= strlen (cp
->spec
[j
]);
3544 value
= do_spec (p
);
3547 this_file_error
= 1;
3550 /* If this file's name does not contain a recognized suffix,
3551 record it as explicit linker input. */
3554 explicit_link_files
[i
] = 1;
3556 /* Clear the delete-on-failure queue, deleting the files in it
3557 if this compilation failed. */
3559 if (this_file_error
)
3561 delete_failure_queue ();
3564 /* If this compilation succeeded, don't delete those files later. */
3565 clear_failure_queue ();
3568 /* Run ld to link all the compiler output files. */
3570 if (error_count
== 0)
3572 int tmp
= execution_count
;
3576 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3578 putenv_from_prefixes (&exec_prefix
, "COMPILER_PATH=");
3579 putenv_from_prefixes (&startfile_prefix
, "LIBRARY_PATH=");
3581 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3583 obstack_grow (&collect_obstack
, "COLLECT_GCC_OPTIONS=",
3584 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3587 for (i
= 0; i
< n_switches
; i
++)
3591 obstack_grow (&collect_obstack
, " ", 1);
3594 obstack_grow (&collect_obstack
, "-", 1);
3595 obstack_grow (&collect_obstack
, switches
[i
].part1
,
3596 strlen (switches
[i
].part1
));
3598 for (args
= switches
[i
].args
; args
&& *args
; args
++)
3600 obstack_grow (&collect_obstack
, " ", 1);
3601 obstack_grow (&collect_obstack
, *args
, strlen (*args
));
3604 obstack_grow (&collect_obstack
, "\0", 1);
3605 putenv (obstack_finish (&collect_obstack
));
3607 value
= do_spec (link_command_spec
);
3610 linker_was_run
= (tmp
!= execution_count
);
3613 /* Warn if a -B option was specified but the prefix was never used. */
3614 unused_prefix_warnings (&exec_prefix
);
3615 unused_prefix_warnings (&startfile_prefix
);
3617 /* If options said don't run linker,
3618 complain about input files to be given to the linker. */
3620 if (! linker_was_run
&& error_count
== 0)
3621 for (i
= 0; i
< n_infiles
; i
++)
3622 if (explicit_link_files
[i
])
3623 error ("%s: linker input file unused since linking not done",
3626 /* Delete some or all of the temporary files we made. */
3629 delete_failure_queue ();
3630 delete_temp_files ();
3632 exit (error_count
> 0 ? (signal_count
? 2 : 1) : 0);
3637 /* Find the proper compilation spec for the file name NAME,
3638 whose length is LENGTH. LANGUAGE is the specified language,
3639 or 0 if none specified. */
3641 static struct compiler
*
3642 lookup_compiler (name
, length
, language
)
3647 struct compiler
*cp
;
3649 /* Look for the language, if one is spec'd. */
3652 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3656 if (cp
->suffix
[0] == '@'
3657 && !strcmp (cp
->suffix
+ 1, language
))
3661 error ("language %s not recognized", language
);
3664 /* Look for a suffix. */
3665 for (cp
= compilers
+ n_compilers
- 1; cp
>= compilers
; cp
--)
3667 if (/* The suffix `-' matches only the file name `-'. */
3668 (!strcmp (cp
->suffix
, "-") && !strcmp (name
, "-"))
3670 (strlen (cp
->suffix
) < length
3671 /* See if the suffix matches the end of NAME. */
3672 && !strcmp (cp
->suffix
,
3673 name
+ length
- strlen (cp
->suffix
))))
3675 if (cp
->spec
[0][0] == '@')
3677 struct compiler
*new;
3678 /* An alias entry maps a suffix to a language.
3679 Search for the language; pass 0 for NAME and LENGTH
3680 to avoid infinite recursion if language not found.
3681 Construct the new compiler spec. */
3682 language
= cp
->spec
[0] + 1;
3683 new = (struct compiler
*) xmalloc (sizeof (struct compiler
));
3684 new->suffix
= cp
->suffix
;
3685 bcopy (lookup_compiler (NULL_PTR
, 0, language
)->spec
,
3686 new->spec
, sizeof new->spec
);
3689 /* A non-alias entry: return it. */
3701 register char *value
= (char *) malloc (size
);
3703 fatal ("virtual memory exhausted");
3708 xrealloc (ptr
, size
)
3712 register char *value
= (char *) realloc (ptr
, size
);
3714 fatal ("virtual memory exhausted");
3718 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3724 int len1
= strlen (s1
), len2
= strlen (s2
), len3
= strlen (s3
);
3725 char *result
= xmalloc (len1
+ len2
+ len3
+ 1);
3727 strcpy (result
, s1
);
3728 strcpy (result
+ len1
, s2
);
3729 strcpy (result
+ len1
+ len2
, s3
);
3730 *(result
+ len1
+ len2
+ len3
) = 0;
3736 save_string (s
, len
)
3740 register char *result
= xmalloc (len
+ 1);
3742 bcopy (s
, result
, len
);
3748 pfatal_with_name (name
)
3753 if (errno
< sys_nerr
)
3754 s
= concat ("%s: ", sys_errlist
[errno
], "");
3756 s
= "cannot open %s";
3761 perror_with_name (name
)
3766 if (errno
< sys_nerr
)
3767 s
= concat ("%s: ", sys_errlist
[errno
], "");
3769 s
= "cannot open %s";
3779 if (errno
< sys_nerr
)
3780 s
= concat ("installation problem, cannot exec %s: ",
3781 sys_errlist
[errno
], "");
3783 s
= "installation problem, cannot exec %s";
3787 /* More 'friendly' abort that prints the line and file.
3788 config.h can #define abort fancy_abort if you like that sort of thing. */
3793 fatal ("Internal gcc abort.");
3798 /* Output an error message and exit */
3808 format
= va_arg (ap
, char *);
3809 fprintf (stderr
, "%s: ", programname
);
3810 vfprintf (stderr
, format
, ap
);
3812 fprintf (stderr
, "\n");
3813 delete_temp_files ();
3825 format
= va_arg (ap
, char *);
3826 fprintf (stderr
, "%s: ", programname
);
3827 vfprintf (stderr
, format
, ap
);
3830 fprintf (stderr
, "\n");
3833 #else /* not HAVE_VPRINTF */
3836 fatal (msg
, arg1
, arg2
)
3837 char *msg
, *arg1
, *arg2
;
3839 error (msg
, arg1
, arg2
);
3840 delete_temp_files ();
3845 error (msg
, arg1
, arg2
)
3846 char *msg
, *arg1
, *arg2
;
3848 fprintf (stderr
, "%s: ", programname
);
3849 fprintf (stderr
, msg
, arg1
, arg2
);
3850 fprintf (stderr
, "\n");
3853 #endif /* not HAVE_VPRINTF */
3857 validate_all_switches ()
3859 struct compiler
*comp
;
3862 struct spec_list
*spec
;
3864 for (comp
= compilers
; comp
->spec
[0]; comp
++)
3867 for (i
= 0; i
< sizeof comp
->spec
/ sizeof comp
->spec
[0] && comp
->spec
[i
]; i
++)
3871 if (c
== '%' && *p
== '{')
3872 /* We have a switch spec. */
3873 validate_switches (p
+ 1);
3877 /* look through the linked list of extra specs read from the specs file */
3878 for (spec
= specs
; spec
; spec
= spec
->next
)
3882 if (c
== '%' && *p
== '{')
3883 /* We have a switch spec. */
3884 validate_switches (p
+ 1);
3887 p
= link_command_spec
;
3889 if (c
== '%' && *p
== '{')
3890 /* We have a switch spec. */
3891 validate_switches (p
+ 1);
3893 /* Now notice switches mentioned in the machine-specific specs. */
3897 if (c
== '%' && *p
== '{')
3898 /* We have a switch spec. */
3899 validate_switches (p
+ 1);
3903 if (c
== '%' && *p
== '{')
3904 /* We have a switch spec. */
3905 validate_switches (p
+ 1);
3909 if (c
== '%' && *p
== '{')
3910 /* We have a switch spec. */
3911 validate_switches (p
+ 1);
3913 p
= signed_char_spec
;
3915 if (c
== '%' && *p
== '{')
3916 /* We have a switch spec. */
3917 validate_switches (p
+ 1);
3921 if (c
== '%' && *p
== '{')
3922 /* We have a switch spec. */
3923 validate_switches (p
+ 1);
3927 if (c
== '%' && *p
== '{')
3928 /* We have a switch spec. */
3929 validate_switches (p
+ 1);
3933 if (c
== '%' && *p
== '{')
3934 /* We have a switch spec. */
3935 validate_switches (p
+ 1);
3939 if (c
== '%' && *p
== '{')
3940 /* We have a switch spec. */
3941 validate_switches (p
+ 1);
3945 if (c
== '%' && *p
== '{')
3946 /* We have a switch spec. */
3947 validate_switches (p
+ 1);
3950 /* Look at the switch-name that comes after START
3951 and mark as valid all supplied switches that match it. */
3954 validate_switches (start
)
3957 register char *p
= start
;
3972 while (*p
!= ':' && *p
!= '}') p
++;
3976 else if (p
[-1] == '*')
3978 /* Mark all matching switches as valid. */
3980 for (i
= 0; i
< n_switches
; i
++)
3981 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
))
3982 switches
[i
].valid
= 1;
3986 /* Mark an exact matching switch as valid. */
3987 for (i
= 0; i
< n_switches
; i
++)
3989 if (!strncmp (switches
[i
].part1
, filter
, p
- filter
)
3990 && switches
[i
].part1
[p
- filter
] == 0)
3991 switches
[i
].valid
= 1;