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