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

Replace GCC's local zstd checks with AC_ZSTD.
Keep defining HAVE_ZSTD_H for GCC when zstd support is enabled.

gcc/ChangeLog:

        * aclocal.m4: Add includes for pkg.m4 and zstd.m4.
        * configure: Regenerate.
        * configure.ac: Use AC_ZSTD and define HAVE_ZSTD_H when enabled.

Signed-off-by: Torbjörn SVENSSON <[email protected]>
---
 gcc/aclocal.m4   |   2 +
 gcc/configure    | 516 +++++++++++++++++++++++++++++++++++++----------
 gcc/configure.ac |  80 +-------
 3 files changed, 416 insertions(+), 182 deletions(-)

diff --git a/gcc/aclocal.m4 b/gcc/aclocal.m4
index e44fc5fb2ab66..b0c66709415bd 100644
--- a/gcc/aclocal.m4
+++ b/gcc/aclocal.m4
@@ -89,8 +89,10 @@ m4_include([../config/nls.m4])
 m4_include([../config/override.m4])
 m4_include([../config/picflag.m4])
 m4_include([../config/po.m4])
+m4_include([../config/pkg.m4])
 m4_include([../config/progtest.m4])
 m4_include([../config/stdint.m4])
 m4_include([../config/warnings.m4])
 m4_include([../config/zlib.m4])
+m4_include([../config/zstd.m4])
 m4_include([acinclude.m4])
diff --git a/gcc/configure b/gcc/configure
index 2d211a0beaea7..caf2e1ec4a4c6 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -811,6 +811,11 @@ manext
 LIBICONV_DEP
 LTLIBICONV
 LIBICONV
+ZSTD_LIBS
+ZSTD_CFLAGS
+PKG_CONFIG_LIBDIR
+PKG_CONFIG_PATH
+PKG_CONFIG
 ZSTD_LDFLAGS
 ZSTD_CPPFLAGS
 ZSTD_LIB
@@ -1088,6 +1093,11 @@ CXXFLAGS
 CCC
 CXXCPP
 CPP
+PKG_CONFIG
+PKG_CONFIG_PATH
+PKG_CONFIG_LIBDIR
+ZSTD_CFLAGS
+ZSTD_LIBS
 GMPLIBS
 GMPINC
 ISLLIBS
@@ -1905,8 +1915,11 @@ Optional Packages:
                           Multi-libs configuration string (RISC-V only)
   --with-picolibc         Support for picolibc, including command line options
                           and spec rules
-  --with-zstd=PATH        specify prefix directory for installed zstd library.
-                          Equivalent to --with-zstd-include=PATH/include plus
+  --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-include=PATH
                           specify directory for installed zstd include files
@@ -1962,6 +1975,13 @@ Some influential environment variables:
   CXXFLAGS    C++ compiler flags
   CXXCPP      C++ preprocessor
   CPP         C preprocessor
+  PKG_CONFIG  path to pkg-config utility
+  PKG_CONFIG_PATH
+              directories to add to pkg-config's search path
+  PKG_CONFIG_LIBDIR
+              path overriding pkg-config's built-in search path
+  ZSTD_CFLAGS C compiler flags for ZSTD, overriding pkg-config
+  ZSTD_LIBS   linker flags for ZSTD, overriding pkg-config
   GMPLIBS     How to link GMP
   GMPINC      How to find GMP include files
   ISLLIBS     How to link isl
@@ -2799,6 +2819,52 @@ $as_echo "$ac_res" >&6; }
 
 } # ac_fn_cxx_check_header_preproc
 
