autofs-5.1.6 - Use PKG_CHECK_MODULES to detect the libxml2 library

From: Hugh McMaster <hugh.mcmaster@outlook.com>

This patch replaces the use of xml2-config with PKG_CHECK_MODULES
to detect the libxml2 library.

Debian is removing xml2-config and now only supports pkg-config
and its macros.

This patch builds on Andreas Hasenack's initial patch [1] and
addresses feedback from Ian Kent [2].

[1] https://www.spinics.net/lists/autofs/msg02204.html
[2] https://www.spinics.net/lists/autofs/msg02206.html

Signed-off-by: Hugh McMaster <hugh.mcmaster@outlook.com>
Signed-off-by: Ian Kent <raven@themaw.net>
---
 CHANGELOG        |    1 
 Makefile.conf.in |    2 -
 aclocal.m4       |   32 -------------
 configure        |  131 ++++++++++++++++++++++++++++--------------------------
 configure.in     |    8 ++-
 daemon/Makefile  |    2 -
 lib/Makefile     |    2 -
 modules/Makefile |    2 -
 8 files changed, 79 insertions(+), 101 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index dc887617..12f1e2b4 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -14,6 +14,7 @@ xx/xx/2020 autofs-5.1.7
 - mount_nfs.c fix local rdma share not mounting.
 - configure.in: Remove unneeded second call to PKG_PROG_PKG_CONFIG.
 - configure.in: Do not append parentheses to PKG_PROG_PKG_CONFIG.
+- Use PKG_CHECK_MODULES to detect the libxml2 library.
 
 07/10/2019 autofs-5.1.6
 - support strictexpire mount option.
diff --git a/Makefile.conf.in b/Makefile.conf.in
index a1e5eada..cbc3c0d7 100644
--- a/Makefile.conf.in
+++ b/Makefile.conf.in
@@ -36,7 +36,7 @@ SSSD = @HAVE_SSS_AUTOFS@
 
 # SASL support: yes (1) no (0)
 XML_LIBS = @XML_LIBS@
-XML_FLAGS = @XML_FLAGS@
+XML_CFLAGS = @XML_CFLAGS@
 SASL = @HAVE_SASL@
 LIBSASL= @LIBSASL@
 SASL_FLAGS = @SASL_FLAGS@
diff --git a/aclocal.m4 b/aclocal.m4
index ec362bb4..33561aae 100644
--- a/aclocal.m4
+++ b/aclocal.m4
@@ -272,38 +272,6 @@ else
 fi])
 ])
 
-dnl --------------------------------------------------------------------------
-dnl AF_CHECK_LIBXML
-dnl
-dnl Check for lib xml
-dnl --------------------------------------------------------------------------
-AC_DEFUN([AF_CHECK_LIBXML],
-[AC_PATH_PROGS(XML_CONFIG, xml2-config, no)
-AC_MSG_CHECKING(for libxml2)
-if test "$XML_CONFIG" = "no"
-then
-  AC_MSG_RESULT(no)
-  HAVE_LIBXML=0
-else
-  AC_MSG_RESULT(yes)
-  HAVE_LIBXML=1
-  XML_LIBS=`$XML_CONFIG --libs`
-  XML_FLAGS=`$XML_CONFIG --cflags`
-  XML_VER=`$XML_CONFIG --version`
-  XML_MAJOR=`echo $XML_VER|cut -d\. -f1`
-  if test $XML_MAJOR -le 99
-  then
-    XML_MINOR=`echo $XML_VER|cut -d\. -f2`
-    if test $XML_MINOR -le 99
-    then
-      XML_REV=`echo $XML_VER|cut -d\. -f3`
-      if test $XML_REV -le 99; then
-        AC_DEFINE(LIBXML2_WORKAROUND,1, [Use libxml2 tsd usage workaround])
-      fi
-    fi
-  fi
-fi])
-
 dnl --------------------------------------------------------------------------
 dnl AF_CHECK_KRB5
 dnl
diff --git a/configure b/configure
index 9561ba0c..a2884397 100755
--- a/configure
+++ b/configure
@@ -629,8 +629,6 @@ KRB5_LIBS
 LIBSASL
 HAVE_SASL
 SASL_FLAGS
