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