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