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