* lib/objc.exp: Add -lposix4 on Solaris 2.6 and Solaris 2.7.
[official-gcc.git] / gcc / gcc.c
blob6ccd7ce84c3cbd440bc5235d7b15d97fb19b0a12
1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 This paragraph is here to try to keep Sun CC from dying.
23 The number of chars here seems crucial!!!! */
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.
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". */
35 #include "config.h"
36 #include "system.h"
37 #include <signal.h>
38 #if ! defined( SIGCHLD ) && defined( SIGCLD )
39 # define SIGCHLD SIGCLD
40 #endif
41 #include "obstack.h"
42 #include "intl.h"
43 #include "prefix.h"
44 #include "gcc.h"
46 #ifdef VMS
47 #define exit __posix_exit
48 #endif
50 #ifdef HAVE_SYS_RESOURCE_H
51 #include <sys/resource.h>
52 #endif
53 #if defined (HAVE_DECL_GETRUSAGE) && !HAVE_DECL_GETRUSAGE
54 extern int getrusage PARAMS ((int, struct rusage *));
55 #endif
57 /* By default there is no special suffix for executables. */
58 #ifdef EXECUTABLE_SUFFIX
59 #define HAVE_EXECUTABLE_SUFFIX
60 #else
61 #define EXECUTABLE_SUFFIX ""
62 #endif
64 /* By default, the suffix for object files is ".o". */
65 #ifdef OBJECT_SUFFIX
66 #define HAVE_OBJECT_SUFFIX
67 #else
68 #define OBJECT_SUFFIX ".o"
69 #endif
71 #ifndef VMS
72 /* FIXME: the location independence code for VMS is hairier than this,
73 and hasn't been written. */
74 #ifndef DIR_UP
75 #define DIR_UP ".."
76 #endif /* DIR_UP */
77 #endif /* VMS */
79 static char dir_separator_str[] = { DIR_SEPARATOR, 0 };
81 #define obstack_chunk_alloc xmalloc
82 #define obstack_chunk_free free
84 #ifndef GET_ENV_PATH_LIST
85 #define GET_ENV_PATH_LIST(VAR,NAME) do { (VAR) = getenv (NAME); } while (0)
86 #endif
88 /* Most every one is fine with LIBRARY_PATH. For some, it conflicts. */
89 #ifndef LIBRARY_PATH_ENV
90 #define LIBRARY_PATH_ENV "LIBRARY_PATH"
91 #endif
93 #ifndef HAVE_KILL
94 #define kill(p,s) raise(s)
95 #endif
97 /* If a stage of compilation returns an exit status >= 1,
98 compilation of that file ceases. */
100 #define MIN_FATAL_STATUS 1
102 /* Flag saying to pass the greatest exit code returned by a sub-process
103 to the calling program. */
104 static int pass_exit_codes;
106 /* Definition of string containing the arguments given to configure. */
107 #include "configargs.h"
109 /* Flag saying to print the directories gcc will search through looking for
110 programs, libraries, etc. */
112 static int print_search_dirs;
114 /* Flag saying to print the full filename of this file
115 as found through our usual search mechanism. */
117 static const char *print_file_name = NULL;
119 /* As print_file_name, but search for executable file. */
121 static const char *print_prog_name = NULL;
123 /* Flag saying to print the relative path we'd use to
124 find libgcc.a given the current compiler flags. */
126 static int print_multi_directory;
128 /* Flag saying to print the list of subdirectories and
129 compiler flags used to select them in a standard form. */
131 static int print_multi_lib;
133 /* Flag saying to print the command line options understood by gcc and its
134 sub-processes. */
136 static int print_help_list;
138 /* Flag indicating whether we should print the command and arguments */
140 static int verbose_flag;
142 /* Flag indicating to print target specific command line options. */
144 static int target_help_flag;
146 /* Flag indicating whether we should report subprocess execution times
147 (if this is supported by the system - see pexecute.c). */
149 static int report_times;
151 /* Nonzero means write "temp" files in source directory
152 and use the source file's name in them, and don't delete them. */
154 static int save_temps_flag;
156 /* The compiler version. */
158 static const char *compiler_version;
160 /* The target version specified with -V */
162 static const char *spec_version = DEFAULT_TARGET_VERSION;
164 /* The target machine specified with -b. */
166 static const char *spec_machine = DEFAULT_TARGET_MACHINE;
168 /* Nonzero if cross-compiling.
169 When -b is used, the value comes from the `specs' file. */
171 #ifdef CROSS_COMPILE
172 static const char *cross_compile = "1";
173 #else
174 static const char *cross_compile = "0";
175 #endif
177 #ifdef MODIFY_TARGET_NAME
179 /* Information on how to alter the target name based on a command-line
180 switch. The only case we support now is simply appending or deleting a
181 string to or from the end of the first part of the configuration name. */
183 struct modify_target
185 const char *sw;
186 enum add_del {ADD, DELETE} add_del;
187 const char *str;
189 modify_target[] = MODIFY_TARGET_NAME;
190 #endif
192 /* The number of errors that have occurred; the link phase will not be
193 run if this is non-zero. */
194 static int error_count = 0;
196 /* Greatest exit code of sub-processes that has been encountered up to
197 now. */
198 static int greatest_status = 1;
200 /* This is the obstack which we use to allocate many strings. */
202 static struct obstack obstack;
204 /* This is the obstack to build an environment variable to pass to
205 collect2 that describes all of the relevant switches of what to
206 pass the compiler in building the list of pointers to constructors
207 and destructors. */
209 static struct obstack collect_obstack;
211 /* These structs are used to collect resource usage information for
212 subprocesses. */
213 #ifdef HAVE_GETRUSAGE
214 static struct rusage rus, prus;
215 #endif
217 /* Forward declaration for prototypes. */
218 struct path_prefix;
220 static void init_spec PARAMS ((void));
221 #ifndef VMS
222 static char **split_directories PARAMS ((const char *, int *));
223 static void free_split_directories PARAMS ((char **));
224 static char *make_relative_prefix PARAMS ((const char *, const char *, const char *));
225 #endif /* VMS */
226 static void store_arg PARAMS ((const char *, int, int));
227 static char *load_specs PARAMS ((const char *));
228 static void read_specs PARAMS ((const char *, int));
229 static void set_spec PARAMS ((const char *, const char *));
230 static struct compiler *lookup_compiler PARAMS ((const char *, size_t, const char *));
231 static char *build_search_list PARAMS ((struct path_prefix *, const char *, int));
232 static void putenv_from_prefixes PARAMS ((struct path_prefix *, const char *));
233 static int access_check PARAMS ((const char *, int));
234 static char *find_a_file PARAMS ((struct path_prefix *, const char *, int));
235 static void add_prefix PARAMS ((struct path_prefix *, const char *,
236 const char *, int, int, int *));
237 static void translate_options PARAMS ((int *, const char *const **));
238 static char *skip_whitespace PARAMS ((char *));
239 static void delete_if_ordinary PARAMS ((const char *));
240 static void delete_temp_files PARAMS ((void));
241 static void delete_failure_queue PARAMS ((void));
242 static void clear_failure_queue PARAMS ((void));
243 static int check_live_switch PARAMS ((int, int));
244 static const char *handle_braces PARAMS ((const char *));
245 static char *save_string PARAMS ((const char *, int));
246 static int do_spec_1 PARAMS ((const char *, int, const char *));
247 static const char *find_file PARAMS ((const char *));
248 static int is_directory PARAMS ((const char *, const char *, int));
249 static void validate_switches PARAMS ((const char *));
250 static void validate_all_switches PARAMS ((void));
251 static void give_switch PARAMS ((int, int, int));
252 static int used_arg PARAMS ((const char *, int));
253 static int default_arg PARAMS ((const char *, int));
254 static void set_multilib_dir PARAMS ((void));
255 static void print_multilib_info PARAMS ((void));
256 static void perror_with_name PARAMS ((const char *));
257 static void pfatal_pexecute PARAMS ((const char *, const char *))
258 ATTRIBUTE_NORETURN;
259 static void notice PARAMS ((const char *, ...))
260 ATTRIBUTE_PRINTF_1;
261 static void display_help PARAMS ((void));
262 static void add_preprocessor_option PARAMS ((const char *, int));
263 static void add_assembler_option PARAMS ((const char *, int));
264 static void add_linker_option PARAMS ((const char *, int));
265 static void process_command PARAMS ((int, const char *const *));
266 static int execute PARAMS ((void));
267 static void clear_args PARAMS ((void));
268 static void fatal_error PARAMS ((int));
269 static void set_input PARAMS ((const char *));
270 static void init_gcc_specs PARAMS ((struct obstack *,
271 const char *,
272 const char *));
274 /* Specs are strings containing lines, each of which (if not blank)
275 is made up of a program name, and arguments separated by spaces.
276 The program name must be exact and start from root, since no path
277 is searched and it is unreliable to depend on the current working directory.
278 Redirection of input or output is not supported; the subprograms must
279 accept filenames saying what files to read and write.
281 In addition, the specs can contain %-sequences to substitute variable text
282 or for conditional text. Here is a table of all defined %-sequences.
283 Note that spaces are not generated automatically around the results of
284 expanding these sequences; therefore, you can concatenate them together
285 or with constant text in a single argument.
287 %% substitute one % into the program name or argument.
288 %i substitute the name of the input file being processed.
289 %b substitute the basename of the input file being processed.
290 This is the substring up to (and not including) the last period
291 and not including the directory.
292 %B same as %b, but include the file suffix (text after the last period).
293 %gSUFFIX
294 substitute a file name that has suffix SUFFIX and is chosen
295 once per compilation, and mark the argument a la %d. To reduce
296 exposure to denial-of-service attacks, the file name is now
297 chosen in a way that is hard to predict even when previously
298 chosen file names are known. For example, `%g.s ... %g.o ... %g.s'
299 might turn into `ccUVUUAU.s ccXYAXZ12.o ccUVUUAU.s'. SUFFIX matches
300 the regexp "[.A-Za-z]*%O"; "%O" is treated exactly as if it
301 had been pre-processed. Previously, %g was simply substituted
302 with a file name chosen once per compilation, without regard
303 to any appended suffix (which was therefore treated just like
304 ordinary text), making such attacks more likely to succeed.
305 %uSUFFIX
306 like %g, but generates a new temporary file name even if %uSUFFIX
307 was already seen.
308 %USUFFIX
309 substitutes the last file name generated with %uSUFFIX, generating a
310 new one if there is no such last file name. In the absence of any
311 %uSUFFIX, this is just like %gSUFFIX, except they don't share
312 the same suffix "space", so `%g.s ... %U.s ... %g.s ... %U.s'
313 would involve the generation of two distinct file names, one
314 for each `%g.s' and another for each `%U.s'. Previously, %U was
315 simply substituted with a file name chosen for the previous %u,
316 without regard to any appended suffix.
317 %jSUFFIX
318 substitutes the name of the HOST_BIT_BUCKET, if any, and if it is
319 writable, and if save-temps is off; otherwise, substitute the name
320 of a temporary file, just like %u. This temporary file is not
321 meant for communication between processes, but rather as a junk
322 disposal mechanism.
323 %.SUFFIX
324 substitutes .SUFFIX for the suffixes of a matched switch's args when
325 it is subsequently output with %*. SUFFIX is terminated by the next
326 space or %.
327 %d marks the argument containing or following the %d as a
328 temporary file name, so that that file will be deleted if CC exits
329 successfully. Unlike %g, this contributes no text to the argument.
330 %w marks the argument containing or following the %w as the
331 "output file" of this compilation. This puts the argument
332 into the sequence of arguments that %o will substitute later.
333 %W{...}
334 like %{...} but mark last argument supplied within
335 as a file to be deleted on failure.
336 %o substitutes the names of all the output files, with spaces
337 automatically placed around them. You should write spaces
338 around the %o as well or the results are undefined.
339 %o is for use in the specs for running the linker.
340 Input files whose names have no recognized suffix are not compiled
341 at all, but they are included among the output files, so they will
342 be linked.
343 %O substitutes the suffix for object files. Note that this is
344 handled specially when it immediately follows %g, %u, or %U
345 (with or without a suffix argument) because of the need for
346 those to form complete file names. The handling is such that
347 %O is treated exactly as if it had already been substituted,
348 except that %g, %u, and %U do not currently support additional
349 SUFFIX characters following %O as they would following, for
350 example, `.o'.
351 %p substitutes the standard macro predefinitions for the
352 current target machine. Use this when running cpp.
353 %P like %p, but puts `__' before and after the name of each macro.
354 (Except macros that already have __.)
355 This is for ANSI C.
356 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
357 %s current argument is the name of a library or startup file of some sort.
358 Search for that file in a standard list of directories
359 and substitute the full name found.
360 %eSTR Print STR as an error message. STR is terminated by a newline.
361 Use this when inconsistent options are detected.
362 %nSTR Print STR as an notice. STR is terminated by a newline.
363 %x{OPTION} Accumulate an option for %X.
364 %X Output the accumulated linker options specified by compilations.
365 %Y Output the accumulated assembler options specified by compilations.
366 %Z Output the accumulated preprocessor options specified by compilations.
367 %v1 Substitute the major version number of GCC.
368 (For version 2.5.3, this is 2.)
369 %v2 Substitute the minor version number of GCC.
370 (For version 2.5.3, this is 5.)
371 %v3 Substitute the patch level number of GCC.
372 (For version 2.5.3, this is 3.)
373 %a process ASM_SPEC as a spec.
374 This allows config.h to specify part of the spec for running as.
375 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
376 used here. This can be used to run a post-processor after the
377 assembler has done its job.
378 %D Dump out a -L option for each directory in startfile_prefixes.
379 If multilib_dir is set, extra entries are generated with it affixed.
380 %l process LINK_SPEC as a spec.
381 %L process LIB_SPEC as a spec.
382 %G process LIBGCC_SPEC as a spec.
383 %M output multilib_dir with directory separators replaced with "_";
384 if multilib_dir is not set or is ".", output "".
385 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
386 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
387 %c process SIGNED_CHAR_SPEC as a spec.
388 %C process CPP_SPEC as a spec.
389 %1 process CC1_SPEC as a spec.
390 %2 process CC1PLUS_SPEC as a spec.
391 %| output "-" if the input for the current command is coming from a pipe.
392 %* substitute the variable part of a matched option. (See below.)
393 Note that each comma in the substituted string is replaced by
394 a single space.
395 %{S} substitutes the -S switch, if that switch was given to CC.
396 If that switch was not specified, this substitutes nothing.
397 Here S is a metasyntactic variable.
398 %{S*} substitutes all the switches specified to CC whose names start
399 with -S. This is used for -o, -I, etc; switches that take
400 arguments. CC considers `-o foo' as being one switch whose
401 name starts with `o'. %{o*} would substitute this text,
402 including the space; thus, two arguments would be generated.
403 %{^S*} likewise, but don't put a blank between a switch and any args.
404 %{S*&T*} likewise, but preserve order of S and T options (the order
405 of S and T in the spec is not significant). Can be any number
406 of ampersand-separated variables; for each the wild card is
407 optional. Useful for CPP as %{D*&U*&A*}.
408 %{S*:X} substitutes X if one or more switches whose names start with -S are
409 specified to CC. Note that the tail part of the -S option
410 (i.e. the part matched by the `*') will be substituted for each
411 occurrence of %* within X.
412 %{<S} remove all occurences of -S from the command line.
413 Note - this option is position dependent. % commands in the
414 spec string before this option will see -S, % commands in the
415 spec string after this option will not.
416 %{S:X} substitutes X, but only if the -S switch was given to CC.
417 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
418 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
419 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
420 %{.S:X} substitutes X, but only if processing a file with suffix S.
421 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
422 %{S|P:X} substitutes X if either -S or -P was given to CC. This may be
423 combined with ! and . as above binding stronger than the OR.
424 %(Spec) processes a specification defined in a specs file as *Spec:
425 %[Spec] as above, but put __ around -D arguments
427 The conditional text X in a %{S:X} or %{!S:X} construct may contain
428 other nested % constructs or spaces, or even newlines. They are
429 processed as usual, as described above.
431 The -O, -f, -m, and -W switches are handled specifically in these
432 constructs. If another value of -O or the negated form of a -f, -m, or
433 -W switch is found later in the command line, the earlier switch
434 value is ignored, except with {S*} where S is just one letter; this
435 passes all matching options.
437 The character | at the beginning of the predicate text is used to indicate
438 that a command should be piped to the following command, but only if -pipe
439 is specified.
441 Note that it is built into CC which switches take arguments and which
442 do not. You might think it would be useful to generalize this to
443 allow each compiler's spec to say which switches take arguments. But
444 this cannot be done in a consistent fashion. CC cannot even decide
445 which input files have been specified without knowing which switches
446 take arguments, and it must know which input files to compile in order
447 to tell which compilers to run.
449 CC also knows implicitly that arguments starting in `-l' are to be
450 treated as compiler output files, and passed to the linker in their
451 proper position among the other output files. */
453 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
455 /* config.h can define ASM_SPEC to provide extra args to the assembler
456 or extra switch-translations. */
457 #ifndef ASM_SPEC
458 #define ASM_SPEC ""
459 #endif
461 /* config.h can define ASM_FINAL_SPEC to run a post processor after
462 the assembler has run. */
463 #ifndef ASM_FINAL_SPEC
464 #define ASM_FINAL_SPEC ""
465 #endif
467 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
468 or extra switch-translations. */
469 #ifndef CPP_SPEC
470 #define CPP_SPEC ""
471 #endif
473 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
474 or extra switch-translations. */
475 #ifndef CC1_SPEC
476 #define CC1_SPEC ""
477 #endif
479 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
480 or extra switch-translations. */
481 #ifndef CC1PLUS_SPEC
482 #define CC1PLUS_SPEC ""
483 #endif
485 /* config.h can define LINK_SPEC to provide extra args to the linker
486 or extra switch-translations. */
487 #ifndef LINK_SPEC
488 #define LINK_SPEC ""
489 #endif
491 /* config.h can define LIB_SPEC to override the default libraries. */
492 #ifndef LIB_SPEC
493 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
494 #endif
496 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
497 included. */
498 #ifndef LIBGCC_SPEC
499 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
500 /* Have gcc do the search for libgcc.a. */
501 #define LIBGCC_SPEC "libgcc.a%s"
502 #else
503 #define LIBGCC_SPEC "-lgcc"
504 #endif
505 #endif
507 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
508 #ifndef STARTFILE_SPEC
509 #define STARTFILE_SPEC \
510 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
511 #endif
513 /* config.h can define SWITCHES_NEED_SPACES to control which options
514 require spaces between the option and the argument. */
515 #ifndef SWITCHES_NEED_SPACES
516 #define SWITCHES_NEED_SPACES ""
517 #endif
519 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
520 #ifndef ENDFILE_SPEC
521 #define ENDFILE_SPEC ""
522 #endif
524 /* This spec is used for telling cpp whether char is signed or not. */
525 #ifndef SIGNED_CHAR_SPEC
526 /* Use #if rather than ?:
527 because MIPS C compiler rejects like ?: in initializers. */
528 #if DEFAULT_SIGNED_CHAR
529 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
530 #else
531 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
532 #endif
533 #endif
535 #ifndef LINKER_NAME
536 #define LINKER_NAME "collect2"
537 #endif
539 /* Here is the spec for running the linker, after compiling all files. */
541 /* -u* was put back because both BSD and SysV seem to support it. */
542 /* %{static:} simply prevents an error message if the target machine
543 doesn't handle -static. */
544 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
545 scripts which exist in user specified directories, or in standard
546 directories. */
547 #ifndef LINK_COMMAND_SPEC
548 #define LINK_COMMAND_SPEC "\
549 %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S:\
550 %(linker) %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t}\
551 %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
552 %{static:} %{L*} %(link_libgcc) %o %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
553 %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}"
554 #endif
556 #ifndef LINK_LIBGCC_SPEC
557 # ifdef LINK_LIBGCC_SPECIAL
558 /* Don't generate -L options for startfile prefix list. */
559 # define LINK_LIBGCC_SPEC ""
560 # else
561 /* Do generate them. */
562 # define LINK_LIBGCC_SPEC "%D"
563 # endif
564 #endif
566 static const char *cpp_spec = CPP_SPEC;
567 static const char *cpp_predefines = CPP_PREDEFINES;
568 static const char *cc1_spec = CC1_SPEC;
569 static const char *cc1plus_spec = CC1PLUS_SPEC;
570 static const char *signed_char_spec = SIGNED_CHAR_SPEC;
571 static const char *asm_spec = ASM_SPEC;
572 static const char *asm_final_spec = ASM_FINAL_SPEC;
573 static const char *link_spec = LINK_SPEC;
574 static const char *lib_spec = LIB_SPEC;
575 static const char *libgcc_spec = LIBGCC_SPEC;
576 static const char *endfile_spec = ENDFILE_SPEC;
577 static const char *startfile_spec = STARTFILE_SPEC;
578 static const char *switches_need_spaces = SWITCHES_NEED_SPACES;
579 static const char *linker_name_spec = LINKER_NAME;
580 static const char *link_command_spec = LINK_COMMAND_SPEC;
581 static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
583 /* Standard options to cpp, cc1, and as, to reduce duplication in specs.
584 There should be no need to override these in target dependent files,
585 but we need to copy them to the specs file so that newer versions
586 of the GCC driver can correctly drive older tool chains with the
587 appropriate -B options. */
589 static const char *trad_capable_cpp =
590 "%{traditional|ftraditional|traditional-cpp:trad}cpp0";
592 static const char *cpp_options =
593 "%{C:%{!E:%eGNU C does not support -C without using -E}}\
594 %{std*} %{nostdinc*}\
595 %{C} %{v} %{I*} %{P} %{$} %I\
596 %{MD:-M -MF %W{!o: %b.d}%W{o*:%.d%*}}\
597 %{MMD:-MM -MF %W{!o: %b.d}%W{o*:%.d%*}}\
598 %{M} %{MM} %W{MF*} %{MG} %{MP} %{MQ*} %{MT*} %{M|MD|MM|MMD:%{o*:-MQ %*}}\
599 %{!no-gcc:-D__GNUC__=%v1 -D__GNUC_MINOR__=%v2 -D__GNUC_PATCHLEVEL__=%v3}\
600 %{!undef:%{!ansi:%{!std=*:%p}%{std=gnu*:%p}} %P} %{trigraphs}\
601 %c %{Os:-D__OPTIMIZE_SIZE__} %{O*:%{!O0:-D__OPTIMIZE__}}\
602 %{ffast-math:-D__FAST_MATH__}\
603 %{fshort-wchar:-U__WCHAR_TYPE__ -D__WCHAR_TYPE__=short\\ unsigned\\ int}\
604 %{ffreestanding:-D__STDC_HOSTED__=0} %{fno-hosted:-D__STDC_HOSTED__=0}\
605 %{!ffreestanding:%{!fno-hosted:-D__STDC_HOSTED__=1}}\
606 %{fshow-column} %{fno-show-column}\
607 %{fleading-underscore} %{fno-leading-underscore}\
608 %{fno-operator-names} %{ftabstop=*} %{remap}\
609 %{g3:-dD} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*&U*&A*} %{i*} %Z %i\
610 %{E:%{!M*:%W{o*}}}";
612 /* NB: This is shared amongst all front-ends. */
613 static const char *cc1_options =
614 "%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
615 %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*}\
616 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{std*} %{ansi}\
617 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
618 %{aux-info*} %{Qn:-fno-ident} %{--help:--help}\
619 %{--target-help:--target-help}\
620 %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}}\
621 %{fsyntax-only:-o %j} %{-param*}";
623 static const char *asm_options =
624 "%a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}";
626 static const char *invoke_as =
627 "%{!S:-o %{|!pipe:%g.s} |\n as %(asm_options) %{!pipe:%g.s} %A }";
629 /* Some compilers have limits on line lengths, and the multilib_select
630 and/or multilib_matches strings can be very long, so we build them at
631 run time. */
632 static struct obstack multilib_obstack;
633 static const char *multilib_select;
634 static const char *multilib_matches;
635 static const char *multilib_defaults;
636 static const char *multilib_exclusions;
637 #include "multilib.h"
639 /* Check whether a particular argument is a default argument. */
641 #ifndef MULTILIB_DEFAULTS
642 #define MULTILIB_DEFAULTS { "" }
643 #endif
645 static const char *const multilib_defaults_raw[] = MULTILIB_DEFAULTS;
647 struct user_specs
649 struct user_specs *next;
650 const char *filename;
653 static struct user_specs *user_specs_head, *user_specs_tail;
655 /* This defines which switch letters take arguments. */
657 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
658 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
659 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
660 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
661 || (CHAR) == 'L' || (CHAR) == 'A' || (CHAR) == 'V' \
662 || (CHAR) == 'B' || (CHAR) == 'b')
664 #ifndef SWITCH_TAKES_ARG
665 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
666 #endif
668 /* This defines which multi-letter switches take arguments. */
670 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
671 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
672 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
673 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
674 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
675 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
676 || !strcmp (STR, "isystem") || !strcmp (STR, "-param") \
677 || !strcmp (STR, "specs") \
678 || !strcmp (STR, "MF") || !strcmp (STR, "MT") || !strcmp (STR, "MQ"))
680 #ifndef WORD_SWITCH_TAKES_ARG
681 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
682 #endif
684 #ifdef HAVE_EXECUTABLE_SUFFIX
685 /* This defines which switches stop a full compilation. */
686 #define DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR) \
687 ((CHAR) == 'c' || (CHAR) == 'S')
689 #ifndef SWITCH_CURTAILS_COMPILATION
690 #define SWITCH_CURTAILS_COMPILATION(CHAR) \
691 DEFAULT_SWITCH_CURTAILS_COMPILATION(CHAR)
692 #endif
693 #endif
695 /* Record the mapping from file suffixes for compilation specs. */
697 struct compiler
699 const char *suffix; /* Use this compiler for input files
700 whose names end in this suffix. */
702 const char *spec; /* To use this compiler, run this spec. */
704 const char *cpp_spec; /* If non-NULL, substitute this spec
705 for `%C', rather than the usual
706 cpp_spec. */
709 /* Pointer to a vector of `struct compiler' that gives the spec for
710 compiling a file, based on its suffix.
711 A file that does not end in any of these suffixes will be passed
712 unchanged to the loader and nothing else will be done to it.
714 An entry containing two 0s is used to terminate the vector.
716 If multiple entries match a file, the last matching one is used. */
718 static struct compiler *compilers;
720 /* Number of entries in `compilers', not counting the null terminator. */
722 static int n_compilers;
724 /* The default list of file name suffixes and their compilation specs. */
726 static struct compiler default_compilers[] =
728 /* Add lists of suffixes of known languages here. If those languages
729 were not present when we built the driver, we will hit these copies
730 and be given a more meaningful error than "file not used since
731 linking is not done". */
732 {".m", "#Objective-C", 0}, {".mi", "#Objective-C", 0},
733 {".cc", "#C++", 0}, {".cxx", "#C++", 0}, {".cpp", "#C++", 0},
734 {".cp", "#C++", 0}, {".c++", "#C++", 0}, {".C", "#C++", 0},
735 {".ii", "#C++", 0},
736 {".ads", "#Ada", 0}, {".adb", "#Ada", 0}, {".ada", "#Ada", 0},
737 {".f", "#Fortran", 0}, {".for", "#Fortran", 0}, {".fpp", "#Fortran", 0},
738 {".F", "#Fortran", 0}, {".FOR", "#Fortran", 0}, {".FPP", "#Fortran", 0},
739 {".r", "#Ratfor", 0},
740 {".p", "#Pascal", 0}, {".pas", "#Pascal", 0},
741 {".ch", "#Chill", 0}, {".chi", "#Chill", 0},
742 {".java", "#Java", 0}, {".class", "#Java", 0},
743 {".zip", "#Java", 0}, {".jar", "#Java", 0},
744 /* Next come the entries for C. */
745 {".c", "@c", 0},
746 {"@c",
747 /* cc1 has an integrated ISO C preprocessor. We should invoke the
748 external preprocessor if -save-temps or -traditional is given. */
749 "%{E|M|MM:%(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)}\
750 %{!E:%{!M:%{!MM:\
751 %{save-temps:%(trad_capable_cpp) -lang-c %{ansi:-std=c89}\
752 %(cpp_options) %b.i \n\
753 cc1 -fpreprocessed %b.i %(cc1_options)}\
754 %{!save-temps:\
755 %{traditional|ftraditional|traditional-cpp:\
756 tradcpp0 -lang-c %{ansi:-std=c89} %(cpp_options) %{!pipe:%g.i} |\n\
757 cc1 -fpreprocessed %{!pipe:%g.i} %(cc1_options)}\
758 %{!traditional:%{!ftraditional:%{!traditional-cpp:\
759 cc1 -lang-c %{ansi:-std=c89} %(cpp_options) %(cc1_options)}}}}\
760 %{!fsyntax-only:%(invoke_as)}}}}", 0},
761 {"-",
762 "%{!E:%e-E required when input is from standard input}\
763 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
764 {".h", "@c-header", 0},
765 {"@c-header",
766 "%{!E:%eCompilation of header file requested} \
767 %(trad_capable_cpp) -lang-c %{ansi:-std=c89} %(cpp_options)", 0},
768 {".i", "@cpp-output", 0},
769 {"@cpp-output",
770 "%{!M:%{!MM:%{!E:cc1 -fpreprocessed %i %(cc1_options) %{!fsyntax-only:%(invoke_as)}}}}", 0},
771 {".s", "@assembler", 0},
772 {"@assembler",
773 "%{!M:%{!MM:%{!E:%{!S:as %(asm_options) %i %A }}}}", 0},
774 {".S", "@assembler-with-cpp", 0},
775 {"@assembler-with-cpp",
776 "%(trad_capable_cpp) -lang-asm %(cpp_options)\
777 %{!M:%{!MM:%{!E:%(invoke_as)}}}", 0},
778 #include "specs.h"
779 /* Mark end of table */
780 {0, 0, 0}
783 /* Number of elements in default_compilers, not counting the terminator. */
785 static int n_default_compilers
786 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
788 /* A vector of options to give to the linker.
789 These options are accumulated by %x,
790 and substituted into the linker command with %X. */
791 static int n_linker_options;
792 static char **linker_options;
794 /* A vector of options to give to the assembler.
795 These options are accumulated by -Wa,
796 and substituted into the assembler command with %Y. */
797 static int n_assembler_options;
798 static char **assembler_options;
800 /* A vector of options to give to the preprocessor.
801 These options are accumulated by -Wp,
802 and substituted into the preprocessor command with %Z. */
803 static int n_preprocessor_options;
804 static char **preprocessor_options;
806 /* Define how to map long options into short ones. */
808 /* This structure describes one mapping. */
809 struct option_map
811 /* The long option's name. */
812 const char *name;
813 /* The equivalent short option. */
814 const char *equivalent;
815 /* Argument info. A string of flag chars; NULL equals no options.
816 a => argument required.
817 o => argument optional.
818 j => join argument to equivalent, making one word.
819 * => require other text after NAME as an argument. */
820 const char *arg_info;
823 /* This is the table of mappings. Mappings are tried sequentially
824 for each option encountered; the first one that matches, wins. */
826 struct option_map option_map[] =
828 {"--all-warnings", "-Wall", 0},
829 {"--ansi", "-ansi", 0},
830 {"--assemble", "-S", 0},
831 {"--assert", "-A", "a"},
832 {"--classpath", "-fclasspath=", "aj"},
833 {"--CLASSPATH", "-fCLASSPATH=", "aj"},
834 {"--comments", "-C", 0},
835 {"--compile", "-c", 0},
836 {"--debug", "-g", "oj"},
837 {"--define-macro", "-D", "aj"},
838 {"--dependencies", "-M", 0},
839 {"--dump", "-d", "a"},
840 {"--dumpbase", "-dumpbase", "a"},
841 {"--entry", "-e", 0},
842 {"--extra-warnings", "-W", 0},
843 {"--for-assembler", "-Wa", "a"},
844 {"--for-linker", "-Xlinker", "a"},
845 {"--force-link", "-u", "a"},
846 {"--imacros", "-imacros", "a"},
847 {"--include", "-include", "a"},
848 {"--include-barrier", "-I-", 0},
849 {"--include-directory", "-I", "aj"},
850 {"--include-directory-after", "-idirafter", "a"},
851 {"--include-prefix", "-iprefix", "a"},
852 {"--include-with-prefix", "-iwithprefix", "a"},
853 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
854 {"--include-with-prefix-after", "-iwithprefix", "a"},
855 {"--language", "-x", "a"},
856 {"--library-directory", "-L", "a"},
857 {"--machine", "-m", "aj"},
858 {"--machine-", "-m", "*j"},
859 {"--no-line-commands", "-P", 0},
860 {"--no-precompiled-includes", "-noprecomp", 0},
861 {"--no-standard-includes", "-nostdinc", 0},
862 {"--no-standard-libraries", "-nostdlib", 0},
863 {"--no-warnings", "-w", 0},
864 {"--optimize", "-O", "oj"},
865 {"--output", "-o", "a"},
866 {"--output-class-directory", "-foutput-class-dir=", "ja"},
867 {"--param", "--param", "a"},
868 {"--pedantic", "-pedantic", 0},
869 {"--pedantic-errors", "-pedantic-errors", 0},
870 {"--pipe", "-pipe", 0},
871 {"--prefix", "-B", "a"},
872 {"--preprocess", "-E", 0},
873 {"--print-search-dirs", "-print-search-dirs", 0},
874 {"--print-file-name", "-print-file-name=", "aj"},
875 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
876 {"--print-missing-file-dependencies", "-MG", 0},
877 {"--print-multi-lib", "-print-multi-lib", 0},
878 {"--print-multi-directory", "-print-multi-directory", 0},
879 {"--print-prog-name", "-print-prog-name=", "aj"},
880 {"--profile", "-p", 0},
881 {"--profile-blocks", "-a", 0},
882 {"--quiet", "-q", 0},
883 {"--save-temps", "-save-temps", 0},
884 {"--shared", "-shared", 0},
885 {"--silent", "-q", 0},
886 {"--specs", "-specs=", "aj"},
887 {"--static", "-static", 0},
888 {"--std", "-std=", "aj"},
889 {"--symbolic", "-symbolic", 0},
890 {"--target", "-b", "a"},
891 {"--time", "-time", 0},
892 {"--trace-includes", "-H", 0},
893 {"--traditional", "-traditional", 0},
894 {"--traditional-cpp", "-traditional-cpp", 0},
895 {"--trigraphs", "-trigraphs", 0},
896 {"--undefine-macro", "-U", "aj"},
897 {"--use-version", "-V", "a"},
898 {"--user-dependencies", "-MM", 0},
899 {"--verbose", "-v", 0},
900 {"--version", "-dumpversion", 0},
901 {"--warn-", "-W", "*j"},
902 {"--write-dependencies", "-MD", 0},
903 {"--write-user-dependencies", "-MMD", 0},
904 {"--", "-f", "*j"}
907 /* Translate the options described by *ARGCP and *ARGVP.
908 Make a new vector and store it back in *ARGVP,
909 and store its length in *ARGVC. */
911 static void
912 translate_options (argcp, argvp)
913 int *argcp;
914 const char *const **argvp;
916 int i;
917 int argc = *argcp;
918 const char *const *argv = *argvp;
919 const char **newv =
920 (const char **) xmalloc ((argc + 2) * 2 * sizeof (const char *));
921 int newindex = 0;
923 i = 0;
924 newv[newindex++] = argv[i++];
926 while (i < argc)
928 /* Translate -- options. */
929 if (argv[i][0] == '-' && argv[i][1] == '-')
931 size_t j;
932 /* Find a mapping that applies to this option. */
933 for (j = 0; j < ARRAY_SIZE (option_map); j++)
935 size_t optlen = strlen (option_map[j].name);
936 size_t arglen = strlen (argv[i]);
937 size_t complen = arglen > optlen ? optlen : arglen;
938 const char *arginfo = option_map[j].arg_info;
940 if (arginfo == 0)
941 arginfo = "";
943 if (!strncmp (argv[i], option_map[j].name, complen))
945 const char *arg = 0;
947 if (arglen < optlen)
949 size_t k;
950 for (k = j + 1; k < ARRAY_SIZE (option_map); k++)
951 if (strlen (option_map[k].name) >= arglen
952 && !strncmp (argv[i], option_map[k].name, arglen))
954 error ("Ambiguous abbreviation %s", argv[i]);
955 break;
958 if (k != ARRAY_SIZE (option_map))
959 break;
962 if (arglen > optlen)
964 /* If the option has an argument, accept that. */
965 if (argv[i][optlen] == '=')
966 arg = argv[i] + optlen + 1;
968 /* If this mapping requires extra text at end of name,
969 accept that as "argument". */
970 else if (strchr (arginfo, '*') != 0)
971 arg = argv[i] + optlen;
973 /* Otherwise, extra text at end means mismatch.
974 Try other mappings. */
975 else
976 continue;
979 else if (strchr (arginfo, '*') != 0)
981 error ("Incomplete `%s' option", option_map[j].name);
982 break;
985 /* Handle arguments. */
986 if (strchr (arginfo, 'a') != 0)
988 if (arg == 0)
990 if (i + 1 == argc)
992 error ("Missing argument to `%s' option",
993 option_map[j].name);
994 break;
997 arg = argv[++i];
1000 else if (strchr (arginfo, '*') != 0)
1002 else if (strchr (arginfo, 'o') == 0)
1004 if (arg != 0)
1005 error ("Extraneous argument to `%s' option",
1006 option_map[j].name);
1007 arg = 0;
1010 /* Store the translation as one argv elt or as two. */
1011 if (arg != 0 && strchr (arginfo, 'j') != 0)
1012 newv[newindex++] = concat (option_map[j].equivalent, arg,
1013 NULL_PTR);
1014 else if (arg != 0)
1016 newv[newindex++] = option_map[j].equivalent;
1017 newv[newindex++] = arg;
1019 else
1020 newv[newindex++] = option_map[j].equivalent;
1022 break;
1025 i++;
1028 /* Handle old-fashioned options--just copy them through,
1029 with their arguments. */
1030 else if (argv[i][0] == '-')
1032 const char *p = argv[i] + 1;
1033 int c = *p;
1034 int nskip = 1;
1036 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
1037 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
1038 else if (WORD_SWITCH_TAKES_ARG (p))
1039 nskip += WORD_SWITCH_TAKES_ARG (p);
1040 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
1041 && p[1] == 0)
1042 nskip += 1;
1043 else if (! strcmp (p, "Xlinker"))
1044 nskip += 1;
1046 /* Watch out for an option at the end of the command line that
1047 is missing arguments, and avoid skipping past the end of the
1048 command line. */
1049 if (nskip + i > argc)
1050 nskip = argc - i;
1052 while (nskip > 0)
1054 newv[newindex++] = argv[i++];
1055 nskip--;
1058 else
1059 /* Ordinary operands, or +e options. */
1060 newv[newindex++] = argv[i++];
1063 newv[newindex] = 0;
1065 *argvp = newv;
1066 *argcp = newindex;
1069 static char *
1070 skip_whitespace (p)
1071 char *p;
1073 while (1)
1075 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1076 be considered whitespace. */
1077 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1078 return p + 1;
1079 else if (*p == '\n' || *p == ' ' || *p == '\t')
1080 p++;
1081 else if (*p == '#')
1083 while (*p != '\n')
1084 p++;
1085 p++;
1087 else
1088 break;
1091 return p;
1093 /* Structures to keep track of prefixes to try when looking for files. */
1095 struct prefix_list
1097 char *prefix; /* String to prepend to the path. */
1098 struct prefix_list *next; /* Next in linked list. */
1099 int require_machine_suffix; /* Don't use without machine_suffix. */
1100 /* 2 means try both machine_suffix and just_machine_suffix. */
1101 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1102 int priority; /* Sort key - priority within list */
1105 struct path_prefix
1107 struct prefix_list *plist; /* List of prefixes to try */
1108 int max_len; /* Max length of a prefix in PLIST */
1109 const char *name; /* Name of this list (used in config stuff) */
1112 /* List of prefixes to try when looking for executables. */
1114 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1116 /* List of prefixes to try when looking for startup (crt0) files. */
1118 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1120 /* List of prefixes to try when looking for include files. */
1122 static struct path_prefix include_prefixes = { 0, 0, "include" };
1124 /* Suffix to attach to directories searched for commands.
1125 This looks like `MACHINE/VERSION/'. */
1127 static const char *machine_suffix = 0;
1129 /* Suffix to attach to directories searched for commands.
1130 This is just `MACHINE/'. */
1132 static const char *just_machine_suffix = 0;
1134 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1136 static const char *gcc_exec_prefix;
1138 /* Default prefixes to attach to command names. */
1140 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1141 #undef MD_EXEC_PREFIX
1142 #undef MD_STARTFILE_PREFIX
1143 #undef MD_STARTFILE_PREFIX_1
1144 #endif
1146 /* If no prefixes defined, use the null string, which will disable them. */
1147 #ifndef MD_EXEC_PREFIX
1148 #define MD_EXEC_PREFIX ""
1149 #endif
1150 #ifndef MD_STARTFILE_PREFIX
1151 #define MD_STARTFILE_PREFIX ""
1152 #endif
1153 #ifndef MD_STARTFILE_PREFIX_1
1154 #define MD_STARTFILE_PREFIX_1 ""
1155 #endif
1157 /* Supply defaults for the standard prefixes. */
1159 #ifndef STANDARD_EXEC_PREFIX
1160 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1161 #endif
1162 #ifndef STANDARD_STARTFILE_PREFIX
1163 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1164 #endif
1165 #ifndef TOOLDIR_BASE_PREFIX
1166 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1167 #endif
1168 #ifndef STANDARD_BINDIR_PREFIX
1169 #define STANDARD_BINDIR_PREFIX "/usr/local/bin"
1170 #endif
1172 static const char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1173 static const char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1174 static const char *md_exec_prefix = MD_EXEC_PREFIX;
1176 static const char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1177 static const char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1178 static const char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1179 static const char *standard_startfile_prefix_1 = "/lib/";
1180 static const char *standard_startfile_prefix_2 = "/usr/lib/";
1182 static const char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1183 static const char *tooldir_prefix;
1185 static const char *standard_bindir_prefix = STANDARD_BINDIR_PREFIX;
1187 /* Subdirectory to use for locating libraries. Set by
1188 set_multilib_dir based on the compilation options. */
1190 static const char *multilib_dir;
1192 /* Structure to keep track of the specs that have been defined so far.
1193 These are accessed using %(specname) or %[specname] in a compiler
1194 or link spec. */
1196 struct spec_list
1198 /* The following 2 fields must be first */
1199 /* to allow EXTRA_SPECS to be initialized */
1200 const char *name; /* name of the spec. */
1201 const char *ptr; /* available ptr if no static pointer */
1203 /* The following fields are not initialized */
1204 /* by EXTRA_SPECS */
1205 const char **ptr_spec; /* pointer to the spec itself. */
1206 struct spec_list *next; /* Next spec in linked list. */
1207 int name_len; /* length of the name */
1208 int alloc_p; /* whether string was allocated */
1211 #define INIT_STATIC_SPEC(NAME,PTR) \
1212 { NAME, NULL_PTR, PTR, (struct spec_list *) 0, sizeof (NAME) - 1, 0 }
1214 /* List of statically defined specs. */
1215 static struct spec_list static_specs[] =
1217 INIT_STATIC_SPEC ("asm", &asm_spec),
1218 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1219 INIT_STATIC_SPEC ("asm_options", &asm_options),
1220 INIT_STATIC_SPEC ("invoke_as", &invoke_as),
1221 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1222 INIT_STATIC_SPEC ("cpp_options", &cpp_options),
1223 INIT_STATIC_SPEC ("trad_capable_cpp", &trad_capable_cpp),
1224 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1225 INIT_STATIC_SPEC ("cc1_options", &cc1_options),
1226 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1227 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1228 INIT_STATIC_SPEC ("link", &link_spec),
1229 INIT_STATIC_SPEC ("lib", &lib_spec),
1230 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1231 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1232 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1233 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1234 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1235 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1236 INIT_STATIC_SPEC ("version", &compiler_version),
1237 INIT_STATIC_SPEC ("multilib", &multilib_select),
1238 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1239 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1240 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1241 INIT_STATIC_SPEC ("multilib_exclusions", &multilib_exclusions),
1242 INIT_STATIC_SPEC ("linker", &linker_name_spec),
1243 INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
1244 INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
1245 INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
1246 INIT_STATIC_SPEC ("md_startfile_prefix_1", &md_startfile_prefix_1),
1249 #ifdef EXTRA_SPECS /* additional specs needed */
1250 /* Structure to keep track of just the first two args of a spec_list.
1251 That is all that the EXTRA_SPECS macro gives us. */
1252 struct spec_list_1
1254 const char *name;
1255 const char *ptr;
1258 static struct spec_list_1 extra_specs_1[] = { EXTRA_SPECS };
1259 static struct spec_list *extra_specs = (struct spec_list *) 0;
1260 #endif
1262 /* List of dynamically allocates specs that have been defined so far. */
1264 static struct spec_list *specs = (struct spec_list *) 0;
1266 /* Add appropriate libgcc specs to OBSTACK, taking into account
1267 various permutations of -shared-libgcc, -shared, and such. */
1269 static void
1270 init_gcc_specs (obstack, shared_name, static_name)
1271 struct obstack *obstack;
1272 const char *shared_name;
1273 const char *static_name;
1275 char buffer[128];
1277 /* If we see -shared-libgcc, then use the shared version. */
1278 sprintf (buffer, "%%{shared-libgcc:%s}", shared_name);
1279 obstack_grow (obstack, buffer, strlen (buffer));
1280 /* If we see -static-libgcc, then use the static version. */
1281 sprintf (buffer, "%%{static-libgcc:%s}", static_name);
1282 obstack_grow (obstack, buffer, strlen (buffer));
1283 /* Otherwise, if we see -shared, then use the shared version. */
1284 sprintf (buffer,
1285 "%%{!shared-libgcc:%%{!static-libgcc:%%{shared:%s}}}",
1286 shared_name);
1287 obstack_grow (obstack, buffer, strlen (buffer));
1288 /* Otherwise, use the static version. */
1289 sprintf (buffer,
1290 "%%{!shared-libgcc:%%{!static-libgcc:%%{!shared:%s}}}",
1291 static_name);
1292 obstack_grow (obstack, buffer, strlen (buffer));
1295 /* Initialize the specs lookup routines. */
1297 static void
1298 init_spec ()
1300 struct spec_list *next = (struct spec_list *) 0;
1301 struct spec_list *sl = (struct spec_list *) 0;
1302 int i;
1304 if (specs)
1305 return; /* Already initialized. */
1307 if (verbose_flag)
1308 notice ("Using builtin specs.\n");
1310 #ifdef EXTRA_SPECS
1311 extra_specs = (struct spec_list *)
1312 xcalloc (sizeof (struct spec_list), ARRAY_SIZE (extra_specs_1));
1314 for (i = ARRAY_SIZE (extra_specs_1) - 1; i >= 0; i--)
1316 sl = &extra_specs[i];
1317 sl->name = extra_specs_1[i].name;
1318 sl->ptr = extra_specs_1[i].ptr;
1319 sl->next = next;
1320 sl->name_len = strlen (sl->name);
1321 sl->ptr_spec = &sl->ptr;
1322 next = sl;
1324 #endif
1326 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1328 sl = &static_specs[i];
1329 sl->next = next;
1330 next = sl;
1333 #ifdef ENABLE_SHARED_LIBGCC
1334 /* ??? If neither -shared-libgcc nor --static-libgcc was
1335 seen, then we should be making an educated guess. Some proposed
1336 heuristics for ELF include:
1338 (1) If "-Wl,--export-dynamic", then it's a fair bet that the
1339 program will be doing dynamic loading, which will likely
1340 need the shared libgcc.
1342 (2) If "-ldl", then it's also a fair bet that we're doing
1343 dynamic loading.
1345 (3) For each ET_DYN we're linking against (either through -lfoo
1346 or /some/path/foo.so), check to see whether it or one of
1347 its dependancies depends on a shared libgcc.
1349 (4) If "-shared"
1351 If the runtime is fixed to look for program headers instead
1352 of calling __register_frame_info at all, for each object,
1353 use the shared libgcc if any EH symbol referenced.
1355 If crtstuff is fixed to not invoke __register_frame_info
1356 automatically, for each object, use the shared libgcc if
1357 any non-empty unwind section found.
1359 Doing any of this probably requires invoking an external program to
1360 do the actual object file scanning. */
1362 const char *p = libgcc_spec;
1363 int in_sep = 1;
1365 /* Transform the extant libgcc_spec into one that uses the shared libgcc
1366 when given the proper command line arguments. */
1367 while (*p)
1369 if (in_sep && *p == '-' && strncmp (p, "-lgcc", 5) == 0)
1371 init_gcc_specs (&obstack,
1372 #ifdef NO_SHARED_LIBGCC_MULTILIB
1373 "-lgcc_s"
1374 #else
1375 "-lgcc_s%M"
1376 #endif
1378 "-lgcc");
1379 p += 5;
1380 in_sep = 0;
1382 else if (in_sep && *p == 'l' && strncmp (p, "libgcc.a%s", 10) == 0)
1384 /* Ug. We don't know shared library extensions. Hope that
1385 systems that use this form don't do shared libraries. */
1386 init_gcc_specs (&obstack,
1387 #ifdef NO_SHARED_LIBGCC_MULTILIB
1388 "-lgcc_s"
1389 #else
1390 "-lgcc_s%M"
1391 #endif
1393 "libgcc.a%s");
1394 p += 10;
1395 in_sep = 0;
1397 else
1399 obstack_1grow (&obstack, *p);
1400 in_sep = (*p == ' ');
1401 p += 1;
1405 obstack_1grow (&obstack, '\0');
1406 libgcc_spec = obstack_finish (&obstack);
1408 #endif
1410 specs = sl;
1413 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1414 removed; If the spec starts with a + then SPEC is added to the end of the
1415 current spec. */
1417 static void
1418 set_spec (name, spec)
1419 const char *name;
1420 const char *spec;
1422 struct spec_list *sl;
1423 const char *old_spec;
1424 int name_len = strlen (name);
1425 int i;
1427 /* If this is the first call, initialize the statically allocated specs. */
1428 if (!specs)
1430 struct spec_list *next = (struct spec_list *) 0;
1431 for (i = ARRAY_SIZE (static_specs) - 1; i >= 0; i--)
1433 sl = &static_specs[i];
1434 sl->next = next;
1435 next = sl;
1437 specs = sl;
1440 /* See if the spec already exists. */
1441 for (sl = specs; sl; sl = sl->next)
1442 if (name_len == sl->name_len && !strcmp (sl->name, name))
1443 break;
1445 if (!sl)
1447 /* Not found - make it. */
1448 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1449 sl->name = xstrdup (name);
1450 sl->name_len = name_len;
1451 sl->ptr_spec = &sl->ptr;
1452 sl->alloc_p = 0;
1453 *(sl->ptr_spec) = "";
1454 sl->next = specs;
1455 specs = sl;
1458 old_spec = *(sl->ptr_spec);
1459 *(sl->ptr_spec) = ((spec[0] == '+' && ISSPACE ((unsigned char)spec[1]))
1460 ? concat (old_spec, spec + 1, NULL_PTR)
1461 : xstrdup (spec));
1463 #ifdef DEBUG_SPECS
1464 if (verbose_flag)
1465 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1466 #endif
1468 /* Free the old spec. */
1469 if (old_spec && sl->alloc_p)
1470 free ((PTR) old_spec);
1472 sl->alloc_p = 1;
1475 /* Accumulate a command (program name and args), and run it. */
1477 /* Vector of pointers to arguments in the current line of specifications. */
1479 static const char **argbuf;
1481 /* Number of elements allocated in argbuf. */
1483 static int argbuf_length;
1485 /* Number of elements in argbuf currently in use (containing args). */
1487 static int argbuf_index;
1489 /* This is the list of suffixes and codes (%g/%u/%U/%j) and the associated
1490 temp file. If the HOST_BIT_BUCKET is used for %j, no entry is made for
1491 it here. */
1493 static struct temp_name {
1494 const char *suffix; /* suffix associated with the code. */
1495 int length; /* strlen (suffix). */
1496 int unique; /* Indicates whether %g or %u/%U was used. */
1497 const char *filename; /* associated filename. */
1498 int filename_length; /* strlen (filename). */
1499 struct temp_name *next;
1500 } *temp_names;
1502 /* Number of commands executed so far. */
1504 static int execution_count;
1506 /* Number of commands that exited with a signal. */
1508 static int signal_count;
1510 /* Name with which this program was invoked. */
1512 static const char *programname;
1514 /* Clear out the vector of arguments (after a command is executed). */
1516 static void
1517 clear_args ()
1519 argbuf_index = 0;
1522 /* Add one argument to the vector at the end.
1523 This is done when a space is seen or at the end of the line.
1524 If DELETE_ALWAYS is nonzero, the arg is a filename
1525 and the file should be deleted eventually.
1526 If DELETE_FAILURE is nonzero, the arg is a filename
1527 and the file should be deleted if this compilation fails. */
1529 static void
1530 store_arg (arg, delete_always, delete_failure)
1531 const char *arg;
1532 int delete_always, delete_failure;
1534 if (argbuf_index + 1 == argbuf_length)
1535 argbuf
1536 = (const char **) xrealloc (argbuf,
1537 (argbuf_length *= 2) * sizeof (const char *));
1539 argbuf[argbuf_index++] = arg;
1540 argbuf[argbuf_index] = 0;
1542 if (delete_always || delete_failure)
1543 record_temp_file (arg, delete_always, delete_failure);
1546 /* Load specs from a file name named FILENAME, replacing occurances of
1547 various different types of line-endings, \r\n, \n\r and just \r, with
1548 a single \n. */
1550 static char *
1551 load_specs (filename)
1552 const char *filename;
1554 int desc;
1555 int readlen;
1556 struct stat statbuf;
1557 char *buffer;
1558 char *buffer_p;
1559 char *specs;
1560 char *specs_p;
1562 if (verbose_flag)
1563 notice ("Reading specs from %s\n", filename);
1565 /* Open and stat the file. */
1566 desc = open (filename, O_RDONLY, 0);
1567 if (desc < 0)
1568 pfatal_with_name (filename);
1569 if (stat (filename, &statbuf) < 0)
1570 pfatal_with_name (filename);
1572 /* Read contents of file into BUFFER. */
1573 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1574 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1575 if (readlen < 0)
1576 pfatal_with_name (filename);
1577 buffer[readlen] = 0;
1578 close (desc);
1580 specs = xmalloc (readlen + 1);
1581 specs_p = specs;
1582 for (buffer_p = buffer; buffer_p && *buffer_p; buffer_p++)
1584 int skip = 0;
1585 char c = *buffer_p;
1586 if (c == '\r')
1588 if (buffer_p > buffer && *(buffer_p - 1) == '\n') /* \n\r */
1589 skip = 1;
1590 else if (*(buffer_p + 1) == '\n') /* \r\n */
1591 skip = 1;
1592 else /* \r */
1593 c = '\n';
1595 if (! skip)
1596 *specs_p++ = c;
1598 *specs_p = '\0';
1600 free (buffer);
1601 return (specs);
1604 /* Read compilation specs from a file named FILENAME,
1605 replacing the default ones.
1607 A suffix which starts with `*' is a definition for
1608 one of the machine-specific sub-specs. The "suffix" should be
1609 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1610 The corresponding spec is stored in asm_spec, etc.,
1611 rather than in the `compilers' vector.
1613 Anything invalid in the file is a fatal error. */
1615 static void
1616 read_specs (filename, main_p)
1617 const char *filename;
1618 int main_p;
1620 char *buffer;
1621 register char *p;
1623 buffer = load_specs (filename);
1625 /* Scan BUFFER for specs, putting them in the vector. */
1626 p = buffer;
1627 while (1)
1629 char *suffix;
1630 char *spec;
1631 char *in, *out, *p1, *p2, *p3;
1633 /* Advance P in BUFFER to the next nonblank nocomment line. */
1634 p = skip_whitespace (p);
1635 if (*p == 0)
1636 break;
1638 /* Is this a special command that starts with '%'? */
1639 /* Don't allow this for the main specs file, since it would
1640 encourage people to overwrite it. */
1641 if (*p == '%' && !main_p)
1643 p1 = p;
1644 while (*p && *p != '\n')
1645 p++;
1647 /* Skip '\n'. */
1648 p++;
1650 if (!strncmp (p1, "%include", sizeof ("%include") - 1)
1651 && (p1[sizeof "%include" - 1] == ' '
1652 || p1[sizeof "%include" - 1] == '\t'))
1654 char *new_filename;
1656 p1 += sizeof ("%include");
1657 while (*p1 == ' ' || *p1 == '\t')
1658 p1++;
1660 if (*p1++ != '<' || p[-2] != '>')
1661 fatal ("specs %%include syntax malformed after %ld characters",
1662 (long) (p1 - buffer + 1));
1664 p[-2] = '\0';
1665 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1666 read_specs (new_filename ? new_filename : p1, FALSE);
1667 continue;
1669 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1670 && (p1[sizeof "%include_noerr" - 1] == ' '
1671 || p1[sizeof "%include_noerr" - 1] == '\t'))
1673 char *new_filename;
1675 p1 += sizeof "%include_noerr";
1676 while (*p1 == ' ' || *p1 == '\t')
1677 p1++;
1679 if (*p1++ != '<' || p[-2] != '>')
1680 fatal ("specs %%include syntax malformed after %ld characters",
1681 (long) (p1 - buffer + 1));
1683 p[-2] = '\0';
1684 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1685 if (new_filename)
1686 read_specs (new_filename, FALSE);
1687 else if (verbose_flag)
1688 notice ("Could not find specs file %s\n", p1);
1689 continue;
1691 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1692 && (p1[sizeof "%rename" - 1] == ' '
1693 || p1[sizeof "%rename" - 1] == '\t'))
1695 int name_len;
1696 struct spec_list *sl;
1698 /* Get original name */
1699 p1 += sizeof "%rename";
1700 while (*p1 == ' ' || *p1 == '\t')
1701 p1++;
1703 if (! ISALPHA ((unsigned char) *p1))
1704 fatal ("specs %%rename syntax malformed after %ld characters",
1705 (long) (p1 - buffer));
1707 p2 = p1;
1708 while (*p2 && !ISSPACE ((unsigned char) *p2))
1709 p2++;
1711 if (*p2 != ' ' && *p2 != '\t')
1712 fatal ("specs %%rename syntax malformed after %ld characters",
1713 (long) (p2 - buffer));
1715 name_len = p2 - p1;
1716 *p2++ = '\0';
1717 while (*p2 == ' ' || *p2 == '\t')
1718 p2++;
1720 if (! ISALPHA ((unsigned char) *p2))
1721 fatal ("specs %%rename syntax malformed after %ld characters",
1722 (long) (p2 - buffer));
1724 /* Get new spec name. */
1725 p3 = p2;
1726 while (*p3 && !ISSPACE ((unsigned char) *p3))
1727 p3++;
1729 if (p3 != p - 1)
1730 fatal ("specs %%rename syntax malformed after %ld characters",
1731 (long) (p3 - buffer));
1732 *p3 = '\0';
1734 for (sl = specs; sl; sl = sl->next)
1735 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1736 break;
1738 if (!sl)
1739 fatal ("specs %s spec was not found to be renamed", p1);
1741 if (strcmp (p1, p2) == 0)
1742 continue;
1744 if (verbose_flag)
1746 notice ("rename spec %s to %s\n", p1, p2);
1747 #ifdef DEBUG_SPECS
1748 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1749 #endif
1752 set_spec (p2, *(sl->ptr_spec));
1753 if (sl->alloc_p)
1754 free ((PTR) *(sl->ptr_spec));
1756 *(sl->ptr_spec) = "";
1757 sl->alloc_p = 0;
1758 continue;
1760 else
1761 fatal ("specs unknown %% command after %ld characters",
1762 (long) (p1 - buffer));
1765 /* Find the colon that should end the suffix. */
1766 p1 = p;
1767 while (*p1 && *p1 != ':' && *p1 != '\n')
1768 p1++;
1770 /* The colon shouldn't be missing. */
1771 if (*p1 != ':')
1772 fatal ("specs file malformed after %ld characters",
1773 (long) (p1 - buffer));
1775 /* Skip back over trailing whitespace. */
1776 p2 = p1;
1777 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1778 p2--;
1780 /* Copy the suffix to a string. */
1781 suffix = save_string (p, p2 - p);
1782 /* Find the next line. */
1783 p = skip_whitespace (p1 + 1);
1784 if (p[1] == 0)
1785 fatal ("specs file malformed after %ld characters",
1786 (long) (p - buffer));
1788 p1 = p;
1789 /* Find next blank line or end of string. */
1790 while (*p1 && !(*p1 == '\n' && (p1[1] == '\n' || p1[1] == '\0')))
1791 p1++;
1793 /* Specs end at the blank line and do not include the newline. */
1794 spec = save_string (p, p1 - p);
1795 p = p1;
1797 /* Delete backslash-newline sequences from the spec. */
1798 in = spec;
1799 out = spec;
1800 while (*in != 0)
1802 if (in[0] == '\\' && in[1] == '\n')
1803 in += 2;
1804 else if (in[0] == '#')
1805 while (*in && *in != '\n')
1806 in++;
1808 else
1809 *out++ = *in++;
1811 *out = 0;
1813 if (suffix[0] == '*')
1815 if (! strcmp (suffix, "*link_command"))
1816 link_command_spec = spec;
1817 else
1818 set_spec (suffix + 1, spec);
1820 else
1822 /* Add this pair to the vector. */
1823 compilers
1824 = ((struct compiler *)
1825 xrealloc (compilers,
1826 (n_compilers + 2) * sizeof (struct compiler)));
1828 compilers[n_compilers].suffix = suffix;
1829 compilers[n_compilers].spec = spec;
1830 n_compilers++;
1831 memset (&compilers[n_compilers], 0, sizeof compilers[n_compilers]);
1834 if (*suffix == 0)
1835 link_command_spec = spec;
1838 if (link_command_spec == 0)
1839 fatal ("spec file has no spec for linking");
1842 /* Record the names of temporary files we tell compilers to write,
1843 and delete them at the end of the run. */
1845 /* This is the common prefix we use to make temp file names.
1846 It is chosen once for each run of this program.
1847 It is substituted into a spec by %g or %j.
1848 Thus, all temp file names contain this prefix.
1849 In practice, all temp file names start with this prefix.
1851 This prefix comes from the envvar TMPDIR if it is defined;
1852 otherwise, from the P_tmpdir macro if that is defined;
1853 otherwise, in /usr/tmp or /tmp;
1854 or finally the current directory if all else fails. */
1856 static const char *temp_filename;
1858 /* Length of the prefix. */
1860 static int temp_filename_length;
1862 /* Define the list of temporary files to delete. */
1864 struct temp_file
1866 const char *name;
1867 struct temp_file *next;
1870 /* Queue of files to delete on success or failure of compilation. */
1871 static struct temp_file *always_delete_queue;
1872 /* Queue of files to delete on failure of compilation. */
1873 static struct temp_file *failure_delete_queue;
1875 /* Record FILENAME as a file to be deleted automatically.
1876 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1877 otherwise delete it in any case.
1878 FAIL_DELETE nonzero means delete it if a compilation step fails;
1879 otherwise delete it in any case. */
1881 void
1882 record_temp_file (filename, always_delete, fail_delete)
1883 const char *filename;
1884 int always_delete;
1885 int fail_delete;
1887 register char *const name = xstrdup (filename);
1889 if (always_delete)
1891 register struct temp_file *temp;
1892 for (temp = always_delete_queue; temp; temp = temp->next)
1893 if (! strcmp (name, temp->name))
1894 goto already1;
1896 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1897 temp->next = always_delete_queue;
1898 temp->name = name;
1899 always_delete_queue = temp;
1901 already1:;
1904 if (fail_delete)
1906 register struct temp_file *temp;
1907 for (temp = failure_delete_queue; temp; temp = temp->next)
1908 if (! strcmp (name, temp->name))
1909 goto already2;
1911 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1912 temp->next = failure_delete_queue;
1913 temp->name = name;
1914 failure_delete_queue = temp;
1916 already2:;
1920 /* Delete all the temporary files whose names we previously recorded. */
1922 static void
1923 delete_if_ordinary (name)
1924 const char *name;
1926 struct stat st;
1927 #ifdef DEBUG
1928 int i, c;
1930 printf ("Delete %s? (y or n) ", name);
1931 fflush (stdout);
1932 i = getchar ();
1933 if (i != '\n')
1934 while ((c = getchar ()) != '\n' && c != EOF)
1937 if (i == 'y' || i == 'Y')
1938 #endif /* DEBUG */
1939 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1940 if (unlink (name) < 0)
1941 if (verbose_flag)
1942 perror_with_name (name);
1945 static void
1946 delete_temp_files ()
1948 register struct temp_file *temp;
1950 for (temp = always_delete_queue; temp; temp = temp->next)
1951 delete_if_ordinary (temp->name);
1952 always_delete_queue = 0;
1955 /* Delete all the files to be deleted on error. */
1957 static void
1958 delete_failure_queue ()
1960 register struct temp_file *temp;
1962 for (temp = failure_delete_queue; temp; temp = temp->next)
1963 delete_if_ordinary (temp->name);
1966 static void
1967 clear_failure_queue ()
1969 failure_delete_queue = 0;
1972 /* Routine to add variables to the environment. We do this to pass
1973 the pathname of the gcc driver, and the directories search to the
1974 collect2 program, which is being run as ld. This way, we can be
1975 sure of executing the right compiler when collect2 wants to build
1976 constructors and destructors. Since the environment variables we
1977 use come from an obstack, we don't have to worry about allocating
1978 space for them. */
1980 #ifndef HAVE_PUTENV
1982 void
1983 putenv (str)
1984 char *str;
1986 #ifndef VMS /* nor about VMS */
1988 extern char **environ;
1989 char **old_environ = environ;
1990 char **envp;
1991 int num_envs = 0;
1992 int name_len = 1;
1993 int str_len = strlen (str);
1994 char *p = str;
1995 int ch;
1997 while ((ch = *p++) != '\0' && ch != '=')
1998 name_len++;
2000 if (!ch)
2001 abort ();
2003 /* Search for replacing an existing environment variable, and
2004 count the number of total environment variables. */
2005 for (envp = old_environ; *envp; envp++)
2007 num_envs++;
2008 if (!strncmp (str, *envp, name_len))
2010 *envp = str;
2011 return;
2015 /* Add a new environment variable */
2016 environ = (char **) xmalloc (sizeof (char *) * (num_envs + 2));
2017 *environ = str;
2018 memcpy ((char *) (environ + 1), (char *) old_environ,
2019 sizeof (char *) * (num_envs + 1));
2021 #endif /* VMS */
2024 #endif /* HAVE_PUTENV */
2026 /* Build a list of search directories from PATHS.
2027 PREFIX is a string to prepend to the list.
2028 If CHECK_DIR_P is non-zero we ensure the directory exists.
2029 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
2030 It is also used by the --print-search-dirs flag. */
2032 static char *
2033 build_search_list (paths, prefix, check_dir_p)
2034 struct path_prefix *paths;
2035 const char *prefix;
2036 int check_dir_p;
2038 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
2039 int just_suffix_len
2040 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
2041 int first_time = TRUE;
2042 struct prefix_list *pprefix;
2044 obstack_grow (&collect_obstack, prefix, strlen (prefix));
2045 obstack_1grow (&collect_obstack, '=');
2047 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
2049 int len = strlen (pprefix->prefix);
2051 if (machine_suffix
2052 && (! check_dir_p
2053 || is_directory (pprefix->prefix, machine_suffix, 0)))
2055 if (!first_time)
2056 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2058 first_time = FALSE;
2059 obstack_grow (&collect_obstack, pprefix->prefix, len);
2060 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
2063 if (just_machine_suffix
2064 && pprefix->require_machine_suffix == 2
2065 && (! check_dir_p
2066 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
2068 if (! first_time)
2069 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2071 first_time = FALSE;
2072 obstack_grow (&collect_obstack, pprefix->prefix, len);
2073 obstack_grow (&collect_obstack, just_machine_suffix,
2074 just_suffix_len);
2077 if (! pprefix->require_machine_suffix)
2079 if (! first_time)
2080 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
2082 first_time = FALSE;
2083 obstack_grow (&collect_obstack, pprefix->prefix, len);
2087 obstack_1grow (&collect_obstack, '\0');
2088 return obstack_finish (&collect_obstack);
2091 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
2092 for collect. */
2094 static void
2095 putenv_from_prefixes (paths, env_var)
2096 struct path_prefix *paths;
2097 const char *env_var;
2099 putenv (build_search_list (paths, env_var, 1));
2102 #ifndef VMS
2104 /* FIXME: the location independence code for VMS is hairier than this,
2105 and hasn't been written. */
2107 /* Split a filename into component directories. */
2109 static char **
2110 split_directories (name, ptr_num_dirs)
2111 const char *name;
2112 int *ptr_num_dirs;
2114 int num_dirs = 0;
2115 char **dirs;
2116 const char *p, *q;
2117 int ch;
2119 /* Count the number of directories. Special case MSDOS disk names as part
2120 of the initial directory. */
2121 p = name;
2122 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2123 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2125 p += 3;
2126 num_dirs++;
2128 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2130 while ((ch = *p++) != '\0')
2132 if (IS_DIR_SEPARATOR (ch))
2134 num_dirs++;
2135 while (IS_DIR_SEPARATOR (*p))
2136 p++;
2140 dirs = (char **) xmalloc (sizeof (char *) * (num_dirs + 2));
2142 /* Now copy the directory parts. */
2143 num_dirs = 0;
2144 p = name;
2145 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
2146 if (name[1] == ':' && IS_DIR_SEPARATOR (name[2]))
2148 dirs[num_dirs++] = save_string (p, 3);
2149 p += 3;
2151 #endif /* HAVE_DOS_BASED_FILE_SYSTEM */
2153 q = p;
2154 while ((ch = *p++) != '\0')
2156 if (IS_DIR_SEPARATOR (ch))
2158 while (IS_DIR_SEPARATOR (*p))
2159 p++;
2161 dirs[num_dirs++] = save_string (q, p - q);
2162 q = p;
2166 if (p - 1 - q > 0)
2167 dirs[num_dirs++] = save_string (q, p - 1 - q);
2169 dirs[num_dirs] = NULL_PTR;
2170 if (ptr_num_dirs)
2171 *ptr_num_dirs = num_dirs;
2173 return dirs;
2176 /* Release storage held by split directories. */
2178 static void
2179 free_split_directories (dirs)
2180 char **dirs;
2182 int i = 0;
2184 while (dirs[i] != NULL_PTR)
2185 free (dirs[i++]);
2187 free ((char *) dirs);
2190 /* Given three strings PROGNAME, BIN_PREFIX, PREFIX, return a string that gets
2191 to PREFIX starting with the directory portion of PROGNAME and a relative
2192 pathname of the difference between BIN_PREFIX and PREFIX.
2194 For example, if BIN_PREFIX is /alpha/beta/gamma/gcc/delta, PREFIX is
2195 /alpha/beta/gamma/omega/, and PROGNAME is /red/green/blue/gcc, then this
2196 function will return /red/green/blue/../omega.
2198 If no relative prefix can be found, return NULL. */
2200 static char *
2201 make_relative_prefix (progname, bin_prefix, prefix)
2202 const char *progname;
2203 const char *bin_prefix;
2204 const char *prefix;
2206 char **prog_dirs, **bin_dirs, **prefix_dirs;
2207 int prog_num, bin_num, prefix_num, std_loc_p;
2208 int i, n, common;
2210 prog_dirs = split_directories (progname, &prog_num);
2211 bin_dirs = split_directories (bin_prefix, &bin_num);
2213 /* If there is no full pathname, try to find the program by checking in each
2214 of the directories specified in the PATH environment variable. */
2215 if (prog_num == 1)
2217 char *temp;
2219 GET_ENV_PATH_LIST (temp, "PATH");
2220 if (temp)
2222 char *startp, *endp;
2223 char *nstore = (char *) alloca (strlen (temp) + strlen (progname) + 1);
2225 startp = endp = temp;
2226 while (1)
2228 if (*endp == PATH_SEPARATOR || *endp == 0)
2230 if (endp == startp)
2232 nstore[0] = '.';
2233 nstore[1] = DIR_SEPARATOR;
2234 nstore[2] = '\0';
2236 else
2238 strncpy (nstore, startp, endp - startp);
2239 if (! IS_DIR_SEPARATOR (endp[-1]))
2241 nstore[endp - startp] = DIR_SEPARATOR;
2242 nstore[endp - startp + 1] = 0;
2244 else
2245 nstore[endp - startp] = 0;
2247 strcat (nstore, progname);
2248 if (! access (nstore, X_OK)
2249 #ifdef HAVE_EXECUTABLE_SUFFIX
2250 || ! access (strcat (nstore, EXECUTABLE_SUFFIX), X_OK)
2251 #endif
2254 free_split_directories (prog_dirs);
2255 progname = nstore;
2256 prog_dirs = split_directories (progname, &prog_num);
2257 break;
2260 if (*endp == 0)
2261 break;
2262 endp = startp = endp + 1;
2264 else
2265 endp++;
2270 /* Remove the program name from comparison of directory names. */
2271 prog_num--;
2273 /* Determine if the compiler is installed in the standard location, and if
2274 so, we don't need to specify relative directories. Also, if argv[0]
2275 doesn't contain any directory specifiers, there is not much we can do. */
2276 std_loc_p = 0;
2277 if (prog_num == bin_num)
2279 for (i = 0; i < bin_num; i++)
2281 if (strcmp (prog_dirs[i], bin_dirs[i]) != 0)
2282 break;
2285 if (prog_num <= 0 || i == bin_num)
2287 std_loc_p = 1;
2288 free_split_directories (prog_dirs);
2289 free_split_directories (bin_dirs);
2290 prog_dirs = bin_dirs = (char **) 0;
2291 return NULL_PTR;
2295 prefix_dirs = split_directories (prefix, &prefix_num);
2297 /* Find how many directories are in common between bin_prefix & prefix. */
2298 n = (prefix_num < bin_num) ? prefix_num : bin_num;
2299 for (common = 0; common < n; common++)
2301 if (strcmp (bin_dirs[common], prefix_dirs[common]) != 0)
2302 break;
2305 /* If there are no common directories, there can be no relative prefix. */
2306 if (common == 0)
2308 free_split_directories (prog_dirs);
2309 free_split_directories (bin_dirs);
2310 free_split_directories (prefix_dirs);
2311 return NULL_PTR;
2314 /* Build up the pathnames in argv[0]. */
2315 for (i = 0; i < prog_num; i++)
2316 obstack_grow (&obstack, prog_dirs[i], strlen (prog_dirs[i]));
2318 /* Now build up the ..'s. */
2319 for (i = common; i < n; i++)
2321 obstack_grow (&obstack, DIR_UP, sizeof (DIR_UP) - 1);
2322 obstack_1grow (&obstack, DIR_SEPARATOR);
2325 /* Put in directories to move over to prefix. */
2326 for (i = common; i < prefix_num; i++)
2327 obstack_grow (&obstack, prefix_dirs[i], strlen (prefix_dirs[i]));
2329 free_split_directories (prog_dirs);
2330 free_split_directories (bin_dirs);
2331 free_split_directories (prefix_dirs);
2333 obstack_1grow (&obstack, '\0');
2334 return obstack_finish (&obstack);
2336 #endif /* VMS */
2338 /* Check whether NAME can be accessed in MODE. This is like access,
2339 except that it never considers directories to be executable. */
2341 static int
2342 access_check (name, mode)
2343 const char *name;
2344 int mode;
2346 if (mode == X_OK)
2348 struct stat st;
2350 if (stat (name, &st) < 0
2351 || S_ISDIR (st.st_mode))
2352 return -1;
2355 return access (name, mode);
2358 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
2359 access to check permissions.
2360 Return 0 if not found, otherwise return its name, allocated with malloc. */
2362 static char *
2363 find_a_file (pprefix, name, mode)
2364 struct path_prefix *pprefix;
2365 const char *name;
2366 int mode;
2368 char *temp;
2369 const char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
2370 struct prefix_list *pl;
2371 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
2373 #ifdef DEFAULT_ASSEMBLER
2374 if (! strcmp (name, "as") && access (DEFAULT_ASSEMBLER, mode) == 0)
2375 return xstrdup (DEFAULT_ASSEMBLER);
2376 #endif
2378 #ifdef DEFAULT_LINKER
2379 if (! strcmp(name, "ld") && access (DEFAULT_LINKER, mode) == 0)
2380 return xstrdup (DEFAULT_LINKER);
2381 #endif
2383 if (machine_suffix)
2384 len += strlen (machine_suffix);
2386 temp = xmalloc (len);
2388 /* Determine the filename to execute (special case for absolute paths). */
2390 if (IS_ABSOLUTE_PATHNAME (name))
2392 if (access (name, mode) == 0)
2394 strcpy (temp, name);
2395 return temp;
2398 else
2399 for (pl = pprefix->plist; pl; pl = pl->next)
2401 if (machine_suffix)
2403 /* Some systems have a suffix for executable files.
2404 So try appending that first. */
2405 if (file_suffix[0] != 0)
2407 strcpy (temp, pl->prefix);
2408 strcat (temp, machine_suffix);
2409 strcat (temp, name);
2410 strcat (temp, file_suffix);
2411 if (access_check (temp, mode) == 0)
2413 if (pl->used_flag_ptr != 0)
2414 *pl->used_flag_ptr = 1;
2415 return temp;
2419 /* Now try just the name. */
2420 strcpy (temp, pl->prefix);
2421 strcat (temp, machine_suffix);
2422 strcat (temp, name);
2423 if (access_check (temp, mode) == 0)
2425 if (pl->used_flag_ptr != 0)
2426 *pl->used_flag_ptr = 1;
2427 return temp;
2431 /* Certain prefixes are tried with just the machine type,
2432 not the version. This is used for finding as, ld, etc. */
2433 if (just_machine_suffix && pl->require_machine_suffix == 2)
2435 /* Some systems have a suffix for executable files.
2436 So try appending that first. */
2437 if (file_suffix[0] != 0)
2439 strcpy (temp, pl->prefix);
2440 strcat (temp, just_machine_suffix);
2441 strcat (temp, name);
2442 strcat (temp, file_suffix);
2443 if (access_check (temp, mode) == 0)
2445 if (pl->used_flag_ptr != 0)
2446 *pl->used_flag_ptr = 1;
2447 return temp;
2451 strcpy (temp, pl->prefix);
2452 strcat (temp, just_machine_suffix);
2453 strcat (temp, name);
2454 if (access_check (temp, mode) == 0)
2456 if (pl->used_flag_ptr != 0)
2457 *pl->used_flag_ptr = 1;
2458 return temp;
2462 /* Certain prefixes can't be used without the machine suffix
2463 when the machine or version is explicitly specified. */
2464 if (! pl->require_machine_suffix)
2466 /* Some systems have a suffix for executable files.
2467 So try appending that first. */
2468 if (file_suffix[0] != 0)
2470 strcpy (temp, pl->prefix);
2471 strcat (temp, name);
2472 strcat (temp, file_suffix);
2473 if (access_check (temp, mode) == 0)
2475 if (pl->used_flag_ptr != 0)
2476 *pl->used_flag_ptr = 1;
2477 return temp;
2481 strcpy (temp, pl->prefix);
2482 strcat (temp, name);
2483 if (access_check (temp, mode) == 0)
2485 if (pl->used_flag_ptr != 0)
2486 *pl->used_flag_ptr = 1;
2487 return temp;
2492 free (temp);
2493 return 0;
2496 /* Ranking of prefixes in the sort list. -B prefixes are put before
2497 all others. */
2499 enum path_prefix_priority
2501 PREFIX_PRIORITY_B_OPT,
2502 PREFIX_PRIORITY_LAST
2505 /* Add an entry for PREFIX in PLIST. The PLIST is kept in assending
2506 order according to PRIORITY. Within each PRIORITY, new entries are
2507 appended.
2509 If WARN is nonzero, we will warn if no file is found
2510 through this prefix. WARN should point to an int
2511 which will be set to 1 if this entry is used.
2513 COMPONENT is the value to be passed to update_path.
2515 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2516 the complete value of machine_suffix.
2517 2 means try both machine_suffix and just_machine_suffix. */
2519 static void
2520 add_prefix (pprefix, prefix, component, priority, require_machine_suffix, warn)
2521 struct path_prefix *pprefix;
2522 const char *prefix;
2523 const char *component;
2524 /* enum prefix_priority */ int priority;
2525 int require_machine_suffix;
2526 int *warn;
2528 struct prefix_list *pl, **prev;
2529 int len;
2531 for (prev = &pprefix->plist;
2532 (*prev) != NULL && (*prev)->priority <= priority;
2533 prev = &(*prev)->next)
2536 /* Keep track of the longest prefix */
2538 prefix = update_path (prefix, component);
2539 len = strlen (prefix);
2540 if (len > pprefix->max_len)
2541 pprefix->max_len = len;
2543 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2544 pl->prefix = save_string (prefix, len);
2545 pl->require_machine_suffix = require_machine_suffix;
2546 pl->used_flag_ptr = warn;
2547 pl->priority = priority;
2548 if (warn)
2549 *warn = 0;
2551 /* Insert after PREV */
2552 pl->next = (*prev);
2553 (*prev) = pl;
2556 /* Execute the command specified by the arguments on the current line of spec.
2557 When using pipes, this includes several piped-together commands
2558 with `|' between them.
2560 Return 0 if successful, -1 if failed. */
2562 static int
2563 execute ()
2565 int i;
2566 int n_commands; /* # of command. */
2567 char *string;
2568 struct command
2570 const char *prog; /* program name. */
2571 const char **argv; /* vector of args. */
2572 int pid; /* pid of process for this command. */
2575 struct command *commands; /* each command buffer with above info. */
2577 /* Count # of piped commands. */
2578 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2579 if (strcmp (argbuf[i], "|") == 0)
2580 n_commands++;
2582 /* Get storage for each command. */
2583 commands = (struct command *) alloca (n_commands * sizeof (struct command));
2585 /* Split argbuf into its separate piped processes,
2586 and record info about each one.
2587 Also search for the programs that are to be run. */
2589 commands[0].prog = argbuf[0]; /* first command. */
2590 commands[0].argv = &argbuf[0];
2591 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2593 if (string)
2594 commands[0].argv[0] = string;
2596 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2597 if (strcmp (argbuf[i], "|") == 0)
2598 { /* each command. */
2599 #if defined (__MSDOS__) || defined (OS2) || defined (VMS)
2600 fatal ("-pipe not supported");
2601 #endif
2602 argbuf[i] = 0; /* termination of command args. */
2603 commands[n_commands].prog = argbuf[i + 1];
2604 commands[n_commands].argv = &argbuf[i + 1];
2605 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2606 if (string)
2607 commands[n_commands].argv[0] = string;
2608 n_commands++;
2611 argbuf[argbuf_index] = 0;
2613 /* If -v, print what we are about to do, and maybe query. */
2615 if (verbose_flag)
2617 /* For help listings, put a blank line between sub-processes. */
2618 if (print_help_list)
2619 fputc ('\n', stderr);
2621 /* Print each piped command as a separate line. */
2622 for (i = 0; i < n_commands; i++)
2624 const char *const *j;
2626 for (j = commands[i].argv; *j; j++)
2627 fprintf (stderr, " %s", *j);
2629 /* Print a pipe symbol after all but the last command. */
2630 if (i + 1 != n_commands)
2631 fprintf (stderr, " |");
2632 fprintf (stderr, "\n");
2634 fflush (stderr);
2635 #ifdef DEBUG
2636 notice ("\nGo ahead? (y or n) ");
2637 fflush (stderr);
2638 i = getchar ();
2639 if (i != '\n')
2640 while (getchar () != '\n')
2643 if (i != 'y' && i != 'Y')
2644 return 0;
2645 #endif /* DEBUG */
2648 /* Run each piped subprocess. */
2650 for (i = 0; i < n_commands; i++)
2652 char *errmsg_fmt, *errmsg_arg;
2653 const char *string = commands[i].argv[0];
2655 /* For some bizarre reason, the second argument of execvp() is
2656 char *const *, not const char *const *. */
2657 commands[i].pid = pexecute (string, (char *const *) commands[i].argv,
2658 programname, temp_filename,
2659 &errmsg_fmt, &errmsg_arg,
2660 ((i == 0 ? PEXECUTE_FIRST : 0)
2661 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2662 | (string == commands[i].prog
2663 ? PEXECUTE_SEARCH : 0)
2664 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2666 if (commands[i].pid == -1)
2667 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2669 if (string != commands[i].prog)
2670 free ((PTR) string);
2673 execution_count++;
2675 /* Wait for all the subprocesses to finish.
2676 We don't care what order they finish in;
2677 we know that N_COMMANDS waits will get them all.
2678 Ignore subprocesses that we don't know about,
2679 since they can be spawned by the process that exec'ed us. */
2682 int ret_code = 0;
2683 #ifdef HAVE_GETRUSAGE
2684 struct timeval d;
2685 double ut = 0.0, st = 0.0;
2686 #endif
2688 for (i = 0; i < n_commands;)
2690 int j;
2691 int status;
2692 int pid;
2694 pid = pwait (commands[i].pid, &status, 0);
2695 if (pid < 0)
2696 abort ();
2698 #ifdef HAVE_GETRUSAGE
2699 if (report_times)
2701 /* getrusage returns the total resource usage of all children
2702 up to now. Copy the previous values into prus, get the
2703 current statistics, then take the difference. */
2705 prus = rus;
2706 getrusage (RUSAGE_CHILDREN, &rus);
2707 d.tv_sec = rus.ru_utime.tv_sec - prus.ru_utime.tv_sec;
2708 d.tv_usec = rus.ru_utime.tv_usec - prus.ru_utime.tv_usec;
2709 ut = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2711 d.tv_sec = rus.ru_stime.tv_sec - prus.ru_stime.tv_sec;
2712 d.tv_usec = rus.ru_stime.tv_usec - prus.ru_stime.tv_usec;
2713 st = (double) d.tv_sec + (double) d.tv_usec / 1.0e6;
2715 #endif
2717 for (j = 0; j < n_commands; j++)
2718 if (commands[j].pid == pid)
2720 i++;
2721 if (WIFSIGNALED (status))
2723 #ifdef SIGPIPE
2724 /* SIGPIPE is a special case. It happens in -pipe mode
2725 when the compiler dies before the preprocessor is
2726 done, or the assembler dies before the compiler is
2727 done. There's generally been an error already, and
2728 this is just fallout. So don't generate another error
2729 unless we would otherwise have succeeded. */
2730 if (WTERMSIG (status) == SIGPIPE
2731 && (signal_count || greatest_status >= MIN_FATAL_STATUS))
2733 else
2734 #endif
2735 fatal ("\
2736 Internal error: %s (program %s)\n\
2737 Please submit a full bug report.\n\
2738 See %s for instructions.",
2739 strsignal (WTERMSIG (status)), commands[j].prog,
2740 GCCBUGURL);
2741 signal_count++;
2742 ret_code = -1;
2744 else if (WIFEXITED (status)
2745 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2747 if (WEXITSTATUS (status) > greatest_status)
2748 greatest_status = WEXITSTATUS (status);
2749 ret_code = -1;
2751 #ifdef HAVE_GETRUSAGE
2752 if (report_times && ut + st != 0)
2753 notice ("# %s %.2f %.2f\n", commands[j].prog, ut, st);
2754 #endif
2755 break;
2758 return ret_code;
2762 /* Find all the switches given to us
2763 and make a vector describing them.
2764 The elements of the vector are strings, one per switch given.
2765 If a switch uses following arguments, then the `part1' field
2766 is the switch itself and the `args' field
2767 is a null-terminated vector containing the following arguments.
2768 The `live_cond' field is:
2769 0 when initialized
2770 1 if the switch is true in a conditional spec,
2771 -1 if false (overridden by a later switch)
2772 -2 if this switch should be ignored (used in %{<S})
2773 The `validated' field is nonzero if any spec has looked at this switch;
2774 if it remains zero at the end of the run, it must be meaningless. */
2776 #define SWITCH_OK 0
2777 #define SWITCH_FALSE -1
2778 #define SWITCH_IGNORE -2
2779 #define SWITCH_LIVE 1
2781 struct switchstr
2783 const char *part1;
2784 const char **args;
2785 int live_cond;
2786 unsigned char validated;
2787 unsigned char ordering;
2790 static struct switchstr *switches;
2792 static int n_switches;
2794 struct infile
2796 const char *name;
2797 const char *language;
2800 /* Also a vector of input files specified. */
2802 static struct infile *infiles;
2804 static int n_infiles;
2806 /* This counts the number of libraries added by lang_specific_driver, so that
2807 we can tell if there were any user supplied any files or libraries. */
2809 static int added_libraries;
2811 /* And a vector of corresponding output files is made up later. */
2813 static const char **outfiles;
2815 /* Used to track if none of the -B paths are used. */
2816 static int warn_B;
2818 /* Used to track if standard path isn't used and -b or -V is specified. */
2819 static int warn_std;
2821 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2822 static int *warn_std_ptr = 0;
2824 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2826 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2827 is true if we should look for an executable suffix as well. */
2829 static char *
2830 convert_filename (name, do_exe)
2831 char *name;
2832 int do_exe;
2834 int i;
2835 int len;
2837 if (name == NULL)
2838 return NULL;
2840 len = strlen (name);
2842 #ifdef HAVE_OBJECT_SUFFIX
2843 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2844 if (len > 2
2845 && name[len - 2] == '.'
2846 && name[len - 1] == 'o')
2848 obstack_grow (&obstack, name, len - 2);
2849 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2850 name = obstack_finish (&obstack);
2852 #endif
2854 #if defined(HAVE_EXECUTABLE_SUFFIX) && !defined(NO_AUTO_EXE_SUFFIX)
2855 /* If there is no filetype, make it the executable suffix (which includes
2856 the "."). But don't get confused if we have just "-o". */
2857 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2858 return name;
2860 for (i = len - 1; i >= 0; i--)
2861 if (IS_DIR_SEPARATOR (name[i]))
2862 break;
2864 for (i++; i < len; i++)
2865 if (name[i] == '.')
2866 return name;
2868 obstack_grow (&obstack, name, len);
2869 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2870 name = obstack_finish (&obstack);
2871 #endif
2873 return name;
2875 #endif
2877 /* Display the command line switches accepted by gcc. */
2878 static void
2879 display_help ()
2881 printf (_("Usage: %s [options] file...\n"), programname);
2882 fputs (_("Options:\n"), stdout);
2884 fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
2885 fputs (_(" --help Display this information\n"), stdout);
2886 fputs (_(" --target-help Display target specific command line options\n"), stdout);
2887 if (! verbose_flag)
2888 fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
2889 fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
2890 fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
2891 fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
2892 fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
2893 fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
2894 fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
2895 fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
2896 fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
2897 fputs (_("\
2898 -print-multi-lib Display the mapping between command line options and\n\
2899 multiple library search directories\n"), stdout);
2900 fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
2901 fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
2902 fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
2903 fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
2904 fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
2905 fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
2906 fputs (_(" -time Time the execution of each subprocess\n"), stdout);
2907 fputs (_(" -specs=<file> Override builtin specs with the contents of <file>\n"), stdout);
2908 fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
2909 fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
2910 fputs (_(" -b <machine> Run gcc for target <machine>, if installed\n"), stdout);
2911 fputs (_(" -V <version> Run gcc version number <version>, if installed\n"), stdout);
2912 fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
2913 fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
2914 fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
2915 fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
2916 fputs (_(" -o <file> Place the output into <file>\n"), stdout);
2917 fputs (_("\
2918 -x <language> Specify the language of the following input files\n\
2919 Permissable languages include: c c++ assembler none\n\
2920 'none' means revert to the default behaviour of\n\
2921 guessing the language based on the file's extension\n\
2922 "), stdout);
2924 printf (_("\
2925 \nOptions starting with -g, -f, -m, -O, -W, or --param are automatically\n\
2926 passed on to the various sub-processes invoked by %s. In order to pass\n\
2927 other options on to these processes the -W<letter> options must be used.\n\
2928 "), programname);
2930 /* The rest of the options are displayed by invocations of the various
2931 sub-processes. */
2934 static void
2935 add_preprocessor_option (option, len)
2936 const char *option;
2937 int len;
2939 n_preprocessor_options++;
2941 if (! preprocessor_options)
2942 preprocessor_options
2943 = (char **) xmalloc (n_preprocessor_options * sizeof (char *));
2944 else
2945 preprocessor_options
2946 = (char **) xrealloc (preprocessor_options,
2947 n_preprocessor_options * sizeof (char *));
2949 preprocessor_options [n_preprocessor_options - 1] =
2950 save_string (option, len);
2953 static void
2954 add_assembler_option (option, len)
2955 const char *option;
2956 int len;
2958 n_assembler_options++;
2960 if (! assembler_options)
2961 assembler_options
2962 = (char **) xmalloc (n_assembler_options * sizeof (char *));
2963 else
2964 assembler_options
2965 = (char **) xrealloc (assembler_options,
2966 n_assembler_options * sizeof (char *));
2968 assembler_options [n_assembler_options - 1] = save_string (option, len);
2971 static void
2972 add_linker_option (option, len)
2973 const char *option;
2974 int len;
2976 n_linker_options++;
2978 if (! linker_options)
2979 linker_options
2980 = (char **) xmalloc (n_linker_options * sizeof (char *));
2981 else
2982 linker_options
2983 = (char **) xrealloc (linker_options,
2984 n_linker_options * sizeof (char *));
2986 linker_options [n_linker_options - 1] = save_string (option, len);
2989 /* Create the vector `switches' and its contents.
2990 Store its length in `n_switches'. */
2992 static void
2993 process_command (argc, argv)
2994 int argc;
2995 const char *const *argv;
2997 register int i;
2998 const char *temp;
2999 char *temp1;
3000 const char *spec_lang = 0;
3001 int last_language_n_infiles;
3002 int have_c = 0;
3003 int have_o = 0;
3004 int lang_n_infiles = 0;
3005 #ifdef MODIFY_TARGET_NAME
3006 int is_modify_target_name;
3007 int j;
3008 #endif
3010 GET_ENV_PATH_LIST (gcc_exec_prefix, "GCC_EXEC_PREFIX");
3012 n_switches = 0;
3013 n_infiles = 0;
3014 added_libraries = 0;
3016 /* Figure compiler version from version string. */
3018 compiler_version = temp1 = xstrdup (version_string);
3020 for (; *temp1; ++temp1)
3022 if (*temp1 == ' ')
3024 *temp1 = '\0';
3025 break;
3029 /* Set up the default search paths. If there is no GCC_EXEC_PREFIX,
3030 see if we can create it from the pathname specified in argv[0]. */
3032 #ifndef VMS
3033 /* FIXME: make_relative_prefix doesn't yet work for VMS. */
3034 if (!gcc_exec_prefix)
3036 gcc_exec_prefix = make_relative_prefix (argv[0], standard_bindir_prefix,
3037 standard_exec_prefix);
3038 if (gcc_exec_prefix)
3039 putenv (concat ("GCC_EXEC_PREFIX=", gcc_exec_prefix, NULL_PTR));
3041 #endif
3043 if (gcc_exec_prefix)
3045 int len = strlen (gcc_exec_prefix);
3047 if (len > (int) sizeof ("/lib/gcc-lib/") - 1
3048 && (IS_DIR_SEPARATOR (gcc_exec_prefix[len-1])))
3050 temp = gcc_exec_prefix + len - sizeof ("/lib/gcc-lib/") + 1;
3051 if (IS_DIR_SEPARATOR (*temp)
3052 && strncmp (temp + 1, "lib", 3) == 0
3053 && IS_DIR_SEPARATOR (temp[4])
3054 && strncmp (temp + 5, "gcc-lib", 7) == 0)
3055 len -= sizeof ("/lib/gcc-lib/") - 1;
3058 set_std_prefix (gcc_exec_prefix, len);
3059 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC",
3060 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3061 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC",
3062 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3065 /* COMPILER_PATH and LIBRARY_PATH have values
3066 that are lists of directory names with colons. */
3068 GET_ENV_PATH_LIST (temp, "COMPILER_PATH");
3069 if (temp)
3071 const char *startp, *endp;
3072 char *nstore = (char *) alloca (strlen (temp) + 3);
3074 startp = endp = temp;
3075 while (1)
3077 if (*endp == PATH_SEPARATOR || *endp == 0)
3079 strncpy (nstore, startp, endp - startp);
3080 if (endp == startp)
3081 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
3082 else if (!IS_DIR_SEPARATOR (endp[-1]))
3084 nstore[endp - startp] = DIR_SEPARATOR;
3085 nstore[endp - startp + 1] = 0;
3087 else
3088 nstore[endp - startp] = 0;
3089 add_prefix (&exec_prefixes, nstore, 0,
3090 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3091 add_prefix (&include_prefixes,
3092 concat (nstore, "include", NULL_PTR),
3093 0, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3094 if (*endp == 0)
3095 break;
3096 endp = startp = endp + 1;
3098 else
3099 endp++;
3103 GET_ENV_PATH_LIST (temp, LIBRARY_PATH_ENV);
3104 if (temp && *cross_compile == '0')
3106 const char *startp, *endp;
3107 char *nstore = (char *) alloca (strlen (temp) + 3);
3109 startp = endp = temp;
3110 while (1)
3112 if (*endp == PATH_SEPARATOR || *endp == 0)
3114 strncpy (nstore, startp, endp - startp);
3115 if (endp == startp)
3116 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
3117 else if (!IS_DIR_SEPARATOR (endp[-1]))
3119 nstore[endp - startp] = DIR_SEPARATOR;
3120 nstore[endp - startp + 1] = 0;
3122 else
3123 nstore[endp - startp] = 0;
3124 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
3125 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3126 if (*endp == 0)
3127 break;
3128 endp = startp = endp + 1;
3130 else
3131 endp++;
3135 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
3136 GET_ENV_PATH_LIST (temp, "LPATH");
3137 if (temp && *cross_compile == '0')
3139 const char *startp, *endp;
3140 char *nstore = (char *) alloca (strlen (temp) + 3);
3142 startp = endp = temp;
3143 while (1)
3145 if (*endp == PATH_SEPARATOR || *endp == 0)
3147 strncpy (nstore, startp, endp - startp);
3148 if (endp == startp)
3149 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
3150 else if (!IS_DIR_SEPARATOR (endp[-1]))
3152 nstore[endp - startp] = DIR_SEPARATOR;
3153 nstore[endp - startp + 1] = 0;
3155 else
3156 nstore[endp - startp] = 0;
3157 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
3158 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3159 if (*endp == 0)
3160 break;
3161 endp = startp = endp + 1;
3163 else
3164 endp++;
3168 /* Convert new-style -- options to old-style. */
3169 translate_options (&argc, &argv);
3171 /* Do language-specific adjustment/addition of flags. */
3172 lang_specific_driver (&argc, &argv, &added_libraries);
3174 /* Scan argv twice. Here, the first time, just count how many switches
3175 there will be in their vector, and how many input files in theirs.
3176 Also parse any switches that determine the configuration name, such as -b.
3177 Here we also parse the switches that cc itself uses (e.g. -v). */
3179 for (i = 1; i < argc; i++)
3181 if (! strcmp (argv[i], "-dumpspecs"))
3183 struct spec_list *sl;
3184 init_spec ();
3185 for (sl = specs; sl; sl = sl->next)
3186 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
3187 if (link_command_spec)
3188 printf ("*link_command:\n%s\n\n", link_command_spec);
3189 exit (0);
3191 else if (! strcmp (argv[i], "-dumpversion"))
3193 printf ("%s\n", spec_version);
3194 exit (0);
3196 else if (! strcmp (argv[i], "-dumpmachine"))
3198 printf ("%s\n", spec_machine);
3199 exit (0);
3201 else if (strcmp (argv[i], "-fhelp") == 0)
3203 /* translate_options () has turned --help into -fhelp. */
3204 print_help_list = 1;
3206 /* We will be passing a dummy file on to the sub-processes. */
3207 n_infiles++;
3208 n_switches++;
3210 add_preprocessor_option ("--help", 6);
3211 add_assembler_option ("--help", 6);
3212 add_linker_option ("--help", 6);
3214 else if (strcmp (argv[i], "-ftarget-help") == 0)
3216 /* translate_options() has turned --target-help into -ftarget-help. */
3217 target_help_flag = 1;
3219 /* We will be passing a dummy file on to the sub-processes. */
3220 n_infiles++;
3221 n_switches++;
3223 add_preprocessor_option ("--target-help", 13);
3224 add_assembler_option ("--target-help", 13);
3225 add_linker_option ("--target-help", 13);
3227 else if (! strcmp (argv[i], "-pass-exit-codes"))
3229 pass_exit_codes = 1;
3230 n_switches++;
3232 else if (! strcmp (argv[i], "-print-search-dirs"))
3233 print_search_dirs = 1;
3234 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3235 print_file_name = "libgcc.a";
3236 else if (! strncmp (argv[i], "-print-file-name=", 17))
3237 print_file_name = argv[i] + 17;
3238 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3239 print_prog_name = argv[i] + 17;
3240 else if (! strcmp (argv[i], "-print-multi-lib"))
3241 print_multi_lib = 1;
3242 else if (! strcmp (argv[i], "-print-multi-directory"))
3243 print_multi_directory = 1;
3244 else if (! strncmp (argv[i], "-Wa,", 4))
3246 int prev, j;
3247 /* Pass the rest of this option to the assembler. */
3249 /* Split the argument at commas. */
3250 prev = 4;
3251 for (j = 4; argv[i][j]; j++)
3252 if (argv[i][j] == ',')
3254 add_assembler_option (argv[i] + prev, j - prev);
3255 prev = j + 1;
3258 /* Record the part after the last comma. */
3259 add_assembler_option (argv[i] + prev, j - prev);
3261 else if (! strncmp (argv[i], "-Wp,", 4))
3263 int prev, j;
3264 /* Pass the rest of this option to the preprocessor. */
3266 /* Split the argument at commas. */
3267 prev = 4;
3268 for (j = 4; argv[i][j]; j++)
3269 if (argv[i][j] == ',')
3271 add_preprocessor_option (argv[i] + prev, j - prev);
3272 prev = j + 1;
3275 /* Record the part after the last comma. */
3276 add_preprocessor_option (argv[i] + prev, j - prev);
3278 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3279 /* The +e options to the C++ front-end. */
3280 n_switches++;
3281 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3283 int j;
3284 /* Split the argument at commas. */
3285 for (j = 3; argv[i][j]; j++)
3286 n_infiles += (argv[i][j] == ',');
3288 else if (strcmp (argv[i], "-Xlinker") == 0)
3290 if (i + 1 == argc)
3291 fatal ("argument to `-Xlinker' is missing");
3293 n_infiles++;
3294 i++;
3296 else if (strncmp (argv[i], "-l", 2) == 0)
3297 n_infiles++;
3298 else if (strcmp (argv[i], "-save-temps") == 0)
3300 save_temps_flag = 1;
3301 n_switches++;
3303 else if (strcmp (argv[i], "-specs") == 0)
3305 struct user_specs *user = (struct user_specs *)
3306 xmalloc (sizeof (struct user_specs));
3307 if (++i >= argc)
3308 fatal ("argument to `-specs' is missing");
3310 user->next = (struct user_specs *) 0;
3311 user->filename = argv[i];
3312 if (user_specs_tail)
3313 user_specs_tail->next = user;
3314 else
3315 user_specs_head = user;
3316 user_specs_tail = user;
3318 else if (strncmp (argv[i], "-specs=", 7) == 0)
3320 struct user_specs *user = (struct user_specs *)
3321 xmalloc (sizeof (struct user_specs));
3322 if (strlen (argv[i]) == 7)
3323 fatal ("argument to `-specs=' is missing");
3325 user->next = (struct user_specs *) 0;
3326 user->filename = argv[i] + 7;
3327 if (user_specs_tail)
3328 user_specs_tail->next = user;
3329 else
3330 user_specs_head = user;
3331 user_specs_tail = user;
3333 else if (strcmp (argv[i], "-time") == 0)
3334 report_times = 1;
3335 else if (argv[i][0] == '-' && argv[i][1] != 0)
3337 register const char *p = &argv[i][1];
3338 register int c = *p;
3340 switch (c)
3342 case 'b':
3343 n_switches++;
3344 if (p[1] == 0 && i + 1 == argc)
3345 fatal ("argument to `-b' is missing");
3346 if (p[1] == 0)
3347 spec_machine = argv[++i];
3348 else
3349 spec_machine = p + 1;
3351 warn_std_ptr = &warn_std;
3352 break;
3354 case 'B':
3356 const char *value;
3357 if (p[1] == 0 && i + 1 == argc)
3358 fatal ("argument to `-B' is missing");
3359 if (p[1] == 0)
3360 value = argv[++i];
3361 else
3362 value = p + 1;
3364 /* As a kludge, if the arg is "[foo/]stageN/", just
3365 add "[foo/]include" to the include prefix. */
3366 int len = strlen (value);
3367 if ((len == 7
3368 || (len > 7
3369 && (IS_DIR_SEPARATOR (value[len - 8]))))
3370 && strncmp (value + len - 7, "stage", 5) == 0
3371 && ISDIGIT (value[len - 2])
3372 && (IS_DIR_SEPARATOR (value[len - 1])))
3374 if (len == 7)
3375 add_prefix (&include_prefixes, "include", NULL_PTR,
3376 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3377 else
3379 char *string = xmalloc (len + 1);
3380 strncpy (string, value, len-7);
3381 strcpy (string+len-7, "include");
3382 add_prefix (&include_prefixes, string, NULL_PTR,
3383 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3387 add_prefix (&exec_prefixes, value, NULL_PTR,
3388 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3389 add_prefix (&startfile_prefixes, value, NULL_PTR,
3390 PREFIX_PRIORITY_B_OPT, 0, &warn_B);
3391 add_prefix (&include_prefixes, concat (value, "include",
3392 NULL_PTR),
3393 NULL_PTR,
3394 PREFIX_PRIORITY_B_OPT, 0, NULL_PTR);
3395 n_switches++;
3397 break;
3399 case 'v': /* Print our subcommands and print versions. */
3400 n_switches++;
3401 /* If they do anything other than exactly `-v', don't set
3402 verbose_flag; rather, continue on to give the error. */
3403 if (p[1] != 0)
3404 break;
3405 verbose_flag++;
3406 break;
3408 case 'V':
3409 n_switches++;
3410 if (p[1] == 0 && i + 1 == argc)
3411 fatal ("argument to `-V' is missing");
3412 if (p[1] == 0)
3413 spec_version = argv[++i];
3414 else
3415 spec_version = p + 1;
3416 compiler_version = spec_version;
3417 warn_std_ptr = &warn_std;
3419 /* Validate the version number. Use the same checks
3420 done when inserting it into a spec.
3422 The format of the version string is
3423 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3425 const char *v = compiler_version;
3427 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3428 while (! ISDIGIT (*v))
3429 v++;
3431 if (v > compiler_version && v[-1] != '-')
3432 fatal ("invalid version number format");
3434 /* Set V after the first period. */
3435 while (ISDIGIT (*v))
3436 v++;
3438 if (*v != '.')
3439 fatal ("invalid version number format");
3441 v++;
3442 while (ISDIGIT (*v))
3443 v++;
3445 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
3446 fatal ("invalid version number format");
3448 break;
3450 case 'S':
3451 case 'c':
3452 if (p[1] == 0)
3454 have_c = 1;
3455 n_switches++;
3456 break;
3458 goto normal_switch;
3460 case 'o':
3461 have_o = 1;
3462 #if defined(HAVE_EXECUTABLE_SUFFIX)
3463 if (! have_c)
3465 int skip;
3467 /* Forward scan, just in case -S or -c is specified
3468 after -o. */
3469 int j = i + 1;
3470 if (p[1] == 0)
3471 ++j;
3472 while (j < argc)
3474 if (argv[j][0] == '-')
3476 if (SWITCH_CURTAILS_COMPILATION (argv[j][1])
3477 && argv[j][2] == 0)
3479 have_c = 1;
3480 break;
3482 else if (skip = SWITCH_TAKES_ARG (argv[j][1]))
3483 j += skip - (argv[j][2] != 0);
3484 else if (skip = WORD_SWITCH_TAKES_ARG (argv[j] + 1))
3485 j += skip;
3487 j++;
3490 #endif
3491 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
3492 if (p[1] == 0)
3493 argv[i + 1] = convert_filename (argv[i + 1], ! have_c);
3494 else
3495 argv[i] = convert_filename (argv[i], ! have_c);
3496 #endif
3497 goto normal_switch;
3499 default:
3500 normal_switch:
3502 #ifdef MODIFY_TARGET_NAME
3503 is_modify_target_name = 0;
3505 for (j = 0;
3506 j < sizeof modify_target / sizeof modify_target[0]; j++)
3507 if (! strcmp (argv[i], modify_target[j].sw))
3509 char *new_name
3510 = (char *) xmalloc (strlen (modify_target[j].str)
3511 + strlen (spec_machine));
3512 const char *p, *r;
3513 char *q;
3514 int made_addition = 0;
3516 is_modify_target_name = 1;
3517 for (p = spec_machine, q = new_name; *p != 0; )
3519 if (modify_target[j].add_del == DELETE
3520 && (! strncmp (q, modify_target[j].str,
3521 strlen (modify_target[j].str))))
3522 p += strlen (modify_target[j].str);
3523 else if (modify_target[j].add_del == ADD
3524 && ! made_addition && *p == '-')
3526 for (r = modify_target[j].str; *r != 0; )
3527 *q++ = *r++;
3528 made_addition = 1;
3531 *q++ = *p++;
3534 spec_machine = new_name;
3537 if (is_modify_target_name)
3538 break;
3539 #endif
3541 n_switches++;
3543 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
3544 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
3545 else if (WORD_SWITCH_TAKES_ARG (p))
3546 i += WORD_SWITCH_TAKES_ARG (p);
3549 else
3551 n_infiles++;
3552 lang_n_infiles++;
3556 if (have_c && have_o && lang_n_infiles > 1)
3557 fatal ("cannot specify -o with -c or -S and multiple compilations");
3559 /* Set up the search paths before we go looking for config files. */
3561 /* These come before the md prefixes so that we will find gcc's subcommands
3562 (such as cpp) rather than those of the host system. */
3563 /* Use 2 as fourth arg meaning try just the machine as a suffix,
3564 as well as trying the machine and the version. */
3565 #ifndef OS2
3566 add_prefix (&exec_prefixes, standard_exec_prefix, "GCC",
3567 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3568 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
3569 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3570 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
3571 PREFIX_PRIORITY_LAST, 2, warn_std_ptr);
3572 #endif
3574 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
3575 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3576 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
3577 PREFIX_PRIORITY_LAST, 1, warn_std_ptr);
3579 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
3580 dir_separator_str, NULL_PTR);
3582 /* If tooldir is relative, base it on exec_prefixes. A relative
3583 tooldir lets us move the installed tree as a unit.
3585 If GCC_EXEC_PREFIX is defined, then we want to add two relative
3586 directories, so that we can search both the user specified directory
3587 and the standard place. */
3589 if (!IS_ABSOLUTE_PATHNAME (tooldir_prefix))
3591 if (gcc_exec_prefix)
3593 char *gcc_exec_tooldir_prefix
3594 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
3595 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
3597 add_prefix (&exec_prefixes,
3598 concat (gcc_exec_tooldir_prefix, "bin",
3599 dir_separator_str, NULL_PTR),
3600 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3601 add_prefix (&startfile_prefixes,
3602 concat (gcc_exec_tooldir_prefix, "lib",
3603 dir_separator_str, NULL_PTR),
3604 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3607 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
3608 dir_separator_str, spec_version,
3609 dir_separator_str, tooldir_prefix, NULL_PTR);
3612 add_prefix (&exec_prefixes,
3613 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
3614 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3615 add_prefix (&startfile_prefixes,
3616 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
3617 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
3619 /* More prefixes are enabled in main, after we read the specs file
3620 and determine whether this is cross-compilation or not. */
3622 /* Then create the space for the vectors and scan again. */
3624 switches = ((struct switchstr *)
3625 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
3626 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
3627 n_switches = 0;
3628 n_infiles = 0;
3629 last_language_n_infiles = -1;
3631 /* This, time, copy the text of each switch and store a pointer
3632 to the copy in the vector of switches.
3633 Store all the infiles in their vector. */
3635 for (i = 1; i < argc; i++)
3637 /* Just skip the switches that were handled by the preceding loop. */
3638 #ifdef MODIFY_TARGET_NAME
3639 is_modify_target_name = 0;
3641 for (j = 0; j < sizeof modify_target / sizeof modify_target[0]; j++)
3642 if (! strcmp (argv[i], modify_target[j].sw))
3643 is_modify_target_name = 1;
3645 if (is_modify_target_name)
3647 else
3648 #endif
3649 if (! strncmp (argv[i], "-Wa,", 4))
3651 else if (! strncmp (argv[i], "-Wp,", 4))
3653 else if (! strcmp (argv[i], "-pass-exit-codes"))
3655 else if (! strcmp (argv[i], "-print-search-dirs"))
3657 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
3659 else if (! strncmp (argv[i], "-print-file-name=", 17))
3661 else if (! strncmp (argv[i], "-print-prog-name=", 17))
3663 else if (! strcmp (argv[i], "-print-multi-lib"))
3665 else if (! strcmp (argv[i], "-print-multi-directory"))
3667 else if (strcmp (argv[i], "-ftarget-help") == 0)
3669 /* Create a dummy input file, so that we can pass --target-help on to
3670 the various sub-processes. */
3671 infiles[n_infiles].language = "c";
3672 infiles[n_infiles++].name = "target-dummy";
3674 /* Preserve the --target-help switch so that it can be caught by
3675 the cc1 spec string. */
3676 switches[n_switches].part1 = "--target-help";
3677 switches[n_switches].args = 0;
3678 switches[n_switches].live_cond = SWITCH_OK;
3679 switches[n_switches].validated = 0;
3681 n_switches++;
3683 else if (strcmp (argv[i], "-fhelp") == 0)
3685 if (verbose_flag)
3687 /* Create a dummy input file, so that we can pass --help on to
3688 the various sub-processes. */
3689 infiles[n_infiles].language = "c";
3690 infiles[n_infiles++].name = "help-dummy";
3692 /* Preserve the --help switch so that it can be caught by the
3693 cc1 spec string. */
3694 switches[n_switches].part1 = "--help";
3695 switches[n_switches].args = 0;
3696 switches[n_switches].live_cond = SWITCH_OK;
3697 switches[n_switches].validated = 0;
3699 n_switches++;
3702 else if (argv[i][0] == '+' && argv[i][1] == 'e')
3704 /* Compensate for the +e options to the C++ front-end;
3705 they're there simply for cfront call-compatibility. We do
3706 some magic in default_compilers to pass them down properly.
3707 Note we deliberately start at the `+' here, to avoid passing
3708 -e0 or -e1 down into the linker. */
3709 switches[n_switches].part1 = &argv[i][0];
3710 switches[n_switches].args = 0;
3711 switches[n_switches].live_cond = SWITCH_OK;
3712 switches[n_switches].validated = 0;
3713 n_switches++;
3715 else if (strncmp (argv[i], "-Wl,", 4) == 0)
3717 int prev, j;
3718 /* Split the argument at commas. */
3719 prev = 4;
3720 for (j = 4; argv[i][j]; j++)
3721 if (argv[i][j] == ',')
3723 infiles[n_infiles].language = "*";
3724 infiles[n_infiles++].name
3725 = save_string (argv[i] + prev, j - prev);
3726 prev = j + 1;
3728 /* Record the part after the last comma. */
3729 infiles[n_infiles].language = "*";
3730 infiles[n_infiles++].name = argv[i] + prev;
3732 else if (strcmp (argv[i], "-Xlinker") == 0)
3734 infiles[n_infiles].language = "*";
3735 infiles[n_infiles++].name = argv[++i];
3737 else if (strncmp (argv[i], "-l", 2) == 0)
3739 infiles[n_infiles].language = "*";
3740 infiles[n_infiles++].name = argv[i];
3742 else if (strcmp (argv[i], "-specs") == 0)
3743 i++;
3744 else if (strncmp (argv[i], "-specs=", 7) == 0)
3746 else if (strcmp (argv[i], "-time") == 0)
3748 else if ((save_temps_flag || report_times)
3749 && strcmp (argv[i], "-pipe") == 0)
3751 /* -save-temps overrides -pipe, so that temp files are produced */
3752 if (save_temps_flag)
3753 error ("Warning: -pipe ignored because -save-temps specified");
3754 /* -time overrides -pipe because we can't get correct stats when
3755 multiple children are running at once. */
3756 else if (report_times)
3757 error ("Warning: -pipe ignored because -time specified");
3759 else if (argv[i][0] == '-' && argv[i][1] != 0)
3761 const char *p = &argv[i][1];
3762 int c = *p;
3764 if (c == 'x')
3766 if (p[1] == 0 && i + 1 == argc)
3767 fatal ("argument to `-x' is missing");
3768 if (p[1] == 0)
3769 spec_lang = argv[++i];
3770 else
3771 spec_lang = p + 1;
3772 if (! strcmp (spec_lang, "none"))
3773 /* Suppress the warning if -xnone comes after the last input
3774 file, because alternate command interfaces like g++ might
3775 find it useful to place -xnone after each input file. */
3776 spec_lang = 0;
3777 else
3778 last_language_n_infiles = n_infiles;
3779 continue;
3781 switches[n_switches].part1 = p;
3782 /* Deal with option arguments in separate argv elements. */
3783 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
3784 || WORD_SWITCH_TAKES_ARG (p))
3786 int j = 0;
3787 int n_args = WORD_SWITCH_TAKES_ARG (p);
3789 if (n_args == 0)
3791 /* Count only the option arguments in separate argv elements. */
3792 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3794 if (i + n_args >= argc)
3795 fatal ("argument to `-%s' is missing", p);
3796 switches[n_switches].args
3797 = (const char **) xmalloc ((n_args + 1) * sizeof(const char *));
3798 while (j < n_args)
3799 switches[n_switches].args[j++] = argv[++i];
3800 /* Null-terminate the vector. */
3801 switches[n_switches].args[j] = 0;
3803 else if (strchr (switches_need_spaces, c))
3805 /* On some systems, ld cannot handle some options without
3806 a space. So split the option from its argument. */
3807 char *part1 = (char *) xmalloc (2);
3808 char *tmp;
3809 part1[0] = c;
3810 part1[1] = '\0';
3812 switches[n_switches].part1 = part1;
3813 switches[n_switches].args
3814 = (const char **) xmalloc (2 * sizeof (const char *));
3815 switches[n_switches].args[0] = tmp = xmalloc (strlen (p));
3816 strcpy (tmp, &p[1]);
3817 switches[n_switches].args[1] = 0;
3819 else
3820 switches[n_switches].args = 0;
3822 switches[n_switches].live_cond = SWITCH_OK;
3823 switches[n_switches].validated = 0;
3824 switches[n_switches].ordering = 0;
3825 /* These are always valid, since gcc.c itself understands it. */
3826 if (!strcmp (p, "save-temps")
3827 || !strcmp (p, "static-libgcc")
3828 || !strcmp (p, "shared-libgcc"))
3829 switches[n_switches].validated = 1;
3830 else
3832 char ch = switches[n_switches].part1[0];
3833 if (ch == 'V' || ch == 'b' || ch == 'B')
3834 switches[n_switches].validated = 1;
3836 n_switches++;
3838 else
3840 #ifdef HAVE_OBJECT_SUFFIX
3841 argv[i] = convert_filename (argv[i], 0);
3842 #endif
3844 if (strcmp (argv[i], "-") != 0 && access (argv[i], F_OK) < 0)
3846 perror_with_name (argv[i]);
3847 error_count++;
3849 else
3851 infiles[n_infiles].language = spec_lang;
3852 infiles[n_infiles++].name = argv[i];
3857 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3858 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3860 switches[n_switches].part1 = 0;
3861 infiles[n_infiles].name = 0;
3864 /* Process a spec string, accumulating and running commands. */
3866 /* These variables describe the input file name.
3867 input_file_number is the index on outfiles of this file,
3868 so that the output file name can be stored for later use by %o.
3869 input_basename is the start of the part of the input file
3870 sans all directory names, and basename_length is the number
3871 of characters starting there excluding the suffix .c or whatever. */
3873 const char *input_filename;
3874 static int input_file_number;
3875 size_t input_filename_length;
3876 static int basename_length;
3877 static int suffixed_basename_length;
3878 static const char *input_basename;
3879 static const char *input_suffix;
3881 /* The compiler used to process the current input file. */
3882 static struct compiler *input_file_compiler;
3884 /* These are variables used within do_spec and do_spec_1. */
3886 /* Nonzero if an arg has been started and not yet terminated
3887 (with space, tab or newline). */
3888 static int arg_going;
3890 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3891 is a temporary file name. */
3892 static int delete_this_arg;
3894 /* Nonzero means %w has been seen; the next arg to be terminated
3895 is the output file name of this compilation. */
3896 static int this_is_output_file;
3898 /* Nonzero means %s has been seen; the next arg to be terminated
3899 is the name of a library file and we should try the standard
3900 search dirs for it. */
3901 static int this_is_library_file;
3903 /* Nonzero means that the input of this command is coming from a pipe. */
3904 static int input_from_pipe;
3906 /* Nonnull means substitute this for any suffix when outputting a switches
3907 arguments. */
3908 static const char *suffix_subst;
3910 /* Process the spec SPEC and run the commands specified therein.
3911 Returns 0 if the spec is successfully processed; -1 if failed. */
3914 do_spec (spec)
3915 const char *spec;
3917 int value;
3919 clear_args ();
3920 arg_going = 0;
3921 delete_this_arg = 0;
3922 this_is_output_file = 0;
3923 this_is_library_file = 0;
3924 input_from_pipe = 0;
3925 suffix_subst = NULL;
3927 value = do_spec_1 (spec, 0, NULL_PTR);
3929 /* Force out any unfinished command.
3930 If -pipe, this forces out the last command if it ended in `|'. */
3931 if (value == 0)
3933 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3934 argbuf_index--;
3936 if (argbuf_index > 0)
3937 value = execute ();
3940 return value;
3943 /* Process the sub-spec SPEC as a portion of a larger spec.
3944 This is like processing a whole spec except that we do
3945 not initialize at the beginning and we do not supply a
3946 newline by default at the end.
3947 INSWITCH nonzero means don't process %-sequences in SPEC;
3948 in this case, % is treated as an ordinary character.
3949 This is used while substituting switches.
3950 INSWITCH nonzero also causes SPC not to terminate an argument.
3952 Value is zero unless a line was finished
3953 and the command on that line reported an error. */
3955 static int
3956 do_spec_1 (spec, inswitch, soft_matched_part)
3957 const char *spec;
3958 int inswitch;
3959 const char *soft_matched_part;
3961 register const char *p = spec;
3962 register int c;
3963 int i;
3964 const char *string;
3965 int value;
3967 while ((c = *p++))
3968 /* If substituting a switch, treat all chars like letters.
3969 Otherwise, NL, SPC, TAB and % are special. */
3970 switch (inswitch ? 'a' : c)
3972 case '\n':
3973 /* End of line: finish any pending argument,
3974 then run the pending command if one has been started. */
3975 if (arg_going)
3977 obstack_1grow (&obstack, 0);
3978 string = obstack_finish (&obstack);
3979 if (this_is_library_file)
3980 string = find_file (string);
3981 store_arg (string, delete_this_arg, this_is_output_file);
3982 if (this_is_output_file)
3983 outfiles[input_file_number] = string;
3985 arg_going = 0;
3987 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3989 for (i = 0; i < n_switches; i++)
3990 if (!strcmp (switches[i].part1, "pipe"))
3991 break;
3993 /* A `|' before the newline means use a pipe here,
3994 but only if -pipe was specified.
3995 Otherwise, execute now and don't pass the `|' as an arg. */
3996 if (i < n_switches)
3998 input_from_pipe = 1;
3999 switches[i].validated = 1;
4000 break;
4002 else
4003 argbuf_index--;
4006 if (argbuf_index > 0)
4008 value = execute ();
4009 if (value)
4010 return value;
4012 /* Reinitialize for a new command, and for a new argument. */
4013 clear_args ();
4014 arg_going = 0;
4015 delete_this_arg = 0;
4016 this_is_output_file = 0;
4017 this_is_library_file = 0;
4018 input_from_pipe = 0;
4019 break;
4021 case '|':
4022 /* End any pending argument. */
4023 if (arg_going)
4025 obstack_1grow (&obstack, 0);
4026 string = obstack_finish (&obstack);
4027 if (this_is_library_file)
4028 string = find_file (string);
4029 store_arg (string, delete_this_arg, this_is_output_file);
4030 if (this_is_output_file)
4031 outfiles[input_file_number] = string;
4034 /* Use pipe */
4035 obstack_1grow (&obstack, c);
4036 arg_going = 1;
4037 break;
4039 case '\t':
4040 case ' ':
4041 /* Space or tab ends an argument if one is pending. */
4042 if (arg_going)
4044 obstack_1grow (&obstack, 0);
4045 string = obstack_finish (&obstack);
4046 if (this_is_library_file)
4047 string = find_file (string);
4048 store_arg (string, delete_this_arg, this_is_output_file);
4049 if (this_is_output_file)
4050 outfiles[input_file_number] = string;
4052 /* Reinitialize for a new argument. */
4053 arg_going = 0;
4054 delete_this_arg = 0;
4055 this_is_output_file = 0;
4056 this_is_library_file = 0;
4057 break;
4059 case '%':
4060 switch (c = *p++)
4062 case 0:
4063 fatal ("Invalid specification! Bug in cc.");
4065 case 'b':
4066 obstack_grow (&obstack, input_basename, basename_length);
4067 arg_going = 1;
4068 break;
4070 case 'B':
4071 obstack_grow (&obstack, input_basename, suffixed_basename_length);
4072 arg_going = 1;
4073 break;
4075 case 'd':
4076 delete_this_arg = 2;
4077 break;
4079 /* Dump out the directories specified with LIBRARY_PATH,
4080 followed by the absolute directories
4081 that we search for startfiles. */
4082 case 'D':
4084 struct prefix_list *pl = startfile_prefixes.plist;
4085 size_t bufsize = 100;
4086 char *buffer = (char *) xmalloc (bufsize);
4087 int idx;
4089 for (; pl; pl = pl->next)
4091 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
4092 /* Used on systems which record the specified -L dirs
4093 and use them to search for dynamic linking. */
4094 /* Relative directories always come from -B,
4095 and it is better not to use them for searching
4096 at run time. In particular, stage1 loses. */
4097 if (!IS_ABSOLUTE_PATHNAME (pl->prefix))
4098 continue;
4099 #endif
4100 /* Try subdirectory if there is one. */
4101 if (multilib_dir != NULL)
4103 if (machine_suffix)
4105 if (strlen (pl->prefix) + strlen (machine_suffix)
4106 >= bufsize)
4107 bufsize = (strlen (pl->prefix)
4108 + strlen (machine_suffix)) * 2 + 1;
4109 buffer = (char *) xrealloc (buffer, bufsize);
4110 strcpy (buffer, pl->prefix);
4111 strcat (buffer, machine_suffix);
4112 if (is_directory (buffer, multilib_dir, 1))
4114 do_spec_1 ("-L", 0, NULL_PTR);
4115 #ifdef SPACE_AFTER_L_OPTION
4116 do_spec_1 (" ", 0, NULL_PTR);
4117 #endif
4118 do_spec_1 (buffer, 1, NULL_PTR);
4119 do_spec_1 (multilib_dir, 1, NULL_PTR);
4120 /* Make this a separate argument. */
4121 do_spec_1 (" ", 0, NULL_PTR);
4124 if (!pl->require_machine_suffix)
4126 if (is_directory (pl->prefix, multilib_dir, 1))
4128 do_spec_1 ("-L", 0, NULL_PTR);
4129 #ifdef SPACE_AFTER_L_OPTION
4130 do_spec_1 (" ", 0, NULL_PTR);
4131 #endif
4132 do_spec_1 (pl->prefix, 1, NULL_PTR);
4133 do_spec_1 (multilib_dir, 1, NULL_PTR);
4134 /* Make this a separate argument. */
4135 do_spec_1 (" ", 0, NULL_PTR);
4139 if (machine_suffix)
4141 if (is_directory (pl->prefix, machine_suffix, 1))
4143 do_spec_1 ("-L", 0, NULL_PTR);
4144 #ifdef SPACE_AFTER_L_OPTION
4145 do_spec_1 (" ", 0, NULL_PTR);
4146 #endif
4147 do_spec_1 (pl->prefix, 1, NULL_PTR);
4148 /* Remove slash from machine_suffix. */
4149 if (strlen (machine_suffix) >= bufsize)
4150 bufsize = strlen (machine_suffix) * 2 + 1;
4151 buffer = (char *) xrealloc (buffer, bufsize);
4152 strcpy (buffer, machine_suffix);
4153 idx = strlen (buffer);
4154 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4155 buffer[idx - 1] = 0;
4156 do_spec_1 (buffer, 1, NULL_PTR);
4157 /* Make this a separate argument. */
4158 do_spec_1 (" ", 0, NULL_PTR);
4161 if (!pl->require_machine_suffix)
4163 if (is_directory (pl->prefix, "", 1))
4165 do_spec_1 ("-L", 0, NULL_PTR);
4166 #ifdef SPACE_AFTER_L_OPTION
4167 do_spec_1 (" ", 0, NULL_PTR);
4168 #endif
4169 /* Remove slash from pl->prefix. */
4170 if (strlen (pl->prefix) >= bufsize)
4171 bufsize = strlen (pl->prefix) * 2 + 1;
4172 buffer = (char *) xrealloc (buffer, bufsize);
4173 strcpy (buffer, pl->prefix);
4174 idx = strlen (buffer);
4175 if (IS_DIR_SEPARATOR (buffer[idx - 1]))
4176 buffer[idx - 1] = 0;
4177 do_spec_1 (buffer, 1, NULL_PTR);
4178 /* Make this a separate argument. */
4179 do_spec_1 (" ", 0, NULL_PTR);
4183 free (buffer);
4185 break;
4187 case 'e':
4188 /* %efoo means report an error with `foo' as error message
4189 and don't execute any more commands for this file. */
4191 const char *q = p;
4192 char *buf;
4193 while (*p != 0 && *p != '\n')
4194 p++;
4195 buf = (char *) alloca (p - q + 1);
4196 strncpy (buf, q, p - q);
4197 buf[p - q] = 0;
4198 error ("%s", buf);
4199 return -1;
4201 break;
4202 case 'n':
4203 /* %nfoo means report an notice with `foo' on stderr. */
4205 const char *q = p;
4206 char *buf;
4207 while (*p != 0 && *p != '\n')
4208 p++;
4209 buf = (char *) alloca (p - q + 1);
4210 strncpy (buf, q, p - q);
4211 buf[p - q] = 0;
4212 notice ("%s\n", buf);
4213 if (*p)
4214 p++;
4216 break;
4218 case 'j':
4220 struct stat st;
4222 /* If save_temps_flag is off, and the HOST_BIT_BUCKET is defined,
4223 and it is not a directory, and it is writable, use it.
4224 Otherwise, fall through and treat this like any other
4225 temporary file. */
4227 if ((!save_temps_flag)
4228 && (stat (HOST_BIT_BUCKET, &st) == 0) && (!S_ISDIR (st.st_mode))
4229 && (access (HOST_BIT_BUCKET, W_OK) == 0))
4231 obstack_grow (&obstack, HOST_BIT_BUCKET,
4232 strlen (HOST_BIT_BUCKET));
4233 delete_this_arg = 0;
4234 arg_going = 1;
4235 break;
4238 case 'g':
4239 case 'u':
4240 case 'U':
4241 if (save_temps_flag)
4243 obstack_grow (&obstack, input_basename, basename_length);
4244 delete_this_arg = 0;
4246 else
4248 struct temp_name *t;
4249 int suffix_length;
4250 const char *suffix = p;
4251 char *saved_suffix = NULL;
4253 while (*p == '.' || ISALPHA ((unsigned char) *p))
4254 p++;
4255 suffix_length = p - suffix;
4256 if (p[0] == '%' && p[1] == 'O')
4258 p += 2;
4259 /* We don't support extra suffix characters after %O. */
4260 if (*p == '.' || ISALPHA ((unsigned char) *p))
4261 abort ();
4262 if (suffix_length == 0)
4263 suffix = OBJECT_SUFFIX;
4264 else
4266 saved_suffix
4267 = (char *) xmalloc (suffix_length
4268 + strlen (OBJECT_SUFFIX));
4269 strncpy (saved_suffix, suffix, suffix_length);
4270 strcpy (saved_suffix + suffix_length,
4271 OBJECT_SUFFIX);
4273 suffix_length += strlen (OBJECT_SUFFIX);
4276 /* See if we already have an association of %g/%u/%U and
4277 suffix. */
4278 for (t = temp_names; t; t = t->next)
4279 if (t->length == suffix_length
4280 && strncmp (t->suffix, suffix, suffix_length) == 0
4281 && t->unique == (c != 'g'))
4282 break;
4284 /* Make a new association if needed. %u and %j require one. */
4285 if (t == 0 || c == 'u' || c == 'j')
4287 if (t == 0)
4289 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
4290 t->next = temp_names;
4291 temp_names = t;
4293 t->length = suffix_length;
4294 t->suffix = save_string (suffix, suffix_length);
4295 t->unique = (c != 'g');
4296 temp_filename = make_temp_file (t->suffix);
4297 temp_filename_length = strlen (temp_filename);
4298 t->filename = temp_filename;
4299 t->filename_length = temp_filename_length;
4302 if (saved_suffix)
4303 free (saved_suffix);
4305 obstack_grow (&obstack, t->filename, t->filename_length);
4306 delete_this_arg = 1;
4308 arg_going = 1;
4309 break;
4311 case 'i':
4312 obstack_grow (&obstack, input_filename, input_filename_length);
4313 arg_going = 1;
4314 break;
4316 case 'I':
4318 struct prefix_list *pl = include_prefixes.plist;
4320 if (gcc_exec_prefix)
4322 do_spec_1 ("-iprefix", 1, NULL_PTR);
4323 /* Make this a separate argument. */
4324 do_spec_1 (" ", 0, NULL_PTR);
4325 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
4326 do_spec_1 (" ", 0, NULL_PTR);
4329 for (; pl; pl = pl->next)
4331 do_spec_1 ("-isystem", 1, NULL_PTR);
4332 /* Make this a separate argument. */
4333 do_spec_1 (" ", 0, NULL_PTR);
4334 do_spec_1 (pl->prefix, 1, NULL_PTR);
4335 do_spec_1 (" ", 0, NULL_PTR);
4338 break;
4340 case 'o':
4342 int max = n_infiles;
4343 max += lang_specific_extra_outfiles;
4345 for (i = 0; i < max; i++)
4346 if (outfiles[i])
4347 store_arg (outfiles[i], 0, 0);
4348 break;
4351 case 'O':
4352 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
4353 arg_going = 1;
4354 break;
4356 case 's':
4357 this_is_library_file = 1;
4358 break;
4360 case 'w':
4361 this_is_output_file = 1;
4362 break;
4364 case 'W':
4366 int cur_index = argbuf_index;
4367 /* Handle the {...} following the %W. */
4368 if (*p != '{')
4369 abort ();
4370 p = handle_braces (p + 1);
4371 if (p == 0)
4372 return -1;
4373 /* If any args were output, mark the last one for deletion
4374 on failure. */
4375 if (argbuf_index != cur_index)
4376 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
4377 break;
4380 /* %x{OPTION} records OPTION for %X to output. */
4381 case 'x':
4383 const char *p1 = p;
4384 char *string;
4386 /* Skip past the option value and make a copy. */
4387 if (*p != '{')
4388 abort ();
4389 while (*p++ != '}')
4391 string = save_string (p1 + 1, p - p1 - 2);
4393 /* See if we already recorded this option. */
4394 for (i = 0; i < n_linker_options; i++)
4395 if (! strcmp (string, linker_options[i]))
4397 free (string);
4398 return 0;
4401 /* This option is new; add it. */
4402 add_linker_option (string, strlen (string));
4404 break;
4406 /* Dump out the options accumulated previously using %x. */
4407 case 'X':
4408 for (i = 0; i < n_linker_options; i++)
4410 do_spec_1 (linker_options[i], 1, NULL_PTR);
4411 /* Make each accumulated option a separate argument. */
4412 do_spec_1 (" ", 0, NULL_PTR);
4414 break;
4416 /* Dump out the options accumulated previously using -Wa,. */
4417 case 'Y':
4418 for (i = 0; i < n_assembler_options; i++)
4420 do_spec_1 (assembler_options[i], 1, NULL_PTR);
4421 /* Make each accumulated option a separate argument. */
4422 do_spec_1 (" ", 0, NULL_PTR);
4424 break;
4426 /* Dump out the options accumulated previously using -Wp,. */
4427 case 'Z':
4428 for (i = 0; i < n_preprocessor_options; i++)
4430 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
4431 /* Make each accumulated option a separate argument. */
4432 do_spec_1 (" ", 0, NULL_PTR);
4434 break;
4436 /* Here are digits and numbers that just process
4437 a certain constant string as a spec. */
4439 case '1':
4440 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
4441 if (value != 0)
4442 return value;
4443 break;
4445 case '2':
4446 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
4447 if (value != 0)
4448 return value;
4449 break;
4451 case 'a':
4452 value = do_spec_1 (asm_spec, 0, NULL_PTR);
4453 if (value != 0)
4454 return value;
4455 break;
4457 case 'A':
4458 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
4459 if (value != 0)
4460 return value;
4461 break;
4463 case 'c':
4464 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
4465 if (value != 0)
4466 return value;
4467 break;
4469 case 'C':
4471 const char* spec
4472 = (input_file_compiler->cpp_spec
4473 ? input_file_compiler->cpp_spec
4474 : cpp_spec);
4475 value = do_spec_1 (spec, 0, NULL_PTR);
4476 if (value != 0)
4477 return value;
4479 break;
4481 case 'E':
4482 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
4483 if (value != 0)
4484 return value;
4485 break;
4487 case 'l':
4488 value = do_spec_1 (link_spec, 0, NULL_PTR);
4489 if (value != 0)
4490 return value;
4491 break;
4493 case 'L':
4494 value = do_spec_1 (lib_spec, 0, NULL_PTR);
4495 if (value != 0)
4496 return value;
4497 break;
4499 case 'G':
4500 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
4501 if (value != 0)
4502 return value;
4503 break;
4505 case 'M':
4506 if (multilib_dir && strcmp (multilib_dir, ".") != 0)
4508 char *p;
4509 const char *q;
4510 size_t len;
4512 len = strlen (multilib_dir);
4513 obstack_blank (&obstack, len + 1);
4514 p = obstack_next_free (&obstack) - (len + 1);
4516 *p++ = '_';
4517 for (q = multilib_dir; *q ; ++q, ++p)
4518 *p = (IS_DIR_SEPARATOR (*q) ? '_' : *q);
4520 break;
4522 case 'p':
4524 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
4525 char *buf = x;
4526 const char *y;
4528 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
4529 y = cpp_predefines;
4530 while (*y != 0)
4532 if (! strncmp (y, "-D", 2))
4533 /* Copy the whole option. */
4534 while (*y && *y != ' ' && *y != '\t')
4535 *x++ = *y++;
4536 else if (*y == ' ' || *y == '\t')
4537 /* Copy whitespace to the result. */
4538 *x++ = *y++;
4539 /* Don't copy other options. */
4540 else
4541 y++;
4544 *x = 0;
4546 value = do_spec_1 (buf, 0, NULL_PTR);
4547 if (value != 0)
4548 return value;
4550 break;
4552 case 'P':
4554 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
4555 char *buf = x;
4556 const char *y;
4558 /* Copy all of CPP_PREDEFINES into BUF,
4559 but force them all into the reserved name space if they
4560 aren't already there. The reserved name space is all
4561 identifiers beginning with two underscores or with one
4562 underscore and a capital letter. We do the forcing by
4563 adding up to two underscores to the beginning and end
4564 of each symbol. e.g. mips, _mips, mips_, and _mips_ all
4565 become __mips__. */
4566 y = cpp_predefines;
4567 while (*y != 0)
4569 if (! strncmp (y, "-D", 2))
4571 int flag = 0;
4573 *x++ = *y++;
4574 *x++ = *y++;
4576 if (*y != '_'
4577 || (*(y + 1) != '_'
4578 && ! ISUPPER ((unsigned char) *(y + 1))))
4580 /* Stick __ at front of macro name. */
4581 if (*y != '_')
4582 *x++ = '_';
4583 *x++ = '_';
4584 /* Arrange to stick __ at the end as well. */
4585 flag = 1;
4588 /* Copy the macro name. */
4589 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4590 *x++ = *y++;
4592 if (flag)
4594 if (x[-1] != '_')
4596 if (x[-2] != '_')
4597 *x++ = '_';
4598 *x++ = '_';
4602 /* Copy the value given, if any. */
4603 while (*y && *y != ' ' && *y != '\t')
4604 *x++ = *y++;
4606 else if (*y == ' ' || *y == '\t')
4607 /* Copy whitespace to the result. */
4608 *x++ = *y++;
4609 /* Don't copy -A options */
4610 else
4611 y++;
4613 *x++ = ' ';
4615 /* Copy all of CPP_PREDEFINES into BUF,
4616 but put __ after every -D. */
4617 y = cpp_predefines;
4618 while (*y != 0)
4620 if (! strncmp (y, "-D", 2))
4622 y += 2;
4624 if (*y != '_'
4625 || (*(y + 1) != '_'
4626 && ! ISUPPER ((unsigned char) *(y + 1))))
4628 /* Stick -D__ at front of macro name. */
4629 *x++ = '-';
4630 *x++ = 'D';
4631 if (*y != '_')
4632 *x++ = '_';
4633 *x++ = '_';
4635 /* Copy the macro name. */
4636 while (*y && *y != '=' && *y != ' ' && *y != '\t')
4637 *x++ = *y++;
4639 /* Copy the value given, if any. */
4640 while (*y && *y != ' ' && *y != '\t')
4641 *x++ = *y++;
4643 else
4645 /* Do not copy this macro - we have just done it before */
4646 while (*y && *y != ' ' && *y != '\t')
4647 y++;
4650 else if (*y == ' ' || *y == '\t')
4651 /* Copy whitespace to the result. */
4652 *x++ = *y++;
4653 /* Don't copy -A options. */
4654 else
4655 y++;
4657 *x++ = ' ';
4659 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
4660 y = cpp_predefines;
4661 while (*y != 0)
4663 if (! strncmp (y, "-A", 2))
4664 /* Copy the whole option. */
4665 while (*y && *y != ' ' && *y != '\t')
4666 *x++ = *y++;
4667 else if (*y == ' ' || *y == '\t')
4668 /* Copy whitespace to the result. */
4669 *x++ = *y++;
4670 /* Don't copy other options. */
4671 else
4672 y++;
4675 *x = 0;
4677 value = do_spec_1 (buf, 0, NULL_PTR);
4678 if (value != 0)
4679 return value;
4681 break;
4683 case 'S':
4684 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
4685 if (value != 0)
4686 return value;
4687 break;
4689 /* Here we define characters other than letters and digits. */
4691 case '{':
4692 p = handle_braces (p);
4693 if (p == 0)
4694 return -1;
4695 break;
4697 case '%':
4698 obstack_1grow (&obstack, '%');
4699 break;
4701 case '.':
4703 unsigned len = 0;
4705 while (p[len] && p[len] != ' ' && p[len] != '%')
4706 len++;
4707 suffix_subst = save_string (p - 1, len + 1);
4708 p += len;
4710 break;
4712 case '*':
4713 if (soft_matched_part)
4715 do_spec_1 (soft_matched_part, 1, NULL_PTR);
4716 do_spec_1 (" ", 0, NULL_PTR);
4718 else
4719 /* Catch the case where a spec string contains something like
4720 '%{foo:%*}'. ie there is no * in the pattern on the left
4721 hand side of the :. */
4722 error ("Spec failure: '%%*' has not been initialised by pattern match");
4723 break;
4725 /* Process a string found as the value of a spec given by name.
4726 This feature allows individual machine descriptions
4727 to add and use their own specs.
4728 %[...] modifies -D options the way %P does;
4729 %(...) uses the spec unmodified. */
4730 case '[':
4731 error ("Warning: use of obsolete %%[ operator in specs");
4732 case '(':
4734 const char *name = p;
4735 struct spec_list *sl;
4736 int len;
4738 /* The string after the S/P is the name of a spec that is to be
4739 processed. */
4740 while (*p && *p != ')' && *p != ']')
4741 p++;
4743 /* See if it's in the list. */
4744 for (len = p - name, sl = specs; sl; sl = sl->next)
4745 if (sl->name_len == len && !strncmp (sl->name, name, len))
4747 name = *(sl->ptr_spec);
4748 #ifdef DEBUG_SPECS
4749 notice ("Processing spec %c%s%c, which is '%s'\n",
4750 c, sl->name, (c == '(') ? ')' : ']', name);
4751 #endif
4752 break;
4755 if (sl)
4757 if (c == '(')
4759 value = do_spec_1 (name, 0, NULL_PTR);
4760 if (value != 0)
4761 return value;
4763 else
4765 char *x = (char *) alloca (strlen (name) * 2 + 1);
4766 char *buf = x;
4767 const char *y = name;
4768 int flag = 0;
4770 /* Copy all of NAME into BUF, but put __ after
4771 every -D and at the end of each arg. */
4772 while (1)
4774 if (! strncmp (y, "-D", 2))
4776 *x++ = '-';
4777 *x++ = 'D';
4778 *x++ = '_';
4779 *x++ = '_';
4780 y += 2;
4781 flag = 1;
4782 continue;
4784 else if (flag
4785 && (*y == ' ' || *y == '\t' || *y == '='
4786 || *y == '}' || *y == 0))
4788 *x++ = '_';
4789 *x++ = '_';
4790 flag = 0;
4792 if (*y == 0)
4793 break;
4794 else
4795 *x++ = *y++;
4797 *x = 0;
4799 value = do_spec_1 (buf, 0, NULL_PTR);
4800 if (value != 0)
4801 return value;
4805 /* Discard the closing paren or bracket. */
4806 if (*p)
4807 p++;
4809 break;
4811 case 'v':
4813 int c1 = *p++; /* Select first or second version number. */
4814 const char *v = compiler_version;
4815 const char *q;
4816 static const char zeroc = '0';
4818 /* The format of the version string is
4819 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
4821 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
4822 while (! ISDIGIT (*v))
4823 v++;
4824 if (v > compiler_version && v[-1] != '-')
4825 abort ();
4827 /* If desired, advance to second version number. */
4828 if (c1 >= '2')
4830 /* Set V after the first period. */
4831 while (ISDIGIT (*v))
4832 v++;
4833 if (*v != '.')
4834 abort ();
4835 v++;
4838 /* If desired, advance to third version number.
4839 But don't complain if it's not present */
4840 if (c1 == '3')
4842 /* Set V after the second period. */
4843 while (ISDIGIT (*v))
4844 v++;
4845 if ((*v != 0) && (*v != ' ') && (*v != '.') && (*v != '-'))
4846 abort ();
4847 if (*v != 0)
4848 v++;
4851 /* Set Q at the next period or at the end. */
4852 q = v;
4853 while (ISDIGIT (*q))
4854 q++;
4855 if (*q != 0 && q > v && *q != ' ' && *q != '.' && *q != '-')
4856 abort ();
4858 if (q > v)
4859 /* Put that part into the command. */
4860 obstack_grow (&obstack, v, q - v);
4861 else
4862 /* Default to "0" */
4863 obstack_grow (&obstack, &zeroc, 1);
4864 arg_going = 1;
4866 break;
4868 case '|':
4869 if (input_from_pipe)
4870 do_spec_1 ("-", 0, NULL_PTR);
4871 break;
4873 default:
4874 error ("Spec failure: Unrecognised spec option '%c'", c);
4875 break;
4877 break;
4879 case '\\':
4880 /* Backslash: treat next character as ordinary. */
4881 c = *p++;
4883 /* fall through */
4884 default:
4885 /* Ordinary character: put it into the current argument. */
4886 obstack_1grow (&obstack, c);
4887 arg_going = 1;
4890 /* End of string. */
4891 return 0;
4894 /* Return 0 if we call do_spec_1 and that returns -1. */
4896 static const char *
4897 handle_braces (p)
4898 register const char *p;
4900 const char *filter, *body = NULL, *endbody = NULL;
4901 int pipe_p = 0;
4902 int true_once = 0; /* If, in %{a|b:d}, at least one of a,b was seen. */
4903 int negate;
4904 int suffix;
4905 int include_blanks = 1;
4906 int elide_switch = 0;
4907 int ordered = 0;
4909 if (*p == '^')
4911 /* A '^' after the open-brace means to not give blanks before args. */
4912 include_blanks = 0;
4913 ++p;
4916 if (*p == '|')
4918 /* A `|' after the open-brace means,
4919 if the test fails, output a single minus sign rather than nothing.
4920 This is used in %{|!pipe:...}. */
4921 pipe_p = 1;
4922 ++p;
4925 if (*p == '<')
4927 /* A `<' after the open-brace means that the switch should be
4928 removed from the command-line. */
4929 elide_switch = 1;
4930 ++p;
4933 next_member:
4934 negate = suffix = 0;
4936 if (*p == '!')
4937 /* A `!' after the open-brace negates the condition:
4938 succeed if the specified switch is not present. */
4939 negate = 1, ++p;
4941 if (*p == '.')
4942 /* A `.' after the open-brace means test against the current suffix. */
4944 if (pipe_p)
4945 abort ();
4947 suffix = 1;
4948 ++p;
4951 if (elide_switch && (negate || pipe_p || suffix))
4953 /* It doesn't make sense to mix elision with other flags. We
4954 could fatal() here, but the standard seems to be to abort. */
4955 abort ();
4958 next_ampersand:
4959 filter = p;
4960 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
4961 p++;
4963 if (*p == '|' && (pipe_p || ordered))
4964 abort ();
4966 if (!body)
4968 if (*p != '}' && *p != '&')
4970 register int count = 1;
4971 register const char *q = p;
4973 while (*q++ != ':')
4974 continue;
4975 body = q;
4977 while (count > 0)
4979 if (*q == '{')
4980 count++;
4981 else if (*q == '}')
4982 count--;
4983 else if (*q == 0)
4984 abort ();
4985 q++;
4987 endbody = q;
4989 else
4990 body = p, endbody = p + 1;
4993 if (suffix)
4995 int found = (input_suffix != 0
4996 && (long) strlen (input_suffix) == (long) (p - filter)
4997 && strncmp (input_suffix, filter, p - filter) == 0);
4999 if (body[0] == '}')
5000 abort ();
5002 if (negate != found
5003 && do_spec_1 (save_string (body, endbody-body-1), 0, NULL_PTR) < 0)
5004 return 0;
5006 else if (p[-1] == '*' && (p[0] == '}' || p[0] == '&'))
5008 /* Substitute all matching switches as separate args. */
5009 register int i;
5011 for (i = 0; i < n_switches; i++)
5012 if (!strncmp (switches[i].part1, filter, p - 1 - filter)
5013 && check_live_switch (i, p - 1 - filter))
5015 if (elide_switch)
5017 switches[i].live_cond = SWITCH_IGNORE;
5018 switches[i].validated = 1;
5020 else
5021 ordered = 1, switches[i].ordering = 1;
5024 else
5026 /* Test for presence of the specified switch. */
5027 register int i;
5028 int present = 0;
5030 /* If name specified ends in *, as in {x*:...},
5031 check for %* and handle that case. */
5032 if (p[-1] == '*' && !negate)
5034 int substitution;
5035 const char *r = body;
5037 /* First see whether we have %*. */
5038 substitution = 0;
5039 while (r < endbody)
5041 if (*r == '%' && r[1] == '*')
5042 substitution = 1;
5043 r++;
5045 /* If we do, handle that case. */
5046 if (substitution)
5048 /* Substitute all matching switches as separate args.
5049 But do this by substituting for %*
5050 in the text that follows the colon. */
5052 unsigned hard_match_len = p - filter - 1;
5053 char *string = save_string (body, endbody - body - 1);
5055 for (i = 0; i < n_switches; i++)
5056 if (!strncmp (switches[i].part1, filter, hard_match_len)
5057 && check_live_switch (i, -1))
5059 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
5060 /* Pass any arguments this switch has. */
5061 give_switch (i, 1, 1);
5062 suffix_subst = NULL;
5065 /* We didn't match. Try again. */
5066 if (*p++ == '|')
5067 goto next_member;
5068 return endbody;
5072 /* If name specified ends in *, as in {x*:...},
5073 check for presence of any switch name starting with x. */
5074 if (p[-1] == '*')
5076 for (i = 0; i < n_switches; i++)
5078 unsigned hard_match_len = p - filter - 1;
5080 if (!strncmp (switches[i].part1, filter, hard_match_len)
5081 && check_live_switch (i, hard_match_len))
5083 present = 1;
5084 break;
5088 /* Otherwise, check for presence of exact name specified. */
5089 else
5091 for (i = 0; i < n_switches; i++)
5093 if (!strncmp (switches[i].part1, filter, p - filter)
5094 && switches[i].part1[p - filter] == 0
5095 && check_live_switch (i, -1))
5097 present = 1;
5098 break;
5103 /* If it is as desired (present for %{s...}, absent for %{!s...})
5104 then substitute either the switch or the specified
5105 conditional text. */
5106 if (present != negate)
5108 if (elide_switch)
5110 switches[i].live_cond = SWITCH_IGNORE;
5111 switches[i].validated = 1;
5113 else if (ordered || *p == '&')
5114 ordered = 1, switches[i].ordering = 1;
5115 else if (*p == '}')
5116 give_switch (i, 0, include_blanks);
5117 else
5118 /* Even if many alternatives are matched, only output once. */
5119 true_once = 1;
5121 else if (pipe_p)
5123 /* Here if a %{|...} conditional fails: output a minus sign,
5124 which means "standard output" or "standard input". */
5125 do_spec_1 ("-", 0, NULL_PTR);
5126 return endbody;
5130 /* We didn't match; try again. */
5131 if (*p++ == '|')
5132 goto next_member;
5134 if (p[-1] == '&')
5136 body = 0;
5137 goto next_ampersand;
5140 if (ordered)
5142 int i;
5143 /* Doing this set of switches later preserves their command-line
5144 ordering. This is needed for e.g. -U, -D and -A. */
5145 for (i = 0; i < n_switches; i++)
5146 if (switches[i].ordering == 1)
5148 switches[i].ordering = 0;
5149 give_switch (i, 0, include_blanks);
5152 /* Process the spec just once, regardless of match count. */
5153 else if (true_once)
5155 if (do_spec_1 (save_string (body, endbody - body - 1),
5156 0, NULL_PTR) < 0)
5157 return 0;
5160 return endbody;
5163 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
5164 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
5165 spec, or -1 if either exact match or %* is used.
5167 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
5168 whose value does not begin with "no-" is obsoleted by the same value
5169 with the "no-", similarly for a switch with the "no-" prefix. */
5171 static int
5172 check_live_switch (switchnum, prefix_length)
5173 int switchnum;
5174 int prefix_length;
5176 const char *name = switches[switchnum].part1;
5177 int i;
5179 /* In the common case of {<at-most-one-letter>*}, a negating
5180 switch would always match, so ignore that case. We will just
5181 send the conflicting switches to the compiler phase. */
5182 if (prefix_length >= 0 && prefix_length <= 1)
5183 return 1;
5185 /* If we already processed this switch and determined if it was
5186 live or not, return our past determination. */
5187 if (switches[switchnum].live_cond != 0)
5188 return switches[switchnum].live_cond > 0;
5190 /* Now search for duplicate in a manner that depends on the name. */
5191 switch (*name)
5193 case 'O':
5194 for (i = switchnum + 1; i < n_switches; i++)
5195 if (switches[i].part1[0] == 'O')
5197 switches[switchnum].validated = 1;
5198 switches[switchnum].live_cond = SWITCH_FALSE;
5199 return 0;
5201 break;
5203 case 'W': case 'f': case 'm':
5204 if (! strncmp (name + 1, "no-", 3))
5206 /* We have Xno-YYY, search for XYYY. */
5207 for (i = switchnum + 1; i < n_switches; i++)
5208 if (switches[i].part1[0] == name[0]
5209 && ! strcmp (&switches[i].part1[1], &name[4]))
5211 switches[switchnum].validated = 1;
5212 switches[switchnum].live_cond = SWITCH_FALSE;
5213 return 0;
5216 else
5218 /* We have XYYY, search for Xno-YYY. */
5219 for (i = switchnum + 1; i < n_switches; i++)
5220 if (switches[i].part1[0] == name[0]
5221 && switches[i].part1[1] == 'n'
5222 && switches[i].part1[2] == 'o'
5223 && switches[i].part1[3] == '-'
5224 && !strcmp (&switches[i].part1[4], &name[1]))
5226 switches[switchnum].validated = 1;
5227 switches[switchnum].live_cond = SWITCH_FALSE;
5228 return 0;
5231 break;
5234 /* Otherwise the switch is live. */
5235 switches[switchnum].live_cond = SWITCH_LIVE;
5236 return 1;
5239 /* Pass a switch to the current accumulating command
5240 in the same form that we received it.
5241 SWITCHNUM identifies the switch; it is an index into
5242 the vector of switches gcc received, which is `switches'.
5243 This cannot fail since it never finishes a command line.
5245 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
5247 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
5248 of the switch. */
5250 static void
5251 give_switch (switchnum, omit_first_word, include_blanks)
5252 int switchnum;
5253 int omit_first_word;
5254 int include_blanks;
5256 if (switches[switchnum].live_cond == SWITCH_IGNORE)
5257 return;
5259 if (!omit_first_word)
5261 do_spec_1 ("-", 0, NULL_PTR);
5262 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
5265 if (switches[switchnum].args != 0)
5267 const char **p;
5268 for (p = switches[switchnum].args; *p; p++)
5270 const char *arg = *p;
5272 if (include_blanks)
5273 do_spec_1 (" ", 0, NULL_PTR);
5274 if (suffix_subst)
5276 unsigned length = strlen (arg);
5278 while (length-- && !IS_DIR_SEPARATOR (arg[length]))
5279 if (arg[length] == '.')
5281 ((char *)arg)[length] = 0;
5282 break;
5284 do_spec_1 (arg, 1, NULL_PTR);
5285 if (!arg[length])
5287 ((char *)arg)[length] = '.';
5288 do_spec_1 (suffix_subst, 1, NULL_PTR);
5291 else
5292 do_spec_1 (arg, 1, NULL_PTR);
5296 do_spec_1 (" ", 0, NULL_PTR);
5297 switches[switchnum].validated = 1;
5300 /* Search for a file named NAME trying various prefixes including the
5301 user's -B prefix and some standard ones.
5302 Return the absolute file name found. If nothing is found, return NAME. */
5304 static const char *
5305 find_file (name)
5306 const char *name;
5308 char *newname;
5310 /* Try multilib_dir if it is defined. */
5311 if (multilib_dir != NULL)
5313 char *try;
5315 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
5316 strcpy (try, multilib_dir);
5317 strcat (try, dir_separator_str);
5318 strcat (try, name);
5320 newname = find_a_file (&startfile_prefixes, try, R_OK);
5322 /* If we don't find it in the multi library dir, then fall
5323 through and look for it in the normal places. */
5324 if (newname != NULL)
5325 return newname;
5328 newname = find_a_file (&startfile_prefixes, name, R_OK);
5329 return newname ? newname : name;
5332 /* Determine whether a directory exists. If LINKER, return 0 for
5333 certain fixed names not needed by the linker. If not LINKER, it is
5334 only important to return 0 if the host machine has a small ARG_MAX
5335 limit. */
5337 static int
5338 is_directory (path1, path2, linker)
5339 const char *path1;
5340 const char *path2;
5341 int linker;
5343 int len1 = strlen (path1);
5344 int len2 = strlen (path2);
5345 char *path = (char *) alloca (3 + len1 + len2);
5346 char *cp;
5347 struct stat st;
5349 #ifndef SMALL_ARG_MAX
5350 if (! linker)
5351 return 1;
5352 #endif
5354 /* Construct the path from the two parts. Ensure the string ends with "/.".
5355 The resulting path will be a directory even if the given path is a
5356 symbolic link. */
5357 memcpy (path, path1, len1);
5358 memcpy (path + len1, path2, len2);
5359 cp = path + len1 + len2;
5360 if (!IS_DIR_SEPARATOR (cp[-1]))
5361 *cp++ = DIR_SEPARATOR;
5362 *cp++ = '.';
5363 *cp = '\0';
5365 /* Exclude directories that the linker is known to search. */
5366 if (linker
5367 && ((cp - path == 6
5368 && strcmp (path, concat (dir_separator_str, "lib",
5369 dir_separator_str, ".", NULL_PTR)) == 0)
5370 || (cp - path == 10
5371 && strcmp (path, concat (dir_separator_str, "usr",
5372 dir_separator_str, "lib",
5373 dir_separator_str, ".", NULL_PTR)) == 0)))
5374 return 0;
5376 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
5379 /* Set up the various global variables to indicate that we're processing
5380 the input file named FILENAME. */
5382 static void
5383 set_input (filename)
5384 const char *filename;
5386 register const char *p;
5388 input_filename = filename;
5389 input_filename_length = strlen (input_filename);
5391 input_basename = input_filename;
5392 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
5393 /* Skip drive name so 'x:foo' is handled properly. */
5394 if (input_basename[1] == ':')
5395 input_basename += 2;
5396 #endif
5397 for (p = input_basename; *p; p++)
5398 if (IS_DIR_SEPARATOR (*p))
5399 input_basename = p + 1;
5401 /* Find a suffix starting with the last period,
5402 and set basename_length to exclude that suffix. */
5403 basename_length = strlen (input_basename);
5404 suffixed_basename_length = basename_length;
5405 p = input_basename + basename_length;
5406 while (p != input_basename && *p != '.')
5407 --p;
5408 if (*p == '.' && p != input_basename)
5410 basename_length = p - input_basename;
5411 input_suffix = p + 1;
5413 else
5414 input_suffix = "";
5417 /* On fatal signals, delete all the temporary files. */
5419 static void
5420 fatal_error (signum)
5421 int signum;
5423 signal (signum, SIG_DFL);
5424 delete_failure_queue ();
5425 delete_temp_files ();
5426 /* Get the same signal again, this time not handled,
5427 so its normal effect occurs. */
5428 kill (getpid (), signum);
5431 extern int main PARAMS ((int, const char *const *));
5434 main (argc, argv)
5435 int argc;
5436 const char *const *argv;
5438 size_t i;
5439 int value;
5440 int linker_was_run = 0;
5441 char *explicit_link_files;
5442 char *specs_file;
5443 const char *p;
5444 struct user_specs *uptr;
5446 p = argv[0] + strlen (argv[0]);
5447 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
5448 --p;
5449 programname = p;
5451 xmalloc_set_program_name (programname);
5453 #ifdef GCC_DRIVER_HOST_INITIALIZATION
5454 /* Perform host dependant initialization when needed. */
5455 GCC_DRIVER_HOST_INITIALIZATION;
5456 #endif
5458 /* LC_CTYPE determines the character set used by the terminal so it has be set
5459 to output messages correctly. */
5461 #ifdef HAVE_LC_MESSAGES
5462 setlocale (LC_CTYPE, "");
5463 setlocale (LC_MESSAGES, "");
5464 #else
5465 setlocale (LC_ALL, "");
5466 #endif
5468 (void) bindtextdomain (PACKAGE, localedir);
5469 (void) textdomain (PACKAGE);
5471 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
5472 signal (SIGINT, fatal_error);
5473 #ifdef SIGHUP
5474 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
5475 signal (SIGHUP, fatal_error);
5476 #endif
5477 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
5478 signal (SIGTERM, fatal_error);
5479 #ifdef SIGPIPE
5480 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
5481 signal (SIGPIPE, fatal_error);
5482 #endif
5483 #ifdef SIGCHLD
5484 /* We *MUST* set SIGCHLD to SIG_DFL so that the wait4() call will
5485 receive the signal. A different setting is inheritable */
5486 signal (SIGCHLD, SIG_DFL);
5487 #endif
5489 argbuf_length = 10;
5490 argbuf = (const char **) xmalloc (argbuf_length * sizeof (const char *));
5492 obstack_init (&obstack);
5494 /* Build multilib_select, et. al from the separate lines that make up each
5495 multilib selection. */
5497 const char *const *q = multilib_raw;
5498 int need_space;
5500 obstack_init (&multilib_obstack);
5501 while ((p = *q++) != (char *) 0)
5502 obstack_grow (&multilib_obstack, p, strlen (p));
5504 obstack_1grow (&multilib_obstack, 0);
5505 multilib_select = obstack_finish (&multilib_obstack);
5507 q = multilib_matches_raw;
5508 while ((p = *q++) != (char *) 0)
5509 obstack_grow (&multilib_obstack, p, strlen (p));
5511 obstack_1grow (&multilib_obstack, 0);
5512 multilib_matches = obstack_finish (&multilib_obstack);
5514 q = multilib_exclusions_raw;
5515 while ((p = *q++) != (char *) 0)
5516 obstack_grow (&multilib_obstack, p, strlen (p));
5518 obstack_1grow (&multilib_obstack, 0);
5519 multilib_exclusions = obstack_finish (&multilib_obstack);
5521 need_space = FALSE;
5522 for (i = 0; i < ARRAY_SIZE (multilib_defaults_raw); i++)
5524 if (need_space)
5525 obstack_1grow (&multilib_obstack, ' ');
5526 obstack_grow (&multilib_obstack,
5527 multilib_defaults_raw[i],
5528 strlen (multilib_defaults_raw[i]));
5529 need_space = TRUE;
5532 obstack_1grow (&multilib_obstack, 0);
5533 multilib_defaults = obstack_finish (&multilib_obstack);
5536 /* Set up to remember the pathname of gcc and any options
5537 needed for collect. We use argv[0] instead of programname because
5538 we need the complete pathname. */
5539 obstack_init (&collect_obstack);
5540 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=") - 1);
5541 obstack_grow (&collect_obstack, argv[0], strlen (argv[0]) + 1);
5542 putenv (obstack_finish (&collect_obstack));
5544 #ifdef INIT_ENVIRONMENT
5545 /* Set up any other necessary machine specific environment variables. */
5546 putenv (INIT_ENVIRONMENT);
5547 #endif
5549 /* Make a table of what switches there are (switches, n_switches).
5550 Make a table of specified input files (infiles, n_infiles).
5551 Decode switches that are handled locally. */
5553 process_command (argc, argv);
5556 int first_time;
5558 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
5559 the compiler. */
5560 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
5561 sizeof ("COLLECT_GCC_OPTIONS=") - 1);
5563 first_time = TRUE;
5564 for (i = 0; (int) i < n_switches; i++)
5566 const char *const *args;
5567 const char *p, *q;
5568 if (!first_time)
5569 obstack_grow (&collect_obstack, " ", 1);
5571 first_time = FALSE;
5572 obstack_grow (&collect_obstack, "'-", 2);
5573 q = switches[i].part1;
5574 while ((p = strchr (q, '\'')))
5576 obstack_grow (&collect_obstack, q, p - q);
5577 obstack_grow (&collect_obstack, "'\\''", 4);
5578 q = ++p;
5580 obstack_grow (&collect_obstack, q, strlen (q));
5581 obstack_grow (&collect_obstack, "'", 1);
5583 for (args = switches[i].args; args && *args; args++)
5585 obstack_grow (&collect_obstack, " '", 2);
5586 q = *args;
5587 while ((p = strchr (q, '\'')))
5589 obstack_grow (&collect_obstack, q, p - q);
5590 obstack_grow (&collect_obstack, "'\\''", 4);
5591 q = ++p;
5593 obstack_grow (&collect_obstack, q, strlen (q));
5594 obstack_grow (&collect_obstack, "'", 1);
5597 obstack_grow (&collect_obstack, "\0", 1);
5598 putenv (obstack_finish (&collect_obstack));
5601 /* Initialize the vector of specs to just the default.
5602 This means one element containing 0s, as a terminator. */
5604 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
5605 memcpy ((char *) compilers, (char *) default_compilers,
5606 sizeof default_compilers);
5607 n_compilers = n_default_compilers;
5609 /* Read specs from a file if there is one. */
5611 machine_suffix = concat (spec_machine, dir_separator_str,
5612 spec_version, dir_separator_str, NULL_PTR);
5613 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
5615 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
5616 /* Read the specs file unless it is a default one. */
5617 if (specs_file != 0 && strcmp (specs_file, "specs"))
5618 read_specs (specs_file, TRUE);
5619 else
5620 init_spec ();
5622 /* We need to check standard_exec_prefix/just_machine_suffix/specs
5623 for any override of as, ld and libraries. */
5624 specs_file = (char *) alloca (strlen (standard_exec_prefix)
5625 + strlen (just_machine_suffix)
5626 + sizeof ("specs"));
5628 strcpy (specs_file, standard_exec_prefix);
5629 strcat (specs_file, just_machine_suffix);
5630 strcat (specs_file, "specs");
5631 if (access (specs_file, R_OK) == 0)
5632 read_specs (specs_file, TRUE);
5634 /* If not cross-compiling, look for startfiles in the standard places. */
5635 if (*cross_compile == '0')
5637 if (*md_exec_prefix)
5639 add_prefix (&exec_prefixes, md_exec_prefix, "GCC",
5640 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5641 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC",
5642 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5645 if (*md_startfile_prefix)
5646 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
5647 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5649 if (*md_startfile_prefix_1)
5650 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
5651 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5653 /* If standard_startfile_prefix is relative, base it on
5654 standard_exec_prefix. This lets us move the installed tree
5655 as a unit. If GCC_EXEC_PREFIX is defined, base
5656 standard_startfile_prefix on that as well. */
5657 if (IS_ABSOLUTE_PATHNAME (standard_startfile_prefix))
5658 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
5659 PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5660 else
5662 if (gcc_exec_prefix)
5663 add_prefix (&startfile_prefixes,
5664 concat (gcc_exec_prefix, machine_suffix,
5665 standard_startfile_prefix, NULL_PTR),
5666 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5667 add_prefix (&startfile_prefixes,
5668 concat (standard_exec_prefix,
5669 machine_suffix,
5670 standard_startfile_prefix, NULL_PTR),
5671 NULL_PTR, PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5674 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
5675 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5676 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
5677 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5678 #if 0 /* Can cause surprises, and one can use -B./ instead. */
5679 add_prefix (&startfile_prefixes, "./", NULL_PTR,
5680 PREFIX_PRIORITY_LAST, 1, NULL_PTR);
5681 #endif
5683 else
5685 if (!IS_ABSOLUTE_PATHNAME (standard_startfile_prefix)
5686 && gcc_exec_prefix)
5687 add_prefix (&startfile_prefixes,
5688 concat (gcc_exec_prefix, machine_suffix,
5689 standard_startfile_prefix, NULL_PTR),
5690 "BINUTILS", PREFIX_PRIORITY_LAST, 0, NULL_PTR);
5693 /* Process any user specified specs in the order given on the command
5694 line. */
5695 for (uptr = user_specs_head; uptr; uptr = uptr->next)
5697 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
5698 read_specs (filename ? filename : uptr->filename, FALSE);
5701 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
5702 if (gcc_exec_prefix)
5704 char *temp = (char *) xmalloc (strlen (gcc_exec_prefix)
5705 + strlen (spec_version)
5706 + strlen (spec_machine) + 3);
5707 strcpy (temp, gcc_exec_prefix);
5708 strcat (temp, spec_machine);
5709 strcat (temp, dir_separator_str);
5710 strcat (temp, spec_version);
5711 strcat (temp, dir_separator_str);
5712 gcc_exec_prefix = temp;
5715 /* Now we have the specs.
5716 Set the `valid' bits for switches that match anything in any spec. */
5718 validate_all_switches ();
5720 /* Now that we have the switches and the specs, set
5721 the subdirectory based on the options. */
5722 set_multilib_dir ();
5724 /* Warn about any switches that no pass was interested in. */
5726 for (i = 0; (int) i < n_switches; i++)
5727 if (! switches[i].validated)
5728 error ("unrecognized option `-%s'", switches[i].part1);
5730 /* Obey some of the options. */
5732 if (print_search_dirs)
5734 printf (_("install: %s%s\n"), standard_exec_prefix, machine_suffix);
5735 printf (_("programs: %s\n"), build_search_list (&exec_prefixes, "", 0));
5736 printf (_("libraries: %s\n"), build_search_list (&startfile_prefixes, "", 0));
5737 return (0);
5740 if (print_file_name)
5742 printf ("%s\n", find_file (print_file_name));
5743 return (0);
5746 if (print_prog_name)
5748 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
5749 printf ("%s\n", (newname ? newname : print_prog_name));
5750 return (0);
5753 if (print_multi_lib)
5755 print_multilib_info ();
5756 return (0);
5759 if (print_multi_directory)
5761 if (multilib_dir == NULL)
5762 printf (".\n");
5763 else
5764 printf ("%s\n", multilib_dir);
5765 return (0);
5768 if (target_help_flag)
5770 /* Print if any target specific options.*/
5772 /* We do not exit here. Instead we have created a fake input file
5773 called 'target-dummy' which needs to be compiled, and we pass this
5774 on to the various sub-processes, along with the --target-help
5775 switch. */
5778 if (print_help_list)
5780 display_help ();
5782 if (! verbose_flag)
5784 printf (_("\nFor bug reporting instructions, please see:\n"));
5785 printf ("%s.\n", GCCBUGURL);
5787 return (0);
5790 /* We do not exit here. Instead we have created a fake input file
5791 called 'help-dummy' which needs to be compiled, and we pass this
5792 on the the various sub-processes, along with the --help switch. */
5795 if (verbose_flag)
5797 int n;
5798 const char *thrmod;
5800 notice ("Configured with: %s\n", configuration_arguments);
5802 #ifdef THREAD_MODEL_SPEC
5803 /* We could have defined THREAD_MODEL_SPEC to "%*" by default,
5804 but there's no point in doing all this processing just to get
5805 thread_model back. */
5806 obstack_init (&obstack);
5807 do_spec_1 (THREAD_MODEL_SPEC, 0, thread_model);
5808 obstack_1grow (&obstack, '\0');
5809 thrmod = obstack_finish (&obstack);
5810 #else
5811 thrmod = thread_model;
5812 #endif
5814 notice ("Thread model: %s\n", thrmod);
5816 /* compiler_version is truncated at the first space when initialized
5817 from version string, so truncate version_string at the first space
5818 before comparing. */
5819 for (n = 0; version_string[n]; n++)
5820 if (version_string[n] == ' ')
5821 break;
5823 if (! strncmp (version_string, compiler_version, n)
5824 && compiler_version[n] == 0)
5825 notice ("gcc version %s\n", version_string);
5826 else
5827 notice ("gcc driver version %s executing gcc version %s\n",
5828 version_string, compiler_version);
5830 if (n_infiles == 0)
5831 return (0);
5834 if (n_infiles == added_libraries)
5835 fatal ("No input files");
5837 /* Make a place to record the compiler output file names
5838 that correspond to the input files. */
5840 i = n_infiles;
5841 i += lang_specific_extra_outfiles;
5842 outfiles = (const char **) xcalloc (i, sizeof (char *));
5844 /* Record which files were specified explicitly as link input. */
5846 explicit_link_files = xcalloc (1, n_infiles);
5848 for (i = 0; (int) i < n_infiles; i++)
5850 int this_file_error = 0;
5852 /* Tell do_spec what to substitute for %i. */
5854 input_file_number = i;
5855 set_input (infiles[i].name);
5857 /* Use the same thing in %o, unless cp->spec says otherwise. */
5859 outfiles[i] = input_filename;
5861 /* Figure out which compiler from the file's suffix. */
5863 input_file_compiler
5864 = lookup_compiler (infiles[i].name, input_filename_length,
5865 infiles[i].language);
5867 if (input_file_compiler)
5869 /* Ok, we found an applicable compiler. Run its spec. */
5871 if (input_file_compiler->spec[0] == '#')
5872 error ("%s: %s compiler not installed on this system",
5873 input_filename, &input_file_compiler->spec[1]);
5874 value = do_spec (input_file_compiler->spec);
5875 if (value < 0)
5876 this_file_error = 1;
5879 /* If this file's name does not contain a recognized suffix,
5880 record it as explicit linker input. */
5882 else
5883 explicit_link_files[i] = 1;
5885 /* Clear the delete-on-failure queue, deleting the files in it
5886 if this compilation failed. */
5888 if (this_file_error)
5890 delete_failure_queue ();
5891 error_count++;
5893 /* If this compilation succeeded, don't delete those files later. */
5894 clear_failure_queue ();
5897 /* Reset the output file name to the first input file name, for use
5898 with %b in LINK_SPEC on a target that prefers not to emit a.out
5899 by default. */
5900 if (n_infiles > 0)
5901 set_input (infiles[0].name);
5903 if (error_count == 0)
5905 /* Make sure INPUT_FILE_NUMBER points to first available open
5906 slot. */
5907 input_file_number = n_infiles;
5908 if (lang_specific_pre_link ())
5909 error_count++;
5912 /* Run ld to link all the compiler output files. */
5914 if (error_count == 0)
5916 int tmp = execution_count;
5918 /* We'll use ld if we can't find collect2. */
5919 if (! strcmp (linker_name_spec, "collect2"))
5921 char *s = find_a_file (&exec_prefixes, "collect2", X_OK);
5922 if (s == NULL)
5923 linker_name_spec = "ld";
5925 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
5926 for collect. */
5927 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH");
5928 putenv_from_prefixes (&startfile_prefixes, LIBRARY_PATH_ENV);
5930 value = do_spec (link_command_spec);
5931 if (value < 0)
5932 error_count = 1;
5933 linker_was_run = (tmp != execution_count);
5936 /* If options said don't run linker,
5937 complain about input files to be given to the linker. */
5939 if (! linker_was_run && error_count == 0)
5940 for (i = 0; (int) i < n_infiles; i++)
5941 if (explicit_link_files[i])
5942 error ("%s: linker input file unused because linking not done",
5943 outfiles[i]);
5945 /* Delete some or all of the temporary files we made. */
5947 if (error_count)
5948 delete_failure_queue ();
5949 delete_temp_files ();
5951 if (print_help_list)
5953 printf (("\nFor bug reporting instructions, please see:\n"));
5954 printf ("%s\n", GCCBUGURL);
5957 return (signal_count != 0 ? 2
5958 : error_count > 0 ? (pass_exit_codes ? greatest_status : 1)
5959 : 0);
5962 /* Find the proper compilation spec for the file name NAME,
5963 whose length is LENGTH. LANGUAGE is the specified language,
5964 or 0 if this file is to be passed to the linker. */
5966 static struct compiler *
5967 lookup_compiler (name, length, language)
5968 const char *name;
5969 size_t length;
5970 const char *language;
5972 struct compiler *cp;
5974 /* If this was specified by the user to be a linker input, indicate that. */
5975 if (language != 0 && language[0] == '*')
5976 return 0;
5978 /* Otherwise, look for the language, if one is spec'd. */
5979 if (language != 0)
5981 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5982 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
5983 return cp;
5985 error ("language %s not recognized", language);
5986 return 0;
5989 /* Look for a suffix. */
5990 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
5992 if (/* The suffix `-' matches only the file name `-'. */
5993 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
5994 || (strlen (cp->suffix) < length
5995 /* See if the suffix matches the end of NAME. */
5996 && !strcmp (cp->suffix,
5997 name + length - strlen (cp->suffix))
5999 break;
6002 #if defined (OS2) ||defined (HAVE_DOS_BASED_FILE_SYSTEM)
6003 /* look again, but case-insensitively this time. */
6004 if (cp < compilers)
6005 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
6007 if (/* The suffix `-' matches only the file name `-'. */
6008 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
6009 || (strlen (cp->suffix) < length
6010 /* See if the suffix matches the end of NAME. */
6011 && ((!strcmp (cp->suffix,
6012 name + length - strlen (cp->suffix))
6013 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
6014 && !strcasecmp (cp->suffix,
6015 name + length - strlen (cp->suffix)))
6017 break;
6019 #endif
6021 if (cp >= compilers)
6023 if (cp->spec[0] != '@')
6024 /* A non-alias entry: return it. */
6025 return cp;
6027 /* An alias entry maps a suffix to a language.
6028 Search for the language; pass 0 for NAME and LENGTH
6029 to avoid infinite recursion if language not found. */
6030 return lookup_compiler (NULL_PTR, 0, cp->spec + 1);
6032 return 0;
6035 static char *
6036 save_string (s, len)
6037 const char *s;
6038 int len;
6040 register char *result = xmalloc (len + 1);
6042 memcpy (result, s, len);
6043 result[len] = 0;
6044 return result;
6047 void
6048 pfatal_with_name (name)
6049 const char *name;
6051 perror_with_name (name);
6052 delete_temp_files ();
6053 exit (1);
6056 static void
6057 perror_with_name (name)
6058 const char *name;
6060 error ("%s: %s", name, xstrerror (errno));
6063 static void
6064 pfatal_pexecute (errmsg_fmt, errmsg_arg)
6065 const char *errmsg_fmt;
6066 const char *errmsg_arg;
6068 if (errmsg_arg)
6070 int save_errno = errno;
6072 /* Space for trailing '\0' is in %s. */
6073 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
6074 sprintf (msg, errmsg_fmt, errmsg_arg);
6075 errmsg_fmt = msg;
6077 errno = save_errno;
6080 pfatal_with_name (errmsg_fmt);
6083 /* Output an error message and exit */
6085 void
6086 fancy_abort ()
6088 fatal ("Internal gcc abort.");
6091 /* Output an error message and exit */
6093 void
6094 fatal VPARAMS ((const char *msgid, ...))
6096 #ifndef ANSI_PROTOTYPES
6097 const char *msgid;
6098 #endif
6099 va_list ap;
6101 VA_START (ap, msgid);
6103 #ifndef ANSI_PROTOTYPES
6104 msgid = va_arg (ap, const char *);
6105 #endif
6107 fprintf (stderr, "%s: ", programname);
6108 vfprintf (stderr, _(msgid), ap);
6109 va_end (ap);
6110 fprintf (stderr, "\n");
6111 delete_temp_files ();
6112 exit (1);
6115 void
6116 error VPARAMS ((const char *msgid, ...))
6118 #ifndef ANSI_PROTOTYPES
6119 const char *msgid;
6120 #endif
6121 va_list ap;
6123 VA_START (ap, msgid);
6125 #ifndef ANSI_PROTOTYPES
6126 msgid = va_arg (ap, const char *);
6127 #endif
6129 fprintf (stderr, "%s: ", programname);
6130 vfprintf (stderr, _(msgid), ap);
6131 va_end (ap);
6133 fprintf (stderr, "\n");
6136 static void
6137 notice VPARAMS ((const char *msgid, ...))
6139 #ifndef ANSI_PROTOTYPES
6140 const char *msgid;
6141 #endif
6142 va_list ap;
6144 VA_START (ap, msgid);
6146 #ifndef ANSI_PROTOTYPES
6147 msgid = va_arg (ap, const char *);
6148 #endif
6150 vfprintf (stderr, _(msgid), ap);
6151 va_end (ap);
6154 static void
6155 validate_all_switches ()
6157 struct compiler *comp;
6158 register const char *p;
6159 register char c;
6160 struct spec_list *spec;
6162 for (comp = compilers; comp->spec; comp++)
6164 p = comp->spec;
6165 while ((c = *p++))
6166 if (c == '%' && *p == '{')
6167 /* We have a switch spec. */
6168 validate_switches (p + 1);
6171 /* Look through the linked list of specs read from the specs file. */
6172 for (spec = specs; spec; spec = spec->next)
6174 p = *(spec->ptr_spec);
6175 while ((c = *p++))
6176 if (c == '%' && *p == '{')
6177 /* We have a switch spec. */
6178 validate_switches (p + 1);
6181 p = link_command_spec;
6182 while ((c = *p++))
6183 if (c == '%' && *p == '{')
6184 /* We have a switch spec. */
6185 validate_switches (p + 1);
6188 /* Look at the switch-name that comes after START
6189 and mark as valid all supplied switches that match it. */
6191 static void
6192 validate_switches (start)
6193 const char *start;
6195 register const char *p = start;
6196 const char *filter;
6197 register int i;
6198 int suffix;
6200 if (*p == '|')
6201 ++p;
6203 next_member:
6204 if (*p == '!')
6205 ++p;
6207 suffix = 0;
6208 if (*p == '.')
6209 suffix = 1, ++p;
6211 filter = p;
6212 while (*p != ':' && *p != '}' && *p != '|' && *p != '&')
6213 p++;
6215 if (suffix)
6217 else if (p[-1] == '*')
6219 /* Mark all matching switches as valid. */
6220 for (i = 0; i < n_switches; i++)
6221 if (!strncmp (switches[i].part1, filter, p - filter - 1))
6222 switches[i].validated = 1;
6224 else
6226 /* Mark an exact matching switch as valid. */
6227 for (i = 0; i < n_switches; i++)
6229 if (!strncmp (switches[i].part1, filter, p - filter)
6230 && switches[i].part1[p - filter] == 0)
6231 switches[i].validated = 1;
6235 if (*p++ == '|' || p[-1] == '&')
6236 goto next_member;
6239 /* Check whether a particular argument was used. The first time we
6240 canonicalize the switches to keep only the ones we care about. */
6242 static int
6243 used_arg (p, len)
6244 const char *p;
6245 int len;
6247 struct mswitchstr
6249 const char *str;
6250 const char *replace;
6251 int len;
6252 int rep_len;
6255 static struct mswitchstr *mswitches;
6256 static int n_mswitches;
6257 int i, j;
6259 if (!mswitches)
6261 struct mswitchstr *matches;
6262 const char *q;
6263 int cnt = 0;
6265 /* Break multilib_matches into the component strings of string
6266 and replacement string. */
6267 for (q = multilib_matches; *q != '\0'; q++)
6268 if (*q == ';')
6269 cnt++;
6271 matches =
6272 (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
6273 i = 0;
6274 q = multilib_matches;
6275 while (*q != '\0')
6277 matches[i].str = q;
6278 while (*q != ' ')
6280 if (*q == '\0')
6281 abort ();
6282 q++;
6284 matches[i].len = q - matches[i].str;
6286 matches[i].replace = ++q;
6287 while (*q != ';' && *q != '\0')
6289 if (*q == ' ')
6290 abort ();
6291 q++;
6293 matches[i].rep_len = q - matches[i].replace;
6294 i++;
6295 if (*q == ';')
6296 q++;
6299 /* Now build a list of the replacement string for switches that we care
6300 about. Make sure we allocate at least one entry. This prevents
6301 xmalloc from calling fatal, and prevents us from re-executing this
6302 block of code. */
6303 mswitches
6304 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
6305 * (n_switches ? n_switches : 1));
6306 for (i = 0; i < n_switches; i++)
6308 int xlen = strlen (switches[i].part1);
6309 for (j = 0; j < cnt; j++)
6310 if (xlen == matches[j].len
6311 && ! strncmp (switches[i].part1, matches[j].str, xlen))
6313 mswitches[n_mswitches].str = matches[j].replace;
6314 mswitches[n_mswitches].len = matches[j].rep_len;
6315 mswitches[n_mswitches].replace = (char *) 0;
6316 mswitches[n_mswitches].rep_len = 0;
6317 n_mswitches++;
6318 break;
6323 for (i = 0; i < n_mswitches; i++)
6324 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
6325 return 1;
6327 return 0;
6330 static int
6331 default_arg (p, len)
6332 const char *p;
6333 int len;
6335 const char *start, *end;
6337 for (start = multilib_defaults; *start != '\0'; start = end + 1)
6339 while (*start == ' ' || *start == '\t')
6340 start++;
6342 if (*start == '\0')
6343 break;
6345 for (end = start + 1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
6348 if ((end - start) == len && strncmp (p, start, len) == 0)
6349 return 1;
6351 if (*end == '\0')
6352 break;
6355 return 0;
6358 /* Work out the subdirectory to use based on the options. The format of
6359 multilib_select is a list of elements. Each element is a subdirectory
6360 name followed by a list of options followed by a semicolon. The format
6361 of multilib_exclusions is the same, but without the preceding
6362 directory. First gcc will check the exclusions, if none of the options
6363 beginning with an exclamation point are present, and all of the other
6364 options are present, then we will ignore this completely. Passing
6365 that, gcc will consider each multilib_select in turn using the same
6366 rules for matching the options. If a match is found, that subdirectory
6367 will be used. */
6369 static void
6370 set_multilib_dir ()
6372 const char *p;
6373 unsigned int this_path_len;
6374 const char *this_path, *this_arg;
6375 int not_arg;
6376 int ok;
6378 p = multilib_exclusions;
6379 while (*p != '\0')
6381 /* Ignore newlines. */
6382 if (*p == '\n')
6384 ++p;
6385 continue;
6388 /* Check the arguments. */
6389 ok = 1;
6390 while (*p != ';')
6392 if (*p == '\0')
6393 abort ();
6395 if (! ok)
6397 ++p;
6398 continue;
6401 this_arg = p;
6402 while (*p != ' ' && *p != ';')
6404 if (*p == '\0')
6405 abort ();
6406 ++p;
6409 if (*this_arg != '!')
6410 not_arg = 0;
6411 else
6413 not_arg = 1;
6414 ++this_arg;
6417 ok = used_arg (this_arg, p - this_arg);
6418 if (not_arg)
6419 ok = ! ok;
6421 if (*p == ' ')
6422 ++p;
6425 if (ok)
6426 return;
6428 ++p;
6431 p = multilib_select;
6432 while (*p != '\0')
6434 /* Ignore newlines. */
6435 if (*p == '\n')
6437 ++p;
6438 continue;
6441 /* Get the initial path. */
6442 this_path = p;
6443 while (*p != ' ')
6445 if (*p == '\0')
6446 abort ();
6447 ++p;
6449 this_path_len = p - this_path;
6451 /* Check the arguments. */
6452 ok = 1;
6453 ++p;
6454 while (*p != ';')
6456 if (*p == '\0')
6457 abort ();
6459 if (! ok)
6461 ++p;
6462 continue;
6465 this_arg = p;
6466 while (*p != ' ' && *p != ';')
6468 if (*p == '\0')
6469 abort ();
6470 ++p;
6473 if (*this_arg != '!')
6474 not_arg = 0;
6475 else
6477 not_arg = 1;
6478 ++this_arg;
6481 /* If this is a default argument, we can just ignore it.
6482 This is true even if this_arg begins with '!'. Beginning
6483 with '!' does not mean that this argument is necessarily
6484 inappropriate for this library: it merely means that
6485 there is a more specific library which uses this
6486 argument. If this argument is a default, we need not
6487 consider that more specific library. */
6488 if (! default_arg (this_arg, p - this_arg))
6490 ok = used_arg (this_arg, p - this_arg);
6491 if (not_arg)
6492 ok = ! ok;
6495 if (*p == ' ')
6496 ++p;
6499 if (ok)
6501 if (this_path_len != 1
6502 || this_path[0] != '.')
6504 char *new_multilib_dir = xmalloc (this_path_len + 1);
6505 strncpy (new_multilib_dir, this_path, this_path_len);
6506 new_multilib_dir[this_path_len] = '\0';
6507 multilib_dir = new_multilib_dir;
6509 break;
6512 ++p;
6516 /* Print out the multiple library subdirectory selection
6517 information. This prints out a series of lines. Each line looks
6518 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
6519 required. Only the desired options are printed out, the negative
6520 matches. The options are print without a leading dash. There are
6521 no spaces to make it easy to use the information in the shell.
6522 Each subdirectory is printed only once. This assumes the ordering
6523 generated by the genmultilib script. Also, we leave out ones that match
6524 the exclusions. */
6526 static void
6527 print_multilib_info ()
6529 const char *p = multilib_select;
6530 const char *last_path = 0, *this_path;
6531 int skip;
6532 unsigned int last_path_len = 0;
6534 while (*p != '\0')
6536 skip = 0;
6537 /* Ignore newlines. */
6538 if (*p == '\n')
6540 ++p;
6541 continue;
6544 /* Get the initial path. */
6545 this_path = p;
6546 while (*p != ' ')
6548 if (*p == '\0')
6549 abort ();
6550 ++p;
6553 /* Check for matches with the multilib_exclusions. We don't bother
6554 with the '!' in either list. If any of the exclusion rules match
6555 all of its options with the select rule, we skip it. */
6557 const char *e = multilib_exclusions;
6558 const char *this_arg;
6560 while (*e != '\0')
6562 int m = 1;
6563 /* Ignore newlines. */
6564 if (*e == '\n')
6566 ++e;
6567 continue;
6570 /* Check the arguments. */
6571 while (*e != ';')
6573 const char *q;
6574 int mp = 0;
6576 if (*e == '\0')
6577 abort ();
6579 if (! m)
6581 ++e;
6582 continue;
6585 this_arg = e;
6587 while (*e != ' ' && *e != ';')
6589 if (*e == '\0')
6590 abort ();
6591 ++e;
6594 q = p + 1;
6595 while (*q != ';')
6597 const char *arg;
6598 int len = e - this_arg;
6600 if (*q == '\0')
6601 abort ();
6603 arg = q;
6605 while (*q != ' ' && *q != ';')
6607 if (*q == '\0')
6608 abort ();
6609 ++q;
6612 if (! strncmp (arg, this_arg, (len < q - arg) ? q - arg : len) ||
6613 default_arg (this_arg, e - this_arg))
6615 mp = 1;
6616 break;
6619 if (*q == ' ')
6620 ++q;
6623 if (! mp)
6624 m = 0;
6626 if (*e == ' ')
6627 ++e;
6630 if (m)
6632 skip = 1;
6633 break;
6636 if (*e != '\0')
6637 ++e;
6641 if (! skip)
6643 /* If this is a duplicate, skip it. */
6644 skip = (last_path != 0 && (unsigned int) (p - this_path) == last_path_len
6645 && ! strncmp (last_path, this_path, last_path_len));
6647 last_path = this_path;
6648 last_path_len = p - this_path;
6651 /* If this directory requires any default arguments, we can skip
6652 it. We will already have printed a directory identical to
6653 this one which does not require that default argument. */
6654 if (! skip)
6656 const char *q;
6658 q = p + 1;
6659 while (*q != ';')
6661 const char *arg;
6663 if (*q == '\0')
6664 abort ();
6666 if (*q == '!')
6667 arg = NULL;
6668 else
6669 arg = q;
6671 while (*q != ' ' && *q != ';')
6673 if (*q == '\0')
6674 abort ();
6675 ++q;
6678 if (arg != NULL
6679 && default_arg (arg, q - arg))
6681 skip = 1;
6682 break;
6685 if (*q == ' ')
6686 ++q;
6690 if (! skip)
6692 const char *p1;
6694 for (p1 = last_path; p1 < p; p1++)
6695 putchar (*p1);
6696 putchar (';');
6699 ++p;
6700 while (*p != ';')
6702 int use_arg;
6704 if (*p == '\0')
6705 abort ();
6707 if (skip)
6709 ++p;
6710 continue;
6713 use_arg = *p != '!';
6715 if (use_arg)
6716 putchar ('@');
6718 while (*p != ' ' && *p != ';')
6720 if (*p == '\0')
6721 abort ();
6722 if (use_arg)
6723 putchar (*p);
6724 ++p;
6727 if (*p == ' ')
6728 ++p;
6731 if (! skip)
6733 /* If there are extra options, print them now. */
6734 if (multilib_extra && *multilib_extra)
6736 int print_at = TRUE;
6737 const char *q;
6739 for (q = multilib_extra; *q != '\0'; q++)
6741 if (*q == ' ')
6742 print_at = TRUE;
6743 else
6745 if (print_at)
6746 putchar ('@');
6747 putchar (*q);
6748 print_at = FALSE;
6753 putchar ('\n');
6756 ++p;