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