From 2698385647a6ebd58b5d25147333e494c3da2409 Mon Sep 17 00:00:00 2001
From: Petr Mensik <pemensik@redhat.com>
Date: Fri, 9 Feb 2018 15:24:53 +0100
Subject: [PATCH] Support for isc-config.sh script on --with-libbind parameter

Move checks only to isc-config section

Fix detection of bind flags from config

Add support for with-libbind=config, Improve help message
---
 client/Makefile.am       |   7 +-
 client/tests/Makefile.am |   8 +--
 common/tests/Makefile.am |  31 ++-------
 configure.ac             | 143 ++++++++++++++++++++++++++-------------
 dhcpctl/Makefile.am      |  15 +---
 omapip/Makefile.am       |  12 +---
 relay/Makefile.am        |   7 +-
 relay/tests/Makefile.am  |   6 +-
 server/Makefile.am       |   6 +-
 server/tests/Makefile.am |   8 +--
 10 files changed, 124 insertions(+), 119 deletions(-)

diff --git a/client/Makefile.am b/client/Makefile.am
index 0689185..348d2ab 100644
--- a/client/Makefile.am
+++ b/client/Makefile.am
@@ -4,6 +4,8 @@
 # production code. Sadly, we are not there yet.
 SUBDIRS = . tests
 
+BIND_LIBS = @BIND_LIBS@
+
 AM_CPPFLAGS = -DCLIENT_PATH='"PATH=$(sbindir):/sbin:/bin:/usr/sbin:/usr/bin"'
 AM_CPPFLAGS += -DLOCALSTATEDIR='"$(localstatedir)"'
 
@@ -14,10 +16,7 @@ dhclient_SOURCES = client_tables.c clparse.c dhclient.c dhc6.c \
 		   scripts/netbsd scripts/nextstep scripts/openbsd \
 		   scripts/solaris scripts/openwrt
 dhclient_LDADD = ../common/libdhcp.@A@ ../omapip/libomapi.@A@ \
-		 @BINDLIBIRSDIR@/libirs.@A@ \
-		 @BINDLIBDNSDIR@/libdns.@A@ \
-		 @BINDLIBISCCFGDIR@/libisccfg.@A@ \
-		 @BINDLIBISCDIR@/libisc.@A@ \
+		 $(BIND_LIBS) \
 		 $(CAPNG_LDADD)
 man_MANS = dhclient.8 dhclient-script.8 dhclient.conf.5 dhclient.leases.5
 EXTRA_DIST = $(man_MANS)
diff --git a/client/tests/Makefile.am b/client/tests/Makefile.am
index 8f1f058..13ff931 100644
--- a/client/tests/Makefile.am
+++ b/client/tests/Makefile.am
@@ -1,7 +1,8 @@
 SUBDIRS = .
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = $(ATF_CFLAGS) -DUNIT_TEST -I$(top_srcdir)/includes
-AM_CPPFLAGS += -I@BINDDIR@/include -I$(top_srcdir)
+AM_CPPFLAGS += @BIND_CPPFLAGS@ -I$(top_srcdir)
 AM_CPPFLAGS += -DLOCALSTATEDIR='"."'
 AM_CPPFLAGS += -DCLIENT_PATH='"."'
 
@@ -19,10 +20,7 @@ DHCPSRC = ../clparse.c ../dhc6.c ../dhclient.c
 DHCPLIBS = $(top_builddir)/common/libdhcp.@A@ \
 	  $(top_builddir)/omapip/libomapi.@A@    \
           $(top_builddir)/dhcpctl/libdhcpctl.@A@ \
-	  @BINDLIBIRSDIR@/libirs.@A@ \
-	  @BINDLIBDNSDIR@/libdns.@A@ \
-	  @BINDLIBISCCFGDIR@/libisccfg.@A@ \
-	  @BINDLIBISCDIR@/libisc.@A@
+	  $(BIND_LIBS)
 
 ATF_TESTS =
 if HAVE_ATF
diff --git a/common/tests/Makefile.am b/common/tests/Makefile.am
index a515265..35fb93f 100644
--- a/common/tests/Makefile.am
+++ b/common/tests/Makefile.am
@@ -1,4 +1,5 @@
 SUBDIRS = .
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = $(ATF_CFLAGS) -I$(top_srcdir)/includes
 
