]> gcc.gnu.org Git - gcc.git/blob - gcc/gcc.c
*** empty log message ***
[gcc.git] / gcc / gcc.c
1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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)
9 any later version.
10
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.
15
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.
19
20 This paragraph is here to try to keep Sun CC from dying.
21 The number of chars here seems crucial!!!! */
22
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.
28
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". */
32 \f
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <ctype.h>
36 #include <signal.h>
37 #include <sys/file.h>
38 #include <sys/stat.h>
39
40 #include "config.h"
41 #include "obstack.h"
42 #include "gvarargs.h"
43
44 #ifdef USG
45 #ifndef R_OK
46 #define R_OK 4
47 #define W_OK 2
48 #define X_OK 1
49 #endif
50
51 #define vfork fork
52 #endif /* USG */
53
54 /* On MSDOS, write temp files in current dir
55 because there's no place else we can expect to use. */
56 #if __MSDOS__
57 #ifndef P_tmpdir
58 #define P_tmpdir "./"
59 #endif
60 #endif
61
62 /* Test if something is a normal file. */
63 #ifndef S_ISREG
64 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
65 #endif
66
67 /* Test if something is a directory. */
68 #ifndef S_ISDIR
69 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
70 #endif
71
72 /* By default there is no special suffix for executables. */
73 #ifndef EXECUTABLE_SUFFIX
74 #if __MSDOS__
75 #define EXECUTABLE_SUFFIX ".exe"
76 #else
77 #define EXECUTABLE_SUFFIX ""
78 #endif
79 #endif
80
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
83
84 extern void free ();
85 extern char *getenv ();
86
87 extern int errno, sys_nerr;
88 extern char *sys_errlist[];
89
90 extern int execv (), execvp ();
91
92 /* If a stage of compilation returns an exit status >= 1,
93 compilation of that file ceases. */
94
95 #define MIN_FATAL_STATUS 1
96
97 /* Flag indicating whether we should print the command and arguments */
98
99 static int verbose_flag;
100
101 /* Nonzero means write "temp" files in source directory
102 and use the source file's name in them, and don't delete them. */
103
104 static int save_temps_flag;
105
106 /* The compiler version specified with -V */
107
108 static char *spec_version;
109
110 /* The target machine specified with -b. */
111
112 static char *spec_machine = DEFAULT_TARGET_MACHINE;
113
114 /* Nonzero if cross-compiling.
115 When -b is used, the value comes from the `specs' file. */
116
117 #ifdef CROSS_COMPILE
118 static int cross_compile = 1;
119 #else
120 static int cross_compile = 0;
121 #endif
122
123 /* This is the obstack which we use to allocate many strings. */
124
125 static struct obstack obstack;
126
127 extern char *version_string;
128
129 static void set_spec ();
130 static struct compiler *lookup_compiler ();
131 static char *find_a_file ();
132 static void add_prefix ();
133 static char *skip_whitespace ();
134 static void record_temp_file ();
135 static char *handle_braces ();
136 static char *save_string ();
137 static char *concat ();
138 static int do_spec ();
139 static int do_spec_1 ();
140 static char *find_file ();
141 static int is_linker_dir ();
142 static void validate_switches ();
143 static void validate_all_switches ();
144 static void give_switch ();
145 static void pfatal_with_name ();
146 static void perror_with_name ();
147 static void perror_exec ();
148 static void fatal ();
149 static void error ();
150 void fancy_abort ();
151 char *xmalloc ();
152 char *xrealloc ();
153 \f
154 /* Specs are strings containing lines, each of which (if not blank)
155 is made up of a program name, and arguments separated by spaces.
156 The program name must be exact and start from root, since no path
157 is searched and it is unreliable to depend on the current working directory.
158 Redirection of input or output is not supported; the subprograms must
159 accept filenames saying what files to read and write.
160
161 In addition, the specs can contain %-sequences to substitute variable text
162 or for conditional text. Here is a table of all defined %-sequences.
163 Note that spaces are not generated automatically around the results of
164 expanding these sequences; therefore, you can concatenate them together
165 or with constant text in a single argument.
166
167 %% substitute one % into the program name or argument.
168 %i substitute the name of the input file being processed.
169 %b substitute the basename of the input file being processed.
170 This is the substring up to (and not including) the last period
171 and not including the directory.
172 %g substitute the temporary-file-name-base. This is a string chosen
173 once per compilation. Different temporary file names are made by
174 concatenation of constant strings on the end, as in `%g.s'.
175 %g also has the same effect of %d.
176 %d marks the argument containing or following the %d as a
177 temporary file name, so that that file will be deleted if CC exits
178 successfully. Unlike %g, this contributes no text to the argument.
179 %w marks the argument containing or following the %w as the
180 "output file" of this compilation. This puts the argument
181 into the sequence of arguments that %o will substitute later.
182 %W{...}
183 like %{...} but mark last argument supplied within
184 as a file to be deleted on failure.
185 %o substitutes the names of all the output files, with spaces
186 automatically placed around them. You should write spaces
187 around the %o as well or the results are undefined.
188 %o is for use in the specs for running the linker.
189 Input files whose names have no recognized suffix are not compiled
190 at all, but they are included among the output files, so they will
191 be linked.
192 %p substitutes the standard macro predefinitions for the
193 current target machine. Use this when running cpp.
194 %P like %p, but puts `__' before and after the name of each macro.
195 (Except macros that already have __.)
196 This is for ANSI C.
197 %s current argument is the name of a library or startup file of some sort.
198 Search for that file in a standard list of directories
199 and substitute the full name found.
200 %eSTR Print STR as an error message. STR is terminated by a newline.
201 Use this when inconsistent options are detected.
202 %x{OPTION} Accumulate an option for %X.
203 %X Output the accumulated linker options specified by compilations.
204 %a process ASM_SPEC as a spec.
205 This allows config.h to specify part of the spec for running as.
206 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
207 used here. This can be used to run a post-processor after the
208 assembler has done it's job.
209 %D Dump out a -L option for each directory in library_prefix,
210 followed by a -L option for each directory in startfile_prefix.
211 %l process LINK_SPEC as a spec.
212 %L process LIB_SPEC as a spec.
213 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
214 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
215 %c process SIGNED_CHAR_SPEC as a spec.
216 %C process CPP_SPEC as a spec. A capital C is actually used here.
217 %1 process CC1_SPEC as a spec.
218 %2 process CC1PLUS_SPEC as a spec.
219 %* substitute the variable part of a matched option. (See below.)
220 Note that each comma in the substituted string is replaced by
221 a single space.
222 %{S} substitutes the -S switch, if that switch was given to CC.
223 If that switch was not specified, this substitutes nothing.
224 Here S is a metasyntactic variable.
225 %{S*} substitutes all the switches specified to CC whose names start
226 with -S. This is used for -o, -D, -I, etc; switches that take
227 arguments. CC considers `-o foo' as being one switch whose
228 name starts with `o'. %{o*} would substitute this text,
229 including the space; thus, two arguments would be generated.
230 %{S*:X} substitutes X if one or more switches whose names with -S are
231 specified to CC. Note that the tail part of the -S option
232 (i.e. the part matched by the `*') will be substituted for each
233 occurance of %* within X.
234 %{S:X} substitutes X, but only if the -S switch was given to CC.
235 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
236 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
237 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
238 %{.S:X} substitutes X, but only if processing a file with suffix S.
239 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
240
241 The conditional text X in a %{S:X} or %{!S:X} construct may contain
242 other nested % constructs or spaces, or even newlines. They are
243 processed as usual, as described above.
244
245 The character | is used to indicate that a command should be piped to
246 the following command, but only if -pipe is specified.
247
248 Note that it is built into CC which switches take arguments and which
249 do not. You might think it would be useful to generalize this to
250 allow each compiler's spec to say which switches take arguments. But
251 this cannot be done in a consistent fashion. CC cannot even decide
252 which input files have been specified without knowing which switches
253 take arguments, and it must know which input files to compile in order
254 to tell which compilers to run.
255
256 CC also knows implicitly that arguments starting in `-l' are to be
257 treated as compiler output files, and passed to the linker in their
258 proper position among the other output files. */
259 \f
260 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
261
262 /* config.h can define ASM_SPEC to provide extra args to the assembler
263 or extra switch-translations. */
264 #ifndef ASM_SPEC
265 #define ASM_SPEC ""
266 #endif
267
268 /* config.h can define ASM_FINAL_SPEC to run a post processor after
269 the assembler has run. */
270 #ifndef ASM_FINAL_SPEC
271 #define ASM_FINAL_SPEC ""
272 #endif
273
274 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
275 or extra switch-translations. */
276 #ifndef CPP_SPEC
277 #define CPP_SPEC ""
278 #endif
279
280 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
281 or extra switch-translations. */
282 #ifndef CC1_SPEC
283 #define CC1_SPEC ""
284 #endif
285
286 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
287 or extra switch-translations. */
288 #ifndef CC1PLUS_SPEC
289 #define CC1PLUS_SPEC ""
290 #endif
291
292 /* config.h can define LINK_SPEC to provide extra args to the linker
293 or extra switch-translations. */
294 #ifndef LINK_SPEC
295 #define LINK_SPEC ""
296 #endif
297
298 /* config.h can define LIB_SPEC to override the default libraries. */
299 #ifndef LIB_SPEC
300 #define LIB_SPEC "%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
301 #endif
302
303 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
304 #ifndef STARTFILE_SPEC
305 #define STARTFILE_SPEC \
306 "%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
307 #endif
308
309 /* config.h can define SWITCHES_NEED_SPACES to control passing -o and -L.
310 Make the string nonempty to require spaces there. */
311 #ifndef SWITCHES_NEED_SPACES
312 #define SWITCHES_NEED_SPACES ""
313 #endif
314
315 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
316 #ifndef ENDFILE_SPEC
317 #define ENDFILE_SPEC ""
318 #endif
319
320 /* This spec is used for telling cpp whether char is signed or not. */
321 #ifndef SIGNED_CHAR_SPEC
322 #define SIGNED_CHAR_SPEC \
323 (DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
324 : "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
325 #endif
326
327 static char *cpp_spec = CPP_SPEC;
328 static char *cpp_predefines = CPP_PREDEFINES;
329 static char *cc1_spec = CC1_SPEC;
330 static char *cc1plus_spec = CC1PLUS_SPEC;
331 static char *signed_char_spec = SIGNED_CHAR_SPEC;
332 static char *asm_spec = ASM_SPEC;
333 static char *asm_final_spec = ASM_FINAL_SPEC;
334 static char *link_spec = LINK_SPEC;
335 static char *lib_spec = LIB_SPEC;
336 static char *endfile_spec = ENDFILE_SPEC;
337 static char *startfile_spec = STARTFILE_SPEC;
338 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
339
340 /* This defines which switch letters take arguments. */
341
342 #ifndef SWITCH_TAKES_ARG
343 #define SWITCH_TAKES_ARG(CHAR) \
344 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
345 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
346 || (CHAR) == 'I' || (CHAR) == 'm' \
347 || (CHAR) == 'L' || (CHAR) == 'A')
348 #endif
349
350 /* This defines which multi-letter switches take arguments. */
351
352 #ifndef WORD_SWITCH_TAKES_ARG
353 #define WORD_SWITCH_TAKES_ARG(STR) \
354 (!strcmp (STR, "Tdata") || !strcmp (STR, "include") \
355 || !strcmp (STR, "imacros"))
356 #endif
357 \f
358 /* Record the mapping from file suffixes for compilation specs. */
359
360 struct compiler
361 {
362 char *suffix; /* Use this compiler for input files
363 whose names end in this suffix. */
364 char *spec; /* To use this compiler, pass this spec
365 to do_spec. */
366 };
367
368 /* Pointer to a vector of `struct compiler' that gives the spec for
369 compiling a file, based on its suffix.
370 A file that does not end in any of these suffixes will be passed
371 unchanged to the loader and nothing else will be done to it.
372
373 An entry containing two 0s is used to terminate the vector.
374
375 If multiple entries match a file, the last matching one is used. */
376
377 static struct compiler *compilers;
378
379 /* Number of entries in `compilers', not counting the null terminator. */
380
381 static int n_compilers;
382
383 /* The default list of file name suffixes and their compilation specs. */
384
385 static struct compiler default_compilers[] =
386 {
387 {".c", "@c"},
388 {"@c",
389 "cpp -lang-c %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
390 %{C:%{!E:%eGNU C does not support -C without using -E}}\
391 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
392 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
393 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
394 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
395 %{traditional-cpp:-traditional}\
396 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
397 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
398 %{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
399 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a}\
400 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
401 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
402 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
403 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
404 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
405 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
406 %{!pipe:%g.s} %A\n }}}}"},
407 {"-",
408 "%{E:cpp -lang-c %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
409 %{C:%{!E:%eGNU C does not support -C without using -E}}\
410 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
411 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
412 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
413 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
414 %{traditional-cpp:-traditional}\
415 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
416 %i %W{o*}}\
417 %{!E:%e-E required when input is from standard input}"},
418 {".m", "@objective-c"},
419 {"@objective-c",
420 "cpp -lang-objc %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
421 %{C:%{!E:%eGNU C does not support -C without using -E}}\
422 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d}\
423 -undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
424 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
425 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
426 %{traditional-cpp:-traditional}\
427 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
428 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
429 %{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
430 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a}\
431 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
432 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
433 -lang-objc %{gen-decls} \
434 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
435 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
436 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
437 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
438 %{!pipe:%g.s} %A\n }}}}"},
439 {".h", "@c-header"},
440 {"@c-header",
441 "%{!E:%eCompilation of header file requested} \
442 cpp %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
443 %{C:%{!E:%eGNU C does not support -C without using -E}}\
444 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
445 -undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
446 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
447 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
448 %{traditional-cpp:-traditional}\
449 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
450 %i %W{o*}"},
451 {".cc", "@c++"},
452 {".cxx", "@c++"},
453 {".C", "@c++"},
454 {"@c++",
455 "cpp -lang-c++ %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
456 %{C:%{!E:%eGNU C++ does not support -C without using -E}}\
457 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} \
458 -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus \
459 %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!undef:%{!ansi:%p} %P}\
460 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
461 %{traditional-cpp:-traditional} %{trigraphs}\
462 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
463 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
464 %{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.i} %1 %2\
465 %{!Q:-quiet} -dumpbase %b.cc %{d*} %{m*} %{a}\
466 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
467 %{v:-version} %{pg:-p} %{p} %{f*}\
468 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
469 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
470 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
471 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
472 %{!pipe:%g.s} %A\n }}}}"},
473 {".i", "@cpp-output"},
474 {"@cpp-output",
475 "cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a}\
476 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
477 %{v:-version} %{pg:-p} %{p} %{f*}\
478 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
479 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
480 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
481 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s} %A\n }"},
482 {".ii", "@c++-cpp-output"},
483 {"@c++-cpp-output",
484 "cc1plus %i %1 %2 %{!Q:-quiet} %{d*} %{m*} %{a}\
485 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
486 %{v:-version} %{pg:-p} %{p} %{f*}\
487 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
488 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
489 %{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
490 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
491 %{!pipe:%g.s} %A\n }"},
492 {".s", "@assembler"},
493 {"@assembler",
494 "%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
495 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i %A\n }"},
496 {".S", "@assembler-with-cpp"},
497 {"@assembler-with-cpp",
498 "cpp -lang-asm %{nostdinc} %{C} %{v} %{A*} %{D*} %{U*} %{I*} %{i*} %{P}\
499 %{C:%{!E:%eGNU C does not support -C without using -E}}\
500 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{trigraphs} \
501 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
502 %c %{O*:-D__OPTIMIZE__} %{traditional} %{ftraditional:-traditional}\
503 %{traditional-cpp:-traditional}\
504 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C\
505 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
506 %{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
507 %{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
508 %{!pipe:%g.s} %A\n }}}}"},
509 /* Mark end of table */
510 {0, 0}
511 };
512
513 /* Number of elements in default_compilers, not counting the terminator. */
514
515 static int n_default_compilers
516 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
517
518 /* Here is the spec for running the linker, after compiling all files. */
519
520 #ifdef LINK_LIBGCC_SPECIAL
521 /* Have gcc do the search. */
522 static char *link_command_spec = "\
523 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %X %l %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
524 %{r} %{s} %{T*} %{t} %{x} %{z}\
525 %{!A:%{!nostdlib:%S}} \
526 %{L*} %D %o %{!nostdlib:libgcc.a%s %L libgcc.a%s %{!A:%E}}\n }}}}}";
527 #else
528 /* Use -l and have the linker do the search. */
529 static char *link_command_spec = "\
530 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %X %l %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
531 %{r} %{T*} %{t} %{x} %{z}\
532 %{!A:%{!nostdlib:%S}} \
533 %{L*} %D %o %{!nostdlib:-lgcc %L -lgcc %{!A:%E}}\n }}}}}";
534 #endif
535
536 /* A vector of options to give to the linker.
537 These options are accumlated by %x
538 and substituted into the linker command with %X. */
539 static int n_linker_options;
540 static char **linker_options;
541 \f
542 /* Read compilation specs from a file named FILENAME,
543 replacing the default ones.
544
545 A suffix which starts with `*' is a definition for
546 one of the machine-specific sub-specs. The "suffix" should be
547 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
548 The corresponding spec is stored in asm_spec, etc.,
549 rather than in the `compilers' vector.
550
551 Anything invalid in the file is a fatal error. */
552
553 static void
554 read_specs (filename)
555 char *filename;
556 {
557 int desc;
558 struct stat statbuf;
559 char *buffer;
560 register char *p;
561
562 if (verbose_flag)
563 fprintf (stderr, "Reading specs from %s\n", filename);
564
565 /* Open and stat the file. */
566 desc = open (filename, 0, 0);
567 if (desc < 0)
568 pfatal_with_name (filename);
569 if (stat (filename, &statbuf) < 0)
570 pfatal_with_name (filename);
571
572 /* Read contents of file into BUFFER. */
573 buffer = xmalloc (statbuf.st_size + 1);
574 read (desc, buffer, statbuf.st_size);
575 buffer[statbuf.st_size] = 0;
576 close (desc);
577
578 /* Scan BUFFER for specs, putting them in the vector. */
579 p = buffer;
580 while (1)
581 {
582 char *suffix;
583 char *spec;
584 char *in, *out, *p1, *p2;
585
586 /* Advance P in BUFFER to the next nonblank nocomment line. */
587 p = skip_whitespace (p);
588 if (*p == 0)
589 break;
590
591 /* Find the colon that should end the suffix. */
592 p1 = p;
593 while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
594 /* The colon shouldn't be missing. */
595 if (*p1 != ':')
596 fatal ("specs file malformed after %d characters", p1 - buffer);
597 /* Skip back over trailing whitespace. */
598 p2 = p1;
599 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
600 /* Copy the suffix to a string. */
601 suffix = save_string (p, p2 - p);
602 /* Find the next line. */
603 p = skip_whitespace (p1 + 1);
604 if (p[1] == 0)
605 fatal ("specs file malformed after %d characters", p - buffer);
606 p1 = p;
607 /* Find next blank line. */
608 while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
609 /* Specs end at the blank line and do not include the newline. */
610 spec = save_string (p, p1 - p);
611 p = p1;
612
613 /* Delete backslash-newline sequences from the spec. */
614 in = spec;
615 out = spec;
616 while (*in != 0)
617 {
618 if (in[0] == '\\' && in[1] == '\n')
619 in += 2;
620 else if (in[0] == '#')
621 {
622 while (*in && *in != '\n') in++;
623 if (*in) in++;
624 }
625 else
626 *out++ = *in++;
627 }
628 *out = 0;
629
630 if (suffix[0] == '*')
631 {
632 if (! strcmp (suffix, "*link_command"))
633 link_command_spec = spec;
634 else
635 set_spec (suffix + 1, spec);
636 }
637 else
638 {
639 /* Add this pair to the vector. */
640 compilers
641 = ((struct compiler *)
642 xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
643 compilers[n_compilers].suffix = suffix;
644 compilers[n_compilers].spec = spec;
645 n_compilers++;
646 }
647
648 if (*suffix == 0)
649 link_command_spec = spec;
650 }
651
652 if (link_command_spec == 0)
653 fatal ("spec file has no spec for linking");
654 }
655
656 static char *
657 skip_whitespace (p)
658 char *p;
659 {
660 while (1)
661 {
662 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
663 be considered whitespace. */
664 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
665 return p + 1;
666 else if (*p == '\n' || *p == ' ' || *p == '\t')
667 p++;
668 else if (*p == '#')
669 {
670 while (*p != '\n') p++;
671 p++;
672 }
673 else
674 break;
675 }
676
677 return p;
678 }
679 \f
680 /* Structure to keep track of the specs that have been defined so far. These
681 are accessed using %Sspecname in a compiler or link spec. */
682
683 struct spec_list
684 {
685 char *name; /* Name of the spec. */
686 char *spec; /* The spec itself. */
687 struct spec_list *next; /* Next spec in linked list. */
688 };
689
690 /* List of specs that have been defined so far. */
691
692 static struct spec_list *specs = (struct spec_list *) 0;
693 \f
694 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
695 removed; If the spec starts with a + then SPEC is added to the end of the
696 current spec. */
697
698 static void
699 set_spec (name, spec)
700 char *name;
701 char *spec;
702 {
703 struct spec_list *sl;
704 char *old_spec;
705
706 /* See if the spec already exists */
707 for (sl = specs; sl; sl = sl->next)
708 if (strcmp (sl->name, name) == 0)
709 break;
710
711 if (!sl)
712 {
713 /* Not found - make it */
714 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
715 sl->name = save_string (name, strlen (name));
716 sl->spec = save_string ("", 0);
717 sl->next = specs;
718 specs = sl;
719 }
720
721 old_spec = sl->spec;
722 if (name && spec[0] == '+' && isspace (spec[1]))
723 sl->spec = concat (name, spec + 1, "");
724 else
725 sl->spec = save_string (spec, strlen (spec));
726
727 if (! strcmp (name, "asm"))
728 asm_spec = sl->spec;
729 else if (! strcmp (name, "asm_final"))
730 asm_final_spec = sl->spec;
731 else if (! strcmp (name, "cc1"))
732 cc1_spec = sl->spec;
733 else if (! strcmp (name, "cc1plus"))
734 cc1plus_spec = sl->spec;
735 else if (! strcmp (name, "cpp"))
736 cpp_spec = sl->spec;
737 else if (! strcmp (name, "endfile"))
738 endfile_spec = sl->spec;
739 else if (! strcmp (name, "lib"))
740 lib_spec = sl->spec;
741 else if (! strcmp (name, "link"))
742 link_spec = sl->spec;
743 else if (! strcmp (name, "predefines"))
744 cpp_predefines = sl->spec;
745 else if (! strcmp (name, "signed_char"))
746 signed_char_spec = sl->spec;
747 else if (! strcmp (name, "startfile"))
748 startfile_spec = sl->spec;
749 else if (! strcmp (name, "switches_need_spaces"))
750 switches_need_spaces = sl->spec;
751 else if (! strcmp (name, "cross_compile"))
752 cross_compile = atoi (sl->spec);
753 /* Free the old spec */
754 if (old_spec)
755 free (old_spec);
756 }
757 \f
758 /* Accumulate a command (program name and args), and run it. */
759
760 /* Vector of pointers to arguments in the current line of specifications. */
761
762 static char **argbuf;
763
764 /* Number of elements allocated in argbuf. */
765
766 static int argbuf_length;
767
768 /* Number of elements in argbuf currently in use (containing args). */
769
770 static int argbuf_index;
771
772 /* Number of commands executed so far. */
773
774 static int execution_count;
775
776 /* Name with which this program was invoked. */
777
778 static char *programname;
779 \f
780 /* Structures to keep track of prefixes to try when looking for files. */
781
782 struct prefix_list
783 {
784 char *prefix; /* String to prepend to the path. */
785 struct prefix_list *next; /* Next in linked list. */
786 int require_machine_suffix; /* Don't use without machine_suffix. */
787 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
788 };
789
790 struct path_prefix
791 {
792 struct prefix_list *plist; /* List of prefixes to try */
793 int max_len; /* Max length of a prefix in PLIST */
794 char *name; /* Name of this list (used in config stuff) */
795 };
796
797 /* List of prefixes to try when looking for executables. */
798
799 static struct path_prefix exec_prefix = { 0, 0, "exec" };
800
801 /* List of prefixes to try when looking for startup (crt0) files. */
802
803 static struct path_prefix startfile_prefix = { 0, 0, "startfile" };
804
805 /* List of prefixes to try when looking for libraries. */
806
807 static struct path_prefix library_prefix = { 0, 0, "libraryfile" };
808
809 /* Suffix to attach to directories searched for commands. */
810
811 static char *machine_suffix = 0;
812
813 /* Default prefixes to attach to command names. */
814
815 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
816 #undef MD_EXEC_PREFIX
817 #undef MD_STARTFILE_PREFIX
818 #endif
819
820 #ifndef STANDARD_EXEC_PREFIX
821 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
822 #endif /* !defined STANDARD_EXEC_PREFIX */
823
824 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
825 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
826 #ifdef MD_EXEC_PREFIX
827 static char *md_exec_prefix = MD_EXEC_PREFIX;
828 #endif
829
830 #ifndef STANDARD_STARTFILE_PREFIX
831 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
832 #endif /* !defined STANDARD_STARTFILE_PREFIX */
833
834 #ifdef MD_STARTFILE_PREFIX
835 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
836 #endif
837 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
838 static char *standard_startfile_prefix_1 = "/lib/";
839 static char *standard_startfile_prefix_2 = "/usr/lib/";
840
841 /* Clear out the vector of arguments (after a command is executed). */
842
843 static void
844 clear_args ()
845 {
846 argbuf_index = 0;
847 }
848
849 /* Add one argument to the vector at the end.
850 This is done when a space is seen or at the end of the line.
851 If DELETE_ALWAYS is nonzero, the arg is a filename
852 and the file should be deleted eventually.
853 If DELETE_FAILURE is nonzero, the arg is a filename
854 and the file should be deleted if this compilation fails. */
855
856 static void
857 store_arg (arg, delete_always, delete_failure)
858 char *arg;
859 int delete_always, delete_failure;
860 {
861 if (argbuf_index + 1 == argbuf_length)
862 {
863 argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
864 }
865
866 argbuf[argbuf_index++] = arg;
867 argbuf[argbuf_index] = 0;
868
869 if (delete_always || delete_failure)
870 record_temp_file (arg, delete_always, delete_failure);
871 }
872 \f
873 /* Record the names of temporary files we tell compilers to write,
874 and delete them at the end of the run. */
875
876 /* This is the common prefix we use to make temp file names.
877 It is chosen once for each run of this program.
878 It is substituted into a spec by %g.
879 Thus, all temp file names contain this prefix.
880 In practice, all temp file names start with this prefix.
881
882 This prefix comes from the envvar TMPDIR if it is defined;
883 otherwise, from the P_tmpdir macro if that is defined;
884 otherwise, in /usr/tmp or /tmp. */
885
886 static char *temp_filename;
887
888 /* Length of the prefix. */
889
890 static int temp_filename_length;
891
892 /* Define the list of temporary files to delete. */
893
894 struct temp_file
895 {
896 char *name;
897 struct temp_file *next;
898 };
899
900 /* Queue of files to delete on success or failure of compilation. */
901 static struct temp_file *always_delete_queue;
902 /* Queue of files to delete on failure of compilation. */
903 static struct temp_file *failure_delete_queue;
904
905 /* Record FILENAME as a file to be deleted automatically.
906 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
907 otherwise delete it in any case.
908 FAIL_DELETE nonzero means delete it if a compilation step fails;
909 otherwise delete it in any case. */
910
911 static void
912 record_temp_file (filename, always_delete, fail_delete)
913 char *filename;
914 int always_delete;
915 int fail_delete;
916 {
917 register char *name;
918 name = xmalloc (strlen (filename) + 1);
919 strcpy (name, filename);
920
921 if (always_delete)
922 {
923 register struct temp_file *temp;
924 for (temp = always_delete_queue; temp; temp = temp->next)
925 if (! strcmp (name, temp->name))
926 goto already1;
927 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
928 temp->next = always_delete_queue;
929 temp->name = name;
930 always_delete_queue = temp;
931 already1:;
932 }
933
934 if (fail_delete)
935 {
936 register struct temp_file *temp;
937 for (temp = failure_delete_queue; temp; temp = temp->next)
938 if (! strcmp (name, temp->name))
939 goto already2;
940 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
941 temp->next = failure_delete_queue;
942 temp->name = name;
943 failure_delete_queue = temp;
944 already2:;
945 }
946 }
947
948 /* Delete all the temporary files whose names we previously recorded. */
949
950 static void
951 delete_temp_files ()
952 {
953 register struct temp_file *temp;
954
955 for (temp = always_delete_queue; temp; temp = temp->next)
956 {
957 #ifdef DEBUG
958 int i;
959 printf ("Delete %s? (y or n) ", temp->name);
960 fflush (stdout);
961 i = getchar ();
962 if (i != '\n')
963 while (getchar () != '\n') ;
964 if (i == 'y' || i == 'Y')
965 #endif /* DEBUG */
966 {
967 struct stat st;
968 if (stat (temp->name, &st) >= 0)
969 {
970 /* Delete only ordinary files. */
971 if (S_ISREG (st.st_mode))
972 if (unlink (temp->name) < 0)
973 if (verbose_flag)
974 perror_with_name (temp->name);
975 }
976 }
977 }
978
979 always_delete_queue = 0;
980 }
981
982 /* Delete all the files to be deleted on error. */
983
984 static void
985 delete_failure_queue ()
986 {
987 register struct temp_file *temp;
988
989 for (temp = failure_delete_queue; temp; temp = temp->next)
990 {
991 #ifdef DEBUG
992 int i;
993 printf ("Delete %s? (y or n) ", temp->name);
994 fflush (stdout);
995 i = getchar ();
996 if (i != '\n')
997 while (getchar () != '\n') ;
998 if (i == 'y' || i == 'Y')
999 #endif /* DEBUG */
1000 {
1001 if (unlink (temp->name) < 0)
1002 if (verbose_flag)
1003 perror_with_name (temp->name);
1004 }
1005 }
1006 }
1007
1008 static void
1009 clear_failure_queue ()
1010 {
1011 failure_delete_queue = 0;
1012 }
1013
1014 /* Compute a string to use as the base of all temporary file names.
1015 It is substituted for %g. */
1016
1017 static void
1018 choose_temp_base ()
1019 {
1020 char *base = getenv ("TMPDIR");
1021 int len;
1022
1023 if (base == (char *)0)
1024 {
1025 #ifdef P_tmpdir
1026 if (access (P_tmpdir, R_OK | W_OK) == 0)
1027 base = P_tmpdir;
1028 #endif
1029 if (base == (char *)0)
1030 {
1031 if (access ("/usr/tmp", R_OK | W_OK) == 0)
1032 base = "/usr/tmp/";
1033 else
1034 base = "/tmp/";
1035 }
1036 }
1037
1038 len = strlen (base);
1039 temp_filename = xmalloc (len + sizeof("/ccXXXXXX"));
1040 strcpy (temp_filename, base);
1041 if (len > 0 && temp_filename[len-1] != '/')
1042 temp_filename[len++] = '/';
1043 strcpy (temp_filename + len, "ccXXXXXX");
1044
1045 mktemp (temp_filename);
1046 temp_filename_length = strlen (temp_filename);
1047 }
1048 \f
1049 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1050 access to check permissions.
1051 Return 0 if not found, otherwise return its name, allocated with malloc. */
1052
1053 static char *
1054 find_a_file (pprefix, name, mode)
1055 struct path_prefix *pprefix;
1056 char *name;
1057 int mode;
1058 {
1059 char *temp;
1060 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1061 struct prefix_list *pl;
1062 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1063
1064 if (machine_suffix)
1065 len += strlen (machine_suffix);
1066
1067 temp = xmalloc (len);
1068
1069 /* Determine the filename to execute (special case for absolute paths). */
1070
1071 if (*name == '/')
1072 {
1073 if (access (name, mode))
1074 {
1075 strcpy (temp, name);
1076 return temp;
1077 }
1078 }
1079 else
1080 for (pl = pprefix->plist; pl; pl = pl->next)
1081 {
1082 if (machine_suffix)
1083 {
1084 strcpy (temp, pl->prefix);
1085 strcat (temp, machine_suffix);
1086 strcat (temp, name);
1087 if (access (temp, mode) == 0)
1088 {
1089 if (pl->used_flag_ptr != 0)
1090 *pl->used_flag_ptr = 1;
1091 return temp;
1092 }
1093 /* Some systems have a suffix for executable files.
1094 So try appending that. */
1095 if (file_suffix[0] != 0)
1096 {
1097 strcat (temp, file_suffix);
1098 if (access (temp, mode) == 0)
1099 {
1100 if (pl->used_flag_ptr != 0)
1101 *pl->used_flag_ptr = 1;
1102 return temp;
1103 }
1104 }
1105 }
1106 /* Certain prefixes can't be used without the machine suffix
1107 when the machine or version is explicitly specified. */
1108 if (!pl->require_machine_suffix)
1109 {
1110 strcpy (temp, pl->prefix);
1111 strcat (temp, name);
1112 if (access (temp, mode) == 0)
1113 {
1114 if (pl->used_flag_ptr != 0)
1115 *pl->used_flag_ptr = 1;
1116 return temp;
1117 }
1118 /* Some systems have a suffix for executable files.
1119 So try appending that. */
1120 if (file_suffix[0] != 0)
1121 {
1122 strcat (temp, file_suffix);
1123 if (access (temp, mode) == 0)
1124 {
1125 if (pl->used_flag_ptr != 0)
1126 *pl->used_flag_ptr = 1;
1127 return temp;
1128 }
1129 }
1130 }
1131 }
1132
1133 free (temp);
1134 return 0;
1135 }
1136
1137 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1138 at the start of the list, otherwise it goes at the end.
1139
1140 If WARN is nonzero, we will warn if no file is found
1141 through this prefix. WARN should point to an int
1142 which will be set to 1 if this entry is used. */
1143
1144 static void
1145 add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
1146 struct path_prefix *pprefix;
1147 char *prefix;
1148 int first;
1149 int require_machine_suffix;
1150 int *warn;
1151 {
1152 struct prefix_list *pl, **prev;
1153 int len;
1154
1155 if (!first && pprefix->plist)
1156 {
1157 for (pl = pprefix->plist; pl->next; pl = pl->next)
1158 ;
1159 prev = &pl->next;
1160 }
1161 else
1162 prev = &pprefix->plist;
1163
1164 /* Keep track of the longest prefix */
1165
1166 len = strlen (prefix);
1167 if (len > pprefix->max_len)
1168 pprefix->max_len = len;
1169
1170 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
1171 pl->prefix = save_string (prefix, len);
1172 pl->require_machine_suffix = require_machine_suffix;
1173 pl->used_flag_ptr = warn;
1174 if (warn)
1175 *warn = 0;
1176
1177 if (*prev)
1178 pl->next = *prev;
1179 else
1180 pl->next = (struct prefix_list *) 0;
1181 *prev = pl;
1182 }
1183
1184 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
1185
1186 static void
1187 unused_prefix_warnings (pprefix)
1188 struct path_prefix *pprefix;
1189 {
1190 struct prefix_list *pl = pprefix->plist;
1191
1192 while (pl)
1193 {
1194 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
1195 {
1196 error ("file path prefix `%s' never used",
1197 pl->prefix);
1198 /* Prevent duplicate warnings. */
1199 *pl->used_flag_ptr = 1;
1200 }
1201 pl = pl->next;
1202 }
1203 }
1204
1205 /* Get rid of all prefixes built up so far in *PLISTP. */
1206
1207 static void
1208 free_path_prefix (pprefix)
1209 struct path_prefix *pprefix;
1210 {
1211 struct prefix_list *pl = pprefix->plist;
1212 struct prefix_list *temp;
1213
1214 while (pl)
1215 {
1216 temp = pl;
1217 pl = pl->next;
1218 free (temp->prefix);
1219 free ((char *) temp);
1220 }
1221 pprefix->plist = (struct prefix_list *) 0;
1222 }
1223 \f
1224 /* stdin file number. */
1225 #define STDIN_FILE_NO 0
1226
1227 /* stdout file number. */
1228 #define STDOUT_FILE_NO 1
1229
1230 /* value of `pipe': port index for reading. */
1231 #define READ_PORT 0
1232
1233 /* value of `pipe': port index for writing. */
1234 #define WRITE_PORT 1
1235
1236 /* Pipe waiting from last process, to be used as input for the next one.
1237 Value is STDIN_FILE_NO if no pipe is waiting
1238 (i.e. the next command is the first of a group). */
1239
1240 static int last_pipe_input;
1241
1242 /* Fork one piped subcommand. FUNC is the system call to use
1243 (either execv or execvp). ARGV is the arg vector to use.
1244 NOT_LAST is nonzero if this is not the last subcommand
1245 (i.e. its output should be piped to the next one.) */
1246
1247 #ifdef __MSDOS__
1248
1249 /* Declare these to avoid compilation error. They won't be called. */
1250 int execv(const char *a, const char **b){}
1251 int execvp(const char *a, const char **b){}
1252
1253 static int
1254 pexecute (func, program, argv, not_last)
1255 char *program;
1256 int (*func)();
1257 char *argv[];
1258 int not_last;
1259 {
1260 char *scmd;
1261 FILE *argfile;
1262 int i;
1263
1264 scmd = (char *)malloc(strlen(program) + strlen(temp_filename) + 6);
1265 sprintf(scmd, "%s @%s.gp", program, temp_filename);
1266 argfile = fopen(scmd+strlen(program)+2, "w");
1267 if (argfile == 0)
1268 pfatal_with_name(scmd+strlen(program)+2);
1269
1270 for (i=1; argv[i]; i++)
1271 fprintf(argfile, "%s\n", argv[i]);
1272 fclose(argfile);
1273
1274 i = system(scmd);
1275
1276 remove(scmd+strlen(program)+2);
1277 return i << 8;
1278 }
1279
1280 #else /* not __MSDOS__ */
1281
1282 static int
1283 pexecute (func, program, argv, not_last)
1284 char *program;
1285 int (*func)();
1286 char *argv[];
1287 int not_last;
1288 {
1289 int pid;
1290 int pdes[2];
1291 int input_desc = last_pipe_input;
1292 int output_desc = STDOUT_FILE_NO;
1293 int retries, sleep_interval;
1294
1295 /* If this isn't the last process, make a pipe for its output,
1296 and record it as waiting to be the input to the next process. */
1297
1298 if (not_last)
1299 {
1300 if (pipe (pdes) < 0)
1301 pfatal_with_name ("pipe");
1302 output_desc = pdes[WRITE_PORT];
1303 last_pipe_input = pdes[READ_PORT];
1304 }
1305 else
1306 last_pipe_input = STDIN_FILE_NO;
1307
1308 /* Fork a subprocess; wait and retry if it fails. */
1309 sleep_interval = 1;
1310 for (retries = 0; retries < 4; retries++)
1311 {
1312 pid = vfork ();
1313 if (pid >= 0)
1314 break;
1315 sleep (sleep_interval);
1316 sleep_interval *= 2;
1317 }
1318
1319 switch (pid)
1320 {
1321 case -1:
1322 #ifdef vfork
1323 pfatal_with_name ("fork");
1324 #else
1325 pfatal_with_name ("vfork");
1326 #endif
1327 /* NOTREACHED */
1328 return 0;
1329
1330 case 0: /* child */
1331 /* Move the input and output pipes into place, if nec. */
1332 if (input_desc != STDIN_FILE_NO)
1333 {
1334 close (STDIN_FILE_NO);
1335 dup (input_desc);
1336 close (input_desc);
1337 }
1338 if (output_desc != STDOUT_FILE_NO)
1339 {
1340 close (STDOUT_FILE_NO);
1341 dup (output_desc);
1342 close (output_desc);
1343 }
1344
1345 /* Close the parent's descs that aren't wanted here. */
1346 if (last_pipe_input != STDIN_FILE_NO)
1347 close (last_pipe_input);
1348
1349 /* Exec the program. */
1350 (*func) (program, argv);
1351 perror_exec (program);
1352 exit (-1);
1353 /* NOTREACHED */
1354 return 0;
1355
1356 default:
1357 /* In the parent, after forking.
1358 Close the descriptors that we made for this child. */
1359 if (input_desc != STDIN_FILE_NO)
1360 close (input_desc);
1361 if (output_desc != STDOUT_FILE_NO)
1362 close (output_desc);
1363
1364 /* Return child's process number. */
1365 return pid;
1366 }
1367 }
1368
1369 #endif /* not __MSDOS__ */
1370 \f
1371 /* Execute the command specified by the arguments on the current line of spec.
1372 When using pipes, this includes several piped-together commands
1373 with `|' between them.
1374
1375 Return 0 if successful, -1 if failed. */
1376
1377 static int
1378 execute ()
1379 {
1380 int i;
1381 int n_commands; /* # of command. */
1382 char *string;
1383 struct command
1384 {
1385 char *prog; /* program name. */
1386 char **argv; /* vector of args. */
1387 int pid; /* pid of process for this command. */
1388 };
1389
1390 struct command *commands; /* each command buffer with above info. */
1391
1392 /* Count # of piped commands. */
1393 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1394 if (strcmp (argbuf[i], "|") == 0)
1395 n_commands++;
1396
1397 /* Get storage for each command. */
1398 commands
1399 = (struct command *) alloca (n_commands * sizeof (struct command));
1400
1401 /* Split argbuf into its separate piped processes,
1402 and record info about each one.
1403 Also search for the programs that are to be run. */
1404
1405 commands[0].prog = argbuf[0]; /* first command. */
1406 commands[0].argv = &argbuf[0];
1407 string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
1408 if (string)
1409 commands[0].argv[0] = string;
1410
1411 for (n_commands = 1, i = 0; i < argbuf_index; i++)
1412 if (strcmp (argbuf[i], "|") == 0)
1413 { /* each command. */
1414 #ifdef __MSDOS__
1415 fatal ("-pipe not supported under MS-DOS");
1416 #endif
1417 argbuf[i] = 0; /* termination of command args. */
1418 commands[n_commands].prog = argbuf[i + 1];
1419 commands[n_commands].argv = &argbuf[i + 1];
1420 string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
1421 if (string)
1422 commands[n_commands].argv[0] = string;
1423 n_commands++;
1424 }
1425
1426 argbuf[argbuf_index] = 0;
1427
1428 /* If -v, print what we are about to do, and maybe query. */
1429
1430 if (verbose_flag || save_temps_flag)
1431 {
1432 /* Print each piped command as a separate line. */
1433 for (i = 0; i < n_commands ; i++)
1434 {
1435 char **j;
1436
1437 for (j = commands[i].argv; *j; j++)
1438 fprintf (stderr, " %s", *j);
1439
1440 /* Print a pipe symbol after all but the last command. */
1441 if (i + 1 != n_commands)
1442 fprintf (stderr, " |");
1443 fprintf (stderr, "\n");
1444 }
1445 fflush (stderr);
1446 #ifdef DEBUG
1447 fprintf (stderr, "\nGo ahead? (y or n) ");
1448 fflush (stderr);
1449 i = getchar ();
1450 if (i != '\n')
1451 while (getchar () != '\n') ;
1452 if (i != 'y' && i != 'Y')
1453 return 0;
1454 #endif /* DEBUG */
1455 }
1456
1457 /* Run each piped subprocess. */
1458
1459 last_pipe_input = STDIN_FILE_NO;
1460 for (i = 0; i < n_commands; i++)
1461 {
1462 char *string = commands[i].argv[0];
1463
1464 commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
1465 string, commands[i].argv,
1466 i + 1 < n_commands);
1467
1468 if (string != commands[i].prog)
1469 free (string);
1470 }
1471
1472 execution_count++;
1473
1474 /* Wait for all the subprocesses to finish.
1475 We don't care what order they finish in;
1476 we know that N_COMMANDS waits will get them all. */
1477
1478 {
1479 int ret_code = 0;
1480
1481 for (i = 0; i < n_commands; i++)
1482 {
1483 int status;
1484 int pid;
1485 char *prog;
1486
1487 #ifdef __MSDOS__
1488 status = pid = commands[i].pid;
1489 #else
1490 pid = wait (&status);
1491 #endif
1492 if (pid < 0)
1493 abort ();
1494
1495 if (status != 0)
1496 {
1497 int j;
1498 for (j = 0; j < n_commands; j++)
1499 if (commands[j].pid == pid)
1500 prog = commands[j].prog;
1501
1502 if ((status & 0x7F) != 0)
1503 fatal ("Internal compiler error: program %s got fatal signal %d",
1504 prog, (status & 0x7F));
1505 if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
1506 ret_code = -1;
1507 }
1508 }
1509 return ret_code;
1510 }
1511 }
1512 \f
1513 /* Find all the switches given to us
1514 and make a vector describing them.
1515 The elements of the vector are strings, one per switch given.
1516 If a switch uses following arguments, then the `part1' field
1517 is the switch itself and the `args' field
1518 is a null-terminated vector containing the following arguments.
1519 The `valid' field is nonzero if any spec has looked at this switch;
1520 if it remains zero at the end of the run, it must be meaningless. */
1521
1522 struct switchstr
1523 {
1524 char *part1;
1525 char **args;
1526 int valid;
1527 };
1528
1529 static struct switchstr *switches;
1530
1531 static int n_switches;
1532
1533 struct infile
1534 {
1535 char *name;
1536 char *language;
1537 };
1538
1539 /* Also a vector of input files specified. */
1540
1541 static struct infile *infiles;
1542
1543 static int n_infiles;
1544
1545 /* And a vector of corresponding output files is made up later. */
1546
1547 static char **outfiles;
1548
1549 /* Create the vector `switches' and its contents.
1550 Store its length in `n_switches'. */
1551
1552 static void
1553 process_command (argc, argv)
1554 int argc;
1555 char **argv;
1556 {
1557 register int i;
1558 char *temp;
1559 char *spec_lang = 0;
1560 int last_language_n_infiles;
1561
1562 n_switches = 0;
1563 n_infiles = 0;
1564 spec_version = version_string;
1565
1566 /* Set up the default search paths. */
1567
1568 temp = getenv ("GCC_EXEC_PREFIX");
1569 if (temp)
1570 {
1571 add_prefix (&exec_prefix, temp, 0, 0, 0);
1572 add_prefix (&startfile_prefix, temp, 0, 0, 0);
1573 }
1574
1575 /* COMPILER_PATH and LIBRARY_PATH have values
1576 that are lists of directory names with colons. */
1577
1578 temp = getenv ("COMPILER_PATH");
1579 if (temp)
1580 {
1581 char *startp, *endp;
1582 char *nstore = (char *) alloca (strlen (temp) + 3);
1583
1584 startp = endp = temp;
1585 while (1)
1586 {
1587 if ((*endp == ':') || (*endp == 0))
1588 {
1589 strncpy (nstore, startp, endp-startp);
1590 if (endp == startp)
1591 {
1592 strcpy (nstore, "./");
1593 }
1594 else if (endp[-1] != '/')
1595 {
1596 nstore[endp-startp] = '/';
1597 nstore[endp-startp+1] = 0;
1598 }
1599 else
1600 nstore[endp-startp] = 0;
1601 add_prefix (&exec_prefix, nstore, 0, 0, 0);
1602 if (*endp == 0)
1603 break;
1604 endp = startp = endp + 1;
1605 }
1606 else
1607 endp++;
1608 }
1609 }
1610
1611 temp = getenv ("LIBRARY_PATH");
1612 if (temp)
1613 {
1614 char *startp, *endp;
1615 char *nstore = (char *) alloca (strlen (temp) + 3);
1616
1617 startp = endp = temp;
1618 while (1)
1619 {
1620 if ((*endp == ':') || (*endp == 0))
1621 {
1622 strncpy (nstore, startp, endp-startp);
1623 if (endp == startp)
1624 {
1625 strcpy (nstore, "./");
1626 }
1627 else if (endp[-1] != '/')
1628 {
1629 nstore[endp-startp] = '/';
1630 nstore[endp-startp+1] = 0;
1631 }
1632 else
1633 nstore[endp-startp] = 0;
1634 add_prefix (&startfile_prefix, nstore, 0, 0, 0);
1635 /* Make separate list of dirs that came from LIBRARY_PATH. */
1636 add_prefix (&library_prefix, nstore, 0, 0, 0);
1637 if (*endp == 0)
1638 break;
1639 endp = startp = endp + 1;
1640 }
1641 else
1642 endp++;
1643 }
1644 }
1645
1646 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
1647 temp = getenv ("LPATH");
1648 if (temp)
1649 {
1650 char *startp, *endp;
1651 char *nstore = (char *) alloca (strlen (temp) + 3);
1652
1653 startp = endp = temp;
1654 while (1)
1655 {
1656 if ((*endp == ':') || (*endp == 0))
1657 {
1658 strncpy (nstore, startp, endp-startp);
1659 if (endp == startp)
1660 {
1661 strcpy (nstore, "./");
1662 }
1663 else if (endp[-1] != '/')
1664 {
1665 nstore[endp-startp] = '/';
1666 nstore[endp-startp+1] = 0;
1667 }
1668 else
1669 nstore[endp-startp] = 0;
1670 add_prefix (&startfile_prefix, nstore, 0, 0, 0);
1671 /* Make separate list of dirs that came from LIBRARY_PATH. */
1672 add_prefix (&library_prefix, nstore, 0, 0, 0);
1673 if (*endp == 0)
1674 break;
1675 endp = startp = endp + 1;
1676 }
1677 else
1678 endp++;
1679 }
1680 }
1681
1682 /* Scan argv twice. Here, the first time, just count how many switches
1683 there will be in their vector, and how many input files in theirs.
1684 Here we also parse the switches that cc itself uses (e.g. -v). */
1685
1686 for (i = 1; i < argc; i++)
1687 {
1688 if (! strcmp (argv[i], "-dumpspecs"))
1689 {
1690 printf ("*asm:\n%s\n\n", asm_spec);
1691 printf ("*asm_final:\n%s\n\n", asm_final_spec);
1692 printf ("*cpp:\n%s\n\n", cpp_spec);
1693 printf ("*cc1:\n%s\n\n", cc1_spec);
1694 printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
1695 printf ("*endfile:\n%s\n\n", endfile_spec);
1696 printf ("*link:\n%s\n\n", link_spec);
1697 printf ("*lib:\n%s\n\n", lib_spec);
1698 printf ("*startfile:\n%s\n\n", startfile_spec);
1699 printf ("*switches_need_spaces:\n%s\n\n", switches_need_spaces);
1700 printf ("*signed_char:\n%s\n\n", signed_char_spec);
1701 printf ("*predefines:\n%s\n\n", cpp_predefines);
1702 printf ("*cross_compile:\n%d\n\n", cross_compile);
1703
1704 exit (0);
1705 }
1706 else if (! strcmp (argv[i], "-dumpversion"))
1707 {
1708 printf ("%s\n", version_string);
1709 exit (0);
1710 }
1711 else if (! strcmp (argv[i], "-Xlinker"))
1712 {
1713 /* Pass the argument of this option to the linker when we link. */
1714
1715 if (i + 1 == argc)
1716 fatal ("argument to `-Xlinker' is missing");
1717
1718 n_linker_options++;
1719 if (!linker_options)
1720 linker_options
1721 = (char **) xmalloc (n_linker_options * sizeof (char **));
1722 else
1723 linker_options
1724 = (char **) xrealloc (linker_options,
1725 n_linker_options * sizeof (char **));
1726
1727 linker_options[n_linker_options - 1] = argv[++i];
1728 }
1729 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1730 {
1731 register char *p = &argv[i][1];
1732 register int c = *p;
1733
1734 switch (c)
1735 {
1736 case 'b':
1737 if (p[1] == 0 && i + 1 == argc)
1738 fatal ("argument to `-b' is missing");
1739 if (p[1] == 0)
1740 spec_machine = argv[++i];
1741 else
1742 spec_machine = p + 1;
1743 break;
1744
1745 case 'B':
1746 {
1747 int *temp = (int *) xmalloc (sizeof (int));
1748 char *value;
1749 if (p[1] == 0 && i + 1 == argc)
1750 fatal ("argument to `-B' is missing");
1751 if (p[1] == 0)
1752 value = argv[++i];
1753 else
1754 value = p + 1;
1755 add_prefix (&exec_prefix, value, 1, 0, temp);
1756 add_prefix (&startfile_prefix, value, 1, 0, temp);
1757 }
1758 break;
1759
1760 case 'v': /* Print our subcommands and print versions. */
1761 verbose_flag++;
1762 n_switches++;
1763 break;
1764
1765 case 'V':
1766 if (p[1] == 0 && i + 1 == argc)
1767 fatal ("argument to `-V' is missing");
1768 if (p[1] == 0)
1769 spec_version = argv[++i];
1770 else
1771 spec_version = p + 1;
1772 break;
1773
1774 case 's':
1775 if (!strcmp (p, "save-temps"))
1776 {
1777 save_temps_flag = 1;
1778 break;
1779 }
1780 default:
1781 n_switches++;
1782
1783 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1784 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1785 else if (WORD_SWITCH_TAKES_ARG (p))
1786 i += WORD_SWITCH_TAKES_ARG (p);
1787 }
1788 }
1789 else
1790 n_infiles++;
1791 }
1792
1793 /* Set up the search paths before we go looking for config files. */
1794
1795 /* These come before the md prefixes so that we will find gcc's subcommands
1796 (such as cpp) rather than those of the host system. */
1797 add_prefix (&exec_prefix, standard_exec_prefix, 0, 1, 0);
1798 add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 1, 0);
1799
1800 add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, 0);
1801 add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, 0);
1802
1803 /* More prefixes are enabled in main, after we read the specs file
1804 and determine whether this is cross-compilation or not. */
1805
1806
1807 /* Then create the space for the vectors and scan again. */
1808
1809 switches = ((struct switchstr *)
1810 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
1811 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
1812 n_switches = 0;
1813 n_infiles = 0;
1814 last_language_n_infiles = -1;
1815
1816 /* This, time, copy the text of each switch and store a pointer
1817 to the copy in the vector of switches.
1818 Store all the infiles in their vector. */
1819
1820 for (i = 1; i < argc; i++)
1821 {
1822 if (!strcmp (argv[i], "-Xlinker"))
1823 i++;
1824 else if (argv[i][0] == '-' && argv[i][1] != 0 && argv[i][1] != 'l')
1825 {
1826 register char *p = &argv[i][1];
1827 register int c = *p;
1828
1829 if (c == 'B' || c == 'b' || c == 'V')
1830 {
1831 /* Skip a separate arg, if any. */
1832 if (p[1] == 0)
1833 i++;
1834 continue;
1835 }
1836 if (c == 'x')
1837 {
1838 if (p[1] == 0 && i + 1 == argc)
1839 fatal ("argument to `-x' is missing");
1840 if (p[1] == 0)
1841 spec_lang = argv[++i];
1842 else
1843 spec_lang = p + 1;
1844 if (! strcmp (spec_lang, "none"))
1845 /* Suppress the warning if -xnone comes after the last input file,
1846 because alternate command interfaces like g++ might find it
1847 useful to place -xnone after each input file. */
1848 spec_lang = 0;
1849 else
1850 last_language_n_infiles = n_infiles;
1851 continue;
1852 }
1853 switches[n_switches].part1 = p;
1854 /* Deal with option arguments in separate argv elements. */
1855 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
1856 || WORD_SWITCH_TAKES_ARG (p)) {
1857 int j = 0;
1858 int n_args = WORD_SWITCH_TAKES_ARG (p);
1859
1860 if (n_args == 0) {
1861 /* Count only the option arguments in separate argv elements. */
1862 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
1863 }
1864 switches[n_switches].args
1865 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
1866 while (j < n_args)
1867 switches[n_switches].args[j++] = argv[++i];
1868 /* Null-terminate the vector. */
1869 switches[n_switches].args[j] = 0;
1870 } else if (*switches_need_spaces != 0 && (c == 'o' || c == 'L')) {
1871 /* On some systems, ld cannot handle -o or -L without space.
1872 So split the -o or -L from its argument. */
1873 switches[n_switches].part1 = (c == 'o' ? "o" : "L");
1874 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
1875 switches[n_switches].args[0] = xmalloc (strlen (p));
1876 strcpy (switches[n_switches].args[0], &p[1]);
1877 switches[n_switches].args[1] = 0;
1878 } else
1879 switches[n_switches].args = 0;
1880 switches[n_switches].valid = 0;
1881 /* This is always valid, since gcc.c itself understands it. */
1882 if (!strcmp (p, "save-temps"))
1883 switches[n_switches].valid = 1;
1884 n_switches++;
1885 }
1886 else
1887 {
1888 infiles[n_infiles].language = spec_lang;
1889 infiles[n_infiles++].name = argv[i];
1890 }
1891 }
1892
1893 if (n_infiles == last_language_n_infiles)
1894 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
1895
1896 switches[n_switches].part1 = 0;
1897 infiles[n_infiles].name = 0;
1898 }
1899 \f
1900 /* Process a spec string, accumulating and running commands. */
1901
1902 /* These variables describe the input file name.
1903 input_file_number is the index on outfiles of this file,
1904 so that the output file name can be stored for later use by %o.
1905 input_basename is the start of the part of the input file
1906 sans all directory names, and basename_length is the number
1907 of characters starting there excluding the suffix .c or whatever. */
1908
1909 static char *input_filename;
1910 static int input_file_number;
1911 static int input_filename_length;
1912 static int basename_length;
1913 static char *input_basename;
1914 static char *input_suffix;
1915
1916 /* These are variables used within do_spec and do_spec_1. */
1917
1918 /* Nonzero if an arg has been started and not yet terminated
1919 (with space, tab or newline). */
1920 static int arg_going;
1921
1922 /* Nonzero means %d or %g has been seen; the next arg to be terminated
1923 is a temporary file name. */
1924 static int delete_this_arg;
1925
1926 /* Nonzero means %w has been seen; the next arg to be terminated
1927 is the output file name of this compilation. */
1928 static int this_is_output_file;
1929
1930 /* Nonzero means %s has been seen; the next arg to be terminated
1931 is the name of a library file and we should try the standard
1932 search dirs for it. */
1933 static int this_is_library_file;
1934
1935 /* Process the spec SPEC and run the commands specified therein.
1936 Returns 0 if the spec is successfully processed; -1 if failed. */
1937
1938 static int
1939 do_spec (spec)
1940 char *spec;
1941 {
1942 int value;
1943
1944 clear_args ();
1945 arg_going = 0;
1946 delete_this_arg = 0;
1947 this_is_output_file = 0;
1948 this_is_library_file = 0;
1949
1950 value = do_spec_1 (spec, 0, NULL);
1951
1952 /* Force out any unfinished command.
1953 If -pipe, this forces out the last command if it ended in `|'. */
1954 if (value == 0)
1955 {
1956 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
1957 argbuf_index--;
1958
1959 if (argbuf_index > 0)
1960 value = execute ();
1961 }
1962
1963 return value;
1964 }
1965
1966 /* Process the sub-spec SPEC as a portion of a larger spec.
1967 This is like processing a whole spec except that we do
1968 not initialize at the beginning and we do not supply a
1969 newline by default at the end.
1970 INSWITCH nonzero means don't process %-sequences in SPEC;
1971 in this case, % is treated as an ordinary character.
1972 This is used while substituting switches.
1973 INSWITCH nonzero also causes SPC not to terminate an argument.
1974
1975 Value is zero unless a line was finished
1976 and the command on that line reported an error. */
1977
1978 static int
1979 do_spec_1 (spec, inswitch, soft_matched_part)
1980 char *spec;
1981 int inswitch;
1982 char *soft_matched_part;
1983 {
1984 register char *p = spec;
1985 register int c;
1986 int i;
1987 char *string;
1988
1989 while (c = *p++)
1990 /* If substituting a switch, treat all chars like letters.
1991 Otherwise, NL, SPC, TAB and % are special. */
1992 switch (inswitch ? 'a' : c)
1993 {
1994 case '\n':
1995 /* End of line: finish any pending argument,
1996 then run the pending command if one has been started. */
1997 if (arg_going)
1998 {
1999 obstack_1grow (&obstack, 0);
2000 string = obstack_finish (&obstack);
2001 if (this_is_library_file)
2002 string = find_file (string);
2003 store_arg (string, delete_this_arg, this_is_output_file);
2004 if (this_is_output_file)
2005 outfiles[input_file_number] = string;
2006 }
2007 arg_going = 0;
2008
2009 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
2010 {
2011 int i;
2012 for (i = 0; i < n_switches; i++)
2013 if (!strcmp (switches[i].part1, "pipe"))
2014 break;
2015
2016 /* A `|' before the newline means use a pipe here,
2017 but only if -pipe was specified.
2018 Otherwise, execute now and don't pass the `|' as an arg. */
2019 if (i < n_switches)
2020 {
2021 switches[i].valid = 1;
2022 break;
2023 }
2024 else
2025 argbuf_index--;
2026 }
2027
2028 if (argbuf_index > 0)
2029 {
2030 int value = execute ();
2031 if (value)
2032 return value;
2033 }
2034 /* Reinitialize for a new command, and for a new argument. */
2035 clear_args ();
2036 arg_going = 0;
2037 delete_this_arg = 0;
2038 this_is_output_file = 0;
2039 this_is_library_file = 0;
2040 break;
2041
2042 case '|':
2043 /* End any pending argument. */
2044 if (arg_going)
2045 {
2046 obstack_1grow (&obstack, 0);
2047 string = obstack_finish (&obstack);
2048 if (this_is_library_file)
2049 string = find_file (string);
2050 store_arg (string, delete_this_arg, this_is_output_file);
2051 if (this_is_output_file)
2052 outfiles[input_file_number] = string;
2053 }
2054
2055 /* Use pipe */
2056 obstack_1grow (&obstack, c);
2057 arg_going = 1;
2058 break;
2059
2060 case '\t':
2061 case ' ':
2062 /* Space or tab ends an argument if one is pending. */
2063 if (arg_going)
2064 {
2065 obstack_1grow (&obstack, 0);
2066 string = obstack_finish (&obstack);
2067 if (this_is_library_file)
2068 string = find_file (string);
2069 store_arg (string, delete_this_arg, this_is_output_file);
2070 if (this_is_output_file)
2071 outfiles[input_file_number] = string;
2072 }
2073 /* Reinitialize for a new argument. */
2074 arg_going = 0;
2075 delete_this_arg = 0;
2076 this_is_output_file = 0;
2077 this_is_library_file = 0;
2078 break;
2079
2080 case '%':
2081 switch (c = *p++)
2082 {
2083 case 0:
2084 fatal ("Invalid specification! Bug in cc.");
2085
2086 case 'b':
2087 obstack_grow (&obstack, input_basename, basename_length);
2088 arg_going = 1;
2089 break;
2090
2091 case 'd':
2092 delete_this_arg = 2;
2093 break;
2094
2095 /* Dump out the directories specified with LIBRARY_PATH,
2096 followed by the absolute directories
2097 that we search for startfiles. */
2098 case 'D':
2099 for (i = 0; i < 2; i++)
2100 {
2101 struct prefix_list *pl
2102 = (i == 0 ? library_prefix.plist : startfile_prefix.plist);
2103 for (; pl; pl = pl->next)
2104 {
2105 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
2106 /* Used on systems which record the specified -L dirs
2107 and use them to search for dynamic linking. */
2108 /* Relative directories always come from -B,
2109 and it is better not to use them for searching
2110 at run time. In particular, stage1 loses */
2111 if (pl->prefix[0] != '/')
2112 continue;
2113 #endif
2114 if (machine_suffix)
2115 {
2116 if (is_linker_dir (pl->prefix, machine_suffix))
2117 {
2118 do_spec_1 ("-L", 0, 0);
2119 #ifdef SPACE_AFTER_L_OPTION
2120 do_spec_1 (" ", 0, 0);
2121 #endif
2122 do_spec_1 (pl->prefix, 1, 0);
2123 do_spec_1 (machine_suffix, 1, 0);
2124 /* Make this a separate argument. */
2125 do_spec_1 (" ", 0, 0);
2126 }
2127 }
2128 if (!pl->require_machine_suffix)
2129 {
2130 if (is_linker_dir (pl->prefix, ""))
2131 {
2132 do_spec_1 ("-L", 0, 0);
2133 #ifdef SPACE_AFTER_L_OPTION
2134 do_spec_1 (" ", 0, 0);
2135 #endif
2136 do_spec_1 (pl->prefix, 1, 0);
2137 /* Make this a separate argument. */
2138 do_spec_1 (" ", 0, 0);
2139 }
2140 }
2141 }
2142 }
2143 break;
2144
2145 case 'e':
2146 /* {...:%efoo} means report an error with `foo' as error message
2147 and don't execute any more commands for this file. */
2148 {
2149 char *q = p;
2150 char *buf;
2151 while (*p != 0 && *p != '\n') p++;
2152 buf = (char *) alloca (p - q + 1);
2153 strncpy (buf, q, p - q);
2154 buf[p - q] = 0;
2155 error ("%s", buf);
2156 return -1;
2157 }
2158 break;
2159
2160 case 'g':
2161 if (save_temps_flag)
2162 obstack_grow (&obstack, input_basename, basename_length);
2163 else
2164 {
2165 obstack_grow (&obstack, temp_filename, temp_filename_length);
2166 delete_this_arg = 1;
2167 }
2168 arg_going = 1;
2169 break;
2170
2171 case 'i':
2172 obstack_grow (&obstack, input_filename, input_filename_length);
2173 arg_going = 1;
2174 break;
2175
2176 case 'o':
2177 {
2178 register int f;
2179 for (f = 0; f < n_infiles; f++)
2180 store_arg (outfiles[f], 0, 0);
2181 }
2182 break;
2183
2184 case 's':
2185 this_is_library_file = 1;
2186 break;
2187
2188 case 'w':
2189 this_is_output_file = 1;
2190 break;
2191
2192 case 'W':
2193 {
2194 int index = argbuf_index;
2195 /* Handle the {...} following the %W. */
2196 if (*p != '{')
2197 abort ();
2198 p = handle_braces (p + 1);
2199 if (p == 0)
2200 return -1;
2201 /* If any args were output, mark the last one for deletion
2202 on failure. */
2203 if (argbuf_index != index)
2204 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
2205 break;
2206 }
2207
2208 /* %x{OPTION} records OPTION for %X to output. */
2209 case 'x':
2210 {
2211 char *p1 = p;
2212 char *string;
2213
2214 /* Skip past the option value and make a copy. */
2215 if (*p != '{')
2216 abort ();
2217 while (*p++ != '}')
2218 ;
2219 string = save_string (p1 + 1, p - p1 - 2);
2220
2221 /* See if we already recorded this option. */
2222 for (i = 0; i < n_linker_options; i++)
2223 if (! strcmp (string, linker_options[i]))
2224 {
2225 free (string);
2226 return 0;
2227 }
2228
2229 /* This option is new; add it. */
2230 n_linker_options++;
2231 if (!linker_options)
2232 linker_options
2233 = (char **) xmalloc (n_linker_options * sizeof (char **));
2234 else
2235 linker_options
2236 = (char **) xrealloc (linker_options,
2237 n_linker_options * sizeof (char **));
2238
2239 linker_options[n_linker_options - 1] = string;
2240 }
2241 break;
2242
2243 /* Dump out the options accumulated previously using %x. */
2244 case 'X':
2245 for (i = 0; i < n_linker_options; i++)
2246 {
2247 do_spec_1 (linker_options[i], 1, NULL);
2248 /* Make each accumulated option a separate argument. */
2249 do_spec_1 (" ", 0, NULL);
2250 }
2251 break;
2252
2253 /* Here are digits and numbers that just process
2254 a certain constant string as a spec. */
2255
2256 case '1':
2257 do_spec_1 (cc1_spec, 0, NULL);
2258 break;
2259
2260 case '2':
2261 do_spec_1 (cc1plus_spec, 0, NULL);
2262 break;
2263
2264 case 'a':
2265 do_spec_1 (asm_spec, 0, NULL);
2266 break;
2267
2268 case 'A':
2269 do_spec_1 (asm_final_spec, 0, NULL);
2270 break;
2271
2272 case 'c':
2273 do_spec_1 (signed_char_spec, 0, NULL);
2274 break;
2275
2276 case 'C':
2277 do_spec_1 (cpp_spec, 0, NULL);
2278 break;
2279
2280 case 'E':
2281 do_spec_1 (endfile_spec, 0, NULL);
2282 break;
2283
2284 case 'l':
2285 do_spec_1 (link_spec, 0, NULL);
2286 break;
2287
2288 case 'L':
2289 do_spec_1 (lib_spec, 0, NULL);
2290 break;
2291
2292 case 'p':
2293 {
2294 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
2295 char *buf = x;
2296 char *y;
2297
2298 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
2299 y = cpp_predefines;
2300 while (*y != 0)
2301 {
2302 if (! strncmp (y, "-D", 2))
2303 /* Copy the whole option. */
2304 while (*y && *y != ' ' && *y != '\t')
2305 *x++ = *y++;
2306 else if (*y == ' ' || *y == '\t')
2307 /* Copy whitespace to the result. */
2308 *x++ = *y++;
2309 /* Don't copy other options. */
2310 else
2311 y++;
2312 }
2313
2314 *x = 0;
2315
2316 do_spec_1 (buf, 0, NULL);
2317 }
2318 break;
2319
2320 case 'P':
2321 {
2322 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
2323 char *buf = x;
2324 char *y;
2325
2326 /* Copy all of CPP_PREDEFINES into BUF,
2327 but put __ after every -D and at the end of each arg. */
2328 y = cpp_predefines;
2329 while (*y != 0)
2330 {
2331 if (! strncmp (y, "-D", 2))
2332 {
2333 int flag = 0;
2334
2335 *x++ = *y++;
2336 *x++ = *y++;
2337
2338 if (strncmp (y, "__", 2))
2339 {
2340 /* Stick __ at front of macro name. */
2341 *x++ = '_';
2342 *x++ = '_';
2343 /* Arrange to stick __ at the end as well. */
2344 flag = 1;
2345 }
2346
2347 /* Copy the macro name. */
2348 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2349 *x++ = *y++;
2350
2351 if (flag)
2352 {
2353 *x++ = '_';
2354 *x++ = '_';
2355 }
2356
2357 /* Copy the value given, if any. */
2358 while (*y && *y != ' ' && *y != '\t')
2359 *x++ = *y++;
2360 }
2361 else if (*y == ' ' || *y == '\t')
2362 /* Copy whitespace to the result. */
2363 *x++ = *y++;
2364 /* Don't copy -A options */
2365 else
2366 y++;
2367 }
2368 *x++ = ' ';
2369
2370 /* Copy all of CPP_PREDEFINES into BUF,
2371 but put __ after every -D. */
2372 y = cpp_predefines;
2373 while (*y != 0)
2374 {
2375 if (! strncmp (y, "-D", 2))
2376 {
2377 *x++ = *y++;
2378 *x++ = *y++;
2379
2380 if (strncmp (y, "__", 2))
2381 {
2382 /* Stick __ at front of macro name. */
2383 *x++ = '_';
2384 *x++ = '_';
2385 }
2386
2387 /* Copy the macro name. */
2388 while (*y && *y != '=' && *y != ' ' && *y != '\t')
2389 *x++ = *y++;
2390
2391 /* Copy the value given, if any. */
2392 while (*y && *y != ' ' && *y != '\t')
2393 *x++ = *y++;
2394 }
2395 else if (*y == ' ' || *y == '\t')
2396 /* Copy whitespace to the result. */
2397 *x++ = *y++;
2398 /* Don't copy -A options */
2399 else
2400 y++;
2401 }
2402 *x++ = ' ';
2403
2404 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
2405 y = cpp_predefines;
2406 while (*y != 0)
2407 {
2408 if (! strncmp (y, "-A", 2))
2409 /* Copy the whole option. */
2410 while (*y && *y != ' ' && *y != '\t')
2411 *x++ = *y++;
2412 else if (*y == ' ' || *y == '\t')
2413 /* Copy whitespace to the result. */
2414 *x++ = *y++;
2415 /* Don't copy other options. */
2416 else
2417 y++;
2418 }
2419
2420 *x = 0;
2421
2422 do_spec_1 (buf, 0, NULL);
2423 }
2424 break;
2425
2426 case 'S':
2427 do_spec_1 (startfile_spec, 0, NULL);
2428 break;
2429
2430 /* Here we define characters other than letters and digits. */
2431
2432 case '{':
2433 p = handle_braces (p);
2434 if (p == 0)
2435 return -1;
2436 break;
2437
2438 case '%':
2439 obstack_1grow (&obstack, '%');
2440 break;
2441
2442 case '*':
2443 do_spec_1 (soft_matched_part, 1, NULL);
2444 do_spec_1 (" ", 0, NULL);
2445 break;
2446
2447 /* Process a string found as the value of a spec given by name.
2448 This feature allows individual machine descriptions
2449 to add and use their own specs.
2450 %[...] modifies -D options the way %P does;
2451 %(...) uses the spec unmodified. */
2452 case '(':
2453 case '[':
2454 {
2455 char *name = p;
2456 struct spec_list *sl;
2457 int len;
2458
2459 /* The string after the S/P is the name of a spec that is to be
2460 processed. */
2461 while (*p && *p != ')' && *p != ']')
2462 p++;
2463
2464 /* See if it's in the list */
2465 for (len = p - name, sl = specs; sl; sl = sl->next)
2466 if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
2467 {
2468 name = sl->spec;
2469 break;
2470 }
2471
2472 if (sl)
2473 {
2474 if (c == '(')
2475 do_spec_1 (name, 0, NULL);
2476 else
2477 {
2478 char *x = (char *) alloca (strlen (name) * 2 + 1);
2479 char *buf = x;
2480 char *y = name;
2481
2482 /* Copy all of NAME into BUF, but put __ after
2483 every -D and at the end of each arg, */
2484 while (1)
2485 {
2486 if (! strncmp (y, "-D", 2))
2487 {
2488 *x++ = '-';
2489 *x++ = 'D';
2490 *x++ = '_';
2491 *x++ = '_';
2492 y += 2;
2493 }
2494 else if (*y == ' ' || *y == 0)
2495 {
2496 *x++ = '_';
2497 *x++ = '_';
2498 if (*y == 0)
2499 break;
2500 else
2501 *x++ = *y++;
2502 }
2503 else
2504 *x++ = *y++;
2505 }
2506 *x = 0;
2507
2508 do_spec_1 (buf, 0, NULL);
2509 }
2510 }
2511 }
2512 break;
2513
2514 default:
2515 abort ();
2516 }
2517 break;
2518
2519 case '\\':
2520 /* Backslash: treat next character as ordinary. */
2521 c = *p++;
2522
2523 /* fall through */
2524 default:
2525 /* Ordinary character: put it into the current argument. */
2526 obstack_1grow (&obstack, c);
2527 arg_going = 1;
2528 }
2529
2530 return 0; /* End of string */
2531 }
2532
2533 /* Return 0 if we call do_spec_1 and that returns -1. */
2534
2535 static char *
2536 handle_braces (p)
2537 register char *p;
2538 {
2539 register char *q;
2540 char *filter;
2541 int pipe = 0;
2542 int negate = 0;
2543 int suffix = 0;
2544
2545 if (*p == '|')
2546 /* A `|' after the open-brace means,
2547 if the test fails, output a single minus sign rather than nothing.
2548 This is used in %{|!pipe:...}. */
2549 pipe = 1, ++p;
2550
2551 if (*p == '!')
2552 /* A `!' after the open-brace negates the condition:
2553 succeed if the specified switch is not present. */
2554 negate = 1, ++p;
2555
2556 if (*p == '.')
2557 /* A `.' after the open-brace means test against the current suffix. */
2558 {
2559 if (pipe)
2560 abort ();
2561
2562 suffix = 1;
2563 ++p;
2564 }
2565
2566 filter = p;
2567 while (*p != ':' && *p != '}') p++;
2568 if (*p != '}')
2569 {
2570 register int count = 1;
2571 q = p + 1;
2572 while (count > 0)
2573 {
2574 if (*q == '{')
2575 count++;
2576 else if (*q == '}')
2577 count--;
2578 else if (*q == 0)
2579 abort ();
2580 q++;
2581 }
2582 }
2583 else
2584 q = p + 1;
2585
2586 if (suffix)
2587 {
2588 int found = (input_suffix != 0
2589 && strlen (input_suffix) == p - filter
2590 && strncmp (input_suffix, filter, p - filter) == 0);
2591
2592 if (p[0] == '}')
2593 abort ();
2594
2595 if (negate != found
2596 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL) < 0)
2597 return 0;
2598
2599 return q;
2600 }
2601 else if (p[-1] == '*' && p[0] == '}')
2602 {
2603 /* Substitute all matching switches as separate args. */
2604 register int i;
2605 --p;
2606 for (i = 0; i < n_switches; i++)
2607 if (!strncmp (switches[i].part1, filter, p - filter))
2608 give_switch (i, 0);
2609 }
2610 else
2611 {
2612 /* Test for presence of the specified switch. */
2613 register int i;
2614 int present = 0;
2615
2616 /* If name specified ends in *, as in {x*:...},
2617 check for %* and handle that case. */
2618 if (p[-1] == '*' && !negate)
2619 {
2620 int substitution;
2621 char *r = p;
2622
2623 /* First see whether we have %*. */
2624 substitution = 0;
2625 while (*r && *r == '}')
2626 {
2627 if (*r == '%' && r[1] == '*')
2628 substitution = 1;
2629 r++;
2630 }
2631 /* If we do, handle that case. */
2632 if (substitution)
2633 {
2634 /* Substitute all matching switches as separate args.
2635 But do this by substituting for %*
2636 in the text that follows the colon. */
2637
2638 unsigned hard_match_len = p - filter - 1;
2639 char *string = save_string (p + 1, q - p - 2);
2640
2641 for (i = 0; i < n_switches; i++)
2642 if (!strncmp (switches[i].part1, filter, hard_match_len))
2643 {
2644 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
2645 /* Pass any arguments this switch has. */
2646 give_switch (i, 1);
2647 }
2648
2649 return q;
2650 }
2651 }
2652
2653 /* If name specified ends in *, as in {x*:...},
2654 check for presence of any switch name starting with x. */
2655 if (p[-1] == '*')
2656 {
2657 for (i = 0; i < n_switches; i++)
2658 {
2659 unsigned hard_match_len = p - filter - 1;
2660
2661 if (!strncmp (switches[i].part1, filter, hard_match_len))
2662 {
2663 switches[i].valid = 1;
2664 present = 1;
2665 }
2666 }
2667 }
2668 /* Otherwise, check for presence of exact name specified. */
2669 else
2670 {
2671 for (i = 0; i < n_switches; i++)
2672 {
2673 if (!strncmp (switches[i].part1, filter, p - filter)
2674 && switches[i].part1[p - filter] == 0)
2675 {
2676 switches[i].valid = 1;
2677 present = 1;
2678 break;
2679 }
2680 }
2681 }
2682
2683 /* If it is as desired (present for %{s...}, absent for %{-s...})
2684 then substitute either the switch or the specified
2685 conditional text. */
2686 if (present != negate)
2687 {
2688 if (*p == '}')
2689 {
2690 give_switch (i, 0);
2691 }
2692 else
2693 {
2694 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL) < 0)
2695 return 0;
2696 }
2697 }
2698 else if (pipe)
2699 {
2700 /* Here if a %{|...} conditional fails: output a minus sign,
2701 which means "standard output" or "standard input". */
2702 do_spec_1 ("-", 0, NULL);
2703 }
2704 }
2705
2706 return q;
2707 }
2708
2709 /* Pass a switch to the current accumulating command
2710 in the same form that we received it.
2711 SWITCHNUM identifies the switch; it is an index into
2712 the vector of switches gcc received, which is `switches'.
2713 This cannot fail since it never finishes a command line.
2714
2715 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument. */
2716
2717 static void
2718 give_switch (switchnum, omit_first_word)
2719 int switchnum;
2720 int omit_first_word;
2721 {
2722 if (!omit_first_word)
2723 {
2724 do_spec_1 ("-", 0, NULL);
2725 do_spec_1 (switches[switchnum].part1, 1, NULL);
2726 }
2727 do_spec_1 (" ", 0, NULL);
2728 if (switches[switchnum].args != 0)
2729 {
2730 char **p;
2731 for (p = switches[switchnum].args; *p; p++)
2732 {
2733 do_spec_1 (*p, 1, NULL);
2734 do_spec_1 (" ", 0, NULL);
2735 }
2736 }
2737 switches[switchnum].valid = 1;
2738 }
2739 \f
2740 /* Search for a file named NAME trying various prefixes including the
2741 user's -B prefix and some standard ones.
2742 Return the absolute file name found. If nothing is found, return NAME. */
2743
2744 static char *
2745 find_file (name)
2746 char *name;
2747 {
2748 char *newname;
2749
2750 newname = find_a_file (&startfile_prefix, name, R_OK);
2751 return newname ? newname : name;
2752 }
2753
2754 /* Determine whether a -L option is relevant. Not required for certain
2755 fixed names and for directories that don't exist. */
2756
2757 static int
2758 is_linker_dir (path1, path2)
2759 char *path1;
2760 char *path2;
2761 {
2762 int len1 = strlen (path1);
2763 int len2 = strlen (path2);
2764 char *path = (char *) alloca (3 + len1 + len2);
2765 char *cp;
2766 struct stat st;
2767
2768 /* Construct the path from the two parts. Ensure the string ends with "/.".
2769 The resulting path will be a directory even if the given path is a
2770 symbolic link. */
2771 bcopy (path1, path, len1);
2772 bcopy (path2, path + len1, len2);
2773 cp = path + len1 + len2;
2774 if (cp[-1] != '/')
2775 *cp++ = '/';
2776 *cp++ = '.';
2777 *cp = '\0';
2778
2779 /* Exclude directories that the linker is known to search. */
2780 if ((cp - path == 6 && strcmp (path, "/lib/.") == 0)
2781 || (cp - path == 10 && strcmp (path, "/usr/lib/.") == 0))
2782 return 0;
2783
2784 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
2785 }
2786 \f
2787 /* On fatal signals, delete all the temporary files. */
2788
2789 static void
2790 fatal_error (signum)
2791 int signum;
2792 {
2793 signal (signum, SIG_DFL);
2794 delete_failure_queue ();
2795 delete_temp_files ();
2796 /* Get the same signal again, this time not handled,
2797 so its normal effect occurs. */
2798 kill (getpid (), signum);
2799 }
2800
2801 int
2802 main (argc, argv)
2803 int argc;
2804 char **argv;
2805 {
2806 register int i;
2807 int value;
2808 int error_count = 0;
2809 int linker_was_run = 0;
2810 char *explicit_link_files;
2811 char *specs_file;
2812
2813 programname = argv[0];
2814
2815 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
2816 signal (SIGINT, fatal_error);
2817 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
2818 signal (SIGHUP, fatal_error);
2819 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
2820 signal (SIGTERM, fatal_error);
2821 #ifdef SIGPIPE
2822 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
2823 signal (SIGPIPE, fatal_error);
2824 #endif
2825
2826 argbuf_length = 10;
2827 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
2828
2829 obstack_init (&obstack);
2830
2831 /* Choose directory for temp files. */
2832
2833 choose_temp_base ();
2834
2835 /* Make a table of what switches there are (switches, n_switches).
2836 Make a table of specified input files (infiles, n_infiles).
2837 Decode switches that are handled locally. */
2838
2839 process_command (argc, argv);
2840
2841 /* Initialize the vector of specs to just the default.
2842 This means one element containing 0s, as a terminator. */
2843
2844 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
2845 bcopy (default_compilers, compilers, sizeof default_compilers);
2846 n_compilers = n_default_compilers;
2847
2848 /* Read specs from a file if there is one. */
2849
2850 machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
2851
2852 specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
2853 /* Read the specs file unless it is a default one. */
2854 if (specs_file != 0 && strcmp (specs_file, "specs"))
2855 read_specs (specs_file);
2856
2857 /* If not cross-compiling, look for startfiles in the standard places. */
2858 /* The fact that these are done here, after reading the specs file,
2859 means that it cannot be found in these directories.
2860 But that's okay. It should never be there anyway. */
2861 if (!cross_compile)
2862 {
2863 #ifdef MD_EXEC_PREFIX
2864 add_prefix (&exec_prefix, md_exec_prefix, 0, 0, 0);
2865 add_prefix (&startfile_prefix, md_exec_prefix, 0, 0, 0);
2866 #endif
2867
2868 #ifdef MD_STARTFILE_PREFIX
2869 add_prefix (&startfile_prefix, md_startfile_prefix, 0, 0, 0);
2870 #endif
2871
2872 add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0, 0);
2873 add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0, 0);
2874 add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0, 0);
2875 #if 0 /* Can cause surprises, and one can use -B./ instead. */
2876 add_prefix (&startfile_prefix, "./", 0, 1, 0);
2877 #endif
2878 }
2879
2880 /* Now we have the specs.
2881 Set the `valid' bits for switches that match anything in any spec. */
2882
2883 validate_all_switches ();
2884
2885 /* Warn about any switches that no pass was interested in. */
2886
2887 for (i = 0; i < n_switches; i++)
2888 if (! switches[i].valid)
2889 error ("unrecognized option `-%s'", switches[i].part1);
2890
2891 /* Obey some of the options. */
2892
2893 if (verbose_flag)
2894 {
2895 fprintf (stderr, "gcc version %s\n", version_string);
2896 if (n_infiles == 0)
2897 exit (0);
2898 }
2899
2900 if (n_infiles == 0)
2901 fatal ("No input files specified.");
2902
2903 /* Make a place to record the compiler output file names
2904 that correspond to the input files. */
2905
2906 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
2907 bzero (outfiles, n_infiles * sizeof (char *));
2908
2909 /* Record which files were specified explicitly as link input. */
2910
2911 explicit_link_files = xmalloc (n_infiles);
2912 bzero (explicit_link_files, n_infiles);
2913
2914 for (i = 0; i < n_infiles; i++)
2915 {
2916 register struct compiler *cp = 0;
2917 int this_file_error = 0;
2918
2919 /* Tell do_spec what to substitute for %i. */
2920
2921 input_filename = infiles[i].name;
2922 input_filename_length = strlen (input_filename);
2923 input_file_number = i;
2924
2925 /* Use the same thing in %o, unless cp->spec says otherwise. */
2926
2927 outfiles[i] = input_filename;
2928
2929 /* Figure out which compiler from the file's suffix. */
2930
2931 cp = lookup_compiler (infiles[i].name, input_filename_length,
2932 infiles[i].language);
2933
2934 if (cp)
2935 {
2936 /* Ok, we found an applicable compiler. Run its spec. */
2937 /* First say how much of input_filename to substitute for %b */
2938 register char *p;
2939
2940 input_basename = input_filename;
2941 for (p = input_filename; *p; p++)
2942 if (*p == '/')
2943 input_basename = p + 1;
2944
2945 /* Find a suffix starting with the last period,
2946 and set basename_length to exclude that suffix. */
2947 basename_length = strlen (input_basename);
2948 p = input_basename + basename_length;
2949 while (p != input_basename && *p != '.') --p;
2950 if (*p == '.' && p != input_basename)
2951 {
2952 basename_length = p - input_basename;
2953 input_suffix = p + 1;
2954 }
2955 else
2956 input_suffix = "";
2957
2958 value = do_spec (cp->spec);
2959 if (value < 0)
2960 this_file_error = 1;
2961 }
2962
2963 /* If this file's name does not contain a recognized suffix,
2964 record it as explicit linker input. */
2965
2966 else
2967 explicit_link_files[i] = 1;
2968
2969 /* Clear the delete-on-failure queue, deleting the files in it
2970 if this compilation failed. */
2971
2972 if (this_file_error)
2973 {
2974 delete_failure_queue ();
2975 error_count++;
2976 }
2977 /* If this compilation succeeded, don't delete those files later. */
2978 clear_failure_queue ();
2979 }
2980
2981 /* Run ld to link all the compiler output files. */
2982
2983 if (error_count == 0)
2984 {
2985 int tmp = execution_count;
2986 value = do_spec (link_command_spec);
2987 if (value < 0)
2988 error_count = 1;
2989 linker_was_run = (tmp != execution_count);
2990 }
2991
2992 /* Warn if a -B option was specified but the prefix was never used. */
2993 unused_prefix_warnings (&exec_prefix);
2994 unused_prefix_warnings (&startfile_prefix);
2995
2996 /* If options said don't run linker,
2997 complain about input files to be given to the linker. */
2998
2999 if (! linker_was_run && error_count == 0)
3000 for (i = 0; i < n_infiles; i++)
3001 if (explicit_link_files[i])
3002 error ("%s: linker input file unused since linking not done",
3003 outfiles[i]);
3004
3005 /* Delete some or all of the temporary files we made. */
3006
3007 if (error_count)
3008 delete_failure_queue ();
3009 delete_temp_files ();
3010
3011 exit (error_count);
3012 /* NOTREACHED */
3013 return 0;
3014 }
3015
3016 /* Find the proper compilation spec for the file name NAME,
3017 whose length is LENGTH. LANGUAGE is the specified language,
3018 or 0 if none specified. */
3019
3020 static struct compiler *
3021 lookup_compiler (name, length, language)
3022 char *name;
3023 int length;
3024 char *language;
3025 {
3026 struct compiler *cp;
3027
3028 /* Look for the language, if one is spec'd. */
3029 if (language != 0)
3030 {
3031 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3032 {
3033 if (language != 0)
3034 {
3035 if (cp->suffix[0] == '@'
3036 && !strcmp (cp->suffix + 1, language))
3037 return cp;
3038 }
3039 }
3040 error ("language %s not recognized", language);
3041 }
3042
3043 /* Look for a suffix. */
3044 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
3045 {
3046 if (strlen (cp->suffix) < length
3047 /* See if the suffix matches the end of NAME. */
3048 && !strcmp (cp->suffix,
3049 name + length - strlen (cp->suffix))
3050 /* The suffix `-' matches only the file name `-'. */
3051 && !(!strcmp (cp->suffix, "-") && length != 1))
3052 {
3053 if (cp->spec[0] == '@')
3054 {
3055 struct compiler *new;
3056 /* An alias entry maps a suffix to a language.
3057 Search for the language; pass 0 for NAME and LENGTH
3058 to avoid infinite recursion if language not found.
3059 Construct the new compiler spec. */
3060 language = cp->spec + 1;
3061 new = (struct compiler *) xmalloc (sizeof (struct compiler));
3062 new->suffix = cp->suffix;
3063 new->spec = lookup_compiler (0, 0, language)->spec;
3064 return new;
3065 }
3066 /* A non-alias entry: return it. */
3067 return cp;
3068 }
3069 }
3070
3071 return 0;
3072 }
3073 \f
3074 char *
3075 xmalloc (size)
3076 unsigned size;
3077 {
3078 register char *value = (char *) malloc (size);
3079 if (value == 0)
3080 fatal ("virtual memory exhausted");
3081 return value;
3082 }
3083
3084 char *
3085 xrealloc (ptr, size)
3086 char *ptr;
3087 unsigned size;
3088 {
3089 register char *value = (char *) realloc (ptr, size);
3090 if (value == 0)
3091 fatal ("virtual memory exhausted");
3092 return value;
3093 }
3094
3095 /* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
3096
3097 static char *
3098 concat (s1, s2, s3)
3099 char *s1, *s2, *s3;
3100 {
3101 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
3102 char *result = xmalloc (len1 + len2 + len3 + 1);
3103
3104 strcpy (result, s1);
3105 strcpy (result + len1, s2);
3106 strcpy (result + len1 + len2, s3);
3107 *(result + len1 + len2 + len3) = 0;
3108
3109 return result;
3110 }
3111
3112 static char *
3113 save_string (s, len)
3114 char *s;
3115 int len;
3116 {
3117 register char *result = xmalloc (len + 1);
3118
3119 bcopy (s, result, len);
3120 result[len] = 0;
3121 return result;
3122 }
3123
3124 static void
3125 pfatal_with_name (name)
3126 char *name;
3127 {
3128 char *s;
3129
3130 if (errno < sys_nerr)
3131 s = concat ("%s: ", sys_errlist[errno], "");
3132 else
3133 s = "cannot open %s";
3134 fatal (s, name);
3135 }
3136
3137 static void
3138 perror_with_name (name)
3139 char *name;
3140 {
3141 char *s;
3142
3143 if (errno < sys_nerr)
3144 s = concat ("%s: ", sys_errlist[errno], "");
3145 else
3146 s = "cannot open %s";
3147 error (s, name);
3148 }
3149
3150 static void
3151 perror_exec (name)
3152 char *name;
3153 {
3154 char *s;
3155
3156 if (errno < sys_nerr)
3157 s = concat ("installation problem, cannot exec %s: ",
3158 sys_errlist[errno], "");
3159 else
3160 s = "installation problem, cannot exec %s";
3161 error (s, name);
3162 }
3163
3164 /* More 'friendly' abort that prints the line and file.
3165 config.h can #define abort fancy_abort if you like that sort of thing. */
3166
3167 void
3168 fancy_abort ()
3169 {
3170 fatal ("Internal gcc abort.");
3171 }
3172 \f
3173 #ifdef HAVE_VPRINTF
3174
3175 /* Output an error message and exit */
3176
3177 static void
3178 fatal (va_alist)
3179 va_dcl
3180 {
3181 va_list ap;
3182 char *format;
3183
3184 va_start (ap);
3185 format = va_arg (ap, char *);
3186 fprintf (stderr, "%s: ", programname);
3187 vfprintf (stderr, format, ap);
3188 va_end (ap);
3189 fprintf (stderr, "\n");
3190 delete_temp_files ();
3191 exit (1);
3192 }
3193
3194 static void
3195 error (va_alist)
3196 va_dcl
3197 {
3198 va_list ap;
3199 char *format;
3200
3201 va_start (ap);
3202 format = va_arg (ap, char *);
3203 fprintf (stderr, "%s: ", programname);
3204 vfprintf (stderr, format, ap);
3205 va_end (ap);
3206
3207 fprintf (stderr, "\n");
3208 }
3209
3210 #else /* not HAVE_VPRINTF */
3211
3212 static void
3213 fatal (msg, arg1, arg2)
3214 char *msg, *arg1, *arg2;
3215 {
3216 error (msg, arg1, arg2);
3217 delete_temp_files ();
3218 exit (1);
3219 }
3220
3221 static void
3222 error (msg, arg1, arg2)
3223 char *msg, *arg1, *arg2;
3224 {
3225 fprintf (stderr, "%s: ", programname);
3226 fprintf (stderr, msg, arg1, arg2);
3227 fprintf (stderr, "\n");
3228 }
3229
3230 #endif /* not HAVE_VPRINTF */
3231
3232 \f
3233 static void
3234 validate_all_switches ()
3235 {
3236 struct compiler *comp;
3237 register char *p;
3238 register char c;
3239
3240 for (comp = compilers; comp->spec; comp++)
3241 {
3242 p = comp->spec;
3243 while (c = *p++)
3244 if (c == '%' && *p == '{')
3245 /* We have a switch spec. */
3246 validate_switches (p + 1);
3247 }
3248
3249 p = link_command_spec;
3250 while (c = *p++)
3251 if (c == '%' && *p == '{')
3252 /* We have a switch spec. */
3253 validate_switches (p + 1);
3254
3255 /* Now notice switches mentioned in the machine-specific specs. */
3256
3257 p = asm_spec;
3258 while (c = *p++)
3259 if (c == '%' && *p == '{')
3260 /* We have a switch spec. */
3261 validate_switches (p + 1);
3262
3263 p = asm_final_spec;
3264 while (c = *p++)
3265 if (c == '%' && *p == '{')
3266 /* We have a switch spec. */
3267 validate_switches (p + 1);
3268
3269 p = cpp_spec;
3270 while (c = *p++)
3271 if (c == '%' && *p == '{')
3272 /* We have a switch spec. */
3273 validate_switches (p + 1);
3274
3275 p = signed_char_spec;
3276 while (c = *p++)
3277 if (c == '%' && *p == '{')
3278 /* We have a switch spec. */
3279 validate_switches (p + 1);
3280
3281 p = cc1_spec;
3282 while (c = *p++)
3283 if (c == '%' && *p == '{')
3284 /* We have a switch spec. */
3285 validate_switches (p + 1);
3286
3287 p = cc1plus_spec;
3288 while (c = *p++)
3289 if (c == '%' && *p == '{')
3290 /* We have a switch spec. */
3291 validate_switches (p + 1);
3292
3293 p = link_spec;
3294 while (c = *p++)
3295 if (c == '%' && *p == '{')
3296 /* We have a switch spec. */
3297 validate_switches (p + 1);
3298
3299 p = lib_spec;
3300 while (c = *p++)
3301 if (c == '%' && *p == '{')
3302 /* We have a switch spec. */
3303 validate_switches (p + 1);
3304
3305 p = startfile_spec;
3306 while (c = *p++)
3307 if (c == '%' && *p == '{')
3308 /* We have a switch spec. */
3309 validate_switches (p + 1);
3310 }
3311
3312 /* Look at the switch-name that comes after START
3313 and mark as valid all supplied switches that match it. */
3314
3315 static void
3316 validate_switches (start)
3317 char *start;
3318 {
3319 register char *p = start;
3320 char *filter;
3321 register int i;
3322 int suffix = 0;
3323
3324 if (*p == '|')
3325 ++p;
3326
3327 if (*p == '!')
3328 ++p;
3329
3330 if (*p == '.')
3331 suffix = 1, ++p;
3332
3333 filter = p;
3334 while (*p != ':' && *p != '}') p++;
3335
3336 if (suffix)
3337 ;
3338 else if (p[-1] == '*')
3339 {
3340 /* Mark all matching switches as valid. */
3341 --p;
3342 for (i = 0; i < n_switches; i++)
3343 if (!strncmp (switches[i].part1, filter, p - filter))
3344 switches[i].valid = 1;
3345 }
3346 else
3347 {
3348 /* Mark an exact matching switch as valid. */
3349 for (i = 0; i < n_switches; i++)
3350 {
3351 if (!strncmp (switches[i].part1, filter, p - filter)
3352 && switches[i].part1[p - filter] == 0)
3353 switches[i].valid = 1;
3354 }
3355 }
3356 }
This page took 0.19908 seconds and 6 git commands to generate.