Extend AC_ZSTD to support pkg-config detection, installation prefixes,
and custom include and library paths. 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]>
---
 config/zstd.m4 | 119 +++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 111 insertions(+), 8 deletions(-)

diff --git a/config/zstd.m4 b/config/zstd.m4
index eb34c0f36d7..f1ea5cf980d 100644
--- a/config/zstd.m4
+++ b/config/zstd.m4
@@ -5,19 +5,122 @@ 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_HELP_STRING([--with-zstd=VALUE],
+    [VALUE may be 'auto' (detect with pkg-config; 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
 
-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 "$zstd_use_pkg_config" = yes; then
+  PKG_CHECK_MODULES([ZSTD], [libzstd >= 1.4.0], [
+    zstd_pkg_config=yes
   ], [
     if test "$with_zstd" = yes; then
-      AC_MSG_ERROR([--with-zstd was given, but pkgconfig/libzstd.pc is not 
found])
+      AC_MSG_ERROR([--with-zstd=yes was given, but pkg-config could not find
+       libzstd >= 1.4.0])
+    elif test "x$with_zstd_include" != x \
+         || test "x$with_zstd_lib" != x; then
+      # Try the explicitly supplied paths below.
+      :
+    else
+      zstd_enabled=no
     fi
   ])
-  ])
+fi
+
+if test "$zstd_enabled" = yes; then
+  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
+
+  saved_CFLAGS="$CFLAGS"
+  CFLAGS="$CFLAGS $ZSTD_CFLAGS"
+
+  # Check for a usable zstd library and header.
+  zstd_lib_usable=no
+  save_LIBS="$LIBS"
+  LIBS="$ZSTD_LIBS"
+  AC_LANG_PUSH([C])
+  AC_SEARCH_LIBS([ZSTD_compress], [zstd],
+    [zstd_lib_usable=yes],
+    [zstd_lib_usable=no])
+  ZSTD_LIBS="$LIBS"
+  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])])
+  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
 ])
-- 
2.43.0

Reply via email to