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

* Re: prevent "undef var" errors on gcc --help or --version
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Bernd Schmidt @ 2016-01-11 16:09 UTC (permalink / raw)
  To: Olivier Hainque, GCC Patches

On 01/08/2016 02:23 PM, Olivier Hainque wrote:
> +  /* 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));
> +

This doesn't follow the formatting rules. Also, there are a couple of 
other options that cause gcc to just print something and exit. Are these 
affected by missing env vars?


Bernd

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

* Re: prevent "undef var" errors on gcc --help or --version
  2016-01-11 16:09 ` Bernd Schmidt
@ 2016-01-12 16:11   ` Olivier Hainque
  2016-01-12 16:14     ` Bernd Schmidt
  0 siblings, 1 reply; 5+ messages in thread
From: Olivier Hainque @ 2016-01-12 16:11 UTC (permalink / raw)
  To: Bernd Schmidt; +Cc: GCC Patches

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

Hello Bernd,

Thanks for your feedback on this :-)

> On 11 Jan 2016, at 17:09, Bernd Schmidt <bschmidt@redhat.com> wrote:
> 
> On 01/08/2016 02:23 PM, Olivier Hainque wrote:
>> +  /* 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));
>> +
> 
> This doesn't follow the formatting rules.

Arg, indeed. Revised version attached.

> Also, there are a couple of other options that cause gcc to just print something and exit. Are these affected by missing env vars?

Some of these, for sure. For example, a common use case here is to
define a default --sysroot. We need this to be set properly for at
least --print-search-dirs and --print-prog-name, probably --print-file-name.

The print-multi family might be ok. It's heavily based on the presence
of other options on the command line, but maybe never depending on argument
values. I wasn't ready to bet though and opted for a conservative approach
first.

The attached patch is doing the same as the previous one, except more
explicitly and making it easier to adapt if deemed useful.

I could extract the decision code in a separate function if you prefer.

Olivier


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

diff --git a/gcc/gcc.c b/gcc/gcc.c
index 319a073..92fb051 100644
--- 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,27 @@ process_command (unsigned int decoded_options_count,
       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 = (decoded_options_count == n_varsafe_options + 1);
+  }
+
   alloc_switch ();
   switches[n_switches].part1 = 0;
   alloc_infile ();
@@ -9085,14 +9111,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 +9129,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

[-- Attachment #3: Type: text/plain, Size: 2 bytes --]




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

* Re: prevent "undef var" errors on gcc --help or --version
  2016-01-12 16:11   ` Olivier Hainque
@ 2016-01-12 16:14     ` Bernd Schmidt
  2016-01-12 16:37       ` Olivier Hainque
  0 siblings, 1 reply; 5+ messages in thread
From: Bernd Schmidt @ 2016-01-12 16:14 UTC (permalink / raw)
  To: Olivier Hainque; +Cc: GCC Patches



On 01/12/2016 05:11 PM, Olivier Hainque wrote:
> +  /* 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;
> +

I think you can do without the outer braces. Ok with those removed.


Bernd

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

* Re: prevent "undef var" errors on gcc --help or --version
  2016-01-12 16:14     ` Bernd Schmidt
@ 2016-01-12 16:37       ` Olivier Hainque
  0 siblings, 0 replies; 5+ messages in thread
From: Olivier Hainque @ 2016-01-12 16:37 UTC (permalink / raw)
  To: Bernd Schmidt; +Cc: GCC Patches


> On 12 Jan 2016, at 17:14, Bernd Schmidt <bschmidt@redhat.com> wrote:
> 
> I think you can do without the outer braces. Ok with those removed.

Great! Thanks for the review and comments.

With Kind Regards,

Olivier

^ 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).