-XML_LIBS
-XML_FLAGS
 LIBLDAP
 HAVE_LDAP
 LDAP_FLAGS
@@ -647,7 +645,8 @@ NSL_LIBS
 NSL_CFLAGS
 LIBCLOCK_GETTIME
 KRB5_CONFIG
-XML_CONFIG
+XML_LIBS
+XML_CFLAGS
 ENABLE_FEDFS
 sssldir
 HAVE_SSS_AUTOFS
@@ -770,6 +769,8 @@ LIBS
 CPPFLAGS
 TIRPC_CFLAGS
 TIRPC_LIBS
+XML_CFLAGS
+XML_LIBS
 NSL_CFLAGS
 NSL_LIBS
 CPP'
@@ -1430,6 +1431,8 @@ Some influential environment variables:
   TIRPC_CFLAGS
               C compiler flags for TIRPC, overriding pkg-config
   TIRPC_LIBS  linker flags for TIRPC, overriding pkg-config
+  XML_CFLAGS  C compiler flags for XML, overriding pkg-config
+  XML_LIBS    linker flags for XML, overriding pkg-config
   NSL_CFLAGS  C compiler flags for NSL, overriding pkg-config
   NSL_LIBS    linker flags for NSL, overriding pkg-config
   CPP         C preprocessor
@@ -4370,81 +4373,83 @@ fi
 
 
 # LDAP SASL auth needs libxml and Kerberos
