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