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