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