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