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