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