+# ac_fn_c_try_link LINENO
+# -----------------------
+# Try to link conftest.$ac_ext, and return whether this succeeded.
+ac_fn_c_try_link ()
+{
+  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
+  rm -f conftest.$ac_objext conftest$ac_exeext
+  if { { ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
+$as_echo "$ac_try_echo"; } >&5
+  (eval "$ac_link") 2>conftest.err
+  ac_status=$?
+  if test -s conftest.err; then
+    grep -v '^ *+' conftest.err >conftest.er1
+    cat conftest.er1 >&5
+    mv -f conftest.er1 conftest.err
+  fi
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; } && {
+        test -z "$ac_c_werror_flag" ||
+        test ! -s conftest.err
+       } && test -s conftest$ac_exeext && {
+        test "$cross_compiling" = yes ||
+        test -x conftest$ac_exeext
+       }; then :
+  ac_retval=0
+else
+  $as_echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+       ac_retval=1
+fi
+  # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information
+  # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would
+  # interfere with the next link command; also delete a directory that is
+  # left behind by Apple's compiler.  We do this before executing the actions.
+  rm -rf conftest.dSYM conftest_ipa8_conftest.oo
+  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
+  as_fn_set_status $ac_retval
+
+} # ac_fn_c_try_link
+
 # ac_fn_cxx_check_func LINENO FUNC VAR
 # ------------------------------------
 # Tests whether FUNC exists, setting the cache variable VAR accordingly
@@ -2912,52 +2978,6 @@ $as_echo "$ac_res" >&6; }
 
 } # ac_fn_cxx_check_decl
 
-# ac_fn_c_try_link LINENO
-# -----------------------
-# Try to link conftest.$ac_ext, and return whether this succeeded.
-ac_fn_c_try_link ()
-{
-  as_lineno=${as_lineno-"$1"} as_lineno_stack=as_lineno_stack=$as_lineno_stack
-  rm -f conftest.$ac_objext conftest$ac_exeext
-  if { { ac_try="$ac_link"
-case "(($ac_try" in
-  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
-  *) ac_try_echo=$ac_try;;
-esac
-eval ac_try_echo="\"\$as_me:${as_lineno-$LINENO}: $ac_try_echo\""
-$as_echo "$ac_try_echo"; } >&5
-  (eval "$ac_link") 2>conftest.err
-  ac_status=$?
-  if test -s conftest.err; then
-    grep -v '^ *+' conftest.err >conftest.er1
-    cat conftest.er1 >&5
-    mv -f conftest.er1 conftest.err
-  fi
-  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
-  test $ac_status = 0; } && {
-        test -z "$ac_c_werror_flag" ||
-        test ! -s conftest.err
-       } && test -s conftest$ac_exeext && {
-        test "$cross_compiling" = yes ||
-        test -x conftest$ac_exeext
-       }; then :
-  ac_retval=0
-else
-  $as_echo "$as_me: failed program was:" >&5
-sed 's/^/| /' conftest.$ac_ext >&5
-
-       ac_retval=1
-fi
-  # Delete the IPA/IPO (Inter Procedural Analysis/Optimization) information
-  # created by the PGI compiler (conftest_ipa8_conftest.oo), as it would
-  # interfere with the next link command; also delete a directory that is
-  # left behind by Apple's compiler.  We do this before executing the actions.
-  rm -rf conftest.dSYM conftest_ipa8_conftest.oo
-  eval $as_lineno_stack; ${as_lineno_stack:+:} unset as_lineno
-  as_fn_set_status $ac_retval
-
-} # ac_fn_c_try_link
-
 # ac_fn_c_check_header_preproc LINENO HEADER VAR
 # ----------------------------------------------
 # Tests whether HEADER is present, setting the cache variable VAR accordingly.
@@ -10501,18 +10521,141 @@ $as_echo "#define HAVE_AFUNIX_H 1" >>confdefs.h
 fi
 
 # Look for the ZSTD package.
+
+
+
+
+
+
+
+if test "x$ac_cv_env_PKG_CONFIG_set" != "xset"; then
+       if test -n "$ac_tool_prefix"; then
+  # Extract the first word of "${ac_tool_prefix}pkg-config", so it can be a 
program name with args.
+set dummy ${ac_tool_prefix}pkg-config; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_PKG_CONFIG+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $PKG_CONFIG in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_PKG_CONFIG="$PKG_CONFIG" # Let the user override the test with a 
path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_PKG_CONFIG="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" 
>&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+PKG_CONFIG=$ac_cv_path_PKG_CONFIG
+if test -n "$PKG_CONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $PKG_CONFIG" >&5
+$as_echo "$PKG_CONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+
+fi
+if test -z "$ac_cv_path_PKG_CONFIG"; then
+  ac_pt_PKG_CONFIG=$PKG_CONFIG
+  # Extract the first word of "pkg-config", so it can be a program name with 
args.
+set dummy pkg-config; ac_word=$2
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $ac_word" >&5
+$as_echo_n "checking for $ac_word... " >&6; }
+if ${ac_cv_path_ac_pt_PKG_CONFIG+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  case $ac_pt_PKG_CONFIG in
+  [\\/]* | ?:[\\/]*)
+  ac_cv_path_ac_pt_PKG_CONFIG="$ac_pt_PKG_CONFIG" # Let the user override the 
test with a path.
+  ;;
+  *)
+  as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+    for ac_exec_ext in '' $ac_executable_extensions; do
+  if as_fn_executable_p "$as_dir/$ac_word$ac_exec_ext"; then
+    ac_cv_path_ac_pt_PKG_CONFIG="$as_dir/$ac_word$ac_exec_ext"
+    $as_echo "$as_me:${as_lineno-$LINENO}: found $as_dir/$ac_word$ac_exec_ext" 
>&5
+    break 2
+  fi
+done
+  done
+IFS=$as_save_IFS
+
+  ;;
+esac
+fi
+ac_pt_PKG_CONFIG=$ac_cv_path_ac_pt_PKG_CONFIG
+if test -n "$ac_pt_PKG_CONFIG"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_pt_PKG_CONFIG" >&5
+$as_echo "$ac_pt_PKG_CONFIG" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+fi
+
+  if test "x$ac_pt_PKG_CONFIG" = x; then
+    PKG_CONFIG=""
+  else
+    case $cross_compiling:$ac_tool_warned in
+yes:)
+{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: using cross tools not 
prefixed with host triplet" >&5
+$as_echo "$as_me: WARNING: using cross tools not prefixed with host triplet" 
>&2;}
+ac_tool_warned=yes ;;
+esac
+    PKG_CONFIG=$ac_pt_PKG_CONFIG
+  fi
+else
+  PKG_CONFIG="$ac_cv_path_PKG_CONFIG"
+fi
+
+fi
+if test -n "$PKG_CONFIG"; then
+       _pkg_min_version=0.9.0
+       { $as_echo "$as_me:${as_lineno-$LINENO}: checking pkg-config is at 
least version $_pkg_min_version" >&5
+$as_echo_n "checking pkg-config is at least version $_pkg_min_version... " 
>&6; }
+       if $PKG_CONFIG --atleast-pkgconfig-version $_pkg_min_version; then
+               { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+       else
+               { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+               PKG_CONFIG=""
+       fi
+fi
+
 ZSTD_INCLUDE=
-ZSTD_LIB=
 
+ZSTD_LIB=
 
 ZSTD_CPPFLAGS=
-ZSTD_LDFLAGS=
 
+ZSTD_LDFLAGS=
 
 
 # Check whether --with-zstd was given.
 if test "${with_zstd+set}" = set; then :
-  withval=$with_zstd;
+  withval=$with_zstd; with_zstd=$withval
+else
+  with_zstd=auto
 fi
 
 
@@ -10527,42 +10670,190 @@ if test "${with_zstd_lib+set}" = set; then :
   withval=$with_zstd_lib;
 fi
 
+zstd_enabled=no
+zstd_pkg_config=no
+zstd_use_pkg_config=no
+ZSTD_LIBRARY_DIR=
+ZSTD_PKG_LIBS=
 case "x$with_zstd" in
-  x) ;;
   xno)
-    ZSTD_INCLUDE=
-    ZSTD_LIB=
     ;;
-  *) ZSTD_INCLUDE=$with_zstd/include
-     ZSTD_LIB=$with_zstd/lib
-     ;;
+  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
 
-if test "x$with_zstd" != xno; then
-if test "x$with_zstd_include" != x; then
-  ZSTD_INCLUDE=$with_zstd_include
+if test "$zstd_use_pkg_config" = yes; then
+
+pkg_failed=no
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libzstd >= 1.4.0" >&5
+$as_echo_n "checking for libzstd >= 1.4.0... " >&6; }
+
+if test -n "$ZSTD_CFLAGS"; then
+    pkg_cv_ZSTD_CFLAGS="$ZSTD_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists 
--print-errors \"libzstd >= 1.4.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libzstd >= 1.4.0") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_ZSTD_CFLAGS=`$PKG_CONFIG --cflags "libzstd >= 1.4.0" 2>/dev/null`
+                     test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+if test -n "$ZSTD_LIBS"; then
+    pkg_cv_ZSTD_LIBS="$ZSTD_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists 
--print-errors \"libzstd >= 1.4.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libzstd >= 1.4.0") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_ZSTD_LIBS=`$PKG_CONFIG --libs "libzstd >= 1.4.0" 2>/dev/null`
+                     test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
+fi
+
+if test $pkg_failed = no; then
+  pkg_save_LDFLAGS="$LDFLAGS"
+  LDFLAGS="$LDFLAGS $pkg_cv_ZSTD_LIBS"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_link "$LINENO"; then :
+
+else
+  pkg_failed=yes
 fi
-if test "x$with_zstd_lib" != x; then
-  ZSTD_LIB=$with_zstd_lib
+rm -f core conftest.err conftest.$ac_objext \
+    conftest$ac_exeext conftest.$ac_ext
+  LDFLAGS=$pkg_save_LDFLAGS
+fi
+
+
+
+if test $pkg_failed = yes; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+if $PKG_CONFIG --atleast-pkgconfig-version 0.20; then
+        _pkg_short_errors_supported=yes
+else
+        _pkg_short_errors_supported=no
 fi
-if test "x$ZSTD_INCLUDE" != x \
-   && test "x$ZSTD_INCLUDE" != xno; then
-  ZSTD_CPPFLAGS=-I$ZSTD_INCLUDE
+        if test $_pkg_short_errors_supported = yes; then
+               ZSTD_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors 
--cflags --libs "libzstd >= 1.4.0" 2>&1`
+        else
+               ZSTD_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs 
"libzstd >= 1.4.0" 2>&1`
+        fi
+       # Put the nasty error message in config.log where it belongs
+       echo "$ZSTD_PKG_ERRORS" >&5
+
+
+    if test "$with_zstd" = yes; then
+      as_fn_error $? "--with-zstd=yes was given, but pkg-config could not find
+       libzstd >= 1.4.0" "$LINENO" 5
+    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
+
+elif test $pkg_failed = untried; then
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+$as_echo "no" >&6; }
+
+    if test "$with_zstd" = yes; then
+      as_fn_error $? "--with-zstd=yes was given, but pkg-config could not find
+       libzstd >= 1.4.0" "$LINENO" 5
+    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
+
+else
+       ZSTD_CFLAGS=$pkg_cv_ZSTD_CFLAGS
+       ZSTD_LIBS=$pkg_cv_ZSTD_LIBS
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+$as_echo "yes" >&6; }
+
+    zstd_pkg_config=yes
+    ZSTD_CPPFLAGS="$ZSTD_CFLAGS"
+    ZSTD_PKG_LIBS="$ZSTD_LIBS"
+    ZSTD_LIB="$ZSTD_PKG_LIBS"
+
 fi
-if test "x$ZSTD_LIB" != x \
-   && test "x$ZSTD_LIB" != xno; then
-  ZSTD_LDFLAGS=-L$ZSTD_LIB
 fi
 
-saved_CXXFLAGS="$CXXFLAGS"
-saved_LDFLAGS="$LDFLAGS"
-CXXFLAGS="$CXXFLAGS $ZSTD_CPPFLAGS"
-LDFLAGS="$LDFLAGS $ZSTD_LDFLAGS"
+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
 
-# LTO can use zstd compression algorithm
-save_LIBS="$LIBS"
-LIBS=
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing 
ZSTD_compress" >&5
+  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_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext 
$LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for library containing 
ZSTD_compress" >&5
 $as_echo_n "checking for library containing ZSTD_compress... " >&6; }
 if ${ac_cv_search_ZSTD_compress+:} false; then :
   $as_echo_n "(cached) " >&6
