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