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