@@ -14,51 +15,33 @@ ATF_TESTS += alloc_unittest dns_unittest misc_unittest ns_name_unittest \
 alloc_unittest_SOURCES = test_alloc.c $(top_srcdir)/tests/t_api_dhcp.c
 alloc_unittest_LDADD = $(ATF_LDFLAGS)
 alloc_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+			$(BIND_LIBS)
 
 dns_unittest_SOURCES = dns_unittest.c $(top_srcdir)/tests/t_api_dhcp.c
 dns_unittest_LDADD = $(ATF_LDFLAGS)
 dns_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+		      $(BIND_LIBS)
 
 misc_unittest_SOURCES = misc_unittest.c $(top_srcdir)/tests/t_api_dhcp.c
 misc_unittest_LDADD = $(ATF_LDFLAGS)
 misc_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+		       $(BIND_LIBS)
 
 ns_name_unittest_SOURCES = ns_name_test.c $(top_srcdir)/tests/t_api_dhcp.c
 ns_name_unittest_LDADD = $(ATF_LDFLAGS)
 ns_name_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+			  $(BIND_LIBS)
 
 option_unittest_SOURCES = option_unittest.c $(top_srcdir)/tests/t_api_dhcp.c
 option_unittest_LDADD = $(ATF_LDFLAGS)
 option_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+			 $(BIND_LIBS)
 
 domain_name_unittest_SOURCES = domain_name_test.c \
 	$(top_srcdir)/tests/t_api_dhcp.c
 domain_name_unittest_LDADD = $(ATF_LDFLAGS)
 domain_name_unittest_LDADD += ../libdhcp.@A@ ../../omapip/libomapi.@A@ \
-	@BINDLIBIRSDIR@/libirs.@A@ \
-	@BINDLIBDNSDIR@/libdns.@A@ \
-	@BINDLIBISCCFGDIR@/libisccfg.@A@  \
-	@BINDLIBISCDIR@/libisc.@A@
+			      $(BIND_LIBS)
 
 check: $(ATF_TESTS)
 	@if test $(top_srcdir) != ${top_builddir}; then \
diff --git a/configure.ac b/configure.ac
index e1ab5ea..52bab2a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -786,11 +786,11 @@ AC_CHECK_MEMBER(struct tpacket_auxdata.tp_vlan_tci,
 BINDSUBDIR=
 BINDDIR=
 BINDSRCDIR=
-BINDLIBIRSDIR=
-BINDLIBDNSDIR=
-BINDLIBISCCFGDIR=
-BINDLIBISCDIR=
 DISTCHECK_LIBBIND_CONFIGURE_FLAG=
+
+BIND_CONFIG=
+BIND_CPPFLAGS=
+BIND_LIBS=
 AC_ARG_WITH(libbind,
 	AS_HELP_STRING([--with-libbind=PATH],[bind includes and libraries are in PATH]),
 	use_libbind="$withval", use_libbind="no")
@@ -817,42 +817,107 @@ no)
 	fi
 	AC_CONFIG_FILES([$srcdir/bind/Makefile])
 
-	BINDLIBIRSDIR="$BINDSRCDIR/lib/irs"
-	BINDLIBDNSDIR="$BINDSRCDIR/lib/dns"
-	BINDLIBISCCFGDIR="$BINDSRCDIR/lib/isccfg"
-	BINDLIBISCDIR="$BINDSRCDIR/lib/isc"
 	;;
+config)
+        AC_PATH_PROG(BIND_CONFIG, [isc-config.sh bind9-config])
+        ;;
 *)
-	if test ! -d "$use_libbind"; then
-		AC_MSG_ERROR([Cannot find bind directory at $use_libbind])
-	fi
-	if test ! -d "$use_libbind/include" -o \
-	        ! -f "$use_libbind/include/isc/buffer.h"
-	then
-		AC_MSG_ERROR([Cannot find bind includes at $use_libbind/include])
-	fi
-	if test	! -d "$use_libbind/lib" -o \
-	        \( ! -f "$use_libbind/lib/libisc.a" -a \
-		   ! -f	"$use_libbind/lib/libisc.la" \)
-	then
-		AC_MSG_ERROR([Cannot find bind libraries at $use_libbind/lib])
+	if test -f "$use_libbind" -a -x "$use_libbind"; then
+	    # passed full path of isc-config.sh
+	    BIND_CONFIG="$use_libbind"
+	else
+	    if test ! -d "$use_libbind"; then
+		    AC_MSG_ERROR([Cannot find bind directory at $use_libbind])
+	    fi
+	    if test ! -d "$use_libbind/include" -o \
+	            ! -f "$use_libbind/include/isc/buffer.h"
+	    then
+		    AC_MSG_ERROR([Cannot find bind includes at $use_libbind/include])
+	    fi
+	    if test	! -d "$use_libbind/lib" -o \
+	                \( ! -f "$use_libbind/lib/libisc.a" -a \
+		        ! -f	"$use_libbind/lib/libisc.la" \)
+	    then
+		    AC_MSG_ERROR([Cannot find bind libraries at $use_libbind/lib])
+	    fi   
+	    BINDDIR="$use_libbind"
+	    DISTCHECK_LIBBIND_CONFIGURE_FLAG="--with-libbind=$use_libbind"
 	fi
