Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / gcc.c
blob7e72fee1b91ccc3e8afcc827c26c6f6e26f5758e
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". */
35 #include "config.h"
36 #include "system.h"
37 #include <signal.h>
38 #include "obstack.h"
39 #include "gansidecl.h"
40 #include "intl.h"
42 /* ??? Need to find a GCC header to put these in. */
43 extern int pexecute PROTO ((const char *, char * const *, const char *,
44 const char *, char **, char **, int));
45 extern int pwait PROTO ((int, int *, int));
46 extern char *update_path PROTO((char *, char *));
47 /* Flag arguments to pexecute. */
48 #define PEXECUTE_FIRST 1
49 #define PEXECUTE_LAST 2
50 #define PEXECUTE_SEARCH 4
51 #define PEXECUTE_VERBOSE 8
54 #ifdef VMS
55 #define exit __posix_exit
56 #endif
58 /* By default there is no special suffix for executables. */
59 #ifdef EXECUTABLE_SUFFIX
60 #define HAVE_EXECUTABLE_SUFFIX
61 #else
62 #define EXECUTABLE_SUFFIX ""
63 #endif
65 /* By default, the suffix for object files is ".o". */
66 #ifdef OBJECT_SUFFIX
67 #define HAVE_OBJECT_SUFFIX
68 #else
69 #define OBJECT_SUFFIX ".o"
70 #endif
72 /* By default, colon separates directories in a path. */
73 #ifndef PATH_SEPARATOR
74 #define PATH_SEPARATOR ':'
75 #endif
77 #ifndef DIR_SEPARATOR
78 #define DIR_SEPARATOR '/'
79 #endif
81 static char dir_separator_str[] = {DIR_SEPARATOR, 0};
83 #define obstack_chunk_alloc xmalloc
84 #define obstack_chunk_free free
86 extern char *choose_temp_base PROTO((void));
88 #ifndef HAVE_KILL
89 #define kill(p,s) raise(s)
90 #endif
92 /* If a stage of compilation returns an exit status >= 1,
93 compilation of that file ceases. */
95 #define MIN_FATAL_STATUS 1
97 /* Flag saying to print the directories gcc will search through looking for
98 programs, libraries, etc. */
100 static int print_search_dirs;
102 /* Flag saying to print the full filename of this file
103 as found through our usual search mechanism. */
105 static char *print_file_name = NULL;
107 /* As print_file_name, but search for executable file. */
109 static char *print_prog_name = NULL;
111 /* Flag saying to print the relative path we'd use to
112 find libgcc.a given the current compiler flags. */
114 static int print_multi_directory;
116 /* Flag saying to print the list of subdirectories and
117 compiler flags used to select them in a standard form. */
119 static int print_multi_lib;
121 /* Flag indicating whether we should print the command and arguments */
123 static int verbose_flag;
125 /* Nonzero means write "temp" files in source directory
126 and use the source file's name in them, and don't delete them. */
128 static int save_temps_flag;
130 /* The compiler version. */
132 static char *compiler_version;
134 /* The target version specified with -V */
136 static char *spec_version = DEFAULT_TARGET_VERSION;
138 /* The target machine specified with -b. */
140 static char *spec_machine = DEFAULT_TARGET_MACHINE;
142 /* Nonzero if cross-compiling.
143 When -b is used, the value comes from the `specs' file. */
145 #ifdef CROSS_COMPILE
146 static char *cross_compile = "1";
147 #else
148 static char *cross_compile = "0";
149 #endif
151 /* The number of errors that have occurred; the link phase will not be
152 run if this is non-zero. */
153 static int error_count = 0;
155 /* This is the obstack which we use to allocate many strings. */
157 static struct obstack obstack;
159 /* This is the obstack to build an environment variable to pass to
160 collect2 that describes all of the relevant switches of what to
161 pass the compiler in building the list of pointers to constructors
162 and destructors. */
164 static struct obstack collect_obstack;
166 extern char *version_string;
168 /* Forward declaration for prototypes. */
169 struct path_prefix;
171 static void init_spec PROTO((void));
172 static void read_specs PROTO((char *, int));
173 static void set_spec PROTO((char *, char *));
174 static struct compiler *lookup_compiler PROTO((char *, int, char *));
175 static char *build_search_list PROTO((struct path_prefix *, char *, int));
176 static void putenv_from_prefixes PROTO((struct path_prefix *, char *));
177 static char *find_a_file PROTO((struct path_prefix *, char *, int));
178 static void add_prefix PROTO((struct path_prefix *, char *, char *,
179 int, int, int *));
180 static char *skip_whitespace PROTO((char *));
181 static void record_temp_file PROTO((char *, int, int));
182 static void delete_if_ordinary PROTO((char *));
183 static void delete_temp_files PROTO((void));
184 static void delete_failure_queue PROTO((void));
185 static void clear_failure_queue PROTO((void));
186 static int check_live_switch PROTO((int, int));
187 static char *handle_braces PROTO((char *));
188 static char *save_string PROTO((char *, int));
189 static char *concat PVPROTO((char *, ...));
190 static int do_spec PROTO((char *));
191 static int do_spec_1 PROTO((char *, int, char *));
192 static char *find_file PROTO((char *));
193 static int is_directory PROTO((char *, char *, int));
194 static void validate_switches PROTO((char *));
195 static void validate_all_switches PROTO((void));
196 static void give_switch PROTO((int, int, int));
197 static int used_arg PROTO((char *, int));
198 static int default_arg PROTO((char *, int));
199 static void set_multilib_dir PROTO((void));
200 static void print_multilib_info PROTO((void));
201 static void pfatal_with_name PROTO((char *));
202 static void perror_with_name PROTO((char *));
203 static void pfatal_pexecute PROTO((char *, char *));
204 static void snapshot_warning PROTO((void));
205 #ifdef HAVE_VPRINTF
206 static void fatal PVPROTO((char *, ...));
207 static void error PVPROTO((char *, ...));
208 static void notice PVPROTO((char *, ...));
209 #else
210 /* We must not provide any prototype here, even if ANSI C. */
211 static void fatal PROTO(());
212 static void error PROTO(());
213 static void notice PROTO(());
214 #endif
216 void fancy_abort ();
217 char *xmalloc ();
218 char *xrealloc ();
220 /* Specs are strings containing lines, each of which (if not blank)
221 is made up of a program name, and arguments separated by spaces.
222 The program name must be exact and start from root, since no path
223 is searched and it is unreliable to depend on the current working directory.
224 Redirection of input or output is not supported; the subprograms must
225 accept filenames saying what files to read and write.
227 In addition, the specs can contain %-sequences to substitute variable text
228 or for conditional text. Here is a table of all defined %-sequences.
229 Note that spaces are not generated automatically around the results of
230 expanding these sequences; therefore, you can concatenate them together
231 or with constant text in a single argument.
233 %% substitute one % into the program name or argument.
234 %i substitute the name of the input file being processed.
235 %b substitute the basename of the input file being processed.
236 This is the substring up to (and not including) the last period
237 and not including the directory.
238 %g substitute the temporary-file-name-base. This is a string chosen
239 once per compilation. Different temporary file names are made by
240 concatenation of constant strings on the end, as in `%g.s'.
241 %g also has the same effect of %d.
242 %u like %g, but make the temporary file name unique.
243 %U returns the last file name generated with %u.
244 %d marks the argument containing or following the %d as a
245 temporary file name, so that that file will be deleted if CC exits
246 successfully. Unlike %g, this contributes no text to the argument.
247 %w marks the argument containing or following the %w as the
248 "output file" of this compilation. This puts the argument
249 into the sequence of arguments that %o will substitute later.
250 %W{...}
251 like %{...} but mark last argument supplied within
252 as a file to be deleted on failure.
253 %o substitutes the names of all the output files, with spaces
254 automatically placed around them. You should write spaces
255 around the %o as well or the results are undefined.
256 %o is for use in the specs for running the linker.
257 Input files whose names have no recognized suffix are not compiled
258 at all, but they are included among the output files, so they will
259 be linked.
260 %O substitutes the suffix for object files.
261 %p substitutes the standard macro predefinitions for the
262 current target machine. Use this when running cpp.
263 %P like %p, but puts `__' before and after the name of each macro.
264 (Except macros that already have __.)
265 This is for ANSI C.
266 %I Substitute a -iprefix option made from GCC_EXEC_PREFIX.
267 %s current argument is the name of a library or startup file of some sort.
268 Search for that file in a standard list of directories
269 and substitute the full name found.
270 %eSTR Print STR as an error message. STR is terminated by a newline.
271 Use this when inconsistent options are detected.
272 %x{OPTION} Accumulate an option for %X.
273 %X Output the accumulated linker options specified by compilations.
274 %Y Output the accumulated assembler options specified by compilations.
275 %Z Output the accumulated preprocessor options specified by compilations.
276 %v1 Substitute the major version number of GCC.
277 (For version 2.5.n, this is 2.)
278 %v2 Substitute the minor version number of GCC.
279 (For version 2.5.n, this is 5.)
280 %a process ASM_SPEC as a spec.
281 This allows config.h to specify part of the spec for running as.
282 %A process ASM_FINAL_SPEC as a spec. A capital A is actually
283 used here. This can be used to run a post-processor after the
284 assembler has done its job.
285 %D Dump out a -L option for each directory in startfile_prefixes.
286 If multilib_dir is set, extra entries are generated with it affixed.
287 %l process LINK_SPEC as a spec.
288 %L process LIB_SPEC as a spec.
289 %G process LIBGCC_SPEC as a spec.
290 %S process STARTFILE_SPEC as a spec. A capital S is actually used here.
291 %E process ENDFILE_SPEC as a spec. A capital E is actually used here.
292 %c process SIGNED_CHAR_SPEC as a spec.
293 %C process CPP_SPEC as a spec. A capital C is actually used here.
294 %1 process CC1_SPEC as a spec.
295 %2 process CC1PLUS_SPEC as a spec.
296 %| output "-" if the input for the current command is coming from a pipe.
297 %* substitute the variable part of a matched option. (See below.)
298 Note that each comma in the substituted string is replaced by
299 a single space.
300 %{S} substitutes the -S switch, if that switch was given to CC.
301 If that switch was not specified, this substitutes nothing.
302 Here S is a metasyntactic variable.
303 %{S*} substitutes all the switches specified to CC whose names start
304 with -S. This is used for -o, -D, -I, etc; switches that take
305 arguments. CC considers `-o foo' as being one switch whose
306 name starts with `o'. %{o*} would substitute this text,
307 including the space; thus, two arguments would be generated.
308 %{^S*} likewise, but don't put a blank between a switch and any args.
309 %{S*:X} substitutes X if one or more switches whose names start with -S are
310 specified to CC. Note that the tail part of the -S option
311 (i.e. the part matched by the `*') will be substituted for each
312 occurrence of %* within X.
313 %{S:X} substitutes X, but only if the -S switch was given to CC.
314 %{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
315 %{|S:X} like %{S:X}, but if no S switch, substitute `-'.
316 %{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
317 %{.S:X} substitutes X, but only if processing a file with suffix S.
318 %{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
319 %(Spec) processes a specification defined in a specs file as *Spec:
320 %[Spec] as above, but put __ around -D arguments
322 The conditional text X in a %{S:X} or %{!S:X} construct may contain
323 other nested % constructs or spaces, or even newlines. They are
324 processed as usual, as described above.
326 The -O, -f, -m, and -W switches are handled specifically in these
327 constructs. If another value of -O or the negated form of a -f, -m, or
328 -W switch is found later in the command line, the earlier switch
329 value is ignored, except with {S*} where S is just one letter; this
330 passes all matching options.
332 The character | is used to indicate that a command should be piped to
333 the following command, but only if -pipe is specified.
335 Note that it is built into CC which switches take arguments and which
336 do not. You might think it would be useful to generalize this to
337 allow each compiler's spec to say which switches take arguments. But
338 this cannot be done in a consistent fashion. CC cannot even decide
339 which input files have been specified without knowing which switches
340 take arguments, and it must know which input files to compile in order
341 to tell which compilers to run.
343 CC also knows implicitly that arguments starting in `-l' are to be
344 treated as compiler output files, and passed to the linker in their
345 proper position among the other output files. */
347 /* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
349 /* config.h can define ASM_SPEC to provide extra args to the assembler
350 or extra switch-translations. */
351 #ifndef ASM_SPEC
352 #define ASM_SPEC ""
353 #endif
355 /* config.h can define ASM_FINAL_SPEC to run a post processor after
356 the assembler has run. */
357 #ifndef ASM_FINAL_SPEC
358 #define ASM_FINAL_SPEC ""
359 #endif
361 /* config.h can define CPP_SPEC to provide extra args to the C preprocessor
362 or extra switch-translations. */
363 #ifndef CPP_SPEC
364 #define CPP_SPEC ""
365 #endif
367 /* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
368 or extra switch-translations. */
369 #ifndef CC1_SPEC
370 #define CC1_SPEC ""
371 #endif
373 /* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
374 or extra switch-translations. */
375 #ifndef CC1PLUS_SPEC
376 #define CC1PLUS_SPEC ""
377 #endif
379 /* config.h can define LINK_SPEC to provide extra args to the linker
380 or extra switch-translations. */
381 #ifndef LINK_SPEC
382 #define LINK_SPEC ""
383 #endif
385 /* config.h can define LIB_SPEC to override the default libraries. */
386 #ifndef LIB_SPEC
387 #define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
388 #endif
390 /* config.h can define LIBGCC_SPEC to override how and when libgcc.a is
391 included. */
392 #ifndef LIBGCC_SPEC
393 #if defined(LINK_LIBGCC_SPECIAL) || defined(LINK_LIBGCC_SPECIAL_1)
394 /* Have gcc do the search for libgcc.a. */
395 #define LIBGCC_SPEC "libgcc.a%s"
396 #else
397 #define LIBGCC_SPEC "-lgcc"
398 #endif
399 #endif
401 /* config.h can define STARTFILE_SPEC to override the default crt0 files. */
402 #ifndef STARTFILE_SPEC
403 #define STARTFILE_SPEC \
404 "%{!shared:%{pg:gcrt0%O%s}%{!pg:%{p:mcrt0%O%s}%{!p:crt0%O%s}}}"
405 #endif
407 /* config.h can define SWITCHES_NEED_SPACES to control which options
408 require spaces between the option and the argument. */
409 #ifndef SWITCHES_NEED_SPACES
410 #define SWITCHES_NEED_SPACES ""
411 #endif
413 /* config.h can define ENDFILE_SPEC to override the default crtn files. */
414 #ifndef ENDFILE_SPEC
415 #define ENDFILE_SPEC ""
416 #endif
418 /* This spec is used for telling cpp whether char is signed or not. */
419 #ifndef SIGNED_CHAR_SPEC
420 /* Use #if rather than ?:
421 because MIPS C compiler rejects like ?: in initializers. */
422 #if DEFAULT_SIGNED_CHAR
423 #define SIGNED_CHAR_SPEC "%{funsigned-char:-D__CHAR_UNSIGNED__}"
424 #else
425 #define SIGNED_CHAR_SPEC "%{!fsigned-char:-D__CHAR_UNSIGNED__}"
426 #endif
427 #endif
429 static char *cpp_spec = CPP_SPEC;
430 static char *cpp_predefines = CPP_PREDEFINES;
431 static char *cc1_spec = CC1_SPEC;
432 static char *cc1plus_spec = CC1PLUS_SPEC;
433 static char *signed_char_spec = SIGNED_CHAR_SPEC;
434 static char *asm_spec = ASM_SPEC;
435 static char *asm_final_spec = ASM_FINAL_SPEC;
436 static char *link_spec = LINK_SPEC;
437 static char *lib_spec = LIB_SPEC;
438 static char *libgcc_spec = LIBGCC_SPEC;
439 static char *endfile_spec = ENDFILE_SPEC;
440 static char *startfile_spec = STARTFILE_SPEC;
441 static char *switches_need_spaces = SWITCHES_NEED_SPACES;
443 /* Some compilers have limits on line lengths, and the multilib_select
444 and/or multilib_matches strings can be very long, so we build them at
445 run time. */
446 static struct obstack multilib_obstack;
447 static char *multilib_select;
448 static char *multilib_matches;
449 static char *multilib_defaults;
450 #include "multilib.h"
452 /* Check whether a particular argument is a default argument. */
454 #ifndef MULTILIB_DEFAULTS
455 #define MULTILIB_DEFAULTS { "" }
456 #endif
458 static char *multilib_defaults_raw[] = MULTILIB_DEFAULTS;
460 struct user_specs {
461 struct user_specs *next;
462 char *filename;
465 static struct user_specs *user_specs_head, *user_specs_tail;
467 /* This defines which switch letters take arguments. */
469 #define DEFAULT_SWITCH_TAKES_ARG(CHAR) \
470 ((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
471 || (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
472 || (CHAR) == 'I' || (CHAR) == 'm' || (CHAR) == 'x' \
473 || (CHAR) == 'L' || (CHAR) == 'A')
475 #ifndef SWITCH_TAKES_ARG
476 #define SWITCH_TAKES_ARG(CHAR) DEFAULT_SWITCH_TAKES_ARG(CHAR)
477 #endif
479 /* This defines which multi-letter switches take arguments. */
481 #define DEFAULT_WORD_SWITCH_TAKES_ARG(STR) \
482 (!strcmp (STR, "Tdata") || !strcmp (STR, "Ttext") \
483 || !strcmp (STR, "Tbss") || !strcmp (STR, "include") \
484 || !strcmp (STR, "imacros") || !strcmp (STR, "aux-info") \
485 || !strcmp (STR, "idirafter") || !strcmp (STR, "iprefix") \
486 || !strcmp (STR, "iwithprefix") || !strcmp (STR, "iwithprefixbefore") \
487 || !strcmp (STR, "isystem") || !strcmp (STR, "specs"))
489 #ifndef WORD_SWITCH_TAKES_ARG
490 #define WORD_SWITCH_TAKES_ARG(STR) DEFAULT_WORD_SWITCH_TAKES_ARG (STR)
491 #endif
493 /* Record the mapping from file suffixes for compilation specs. */
495 struct compiler
497 char *suffix; /* Use this compiler for input files
498 whose names end in this suffix. */
500 char *spec[4]; /* To use this compiler, concatenate these
501 specs and pass to do_spec. */
504 /* Pointer to a vector of `struct compiler' that gives the spec for
505 compiling a file, based on its suffix.
506 A file that does not end in any of these suffixes will be passed
507 unchanged to the loader and nothing else will be done to it.
509 An entry containing two 0s is used to terminate the vector.
511 If multiple entries match a file, the last matching one is used. */
513 static struct compiler *compilers;
515 /* Number of entries in `compilers', not counting the null terminator. */
517 static int n_compilers;
519 /* The default list of file name suffixes and their compilation specs. */
521 static struct compiler default_compilers[] =
523 /* Add lists of suffixes of known languages here. If those languages
524 were not present when we built the driver, we will hit these copies
525 and be given a more meaningful error than "file not used since
526 linking is not done". */
527 {".cc", "#C++"}, {".cxx", "#C++"}, {".cpp", "#C++"}, {".c++", "#C++"},
528 {".C", "#C++"},
529 {".ads", "#Ada"}, {".adb", "#Ada"},
530 {".f", "#Fortran"}, {".for", "#Fortran"}, {".F", "#Fortran"},
531 {".fpp", "#Fortran"},
532 {".p", "#Pascal"}, {".pas", "#Pascal"},
533 /* Next come the entries for C. */
534 {".c", "@c"},
535 {"@c",
536 "cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
537 %{C:%{!E:%eGNU C does not support -C without using -E}}\
538 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
539 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
540 %{ansi:-trigraphs -D__STRICT_ANSI__}\
541 %{!undef:%{!ansi:%p} %P} %{trigraphs} \
542 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
543 %{traditional-cpp:-traditional}\
544 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
545 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
546 "%{!M:%{!MM:%{!E:cc1 %{!pipe:%g.i} %1 \
547 %{!Q:-quiet} -dumpbase %b.c %{d*} %{m*} %{a*}\
548 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
549 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
550 %{aux-info*}\
551 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
552 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
553 %{!S:as %a %Y\
554 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
555 %{!pipe:%g.s} %A\n }}}}"},
556 {"-",
557 "%{E:cpp -lang-c%{ansi:89} %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
558 %{C:%{!E:%eGNU C does not support -C without using -E}}\
559 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
560 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
561 %{ansi:-trigraphs -D__STRICT_ANSI__}\
562 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
563 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
564 %{traditional-cpp:-traditional}\
565 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
566 %i %W{o*}}\
567 %{!E:%e-E required when input is from standard input}"},
568 {".m", "@objective-c"},
569 {"@objective-c",
570 "cpp -lang-objc %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
571 %{C:%{!E:%eGNU C does not support -C without using -E}}\
572 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
573 -undef -D__OBJC__ -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
574 %{ansi:-trigraphs -D__STRICT_ANSI__}\
575 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
576 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
577 %{traditional-cpp:-traditional}\
578 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
579 %i %{!M:%{!MM:%{!E:%{!pipe:%g.i}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
580 "%{!M:%{!MM:%{!E:cc1obj %{!pipe:%g.i} %1 \
581 %{!Q:-quiet} -dumpbase %b.m %{d*} %{m*} %{a*}\
582 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi} \
583 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*} \
584 -lang-objc %{gen-decls} \
585 %{aux-info*}\
586 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
587 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
588 %{!S:as %a %Y\
589 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
590 %{!pipe:%g.s} %A\n }}}}"},
591 {".h", "@c-header"},
592 {"@c-header",
593 "%{!E:%eCompilation of header file requested} \
594 cpp %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
595 %{C:%{!E:%eGNU C does not support -C without using -E}}\
596 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG}\
597 -undef -D__GNUC__=%v1 -D__GNUC_MINOR__=%v2\
598 %{ansi:-trigraphs -D__STRICT_ANSI__}\
599 %{!undef:%{!ansi:%p} %P} %{trigraphs}\
600 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
601 %{traditional-cpp:-traditional}\
602 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
603 %i %W{o*}"},
604 {".i", "@cpp-output"},
605 {"@cpp-output",
606 "%{!M:%{!MM:%{!E:cc1 %i %1 %{!Q:-quiet} %{d*} %{m*} %{a*}\
607 %{g*} %{O*} %{W*} %{w} %{pedantic*} %{ansi}\
608 %{traditional} %{v:-version} %{pg:-p} %{p} %{f*}\
609 %{aux-info*}\
610 %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
611 %{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
612 %{!S:as %a %Y\
613 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
614 %{!pipe:%g.s} %A\n }}}}"},
615 {".s", "@assembler"},
616 {"@assembler",
617 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
618 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
619 %i %A\n }}}}"},
620 {".S", "@assembler-with-cpp"},
621 {"@assembler-with-cpp",
622 "cpp -lang-asm %{nostdinc*} %{C} %{v} %{A*} %{I*} %{P} %I\
623 %{C:%{!E:%eGNU C does not support -C without using -E}}\
624 %{M} %{MM} %{MD:-MD %b.d} %{MMD:-MMD %b.d} %{MG} %{trigraphs}\
625 -undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
626 %c %{O*:%{!O0:-D__OPTIMIZE__}} %{traditional} %{ftraditional:-traditional}\
627 %{traditional-cpp:-traditional}\
628 %{g*} %{W*} %{w} %{pedantic*} %{H} %{d*} %C %{D*} %{U*} %{i*} %Z\
629 %i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n",
630 "%{!M:%{!MM:%{!E:%{!S:as %a %Y\
631 %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O}\
632 %{!pipe:%g.s} %A\n }}}}"},
633 #include "specs.h"
634 /* Mark end of table */
635 {0, 0}
638 /* Number of elements in default_compilers, not counting the terminator. */
640 static int n_default_compilers
641 = (sizeof default_compilers / sizeof (struct compiler)) - 1;
643 /* Here is the spec for running the linker, after compiling all files. */
645 /* -u* was put back because both BSD and SysV seem to support it. */
646 /* %{static:} simply prevents an error message if the target machine
647 doesn't handle -static. */
648 /* We want %{T*} after %{L*} and %D so that it can be used to specify linker
649 scripts which exist in user specified directories, or in standard
650 directories. */
651 #ifdef LINK_COMMAND_SPEC
652 /* Provide option to override link_command_spec from machine specific
653 configuration files. */
654 static char *link_command_spec =
655 LINK_COMMAND_SPEC;
656 #else
657 #ifdef LINK_LIBGCC_SPECIAL
658 /* Don't generate -L options. */
659 static char *link_command_spec = "\
660 %{!fsyntax-only: \
661 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
662 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
663 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
664 %{static:} %{L*} %o\
665 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
666 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
667 %{T*}\
668 \n }}}}}}";
669 #else
670 /* Use -L. */
671 static char *link_command_spec = "\
672 %{!fsyntax-only: \
673 %{!c:%{!M:%{!MM:%{!E:%{!S:ld %l %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
674 %{r} %{s} %{t} %{u*} %{x} %{z} %{Z}\
675 %{!A:%{!nostdlib:%{!nostartfiles:%S}}}\
676 %{static:} %{L*} %D %o\
677 %{!nostdlib:%{!nodefaultlibs:%G %L %G}}\
678 %{!A:%{!nostdlib:%{!nostartfiles:%E}}}\
679 %{T*}\
680 \n }}}}}}";
681 #endif
682 #endif
684 /* A vector of options to give to the linker.
685 These options are accumulated by %x,
686 and substituted into the linker command with %X. */
687 static int n_linker_options;
688 static char **linker_options;
690 /* A vector of options to give to the assembler.
691 These options are accumulated by -Wa,
692 and substituted into the assembler command with %Y. */
693 static int n_assembler_options;
694 static char **assembler_options;
696 /* A vector of options to give to the preprocessor.
697 These options are accumulated by -Wp,
698 and substituted into the preprocessor command with %Z. */
699 static int n_preprocessor_options;
700 static char **preprocessor_options;
702 /* Define how to map long options into short ones. */
704 /* This structure describes one mapping. */
705 struct option_map
707 /* The long option's name. */
708 char *name;
709 /* The equivalent short option. */
710 char *equivalent;
711 /* Argument info. A string of flag chars; NULL equals no options.
712 a => argument required.
713 o => argument optional.
714 j => join argument to equivalent, making one word.
715 * => require other text after NAME as an argument. */
716 char *arg_info;
719 /* This is the table of mappings. Mappings are tried sequentially
720 for each option encountered; the first one that matches, wins. */
722 struct option_map option_map[] =
724 {"--all-warnings", "-Wall", 0},
725 {"--ansi", "-ansi", 0},
726 {"--assemble", "-S", 0},
727 {"--assert", "-A", "a"},
728 {"--comments", "-C", 0},
729 {"--compile", "-c", 0},
730 {"--debug", "-g", "oj"},
731 {"--define-macro", "-D", "aj"},
732 {"--dependencies", "-M", 0},
733 {"--dump", "-d", "a"},
734 {"--dumpbase", "-dumpbase", "a"},
735 {"--entry", "-e", 0},
736 {"--extra-warnings", "-W", 0},
737 {"--for-assembler", "-Wa", "a"},
738 {"--for-linker", "-Xlinker", "a"},
739 {"--force-link", "-u", "a"},
740 {"--imacros", "-imacros", "a"},
741 {"--include", "-include", "a"},
742 {"--include-barrier", "-I-", 0},
743 {"--include-directory", "-I", "aj"},
744 {"--include-directory-after", "-idirafter", "a"},
745 {"--include-prefix", "-iprefix", "a"},
746 {"--include-with-prefix", "-iwithprefix", "a"},
747 {"--include-with-prefix-before", "-iwithprefixbefore", "a"},
748 {"--include-with-prefix-after", "-iwithprefix", "a"},
749 {"--language", "-x", "a"},
750 {"--library-directory", "-L", "a"},
751 {"--machine", "-m", "aj"},
752 {"--machine-", "-m", "*j"},
753 {"--no-line-commands", "-P", 0},
754 {"--no-precompiled-includes", "-noprecomp", 0},
755 {"--no-standard-includes", "-nostdinc", 0},
756 {"--no-standard-libraries", "-nostdlib", 0},
757 {"--no-warnings", "-w", 0},
758 {"--optimize", "-O", "oj"},
759 {"--output", "-o", "a"},
760 {"--pedantic", "-pedantic", 0},
761 {"--pedantic-errors", "-pedantic-errors", 0},
762 {"--pipe", "-pipe", 0},
763 {"--prefix", "-B", "a"},
764 {"--preprocess", "-E", 0},
765 {"--print-search-dirs", "-print-search-dirs", 0},
766 {"--print-file-name", "-print-file-name=", "aj"},
767 {"--print-libgcc-file-name", "-print-libgcc-file-name", 0},
768 {"--print-missing-file-dependencies", "-MG", 0},
769 {"--print-multi-lib", "-print-multi-lib", 0},
770 {"--print-multi-directory", "-print-multi-directory", 0},
771 {"--print-prog-name", "-print-prog-name=", "aj"},
772 {"--profile", "-p", 0},
773 {"--profile-blocks", "-a", 0},
774 {"--quiet", "-q", 0},
775 {"--save-temps", "-save-temps", 0},
776 {"--shared", "-shared", 0},
777 {"--silent", "-q", 0},
778 {"--specs", "-specs=", "aj"},
779 {"--static", "-static", 0},
780 {"--symbolic", "-symbolic", 0},
781 {"--target", "-b", "a"},
782 {"--trace-includes", "-H", 0},
783 {"--traditional", "-traditional", 0},
784 {"--traditional-cpp", "-traditional-cpp", 0},
785 {"--trigraphs", "-trigraphs", 0},
786 {"--undefine-macro", "-U", "aj"},
787 {"--use-version", "-V", "a"},
788 {"--user-dependencies", "-MM", 0},
789 {"--verbose", "-v", 0},
790 {"--version", "-dumpversion", 0},
791 {"--warn-", "-W", "*j"},
792 {"--write-dependencies", "-MD", 0},
793 {"--write-user-dependencies", "-MMD", 0},
794 {"--", "-f", "*j"}
797 /* Translate the options described by *ARGCP and *ARGVP.
798 Make a new vector and store it back in *ARGVP,
799 and store its length in *ARGVC. */
801 static void
802 translate_options (argcp, argvp)
803 int *argcp;
804 char ***argvp;
806 int i, j, k;
807 int argc = *argcp;
808 char **argv = *argvp;
809 char **newv = (char **) xmalloc ((argc + 2) * 2 * sizeof (char *));
810 int newindex = 0;
812 i = 0;
813 newv[newindex++] = argv[i++];
815 while (i < argc)
817 /* Translate -- options. */
818 if (argv[i][0] == '-' && argv[i][1] == '-')
820 /* Find a mapping that applies to this option. */
821 for (j = 0; j < sizeof (option_map) / sizeof (option_map[0]); j++)
823 int optlen = strlen (option_map[j].name);
824 int arglen = strlen (argv[i]);
825 int complen = arglen > optlen ? optlen : arglen;
826 char *arginfo = option_map[j].arg_info;
828 if (arginfo == 0)
829 arginfo = "";
831 if (!strncmp (argv[i], option_map[j].name, complen))
833 char *arg = 0;
835 if (arglen < optlen)
837 for (k = j + 1;
838 k < sizeof (option_map) / sizeof (option_map[0]);
839 k++)
840 if (strlen (option_map[k].name) >= arglen
841 && !strncmp (argv[i], option_map[k].name, arglen))
843 error ("Ambiguous abbreviation %s", argv[i]);
844 break;
847 if (k != sizeof (option_map) / sizeof (option_map[0]))
848 break;
851 if (arglen > optlen)
853 /* If the option has an argument, accept that. */
854 if (argv[i][optlen] == '=')
855 arg = argv[i] + optlen + 1;
857 /* If this mapping requires extra text at end of name,
858 accept that as "argument". */
859 else if (index (arginfo, '*') != 0)
860 arg = argv[i] + optlen;
862 /* Otherwise, extra text at end means mismatch.
863 Try other mappings. */
864 else
865 continue;
868 else if (index (arginfo, '*') != 0)
870 error ("Incomplete `%s' option", option_map[j].name);
871 break;
874 /* Handle arguments. */
875 if (index (arginfo, 'a') != 0)
877 if (arg == 0)
879 if (i + 1 == argc)
881 error ("Missing argument to `%s' option",
882 option_map[j].name);
883 break;
886 arg = argv[++i];
889 else if (index (arginfo, '*') != 0)
891 else if (index (arginfo, 'o') == 0)
893 if (arg != 0)
894 error ("Extraneous argument to `%s' option",
895 option_map[j].name);
896 arg = 0;
899 /* Store the translation as one argv elt or as two. */
900 if (arg != 0 && index (arginfo, 'j') != 0)
901 newv[newindex++] = concat (option_map[j].equivalent, arg,
902 NULL_PTR);
903 else if (arg != 0)
905 newv[newindex++] = option_map[j].equivalent;
906 newv[newindex++] = arg;
908 else
909 newv[newindex++] = option_map[j].equivalent;
911 break;
914 i++;
917 /* Handle old-fashioned options--just copy them through,
918 with their arguments. */
919 else if (argv[i][0] == '-')
921 char *p = argv[i] + 1;
922 int c = *p;
923 int nskip = 1;
925 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
926 nskip += SWITCH_TAKES_ARG (c) - (p[1] != 0);
927 else if (WORD_SWITCH_TAKES_ARG (p))
928 nskip += WORD_SWITCH_TAKES_ARG (p);
929 else if ((c == 'B' || c == 'b' || c == 'V' || c == 'x')
930 && p[1] == 0)
931 nskip += 1;
932 else if (! strcmp (p, "Xlinker"))
933 nskip += 1;
935 /* Watch out for an option at the end of the command line that
936 is missing arguments, and avoid skipping past the end of the
937 command line. */
938 if (nskip + i > argc)
939 nskip = argc - i;
941 while (nskip > 0)
943 newv[newindex++] = argv[i++];
944 nskip--;
947 else
948 /* Ordinary operands, or +e options. */
949 newv[newindex++] = argv[i++];
952 newv[newindex] = 0;
954 *argvp = newv;
955 *argcp = newindex;
958 char *
959 my_strerror(e)
960 int e;
962 #ifdef HAVE_STRERROR
964 return strerror(e);
966 #else
968 if (!e)
969 return "errno = 0";
971 if (e > 0 && e < sys_nerr)
972 return sys_errlist[e];
974 return "errno = ?";
975 #endif
978 static char *
979 skip_whitespace (p)
980 char *p;
982 while (1)
984 /* A fully-blank line is a delimiter in the SPEC file and shouldn't
985 be considered whitespace. */
986 if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
987 return p + 1;
988 else if (*p == '\n' || *p == ' ' || *p == '\t')
989 p++;
990 else if (*p == '#')
992 while (*p != '\n') p++;
993 p++;
995 else
996 break;
999 return p;
1002 /* Structure to keep track of the specs that have been defined so far.
1003 These are accessed using %(specname) or %[specname] in a compiler
1004 or link spec. */
1006 struct spec_list
1008 /* The following 2 fields must be first */
1009 /* to allow EXTRA_SPECS to be initialized */
1010 char *name; /* name of the spec. */
1011 char *ptr; /* available ptr if no static pointer */
1013 /* The following fields are not initialized */
1014 /* by EXTRA_SPECS */
1015 char **ptr_spec; /* pointer to the spec itself. */
1016 struct spec_list *next; /* Next spec in linked list. */
1017 int name_len; /* length of the name */
1018 int alloc_p; /* whether string was allocated */
1021 #define INIT_STATIC_SPEC(NAME,PTR) \
1022 { NAME, NULL_PTR, PTR, (struct spec_list *)0, sizeof (NAME)-1, 0 }
1024 /* List of statically defined specs */
1025 static struct spec_list static_specs[] = {
1026 INIT_STATIC_SPEC ("asm", &asm_spec),
1027 INIT_STATIC_SPEC ("asm_final", &asm_final_spec),
1028 INIT_STATIC_SPEC ("cpp", &cpp_spec),
1029 INIT_STATIC_SPEC ("cc1", &cc1_spec),
1030 INIT_STATIC_SPEC ("cc1plus", &cc1plus_spec),
1031 INIT_STATIC_SPEC ("endfile", &endfile_spec),
1032 INIT_STATIC_SPEC ("link", &link_spec),
1033 INIT_STATIC_SPEC ("lib", &lib_spec),
1034 INIT_STATIC_SPEC ("libgcc", &libgcc_spec),
1035 INIT_STATIC_SPEC ("startfile", &startfile_spec),
1036 INIT_STATIC_SPEC ("switches_need_spaces", &switches_need_spaces),
1037 INIT_STATIC_SPEC ("signed_char", &signed_char_spec),
1038 INIT_STATIC_SPEC ("predefines", &cpp_predefines),
1039 INIT_STATIC_SPEC ("cross_compile", &cross_compile),
1040 INIT_STATIC_SPEC ("version", &compiler_version),
1041 INIT_STATIC_SPEC ("multilib", &multilib_select),
1042 INIT_STATIC_SPEC ("multilib_defaults", &multilib_defaults),
1043 INIT_STATIC_SPEC ("multilib_extra", &multilib_extra),
1044 INIT_STATIC_SPEC ("multilib_matches", &multilib_matches),
1047 #ifdef EXTRA_SPECS /* additional specs needed */
1048 static struct spec_list extra_specs[] = { EXTRA_SPECS };
1049 #endif
1051 /* List of dynamically allocates specs that have been defined so far. */
1053 static struct spec_list *specs = (struct spec_list *)0;
1056 /* Initialize the specs lookup routines. */
1058 static void
1059 init_spec ()
1061 struct spec_list *next = (struct spec_list *)0;
1062 struct spec_list *sl = (struct spec_list *)0;
1063 int i;
1065 if (specs)
1066 return; /* already initialized */
1068 if (verbose_flag)
1069 notice ("Using builtin specs.\n");
1071 #ifdef EXTRA_SPECS
1072 for (i = (sizeof (extra_specs) / sizeof (extra_specs[0])) - 1; i >= 0; i--)
1074 sl = &extra_specs[i];
1075 sl->next = next;
1076 sl->name_len = strlen (sl->name);
1077 sl->ptr_spec = &sl->ptr;
1078 next = sl;
1080 #endif
1082 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1; i >= 0; i--)
1084 sl = &static_specs[i];
1085 sl->next = next;
1086 next = sl;
1089 specs = sl;
1093 /* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
1094 removed; If the spec starts with a + then SPEC is added to the end of the
1095 current spec. */
1097 static void
1098 set_spec (name, spec)
1099 char *name;
1100 char *spec;
1102 struct spec_list *sl;
1103 char *old_spec;
1104 int name_len = strlen (name);
1105 int i;
1107 /* If this is the first call, initialize the statically allocated specs */
1108 if (!specs)
1110 struct spec_list *next = (struct spec_list *)0;
1111 for (i = (sizeof (static_specs) / sizeof (static_specs[0])) - 1;
1112 i >= 0; i--)
1114 sl = &static_specs[i];
1115 sl->next = next;
1116 next = sl;
1118 specs = sl;
1121 /* See if the spec already exists */
1122 for (sl = specs; sl; sl = sl->next)
1123 if (name_len == sl->name_len && !strcmp (sl->name, name))
1124 break;
1126 if (!sl)
1128 /* Not found - make it */
1129 sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
1130 sl->name = save_string (name, strlen (name));
1131 sl->name_len = name_len;
1132 sl->ptr_spec = &sl->ptr;
1133 sl->alloc_p = 0;
1134 *(sl->ptr_spec) = "";
1135 sl->next = specs;
1136 specs = sl;
1139 old_spec = *(sl->ptr_spec);
1140 *(sl->ptr_spec) = ((spec[0] == '+' && isspace (spec[1]))
1141 ? concat (old_spec, spec + 1, NULL_PTR)
1142 : save_string (spec, strlen (spec)));
1144 #ifdef DEBUG_SPECS
1145 if (verbose_flag)
1146 notice ("Setting spec %s to '%s'\n\n", name, *(sl->ptr_spec));
1147 #endif
1149 /* Free the old spec */
1150 if (old_spec && sl->alloc_p)
1151 free (old_spec);
1153 sl->alloc_p = 1;
1156 /* Accumulate a command (program name and args), and run it. */
1158 /* Vector of pointers to arguments in the current line of specifications. */
1160 static char **argbuf;
1162 /* Number of elements allocated in argbuf. */
1164 static int argbuf_length;
1166 /* Number of elements in argbuf currently in use (containing args). */
1168 static int argbuf_index;
1170 /* This is the list of suffixes and codes (%g/%u/%U) and the associated
1171 temp file. Used only if MKTEMP_EACH_FILE. */
1173 static struct temp_name {
1174 char *suffix; /* suffix associated with the code. */
1175 int length; /* strlen (suffix). */
1176 int unique; /* Indicates whether %g or %u/%U was used. */
1177 char *filename; /* associated filename. */
1178 int filename_length; /* strlen (filename). */
1179 struct temp_name *next;
1180 } *temp_names;
1182 /* Number of commands executed so far. */
1184 static int execution_count;
1186 /* Number of commands that exited with a signal. */
1188 static int signal_count;
1190 /* Name with which this program was invoked. */
1192 static char *programname;
1194 /* Structures to keep track of prefixes to try when looking for files. */
1196 struct prefix_list
1198 char *prefix; /* String to prepend to the path. */
1199 struct prefix_list *next; /* Next in linked list. */
1200 int require_machine_suffix; /* Don't use without machine_suffix. */
1201 /* 2 means try both machine_suffix and just_machine_suffix. */
1202 int *used_flag_ptr; /* 1 if a file was found with this prefix. */
1205 struct path_prefix
1207 struct prefix_list *plist; /* List of prefixes to try */
1208 int max_len; /* Max length of a prefix in PLIST */
1209 char *name; /* Name of this list (used in config stuff) */
1212 /* List of prefixes to try when looking for executables. */
1214 static struct path_prefix exec_prefixes = { 0, 0, "exec" };
1216 /* List of prefixes to try when looking for startup (crt0) files. */
1218 static struct path_prefix startfile_prefixes = { 0, 0, "startfile" };
1220 /* List of prefixes to try when looking for include files. */
1222 static struct path_prefix include_prefixes = { 0, 0, "include" };
1224 /* Suffix to attach to directories searched for commands.
1225 This looks like `MACHINE/VERSION/'. */
1227 static char *machine_suffix = 0;
1229 /* Suffix to attach to directories searched for commands.
1230 This is just `MACHINE/'. */
1232 static char *just_machine_suffix = 0;
1234 /* Adjusted value of GCC_EXEC_PREFIX envvar. */
1236 static char *gcc_exec_prefix;
1238 /* Default prefixes to attach to command names. */
1240 #ifdef CROSS_COMPILE /* Don't use these prefixes for a cross compiler. */
1241 #undef MD_EXEC_PREFIX
1242 #undef MD_STARTFILE_PREFIX
1243 #undef MD_STARTFILE_PREFIX_1
1244 #endif
1246 #ifndef STANDARD_EXEC_PREFIX
1247 #define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-lib/"
1248 #endif /* !defined STANDARD_EXEC_PREFIX */
1250 static char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
1251 static char *standard_exec_prefix_1 = "/usr/lib/gcc/";
1252 #ifdef MD_EXEC_PREFIX
1253 static char *md_exec_prefix = MD_EXEC_PREFIX;
1254 #endif
1256 #ifndef STANDARD_STARTFILE_PREFIX
1257 #define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
1258 #endif /* !defined STANDARD_STARTFILE_PREFIX */
1260 #ifdef MD_STARTFILE_PREFIX
1261 static char *md_startfile_prefix = MD_STARTFILE_PREFIX;
1262 #endif
1263 #ifdef MD_STARTFILE_PREFIX_1
1264 static char *md_startfile_prefix_1 = MD_STARTFILE_PREFIX_1;
1265 #endif
1266 static char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
1267 static char *standard_startfile_prefix_1 = "/lib/";
1268 static char *standard_startfile_prefix_2 = "/usr/lib/";
1270 #ifndef TOOLDIR_BASE_PREFIX
1271 #define TOOLDIR_BASE_PREFIX "/usr/local/"
1272 #endif
1273 static char *tooldir_base_prefix = TOOLDIR_BASE_PREFIX;
1274 static char *tooldir_prefix;
1276 /* Subdirectory to use for locating libraries. Set by
1277 set_multilib_dir based on the compilation options. */
1279 static char *multilib_dir;
1281 /* Clear out the vector of arguments (after a command is executed). */
1283 static void
1284 clear_args ()
1286 argbuf_index = 0;
1289 /* Add one argument to the vector at the end.
1290 This is done when a space is seen or at the end of the line.
1291 If DELETE_ALWAYS is nonzero, the arg is a filename
1292 and the file should be deleted eventually.
1293 If DELETE_FAILURE is nonzero, the arg is a filename
1294 and the file should be deleted if this compilation fails. */
1296 static void
1297 store_arg (arg, delete_always, delete_failure)
1298 char *arg;
1299 int delete_always, delete_failure;
1301 if (argbuf_index + 1 == argbuf_length)
1302 argbuf
1303 = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
1305 argbuf[argbuf_index++] = arg;
1306 argbuf[argbuf_index] = 0;
1308 if (delete_always || delete_failure)
1309 record_temp_file (arg, delete_always, delete_failure);
1312 /* Read compilation specs from a file named FILENAME,
1313 replacing the default ones.
1315 A suffix which starts with `*' is a definition for
1316 one of the machine-specific sub-specs. The "suffix" should be
1317 *asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
1318 The corresponding spec is stored in asm_spec, etc.,
1319 rather than in the `compilers' vector.
1321 Anything invalid in the file is a fatal error. */
1323 static void
1324 read_specs (filename, main_p)
1325 char *filename;
1326 int main_p;
1328 int desc;
1329 int readlen;
1330 struct stat statbuf;
1331 char *buffer;
1332 register char *p;
1334 if (verbose_flag)
1335 notice ("Reading specs from %s\n", filename);
1337 /* Open and stat the file. */
1338 desc = open (filename, O_RDONLY, 0);
1339 if (desc < 0)
1340 pfatal_with_name (filename);
1341 if (stat (filename, &statbuf) < 0)
1342 pfatal_with_name (filename);
1344 /* Read contents of file into BUFFER. */
1345 buffer = xmalloc ((unsigned) statbuf.st_size + 1);
1346 readlen = read (desc, buffer, (unsigned) statbuf.st_size);
1347 if (readlen < 0)
1348 pfatal_with_name (filename);
1349 buffer[readlen] = 0;
1350 close (desc);
1352 /* Scan BUFFER for specs, putting them in the vector. */
1353 p = buffer;
1354 while (1)
1356 char *suffix;
1357 char *spec;
1358 char *in, *out, *p1, *p2, *p3;
1360 /* Advance P in BUFFER to the next nonblank nocomment line. */
1361 p = skip_whitespace (p);
1362 if (*p == 0)
1363 break;
1365 /* Is this a special command that starts with '%'? */
1366 /* Don't allow this for the main specs file, since it would
1367 encourage people to overwrite it. */
1368 if (*p == '%' && !main_p)
1370 p1 = p;
1371 while (*p && *p != '\n')
1372 p++;
1374 p++; /* Skip '\n' */
1376 if (!strncmp (p1, "%include", sizeof ("%include")-1)
1377 && (p1[sizeof "%include" - 1] == ' '
1378 || p1[sizeof "%include" - 1] == '\t'))
1380 char *new_filename;
1382 p1 += sizeof ("%include");
1383 while (*p1 == ' ' || *p1 == '\t')
1384 p1++;
1386 if (*p1++ != '<' || p[-2] != '>')
1387 fatal ("specs %%include syntax malformed after %d characters",
1388 p1 - buffer + 1);
1390 p[-2] = '\0';
1391 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1392 read_specs (new_filename ? new_filename : p1, FALSE);
1393 continue;
1395 else if (!strncmp (p1, "%include_noerr", sizeof "%include_noerr" - 1)
1396 && (p1[sizeof "%include_noerr" - 1] == ' '
1397 || p1[sizeof "%include_noerr" - 1] == '\t'))
1399 char *new_filename;
1401 p1 += sizeof "%include_noerr";
1402 while (*p1 == ' ' || *p1 == '\t') p1++;
1404 if (*p1++ != '<' || p[-2] != '>')
1405 fatal ("specs %%include syntax malformed after %d characters",
1406 p1 - buffer + 1);
1408 p[-2] = '\0';
1409 new_filename = find_a_file (&startfile_prefixes, p1, R_OK);
1410 if (new_filename)
1411 read_specs (new_filename, FALSE);
1412 else if (verbose_flag)
1413 notice ("Could not find specs file %s\n", p1);
1414 continue;
1416 else if (!strncmp (p1, "%rename", sizeof "%rename" - 1)
1417 && (p1[sizeof "%rename" - 1] == ' '
1418 || p1[sizeof "%rename" - 1] == '\t'))
1420 int name_len;
1421 struct spec_list *sl;
1423 /* Get original name */
1424 p1 += sizeof "%rename";
1425 while (*p1 == ' ' || *p1 == '\t')
1426 p1++;
1428 if (! isalpha (*p1))
1429 fatal ("specs %%rename syntax malformed after %d characters",
1430 p1 - buffer);
1432 p2 = p1;
1433 while (*p2 && !isspace (*p2))
1434 p2++;
1436 if (*p2 != ' ' && *p2 != '\t')
1437 fatal ("specs %%rename syntax malformed after %d characters",
1438 p2 - buffer);
1440 name_len = p2 - p1;
1441 *p2++ = '\0';
1442 while (*p2 == ' ' || *p2 == '\t')
1443 p2++;
1445 if (! isalpha (*p2))
1446 fatal ("specs %%rename syntax malformed after %d characters",
1447 p2 - buffer);
1449 /* Get new spec name */
1450 p3 = p2;
1451 while (*p3 && !isspace (*p3))
1452 p3++;
1454 if (p3 != p-1)
1455 fatal ("specs %%rename syntax malformed after %d characters",
1456 p3 - buffer);
1457 *p3 = '\0';
1459 for (sl = specs; sl; sl = sl->next)
1460 if (name_len == sl->name_len && !strcmp (sl->name, p1))
1461 break;
1463 if (!sl)
1464 fatal ("specs %s spec was not found to be renamed", p1);
1466 if (strcmp (p1, p2) == 0)
1467 continue;
1469 if (verbose_flag)
1471 notice ("rename spec %s to %s\n", p1, p2);
1472 #ifdef DEBUG_SPECS
1473 notice ("spec is '%s'\n\n", *(sl->ptr_spec));
1474 #endif
1477 set_spec (p2, *(sl->ptr_spec));
1478 if (sl->alloc_p)
1479 free (*(sl->ptr_spec));
1481 *(sl->ptr_spec) = "";
1482 sl->alloc_p = 0;
1483 continue;
1485 else
1486 fatal ("specs unknown %% command after %d characters",
1487 p1 - buffer);
1490 /* Find the colon that should end the suffix. */
1491 p1 = p;
1492 while (*p1 && *p1 != ':' && *p1 != '\n')
1493 p1++;
1495 /* The colon shouldn't be missing. */
1496 if (*p1 != ':')
1497 fatal ("specs file malformed after %d characters", p1 - buffer);
1499 /* Skip back over trailing whitespace. */
1500 p2 = p1;
1501 while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t'))
1502 p2--;
1504 /* Copy the suffix to a string. */
1505 suffix = save_string (p, p2 - p);
1506 /* Find the next line. */
1507 p = skip_whitespace (p1 + 1);
1508 if (p[1] == 0)
1509 fatal ("specs file malformed after %d characters", p - buffer);
1511 p1 = p;
1512 /* Find next blank line. */
1513 while (*p1 && !(*p1 == '\n' && p1[1] == '\n'))
1514 p1++;
1516 /* Specs end at the blank line and do not include the newline. */
1517 spec = save_string (p, p1 - p);
1518 p = p1;
1520 /* Delete backslash-newline sequences from the spec. */
1521 in = spec;
1522 out = spec;
1523 while (*in != 0)
1525 if (in[0] == '\\' && in[1] == '\n')
1526 in += 2;
1527 else if (in[0] == '#')
1528 while (*in && *in != '\n')
1529 in++;
1531 else
1532 *out++ = *in++;
1534 *out = 0;
1536 if (suffix[0] == '*')
1538 if (! strcmp (suffix, "*link_command"))
1539 link_command_spec = spec;
1540 else
1541 set_spec (suffix + 1, spec);
1543 else
1545 /* Add this pair to the vector. */
1546 compilers
1547 = ((struct compiler *)
1548 xrealloc (compilers,
1549 (n_compilers + 2) * sizeof (struct compiler)));
1551 compilers[n_compilers].suffix = suffix;
1552 bzero ((char *) compilers[n_compilers].spec,
1553 sizeof compilers[n_compilers].spec);
1554 compilers[n_compilers].spec[0] = spec;
1555 n_compilers++;
1556 bzero ((char *) &compilers[n_compilers],
1557 sizeof compilers[n_compilers]);
1560 if (*suffix == 0)
1561 link_command_spec = spec;
1564 if (link_command_spec == 0)
1565 fatal ("spec file has no spec for linking");
1568 /* Record the names of temporary files we tell compilers to write,
1569 and delete them at the end of the run. */
1571 /* This is the common prefix we use to make temp file names.
1572 It is chosen once for each run of this program.
1573 It is substituted into a spec by %g.
1574 Thus, all temp file names contain this prefix.
1575 In practice, all temp file names start with this prefix.
1577 This prefix comes from the envvar TMPDIR if it is defined;
1578 otherwise, from the P_tmpdir macro if that is defined;
1579 otherwise, in /usr/tmp or /tmp;
1580 or finally the current directory if all else fails. */
1582 static char *temp_filename;
1584 /* Length of the prefix. */
1586 static int temp_filename_length;
1588 /* Define the list of temporary files to delete. */
1590 struct temp_file
1592 char *name;
1593 struct temp_file *next;
1596 /* Queue of files to delete on success or failure of compilation. */
1597 static struct temp_file *always_delete_queue;
1598 /* Queue of files to delete on failure of compilation. */
1599 static struct temp_file *failure_delete_queue;
1601 /* Record FILENAME as a file to be deleted automatically.
1602 ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
1603 otherwise delete it in any case.
1604 FAIL_DELETE nonzero means delete it if a compilation step fails;
1605 otherwise delete it in any case. */
1607 static void
1608 record_temp_file (filename, always_delete, fail_delete)
1609 char *filename;
1610 int always_delete;
1611 int fail_delete;
1613 register char *name;
1614 name = xmalloc (strlen (filename) + 1);
1615 strcpy (name, filename);
1617 if (always_delete)
1619 register struct temp_file *temp;
1620 for (temp = always_delete_queue; temp; temp = temp->next)
1621 if (! strcmp (name, temp->name))
1622 goto already1;
1624 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1625 temp->next = always_delete_queue;
1626 temp->name = name;
1627 always_delete_queue = temp;
1629 already1:;
1632 if (fail_delete)
1634 register struct temp_file *temp;
1635 for (temp = failure_delete_queue; temp; temp = temp->next)
1636 if (! strcmp (name, temp->name))
1637 goto already2;
1639 temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
1640 temp->next = failure_delete_queue;
1641 temp->name = name;
1642 failure_delete_queue = temp;
1644 already2:;
1648 /* Delete all the temporary files whose names we previously recorded. */
1650 static void
1651 delete_if_ordinary (name)
1652 char *name;
1654 struct stat st;
1655 #ifdef DEBUG
1656 int i, c;
1658 printf ("Delete %s? (y or n) ", name);
1659 fflush (stdout);
1660 i = getchar ();
1661 if (i != '\n')
1662 while ((c = getchar ()) != '\n' && c != EOF)
1665 if (i == 'y' || i == 'Y')
1666 #endif /* DEBUG */
1667 if (stat (name, &st) >= 0 && S_ISREG (st.st_mode))
1668 if (unlink (name) < 0)
1669 if (verbose_flag)
1670 perror_with_name (name);
1673 static void
1674 delete_temp_files ()
1676 register struct temp_file *temp;
1678 for (temp = always_delete_queue; temp; temp = temp->next)
1679 delete_if_ordinary (temp->name);
1680 always_delete_queue = 0;
1683 /* Delete all the files to be deleted on error. */
1685 static void
1686 delete_failure_queue ()
1688 register struct temp_file *temp;
1690 for (temp = failure_delete_queue; temp; temp = temp->next)
1691 delete_if_ordinary (temp->name);
1694 static void
1695 clear_failure_queue ()
1697 failure_delete_queue = 0;
1700 /* Routine to add variables to the environment. We do this to pass
1701 the pathname of the gcc driver, and the directories search to the
1702 collect2 program, which is being run as ld. This way, we can be
1703 sure of executing the right compiler when collect2 wants to build
1704 constructors and destructors. Since the environment variables we
1705 use come from an obstack, we don't have to worry about allocating
1706 space for them. */
1708 #ifndef HAVE_PUTENV
1710 void
1711 putenv (str)
1712 char *str;
1714 #ifndef VMS /* nor about VMS */
1716 extern char **environ;
1717 char **old_environ = environ;
1718 char **envp;
1719 int num_envs = 0;
1720 int name_len = 1;
1721 int str_len = strlen (str);
1722 char *p = str;
1723 int ch;
1725 while ((ch = *p++) != '\0' && ch != '=')
1726 name_len++;
1728 if (!ch)
1729 abort ();
1731 /* Search for replacing an existing environment variable, and
1732 count the number of total environment variables. */
1733 for (envp = old_environ; *envp; envp++)
1735 num_envs++;
1736 if (!strncmp (str, *envp, name_len))
1738 *envp = str;
1739 return;
1743 /* Add a new environment variable */
1744 environ = (char **) xmalloc (sizeof (char *) * (num_envs+2));
1745 *environ = str;
1746 bcopy ((char *) old_environ, (char *) (environ + 1),
1747 sizeof (char *) * (num_envs+1));
1749 #endif /* VMS */
1752 #endif /* HAVE_PUTENV */
1755 /* Build a list of search directories from PATHS.
1756 PREFIX is a string to prepend to the list.
1757 If CHECK_DIR_P is non-zero we ensure the directory exists.
1758 This is used mostly by putenv_from_prefixes so we use `collect_obstack'.
1759 It is also used by the --print-search-dirs flag. */
1761 static char *
1762 build_search_list (paths, prefix, check_dir_p)
1763 struct path_prefix *paths;
1764 char *prefix;
1765 int check_dir_p;
1767 int suffix_len = (machine_suffix) ? strlen (machine_suffix) : 0;
1768 int just_suffix_len
1769 = (just_machine_suffix) ? strlen (just_machine_suffix) : 0;
1770 int first_time = TRUE;
1771 struct prefix_list *pprefix;
1773 obstack_grow (&collect_obstack, prefix, strlen (prefix));
1775 for (pprefix = paths->plist; pprefix != 0; pprefix = pprefix->next)
1777 int len = strlen (pprefix->prefix);
1779 if (machine_suffix
1780 && (! check_dir_p
1781 || is_directory (pprefix->prefix, machine_suffix, 0)))
1783 if (!first_time)
1784 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1786 first_time = FALSE;
1787 obstack_grow (&collect_obstack, pprefix->prefix, len);
1788 obstack_grow (&collect_obstack, machine_suffix, suffix_len);
1791 if (just_machine_suffix
1792 && pprefix->require_machine_suffix == 2
1793 && (! check_dir_p
1794 || is_directory (pprefix->prefix, just_machine_suffix, 0)))
1796 if (! first_time)
1797 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1799 first_time = FALSE;
1800 obstack_grow (&collect_obstack, pprefix->prefix, len);
1801 obstack_grow (&collect_obstack, just_machine_suffix,
1802 just_suffix_len);
1805 if (! pprefix->require_machine_suffix)
1807 if (! first_time)
1808 obstack_1grow (&collect_obstack, PATH_SEPARATOR);
1810 first_time = FALSE;
1811 obstack_grow (&collect_obstack, pprefix->prefix, len);
1815 obstack_1grow (&collect_obstack, '\0');
1816 return obstack_finish (&collect_obstack);
1819 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
1820 for collect. */
1822 static void
1823 putenv_from_prefixes (paths, env_var)
1824 struct path_prefix *paths;
1825 char *env_var;
1827 putenv (build_search_list (paths, env_var, 1));
1830 /* Search for NAME using the prefix list PREFIXES. MODE is passed to
1831 access to check permissions.
1832 Return 0 if not found, otherwise return its name, allocated with malloc. */
1834 static char *
1835 find_a_file (pprefix, name, mode)
1836 struct path_prefix *pprefix;
1837 char *name;
1838 int mode;
1840 char *temp;
1841 char *file_suffix = ((mode & X_OK) != 0 ? EXECUTABLE_SUFFIX : "");
1842 struct prefix_list *pl;
1843 int len = pprefix->max_len + strlen (name) + strlen (file_suffix) + 1;
1845 if (machine_suffix)
1846 len += strlen (machine_suffix);
1848 temp = xmalloc (len);
1850 /* Determine the filename to execute (special case for absolute paths). */
1852 if (*name == '/' || *name == DIR_SEPARATOR
1853 /* Check for disk name on MS-DOS-based systems. */
1854 || (DIR_SEPARATOR == '\\' && name[1] == ':'
1855 && (name[2] == DIR_SEPARATOR || name[2] == '/')))
1857 if (access (name, mode))
1859 strcpy (temp, name);
1860 return temp;
1863 else
1864 for (pl = pprefix->plist; pl; pl = pl->next)
1866 if (machine_suffix)
1868 /* Some systems have a suffix for executable files.
1869 So try appending that first. */
1870 if (file_suffix[0] != 0)
1872 strcpy (temp, pl->prefix);
1873 strcat (temp, machine_suffix);
1874 strcat (temp, name);
1875 strcat (temp, file_suffix);
1876 if (access (temp, mode) == 0)
1878 if (pl->used_flag_ptr != 0)
1879 *pl->used_flag_ptr = 1;
1880 return temp;
1884 /* Now try just the name. */
1885 strcpy (temp, pl->prefix);
1886 strcat (temp, machine_suffix);
1887 strcat (temp, name);
1888 if (access (temp, mode) == 0)
1890 if (pl->used_flag_ptr != 0)
1891 *pl->used_flag_ptr = 1;
1892 return temp;
1896 /* Certain prefixes are tried with just the machine type,
1897 not the version. This is used for finding as, ld, etc. */
1898 if (just_machine_suffix && pl->require_machine_suffix == 2)
1900 /* Some systems have a suffix for executable files.
1901 So try appending that first. */
1902 if (file_suffix[0] != 0)
1904 strcpy (temp, pl->prefix);
1905 strcat (temp, just_machine_suffix);
1906 strcat (temp, name);
1907 strcat (temp, file_suffix);
1908 if (access (temp, mode) == 0)
1910 if (pl->used_flag_ptr != 0)
1911 *pl->used_flag_ptr = 1;
1912 return temp;
1916 strcpy (temp, pl->prefix);
1917 strcat (temp, just_machine_suffix);
1918 strcat (temp, name);
1919 if (access (temp, mode) == 0)
1921 if (pl->used_flag_ptr != 0)
1922 *pl->used_flag_ptr = 1;
1923 return temp;
1927 /* Certain prefixes can't be used without the machine suffix
1928 when the machine or version is explicitly specified. */
1929 if (! pl->require_machine_suffix)
1931 /* Some systems have a suffix for executable files.
1932 So try appending that first. */
1933 if (file_suffix[0] != 0)
1935 strcpy (temp, pl->prefix);
1936 strcat (temp, name);
1937 strcat (temp, file_suffix);
1938 if (access (temp, mode) == 0)
1940 if (pl->used_flag_ptr != 0)
1941 *pl->used_flag_ptr = 1;
1942 return temp;
1946 strcpy (temp, pl->prefix);
1947 strcat (temp, name);
1948 if (access (temp, mode) == 0)
1950 if (pl->used_flag_ptr != 0)
1951 *pl->used_flag_ptr = 1;
1952 return temp;
1957 free (temp);
1958 return 0;
1961 /* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
1962 at the start of the list, otherwise it goes at the end.
1964 If WARN is nonzero, we will warn if no file is found
1965 through this prefix. WARN should point to an int
1966 which will be set to 1 if this entry is used.
1968 COMPONENT is the value to be passed to update_path.
1970 REQUIRE_MACHINE_SUFFIX is 1 if this prefix can't be used without
1971 the complete value of machine_suffix.
1972 2 means try both machine_suffix and just_machine_suffix. */
1974 static void
1975 add_prefix (pprefix, prefix, component, first, require_machine_suffix, warn)
1976 struct path_prefix *pprefix;
1977 char *prefix;
1978 char *component;
1979 int first;
1980 int require_machine_suffix;
1981 int *warn;
1983 struct prefix_list *pl, **prev;
1984 int len;
1986 if (! first && pprefix->plist)
1988 for (pl = pprefix->plist; pl->next; pl = pl->next)
1990 prev = &pl->next;
1992 else
1993 prev = &pprefix->plist;
1995 /* Keep track of the longest prefix */
1997 prefix = update_path (prefix, component);
1998 len = strlen (prefix);
1999 if (len > pprefix->max_len)
2000 pprefix->max_len = len;
2002 pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
2003 pl->prefix = save_string (prefix, len);
2004 pl->require_machine_suffix = require_machine_suffix;
2005 pl->used_flag_ptr = warn;
2006 if (warn)
2007 *warn = 0;
2009 if (*prev)
2010 pl->next = *prev;
2011 else
2012 pl->next = (struct prefix_list *) 0;
2013 *prev = pl;
2016 /* Print warnings for any prefixes in the list PPREFIX that were not used. */
2018 static void
2019 unused_prefix_warnings (pprefix)
2020 struct path_prefix *pprefix;
2022 struct prefix_list *pl = pprefix->plist;
2024 while (pl)
2026 if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
2028 if (pl->require_machine_suffix && machine_suffix)
2029 error ("file path prefix `%s%s' never used", pl->prefix,
2030 machine_suffix);
2031 else
2032 error ("file path prefix `%s' never used", pl->prefix);
2034 /* Prevent duplicate warnings. */
2035 *pl->used_flag_ptr = 1;
2038 pl = pl->next;
2042 /* Get rid of all prefixes built up so far in *PLISTP. */
2044 static void
2045 free_path_prefix (pprefix)
2046 struct path_prefix *pprefix;
2048 struct prefix_list *pl = pprefix->plist;
2049 struct prefix_list *temp;
2051 while (pl)
2053 temp = pl;
2054 pl = pl->next;
2055 free (temp->prefix);
2056 free ((char *) temp);
2059 pprefix->plist = (struct prefix_list *) 0;
2062 /* Execute the command specified by the arguments on the current line of spec.
2063 When using pipes, this includes several piped-together commands
2064 with `|' between them.
2066 Return 0 if successful, -1 if failed. */
2068 static int
2069 execute ()
2071 int i;
2072 int n_commands; /* # of command. */
2073 char *string;
2074 struct command
2076 char *prog; /* program name. */
2077 char **argv; /* vector of args. */
2078 int pid; /* pid of process for this command. */
2081 struct command *commands; /* each command buffer with above info. */
2083 /* Count # of piped commands. */
2084 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2085 if (strcmp (argbuf[i], "|") == 0)
2086 n_commands++;
2088 /* Get storage for each command. */
2089 commands
2090 = (struct command *) alloca (n_commands * sizeof (struct command));
2092 /* Split argbuf into its separate piped processes,
2093 and record info about each one.
2094 Also search for the programs that are to be run. */
2096 commands[0].prog = argbuf[0]; /* first command. */
2097 commands[0].argv = &argbuf[0];
2098 string = find_a_file (&exec_prefixes, commands[0].prog, X_OK);
2099 if (string)
2100 commands[0].argv[0] = string;
2102 for (n_commands = 1, i = 0; i < argbuf_index; i++)
2103 if (strcmp (argbuf[i], "|") == 0)
2104 { /* each command. */
2105 #if defined (__MSDOS__) || (defined (_WIN32) && ! defined (__CYGWIN32__)) || defined (OS2) || defined (VMS)
2106 fatal ("-pipe not supported");
2107 #endif
2108 argbuf[i] = 0; /* termination of command args. */
2109 commands[n_commands].prog = argbuf[i + 1];
2110 commands[n_commands].argv = &argbuf[i + 1];
2111 string = find_a_file (&exec_prefixes, commands[n_commands].prog, X_OK);
2112 if (string)
2113 commands[n_commands].argv[0] = string;
2114 n_commands++;
2117 argbuf[argbuf_index] = 0;
2119 /* If -v, print what we are about to do, and maybe query. */
2121 if (verbose_flag)
2123 /* Print each piped command as a separate line. */
2124 for (i = 0; i < n_commands ; i++)
2126 char **j;
2128 for (j = commands[i].argv; *j; j++)
2129 fprintf (stderr, " %s", *j);
2131 /* Print a pipe symbol after all but the last command. */
2132 if (i + 1 != n_commands)
2133 fprintf (stderr, " |");
2134 fprintf (stderr, "\n");
2136 fflush (stderr);
2137 #ifdef DEBUG
2138 notice ("\nGo ahead? (y or n) ");
2139 fflush (stderr);
2140 i = getchar ();
2141 if (i != '\n')
2142 while (getchar () != '\n')
2145 if (i != 'y' && i != 'Y')
2146 return 0;
2147 #endif /* DEBUG */
2150 /* Run each piped subprocess. */
2152 for (i = 0; i < n_commands; i++)
2154 char *errmsg_fmt, *errmsg_arg;
2155 char *string = commands[i].argv[0];
2157 commands[i].pid = pexecute (string, commands[i].argv,
2158 programname, temp_filename,
2159 &errmsg_fmt, &errmsg_arg,
2160 ((i == 0 ? PEXECUTE_FIRST : 0)
2161 | (i + 1 == n_commands ? PEXECUTE_LAST : 0)
2162 | (string == commands[i].prog
2163 ? PEXECUTE_SEARCH : 0)
2164 | (verbose_flag ? PEXECUTE_VERBOSE : 0)));
2166 if (commands[i].pid == -1)
2167 pfatal_pexecute (errmsg_fmt, errmsg_arg);
2169 if (string != commands[i].prog)
2170 free (string);
2173 execution_count++;
2175 /* Wait for all the subprocesses to finish.
2176 We don't care what order they finish in;
2177 we know that N_COMMANDS waits will get them all.
2178 Ignore subprocesses that we don't know about,
2179 since they can be spawned by the process that exec'ed us. */
2182 int ret_code = 0;
2184 for (i = 0; i < n_commands; )
2186 int j;
2187 int status;
2188 int pid;
2190 pid = pwait (commands[i].pid, &status, 0);
2191 if (pid < 0)
2192 abort ();
2194 for (j = 0; j < n_commands; j++)
2195 if (commands[j].pid == pid)
2197 i++;
2198 if (status != 0)
2200 if (WIFSIGNALED (status))
2202 fatal ("Internal compiler error: program %s got fatal signal %d",
2203 commands[j].prog, WTERMSIG (status));
2204 signal_count++;
2205 ret_code = -1;
2207 else if (WIFEXITED (status)
2208 && WEXITSTATUS (status) >= MIN_FATAL_STATUS)
2209 ret_code = -1;
2211 break;
2214 return ret_code;
2218 /* Find all the switches given to us
2219 and make a vector describing them.
2220 The elements of the vector are strings, one per switch given.
2221 If a switch uses following arguments, then the `part1' field
2222 is the switch itself and the `args' field
2223 is a null-terminated vector containing the following arguments.
2224 The `live_cond' field is 1 if the switch is true in a conditional spec,
2225 -1 if false (overridden by a later switch), and is initialized to zero.
2226 The `validated' field is nonzero if any spec has looked at this switch;
2227 if it remains zero at the end of the run, it must be meaningless. */
2229 struct switchstr
2231 char *part1;
2232 char **args;
2233 int live_cond;
2234 int validated;
2237 static struct switchstr *switches;
2239 static int n_switches;
2241 struct infile
2243 char *name;
2244 char *language;
2247 /* Also a vector of input files specified. */
2249 static struct infile *infiles;
2251 static int n_infiles;
2253 /* And a vector of corresponding output files is made up later. */
2255 static char **outfiles;
2257 /* Used to track if none of the -B paths are used. */
2258 static int warn_B;
2260 /* Used to track if standard path isn't used and -b or -V is specified. */
2261 static int warn_std;
2263 /* Gives value to pass as "warn" to add_prefix for standard prefixes. */
2264 static int *warn_std_ptr = 0;
2267 #if defined(HAVE_OBJECT_SUFFIX) || defined(HAVE_EXECUTABLE_SUFFIX)
2269 /* Convert NAME to a new name if it is the standard suffix. DO_EXE
2270 is true if we should look for an executable suffix as well. */
2272 static char *
2273 convert_filename (name, do_exe)
2274 char *name;
2275 int do_exe;
2277 int i;
2278 int len = strlen (name);
2280 #ifdef HAVE_OBJECT_SUFFIX
2281 /* Convert x.o to x.obj if OBJECT_SUFFIX is ".obj". */
2282 if (len > 2
2283 && name[len - 2] == '.'
2284 && name[len - 1] == 'o')
2286 obstack_grow (&obstack, name, len - 2);
2287 obstack_grow0 (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
2288 name = obstack_finish (&obstack);
2290 #endif
2292 #ifdef HAVE_EXECUTABLE_SUFFIX
2293 /* If there is no filetype, make it the executable suffix (which includes
2294 the "."). But don't get confused if we have just "-o". */
2295 if (! do_exe || EXECUTABLE_SUFFIX[0] == 0 || (len == 2 && name[0] == '-'))
2296 return name;
2298 for (i = len - 1; i >= 0; i--)
2299 if (name[i] == '/' || name[i] == DIR_SEPARATOR)
2300 break;
2302 for (i++; i < len; i++)
2303 if (name[i] == '.')
2304 return name;
2306 obstack_grow (&obstack, name, len);
2307 obstack_grow0 (&obstack, EXECUTABLE_SUFFIX, strlen (EXECUTABLE_SUFFIX));
2308 name = obstack_finish (&obstack);
2309 #endif
2311 return name;
2313 #endif
2315 /* Create the vector `switches' and its contents.
2316 Store its length in `n_switches'. */
2318 static void
2319 process_command (argc, argv)
2320 int argc;
2321 char **argv;
2323 register int i;
2324 char *temp;
2325 char *spec_lang = 0;
2326 int last_language_n_infiles;
2327 int have_c = 0;
2328 int have_o = 0;
2329 int lang_n_infiles = 0;
2331 gcc_exec_prefix = getenv ("GCC_EXEC_PREFIX");
2333 n_switches = 0;
2334 n_infiles = 0;
2336 /* Figure compiler version from version string. */
2338 compiler_version = save_string (version_string, strlen (version_string));
2339 for (temp = compiler_version; *temp; ++temp)
2341 if (*temp == ' ')
2343 *temp = '\0';
2344 break;
2348 /* Set up the default search paths. */
2350 if (gcc_exec_prefix)
2352 add_prefix (&exec_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2353 add_prefix (&startfile_prefixes, gcc_exec_prefix, "GCC", 0, 0, NULL_PTR);
2356 /* COMPILER_PATH and LIBRARY_PATH have values
2357 that are lists of directory names with colons. */
2359 temp = getenv ("COMPILER_PATH");
2360 if (temp)
2362 char *startp, *endp;
2363 char *nstore = (char *) alloca (strlen (temp) + 3);
2365 startp = endp = temp;
2366 while (1)
2368 if (*endp == PATH_SEPARATOR || *endp == 0)
2370 strncpy (nstore, startp, endp-startp);
2371 if (endp == startp)
2372 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2373 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2375 nstore[endp-startp] = DIR_SEPARATOR;
2376 nstore[endp-startp+1] = 0;
2378 else
2379 nstore[endp-startp] = 0;
2380 add_prefix (&exec_prefixes, nstore, 0, 0, 0, NULL_PTR);
2381 if (*endp == 0)
2382 break;
2383 endp = startp = endp + 1;
2385 else
2386 endp++;
2390 temp = getenv ("LIBRARY_PATH");
2391 if (temp && *cross_compile == '0')
2393 char *startp, *endp;
2394 char *nstore = (char *) alloca (strlen (temp) + 3);
2396 startp = endp = temp;
2397 while (1)
2399 if (*endp == PATH_SEPARATOR || *endp == 0)
2401 strncpy (nstore, startp, endp-startp);
2402 if (endp == startp)
2403 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2404 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2406 nstore[endp-startp] = DIR_SEPARATOR;
2407 nstore[endp-startp+1] = 0;
2409 else
2410 nstore[endp-startp] = 0;
2411 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2412 0, 0, NULL_PTR);
2413 if (*endp == 0)
2414 break;
2415 endp = startp = endp + 1;
2417 else
2418 endp++;
2422 /* Use LPATH like LIBRARY_PATH (for the CMU build program). */
2423 temp = getenv ("LPATH");
2424 if (temp && *cross_compile == '0')
2426 char *startp, *endp;
2427 char *nstore = (char *) alloca (strlen (temp) + 3);
2429 startp = endp = temp;
2430 while (1)
2432 if (*endp == PATH_SEPARATOR || *endp == 0)
2434 strncpy (nstore, startp, endp-startp);
2435 if (endp == startp)
2436 strcpy (nstore, concat (".", dir_separator_str, NULL_PTR));
2437 else if (endp[-1] != '/' && endp[-1] != DIR_SEPARATOR)
2439 nstore[endp-startp] = DIR_SEPARATOR;
2440 nstore[endp-startp+1] = 0;
2442 else
2443 nstore[endp-startp] = 0;
2444 add_prefix (&startfile_prefixes, nstore, NULL_PTR,
2445 0, 0, NULL_PTR);
2446 if (*endp == 0)
2447 break;
2448 endp = startp = endp + 1;
2450 else
2451 endp++;
2455 /* Convert new-style -- options to old-style. */
2456 translate_options (&argc, &argv);
2458 #ifdef LANG_SPECIFIC_DRIVER
2459 /* Do language-specific adjustment/addition of flags. */
2460 lang_specific_driver (fatal, &argc, &argv);
2461 #endif
2463 /* Scan argv twice. Here, the first time, just count how many switches
2464 there will be in their vector, and how many input files in theirs.
2465 Here we also parse the switches that cc itself uses (e.g. -v). */
2467 for (i = 1; i < argc; i++)
2469 if (! strcmp (argv[i], "-dumpspecs"))
2471 struct spec_list *sl;
2472 init_spec ();
2473 for (sl = specs; sl; sl = sl->next)
2474 printf ("*%s:\n%s\n\n", sl->name, *(sl->ptr_spec));
2475 exit (0);
2477 else if (! strcmp (argv[i], "-dumpversion"))
2479 printf ("%s\n", spec_version);
2480 exit (0);
2482 else if (! strcmp (argv[i], "-dumpmachine"))
2484 printf ("%s\n", spec_machine);
2485 exit (0);
2487 else if (! strcmp (argv[i], "-print-search-dirs"))
2488 print_search_dirs = 1;
2489 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2490 print_file_name = "libgcc.a";
2491 else if (! strncmp (argv[i], "-print-file-name=", 17))
2492 print_file_name = argv[i] + 17;
2493 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2494 print_prog_name = argv[i] + 17;
2495 else if (! strcmp (argv[i], "-print-multi-lib"))
2496 print_multi_lib = 1;
2497 else if (! strcmp (argv[i], "-print-multi-directory"))
2498 print_multi_directory = 1;
2499 else if (! strncmp (argv[i], "-Wa,", 4))
2501 int prev, j;
2502 /* Pass the rest of this option to the assembler. */
2504 n_assembler_options++;
2505 if (!assembler_options)
2506 assembler_options
2507 = (char **) xmalloc (n_assembler_options * sizeof (char **));
2508 else
2509 assembler_options
2510 = (char **) xrealloc (assembler_options,
2511 n_assembler_options * sizeof (char **));
2513 /* Split the argument at commas. */
2514 prev = 4;
2515 for (j = 4; argv[i][j]; j++)
2516 if (argv[i][j] == ',')
2518 assembler_options[n_assembler_options - 1]
2519 = save_string (argv[i] + prev, j - prev);
2520 n_assembler_options++;
2521 assembler_options
2522 = (char **) xrealloc (assembler_options,
2523 n_assembler_options * sizeof (char **));
2524 prev = j + 1;
2526 /* Record the part after the last comma. */
2527 assembler_options[n_assembler_options - 1] = argv[i] + prev;
2529 else if (! strncmp (argv[i], "-Wp,", 4))
2531 int prev, j;
2532 /* Pass the rest of this option to the preprocessor. */
2534 n_preprocessor_options++;
2535 if (!preprocessor_options)
2536 preprocessor_options
2537 = (char **) xmalloc (n_preprocessor_options * sizeof (char **));
2538 else
2539 preprocessor_options
2540 = (char **) xrealloc (preprocessor_options,
2541 n_preprocessor_options * sizeof (char **));
2543 /* Split the argument at commas. */
2544 prev = 4;
2545 for (j = 4; argv[i][j]; j++)
2546 if (argv[i][j] == ',')
2548 preprocessor_options[n_preprocessor_options - 1]
2549 = save_string (argv[i] + prev, j - prev);
2550 n_preprocessor_options++;
2551 preprocessor_options
2552 = (char **) xrealloc (preprocessor_options,
2553 n_preprocessor_options * sizeof (char **));
2554 prev = j + 1;
2556 /* Record the part after the last comma. */
2557 preprocessor_options[n_preprocessor_options - 1] = argv[i] + prev;
2559 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2560 /* The +e options to the C++ front-end. */
2561 n_switches++;
2562 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2564 int j;
2565 /* Split the argument at commas. */
2566 for (j = 3; argv[i][j]; j++)
2567 n_infiles += (argv[i][j] == ',');
2569 else if (strcmp (argv[i], "-Xlinker") == 0)
2571 if (i + 1 == argc)
2572 fatal ("argument to `-Xlinker' is missing");
2574 n_infiles++;
2575 i++;
2577 else if (strncmp (argv[i], "-l", 2) == 0)
2578 n_infiles++;
2579 else if (strcmp (argv[i], "-save-temps") == 0)
2581 save_temps_flag = 1;
2582 n_switches++;
2584 else if (strcmp (argv[i], "-specs") == 0)
2586 struct user_specs *user = (struct user_specs *)
2587 xmalloc (sizeof (struct user_specs));
2588 if (++i >= argc)
2589 fatal ("argument to `-specs' is missing");
2591 user->next = (struct user_specs *)0;
2592 user->filename = argv[i];
2593 if (user_specs_tail)
2594 user_specs_tail->next = user;
2595 else
2596 user_specs_head = user;
2597 user_specs_tail = user;
2599 else if (strncmp (argv[i], "-specs=", 7) == 0)
2601 struct user_specs *user = (struct user_specs *)
2602 xmalloc (sizeof (struct user_specs));
2603 if (strlen (argv[i]) == 7)
2604 fatal ("argument to `-specs=' is missing");
2606 user->next = (struct user_specs *)0;
2607 user->filename = argv[i]+7;
2608 if (user_specs_tail)
2609 user_specs_tail->next = user;
2610 else
2611 user_specs_head = user;
2612 user_specs_tail = user;
2614 else if (argv[i][0] == '-' && argv[i][1] != 0)
2616 register char *p = &argv[i][1];
2617 register int c = *p;
2619 switch (c)
2621 case 'b':
2622 if (p[1] == 0 && i + 1 == argc)
2623 fatal ("argument to `-b' is missing");
2624 if (p[1] == 0)
2625 spec_machine = argv[++i];
2626 else
2627 spec_machine = p + 1;
2629 warn_std_ptr = &warn_std;
2630 break;
2632 case 'B':
2634 int *temp = (int *) xmalloc (sizeof (int));
2635 char *value;
2636 if (p[1] == 0 && i + 1 == argc)
2637 fatal ("argument to `-B' is missing");
2638 if (p[1] == 0)
2639 value = argv[++i];
2640 else
2641 value = p + 1;
2642 add_prefix (&exec_prefixes, value, NULL_PTR, 1, 0, &warn_B);
2643 add_prefix (&startfile_prefixes, value, NULL_PTR,
2644 1, 0, &warn_B);
2645 add_prefix (&include_prefixes, concat (value, "include",
2646 NULL_PTR),
2647 NULL_PTR, 1, 0, NULL_PTR);
2649 /* As a kludge, if the arg is "[foo/]stageN/", just add
2650 "[foo/]include" to the include prefix. */
2652 int len = strlen (value);
2653 if ((len == 7
2654 || (len > 7
2655 && (value[len - 8] == '/'
2656 || value[len - 8] == DIR_SEPARATOR)))
2657 && strncmp (value + len - 7, "stage", 5) == 0
2658 && isdigit (value[len - 2])
2659 && (value[len - 1] == '/'
2660 || value[len - 1] == DIR_SEPARATOR))
2662 if (len == 7)
2663 add_prefix (&include_prefixes, "include", NULL_PTR,
2664 1, 0, NULL_PTR);
2665 else
2667 char *string = xmalloc (len + 1);
2668 strncpy (string, value, len-7);
2669 strcpy (string+len-7, "include");
2670 add_prefix (&include_prefixes, string, NULL_PTR,
2671 1, 0, NULL_PTR);
2676 break;
2678 case 'v': /* Print our subcommands and print versions. */
2679 n_switches++;
2680 /* If they do anything other than exactly `-v', don't set
2681 verbose_flag; rather, continue on to give the error. */
2682 if (p[1] != 0)
2683 break;
2684 verbose_flag++;
2685 break;
2687 case 'V':
2688 if (p[1] == 0 && i + 1 == argc)
2689 fatal ("argument to `-V' is missing");
2690 if (p[1] == 0)
2691 spec_version = argv[++i];
2692 else
2693 spec_version = p + 1;
2694 compiler_version = spec_version;
2695 warn_std_ptr = &warn_std;
2697 /* Validate the version number. Use the same checks
2698 done when inserting it into a spec.
2700 The format of the version string is
2701 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
2703 char *v = compiler_version;
2705 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
2706 while (! isdigit (*v))
2707 v++;
2709 if (v > compiler_version && v[-1] != '-')
2710 fatal ("invalid version number format");
2712 /* Set V after the first period. */
2713 while (isdigit (*v))
2714 v++;
2716 if (*v != '.')
2717 fatal ("invalid version number format");
2719 v++;
2720 while (isdigit (*v))
2721 v++;
2723 if (*v != 0 && *v != ' ' && *v != '.' && *v != '-')
2724 fatal ("invalid version number format");
2726 break;
2728 case 'c':
2729 if (p[1] == 0)
2731 have_c = 1;
2732 n_switches++;
2733 break;
2735 goto normal_switch;
2737 case 'o':
2738 have_o = 1;
2739 #if defined(HAVE_EXECUTABLE_SUFFIX) || defined(HAVE_OBJECT_SUFFIX)
2740 argv[i] = convert_filename (argv[i], 1);
2741 if (p[1] == 0)
2742 argv[i+1] = convert_filename (argv[i+1], 1);
2743 #endif
2744 goto normal_switch;
2746 default:
2747 normal_switch:
2748 n_switches++;
2750 if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
2751 i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
2752 else if (WORD_SWITCH_TAKES_ARG (p))
2753 i += WORD_SWITCH_TAKES_ARG (p);
2756 else
2758 n_infiles++;
2759 lang_n_infiles++;
2763 if (have_c && have_o && lang_n_infiles > 1)
2764 fatal ("cannot specify -o with -c and multiple compilations");
2766 /* Set up the search paths before we go looking for config files. */
2768 /* These come before the md prefixes so that we will find gcc's subcommands
2769 (such as cpp) rather than those of the host system. */
2770 /* Use 2 as fourth arg meaning try just the machine as a suffix,
2771 as well as trying the machine and the version. */
2772 #ifndef OS2
2773 add_prefix (&exec_prefixes, standard_exec_prefix, "BINUTILS",
2774 0, 2, warn_std_ptr);
2775 add_prefix (&exec_prefixes, standard_exec_prefix_1, "BINUTILS",
2776 0, 2, warn_std_ptr);
2777 #endif
2779 add_prefix (&startfile_prefixes, standard_exec_prefix, "BINUTILS",
2780 0, 1, warn_std_ptr);
2781 add_prefix (&startfile_prefixes, standard_exec_prefix_1, "BINUTILS",
2782 0, 1, warn_std_ptr);
2784 tooldir_prefix = concat (tooldir_base_prefix, spec_machine,
2785 dir_separator_str, NULL_PTR);
2787 /* If tooldir is relative, base it on exec_prefixes. A relative
2788 tooldir lets us move the installed tree as a unit.
2790 If GCC_EXEC_PREFIX is defined, then we want to add two relative
2791 directories, so that we can search both the user specified directory
2792 and the standard place. */
2794 if (*tooldir_prefix != '/' && *tooldir_prefix != DIR_SEPARATOR)
2796 if (gcc_exec_prefix)
2798 char *gcc_exec_tooldir_prefix
2799 = concat (gcc_exec_prefix, spec_machine, dir_separator_str,
2800 spec_version, dir_separator_str, tooldir_prefix, NULL_PTR);
2802 add_prefix (&exec_prefixes,
2803 concat (gcc_exec_tooldir_prefix, "bin",
2804 dir_separator_str, NULL_PTR),
2805 NULL_PTR, 0, 0, NULL_PTR);
2806 add_prefix (&startfile_prefixes,
2807 concat (gcc_exec_tooldir_prefix, "lib",
2808 dir_separator_str, NULL_PTR),
2809 NULL_PTR, 0, 0, NULL_PTR);
2812 tooldir_prefix = concat (standard_exec_prefix, spec_machine,
2813 dir_separator_str, spec_version,
2814 dir_separator_str, tooldir_prefix, NULL_PTR);
2817 add_prefix (&exec_prefixes,
2818 concat (tooldir_prefix, "bin", dir_separator_str, NULL_PTR),
2819 "BINUTILS", 0, 0, NULL_PTR);
2820 add_prefix (&startfile_prefixes,
2821 concat (tooldir_prefix, "lib", dir_separator_str, NULL_PTR),
2822 "BINUTILS", 0, 0, NULL_PTR);
2824 /* More prefixes are enabled in main, after we read the specs file
2825 and determine whether this is cross-compilation or not. */
2828 /* Then create the space for the vectors and scan again. */
2830 switches = ((struct switchstr *)
2831 xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
2832 infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
2833 n_switches = 0;
2834 n_infiles = 0;
2835 last_language_n_infiles = -1;
2837 /* This, time, copy the text of each switch and store a pointer
2838 to the copy in the vector of switches.
2839 Store all the infiles in their vector. */
2841 for (i = 1; i < argc; i++)
2843 /* Just skip the switches that were handled by the preceding loop. */
2844 if (! strncmp (argv[i], "-Wa,", 4))
2846 else if (! strncmp (argv[i], "-Wp,", 4))
2848 else if (! strcmp (argv[i], "-print-search-dirs"))
2850 else if (! strcmp (argv[i], "-print-libgcc-file-name"))
2852 else if (! strncmp (argv[i], "-print-file-name=", 17))
2854 else if (! strncmp (argv[i], "-print-prog-name=", 17))
2856 else if (! strcmp (argv[i], "-print-multi-lib"))
2858 else if (! strcmp (argv[i], "-print-multi-directory"))
2860 else if (argv[i][0] == '+' && argv[i][1] == 'e')
2862 /* Compensate for the +e options to the C++ front-end;
2863 they're there simply for cfront call-compatibility. We do
2864 some magic in default_compilers to pass them down properly.
2865 Note we deliberately start at the `+' here, to avoid passing
2866 -e0 or -e1 down into the linker. */
2867 switches[n_switches].part1 = &argv[i][0];
2868 switches[n_switches].args = 0;
2869 switches[n_switches].live_cond = 0;
2870 switches[n_switches].validated = 0;
2871 n_switches++;
2873 else if (strncmp (argv[i], "-Wl,", 4) == 0)
2875 int prev, j;
2876 /* Split the argument at commas. */
2877 prev = 4;
2878 for (j = 4; argv[i][j]; j++)
2879 if (argv[i][j] == ',')
2881 infiles[n_infiles].language = "*";
2882 infiles[n_infiles++].name
2883 = save_string (argv[i] + prev, j - prev);
2884 prev = j + 1;
2886 /* Record the part after the last comma. */
2887 infiles[n_infiles].language = "*";
2888 infiles[n_infiles++].name = argv[i] + prev;
2890 else if (strcmp (argv[i], "-Xlinker") == 0)
2892 infiles[n_infiles].language = "*";
2893 infiles[n_infiles++].name = argv[++i];
2895 else if (strncmp (argv[i], "-l", 2) == 0)
2897 infiles[n_infiles].language = "*";
2898 infiles[n_infiles++].name = argv[i];
2900 else if (strcmp (argv[i], "-specs") == 0)
2901 i++;
2902 else if (strncmp (argv[i], "-specs=", 7) == 0)
2904 /* -save-temps overrides -pipe, so that temp files are produced */
2905 else if (save_temps_flag && strcmp (argv[i], "-pipe") == 0)
2906 error ("Warning: -pipe ignored since -save-temps specified");
2907 else if (argv[i][0] == '-' && argv[i][1] != 0)
2909 register char *p = &argv[i][1];
2910 register int c = *p;
2912 if (c == 'B' || c == 'b' || c == 'V')
2914 /* Skip a separate arg, if any. */
2915 if (p[1] == 0)
2916 i++;
2917 continue;
2919 if (c == 'x')
2921 if (p[1] == 0 && i + 1 == argc)
2922 fatal ("argument to `-x' is missing");
2923 if (p[1] == 0)
2924 spec_lang = argv[++i];
2925 else
2926 spec_lang = p + 1;
2927 if (! strcmp (spec_lang, "none"))
2928 /* Suppress the warning if -xnone comes after the last input
2929 file, because alternate command interfaces like g++ might
2930 find it useful to place -xnone after each input file. */
2931 spec_lang = 0;
2932 else
2933 last_language_n_infiles = n_infiles;
2934 continue;
2936 switches[n_switches].part1 = p;
2937 /* Deal with option arguments in separate argv elements. */
2938 if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
2939 || WORD_SWITCH_TAKES_ARG (p))
2941 int j = 0;
2942 int n_args = WORD_SWITCH_TAKES_ARG (p);
2944 if (n_args == 0)
2946 /* Count only the option arguments in separate argv elements. */
2947 n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
2949 if (i + n_args >= argc)
2950 fatal ("argument to `-%s' is missing", p);
2951 switches[n_switches].args
2952 = (char **) xmalloc ((n_args + 1) * sizeof (char *));
2953 while (j < n_args)
2954 switches[n_switches].args[j++] = argv[++i];
2955 /* Null-terminate the vector. */
2956 switches[n_switches].args[j] = 0;
2958 else if (index (switches_need_spaces, c))
2960 /* On some systems, ld cannot handle some options without
2961 a space. So split the option from its argument. */
2962 char *part1 = (char *) xmalloc (2);
2963 part1[0] = c;
2964 part1[1] = '\0';
2966 switches[n_switches].part1 = part1;
2967 switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
2968 switches[n_switches].args[0] = xmalloc (strlen (p));
2969 strcpy (switches[n_switches].args[0], &p[1]);
2970 switches[n_switches].args[1] = 0;
2972 else
2973 switches[n_switches].args = 0;
2975 switches[n_switches].live_cond = 0;
2976 switches[n_switches].validated = 0;
2977 /* This is always valid, since gcc.c itself understands it. */
2978 if (!strcmp (p, "save-temps"))
2979 switches[n_switches].validated = 1;
2980 n_switches++;
2982 else
2984 #ifdef HAVE_OBJECT_SUFFIX
2985 argv[i] = convert_filename (argv[i], 0);
2986 #endif
2988 if (strcmp (argv[i], "-") != 0 && access (argv[i], R_OK) < 0)
2990 perror_with_name (argv[i]);
2991 error_count++;
2993 else
2995 infiles[n_infiles].language = spec_lang;
2996 infiles[n_infiles++].name = argv[i];
3001 if (n_infiles == last_language_n_infiles && spec_lang != 0)
3002 error ("Warning: `-x %s' after last input file has no effect", spec_lang);
3004 switches[n_switches].part1 = 0;
3005 infiles[n_infiles].name = 0;
3008 /* Process a spec string, accumulating and running commands. */
3010 /* These variables describe the input file name.
3011 input_file_number is the index on outfiles of this file,
3012 so that the output file name can be stored for later use by %o.
3013 input_basename is the start of the part of the input file
3014 sans all directory names, and basename_length is the number
3015 of characters starting there excluding the suffix .c or whatever. */
3017 static char *input_filename;
3018 static int input_file_number;
3019 static int input_filename_length;
3020 static int basename_length;
3021 static char *input_basename;
3022 static char *input_suffix;
3024 /* These are variables used within do_spec and do_spec_1. */
3026 /* Nonzero if an arg has been started and not yet terminated
3027 (with space, tab or newline). */
3028 static int arg_going;
3030 /* Nonzero means %d or %g has been seen; the next arg to be terminated
3031 is a temporary file name. */
3032 static int delete_this_arg;
3034 /* Nonzero means %w has been seen; the next arg to be terminated
3035 is the output file name of this compilation. */
3036 static int this_is_output_file;
3038 /* Nonzero means %s has been seen; the next arg to be terminated
3039 is the name of a library file and we should try the standard
3040 search dirs for it. */
3041 static int this_is_library_file;
3043 /* Nonzero means that the input of this command is coming from a pipe. */
3044 static int input_from_pipe;
3046 /* Process the spec SPEC and run the commands specified therein.
3047 Returns 0 if the spec is successfully processed; -1 if failed. */
3049 static int
3050 do_spec (spec)
3051 char *spec;
3053 int value;
3055 clear_args ();
3056 arg_going = 0;
3057 delete_this_arg = 0;
3058 this_is_output_file = 0;
3059 this_is_library_file = 0;
3060 input_from_pipe = 0;
3062 value = do_spec_1 (spec, 0, NULL_PTR);
3064 /* Force out any unfinished command.
3065 If -pipe, this forces out the last command if it ended in `|'. */
3066 if (value == 0)
3068 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3069 argbuf_index--;
3071 if (argbuf_index > 0)
3072 value = execute ();
3075 return value;
3078 /* Process the sub-spec SPEC as a portion of a larger spec.
3079 This is like processing a whole spec except that we do
3080 not initialize at the beginning and we do not supply a
3081 newline by default at the end.
3082 INSWITCH nonzero means don't process %-sequences in SPEC;
3083 in this case, % is treated as an ordinary character.
3084 This is used while substituting switches.
3085 INSWITCH nonzero also causes SPC not to terminate an argument.
3087 Value is zero unless a line was finished
3088 and the command on that line reported an error. */
3090 static int
3091 do_spec_1 (spec, inswitch, soft_matched_part)
3092 char *spec;
3093 int inswitch;
3094 char *soft_matched_part;
3096 register char *p = spec;
3097 register int c;
3098 int i;
3099 char *string;
3100 int value;
3102 while (c = *p++)
3103 /* If substituting a switch, treat all chars like letters.
3104 Otherwise, NL, SPC, TAB and % are special. */
3105 switch (inswitch ? 'a' : c)
3107 case '\n':
3108 /* End of line: finish any pending argument,
3109 then run the pending command if one has been started. */
3110 if (arg_going)
3112 obstack_1grow (&obstack, 0);
3113 string = obstack_finish (&obstack);
3114 if (this_is_library_file)
3115 string = find_file (string);
3116 store_arg (string, delete_this_arg, this_is_output_file);
3117 if (this_is_output_file)
3118 outfiles[input_file_number] = string;
3120 arg_going = 0;
3122 if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
3124 for (i = 0; i < n_switches; i++)
3125 if (!strcmp (switches[i].part1, "pipe"))
3126 break;
3128 /* A `|' before the newline means use a pipe here,
3129 but only if -pipe was specified.
3130 Otherwise, execute now and don't pass the `|' as an arg. */
3131 if (i < n_switches)
3133 input_from_pipe = 1;
3134 switches[i].validated = 1;
3135 break;
3137 else
3138 argbuf_index--;
3141 if (argbuf_index > 0)
3143 value = execute ();
3144 if (value)
3145 return value;
3147 /* Reinitialize for a new command, and for a new argument. */
3148 clear_args ();
3149 arg_going = 0;
3150 delete_this_arg = 0;
3151 this_is_output_file = 0;
3152 this_is_library_file = 0;
3153 input_from_pipe = 0;
3154 break;
3156 case '|':
3157 /* End any pending argument. */
3158 if (arg_going)
3160 obstack_1grow (&obstack, 0);
3161 string = obstack_finish (&obstack);
3162 if (this_is_library_file)
3163 string = find_file (string);
3164 store_arg (string, delete_this_arg, this_is_output_file);
3165 if (this_is_output_file)
3166 outfiles[input_file_number] = string;
3169 /* Use pipe */
3170 obstack_1grow (&obstack, c);
3171 arg_going = 1;
3172 break;
3174 case '\t':
3175 case ' ':
3176 /* Space or tab ends an argument if one is pending. */
3177 if (arg_going)
3179 obstack_1grow (&obstack, 0);
3180 string = obstack_finish (&obstack);
3181 if (this_is_library_file)
3182 string = find_file (string);
3183 store_arg (string, delete_this_arg, this_is_output_file);
3184 if (this_is_output_file)
3185 outfiles[input_file_number] = string;
3187 /* Reinitialize for a new argument. */
3188 arg_going = 0;
3189 delete_this_arg = 0;
3190 this_is_output_file = 0;
3191 this_is_library_file = 0;
3192 break;
3194 case '%':
3195 switch (c = *p++)
3197 case 0:
3198 fatal ("Invalid specification! Bug in cc.");
3200 case 'b':
3201 obstack_grow (&obstack, input_basename, basename_length);
3202 arg_going = 1;
3203 break;
3205 case 'd':
3206 delete_this_arg = 2;
3207 break;
3209 /* Dump out the directories specified with LIBRARY_PATH,
3210 followed by the absolute directories
3211 that we search for startfiles. */
3212 case 'D':
3214 struct prefix_list *pl = startfile_prefixes.plist;
3215 int bufsize = 100;
3216 char *buffer = (char *) xmalloc (bufsize);
3217 int idx;
3219 for (; pl; pl = pl->next)
3221 #ifdef RELATIVE_PREFIX_NOT_LINKDIR
3222 /* Used on systems which record the specified -L dirs
3223 and use them to search for dynamic linking. */
3224 /* Relative directories always come from -B,
3225 and it is better not to use them for searching
3226 at run time. In particular, stage1 loses */
3227 if (pl->prefix[0] != '/' && pl->prefix[0] != DIR_SEPARATOR)
3228 continue;
3229 #endif
3230 /* Try subdirectory if there is one. */
3231 if (multilib_dir != NULL)
3233 if (machine_suffix)
3235 if (strlen (pl->prefix) + strlen (machine_suffix)
3236 >= bufsize)
3237 bufsize = (strlen (pl->prefix)
3238 + strlen (machine_suffix)) * 2 + 1;
3239 buffer = (char *) xrealloc (buffer, bufsize);
3240 strcpy (buffer, pl->prefix);
3241 strcat (buffer, machine_suffix);
3242 if (is_directory (buffer, multilib_dir, 1))
3244 do_spec_1 ("-L", 0, NULL_PTR);
3245 #ifdef SPACE_AFTER_L_OPTION
3246 do_spec_1 (" ", 0, NULL_PTR);
3247 #endif
3248 do_spec_1 (buffer, 1, NULL_PTR);
3249 do_spec_1 (multilib_dir, 1, NULL_PTR);
3250 /* Make this a separate argument. */
3251 do_spec_1 (" ", 0, NULL_PTR);
3254 if (!pl->require_machine_suffix)
3256 if (is_directory (pl->prefix, multilib_dir, 1))
3258 do_spec_1 ("-L", 0, NULL_PTR);
3259 #ifdef SPACE_AFTER_L_OPTION
3260 do_spec_1 (" ", 0, NULL_PTR);
3261 #endif
3262 do_spec_1 (pl->prefix, 1, NULL_PTR);
3263 do_spec_1 (multilib_dir, 1, NULL_PTR);
3264 /* Make this a separate argument. */
3265 do_spec_1 (" ", 0, NULL_PTR);
3269 if (machine_suffix)
3271 if (is_directory (pl->prefix, machine_suffix, 1))
3273 do_spec_1 ("-L", 0, NULL_PTR);
3274 #ifdef SPACE_AFTER_L_OPTION
3275 do_spec_1 (" ", 0, NULL_PTR);
3276 #endif
3277 do_spec_1 (pl->prefix, 1, NULL_PTR);
3278 /* Remove slash from machine_suffix. */
3279 if (strlen (machine_suffix) >= bufsize)
3280 bufsize = strlen (machine_suffix) * 2 + 1;
3281 buffer = (char *) xrealloc (buffer, bufsize);
3282 strcpy (buffer, machine_suffix);
3283 idx = strlen (buffer);
3284 if (buffer[idx - 1] == '/'
3285 || buffer[idx - 1] == DIR_SEPARATOR)
3286 buffer[idx - 1] = 0;
3287 do_spec_1 (buffer, 1, NULL_PTR);
3288 /* Make this a separate argument. */
3289 do_spec_1 (" ", 0, NULL_PTR);
3292 if (!pl->require_machine_suffix)
3294 if (is_directory (pl->prefix, "", 1))
3296 do_spec_1 ("-L", 0, NULL_PTR);
3297 #ifdef SPACE_AFTER_L_OPTION
3298 do_spec_1 (" ", 0, NULL_PTR);
3299 #endif
3300 /* Remove slash from pl->prefix. */
3301 if (strlen (pl->prefix) >= bufsize)
3302 bufsize = strlen (pl->prefix) * 2 + 1;
3303 buffer = (char *) xrealloc (buffer, bufsize);
3304 strcpy (buffer, pl->prefix);
3305 idx = strlen (buffer);
3306 if (buffer[idx - 1] == '/'
3307 || buffer[idx - 1] == DIR_SEPARATOR)
3308 buffer[idx - 1] = 0;
3309 do_spec_1 (buffer, 1, NULL_PTR);
3310 /* Make this a separate argument. */
3311 do_spec_1 (" ", 0, NULL_PTR);
3315 free (buffer);
3317 break;
3319 case 'e':
3320 /* %efoo means report an error with `foo' as error message
3321 and don't execute any more commands for this file. */
3323 char *q = p;
3324 char *buf;
3325 while (*p != 0 && *p != '\n') p++;
3326 buf = (char *) alloca (p - q + 1);
3327 strncpy (buf, q, p - q);
3328 buf[p - q] = 0;
3329 error (buf);
3330 return -1;
3332 break;
3334 case 'g':
3335 case 'u':
3336 case 'U':
3337 if (save_temps_flag)
3339 obstack_grow (&obstack, input_basename, basename_length);
3340 delete_this_arg = 0;
3342 else
3344 #ifdef MKTEMP_EACH_FILE
3345 /* ??? This has a problem: the total number of
3346 values mktemp can return is limited.
3347 That matters for the names of object files.
3348 In 2.4, do something about that. */
3349 struct temp_name *t;
3350 char *suffix = p;
3351 while (*p == '.' || isalpha (*p)
3352 || (p[0] == '%' && p[1] == 'O'))
3353 p++;
3355 /* See if we already have an association of %g/%u/%U and
3356 suffix. */
3357 for (t = temp_names; t; t = t->next)
3358 if (t->length == p - suffix
3359 && strncmp (t->suffix, suffix, p - suffix) == 0
3360 && t->unique == (c != 'g'))
3361 break;
3363 /* Make a new association if needed. %u requires one. */
3364 if (t == 0 || c == 'u')
3366 if (t == 0)
3368 t = (struct temp_name *) xmalloc (sizeof (struct temp_name));
3369 t->next = temp_names;
3370 temp_names = t;
3372 t->length = p - suffix;
3373 t->suffix = save_string (suffix, p - suffix);
3374 t->unique = (c != 'g');
3375 temp_filename = choose_temp_base ();
3376 temp_filename_length = strlen (temp_filename);
3377 t->filename = temp_filename;
3378 t->filename_length = temp_filename_length;
3381 obstack_grow (&obstack, t->filename, t->filename_length);
3382 delete_this_arg = 1;
3383 #else
3384 obstack_grow (&obstack, temp_filename, temp_filename_length);
3385 if (c == 'u' || c == 'U')
3387 static int unique;
3388 char buff[9];
3389 if (c == 'u')
3390 unique++;
3391 sprintf (buff, "%d", unique);
3392 obstack_grow (&obstack, buff, strlen (buff));
3394 #endif
3395 delete_this_arg = 1;
3397 arg_going = 1;
3398 break;
3400 case 'i':
3401 obstack_grow (&obstack, input_filename, input_filename_length);
3402 arg_going = 1;
3403 break;
3405 case 'I':
3407 struct prefix_list *pl = include_prefixes.plist;
3409 if (gcc_exec_prefix)
3411 do_spec_1 ("-iprefix", 1, NULL_PTR);
3412 /* Make this a separate argument. */
3413 do_spec_1 (" ", 0, NULL_PTR);
3414 do_spec_1 (gcc_exec_prefix, 1, NULL_PTR);
3415 do_spec_1 (" ", 0, NULL_PTR);
3418 for (; pl; pl = pl->next)
3420 do_spec_1 ("-isystem", 1, NULL_PTR);
3421 /* Make this a separate argument. */
3422 do_spec_1 (" ", 0, NULL_PTR);
3423 do_spec_1 (pl->prefix, 1, NULL_PTR);
3424 do_spec_1 (" ", 0, NULL_PTR);
3427 break;
3429 case 'o':
3430 for (i = 0; i < n_infiles; i++)
3431 store_arg (outfiles[i], 0, 0);
3432 break;
3434 case 'O':
3435 obstack_grow (&obstack, OBJECT_SUFFIX, strlen (OBJECT_SUFFIX));
3436 arg_going = 1;
3437 break;
3439 case 's':
3440 this_is_library_file = 1;
3441 break;
3443 case 'w':
3444 this_is_output_file = 1;
3445 break;
3447 case 'W':
3449 int cur_index = argbuf_index;
3450 /* Handle the {...} following the %W. */
3451 if (*p != '{')
3452 abort ();
3453 p = handle_braces (p + 1);
3454 if (p == 0)
3455 return -1;
3456 /* If any args were output, mark the last one for deletion
3457 on failure. */
3458 if (argbuf_index != cur_index)
3459 record_temp_file (argbuf[argbuf_index - 1], 0, 1);
3460 break;
3463 /* %x{OPTION} records OPTION for %X to output. */
3464 case 'x':
3466 char *p1 = p;
3467 char *string;
3469 /* Skip past the option value and make a copy. */
3470 if (*p != '{')
3471 abort ();
3472 while (*p++ != '}')
3474 string = save_string (p1 + 1, p - p1 - 2);
3476 /* See if we already recorded this option. */
3477 for (i = 0; i < n_linker_options; i++)
3478 if (! strcmp (string, linker_options[i]))
3480 free (string);
3481 return 0;
3484 /* This option is new; add it. */
3485 n_linker_options++;
3486 if (!linker_options)
3487 linker_options
3488 = (char **) xmalloc (n_linker_options * sizeof (char **));
3489 else
3490 linker_options
3491 = (char **) xrealloc (linker_options,
3492 n_linker_options * sizeof (char **));
3494 linker_options[n_linker_options - 1] = string;
3496 break;
3498 /* Dump out the options accumulated previously using %x. */
3499 case 'X':
3500 for (i = 0; i < n_linker_options; i++)
3502 do_spec_1 (linker_options[i], 1, NULL_PTR);
3503 /* Make each accumulated option a separate argument. */
3504 do_spec_1 (" ", 0, NULL_PTR);
3506 break;
3508 /* Dump out the options accumulated previously using -Wa,. */
3509 case 'Y':
3510 for (i = 0; i < n_assembler_options; i++)
3512 do_spec_1 (assembler_options[i], 1, NULL_PTR);
3513 /* Make each accumulated option a separate argument. */
3514 do_spec_1 (" ", 0, NULL_PTR);
3516 break;
3518 /* Dump out the options accumulated previously using -Wp,. */
3519 case 'Z':
3520 for (i = 0; i < n_preprocessor_options; i++)
3522 do_spec_1 (preprocessor_options[i], 1, NULL_PTR);
3523 /* Make each accumulated option a separate argument. */
3524 do_spec_1 (" ", 0, NULL_PTR);
3526 break;
3528 /* Here are digits and numbers that just process
3529 a certain constant string as a spec. */
3531 case '1':
3532 value = do_spec_1 (cc1_spec, 0, NULL_PTR);
3533 if (value != 0)
3534 return value;
3535 break;
3537 case '2':
3538 value = do_spec_1 (cc1plus_spec, 0, NULL_PTR);
3539 if (value != 0)
3540 return value;
3541 break;
3543 case 'a':
3544 value = do_spec_1 (asm_spec, 0, NULL_PTR);
3545 if (value != 0)
3546 return value;
3547 break;
3549 case 'A':
3550 value = do_spec_1 (asm_final_spec, 0, NULL_PTR);
3551 if (value != 0)
3552 return value;
3553 break;
3555 case 'c':
3556 value = do_spec_1 (signed_char_spec, 0, NULL_PTR);
3557 if (value != 0)
3558 return value;
3559 break;
3561 case 'C':
3562 value = do_spec_1 (cpp_spec, 0, NULL_PTR);
3563 if (value != 0)
3564 return value;
3565 break;
3567 case 'E':
3568 value = do_spec_1 (endfile_spec, 0, NULL_PTR);
3569 if (value != 0)
3570 return value;
3571 break;
3573 case 'l':
3574 value = do_spec_1 (link_spec, 0, NULL_PTR);
3575 if (value != 0)
3576 return value;
3577 break;
3579 case 'L':
3580 value = do_spec_1 (lib_spec, 0, NULL_PTR);
3581 if (value != 0)
3582 return value;
3583 break;
3585 case 'G':
3586 value = do_spec_1 (libgcc_spec, 0, NULL_PTR);
3587 if (value != 0)
3588 return value;
3589 break;
3591 case 'p':
3593 char *x = (char *) alloca (strlen (cpp_predefines) + 1);
3594 char *buf = x;
3595 char *y;
3597 /* Copy all of the -D options in CPP_PREDEFINES into BUF. */
3598 y = cpp_predefines;
3599 while (*y != 0)
3601 if (! strncmp (y, "-D", 2))
3602 /* Copy the whole option. */
3603 while (*y && *y != ' ' && *y != '\t')
3604 *x++ = *y++;
3605 else if (*y == ' ' || *y == '\t')
3606 /* Copy whitespace to the result. */
3607 *x++ = *y++;
3608 /* Don't copy other options. */
3609 else
3610 y++;
3613 *x = 0;
3615 value = do_spec_1 (buf, 0, NULL_PTR);
3616 if (value != 0)
3617 return value;
3619 break;
3621 case 'P':
3623 char *x = (char *) alloca (strlen (cpp_predefines) * 4 + 1);
3624 char *buf = x;
3625 char *y;
3627 /* Copy all of CPP_PREDEFINES into BUF,
3628 but put __ after every -D and at the end of each arg. */
3629 y = cpp_predefines;
3630 while (*y != 0)
3632 if (! strncmp (y, "-D", 2))
3634 int flag = 0;
3636 *x++ = *y++;
3637 *x++ = *y++;
3639 if (*y != '_'
3640 || (*(y+1) != '_' && ! isupper (*(y+1))))
3642 /* Stick __ at front of macro name. */
3643 *x++ = '_';
3644 *x++ = '_';
3645 /* Arrange to stick __ at the end as well. */
3646 flag = 1;
3649 /* Copy the macro name. */
3650 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3651 *x++ = *y++;
3653 if (flag)
3655 *x++ = '_';
3656 *x++ = '_';
3659 /* Copy the value given, if any. */
3660 while (*y && *y != ' ' && *y != '\t')
3661 *x++ = *y++;
3663 else if (*y == ' ' || *y == '\t')
3664 /* Copy whitespace to the result. */
3665 *x++ = *y++;
3666 /* Don't copy -A options */
3667 else
3668 y++;
3670 *x++ = ' ';
3672 /* Copy all of CPP_PREDEFINES into BUF,
3673 but put __ after every -D. */
3674 y = cpp_predefines;
3675 while (*y != 0)
3677 if (! strncmp (y, "-D", 2))
3679 y += 2;
3681 if (*y != '_'
3682 || (*(y+1) != '_' && ! isupper (*(y+1))))
3684 /* Stick -D__ at front of macro name. */
3685 *x++ = '-';
3686 *x++ = 'D';
3687 *x++ = '_';
3688 *x++ = '_';
3690 /* Copy the macro name. */
3691 while (*y && *y != '=' && *y != ' ' && *y != '\t')
3692 *x++ = *y++;
3694 /* Copy the value given, if any. */
3695 while (*y && *y != ' ' && *y != '\t')
3696 *x++ = *y++;
3698 else
3700 /* Do not copy this macro - we have just done it before */
3701 while (*y && *y != ' ' && *y != '\t')
3702 y++;
3705 else if (*y == ' ' || *y == '\t')
3706 /* Copy whitespace to the result. */
3707 *x++ = *y++;
3708 /* Don't copy -A options */
3709 else
3710 y++;
3712 *x++ = ' ';
3714 /* Copy all of the -A options in CPP_PREDEFINES into BUF. */
3715 y = cpp_predefines;
3716 while (*y != 0)
3718 if (! strncmp (y, "-A", 2))
3719 /* Copy the whole option. */
3720 while (*y && *y != ' ' && *y != '\t')
3721 *x++ = *y++;
3722 else if (*y == ' ' || *y == '\t')
3723 /* Copy whitespace to the result. */
3724 *x++ = *y++;
3725 /* Don't copy other options. */
3726 else
3727 y++;
3730 *x = 0;
3732 value = do_spec_1 (buf, 0, NULL_PTR);
3733 if (value != 0)
3734 return value;
3736 break;
3738 case 'S':
3739 value = do_spec_1 (startfile_spec, 0, NULL_PTR);
3740 if (value != 0)
3741 return value;
3742 break;
3744 /* Here we define characters other than letters and digits. */
3746 case '{':
3747 p = handle_braces (p);
3748 if (p == 0)
3749 return -1;
3750 break;
3752 case '%':
3753 obstack_1grow (&obstack, '%');
3754 break;
3756 case '*':
3757 do_spec_1 (soft_matched_part, 1, NULL_PTR);
3758 do_spec_1 (" ", 0, NULL_PTR);
3759 break;
3761 /* Process a string found as the value of a spec given by name.
3762 This feature allows individual machine descriptions
3763 to add and use their own specs.
3764 %[...] modifies -D options the way %P does;
3765 %(...) uses the spec unmodified. */
3766 case '(':
3767 case '[':
3769 char *name = p;
3770 struct spec_list *sl;
3771 int len;
3773 /* The string after the S/P is the name of a spec that is to be
3774 processed. */
3775 while (*p && *p != ')' && *p != ']')
3776 p++;
3778 /* See if it's in the list */
3779 for (len = p - name, sl = specs; sl; sl = sl->next)
3780 if (sl->name_len == len && !strncmp (sl->name, name, len))
3782 name = *(sl->ptr_spec);
3783 #ifdef DEBUG_SPECS
3784 notice ("Processing spec %c%s%c, which is '%s'\n",
3785 c, sl->name, (c == '(') ? ')' : ']', name);
3786 #endif
3787 break;
3790 if (sl)
3792 if (c == '(')
3794 value = do_spec_1 (name, 0, NULL_PTR);
3795 if (value != 0)
3796 return value;
3798 else
3800 char *x = (char *) alloca (strlen (name) * 2 + 1);
3801 char *buf = x;
3802 char *y = name;
3804 /* Copy all of NAME into BUF, but put __ after
3805 every -D and at the end of each arg, */
3806 while (1)
3808 int flag;
3810 if (! strncmp (y, "-D", 2))
3812 *x++ = '-';
3813 *x++ = 'D';
3814 *x++ = '_';
3815 *x++ = '_';
3816 y += 2;
3817 flag = 1;
3818 continue;
3820 else if (flag && (*y == ' ' || *y == '\t' || *y == '='
3821 || *y == '}' || *y == 0))
3823 *x++ = '_';
3824 *x++ = '_';
3825 flag = 0;
3827 if (*y == 0)
3828 break;
3829 else
3830 *x++ = *y++;
3832 *x = 0;
3834 value = do_spec_1 (buf, 0, NULL_PTR);
3835 if (value != 0)
3836 return value;
3840 /* Discard the closing paren or bracket. */
3841 if (*p)
3842 p++;
3844 break;
3846 case 'v':
3848 int c1 = *p++; /* Select first or second version number. */
3849 char *v = compiler_version;
3850 char *q;
3852 /* The format of the version string is
3853 ([^0-9]*-)?[0-9]+[.][0-9]+([.][0-9]+)?([- ].*)? */
3855 /* Ignore leading non-digits. i.e. "foo-" in "foo-2.7.2". */
3856 while (! isdigit (*v))
3857 v++;
3858 if (v > compiler_version && v[-1] != '-')
3859 abort ();
3861 /* If desired, advance to second version number. */
3862 if (c1 == '2')
3864 /* Set V after the first period. */
3865 while (isdigit (*v))
3866 v++;
3867 if (*v != '.')
3868 abort ();
3869 v++;
3872 /* Set Q at the next period or at the end. */
3873 q = v;
3874 while (isdigit (*q))
3875 q++;
3876 if (*q != 0 && *q != ' ' && *q != '.' && *q != '-')
3877 abort ();
3879 /* Put that part into the command. */
3880 obstack_grow (&obstack, v, q - v);
3881 arg_going = 1;
3883 break;
3885 case '|':
3886 if (input_from_pipe)
3887 do_spec_1 ("-", 0, NULL_PTR);
3888 break;
3890 default:
3891 abort ();
3893 break;
3895 case '\\':
3896 /* Backslash: treat next character as ordinary. */
3897 c = *p++;
3899 /* fall through */
3900 default:
3901 /* Ordinary character: put it into the current argument. */
3902 obstack_1grow (&obstack, c);
3903 arg_going = 1;
3906 return 0; /* End of string */
3909 /* Return 0 if we call do_spec_1 and that returns -1. */
3911 static char *
3912 handle_braces (p)
3913 register char *p;
3915 register char *q;
3916 char *filter;
3917 int pipe_p = 0;
3918 int negate = 0;
3919 int suffix = 0;
3920 int include_blanks = 1;
3922 if (*p == '^')
3923 /* A '^' after the open-brace means to not give blanks before args. */
3924 include_blanks = 0, ++p;
3926 if (*p == '|')
3927 /* A `|' after the open-brace means,
3928 if the test fails, output a single minus sign rather than nothing.
3929 This is used in %{|!pipe:...}. */
3930 pipe_p = 1, ++p;
3932 if (*p == '!')
3933 /* A `!' after the open-brace negates the condition:
3934 succeed if the specified switch is not present. */
3935 negate = 1, ++p;
3937 if (*p == '.')
3938 /* A `.' after the open-brace means test against the current suffix. */
3940 if (pipe_p)
3941 abort ();
3943 suffix = 1;
3944 ++p;
3947 filter = p;
3948 while (*p != ':' && *p != '}') p++;
3949 if (*p != '}')
3951 register int count = 1;
3952 q = p + 1;
3953 while (count > 0)
3955 if (*q == '{')
3956 count++;
3957 else if (*q == '}')
3958 count--;
3959 else if (*q == 0)
3960 abort ();
3961 q++;
3964 else
3965 q = p + 1;
3967 if (suffix)
3969 int found = (input_suffix != 0
3970 && strlen (input_suffix) == p - filter
3971 && strncmp (input_suffix, filter, p - filter) == 0);
3973 if (p[0] == '}')
3974 abort ();
3976 if (negate != found
3977 && do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
3978 return 0;
3980 return q;
3982 else if (p[-1] == '*' && p[0] == '}')
3984 /* Substitute all matching switches as separate args. */
3985 register int i;
3986 --p;
3987 for (i = 0; i < n_switches; i++)
3988 if (!strncmp (switches[i].part1, filter, p - filter)
3989 && check_live_switch (i, p - filter))
3990 give_switch (i, 0, include_blanks);
3992 else
3994 /* Test for presence of the specified switch. */
3995 register int i;
3996 int present = 0;
3998 /* If name specified ends in *, as in {x*:...},
3999 check for %* and handle that case. */
4000 if (p[-1] == '*' && !negate)
4002 int substitution;
4003 char *r = p;
4005 /* First see whether we have %*. */
4006 substitution = 0;
4007 while (r < q)
4009 if (*r == '%' && r[1] == '*')
4010 substitution = 1;
4011 r++;
4013 /* If we do, handle that case. */
4014 if (substitution)
4016 /* Substitute all matching switches as separate args.
4017 But do this by substituting for %*
4018 in the text that follows the colon. */
4020 unsigned hard_match_len = p - filter - 1;
4021 char *string = save_string (p + 1, q - p - 2);
4023 for (i = 0; i < n_switches; i++)
4024 if (!strncmp (switches[i].part1, filter, hard_match_len)
4025 && check_live_switch (i, -1))
4027 do_spec_1 (string, 0, &switches[i].part1[hard_match_len]);
4028 /* Pass any arguments this switch has. */
4029 give_switch (i, 1, 1);
4032 return q;
4036 /* If name specified ends in *, as in {x*:...},
4037 check for presence of any switch name starting with x. */
4038 if (p[-1] == '*')
4040 for (i = 0; i < n_switches; i++)
4042 unsigned hard_match_len = p - filter - 1;
4044 if (!strncmp (switches[i].part1, filter, hard_match_len)
4045 && check_live_switch (i, hard_match_len))
4047 present = 1;
4051 /* Otherwise, check for presence of exact name specified. */
4052 else
4054 for (i = 0; i < n_switches; i++)
4056 if (!strncmp (switches[i].part1, filter, p - filter)
4057 && switches[i].part1[p - filter] == 0
4058 && check_live_switch (i, -1))
4060 present = 1;
4061 break;
4066 /* If it is as desired (present for %{s...}, absent for %{-s...})
4067 then substitute either the switch or the specified
4068 conditional text. */
4069 if (present != negate)
4071 if (*p == '}')
4073 give_switch (i, 0, include_blanks);
4075 else
4077 if (do_spec_1 (save_string (p + 1, q - p - 2), 0, NULL_PTR) < 0)
4078 return 0;
4081 else if (pipe_p)
4083 /* Here if a %{|...} conditional fails: output a minus sign,
4084 which means "standard output" or "standard input". */
4085 do_spec_1 ("-", 0, NULL_PTR);
4089 return q;
4092 /* Return 0 iff switch number SWITCHNUM is obsoleted by a later switch
4093 on the command line. PREFIX_LENGTH is the length of XXX in an {XXX*}
4094 spec, or -1 if either exact match or %* is used.
4096 A -O switch is obsoleted by a later -O switch. A -f, -m, or -W switch
4097 whose value does not begin with "no-" is obsoleted by the same value
4098 with the "no-", similarly for a switch with the "no-" prefix. */
4100 static int
4101 check_live_switch (switchnum, prefix_length)
4102 int switchnum;
4103 int prefix_length;
4105 char *name = switches[switchnum].part1;
4106 int i;
4108 /* In the common case of {<at-most-one-letter>*}, a negating
4109 switch would always match, so ignore that case. We will just
4110 send the conflicting switches to the compiler phase. */
4111 if (prefix_length >= 0 && prefix_length <= 1)
4112 return 1;
4114 /* If we already processed this switch and determined if it was
4115 live or not, return our past determination. */
4116 if (switches[switchnum].live_cond != 0)
4117 return switches[switchnum].live_cond > 0;
4119 /* Now search for duplicate in a manner that depends on the name. */
4120 switch (*name)
4122 case 'O':
4123 for (i = switchnum + 1; i < n_switches; i++)
4124 if (switches[i].part1[0] == 'O')
4126 switches[switchnum].validated = 1;
4127 switches[switchnum].live_cond = -1;
4128 return 0;
4130 break;
4132 case 'W': case 'f': case 'm':
4133 if (! strncmp (name + 1, "no-", 3))
4135 /* We have Xno-YYY, search for XYYY. */
4136 for (i = switchnum + 1; i < n_switches; i++)
4137 if (switches[i].part1[0] == name[0]
4138 && ! strcmp (&switches[i].part1[1], &name[4]))
4140 switches[switchnum].validated = 1;
4141 switches[switchnum].live_cond = -1;
4142 return 0;
4145 else
4147 /* We have XYYY, search for Xno-YYY. */
4148 for (i = switchnum + 1; i < n_switches; i++)
4149 if (switches[i].part1[0] == name[0]
4150 && switches[i].part1[1] == 'n'
4151 && switches[i].part1[2] == 'o'
4152 && switches[i].part1[3] == '-'
4153 && !strcmp (&switches[i].part1[4], &name[1]))
4155 switches[switchnum].validated = 1;
4156 switches[switchnum].live_cond = -1;
4157 return 0;
4160 break;
4163 /* Otherwise the switch is live. */
4164 switches[switchnum].live_cond = 1;
4165 return 1;
4168 /* Pass a switch to the current accumulating command
4169 in the same form that we received it.
4170 SWITCHNUM identifies the switch; it is an index into
4171 the vector of switches gcc received, which is `switches'.
4172 This cannot fail since it never finishes a command line.
4174 If OMIT_FIRST_WORD is nonzero, then we omit .part1 of the argument.
4176 If INCLUDE_BLANKS is nonzero, then we include blanks before each argument
4177 of the switch. */
4179 static void
4180 give_switch (switchnum, omit_first_word, include_blanks)
4181 int switchnum;
4182 int omit_first_word;
4183 int include_blanks;
4185 if (!omit_first_word)
4187 do_spec_1 ("-", 0, NULL_PTR);
4188 do_spec_1 (switches[switchnum].part1, 1, NULL_PTR);
4191 if (switches[switchnum].args != 0)
4193 char **p;
4194 for (p = switches[switchnum].args; *p; p++)
4196 if (include_blanks)
4197 do_spec_1 (" ", 0, NULL_PTR);
4198 do_spec_1 (*p, 1, NULL_PTR);
4202 do_spec_1 (" ", 0, NULL_PTR);
4203 switches[switchnum].validated = 1;
4206 /* Search for a file named NAME trying various prefixes including the
4207 user's -B prefix and some standard ones.
4208 Return the absolute file name found. If nothing is found, return NAME. */
4210 static char *
4211 find_file (name)
4212 char *name;
4214 char *newname;
4216 /* Try multilib_dir if it is defined. */
4217 if (multilib_dir != NULL)
4219 char *try;
4221 try = (char *) alloca (strlen (multilib_dir) + strlen (name) + 2);
4222 strcpy (try, multilib_dir);
4223 strcat (try, dir_separator_str);
4224 strcat (try, name);
4226 newname = find_a_file (&startfile_prefixes, try, R_OK);
4228 /* If we don't find it in the multi library dir, then fall
4229 through and look for it in the normal places. */
4230 if (newname != NULL)
4231 return newname;
4234 newname = find_a_file (&startfile_prefixes, name, R_OK);
4235 return newname ? newname : name;
4238 /* Determine whether a directory exists. If LINKER, return 0 for
4239 certain fixed names not needed by the linker. If not LINKER, it is
4240 only important to return 0 if the host machine has a small ARG_MAX
4241 limit. */
4243 static int
4244 is_directory (path1, path2, linker)
4245 char *path1;
4246 char *path2;
4247 int linker;
4249 int len1 = strlen (path1);
4250 int len2 = strlen (path2);
4251 char *path = (char *) alloca (3 + len1 + len2);
4252 char *cp;
4253 struct stat st;
4255 #ifndef SMALL_ARG_MAX
4256 if (! linker)
4257 return 1;
4258 #endif
4260 /* Construct the path from the two parts. Ensure the string ends with "/.".
4261 The resulting path will be a directory even if the given path is a
4262 symbolic link. */
4263 bcopy (path1, path, len1);
4264 bcopy (path2, path + len1, len2);
4265 cp = path + len1 + len2;
4266 if (cp[-1] != '/' && cp[-1] != DIR_SEPARATOR)
4267 *cp++ = DIR_SEPARATOR;
4268 *cp++ = '.';
4269 *cp = '\0';
4271 /* Exclude directories that the linker is known to search. */
4272 if (linker
4273 && ((cp - path == 6
4274 && strcmp (path, concat (dir_separator_str, "lib",
4275 dir_separator_str, ".", NULL_PTR)) == 0)
4276 || (cp - path == 10
4277 && strcmp (path, concat (dir_separator_str, "usr",
4278 dir_separator_str, "lib",
4279 dir_separator_str, ".", NULL_PTR)) == 0)))
4280 return 0;
4282 return (stat (path, &st) >= 0 && S_ISDIR (st.st_mode));
4285 /* On fatal signals, delete all the temporary files. */
4287 static void
4288 fatal_error (signum)
4289 int signum;
4291 signal (signum, SIG_DFL);
4292 delete_failure_queue ();
4293 delete_temp_files ();
4294 /* Get the same signal again, this time not handled,
4295 so its normal effect occurs. */
4296 kill (getpid (), signum);
4300 main (argc, argv)
4301 int argc;
4302 char **argv;
4304 register int i;
4305 int j;
4306 int value;
4307 int linker_was_run = 0;
4308 char *explicit_link_files;
4309 char *specs_file;
4310 char *p;
4311 struct user_specs *uptr;
4313 p = argv[0] + strlen (argv[0]);
4314 while (p != argv[0] && p[-1] != '/' && p[-1] != DIR_SEPARATOR) --p;
4315 programname = p;
4317 setlocale (LC_MESSAGES, "");
4318 bindtextdomain (PACKAGE, localedir);
4319 textdomain (PACKAGE);
4321 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
4322 signal (SIGINT, fatal_error);
4323 #ifdef SIGHUP
4324 if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
4325 signal (SIGHUP, fatal_error);
4326 #endif
4327 if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
4328 signal (SIGTERM, fatal_error);
4329 #ifdef SIGPIPE
4330 if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
4331 signal (SIGPIPE, fatal_error);
4332 #endif
4334 /* If this is a test release of GCC, issue a warning. */
4335 if (version_string[0] == 't' && version_string[1] == 'e')
4336 snapshot_warning ();
4338 argbuf_length = 10;
4339 argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
4341 obstack_init (&obstack);
4343 /* Build multilib_select, et. al from the separate lines that make up each
4344 multilib selection. */
4346 char **q = multilib_raw;
4347 int need_space;
4349 obstack_init (&multilib_obstack);
4350 while ((p = *q++) != (char *) 0)
4351 obstack_grow (&multilib_obstack, p, strlen (p));
4353 obstack_1grow (&multilib_obstack, 0);
4354 multilib_select = obstack_finish (&multilib_obstack);
4356 q = multilib_matches_raw;
4357 while ((p = *q++) != (char *) 0)
4358 obstack_grow (&multilib_obstack, p, strlen (p));
4360 obstack_1grow (&multilib_obstack, 0);
4361 multilib_matches = obstack_finish (&multilib_obstack);
4363 need_space = FALSE;
4364 for (i = 0;
4365 i < sizeof (multilib_defaults_raw) / sizeof (multilib_defaults_raw[0]);
4366 i++)
4368 if (need_space)
4369 obstack_1grow (&multilib_obstack, ' ');
4370 obstack_grow (&multilib_obstack,
4371 multilib_defaults_raw[i],
4372 strlen (multilib_defaults_raw[i]));
4373 need_space = TRUE;
4376 obstack_1grow (&multilib_obstack, 0);
4377 multilib_defaults = obstack_finish (&multilib_obstack);
4380 /* Set up to remember the pathname of gcc and any options
4381 needed for collect. We use argv[0] instead of programname because
4382 we need the complete pathname. */
4383 obstack_init (&collect_obstack);
4384 obstack_grow (&collect_obstack, "COLLECT_GCC=", sizeof ("COLLECT_GCC=")-1);
4385 obstack_grow (&collect_obstack, argv[0], strlen (argv[0])+1);
4386 putenv (obstack_finish (&collect_obstack));
4388 #ifdef INIT_ENVIRONMENT
4389 /* Set up any other necessary machine specific environment variables. */
4390 putenv (INIT_ENVIRONMENT);
4391 #endif
4393 /* Choose directory for temp files. */
4395 temp_filename = choose_temp_base ();
4396 temp_filename_length = strlen (temp_filename);
4398 /* Make a table of what switches there are (switches, n_switches).
4399 Make a table of specified input files (infiles, n_infiles).
4400 Decode switches that are handled locally. */
4402 process_command (argc, argv);
4404 /* Initialize the vector of specs to just the default.
4405 This means one element containing 0s, as a terminator. */
4407 compilers = (struct compiler *) xmalloc (sizeof default_compilers);
4408 bcopy ((char *) default_compilers, (char *) compilers,
4409 sizeof default_compilers);
4410 n_compilers = n_default_compilers;
4412 /* Read specs from a file if there is one. */
4414 machine_suffix = concat (spec_machine, dir_separator_str,
4415 spec_version, dir_separator_str, NULL_PTR);
4416 just_machine_suffix = concat (spec_machine, dir_separator_str, NULL_PTR);
4418 specs_file = find_a_file (&startfile_prefixes, "specs", R_OK);
4419 /* Read the specs file unless it is a default one. */
4420 if (specs_file != 0 && strcmp (specs_file, "specs"))
4421 read_specs (specs_file, TRUE);
4422 else
4423 init_spec ();
4425 /* We need to check standard_exec_prefix/just_machine_suffix/specs
4426 for any override of as, ld and libraries. */
4427 specs_file = (char *) alloca (strlen (standard_exec_prefix)
4428 + strlen (just_machine_suffix)
4429 + sizeof ("specs"));
4431 strcpy (specs_file, standard_exec_prefix);
4432 strcat (specs_file, just_machine_suffix);
4433 strcat (specs_file, "specs");
4434 if (access (specs_file, R_OK) == 0)
4435 read_specs (specs_file, TRUE);
4437 /* Process any user specified specs in the order given on the command
4438 line. */
4439 for (uptr = user_specs_head; uptr; uptr = uptr->next)
4441 char *filename = find_a_file (&startfile_prefixes, uptr->filename, R_OK);
4442 read_specs (filename ? filename : uptr->filename, FALSE);
4445 /* If not cross-compiling, look for startfiles in the standard places. */
4446 /* The fact that these are done here, after reading the specs file,
4447 means that it cannot be found in these directories.
4448 But that's okay. It should never be there anyway. */
4449 if (*cross_compile == '0')
4451 #ifdef MD_EXEC_PREFIX
4452 add_prefix (&exec_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4453 add_prefix (&startfile_prefixes, md_exec_prefix, "GCC", 0, 0, NULL_PTR);
4454 #endif
4456 #ifdef MD_STARTFILE_PREFIX
4457 add_prefix (&startfile_prefixes, md_startfile_prefix, "GCC",
4458 0, 0, NULL_PTR);
4459 #endif
4461 #ifdef MD_STARTFILE_PREFIX_1
4462 add_prefix (&startfile_prefixes, md_startfile_prefix_1, "GCC",
4463 0, 0, NULL_PTR);
4464 #endif
4466 /* If standard_startfile_prefix is relative, base it on
4467 standard_exec_prefix. This lets us move the installed tree
4468 as a unit. If GCC_EXEC_PREFIX is defined, base
4469 standard_startfile_prefix on that as well. */
4470 if (*standard_startfile_prefix == '/'
4471 || *standard_startfile_prefix == DIR_SEPARATOR
4472 || *standard_startfile_prefix == '$'
4473 #ifdef __MSDOS__
4474 /* Check for disk name on MS-DOS-based systems. */
4475 || (standard_startfile_prefix[1] == ':'
4476 && (standard_startfile_prefix[2] == DIR_SEPARATOR
4477 || standard_startfile_prefix[2] == '/'))
4478 #endif
4480 add_prefix (&startfile_prefixes, standard_startfile_prefix, "BINUTILS",
4481 0, 0, NULL_PTR);
4482 else
4484 if (gcc_exec_prefix)
4485 add_prefix (&startfile_prefixes,
4486 concat (gcc_exec_prefix, machine_suffix,
4487 standard_startfile_prefix, NULL_PTR),
4488 NULL_PTR, 0, 0, NULL_PTR);
4489 add_prefix (&startfile_prefixes,
4490 concat (standard_exec_prefix,
4491 machine_suffix,
4492 standard_startfile_prefix, NULL_PTR),
4493 NULL_PTR, 0, 0, NULL_PTR);
4496 add_prefix (&startfile_prefixes, standard_startfile_prefix_1,
4497 "BINUTILS", 0, 0, NULL_PTR);
4498 add_prefix (&startfile_prefixes, standard_startfile_prefix_2,
4499 "BINUTILS", 0, 0, NULL_PTR);
4500 #if 0 /* Can cause surprises, and one can use -B./ instead. */
4501 add_prefix (&startfile_prefixes, "./", NULL_PTR, 0, 1, NULL_PTR);
4502 #endif
4504 else
4506 if (*standard_startfile_prefix != DIR_SEPARATOR && gcc_exec_prefix)
4507 add_prefix (&startfile_prefixes,
4508 concat (gcc_exec_prefix, machine_suffix,
4509 standard_startfile_prefix, NULL_PTR),
4510 "BINUTILS", 0, 0, NULL_PTR);
4513 /* If we have a GCC_EXEC_PREFIX envvar, modify it for cpp's sake. */
4514 if (gcc_exec_prefix)
4516 char * temp = (char *) xmalloc (strlen (gcc_exec_prefix)
4517 + strlen (spec_version)
4518 + strlen (spec_machine) + 3);
4519 strcpy (temp, gcc_exec_prefix);
4520 strcat (temp, spec_machine);
4521 strcat (temp, dir_separator_str);
4522 strcat (temp, spec_version);
4523 strcat (temp, dir_separator_str);
4524 gcc_exec_prefix = temp;
4527 /* Now we have the specs.
4528 Set the `valid' bits for switches that match anything in any spec. */
4530 validate_all_switches ();
4532 /* Now that we have the switches and the specs, set
4533 the subdirectory based on the options. */
4534 set_multilib_dir ();
4536 /* Warn about any switches that no pass was interested in. */
4538 for (i = 0; i < n_switches; i++)
4539 if (! switches[i].validated)
4540 error ("unrecognized option `-%s'", switches[i].part1);
4542 /* Obey some of the options. */
4544 if (print_search_dirs)
4546 printf ("install: %s%s\n", standard_exec_prefix, machine_suffix);
4547 printf ("programs: %s\n", build_search_list (&exec_prefixes, "", 0));
4548 printf ("libraries: %s\n", build_search_list (&startfile_prefixes, "", 0));
4549 exit (0);
4552 if (print_file_name)
4554 printf ("%s\n", find_file (print_file_name));
4555 exit (0);
4558 if (print_prog_name)
4560 char *newname = find_a_file (&exec_prefixes, print_prog_name, X_OK);
4561 printf ("%s\n", (newname ? newname : print_prog_name));
4562 exit (0);
4565 if (print_multi_lib)
4567 print_multilib_info ();
4568 exit (0);
4571 if (print_multi_directory)
4573 if (multilib_dir == NULL)
4574 printf (".\n");
4575 else
4576 printf ("%s\n", multilib_dir);
4577 exit (0);
4580 if (verbose_flag)
4582 int n;
4584 /* compiler_version is truncated at the first space when initialized
4585 from version string, so truncate version_string at the first space
4586 before comparing. */
4587 for (n = 0; version_string[n]; n++)
4588 if (version_string[n] == ' ')
4589 break;
4591 if (! strncmp (version_string, compiler_version, n)
4592 && compiler_version[n] == 0)
4593 notice ("gcc version %s\n", version_string);
4594 else
4595 notice ("gcc driver version %s executing gcc version %s\n",
4596 version_string, compiler_version);
4598 if (n_infiles == 0)
4599 exit (0);
4602 if (n_infiles == 0)
4603 fatal ("No input files");
4605 /* Make a place to record the compiler output file names
4606 that correspond to the input files. */
4608 outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
4609 bzero ((char *) outfiles, n_infiles * sizeof (char *));
4611 /* Record which files were specified explicitly as link input. */
4613 explicit_link_files = xmalloc (n_infiles);
4614 bzero (explicit_link_files, n_infiles);
4616 for (i = 0; i < n_infiles; i++)
4618 register struct compiler *cp = 0;
4619 int this_file_error = 0;
4621 /* Tell do_spec what to substitute for %i. */
4623 input_filename = infiles[i].name;
4624 input_filename_length = strlen (input_filename);
4625 input_file_number = i;
4627 /* Use the same thing in %o, unless cp->spec says otherwise. */
4629 outfiles[i] = input_filename;
4631 /* Figure out which compiler from the file's suffix. */
4633 cp = lookup_compiler (infiles[i].name, input_filename_length,
4634 infiles[i].language);
4636 if (cp)
4638 /* Ok, we found an applicable compiler. Run its spec. */
4639 /* First say how much of input_filename to substitute for %b */
4640 register char *p;
4641 int len;
4643 if (cp->spec[0][0] == '#')
4644 error ("%s: %s compiler not installed on this system",
4645 input_filename, &cp->spec[0][1]);
4647 input_basename = input_filename;
4648 for (p = input_filename; *p; p++)
4649 if (*p == '/' || *p == DIR_SEPARATOR)
4650 input_basename = p + 1;
4652 /* Find a suffix starting with the last period,
4653 and set basename_length to exclude that suffix. */
4654 basename_length = strlen (input_basename);
4655 p = input_basename + basename_length;
4656 while (p != input_basename && *p != '.') --p;
4657 if (*p == '.' && p != input_basename)
4659 basename_length = p - input_basename;
4660 input_suffix = p + 1;
4662 else
4663 input_suffix = "";
4665 len = 0;
4666 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4667 if (cp->spec[j])
4668 len += strlen (cp->spec[j]);
4670 p = (char *) xmalloc (len + 1);
4672 len = 0;
4673 for (j = 0; j < sizeof cp->spec / sizeof cp->spec[0]; j++)
4674 if (cp->spec[j])
4676 strcpy (p + len, cp->spec[j]);
4677 len += strlen (cp->spec[j]);
4680 value = do_spec (p);
4681 free (p);
4682 if (value < 0)
4683 this_file_error = 1;
4686 /* If this file's name does not contain a recognized suffix,
4687 record it as explicit linker input. */
4689 else
4690 explicit_link_files[i] = 1;
4692 /* Clear the delete-on-failure queue, deleting the files in it
4693 if this compilation failed. */
4695 if (this_file_error)
4697 delete_failure_queue ();
4698 error_count++;
4700 /* If this compilation succeeded, don't delete those files later. */
4701 clear_failure_queue ();
4704 /* Run ld to link all the compiler output files. */
4706 if (error_count == 0)
4708 int tmp = execution_count;
4709 int i;
4710 int first_time;
4712 /* Rebuild the COMPILER_PATH and LIBRARY_PATH environment variables
4713 for collect. */
4714 putenv_from_prefixes (&exec_prefixes, "COMPILER_PATH=");
4715 putenv_from_prefixes (&startfile_prefixes, "LIBRARY_PATH=");
4717 /* Build COLLECT_GCC_OPTIONS to have all of the options specified to
4718 the compiler. */
4719 obstack_grow (&collect_obstack, "COLLECT_GCC_OPTIONS=",
4720 sizeof ("COLLECT_GCC_OPTIONS=")-1);
4722 first_time = TRUE;
4723 for (i = 0; i < n_switches; i++)
4725 char **args;
4726 if (!first_time)
4727 obstack_grow (&collect_obstack, " ", 1);
4729 first_time = FALSE;
4730 obstack_grow (&collect_obstack, "-", 1);
4731 obstack_grow (&collect_obstack, switches[i].part1,
4732 strlen (switches[i].part1));
4734 for (args = switches[i].args; args && *args; args++)
4736 obstack_grow (&collect_obstack, " ", 1);
4737 obstack_grow (&collect_obstack, *args, strlen (*args));
4740 obstack_grow (&collect_obstack, "\0", 1);
4741 putenv (obstack_finish (&collect_obstack));
4743 value = do_spec (link_command_spec);
4744 if (value < 0)
4745 error_count = 1;
4746 linker_was_run = (tmp != execution_count);
4749 /* Warn if a -B option was specified but the prefix was never used. */
4750 unused_prefix_warnings (&exec_prefixes);
4751 unused_prefix_warnings (&startfile_prefixes);
4753 /* If options said don't run linker,
4754 complain about input files to be given to the linker. */
4756 if (! linker_was_run && error_count == 0)
4757 for (i = 0; i < n_infiles; i++)
4758 if (explicit_link_files[i])
4759 error ("%s: linker input file unused since linking not done",
4760 outfiles[i]);
4762 /* Delete some or all of the temporary files we made. */
4764 if (error_count)
4765 delete_failure_queue ();
4766 delete_temp_files ();
4768 exit (error_count > 0 ? (signal_count ? 2 : 1) : 0);
4769 /* NOTREACHED */
4770 return 0;
4773 /* Find the proper compilation spec for the file name NAME,
4774 whose length is LENGTH. LANGUAGE is the specified language,
4775 or 0 if this file is to be passed to the linker. */
4777 static struct compiler *
4778 lookup_compiler (name, length, language)
4779 char *name;
4780 int length;
4781 char *language;
4783 struct compiler *cp;
4785 /* If this was specified by the user to be a linker input, indicate that. */
4786 if (language != 0 && language[0] == '*')
4787 return 0;
4789 /* Otherwise, look for the language, if one is spec'd. */
4790 if (language != 0)
4792 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4793 if (cp->suffix[0] == '@' && !strcmp (cp->suffix + 1, language))
4794 return cp;
4796 error ("language %s not recognized", language);
4797 return 0;
4800 /* Look for a suffix. */
4801 for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
4803 if (/* The suffix `-' matches only the file name `-'. */
4804 (!strcmp (cp->suffix, "-") && !strcmp (name, "-"))
4805 || (strlen (cp->suffix) < length
4806 /* See if the suffix matches the end of NAME. */
4807 #ifdef OS2
4808 && ((!strcmp (cp->suffix,
4809 name + length - strlen (cp->suffix))
4810 || !strpbrk (cp->suffix, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))
4811 && !strcasecmp (cp->suffix,
4812 name + length - strlen (cp->suffix)))
4813 #else
4814 && !strcmp (cp->suffix,
4815 name + length - strlen (cp->suffix))
4816 #endif
4819 if (cp->spec[0][0] == '@')
4821 struct compiler *new;
4823 /* An alias entry maps a suffix to a language.
4824 Search for the language; pass 0 for NAME and LENGTH
4825 to avoid infinite recursion if language not found.
4826 Construct the new compiler spec. */
4827 language = cp->spec[0] + 1;
4828 new = (struct compiler *) xmalloc (sizeof (struct compiler));
4829 new->suffix = cp->suffix;
4830 bcopy ((char *) lookup_compiler (NULL_PTR, 0, language)->spec,
4831 (char *) new->spec, sizeof new->spec);
4832 return new;
4835 /* A non-alias entry: return it. */
4836 return cp;
4840 return 0;
4843 char *
4844 xmalloc (size)
4845 unsigned size;
4847 register char *value = (char *) malloc (size);
4848 if (value == 0)
4849 fatal ("virtual memory exhausted");
4850 return value;
4853 char *
4854 xrealloc (ptr, size)
4855 char *ptr;
4856 unsigned size;
4858 register char *value = (char *) realloc (ptr, size);
4859 if (value == 0)
4860 fatal ("virtual memory exhausted");
4861 return value;
4864 /* This function is based on the one in libiberty. */
4866 static char *
4867 concat VPROTO((char *first, ...))
4869 register int length;
4870 register char *newstr;
4871 register char *end;
4872 register char *arg;
4873 va_list args;
4874 #ifndef ANSI_PROTOTYPES
4875 char *first;
4876 #endif
4878 /* First compute the size of the result and get sufficient memory. */
4880 VA_START (args, first);
4881 #ifndef ANSI_PROTOTYPES
4882 first = va_arg (args, char *);
4883 #endif
4885 arg = first;
4886 length = 0;
4888 while (arg != 0)
4890 length += strlen (arg);
4891 arg = va_arg (args, char *);
4894 newstr = (char *) xmalloc (length + 1);
4895 va_end (args);
4897 /* Now copy the individual pieces to the result string. */
4899 VA_START (args, first);
4900 #ifndef ANSI_PROTOTYPES
4901 first = va_arg (args, char *);
4902 #endif
4904 end = newstr;
4905 arg = first;
4906 while (arg != 0)
4908 while (*arg)
4909 *end++ = *arg++;
4910 arg = va_arg (args, char *);
4912 *end = '\000';
4913 va_end (args);
4915 return (newstr);
4918 static char *
4919 save_string (s, len)
4920 char *s;
4921 int len;
4923 register char *result = xmalloc (len + 1);
4925 bcopy (s, result, len);
4926 result[len] = 0;
4927 return result;
4930 static void
4931 pfatal_with_name (name)
4932 char *name;
4934 perror_with_name (name);
4935 delete_temp_files ();
4936 exit (1);
4939 static void
4940 perror_with_name (name)
4941 char *name;
4943 fprintf (stderr, "%s: %s: %s\n", programname, name, my_strerror (errno));
4946 static void
4947 pfatal_pexecute (errmsg_fmt, errmsg_arg)
4948 char *errmsg_fmt;
4949 char *errmsg_arg;
4951 if (errmsg_arg)
4953 int save_errno = errno;
4955 /* Space for trailing '\0' is in %s. */
4956 char *msg = xmalloc (strlen (errmsg_fmt) + strlen (errmsg_arg));
4957 sprintf (msg, errmsg_fmt, errmsg_arg);
4958 errmsg_fmt = msg;
4960 errno = save_errno;
4963 pfatal_with_name (errmsg_fmt);
4966 /* More 'friendly' abort that prints the line and file.
4967 config.h can #define abort fancy_abort if you like that sort of thing. */
4969 void
4970 fancy_abort ()
4972 fatal ("Internal gcc abort.");
4975 #ifdef HAVE_VPRINTF
4977 /* Output an error message and exit */
4979 static void
4980 fatal VPROTO((char *msgid, ...))
4982 #ifndef ANSI_PROTOTYPES
4983 char *msgid;
4984 #endif
4985 va_list ap;
4987 VA_START (ap, msgid);
4989 #ifndef ANSI_PROTOTYPES
4990 msgid = va_arg (ap, char *);
4991 #endif
4993 fprintf (stderr, "%s: ", programname);
4994 vfprintf (stderr, _(msgid), ap);
4995 va_end (ap);
4996 fprintf (stderr, "\n");
4997 delete_temp_files ();
4998 exit (1);
5001 static void
5002 error VPROTO((char *msgid, ...))
5004 #ifndef ANSI_PROTOTYPES
5005 char *msgid;
5006 #endif
5007 va_list ap;
5009 VA_START (ap, msgid);
5011 #ifndef ANSI_PROTOTYPES
5012 msgid = va_arg (ap, char *);
5013 #endif
5015 fprintf (stderr, "%s: ", programname);
5016 vfprintf (stderr, _(msgid), ap);
5017 va_end (ap);
5019 fprintf (stderr, "\n");
5022 static void
5023 notice VPROTO((char *msgid, ...))
5025 #ifndef ANSI_PROTOTYPES
5026 char *msgid;
5027 #endif
5028 va_list ap;
5030 VA_START (ap, msgid);
5032 #ifndef ANSI_PROTOTYPES
5033 msgid = va_arg (ap, char *);
5034 #endif
5036 vfprintf (stderr, _(msgid), ap);
5037 va_end (ap);
5040 #else /* not HAVE_VPRINTF */
5042 static void
5043 fatal (msgid, arg1, arg2)
5044 char *msgid, *arg1, *arg2;
5046 error (msgid, arg1, arg2);
5047 delete_temp_files ();
5048 exit (1);
5051 static void
5052 error (msgid, arg1, arg2)
5053 char *msgid, *arg1, *arg2;
5055 fprintf (stderr, "%s: ", programname);
5056 fprintf (stderr, _(msgid), arg1, arg2);
5057 fprintf (stderr, "\n");
5060 static void
5061 notice (msgid, arg1, arg2)
5062 char *msgid, *arg1, *arg2;
5064 fprintf (stderr, _(msgid), arg1, arg2);
5067 #endif /* not HAVE_VPRINTF */
5070 static void
5071 validate_all_switches ()
5073 struct compiler *comp;
5074 register char *p;
5075 register char c;
5076 struct spec_list *spec;
5078 for (comp = compilers; comp->spec[0]; comp++)
5080 int i;
5081 for (i = 0; i < sizeof comp->spec / sizeof comp->spec[0] && comp->spec[i]; i++)
5083 p = comp->spec[i];
5084 while (c = *p++)
5085 if (c == '%' && *p == '{')
5086 /* We have a switch spec. */
5087 validate_switches (p + 1);
5091 /* look through the linked list of specs read from the specs file */
5092 for (spec = specs; spec ; spec = spec->next)
5094 p = *(spec->ptr_spec);
5095 while (c = *p++)
5096 if (c == '%' && *p == '{')
5097 /* We have a switch spec. */
5098 validate_switches (p + 1);
5101 p = link_command_spec;
5102 while (c = *p++)
5103 if (c == '%' && *p == '{')
5104 /* We have a switch spec. */
5105 validate_switches (p + 1);
5108 /* Look at the switch-name that comes after START
5109 and mark as valid all supplied switches that match it. */
5111 static void
5112 validate_switches (start)
5113 char *start;
5115 register char *p = start;
5116 char *filter;
5117 register int i;
5118 int suffix = 0;
5120 if (*p == '|')
5121 ++p;
5123 if (*p == '!')
5124 ++p;
5126 if (*p == '.')
5127 suffix = 1, ++p;
5129 filter = p;
5130 while (*p != ':' && *p != '}') p++;
5132 if (suffix)
5134 else if (p[-1] == '*')
5136 /* Mark all matching switches as valid. */
5137 --p;
5138 for (i = 0; i < n_switches; i++)
5139 if (!strncmp (switches[i].part1, filter, p - filter))
5140 switches[i].validated = 1;
5142 else
5144 /* Mark an exact matching switch as valid. */
5145 for (i = 0; i < n_switches; i++)
5147 if (!strncmp (switches[i].part1, filter, p - filter)
5148 && switches[i].part1[p - filter] == 0)
5149 switches[i].validated = 1;
5154 /* Check whether a particular argument was used. The first time we
5155 canonicalize the switches to keep only the ones we care about. */
5157 static int
5158 used_arg (p, len)
5159 char *p;
5160 int len;
5162 struct mswitchstr {
5163 char *str;
5164 char *replace;
5165 int len;
5166 int rep_len;
5169 static struct mswitchstr *mswitches;
5170 static int n_mswitches;
5171 int i, j;
5173 if (!mswitches)
5175 struct mswitchstr *matches;
5176 char *q;
5177 int cnt = 0;
5179 /* Break multilib_matches into the component strings of string and replacement
5180 string */
5181 for (q = multilib_matches; *q != '\0'; q++)
5182 if (*q == ';')
5183 cnt++;
5185 matches = (struct mswitchstr *) alloca ((sizeof (struct mswitchstr)) * cnt);
5186 i = 0;
5187 q = multilib_matches;
5188 while (*q != '\0')
5190 matches[i].str = q;
5191 while (*q != ' ')
5193 if (*q == '\0')
5194 abort ();
5195 q++;
5197 *q = '\0';
5198 matches[i].len = q - matches[i].str;
5200 matches[i].replace = ++q;
5201 while (*q != ';' && *q != '\0')
5203 if (*q == ' ')
5204 abort ();
5205 q++;
5207 matches[i].rep_len = q - matches[i].replace;
5208 i++;
5209 if (*q == ';')
5210 *q++ = '\0';
5211 else
5212 break;
5215 /* Now build a list of the replacement string for switches that we care
5216 about. Make sure we allocate at least one entry. This prevents
5217 xmalloc from calling fatal, and prevents us from re-executing this
5218 block of code. */
5219 mswitches
5220 = (struct mswitchstr *) xmalloc ((sizeof (struct mswitchstr))
5221 * (n_switches ? n_switches : 1));
5222 for (i = 0; i < n_switches; i++)
5224 int xlen = strlen (switches[i].part1);
5225 for (j = 0; j < cnt; j++)
5226 if (xlen == matches[j].len && ! strcmp (switches[i].part1, matches[j].str))
5228 mswitches[n_mswitches].str = matches[j].replace;
5229 mswitches[n_mswitches].len = matches[j].rep_len;
5230 mswitches[n_mswitches].replace = (char *)0;
5231 mswitches[n_mswitches].rep_len = 0;
5232 n_mswitches++;
5233 break;
5238 for (i = 0; i < n_mswitches; i++)
5239 if (len == mswitches[i].len && ! strncmp (p, mswitches[i].str, len))
5240 return 1;
5242 return 0;
5245 static int
5246 default_arg (p, len)
5247 char *p;
5248 int len;
5250 char *start, *end;
5251 int i;
5253 for (start = multilib_defaults; *start != '\0'; start = end+1)
5255 while (*start == ' ' || *start == '\t')
5256 start++;
5258 if (*start == '\0')
5259 break;
5261 for (end = start+1; *end != ' ' && *end != '\t' && *end != '\0'; end++)
5264 if ((end - start) == len && strncmp (p, start, len) == 0)
5265 return 1;
5267 if (*end == '\0')
5268 break;
5271 return 0;
5274 /* Work out the subdirectory to use based on the
5275 options. The format of multilib_select is a list of elements.
5276 Each element is a subdirectory name followed by a list of options
5277 followed by a semicolon. gcc will consider each line in turn. If
5278 none of the options beginning with an exclamation point are
5279 present, and all of the other options are present, that
5280 subdirectory will be used. */
5282 static void
5283 set_multilib_dir ()
5285 char *p = multilib_select;
5286 int this_path_len;
5287 char *this_path, *this_arg;
5288 int not_arg;
5289 int ok;
5291 while (*p != '\0')
5293 /* Ignore newlines. */
5294 if (*p == '\n')
5296 ++p;
5297 continue;
5300 /* Get the initial path. */
5301 this_path = p;
5302 while (*p != ' ')
5304 if (*p == '\0')
5305 abort ();
5306 ++p;
5308 this_path_len = p - this_path;
5310 /* Check the arguments. */
5311 ok = 1;
5312 ++p;
5313 while (*p != ';')
5315 if (*p == '\0')
5316 abort ();
5318 if (! ok)
5320 ++p;
5321 continue;
5324 this_arg = p;
5325 while (*p != ' ' && *p != ';')
5327 if (*p == '\0')
5328 abort ();
5329 ++p;
5332 if (*this_arg != '!')
5333 not_arg = 0;
5334 else
5336 not_arg = 1;
5337 ++this_arg;
5340 /* If this is a default argument, we can just ignore it.
5341 This is true even if this_arg begins with '!'. Beginning
5342 with '!' does not mean that this argument is necessarily
5343 inappropriate for this library: it merely means that
5344 there is a more specific library which uses this
5345 argument. If this argument is a default, we need not
5346 consider that more specific library. */
5347 if (! default_arg (this_arg, p - this_arg))
5349 ok = used_arg (this_arg, p - this_arg);
5350 if (not_arg)
5351 ok = ! ok;
5354 if (*p == ' ')
5355 ++p;
5358 if (ok)
5360 if (this_path_len != 1
5361 || this_path[0] != '.')
5363 multilib_dir = xmalloc (this_path_len + 1);
5364 strncpy (multilib_dir, this_path, this_path_len);
5365 multilib_dir[this_path_len] = '\0';
5367 break;
5370 ++p;
5374 /* Print out the multiple library subdirectory selection
5375 information. This prints out a series of lines. Each line looks
5376 like SUBDIRECTORY;@OPTION@OPTION, with as many options as is
5377 required. Only the desired options are printed out, the negative
5378 matches. The options are print without a leading dash. There are
5379 no spaces to make it easy to use the information in the shell.
5380 Each subdirectory is printed only once. This assumes the ordering
5381 generated by the genmultilib script. */
5383 static void
5384 print_multilib_info ()
5386 char *p = multilib_select;
5387 char *last_path = 0, *this_path;
5388 int skip;
5389 int last_path_len = 0;
5391 while (*p != '\0')
5393 /* Ignore newlines. */
5394 if (*p == '\n')
5396 ++p;
5397 continue;
5400 /* Get the initial path. */
5401 this_path = p;
5402 while (*p != ' ')
5404 if (*p == '\0')
5405 abort ();
5406 ++p;
5409 /* If this is a duplicate, skip it. */
5410 skip = (last_path != 0 && p - this_path == last_path_len
5411 && ! strncmp (last_path, this_path, last_path_len));
5413 last_path = this_path;
5414 last_path_len = p - this_path;
5416 /* If this directory requires any default arguments, we can skip
5417 it. We will already have printed a directory identical to
5418 this one which does not require that default argument. */
5419 if (! skip)
5421 char *q;
5423 q = p + 1;
5424 while (*q != ';')
5426 char *arg;
5428 if (*q == '\0')
5429 abort ();
5431 if (*q == '!')
5432 arg = NULL;
5433 else
5434 arg = q;
5436 while (*q != ' ' && *q != ';')
5438 if (*q == '\0')
5439 abort ();
5440 ++q;
5443 if (arg != NULL
5444 && default_arg (arg, q - arg))
5446 skip = 1;
5447 break;
5450 if (*q == ' ')
5451 ++q;
5455 if (! skip)
5457 char *p1;
5459 for (p1 = last_path; p1 < p; p1++)
5460 putchar (*p1);
5461 putchar (';');
5464 ++p;
5465 while (*p != ';')
5467 int use_arg;
5469 if (*p == '\0')
5470 abort ();
5472 if (skip)
5474 ++p;
5475 continue;
5478 use_arg = *p != '!';
5480 if (use_arg)
5481 putchar ('@');
5483 while (*p != ' ' && *p != ';')
5485 if (*p == '\0')
5486 abort ();
5487 if (use_arg)
5488 putchar (*p);
5489 ++p;
5492 if (*p == ' ')
5493 ++p;
5496 if (! skip)
5498 /* If there are extra options, print them now */
5499 if (multilib_extra && *multilib_extra)
5501 int print_at = TRUE;
5502 char *q;
5504 for (q = multilib_extra; *q != '\0'; q++)
5506 if (*q == ' ')
5507 print_at = TRUE;
5508 else
5510 if (print_at)
5511 putchar ('@');
5512 putchar (*q);
5513 print_at = FALSE;
5517 putchar ('\n');
5520 ++p;
5524 /* If a snapshot, warn the user that this version of gcc is for testing and
5525 developing only. If we can find a home directory, we can restrict the
5526 warning to once per day. Otherwise always issue it. */
5528 #define TIMESTAMP_FILE ".gcc-test-time"
5529 #define ONE_DAY (24*60*60)
5531 static void
5532 snapshot_warning ()
5534 char *home;
5535 int print_p = 1;
5537 /* Every function here but `time' is called elsewhere in this file,
5538 but we only can be sure we have it for Unix and the Windows systems,
5539 so conditionalize this on those.
5541 ??? This should use autoconf at some point. */
5543 #if defined(unix) || defined(__CYGWIN32__) || defined(_MINGW32__)
5545 home = getenv ("HOME");
5546 if (home != 0)
5548 char *file_name
5549 = (char *) alloca (strlen (home) + 1 + sizeof (TIMESTAMP_FILE));
5550 struct stat statbuf;
5551 time_t now = time (NULL);
5552 int s;
5554 sprintf (file_name, "%s/%s", home, TIMESTAMP_FILE);
5555 s = stat (file_name, &statbuf);
5556 if (s == 0
5557 && (statbuf.st_mtime + ONE_DAY > now))
5558 print_p = 0;
5559 else
5561 FILE *f = fopen (file_name, "w");
5563 if (f != 0)
5565 fputc ('\n', f);
5566 fclose (f);
5570 #endif
5572 if (print_p)
5574 notice ("*** This is a development snapshot of GCC.\n\
5575 *** It is not a reliable release, and the GCC developers\n\
5576 *** warn you not to use it for anything except to test it.\n");