[PATCH v1 1/1] bfd: add a mean to programatically detect a release version

Sam James sam@gentoo.org
Thu Aug 14 15:49:41 GMT 2025


Matthieu Longo <matthieu.longo@arm.com> writes:

> In a recent discussion around Object Attributes v2, it appeared that we
> needed a way to detect whether a tool was built as a release version,
> or development version.
>
> A previous patch (7b40f4c6587c) introduced this detection in gas.
> This patch reuses the same principle but moved the code into a utility
> function in bsd, so that gas, ld, and others can rely on it. It also
> adds a Tcl function 'is_release' that can be used with a '#target: '
> directive to disable some tests relying on development-only features.
> ---
>  bfd/bfd-in.h                               |  2 ++
>  bfd/bfd-in2.h                              |  2 ++
>  bfd/bfd.c                                  |  9 +++++++++
>  binutils/testsuite/lib/binutils-common.exp | 13 +++++++++++++
>  gas/as.c                                   |  2 +-
>  5 files changed, 27 insertions(+), 1 deletion(-)
>
> diff --git a/bfd/bfd-in.h b/bfd/bfd-in.h
> index df5568e9639..ad126644845 100644
> --- a/bfd/bfd-in.h
> +++ b/bfd/bfd-in.h
> @@ -159,3 +159,5 @@ startswith (const char *str, const char *prefix)
>    return strncmp (str, prefix, strlen (prefix)) == 0;
>  }
>  
> +extern bool
> +is_release_version (void);
> diff --git a/bfd/bfd-in2.h b/bfd/bfd-in2.h
> index 12512a3962c..3ce4bad278d 100644
> --- a/bfd/bfd-in2.h
> +++ b/bfd/bfd-in2.h
> @@ -166,6 +166,8 @@ startswith (const char *str, const char *prefix)
>    return strncmp (str, prefix, strlen (prefix)) == 0;
>  }
>  
> +extern bool
> +is_release_version (void);
>  /* Extracted from libbfd.c.  */
>  void *bfd_alloc (bfd *abfd, bfd_size_type wanted);
>  
> diff --git a/bfd/bfd.c b/bfd/bfd.c
> index 4aded6809bb..aaa04cdeba9 100644
> --- a/bfd/bfd.c
> +++ b/bfd/bfd.c
> @@ -3103,3 +3103,12 @@ bfd_group_signature (asection *group, asymbol **isympp)
>      }
>    return NULL;
>  }
> +
> +/* Return True if the current tool artifact is a development version or a
> +   release version.  */
> +
> +bool
> +is_release_version ()
> +{
> +  return strstr (BFD_VERSION_STRING, "." XSTRING (BFD_VERSION_DATE)) == NULL;
> +}
> diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp
> index 2ee859aa3eb..435d2108378 100644
> --- a/binutils/testsuite/lib/binutils-common.exp
> +++ b/binutils/testsuite/lib/binutils-common.exp
> @@ -17,6 +17,19 @@
>  # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
>  # MA 02110-1301, USA.
>  
> +# True if the current tool is a release version.
> +#
> +proc is_release {} {
> +  global AS
> +  catch "exec $AS --version | head -1" exec_output

Do head -n1. head -1 isn't POSIX.

> +  if [regexp {\d+\.\d+\.\d+(\.\d+)} $exec_output matched sub1] then {
> +    if {$sub1 eq ""} {
> +      return 1
> +    }
> +  }
> +  return 0
> +}
> +
>  # True if the object format is known to be ELF.
>  #
>  proc is_elf_format {} {
> diff --git a/gas/as.c b/gas/as.c
> index 58ed0a59e21..4e8f6a4aa02 100644
> --- a/gas/as.c
> +++ b/gas/as.c
> @@ -1243,7 +1243,7 @@ perform_an_assembly_pass (int argc, char ** argv)
>  #endif
>  
>    predefine_symbol ("version", BFD_VERSION);
> -  if (strstr (BFD_VERSION_STRING, "." XSTRING (BFD_VERSION_DATE)) != NULL)
> +  if (!is_release_version ())
>      predefine_symbol ("date", BFD_VERSION_DATE);
>  
>    /* This may add symbol table entries, which requires having an open BFD,


More information about the Binutils mailing list