home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Education Sampler 1992 [NeXTSTEP]
/
Education_1992_Sampler.iso
/
NeXT
/
GnuSource
/
cc-61.0.1
/
cc
/
gcc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-02
|
76KB
|
2,873 lines
/* Compiler driver program that can handle many languages.
Copyright (C) 1987,1989 Free Software Foundation, Inc.
This file is part of GNU CC.
GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
This paragraph is here to try to keep Sun CC from dying.
The number of chars here seems crucial!!!! */
/* This program is the user interface to the C compiler and possibly to
other compilers. It is used because compilation is a complicated procedure
which involves running several programs and passing temporary files between
them, forwarding the users switches to those programs selectively,
and deleting the temporary files at the end.
CC recognizes how to compile each input file by suffixes in the file names.
Once it knows which kind of compilation to perform, the procedure for
compilation is specified by a string called a "spec". */
#include <stdio.h>
#include <sys/types.h>
#include <ctype.h>
#include <signal.h>
#include <sys/file.h>
#include <sys/stat.h>
#include "config.h"
#include "obstack.h"
#include "gvarargs.h"
#ifdef USG
#ifndef R_OK
#define R_OK 4
#define W_OK 2
#define X_OK 1
#endif
#define vfork fork
#endif /* USG */
#define obstack_chunk_alloc xmalloc
#define obstack_chunk_free free
extern int xmalloc ();
extern void free ();
/* If a stage of compilation returns an exit status >= 1,
compilation of that file ceases. */
#define MIN_FATAL_STATUS 1
/* Flag indicating whether we should print the command and arguments */
int verbose_flag;
/* Nonzero means write "temp" files in source directory
and use the source file's name in them, and don't delete them. */
int save_temps_flag;
/* The compiler version specified with -V */
char *spec_version;
/* The target machine specified with -b. */
#ifdef NeXT
char *spec_machine = "m68k";
#else /* NeXT */
char *spec_machine = DEFAULT_TARGET_MACHINE;
#endif /* NeXT */
/* This is the obstack which we use to allocate many strings. */
struct obstack obstack;
extern char *version_string;
void set_spec ();
struct compiler *lookup_compiler ();
char *find_a_file ();
void add_prefix ();
char *skip_whitespace ();
void record_temp_file ();
char *handle_braces ();
char *save_string ();
char *concat ();
int do_spec ();
int do_spec_1 ();
char *find_file ();
void validate_switches ();
void validate_all_switches ();
void give_switch ();
void pfatal_with_name ();
void perror_with_name ();
void perror_exec ();
void fatal ();
void error ();
void fancy_abort ();
/* Specs are strings containing lines, each of which (if not blank)
is made up of a program name, and arguments separated by spaces.
The program name must be exact and start from root, since no path
is searched and it is unreliable to depend on the current working directory.
Redirection of input or output is not supported; the subprograms must
accept filenames saying what files to read and write.
In addition, the specs can contain %-sequences to substitute variable text
or for conditional text. Here is a table of all defined %-sequences.
Note that spaces are not generated automatically around the results of
expanding these sequences; therefore, you can concatenate them together
or with constant text in a single argument.
%% substitute one % into the program name or argument.
%i substitute the name of the input file being processed.
%b substitute the basename of the input file being processed.
This is the substring up to (and not including) the last period
and not including the directory.
%g substitute the temporary-file-name-base. This is a string chosen
once per compilation. Different temporary file names are made by
concatenation of constant strings on the end, as in `%g.s'.
%g also has the same effect of %d.
%d marks the argument containing or following the %d as a
temporary file name, so that that file will be deleted if CC exits
successfully. Unlike %g, this contributes no text to the argument.
%w marks the argument containing or following the %w as the
"output file" of this compilation. This puts the argument
into the sequence of arguments that %o will substitute later.
%W{...}
like %{...} but mark last argument supplied within
as a file to be deleted on failure.
%o substitutes the names of all the output files, with spaces
automatically placed around them. You should write spaces
around the %o as well or the results are undefined.
%o is for use in the specs for running the linker.
Input files whose names have no recognized suffix are not compiled
at all, but they are included among the output files, so they will
be linked.
%D substitutes the name of the dependency output file which can set by the
-dependency-file option to the cc driver. This file is passed to cpp
when the -MD option is used.
%p substitutes the standard macro predefinitions for the
current target machine. Use this when running cpp.
%P like %p, but puts `__' before and after the name of each macro.
(Except macros that already have __.)
This is for ANSI C.
%s current argument is the name of a library or startup file of some sort.
Search for that file in a standard list of directories
and substitute the full pathname found.
%eSTR Print STR as an error message. STR is terminated by a newline.
Use this when inconsistent options are detected.
%a process ASM_SPEC as a spec.
This allows config.h to specify part of the spec for running as.
%l process LINK_SPEC as a spec.
%L process LIB_SPEC as a spec.
%S process STARTFILE_SPEC as a spec. A capital S is actually used here.
%E process ENDFILE_SPEC as a spec. A capital E is actually used here.
%c process SIGNED_CHAR_SPEC as a spec.
%C process CPP_SPEC as a spec. A capital C is actually used here.
%1 process CC1_SPEC as a spec.
%2 process CC1PLUS_SPEC as a spec.
%{S} substitutes the -S switch, if that switch was given to CC.
If that switch was not specified, this substitutes nothing.
Here S is a metasyntactic variable.
%{S*} substitutes all the switches specified to CC whose names start
with -S. This is used for -o, -D, -I, etc; switches that take
arguments. CC considers `-o foo' as being one switch whose
name starts with `o'. %{o*} would substitute this text,
including the space; thus, two arguments would be generated.
%{S:X} substitutes X, but only if the -S switch was given to CC.
%{!S:X} substitutes X, but only if the -S switch was NOT given to CC.
%{|S:X} like %{S:X}, but if no S switch, substitute `-'.
%{|!S:X} like %{!S:X}, but if there is an S switch, substitute `-'.
%{.S:X} substitutes X, but only if processing a file with suffix S.
%{!.S:X} substitutes X, but only if NOT processing a file with suffix S.
The conditional text X in a %{S:X} or %{!S:X} construct may contain
other nested % constructs or spaces, or even newlines. They are
processed as usual, as described above.
The character | is used to indicate that a command should be piped to
the following command, but only if -pipe is specified.
Note that it is built into CC which switches take arguments and which
do not. You might think it would be useful to generalize this to
allow each compiler's spec to say which switches take arguments. But
this cannot be done in a consistent fashion. CC cannot even decide
which input files have been specified without knowing which switches
take arguments, and it must know which input files to compile in order
to tell which compilers to run.
CC also knows implicitly that arguments starting in `-l' are to be
treated as compiler output files, and passed to the linker in their
proper position among the other output files. */
/* Define the macros used for specs %a, %l, %L, %S, %c, %C, %1. */
/* config.h can define ASM_SPEC to provide extra args to the assembler
or extra switch-translations. */
#ifndef ASM_SPEC
#define ASM_SPEC ""
#endif
/* config.h can define CPP_SPEC to provide extra args to the C preprocessor
or extra switch-translations. */
#ifndef CPP_SPEC
#define CPP_SPEC ""
#endif
/* config.h can define CC1_SPEC to provide extra args to cc1 and cc1plus
or extra switch-translations. */
#ifndef CC1_SPEC
#define CC1_SPEC ""
#endif
/* config.h can define CC1PLUS_SPEC to provide extra args to cc1plus
or extra switch-translations. */
#ifndef CC1PLUS_SPEC
#define CC1PLUS_SPEC ""
#endif
/* config.h can define LINK_SPEC to provide extra args to the linker
or extra switch-translations. */
#ifndef LINK_SPEC
#define LINK_SPEC ""
#endif
/* config.h can define LIB_SPEC to override the default libraries. */
#ifndef LIB_SPEC
#define LIB_SPEC "%{g:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"
#endif
/* config.h can define STARTFILE_SPEC to override the default crt0 files. */
#ifndef STARTFILE_SPEC
#define STARTFILE_SPEC \
"%{pg:gcrt0.o%s}%{!pg:%{p:mcrt0.o%s}%{!p:crt0.o%s}}"
#endif
/* config.h can define ENDFILE_SPEC to override the default crtn files. */
#ifndef ENDFILE_SPEC
#define ENDFILE_SPEC ""
#endif
/* This spec is used for telling cpp whether char is signed or not. */
#define SIGNED_CHAR_SPEC \
(DEFAULT_SIGNED_CHAR ? "%{funsigned-char:-D__CHAR_UNSIGNED__}" \
: "%{!fsigned-char:-D__CHAR_UNSIGNED__}")
char *cpp_spec = CPP_SPEC;
char *cpp_predefines = CPP_PREDEFINES;
char *cc1_spec = CC1_SPEC;
char *cc1plus_spec = CC1PLUS_SPEC;
char *signed_char_spec = SIGNED_CHAR_SPEC;
char *asm_spec = ASM_SPEC;
char *link_spec = LINK_SPEC;
char *lib_spec = LIB_SPEC;
char *startfile_spec = STARTFILE_SPEC;
/* This defines which switch letters take arguments. */
#ifndef SWITCH_TAKES_ARG
#define SWITCH_TAKES_ARG(CHAR) \
((CHAR) == 'D' || (CHAR) == 'U' || (CHAR) == 'o' \
|| (CHAR) == 'e' || (CHAR) == 'T' || (CHAR) == 'u' \
|| (CHAR) == 'I' || (CHAR) == 'Y' || (CHAR) == 'm' \
|| (CHAR) == 'L' || (CHAR) == 'i' || (CHAR) == 'A')
#endif
/* This defines which multi-letter switches take arguments. */
#ifndef WORD_SWITCH_TAKES_ARG
#define WORD_SWITCH_TAKES_ARG(STR) (!strcmp (STR, "Tdata"))
#endif
/* Record the mapping from file suffixes for compilation specs. */
struct compiler
{
char *suffix; /* Use this compiler for input files
whose names end in this suffix. */
char *spec; /* To use this compiler, pass this spec
to do_spec. */
};
/* Pointer to a vector of `struct compiler' that gives the spec for
compiling a file, based on its suffix.
A file that does not end in any of these suffixes will be passed
unchanged to the loader and nothing else will be done to it.
An entry containing two 0s is used to terminate the vector.
If multiple entries match a file, the last matching one is used. */
struct compiler *compilers;
/* Number of entries in `compilers', not counting the null terminator. */
int n_compilers;
/* Here is the spec for running the linker, after compiling all files. */
char *link_command_spec = "\
%{!c:%{!M:%{!MM:%{!E:%{!precomp:%{!S:ld %l %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} \
%{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\
%{y*} %{!A:%{!nostdlib:%S}} \
%{L*} %o %{!nostdlib:%L %{!A:%E}}\n }}}}}}";
/* The default list of file name suffixes and their compilation specs. */
struct compiler default_compilers[] =
{
{".c", "@c"},
{"@c",
"%{E:cpp} %{!E:%{traditional:cpp}}\
%{!E:%{!traditional:%{traditional-cpp:cpp}}}\
%{!E:%{!traditional:%{!traditional-cpp: cpp-precomp -smart}}}\
%{!ObjC:-lang-c} %{ObjC:-lang-objc}\
%{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D}\
-undef %{ObjC:-D__OBJC__} -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
-D__GNU__ %{precomp} %{no-precomp}\
%{!undef:%{!ansi:%p} %P} %{trigraphs}\
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-traditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%{Wprecomp} %{Wno-precomp}\
%i %{!M:%{!MM:%{!E:%{!precomp:%{!pipe:%g.cpp}}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}}%{precomp:%W{o*}%{!o*:-o %b.p}} |\n\
%{!M:%{!MM:%{!E:%{!precomp:cc1obj %{!pipe:%g.cpp} %1 \
%{!Q:-quiet} -dumpbase %b.c %{Y*} %{d*} %{m*} %{a}\
%{g} %{O} %{O1} %{O2} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
%{v:-version} %{pg:-p} %{p} %{f*}\
%{ObjC:-lang-objc %{gen-decls}}\
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
%{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}}}"},
{"-",
"%{E:cpp -lang-c %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D}\
-D__GNU__ %{precomp} %{no-precomp}\
-undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
%{!undef:%{!ansi:%p} %P} %{trigraphs}\
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-traditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%{Wprecomp} %{Wno-precomp}\
%i %W{o*}}\
%{!E:%e-E required when input is from standard input}"},
{".m", "@objective-c"},
{"@objective-c",
"%{E:cpp} %{!E:%{traditional:cpp}}\
%{!E:%{!traditional:%{traditional-cpp:cpp}}}\
%{!E:%{!traditional:%{!traditional-cpp: cpp-precomp -smart}}} -lang-objc\
%{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D}\
-undef -D__OBJC__ -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
-D__GNU__ %{precomp} %{no-precomp}\
%{!undef:%{!ansi:%p} %P} %{trigraphs}\
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-traditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%{Wprecomp} %{Wno-precomp}\
%i %{!M:%{!MM:%{!E:%{!precomp:%{!pipe:%g.cpp}}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}}%{precomp:%W{o*}%{!o*:-o %b.p}} |\n\
%{!M:%{!MM:%{!E:%{!precomp:cc1obj %{!pipe:%g.cpp} %1 \
%{!Q:-quiet} -dumpbase %b.m %{Y*} %{d*} %{m*} %{a}\
%{g} %{O} %{O1} %{O2} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
%{v:-version} %{pg:-p} %{p} %{f*}\
-lang-objc %{gen-decls}\
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
%{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}}}"},
{".h", "@c-header"},
{"@c-header",
"%{!E:%{!precomp:%eCompilation of header file requested}} \
%{E:cpp} %{!E:%{traditional:cpp}}\
%{!E:%{!traditional:%{traditional-cpp:cpp}}}\
%{!E:%{!traditional:%{!traditional-cpp: cpp-precomp -smart}}}\
%{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D} \
-D__GNU__ %{precomp} %{no-precomp}\
-undef -D__GNUC__=2 %{ansi:-trigraphs -$ -D__STRICT_ANSI__}\
%{!undef:%{!ansi:%p} %P} %{trigraphs}\
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-traditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%{Wprecomp} %{Wno-precomp}\
%i %W{o*}%{precomp:%{!o*:-o %b.p }}"},
{".cc", "@c++"},
{".cxx", "@c++"},
{".C", "@c++"},
{"@c++",
"cpp -lang-c++ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C++ does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D} \
-D__GNU__ %{precomp:%ePrecompilation of C++ not supported}\
-undef %{ObjC++:-D__OBJC__} -D__GNUC__=2 -D__GNUG__ -D__cplusplus %{!undef:%p %P}\
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-traditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%i %{!M:%{!MM:%{!E:%{!pipe:%g.cpp}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
%{!M:%{!MM:%{!E:cc1plus %{!pipe:%g.cpp} %1 %2\
%{!Q:-quiet} -dumpbase %b.cc %{Y*} %{d*} %{m*} %{a}\
%{g*} %{O} %{O1} %{O2} %{W*} %{w} %{pedantic*} %{traditional}\
%{v:-version} %{pg:-p} %{p} %{f*}\
%{ObjC++:-lang-objc %{gen-decls}}\
%{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}}\
%{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}}"},
{".cpp", "@cpp-output"},
{".i", "@cpp-output"},
{"@cpp-output",
"cc1obj %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{a}\
%{g} %{O} %{O1} %{O2} %{W*} %{w} %{pedantic*} %{ansi} %{traditional}\
%{v:-version} %{pg:-p} %{p} %{f*}\
%{S:%W{o*}%{!o*:-o %b.s}}%{!S:-o %{|!pipe:%g.s}} |\n\
%{!S:as %{R} %{j} %{J} %{h} %{d2} %a %{gg:-G %g.sym}\
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %{!pipe:%g.s}\n }"},
{".s", "@assembler-with-cpp"},
{"@assembler",
"%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o} %i\n }"},
{".S", "@assembler-with-cpp"},
{"@assembler-with-cpp",
"cpp -lang-asm %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{i*} %{P}\
%{C:%{!E:%eGNU C does not support -C without using -E}}\
%{M} %{MM} %{MD:-MD %D} %{MMD:-MMD %D} %{trigraphs} \
-D__GNU__ %{precomp:%ePrecompilation of assembler not supported}\
-undef -$ %{!undef:%p %P} -D__ASSEMBLER__ \
%c %{O:-D__OPTIMIZE__} %{O1:-D__OPTIMIZE__} %{O2:-D__OPTIMIZE__} \
%{traditional} %{ftraditional:-ftraditional}\
%{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %{pedantic*} %{H} %{d*} %C\
%i %{!M:%{!MM:%{!E:%{!pipe:%g.s}}}}%{E:%W{o*}}%{M:%W{o*}}%{MM:%W{o*}} |\n\
%{!M:%{!MM:%{!E:%{!S:as %{R} %{j} %{J} %{h} %{d2} %a \
%{c:%W{o*}%{!o*:-o %w%b.o}}%{!c:-o %d%w%b.o}\
%{!pipe:%g.s}\n }}}}"},
/* Mark end of table */
{0, 0}
};
/* Number of elements in default_compilers, not counting the terminator. */
int n_default_compilers
= (sizeof default_compilers / sizeof (struct compiler)) - 1;
/* Read compilation specs from a file named FILENAME,
replacing the default ones.
A suffix which starts with `*' is a definition for
one of the machine-specific sub-specs. The "suffix" should be
*asm, *cc1, *cpp, *link, *startfile, *signed_char, etc.
The corresponding spec is stored in asm_spec, etc.,
rather than in the `compilers' vector.
Anything invalid in the file is a fatal error. */
void
read_specs (filename)
char *filename;
{
int desc;
struct stat statbuf;
char *buffer;
register char *p;
if (verbose_flag)
fprintf (stderr, "Reading specs from %s\n", filename);
/* Open and stat the file. */
desc = open (filename, 0, 0);
if (desc < 0)
pfatal_with_name (filename);
if (stat (filename, &statbuf) < 0)
pfatal_with_name (filename);
/* Read contents of file into BUFFER. */
buffer = (char *) xmalloc (statbuf.st_size + 1);
read (desc, buffer, statbuf.st_size);
buffer[statbuf.st_size] = 0;
close (desc);
/* Scan BUFFER for specs, putting them in the vector. */
p = buffer;
while (1)
{
char *suffix;
char *spec;
char *in, *out, *p1, *p2;
/* Advance P in BUFFER to the next nonblank nocomment line. */
p = skip_whitespace (p);
if (*p == 0)
break;
/* Find the colon that should end the suffix. */
p1 = p;
while (*p1 && *p1 != ':' && *p1 != '\n') p1++;
/* The colon shouldn't be missing. */
if (*p1 != ':')
fatal ("specs file malformed after %d characters", p1 - buffer);
/* Skip back over trailing whitespace. */
p2 = p1;
while (p2 > buffer && (p2[-1] == ' ' || p2[-1] == '\t')) p2--;
/* Copy the suffix to a string. */
suffix = save_string (p, p2 - p);
/* Find the next line. */
p = skip_whitespace (p1 + 1);
if (p[1] == 0)
fatal ("specs file malformed after %d characters", p - buffer);
p1 = p;
/* Find next blank line. */
while (*p1 && !(*p1 == '\n' && p1[1] == '\n')) p1++;
/* Specs end at the blank line and do not include the newline. */
spec = save_string (p, p1 - p);
p = p1;
/* Delete backslash-newline sequences from the spec. */
in = spec;
out = spec;
while (*in != 0)
{
if (in[0] == '\\' && in[1] == '\n')
in += 2;
else if (in[0] == '#')
{
while (*in && *in != '\n') in++;
if (*in) in++;
}
else
*out++ = *in++;
}
*out = 0;
if (suffix[0] == '*')
{
if (! strcmp (suffix, "*link_command"))
link_command_spec = spec;
else
set_spec (suffix + 1, spec);
}
else
{
/* Add this pair to the vector. */
compilers
= ((struct compiler *)
xrealloc (compilers, (n_compilers + 2) * sizeof (struct compiler)));
compilers[n_compilers].suffix = suffix;
compilers[n_compilers].spec = spec;
n_compilers++;
}
if (*suffix == 0)
link_command_spec = spec;
}
if (link_command_spec == 0)
fatal ("spec file has no spec for linking");
}
char *
skip_whitespace (p)
char *p;
{
while (1)
{
/* A fully-blank line is a delimiter in the SPEC file and shouldn't
be considered whitespace. */
if (p[0] == '\n' && p[1] == '\n' && p[2] == '\n')
return p + 1;
else if (*p == '\n' || *p == ' ' || *p == '\t')
p++;
else if (*p == '#')
{
while (*p != '\n') p++;
p++;
}
else
break;
}
return p;
}
/* Structure to keep track of the specs that have been defined so far. These
are accessed using %Sspecname in a compiler or link spec. */
struct spec_list
{
char *name; /* Name of the spec. */
char *spec; /* The spec itself. */
struct spec_list *next; /* Next spec in linked list. */
};
/* List of specs that have been defined so far. */
struct spec_list *specs = (struct spec_list *) 0;
/* Change the value of spec NAME to SPEC. If SPEC is empty, then the spec is
removed; If the spec starts with a + then SPEC is added to the end of the
current spec. */
void
set_spec (name, spec)
char *name;
char *spec;
{
struct spec_list *sl;
char *old_spec;
/* See if the spec already exists */
for (sl = specs; sl; sl = sl->next)
if (strcmp (sl->name, name) == 0)
break;
if (!sl)
{
/* Not found - make it */
sl = (struct spec_list *) xmalloc (sizeof (struct spec_list));
sl->name = save_string (name, strlen (name));
sl->spec = save_string ("", 0);
sl->next = specs;
specs = sl;
}
old_spec = sl->spec;
if (name && spec[0] == '+' && isspace (spec[1]))
sl->spec = concat (name, spec + 1, "");
else
sl->spec = save_string (spec, strlen (spec));
if (! strcmp (name, "asm"))
asm_spec = sl->spec;
else if (! strcmp (name, "cc1"))
cc1_spec = sl->spec;
else if (! strcmp (name, "cc1plus"))
cc1plus_spec = sl->spec;
else if (! strcmp (name, "cpp"))
cpp_spec = sl->spec;
else if (! strcmp (name, "signed_char"))
signed_char_spec = sl->spec;
else if (! strcmp (name, "link"))
link_spec = sl->spec;
else if (! strcmp (name, "lib"))
lib_spec = sl->spec;
else if (! strcmp (name, "startfile"))
startfile_spec = sl->spec;
else if (! strcmp (name, "predefines"))
cpp_predefines = sl->spec;
/* Free the old spec */
if (old_spec)
free (old_spec);
}
/* Accumulate a command (program name and args), and run it. */
/* Vector of pointers to arguments in the current line of specifications. */
char **argbuf;
/* Number of elements allocated in argbuf. */
int argbuf_length;
/* Number of elements in argbuf currently in use (containing args). */
int argbuf_index;
/* Number of commands executed so far. */
int execution_count;
/* Name with which this program was invoked. */
char *programname;
/* Structures to keep track of prefixes to try when looking for files. */
struct prefix_list
{
char *prefix; /* String to prepend to the path. */
struct prefix_list *next; /* Next in linked list. */
int require_machine_suffix; /* Don't use without machine_suffix. */
int *used_flag_ptr; /* 1 if a file was found with this prefix. */
};
struct path_prefix
{
struct prefix_list *plist; /* List of prefixes to try */
int max_len; /* Max length of a prefix in PLIST */
char *name; /* Name of this list (used in config stuff) */
};
/* List of prefixes to try when looking for executables. */
struct path_prefix exec_prefix = { 0, 0, "exec" };
/* List of prefixes to try when looking for startup (crt0) files. */
struct path_prefix startfile_prefix = { 0, 0, "startfile" };
/* Suffix to attach to directories searched for commands. */
char *machine_suffix = 0;
/* Nonzero means don't bypass the machine_suffix. */
int machine_explicit;
/* Default prefixes to attach to command names. */
#ifndef STANDARD_EXEC_PREFIX
#define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc/"
#endif /* !defined STANDARD_EXEC_PREFIX */
#ifdef MD_EXEC_PREFIX
char *md_exec_prefix = MD_EXEC_PREFIX;
#endif
char *standard_exec_prefix = STANDARD_EXEC_PREFIX;
#ifdef NeXT
char *standard_exec_prefix_1 = "/usr/local/lib/";
char *standard_exec_prefix_2 = "/bin/";
#else /* NeXT */
char *standard_exec_prefix_1 = "/usr/lib/gcc/";
#endif /* NeXT */
#ifndef STANDARD_STARTFILE_PREFIX
#define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"
#endif /* !defined STANDARD_STARTFILE_PREFIX */
#ifdef MD_STARTFILE_PREFIX
char *md_startfile_prefix = MD_STARTFILE_PREFIX;
#endif
char *standard_startfile_prefix = STANDARD_STARTFILE_PREFIX;
char *standard_startfile_prefix_1 = "/lib/";
char *standard_startfile_prefix_2 = "/usr/lib/";
/* Clear out the vector of arguments (after a command is executed). */
void
clear_args ()
{
argbuf_index = 0;
}
/* Add one argument to the vector at the end.
This is done when a space is seen or at the end of the line.
If DELETE_ALWAYS is nonzero, the arg is a filename
and the file should be deleted eventually.
If DELETE_FAILURE is nonzero, the arg is a filename
and the file should be deleted if this compilation fails. */
void
store_arg (arg, delete_always, delete_failure)
char *arg;
int delete_always, delete_failure;
{
if (argbuf_index + 1 == argbuf_length)
{
argbuf = (char **) xrealloc (argbuf, (argbuf_length *= 2) * sizeof (char *));
}
argbuf[argbuf_index++] = arg;
argbuf[argbuf_index] = 0;
if (delete_always || delete_failure)
record_temp_file (arg, delete_always, delete_failure);
}
/* Record the names of temporary files we tell compilers to write,
and delete them at the end of the run. */
/* This is the common prefix we use to make temp file names.
It is chosen once for each run of this program.
It is substituted into a spec by %g.
Thus, all temp file names contain this prefix.
In practice, all temp file names start with this prefix.
This prefix comes from the envvar TMPDIR if it is defined;
otherwise, from the P_tmpdir macro if that is defined;
otherwise, in /usr/tmp or /tmp. */
char *temp_filename;
/* Length of the prefix. */
int temp_filename_length;
/* Define the list of temporary files to delete. */
struct temp_file
{
char *name;
struct temp_file *next;
};
/* Queue of files to delete on success or failure of compilation. */
struct temp_file *always_delete_queue;
/* Queue of files to delete on failure of compilation. */
struct temp_file *failure_delete_queue;
/* Record FILENAME as a file to be deleted automatically.
ALWAYS_DELETE nonzero means delete it if all compilation succeeds;
otherwise delete it in any case.
FAIL_DELETE nonzero means delete it if a compilation step fails;
otherwise delete it in any case. */
void
record_temp_file (filename, always_delete, fail_delete)
char *filename;
int always_delete;
int fail_delete;
{
register char *name;
name = (char *) xmalloc (strlen (filename) + 1);
strcpy (name, filename);
if (always_delete)
{
register struct temp_file *temp;
for (temp = always_delete_queue; temp; temp = temp->next)
if (! strcmp (name, temp->name))
goto already1;
temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
temp->next = always_delete_queue;
temp->name = name;
always_delete_queue = temp;
already1:;
}
if (fail_delete)
{
register struct temp_file *temp;
for (temp = failure_delete_queue; temp; temp = temp->next)
if (! strcmp (name, temp->name))
goto already2;
temp = (struct temp_file *) xmalloc (sizeof (struct temp_file));
temp->next = failure_delete_queue;
temp->name = name;
failure_delete_queue = temp;
already2:;
}
}
/* Delete all the temporary files whose names we previously recorded. */
void
delete_temp_files ()
{
register struct temp_file *temp;
for (temp = always_delete_queue; temp; temp = temp->next)
{
#ifdef DEBUG
int i;
printf ("Delete %s? (y or n) ", temp->name);
fflush (stdout);
i = getchar ();
if (i != '\n')
while (getchar () != '\n') ;
if (i == 'y' || i == 'Y')
#endif /* DEBUG */
{
struct stat st;
if (stat (temp->name, &st) >= 0)
{
/* Delete only ordinary files. */
if ((st.st_mode & S_IFMT) == S_IFREG)
if (unlink (temp->name) < 0)
if (verbose_flag)
perror_with_name (temp->name);
}
}
}
always_delete_queue = 0;
}
/* Delete all the files to be deleted on error. */
void
delete_failure_queue ()
{
register struct temp_file *temp;
for (temp = failure_delete_queue; temp; temp = temp->next)
{
#ifdef DEBUG
int i;
printf ("Delete %s? (y or n) ", temp->name);
fflush (stdout);
i = getchar ();
if (i != '\n')
while (getchar () != '\n') ;
if (i == 'y' || i == 'Y')
#endif /* DEBUG */
{
if (unlink (temp->name) < 0)
if (verbose_flag)
perror_with_name (temp->name);
}
}
}
void
clear_failure_queue ()
{
failure_delete_queue = 0;
}
/* Compute a string to use as the base of all temporary file names.
It is substituted for %g. */
void
choose_temp_base ()
{
extern char *getenv ();
char *base = getenv ("TMPDIR");
int len;
if (base == (char *)0)
{
#ifdef P_tmpdir
if (access (P_tmpdir, R_OK | W_OK) == 0)
base = P_tmpdir;
#endif
if (base == (char *)0)
{
if (access ("/usr/tmp", R_OK | W_OK) == 0)
base = "/usr/tmp/";
else
base = "/tmp/";
}
}
len = strlen (base);
temp_filename = (char *) xmalloc (len + sizeof("/ccXXXXXX"));
strcpy (temp_filename, base);
if (len > 0 && temp_filename[len-1] != '/')
temp_filename[len++] = '/';
strcpy (temp_filename + len, "ccXXXXXX");
mktemp (temp_filename);
temp_filename_length = strlen (temp_filename);
}
/* Search for NAME using the prefix list PREFIXES. MODE is passed to
access to check permissions.
Return 0 if not found, otherwise return its name, allocated with malloc. */
char *
find_a_file (pprefix, name, mode)
struct path_prefix *pprefix;
char *name;
int mode;
{
char *temp;
struct prefix_list *pl;
int len = pprefix->max_len + strlen (name) + 1;
if (machine_suffix)
len += strlen (machine_suffix);
temp = (char *) xmalloc (len);
/* Determine the filename to execute (special case for absolute paths). */
if (*name == '/')
{
if (access (name, mode))
{
strcpy (temp, name);
return temp;
}
}
else
for (pl = pprefix->plist; pl; pl = pl->next)
{
if (machine_suffix)
{
strcpy (temp, pl->prefix);
strcat (temp, machine_suffix);
strcat (temp, name);
if (access (temp, mode) == 0)
{
if (pl->used_flag_ptr != 0)
*pl->used_flag_ptr = 1;
return temp;
}
}
/* Certain prefixes can't be used without the machine suffix
when the machine or version is explicitly specified. */
if (!machine_explicit || !pl->require_machine_suffix)
{
strcpy (temp, pl->prefix);
strcat (temp, name);
if (access (temp, mode) == 0)
{
if (pl->used_flag_ptr != 0)
*pl->used_flag_ptr = 1;
return temp;
}
}
}
free (temp);
return 0;
}
/* Add an entry for PREFIX in PLIST. If FIRST is set, it goes
at the start of the list, otherwise it goes at the end.
If WARN is nonzero, we will warn if no file is found
through this prefix. WARN should point to an int
which will be set to 1 if this entry is used. */
void
add_prefix (pprefix, prefix, first, require_machine_suffix, warn)
struct path_prefix *pprefix;
char *prefix;
int first;
int require_machine_suffix;
int *warn;
{
struct prefix_list *pl, **prev;
int len;
if (!first && pprefix->plist)
{
for (pl = pprefix->plist; pl->next; pl = pl->next)
;
prev = &pl->next;
}
else
prev = &pprefix->plist;
/* Keep track of the longest prefix */
len = strlen (prefix);
if (len > pprefix->max_len)
pprefix->max_len = len;
pl = (struct prefix_list *) xmalloc (sizeof (struct prefix_list));
pl->prefix = save_string (prefix, len);
pl->require_machine_suffix = require_machine_suffix;
pl->used_flag_ptr = warn;
if (warn)
*warn = 0;
if (*prev)
pl->next = *prev;
else
pl->next = (struct prefix_list *) 0;
*prev = pl;
}
/* Print warnings for any prefixes in the list PPREFIX that were not used. */
void
unused_prefix_warnings (pprefix)
struct path_prefix *pprefix;
{
struct prefix_list *pl = pprefix->plist;
while (pl)
{
if (pl->used_flag_ptr != 0 && !*pl->used_flag_ptr)
{
error ("file path prefix `%s' never used",
pl->prefix);
/* Prevent duplicate warnings. */
*pl->used_flag_ptr = 1;
}
pl = pl->next;
}
}
/* Get rid of all prefixes built up so far in *PLISTP. */
void
free_path_prefix (pprefix)
struct path_prefix *pprefix;
{
struct prefix_list *pl = pprefix->plist;
struct prefix_list *temp;
while (pl)
{
temp = pl;
pl = pl->next;
free (temp->prefix);
free ((char *) temp);
}
pprefix->plist = (struct prefix_list *) 0;
}
/* stdin file number. */
#define STDIN_FILE_NO 0
/* stdout file number. */
#define STDOUT_FILE_NO 1
/* value of `pipe': port index for reading. */
#define READ_PORT 0
/* value of `pipe': port index for writing. */
#define WRITE_PORT 1
/* Pipe waiting from last process, to be used as input for the next one.
Value is STDIN_FILE_NO if no pipe is waiting
(i.e. the next command is the first of a group). */
int last_pipe_input;
/* Fork one piped subcommand. FUNC is the system call to use
(either execv or execvp). ARGV is the arg vector to use.
NOT_LAST is nonzero if this is not the last subcommand
(i.e. its output should be piped to the next one.) */
static int
pexecute (func, program, argv, not_last)
char *program;
int (*func)();
char *argv[];
int not_last;
{
int pid;
int pdes[2];
int input_desc = last_pipe_input;
int output_desc = STDOUT_FILE_NO;
int retries, sleep_interval;
/* If this isn't the last process, make a pipe for its output,
and record it as waiting to be the input to the next process. */
if (not_last)
{
if (pipe (pdes) < 0)
pfatal_with_name ("pipe");
output_desc = pdes[WRITE_PORT];
last_pipe_input = pdes[READ_PORT];
}
else
last_pipe_input = STDIN_FILE_NO;
/* Fork a subprocess; wait and retry if it fails. */
sleep_interval = 1;
for (retries = 0; retries < 4; retries++)
{
pid = vfork ();
if (pid >= 0)
break;
sleep (sleep_interval);
sleep_interval *= 2;
}
switch (pid)
{
case -1:
pfatal_with_name ("vfork");
break;
case 0: /* child */
/* Move the input and output pipes into place, if nec. */
if (input_desc != STDIN_FILE_NO)
{
close (STDIN_FILE_NO);
dup (input_desc);
close (input_desc);
}
if (output_desc != STDOUT_FILE_NO)
{
close (STDOUT_FILE_NO);
dup (output_desc);
close (output_desc);
}
/* Close the parent's descs that aren't wanted here. */
if (last_pipe_input != STDIN_FILE_NO)
close (last_pipe_input);
/* Exec the program. */
(*func) (program, argv);
perror_exec (program);
exit (-1);
/* NOTREACHED */
default:
/* In the parent, after forking.
Close the descriptors that we made for this child. */
if (input_desc != STDIN_FILE_NO)
close (input_desc);
if (output_desc != STDOUT_FILE_NO)
close (output_desc);
/* Return child's process number. */
return pid;
}
}
/* Execute the command specified by the arguments on the current line of spec.
When using pipes, this includes several piped-together commands
with `|' between them.
Return 0 if successful, -1 if failed. */
int
execute ()
{
int i;
int n_commands; /* # of command. */
char *string;
struct command
{
char *prog; /* program name. */
char **argv; /* vector of args. */
int pid; /* pid of process for this command. */
};
struct command *commands; /* each command buffer with above info. */
/* Count # of piped commands. */
for (n_commands = 1, i = 0; i < argbuf_index; i++)
if (strcmp (argbuf[i], "|") == 0)
n_commands++;
/* Get storage for each command. */
commands
= (struct command *) alloca (n_commands * sizeof (struct command));
/* Split argbuf into its separate piped processes,
and record info about each one.
Also search for the programs that are to be run. */
commands[0].prog = argbuf[0]; /* first command. */
commands[0].argv = &argbuf[0];
string = find_a_file (&exec_prefix, commands[0].prog, X_OK);
if (string)
commands[0].argv[0] = string;
for (n_commands = 1, i = 0; i < argbuf_index; i++)
if (strcmp (argbuf[i], "|") == 0)
{ /* each command. */
argbuf[i] = 0; /* termination of command args. */
commands[n_commands].prog = argbuf[i + 1];
commands[n_commands].argv = &argbuf[i + 1];
string = find_a_file (&exec_prefix, commands[n_commands].prog, X_OK);
if (string)
commands[n_commands].argv[0] = string;
n_commands++;
}
argbuf[argbuf_index] = 0;
/* If -v, print what we are about to do, and maybe query. */
if (verbose_flag)
{
/* Print each piped command as a separate line. */
for (i = 0; i < n_commands ; i++)
{
char **j;
for (j = commands[i].argv; *j; j++)
fprintf (stderr, " %s", *j);
/* Print a pipe symbol after all but the last command. */
if (i + 1 != n_commands)
fprintf (stderr, " |");
fprintf (stderr, "\n");
}
fflush (stderr);
#ifdef DEBUG
fprintf (stderr, "\nGo ahead? (y or n) ");
fflush (stderr);
i = getchar ();
if (i != '\n')
while (getchar () != '\n') ;
if (i != 'y' && i != 'Y')
return 0;
#endif /* DEBUG */
}
/* Run each piped subprocess. */
last_pipe_input = STDIN_FILE_NO;
for (i = 0; i < n_commands; i++)
{
extern int execv(), execvp();
char *string = commands[i].argv[0];
commands[i].pid = pexecute ((string != commands[i].prog ? execv : execvp),
string, commands[i].argv,
i + 1 < n_commands);
if (string != commands[i].prog)
free (string);
}
execution_count++;
/* Wait for all the subprocesses to finish.
We don't care what order they finish in;
we know that N_COMMANDS waits will get them all. */
{
int ret_code = 0;
for (i = 0; i < n_commands; i++)
{
int status;
int pid;
char *prog;
pid = wait (&status);
if (pid < 0)
abort ();
if (status != 0)
{
int j;
for (j = 0; j < n_commands; j++)
if (commands[j].pid == pid)
prog = commands[j].prog;
if ((status & 0x7F) != 0)
fatal ("Internal compiler error: program %s got fatal signal %d",
prog, (status & 0x7F));
if (((status & 0xFF00) >> 8) >= MIN_FATAL_STATUS)
ret_code = -1;
}
}
return ret_code;
}
}
/* Find all the switches given to us
and make a vector describing them.
The elements of the vector a strings, one per switch given.
If a switch uses following arguments, then the `part1' field
is the switch itself and the `args' field
is a null-terminated vector containing the following arguments.
The `valid' field is nonzero if any spec has looked at this switch;
if it remains zero at the end of the run, it must be meaningless. */
struct switchstr
{
char *part1;
char **args;
int valid;
};
struct switchstr *switches;
int n_switches;
struct infile
{
char *name;
char *language;
};
/* Also a vector of input files specified. */
struct infile *infiles;
int n_infiles;
/* And a vector of corresponding output files is made up later. */
char **outfiles;
#ifdef NeXT
/* The dependency output file (specified with -dependency-file) */
char *dependency_output_file = NULL;
static char *default_language = NULL;
#endif /* NeXT */
/* Create the vector `switches' and its contents.
Store its length in `n_switches'. */
void
process_command (argc, argv)
int argc;
char **argv;
{
extern char *getenv ();
register int i;
char *temp;
char *spec_lang = 0;
int last_language_n_infiles;
n_switches = 0;
n_infiles = 0;
spec_version = version_string;
/* Set up the default search paths. */
temp = getenv ("GCC_EXEC_PREFIX");
if (temp)
{
add_prefix (&exec_prefix, temp, 0, 0, 0);
add_prefix (&startfile_prefix, temp, 0, 0, 0);
}
/* This must be set before we go looking for config files. */
#ifdef MD_EXEC_PREFIX
add_prefix (&exec_prefix, md_exec_prefix, 0, 1, 0);
add_prefix (&startfile_prefix, md_exec_prefix, 0, 1, 0);
#endif
#ifdef MD_STARTFILE_PREFIX
add_prefix (&startfile_prefix, md_startfile_prefix, 0, 1, 0);
#endif
add_prefix (&exec_prefix, standard_exec_prefix, 0, 1, 0);
add_prefix (&exec_prefix, standard_exec_prefix_1, 0, 1, 0);
#ifdef NeXT
add_prefix (&exec_prefix, standard_exec_prefix_2, 0, 1, 0);
#endif /* NeXT */
add_prefix (&startfile_prefix, standard_exec_prefix, 0, 1, 0);
add_prefix (&startfile_prefix, standard_exec_prefix_1, 0, 1, 0);
add_prefix (&startfile_prefix, standard_startfile_prefix, 0, 0, 0);
add_prefix (&startfile_prefix, standard_startfile_prefix_1, 0, 0, 0);
add_prefix (&startfile_prefix, standard_startfile_prefix_2, 0, 0, 0);
add_prefix (&startfile_prefix, "./", 0, 1, 0);
/* Scan argv twice. Here, the first time, just count how many switches
there will be in their vector, and how many input files in theirs.
Here we also parse the switches that cc itself uses (e.g. -v). */
for (i = 1; i < argc; i++)
{
if (! strcmp (argv[i], "-dumpspecs"))
{
printf ("*asm:\n%s\n\n", asm_spec);
printf ("*cpp:\n%s\n\n", cpp_spec);
printf ("*cc1:\n%s\n\n", cc1_spec);
printf ("*cc1plus:\n%s\n\n", cc1plus_spec);
printf ("*link:\n%s\n\n", link_spec);
printf ("*lib:\n%s\n\n", lib_spec);
printf ("*startfile:\n%s\n\n", startfile_spec);
printf ("*signed_char:\n%s\n\n", signed_char_spec);
printf ("*predefines:\n%s\n\n", cpp_predefines);
exit (0);
}
else if (! strcmp (argv[i], "-dumpversion"))
{
printf ("%s\n", version_string);
exit (0);
}
else if (argv[i][0] == '-' && argv[i][1] != 'l')
{
register char *p = &argv[i][1];
register int c = *p;
switch (c)
{
case 'b':
if (p[1] == 0 && i + 1 == argc)
fatal ("argument to `-b' is missing");
if (p[1] == 0)
spec_machine = argv[++i];
#ifdef NeXT
/* Allow NeXT's -bsd switch. */
else if (WORD_SWITCH (p))
{
n_switches++;
i += WORD_SWITCH_TAKES_ARG (p);
}
#endif /* NeXT */
else
spec_machine = p + 1;
break;
case 'B':
{
int *temp = (int *) xmalloc (sizeof (int));
char *value;
if (p[1] == 0 && i + 1 == argc)
fatal ("argument to `-B' is missing");
if (p[1] == 0)
value = argv[++i];
else
value = p + 1;
add_prefix (&exec_prefix, value, 1, 0, temp);
add_prefix (&startfile_prefix, value, 1, 0, temp);
}
break;
case 'v': /* Print our subcommands and print versions. */
verbose_flag++;
n_switches++;
break;
case 'V':
if (p[1] == 0 && i + 1 == argc)
fatal ("argument to `-V' is missing");
if (p[1] == 0)
spec_version = argv[++i];
else
spec_version = p + 1;
break;
#ifdef NeXT
case 'a':
if (!strcmp (p, "arch"))
{
if (i + 1 == argc)
fatal ("argument to `-arch' is missing");
spec_machine = argv[++i];
break;
}
case 'd':
if (!strcmp (p, "dependency-file"))
{
if (i + 1 == argc)
fatal ("argument to `-dependency-file' is missing");
dependency_output_file = argv[++i];
break;
}
#endif /* NeXT */
case 's':
if (!strcmp (p, "save-temps"))
{
save_temps_flag = 1;
break;
}
default:
n_switches++;
if (SWITCH_TAKES_ARG (c) > (p[1] != 0))
i += SWITCH_TAKES_ARG (c) - (p[1] != 0);
else if (WORD_SWITCH_TAKES_ARG (p))
i += WORD_SWITCH_TAKES_ARG (p);
}
}
else
n_infiles++;
}
/* Then create the space for the vectors and scan again. */
switches = ((struct switchstr *)
xmalloc ((n_switches + 1) * sizeof (struct switchstr)));
infiles = (struct infile *) xmalloc ((n_infiles + 1) * sizeof (struct infile));
n_switches = 0;
n_infiles = 0;
last_language_n_infiles = -1;
/* This, time, copy the text of each switch and store a pointer
to the copy in the vector of switches.
Store all the infiles in their vector. */
for (i = 1; i < argc; i++)
{
if (argv[i][0] == '-' && argv[i][1] != 'l')
{
register char *p = &argv[i][1];
register int c = *p;
#ifdef NeXT
/* Allow NeXT's -bsd switch. */
if ((c == 'B' || c == 'b' || c == 'V')
&& ! WORD_SWITCH (p))
#else /* NeXT */
if (c == 'B' || c == 'b' || c == 'V')
#endif /* NeXT */
{
/* Skip a separate arg, if any. */
if (p[1] == 0)
i++;
continue;
}
#ifdef NeXT
if (c == 'a' && !strcmp (p, "arch"))
{
i++;
continue;
}
if (c == 'd' && !strcmp (p, "dependency-file"))
{
i++;
continue;
}
#endif /* NeXT */
if (c == 'x')
{
if (p[1] == 0 && i + 1 == argc)
fatal ("argument to `-x' is missing");
if (p[1] == 0)
spec_lang = argv[++i];
else
spec_lang = p + 1;
last_language_n_infiles = n_infiles;
continue;
}
#ifdef NeXT
if (c == 'O' && !strcmp (p, "ObjC"))
default_language = "objective-c";
if (c == 'O' && !strcmp (p, "ObjC++"))
default_language = "c++";
#endif /* NeXT */
switches[n_switches].part1 = p;
/* Deal with option arguments in separate argv elements. */
if ((SWITCH_TAKES_ARG (c) > (p[1] != 0))
|| WORD_SWITCH_TAKES_ARG (p)) {
int j = 0;
int n_args = WORD_SWITCH_TAKES_ARG (p);
if (n_args == 0) {
/* Count only the option arguments in separate argv elements. */
n_args = SWITCH_TAKES_ARG (c) - (p[1] != 0);
}
switches[n_switches].args
= (char **) xmalloc ((n_args + 1) * sizeof (char *));
while (j < n_args)
switches[n_switches].args[j++] = argv[++i];
/* Null-terminate the vector. */
switches[n_switches].args[j] = 0;
#ifdef NeXT
} else if (c == 'o' && !WORD_SWITCH (p)) {
#else /* NeXT */
} else if (c == 'o') {
#endif /* NeXT */
/* On some systems, ld cannot handle -o without space.
So split the -o from its argument. */
switches[n_switches].part1 = "-o";
switches[n_switches].args = (char **) xmalloc (2 * sizeof (char *));
switches[n_switches].args[0] = (char *) xmalloc (strlen (p));
strcpy (switches[n_switches].args[0], &p[1]);
switches[n_switches].args[1] = 0;
} else
switches[n_switches].args = 0;
switches[n_switches].valid = 0;
/* This is always valid, since gcc.c itself understands it. */
if (!strcmp (p, "save-temps"))
switches[n_switches].valid = 1;
n_switches++;
}
else
{
infiles[n_infiles].language = spec_lang;
infiles[n_infiles++].name = argv[i];
}
}
if (n_infiles == last_language_n_infiles)
error ("Warning: `-x %s' after last input file has no effect", spec_lang);
switches[n_switches].part1 = 0;
infiles[n_infiles].name = 0;
}
/* Process a spec string, accumulating and running commands. */
/* These variables describe the input file name.
input_file_number is the index on outfiles of this file,
so that the output file name can be stored for later use by %o.
input_basename is the start of the part of the input file
sans all directory names, and basename_length is the number
of characters starting there excluding the suffix .c or whatever. */
char *input_filename;
int input_file_number;
int input_filename_length;
int basename_length;
char *input_basename;
char *input_suffix;
/* These are variables used within do_spec and do_spec_1. */
/* Nonzero if an arg has been started and not yet terminated
(with space, tab or newline). */
int arg_going;
/* Nonzero means %d or %g has been seen; the next arg to be terminated
is a temporary file name. */
int delete_this_arg;
/* Nonzero means %w has been seen; the next arg to be terminated
is the output file name of this compilation. */
int this_is_output_file;
/* Nonzero means %s has been seen; the next arg to be terminated
is the name of a library file and we should try the standard
search dirs for it. */
int this_is_library_file;
/* Process the spec SPEC and run the commands specified therein.
Returns 0 if the spec is successfully processed; -1 if failed. */
int
do_spec (spec)
char *spec;
{
int value;
clear_args ();
arg_going = 0;
delete_this_arg = 0;
this_is_output_file = 0;
this_is_library_file = 0;
value = do_spec_1 (spec, 0);
/* Force out any unfinished command.
If -pipe, this forces out the last command if it ended in `|'. */
if (value == 0)
{
if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
argbuf_index--;
if (argbuf_index > 0)
value = execute ();
}
return value;
}
/* Process the sub-spec SPEC as a portion of a larger spec.
This is like processing a whole spec except that we do
not initialize at the beginning and we do not supply a
newline by default at the end.
INSWITCH nonzero means don't process %-sequences in SPEC;
in this case, % is treated as an ordinary character.
This is used while substituting switches.
INSWITCH nonzero also causes SPC not to terminate an argument.
Value is zero unless a line was finished
and the command on that line reported an error. */
int
do_spec_1 (spec, inswitch)
char *spec;
int inswitch;
{
register char *p = spec;
register int c;
char *string;
while (c = *p++)
/* If substituting a switch, treat all chars like letters.
Otherwise, NL, SPC, TAB and % are special. */
switch (inswitch ? 'a' : c)
{
case '\n':
/* End of line: finish any pending argument,
then run the pending command if one has been started. */
if (arg_going)
{
obstack_1grow (&obstack, 0);
string = obstack_finish (&obstack);
if (this_is_library_file)
string = find_file (string);
store_arg (string, delete_this_arg, this_is_output_file);
if (this_is_output_file)
outfiles[input_file_number] = string;
}
arg_going = 0;
if (argbuf_index > 0 && !strcmp (argbuf[argbuf_index - 1], "|"))
{
int i;
for (i = 0; i < n_switches; i++)
if (!strcmp (switches[i].part1, "pipe"))
break;
/* A `|' before the newline means use a pipe here,
but only if -pipe was specified.
Otherwise, execute now and don't pass the `|' as an arg. */
if (i < n_switches)
{
switches[i].valid = 1;
break;
}
else
argbuf_index--;
}
if (argbuf_index > 0)
{
int value;
#ifdef REPORT_EVENT
if (spec == link_command_spec)
REPORT_EVENT (-1, NULL, NULL, 0, "Linking", 0, 0, 0);
#endif
value = execute ();
if (value)
return value;
}
/* Reinitialize for a new command, and for a new argument. */
clear_args ();
arg_going = 0;
delete_this_arg = 0;
this_is_output_file = 0;
this_is_library_file = 0;
break;
case '|':
/* End any pending argument. */
if (arg_going)
{
obstack_1grow (&obstack, 0);
string = obstack_finish (&obstack);
if (this_is_library_file)
string = find_file (string);
store_arg (string, delete_this_arg, this_is_output_file);
if (this_is_output_file)
outfiles[input_file_number] = string;
}
/* Use pipe */
obstack_1grow (&obstack, c);
arg_going = 1;
break;
case '\t':
case ' ':
/* Space or tab ends an argument if one is pending. */
if (arg_going)
{
obstack_1grow (&obstack, 0);
string = obstack_finish (&obstack);
if (this_is_library_file)
string = find_file (string);
store_arg (string, delete_this_arg, this_is_output_file);
if (this_is_output_file)
outfiles[input_file_number] = string;
}
/* Reinitialize for a new argument. */
arg_going = 0;
delete_this_arg = 0;
this_is_output_file = 0;
this_is_library_file = 0;
break;
case '%':
switch (c = *p++)
{
case 0:
fatal ("Invalid specification! Bug in cc.");
case 'b':
obstack_grow (&obstack, input_basename, basename_length);
arg_going = 1;
break;
case 'd':
delete_this_arg = 2;
break;
case 'e':
/* {...:%efoo} means report an error with `foo' as error message
and don't execute any more commands for this file. */
{
char *q = p;
char *buf;
while (*p != 0 && *p != '\n') p++;
buf = (char *) alloca (p - q + 1);
strncpy (buf, q, p - q);
buf[p - q] = 0;
error ("%s", buf);
return -1;
}
break;
case 'g':
if (save_temps_flag)
obstack_grow (&obstack, input_basename, basename_length);
else
{
obstack_grow (&obstack, temp_filename, temp_filename_length);
delete_this_arg = 1;
}
arg_going = 1;
break;
case 'i':
obstack_grow (&obstack, input_filename, input_filename_length);
arg_going = 1;
break;
case 'o':
{
register int f;
for (f = 0; f < n_infiles; f++)
store_arg (outfiles[f], 0, 0);
}
break;
#ifdef NeXT
case 'D':
{
if (dependency_output_file == NULL)
{
obstack_grow (&obstack, input_basename, basename_length);
obstack_grow (&obstack, ".d", 2);
}
else
obstack_grow (&obstack, dependency_output_file,
strlen (dependency_output_file));
arg_going = 1;
}
break;
#endif /* NeXT */
case 's':
this_is_library_file = 1;
break;
case 'w':
this_is_output_file = 1;
break;
case 'W':
{
int index = argbuf_index;
/* Handle the {...} following the %W. */
if (*p != '{')
abort ();
p = handle_braces (p + 1);
if (p == 0)
return -1;
/* If any args were output, mark the last one for deletion
on failure. */
if (argbuf_index != index)
record_temp_file (argbuf[argbuf_index - 1], 0, 1);
break;
}
case '{':
p = handle_braces (p);
if (p == 0)
return -1;
break;
case '%':
obstack_1grow (&obstack, '%');
break;
/*** The rest just process a certain constant string as a spec. */
case '1':
do_spec_1 (cc1_spec, 0);
break;
case '2':
do_spec_1 (cc1plus_spec, 0);
break;
case 'a':
do_spec_1 (asm_spec, 0);
break;
case 'c':
do_spec_1 (signed_char_spec, 0);
break;
case 'C':
do_spec_1 (cpp_spec, 0);
break;
case 'l':
do_spec_1 (link_spec, 0);
break;
case 'L':
do_spec_1 (lib_spec, 0);
break;
case 'p':
do_spec_1 (cpp_predefines, 0);
break;
case 'P':
{
char *x = (char *) alloca (strlen (cpp_predefines) * 2 + 1);
char *buf = x;
char *y = cpp_predefines;
/* Copy all of CPP_PREDEFINES into BUF,
but put __ after every -D and at the end of each arg, */
while (*y != 0)
{
if (! strncmp (y, "-D", 2))
{
int flag = 0;
*x++ = *y++;
*x++ = *y++;
if (strncmp (y, "__", 2))
{
/* Stick __ at front of macro name. */
*x++ = '_';
*x++ = '_';
/* Arrange to stick __ at the end as well. */
flag = 1;
}
/* Copy the macro name. */
while (*y && *y != '=' && *y != ' ' && *y != '\t')
*x++ = *y++;
if (flag)
{
*x++ = '_';
*x++ = '_';
}
/* Copy the value given, if any. */
while (*y && *y != ' ' && *y != '\t')
*x++ = *y++;
}
else
/* Just copy anything other than -D options.
(For example, whitespace. */
*x++ = *y++;
}
*x = 0;
do_spec_1 (buf, 0);
}
break;
case 'S':
do_spec_1 (startfile_spec, 0);
break;
case 'E':
do_spec_1 (ENDFILE_SPEC, 0);
break;
case '(':
case '[':
{
char *name = p;
struct spec_list *sl;
int len;
/* The string after the S/P is the name of a spec that is to be
processed. */
while (*p && *p != ')' && *p != ']')
p++;
/* See if it's in the list */
for (len = p - name, sl = specs; sl; sl = sl->next)
if (strncmp (sl->name, name, len) == 0 && !sl->name[len])
{
name = sl->spec;
break;
}
if (sl)
{
if (c == '(')
do_spec_1 (name, 0);
else
{
char *x = (char *) alloca (strlen (name) * 2 + 1);
char *buf = x;
char *y = name;
/* Copy all of NAME into BUF, but put __ after
every -D and at the end of each arg, */
while (1)
{
if (! strncmp (y, "-D", 2))
{
*x++ = '-';
*x++ = 'D';
*x++ = '_';
*x++ = '_';
y += 2;
}
else if (*y == ' ' || *y == 0)
{
*x++ = '_';
*x++ = '_';
if (*y == 0)
break;
else
*x++ = *y++;
}
else
*x++ = *y++;
}
*x = 0;
do_spec_1 (buf, 0);
}
}
}
break;
default:
abort ();
}
break;
default:
/* Ordinary character: put it into the current argument. */
obstack_1grow (&obstack, c);
arg_going = 1;
}
return 0; /* End of string */
}
/* Return 0 if we call do_spec_1 and that returns -1. */
char *
handle_braces (p)
register char *p;
{
register char *q;
char *filter;
int pipe = 0;
int negate = 0;
int suffix = 0;
if (*p == '|')
/* A `|' after the open-brace means,
if the test fails, output a single minus sign rather than nothing.
This is used in %{|!pipe:...}. */
pipe = 1, ++p;
if (*p == '!')
/* A `!' after the open-brace negates the condition:
succeed if the specified switch is not present. */
negate = 1, ++p;
if (*p == '.')
/* A `.' after the open-brace means test against the current suffix. */
{
if (pipe)
abort ();
suffix = 1;
++p;
}
filter = p;
while (*p != ':' && *p != '}') p++;
if (*p != '}')
{
register int count = 1;
q = p + 1;
while (count > 0)
{
if (*q == '{')
count++;
else if (*q == '}')
count--;
else if (*q == 0)
abort ();
q++;
}
}
else
q = p + 1;
if (suffix)
{
int found = (input_suffix != 0
&& strncmp (input_suffix, filter, p - filter) == 0);
if (p[0] == '}')
abort ();
if (negate != found && do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
return 0;
return q;
}
else if (p[-1] == '*' && p[0] == '}')
{
/* Substitute all matching switches as separate args. */
register int i;
--p;
for (i = 0; i < n_switches; i++)
if (!strncmp (switches[i].part1, filter, p - filter))
give_switch (i);
}
else
{
/* Test for presence of the specified switch. */
register int i;
int present = 0;
/* If name specified ends in *, as in {x*:...},
check for presence of any switch name starting with x. */
if (p[-1] == '*')
{
for (i = 0; i < n_switches; i++)
{
if (!strncmp (switches[i].part1, filter, p - filter - 1))
{
switches[i].valid = 1;
present = 1;
}
}
}
/* Otherwise, check for presence of exact name specified. */
else
{
for (i = 0; i < n_switches; i++)
{
if (!strncmp (switches[i].part1, filter, p - filter)
&& switches[i].part1[p - filter] == 0)
{
switches[i].valid = 1;
present = 1;
break;
}
}
}
/* If it is as desired (present for %{s...}, absent for %{-s...})
then substitute either the switch or the specified
conditional text. */
if (present != negate)
{
if (*p == '}')
{
give_switch (i);
}
else
{
if (do_spec_1 (save_string (p + 1, q - p - 2), 0) < 0)
return 0;
}
}
else if (pipe)
{
/* Here if a %{|...} conditional fails: output a minus sign,
which means "standard output" or "standard input". */
do_spec_1 ("-", 0);
}
}
return q;
}
/* Pass a switch to the current accumulating command
in the same form that we received it.
SWITCHNUM identifies the switch; it is an index into
the vector of switches gcc received, which is `switches'.
This cannot fail since it never finishes a command line. */
void
give_switch (switchnum)
int switchnum;
{
do_spec_1 ("-", 0);
do_spec_1 (switches[switchnum].part1, 1);
do_spec_1 (" ", 0);
if (switches[switchnum].args != 0)
{
char **p;
for (p = switches[switchnum].args; *p; p++) {
do_spec_1 (*p, 1);
do_spec_1 (" ", 0);
}
}
switches[switchnum].valid = 1;
}
/* Search for a file named NAME trying various prefixes including the
user's -B prefix and some standard ones.
Return the absolute pathname found. If nothing is found, return NAME. */
char *
find_file (name)
char *name;
{
char *newname;
newname = find_a_file (&startfile_prefix, name, R_OK);
return newname ? newname : name;
}
/* On fatal signals, delete all the temporary files. */
void
fatal_error (signum)
int signum;
{
signal (signum, SIG_DFL);
delete_failure_queue ();
delete_temp_files ();
/* Get the same signal again, this time not handled,
so its normal effect occurs. */
kill (getpid (), signum);
}
int
main (argc, argv)
int argc;
char **argv;
{
register int i;
int value;
int error_count = 0;
int linker_was_run = 0;
char *explicit_link_files;
char *specs_file;
programname = argv[0];
if (signal (SIGINT, SIG_IGN) != SIG_IGN)
signal (SIGINT, fatal_error);
if (signal (SIGHUP, SIG_IGN) != SIG_IGN)
signal (SIGHUP, fatal_error);
if (signal (SIGTERM, SIG_IGN) != SIG_IGN)
signal (SIGTERM, fatal_error);
#ifdef SIGPIPE
if (signal (SIGPIPE, SIG_IGN) != SIG_IGN)
signal (SIGPIPE, fatal_error);
#endif
argbuf_length = 10;
argbuf = (char **) xmalloc (argbuf_length * sizeof (char *));
obstack_init (&obstack);
/* Choose directory for temp files. */
choose_temp_base ();
/* Make a table of what switches there are (switches, n_switches).
Make a table of specified input files (infiles, n_infiles).
Decode switches that are handled locally. */
process_command (argc, argv);
/* Initialize the vector of specs to just the default.
This means one element containing 0s, as a terminator. */
compilers = (struct compiler *) xmalloc (sizeof default_compilers);
bcopy (default_compilers, compilers, sizeof default_compilers);
n_compilers = n_default_compilers;
/* Read specs from a file if there is one. */
#ifdef NeXT
machine_suffix = concat (spec_machine, "/", "");
#else /* NeXT */
machine_suffix = concat (spec_machine, "/", concat (spec_version, "/", ""));
#endif /* NeXT */
specs_file = find_a_file (&startfile_prefix, "specs", R_OK);
/* Read the specs file unless it is a default one. */
if (specs_file != 0 && strcmp (specs_file, "specs"))
read_specs (specs_file);
/* Now we have the specs.
Set the `valid' bits for switches that match anything in any spec. */
validate_all_switches ();
/* Warn about any switches that no pass was interested in. */
for (i = 0; i < n_switches; i++)
if (! switches[i].valid)
error ("unrecognized option `-%s'", switches[i].part1);
/* Obey some of the options. */
if (verbose_flag)
{
fprintf (stderr, "gcc version %s", version_string);
#ifdef TARGET_VERSION
TARGET_VERSION;
#endif
fprintf (stderr, "\n");
if (n_infiles == 0)
exit (0);
}
if (n_infiles == 0)
fatal ("No input files specified.");
/* Make a place to record the compiler output file names
that correspond to the input files. */
outfiles = (char **) xmalloc (n_infiles * sizeof (char *));
bzero (outfiles, n_infiles * sizeof (char *));
/* Record which files were specified explicitly as link input. */
explicit_link_files = (char *) xmalloc (n_infiles);
bzero (explicit_link_files, n_infiles);
for (i = 0; i < n_infiles; i++)
{
register struct compiler *cp = 0;
int this_file_error = 0;
/* Tell do_spec what to substitute for %i. */
input_filename = infiles[i].name;
input_filename_length = strlen (input_filename);
input_file_number = i;
/* Use the same thing in %o, unless cp->spec says otherwise. */
outfiles[i] = input_filename;
/* Figure out which compiler from the file's suffix. */
cp = lookup_compiler (infiles[i].name, input_filename_length,
infiles[i].language);
if (cp)
{
/* Ok, we found an applicable compiler. Run its spec. */
/* First say how much of input_filename to substitute for %b */
register char *p;
input_basename = input_filename;
for (p = input_filename; *p; p++)
if (*p == '/')
input_basename = p + 1;
/* Find a suffix starting with the last period,
and set basename_length to exclude that suffix. */
basename_length = strlen (input_basename);
p = input_basename + basename_length;
while (p != input_basename && *p != '.') --p;
if (*p == '.' && p != input_basename)
{
basename_length = p - input_basename;
input_suffix = p + 1;
}
else
input_suffix = "";
#ifdef REPORT_EVENT
REPORT_EVENT (-1, NULL, input_basename, 0, "Compiling", 0, 0, 0);
#endif
value = do_spec (cp->spec);
if (value < 0)
this_file_error = 1;
}
/* If this file's name does not contain a recognized suffix,
record it as explicit linker input. */
else
explicit_link_files[i] = 1;
/* Clear the delete-on-failure queue, deleting the files in it
if this compilation failed. */
if (this_file_error)
{
delete_failure_queue ();
error_count++;
}
/* If this compilation succeeded, don't delete those files later. */
clear_failure_queue ();
}
/* Run ld to link all the compiler output files. */
if (error_count == 0)
{
int tmp = execution_count;
value = do_spec (link_command_spec);
if (value < 0)
error_count = 1;
linker_was_run = (tmp != execution_count);
}
#ifndef NeXT
/* Warn if a -B option was specified but the prefix was never used. */
unused_prefix_warnings (&exec_prefix);
unused_prefix_warnings (&startfile_prefix);
#endif /* not NeXT */
/* If options said don't run linker,
complain about input files to be given to the linker. */
if (! linker_was_run && error_count == 0)
for (i = 0; i < n_infiles; i++)
if (explicit_link_files[i])
error ("%s: linker input file unused since linking not done",
outfiles[i]);
/* Delete some or all of the temporary files we made. */
if (error_count)
delete_failure_queue ();
delete_temp_files ();
exit (error_count);
}
/* Find the proper compilation spec for the file name NAME,
whose length is LENGTH. LANGUAGE is the specified langauge,
or 0 if none specified. */
struct compiler *
lookup_compiler (name, length, language)
char *name;
int length;
char *language;
{
struct compiler *cp;
/* Look for the language, if one is spec'd. */
if (language != 0)
{
for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
{
if (language != 0)
{
if (cp->suffix[0] == '@'
&& !strcmp (cp->suffix + 1, language))
return cp;
}
}
error ("language %s not recognized", language);
}
/* Look for a suffix. */
for (cp = compilers + n_compilers - 1; cp >= compilers; cp--)
{
if (strlen (cp->suffix) < length
/* See if the suffix matches the end of NAME. */
&& !strcmp (cp->suffix,
name + length - strlen (cp->suffix))
/* The suffix `-' matches only the file name `-'. */
&& !(!strcmp (cp->suffix, "-") && length != 1))
{
#ifdef NeXT
/* If we found any compiler at all for a suffix, return
the default compiler, if one was specified. */
if (default_language)
return lookup_compiler (0, 0, default_language);
#endif /* NeXT */
if (cp->spec[0] == '@')
{
struct compiler *new;
/* An alias entry maps a suffix to a language.
Search for the language; pass 0 for NAME and LENGTH
to avoid infinite recursion if language not found.
Construct the new compiler spec. */
language = cp->spec + 1;
new = (struct compiler *) xmalloc (sizeof (struct compiler));
new->suffix = cp->suffix;
new->spec = lookup_compiler (0, 0, language)->spec;
return new;
}
/* A non-alias entry: return it. */
return cp;
}
}
return 0;
}
int
xmalloc (size)
int size;
{
register int value = malloc (size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
int
xrealloc (ptr, size)
int ptr, size;
{
register int value = realloc (ptr, size);
if (value == 0)
fatal ("virtual memory exhausted");
return value;
}
/* Return a newly-allocated string whose contents concatenate those of s1, s2, s3. */
char *
concat (s1, s2, s3)
char *s1, *s2, *s3;
{
int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
char *result = (char *) xmalloc (len1 + len2 + len3 + 1);
strcpy (result, s1);
strcpy (result + len1, s2);
strcpy (result + len1 + len2, s3);
*(result + len1 + len2 + len3) = 0;
return result;
}
char *
save_string (s, len)
char *s;
int len;
{
register char *result = (char *) xmalloc (len + 1);
bcopy (s, result, len);
result[len] = 0;
return result;
}
void
pfatal_with_name (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("%s: ", sys_errlist[errno], "");
else
s = "cannot open %s";
fatal (s, name);
}
void
perror_with_name (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("%s: ", sys_errlist[errno], "");
else
s = "cannot open %s";
error (s, name);
}
void
perror_exec (name)
char *name;
{
extern int errno, sys_nerr;
extern char *sys_errlist[];
char *s;
if (errno < sys_nerr)
s = concat ("installation problem, cannot exec %s: ",
sys_errlist[errno], "");
else
s = "installation problem, cannot exec %s";
error (s, name);
}
/* More 'friendly' abort that prints the line and file.
config.h can #define abort fancy_abort if you like that sort of thing. */
void
fancy_abort ()
{
fatal ("Internal gcc abort.");
}
#ifdef HAVE_VPRINTF
/* Output an error message and exit */
void
fatal (va_alist)
va_dcl
{
va_list ap;
char *format;
va_start(ap);
format = va_arg (ap, char *);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf (stderr, "\n");
delete_temp_files ();
exit (1);
}
void
error (va_alist)
va_dcl
{
va_list ap;
char *format;
va_start(ap);
format = va_arg (ap, char *);
fprintf (stderr, "%s: ", programname);
vfprintf (stderr, format, ap);
va_end (ap);
fprintf (stderr, "\n");
}
#else /* not HAVE_VPRINTF */
void
fatal (msg, arg1, arg2)
char *msg, *arg1, *arg2;
{
error (msg, arg1, arg2);
delete_temp_files ();
exit (1);
}
void
error (msg, arg1, arg2)
char *msg, *arg1, *arg2;
{
fprintf (stderr, "%s: ", programname);
fprintf (stderr, msg, arg1, arg2);
fprintf (stderr, "\n");
}
#endif /* not HAVE_VPRINTF */
void
validate_all_switches ()
{
struct compiler *comp;
register char *p;
register char c;
for (comp = compilers; comp->spec; comp++)
{
p = comp->spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
}
p = link_command_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
/* Now notice switches mentioned in the machine-specific specs. */
p = asm_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = cpp_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = signed_char_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = cc1_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = cc1plus_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = link_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = lib_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
p = startfile_spec;
while (c = *p++)
if (c == '%' && *p == '{')
/* We have a switch spec. */
validate_switches (p + 1);
}
/* Look at the switch-name that comes after START
and mark as valid all supplied switches that match it. */
void
validate_switches (start)
char *start;
{
register char *p = start;
char *filter;
register int i;
int suffix = 0;
if (*p == '|')
++p;
if (*p == '!')
++p;
if (*p == '.')
suffix = 1, ++p;
filter = p;
while (*p != ':' && *p != '}') p++;
if (suffix)
;
else if (p[-1] == '*')
{
/* Mark all matching switches as valid. */
--p;
for (i = 0; i < n_switches; i++)
if (!strncmp (switches[i].part1, filter, p - filter))
switches[i].valid = 1;
}
else
{
/* Mark an exact matching switch as valid. */
for (i = 0; i < n_switches; i++)
{
if (!strncmp (switches[i].part1, filter, p - filter)
&& switches[i].part1[p - filter] == 0)
switches[i].valid = 1;
}
}
}