public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [patch] Add an if-exists-then-else spec function
@ 2020-10-01 16:04 Olivier Hainque
  2020-10-01 16:20 ` Armin Brauns
  0 siblings, 1 reply; 6+ messages in thread
From: Olivier Hainque @ 2020-10-01 16:04 UTC (permalink / raw)
  To: GCC Patches; +Cc: Olivier Hainque, Douglas B Rupp

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

Hello,

This patch is a proposal to add an if-exists-then-else
builtin spec function, which tests for the existence of
a file and returns one or the other of the following
arguments depending on the result of the test.

This differs from the existing if-exists or
if-exists-else function which return the name of the
tested file if it exists.

This new function is of help to a forthcoming change for
VxWorks where we check for the presence of a specific header
file to decide the name of a library to include in the link
closure, like:

  #define VXWORKS_NET_LIBS_RTP "-l%:if-exists-then-else(%:getenv(VSB_DIR /usr/h/public/rtnetStackLib.h) rtnet net)"


We have been using this for months in nightly gcc-9 based
compilers for numerous targets. It passes a build + local test sequence
with gcc-10 for powerpc-vxworks7r2 and a sanity check build with
a recent mainline.

Is this ok to commit ?

Thanks a lot in advance,

With Kind Regards,

Olivier


2020-10-01  Douglas Rupp  <rupp@adacore.com>

	* gcc.c (if-exists-then-else): New built-in spec function.


[-- Attachment #2: 0013-Add-a-if-exists-then-else-built-in-spec-function.diff --]
[-- Type: application/octet-stream, Size: 1841 bytes --]

--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -416,6 +416,7 @@ static void try_generate_repro (const char **argv);
 static const char *getenv_spec_function (int, const char **);
 static const char *if_exists_spec_function (int, const char **);
 static const char *if_exists_else_spec_function (int, const char **);
+static const char *if_exists_then_else_spec_function (int, const char **);
 static const char *sanitize_spec_function (int, const char **);
 static const char *replace_outfile_spec_function (int, const char **);
 static const char *remove_outfile_spec_function (int, const char **);
@@ -1676,6 +1677,7 @@ static const struct spec_function static_spec_functions[] =
   { "getenv",                   getenv_spec_function },
   { "if-exists",		if_exists_spec_function },
   { "if-exists-else",		if_exists_else_spec_function },
+  { "if-exists-then-else",	if_exists_then_else_spec_function },
   { "sanitize",			sanitize_spec_function },
   { "replace-outfile",		replace_outfile_spec_function },
   { "remove-outfile",		remove_outfile_spec_function },
@@ -10039,6 +10041,29 @@ if_exists_else_spec_function (int argc, const char **argv)
   return argv[1];
 }
 
+/* if-exists-then-else built-in spec function.
+
+   Checks to see if the file specified by the absolute pathname in
+   the first arg exists.  Returns the second arg if so, otherwise returns
+   the third arg if it is present.  */
+
+static const char *
+if_exists_then_else_spec_function (int argc, const char **argv)
+{
+
+  /* Must have two or three arguments.  */
+  if (argc != 2 && argc != 3)
+    return NULL;
+
+  if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
+    return argv[1];
+
+  if (argc == 3)
+    return argv[2];
+
+  return NULL;
+}
+
 /* sanitize built-in spec function.
 
    This returns non-NULL, if sanitizing address, thread or
-- 
2.17.1


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

* Re: [patch] Add an if-exists-then-else spec function
  2020-10-01 16:04 [patch] Add an if-exists-then-else spec function Olivier Hainque
@ 2020-10-01 16:20 ` Armin Brauns
  2020-10-07 16:26   ` Olivier Hainque
  0 siblings, 1 reply; 6+ messages in thread
From: Armin Brauns @ 2020-10-01 16:20 UTC (permalink / raw)
  To: gcc-patches

On 01/10/2020 18.04, Olivier Hainque wrote:
> Hello,
>
> This patch is a proposal to add an if-exists-then-else
> builtin spec function, which tests for the existence of
> a file and returns one or the other of the following
> arguments depending on the result of the test.
>
Hello,

could you please make sure to update the documentation around gcc/doc/invoke.texi:31574 accordingly? There's already a pending patch to make it more complete at https://gcc.gnu.org/pipermail/gcc-patches/2020-September/553321.html, but there shouldn't be any major conflicts between the two.

Regards,

Armin



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

* Re: [patch] Add an if-exists-then-else spec function
  2020-10-01 16:20 ` Armin Brauns
@ 2020-10-07 16:26   ` Olivier Hainque
  2020-10-14 19:10     ` Olivier Hainque
  0 siblings, 1 reply; 6+ messages in thread
From: Olivier Hainque @ 2020-10-07 16:26 UTC (permalink / raw)
  To: Armin Brauns; +Cc: Olivier Hainque, gcc-patches

Hello Armin,

> On 1 Oct 2020, at 18:20, Armin Brauns via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
> 
> 
> could you please make sure to update the documentation around gcc/doc/invoke.texi:31574 accordingly?

Oh, sure.

> There's already a pending patch to make it more complete at https://gcc.gnu.org/pipermail/gcc-patches/2020-September/553321.html, but there shouldn't be any major conflicts between the two.

Understood, thanks for the heads-up.

I’ll post an updated version once I’m
done retesting it together with a couple of changes
addressing comments on other patches.

Thanks for your feedback!

Olivier


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