@@ -10593,7 +10884,7 @@ for ac_lib in '' zstd; do
     ac_res=-l$ac_lib
     LIBS="-l$ac_lib  $ac_func_search_save_LIBS"
   fi
-  if ac_fn_cxx_try_link "$LINENO"; then :
+  if ac_fn_c_try_link "$LINENO"; then :
   ac_cv_search_ZSTD_compress=$ac_res
 fi
 rm -f core conftest.err conftest.$ac_objext \
@@ -10615,28 +10906,27 @@ $as_echo "$ac_cv_search_ZSTD_compress" >&6; }
 ac_res=$ac_cv_search_ZSTD_compress
 if test "$ac_res" != no; then :
   test "$ac_res" = "none required" || LIBS="$ac_res $LIBS"
-
+  zstd_lib_usable=yes
 else
-  gcc_cv_header_zstd_h=no
+  zstd_lib_usable=no
 fi
 
-ZSTD_LIB="$LIBS"
-LIBS="$save_LIBS"
+  ZSTD_LIB="$LIBS"
+  LIBS="$save_LIBS"
 
-
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for zstd.h" >&5
+  { $as_echo "$as_me:${as_lineno-$LINENO}: checking for zstd.h" >&5
 $as_echo_n "checking for zstd.h... " >&6; }
-if ${gcc_cv_header_zstd_h+:} false; then :
+if ${ac_cv_zstd_h+:} false; then :
   $as_echo_n "(cached) " >&6
 else
-  # We require version 1.3.0 or later.  This is the first version that has
-# ZSTD_getFrameContentSize.
-cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
 /* end confdefs.h.  */
-#include <zstd.h>
-#if ZSTD_VERSION_NUMBER < 10300
-#error "need zstd 1.3.0 or better"
-#endif
+
+      #include <zstd.h>
+      #if ZSTD_VERSION_NUMBER < 10400
+      #error "need zstd 1.4.0 or better"
+      #endif
+
 int
 main ()
 {
@@ -10645,26 +10935,44 @@ main ()
   return 0;
 }
 _ACEOF
-if ac_fn_cxx_try_compile "$LINENO"; then :
-  gcc_cv_header_zstd_h=yes
+if ac_fn_c_try_compile "$LINENO"; then :
+  ac_cv_zstd_h=yes
 else
-  gcc_cv_header_zstd_h=no
+  ac_cv_zstd_h=no
 fi
 rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
 fi
+{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_zstd_h" >&5
+$as_echo "$ac_cv_zstd_h" >&6; }
+  ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS 
conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
 
-{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_header_zstd_h" >&5
-$as_echo "$gcc_cv_header_zstd_h" >&6; }
-if test $gcc_cv_header_zstd_h = yes; then
+  if test "$zstd_lib_usable" = yes && test "$ac_cv_zstd_h" = yes; then
 
-$as_echo "#define HAVE_ZSTD_H 1" >>confdefs.h
+$as_echo "#define HAVE_ZSTD 1" >>confdefs.h
+
+  elif test "$with_zstd" != auto; then
+    as_fn_error $? "Unable to find a usable zstd library and zstd.h.  See
+      config.log for details." "$LINENO" 5
+  else
+    # In auto mode, an unusable installation simply disables zstd.
+    zstd_enabled=no
+    ZSTD_CPPFLAGS=
+    ZSTD_LDFLAGS=
+    ZSTD_LIB=
+  fi
 
-elif test "x$with_zstd" != x; then
-    as_fn_error $? "Unable to find zstd.h.  See config.log for details." 
"$LINENO" 5
+  CPPFLAGS="$saved_CPPFLAGS"
+  LDFLAGS="$saved_LDFLAGS"
 fi
 
-CXXFLAGS="$saved_CXXFLAGS"
-LDFLAGS="$saved_LDFLAGS"
+if test "$zstd_enabled" = yes; then
+
+$as_echo "#define HAVE_ZSTD_H 1" >>confdefs.h
+
 fi
 
 
