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