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