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