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