public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* prevent "undef var" errors on gcc --help or --version
@ 2016-01-08 13:23 Olivier Hainque
  2016-01-11 16:09 ` Bernd Schmidt
  0 siblings, 1 reply; 5+ messages in thread
From: Olivier Hainque @ 2016-01-08 13:23 UTC (permalink / raw)
  To: GCC Patches

[-- Attachment #1: Type: text/plain, Size: 1316 bytes --]

Hello,

self-specs setup with configure --with-specs are allowed to contain %:getenv
environment variable references. We are using this capability in a few cases
for cross ports.

In such configurations, gcc --help or gcc --version fail if the variables
aren't defined, like so:

gcc --version             
Using built-in specs.
gcc: fatal error: environment variable ‘<VARIABLE_NAME_HERE>’ not defined
compilation terminated.

It is a bit annoying to have to define environment variables just to
be able to retrieve version info. We have been using a close variant of
the attached patch to address this on gcc-4.7 and 4.9 for a while now.

The patch was adjusted for mainline, I tested that it still operates
as intended and performed a regular bootstrap + testsuite cycle showing
no regression on x86_64-linux.

OK to commit ?

Thanks much in advance for your feedback,

With Kind Regards,

Olivier


2016-01-08  Olivier Hainque  <hainque@adacore.com>

	* gcc.c (spec_undefvar_allowed): New global.
       (process_command): Set to true when running for --version or --help
       alone, or together.
       (getenv_spec_function): When the variable is not defined, use the
       variable name as the variable value if we're allowed not to issue
       a fatal error.


[-- Attachment #2: spec-undefvar.diff --]
[-- Type: application/octet-stream, Size: 2300 bytes --]

--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -3299,6 +3299,11 @@ int n_infiles;
 
 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.  */
 
@@ -4542,6 +4547,15 @@ process_command (unsigned int decoded_options_count,
       add_infile ("help-dummy", "c");
     }
 
+  /* Undefined variable references in specs are harmless if
+     we're running for --help or --version alone, or together.  */
+  spec_undefvar_allowed =
+    (((print_version || print_help_list)
+      && decoded_options_count == 2)
+     ||
+     ((print_version && print_help_list)
+      && decoded_options_count == 3));
+
   alloc_switch ();
   switches[n_switches].part1 = 0;
   alloc_infile ();
@@ -9085,14 +9099,17 @@ print_multilib_info (void)
 \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;
@@ -9100,10 +9117,15 @@ getenv_spec_function (int argc, const char **argv)
   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

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-01-12 16:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-08 13:23 prevent "undef var" errors on gcc --help or --version Olivier Hainque
2016-01-11 16:09 ` Bernd Schmidt
2016-01-12 16:11   ` Olivier Hainque
2016-01-12 16:14     ` Bernd Schmidt
2016-01-12 16:37       ` Olivier Hainque

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).