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