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