* Re: [patch] Add an if-exists-then-else spec function
  2020-10-07 16:26   ` Olivier Hainque
@ 2020-10-14 19:10     ` Olivier Hainque
  2020-10-22 20:52       ` Joseph Myers
  0 siblings, 1 reply; 6+ messages in thread
From: Olivier Hainque @ 2020-10-14 19:10 UTC (permalink / raw)
  To: GCC Patches; +Cc: Olivier Hainque, Armin Brauns, Douglas B Rupp

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

Hello,

Here’s an updated version of originally proposed at

  https://gcc.gnu.org/pipermail/gcc-patches/2020-October/555271.html

with an extra documentation bit, as suggested by Armin’s
comment quoted below.

Re-tested with a couple of VxWorks builds.

Ok to commit ?

Thanks in advance!

Best Regards,

2020-10-14  Douglas Rupp  <rupp@adacore.com>

	* gcc.c (if-exists-then-else): New built-in spec function.
	* doc/invoke.texi: Document it.

>> On 1 Oct 2020, at 18:20, Armin Brauns via Gcc-patches <gcc-patches@gcc.gnu.org> wrote:
>> 
>> could you please make sure to update the documentation around gcc/doc/invoke.texi:31574 accordingly?


[-- Attachment #2: if-exists.txt --]
[-- Type: text/plain, Size: 3043 bytes --]

diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 47aa69530ab6..288792214e72 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -31665,6 +31665,19 @@ crt0%O%s %:if-exists(crti%O%s) \
 %:if-exists-else(crtbeginT%O%s crtbegin%O%s)
 @end smallexample
 
+@item @code{if-exists-then-else}
+The @code{if-exists-then-else} spec function takes at least two arguments
+and an optional third one. The first argument is an absolute pathname to a
+file.  If the file exists, the function returns the second argument.
+If the file does not exist, the function returns the third argument if there
+is one, or NULL otherwise. This can be used to expand one text, or optionally
+another, based on the existence of a file.  Here is a small example of its
+usage:
+
+@smallexample
+-l%:if-exists-then-else(%:getenv(VSB_DIR rtnet.h) rtnet net)
+@end smallexample
+
 @item @code{replace-outfile}
 The @code{replace-outfile} spec function takes two arguments.  It looks for the
 first argument in the outfiles array and replaces it with the second argument.  Here
diff --git a/gcc/gcc.c b/gcc/gcc.c
index ff7b6c4a3205..337c27442a39 100644
--- a/gcc/gcc.c
+++ b/gcc/gcc.c
@@ -416,6 +416,7 @@ static void try_generate_repro (const char **argv);
 static const char *getenv_spec_function (int, const char **);
 static const char *if_exists_spec_function (int, const char **);
 static const char *if_exists_else_spec_function (int, const char **);
+static const char *if_exists_then_else_spec_function (int, const char **);
 static const char *sanitize_spec_function (int, const char **);
 static const char *replace_outfile_spec_function (int, const char **);
 static const char *remove_outfile_spec_function (int, const char **);
@@ -1723,6 +1724,7 @@ static const struct spec_function static_spec_functions[] =
   { "getenv",                   getenv_spec_function },
   { "if-exists",		if_exists_spec_function },
   { "if-exists-else",		if_exists_else_spec_function },
+  { "if-exists-then-else",	if_exists_then_else_spec_function },
   { "sanitize",			sanitize_spec_function },
   { "replace-outfile",		replace_outfile_spec_function },
   { "remove-outfile",		remove_outfile_spec_function },
@@ -10087,6 +10089,29 @@ if_exists_else_spec_function (int argc, const char **argv)
   return argv[1];
 }
 
+/* if-exists-then-else built-in spec function.
+
+   Checks to see if the file specified by the absolute pathname in
+   the first arg exists.  Returns the second arg if so, otherwise returns
+   the third arg if it is present.  */
+
+static const char *
+if_exists_then_else_spec_function (int argc, const char **argv)
+{
+
+  /* Must have two or three arguments.  */
+  if (argc != 2 && argc != 3)
+    return NULL;
+
+  if (IS_ABSOLUTE_PATH (argv[0]) && ! access (argv[0], R_OK))
+    return argv[1];
+
+  if (argc == 3)
+    return argv[2];
+
+  return NULL;
+}
+
 /* sanitize built-in spec function.
 
    This returns non-NULL, if sanitizing address, thread or

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

* Re: [patch] Add an if-exists-then-else spec function
  2020-10-14 19:10     ` Olivier Hainque
@ 2020-10-22 20:52       ` Joseph Myers
  2020-10-23 14:24         ` Olivier Hainque
  0 siblings, 1 reply; 6+ messages in thread
From: Joseph Myers @ 2020-10-22 20:52 UTC (permalink / raw)
  To: Olivier Hainque; +Cc: GCC Patches, Armin Brauns, Douglas B Rupp

This is OK.

-- 
Joseph S. Myers
joseph@codesourcery.com

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

* Re: [patch] Add an if-exists-then-else spec function
  2020-10-22 20:52       ` Joseph Myers
@ 2020-10-23 14:24         ` Olivier Hainque
  0 siblings, 0 replies; 6+ messages in thread
From: Olivier Hainque @ 2020-10-23 14:24 UTC (permalink / raw)
  To: Joseph Myers; +Cc: Olivier Hainque, GCC Patches, Armin Brauns, Douglas B Rupp



> On 22 Oct 2020, at 22:52, Joseph Myers <joseph@codesourcery.com> wrote:
> 
> This is OK.

Great, thanks Joseph!


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

end of thread, other threads:[~2020-10-23 14:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-01 16:04 [patch] Add an if-exists-then-else spec function Olivier Hainque
2020-10-01 16:20 ` Armin Brauns
2020-10-07 16:26   ` Olivier Hainque
2020-10-14 19:10     ` Olivier Hainque
2020-10-22 20:52       ` Joseph Myers
2020-10-23 14:24         ` 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).