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