📄 gcc.c
字号:
/* 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 modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 1, 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe 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!!!! */void record_temp_file ();/* This program is the user interface to the C compiler and possibly toother compilers. It is used because compilation is a complicated procedurewhich involves running several programs and passing temporary files betweenthem, 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 forcompilation is specified by a string called a "spec".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 pathis searched and it is unreliable to depend on the current working directory.Redirection of input or output is not supported; the subprograms mustaccept filenames saying what files to read and write.In addition, the specs can contain %-sequences to substitute variable textor for conditional text. Here is a table of all defined %-sequences.Note that spaces are not generated automatically around the results ofexpanding these sequences; therefore, you can concatenate them togetheror 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. %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. %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. 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. %G process LIBG_SPEC as a spec. A capital G is actually used here. %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. %{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 `-'.The conditional text X in a %{S:X} or %{!S:X} construct may containother 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 tothe following command, but only if -pipe is specified.Note that it is built into CC which switches take arguments and whichdo not. You might think it would be useful to generalize this toallow each compiler's spec to say which switches take arguments. Butthis cannot be done in a consistent fashion. CC cannot even decidewhich input files have been specified without knowing which switchestake arguments, and it must know which input files to compile in orderto tell which compilers to run.CC also knows implicitly that arguments starting in `-l' are tobe treated as compiler output files, and passed to the linker in their properposition among the other output files.*/#include <stdio.h>#include <sys/types.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 *//* Test if something is a normal file. */#ifndef S_ISREG#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)#endif#define obstack_chunk_alloc xmalloc#define obstack_chunk_free freeextern 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/* This is the obstack which we use to allocate many strings. */struct obstack obstack;char *handle_braces ();char *save_string ();char *concat ();int do_spec ();int do_spec_1 ();char *find_file ();static char *find_exec_file ();void validate_switches ();void validate_all_switches ();void fancy_abort ();/* 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 or extra switch-translations. */#ifndef CC1_SPEC#define CC1_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 "%{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}"#endif/* config.h can define ENDFILE_SPEC to override the default crtn files. */#ifndef ENDFILE_SPEC#define ENDFILE_SPEC ""#endif/* config.h can define LIBG_SPEC to override the default debug libraries. */#ifndef LIBG_SPEC#define LIBG_SPEC "%{g:-lg}"#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/* 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__}")/* 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/* This structure says how to run one compiler, and when to do so. */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. */};/* Here are the specs for compiling files with various known suffixes. 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. */struct compiler compilers[] ={ {".c", "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} %{trigraphs} -undef \ -D__GNUC__ %{ansi:-trigraphs -$ -D__STRICT_ANSI__} %{!ansi:%p} %P\ %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\ %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\ %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\ %{!M*:%{!E:cc1 %{!pipe:%g.cpp} %1 \ %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\ %{g} %{O*} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\ %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\ %{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 }}}"}, {".cc", "cpp -+ %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{i*} \ -undef -D__GNUC__ -D__GNUG__ %p %P\ %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\ %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\ %i %{!M*:%{!E:%{!pipe:%g.cpp}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\ %{!M*:%{!E:cc1plus %{!pipe:%g.cpp} %1\ %{!Q:-quiet} -dumpbase %i %{Y*} %{d*} %{m*} %{f*} %{a}\ %{g} %{O*} %{W*} %{w} %{pedantic} %{traditional}\ %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\ %{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 }}}"}, {".i", "cc1 %i %1 %{!Q:-quiet} %{Y*} %{d*} %{m*} %{f*} %{a}\ %{g} %{O*} %{W*} %{w} %{pedantic} %{ansi} %{traditional}\ %{v:-version} %{gg:-symout %g.sym} %{pg:-p} %{p}\ %{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", "%{!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", "cpp %{nostdinc} %{C} %{v} %{D*} %{U*} %{I*} %{M*} %{trigraphs} \ -undef -D__GNUC__ -$ %p %P\ %c %{O:-D__OPTIMIZE__} %{traditional} %{pedantic} %{P}\ %{Wcomment*} %{Wtrigraphs} %{Wall} %{w} %C\ %i %{!M*:%{!E:%{!pipe:%g.s}}}%{E:%W{o*}}%{M*:%W{o*}} |\n\ %{!M*:%{!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}};/* Here is the spec for running the linker, after compiling all files. */char *link_spec = "%{!c:%{!M*:%{!E:%{!S:ld %{o*} %l\ %{A} %{d} %{e*} %{N} %{n} %{r} %{s} %{S} %{T*} %{t} %{u*} %{X} %{x} %{z}\ %{y*} %{!A:%{!nostdlib:%S}} \ %{L*} %o %{!nostdlib:%G gnulib%s %L gnulib%s %{!A:%E}}\n }}}}";/* 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;/* Flag indicating whether we should print the command and arguments */unsigned char vflag;/* Name with which this program was invoked. */char *programname;/* User-specified -B prefix to attach to command names, or 0 if none specified. */char *user_exec_prefix = 0;/* Environment-specified prefix to attach to command names, or 0 if none specified. */char *env_exec_prefix = 0;/* Suffix to attach to directories searched for commands. */char *machine_suffix = 0;/* Default prefixes to attach to command names. */#ifndef STANDARD_EXEC_PREFIX#define STANDARD_EXEC_PREFIX "/usr/local/lib/gcc-"#endif /* !defined STANDARD_EXEC_PREFIX */char *standard_exec_prefix = STANDARD_EXEC_PREFIX;char *standard_exec_prefix_1 = "/usr/lib/gcc-";#ifndef STANDARD_STARTFILE_PREFIX#define STANDARD_STARTFILE_PREFIX "/usr/local/lib/"#endif /* !defined STANDARD_STARTFILE_PREFIX */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). */voidclear_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. */voidstore_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. */voidrecord_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. */voiddelete_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 (S_ISREG (st.st_mode)) if (unlink (temp->name) < 0) if (vflag) perror_with_name (temp->name); } } } always_delete_queue = 0;}/* Delete all the files to be deleted on error. */voiddelete_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 (vflag) perror_with_name (temp->name); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -