From: Torbjörn SVENSSON <[email protected]>

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 | 130 ++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 122 insertions(+), 8 deletions(-)

diff --git a/config/zstd.m4 b/config/zstd.m4
index eb34c0f36d7b0..cb4c5e9fb9f86 100644
--- a/config/zstd.m4
+++ b/config/zstd.m4
@@ -5,19 +5,133 @@ 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], [
+ZSTD_INCLUDE=
+AC_SUBST(ZSTD_INCLUDE)
+ZSTD_LIB=
+AC_SUBST(ZSTD_LIB)
+ZSTD_CPPFLAGS=
+AC_SUBST(ZSTD_CPPFLAGS)
+ZSTD_LDFLAGS=
+AC_SUBST(ZSTD_LDFLAGS)
 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_LIBRARY_DIR=
+ZSTD_PKG_LIBS=
+case "x$with_zstd" in
+  xno)
+    ;;
+  xyes|xauto)
+    zstd_enabled=yes
+    zstd_use_pkg_config=yes
+    ;;
+  *)
+    zstd_enabled=yes
+    ZSTD_INCLUDE=$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
+    ZSTD_CPPFLAGS="$ZSTD_CFLAGS"
+    ZSTD_PKG_LIBS="$ZSTD_LIBS"
+    ZSTD_LIB="$ZSTD_PKG_LIBS"
   ], [
     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_CPPFLAGS="-I$with_zstd_include $ZSTD_CPPFLAGS"
+    else
+      ZSTD_INCLUDE=$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
+    if test "x$ZSTD_INCLUDE" != x \
+       && test "x$ZSTD_INCLUDE" != xno; then
+      ZSTD_CPPFLAGS=-I$ZSTD_INCLUDE
+    fi
+    if test "x$ZSTD_LIBRARY_DIR" != x \
+       && test "x$ZSTD_LIBRARY_DIR" != xno; then
+      ZSTD_LDFLAGS=-L$ZSTD_LIBRARY_DIR
+    fi
+  elif test "x$ZSTD_LIBRARY_DIR" != x; then
+    ZSTD_LDFLAGS=-L$ZSTD_LIBRARY_DIR
+  fi
+
+  saved_CPPFLAGS="$CPPFLAGS"
+  saved_LDFLAGS="$LDFLAGS"
+  CPPFLAGS="$CPPFLAGS $ZSTD_CPPFLAGS"
+  LDFLAGS="$LDFLAGS $ZSTD_LDFLAGS"
+
+  # Check for a usable zstd library and header.
+  zstd_lib_usable=no
+  save_LIBS="$LIBS"
+  LIBS="$ZSTD_PKG_LIBS"
+  AC_LANG_PUSH([C])
+  AC_SEARCH_LIBS([ZSTD_compress], [zstd],
+    [zstd_lib_usable=yes],
+    [zstd_lib_usable=no])
+  ZSTD_LIB="$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
+    ZSTD_CPPFLAGS=
+    ZSTD_LDFLAGS=
+    ZSTD_LIB=
+  fi
+
+  CPPFLAGS="$saved_CPPFLAGS"
+  LDFLAGS="$saved_LDFLAGS"
+fi
 ])
-- 
2.54.0

Reply via email to