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