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