1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 1989, 1992, 1993 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;
108 extern const char *const sys_errlist[];
110 extern char *sys_errlist[];
113 extern int execv (), execvp ();
115 /* If a stage of compilation returns an exit status >= 1,
116 compilation of that file ceases. */
118 #define MIN_FATAL_STATUS 1
120 /* Flag saying to print the full filename of libgcc.a
121 as found through our usual search mechanism. */
123 static int print_libgcc_file_name;
125 /* Flag indicating whether we should print the command and arguments */
127 static int verbose_flag;
129 /* Nonzero means write "temp" files in source directory
130 and use the source file's name in them, and don't delete them. */
132 static int save_temps_flag;
134 /* The compiler version. */
136 static char *compiler_version;
138 /* The target version specified with -V */
140 static char *spec_version = DEFAULT_TARGET_VERSION;
142 /* The target machine specified with -b. */
144 static char *spec_machine = DEFAULT_TARGET_MACHINE;
146 /* Nonzero if cross-compiling.
147 When -b is used, the value comes from the `specs' file. */
150 static int cross_compile = 1;
152 static int cross_compile = 0;
155 /* The number of errors that have occurred; the link phase will not be
156 run if this is non-zero. */
157 static int error_count = 0;
159 /* This is the obstack which we use to allocate many strings. */
161 static struct obstack obstack;
163 /* This is the obstack to build an environment variable to pass to
164 collect2 that describes all of the relevant switches of what to
165 pass the compiler in building the list of pointers to constructors
168 static struct obstack collect_obstack;
170 extern char *version_string;
172 static void set_spec ();
173 static struct compiler *lookup_compiler ();
174 static char *find_a_file ();
175 static void add_prefix ();
176 static char *skip_whitespace ();
177 static void record_temp_file ();
178 static char *handle_braces ();
179 static char *save_string ();
180 static char *concat ();
181 static int do_spec ();
182 static int do_spec_1 ();
183 static char *find_file ();
184 static int is_directory ();
185 static void validate_switches ();
186 static void validate_all_switches ();
187 static void give_switch ();
188 static void pfatal_with_name ();
189 static void perror_with_name ();
190 static void perror_exec ();
191 static void fatal ();
192 static void error ();
197 /* Specs are strings containing lines, each of which (if not blank)
198 is made up of a program name, and arguments separated by spaces.
199 The program name must be exact and start from root, since no path
200 is searched and it is unreliable to depend on the current working directory.
201 Redirection of input or output is not supported; the subprograms must
202 accept filenames saying what files to read and write.
204 In addition, the specs can contain %-sequences to substitute variable text
205 or for conditional text. Here is a table of all defined %-sequences.
206 Note that spaces are not generated automatically around the results of
207 expanding these sequences; therefore, you can concatenate them together
208 or with constant text in a single argument.
210 %% substitute one % into the program name or argument.
211 %i substitute the name of the input file being processed.
212 %b substitute the basename of the input file being processed.
213 This is the substring up to (and not including) the last period
214 and not including the directory.
215 %g substitute the temporary-file-name-base. This is a string chosen
216 once per compilation. Different temporary file names are made by
217 concatenation of constant strings on the end, as in `%g.s'.
218 %g also has the same effect of %d.
219 %u like %g, but make the temporary file name unique.
220 %U returns the last file name generated with %u.
221 %d marks the argument containing or following the %d as a
222 temporary file name, so that that file will be deleted if CC exits
223 successfully. Unlike %g, this contributes no text to the argument.
224 %w marks the argument containing or following the %w as the
225 "output file" of this compilation. This puts the argument
226 into the sequence of arguments that %o will substitute later.
228 like %{...} but mark last argument supplied within
229 as a file to be deleted on failure.
230 %o substitutes the names of all the output files, with spaces
231 automatically placed around them. You should write spaces
232 around the %o as well or the results are undefined.
233 %o is for use in the specs for running the linker.
234 Input files whose names have no recognized suffix are not compiled
235 at all, but they are included among the output files, so they will
237 %p substitutes the standard macro predefinitions for the
238 current target machine. Use this when running cpp.
239 %P like %p, but puts `__' before and after the name of each macro.
240 (Except macros that already have __.)
242 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
243 %s current argument is the name of a library or startup file of some sort.
244 Search for that file in a standard list of directories
245 and substitute the full name found.
246 %eSTR Print STR as an error message. STR is terminated by a newline.
247 Use this when inconsistent options are detected.
248 %x{OPTION} Accumulate an option for %X.
249 %X Output the accumulated linker options specified by compilations.
250 %Y Output the accumulated assembler options specified by compilations.
251 %v1 Substitute the major version number of GCC.
252 (For version 2.5.n, this is 2.)
253 %v2 Substitute the minor version number of GCC.
254 (For version 2.5.n, this is 5.)
255 %a process ASM_SPEC as a spec.
256 This allows config.h to specify part of the spec for running as.
257 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
258 used here. This can be used to run a post-processor after the
259 assembler has done it's job.
260 %D Dump out a -L option for each directory in startfile_prefix.
261 %l process LINK_SPEC as a spec.
262 %L process LIB_SPEC as a spec.
263 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
264 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
265 %c process SIGNED_CHAR_SPEC as a spec.
266 %C process CPP_SPEC as a spec. A capital C is actually used here.
267 %1 process CC1_SPEC as a spec.
268 %2 process CC1PLUS_SPEC as a spec.
269 %| output "-" if the input for the current command is coming from a pipe.
270 %* substitute the variable part of a matched option. (See below.)
271 Note that each comma in the substituted string is replaced by
273 %{S} substitutes the -S switch, if that switch was given to CC.
274 If that switch was not specified, this substitutes nothing.
275 Here S is a metasyntactic variable.
276 %{S*} substitutes all the switches specified to CC whose names start
277 with -S. This is used for -o, -D, -I, etc; switches that take
278 arguments. CC considers `-o foo' as being one switch whose
279 name starts with `o'. %{o*} would substitute this text,
280 including the space; thus, two arguments would be generated.
281 %{S*:X} substitutes X if one or more switches whose names start with -S are
282 specified to CC. Note that the tail part of the -S option
283 (i.e. the part matched by the `*') will be substituted for each
284 occurrence of %* within X.
285 %{S:X} substitutes X, but only if the -S switch was given to CC.
286 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
287 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
288 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
289 %{.S:X} substitutes X, but only if processing a file with suffix S.
290 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
291 %(Spec) processes a specification defined in a specs file as *Spec:
292 %[Spec] as above, but put __ around -D arguments
294 The conditional text X in a %{S:X} or %{!S:X} construct may contain
295 other nested % constructs or spaces, or even newlines. They are
296 processed as usual, as described above.
298 The character | is used to indicate that a command should be piped to
299 the following command, but only if -pipe is specified.
301 Note that it is built into CC which switches take arguments and which
302 do not. You might think it would be useful to generalize this to
303 allow each compiler's spec to say which switches take arguments. But
304 this cannot be done in a consistent fashion. CC cannot even decide
305 which input files have been specified without knowing which switches
306 take arguments, and it must know which input files to compile in order
307 to tell which compilers to run.
309 CC also knows implicitly that arguments starting in `-l' are to be
310 treated as compiler output files, and passed to the linker in their
311 proper position among the other output files. */
313 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
315 /* config.h can define ASM_SPEC to provide extra args to the assembler
316 or extra switch-translations. */
321 /* config.h can define ASM_FINAL_SPEC to run a post processor after
322 the assembler has run. */
323 #ifndef ASM_FINAL_SPEC
324 #define ASM_FINAL_SPEC ""
327 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
328 or extra switch-translations. */
333 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
334 or extra switch-translations. */
339 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
340 or extra switch-translations. */
342 #define CC1PLUS_SPEC ""
345 /* config.h can define LINK_SPEC to provide extra args to the linker
346 or extra switch-translations. */
351 /* config.h can define LIB_SPEC to override the default libraries. */
353 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
356 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
357 #ifndef STARTFILE_SPEC
358 #define STARTFILE_SPEC \
359 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
362 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
363 Make the string nonempty to require spaces there. */
364 #ifndef SWITCHES_NEED_SPACES
365 #define SWITCHES_NEED_SPACES ""
368 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
370 #define ENDFILE_SPEC ""
373 /* This spec is used for telling cpp whether char is signed or not. */
374 #ifndef SIGNED_CHAR_SPEC
375 /* Use #if rather than ?:
376 because MIPS C compiler rejects like ?: in initializers. */
377 #if DEFAULT_SIGNED_CHAR
378 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
380 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
384 static char *cpp_spec = CPP_SPEC;
385 static char *cpp_predefines = CPP_PREDEFINES;
386 static char *cc1_spec = CC1_SPEC;
387 static char *cc1plus_spec = CC1PLUS_SPEC;
388 static char *signed_char_spec = SIGNED_CHAR_SPEC;
389 static char *asm_spec = ASM_SPEC;
390 static char *asm_final_spec = ASM_FINAL_SPEC;
391 static char *link_spec = LINK_SPEC;
392 static char *lib_spec = LIB_SPEC;
393 static char *endfile_spec = ENDFILE_SPEC;
394 static char *startfile_spec = STARTFILE_SPEC;
395 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
397 /* This defines which switch letters take arguments. */
399 #ifndef SWITCH_TAKES_ARG
400 #define SWITCH_TAKES_ARG(CHAR) \
401 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
402 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
403 || (CHAR) == 'I' || (CHAR) == 'm' \
404 || (CHAR) == 'L' || (CHAR) == 'A')
407 /* This defines which multi-letter switches take arguments. */
409 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
410 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
411 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
412 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
413 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
414 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore"))
416 #ifndef WORD_SWITCH_TAKES_ARG
417 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
420 /* Record the mapping from file suffixes for compilation specs. */
424 char *suffix; /* Use this compiler for input files
425 whose names end in this suffix. */
427 char *spec[4]; /* To use this compiler, concatenate these
428 specs and pass to do_spec. */
431 /* Pointer to a vector of `struct compiler' that gives the spec for
432 compiling a file, based on its suffix.
433 A file that does not end in any of these suffixes will be passed
434 unchanged to the loader and nothing else will be done to it.
436 An entry containing two 0s is used to terminate the vector.
438 If multiple entries match a file, the last matching one is used. */
440 static struct compiler *compilers;
442 /* Number of entries in `compilers', not counting the null terminator. */
444 static int n_compilers;
446 /* The default list of file name suffixes and their compilation specs. */
448 static struct compiler default_compilers[] =
452 "cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
453 %{C:%{!E:%eGNU C does not support -C without using -E}}\
454 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
455 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
456 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
457 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
458 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
459 %{traditional-cpp:-traditional}\
460 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
461 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
462 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
463 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
464 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
465 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
467 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
468 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
469 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
470 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
471 %{!pipe:%g.s} %A\n }}}}"},
473 "%{E:cpp -lang-c %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
474 %{C:%{!E:%eGNU C does not support -C without using -E}}\
475 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
476 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
477 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
478 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
479 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
480 %{traditional-cpp:-traditional}\
481 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
483 %{!E:%e-E required when input is from standard input}"},
484 {".m", "@objective-c"},
486 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
487 %{C:%{!E:%eGNU C does not support -C without using -E}}\
488 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
489 -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
490 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
491 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
492 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
493 %{traditional-cpp:-traditional}\
494 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
495 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
496 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
497 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
498 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
499 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
500 -lang-objc %{gen-decls} \
502 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
503 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
504 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
505 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
506 %{!pipe:%g.s} %A\n }}}}"},
509 "%{!E:%eCompilation of header file requested} \
510 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
511 %{C:%{!E:%eGNU C does not support -C without using -E}}\
512 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
513 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
514 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
515 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
516 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
517 %{traditional-cpp:-traditional}\
518 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
525 "cpp -lang-c++ %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
526 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
527 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
528 -undef -D__GNUC__=%v1 -D__GNUG__=%v1 -D__cplusplus -D__GNUC_MINOR__=%v2\
529 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
530 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
531 %{traditional-cpp:-traditional} %{trigraphs}\
532 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
533 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
534 "%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
535 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
536 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
537 %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
539 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
540 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
541 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
542 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
543 %{!pipe:%g.s} %A\n }}}}"},
544 {".i", "@cpp-output"},
546 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
547 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
548 %{v:-version} %{pg:-p} %{p} %{f*}\
550 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
551 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
552 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
553 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %{!pipe:%g.s} %A\n }"},
554 {".ii", "@c++-cpp-output"},
556 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
557 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
558 %{v:-version} %{pg:-p} %{p} %{f*} %{+e*}\
560 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
561 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
562 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
563 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
564 %{!pipe:%g.s} %A\n }"},
565 {".s", "@assembler"},
567 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
568 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o} %i %A\n }"},
569 {".S", "@assembler-with-cpp"},
570 {"@assembler-with-cpp",
571 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
572 %{C:%{!E:%eGNU C does not support -C without using -E}}\
573 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
574 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
575 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
576 %{traditional-cpp:-traditional}\
577 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*}\
578 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
579 "%{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
580 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
581 %{!pipe:%g.s} %A\n }}}}"},
586 "gnat1 %{gnat*} %{k8:-gnatk8} %{!v:-quiet -gnatb} %{w:-gnatws}\
588 %{g*} %{O*} %{p} %{pg:-p} %{f*} %{d*}\
589 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
590 %i %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} | \n",
591 "%{!S:%{!gnatc:%{!gnats:as %{R} %{j} %{J} %{h} %{d2} %a %Y\
592 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%u.o}\
593 %{!pipe:%g.s} %A\n}}} "},
594 /* Mark end of table */
598 /* Number of elements in default_compilers, not counting the terminator. */
600 static int n_default_compilers
601 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
603 /* Here is the spec for running the linker, after compiling all files. */
605 /* -u* was put back because both BSD and SysV seem to support it. */
606 /* %{static:} simply prevents an error message if the target machine
607 doesn't handle -static. */
608 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
609 scripts which exist in user specified directories, or in standard
611 #ifdef LINK_LIBGCC_SPECIAL_1
612 /* Have gcc do the search for libgcc.a, but generate -L options as usual. */
613 static char *link_command_spec = "\
615 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
616 %{r} %{s} %{t} %{u*} %{x} %{z}\
617 %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
618 %{L*} %D %{T*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}}";
620 #ifdef LINK_LIBGCC_SPECIAL
621 /* Have gcc do the search for libgcc.a, and don't generate -L options. */
622 static char *link_command_spec = "\
624 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
625 %{r} %{s} %{t} %{u*} %{x} %{z}\
626 %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
627 %{L*} %{T*} %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}}";
629 /* Use -L and have the linker do the search for -lgcc. */
630 static char *link_command_spec = "\
632 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
633 %{r} %{s} %{t} %{u*} %{x} %{z}\
634 %{!A:%{!nostartfiles:%{!nostdlib:%S}}} %{static:}\
635 %{L*} %D %{T*} %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}}";
639 /* A vector of options to give to the linker.
640 These options are accumulated by -Xlinker and -Wl,
641 and substituted into the linker command with %X. */
642 static int n_linker_options;
643 static char **linker_options;
645 /* A vector of options to give to the assembler.
646 These options are accumulated by -Wa,
647 and substituted into the assembler command with %X. */
648 static int n_assembler_options;
649 static char **assembler_options;
651 /* Define how to map long options into short ones. */
653 /* This structure describes one mapping. */
656 /* The long option's name. */
658 /* The equivalent short option. */
660 /* Argument info. A string of flag chars; NULL equals no options.
661 a => argument required.
662 o => argument optional.
663 j => join argument to equivalent, making one word.
664 * => allow other text after NAME as an argument. */
668 /* This is the table of mappings. Mappings are tried sequentially
669 for each option encountered; the first one that matches, wins. */
671 struct option_map option_map[] =
673 {"--profile-blocks", "-a", 0},
674 {"--target", "-b", "a"},
675 {"--compile", "-c", 0},
676 {"--dump", "-d", "a"},
677 {"--entry", "-e", 0},
678 {"--debug", "-g", "oj"},
679 {"--include", "-include", "a"},
680 {"--imacros", "-imacros", "a"},
681 {"--include-prefix", "-iprefix", "a"},
682 {"--include-directory-after", "-idirafter", "a"},
683 {"--include-with-prefix", "-iwithprefix", "a"},
684 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
685 {"--include-with-prefix-after", "-iwithprefix", "a"},
686 {"--machine-", "-m", "*j"},
687 {"--machine", "-m", "aj"},
688 {"--no-standard-includes", "-nostdinc", 0},
689 {"--no-standard-libraries", "-nostdlib", 0},
690 {"--no-precompiled-includes", "-noprecomp", 0},
691 {"--output", "-o", "a"},
692 {"--profile", "-p", 0},
693 {"--quiet", "-q", 0},
694 {"--silent", "-q", 0},
695 {"--force-link", "-u", "a"},
696 {"--verbose", "-v", 0},
697 {"--version", "-dumpversion", 0},
698 {"--no-warnings", "-w", 0},
699 {"--language", "-x", "a"},
701 {"--assert", "-A", "a"},
702 {"--prefix", "-B", "a"},
703 {"--comments", "-C", 0},
704 {"--define-macro", "-D", "a"},
705 {"--preprocess", "-E", 0},
706 {"--trace-includes", "-H", 0},
707 {"--include-directory", "-I", "a"},
708 {"--include-barrier", "-I-", 0},
709 {"--library-directory", "-L", "a"},
710 {"--dependencies", "-M", 0},
711 {"--user-dependencies", "-MM", 0},
712 {"--write-dependencies", "-MD", 0},
713 {"--write-user-dependencies", "-MMD", 0},
714 {"--optimize", "-O", "oj"},
715 {"--no-line-commands", "-P", 0},
716 {"--assemble", "-S", 0},
717 {"--undefine-macro", "-U", "a"},
718 {"--use-version", "-V", "a"},
719 {"--for-assembler", "-Wa", "a"},
720 {"--extra-warnings", "-W", 0},
721 {"--all-warnings", "-Wall", 0},
722 {"--warn-", "-W", "*j"},
723 {"--for-linker", "-Xlinker", "a"},
725 {"--ansi", "-ansi", 0},
726 {"--traditional", "-traditional", 0},
727 {"--traditional-cpp", "-traditional-cpp", 0},
728 {"--trigraphs", "-trigraphs", 0},
729 {"--pipe", "-pipe", 0},
730 {"--dumpbase", "-dumpbase", "a"},
731 {"--pedantic", "-pedantic", 0},
732 {"--pedantic-errors", "-pedantic-errors", 0},
733 {"--save-temps", "-save-temps", 0},
734 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
735 {"--static", "-static", 0},
736 {"--shared", "-shared", 0},
737 {"--symbolic", "-symbolic", 0},
741 /* Translate the options described by *ARGCP and *ARGVP.
742 Make a new vector and store it back in *ARGVP,
743 and store its length in *ARGVC. */
746 translate_options (argcp, argvp)
752 char **argv = *argvp;
753 char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
757 newv[newindex++] = argv[i++];
761 /* Translate -- options. */
762 if (argv[i][0] == '-' && argv[i][1] == '-')
764 /* Find a mapping that applies to this option. */
765 for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
767 int optlen = strlen (option_map[j].name);
768 int complen = strlen (argv[i]);
769 char *arginfo = option_map[j].arg_info;
773 if (complen > optlen)
775 if (!strncmp (argv[i], option_map[j].name, complen))
777 int extra = strlen (argv[i]) > optlen;
782 /* If the option has an argument, accept that. */
783 if (argv[i][optlen] == '=')
784 arg = argv[i] + optlen + 1;
785 /* If this mapping allows extra text at end of name,
786 accept that as "argument". */
787 else if (index (arginfo, '*') != 0)
788 arg = argv[i] + optlen;
789 /* Otherwise, extra text at end means mismatch.
790 Try other mappings. */
794 else if (index (arginfo, '*') != 0)
795 error ("Incomplete `%s' option", option_map[j].name);
797 /* Handle arguments. */
798 if (index (arginfo, 'o') != 0)
803 error ("Missing argument to `%s' option",
808 else if (index (arginfo, '*') != 0)
810 else if (index (arginfo, 'a') == 0)
813 error ("Extraneous argument to `%s' option",
818 /* Store the translation as one argv elt or as two. */
819 if (arg != 0 && index (arginfo, 'j') != 0)
820 newv[newindex++] = concat (option_map[j].equivalent,
824 newv[newindex++] = option_map[j].equivalent;
825 newv[newindex++] = arg;
828 newv[newindex++] = option_map[j].equivalent;
835 /* Handle old-fashioned options--just copy them through,
836 with their arguments. */
837 else if (argv[i][0] == '-')
839 char *p = argv[i] + 1;
843 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
844 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
845 else if (WORD_SWITCH_TAKES_ARG (p))
846 nskip += WORD_SWITCH_TAKES_ARG (p);
850 newv[newindex++] = argv[i++];
855 /* Ordinary operands, or +e options. */
856 newv[newindex++] = argv[i++];
865 /* Read compilation specs from a file named FILENAME,
866 replacing the default ones.
868 A suffix which starts with `*' is a definition for
869 one of the machine-specific sub-specs. The "suffix" should be
870 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
871 The corresponding spec is stored in asm_spec, etc.,
872 rather than in the `compilers' vector.
874 Anything invalid in the file is a fatal error. */
877 read_specs (filename)
886 fprintf (stderr, "Reading specs from %s\n", filename);
888 /* Open and stat the file. */
889 desc = open (filename, 0, 0);
891 pfatal_with_name (filename);
892 if (stat (filename, &statbuf) < 0)
893 pfatal_with_name (filename);
895 /* Read contents of file into BUFFER. */
896 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
897 read (desc, buffer, (unsigned) statbuf.st_size);
898 buffer[statbuf.st_size] = 0;
901 /* Scan BUFFER for specs, putting them in the vector. */
907 char *in, *out, *p1, *p2;
909 /* Advance P in BUFFER to the next nonblank nocomment line. */
910 p = skip_whitespace (p);
914 /* Find the colon that should end the suffix. */
916 while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
917 /* The colon shouldn't be missing. */
919 fatal ("specs file malformed after %d characters", p1 - buffer);
920 /* Skip back over trailing whitespace. */
922 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
923 /* Copy the suffix to a string. */
924 suffix = save_string (p, p2 - p);
925 /* Find the next line. */
926 p = skip_whitespace (p1 + 1);
928 fatal ("specs file malformed after %d characters", p - buffer);
930 /* Find next blank line. */
931 while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
932 /* Specs end at the blank line and do not include the newline. */
933 spec = save_string (p, p1 - p);
936 /* Delete backslash-newline sequences from the spec. */
941 if (in[0] == '\\' && in[1] == '\n')
943 else if (in[0] == '#')
945 while (*in && *in != '\n') in++;
952 if (suffix[0] == '*')
954 if (! strcmp (suffix, "*link_command"))
955 link_command_spec = spec;
957 set_spec (suffix + 1, spec);
961 /* Add this pair to the vector. */
963 = ((struct compiler *)
964 xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
965 compilers[n_compilers].suffix = suffix;
966 bzero (compilers[n_compilers].spec,
967 sizeof compilers[n_compilers].spec);
968 compilers[n_compilers].spec[0] = spec;
970 bzero (&compilers[n_compilers], sizeof compilers[n_compilers]);
974 link_command_spec = spec;
977 if (link_command_spec == 0)
978 fatal ("spec file has no spec for linking");
987 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
988 be considered whitespace. */
989 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
991 else if (*p == '\n' || *p == ' ' || *p == '\t')
995 while (*p != '\n') p++;
1005 /* Structure to keep track of the specs that have been defined so far. These
1006 are accessed using %(specname) or %[specname] in a compiler or link spec. */
1010 char *name; /* Name of the spec. */
1011 char *spec; /* The spec itself. */
1012 struct spec_list *next; /* Next spec in linked list. */
1015 /* List of specs that have been defined so far. */
1017 static struct spec_list *specs = (struct spec_list *) 0;
1019 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1020 removed; If the spec starts with a + then SPEC is added to the end of the
1024 set_spec (name, spec)
1028 struct spec_list *sl;
1031 /* See if the spec already exists */
1032 for (sl = specs; sl; sl = sl->next)
1033 if (strcmp (sl->name, name) == 0)
1038 /* Not found - make it */
1039 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1040 sl->name = save_string (name, strlen (name));
1041 sl->spec = save_string ("", 0);
1046 old_spec = sl->spec;
1047 if (name && spec[0] == '+' && isspace (spec[1]))
1048 sl->spec = concat (old_spec, spec + 1, "");
1050 sl->spec = save_string (spec, strlen (spec));
1052 if (! strcmp (name, "asm"))
1053 asm_spec = sl->spec;
1054 else if (! strcmp (name, "asm_final"))
1055 asm_final_spec = sl->spec;
1056 else if (! strcmp (name, "cc1"))
1057 cc1_spec = sl->spec;
1058 else if (! strcmp (name, "cc1plus"))
1059 cc1plus_spec = sl->spec;
1060 else if (! strcmp (name, "cpp"))
1061 cpp_spec = sl->spec;
1062 else if (! strcmp (name, "endfile"))
1063 endfile_spec = sl->spec;
1064 else if (! strcmp (name, "lib"))
1065 lib_spec = sl->spec;
1066 else if (! strcmp (name, "link"))
1067 link_spec = sl->spec;
1068 else if (! strcmp (name, "predefines"))
1069 cpp_predefines = sl->spec;
1070 else if (! strcmp (name, "signed_char"))
1071 signed_char_spec = sl->spec;
1072 else if (! strcmp (name, "startfile"))
1073 startfile_spec = sl->spec;
1074 else if (! strcmp (name, "switches_need_spaces"))
1075 switches_need_spaces = sl->spec;
1076 else if (! strcmp (name, "cross_compile"))
1077 cross_compile = atoi (sl->spec);
1078 /* Free the old spec */
1083 /* Accumulate a command (program name and args), and run it. */
1085 /* Vector of pointers to arguments in the current line of specifications. */
1087 static char **argbuf;
1089 /* Number of elements allocated in argbuf. */
1091 static int argbuf_length;
1093 /* Number of elements in argbuf currently in use (containing args). */
1095 static int argbuf_index;
1097 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1098 temp file. Used only if MKTEMP_EACH_FILE. */
1100 static struct temp_name {
1101 char *suffix; /* suffix associated with the code. */
1102 int length; /* strlen (suffix). */
1103 int unique; /* Indicates whether %g or %u/%U was used. */
1104 char *filename; /* associated filename. */
1105 int filename_length; /* strlen (filename). */
1106 struct temp_name *next;
1109 /* Number of commands executed so far. */
1111 static int execution_count;
1113 /* Number of commands that exited with a signal. */
1115 static int signal_count;
1117 /* Name with which this program was invoked. */
1119 static char *programname;
1121 /* Structures to keep track of prefixes to try when looking for files. */
1125 char *prefix; /* String to prepend to the path. */
1126 struct prefix_list *next; /* Next in linked list. */
1127 int require_machine_suffix; /* Don't use without machine_suffix. */
1128 /* 2 means try both machine_suffix and just_machine_suffix. */
1129 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1134 struct prefix_list *plist; /* List of prefixes to try */
1135 int max_len; /* Max length of a prefix in PLIST */
1136 char *name; /* Name of this list (used in config stuff) */
1139 /* List of prefixes to try when looking for executables. */
1141 static struct path_prefix exec_prefix = { 0, 0, "exec" };
1143 /* List of prefixes to try when looking for startup (crt0) files. */
1145 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
1147 /* Suffix to attach to directories searched for commands.
1148 This looks like `MACHINE/VERSION/'. */
1150 static char *machine_suffix = 0;
1152 /* Suffix to attach to directories searched for commands.
1153 This is just `MACHINE/'. */
1155 static char *just_machine_suffix = 0;
1157 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1159 static char *gcc_exec_prefix;
1161 /* Default prefixes to attach to command names. */
1163 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1164 #undef MD_EXEC_PREFIX
1165 #undef MD_STARTFILE_PREFIX
1166 #undef MD_STARTFILE_PREFIX_1
1169 #ifndef STANDARD_EXEC_PREFIX
1170 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1171 #endif /* !defined STANDARD_EXEC_PREFIX */
1173 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1174 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1175 #ifdef MD_EXEC_PREFIX
1176 static char *md_exec_prefix = MD_EXEC_PREFIX;
1179 #ifndef STANDARD_STARTFILE_PREFIX
1180 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1181 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1183 #ifdef MD_STARTFILE_PREFIX
1184 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1186 #ifdef MD_STARTFILE_PREFIX_1
1187 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1189 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1190 static char *standard_startfile_prefix_1 = "/lib/";
1191 static char *standard_startfile_prefix_2 = "/usr/lib/";
1193 #ifndef TOOLDIR_BASE_PREFIX
1194 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1196 static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1197 static char *tooldir_prefix;
1199 /* Clear out the vector of arguments (after a command is executed). */
1207 /* Add one argument to the vector at the end.
1208 This is done when a space is seen or at the end of the line.
1209 If DELETE_ALWAYS is nonzero, the arg is a filename
1210 and the file should be deleted eventually.
1211 If DELETE_FAILURE is nonzero, the arg is a filename
1212 and the file should be deleted if this compilation fails. */
1215 store_arg (arg, delete_always, delete_failure)
1217 int delete_always, delete_failure;
1219 if (argbuf_index + 1 == argbuf_length)
1221 argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1224 argbuf[argbuf_index++] = arg;
1225 argbuf[argbuf_index] = 0;
1227 if (delete_always || delete_failure)
1228 record_temp_file (arg, delete_always, delete_failure);
1231 /* Record the names of temporary files we tell compilers to write,
1232 and delete them at the end of the run. */
1234 /* This is the common prefix we use to make temp file names.
1235 It is chosen once for each run of this program.
1236 It is substituted into a spec by %g.
1237 Thus, all temp file names contain this prefix.
1238 In practice, all temp file names start with this prefix.
1240 This prefix comes from the envvar TMPDIR if it is defined;
1241 otherwise, from the P_tmpdir macro if that is defined;
1242 otherwise, in /usr/tmp or /tmp. */
1244 static char *temp_filename;
1246 /* Length of the prefix. */
1248 static int temp_filename_length;
1250 /* Define the list of temporary files to delete. */
1255 struct temp_file *next;
1258 /* Queue of files to delete on success or failure of compilation. */
1259 static struct temp_file *always_delete_queue;
1260 /* Queue of files to delete on failure of compilation. */
1261 static struct temp_file *failure_delete_queue;
1263 /* Record FILENAME as a file to be deleted automatically.
1264 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1265 otherwise delete it in any case.
1266 FAIL_DELETE nonzero means delete it if a compilation step fails;
1267 otherwise delete it in any case. */
1270 record_temp_file (filename, always_delete, fail_delete)
1275 register char *name;
1276 name = xmalloc (strlen (filename) + 1);
1277 strcpy (name, filename);
1281 register struct temp_file *temp;
1282 for (temp = always_delete_queue; temp; temp = temp->next)
1283 if (! strcmp (name, temp->name))
1285 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1286 temp->next = always_delete_queue;
1288 always_delete_queue = temp;
1294 register struct temp_file *temp;
1295 for (temp = failure_delete_queue; temp; temp = temp->next)
1296 if (! strcmp (name, temp->name))
1298 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1299 temp->next = failure_delete_queue;
1301 failure_delete_queue = temp;
1306 /* Delete all the temporary files whose names we previously recorded. */
1309 delete_temp_files ()
1311 register struct temp_file *temp;
1313 for (temp = always_delete_queue; temp; temp = temp->next)
1317 printf ("Delete %s? (y or n) ", temp->name);
1321 while (getchar () != '\n') ;
1322 if (i == 'y' || i == 'Y')
1326 if (stat (temp->name, &st) >= 0)
1328 /* Delete only ordinary files. */
1329 if (S_ISREG (st.st_mode))
1330 if (unlink (temp->name) < 0)
1332 perror_with_name (temp->name);
1337 always_delete_queue = 0;
1340 /* Delete all the files to be deleted on error. */
1343 delete_failure_queue ()
1345 register struct temp_file *temp;
1347 for (temp = failure_delete_queue; temp; temp = temp->next)
1351 printf ("Delete %s? (y or n) ", temp->name);
1355 while (getchar () != '\n') ;
1356 if (i == 'y' || i == 'Y')
1359 if (unlink (temp->name) < 0)
1361 perror_with_name (temp->name);
1367 clear_failure_queue ()
1369 failure_delete_queue = 0;
1372 /* Compute a string to use as the base of all temporary file names.
1373 It is substituted for %g. */
1376 choose_temp_base_try (try, base)
1383 else if (try == (char *)0)
1385 else if (access (try, R_OK | W_OK) != 0)
1398 base = choose_temp_base_try (getenv ("TMPDIR"), base);
1399 base = choose_temp_base_try (getenv ("TMP"), base);
1400 base = choose_temp_base_try (getenv ("TEMP"), base);
1403 base = choose_temp_base_try (P_tmpdir, base);
1406 base = choose_temp_base_try ("/usr/tmp", base);
1407 base = choose_temp_base_try ("/tmp", base);
1409 /* If all else fails, use the current directory! */
1410 if (base == (char *)0)
1413 len = strlen (base);
1414 temp_filename = xmalloc (len + sizeof("/ccXXXXXX") + 1);
1415 strcpy (temp_filename, base);
1416 if (len > 0 && temp_filename[len-1] != '/')
1417 temp_filename[len++] = '/';
1418 strcpy (temp_filename + len, "ccXXXXXX");
1420 mktemp (temp_filename);
1421 temp_filename_length = strlen (temp_filename);
1422 if (temp_filename_length == 0)
1427 /* Routine to add variables to the environment. We do this to pass
1428 the pathname of the gcc driver, and the directories search to the
1429 collect2 program, which is being run as ld. This way, we can be
1430 sure of executing the right compiler when collect2 wants to build
1431 constructors and destructors. Since the environment variables we
1432 use come from an obstack, we don't have to worry about allocating
1441 #ifndef VMS /* nor about VMS */
1443 extern char **environ;
1444 char **old_environ = environ;
1448 int str_len = strlen (str);
1452 while ((ch = *p++) != '\0' && ch != '=')
1458 /* Search for replacing an existing environment variable, and
1459 count the number of total environment variables. */
1460 for (envp = old_environ; *envp; envp++)
1463 if (!strncmp (str, *envp, name_len))
1470 /* Add a new environment variable */
1471 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1473 bcopy (old_environ, environ+1, sizeof (char *) * (num_envs+1));
1478 #endif /* HAVE_PUTENV */
1481 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables for collect. */
1484 putenv_from_prefixes (paths, env_var)
1485 struct path_prefix *paths;
1488 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1490 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1491 int first_time = TRUE;
1492 struct prefix_list *pprefix;
1494 obstack_grow (&collect_obstack, env_var, strlen (env_var));
1496 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1498 int len = strlen (pprefix->prefix);
1501 && is_directory (pprefix->prefix, machine_suffix, 0))
1504 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1507 obstack_grow (&collect_obstack, pprefix->prefix, len);
1508 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1511 if (just_machine_suffix
1512 && pprefix->require_machine_suffix == 2
1513 && is_directory (pprefix->prefix, just_machine_suffix, 0))
1516 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1519 obstack_grow (&collect_obstack, pprefix->prefix, len);
1520 obstack_grow (&collect_obstack, just_machine_suffix,
1524 if (!pprefix->require_machine_suffix)
1527 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1530 obstack_grow (&collect_obstack, pprefix->prefix, len);
1533 obstack_1grow (&collect_obstack, '\0');
1534 putenv (obstack_finish (&collect_obstack));
1538 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1539 access to check permissions.
1540 Return 0 if not found, otherwise return its name, allocated with malloc. */
1543 find_a_file (pprefix, name, mode)
1544 struct path_prefix *pprefix;
1549 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1550 struct prefix_list *pl;
1551 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1554 len += strlen (machine_suffix);
1556 temp = xmalloc (len);
1558 /* Determine the filename to execute (special case for absolute paths). */
1562 if (access (name, mode))
1564 strcpy (temp, name);
1569 for (pl = pprefix->plist; pl; pl = pl->next)
1573 strcpy (temp, pl->prefix);
1574 strcat (temp, machine_suffix);
1575 strcat (temp, name);
1576 if (access (temp, mode) == 0)
1578 if (pl->used_flag_ptr != 0)
1579 *pl->used_flag_ptr = 1;
1582 /* Some systems have a suffix for executable files.
1583 So try appending that. */
1584 if (file_suffix[0] != 0)
1586 strcat (temp, file_suffix);
1587 if (access (temp, mode) == 0)
1589 if (pl->used_flag_ptr != 0)
1590 *pl->used_flag_ptr = 1;
1595 /* Certain prefixes are tried with just the machine type,
1596 not the version. This is used for finding as, ld, etc. */
1597 if (just_machine_suffix && pl->require_machine_suffix == 2)
1599 strcpy (temp, pl->prefix);
1600 strcat (temp, just_machine_suffix);
1601 strcat (temp, name);
1602 if (access (temp, mode) == 0)
1604 if (pl->used_flag_ptr != 0)
1605 *pl->used_flag_ptr = 1;
1608 /* Some systems have a suffix for executable files.
1609 So try appending that. */
1610 if (file_suffix[0] != 0)
1612 strcat (temp, file_suffix);
1613 if (access (temp, mode) == 0)
1615 if (pl->used_flag_ptr != 0)
1616 *pl->used_flag_ptr = 1;
1621 /* Certain prefixes can't be used without the machine suffix
1622 when the machine or version is explicitly specified. */
1623 if (!pl->require_machine_suffix)
1625 strcpy (temp, pl->prefix);
1626 strcat (temp, name);
1627 if (access (temp, mode) == 0)
1629 if (pl->used_flag_ptr != 0)
1630 *pl->used_flag_ptr = 1;
1633 /* Some systems have a suffix for executable files.
1634 So try appending that. */
1635 if (file_suffix[0] != 0)
1637 strcat (temp, file_suffix);
1638 if (access (temp, mode) == 0)
1640 if (pl->used_flag_ptr != 0)
1641 *pl->used_flag_ptr = 1;
1652 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1653 at the start of the list, otherwise it goes at the end.
1655 If WARN is nonzero, we will warn if no file is found
1656 through this prefix. WARN should point to an int
1657 which will be set to 1 if this entry is used.
1659 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1660 the complete value of machine_suffix.
1661 2 means try both machine_suffix and just_machine_suffix. */
1664 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1665 struct path_prefix *pprefix;
1668 int require_machine_suffix;
1671 struct prefix_list *pl, **prev;
1674 if (!first && pprefix->plist)
1676 for (pl = pprefix->plist; pl->next; pl = pl->next)
1681 prev = &pprefix->plist;
1683 /* Keep track of the longest prefix */
1685 len = strlen (prefix);
1686 if (len > pprefix->max_len)
1687 pprefix->max_len = len;
1689 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1690 pl->prefix = save_string (prefix, len);
1691 pl->require_machine_suffix = require_machine_suffix;
1692 pl->used_flag_ptr = warn;
1699 pl->next = (struct prefix_list *) 0;
1703 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1706 unused_prefix_warnings (pprefix)
1707 struct path_prefix *pprefix;
1709 struct prefix_list *pl = pprefix->plist;
1713 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1715 error ("file path prefix `%s' never used",
1717 /* Prevent duplicate warnings. */
1718 *pl->used_flag_ptr = 1;
1724 /* Get rid of all prefixes built up so far in *PLISTP. */
1727 free_path_prefix (pprefix)
1728 struct path_prefix *pprefix;
1730 struct prefix_list *pl = pprefix->plist;
1731 struct prefix_list *temp;
1737 free (temp->prefix);
1738 free ((char *) temp);
1740 pprefix->plist = (struct prefix_list *) 0;
1743 /* stdin file number. */
1744 #define STDIN_FILE_NO 0
1746 /* stdout file number. */
1747 #define STDOUT_FILE_NO 1
1749 /* value of `pipe': port index for reading. */
1752 /* value of `pipe': port index for writing. */
1753 #define WRITE_PORT 1
1755 /* Pipe waiting from last process, to be used as input for the next one.
1756 Value is STDIN_FILE_NO if no pipe is waiting
1757 (i.e. the next command is the first of a group). */
1759 static int last_pipe_input;
1761 /* Fork one piped subcommand. FUNC is the system call to use
1762 (either execv or execvp). ARGV is the arg vector to use.
1763 NOT_LAST is nonzero if this is not the last subcommand
1764 (i.e. its output should be piped to the next one.) */
1769 /* Declare these to avoid compilation error. They won't be called. */
1770 int execv(const char *a, const char **b){}
1771 int execvp(const char *a, const char **b){}
1774 pexecute (search_flag, program, argv, not_last)
1782 int i, el = search_flag ? 0 : 4;
1784 scmd = (char *)malloc (strlen (program) + strlen (temp_filename) + 6 + el);
1785 rf = scmd + strlen(program) + 2 + el;
1786 sprintf (scmd, "%s%s @%s.gp", program,
1787 (search_flag ? "" : ".exe"), temp_filename);
1788 argfile = fopen (rf, "w");
1790 pfatal_with_name (rf);
1792 for (i=1; argv[i]; i++)
1795 for (cp = argv[i]; *cp; cp++)
1797 if (*cp == '"' || *cp == '\'' || *cp == '\\' || isspace (*cp))
1798 fputc ('\\', argfile);
1799 fputc (*cp, argfile);
1801 fputc ('\n', argfile);
1811 perror_exec (program);
1812 return MIN_FATAL_STATUS << 8;
1818 #else /* not __MSDOS__ */
1821 pexecute (search_flag, program, argv, not_last)
1827 int (*func)() = (search_flag ? execv : execvp);
1830 int input_desc = last_pipe_input;
1831 int output_desc = STDOUT_FILE_NO;
1832 int retries, sleep_interval;
1834 /* If this isn't the last process, make a pipe for its output,
1835 and record it as waiting to be the input to the next process. */
1839 if (pipe (pdes) < 0)
1840 pfatal_with_name ("pipe");
1841 output_desc = pdes[WRITE_PORT];
1842 last_pipe_input = pdes[READ_PORT];
1845 last_pipe_input = STDIN_FILE_NO;
1847 /* Fork a subprocess; wait and retry if it fails. */
1849 for (retries = 0; retries < 4; retries++)
1854 sleep (sleep_interval);
1855 sleep_interval *= 2;
1862 pfatal_with_name ("fork");
1864 pfatal_with_name ("vfork");
1870 /* Move the input and output pipes into place, if nec. */
1871 if (input_desc != STDIN_FILE_NO)
1873 close (STDIN_FILE_NO);
1877 if (output_desc != STDOUT_FILE_NO)
1879 close (STDOUT_FILE_NO);
1881 close (output_desc);
1884 /* Close the parent's descs that aren't wanted here. */
1885 if (last_pipe_input != STDIN_FILE_NO)
1886 close (last_pipe_input);
1888 /* Exec the program. */
1889 (*func) (program, argv);
1890 perror_exec (program);
1896 /* In the parent, after forking.
1897 Close the descriptors that we made for this child. */
1898 if (input_desc != STDIN_FILE_NO)
1900 if (output_desc != STDOUT_FILE_NO)
1901 close (output_desc);
1903 /* Return child's process number. */
1908 #endif /* not __MSDOS__ */
1912 pexecute (search_flag, program, argv, not_last)
1918 return (search_flag ? spawnv : spawnvp) (1, program, argv);
1920 #endif /* not OS2 */
1922 /* Execute the command specified by the arguments on the current line of spec.
1923 When using pipes, this includes several piped-together commands
1924 with `|' between them.
1926 Return 0 if successful, -1 if failed. */
1932 int n_commands; /* # of command. */
1936 char *prog; /* program name. */
1937 char **argv; /* vector of args. */
1938 int pid; /* pid of process for this command. */
1941 struct command *commands; /* each command buffer with above info. */
1943 /* Count # of piped commands. */
1944 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1945 if (strcmp (argbuf[i], "|") == 0)
1948 /* Get storage for each command. */
1950 = (struct command *) alloca (n_commands * sizeof (struct command));
1952 /* Split argbuf into its separate piped processes,
1953 and record info about each one.
1954 Also search for the programs that are to be run. */
1956 commands[0].prog = argbuf[0]; /* first command. */
1957 commands[0].argv = &argbuf[0];
1958 string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1960 commands[0].argv[0] = string;
1962 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1963 if (strcmp (argbuf[i], "|") == 0)
1964 { /* each command. */
1966 fatal ("-pipe not supported under MS-DOS");
1968 argbuf[i] = 0; /* termination of command args. */
1969 commands[n_commands].prog = argbuf[i + 1];
1970 commands[n_commands].argv = &argbuf[i + 1];
1971 string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1973 commands[n_commands].argv[0] = string;
1977 argbuf[argbuf_index] = 0;
1979 /* If -v, print what we are about to do, and maybe query. */
1983 /* Print each piped command as a separate line. */
1984 for (i = 0; i < n_commands ; i++)
1988 for (j = commands[i].argv; *j; j++)
1989 fprintf (stderr, " %s", *j);
1991 /* Print a pipe symbol after all but the last command. */
1992 if (i + 1 != n_commands)
1993 fprintf (stderr, " |");
1994 fprintf (stderr, "\n");
1998 fprintf (stderr, "\nGo ahead? (y or n) ");
2002 while (getchar () != '\n') ;
2003 if (i != 'y' && i != 'Y')
2008 /* Run each piped subprocess. */
2010 last_pipe_input = STDIN_FILE_NO;
2011 for (i = 0; i < n_commands; i++)
2013 char *string = commands[i].argv[0];
2015 commands[i].pid = pexecute (string != commands[i].prog,
2016 string, commands[i].argv,
2017 i + 1 < n_commands);
2019 if (string != commands[i].prog)
2025 /* Wait for all the subprocesses to finish.
2026 We don't care what order they finish in;
2027 we know that N_COMMANDS waits will get them all. */
2032 for (i = 0; i < n_commands; i++)
2039 status = pid = commands[i].pid;
2041 pid = wait (&status);
2049 for (j = 0; j < n_commands; j++)
2050 if (commands[j].pid == pid)
2051 prog = commands[j].prog;
2053 if ((status & 0x7F) != 0)
2055 fatal ("Internal compiler error: program %s got fatal signal %d",
2056 prog, (status & 0x7F));
2059 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
2067 /* Find all the switches given to us
2068 and make a vector describing them.
2069 The elements of the vector are strings, one per switch given.
2070 If a switch uses following arguments, then the `part1' field
2071 is the switch itself and the `args' field
2072 is a null-terminated vector containing the following arguments.
2073 The `valid' field is nonzero if any spec has looked at this switch;
2074 if it remains zero at the end of the run, it must be meaningless. */
2083 static struct switchstr *switches;
2085 static int n_switches;
2093 /* Also a vector of input files specified. */
2095 static struct infile *infiles;
2097 static int n_infiles;
2099 /* And a vector of corresponding output files is made up later. */
2101 static char **outfiles;
2103 /* Create the vector `switches' and its contents.
2104 Store its length in `n_switches'. */
2107 process_command (argc, argv)
2113 char *spec_lang = 0;
2114 int last_language_n_infiles;
2116 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
2121 /* Figure compiler version from version string. */
2123 compiler_version = save_string (version_string, strlen (version_string));
2124 for (temp = compiler_version; *temp; ++temp)
2133 /* Set up the default search paths. */
2135 if (gcc_exec_prefix)
2137 add_prefix (&exec_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
2138 add_prefix (&startfile_prefix, gcc_exec_prefix, 0, 0, NULL_PTR);
2141 /* COMPILER_PATH and LIBRARY_PATH have values
2142 that are lists of directory names with colons. */
2144 temp = getenv ("COMPILER_PATH");
2147 char *startp, *endp;
2148 char *nstore = (char *) alloca (strlen (temp) + 3);
2150 startp = endp = temp;
2153 if (*endp == PATH_SEPARATOR || *endp == 0)
2155 strncpy (nstore, startp, endp-startp);
2158 strcpy (nstore, "./");
2160 else if (endp[-1] != '/')
2162 nstore[endp-startp] = '/';
2163 nstore[endp-startp+1] = 0;
2166 nstore[endp-startp] = 0;
2167 add_prefix (&exec_prefix, nstore, 0, 0, NULL_PTR);
2170 endp = startp = endp + 1;
2177 temp = getenv ("LIBRARY_PATH");
2180 char *startp, *endp;
2181 char *nstore = (char *) alloca (strlen (temp) + 3);
2183 startp = endp = temp;
2186 if (*endp == PATH_SEPARATOR || *endp == 0)
2188 strncpy (nstore, startp, endp-startp);
2191 strcpy (nstore, "./");
2193 else if (endp[-1] != '/')
2195 nstore[endp-startp] = '/';
2196 nstore[endp-startp+1] = 0;
2199 nstore[endp-startp] = 0;
2200 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
2203 endp = startp = endp + 1;
2210 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2211 temp = getenv ("LPATH");
2214 char *startp, *endp;
2215 char *nstore = (char *) alloca (strlen (temp) + 3);
2217 startp = endp = temp;
2220 if (*endp == PATH_SEPARATOR || *endp == 0)
2222 strncpy (nstore, startp, endp-startp);
2225 strcpy (nstore, "./");
2227 else if (endp[-1] != '/')
2229 nstore[endp-startp] = '/';
2230 nstore[endp-startp+1] = 0;
2233 nstore[endp-startp] = 0;
2234 add_prefix (&startfile_prefix, nstore, 0, 0, NULL_PTR);
2237 endp = startp = endp + 1;
2244 /* Convert new-style -- options to old-style. */
2245 translate_options (&argc, &argv);
2247 /* Scan argv twice. Here, the first time, just count how many switches
2248 there will be in their vector, and how many input files in theirs.
2249 Here we also parse the switches that cc itself uses (e.g. -v). */
2251 for (i = 1; i < argc; i++)
2253 if (! strcmp (argv[i], "-dumpspecs"))
2255 printf ("*asm:\n%s\n\n", asm_spec);
2256 printf ("*asm_final:\n%s\n\n", asm_final_spec);
2257 printf ("*cpp:\n%s\n\n", cpp_spec);
2258 printf ("*cc1:\n%s\n\n", cc1_spec);
2259 printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
2260 printf ("*endfile:\n%s\n\n", endfile_spec);
2261 printf ("*link:\n%s\n\n", link_spec);
2262 printf ("*lib:\n%s\n\n", lib_spec);
2263 printf ("*startfile:\n%s\n\n", startfile_spec);
2264 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
2265 printf ("*signed_char:\n%s\n\n", signed_char_spec);
2266 printf ("*predefines:\n%s\n\n", cpp_predefines);
2267 printf ("*cross_compile:\n%d\n\n", cross_compile);
2271 else if (! strcmp (argv[i], "-dumpversion"))
2273 printf ("%s\n", version_string);
2276 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2278 print_libgcc_file_name = 1;
2280 else if (! strcmp (argv[i], "-Xlinker"))
2282 /* Pass the argument of this option to the linker when we link. */
2285 fatal ("argument to `-Xlinker' is missing");
2288 if (!linker_options)
2290 = (char **) xmalloc (n_linker_options * sizeof (char **));
2293 = (char **) xrealloc (linker_options,
2294 n_linker_options * sizeof (char **));
2296 linker_options[n_linker_options - 1] = argv[++i];
2298 else if (! strncmp (argv[i], "-Wl,", 4))
2301 /* Pass the rest of this option to the linker when we link. */
2304 if (!linker_options)
2306 = (char **) xmalloc (n_linker_options * sizeof (char **));
2309 = (char **) xrealloc (linker_options,
2310 n_linker_options * sizeof (char **));
2312 /* Split the argument at commas. */
2314 for (j = 4; argv[i][j]; j++)
2315 if (argv[i][j] == ',')
2317 linker_options[n_linker_options - 1]
2318 = save_string (argv[i] + prev, j - prev);
2321 = (char **) xrealloc (linker_options,
2322 n_linker_options * sizeof (char **));
2325 /* Record the part after the last comma. */
2326 linker_options[n_linker_options - 1] = argv[i] + prev;
2328 else if (! strncmp (argv[i], "-Wa,", 4))
2331 /* Pass the rest of this option to the assembler. */
2333 n_assembler_options++;
2334 if (!assembler_options)
2336 = (char **) xmalloc (n_assembler_options * sizeof (char **));
2339 = (char **) xrealloc (assembler_options,
2340 n_assembler_options * sizeof (char **));
2342 /* Split the argument at commas. */
2344 for (j = 4; argv[i][j]; j++)
2345 if (argv[i][j] == ',')
2347 assembler_options[n_assembler_options - 1]
2348 = save_string (argv[i] + prev, j - prev);
2349 n_assembler_options++;
2351 = (char **) xrealloc (assembler_options,
2352 n_assembler_options * sizeof (char **));
2355 /* Record the part after the last comma. */
2356 assembler_options[n_assembler_options - 1] = argv[i] + prev;
2358 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2359 /* The +e options to the C++ front-end. */
2361 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2363 register char *p = &argv[i][1];
2364 register int c = *p;
2369 if (p[1] == 0 && i + 1 == argc)
2370 fatal ("argument to `-b' is missing");
2372 spec_machine = argv[++i];
2374 spec_machine = p + 1;
2379 int *temp = (int *) xmalloc (sizeof (int));
2381 if (p[1] == 0 && i + 1 == argc)
2382 fatal ("argument to `-B' is missing");
2387 add_prefix (&exec_prefix, value, 1, 0, temp);
2388 add_prefix (&startfile_prefix, value, 1, 0, temp);
2392 case 'v': /* Print our subcommands and print versions. */
2394 /* If they do anything other than exactly `-v', don't set
2395 verbose_flag; rather, continue on to give the error. */
2402 if (p[1] == 0 && i + 1 == argc)
2403 fatal ("argument to `-V' is missing");
2405 spec_version = argv[++i];
2407 spec_version = p + 1;
2408 compiler_version = spec_version;
2412 if (!strcmp (p, "save-temps"))
2414 save_temps_flag = 1;
2421 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
2422 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
2423 else if (WORD_SWITCH_TAKES_ARG (p))
2424 i += WORD_SWITCH_TAKES_ARG (p);
2431 /* Set up the search paths before we go looking for config files. */
2433 /* These come before the md prefixes so that we will find gcc's subcommands
2434 (such as cpp) rather than those of the host system. */
2435 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2436 as well as trying the machine and the version. */
2437 add_prefix (&exec_prefix, standard_exec_prefix, 0, 2, NULL_PTR);
2438 add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 2, NULL_PTR);
2440 add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, NULL_PTR);
2441 add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, NULL_PTR);
2443 tooldir_prefix = concat (tooldir_base_prefix, spec_machine, "/");
2445 /* If tooldir is relative, base it on exec_prefix. A relative
2446 tooldir lets us move the installed tree as a unit.
2448 If GCC_EXEC_PREFIX is defined, then we want to add two relative
2449 directories, so that we can search both the user specified directory
2450 and the standard place. */
2452 if (*tooldir_prefix != '/')
2454 if (gcc_exec_prefix)
2456 char *gcc_exec_tooldir_prefix
2457 = concat (concat (gcc_exec_prefix, spec_machine, "/"),
2458 concat (spec_version, "/", tooldir_prefix),
2461 add_prefix (&exec_prefix, concat (gcc_exec_tooldir_prefix, "bin", "/"),
2463 add_prefix (&startfile_prefix, concat (gcc_exec_tooldir_prefix, "lib", "/"),
2467 tooldir_prefix = concat (concat (standard_exec_prefix, spec_machine, "/"),
2468 concat (spec_version, "/", tooldir_prefix),
2472 add_prefix (&exec_prefix, concat (tooldir_prefix, "bin", "/"),
2474 add_prefix (&startfile_prefix, concat (tooldir_prefix, "lib", "/"),
2477 /* More prefixes are enabled in main, after we read the specs file
2478 and determine whether this is cross-compilation or not. */
2481 /* Then create the space for the vectors and scan again. */
2483 switches = ((struct switchstr *)
2484 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2485 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2488 last_language_n_infiles = -1;
2490 /* This, time, copy the text of each switch and store a pointer
2491 to the copy in the vector of switches.
2492 Store all the infiles in their vector. */
2494 for (i = 1; i < argc; i++)
2496 /* Just skip the switches that were handled by the preceding loop. */
2497 if (!strcmp (argv[i], "-Xlinker"))
2499 else if (! strncmp (argv[i], "-Wl,", 4))
2501 else if (! strncmp (argv[i], "-Wa,", 4))
2503 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2505 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2507 /* Compensate for the +e options to the C++ front-end;
2508 they're there simply for cfront call-compatibility. We do
2509 some magic in default_compilers to pass them down properly.
2510 Note we deliberately start at the `+' here, to avoid passing
2511 -e0 or -e1 down into the linker. */
2512 switches[n_switches].part1 = &argv[i][0];
2513 switches[n_switches].args = 0;
2514 switches[n_switches].valid = 0;
2517 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
2519 register char *p = &argv[i][1];
2520 register int c = *p;
2522 if (c == 'B' || c == 'b' || c == 'V')
2524 /* Skip a separate arg, if any. */
2531 if (p[1] == 0 && i + 1 == argc)
2532 fatal ("argument to `-x' is missing");
2534 spec_lang = argv[++i];
2537 if (! strcmp (spec_lang, "none"))
2538 /* Suppress the warning if -xnone comes after the last input file,
2539 because alternate command interfaces like g++ might find it
2540 useful to place -xnone after each input file. */
2543 last_language_n_infiles = n_infiles;
2546 switches[n_switches].part1 = p;
2547 /* Deal with option arguments in separate argv elements. */
2548 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2549 || WORD_SWITCH_TAKES_ARG (p))
2552 int n_args = WORD_SWITCH_TAKES_ARG (p);
2556 /* Count only the option arguments in separate argv elements. */
2557 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2559 if (i + n_args >= argc)
2560 fatal ("argument to `-%s' is missing", p);
2561 switches[n_switches].args
2562 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2564 switches[n_switches].args[j++] = argv[++i];
2565 /* Null-terminate the vector. */
2566 switches[n_switches].args[j] = 0;
2568 else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L'))
2570 /* On some systems, ld cannot handle -o or -L without space.
2571 So split the -o or -L from its argument. */
2572 switches[n_switches].part1 = (c == 'o' ? "o" : "L");
2573 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2574 switches[n_switches].args[0] = xmalloc (strlen (p));
2575 strcpy (switches[n_switches].args[0], &p[1]);
2576 switches[n_switches].args[1] = 0;
2579 switches[n_switches].args = 0;
2580 switches[n_switches].valid = 0;
2581 /* This is always valid, since gcc.c itself understands it. */
2582 if (!strcmp (p, "save-temps"))
2583 switches[n_switches].valid = 1;
2588 if ((argv[i][0] != '-' || argv[i][1] != 'l')
2589 && strcmp (argv[i], "-")
2590 && access (argv[i], R_OK) < 0)
2592 perror_with_name (argv[i]);
2597 infiles[n_infiles].language = spec_lang;
2598 infiles[n_infiles++].name = argv[i];
2603 if (n_infiles == last_language_n_infiles && spec_lang != 0)
2604 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
2606 switches[n_switches].part1 = 0;
2607 infiles[n_infiles].name = 0;
2609 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
2610 if (gcc_exec_prefix)
2612 temp = (char *) xmalloc (strlen (gcc_exec_prefix) + strlen (spec_version)
2613 + strlen (spec_machine) + 3);
2614 strcpy (temp, gcc_exec_prefix);
2615 strcat (temp, spec_machine);
2617 strcat (temp, spec_version);
2619 gcc_exec_prefix = temp;
2623 /* Process a spec string, accumulating and running commands. */
2625 /* These variables describe the input file name.
2626 input_file_number is the index on outfiles of this file,
2627 so that the output file name can be stored for later use by %o.
2628 input_basename is the start of the part of the input file
2629 sans all directory names, and basename_length is the number
2630 of characters starting there excluding the suffix .c or whatever. */
2632 static char *input_filename;
2633 static int input_file_number;
2634 static int input_filename_length;
2635 static int basename_length;
2636 static char *input_basename;
2637 static char *input_suffix;
2639 /* These are variables used within do_spec and do_spec_1. */
2641 /* Nonzero if an arg has been started and not yet terminated
2642 (with space, tab or newline). */
2643 static int arg_going;
2645 /* Nonzero means %d or %g has been seen; the next arg to be terminated
2646 is a temporary file name. */
2647 static int delete_this_arg;
2649 /* Nonzero means %w has been seen; the next arg to be terminated
2650 is the output file name of this compilation. */
2651 static int this_is_output_file;
2653 /* Nonzero means %s has been seen; the next arg to be terminated
2654 is the name of a library file and we should try the standard
2655 search dirs for it. */
2656 static int this_is_library_file;
2658 /* Nonzero means that the input of this command is coming from a pipe. */
2659 static int input_from_pipe;
2661 /* Process the spec SPEC and run the commands specified therein.
2662 Returns 0 if the spec is successfully processed; -1 if failed. */
2672 delete_this_arg = 0;
2673 this_is_output_file = 0;
2674 this_is_library_file = 0;
2675 input_from_pipe = 0;
2677 value = do_spec_1 (spec, 0, NULL_PTR);
2679 /* Force out any unfinished command.
2680 If -pipe, this forces out the last command if it ended in `|'. */
2683 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2686 if (argbuf_index > 0)
2693 /* Process the sub-spec SPEC as a portion of a larger spec.
2694 This is like processing a whole spec except that we do
2695 not initialize at the beginning and we do not supply a
2696 newline by default at the end.
2697 INSWITCH nonzero means don't process %-sequences in SPEC;
2698 in this case, % is treated as an ordinary character.
2699 This is used while substituting switches.
2700 INSWITCH nonzero also causes SPC not to terminate an argument.
2702 Value is zero unless a line was finished
2703 and the command on that line reported an error. */
2706 do_spec_1 (spec, inswitch, soft_matched_part)
2709 char *soft_matched_part;
2711 register char *p = spec;
2718 /* If substituting a switch, treat all chars like letters.
2719 Otherwise, NL, SPC, TAB and % are special. */
2720 switch (inswitch ? 'a' : c)
2723 /* End of line: finish any pending argument,
2724 then run the pending command if one has been started. */
2727 obstack_1grow (&obstack, 0);
2728 string = obstack_finish (&obstack);
2729 if (this_is_library_file)
2730 string = find_file (string);
2731 store_arg (string, delete_this_arg, this_is_output_file);
2732 if (this_is_output_file)
2733 outfiles[input_file_number] = string;
2737 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2740 for (i = 0; i < n_switches; i++)
2741 if (!strcmp (switches[i].part1, "pipe"))
2744 /* A `|' before the newline means use a pipe here,
2745 but only if -pipe was specified.
2746 Otherwise, execute now and don't pass the `|' as an arg. */
2749 input_from_pipe = 1;
2750 switches[i].valid = 1;
2757 if (argbuf_index > 0)
2763 /* Reinitialize for a new command, and for a new argument. */
2766 delete_this_arg = 0;
2767 this_is_output_file = 0;
2768 this_is_library_file = 0;
2769 input_from_pipe = 0;
2773 /* End any pending argument. */
2776 obstack_1grow (&obstack, 0);
2777 string = obstack_finish (&obstack);
2778 if (this_is_library_file)
2779 string = find_file (string);
2780 store_arg (string, delete_this_arg, this_is_output_file);
2781 if (this_is_output_file)
2782 outfiles[input_file_number] = string;
2786 obstack_1grow (&obstack, c);
2792 /* Space or tab ends an argument if one is pending. */
2795 obstack_1grow (&obstack, 0);
2796 string = obstack_finish (&obstack);
2797 if (this_is_library_file)
2798 string = find_file (string);
2799 store_arg (string, delete_this_arg, this_is_output_file);
2800 if (this_is_output_file)
2801 outfiles[input_file_number] = string;
2803 /* Reinitialize for a new argument. */
2805 delete_this_arg = 0;
2806 this_is_output_file = 0;
2807 this_is_library_file = 0;
2814 fatal ("Invalid specification! Bug in cc.");
2817 obstack_grow (&obstack, input_basename, basename_length);
2822 delete_this_arg = 2;
2825 /* Dump out the directories specified with LIBRARY_PATH,
2826 followed by the absolute directories
2827 that we search for startfiles. */
2830 struct prefix_list *pl = startfile_prefix.plist;
2832 char *buffer = (char *) xmalloc (bufsize);
2835 for (; pl; pl = pl->next)
2837 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2838 /* Used on systems which record the specified -L dirs
2839 and use them to search for dynamic linking. */
2840 /* Relative directories always come from -B,
2841 and it is better not to use them for searching
2842 at run time. In particular, stage1 loses */
2843 if (pl->prefix[0] != '/')
2848 if (is_directory (pl->prefix, machine_suffix, 1))
2850 do_spec_1 ("-L", 0, NULL_PTR);
2851 #ifdef SPACE_AFTER_L_OPTION
2852 do_spec_1 (" ", 0, NULL_PTR);
2854 do_spec_1 (pl->prefix, 1, NULL_PTR);
2855 /* Remove slash from machine_suffix. */
2856 if (strlen (machine_suffix) >= bufsize)
2857 bufsize = strlen (machine_suffix) * 2 + 1;
2858 buffer = (char *) xrealloc (buffer, bufsize);
2859 strcpy (buffer, machine_suffix);
2860 idx = strlen (buffer);
2861 if (buffer[idx - 1] == '/')
2862 buffer[idx - 1] = 0;
2863 do_spec_1 (buffer, 1, NULL_PTR);
2864 /* Make this a separate argument. */
2865 do_spec_1 (" ", 0, NULL_PTR);
2868 if (!pl->require_machine_suffix)
2870 if (is_directory (pl->prefix, "", 1))
2872 do_spec_1 ("-L", 0, NULL_PTR);
2873 #ifdef SPACE_AFTER_L_OPTION
2874 do_spec_1 (" ", 0, NULL_PTR);
2876 /* Remove slash from pl->prefix. */
2877 if (strlen (pl->prefix) >= bufsize)
2878 bufsize = strlen (pl->prefix) * 2 + 1;
2879 buffer = (char *) xrealloc (buffer, bufsize);
2880 strcpy (buffer, pl->prefix);
2881 idx = strlen (buffer);
2882 if (buffer[idx - 1] == '/')
2883 buffer[idx - 1] = 0;
2884 do_spec_1 (buffer, 1, NULL_PTR);
2885 /* Make this a separate argument. */
2886 do_spec_1 (" ", 0, NULL_PTR);
2895 /* {...:%efoo} means report an error with `foo' as error message
2896 and don't execute any more commands for this file. */
2900 while (*p != 0 && *p != '\n') p++;
2901 buf = (char *) alloca (p - q + 1);
2902 strncpy (buf, q, p - q);
2912 if (save_temps_flag)
2913 obstack_grow (&obstack, input_basename, basename_length);
2916 #ifdef MKTEMP_EACH_FILE
2917 /* ??? This has a problem: the total number of
2918 values mktemp can return is limited.
2919 That matters for the names of object files.
2920 In 2.4, do something about that. */
2921 struct temp_name *t;
2923 while (*p == '.' || isalpha (*p))
2926 /* See if we already have an association of %g/%u/%U and
2928 for (t = temp_names; t; t = t->next)
2929 if (t->length == p - suffix
2930 && strncmp (t->suffix, suffix, p - suffix) == 0
2931 && t->unique == (c != 'g'))
2934 /* Make a new association if needed. %u requires one. */
2935 if (t == 0 || c == 'u')
2939 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
2940 t->next = temp_names;
2943 t->length = p - suffix;
2944 t->suffix = save_string (suffix, p - suffix);
2945 t->unique = (c != 'g');
2946 choose_temp_base ();
2947 t->filename = temp_filename;
2948 t->filename_length = temp_filename_length;
2951 obstack_grow (&obstack, t->filename, t->filename_length);
2952 delete_this_arg = 1;
2954 obstack_grow (&obstack, temp_filename, temp_filename_length);
2955 if (c == 'u' || c == 'U')
2961 sprintf (buff, "%d", unique);
2962 obstack_grow (&obstack, buff, strlen (buff));
2965 delete_this_arg = 1;
2971 obstack_grow (&obstack, input_filename, input_filename_length);
2976 if (gcc_exec_prefix)
2978 do_spec_1 ("-iprefix", 1, NULL_PTR);
2979 /* Make this a separate argument. */
2980 do_spec_1 (" ", 0, NULL_PTR);
2981 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
2982 do_spec_1 (" ", 0, NULL_PTR);
2989 for (f = 0; f < n_infiles; f++)
2990 store_arg (outfiles[f], 0, 0);
2995 this_is_library_file = 1;
2999 this_is_output_file = 1;
3004 int index = argbuf_index;
3005 /* Handle the {...} following the %W. */
3008 p = handle_braces (p + 1);
3011 /* If any args were output, mark the last one for deletion
3013 if (argbuf_index != index)
3014 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3018 /* %x{OPTION} records OPTION for %X to output. */
3024 /* Skip past the option value and make a copy. */
3029 string = save_string (p1 + 1, p - p1 - 2);
3031 /* See if we already recorded this option. */
3032 for (i = 0; i < n_linker_options; i++)
3033 if (! strcmp (string, linker_options[i]))
3039 /* This option is new; add it. */
3041 if (!linker_options)
3043 = (char **) xmalloc (n_linker_options * sizeof (char **));
3046 = (char **) xrealloc (linker_options,
3047 n_linker_options * sizeof (char **));
3049 linker_options[n_linker_options - 1] = string;
3053 /* Dump out the options accumulated previously using %x,
3054 -Xlinker and -Wl,. */
3056 for (i = 0; i < n_linker_options; i++)
3058 do_spec_1 (linker_options[i], 1, NULL_PTR);
3059 /* Make each accumulated option a separate argument. */
3060 do_spec_1 (" ", 0, NULL_PTR);
3064 /* Dump out the options accumulated previously using -Wa,. */
3066 for (i = 0; i < n_assembler_options; i++)
3068 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3069 /* Make each accumulated option a separate argument. */
3070 do_spec_1 (" ", 0, NULL_PTR);
3074 /* Here are digits and numbers that just process
3075 a certain constant string as a spec. */
3078 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3084 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3090 value = do_spec_1 (asm_spec, 0, NULL_PTR);
3096 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3102 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3108 value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3114 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
3120 value = do_spec_1 (link_spec, 0, NULL_PTR);
3126 value = do_spec_1 (lib_spec, 0, NULL_PTR);
3133 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
3137 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
3141 if (! strncmp (y, "-D", 2))
3142 /* Copy the whole option. */
3143 while (*y && *y != ' ' && *y != '\t')
3145 else if (*y == ' ' || *y == '\t')
3146 /* Copy whitespace to the result. */
3148 /* Don't copy other options. */
3155 value = do_spec_1 (buf, 0, NULL_PTR);
3163 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
3167 /* Copy all of CPP_PREDEFINES into BUF,
3168 but put __ after every -D and at the end of each arg. */
3172 if (! strncmp (y, "-D", 2))
3179 if (strncmp (y, "__", 2))
3181 /* Stick __ at front of macro name. */
3184 /* Arrange to stick __ at the end as well. */
3188 /* Copy the macro name. */
3189 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3198 /* Copy the value given, if any. */
3199 while (*y && *y != ' ' && *y != '\t')
3202 else if (*y == ' ' || *y == '\t')
3203 /* Copy whitespace to the result. */
3205 /* Don't copy -A options */
3211 /* Copy all of CPP_PREDEFINES into BUF,
3212 but put __ after every -D. */
3216 if (! strncmp (y, "-D", 2))
3221 if (strncmp (y, "__", 2))
3223 /* Stick __ at front of macro name. */
3228 /* Copy the macro name. */
3229 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3232 /* Copy the value given, if any. */
3233 while (*y && *y != ' ' && *y != '\t')
3236 else if (*y == ' ' || *y == '\t')
3237 /* Copy whitespace to the result. */
3239 /* Don't copy -A options */
3245 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
3249 if (! strncmp (y, "-A", 2))
3250 /* Copy the whole option. */
3251 while (*y && *y != ' ' && *y != '\t')
3253 else if (*y == ' ' || *y == '\t')
3254 /* Copy whitespace to the result. */
3256 /* Don't copy other options. */
3263 value = do_spec_1 (buf, 0, NULL_PTR);
3270 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
3275 /* Here we define characters other than letters and digits. */
3278 p = handle_braces (p);
3284 obstack_1grow (&obstack, '%');
3288 do_spec_1 (soft_matched_part, 1, NULL_PTR);
3289 do_spec_1 (" ", 0, NULL_PTR);
3292 /* Process a string found as the value of a spec given by name.
3293 This feature allows individual machine descriptions
3294 to add and use their own specs.
3295 %[...] modifies -D options the way %P does;
3296 %(...) uses the spec unmodified. */
3301 struct spec_list *sl;
3304 /* The string after the S/P is the name of a spec that is to be
3306 while (*p && *p != ')' && *p != ']')
3309 /* See if it's in the list */
3310 for (len = p - name, sl = specs; sl; sl = sl->next)
3311 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
3321 value = do_spec_1 (name, 0, NULL_PTR);
3327 char *x = (char *) alloca (strlen (name) * 2 + 1);
3331 /* Copy all of NAME into BUF, but put __ after
3332 every -D and at the end of each arg, */
3335 if (! strncmp (y, "-D", 2))
3343 else if (*y == ' ' || *y == 0)
3357 value = do_spec_1 (buf, 0, NULL_PTR);
3363 /* Discard the closing paren or bracket. */
3371 int c1 = *p++; /* Select first or second version number. */
3372 char *v = compiler_version;
3374 /* If desired, advance to second version number. */
3377 /* Set P after the first period. */
3378 while (*v != 0 && *v != ' ' && *v != '.')
3383 /* Set Q at the next period or at the end. */
3385 while (*q != 0 && *q != ' ' && *q != '.')
3387 /* Empty string means zero. */
3393 /* Put that part into the command. */
3394 obstack_grow (&obstack, v, q - v);
3400 if (input_from_pipe)
3401 do_spec_1 ("-", 0, NULL_PTR);
3410 /* Backslash: treat next character as ordinary. */
3415 /* Ordinary character: put it into the current argument. */
3416 obstack_1grow (&obstack, c);
3420 return 0; /* End of string */
3423 /* Return 0 if we call do_spec_1 and that returns -1. */
3436 /* A `|' after the open-brace means,
3437 if the test fails, output a single minus sign rather than nothing.
3438 This is used in %{|!pipe:...}. */
3442 /* A `!' after the open-brace negates the condition:
3443 succeed if the specified switch is not present. */
3447 /* A `.' after the open-brace means test against the current suffix. */
3457 while (*p != ':' && *p != '}') p++;
3460 register int count = 1;
3478 int found = (input_suffix != 0
3479 && strlen (input_suffix) == p - filter
3480 && strncmp (input_suffix, filter, p - filter) == 0);
3486 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3491 else if (p[-1] == '*' && p[0] == '}')
3493 /* Substitute all matching switches as separate args. */
3496 for (i = 0; i < n_switches; i++)
3497 if (!strncmp (switches[i].part1, filter, p - filter))
3502 /* Test for presence of the specified switch. */
3506 /* If name specified ends in *, as in {x*:...},
3507 check for %* and handle that case. */
3508 if (p[-1] == '*' && !negate)
3513 /* First see whether we have %*. */
3517 if (*r == '%' && r[1] == '*')
3521 /* If we do, handle that case. */
3524 /* Substitute all matching switches as separate args.
3525 But do this by substituting for %*
3526 in the text that follows the colon. */
3528 unsigned hard_match_len = p - filter - 1;
3529 char *string = save_string (p + 1, q - p - 2);
3531 for (i = 0; i < n_switches; i++)
3532 if (!strncmp (switches[i].part1, filter, hard_match_len))
3534 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
3535 /* Pass any arguments this switch has. */
3543 /* If name specified ends in *, as in {x*:...},
3544 check for presence of any switch name starting with x. */
3547 for (i = 0; i < n_switches; i++)
3549 unsigned hard_match_len = p - filter - 1;
3551 if (!strncmp (switches[i].part1, filter, hard_match_len))
3553 switches[i].valid = 1;
3558 /* Otherwise, check for presence of exact name specified. */
3561 for (i = 0; i < n_switches; i++)
3563 if (!strncmp (switches[i].part1, filter, p - filter)
3564 && switches[i].part1[p - filter] == 0)
3566 switches[i].valid = 1;
3573 /* If it is as desired (present for %{s...}, absent for %{-s...})
3574 then substitute either the switch or the specified
3575 conditional text. */
3576 if (present != negate)
3584 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3590 /* Here if a %{|...} conditional fails: output a minus sign,
3591 which means "standard output" or "standard input". */
3592 do_spec_1 ("-", 0, NULL_PTR);
3599 /* Pass a switch to the current accumulating command
3600 in the same form that we received it.
3601 SWITCHNUM identifies the switch; it is an index into
3602 the vector of switches gcc received, which is `switches'.
3603 This cannot fail since it never finishes a command line.
3605 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
3608 give_switch (switchnum, omit_first_word)
3610 int omit_first_word;
3612 if (!omit_first_word)
3614 do_spec_1 ("-", 0, NULL_PTR);
3615 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
3617 do_spec_1 (" ", 0, NULL_PTR);
3618 if (switches[switchnum].args != 0)
3621 for (p = switches[switchnum].args; *p; p++)
3623 do_spec_1 (*p, 1, NULL_PTR);
3624 do_spec_1 (" ", 0, NULL_PTR);
3627 switches[switchnum].valid = 1;
3630 /* Search for a file named NAME trying various prefixes including the
3631 user's -B prefix and some standard ones.
3632 Return the absolute file name found. If nothing is found, return NAME. */
3640 newname = find_a_file (&startfile_prefix, name, R_OK);
3641 return newname ? newname : name;
3644 /* Determine whether a directory exists. If LINKER, return 0 for
3645 certain fixed names not needed by the linker. If not LINKER, it is
3646 only important to return 0 if the host machine has a small ARG_MAX
3650 is_directory (path1, path2, linker)
3655 int len1 = strlen (path1);
3656 int len2 = strlen (path2);
3657 char *path = (char *) alloca (3 + len1 + len2);
3661 #ifndef SMALL_ARG_MAX
3666 /* Construct the path from the two parts. Ensure the string ends with "/.".
3667 The resulting path will be a directory even if the given path is a
3669 bcopy (path1, path, len1);
3670 bcopy (path2, path + len1, len2);
3671 cp = path + len1 + len2;
3677 /* Exclude directories that the linker is known to search. */
3679 && ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
3680 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0)))
3683 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
3686 /* On fatal signals, delete all the temporary files. */
3689 fatal_error (signum)
3692 signal (signum, SIG_DFL);
3693 delete_failure_queue ();
3694 delete_temp_files ();
3695 /* Get the same signal again, this time not handled,
3696 so its normal effect occurs. */
3697 kill (getpid (), signum);
3708 int linker_was_run = 0;
3709 char *explicit_link_files;
3713 p = argv[0] + strlen (argv[0]);
3714 while (p != argv[0] && p[-1] != '/') --p;
3717 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
3718 signal (SIGINT, fatal_error);
3720 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
3721 signal (SIGHUP, fatal_error);
3723 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
3724 signal (SIGTERM, fatal_error);
3726 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
3727 signal (SIGPIPE, fatal_error);
3731 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
3733 obstack_init (&obstack);
3735 /* Set up to remember the pathname of gcc and any options
3736 needed for collect. We use argv[0] instead of programname because
3737 we need the complete pathname. */
3738 obstack_init (&collect_obstack);
3739 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
3740 obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
3741 putenv (obstack_finish (&collect_obstack));
3743 /* Choose directory for temp files. */
3745 choose_temp_base ();
3747 /* Make a table of what switches there are (switches, n_switches).
3748 Make a table of specified input files (infiles, n_infiles).
3749 Decode switches that are handled locally. */
3751 process_command (argc, argv);
3753 /* Initialize the vector of specs to just the default.
3754 This means one element containing 0s, as a terminator. */
3756 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
3757 bcopy (default_compilers, compilers, sizeof default_compilers);
3758 n_compilers = n_default_compilers;
3760 /* Read specs from a file if there is one. */
3762 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
3763 just_machine_suffix = concat (spec_machine, "/", "");
3765 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
3766 /* Read the specs file unless it is a default one. */
3767 if (specs_file != 0 && strcmp (specs_file, "specs"))
3768 read_specs (specs_file);
3770 /* If not cross-compiling, look for startfiles in the standard places. */
3771 /* The fact that these are done here, after reading the specs file,
3772 means that it cannot be found in these directories.
3773 But that's okay. It should never be there anyway. */
3776 #ifdef MD_EXEC_PREFIX
3777 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3778 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, NULL_PTR);
3781 #ifdef MD_STARTFILE_PREFIX
3782 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, NULL_PTR);
3785 #ifdef MD_STARTFILE_PREFIX_1
3786 add_prefix (&startfile_prefix, md_startfile_prefix_1, 0, 0, NULL_PTR);
3789 /* If standard_startfile_prefix is relative, base it on
3790 standard_exec_prefix. This lets us move the installed tree
3791 as a unit. If GCC_EXEC_PREFIX is defined, base
3792 standard_startfile_prefix on that as well. */
3793 if (*standard_startfile_prefix == '/')
3794 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0,
3798 if (gcc_exec_prefix)
3799 add_prefix (&startfile_prefix,
3800 concat (gcc_exec_prefix,
3801 standard_startfile_prefix,
3804 add_prefix (&startfile_prefix,
3805 concat (standard_exec_prefix,
3807 standard_startfile_prefix),
3811 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0,
3813 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0,
3815 #if 0 /* Can cause surprises, and one can use -B./ instead. */
3816 add_prefix (&startfile_prefix, "./", 0, 1, NULL_PTR);
3820 /* Now we have the specs.
3821 Set the `valid' bits for switches that match anything in any spec. */
3823 validate_all_switches ();
3825 /* Warn about any switches that no pass was interested in. */
3827 for (i = 0; i < n_switches; i++)
3828 if (! switches[i].valid)
3829 error ("unrecognized option `-%s'", switches[i].part1);
3831 if (print_libgcc_file_name)
3833 printf ("%s\n", find_file ("libgcc.a"));
3837 /* Obey some of the options. */
3841 fprintf (stderr, "gcc version %s\n", version_string);
3847 fatal ("No input files");
3849 /* Make a place to record the compiler output file names
3850 that correspond to the input files. */
3852 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
3853 bzero (outfiles, n_infiles * sizeof (char *));
3855 /* Record which files were specified explicitly as link input. */
3857 explicit_link_files = xmalloc (n_infiles);
3858 bzero (explicit_link_files, n_infiles);
3860 for (i = 0; i < n_infiles; i++)
3862 register struct compiler *cp = 0;
3863 int this_file_error = 0;
3865 /* Tell do_spec what to substitute for %i. */
3867 input_filename = infiles[i].name;
3868 input_filename_length = strlen (input_filename);
3869 input_file_number = i;
3871 /* Use the same thing in %o, unless cp->spec says otherwise. */
3873 outfiles[i] = input_filename;
3875 /* Figure out which compiler from the file's suffix. */
3877 cp = lookup_compiler (infiles[i].name, input_filename_length,
3878 infiles[i].language);
3882 /* Ok, we found an applicable compiler. Run its spec. */
3883 /* First say how much of input_filename to substitute for %b */
3887 input_basename = input_filename;
3888 for (p = input_filename; *p; p++)
3890 input_basename = p + 1;
3892 /* Find a suffix starting with the last period,
3893 and set basename_length to exclude that suffix. */
3894 basename_length = strlen (input_basename);
3895 p = input_basename + basename_length;
3896 while (p != input_basename && *p != '.') --p;
3897 if (*p == '.' && p != input_basename)
3899 basename_length = p - input_basename;
3900 input_suffix = p + 1;
3906 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3908 len += strlen (cp->spec[j]);
3910 p = (char *) xmalloc (len + 1);
3913 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
3916 strcpy (p + len, cp->spec[j]);
3917 len += strlen (cp->spec[j]);
3920 value = do_spec (p);
3923 this_file_error = 1;
3926 /* If this file's name does not contain a recognized suffix,
3927 record it as explicit linker input. */
3930 explicit_link_files[i] = 1;
3932 /* Clear the delete-on-failure queue, deleting the files in it
3933 if this compilation failed. */
3935 if (this_file_error)
3937 delete_failure_queue ();
3940 /* If this compilation succeeded, don't delete those files later. */
3941 clear_failure_queue ();
3944 /* Run ld to link all the compiler output files. */
3946 if (error_count == 0)
3948 int tmp = execution_count;
3952 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
3954 putenv_from_prefixes (&exec_prefix, "COMPILER_PATH=");
3955 putenv_from_prefixes (&startfile_prefix, "LIBRARY_PATH=");
3957 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
3959 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
3960 sizeof ("COLLECT_GCC_OPTIONS=")-1);
3963 for (i = 0; i < n_switches; i++)
3967 obstack_grow (&collect_obstack, " ", 1);
3970 obstack_grow (&collect_obstack, "-", 1);
3971 obstack_grow (&collect_obstack, switches[i].part1,
3972 strlen (switches[i].part1));
3974 for (args = switches[i].args; args && *args; args++)
3976 obstack_grow (&collect_obstack, " ", 1);
3977 obstack_grow (&collect_obstack, *args, strlen (*args));
3980 obstack_grow (&collect_obstack, "\0", 1);
3981 putenv (obstack_finish (&collect_obstack));
3983 value = do_spec (link_command_spec);
3986 linker_was_run = (tmp != execution_count);
3989 /* Warn if a -B option was specified but the prefix was never used. */
3990 unused_prefix_warnings (&exec_prefix);
3991 unused_prefix_warnings (&startfile_prefix);
3993 /* If options said don't run linker,
3994 complain about input files to be given to the linker. */
3996 if (! linker_was_run && error_count == 0)
3997 for (i = 0; i < n_infiles; i++)
3998 if (explicit_link_files[i])
3999 error ("%s: linker input file unused since linking not done",
4002 /* Delete some or all of the temporary files we made. */
4005 delete_failure_queue ();
4006 delete_temp_files ();
4008 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
4013 /* Find the proper compilation spec for the file name NAME,
4014 whose length is LENGTH. LANGUAGE is the specified language,
4015 or 0 if none specified. */
4017 static struct compiler *
4018 lookup_compiler (name, length, language)
4023 struct compiler *cp;
4025 /* Look for the language, if one is spec'd. */
4028 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4032 if (cp->suffix[0] == '@'
4033 && !strcmp (cp->suffix + 1, language))
4037 error ("language %s not recognized", language);
4040 /* Look for a suffix. */
4041 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4043 if (/* The suffix `-' matches only the file name `-'. */
4044 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
4046 (strlen (cp->suffix) < length
4047 /* See if the suffix matches the end of NAME. */
4048 && !strcmp (cp->suffix,
4049 name + length - strlen (cp->suffix))))
4051 if (cp->spec[0][0] == '@')
4053 struct compiler *new;
4054 /* An alias entry maps a suffix to a language.
4055 Search for the language; pass 0 for NAME and LENGTH
4056 to avoid infinite recursion if language not found.
4057 Construct the new compiler spec. */
4058 language = cp->spec[0] + 1;
4059 new = (struct compiler *) xmalloc (sizeof (struct compiler));
4060 new->suffix = cp->suffix;
4061 bcopy (lookup_compiler (NULL_PTR, 0, language)->spec,
4062 new->spec, sizeof new->spec);
4065 /* A non-alias entry: return it. */
4077 register char *value = (char *) malloc (size);
4079 fatal ("virtual memory exhausted");
4084 xrealloc (ptr, size)
4088 register char *value = (char *) realloc (ptr, size);
4090 fatal ("virtual memory exhausted");
4094 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
4100 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
4101 char *result = xmalloc (len1 + len2 + len3 + 1);
4103 strcpy (result, s1);
4104 strcpy (result + len1, s2);
4105 strcpy (result + len1 + len2, s3);
4106 *(result + len1 + len2 + len3) = 0;
4112 save_string (s, len)
4116 register char *result = xmalloc (len + 1);
4118 bcopy (s, result, len);
4124 pfatal_with_name (name)
4129 if (errno < sys_nerr)
4130 s = concat ("%s: ", sys_errlist[errno], "");
4132 s = "cannot open %s";
4137 perror_with_name (name)
4142 if (errno < sys_nerr)
4143 s = concat ("%s: ", sys_errlist[errno], "");
4145 s = "cannot open %s";
4155 if (errno < sys_nerr)
4156 s = concat ("installation problem, cannot exec %s: ",
4157 sys_errlist[errno], "");
4159 s = "installation problem, cannot exec %s";
4163 /* More 'friendly' abort that prints the line and file.
4164 config.h can #define abort fancy_abort if you like that sort of thing. */
4169 fatal ("Internal gcc abort.");
4174 /* Output an error message and exit */
4184 format = va_arg (ap, char *);
4185 fprintf (stderr, "%s: ", programname);
4186 vfprintf (stderr, format, ap);
4188 fprintf (stderr, "\n");
4189 delete_temp_files ();
4201 format = va_arg (ap, char *);
4202 fprintf (stderr, "%s: ", programname);
4203 vfprintf (stderr, format, ap);
4206 fprintf (stderr, "\n");
4209 #else /* not HAVE_VPRINTF */
4212 fatal (msg, arg1, arg2)
4213 char *msg, *arg1, *arg2;
4215 error (msg, arg1, arg2);
4216 delete_temp_files ();
4221 error (msg, arg1, arg2)
4222 char *msg, *arg1, *arg2;
4224 fprintf (stderr, "%s: ", programname);
4225 fprintf (stderr, msg, arg1, arg2);
4226 fprintf (stderr, "\n");
4229 #endif /* not HAVE_VPRINTF */
4233 validate_all_switches ()
4235 struct compiler *comp;
4238 struct spec_list *spec;
4240 for (comp = compilers; comp->spec[0]; comp++)
4243 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
4247 if (c == '%' && *p == '{')
4248 /* We have a switch spec. */
4249 validate_switches (p + 1);
4253 /* look through the linked list of extra specs read from the specs file */
4254 for (spec = specs; spec ; spec = spec->next)
4258 if (c == '%' && *p == '{')
4259 /* We have a switch spec. */
4260 validate_switches (p + 1);
4263 p = link_command_spec;
4265 if (c == '%' && *p == '{')
4266 /* We have a switch spec. */
4267 validate_switches (p + 1);
4269 /* Now notice switches mentioned in the machine-specific specs. */
4273 if (c == '%' && *p == '{')
4274 /* We have a switch spec. */
4275 validate_switches (p + 1);
4279 if (c == '%' && *p == '{')
4280 /* We have a switch spec. */
4281 validate_switches (p + 1);
4285 if (c == '%' && *p == '{')
4286 /* We have a switch spec. */
4287 validate_switches (p + 1);
4289 p = signed_char_spec;
4291 if (c == '%' && *p == '{')
4292 /* We have a switch spec. */
4293 validate_switches (p + 1);
4297 if (c == '%' && *p == '{')
4298 /* We have a switch spec. */
4299 validate_switches (p + 1);
4303 if (c == '%' && *p == '{')
4304 /* We have a switch spec. */
4305 validate_switches (p + 1);
4309 if (c == '%' && *p == '{')
4310 /* We have a switch spec. */
4311 validate_switches (p + 1);
4315 if (c == '%' && *p == '{')
4316 /* We have a switch spec. */
4317 validate_switches (p + 1);
4321 if (c == '%' && *p == '{')
4322 /* We have a switch spec. */
4323 validate_switches (p + 1);
4326 /* Look at the switch-name that comes after START
4327 and mark as valid all supplied switches that match it. */
4330 validate_switches (start)
4333 register char *p = start;
4348 while (*p != ':' && *p != '}') p++;
4352 else if (p[-1] == '*')
4354 /* Mark all matching switches as valid. */
4356 for (i = 0; i < n_switches; i++)
4357 if (!strncmp (switches[i].part1, filter, p - filter))
4358 switches[i].valid = 1;
4362 /* Mark an exact matching switch as valid. */
4363 for (i = 0; i < n_switches; i++)
4365 if (!strncmp (switches[i].part1, filter, p - filter)
4366 && switches[i].part1[p - filter] == 0)
4367 switches[i].valid = 1;