From 123a847d2813cea3f40c9f28e56ee064d3c2012c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= <marek.vavrusa@nic.cz>
Date: Wed, 12 Nov 2014 19:13:13 +0100
Subject: [PATCH] resolver: use pkg-config to find libknot and libuv

---
 README.md          | 36 ++++++++++++++++++++++++++++++++++--
 configure.ac       | 14 +++++---------
 daemon/Makefile.am | 10 +++++-----
 daemon/main.c      |  2 +-
 daemon/worker.c    |  2 +-
 daemon/worker.h    |  2 +-
 lib/Makefile.am    |  3 +++
 lib/cache.c        |  2 +-
 lib/context.c      |  2 +-
 lib/context.h      |  5 +++--
 lib/delegpt.c      |  4 ++--
 lib/delegpt.h      |  6 +++---
 lib/layer.h        |  3 ++-
 lib/layer/stats.c  |  2 +-
 lib/rplan.h        |  2 +-
 m4/libcmocka.m4    | 19 -------------------
 m4/liblmdb.m4      | 19 -------------------
 m4/libuv.m4        |  7 -------
 tests/Makefile.am  |  2 --
 19 files changed, 64 insertions(+), 78 deletions(-)
 delete mode 100644 m4/libcmocka.m4
 delete mode 100644 m4/liblmdb.m4
 delete mode 100644 m4/libuv.m4

diff --git a/README.md b/README.md
index 25acbf60a..aa1cd2d10 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,38 @@
 # Knot DNS Resolver
 
+## Preparation
+
+The Knot DNS Resolver depends on the Knot DNS library which is introduced in current master, and on the
+reasonably recent version of the `libuv`.
+
+### libuv
+
+If the libuv with a version at least 1.0 is not present on your system,
+compile and install the sources code from the Git repository.
+
+```
+$ git clone https://github.com/joyent/libuv.git
+$ cd libuv
+$ ./autogen.sh
+$ make && make install
+```
+
 ## Compilation
 
-./configure
-make
+```
+$ export PKG_CONFIG_PATH="..." # Change, if you installed the libknot somewhere else
+$ ./configure
+$ autoreconf -if
+$ make
+```
+
+## Running
+
+There is a separate resolver library in the `lib` directory, and a minimalistic daemon in
+the `daemon` directory. The daemon accepts a few CLI parameters, and there's no support for configuration
+right now.
+
+```
+$ ./daemon/kresolved -h
+$ ./daemon/kresolved -a 127.0.0.1#53
+```
diff --git a/configure.ac b/configure.ac
index 93fc7ef23..0244bb04e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -30,13 +30,10 @@ LT_INIT
 PKG_PROG_PKG_CONFIG
 
 # Check for dependencies
-AM_CHECK_CMOCKA
-AM_CHECK_LIBUV
+PKG_CHECK_MODULES([libuv], [libuv])
+PKG_CHECK_MODULES([libknot], [libknot])
 
-# Check headers
-AC_CHECK_HEADERS([stdlib.h],,, [AC_INCLUDES_DEFAULT])
-
-# Search libraries
+# Search other libraries
 AC_SEARCH_LIBS([mdb_env_open], [lmdb])
 
 # Config files
@@ -53,9 +50,8 @@ AC_MSG_RESULT([
 
     Target:   $host_os $host_cpu
     Compiler: ${CC}
-    CFlags:   ${CFLAGS} ${CPPFLAGS}
-    LDFlags:  ${LDFLAGS}
-    Libs:     ${LIBS}
+    CFlags:   ${CFLAGS} ${CPPFLAGS} ${libuv_CFLAGS} ${libknot_CFLAGS}
+    LDFlags:  ${LDFLAGS} ${libuv_LIBS} ${libknot_LIBS}
 
   Continue with 'make' command
 ])
diff --git a/daemon/Makefile.am b/daemon/Makefile.am
index 5544b0f3e..a9a6c15af 100644
--- a/daemon/Makefile.am
+++ b/daemon/Makefile.am
@@ -1,8 +1,10 @@
 AM_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
-	-I$(top_srcdir)/lib
+	-I$(top_srcdir)/lib \
+	$(libuv_CFLAGS) \
+	$(libknot_CFLAGS)
 
-sbin_PROGRAMS = kresolved 
+sbin_PROGRAMS = kresolved
 
 kresolved_SOURCES =					\
 	layer/query.h					\
@@ -12,6 +14,4 @@ kresolved_SOURCES =					\
 	main.c
 
 # sbin programs
-kresolved_LDADD = $(top_builddir)/lib/libknotresolve.la \
-					$(KNOT_LIBS) \
-					$(libuv_LIBS)
+kresolved_LDADD = $(top_builddir)/lib/libknotresolve.la
diff --git a/daemon/main.c b/daemon/main.c
index f18e976c6..3813c9d28 100644
--- a/daemon/main.c
+++ b/daemon/main.c
@@ -4,7 +4,7 @@
 
 #include <uv.h>
 
-#include <common/sockaddr.h>
+#include <libknot/internal/sockaddr.h>
 #include "lib/resolve.h"
 #include "worker.h"
 
diff --git a/daemon/worker.c b/daemon/worker.c
index 4c752882e..ffcca56ca 100644
--- a/daemon/worker.c
+++ b/daemon/worker.c
@@ -1,7 +1,7 @@
 #include <uv.h>
 
 #include <libknot/packet/pkt.h>
-#include <common/net.h>
+#include <libknot/internal/net.h>
 
 #include "daemon/worker.h"
 #include "daemon/layer/query.h"
diff --git a/daemon/worker.h b/daemon/worker.h
index f4efd10f8..06589fa21 100644
--- a/daemon/worker.h
+++ b/daemon/worker.h
@@ -17,7 +17,7 @@ limitations under the License.
 
 #include <uv.h>
 
