/* Compiler driver program that can handle many languages.
- Copyright (C) 1987-2015 Free Software Foundation, Inc.
+ Copyright (C) 1987-2016 Free Software Foundation, Inc.
This file is part of GCC.
#include "opts.h"
#include "params.h"
#include "filenames.h"
+#include "spellcheck.h"
\f
libgcc. This is not yet a real spec, though it could become one;
it is currently just stuffed into LINK_SPEC. FIXME: This wrapping
only works with GNU ld and gold. */
+#ifdef HAVE_GOLD_NON_DEFAULT_SPLIT_STACK
+#define STACK_SPLIT_SPEC " %{fsplit-stack: -fuse-ld=gold --wrap=pthread_create}"
+#else
#define STACK_SPLIT_SPEC " %{fsplit-stack: --wrap=pthread_create}"
+#endif
#ifndef LIBASAN_SPEC
#define STATIC_LIBASAN_LIBS \
#ifndef LINK_PIE_SPEC
#ifdef HAVE_LD_PIE
+#ifndef LD_PIE_SPEC
#define LD_PIE_SPEC "-pie"
+#endif
#else
#define LD_PIE_SPEC ""
#endif
%{%:sanitize(leak):" LIBLSAN_SPEC "}}}"
#endif
+#ifndef POST_LINK_SPEC
+#define POST_LINK_SPEC ""
+#endif
+
/* This is the spec to use, once the code for creating the vtable
verification runtime library, libvtv.so, has been created. Currently
the vtable verification runtime functions are in libstdc++, so we use
the spec just below this one. */
#ifndef VTABLE_VERIFICATION_SPEC
+#if ENABLE_VTABLE_VERIFY
#define VTABLE_VERIFICATION_SPEC "\
%{!nostdlib:%{fvtable-verify=std: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end}\
%{fvtable-verify=preinit: -lvtv -u_vtable_map_vars_start -u_vtable_map_vars_end}}"
+#else
+#define VTABLE_VERIFICATION_SPEC "\
+%{fvtable-verify=none:} \
+%{fvtable-verify=std: \
+ %e-fvtable-verify=std is not supported in this configuration} \
+%{fvtable-verify=preinit: \
+ %e-fvtable-verify=preinit is not supported in this configuration}"
+#endif
#endif
#ifndef CHKP_SPEC
%{flto} %{fno-lto} %{flto=*} %l " LINK_PIE_SPEC \
"%{fuse-ld=*:-fuse-ld=%*} " LINK_COMPRESS_DEBUG_SPEC \
"%X %{o*} %{e*} %{N} %{n} %{r}\
- %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} " VTABLE_VERIFICATION_SPEC " \
- %{static:} %{L*} %(mfwrap) %(link_libgcc) " SANITIZER_EARLY_SPEC " %o\
- " CHKP_SPEC " \
- %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*} 1):\
+ %{s} %{t} %{u*} %{z} %{Z} %{!nostdlib:%{!nostartfiles:%S}} \
+ %{static:} %{L*} %(mfwrap) %(link_libgcc) " \
+ VTABLE_VERIFICATION_SPEC " " SANITIZER_EARLY_SPEC " %o " CHKP_SPEC " \
+ %{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1):\
%:include(libgomp.spec)%(link_gomp)}\
%{fcilkplus:%:include(libcilkrts.spec)%(link_cilkrts)}\
%{fgnu-tm:%:include(libitm.spec)%(link_itm)}\
%(mflib) " STACK_SPLIT_SPEC "\
%{fprofile-arcs|fprofile-generate*|coverage:-lgcov} " SANITIZER_SPEC " \
%{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}}\
- %{!nostdlib:%{!nostartfiles:%E}} %{T*} }}}}}}"
+ %{!nostdlib:%{!nostartfiles:%E}} %{T*} \n%(post_link) }}}}}}"
#endif
#ifndef LINK_LIBGCC_SPEC
static const char *linker_plugin_file_spec = "";
static const char *lto_wrapper_spec = "";
static const char *lto_gcc_spec = "";
+static const char *post_link_spec = POST_LINK_SPEC;
static const char *link_command_spec = LINK_COMMAND_SPEC;
static const char *link_libgcc_spec = LINK_LIBGCC_SPEC;
static const char *startfile_prefix_spec = STARTFILE_PREFIX_SPEC;
for targets that use different start files and suchlike. */
#ifndef GOMP_SELF_SPECS
#define GOMP_SELF_SPECS \
- "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*} 1): " \
+ "%{fopenacc|fopenmp|%:gt(%{ftree-parallelize-loops=*:%*} 1): " \
"-pthread}"
#endif
INIT_STATIC_SPEC ("linker_plugin_file", &linker_plugin_file_spec),
INIT_STATIC_SPEC ("lto_wrapper", <o_wrapper_spec),
INIT_STATIC_SPEC ("lto_gcc", <o_gcc_spec),
+ INIT_STATIC_SPEC ("post_link", &post_link_spec),
INIT_STATIC_SPEC ("link_libgcc", &link_libgcc_spec),
INIT_STATIC_SPEC ("md_exec_prefix", &md_exec_prefix),
INIT_STATIC_SPEC ("md_startfile_prefix", &md_startfile_prefix),
struct temp_file *temp;
for (temp = always_delete_queue; temp; temp = temp->next)
if (! filename_cmp (name, temp->name))
- goto already1;
+ {
+ free (name);
+ goto already1;
+ }
temp = XNEW (struct temp_file);
temp->next = always_delete_queue;
static int n_infiles_alloc;
+/* True if undefined environment variables encountered during spec processing
+ are ok to ignore, typically when we're running for --help or --version. */
+
+static bool spec_undefvar_allowed;
+
/* True if multiple input files are being compiled to a single
assembly file. */
printf (_("Usage: %s [options] file...\n"), progname);
fputs (_("Options:\n"), stdout);
- fputs (_(" -pass-exit-codes Exit with highest error code from a phase\n"), stdout);
- fputs (_(" --help Display this information\n"), stdout);
- fputs (_(" --target-help Display target specific command line options\n"), stdout);
- fputs (_(" --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...]\n"), stdout);
- fputs (_(" Display specific types of command line options\n"), stdout);
+ fputs (_(" -pass-exit-codes Exit with highest error code from a phase.\n"), stdout);
+ fputs (_(" --help Display this information.\n"), stdout);
+ fputs (_(" --target-help Display target specific command line options.\n"), stdout);
+ fputs (_(" --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].\n"), stdout);
+ fputs (_(" Display specific types of command line options.\n"), stdout);
if (! verbose_flag)
- fputs (_(" (Use '-v --help' to display command line options of sub-processes)\n"), stdout);
- fputs (_(" --version Display compiler version information\n"), stdout);
- fputs (_(" -dumpspecs Display all of the built in spec strings\n"), stdout);
- fputs (_(" -dumpversion Display the version of the compiler\n"), stdout);
- fputs (_(" -dumpmachine Display the compiler's target processor\n"), stdout);
- fputs (_(" -print-search-dirs Display the directories in the compiler's search path\n"), stdout);
- fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library\n"), stdout);
- fputs (_(" -print-file-name=<lib> Display the full path to library <lib>\n"), stdout);
- fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>\n"), stdout);
+ fputs (_(" (Use '-v --help' to display command line options of sub-processes).\n"), stdout);
+ fputs (_(" --version Display compiler version information.\n"), stdout);
+ fputs (_(" -dumpspecs Display all of the built in spec strings.\n"), stdout);
+ fputs (_(" -dumpversion Display the version of the compiler.\n"), stdout);
+ fputs (_(" -dumpmachine Display the compiler's target processor.\n"), stdout);
+ fputs (_(" -print-search-dirs Display the directories in the compiler's search path.\n"), stdout);
+ fputs (_(" -print-libgcc-file-name Display the name of the compiler's companion library.\n"), stdout);
+ fputs (_(" -print-file-name=<lib> Display the full path to library <lib>.\n"), stdout);
+ fputs (_(" -print-prog-name=<prog> Display the full path to compiler component <prog>.\n"), stdout);
fputs (_("\
-print-multiarch Display the target's normalized GNU triplet, used as\n\
- a component in the library path\n"), stdout);
- fputs (_(" -print-multi-directory Display the root directory for versions of libgcc\n"), stdout);
+ a component in the library path.\n"), stdout);
+ fputs (_(" -print-multi-directory Display the root directory for versions of libgcc.\n"), stdout);
fputs (_("\
-print-multi-lib Display the mapping between command line options and\n\
- multiple library search directories\n"), stdout);
- fputs (_(" -print-multi-os-directory Display the relative path to OS libraries\n"), stdout);
- fputs (_(" -print-sysroot Display the target libraries directory\n"), stdout);
- fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers\n"), stdout);
- fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler\n"), stdout);
- fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor\n"), stdout);
- fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker\n"), stdout);
- fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler\n"), stdout);
- fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor\n"), stdout);
- fputs (_(" -Xlinker <arg> Pass <arg> on to the linker\n"), stdout);
- fputs (_(" -save-temps Do not delete intermediate files\n"), stdout);
- fputs (_(" -save-temps=<arg> Do not delete intermediate files\n"), stdout);
+ multiple library search directories.\n"), stdout);
+ fputs (_(" -print-multi-os-directory Display the relative path to OS libraries.\n"), stdout);
+ fputs (_(" -print-sysroot Display the target libraries directory.\n"), stdout);
+ fputs (_(" -print-sysroot-headers-suffix Display the sysroot suffix used to find headers.\n"), stdout);
+ fputs (_(" -Wa,<options> Pass comma-separated <options> on to the assembler.\n"), stdout);
+ fputs (_(" -Wp,<options> Pass comma-separated <options> on to the preprocessor.\n"), stdout);
+ fputs (_(" -Wl,<options> Pass comma-separated <options> on to the linker.\n"), stdout);
+ fputs (_(" -Xassembler <arg> Pass <arg> on to the assembler.\n"), stdout);
+ fputs (_(" -Xpreprocessor <arg> Pass <arg> on to the preprocessor.\n"), stdout);
+ fputs (_(" -Xlinker <arg> Pass <arg> on to the linker.\n"), stdout);
+ fputs (_(" -save-temps Do not delete intermediate files.\n"), stdout);
+ fputs (_(" -save-temps=<arg> Do not delete intermediate files.\n"), stdout);
fputs (_("\
-no-canonical-prefixes Do not canonicalize paths when building relative\n\
- prefixes to other gcc components\n"), stdout);
- fputs (_(" -pipe Use pipes rather than intermediate files\n"), stdout);
- fputs (_(" -time Time the execution of each subprocess\n"), stdout);
- fputs (_(" -specs=<file> Override built-in specs with the contents of <file>\n"), stdout);
- fputs (_(" -std=<standard> Assume that the input sources are for <standard>\n"), stdout);
+ prefixes to other gcc components.\n"), stdout);
+ fputs (_(" -pipe Use pipes rather than intermediate files.\n"), stdout);
+ fputs (_(" -time Time the execution of each subprocess.\n"), stdout);
+ fputs (_(" -specs=<file> Override built-in specs with the contents of <file>.\n"), stdout);
+ fputs (_(" -std=<standard> Assume that the input sources are for <standard>.\n"), stdout);
fputs (_("\
--sysroot=<directory> Use <directory> as the root directory for headers\n\
- and libraries\n"), stdout);
- fputs (_(" -B <directory> Add <directory> to the compiler's search paths\n"), stdout);
- fputs (_(" -v Display the programs invoked by the compiler\n"), stdout);
- fputs (_(" -### Like -v but options quoted and commands not executed\n"), stdout);
- fputs (_(" -E Preprocess only; do not compile, assemble or link\n"), stdout);
- fputs (_(" -S Compile only; do not assemble or link\n"), stdout);
- fputs (_(" -c Compile and assemble, but do not link\n"), stdout);
- fputs (_(" -o <file> Place the output into <file>\n"), stdout);
- fputs (_(" -pie Create a position independent executable\n"), stdout);
- fputs (_(" -shared Create a shared library\n"), stdout);
+ and libraries.\n"), stdout);
+ fputs (_(" -B <directory> Add <directory> to the compiler's search paths.\n"), stdout);
+ fputs (_(" -v Display the programs invoked by the compiler.\n"), stdout);
+ fputs (_(" -### Like -v but options quoted and commands not executed.\n"), stdout);
+ fputs (_(" -E Preprocess only; do not compile, assemble or link.\n"), stdout);
+ fputs (_(" -S Compile only; do not assemble or link.\n"), stdout);
+ fputs (_(" -c Compile and assemble, but do not link.\n"), stdout);
+ fputs (_(" -o <file> Place the output into <file>.\n"), stdout);
+ fputs (_(" -pie Create a position independent executable.\n"), stdout);
+ fputs (_(" -shared Create a shared library.\n"), stdout);
fputs (_("\
- -x <language> Specify the language of the following input files\n\
+ -x <language> Specify the language of the following input files.\n\
Permissible languages include: c c++ assembler none\n\
'none' means revert to the default behavior of\n\
- guessing the language based on the file's extension\n\
+ guessing the language based on the file's extension.\n\
"), stdout);
printf (_("\
n_switches++;
}
+/* Set the SOURCE_DATE_EPOCH environment variable to the current time if it is
+ not set already. */
+
+static void
+set_source_date_epoch_envvar ()
+{
+ /* Array size is 21 = ceil(log_10(2^64)) + 1 to hold string representations
+ of 64 bit integers. */
+ char source_date_epoch[21];
+ time_t tt;
+
+ errno = 0;
+ tt = time (NULL);
+ if (tt < (time_t) 0 || errno != 0)
+ tt = (time_t) 0;
+
+ snprintf (source_date_epoch, 21, "%llu", (unsigned long long) tt);
+ /* Using setenv instead of xputenv because we want the variable to remain
+ after finalizing so that it's still set in the second run when using
+ -fcompare-debug. */
+ setenv ("SOURCE_DATE_EPOCH", source_date_epoch, 0);
+}
+
/* Handle an option DECODED that is unknown to the option-processing
machinery. */
else
compare_debug_opt = arg;
save_switch (compare_debug_replacement_opt, 0, NULL, validated, true);
+ set_source_date_epoch_envvar ();
return true;
case OPT_fdiagnostics_color_:
CL_DRIVER, &handlers, global_dc);
}
-#ifdef ENABLE_OFFLOADING
/* If the user didn't specify any, default to all configured offload
targets. */
- if (offload_targets == NULL)
+ if (ENABLE_OFFLOADING && offload_targets == NULL)
handle_foffload_option (OFFLOAD_TARGETS);
-#endif
if (output_file
&& strcmp (output_file, "-") != 0
}
gcc_assert (!IS_ABSOLUTE_PATH (tooldir_base_prefix));
- tooldir_prefix2 = concat (tooldir_base_prefix, spec_host_machine,
+ tooldir_prefix2 = concat (tooldir_base_prefix, spec_machine,
dir_separator_str, NULL);
/* Look for tools relative to the location from which the driver is
add_infile ("help-dummy", "c");
}
+ /* Decide if undefined variable references are allowed in specs. */
+
+ /* --version and --help alone or together are safe. Note that -v would
+ make them unsafe, as they'd then be run for subprocesses as well, the
+ location of which might depend on variables possibly coming from
+ self-specs.
+
+ Count the number of options we have for which undefined variables
+ are harmless for sure, and check that nothing else is set. */
+
+ unsigned n_varsafe_options = 0;
+
+ if (print_version)
+ n_varsafe_options++;
+
+ if (print_help_list)
+ n_varsafe_options++;
+
+ spec_undefvar_allowed = (n_varsafe_options == decoded_options_count - 1);
+
alloc_switch ();
switches[n_switches].part1 = 0;
alloc_infile ();
driver::driver (bool can_finalize, bool debug) :
explicit_link_files (NULL),
- decoded_options (NULL)
+ decoded_options (NULL),
+ m_option_suggestions (NULL)
{
env.init (can_finalize, debug);
}
{
XDELETEVEC (explicit_link_files);
XDELETEVEC (decoded_options);
+ if (m_option_suggestions)
+ {
+ int i;
+ char *str;
+ FOR_EACH_VEC_ELT (*m_option_suggestions, i, str)
+ free (str);
+ delete m_option_suggestions;
+ }
}
/* driver::main is implemented as a series of driver:: method calls. */
global_init_params ();
finish_params ();
+ init_opts_obstack ();
init_options_struct (&global_options, &global_options_set);
decode_cmdline_options_to_array (argc, argv,
offload_targets = NULL;
}
+/* Helper function for driver::suggest_option. Populate
+ m_option_suggestions with candidate strings for misspelled options.
+ The strings will be freed by the driver's dtor. */
+
+void
+driver::build_option_suggestions (void)
+{
+ gcc_assert (m_option_suggestions == NULL);
+ m_option_suggestions = new auto_vec <char *> ();
+
+ /* We build a vec of m_option_suggestions, using add_misspelling_candidates
+ to add copies of strings, without a leading dash. */
+
+ for (unsigned int i = 0; i < cl_options_count; i++)
+ {
+ const struct cl_option *option = &cl_options[i];
+ const char *opt_text = option->opt_text;
+ switch (i)
+ {
+ default:
+ if (option->var_type == CLVC_ENUM)
+ {
+ const struct cl_enum *e = &cl_enums[option->var_enum];
+ for (unsigned j = 0; e->values[j].arg != NULL; j++)
+ {
+ char *with_arg = concat (opt_text, e->values[j].arg, NULL);
+ add_misspelling_candidates (m_option_suggestions, with_arg);
+ free (with_arg);
+ }
+ }
+ else
+ add_misspelling_candidates (m_option_suggestions, opt_text);
+ break;
+
+ case OPT_fsanitize_:
+ case OPT_fsanitize_recover_:
+ /* -fsanitize= and -fsanitize-recover= can take
+ a comma-separated list of arguments. Given that combinations
+ are supported, we can't add all potential candidates to the
+ vec, but if we at least add them individually without commas,
+ we should do a better job e.g. correcting
+ "-sanitize=address"
+ to
+ "-fsanitize=address"
+ rather than to "-Wframe-address" (PR driver/69265). */
+ {
+ for (int j = 0; sanitizer_opts[j].name != NULL; ++j)
+ {
+ /* Get one arg at a time e.g. "-fsanitize=address". */
+ char *with_arg = concat (opt_text,
+ sanitizer_opts[j].name,
+ NULL);
+ /* Add with_arg and all of its variant spellings e.g.
+ "-fno-sanitize=address" to candidates (albeit without
+ leading dashes). */
+ add_misspelling_candidates (m_option_suggestions, with_arg);
+ free (with_arg);
+ }
+ }
+ break;
+ }
+ }
+}
+
+/* Helper function for driver::handle_unrecognized_options.
+
+ Given an unrecognized option BAD_OPT (without the leading dash),
+ locate the closest reasonable matching option (again, without the
+ leading dash), or NULL.
+
+ The returned string is owned by the driver instance. */
+
+const char *
+driver::suggest_option (const char *bad_opt)
+{
+ /* Lazily populate m_option_suggestions. */
+ if (!m_option_suggestions)
+ build_option_suggestions ();
+ gcc_assert (m_option_suggestions);
+
+ /* "m_option_suggestions" is now populated. Use it. */
+ return find_closest_string
+ (bad_opt,
+ (auto_vec <const char *> *) m_option_suggestions);
+}
+
/* Reject switches that no pass was interested in. */
void
-driver::handle_unrecognized_options () const
+driver::handle_unrecognized_options ()
{
for (size_t i = 0; (int) i < n_switches; i++)
if (! switches[i].validated)
- error ("unrecognized command line option %<-%s%>", switches[i].part1);
+ {
+ const char *hint = suggest_option (switches[i].part1);
+ if (hint)
+ error ("unrecognized command line option %<-%s%>;"
+ " did you mean %<-%s%>?",
+ switches[i].part1, hint);
+ else
+ error ("unrecognized command line option %<-%s%>",
+ switches[i].part1);
+ }
}
/* Handle the various -print-* options, returning 0 if the driver
{
printf (_("%s %s%s\n"), progname, pkgversion_string,
version_string);
- printf ("Copyright %s 2015 Free Software Foundation, Inc.\n",
+ printf ("Copyright %s 2016 Free Software Foundation, Inc.\n",
_("(C)"));
fputs (_("This is free software; see the source for copying conditions. There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
\f
/* getenv built-in spec function.
- Returns the value of the environment variable given by its first
- argument, concatenated with the second argument. If the
- environment variable is not defined, a fatal error is issued. */
+ Returns the value of the environment variable given by its first argument,
+ concatenated with the second argument. If the variable is not defined, a
+ fatal error is issued unless such undefs are internally allowed, in which
+ case the variable name is used as the variable value. */
static const char *
getenv_spec_function (int argc, const char **argv)
{
const char *value;
+ const char *varname;
+
char *result;
char *ptr;
size_t len;
if (argc != 2)
return NULL;
- value = env.get (argv[0]);
+ varname = argv[0];
+ value = env.get (varname);
+
+ if (!value && spec_undefvar_allowed)
+ value = varname;
+
if (!value)
fatal_error (input_location,
- "environment variable %qs not defined", argv[0]);
+ "environment variable %qs not defined", varname);
/* We have to escape every character of the environment variable so
they are not interpreted as active spec characters. A
return result;
}
-/* Returns "" if the n in ARGV[1] == -opt=<n> is greater than ARGV[2].
+/* Returns "" if ARGV[ARGC - 2] is greater than ARGV[ARGC-1].
Otherwise, return NULL. */
static const char *
if (argc == 1)
return NULL;
- gcc_assert (argc == 3);
- gcc_assert (argv[0][0] == '-');
- gcc_assert (argv[0][1] == '\0');
-
- /* Point p to <n> in in -opt=<n>. */
- const char *p = argv[1];
- while (true)
- {
- char c = *p;
- if (c == '\0')
- gcc_unreachable ();
-
- ++p;
-
- if (c == '=')
- break;
- }
+ gcc_assert (argc >= 2);
- long arg = strtol (p, &converted, 10);
- gcc_assert (converted != p);
+ long arg = strtol (argv[argc - 2], &converted, 10);
+ gcc_assert (converted != argv[argc - 2]);
- long lim = strtol (argv[2], &converted, 10);
- gcc_assert (converted != argv[2]);
+ long lim = strtol (argv[argc - 1], &converted, 10);
+ gcc_assert (converted != argv[argc - 1]);
if (arg > lim)
return "";
multilib_os_dir = 0;
multiarch_dir = 0;
- XDELETEVEC (specs);
- specs = 0;
+ /* Free any specs dynamically-allocated by set_spec.
+ These will be at the head of the list, before the
+ statically-allocated ones. */
+ if (specs)
+ {
+ while (specs != static_specs)
+ {
+ spec_list *next = specs->next;
+ free (const_cast <char *> (specs->name));
+ XDELETE (specs);
+ specs = next;
+ }
+ specs = 0;
+ }
for (unsigned i = 0; i < ARRAY_SIZE (static_specs); i++)
{
spec_list *sl = &static_specs[i];
size_t i;
obstack_init (&obstack);
- gcc_obstack_init (&opts_obstack);
+ init_opts_obstack ();
n_switches = 0;
for (i = 0; i < ARRAY_SIZE (option_default_specs); i++)