-	BINDDIR="$use_libbind"
-	BINDLIBIRSDIR="$BINDDIR/lib"
-	BINDLIBDNSDIR="$BINDDIR/lib"
-	BINDLIBISCCFGDIR="$BINDDIR/lib"
-	BINDLIBISCDIR="$BINDDIR/lib"	
-	DISTCHECK_LIBBIND_CONFIGURE_FLAG="--with-libbind=$use_libbind"
 	;;
 esac
+
+if test -z "$BIND_CONFIG"; then
+   BIND_CPPFLAGS="-I${BINDDIR}/include"
+   BIND_LIBDIR="${BINDDIR}/lib"
+   BIND_LIBS="${BINDDIR}/lib/libirs.a ${BINDDIR}/lib/libdns.a ${BINDDIR}/lib//libisccfg.a  ${BINDDIR}/lib/libisc.a"
+else
+   BIND_CPPFLAGS=`$BIND_CONFIG --cflags`
+   BIND_LIBS=`$BIND_CONFIG --libs irs dns isccfg isc`
+
+   # bind is already built
+   AC_CHECKING([Checking bind libraries have no thread support])
+   saved_libs="$LIBS"
+   saved_CPPFLAGS="$CPPFLAGS"
+   CPPFLAGS="${CPPFLAGS} ${BIND_CPPFLAGS}"
+   LIBS="${LIBS} ${BIND_LIBS}"
+AC_TRY_LINK([
+#include <isc/bind9.h>
+#include <isc/lib.h>
+],[
+#ifdef BIND9
+#error Export BIND library has to be used with BIND version up to 9.9
+#endif
+isc_lib_register();
+], [AC_MSG_RESULT(Bind export library found)
+    BIND_EXPORT=yes], [BIND_EXPORT=no]
+)
+
+# Allow build with disabled threads for dhcp
+AC_TRY_LINK([
+#include <isc/platform.h>
+#include <isc/bind9.h>
+#include <isc/lib.h>
+],[
+#ifdef ISC_PLATFORM_USETHREADS
+#error Bind library must not be compiled with threads
+#endif
+isc_lib_register();
+], [AC_MSG_RESULT(Bind export library found)
+    BIND_EXPORT=yes], [BIND_EXPORT=no]
+)
+
+# Allow build with disabled threads for dhcp
+AC_TRY_LINK([
+#include <isc/platform.h>
+#include <isc/bind9.h>
+#include <isc/lib.h>
+],[
+#ifdef ISC_PLATFORM_USETHREADS
+#error Bind library must not be compiled with threads
+#endif
+isc_lib_register();
+if (isc_bind9 != 0) {}
+], [AC_MSG_RESULT(Bind single thread library found)
+    BIND_SINGLETHREAD=yes], [BIND_SINGLETHREAD=no]
+)
+
+   if test "x$BIND_EXPORT" != xyes -a "x$BIND_SINGLETHREADED" != xyes
+   then
+       AC_MSG_RESULT([BIND_CONFIG=${BIND_CONFIG}])
+       AC_MSG_RESULT([BIND_CPPFLAGS=${BIND_CPPFLAGS}])
+       AC_MSG_RESULT([BIND_LIBS=${BIND_LIBS}])
+       AC_MSG_ERROR([Bind libraries are not useable for dhcp])
+   fi
+   CPPFLAGS="$saved_CPPFLAGS"
+   LIBS="$saved_LIBS"
+fi
+
 AC_SUBST(BINDSUBDIR)
 AC_SUBST(BINDDIR)
 AC_SUBST(BINDSRCDIR)
-AC_SUBST(BINDLIBIRSDIR)
-AC_SUBST(BINDLIBDNSDIR)
-AC_SUBST(BINDLIBISCCFGDIR)
-AC_SUBST(BINDLIBISCDIR)
 AC_SUBST(DISTCHECK_LIBBIND_CONFIGURE_FLAG)
+AC_SUBST(BIND_CPPFLAGS)
+AC_SUBST(BIND_LIBS)
 AM_CONDITIONAL(HAVE_BINDDIR, test "$use_libbind" = "no")
 
 #
@@ -877,20 +942,6 @@ AC_ARG_ENABLE(libtool,
 [use GNU libtool for dynamic shared libraries (default is no).]),
 	want_libtool="$enableval")
 
