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