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