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