https://gcc.gnu.org/g:bb3be4a06a5222502172f821f43214e0e80b278e

commit r17-5385-gbb3be4a06a5222502172f821f43214e0e80b278e
Author: Torbjörn SVENSSON <[email protected]>
Date:   Tue Sep 29 14:34:51 2026 +0200

    config: Expand the shared zstd configure macro
    
    Extend AC_ZSTD to support installation prefixes and custom include and
    library paths. For auto and yes, try pkg-config first, falling back to
    compiler search if the package is missing or unusable. Require a usable
    installation for yes; otherwise disable support if detection fails.
    
    Check for a usable zstd library and header, requiring version 1.4.0 or
    later, and export the compiler and linker flags.
    
    config/ChangeLog:
    
            * zstd.m4 (AC_ZSTD): Set up zstd options and check the library
            and header.
    
    Signed-off-by: Torbjörn SVENSSON <[email protected]>

Diff:
---
 config/zstd.m4 | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 122 insertions(+), 12 deletions(-)

diff --git a/config/zstd.m4 b/config/zstd.m4
index eb34c0f36d7b..28788782d3b0 100644
--- a/config/zstd.m4
+++ b/config/zstd.m4
@@ -5,19 +5,129 @@ dnl Public License, this file may be distributed as part of 
a program
 dnl that contains a configuration script generated by Autoconf, under
 dnl the same distribution terms as the rest of that program.
 
-dnl Enable features using the zstd library.
+dnl Set up and check zstd support.
 AC_DEFUN([AC_ZSTD], [
+AC_SUBST(ZSTD_CFLAGS)
+AC_SUBST(ZSTD_LIBS)
 AC_ARG_WITH(zstd,
-  [AS_HELP_STRING([--with-zstd], [support zstd compressed debug sections 
(default=auto)])],
-  [], [with_zstd=auto])
-
-AS_IF([test "$with_zstd" != no],
-  [PKG_CHECK_MODULES(ZSTD, [libzstd >= 1.4.0], [
-    AC_DEFINE(HAVE_ZSTD, 1, [Define to 1 if zstd is enabled.])
-  ], [
-    if test "$with_zstd" = yes; then
-      AC_MSG_ERROR([--with-zstd was given, but pkgconfig/libzstd.pc is not 
found])
+  [AS_HELP_STRING([--with-zstd=VALUE],
+    [VALUE may be 'auto' (try pkg-config, then compiler search; default),
+      'yes' (require zstd), 'no' (disable it), or a PATH for the
+      installation prefix.  When VALUE is a PATH, this is equivalent to
+      --with-zstd-include=PATH/include and
+      --with-zstd-lib=PATH/lib])],
+  [with_zstd=$withval], [with_zstd=auto])
+AC_ARG_WITH(zstd-include,
+  [AS_HELP_STRING([--with-zstd-include=PATH],
+    [specify directory for installed zstd include files])])
+AC_ARG_WITH(zstd-lib,
+  [AS_HELP_STRING([--with-zstd-lib=PATH],
+    [specify directory for the installed zstd library])])
+zstd_enabled=no
+zstd_pkg_config=no
+zstd_use_pkg_config=no
+zstd_include_dir=
+zstd_library_dir=
+case "x$with_zstd" in
+  xno)
+    ;;
+  xyes|xauto)
+    zstd_enabled=yes
+    zstd_use_pkg_config=yes
+    ;;
+  *)
+    zstd_enabled=yes
+    zstd_include_dir=$with_zstd/include
+    zstd_library_dir=$with_zstd/lib
+    ;;
+esac
+
+if test "$zstd_use_pkg_config" = yes; then
+  PKG_CHECK_MODULES([ZSTD], [libzstd >= 1.4.0], [
+    zstd_pkg_config=yes
+  ], [:])
+fi
+
+if test "$zstd_enabled" = yes; then
+  saved_CFLAGS="$CFLAGS"
+  save_LIBS="$LIBS"
+  AC_LANG_PUSH([C])
+  while :; do
+    if test "x$with_zstd_include" != x; then
+      if test "$zstd_pkg_config" = yes; then
+       ZSTD_CFLAGS="-I$with_zstd_include $ZSTD_CFLAGS"
+      else
+       zstd_include_dir=$with_zstd_include
+      fi
+    fi
+    if test "x$with_zstd_lib" != x; then
+      zstd_library_dir=$with_zstd_lib
+    fi
+    if test "$zstd_pkg_config" != yes; then
+      ZSTD_CFLAGS=
+      ZSTD_LIBS=
+      if test "x$zstd_include_dir" != x \
+        && test "x$zstd_include_dir" != xno; then
+       ZSTD_CFLAGS=-I$zstd_include_dir
+      fi
+    fi
+    if test "x$zstd_library_dir" != x \
+       && test "x$zstd_library_dir" != xno; then
+      ZSTD_LIBS="-L$zstd_library_dir $ZSTD_LIBS"
+    fi
+
+    CFLAGS="$saved_CFLAGS $ZSTD_CFLAGS"
+
+    # Check for a usable zstd library and header.  Ignore cached results
+    # that may have been obtained with different search flags.
+    unset ac_cv_search_ZSTD_compress ac_cv_zstd_h
+    zstd_lib_usable=no
+    LIBS="$ZSTD_LIBS $save_LIBS"
+
+    AC_SEARCH_LIBS([ZSTD_compress], [zstd],
+      [zstd_lib_usable=yes],
+      [zstd_lib_usable=no])
+    if test "$zstd_lib_usable" = yes \
+       && test "$ac_cv_search_ZSTD_compress" != "none required"; then
+      # Add the library only if AC_SEARCH_LIBS needed additional flags.
+      ZSTD_LIBS="$ac_cv_search_ZSTD_compress $ZSTD_LIBS"
     fi
-  ])
-  ])
+    LIBS="$save_LIBS"
+
+    AC_CACHE_CHECK([for zstd.h], [ac_cv_zstd_h],
+      [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+       #include <zstd.h>
+       #if ZSTD_VERSION_NUMBER < 10400
+       #error "need zstd 1.4.0 or better"
+       #endif
+      ]], [[]])],
+       [ac_cv_zstd_h=yes],
+       [ac_cv_zstd_h=no])])
+    if test "$zstd_pkg_config" = yes \
+       && { test "$zstd_lib_usable" != yes || test "$ac_cv_zstd_h" != yes; }; 
then
+      AC_MSG_NOTICE([pkg-config zstd is unusable; trying compiler search])
+      zstd_pkg_config=no
+      ZSTD_CFLAGS=
+      ZSTD_LIBS=
+    else
+      break
+    fi
+  done
+  AC_LANG_POP([C])
+  if test "$zstd_lib_usable" = yes && test "$ac_cv_zstd_h" = yes; then
+    AC_DEFINE(HAVE_ZSTD, 1, [Define if zstd is available.])
+  elif test "$with_zstd" != auto; then
+    AC_MSG_ERROR([Unable to find a usable zstd library and zstd.h.  See
+      config.log for details.])
+  else
+    # In auto mode, an unusable installation simply disables zstd.
+    zstd_enabled=no
+  fi
+
+  CFLAGS="$saved_CFLAGS"
+fi
+if test "$zstd_enabled" != yes; then
+  ZSTD_CFLAGS=
+  ZSTD_LIBS=
+fi
 ])

Reply via email to