Import gcc-2.8.1.tar.bz2
[official-gcc.git] / gcc / gcc.c
blobc548d7fdb62037d5942c66d3213bba4cf7406752
1 /* Compiler driver program that can handle many languages.
2 Copyright (C) 1987, 89, 92-97, 1998 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 This paragraph is here to try to keep Sun CC from dying.
22 The number of chars here seems crucial!!!! */
24 /* This program is the user interface to the C compiler and possibly to
25 other compilers. It is used because compilation is a complicated procedure
26 which involves running several programs and passing temporary files between
27 them, forwarding the users switches to those programs selectively,
28 and deleting the temporary files at the end.
30 CC recognizes how to compile each input file by suffixes in the file names.
31 Once it knows which kind of compilation to perform, the procedure for
32 compilation is specified by a string called a "spec". */
34 #include "config.h"
36 #include <sys/types.h>
37 #include <ctype.h>
38 #include <signal.h>
39 #include <sys/stat.h>
40 #include <errno.h>
42 #ifdef HAVE_SYS_FILE_H
43 #include <sys/file.h> /* May get R_OK, etc. on some systems. */
44 #endif
46 #include "obstack.h"
47 #include "gansidecl.h"
49 #ifdef __STDC__
50 #include <stdarg.h>
51 #else
52 #include <varargs.h>
53 #endif
54 #include <stdio.h>
56 #ifndef R_OK
57 #define R_OK 4
58 #define W_OK 2
59 #define X_OK 1
60 #endif
62 /* ??? Need to find a GCC header to put these in. */
63 extern int pexecute PROTO ((const char *, char * const *, const char *,
64 const char *, char **, char **, int));
65 extern int pwait PROTO ((int, int *, int));
66 extern char *update_path PROTO((char *, char *));
67 /* Flag arguments to pexecute. */
68 #define PEXECUTE_FIRST 1
69 #define PEXECUTE_LAST 2
70 #define PEXECUTE_SEARCH 4
71 #define PEXECUTE_VERBOSE 8
73 #ifndef WIFSIGNALED
74 #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
75 #endif
76 #ifndef WTERMSIG
77 #define WTERMSIG(S) ((S) & 0x7f)
78 #endif
79 #ifndef WIFEXITED
80 #define WIFEXITED(S) (((S) & 0xff) == 0)
81 #endif
82 #ifndef WEXITSTATUS
83 #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
84 #endif
86 #ifdef VMS
87 #define exit __posix_exit
88 #endif
90 /* Define O_RDONLY if the system hasn't defined it for us. */
91 #ifndef O_RDONLY
92 #define O_RDONLY 0
93 #endif
95 #ifdef USG
96 #define vfork fork
97 #endif /* USG */
99 /* Test if something is a normal file. */
100 #ifndef S_ISREG
101 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
102 #endif
104 /* Test if something is a directory. */
105 #ifndef S_ISDIR
106 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
107 #endif
109 /* By default there is no special suffix for executables. */
110 #ifdef EXECUTABLE_SUFFIX
111 #define HAVE_EXECUTABLE_SUFFIX
112 #else
113 #define EXECUTABLE_SUFFIX ""
114 #endif
116 /* By default, the suffix for object files is ".o". */
117 #ifdef OBJECT_SUFFIX
118 #define HAVE_OBJECT_SUFFIX
119 #else
120 #define OBJECT_SUFFIX ".o"
121 #endif
123 /* By default, colon separates directories in a path. */
124 #ifndef PATH_SEPARATOR
125 #define PATH_SEPARATOR ':'
126 #endif
128 #ifndef DIR_SEPARATOR
129 #define DIR_SEPARATOR '/'
130 #endif
132 static char dir_separator_str[] = {DIR_SEPARATOR, 0};
134 #define obstack_chunk_alloc xmalloc
135 #define obstack_chunk_free free
137 extern void free ();
138 extern char *getenv ();
140 extern char *choose_temp_base PROTO((void));
142 #ifndef errno
143 extern int errno;
144 #endif
146 #ifndef HAVE_STRERROR
147 extern int sys_nerr;
148 extern char *sys_errlist[];
149 #else
150 extern char *strerror();
151 #endif
153 #ifndef HAVE_KILL
154 #define kill(p,s) raise(s)
155 #endif
157 /* If a stage of compilation returns an exit status >= 1,
158 compilation of that file ceases. */
160 #define MIN_FATAL_STATUS 1
162 /* Flag saying to print the directories gcc will search through looking for
163 programs, libraries, etc. */
165 static int print_search_dirs;
167 /* Flag saying to print the full filename of this file
168 as found through our usual search mechanism. */
170 static char *print_file_name = NULL;
172 /* As print_file_name, but search for executable file. */
174 static char *print_prog_name = NULL;
176 /* Flag saying to print the relative path we'd use to
177 find libgcc.a given the current compiler flags. */
179 static int print_multi_directory;
181 /* Flag saying to print the list of subdirectories and
182 compiler flags used to select them in a standard form. */
184 static int print_multi_lib;
186 /* Flag indicating whether we should print the command and arguments */
188 static int verbose_flag;
190 /* Nonzero means write "temp" files in source directory
191 and use the source file's name in them, and don't delete them. */
193 static int save_temps_flag;
195 /* The compiler version. */
197 static char *compiler_version;
199 /* The target version specified with -V */
201 static char *spec_version = DEFAULT_TARGET_VERSION;
203 /* The target machine specified with -b. */
205 static char *spec_machine = DEFAULT_TARGET_MACHINE;
207 /* Nonzero if cross-compiling.
208 When -b is used, the value comes from the `specs' file. */
210 #ifdef CROSS_COMPILE
211 static char *cross_compile = "1";
212 #else
213 static char *cross_compile = "0";
214 #endif
216 /* The number of errors that have occurred; the link phase will not be
217 run if this is non-zero. */
218 static int error_count = 0;
220 /* This is the obstack which we use to allocate many strings. */
222 static struct obstack obstack;
224 /* This is the obstack to build an environment variable to pass to
225 collect2 that describes all of the relevant switches of what to
226 pass the compiler in building the list of pointers to constructors
227 and destructors. */
229 static struct obstack collect_obstack;
231 extern char *version_string;
233 /* Forward declaration for prototypes. */
234 struct path_prefix;
236 static void init_spec PROTO((void));
237 static void read_specs PROTO((char *, int));
238 static void set_spec PROTO((char *, char *));
239 static struct compiler *lookup_compiler PROTO((char *, int, char *));
240 static char *build_search_list PROTO((struct path_prefix *, char *, int));
241 static void putenv_from_prefixes PROTO((struct path_prefix *, char *));
242 static char *find_a_file PROTO((struct path_prefix *, char *, int));
243 static void add_prefix PROTO((struct path_prefix *, char *, char *,
244 int, int, int *));
245 static char *skip_whitespace PROTO((char *));
246 static void record_temp_file PROTO((char *, int, int));
247 static void delete_if_ordinary PROTO((char *));
248 static void delete_temp_files PROTO((void));
249 static void delete_failure_queue PROTO((void));
250 static void clear_failure_queue PROTO((void));
251 static int check_live_switch PROTO((int, int));
252 static char *handle_braces PROTO((char *));
253 static char *save_string PROTO((char *, int));
254 static char *concat PVPROTO((char *, ...));
255 static int do_spec PROTO((char *));
256 static int do_spec_1 PROTO((char *, int, char *));
257 static char *find_file PROTO((char *));
258 static int is_directory PROTO((char *, char *, int));
259 static void validate_switches PROTO((char *));
260 static void validate_all_switches PROTO((void));
261 static void give_switch PROTO((int, int, int));
262 static int used_arg PROTO((char *, int));
263 static int default_arg PROTO((char *, int));
264 static void set_multilib_dir PROTO((void));
265 static void print_multilib_info PROTO((void));
266 static void pfatal_with_name PROTO((char *));
267 static void perror_with_name PROTO((char *));
268 static void pfatal_pexecute PROTO((char *, char *));
269 static void snapshot_warning PROTO((void));
270 #ifdef HAVE_VPRINTF
271 static void fatal PVPROTO((char *, ...));
272 static void error PVPROTO((char *, ...));
273 #else
274 /* We must not provide any prototype here, even if ANSI C. */
275 static void fatal PROTO(());
276 static void error PROTO(());
277 #endif
279 void fancy_abort ();
280 char *xmalloc ();
281 char *xrealloc ();
283 /* Specs are strings containing lines, each of which (if not blank)
284 is made up of a program name, and arguments separated by spaces.
285 The program name must be exact and start from root, since no path
286 is searched and it is unreliable to depend on the current working directory.
287 Redirection of input or output is not supported; the subprograms must
288 accept filenames saying what files to read and write.
290 In addition, the specs can contain %-sequences to substitute variable text
291 or for conditional text. Here is a table of all defined %-sequences.
292 Note that spaces are not generated automatically around the results of
293 expanding these sequences; therefore, you can concatenate them together
294 or with constant text in a single argument.
296 %% substitute one % into the program name or argument.
297 %i substitute the name of the input file being processed.
298 %b substitute the basename of the input file being processed.
299 This is the substring up to (and not including) the last period
300 and not including the directory.
301 %g substitute the temporary-file-name-base. This is a string chosen
302 once per compilation. Different temporary file names are made by
303 concatenation of constant strings on the end, as in `%g.s'.
304 %g also has the same effect of %d.
305 %u like %g, but make the temporary file name unique.
306 %U returns the last file name generated with %u.
307 %d marks the argument containing or following the %d as a
308 temporary file name, so that that file will be deleted if CC exits
309 successfully. Unlike %g, this contributes no text to the argument.
310 %w marks the argument containing or following the %w as the
311 "output file" of this compilation. This puts the argument
312 into the sequence of arguments that %o will substitute later.
313 %W{...}
314 like %{...} but mark last argument supplied within
315 as a file to be deleted on failure.
316 %o substitutes the names of all the output files, with spaces
317 automatically placed around them. You should write spaces
318 around the %o as well or the results are undefined.
319 %o is for use in the specs for running the linker.
320 Input files whose names have no recognized suffix are not compiled
321 at all, but they are included among the output files, so they will
322 be linked.
323 %O substitutes the suffix for object files.
324 %p substitutes the standard macro predefinitions for the
325 current target machine. Use this when running cpp.
326 %P like %p, but puts `__' before and after the name of each macro.
327 (Except macros that already have __.)
328 This is for ANSI C.
329 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
330 %s current argument is the name of a library or startup file of some sort.
331 Search for that file in a standard list of directories
332 and substitute the full name found.
333 %eSTR Print STR as an error message. STR is terminated by a newline.
334 Use this when inconsistent options are detected.
335 %x{OPTION} Accumulate an option for %X.
336 %X Output the accumulated linker options specified by compilations.
337 %Y Output the accumulated assembler options specified by compilations.
338 %Z Output the accumulated preprocessor options specified by compilations.
339 %v1 Substitute the major version number of GCC.
340 (For version 2.5.n, this is 2.)
341 %v2 Substitute the minor version number of GCC.
342 (For version 2.5.n, this is 5.)
343 %a process ASM_SPEC as a spec.
344 This allows config.h to specify part of the spec for running as.
345 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
346 used here. This can be used to run a post-processor after the
347 assembler has done it's job.
348 %D Dump out a -L option for each directory in startfile_prefixes.
349 If multilib_dir is set, extra entries are generated with it affixed.
350 %l process LINK_SPEC as a spec.
351 %L process LIB_SPEC as a spec.
352 %G process LIBGCC_SPEC as a spec.
353 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
354 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
355 %c process SIGNED_CHAR_SPEC as a spec.
356 %C process CPP_SPEC as a spec. A capital C is actually used here.
357 %1 process CC1_SPEC as a spec.
358 %2 process CC1PLUS_SPEC as a spec.
359 %| output "-" if the input for the current command is coming from a pipe.
360 %* substitute the variable part of a matched option. (See below.)
361 Note that each comma in the substituted string is replaced by
362 a single space.
363 %{S} substitutes the -S switch, if that switch was given to CC.
364 If that switch was not specified, this substitutes nothing.
365 Here S is a metasyntactic variable.
366 %{S*} substitutes all the switches specified to CC whose names start
367 with -S. This is used for -o, -D, -I, etc; switches that take
368 arguments. CC considers `-o foo' as being one switch whose
369 name starts with `o'. %{o*} would substitute this text,
370 including the space; thus, two arguments would be generated.
371 %{^S*} likewise, but don't put a blank between a switch and any args.
372 %{S*:X} substitutes X if one or more switches whose names start with -S are
373 specified to CC. Note that the tail part of the -S option
374 (i.e. the part matched by the `*') will be substituted for each
375 occurrence of %* within X.
376 %{S:X} substitutes X, but only if the -S switch was given to CC.
377 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
378 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
379 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
380 %{.S:X} substitutes X, but only if processing a file with suffix S.
381 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
382 %(Spec) processes a specification defined in a specs file as *Spec:
383 %[Spec] as above, but put __ around -D arguments
385 The conditional text X in a %{S:X} or %{!S:X} construct may contain
386 other nested % constructs or spaces, or even newlines. They are
387 processed as usual, as described above.
389 The -O, -f, -m, and -W switches are handled specifically in these
390 constructs. If another value of -O or the negated form of a -f, -m, or
391 -W switch is found later in the command line, the earlier switch
392 value is ignored, except with {S*} where S is just one letter; this
393 passes all matching options.
395 The character | is used to indicate that a command should be piped to
396 the following command, but only if -pipe is specified.
398 Note that it is built into CC which switches take arguments and which
399 do not. You might think it would be useful to generalize this to
400 allow each compiler's spec to say which switches take arguments. But
401 this cannot be done in a consistent fashion. CC cannot even decide
402 which input files have been specified without knowing which switches
403 take arguments, and it must know which input files to compile in order
404 to tell which compilers to run.
406 CC also knows implicitly that arguments starting in `-l' are to be
407 treated as compiler output files, and passed to the linker in their
408 proper position among the other output files. */
410 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
412 /* config.h can define ASM_SPEC to provide extra args to the assembler
413 or extra switch-translations. */
414 #ifndef ASM_SPEC
415 #define ASM_SPEC ""
416 #endif
418 /* config.h can define ASM_FINAL_SPEC to run a post processor after
419 the assembler has run. */
420 #ifndef ASM_FINAL_SPEC
421 #define ASM_FINAL_SPEC ""
422 #endif
424 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
425 or extra switch-translations. */
426 #ifndef CPP_SPEC
427 #define CPP_SPEC ""
428 #endif
430 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
431 or extra switch-translations. */
432 #ifndef CC1_SPEC
433 #define CC1_SPEC ""
434 #endif
436 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
437 or extra switch-translations. */
438 #ifndef CC1PLUS_SPEC
439 #define CC1PLUS_SPEC ""
440 #endif
442 /* config.h can define LINK_SPEC to provide extra args to the linker
443 or extra switch-translations. */
444 #ifndef LINK_SPEC
445 #define LINK_SPEC ""
446 #endif
448 /* config.h can define LIB_SPEC to override the default libraries. */
449 #ifndef LIB_SPEC
450 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
451 #endif
453 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
454 included. */
455 #ifndef LIBGCC_SPEC
456 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
457 /* Have gcc do the search for libgcc.a. */
458 #define LIBGCC_SPEC "libgcc.a%s"
459 #else
460 #define LIBGCC_SPEC "-lgcc"
461 #endif
462 #endif
464 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
465 #ifndef STARTFILE_SPEC
466 #define STARTFILE_SPEC \
467 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
468 #endif
470 /* config.h can define SWITCHES_NEED_SPACES to control which options
471 require spaces between the option and the argument. */
472 #ifndef SWITCHES_NEED_SPACES
473 #define SWITCHES_NEED_SPACES ""
474 #endif
476 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
477 #ifndef ENDFILE_SPEC
478 #define ENDFILE_SPEC ""
479 #endif
481 /* This spec is used for telling cpp whether char is signed or not. */
482 #ifndef SIGNED_CHAR_SPEC
483 /* Use #if rather than ?:
484 because MIPS C compiler rejects like ?: in initializers. */
485 #if DEFAULT_SIGNED_CHAR
486 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
487 #else
488 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
489 #endif
490 #endif
492 static char *cpp_spec = CPP_SPEC;
493 static char *cpp_predefines = CPP_PREDEFINES;
494 static char *cc1_spec = CC1_SPEC;
495 static char *cc1plus_spec = CC1PLUS_SPEC;
496 static char *signed_char_spec = SIGNED_CHAR_SPEC;
497 static char *asm_spec = ASM_SPEC;
498 static char *asm_final_spec = ASM_FINAL_SPEC;
499 static char *link_spec = LINK_SPEC;
500 static char *lib_spec = LIB_SPEC;
501 static char *libgcc_spec = LIBGCC_SPEC;
502 static char *endfile_spec = ENDFILE_SPEC;
503 static char *startfile_spec = STARTFILE_SPEC;
504 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
506 /* Some compilers have limits on line lengths, and the multilib_select
507 and/or multilib_matches strings can be very long, so we build them at
508 run time. */
509 static struct obstack multilib_obstack;
510 static char *multilib_select;
511 static char *multilib_matches;
512 static char *multilib_defaults;
513 #include "multilib.h"
515 /* Check whether a particular argument is a default argument. */
517 #ifndef MULTILIB_DEFAULTS
518 #define MULTILIB_DEFAULTS { "" }
519 #endif
521 static char *multilib_defaults_raw[] = MULTILIB_DEFAULTS;
523 struct user_specs {
524 struct user_specs *next;
525 char *filename;
528 static struct user_specs *user_specs_head, *user_specs_tail;
530 /* This defines which switch letters take arguments. */
532 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
533 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
534 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
535 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
536 || (CHAR) == 'L' || (CHAR) == 'A')
538 #ifndef SWITCH_TAKES_ARG
539 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
540 #endif
542 /* This defines which multi-letter switches take arguments. */
544 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
545 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
546 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
547 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
548 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
549 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
550 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
552 #ifndef WORD_SWITCH_TAKES_ARG
553 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
554 #endif
556 /* Record the mapping from file suffixes for compilation specs. */
558 struct compiler
560 char *suffix; /* Use this compiler for input files
561 whose names end in this suffix. */
563 char *spec[4]; /* To use this compiler, concatenate these
564 specs and pass to do_spec. */
567 /* Pointer to a vector of `struct compiler' that gives the spec for
568 compiling a file, based on its suffix.
569 A file that does not end in any of these suffixes will be passed
570 unchanged to the loader and nothing else will be done to it.
572 An entry containing two 0s is used to terminate the vector.
574 If multiple entries match a file, the last matching one is used. */
576 static struct compiler *compilers;
578 /* Number of entries in `compilers', not counting the null terminator. */
580 static int n_compilers;
582 /* The default list of file name suffixes and their compilation specs. */
584 static struct compiler default_compilers[] =
586 /* Add lists of suffixes of known languages here. If those languages
587 were not present when we built the driver, we will hit these copies
588 and be given a more meaningful error than "file not used since
589 linking is not done". */
590 {".cc", "#C++"}, {".cxx", "#C++"}, {".cpp", "#C++"}, {".c++", "#C++"},
591 {".C", "#C++"}, {".ads", "#Ada"}, {".adb", "#Ada"}, {".ada", "#Ada"},
592 {".f", "#Fortran"}, {".for", "#Fortran"}, {".F", "#Fortran"},
593 {".fpp", "#Fortran"},
594 {".p", "#Pascal"}, {".pas", "#Pascal"},
595 /* Next come the entries for C. */
596 {".c", "@c"},
597 {"@c",
598 "cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
599 %{C:%{!E:%eGNU C does not support -C without using -E}}\
600 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
601 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
602 %{ansi:-trigraphs -D__STRICT_ANSI__}\
603 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
604 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
605 %{traditional-cpp:-traditional}\
606 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
607 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
608 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
609 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
610 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
611 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
612 %{aux-info*}\
613 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
614 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
615 %{!S:as %a %Y\
616 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
617 %{!pipe:%g.s} %A\n }}}}"},
618 {"-",
619 "%{E:cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
620 %{C:%{!E:%eGNU C does not support -C without using -E}}\
621 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
622 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
623 %{ansi:-trigraphs -D__STRICT_ANSI__}\
624 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
625 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
626 %{traditional-cpp:-traditional}\
627 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
628 %i %W{o*}}\
629 %{!E:%e-E required when input is from standard input}"},
630 {".m", "@objective-c"},
631 {"@objective-c",
632 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
633 %{C:%{!E:%eGNU C does not support -C without using -E}}\
634 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
635 -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
636 %{ansi:-trigraphs -D__STRICT_ANSI__}\
637 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
638 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
639 %{traditional-cpp:-traditional}\
640 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
641 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
642 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
643 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a*}\
644 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
645 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
646 -lang-objc %{gen-decls} \
647 %{aux-info*}\
648 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
649 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
650 %{!S:as %a %Y\
651 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
652 %{!pipe:%g.s} %A\n }}}}"},
653 {".h", "@c-header"},
654 {"@c-header",
655 "%{!E:%eCompilation of header file requested} \
656 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
657 %{C:%{!E:%eGNU C does not support -C without using -E}}\
658 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
659 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
660 %{ansi:-trigraphs -D__STRICT_ANSI__}\
661 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
662 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
663 %{traditional-cpp:-traditional}\
664 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
665 %i %W{o*}"},
666 {".i", "@cpp-output"},
667 {"@cpp-output",
668 "%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a*}\
669 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi}\
670 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
671 %{aux-info*}\
672 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
673 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
674 %{!S:as %a %Y\
675 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
676 %{!pipe:%g.s} %A\n }}}}"},
677 {".s", "@assembler"},
678 {"@assembler",
679 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
680 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
681 %i %A\n }}}}"},
682 {".S", "@assembler-with-cpp"},
683 {"@assembler-with-cpp",
684 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
685 %{C:%{!E:%eGNU C does not support -C without using -E}}\
686 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
687 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
688 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
689 %{traditional-cpp:-traditional}\
690 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
691 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
692 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
693 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
694 %{!pipe:%g.s} %A\n }}}}"},
695 #include "specs.h"
696 /* Mark end of table */
697 {0, 0}
700 /* Number of elements in default_compilers, not counting the terminator. */
702 static int n_default_compilers
703 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
705 /* Here is the spec for running the linker, after compiling all files. */
707 /* -u* was put back because both BSD and SysV seem to support it. */
708 /* %{static:} simply prevents an error message if the target machine
709 doesn't handle -static. */
710 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
711 scripts which exist in user specified directories, or in standard
712 directories. */
713 #ifdef LINK_LIBGCC_SPECIAL
714 /* Don't generate -L options. */
715 static char *link_command_spec = "\
716 %{!fsyntax-only: \
717 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
718 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
719 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
720 %{static:} %{L*} %o\
721 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
722 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
723 %{T*}\
724 \n }}}}}}";
725 #else
726 /* Use -L. */
727 static char *link_command_spec = "\
728 %{!fsyntax-only: \
729 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
730 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
731 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
732 %{static:} %{L*} %D %o\
733 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
734 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
735 %{T*}\
736 \n }}}}}}";
737 #endif
739 /* A vector of options to give to the linker.
740 These options are accumulated by %x,
741 and substituted into the linker command with %X. */
742 static int n_linker_options;
743 static char **linker_options;
745 /* A vector of options to give to the assembler.
746 These options are accumulated by -Wa,
747 and substituted into the assembler command with %Y. */
748 static int n_assembler_options;
749 static char **assembler_options;
751 /* A vector of options to give to the preprocessor.
752 These options are accumulated by -Wp,
753 and substituted into the preprocessor command with %Z. */
754 static int n_preprocessor_options;
755 static char **preprocessor_options;
757 /* Define how to map long options into short ones. */
759 /* This structure describes one mapping. */
760 struct option_map
762 /* The long option's name. */
763 char *name;
764 /* The equivalent short option. */
765 char *equivalent;
766 /* Argument info. A string of flag chars; NULL equals no options.
767 a => argument required.
768 o => argument optional.
769 j => join argument to equivalent, making one word.
770 * => require other text after NAME as an argument. */
771 char *arg_info;
774 /* This is the table of mappings. Mappings are tried sequentially
775 for each option encountered; the first one that matches, wins. */
777 struct option_map option_map[] =
779 {"--all-warnings", "-Wall", 0},
780 {"--ansi", "-ansi", 0},
781 {"--assemble", "-S", 0},
782 {"--assert", "-A", "a"},
783 {"--comments", "-C", 0},
784 {"--compile", "-c", 0},
785 {"--debug", "-g", "oj"},
786 {"--define-macro", "-D", "aj"},
787 {"--dependencies", "-M", 0},
788 {"--dump", "-d", "a"},
789 {"--dumpbase", "-dumpbase", "a"},
790 {"--entry", "-e", 0},
791 {"--extra-warnings", "-W", 0},
792 {"--for-assembler", "-Wa", "a"},
793 {"--for-linker", "-Xlinker", "a"},
794 {"--force-link", "-u", "a"},
795 {"--imacros", "-imacros", "a"},
796 {"--include", "-include", "a"},
797 {"--include-barrier", "-I-", 0},
798 {"--include-directory", "-I", "aj"},
799 {"--include-directory-after", "-idirafter", "a"},
800 {"--include-prefix", "-iprefix", "a"},
801 {"--include-with-prefix", "-iwithprefix", "a"},
802 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
803 {"--include-with-prefix-after", "-iwithprefix", "a"},
804 {"--language", "-x", "a"},
805 {"--library-directory", "-L", "a"},
806 {"--machine", "-m", "aj"},
807 {"--machine-", "-m", "*j"},
808 {"--no-line-commands", "-P", 0},
809 {"--no-precompiled-includes", "-noprecomp", 0},
810 {"--no-standard-includes", "-nostdinc", 0},
811 {"--no-standard-libraries", "-nostdlib", 0},
812 {"--no-warnings", "-w", 0},
813 {"--optimize", "-O", "oj"},
814 {"--output", "-o", "a"},
815 {"--pedantic", "-pedantic", 0},
816 {"--pedantic-errors", "-pedantic-errors", 0},
817 {"--pipe", "-pipe", 0},
818 {"--prefix", "-B", "a"},
819 {"--preprocess", "-E", 0},
820 {"--print-search-dirs", "-print-search-dirs", 0},
821 {"--print-file-name", "-print-file-name=", "aj"},
822 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
823 {"--print-missing-file-dependencies", "-MG", 0},
824 {"--print-multi-lib", "-print-multi-lib", 0},
825 {"--print-multi-directory", "-print-multi-directory", 0},
826 {"--print-prog-name", "-print-prog-name=", "aj"},
827 {"--profile", "-p", 0},
828 {"--profile-blocks", "-a", 0},
829 {"--quiet", "-q", 0},
830 {"--save-temps", "-save-temps", 0},
831 {"--shared", "-shared", 0},
832 {"--silent", "-q", 0},
833 {"--specs", "-specs=", "aj"},
834 {"--static", "-static", 0},
835 {"--symbolic", "-symbolic", 0},
836 {"--target", "-b", "a"},
837 {"--trace-includes", "-H", 0},
838 {"--traditional", "-traditional", 0},
839 {"--traditional-cpp", "-traditional-cpp", 0},
840 {"--trigraphs", "-trigraphs", 0},
841 {"--undefine-macro", "-U", "aj"},
842 {"--use-version", "-V", "a"},
843 {"--user-dependencies", "-MM", 0},
844 {"--verbose", "-v", 0},
845 {"--version", "-dumpversion", 0},
846 {"--warn-", "-W", "*j"},
847 {"--write-dependencies", "-MD", 0},
848 {"--write-user-dependencies", "-MMD", 0},
849 {"--", "-f", "*j"}
852 /* Translate the options described by *ARGCP and *ARGVP.
853 Make a new vector and store it back in *ARGVP,
854 and store its length in *ARGVC. */
856 static void
857 translate_options (argcp, argvp)
858 int *argcp;
859 char ***argvp;
861 int i, j, k;
862 int argc = *argcp;
863 char **argv = *argvp;
864 char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
865 int newindex = 0;
867 i = 0;
868 newv[newindex++] = argv[i++];
870 while (i < argc)
872 /* Translate -- options. */
873 if (argv[i][0] == '-' && argv[i][1] == '-')
875 /* Find a mapping that applies to this option. */
876 for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
878 int optlen = strlen (option_map[j].name);
879 int arglen = strlen (argv[i]);
880 int complen = arglen > optlen ? optlen : arglen;
881 char *arginfo = option_map[j].arg_info;
883 if (arginfo == 0)
884 arginfo = "";
886 if (!strncmp (argv[i], option_map[j].name, complen))
888 char *arg = 0;
890 if (arglen < optlen)
892 for (k = j + 1;
893 k < sizeof (option_map) / sizeof (option_map[0]);
894 k++)
895 if (strlen (option_map[k].name) >= arglen
896 && !strncmp (argv[i], option_map[k].name, arglen))
898 error ("Ambiguous abbreviation %s", argv[i]);
899 break;
902 if (k != sizeof (option_map) / sizeof (option_map[0]))
903 break;
906 if (arglen > optlen)
908 /* If the option has an argument, accept that. */
909 if (argv[i][optlen] == '=')
910 arg = argv[i] + optlen + 1;
912 /* If this mapping requires extra text at end of name,
913 accept that as "argument". */
914 else if (index (arginfo, '*') != 0)
915 arg = argv[i] + optlen;
917 /* Otherwise, extra text at end means mismatch.
918 Try other mappings. */
919 else
920 continue;
923 else if (index (arginfo, '*') != 0)
925 error ("Incomplete `%s' option", option_map[j].name);
926 break;
929 /* Handle arguments. */
930 if (index (arginfo, 'a') != 0)
932 if (arg == 0)
934 if (i + 1 == argc)
936 error ("Missing argument to `%s' option",
937 option_map[j].name);
938 break;
941 arg = argv[++i];
944 else if (index (arginfo, '*') != 0)
946 else if (index (arginfo, 'o') == 0)
948 if (arg != 0)
949 error ("Extraneous argument to `%s' option",
950 option_map[j].name);
951 arg = 0;
954 /* Store the translation as one argv elt or as two. */
955 if (arg != 0 && index (arginfo, 'j') != 0)
956 newv[newindex++] = concat (option_map[j].equivalent, arg,
957 NULL_PTR);
958 else if (arg != 0)
960 newv[newindex++] = option_map[j].equivalent;
961 newv[newindex++] = arg;
963 else
964 newv[newindex++] = option_map[j].equivalent;
966 break;
969 i++;
972 /* Handle old-fashioned options--just copy them through,
973 with their arguments. */
974 else if (argv[i][0] == '-')
976 char *p = argv[i] + 1;
977 int c = *p;
978 int nskip = 1;
980 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
981 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
982 else if (WORD_SWITCH_TAKES_ARG (p))
983 nskip += WORD_SWITCH_TAKES_ARG (p);
984 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
985 && p[1] == 0)
986 nskip += 1;
987 else if (! strcmp (p, "Xlinker"))
988 nskip += 1;
990 /* Watch out for an option at the end of the command line that
991 is missing arguments, and avoid skipping past the end of the
992 command line. */
993 if (nskip + i > argc)
994 nskip = argc - i;
996 while (nskip > 0)
998 newv[newindex++] = argv[i++];
999 nskip--;
1002 else
1003 /* Ordinary operands, or +e options. */
1004 newv[newindex++] = argv[i++];
1007 newv[newindex] = 0;
1009 *argvp = newv;
1010 *argcp = newindex;
1013 char *
1014 my_strerror(e)
1015 int e;
1017 #ifdef HAVE_STRERROR
1019 return strerror(e);
1021 #else
1023 static char buffer[30];
1024 if (!e)
1025 return "cannot access";
1027 if (e > 0 && e < sys_nerr)
1028 return sys_errlist[e];
1030 sprintf (buffer, "Unknown error %d", e);
1031 return buffer;
1032 #endif
1035 static char *
1036 skip_whitespace (p)
1037 char *p;
1039 while (1)
1041 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
1042 be considered whitespace. */
1043 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
1044 return p + 1;
1045 else if (*p == '\n' || *p == ' ' || *p == '\t')
1046 p++;
1047 else if (*p == '#')
1049 while (*p != '\n') p++;
1050 p++;
1052 else
1053 break;
1056 return p;
1059 /* Structure to keep track of the specs that have been defined so far.
1060 These are accessed using %(specname) or %[specname] in a compiler
1061 or link spec. */
1063 struct spec_list
1065 /* The following 2 fields must be first */
1066 /* to allow EXTRA_SPECS to be initialized */
1067 char *name; /* name of the spec. */
1068 char *ptr; /* available ptr if no static pointer */
1070 /* The following fields are not initialized */
1071 /* by EXTRA_SPECS */
1072 char **ptr_spec; /* pointer to the spec itself. */
1073 struct spec_list *next; /* Next spec in linked list. */
1074 int name_len; /* length of the name */
1075 int alloc_p; /* whether string was allocated */
1078 #define INIT_STATIC_SPEC(NAME,PTR) \
1079 { NAME, NULL_PTR, PTR, (struct spec_list *)0, sizeof (NAME)-1, 0 }
1081 /* List of statically defined specs */
1082 static struct spec_list static_specs[] = {
1083 INIT_STATIC_SPEC ("asm", &asm_spec),
1084 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1085 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1086 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1087 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1088 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1089 INIT_STATIC_SPEC ("link", &link_spec),
1090 INIT_STATIC_SPEC ("lib", &lib_spec),
1091 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1092 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1093 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1094 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1095 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1096 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1097 INIT_STATIC_SPEC ("version", &compiler_version),
1098 INIT_STATIC_SPEC ("multilib", &multilib_select),
1099 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1100 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1101 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1104 #ifdef EXTRA_SPECS /* additional specs needed */
1105 static struct spec_list extra_specs[] = { EXTRA_SPECS };
1106 #endif
1108 /* List of dynamically allocates specs that have been defined so far. */
1110 static struct spec_list *specs = (struct spec_list *)0;
1113 /* Initialize the specs lookup routines. */
1115 static void
1116 init_spec ()
1118 struct spec_list *next = (struct spec_list *)0;
1119 struct spec_list *sl = (struct spec_list *)0;
1120 int i;
1122 if (specs)
1123 return; /* already initialized */
1125 if (verbose_flag)
1126 fprintf (stderr, "Using builtin specs.\n");
1128 #ifdef EXTRA_SPECS
1129 for (i = (sizeof (extra_specs) / sizeof (extra_specs[0])) - 1; i >= 0; i--)
1131 sl = &extra_specs[i];
1132 sl->next = next;
1133 sl->name_len = strlen (sl->name);
1134 sl->ptr_spec = &sl->ptr;
1135 next = sl;
1137 #endif
1139 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1; i >= 0; i--)
1141 sl = &static_specs[i];
1142 sl->next = next;
1143 next = sl;
1146 specs = sl;
1150 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1151 removed; If the spec starts with a + then SPEC is added to the end of the
1152 current spec. */
1154 static void
1155 set_spec (name, spec)
1156 char *name;
1157 char *spec;
1159 struct spec_list *sl;
1160 char *old_spec;
1161 int name_len = strlen (name);
1162 int i;
1164 /* If this is the first call, initialize the statically allocated specs */
1165 if (!specs)
1167 struct spec_list *next = (struct spec_list *)0;
1168 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1;
1169 i >= 0; i--)
1171 sl = &static_specs[i];
1172 sl->next = next;
1173 next = sl;
1175 specs = sl;
1178 /* See if the spec already exists */
1179 for (sl = specs; sl; sl = sl->next)
1180 if (name_len == sl->name_len && !strcmp (sl->name, name))
1181 break;
1183 if (!sl)
1185 /* Not found - make it */
1186 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1187 sl->name = save_string (name, strlen (name));
1188 sl->name_len = name_len;
1189 sl->ptr_spec = &sl->ptr;
1190 sl->alloc_p = 0;
1191 *(sl->ptr_spec) = "";
1192 sl->next = specs;
1193 specs = sl;
1196 old_spec = *(sl->ptr_spec);
1197 *(sl->ptr_spec) = ((spec[0] == '+' && isspace (spec[1]))
1198 ? concat (old_spec, spec + 1, NULL_PTR)
1199 : save_string (spec, strlen (spec)));
1201 #ifdef DEBUG_SPECS
1202 if (verbose_flag)
1203 fprintf (stderr, "Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1204 #endif
1206 /* Free the old spec */
1207 if (old_spec && sl->alloc_p)
1208 free (old_spec);
1210 sl->alloc_p = 1;
1213 /* Accumulate a command (program name and args), and run it. */
1215 /* Vector of pointers to arguments in the current line of specifications. */
1217 static char **argbuf;
1219 /* Number of elements allocated in argbuf. */
1221 static int argbuf_length;
1223 /* Number of elements in argbuf currently in use (containing args). */
1225 static int argbuf_index;
1227 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1228 temp file. Used only if MKTEMP_EACH_FILE. */
1230 static struct temp_name {
1231 char *suffix; /* suffix associated with the code. */
1232 int length; /* strlen (suffix). */
1233 int unique; /* Indicates whether %g or %u/%U was used. */
1234 char *filename; /* associated filename. */
1235 int filename_length; /* strlen (filename). */
1236 struct temp_name *next;
1237 } *temp_names;
1239 /* Number of commands executed so far. */
1241 static int execution_count;
1243 /* Number of commands that exited with a signal. */
1245 static int signal_count;
1247 /* Name with which this program was invoked. */
1249 static char *programname;
1251 /* Structures to keep track of prefixes to try when looking for files. */
1253 struct prefix_list
1255 char *prefix; /* String to prepend to the path. */
1256 struct prefix_list *next; /* Next in linked list. */
1257 int require_machine_suffix; /* Don't use without machine_suffix. */
1258 /* 2 means try both machine_suffix and just_machine_suffix. */
1259 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1262 struct path_prefix
1264 struct prefix_list *plist; /* List of prefixes to try */
1265 int max_len; /* Max length of a prefix in PLIST */
1266 char *name; /* Name of this list (used in config stuff) */
1269 /* List of prefixes to try when looking for executables. */
1271 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1273 /* List of prefixes to try when looking for startup (crt0) files. */
1275 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1277 /* List of prefixes to try when looking for include files. */
1279 static struct path_prefix include_prefixes = { 0, 0, "include" };
1281 /* Suffix to attach to directories searched for commands.
1282 This looks like `MACHINE/VERSION/'. */
1284 static char *machine_suffix = 0;
1286 /* Suffix to attach to directories searched for commands.
1287 This is just `MACHINE/'. */
1289 static char *just_machine_suffix = 0;
1291 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1293 static char *gcc_exec_prefix;
1295 /* Default prefixes to attach to command names. */
1297 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1298 #undef MD_EXEC_PREFIX
1299 #undef MD_STARTFILE_PREFIX
1300 #undef MD_STARTFILE_PREFIX_1
1301 #endif
1303 #ifndef STANDARD_EXEC_PREFIX
1304 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1305 #endif /* !defined STANDARD_EXEC_PREFIX */
1307 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1308 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1309 #ifdef MD_EXEC_PREFIX
1310 static char *md_exec_prefix = MD_EXEC_PREFIX;
1311 #endif
1313 #ifndef STANDARD_STARTFILE_PREFIX
1314 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1315 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1317 #ifdef MD_STARTFILE_PREFIX
1318 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1319 #endif
1320 #ifdef MD_STARTFILE_PREFIX_1
1321 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1322 #endif
1323 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1324 static char *standard_startfile_prefix_1 = "/lib/";
1325 static char *standard_startfile_prefix_2 = "/usr/lib/";
1327 #ifndef TOOLDIR_BASE_PREFIX
1328 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1329 #endif
1330 static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1331 static char *tooldir_prefix;
1333 /* Subdirectory to use for locating libraries. Set by
1334 set_multilib_dir based on the compilation options. */
1336 static char *multilib_dir;
1338 /* Clear out the vector of arguments (after a command is executed). */
1340 static void
1341 clear_args ()
1343 argbuf_index = 0;
1346 /* Add one argument to the vector at the end.
1347 This is done when a space is seen or at the end of the line.
1348 If DELETE_ALWAYS is nonzero, the arg is a filename
1349 and the file should be deleted eventually.
1350 If DELETE_FAILURE is nonzero, the arg is a filename
1351 and the file should be deleted if this compilation fails. */
1353 static void
1354 store_arg (arg, delete_always, delete_failure)
1355 char *arg;
1356 int delete_always, delete_failure;
1358 if (argbuf_index + 1 == argbuf_length)
1359 argbuf
1360 = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1362 argbuf[argbuf_index++] = arg;
1363 argbuf[argbuf_index] = 0;
1365 if (delete_always || delete_failure)
1366 record_temp_file (arg, delete_always, delete_failure);
1369 /* Read compilation specs from a file named FILENAME,
1370 replacing the default ones.
1372 A suffix which starts with `*' is a definition for
1373 one of the machine-specific sub-specs. The "suffix" should be
1374 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1375 The corresponding spec is stored in asm_spec, etc.,
1376 rather than in the `compilers' vector.
1378 Anything invalid in the file is a fatal error. */
1380 static void
1381 read_specs (filename, main_p)
1382 char *filename;
1383 int main_p;
1385 int desc;
1386 int readlen;
1387 struct stat statbuf;
1388 char *buffer;
1389 register char *p;
1391 if (verbose_flag)
1392 fprintf (stderr, "Reading specs from %s\n", filename);
1394 /* Open and stat the file. */
1395 desc = open (filename, O_RDONLY, 0);
1396 if (desc < 0)
1397 pfatal_with_name (filename);
1398 if (stat (filename, &statbuf) < 0)
1399 pfatal_with_name (filename);
1401 /* Read contents of file into BUFFER. */
1402 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1403 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1404 if (readlen < 0)
1405 pfatal_with_name (filename);
1406 buffer[readlen] = 0;
1407 close (desc);
1409 /* Scan BUFFER for specs, putting them in the vector. */
1410 p = buffer;
1411 while (1)
1413 char *suffix;
1414 char *spec;
1415 char *in, *out, *p1, *p2, *p3;
1417 /* Advance P in BUFFER to the next nonblank nocomment line. */
1418 p = skip_whitespace (p);
1419 if (*p == 0)
1420 break;
1422 /* Is this a special command that starts with '%'? */
1423 /* Don't allow this for the main specs file, since it would
1424 encourage people to overwrite it. */
1425 if (*p == '%' && !main_p)
1427 p1 = p;
1428 while (*p && *p != '\n')
1429 p++;
1431 p++; /* Skip '\n' */
1433 if (!strncmp (p1, "%include", sizeof ("%include")-1)
1434 && (p1[sizeof "%include" - 1] == ' '
1435 || p1[sizeof "%include" - 1] == '\t'))
1437 char *new_filename;
1439 p1 += sizeof ("%include");
1440 while (*p1 == ' ' || *p1 == '\t')
1441 p1++;
1443 if (*p1++ != '<' || p[-2] != '>')
1444 fatal ("specs %%include syntax malformed after %d characters",
1445 p1 - buffer + 1);
1447 p[-2] = '\0';
1448 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1449 read_specs (new_filename ? new_filename : p1, FALSE);
1450 continue;
1452 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1453 && (p1[sizeof "%include_noerr" - 1] == ' '
1454 || p1[sizeof "%include_noerr" - 1] == '\t'))
1456 char *new_filename;
1458 p1 += sizeof "%include_noerr";
1459 while (*p1 == ' ' || *p1 == '\t') p1++;
1461 if (*p1++ != '<' || p[-2] != '>')
1462 fatal ("specs %%include syntax malformed after %d characters",
1463 p1 - buffer + 1);
1465 p[-2] = '\0';
1466 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1467 if (new_filename)
1468 read_specs (new_filename, FALSE);
1469 else if (verbose_flag)
1470 fprintf (stderr, "Could not find specs file %s\n", p1);
1471 continue;
1473 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1474 && (p1[sizeof "%rename" - 1] == ' '
1475 || p1[sizeof "%rename" - 1] == '\t'))
1477 int name_len;
1478 struct spec_list *sl;
1480 /* Get original name */
1481 p1 += sizeof "%rename";
1482 while (*p1 == ' ' || *p1 == '\t')
1483 p1++;
1485 if (! isalpha (*p1))
1486 fatal ("specs %%rename syntax malformed after %d characters",
1487 p1 - buffer);
1489 p2 = p1;
1490 while (*p2 && !isspace (*p2))
1491 p2++;
1493 if (*p2 != ' ' && *p2 != '\t')
1494 fatal ("specs %%rename syntax malformed after %d characters",
1495 p2 - buffer);
1497 name_len = p2 - p1;
1498 *p2++ = '\0';
1499 while (*p2 == ' ' || *p2 == '\t')
1500 p2++;
1502 if (! isalpha (*p2))
1503 fatal ("specs %%rename syntax malformed after %d characters",
1504 p2 - buffer);
1506 /* Get new spec name */
1507 p3 = p2;
1508 while (*p3 && !isspace (*p3))
1509 p3++;
1511 if (p3 != p-1)
1512 fatal ("specs %%rename syntax malformed after %d characters",
1513 p3 - buffer);
1514 *p3 = '\0';
1516 for (sl = specs; sl; sl = sl->next)
1517 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1518 break;
1520 if (!sl)
1521 fatal ("specs %s spec was not found to be renamed", p1);
1523 if (strcmp (p1, p2) == 0)
1524 continue;
1526 if (verbose_flag)
1528 fprintf (stderr, "rename spec %s to %s\n", p1, p2);
1529 #ifdef DEBUG_SPECS
1530 fprintf (stderr, "spec is '%s'\n\n", *(sl->ptr_spec));
1531 #endif
1534 set_spec (p2, *(sl->ptr_spec));
1535 if (sl->alloc_p)
1536 free (*(sl->ptr_spec));
1538 *(sl->ptr_spec) = "";
1539 sl->alloc_p = 0;
1540 continue;
1542 else
1543 fatal ("specs unknown %% command after %d characters",
1544 p1 - buffer);
1547 /* Find the colon that should end the suffix. */
1548 p1 = p;
1549 while (*p1 && *p1 != ':' && *p1 != '\n')
1550 p1++;
1552 /* The colon shouldn't be missing. */
1553 if (*p1 != ':')
1554 fatal ("specs file malformed after %d characters", p1 - buffer);
1556 /* Skip back over trailing whitespace. */
1557 p2 = p1;
1558 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1559 p2--;
1561 /* Copy the suffix to a string. */
1562 suffix = save_string (p, p2 - p);
1563 /* Find the next line. */
1564 p = skip_whitespace (p1 + 1);
1565 if (p[1] == 0)
1566 fatal ("specs file malformed after %d characters", p - buffer);
1568 p1 = p;
1569 /* Find next blank line. */
1570 while (*p1 && !(*p1 == '\n' && p1[1] == '\n'))
1571 p1++;
1573 /* Specs end at the blank line and do not include the newline. */
1574 spec = save_string (p, p1 - p);
1575 p = p1;
1577 /* Delete backslash-newline sequences from the spec. */
1578 in = spec;
1579 out = spec;
1580 while (*in != 0)
1582 if (in[0] == '\\' && in[1] == '\n')
1583 in += 2;
1584 else if (in[0] == '#')
1585 while (*in && *in != '\n')
1586 in++;
1588 else
1589 *out++ = *in++;
1591 *out = 0;
1593 if (suffix[0] == '*')
1595 if (! strcmp (suffix, "*link_command"))
1596 link_command_spec = spec;
1597 else
1598 set_spec (suffix + 1, spec);
1600 else
1602 /* Add this pair to the vector. */
1603 compilers
1604 = ((struct compiler *)
1605 xrealloc (compilers,
1606 (n_compilers + 2) * sizeof (struct compiler)));
1608 compilers[n_compilers].suffix = suffix;
1609 bzero ((char *) compilers[n_compilers].spec,
1610 sizeof compilers[n_compilers].spec);
1611 compilers[n_compilers].spec[0] = spec;
1612 n_compilers++;
1613 bzero ((char *) &compilers[n_compilers],
1614 sizeof compilers[n_compilers]);
1617 if (*suffix == 0)
1618 link_command_spec = spec;
1621 if (link_command_spec == 0)
1622 fatal ("spec file has no spec for linking");
1625 /* Record the names of temporary files we tell compilers to write,
1626 and delete them at the end of the run. */
1628 /* This is the common prefix we use to make temp file names.
1629 It is chosen once for each run of this program.
1630 It is substituted into a spec by %g.
1631 Thus, all temp file names contain this prefix.
1632 In practice, all temp file names start with this prefix.
1634 This prefix comes from the envvar TMPDIR if it is defined;
1635 otherwise, from the P_tmpdir macro if that is defined;
1636 otherwise, in /usr/tmp or /tmp;
1637 or finally the current directory if all else fails. */
1639 static char *temp_filename;
1641 /* Length of the prefix. */
1643 static int temp_filename_length;
1645 /* Define the list of temporary files to delete. */
1647 struct temp_file
1649 char *name;
1650 struct temp_file *next;
1653 /* Queue of files to delete on success or failure of compilation. */
1654 static struct temp_file *always_delete_queue;
1655 /* Queue of files to delete on failure of compilation. */
1656 static struct temp_file *failure_delete_queue;
1658 /* Record FILENAME as a file to be deleted automatically.
1659 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1660 otherwise delete it in any case.
1661 FAIL_DELETE nonzero means delete it if a compilation step fails;
1662 otherwise delete it in any case. */
1664 static void
1665 record_temp_file (filename, always_delete, fail_delete)
1666 char *filename;
1667 int always_delete;
1668 int fail_delete;
1670 register char *name;
1671 name = xmalloc (strlen (filename) + 1);
1672 strcpy (name, filename);
1674 if (always_delete)
1676 register struct temp_file *temp;
1677 for (temp = always_delete_queue; temp; temp = temp->next)
1678 if (! strcmp (name, temp->name))
1679 goto already1;
1681 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1682 temp->next = always_delete_queue;
1683 temp->name = name;
1684 always_delete_queue = temp;
1686 already1:;
1689 if (fail_delete)
1691 register struct temp_file *temp;
1692 for (temp = failure_delete_queue; temp; temp = temp->next)
1693 if (! strcmp (name, temp->name))
1694 goto already2;
1696 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1697 temp->next = failure_delete_queue;
1698 temp->name = name;
1699 failure_delete_queue = temp;
1701 already2:;
1705 /* Delete all the temporary files whose names we previously recorded. */
1707 static void
1708 delete_if_ordinary (name)
1709 char *name;
1711 struct stat st;
1712 #ifdef DEBUG
1713 int i, c;
1715 printf ("Delete %s? (y or n) ", name);
1716 fflush (stdout);
1717 i = getchar ();
1718 if (i != '\n')
1719 while ((c = getchar ()) != '\n' && c != EOF)
1722 if (i == 'y' || i == 'Y')
1723 #endif /* DEBUG */
1724 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1725 if (unlink (name) < 0)
1726 if (verbose_flag)
1727 perror_with_name (name);
1730 static void
1731 delete_temp_files ()
1733 register struct temp_file *temp;
1735 for (temp = always_delete_queue; temp; temp = temp->next)
1736 delete_if_ordinary (temp->name);
1737 always_delete_queue = 0;
1740 /* Delete all the files to be deleted on error. */
1742 static void
1743 delete_failure_queue ()
1745 register struct temp_file *temp;
1747 for (temp = failure_delete_queue; temp; temp = temp->next)
1748 delete_if_ordinary (temp->name);
1751 static void
1752 clear_failure_queue ()
1754 failure_delete_queue = 0;
1757 /* Routine to add variables to the environment. We do this to pass
1758 the pathname of the gcc driver, and the directories search to the
1759 collect2 program, which is being run as ld. This way, we can be
1760 sure of executing the right compiler when collect2 wants to build
1761 constructors and destructors. Since the environment variables we
1762 use come from an obstack, we don't have to worry about allocating
1763 space for them. */
1765 #ifndef HAVE_PUTENV
1767 void
1768 putenv (str)
1769 char *str;
1771 #ifndef VMS /* nor about VMS */
1773 extern char **environ;
1774 char **old_environ = environ;
1775 char **envp;
1776 int num_envs = 0;
1777 int name_len = 1;
1778 int str_len = strlen (str);
1779 char *p = str;
1780 int ch;
1782 while ((ch = *p++) != '\0' && ch != '=')
1783 name_len++;
1785 if (!ch)
1786 abort ();
1788 /* Search for replacing an existing environment variable, and
1789 count the number of total environment variables. */
1790 for (envp = old_environ; *envp; envp++)
1792 num_envs++;
1793 if (!strncmp (str, *envp, name_len))
1795 *envp = str;
1796 return;
1800 /* Add a new environment variable */
1801 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1802 *environ = str;
1803 bcopy ((char *) old_environ, (char *) (environ + 1),
1804 sizeof (char *) * (num_envs+1));
1806 #endif /* VMS */
1809 #endif /* HAVE_PUTENV */
1812 /* Build a list of search directories from PATHS.
1813 PREFIX is a string to prepend to the list.
1814 If CHECK_DIR_P is non-zero we ensure the directory exists.
1815 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
1816 It is also used by the --print-search-dirs flag. */
1818 static char *
1819 build_search_list (paths, prefix, check_dir_p)
1820 struct path_prefix *paths;
1821 char *prefix;
1822 int check_dir_p;
1824 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1825 int just_suffix_len
1826 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1827 int first_time = TRUE;
1828 struct prefix_list *pprefix;
1830 obstack_grow (&collect_obstack, prefix, strlen (prefix));
1832 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1834 int len = strlen (pprefix->prefix);
1836 if (machine_suffix
1837 && (! check_dir_p
1838 || is_directory (pprefix->prefix, machine_suffix, 0)))
1840 if (!first_time)
1841 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1843 first_time = FALSE;
1844 obstack_grow (&collect_obstack, pprefix->prefix, len);
1845 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1848 if (just_machine_suffix
1849 && pprefix->require_machine_suffix == 2
1850 && (! check_dir_p
1851 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
1853 if (! first_time)
1854 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1856 first_time = FALSE;
1857 obstack_grow (&collect_obstack, pprefix->prefix, len);
1858 obstack_grow (&collect_obstack, just_machine_suffix,
1859 just_suffix_len);
1862 if (! pprefix->require_machine_suffix)
1864 if (! first_time)
1865 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1867 first_time = FALSE;
1868 obstack_grow (&collect_obstack, pprefix->prefix, len);
1872 obstack_1grow (&collect_obstack, '\0');
1873 return obstack_finish (&collect_obstack);
1876 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
1877 for collect. */
1879 static void
1880 putenv_from_prefixes (paths, env_var)
1881 struct path_prefix *paths;
1882 char *env_var;
1884 putenv (build_search_list (paths, env_var, 1));
1887 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1888 access to check permissions.
1889 Return 0 if not found, otherwise return its name, allocated with malloc. */
1891 static char *
1892 find_a_file (pprefix, name, mode)
1893 struct path_prefix *pprefix;
1894 char *name;
1895 int mode;
1897 char *temp;
1898 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1899 struct prefix_list *pl;
1900 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1902 if (machine_suffix)
1903 len += strlen (machine_suffix);
1905 temp = xmalloc (len);
1907 /* Determine the filename to execute (special case for absolute paths). */
1909 if (*name == '/' || *name == DIR_SEPARATOR
1910 /* Check for disk name on MS-DOS-based systems. */
1911 || (DIR_SEPARATOR == '\\' && name[1] == ':'
1912 && (name[2] == DIR_SEPARATOR || name[2] == '/')))
1914 if (access (name, mode))
1916 strcpy (temp, name);
1917 return temp;
1920 else
1921 for (pl = pprefix->plist; pl; pl = pl->next)
1923 if (machine_suffix)
1925 /* Some systems have a suffix for executable files.
1926 So try appending that first. */
1927 if (file_suffix[0] != 0)
1929 strcpy (temp, pl->prefix);
1930 strcat (temp, machine_suffix);
1931 strcat (temp, name);
1932 strcat (temp, file_suffix);
1933 if (access (temp, mode) == 0)
1935 if (pl->used_flag_ptr != 0)
1936 *pl->used_flag_ptr = 1;
1937 return temp;
1941 /* Now try just the name. */
1942 strcpy (temp, pl->prefix);
1943 strcat (temp, machine_suffix);
1944 strcat (temp, name);
1945 if (access (temp, mode) == 0)
1947 if (pl->used_flag_ptr != 0)
1948 *pl->used_flag_ptr = 1;
1949 return temp;
1953 /* Certain prefixes are tried with just the machine type,
1954 not the version. This is used for finding as, ld, etc. */
1955 if (just_machine_suffix && pl->require_machine_suffix == 2)
1957 /* Some systems have a suffix for executable files.
1958 So try appending that first. */
1959 if (file_suffix[0] != 0)
1961 strcpy (temp, pl->prefix);
1962 strcat (temp, just_machine_suffix);
1963 strcat (temp, name);
1964 strcat (temp, file_suffix);
1965 if (access (temp, mode) == 0)
1967 if (pl->used_flag_ptr != 0)
1968 *pl->used_flag_ptr = 1;
1969 return temp;
1973 strcpy (temp, pl->prefix);
1974 strcat (temp, just_machine_suffix);
1975 strcat (temp, name);
1976 if (access (temp, mode) == 0)
1978 if (pl->used_flag_ptr != 0)
1979 *pl->used_flag_ptr = 1;
1980 return temp;
1984 /* Certain prefixes can't be used without the machine suffix
1985 when the machine or version is explicitly specified. */
1986 if (! pl->require_machine_suffix)
1988 /* Some systems have a suffix for executable files.
1989 So try appending that first. */
1990 if (file_suffix[0] != 0)
1992 strcpy (temp, pl->prefix);
1993 strcat (temp, name);
1994 strcat (temp, file_suffix);
1995 if (access (temp, mode) == 0)
1997 if (pl->used_flag_ptr != 0)
1998 *pl->used_flag_ptr = 1;
1999 return temp;
2003 strcpy (temp, pl->prefix);
2004 strcat (temp, name);
2005 if (access (temp, mode) == 0)
2007 if (pl->used_flag_ptr != 0)
2008 *pl->used_flag_ptr = 1;
2009 return temp;
2014 free (temp);
2015 return 0;
2018 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
2019 at the start of the list, otherwise it goes at the end.
2021 If WARN is nonzero, we will warn if no file is found
2022 through this prefix. WARN should point to an int
2023 which will be set to 1 if this entry is used.
2025 COMPONENT is the value to be passed to update_path.
2027 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
2028 the complete value of machine_suffix.
2029 2 means try both machine_suffix and just_machine_suffix. */
2031 static void
2032 add_prefix (pprefix, prefix, component, first, require_machine_suffix, warn)
2033 struct path_prefix *pprefix;
2034 char *prefix;
2035 char *component;
2036 int first;
2037 int require_machine_suffix;
2038 int *warn;
2040 struct prefix_list *pl, **prev;
2041 int len;
2043 if (! first && pprefix->plist)
2045 for (pl = pprefix->plist; pl->next; pl = pl->next)
2047 prev = &pl->next;
2049 else
2050 prev = &pprefix->plist;
2052 /* Keep track of the longest prefix */
2054 prefix = update_path (prefix, component);
2055 len = strlen (prefix);
2056 if (len > pprefix->max_len)
2057 pprefix->max_len = len;
2059 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2060 pl->prefix = save_string (prefix, len);
2061 pl->require_machine_suffix = require_machine_suffix;
2062 pl->used_flag_ptr = warn;
2063 if (warn)
2064 *warn = 0;
2066 if (*prev)
2067 pl->next = *prev;
2068 else
2069 pl->next = (struct prefix_list *) 0;
2070 *prev = pl;
2073 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
2075 static void
2076 unused_prefix_warnings (pprefix)
2077 struct path_prefix *pprefix;
2079 struct prefix_list *pl = pprefix->plist;
2081 while (pl)
2083 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
2085 if (pl->require_machine_suffix && machine_suffix)
2086 error ("file path prefix `%s%s' never used", pl->prefix,
2087 machine_suffix);
2088 else
2089 error ("file path prefix `%s' never used", pl->prefix);
2091 /* Prevent duplicate warnings. */
2092 *pl->used_flag_ptr = 1;
2095 pl = pl->next;
2099 /* Get rid of all prefixes built up so far in *PLISTP. */
2101 static void
2102 free_path_prefix (pprefix)
2103 struct path_prefix *pprefix;
2105 struct prefix_list *pl = pprefix->plist;
2106 struct prefix_list *temp;
2108 while (pl)
2110 temp = pl;
2111 pl = pl->next;
2112 free (temp->prefix);
2113 free ((char *) temp);
2116 pprefix->plist = (struct prefix_list *) 0;
2119 /* Execute the command specified by the arguments on the current line of spec.
2120 When using pipes, this includes several piped-together commands
2121 with `|' between them.
2123 Return 0 if successful, -1 if failed. */
2125 static int
2126 execute ()
2128 int i;
2129 int n_commands; /* # of command. */
2130 char *string;
2131 struct command
2133 char *prog; /* program name. */
2134 char **argv; /* vector of args. */
2135 int pid; /* pid of process for this command. */
2138 struct command *commands; /* each command buffer with above info. */
2140 /* Count # of piped commands. */
2141 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2142 if (strcmp (argbuf[i], "|") == 0)
2143 n_commands++;
2145 /* Get storage for each command. */
2146 commands
2147 = (struct command *) alloca (n_commands * sizeof (struct command));
2149 /* Split argbuf into its separate piped processes,
2150 and record info about each one.
2151 Also search for the programs that are to be run. */
2153 commands[0].prog = argbuf[0]; /* first command. */
2154 commands[0].argv = &argbuf[0];
2155 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2156 if (string)
2157 commands[0].argv[0] = string;
2159 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2160 if (strcmp (argbuf[i], "|") == 0)
2161 { /* each command. */
2162 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__)) || defined (OS2) || defined (VMS)
2163 fatal ("-pipe not supported");
2164 #endif
2165 argbuf[i] = 0; /* termination of command args. */
2166 commands[n_commands].prog = argbuf[i + 1];
2167 commands[n_commands].argv = &argbuf[i + 1];
2168 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2169 if (string)
2170 commands[n_commands].argv[0] = string;
2171 n_commands++;
2174 argbuf[argbuf_index] = 0;
2176 /* If -v, print what we are about to do, and maybe query. */
2178 if (verbose_flag)
2180 /* Print each piped command as a separate line. */
2181 for (i = 0; i < n_commands ; i++)
2183 char **j;
2185 for (j = commands[i].argv; *j; j++)
2186 fprintf (stderr, " %s", *j);
2188 /* Print a pipe symbol after all but the last command. */
2189 if (i + 1 != n_commands)
2190 fprintf (stderr, " |");
2191 fprintf (stderr, "\n");
2193 fflush (stderr);
2194 #ifdef DEBUG
2195 fprintf (stderr, "\nGo ahead? (y or n) ");
2196 fflush (stderr);
2197 i = getchar ();
2198 if (i != '\n')
2199 while (getchar () != '\n')
2202 if (i != 'y' && i != 'Y')
2203 return 0;
2204 #endif /* DEBUG */
2207 /* Run each piped subprocess. */
2209 for (i = 0; i < n_commands; i++)
2211 char *errmsg_fmt, *errmsg_arg;
2212 char *string = commands[i].argv[0];
2214 commands[i].pid = pexecute (string, commands[i].argv,
2215 programname, temp_filename,
2216 &errmsg_fmt, &errmsg_arg,
2217 ((i == 0 ? PEXECUTE_FIRST : 0)
2218 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2219 | (string == commands[i].prog
2220 ? PEXECUTE_SEARCH : 0)
2221 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2223 if (commands[i].pid == -1)
2224 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2226 if (string != commands[i].prog)
2227 free (string);
2230 execution_count++;
2232 /* Wait for all the subprocesses to finish.
2233 We don't care what order they finish in;
2234 we know that N_COMMANDS waits will get them all.
2235 Ignore subprocesses that we don't know about,
2236 since they can be spawned by the process that exec'ed us. */
2239 int ret_code = 0;
2241 for (i = 0; i < n_commands; )
2243 int j;
2244 int status;
2245 int pid;
2247 pid = pwait (commands[i].pid, &status, 0);
2248 if (pid < 0)
2249 abort ();
2251 for (j = 0; j < n_commands; j++)
2252 if (commands[j].pid == pid)
2254 i++;
2255 if (status != 0)
2257 if (WIFSIGNALED (status))
2259 fatal ("Internal compiler error: program %s got fatal signal %d",
2260 commands[j].prog, WTERMSIG (status));
2261 signal_count++;
2262 ret_code = -1;
2264 else if (WIFEXITED (status)
2265 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2266 ret_code = -1;
2268 break;
2271 return ret_code;
2275 /* Find all the switches given to us
2276 and make a vector describing them.
2277 The elements of the vector are strings, one per switch given.
2278 If a switch uses following arguments, then the `part1' field
2279 is the switch itself and the `args' field
2280 is a null-terminated vector containing the following arguments.
2281 The `live_cond' field is 1 if the switch is true in a conditional spec,
2282 -1 if false (overridden by a later switch), and is initialized to zero.
2283 The `valid' field is nonzero if any spec has looked at this switch;
2284 if it remains zero at the end of the run, it must be meaningless. */
2286 struct switchstr
2288 char *part1;
2289 char **args;
2290 int live_cond;
2291 int valid;
2294 static struct switchstr *switches;
2296 static int n_switches;
2298 struct infile
2300 char *name;
2301 char *language;
2304 /* Also a vector of input files specified. */
2306 static struct infile *infiles;
2308 static int n_infiles;
2310 /* And a vector of corresponding output files is made up later. */
2312 static char **outfiles;
2314 /* Used to track if none of the -B paths are used. */
2315 static int warn_B;
2317 /* Used to track if standard path isn't used and -b or -V is specified. */
2318 static int warn_std;
2320 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2321 static int *warn_std_ptr = 0;
2324 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2326 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2327 is true if we should look for an executable suffix as well. */
2329 static char *
2330 convert_filename (name, do_exe)
2331 char *name;
2332 int do_exe;
2334 int i;
2335 int len = strlen (name);
2337 #ifdef HAVE_OBJECT_SUFFIX
2338 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2339 if (len > 2
2340 && name[len - 2] == '.'
2341 && name[len - 1] == 'o')
2343 obstack_grow (&obstack, name, len - 2);
2344 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2345 name = obstack_finish (&obstack);
2347 #endif
2349 #ifdef HAVE_EXECUTABLE_SUFFIX
2350 /* If there is no filetype, make it the executable suffix (which includes
2351 the "."). But don't get confused if we have just "-o". */
2352 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2353 return name;
2355 for (i = len - 1; i >= 0; i--)
2356 if (name[i] == '/' || name[i] == DIR_SEPARATOR)
2357 break;
2359 for (i++; i < len; i++)
2360 if (name[i] == '.')
2361 return name;
2363 obstack_grow (&obstack, name, len);
2364 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2365 name = obstack_finish (&obstack);
2366 #endif
2368 return name;
2370 #endif
2372 /* Create the vector `switches' and its contents.
2373 Store its length in `n_switches'. */
2375 static void
2376 process_command (argc, argv)
2377 int argc;
2378 char **argv;
2380 register int i;
2381 char *temp;
2382 char *spec_lang = 0;
2383 int last_language_n_infiles;
2384 int have_c = 0;
2385 int have_o = 0;
2386 int lang_n_infiles = 0;
2388 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
2390 n_switches = 0;
2391 n_infiles = 0;
2393 /* Figure compiler version from version string. */
2395 compiler_version = save_string (version_string, strlen (version_string));
2396 for (temp = compiler_version; *temp; ++temp)
2398 if (*temp == ' ')
2400 *temp = '\0';
2401 break;
2405 /* Set up the default search paths. */
2407 if (gcc_exec_prefix)
2409 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2410 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2413 /* COMPILER_PATH and LIBRARY_PATH have values
2414 that are lists of directory names with colons. */
2416 temp = getenv ("COMPILER_PATH");
2417 if (temp)
2419 char *startp, *endp;
2420 char *nstore = (char *) alloca (strlen (temp) + 3);
2422 startp = endp = temp;
2423 while (1)
2425 if (*endp == PATH_SEPARATOR || *endp == 0)
2427 strncpy (nstore, startp, endp-startp);
2428 if (endp == startp)
2429 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2430 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2432 nstore[endp-startp] = DIR_SEPARATOR;
2433 nstore[endp-startp+1] = 0;
2435 else
2436 nstore[endp-startp] = 0;
2437 add_prefix (&exec_prefixes, nstore, 0, 0, 0, NULL_PTR);
2438 if (*endp == 0)
2439 break;
2440 endp = startp = endp + 1;
2442 else
2443 endp++;
2447 temp = getenv ("LIBRARY_PATH");
2448 if (temp && *cross_compile == '0')
2450 char *startp, *endp;
2451 char *nstore = (char *) alloca (strlen (temp) + 3);
2453 startp = endp = temp;
2454 while (1)
2456 if (*endp == PATH_SEPARATOR || *endp == 0)
2458 strncpy (nstore, startp, endp-startp);
2459 if (endp == startp)
2460 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2461 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2463 nstore[endp-startp] = DIR_SEPARATOR;
2464 nstore[endp-startp+1] = 0;
2466 else
2467 nstore[endp-startp] = 0;
2468 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2469 0, 0, NULL_PTR);
2470 if (*endp == 0)
2471 break;
2472 endp = startp = endp + 1;
2474 else
2475 endp++;
2479 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2480 temp = getenv ("LPATH");
2481 if (temp && *cross_compile == '0')
2483 char *startp, *endp;
2484 char *nstore = (char *) alloca (strlen (temp) + 3);
2486 startp = endp = temp;
2487 while (1)
2489 if (*endp == PATH_SEPARATOR || *endp == 0)
2491 strncpy (nstore, startp, endp-startp);
2492 if (endp == startp)
2493 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2494 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2496 nstore[endp-startp] = DIR_SEPARATOR;
2497 nstore[endp-startp+1] = 0;
2499 else
2500 nstore[endp-startp] = 0;
2501 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2502 0, 0, NULL_PTR);
2503 if (*endp == 0)
2504 break;
2505 endp = startp = endp + 1;
2507 else
2508 endp++;
2512 /* Convert new-style -- options to old-style. */
2513 translate_options (&argc, &argv);
2515 #ifdef LANG_SPECIFIC_DRIVER
2516 /* Do language-specific adjustment/addition of flags. */
2517 lang_specific_driver (fatal, &argc, &argv);
2518 #endif
2520 /* Scan argv twice. Here, the first time, just count how many switches
2521 there will be in their vector, and how many input files in theirs.
2522 Here we also parse the switches that cc itself uses (e.g. -v). */
2524 for (i = 1; i < argc; i++)
2526 if (! strcmp (argv[i], "-dumpspecs"))
2528 struct spec_list *sl;
2529 init_spec ();
2530 for (sl = specs; sl; sl = sl->next)
2531 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
2532 exit (0);
2534 else if (! strcmp (argv[i], "-dumpversion"))
2536 printf ("%s\n", spec_version);
2537 exit (0);
2539 else if (! strcmp (argv[i], "-dumpmachine"))
2541 printf ("%s\n", spec_machine);
2542 exit (0);
2544 else if (! strcmp (argv[i], "-print-search-dirs"))
2545 print_search_dirs = 1;
2546 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2547 print_file_name = "libgcc.a";
2548 else if (! strncmp (argv[i], "-print-file-name=", 17))
2549 print_file_name = argv[i] + 17;
2550 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2551 print_prog_name = argv[i] + 17;
2552 else if (! strcmp (argv[i], "-print-multi-lib"))
2553 print_multi_lib = 1;
2554 else if (! strcmp (argv[i], "-print-multi-directory"))
2555 print_multi_directory = 1;
2556 else if (! strncmp (argv[i], "-Wa,", 4))
2558 int prev, j;
2559 /* Pass the rest of this option to the assembler. */
2561 n_assembler_options++;
2562 if (!assembler_options)
2563 assembler_options
2564 = (char **) xmalloc (n_assembler_options * sizeof (char **));
2565 else
2566 assembler_options
2567 = (char **) xrealloc (assembler_options,
2568 n_assembler_options * sizeof (char **));
2570 /* Split the argument at commas. */
2571 prev = 4;
2572 for (j = 4; argv[i][j]; j++)
2573 if (argv[i][j] == ',')
2575 assembler_options[n_assembler_options - 1]
2576 = save_string (argv[i] + prev, j - prev);
2577 n_assembler_options++;
2578 assembler_options
2579 = (char **) xrealloc (assembler_options,
2580 n_assembler_options * sizeof (char **));
2581 prev = j + 1;
2583 /* Record the part after the last comma. */
2584 assembler_options[n_assembler_options - 1] = argv[i] + prev;
2586 else if (! strncmp (argv[i], "-Wp,", 4))
2588 int prev, j;
2589 /* Pass the rest of this option to the preprocessor. */
2591 n_preprocessor_options++;
2592 if (!preprocessor_options)
2593 preprocessor_options
2594 = (char **) xmalloc (n_preprocessor_options * sizeof (char **));
2595 else
2596 preprocessor_options
2597 = (char **) xrealloc (preprocessor_options,
2598 n_preprocessor_options * sizeof (char **));
2600 /* Split the argument at commas. */
2601 prev = 4;
2602 for (j = 4; argv[i][j]; j++)
2603 if (argv[i][j] == ',')
2605 preprocessor_options[n_preprocessor_options - 1]
2606 = save_string (argv[i] + prev, j - prev);
2607 n_preprocessor_options++;
2608 preprocessor_options
2609 = (char **) xrealloc (preprocessor_options,
2610 n_preprocessor_options * sizeof (char **));
2611 prev = j + 1;
2613 /* Record the part after the last comma. */
2614 preprocessor_options[n_preprocessor_options - 1] = argv[i] + prev;
2616 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2617 /* The +e options to the C++ front-end. */
2618 n_switches++;
2619 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2621 int j;
2622 /* Split the argument at commas. */
2623 for (j = 3; argv[i][j]; j++)
2624 n_infiles += (argv[i][j] == ',');
2626 else if (strcmp (argv[i], "-Xlinker") == 0)
2628 if (i + 1 == argc)
2629 fatal ("argument to `-Xlinker' is missing");
2631 n_infiles++;
2632 i++;
2634 else if (strncmp (argv[i], "-l", 2) == 0)
2635 n_infiles++;
2636 else if (strcmp (argv[i], "-save-temps") == 0)
2638 save_temps_flag = 1;
2639 n_switches++;
2641 else if (strcmp (argv[i], "-specs") == 0)
2643 struct user_specs *user = (struct user_specs *)
2644 xmalloc (sizeof (struct user_specs));
2645 if (++i >= argc)
2646 fatal ("argument to `-specs' is missing");
2648 user->next = (struct user_specs *)0;
2649 user->filename = argv[i];
2650 if (user_specs_tail)
2651 user_specs_tail->next = user;
2652 else
2653 user_specs_head = user;
2654 user_specs_tail = user;
2656 else if (strncmp (argv[i], "-specs=", 7) == 0)
2658 struct user_specs *user = (struct user_specs *)
2659 xmalloc (sizeof (struct user_specs));
2660 if (strlen (argv[i]) == 7)
2661 fatal ("argument to `-specs=' is missing");
2663 user->next = (struct user_specs *)0;
2664 user->filename = argv[i]+7;
2665 if (user_specs_tail)
2666 user_specs_tail->next = user;
2667 else
2668 user_specs_head = user;
2669 user_specs_tail = user;
2671 else if (argv[i][0] == '-' && argv[i][1] != 0)
2673 register char *p = &argv[i][1];
2674 register int c = *p;
2676 switch (c)
2678 case 'b':
2679 if (p[1] == 0 && i + 1 == argc)
2680 fatal ("argument to `-b' is missing");
2681 if (p[1] == 0)
2682 spec_machine = argv[++i];
2683 else
2684 spec_machine = p + 1;
2686 warn_std_ptr = &warn_std;
2687 break;
2689 case 'B':
2691 int *temp = (int *) xmalloc (sizeof (int));
2692 char *value;
2693 if (p[1] == 0 && i + 1 == argc)
2694 fatal ("argument to `-B' is missing");
2695 if (p[1] == 0)
2696 value = argv[++i];
2697 else
2698 value = p + 1;
2699 add_prefix (&exec_prefixes, value, NULL_PTR, 1, 0, &warn_B);
2700 add_prefix (&startfile_prefixes, value, NULL_PTR,
2701 1, 0, &warn_B);
2702 add_prefix (&include_prefixes, concat (value, "include",
2703 NULL_PTR),
2704 NULL_PTR, 1, 0, NULL_PTR);
2706 /* As a kludge, if the arg is "[foo/]stageN/", just add
2707 "[foo/]include" to the include prefix. */
2709 int len = strlen (value);
2710 if ((len == 7
2711 || (len > 7
2712 && (value[len - 8] == '/'
2713 || value[len - 8] == DIR_SEPARATOR)))
2714 && strncmp (value + len - 7, "stage", 5) == 0
2715 && isdigit (value[len - 2])
2716 && (value[len - 1] == '/'
2717 || value[len - 1] == DIR_SEPARATOR))
2719 if (len == 7)
2720 add_prefix (&include_prefixes, "include", NULL_PTR,
2721 1, 0, NULL_PTR);
2722 else
2724 char *string = xmalloc (len + 1);
2725 strncpy (string, value, len-7);
2726 strcpy (string+len-7, "include");
2727 add_prefix (&include_prefixes, string, NULL_PTR,
2728 1, 0, NULL_PTR);
2733 break;
2735 case 'v': /* Print our subcommands and print versions. */
2736 n_switches++;
2737 /* If they do anything other than exactly `-v', don't set
2738 verbose_flag; rather, continue on to give the error. */
2739 if (p[1] != 0)
2740 break;
2741 verbose_flag++;
2742 break;
2744 case 'V':
2745 if (p[1] == 0 && i + 1 == argc)
2746 fatal ("argument to `-V' is missing");
2747 if (p[1] == 0)
2748 spec_version = argv[++i];
2749 else
2750 spec_version = p + 1;
2751 compiler_version = spec_version;
2752 warn_std_ptr = &warn_std;
2754 /* Validate the version number. Use the same checks
2755 done when inserting it into a spec.
2757 The format of the version string is
2758 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
2760 char *v = compiler_version;
2762 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
2763 while (! isdigit (*v))
2764 v++;
2766 if (v > compiler_version && v[-1] != '-')
2767 fatal ("invalid version number format");
2769 /* Set V after the first period. */
2770 while (isdigit (*v))
2771 v++;
2773 if (*v != '.')
2774 fatal ("invalid version number format");
2776 v++;
2777 while (isdigit (*v))
2778 v++;
2780 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
2781 fatal ("invalid version number format");
2783 break;
2785 case 'c':
2786 if (p[1] == 0)
2788 have_c = 1;
2789 n_switches++;
2790 break;
2792 goto normal_switch;
2794 case 'o':
2795 have_o = 1;
2796 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
2797 argv[i] = convert_filename (argv[i], 1);
2798 if (p[1] == 0)
2799 argv[i+1] = convert_filename (argv[i+1], 1);
2800 #endif
2801 goto normal_switch;
2803 default:
2804 normal_switch:
2805 n_switches++;
2807 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
2808 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
2809 else if (WORD_SWITCH_TAKES_ARG (p))
2810 i += WORD_SWITCH_TAKES_ARG (p);
2813 else
2815 n_infiles++;
2816 lang_n_infiles++;
2820 if (have_c && have_o && lang_n_infiles > 1)
2821 fatal ("cannot specify -o with -c and multiple compilations");
2823 /* Set up the search paths before we go looking for config files. */
2825 /* These come before the md prefixes so that we will find gcc's subcommands
2826 (such as cpp) rather than those of the host system. */
2827 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2828 as well as trying the machine and the version. */
2829 #ifndef OS2
2830 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
2831 0, 2, warn_std_ptr);
2832 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
2833 0, 2, warn_std_ptr);
2834 #endif
2836 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
2837 0, 1, warn_std_ptr);
2838 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
2839 0, 1, warn_std_ptr);
2841 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
2842 dir_separator_str, NULL_PTR);
2844 /* If tooldir is relative, base it on exec_prefixes. A relative
2845 tooldir lets us move the installed tree as a unit.
2847 If GCC_EXEC_PREFIX is defined, then we want to add two relative
2848 directories, so that we can search both the user specified directory
2849 and the standard place. */
2851 if (*tooldir_prefix != '/' && *tooldir_prefix != DIR_SEPARATOR)
2853 if (gcc_exec_prefix)
2855 char *gcc_exec_tooldir_prefix
2856 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
2857 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
2859 add_prefix (&exec_prefixes,
2860 concat (gcc_exec_tooldir_prefix, "bin",
2861 dir_separator_str, NULL_PTR),
2862 NULL_PTR, 0, 0, NULL_PTR);
2863 add_prefix (&startfile_prefixes,
2864 concat (gcc_exec_tooldir_prefix, "lib",
2865 dir_separator_str, NULL_PTR),
2866 NULL_PTR, 0, 0, NULL_PTR);
2869 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
2870 dir_separator_str, spec_version,
2871 dir_separator_str, tooldir_prefix, NULL_PTR);
2874 add_prefix (&exec_prefixes,
2875 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
2876 "BINUTILS", 0, 0, NULL_PTR);
2877 add_prefix (&startfile_prefixes,
2878 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
2879 "BINUTILS", 0, 0, NULL_PTR);
2881 /* More prefixes are enabled in main, after we read the specs file
2882 and determine whether this is cross-compilation or not. */
2885 /* Then create the space for the vectors and scan again. */
2887 switches = ((struct switchstr *)
2888 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2889 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2890 n_switches = 0;
2891 n_infiles = 0;
2892 last_language_n_infiles = -1;
2894 /* This, time, copy the text of each switch and store a pointer
2895 to the copy in the vector of switches.
2896 Store all the infiles in their vector. */
2898 for (i = 1; i < argc; i++)
2900 /* Just skip the switches that were handled by the preceding loop. */
2901 if (! strncmp (argv[i], "-Wa,", 4))
2903 else if (! strncmp (argv[i], "-Wp,", 4))
2905 else if (! strcmp (argv[i], "-print-search-dirs"))
2907 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2909 else if (! strncmp (argv[i], "-print-file-name=", 17))
2911 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2913 else if (! strcmp (argv[i], "-print-multi-lib"))
2915 else if (! strcmp (argv[i], "-print-multi-directory"))
2917 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2919 /* Compensate for the +e options to the C++ front-end;
2920 they're there simply for cfront call-compatibility. We do
2921 some magic in default_compilers to pass them down properly.
2922 Note we deliberately start at the `+' here, to avoid passing
2923 -e0 or -e1 down into the linker. */
2924 switches[n_switches].part1 = &argv[i][0];
2925 switches[n_switches].args = 0;
2926 switches[n_switches].live_cond = 0;
2927 switches[n_switches].valid = 0;
2928 n_switches++;
2930 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2932 int prev, j;
2933 /* Split the argument at commas. */
2934 prev = 4;
2935 for (j = 4; argv[i][j]; j++)
2936 if (argv[i][j] == ',')
2938 infiles[n_infiles].language = "*";
2939 infiles[n_infiles++].name
2940 = save_string (argv[i] + prev, j - prev);
2941 prev = j + 1;
2943 /* Record the part after the last comma. */
2944 infiles[n_infiles].language = "*";
2945 infiles[n_infiles++].name = argv[i] + prev;
2947 else if (strcmp (argv[i], "-Xlinker") == 0)
2949 infiles[n_infiles].language = "*";
2950 infiles[n_infiles++].name = argv[++i];
2952 else if (strncmp (argv[i], "-l", 2) == 0)
2954 infiles[n_infiles].language = "*";
2955 infiles[n_infiles++].name = argv[i];
2957 else if (strcmp (argv[i], "-specs") == 0)
2958 i++;
2959 else if (strncmp (argv[i], "-specs=", 7) == 0)
2961 /* -save-temps overrides -pipe, so that temp files are produced */
2962 else if (save_temps_flag && strcmp (argv[i], "-pipe") == 0)
2963 error ("Warning: -pipe ignored since -save-temps specified");
2964 else if (argv[i][0] == '-' && argv[i][1] != 0)
2966 register char *p = &argv[i][1];
2967 register int c = *p;
2969 if (c == 'B' || c == 'b' || c == 'V')
2971 /* Skip a separate arg, if any. */
2972 if (p[1] == 0)
2973 i++;
2974 continue;
2976 if (c == 'x')
2978 if (p[1] == 0 && i + 1 == argc)
2979 fatal ("argument to `-x' is missing");
2980 if (p[1] == 0)
2981 spec_lang = argv[++i];
2982 else
2983 spec_lang = p + 1;
2984 if (! strcmp (spec_lang, "none"))
2985 /* Suppress the warning if -xnone comes after the last input
2986 file, because alternate command interfaces like g++ might
2987 find it useful to place -xnone after each input file. */
2988 spec_lang = 0;
2989 else
2990 last_language_n_infiles = n_infiles;
2991 continue;
2993 switches[n_switches].part1 = p;
2994 /* Deal with option arguments in separate argv elements. */
2995 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2996 || WORD_SWITCH_TAKES_ARG (p))
2998 int j = 0;
2999 int n_args = WORD_SWITCH_TAKES_ARG (p);
3001 if (n_args == 0)
3003 /* Count only the option arguments in separate argv elements. */
3004 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
3006 if (i + n_args >= argc)
3007 fatal ("argument to `-%s' is missing", p);
3008 switches[n_switches].args
3009 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
3010 while (j < n_args)
3011 switches[n_switches].args[j++] = argv[++i];
3012 /* Null-terminate the vector. */
3013 switches[n_switches].args[j] = 0;
3015 else if (index (switches_need_spaces, c))
3017 /* On some systems, ld cannot handle some options without
3018 a space. So split the option from its argument. */
3019 char *part1 = (char *) xmalloc (2);
3020 part1[0] = c;
3021 part1[1] = '\0';
3023 switches[n_switches].part1 = part1;
3024 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
3025 switches[n_switches].args[0] = xmalloc (strlen (p));
3026 strcpy (switches[n_switches].args[0], &p[1]);
3027 switches[n_switches].args[1] = 0;
3029 else
3030 switches[n_switches].args = 0;
3032 switches[n_switches].live_cond = 0;
3033 switches[n_switches].valid = 0;
3034 /* This is always valid, since gcc.c itself understands it. */
3035 if (!strcmp (p, "save-temps"))
3036 switches[n_switches].valid = 1;
3037 n_switches++;
3039 else
3041 #ifdef HAVE_OBJECT_SUFFIX
3042 argv[i] = convert_filename (argv[i], 0);
3043 #endif
3045 if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
3047 perror_with_name (argv[i]);
3048 error_count++;
3050 else
3052 infiles[n_infiles].language = spec_lang;
3053 infiles[n_infiles++].name = argv[i];
3058 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3059 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3061 switches[n_switches].part1 = 0;
3062 infiles[n_infiles].name = 0;
3065 /* Process a spec string, accumulating and running commands. */
3067 /* These variables describe the input file name.
3068 input_file_number is the index on outfiles of this file,
3069 so that the output file name can be stored for later use by %o.
3070 input_basename is the start of the part of the input file
3071 sans all directory names, and basename_length is the number
3072 of characters starting there excluding the suffix .c or whatever. */
3074 static char *input_filename;
3075 static int input_file_number;
3076 static int input_filename_length;
3077 static int basename_length;
3078 static char *input_basename;
3079 static char *input_suffix;
3081 /* These are variables used within do_spec and do_spec_1. */
3083 /* Nonzero if an arg has been started and not yet terminated
3084 (with space, tab or newline). */
3085 static int arg_going;
3087 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3088 is a temporary file name. */
3089 static int delete_this_arg;
3091 /* Nonzero means %w has been seen; the next arg to be terminated
3092 is the output file name of this compilation. */
3093 static int this_is_output_file;
3095 /* Nonzero means %s has been seen; the next arg to be terminated
3096 is the name of a library file and we should try the standard
3097 search dirs for it. */
3098 static int this_is_library_file;
3100 /* Nonzero means that the input of this command is coming from a pipe. */
3101 static int input_from_pipe;
3103 /* Process the spec SPEC and run the commands specified therein.
3104 Returns 0 if the spec is successfully processed; -1 if failed. */
3106 static int
3107 do_spec (spec)
3108 char *spec;
3110 int value;
3112 clear_args ();
3113 arg_going = 0;
3114 delete_this_arg = 0;
3115 this_is_output_file = 0;
3116 this_is_library_file = 0;
3117 input_from_pipe = 0;
3119 value = do_spec_1 (spec, 0, NULL_PTR);
3121 /* Force out any unfinished command.
3122 If -pipe, this forces out the last command if it ended in `|'. */
3123 if (value == 0)
3125 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3126 argbuf_index--;
3128 if (argbuf_index > 0)
3129 value = execute ();
3132 return value;
3135 /* Process the sub-spec SPEC as a portion of a larger spec.
3136 This is like processing a whole spec except that we do
3137 not initialize at the beginning and we do not supply a
3138 newline by default at the end.
3139 INSWITCH nonzero means don't process %-sequences in SPEC;
3140 in this case, % is treated as an ordinary character.
3141 This is used while substituting switches.
3142 INSWITCH nonzero also causes SPC not to terminate an argument.
3144 Value is zero unless a line was finished
3145 and the command on that line reported an error. */
3147 static int
3148 do_spec_1 (spec, inswitch, soft_matched_part)
3149 char *spec;
3150 int inswitch;
3151 char *soft_matched_part;
3153 register char *p = spec;
3154 register int c;
3155 int i;
3156 char *string;
3157 int value;
3159 while (c = *p++)
3160 /* If substituting a switch, treat all chars like letters.
3161 Otherwise, NL, SPC, TAB and % are special. */
3162 switch (inswitch ? 'a' : c)
3164 case '\n':
3165 /* End of line: finish any pending argument,
3166 then run the pending command if one has been started. */
3167 if (arg_going)
3169 obstack_1grow (&obstack, 0);
3170 string = obstack_finish (&obstack);
3171 if (this_is_library_file)
3172 string = find_file (string);
3173 store_arg (string, delete_this_arg, this_is_output_file);
3174 if (this_is_output_file)
3175 outfiles[input_file_number] = string;
3177 arg_going = 0;
3179 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3181 for (i = 0; i < n_switches; i++)
3182 if (!strcmp (switches[i].part1, "pipe"))
3183 break;
3185 /* A `|' before the newline means use a pipe here,
3186 but only if -pipe was specified.
3187 Otherwise, execute now and don't pass the `|' as an arg. */
3188 if (i < n_switches)
3190 input_from_pipe = 1;
3191 switches[i].valid = 1;
3192 break;
3194 else
3195 argbuf_index--;
3198 if (argbuf_index > 0)
3200 value = execute ();
3201 if (value)
3202 return value;
3204 /* Reinitialize for a new command, and for a new argument. */
3205 clear_args ();
3206 arg_going = 0;
3207 delete_this_arg = 0;
3208 this_is_output_file = 0;
3209 this_is_library_file = 0;
3210 input_from_pipe = 0;
3211 break;
3213 case '|':
3214 /* End any pending argument. */
3215 if (arg_going)
3217 obstack_1grow (&obstack, 0);
3218 string = obstack_finish (&obstack);
3219 if (this_is_library_file)
3220 string = find_file (string);
3221 store_arg (string, delete_this_arg, this_is_output_file);
3222 if (this_is_output_file)
3223 outfiles[input_file_number] = string;
3226 /* Use pipe */
3227 obstack_1grow (&obstack, c);
3228 arg_going = 1;
3229 break;
3231 case '\t':
3232 case ' ':
3233 /* Space or tab ends an argument if one is pending. */
3234 if (arg_going)
3236 obstack_1grow (&obstack, 0);
3237 string = obstack_finish (&obstack);
3238 if (this_is_library_file)
3239 string = find_file (string);
3240 store_arg (string, delete_this_arg, this_is_output_file);
3241 if (this_is_output_file)
3242 outfiles[input_file_number] = string;
3244 /* Reinitialize for a new argument. */
3245 arg_going = 0;
3246 delete_this_arg = 0;
3247 this_is_output_file = 0;
3248 this_is_library_file = 0;
3249 break;
3251 case '%':
3252 switch (c = *p++)
3254 case 0:
3255 fatal ("Invalid specification! Bug in cc.");
3257 case 'b':
3258 obstack_grow (&obstack, input_basename, basename_length);
3259 arg_going = 1;
3260 break;
3262 case 'd':
3263 delete_this_arg = 2;
3264 break;
3266 /* Dump out the directories specified with LIBRARY_PATH,
3267 followed by the absolute directories
3268 that we search for startfiles. */
3269 case 'D':
3271 struct prefix_list *pl = startfile_prefixes.plist;
3272 int bufsize = 100;
3273 char *buffer = (char *) xmalloc (bufsize);
3274 int idx;
3276 for (; pl; pl = pl->next)
3278 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
3279 /* Used on systems which record the specified -L dirs
3280 and use them to search for dynamic linking. */
3281 /* Relative directories always come from -B,
3282 and it is better not to use them for searching
3283 at run time. In particular, stage1 loses */
3284 if (pl->prefix[0] != '/' && pl->prefix[0] != DIR_SEPARATOR)
3285 continue;
3286 #endif
3287 /* Try subdirectory if there is one. */
3288 if (multilib_dir != NULL)
3290 if (machine_suffix)
3292 if (strlen (pl->prefix) + strlen (machine_suffix)
3293 >= bufsize)
3294 bufsize = (strlen (pl->prefix)
3295 + strlen (machine_suffix)) * 2 + 1;
3296 buffer = (char *) xrealloc (buffer, bufsize);
3297 strcpy (buffer, pl->prefix);
3298 strcat (buffer, machine_suffix);
3299 if (is_directory (buffer, multilib_dir, 1))
3301 do_spec_1 ("-L", 0, NULL_PTR);
3302 #ifdef SPACE_AFTER_L_OPTION
3303 do_spec_1 (" ", 0, NULL_PTR);
3304 #endif
3305 do_spec_1 (buffer, 1, NULL_PTR);
3306 do_spec_1 (multilib_dir, 1, NULL_PTR);
3307 /* Make this a separate argument. */
3308 do_spec_1 (" ", 0, NULL_PTR);
3311 if (!pl->require_machine_suffix)
3313 if (is_directory (pl->prefix, multilib_dir, 1))
3315 do_spec_1 ("-L", 0, NULL_PTR);
3316 #ifdef SPACE_AFTER_L_OPTION
3317 do_spec_1 (" ", 0, NULL_PTR);
3318 #endif
3319 do_spec_1 (pl->prefix, 1, NULL_PTR);
3320 do_spec_1 (multilib_dir, 1, NULL_PTR);
3321 /* Make this a separate argument. */
3322 do_spec_1 (" ", 0, NULL_PTR);
3326 if (machine_suffix)
3328 if (is_directory (pl->prefix, machine_suffix, 1))
3330 do_spec_1 ("-L", 0, NULL_PTR);
3331 #ifdef SPACE_AFTER_L_OPTION
3332 do_spec_1 (" ", 0, NULL_PTR);
3333 #endif
3334 do_spec_1 (pl->prefix, 1, NULL_PTR);
3335 /* Remove slash from machine_suffix. */
3336 if (strlen (machine_suffix) >= bufsize)
3337 bufsize = strlen (machine_suffix) * 2 + 1;
3338 buffer = (char *) xrealloc (buffer, bufsize);
3339 strcpy (buffer, machine_suffix);
3340 idx = strlen (buffer);
3341 if (buffer[idx - 1] == '/'
3342 || buffer[idx - 1] == DIR_SEPARATOR)
3343 buffer[idx - 1] = 0;
3344 do_spec_1 (buffer, 1, NULL_PTR);
3345 /* Make this a separate argument. */
3346 do_spec_1 (" ", 0, NULL_PTR);
3349 if (!pl->require_machine_suffix)
3351 if (is_directory (pl->prefix, "", 1))
3353 do_spec_1 ("-L", 0, NULL_PTR);
3354 #ifdef SPACE_AFTER_L_OPTION
3355 do_spec_1 (" ", 0, NULL_PTR);
3356 #endif
3357 /* Remove slash from pl->prefix. */
3358 if (strlen (pl->prefix) >= bufsize)
3359 bufsize = strlen (pl->prefix) * 2 + 1;
3360 buffer = (char *) xrealloc (buffer, bufsize);
3361 strcpy (buffer, pl->prefix);
3362 idx = strlen (buffer);
3363 if (buffer[idx - 1] == '/'
3364 || buffer[idx - 1] == DIR_SEPARATOR)
3365 buffer[idx - 1] = 0;
3366 do_spec_1 (buffer, 1, NULL_PTR);
3367 /* Make this a separate argument. */
3368 do_spec_1 (" ", 0, NULL_PTR);
3372 free (buffer);
3374 break;
3376 case 'e':
3377 /* {...:%efoo} means report an error with `foo' as error message
3378 and don't execute any more commands for this file. */
3380 char *q = p;
3381 char *buf;
3382 while (*p != 0 && *p != '\n') p++;
3383 buf = (char *) alloca (p - q + 1);
3384 strncpy (buf, q, p - q);
3385 buf[p - q] = 0;
3386 error ("%s", buf);
3387 return -1;
3389 break;
3391 case 'g':
3392 case 'u':
3393 case 'U':
3394 if (save_temps_flag)
3396 obstack_grow (&obstack, input_basename, basename_length);
3397 delete_this_arg = 0;
3399 else
3401 #ifdef MKTEMP_EACH_FILE
3402 /* ??? This has a problem: the total number of
3403 values mktemp can return is limited.
3404 That matters for the names of object files.
3405 In 2.4, do something about that. */
3406 struct temp_name *t;
3407 char *suffix = p;
3408 while (*p == '.' || isalpha (*p)
3409 || (p[0] == '%' && p[1] == 'O'))
3410 p++;
3412 /* See if we already have an association of %g/%u/%U and
3413 suffix. */
3414 for (t = temp_names; t; t = t->next)
3415 if (t->length == p - suffix
3416 && strncmp (t->suffix, suffix, p - suffix) == 0
3417 && t->unique == (c != 'g'))
3418 break;
3420 /* Make a new association if needed. %u requires one. */
3421 if (t == 0 || c == 'u')
3423 if (t == 0)
3425 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
3426 t->next = temp_names;
3427 temp_names = t;
3429 t->length = p - suffix;
3430 t->suffix = save_string (suffix, p - suffix);
3431 t->unique = (c != 'g');
3432 temp_filename = choose_temp_base ();
3433 temp_filename_length = strlen (temp_filename);
3434 t->filename = temp_filename;
3435 t->filename_length = temp_filename_length;
3438 obstack_grow (&obstack, t->filename, t->filename_length);
3439 delete_this_arg = 1;
3440 #else
3441 obstack_grow (&obstack, temp_filename, temp_filename_length);
3442 if (c == 'u' || c == 'U')
3444 static int unique;
3445 char buff[9];
3446 if (c == 'u')
3447 unique++;
3448 sprintf (buff, "%d", unique);
3449 obstack_grow (&obstack, buff, strlen (buff));
3451 #endif
3452 delete_this_arg = 1;
3454 arg_going = 1;
3455 break;
3457 case 'i':
3458 obstack_grow (&obstack, input_filename, input_filename_length);
3459 arg_going = 1;
3460 break;
3462 case 'I':
3464 struct prefix_list *pl = include_prefixes.plist;
3466 if (gcc_exec_prefix)
3468 do_spec_1 ("-iprefix", 1, NULL_PTR);
3469 /* Make this a separate argument. */
3470 do_spec_1 (" ", 0, NULL_PTR);
3471 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
3472 do_spec_1 (" ", 0, NULL_PTR);
3475 for (; pl; pl = pl->next)
3477 do_spec_1 ("-isystem", 1, NULL_PTR);
3478 /* Make this a separate argument. */
3479 do_spec_1 (" ", 0, NULL_PTR);
3480 do_spec_1 (pl->prefix, 1, NULL_PTR);
3481 do_spec_1 (" ", 0, NULL_PTR);
3484 break;
3486 case 'o':
3487 for (i = 0; i < n_infiles; i++)
3488 store_arg (outfiles[i], 0, 0);
3489 break;
3491 case 'O':
3492 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
3493 arg_going = 1;
3494 break;
3496 case 's':
3497 this_is_library_file = 1;
3498 break;
3500 case 'w':
3501 this_is_output_file = 1;
3502 break;
3504 case 'W':
3506 int cur_index = argbuf_index;
3507 /* Handle the {...} following the %W. */
3508 if (*p != '{')
3509 abort ();
3510 p = handle_braces (p + 1);
3511 if (p == 0)
3512 return -1;
3513 /* If any args were output, mark the last one for deletion
3514 on failure. */
3515 if (argbuf_index != cur_index)
3516 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3517 break;
3520 /* %x{OPTION} records OPTION for %X to output. */
3521 case 'x':
3523 char *p1 = p;
3524 char *string;
3526 /* Skip past the option value and make a copy. */
3527 if (*p != '{')
3528 abort ();
3529 while (*p++ != '}')
3531 string = save_string (p1 + 1, p - p1 - 2);
3533 /* See if we already recorded this option. */
3534 for (i = 0; i < n_linker_options; i++)
3535 if (! strcmp (string, linker_options[i]))
3537 free (string);
3538 return 0;
3541 /* This option is new; add it. */
3542 n_linker_options++;
3543 if (!linker_options)
3544 linker_options
3545 = (char **) xmalloc (n_linker_options * sizeof (char **));
3546 else
3547 linker_options
3548 = (char **) xrealloc (linker_options,
3549 n_linker_options * sizeof (char **));
3551 linker_options[n_linker_options - 1] = string;
3553 break;
3555 /* Dump out the options accumulated previously using %x. */
3556 case 'X':
3557 for (i = 0; i < n_linker_options; i++)
3559 do_spec_1 (linker_options[i], 1, NULL_PTR);
3560 /* Make each accumulated option a separate argument. */
3561 do_spec_1 (" ", 0, NULL_PTR);
3563 break;
3565 /* Dump out the options accumulated previously using -Wa,. */
3566 case 'Y':
3567 for (i = 0; i < n_assembler_options; i++)
3569 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3570 /* Make each accumulated option a separate argument. */
3571 do_spec_1 (" ", 0, NULL_PTR);
3573 break;
3575 /* Dump out the options accumulated previously using -Wp,. */
3576 case 'Z':
3577 for (i = 0; i < n_preprocessor_options; i++)
3579 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
3580 /* Make each accumulated option a separate argument. */
3581 do_spec_1 (" ", 0, NULL_PTR);
3583 break;
3585 /* Here are digits and numbers that just process
3586 a certain constant string as a spec. */
3588 case '1':
3589 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3590 if (value != 0)
3591 return value;
3592 break;
3594 case '2':
3595 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3596 if (value != 0)
3597 return value;
3598 break;
3600 case 'a':
3601 value = do_spec_1 (asm_spec, 0, NULL_PTR);
3602 if (value != 0)
3603 return value;
3604 break;
3606 case 'A':
3607 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3608 if (value != 0)
3609 return value;
3610 break;
3612 case 'c':
3613 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3614 if (value != 0)
3615 return value;
3616 break;
3618 case 'C':
3619 value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3620 if (value != 0)
3621 return value;
3622 break;
3624 case 'E':
3625 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
3626 if (value != 0)
3627 return value;
3628 break;
3630 case 'l':
3631 value = do_spec_1 (link_spec, 0, NULL_PTR);
3632 if (value != 0)
3633 return value;
3634 break;
3636 case 'L':
3637 value = do_spec_1 (lib_spec, 0, NULL_PTR);
3638 if (value != 0)
3639 return value;
3640 break;
3642 case 'G':
3643 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
3644 if (value != 0)
3645 return value;
3646 break;
3648 case 'p':
3650 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
3651 char *buf = x;
3652 char *y;
3654 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
3655 y = cpp_predefines;
3656 while (*y != 0)
3658 if (! strncmp (y, "-D", 2))
3659 /* Copy the whole option. */
3660 while (*y && *y != ' ' && *y != '\t')
3661 *x++ = *y++;
3662 else if (*y == ' ' || *y == '\t')
3663 /* Copy whitespace to the result. */
3664 *x++ = *y++;
3665 /* Don't copy other options. */
3666 else
3667 y++;
3670 *x = 0;
3672 value = do_spec_1 (buf, 0, NULL_PTR);
3673 if (value != 0)
3674 return value;
3676 break;
3678 case 'P':
3680 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
3681 char *buf = x;
3682 char *y;
3684 /* Copy all of CPP_PREDEFINES into BUF,
3685 but put __ after every -D and at the end of each arg. */
3686 y = cpp_predefines;
3687 while (*y != 0)
3689 if (! strncmp (y, "-D", 2))
3691 int flag = 0;
3693 *x++ = *y++;
3694 *x++ = *y++;
3696 if (*y != '_'
3697 || (*(y+1) != '_' && ! isupper (*(y+1))))
3699 /* Stick __ at front of macro name. */
3700 *x++ = '_';
3701 *x++ = '_';
3702 /* Arrange to stick __ at the end as well. */
3703 flag = 1;
3706 /* Copy the macro name. */
3707 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3708 *x++ = *y++;
3710 if (flag)
3712 *x++ = '_';
3713 *x++ = '_';
3716 /* Copy the value given, if any. */
3717 while (*y && *y != ' ' && *y != '\t')
3718 *x++ = *y++;
3720 else if (*y == ' ' || *y == '\t')
3721 /* Copy whitespace to the result. */
3722 *x++ = *y++;
3723 /* Don't copy -A options */
3724 else
3725 y++;
3727 *x++ = ' ';
3729 /* Copy all of CPP_PREDEFINES into BUF,
3730 but put __ after every -D. */
3731 y = cpp_predefines;
3732 while (*y != 0)
3734 if (! strncmp (y, "-D", 2))
3736 y += 2;
3738 if (*y != '_'
3739 || (*(y+1) != '_' && ! isupper (*(y+1))))
3741 /* Stick -D__ at front of macro name. */
3742 *x++ = '-';
3743 *x++ = 'D';
3744 *x++ = '_';
3745 *x++ = '_';
3747 /* Copy the macro name. */
3748 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3749 *x++ = *y++;
3751 /* Copy the value given, if any. */
3752 while (*y && *y != ' ' && *y != '\t')
3753 *x++ = *y++;
3755 else
3757 /* Do not copy this macro - we have just done it before */
3758 while (*y && *y != ' ' && *y != '\t')
3759 y++;
3762 else if (*y == ' ' || *y == '\t')
3763 /* Copy whitespace to the result. */
3764 *x++ = *y++;
3765 /* Don't copy -A options */
3766 else
3767 y++;
3769 *x++ = ' ';
3771 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
3772 y = cpp_predefines;
3773 while (*y != 0)
3775 if (! strncmp (y, "-A", 2))
3776 /* Copy the whole option. */
3777 while (*y && *y != ' ' && *y != '\t')
3778 *x++ = *y++;
3779 else if (*y == ' ' || *y == '\t')
3780 /* Copy whitespace to the result. */
3781 *x++ = *y++;
3782 /* Don't copy other options. */
3783 else
3784 y++;
3787 *x = 0;
3789 value = do_spec_1 (buf, 0, NULL_PTR);
3790 if (value != 0)
3791 return value;
3793 break;
3795 case 'S':
3796 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
3797 if (value != 0)
3798 return value;
3799 break;
3801 /* Here we define characters other than letters and digits. */
3803 case '{':
3804 p = handle_braces (p);
3805 if (p == 0)
3806 return -1;
3807 break;
3809 case '%':
3810 obstack_1grow (&obstack, '%');
3811 break;
3813 case '*':
3814 do_spec_1 (soft_matched_part, 1, NULL_PTR);
3815 do_spec_1 (" ", 0, NULL_PTR);
3816 break;
3818 /* Process a string found as the value of a spec given by name.
3819 This feature allows individual machine descriptions
3820 to add and use their own specs.
3821 %[...] modifies -D options the way %P does;
3822 %(...) uses the spec unmodified. */
3823 case '(':
3824 case '[':
3826 char *name = p;
3827 struct spec_list *sl;
3828 int len;
3830 /* The string after the S/P is the name of a spec that is to be
3831 processed. */
3832 while (*p && *p != ')' && *p != ']')
3833 p++;
3835 /* See if it's in the list */
3836 for (len = p - name, sl = specs; sl; sl = sl->next)
3837 if (sl->name_len == len && !strncmp (sl->name, name, len))
3839 name = *(sl->ptr_spec);
3840 #ifdef DEBUG_SPECS
3841 fprintf (stderr, "Processing spec %c%s%c, which is '%s'\n",
3842 c, sl->name, (c == '(') ? ')' : ']', name);
3843 #endif
3844 break;
3847 if (sl)
3849 if (c == '(')
3851 value = do_spec_1 (name, 0, NULL_PTR);
3852 if (value != 0)
3853 return value;
3855 else
3857 char *x = (char *) alloca (strlen (name) * 2 + 1);
3858 char *buf = x;
3859 char *y = name;
3861 /* Copy all of NAME into BUF, but put __ after
3862 every -D and at the end of each arg, */
3863 while (1)
3865 int flag;
3867 if (! strncmp (y, "-D", 2))
3869 *x++ = '-';
3870 *x++ = 'D';
3871 *x++ = '_';
3872 *x++ = '_';
3873 y += 2;
3874 flag = 1;
3875 continue;
3877 else if (flag && (*y == ' ' || *y == '\t' || *y == '='
3878 || *y == '}' || *y == 0))
3880 *x++ = '_';
3881 *x++ = '_';
3882 flag = 0;
3884 if (*y == 0)
3885 break;
3886 else
3887 *x++ = *y++;
3889 *x = 0;
3891 value = do_spec_1 (buf, 0, NULL_PTR);
3892 if (value != 0)
3893 return value;
3897 /* Discard the closing paren or bracket. */
3898 if (*p)
3899 p++;
3901 break;
3903 case 'v':
3905 int c1 = *p++; /* Select first or second version number. */
3906 char *v = compiler_version;
3907 char *q;
3909 /* The format of the version string is
3910 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3912 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3913 while (! isdigit (*v))
3914 v++;
3915 if (v > compiler_version && v[-1] != '-')
3916 abort ();
3918 /* If desired, advance to second version number. */
3919 if (c1 == '2')
3921 /* Set V after the first period. */
3922 while (isdigit (*v))
3923 v++;
3924 if (*v != '.')
3925 abort ();
3926 v++;
3929 /* Set Q at the next period or at the end. */
3930 q = v;
3931 while (isdigit (*q))
3932 q++;
3933 if (*q != 0 && *q != ' ' && *q != '.' && *q != '-')
3934 abort ();
3936 /* Put that part into the command. */
3937 obstack_grow (&obstack, v, q - v);
3938 arg_going = 1;
3940 break;
3942 case '|':
3943 if (input_from_pipe)
3944 do_spec_1 ("-", 0, NULL_PTR);
3945 break;
3947 default:
3948 abort ();
3950 break;
3952 case '\\':
3953 /* Backslash: treat next character as ordinary. */
3954 c = *p++;
3956 /* fall through */
3957 default:
3958 /* Ordinary character: put it into the current argument. */
3959 obstack_1grow (&obstack, c);
3960 arg_going = 1;
3963 return 0; /* End of string */
3966 /* Return 0 if we call do_spec_1 and that returns -1. */
3968 static char *
3969 handle_braces (p)
3970 register char *p;
3972 register char *q;
3973 char *filter;
3974 int pipe_p = 0;
3975 int negate = 0;
3976 int suffix = 0;
3977 int include_blanks = 1;
3979 if (*p == '^')
3980 /* A '^' after the open-brace means to not give blanks before args. */
3981 include_blanks = 0, ++p;
3983 if (*p == '|')
3984 /* A `|' after the open-brace means,
3985 if the test fails, output a single minus sign rather than nothing.
3986 This is used in %{|!pipe:...}. */
3987 pipe_p = 1, ++p;
3989 if (*p == '!')
3990 /* A `!' after the open-brace negates the condition:
3991 succeed if the specified switch is not present. */
3992 negate = 1, ++p;
3994 if (*p == '.')
3995 /* A `.' after the open-brace means test against the current suffix. */
3997 if (pipe_p)
3998 abort ();
4000 suffix = 1;
4001 ++p;
4004 filter = p;
4005 while (*p != ':' && *p != '}') p++;
4006 if (*p != '}')
4008 register int count = 1;
4009 q = p + 1;
4010 while (count > 0)
4012 if (*q == '{')
4013 count++;
4014 else if (*q == '}')
4015 count--;
4016 else if (*q == 0)
4017 abort ();
4018 q++;
4021 else
4022 q = p + 1;
4024 if (suffix)
4026 int found = (input_suffix != 0
4027 && strlen (input_suffix) == p - filter
4028 && strncmp (input_suffix, filter, p - filter) == 0);
4030 if (p[0] == '}')
4031 abort ();
4033 if (negate != found
4034 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
4035 return 0;
4037 return q;
4039 else if (p[-1] == '*' && p[0] == '}')
4041 /* Substitute all matching switches as separate args. */
4042 register int i;
4043 --p;
4044 for (i = 0; i < n_switches; i++)
4045 if (!strncmp (switches[i].part1, filter, p - filter)
4046 && check_live_switch (i, p - filter))
4047 give_switch (i, 0, include_blanks);
4049 else
4051 /* Test for presence of the specified switch. */
4052 register int i;
4053 int present = 0;
4055 /* If name specified ends in *, as in {x*:...},
4056 check for %* and handle that case. */
4057 if (p[-1] == '*' && !negate)
4059 int substitution;
4060 char *r = p;
4062 /* First see whether we have %*. */
4063 substitution = 0;
4064 while (r < q)
4066 if (*r == '%' && r[1] == '*')
4067 substitution = 1;
4068 r++;
4070 /* If we do, handle that case. */
4071 if (substitution)
4073 /* Substitute all matching switches as separate args.
4074 But do this by substituting for %*
4075 in the text that follows the colon. */
4077 unsigned hard_match_len = p - filter - 1;
4078 char *string = save_string (p + 1, q - p - 2);
4080 for (i = 0; i < n_switches; i++)
4081 if (!strncmp (switches[i].part1, filter, hard_match_len)
4082 && check_live_switch (i, -1))
4084 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
4085 /* Pass any arguments this switch has. */
4086 give_switch (i, 1, 1);
4089 return q;
4093 /* If name specified ends in *, as in {x*:...},
4094 check for presence of any switch name starting with x. */
4095 if (p[-1] == '*')
4097 for (i = 0; i < n_switches; i++)
4099 unsigned hard_match_len = p - filter - 1;
4101 if (!strncmp (switches[i].part1, filter, hard_match_len)
4102 && check_live_switch (i, hard_match_len))
4104 present = 1;
4108 /* Otherwise, check for presence of exact name specified. */
4109 else
4111 for (i = 0; i < n_switches; i++)
4113 if (!strncmp (switches[i].part1, filter, p - filter)
4114 && switches[i].part1[p - filter] == 0
4115 && check_live_switch (i, -1))
4117 present = 1;
4118 break;
4123 /* If it is as desired (present for %{s...}, absent for %{-s...})
4124 then substitute either the switch or the specified
4125 conditional text. */
4126 if (present != negate)
4128 if (*p == '}')
4130 give_switch (i, 0, include_blanks);
4132 else
4134 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
4135 return 0;
4138 else if (pipe_p)
4140 /* Here if a %{|...} conditional fails: output a minus sign,
4141 which means "standard output" or "standard input". */
4142 do_spec_1 ("-", 0, NULL_PTR);
4146 return q;
4149 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
4150 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
4151 spec, or -1 if either exact match or %* is used.
4153 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
4154 whose value does not begin with "no-" is obsoleted by the same value
4155 with the "no-", similarly for a switch with the "no-" prefix. */
4157 static int
4158 check_live_switch (switchnum, prefix_length)
4159 int switchnum;
4160 int prefix_length;
4162 char *name = switches[switchnum].part1;
4163 int i;
4165 /* In the common case of {<at-most-one-letter>*}, a negating
4166 switch would always match, so ignore that case. We will just
4167 send the conflicting switches to the compiler phase. */
4168 if (prefix_length >= 0 && prefix_length <= 1)
4169 return 1;
4171 /* If we already processed this switch and determined if it was
4172 live or not, return our past determination. */
4173 if (switches[switchnum].live_cond != 0)
4174 return switches[switchnum].live_cond > 0;
4176 /* Now search for duplicate in a manner that depends on the name. */
4177 switch (*name)
4179 case 'O':
4180 for (i = switchnum + 1; i < n_switches; i++)
4181 if (switches[i].part1[0] == 'O')
4183 switches[switchnum].valid = 1;
4184 switches[switchnum].live_cond = -1;
4185 return 0;
4187 break;
4189 case 'W': case 'f': case 'm':
4190 if (! strncmp (name + 1, "no-", 3))
4192 /* We have Xno-YYY, search for XYYY. */
4193 for (i = switchnum + 1; i < n_switches; i++)
4194 if (switches[i].part1[0] == name[0]
4195 && ! strcmp (&switches[i].part1[1], &name[4]))
4197 switches[switchnum].valid = 1;
4198 switches[switchnum].live_cond = -1;
4199 return 0;
4202 else
4204 /* We have XYYY, search for Xno-YYY. */
4205 for (i = switchnum + 1; i < n_switches; i++)
4206 if (switches[i].part1[0] == name[0]
4207 && switches[i].part1[1] == 'n'
4208 && switches[i].part1[2] == 'o'
4209 && switches[i].part1[3] == '-'
4210 && !strcmp (&switches[i].part1[4], &name[1]))
4212 switches[switchnum].valid = 1;
4213 switches[switchnum].live_cond = -1;
4214 return 0;
4217 break;
4220 /* Otherwise the switch is live. */
4221 switches[switchnum].live_cond = 1;
4222 return 1;
4225 /* Pass a switch to the current accumulating command
4226 in the same form that we received it.
4227 SWITCHNUM identifies the switch; it is an index into
4228 the vector of switches gcc received, which is `switches'.
4229 This cannot fail since it never finishes a command line.
4231 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
4233 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
4234 of the switch. */
4236 static void
4237 give_switch (switchnum, omit_first_word, include_blanks)
4238 int switchnum;
4239 int omit_first_word;
4240 int include_blanks;
4242 if (!omit_first_word)
4244 do_spec_1 ("-", 0, NULL_PTR);
4245 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
4248 if (switches[switchnum].args != 0)
4250 char **p;
4251 for (p = switches[switchnum].args; *p; p++)
4253 if (include_blanks)
4254 do_spec_1 (" ", 0, NULL_PTR);
4255 do_spec_1 (*p, 1, NULL_PTR);
4259 do_spec_1 (" ", 0, NULL_PTR);
4260 switches[switchnum].valid = 1;
4263 /* Search for a file named NAME trying various prefixes including the
4264 user's -B prefix and some standard ones.
4265 Return the absolute file name found. If nothing is found, return NAME. */
4267 static char *
4268 find_file (name)
4269 char *name;
4271 char *newname;
4273 /* Try multilib_dir if it is defined. */
4274 if (multilib_dir != NULL)
4276 char *try;
4278 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
4279 strcpy (try, multilib_dir);
4280 strcat (try, dir_separator_str);
4281 strcat (try, name);
4283 newname = find_a_file (&startfile_prefixes, try, R_OK);
4285 /* If we don't find it in the multi library dir, then fall
4286 through and look for it in the normal places. */
4287 if (newname != NULL)
4288 return newname;
4291 newname = find_a_file (&startfile_prefixes, name, R_OK);
4292 return newname ? newname : name;
4295 /* Determine whether a directory exists. If LINKER, return 0 for
4296 certain fixed names not needed by the linker. If not LINKER, it is
4297 only important to return 0 if the host machine has a small ARG_MAX
4298 limit. */
4300 static int
4301 is_directory (path1, path2, linker)
4302 char *path1;
4303 char *path2;
4304 int linker;
4306 int len1 = strlen (path1);
4307 int len2 = strlen (path2);
4308 char *path = (char *) alloca (3 + len1 + len2);
4309 char *cp;
4310 struct stat st;
4312 #ifndef SMALL_ARG_MAX
4313 if (! linker)
4314 return 1;
4315 #endif
4317 /* Construct the path from the two parts. Ensure the string ends with "/.".
4318 The resulting path will be a directory even if the given path is a
4319 symbolic link. */
4320 bcopy (path1, path, len1);
4321 bcopy (path2, path + len1, len2);
4322 cp = path + len1 + len2;
4323 if (cp[-1] != '/' && cp[-1] != DIR_SEPARATOR)
4324 *cp++ = DIR_SEPARATOR;
4325 *cp++ = '.';
4326 *cp = '\0';
4328 /* Exclude directories that the linker is known to search. */
4329 if (linker
4330 && ((cp - path == 6
4331 && strcmp (path, concat (dir_separator_str, "lib",
4332 dir_separator_str, ".", NULL_PTR)) == 0)
4333 || (cp - path == 10
4334 && strcmp (path, concat (dir_separator_str, "usr",
4335 dir_separator_str, "lib",
4336 dir_separator_str, ".", NULL_PTR)) == 0)))
4337 return 0;
4339 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
4342 /* On fatal signals, delete all the temporary files. */
4344 static void
4345 fatal_error (signum)
4346 int signum;
4348 signal (signum, SIG_DFL);
4349 delete_failure_queue ();
4350 delete_temp_files ();
4351 /* Get the same signal again, this time not handled,
4352 so its normal effect occurs. */
4353 kill (getpid (), signum);
4357 main (argc, argv)
4358 int argc;
4359 char **argv;
4361 register int i;
4362 int j;
4363 int value;
4364 int linker_was_run = 0;
4365 char *explicit_link_files;
4366 char *specs_file;
4367 char *p;
4368 struct user_specs *uptr;
4370 p = argv[0] + strlen (argv[0]);
4371 while (p != argv[0] && p[-1] != '/' && p[-1] != DIR_SEPARATOR) --p;
4372 programname = p;
4374 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
4375 signal (SIGINT, fatal_error);
4376 #ifdef SIGHUP
4377 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
4378 signal (SIGHUP, fatal_error);
4379 #endif
4380 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
4381 signal (SIGTERM, fatal_error);
4382 #ifdef SIGPIPE
4383 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
4384 signal (SIGPIPE, fatal_error);
4385 #endif
4387 /* If this is a test release of GCC, issue a warning. */
4388 if (version_string[0] == 't' && version_string[1] == 'e')
4389 snapshot_warning ();
4391 argbuf_length = 10;
4392 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
4394 obstack_init (&obstack);
4396 /* Build multilib_select, et. al from the separate lines that make up each
4397 multilib selection. */
4399 char **q = multilib_raw;
4400 int need_space;
4402 obstack_init (&multilib_obstack);
4403 while ((p = *q++) != (char *) 0)
4404 obstack_grow (&multilib_obstack, p, strlen (p));
4406 obstack_1grow (&multilib_obstack, 0);
4407 multilib_select = obstack_finish (&multilib_obstack);
4409 q = multilib_matches_raw;
4410 while ((p = *q++) != (char *) 0)
4411 obstack_grow (&multilib_obstack, p, strlen (p));
4413 obstack_1grow (&multilib_obstack, 0);
4414 multilib_matches = obstack_finish (&multilib_obstack);
4416 need_space = FALSE;
4417 for (i = 0;
4418 i < sizeof (multilib_defaults_raw) / sizeof (multilib_defaults_raw[0]);
4419 i++)
4421 if (need_space)
4422 obstack_1grow (&multilib_obstack, ' ');
4423 obstack_grow (&multilib_obstack,
4424 multilib_defaults_raw[i],
4425 strlen (multilib_defaults_raw[i]));
4426 need_space = TRUE;
4429 obstack_1grow (&multilib_obstack, 0);
4430 multilib_defaults = obstack_finish (&multilib_obstack);
4433 /* Set up to remember the pathname of gcc and any options
4434 needed for collect. We use argv[0] instead of programname because
4435 we need the complete pathname. */
4436 obstack_init (&collect_obstack);
4437 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
4438 obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
4439 putenv (obstack_finish (&collect_obstack));
4441 #ifdef INIT_ENVIRONMENT
4442 /* Set up any other necessary machine specific environment variables. */
4443 putenv (INIT_ENVIRONMENT);
4444 #endif
4446 /* Choose directory for temp files. */
4448 temp_filename = choose_temp_base ();
4449 temp_filename_length = strlen (temp_filename);
4451 /* Make a table of what switches there are (switches, n_switches).
4452 Make a table of specified input files (infiles, n_infiles).
4453 Decode switches that are handled locally. */
4455 process_command (argc, argv);
4457 /* Initialize the vector of specs to just the default.
4458 This means one element containing 0s, as a terminator. */
4460 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
4461 bcopy ((char *) default_compilers, (char *) compilers,
4462 sizeof default_compilers);
4463 n_compilers = n_default_compilers;
4465 /* Read specs from a file if there is one. */
4467 machine_suffix = concat (spec_machine, dir_separator_str,
4468 spec_version, dir_separator_str, NULL_PTR);
4469 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
4471 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
4472 /* Read the specs file unless it is a default one. */
4473 if (specs_file != 0 && strcmp (specs_file, "specs"))
4474 read_specs (specs_file, TRUE);
4475 else
4476 init_spec ();
4478 /* We need to check standard_exec_prefix/just_machine_suffix/specs
4479 for any override of as, ld and libraries. */
4480 specs_file = (char *) alloca (strlen (standard_exec_prefix)
4481 + strlen (just_machine_suffix)
4482 + sizeof ("specs"));
4484 strcpy (specs_file, standard_exec_prefix);
4485 strcat (specs_file, just_machine_suffix);
4486 strcat (specs_file, "specs");
4487 if (access (specs_file, R_OK) == 0)
4488 read_specs (specs_file, TRUE);
4490 /* Process any user specified specs in the order given on the command
4491 line. */
4492 for (uptr = user_specs_head; uptr; uptr = uptr->next)
4494 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
4495 read_specs (filename ? filename : uptr->filename, FALSE);
4498 /* If not cross-compiling, look for startfiles in the standard places. */
4499 /* The fact that these are done here, after reading the specs file,
4500 means that it cannot be found in these directories.
4501 But that's okay. It should never be there anyway. */
4502 if (*cross_compile == '0')
4504 #ifdef MD_EXEC_PREFIX
4505 add_prefix (&exec_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4506 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4507 #endif
4509 #ifdef MD_STARTFILE_PREFIX
4510 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
4511 0, 0, NULL_PTR);
4512 #endif
4514 #ifdef MD_STARTFILE_PREFIX_1
4515 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
4516 0, 0, NULL_PTR);
4517 #endif
4519 /* If standard_startfile_prefix is relative, base it on
4520 standard_exec_prefix. This lets us move the installed tree
4521 as a unit. If GCC_EXEC_PREFIX is defined, base
4522 standard_startfile_prefix on that as well. */
4523 if (*standard_startfile_prefix == '/'
4524 || *standard_startfile_prefix == DIR_SEPARATOR
4525 || *standard_startfile_prefix == '$'
4526 #ifdef __MSDOS__
4527 /* Check for disk name on MS-DOS-based systems. */
4528 || (standard_startfile_prefix[1] == ':'
4529 && (standard_startfile_prefix[2] == DIR_SEPARATOR
4530 || standard_startfile_prefix[2] == '/'))
4531 #endif
4533 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
4534 0, 0, NULL_PTR);
4535 else
4537 if (gcc_exec_prefix)
4538 add_prefix (&startfile_prefixes,
4539 concat (gcc_exec_prefix, machine_suffix,
4540 standard_startfile_prefix, NULL_PTR),
4541 NULL_PTR, 0, 0, NULL_PTR);
4542 add_prefix (&startfile_prefixes,
4543 concat (standard_exec_prefix,
4544 machine_suffix,
4545 standard_startfile_prefix, NULL_PTR),
4546 NULL_PTR, 0, 0, NULL_PTR);
4549 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
4550 "BINUTILS", 0, 0, NULL_PTR);
4551 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
4552 "BINUTILS", 0, 0, NULL_PTR);
4553 #if 0 /* Can cause surprises, and one can use -B./ instead. */
4554 add_prefix (&startfile_prefixes, "./", NULL_PTR, 0, 1, NULL_PTR);
4555 #endif
4557 else
4559 if (*standard_startfile_prefix != DIR_SEPARATOR && gcc_exec_prefix)
4560 add_prefix (&startfile_prefixes,
4561 concat (gcc_exec_prefix, machine_suffix,
4562 standard_startfile_prefix, NULL_PTR),
4563 "BINUTILS", 0, 0, NULL_PTR);
4566 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
4567 if (gcc_exec_prefix)
4569 char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
4570 + strlen (spec_version)
4571 + strlen (spec_machine) + 3);
4572 strcpy (temp, gcc_exec_prefix);
4573 strcat (temp, spec_machine);
4574 strcat (temp, dir_separator_str);
4575 strcat (temp, spec_version);
4576 strcat (temp, dir_separator_str);
4577 gcc_exec_prefix = temp;
4580 /* Now we have the specs.
4581 Set the `valid' bits for switches that match anything in any spec. */
4583 validate_all_switches ();
4585 /* Now that we have the switches and the specs, set
4586 the subdirectory based on the options. */
4587 set_multilib_dir ();
4589 /* Warn about any switches that no pass was interested in. */
4591 for (i = 0; i < n_switches; i++)
4592 if (! switches[i].valid)
4593 error ("unrecognized option `-%s'", switches[i].part1);
4595 /* Obey some of the options. */
4597 if (print_search_dirs)
4599 printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
4600 printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
4601 printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
4602 exit (0);
4605 if (print_file_name)
4607 printf ("%s\n", find_file (print_file_name));
4608 exit (0);
4611 if (print_prog_name)
4613 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
4614 printf ("%s\n", (newname ? newname : print_prog_name));
4615 exit (0);
4618 if (print_multi_lib)
4620 print_multilib_info ();
4621 exit (0);
4624 if (print_multi_directory)
4626 if (multilib_dir == NULL)
4627 printf (".\n");
4628 else
4629 printf ("%s\n", multilib_dir);
4630 exit (0);
4633 if (verbose_flag)
4635 int n;
4637 /* compiler_version is truncated at the first space when initialized
4638 from version string, so truncate version_string at the first space
4639 before comparing. */
4640 for (n = 0; version_string[n]; n++)
4641 if (version_string[n] == ' ')
4642 break;
4644 if (! strncmp (version_string, compiler_version, n)
4645 && compiler_version[n] == 0)
4646 fprintf (stderr, "gcc version %s\n", version_string);
4647 else
4648 fprintf (stderr, "gcc driver version %s executing gcc version %s\n",
4649 version_string, compiler_version);
4651 if (n_infiles == 0)
4652 exit (0);
4655 if (n_infiles == 0)
4656 fatal ("No input files");
4658 /* Make a place to record the compiler output file names
4659 that correspond to the input files. */
4661 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
4662 bzero ((char *) outfiles, n_infiles * sizeof (char *));
4664 /* Record which files were specified explicitly as link input. */
4666 explicit_link_files = xmalloc (n_infiles);
4667 bzero (explicit_link_files, n_infiles);
4669 for (i = 0; i < n_infiles; i++)
4671 register struct compiler *cp = 0;
4672 int this_file_error = 0;
4674 /* Tell do_spec what to substitute for %i. */
4676 input_filename = infiles[i].name;
4677 input_filename_length = strlen (input_filename);
4678 input_file_number = i;
4680 /* Use the same thing in %o, unless cp->spec says otherwise. */
4682 outfiles[i] = input_filename;
4684 /* Figure out which compiler from the file's suffix. */
4686 cp = lookup_compiler (infiles[i].name, input_filename_length,
4687 infiles[i].language);
4689 if (cp)
4691 /* Ok, we found an applicable compiler. Run its spec. */
4692 /* First say how much of input_filename to substitute for %b */
4693 register char *p;
4694 int len;
4696 if (cp->spec[0][0] == '#')
4697 error ("%s: %s compiler not installed on this system",
4698 input_filename, &cp->spec[0][1]);
4700 input_basename = input_filename;
4701 for (p = input_filename; *p; p++)
4702 if (*p == '/' || *p == DIR_SEPARATOR)
4703 input_basename = p + 1;
4705 /* Find a suffix starting with the last period,
4706 and set basename_length to exclude that suffix. */
4707 basename_length = strlen (input_basename);
4708 p = input_basename + basename_length;
4709 while (p != input_basename && *p != '.') --p;
4710 if (*p == '.' && p != input_basename)
4712 basename_length = p - input_basename;
4713 input_suffix = p + 1;
4715 else
4716 input_suffix = "";
4718 len = 0;
4719 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4720 if (cp->spec[j])
4721 len += strlen (cp->spec[j]);
4723 p = (char *) xmalloc (len + 1);
4725 len = 0;
4726 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4727 if (cp->spec[j])
4729 strcpy (p + len, cp->spec[j]);
4730 len += strlen (cp->spec[j]);
4733 value = do_spec (p);
4734 free (p);
4735 if (value < 0)
4736 this_file_error = 1;
4739 /* If this file's name does not contain a recognized suffix,
4740 record it as explicit linker input. */
4742 else
4743 explicit_link_files[i] = 1;
4745 /* Clear the delete-on-failure queue, deleting the files in it
4746 if this compilation failed. */
4748 if (this_file_error)
4750 delete_failure_queue ();
4751 error_count++;
4753 /* If this compilation succeeded, don't delete those files later. */
4754 clear_failure_queue ();
4757 /* Run ld to link all the compiler output files. */
4759 if (error_count == 0)
4761 int tmp = execution_count;
4762 int i;
4763 int first_time;
4765 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
4766 for collect. */
4767 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
4768 putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
4770 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4771 the compiler. */
4772 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4773 sizeof ("COLLECT_GCC_OPTIONS=")-1);
4775 first_time = TRUE;
4776 for (i = 0; i < n_switches; i++)
4778 char **args;
4779 if (!first_time)
4780 obstack_grow (&collect_obstack, " ", 1);
4782 first_time = FALSE;
4783 obstack_grow (&collect_obstack, "-", 1);
4784 obstack_grow (&collect_obstack, switches[i].part1,
4785 strlen (switches[i].part1));
4787 for (args = switches[i].args; args && *args; args++)
4789 obstack_grow (&collect_obstack, " ", 1);
4790 obstack_grow (&collect_obstack, *args, strlen (*args));
4793 obstack_grow (&collect_obstack, "\0", 1);
4794 putenv (obstack_finish (&collect_obstack));
4796 value = do_spec (link_command_spec);
4797 if (value < 0)
4798 error_count = 1;
4799 linker_was_run = (tmp != execution_count);
4802 /* Warn if a -B option was specified but the prefix was never used. */
4803 unused_prefix_warnings (&exec_prefixes);
4804 unused_prefix_warnings (&startfile_prefixes);
4806 /* If options said don't run linker,
4807 complain about input files to be given to the linker. */
4809 if (! linker_was_run && error_count == 0)
4810 for (i = 0; i < n_infiles; i++)
4811 if (explicit_link_files[i])
4812 error ("%s: linker input file unused since linking not done",
4813 outfiles[i]);
4815 /* Delete some or all of the temporary files we made. */
4817 if (error_count)
4818 delete_failure_queue ();
4819 delete_temp_files ();
4821 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
4822 /* NOTREACHED */
4823 return 0;
4826 /* Find the proper compilation spec for the file name NAME,
4827 whose length is LENGTH. LANGUAGE is the specified language,
4828 or 0 if this file is to be passed to the linker. */
4830 static struct compiler *
4831 lookup_compiler (name, length, language)
4832 char *name;
4833 int length;
4834 char *language;
4836 struct compiler *cp;
4838 /* If this was specified by the user to be a linker input, indicate that. */
4839 if (language != 0 && language[0] == '*')
4840 return 0;
4842 /* Otherwise, look for the language, if one is spec'd. */
4843 if (language != 0)
4845 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4846 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
4847 return cp;
4849 error ("language %s not recognized", language);
4850 return 0;
4853 /* Look for a suffix. */
4854 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4856 if (/* The suffix `-' matches only the file name `-'. */
4857 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
4858 || (strlen (cp->suffix) < length
4859 /* See if the suffix matches the end of NAME. */
4860 #ifdef OS2
4861 && ((!strcmp (cp->suffix,
4862 name + length - strlen (cp->suffix))
4863 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
4864 && !strcasecmp (cp->suffix,
4865 name + length - strlen (cp->suffix)))
4866 #else
4867 && !strcmp (cp->suffix,
4868 name + length - strlen (cp->suffix))
4869 #endif
4872 if (cp->spec[0][0] == '@')
4874 struct compiler *new;
4876 /* An alias entry maps a suffix to a language.
4877 Search for the language; pass 0 for NAME and LENGTH
4878 to avoid infinite recursion if language not found.
4879 Construct the new compiler spec. */
4880 language = cp->spec[0] + 1;
4881 new = (struct compiler *) xmalloc (sizeof (struct compiler));
4882 new->suffix = cp->suffix;
4883 bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
4884 (char *) new->spec, sizeof new->spec);
4885 return new;
4888 /* A non-alias entry: return it. */
4889 return cp;
4893 return 0;
4896 char *
4897 xmalloc (size)
4898 unsigned size;
4900 register char *value = (char *) malloc (size);
4901 if (value == 0)
4902 fatal ("virtual memory exhausted");
4903 return value;
4906 char *
4907 xrealloc (ptr, size)
4908 char *ptr;
4909 unsigned size;
4911 register char *value = (char *) realloc (ptr, size);
4912 if (value == 0)
4913 fatal ("virtual memory exhausted");
4914 return value;
4917 /* This function is based on the one in libiberty. */
4919 static char *
4920 concat VPROTO((char *first, ...))
4922 register int length;
4923 register char *newstr;
4924 register char *end;
4925 register char *arg;
4926 va_list args;
4927 #ifndef __STDC__
4928 char *first;
4929 #endif
4931 /* First compute the size of the result and get sufficient memory. */
4933 VA_START (args, first);
4934 #ifndef __STDC__
4935 first = va_arg (args, char *);
4936 #endif
4938 arg = first;
4939 length = 0;
4941 while (arg != 0)
4943 length += strlen (arg);
4944 arg = va_arg (args, char *);
4947 newstr = (char *) xmalloc (length + 1);
4948 va_end (args);
4950 /* Now copy the individual pieces to the result string. */
4952 VA_START (args, first);
4953 #ifndef __STDC__
4954 first = va_arg (args, char *);
4955 #endif
4957 end = newstr;
4958 arg = first;
4959 while (arg != 0)
4961 while (*arg)
4962 *end++ = *arg++;
4963 arg = va_arg (args, char *);
4965 *end = '\000';
4966 va_end (args);
4968 return (newstr);
4971 static char *
4972 save_string (s, len)
4973 char *s;
4974 int len;
4976 register char *result = xmalloc (len + 1);
4978 bcopy (s, result, len);
4979 result[len] = 0;
4980 return result;
4983 static void
4984 pfatal_with_name (name)
4985 char *name;
4987 fatal ("%s: %s", name, my_strerror (errno));
4990 static void
4991 perror_with_name (name)
4992 char *name;
4994 error ("%s: %s", name, my_strerror (errno));
4997 static void
4998 pfatal_pexecute (errmsg_fmt, errmsg_arg)
4999 char *errmsg_fmt;
5000 char *errmsg_arg;
5002 int save_errno = errno;
5004 if (errmsg_arg)
5006 /* Space for trailing '\0' is in %s. */
5007 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
5008 sprintf (msg, errmsg_fmt, errmsg_arg);
5009 errmsg_fmt = msg;
5012 fatal ("%s: %s", errmsg_fmt, my_strerror (save_errno));
5015 /* More 'friendly' abort that prints the line and file.
5016 config.h can #define abort fancy_abort if you like that sort of thing. */
5018 void
5019 fancy_abort ()
5021 fatal ("Internal gcc abort.");
5024 #ifdef HAVE_VPRINTF
5026 /* Output an error message and exit */
5028 static void
5029 fatal VPROTO((char *format, ...))
5031 #ifndef __STDC__
5032 char *format;
5033 #endif
5034 va_list ap;
5036 VA_START (ap, format);
5038 #ifndef __STDC__
5039 format = va_arg (ap, char *);
5040 #endif
5042 fprintf (stderr, "%s: ", programname);
5043 vfprintf (stderr, format, ap);
5044 va_end (ap);
5045 fprintf (stderr, "\n");
5046 delete_temp_files ();
5047 exit (1);
5050 static void
5051 error VPROTO((char *format, ...))
5053 #ifndef __STDC__
5054 char *format;
5055 #endif
5056 va_list ap;
5058 VA_START (ap, format);
5060 #ifndef __STDC__
5061 format = va_arg (ap, char *);
5062 #endif
5064 fprintf (stderr, "%s: ", programname);
5065 vfprintf (stderr, format, ap);
5066 va_end (ap);
5068 fprintf (stderr, "\n");
5071 #else /* not HAVE_VPRINTF */
5073 static void
5074 fatal (msg, arg1, arg2)
5075 char *msg, *arg1, *arg2;
5077 error (msg, arg1, arg2);
5078 delete_temp_files ();
5079 exit (1);
5082 static void
5083 error (msg, arg1, arg2)
5084 char *msg, *arg1, *arg2;
5086 fprintf (stderr, "%s: ", programname);
5087 fprintf (stderr, msg, arg1, arg2);
5088 fprintf (stderr, "\n");
5091 #endif /* not HAVE_VPRINTF */
5094 static void
5095 validate_all_switches ()
5097 struct compiler *comp;
5098 register char *p;
5099 register char c;
5100 struct spec_list *spec;
5102 for (comp = compilers; comp->spec[0]; comp++)
5104 int i;
5105 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
5107 p = comp->spec[i];
5108 while (c = *p++)
5109 if (c == '%' && *p == '{')
5110 /* We have a switch spec. */
5111 validate_switches (p + 1);
5115 /* look through the linked list of specs read from the specs file */
5116 for (spec = specs; spec ; spec = spec->next)
5118 p = *(spec->ptr_spec);
5119 while (c = *p++)
5120 if (c == '%' && *p == '{')
5121 /* We have a switch spec. */
5122 validate_switches (p + 1);
5125 p = link_command_spec;
5126 while (c = *p++)
5127 if (c == '%' && *p == '{')
5128 /* We have a switch spec. */
5129 validate_switches (p + 1);
5132 /* Look at the switch-name that comes after START
5133 and mark as valid all supplied switches that match it. */
5135 static void
5136 validate_switches (start)
5137 char *start;
5139 register char *p = start;
5140 char *filter;
5141 register int i;
5142 int suffix = 0;
5144 if (*p == '|')
5145 ++p;
5147 if (*p == '!')
5148 ++p;
5150 if (*p == '.')
5151 suffix = 1, ++p;
5153 filter = p;
5154 while (*p != ':' && *p != '}') p++;
5156 if (suffix)
5158 else if (p[-1] == '*')
5160 /* Mark all matching switches as valid. */
5161 --p;
5162 for (i = 0; i < n_switches; i++)
5163 if (!strncmp (switches[i].part1, filter, p - filter))
5164 switches[i].valid = 1;
5166 else
5168 /* Mark an exact matching switch as valid. */
5169 for (i = 0; i < n_switches; i++)
5171 if (!strncmp (switches[i].part1, filter, p - filter)
5172 && switches[i].part1[p - filter] == 0)
5173 switches[i].valid = 1;
5178 /* Check whether a particular argument was used. The first time we
5179 canonicalize the switches to keep only the ones we care about. */
5181 static int
5182 used_arg (p, len)
5183 char *p;
5184 int len;
5186 struct mswitchstr {
5187 char *str;
5188 char *replace;
5189 int len;
5190 int rep_len;
5193 static struct mswitchstr *mswitches;
5194 static int n_mswitches;
5195 int i, j;
5197 if (!mswitches)
5199 struct mswitchstr *matches;
5200 char *q;
5201 int cnt = 0;
5203 /* Break multilib_matches into the component strings of string and replacement
5204 string */
5205 for (q = multilib_matches; *q != '\0'; q++)
5206 if (*q == ';')
5207 cnt++;
5209 matches = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
5210 i = 0;
5211 q = multilib_matches;
5212 while (*q != '\0')
5214 matches[i].str = q;
5215 while (*q != ' ')
5217 if (*q == '\0')
5218 abort ();
5219 q++;
5221 *q = '\0';
5222 matches[i].len = q - matches[i].str;
5224 matches[i].replace = ++q;
5225 while (*q != ';' && *q != '\0')
5227 if (*q == ' ')
5228 abort ();
5229 q++;
5231 matches[i].rep_len = q - matches[i].replace;
5232 i++;
5233 if (*q == ';')
5234 *q++ = '\0';
5235 else
5236 break;
5239 /* Now build a list of the replacement string for switches that we care
5240 about. Make sure we allocate at least one entry. This prevents
5241 xmalloc from calling fatal, and prevents us from re-executing this
5242 block of code. */
5243 mswitches
5244 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
5245 * (n_switches ? n_switches : 1));
5246 for (i = 0; i < n_switches; i++)
5248 int xlen = strlen (switches[i].part1);
5249 for (j = 0; j < cnt; j++)
5250 if (xlen == matches[j].len && ! strcmp (switches[i].part1, matches[j].str))
5252 mswitches[n_mswitches].str = matches[j].replace;
5253 mswitches[n_mswitches].len = matches[j].rep_len;
5254 mswitches[n_mswitches].replace = (char *)0;
5255 mswitches[n_mswitches].rep_len = 0;
5256 n_mswitches++;
5257 break;
5262 for (i = 0; i < n_mswitches; i++)
5263 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
5264 return 1;
5266 return 0;
5269 static int
5270 default_arg (p, len)
5271 char *p;
5272 int len;
5274 char *start, *end;
5275 int i;
5277 for (start = multilib_defaults; *start != '\0'; start = end+1)
5279 while (*start == ' ' || *start == '\t')
5280 start++;
5282 if (*start == '\0')
5283 break;
5285 for (end = start+1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
5288 if ((end - start) == len && strncmp (p, start, len) == 0)
5289 return 1;
5292 return 0;
5295 /* Work out the subdirectory to use based on the
5296 options. The format of multilib_select is a list of elements.
5297 Each element is a subdirectory name followed by a list of options
5298 followed by a semicolon. gcc will consider each line in turn. If
5299 none of the options beginning with an exclamation point are
5300 present, and all of the other options are present, that
5301 subdirectory will be used. */
5303 static void
5304 set_multilib_dir ()
5306 char *p = multilib_select;
5307 int this_path_len;
5308 char *this_path, *this_arg;
5309 int not_arg;
5310 int ok;
5312 while (*p != '\0')
5314 /* Ignore newlines. */
5315 if (*p == '\n')
5317 ++p;
5318 continue;
5321 /* Get the initial path. */
5322 this_path = p;
5323 while (*p != ' ')
5325 if (*p == '\0')
5326 abort ();
5327 ++p;
5329 this_path_len = p - this_path;
5331 /* Check the arguments. */
5332 ok = 1;
5333 ++p;
5334 while (*p != ';')
5336 if (*p == '\0')
5337 abort ();
5339 if (! ok)
5341 ++p;
5342 continue;
5345 this_arg = p;
5346 while (*p != ' ' && *p != ';')
5348 if (*p == '\0')
5349 abort ();
5350 ++p;
5353 if (*this_arg != '!')
5354 not_arg = 0;
5355 else
5357 not_arg = 1;
5358 ++this_arg;
5361 /* If this is a default argument, we can just ignore it.
5362 This is true even if this_arg begins with '!'. Beginning
5363 with '!' does not mean that this argument is necessarily
5364 inappropriate for this library: it merely means that
5365 there is a more specific library which uses this
5366 argument. If this argument is a default, we need not
5367 consider that more specific library. */
5368 if (! default_arg (this_arg, p - this_arg))
5370 ok = used_arg (this_arg, p - this_arg);
5371 if (not_arg)
5372 ok = ! ok;
5375 if (*p == ' ')
5376 ++p;
5379 if (ok)
5381 if (this_path_len != 1
5382 || this_path[0] != '.')
5384 multilib_dir = xmalloc (this_path_len + 1);
5385 strncpy (multilib_dir, this_path, this_path_len);
5386 multilib_dir[this_path_len] = '\0';
5388 break;
5391 ++p;
5395 /* Print out the multiple library subdirectory selection
5396 information. This prints out a series of lines. Each line looks
5397 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
5398 required. Only the desired options are printed out, the negative
5399 matches. The options are print without a leading dash. There are
5400 no spaces to make it easy to use the information in the shell.
5401 Each subdirectory is printed only once. This assumes the ordering
5402 generated by the genmultilib script. */
5404 static void
5405 print_multilib_info ()
5407 char *p = multilib_select;
5408 char *last_path = 0, *this_path;
5409 int skip;
5410 int last_path_len = 0;
5412 while (*p != '\0')
5414 /* Ignore newlines. */
5415 if (*p == '\n')
5417 ++p;
5418 continue;
5421 /* Get the initial path. */
5422 this_path = p;
5423 while (*p != ' ')
5425 if (*p == '\0')
5426 abort ();
5427 ++p;
5430 /* If this is a duplicate, skip it. */
5431 skip = (last_path != 0 && p - this_path == last_path_len
5432 && ! strncmp (last_path, this_path, last_path_len));
5434 last_path = this_path;
5435 last_path_len = p - this_path;
5437 /* If this directory requires any default arguments, we can skip
5438 it. We will already have printed a directory identical to
5439 this one which does not require that default argument. */
5440 if (! skip)
5442 char *q;
5444 q = p + 1;
5445 while (*q != ';')
5447 char *arg;
5449 if (*q == '\0')
5450 abort ();
5452 if (*q == '!')
5453 arg = NULL;
5454 else
5455 arg = q;
5457 while (*q != ' ' && *q != ';')
5459 if (*q == '\0')
5460 abort ();
5461 ++q;
5464 if (arg != NULL
5465 && default_arg (arg, q - arg))
5467 skip = 1;
5468 break;
5471 if (*q == ' ')
5472 ++q;
5476 if (! skip)
5478 char *p1;
5480 for (p1 = last_path; p1 < p; p1++)
5481 putchar (*p1);
5482 putchar (';');
5485 ++p;
5486 while (*p != ';')
5488 int use_arg;
5490 if (*p == '\0')
5491 abort ();
5493 if (skip)
5495 ++p;
5496 continue;
5499 use_arg = *p != '!';
5501 if (use_arg)
5502 putchar ('@');
5504 while (*p != ' ' && *p != ';')
5506 if (*p == '\0')
5507 abort ();
5508 if (use_arg)
5509 putchar (*p);
5510 ++p;
5513 if (*p == ' ')
5514 ++p;
5517 if (! skip)
5519 /* If there are extra options, print them now */
5520 if (multilib_extra && *multilib_extra)
5522 int print_at = TRUE;
5523 char *q;
5525 for (q = multilib_extra; *q != '\0'; q++)
5527 if (*q == ' ')
5528 print_at = TRUE;
5529 else
5531 if (print_at)
5532 putchar ('@');
5533 putchar (*q);
5534 print_at = FALSE;
5538 putchar ('\n');
5541 ++p;
5545 /* If a snapshot, warn the user that this version of gcc is for testing and
5546 developing only. If we can find a home directory, we can restrict the
5547 warning to once per day. Otherwise always issue it. */
5549 #define TIMESTAMP_FILE ".gcc-test-time"
5550 #define ONE_DAY (24*60*60)
5552 static void
5553 snapshot_warning ()
5555 char *home;
5556 int print_p = 1;
5558 /* Every function here but `time' is called elsewhere in this file,
5559 but we only can be sure we have it for Unix and the Windows systems,
5560 so conditionalize this on those.
5562 ??? This should use autoconf at some point. */
5564 #if defined(unix) || defined(__CYGWIN32__) || defined(_MINGW32__)
5566 home = getenv ("HOME");
5567 if (home != 0)
5569 char *file_name
5570 = (char *) alloca (strlen (home) + 1 + sizeof (TIMESTAMP_FILE));
5571 struct stat statbuf;
5572 time_t now = time (NULL);
5573 int s;
5575 sprintf (file_name, "%s/%s", home, TIMESTAMP_FILE);
5576 s = stat (file_name, &statbuf);
5577 if (s == 0
5578 && (statbuf.st_mtime + ONE_DAY > now))
5579 print_p = 0;
5580 else
5582 FILE *f = fopen (file_name, "w");
5584 if (f != 0)
5586 fputc ('\n', f);
5587 fclose (f);
5591 #endif
5593 if (print_p)
5595 fprintf (stderr, "*** This is a development snapshot of GCC.\n");
5596 fprintf (stderr,
5597 "*** It is not a reliable release, and the GCC developers\n");
5598 fprintf (stderr,
5599 "*** warn you not to use it for anything except to test it.\n");