-if test "$use_libbind" != "no"; then
-	if test "$want_libtool" = "yes" -a \
-	        ! -f "$use_libbind/lib/libisc.la"
-	then
-		AC_MSG_ERROR([Cannot find dynamic libraries at $use_libbind/lib])
-	fi
-	if test "$want_libtool" = "no" -a \
-	        ! -f "$use_libbind/lib/libisc.a"
-	then
-		AC_MSG_ERROR([Cannot find static libraries at $use_libbind/lib])
-	fi
-fi
-
-
 if test "$want_libtool" = "yes"; then
 	AC_MSG_WARN([legacy configure is used but libtool is enabled. Trying to recover...])
 	# expand $ac_configure_args
@@ -1021,7 +1072,7 @@ fi
 CFLAGS="$CFLAGS $STD_CWARNINGS"
 
 # Try to add the bind and dhcp include directories
-CFLAGS="$CFLAGS -I\$(top_srcdir)/includes -I$BINDDIR/include"
+CFLAGS="$CFLAGS -I\$(top_srcdir)/includes $BIND_CPPFLAGS"
 
 case "$host" in
 *-darwin*)
diff --git a/dhcpctl/Makefile.am b/dhcpctl/Makefile.am
index 0d66971..0e91295 100644
--- a/dhcpctl/Makefile.am
+++ b/dhcpctl/Makefile.am
@@ -1,7 +1,4 @@
-BINDLIBIRSDIR=@BINDLIBIRSDIR@
-BINDLIBDNSDIR=@BINDLIBDNSDIR@
-BINDLIBISCCFGDIR=@BINDLIBISCCFGDIR@
-BINDLIBISCDIR=@BINDLIBISCDIR@
+BIND_LIBS = @BIND_LIBS@
 
 bin_PROGRAMS = omshell
 lib_LIBRARIES = libdhcpctl.a
@@ -11,16 +8,10 @@ EXTRA_DIST = $(man_MANS)
 
 omshell_SOURCES = omshell.c
 omshell_LDADD = libdhcpctl.a ../common/libdhcp.a ../omapip/libomapi.a \
-		$(BINDLIBIRSDIR)/libirs.a \
-		$(BINDLIBDNSDIR)/libdns.a \
-	        $(BINDLIBISCCFGDIR)/libisccfg.a \
-		$(BINDLIBISCDIR)/libisc.a
+		$(BIND_LIBS)
 
 libdhcpctl_a_SOURCES = dhcpctl.c callback.c remote.c
 
 cltest_SOURCES = cltest.c
 cltest_LDADD = libdhcpctl.a ../common/libdhcp.a ../omapip/libomapi.a \
-	       $(BINDLIBIRSDIR)/libirs.a \
-	       $(BINDLIBDNSDIR)/libdns.a \
-	       $(BINDLIBISCCFGDIR)/libisccfg.a \
-	       $(BINDLIBISCDIR)/libisc.a
+	       $(BIND_LIBS)
diff --git a/omapip/Makefile.am b/omapip/Makefile.am
index 5b61581..69be093 100644
--- a/omapip/Makefile.am
+++ b/omapip/Makefile.am
@@ -1,8 +1,4 @@
-BINDLIBIRSDIR=@BINDLIBIRSDIR@
-BINDLIBDNSDIR=@BINDLIBDNSDIR@
-BINDLIBISCCFGDIR=@BINDLIBISCCFGDIR@
-BINDLIBISCDIR=@BINDLIBISCDIR@
-
+BIND_LIBS = @BIND_LIBS@
 lib_LIBRARIES = libomapi.a
 noinst_PROGRAMS = svtest
 
@@ -15,8 +11,4 @@ man_MANS = omapi.3
 EXTRA_DIST = $(man_MANS)
 
 svtest_SOURCES = test.c
-svtest_LDADD = libomapi.a \
-	       $(BINDLIBIRSDIR)/libirs.a \
-	       $(BINDLIBDNSDIR)/libdns.a \
-	       $(BINDLIBISCCFGDIR)/libisccfg.a \
-	       $(BINDLIBISCDIR)/libisc.a
+svtest_LDADD = libomapi.a $(BIND_LIBS)
diff --git a/relay/Makefile.am b/relay/Makefile.am
index 4c5cfa6..eb27ba5 100644
--- a/relay/Makefile.am
+++ b/relay/Makefile.am
@@ -1,14 +1,11 @@
 SUBDIRS = . tests
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = -DLOCALSTATEDIR='"@localstatedir@"'
 
 sbin_PROGRAMS = dhcrelay
 dhcrelay_SOURCES = dhcrelay.c
 dhcrelay_LDADD = ../common/libdhcp.@A@ ../omapip/libomapi.@A@ \