-#include <common/mempattern.h>
+#include <libknot/internal/mempattern.h>
 #include "lib/resolve.h"
 
 struct worker_ctx {
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 7686f19dc..f02ae4e9e 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -20,3 +20,6 @@ libknotresolve_la_SOURCES =		\
 	rplan.c			\
 	cache.h			\
 	cache.c
+
+libknotresolve_la_CPPFLAGS = $(AM_CPPFLAGS) $(libknot_CFLAGS) $(libuv_CFLAGS)
+libknotresolve_la_LDFLAGS  = $(AM_LDFLAGS) $(libknot_LIBS) $(libuv_LIBS)
diff --git a/lib/cache.c b/lib/cache.c
index dcd345d19..40d1a83e4 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -2,7 +2,7 @@
 #include <time.h>
 
 #include <lmdb.h>
-#include <common/mempattern.h>
+#include <libknot/internal/mempattern.h>
 #include <libknot/descriptor.h>
 
 #include "lib/cache.h"
diff --git a/lib/context.c b/lib/context.c
index 823f64e98..11932edbd 100644
--- a/lib/context.c
+++ b/lib/context.c
@@ -1,7 +1,7 @@
 #include <string.h>
 #include <sys/time.h>
 
-#include <common/sockaddr.h>
+#include <libknot/internal/sockaddr.h>
 #include "lib/context.h"
 #include "lib/rplan.h"
 
diff --git a/lib/context.h b/lib/context.h
index 1e8960cbd..17a8c20ee 100644
--- a/lib/context.h
+++ b/lib/context.h
@@ -16,8 +16,9 @@ limitations under the License.
 #pragma once
 
 #include <stdint.h>
-#include <common/mempattern.h>
-#include <common/sockaddr.h>
+
+#include <libknot/internal/mempattern.h>
+#include <libknot/internal/sockaddr.h>
 
 #include "lib/delegpt.h"
 #include "lib/rplan.h"
diff --git a/lib/delegpt.c b/lib/delegpt.c
index a101ddbdf..681908538 100644
--- a/lib/delegpt.c
+++ b/lib/delegpt.c
@@ -1,6 +1,6 @@
 #include "lib/delegpt.h"
 #include "lib/defines.h"
-#include <common/mempool.h>
+#include <libknot/internal/mempool.h>
 
 static void ns_free(struct kr_ns *ns, mm_ctx_t *mm)
 {
@@ -135,4 +135,4 @@ void kr_ns_remove(struct kr_ns *ns, mm_ctx_t *mm)
 {
 	rem_node((node_t *)ns);
 	ns_free(ns, mm);
-}
\ No newline at end of file
+}
diff --git a/lib/delegpt.h b/lib/delegpt.h
index a2841515f..35f97fde5 100644
--- a/lib/delegpt.h
+++ b/lib/delegpt.h
@@ -16,9 +16,9 @@ limitations under the License.
 #pragma once
 
 #include <libknot/packet/pkt.h>
-#include <common/lists.h>
-#include <common/sockaddr.h>
-#include <common/trie/hat-trie.h>
+#include <libknot/internal/lists.h>
+#include <libknot/internal/sockaddr.h>
+#include <libknot/internal/trie/hat-trie.h>
 
 /*! \brief Name server flag. */
 enum kr_ns_flag {
diff --git a/lib/layer.h b/lib/layer.h
index c1ec9d194..780c09129 100644
--- a/lib/layer.h
+++ b/lib/layer.h
@@ -16,6 +16,7 @@ limitations under the License.
 #pragma once
 
 #include <libknot/processing/layer.h>
+
 #include "lib/context.h"
 
 /*!
@@ -24,4 +25,4 @@ limitations under the License.
 struct kr_layer_param {
 	struct kr_context *ctx;
 	struct kr_result *result;
-};
\ No newline at end of file
+};
diff --git a/lib/layer/stats.c b/lib/layer/stats.c
index e696f7641..6da50ebf7 100644
--- a/lib/layer/stats.c
+++ b/lib/layer/stats.c
@@ -13,7 +13,7 @@ See the License for the specific language governing permissions and
 limitations under the License.
 */
 
-#include <common/print.h>
+#include <libknot/internal/print.h>
 
 #include "lib/layer/stats.h"
 #include "lib/rplan.h"
diff --git a/lib/rplan.h b/lib/rplan.h
index d7fd5eb02..555dc1b81 100644
--- a/lib/rplan.h
+++ b/lib/rplan.h
@@ -16,7 +16,7 @@ limitations under the License.
 #pragma once
 
 #include <libknot/dname.h>
-#include <common/lists.h>
+#include <libknot/internal/lists.h>
 
 enum {
 	RESOLVE_QUERY = 0 << 0,
diff --git a/m4/libcmocka.m4 b/m4/libcmocka.m4
deleted file mode 100644
index 539c0ac7a..000000000
--- a/m4/libcmocka.m4
+++ /dev/null
@@ -1,19 +0,0 @@
-dnl A macro to check presence of cmocka on the system
-AC_DEFUN([AM_CHECK_CMOCKA],
-[
-    PKG_CHECK_EXISTS(cmocka,
-        [AC_CHECK_HEADERS([stdarg.h stddef.h setjmp.h],
-            [], dnl We are only intrested in action-if-not-found
-            [AC_MSG_WARN([Header files stdarg.h stddef.h setjmp.h are required by cmocka])
-             cmocka_required_headers="no"
-            ]
-        )
-        AS_IF([test x"$cmocka_required_headers" != x"no"],
-              [PKG_CHECK_MODULES([cmocka], [cmocka], [have_cmocka="yes"])]
-        )],
-        dnl PKG_CHECK_EXISTS ACTION-IF-NOT-FOUND
-        [AC_MSG_WARN([No libcmocka library found, cmocka tests will not be built])]
-    )
-    AM_CONDITIONAL([HAVE_CMOCKA], [test x$have_cmocka = xyes])
-])
-
diff --git a/m4/liblmdb.m4 b/m4/liblmdb.m4
deleted file mode 100644
index 6acfdd8ad..000000000
--- a/m4/liblmdb.m4
+++ /dev/null
@@ -1,19 +0,0 @@
-dnl A macro to check presence of liblmdb on the system
-AC_DEFUN([AM_CHECK_LMDB],
-[
-    PKG_CHECK_EXISTS(lmdb,
-        [AC_CHECK_HEADERS([lmdb.h],
-            [], dnl We are only intrested in action-if-not-found
-            [AC_MSG_WARN([Header file lmdb.h is required.])
-             lmdb_required_headers="no"
-            ]
-        )
-        AS_IF([test x"$lmdb_required_headers" != x"no"],
-              [PKG_CHECK_MODULES([lmdb], [lmdb], [have_lmdb="yes"])]
-        )],
-        dnl PKG_CHECK_EXISTS ACTION-IF-NOT-FOUND
-        [AC_MSG_WARN([No lmdb library found.])]
-    )
-    AM_CONDITIONAL([HAVE_LMDB], [test x$have_lmdb = xyes])
-])
-
diff --git a/m4/libuv.m4 b/m4/libuv.m4
deleted file mode 100644
index ab41a702a..000000000
--- a/m4/libuv.m4
+++ /dev/null
@@ -1,7 +0,0 @@
-dnl A macro to check presence of libuv on the system
-AC_DEFUN([AM_CHECK_LIBUV],
-[
-	AC_CHECK_HEADERS([uv.h], [], [AC_MSG_ERROR([Header file uv.h is required.])])
-	AC_CHECK_LIB([uv], [uv_version], [], [AC_MSG_ERROR([libuv is required.])])
-])
-
diff --git a/tests/Makefile.am b/tests/Makefile.am
index dab72e6d4..544cf3c93 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -4,8 +4,6 @@ AM_CPPFLAGS = \
 
 LDADD = \
 	$(top_builddir)/lib/libknotresolve.la \
-	$(KNOT_LIBS) \
-	$(libuv_LIBS) \
 	$(cmocka_LIBS)
 
 check_PROGRAMS = \
-- 
GitLab