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