@@ -21987,7 +22295,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 21990 "configure"
+#line 22298 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -22093,7 +22401,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 22096 "configure"
+#line 22404 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
diff --git a/gcc/configure.ac b/gcc/configure.ac
index a4878c4a9f965..369b7b642c08c 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1490,86 +1490,10 @@ if test $gcc_cv_header_afunix_h = yes; then
 fi
 
 # Look for the ZSTD package.
-ZSTD_INCLUDE=
-ZSTD_LIB=
-AC_SUBST(ZSTD_INCLUDE)
-AC_SUBST(ZSTD_LIB)
-ZSTD_CPPFLAGS=
-ZSTD_LDFLAGS=
-AC_SUBST(ZSTD_CPPFLAGS)
-AC_SUBST(ZSTD_LDFLAGS)
-AC_ARG_WITH(zstd,
-       [AS_HELP_STRING([--with-zstd=PATH],
-               [specify prefix directory for installed zstd library.
-                Equivalent to --with-zstd-include=PATH/include
-                plus --with-zstd-lib=PATH/lib])])
-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])])
-case "x$with_zstd" in
-  x) ;;
-  xno)
-    ZSTD_INCLUDE=
-    ZSTD_LIB=
-    ;;
-  *) ZSTD_INCLUDE=$with_zstd/include
-     ZSTD_LIB=$with_zstd/lib
-     ;;
-esac
-
-if test "x$with_zstd" != xno; then
-if test "x$with_zstd_include" != x; then
-  ZSTD_INCLUDE=$with_zstd_include
-fi
-if test "x$with_zstd_lib" != x; then
-  ZSTD_LIB=$with_zstd_lib
-fi
-if test "x$ZSTD_INCLUDE" != x \
-   && test "x$ZSTD_INCLUDE" != xno; then
-  ZSTD_CPPFLAGS=-I$ZSTD_INCLUDE
-fi
-if test "x$ZSTD_LIB" != x \
-   && test "x$ZSTD_LIB" != xno; then
-  ZSTD_LDFLAGS=-L$ZSTD_LIB
-fi
-
-saved_CXXFLAGS="$CXXFLAGS"
-saved_LDFLAGS="$LDFLAGS"
-CXXFLAGS="$CXXFLAGS $ZSTD_CPPFLAGS"
-LDFLAGS="$LDFLAGS $ZSTD_LDFLAGS"
-
-# LTO can use zstd compression algorithm
-save_LIBS="$LIBS"
-LIBS=
-AC_SEARCH_LIBS(ZSTD_compress, zstd,, gcc_cv_header_zstd_h=no)
-ZSTD_LIB="$LIBS"
-LIBS="$save_LIBS"
-AC_SUBST(ZSTD_LIB)
-
-AC_MSG_CHECKING(for zstd.h)
-AC_CACHE_VAL(gcc_cv_header_zstd_h,
-# We require version 1.3.0 or later.  This is the first version that has
-# ZSTD_getFrameContentSize.
-[AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
-[[#include <zstd.h>
-#if ZSTD_VERSION_NUMBER < 10300
-#error "need zstd 1.3.0 or better"
-#endif]])],
-  [gcc_cv_header_zstd_h=yes],
-  [gcc_cv_header_zstd_h=no])])
-AC_MSG_RESULT($gcc_cv_header_zstd_h)
-if test $gcc_cv_header_zstd_h = yes; then
+AC_ZSTD
+if test "$zstd_enabled" = yes; then
   AC_DEFINE(HAVE_ZSTD_H, 1,
        [Define if you have a working <zstd.h> header file.])
-elif test "x$with_zstd" != x; then
-    as_fn_error $? "Unable to find zstd.h.  See config.log for details." 
"$LINENO" 5
-fi
-
-CXXFLAGS="$saved_CXXFLAGS"
-LDFLAGS="$saved_LDFLAGS"
 fi
 
 dnl Disabled until we have a complete test for buggy enum bitfields.
-- 
2.54.0

Reply via email to