-for ac_prog in xml2-config
-do
-  # Extract the first word of "$ac_prog", so it can be a program name with args.
-set dummy $ac_prog; 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_XML_CONFIG+:} false; then :
-  $as_echo_n "(cached) " >&6
-else
-  case $XML_CONFIG in
-  [\\/]* | ?:[\\/]*)
-  ac_cv_path_XML_CONFIG="$XML_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_XML_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
+pkg_failed=no
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for XML" >&5
+$as_echo_n "checking for XML... " >&6; }
+
+if test -n "$XML_CFLAGS"; then
+    pkg_cv_XML_CFLAGS="$XML_CFLAGS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libxml-2.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libxml-2.0") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_XML_CFLAGS=`$PKG_CONFIG --cflags "libxml-2.0" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
+else
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
 fi
-XML_CONFIG=$ac_cv_path_XML_CONFIG
-if test -n "$XML_CONFIG"; then
-  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $XML_CONFIG" >&5
-$as_echo "$XML_CONFIG" >&6; }
+if test -n "$XML_LIBS"; then
+    pkg_cv_XML_LIBS="$XML_LIBS"
+ elif test -n "$PKG_CONFIG"; then
+    if test -n "$PKG_CONFIG" && \
+    { { $as_echo "$as_me:${as_lineno-$LINENO}: \$PKG_CONFIG --exists --print-errors \"libxml-2.0\""; } >&5
+  ($PKG_CONFIG --exists --print-errors "libxml-2.0") 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; then
+  pkg_cv_XML_LIBS=`$PKG_CONFIG --libs "libxml-2.0" 2>/dev/null`
+		      test "x$?" != "x0" && pkg_failed=yes
 else
-  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
-$as_echo "no" >&6; }
+  pkg_failed=yes
+fi
+ else
+    pkg_failed=untried
 fi
 
 
-  test -n "$XML_CONFIG" && break
-done
-test -n "$XML_CONFIG" || XML_CONFIG="no"
 
-{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for libxml2" >&5
-$as_echo_n "checking for libxml2... " >&6; }
-if test "$XML_CONFIG" = "no"
-then
-  { $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
+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 $_pkg_short_errors_supported = yes; then
+	        XML_PKG_ERRORS=`$PKG_CONFIG --short-errors --print-errors --cflags --libs "libxml-2.0" 2>&1`
+        else
+	        XML_PKG_ERRORS=`$PKG_CONFIG --print-errors --cflags --libs "libxml-2.0" 2>&1`
+        fi
+	# Put the nasty error message in config.log where it belongs
+	echo "$XML_PKG_ERRORS" >&5
+
+	HAVE_LIBXML=0
+elif test $pkg_failed = untried; then
+     	{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
 $as_echo "no" >&6; }
-  HAVE_LIBXML=0
+	HAVE_LIBXML=0
 else
-  { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
+	XML_CFLAGS=$pkg_cv_XML_CFLAGS
+	XML_LIBS=$pkg_cv_XML_LIBS
+        { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
 $as_echo "yes" >&6; }
+
   HAVE_LIBXML=1
-  XML_LIBS=`$XML_CONFIG --libs`
-  XML_FLAGS=`$XML_CONFIG --cflags`
-  XML_VER=`$XML_CONFIG --version`
-  XML_MAJOR=`echo $XML_VER|cut -d\. -f1`
-  if test $XML_MAJOR -le 99
-  then
-    XML_MINOR=`echo $XML_VER|cut -d\. -f2`
-    if test $XML_MINOR -le 99
-    then
-      XML_REV=`echo $XML_VER|cut -d\. -f3`
-      if test $XML_REV -le 99; then
 
 $as_echo "#define LIBXML2_WORKAROUND 1" >>confdefs.h
 
-      fi
-    fi
-  fi
+
 fi
+
 for ac_prog in krb5-config
 do
   # Extract the first word of "$ac_prog", so it can be a program name with args.
diff --git a/configure.in b/configure.in
index 525f79be..9e211c91 100644
--- a/configure.in
+++ b/configure.in
@@ -210,7 +210,11 @@ fi
 AC_SUBST(ENABLE_FEDFS)
 
 # LDAP SASL auth needs libxml and Kerberos
-AF_CHECK_LIBXML()
+PKG_CHECK_MODULES([XML], [libxml-2.0], [
+  HAVE_LIBXML=1
+  AC_DEFINE(LIBXML2_WORKAROUND, 1, [Use libxml2 tsd usage workaround])
+], [HAVE_LIBXML=0])
+
 AF_CHECK_KRB5()
 
 AC_SEARCH_LIBS([versionsort],[])
@@ -344,7 +348,7 @@ then
 	fi
 fi
 
-AC_SUBST(XML_FLAGS)
+AC_SUBST(XML_CFLAGS)
 AC_SUBST(XML_LIBS)
 AC_SUBST(SASL_FLAGS)
 AC_SUBST(HAVE_SASL)
diff --git a/daemon/Makefile b/daemon/Makefile
index a7ce430e..d8e2d6a4 100644
--- a/daemon/Makefile
+++ b/daemon/Makefile
@@ -28,7 +28,7 @@ ifeq ($(SYSTEMD), 1)
 endif
 
 ifeq ($(LDAP), 1)
-    CFLAGS += $(XML_FLAGS)
+    CFLAGS += $(XML_CFLAGS)
     LIBS += $(XML_LIBS)
 endif
 
diff --git a/lib/Makefile b/lib/Makefile
index 518b483a..b20b95b4 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -25,7 +25,7 @@ CFLAGS += -DAUTOFS_MAP_DIR=\"$(autofsmapdir)\"
 CFLAGS += -DAUTOFS_CONF_DIR=\"$(autofsconfdir)\"
 
 ifeq ($(LDAP), 1)
-  CFLAGS += $(XML_FLAGS) $(XML_LIBS)
+  CFLAGS += $(XML_CFLAGS) $(XML_LIBS)
 endif
 
 .PHONY: all install clean
diff --git a/modules/Makefile b/modules/Makefile
index 0447559a..989c6c1c 100644
--- a/modules/Makefile
+++ b/modules/Makefile
@@ -47,7 +47,7 @@ endif
 ifeq ($(LDAP), 1)
   SRCS += lookup_ldap.c
   MODS += lookup_ldap.so
-  LDAP_FLAGS += $(XML_FLAGS) -DLDAP_THREAD_SAFE
+  LDAP_FLAGS += $(XML_CFLAGS) -DLDAP_THREAD_SAFE
   LIBLDAP += $(XML_LIBS)
   ifeq ($(SASL), 1)
     SASL_OBJ = cyrus-sasl.o cyrus-sasl-extern.o
