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