-		 @BINDLIBIRSDIR@/libirs.@A@ \
-		 @BINDLIBDNSDIR@/libdns.@A@ \
-		 @BINDLIBISCCFGDIR@/libisccfg.@A@ \
-		 @BINDLIBISCDIR@/libisc.@A@ \
-		 $(CAPNG_LDADD)
+		 $(BIND_LIBS) $(CAPNG_LDADD)
 man_MANS = dhcrelay.8
 EXTRA_DIST = $(man_MANS)
diff --git a/relay/tests/Makefile.am b/relay/tests/Makefile.am
index fce3115..0266cbe 100644
--- a/relay/tests/Makefile.am
+++ b/relay/tests/Makefile.am
@@ -1,4 +1,5 @@
 SUBDIRS = .
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = $(ATF_CFLAGS) -DUNIT_TEST -I$(top_srcdir)/includes
 AM_CPPFLAGS += -I@BINDDIR@/include -I$(top_srcdir)
@@ -16,10 +17,7 @@ DHCPSRC = ../dhcrelay.c
 
 DHCPLIBS = $(top_builddir)/common/libdhcp.@A@ \
 	  $(top_builddir)/omapip/libomapi.@A@    \
-	  @BINDLIBIRSDIR@/libirs.@A@ \
-	  @BINDLIBDNSDIR@/libdns.@A@ \
-	  @BINDLIBISCCFGDIR@/libisccfg.@A@ \
-	  @BINDLIBISCDIR@/libisc.@A@
+	  $(BIND_LIBS)
 
 ATF_TESTS =
 if HAVE_ATF
diff --git a/server/Makefile.am b/server/Makefile.am
index 787efca..163f712 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -3,6 +3,7 @@
 # directory. That eliminates any discrepancies between tested code and
 # production code. Sadly, we are not there yet.
 SUBDIRS = . tests
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = -I.. -DLOCALSTATEDIR='"@localstatedir@"'
 
@@ -15,10 +16,7 @@ dhcpd_SOURCES = dhcpd.c dhcp.c bootp.c confpars.c db.c class.c failover.c \
 dhcpd_CFLAGS = $(LDAP_CFLAGS)
 dhcpd_LDADD = ../common/libdhcp.@A@ ../omapip/libomapi.@A@ \
 	      ../dhcpctl/libdhcpctl.@A@ \
-	      $(BINDLIBIRSDIR)/libirs.@A@ \
-	      $(BINDLIBDNSDIR)/libdns.@A@ \
-	      $(BINDLIBISCCFGDIR)/libisccfg.@A@ \
-	      $(BINDLIBISCDIR)/libisc.@A@ $(LDAP_LIBS)
+	      $(BIND_LIBS) $(LDAP_LIBS)
 
 man_MANS = dhcpd.8 dhcpd.conf.5 dhcpd.leases.5
 EXTRA_DIST = $(man_MANS)
diff --git a/server/tests/Makefile.am b/server/tests/Makefile.am
index 699a0b6..1e06d69 100644
--- a/server/tests/Makefile.am
+++ b/server/tests/Makefile.am
@@ -1,7 +1,8 @@
 SUBDIRS = .
+BIND_LIBS = @BIND_LIBS@
 
 AM_CPPFLAGS = $(ATF_CFLAGS) -DUNIT_TEST -I$(top_srcdir)/includes
-AM_CPPFLAGS += -I@BINDDIR@/include -I$(top_srcdir)
+AM_CPPFLAGS +=  $(BIND_CPPFLAGS) -I$(top_srcdir)
 AM_CPPFLAGS += -DLOCALSTATEDIR='"."'
 
 EXTRA_DIST = Atffile Kyuafile
@@ -20,10 +21,7 @@ DHCPSRC = ../dhcp.c ../bootp.c ../confpars.c ../db.c ../class.c      \
 DHCPLIBS = $(top_builddir)/common/libdhcp.@A@ \
 	  $(top_builddir)/omapip/libomapi.@A@ \
           $(top_builddir)/dhcpctl/libdhcpctl.@A@ \
-	  $(BINDLIBIRSDIR)/libirs.@A@ \
-	  $(BINDLIBDNSDIR)/libdns.@A@ \
-	  $(BINDLIBISCCFGDIR)/libisccfg.@A@ \
-	  $(BINDLIBISCDIR)/libisc.@A@
+	  $(BIND_LIBS)
 
 ATF_TESTS =
 if HAVE_ATF
-- 
2.23.0

