From e69d611443d7a28497e12be950f605a770b49e3e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marek=20Vavru=C5=A1a?= <marek.vavrusa@nic.cz>
Date: Fri, 6 Feb 2015 10:13:32 +0100
Subject: [PATCH] build: Makefile

---
 .gitignore                   | 14 +++---
 .travis.yml                  | 16 ++-----
 CMakeLists.txt               | 27 -----------
 Makefile                     | 27 +++++++++++
 config.mk                    | 19 ++++++++
 daemon/kresolved.mk          | 13 ++++++
 help.mk                      | 18 ++++++++
 lib/libkresolve.mk           | 30 ++++++++++++
 lib/rplan.c                  |  2 -
 lib/rplan.h                  |  2 +
 platform.mk                  | 90 ++++++++++++++++++++++++++++++++++++
 scripts/bootstrap-depends.sh |  3 +-
 tests/test_integration.c     |  2 +-
 tests/tests.mk               | 55 ++++++++++++++++++++++
 14 files changed, 267 insertions(+), 51 deletions(-)
 delete mode 100644 CMakeLists.txt
 create mode 100644 Makefile
 create mode 100644 config.mk
 create mode 100644 daemon/kresolved.mk
 create mode 100644 help.mk
 create mode 100644 lib/libkresolve.mk
 create mode 100644 platform.mk
 create mode 100644 tests/tests.mk

diff --git a/.gitignore b/.gitignore
index 159b3e03d..391ed7eb0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,28 +2,26 @@
 *.a
 *.so
 *.so.*
+*.dylib
+*.dylib.*
 *.lo
 *.la
 *.in
 *.Plo
 *.swp
+*.d
 .dirstamp
 .libs
 .deps
 autom4te.cache/*
-config.*
+config.log
+config.h
+config.status
 configure
 ar-lib
-Makefile
 libtool
 missing
 compile
 depcomp
 install-sh
 stamp-h1
-
-# CMake files
-/CMakeCache.txt
-/CMakeFiles
-/Makefile
-/cmake_install.cmake
diff --git a/.travis.yml b/.travis.yml
index 68ae4f272..88743b632 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -3,26 +3,20 @@ compiler:
     - gcc
 env:
     global:
-        - PKG_CONFIG_PATH="${HOME}/fakeroot/lib/pkgconfig"
-        - LDFLAGS="-L${HOME}/fakeroot/lib"
         - PIP_DOWNLOAD_CACHE="${HOME}/.pip-cache"
-        - CFLAGS="${CFLAGS} -DNDEBUG"
         - PATH="${PATH}:${HOME}/.local/bin"
+        - PKG_CONFIG_PATH="${HOME}/fakeroot/lib/pkgconfig"
+        - LD_LIBRARY_PATH="${HOME}/fakeroot/lib"
+        - CFLAGS="${CFLAGS} -O0 -g -DNDEBUG"
 before_script:
     - ./scripts/bootstrap-depends.sh ${HOME}/fakeroot
     - pip install --user travis -r tests/pydnstest/requirements.txt
     - pip install --user travis cpp-coveralls
 script:
-    - ./bootstrap
-    - ./configure --enable-integration-tests --enable-code-coverage
-    - make
-    - make check
-after_script:
-    - cd lib
-    - coveralls -x ".c" --gcov-options '\-lp'
+    - make check COVERAGE=1 LDFLAGS="-L${HOME}/fakeroot/lib"
+    - coveralls -i lib -x ".c" --gcov-options '\-lp'
 sudo: false
 cache:
     directories:
     - ${HOME}/fakeroot
     - ${HOME}/.pip-cache
-    - .autom4te.cache
diff --git a/CMakeLists.txt b/CMakeLists.txt
deleted file mode 100644
index a752c6565..000000000
--- a/CMakeLists.txt
+++ /dev/null
@@ -1,27 +0,0 @@
-cmake_minimum_required(VERSION 2.8)
-project(resolver C)
-set(RESOLVER_VERSION 0.0.1) 
-set(LIBS_VERSION 0.0.1)
-
-# Compiler flags
-
-if(NOT CMAKE_BUILD_TYPE)
-	message(STATUS "No build type specified, setting to 'Release'.")
-	set(CMAKE_BUILD_TYPE "Release")
-endif()
-
-set(CMAKE_C_FLAGS         "-std=gnu99 -Wall -g")
-set(CMAKE_C_FLAGS_DEBUG   "-O0")
-set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
-
-# Modules compilation
-
-include_directories(include)
-
-set(resolver_sources
-	lib/context.c
-	lib/info.c
-)
-
-add_library(resolver SHARED ${resolver_sources})
-set_target_properties(resolver PROPERTIES VERSION ${LIBS_VERSION})
diff --git a/Makefile b/Makefile
new file mode 100644
index 000000000..ab09c1807
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,27 @@
+include config.mk
+include platform.mk
+
+# Targets
+all: info libkresolve kresolved
+install: libkresolve-install kresolved-install
+check: all tests-check
+clean: libkresolve-clean kresolved-clean tests-clean
+.PHONY: all install check clean
+
+# Options
+ifdef COVERAGE
+CFLAGS += --coverage
+endif
+
+# Dependencies
+$(eval $(call find_lib,libknot))
+$(eval $(call find_lib,libuv))
+$(eval $(call find_lib,cmocka))
+$(eval $(call find_python))
+CFLAGS += $(libknot_CFLAGS) $(libuv_CFLAGS) $(cmocka_CFLAGS) $(python_CFLAGS)
+
+# Sub-targets
+include help.mk
+include lib/libkresolve.mk
+include daemon/kresolved.mk
+include tests/tests.mk
\ No newline at end of file
diff --git a/config.mk b/config.mk
new file mode 100644
index 000000000..6a302c22d
--- /dev/null
+++ b/config.mk
@@ -0,0 +1,19 @@
+# Project
+MAJOR := 15
+MINOR := 04
+
+# Paths
+PREFIX := /usr/local
+BINDIR := /bin
+LIBDIR := /lib
+INCLUDEDIR = /include
+
+# Tools
+ifndef CC
+CC	:= cc
+endif
+CFLAGS	+= -std=c99 -D_GNU_SOURCE -Wall -fPIC -I$(abspath .) -DPACKAGE_VERSION="\"$(MAJOR).$(MINOR)\""
+RM	:= rm -f
+LN  := ln -s
+INSTALL := install
+PYTHON := python
\ No newline at end of file
diff --git a/daemon/kresolved.mk b/daemon/kresolved.mk
new file mode 100644
index 000000000..125b66553
--- /dev/null
+++ b/daemon/kresolved.mk
@@ -0,0 +1,13 @@
+kresolved_SOURCES := \
+	daemon/layer/query.c \
+	daemon/udp.c         \
+	daemon/tcp.c         \
+	daemon/worker.c      \
+	daemon/main.c
+
+# Dependencies
+kresolved_DEPEND := libkresolve libknot libuv
+kresolved_LIBS := $(libkresolve_TARGET) $(libuv_LIBS) $(libknot_LIBS)
+
+# Make binary
+$(eval $(call make_bin,kresolved,daemon))
\ No newline at end of file
diff --git a/help.mk b/help.mk
new file mode 100644
index 000000000..11cece625
--- /dev/null
+++ b/help.mk
@@ -0,0 +1,18 @@
+info:
+	$(info )
+	$(info Target:     Knot DNS Resolver $(MAJOR).$(MINOR)-$(PLATFORM))
+	$(info Compiler:   $(CC))
+	$(info CFLAGS:     $(CFLAGS))
+	$(info LDFLAGS:    $(LDFLAGS))
+	$(info PREFIX:     $(PREFIX))
+	$(info BINDIR:     $(BINDIR))
+	$(info LIBDIR:     $(LIBDIR))
+	$(info INCLUDEDIR: $(INCLUDEDIR))
+	$(info )
+	$(info Features)
+	$(info --------)
+	$(info [$(HAS_libknot)] library (libknot))
+	$(info [$(HAS_libuv)] daemon (libuv))
+	$(info [$(HAS_cmocka)] unit tests (libcmocka))
+	$(info [$(HAS_python)] integration tests (libpython))
+	$(info )
\ No newline at end of file
diff --git a/lib/libkresolve.mk b/lib/libkresolve.mk
new file mode 100644
index 000000000..09a1badb4
--- /dev/null
+++ b/lib/libkresolve.mk
@@ -0,0 +1,30 @@
+libkresolve_SOURCES := \
+	lib/layer/iterate.c    \
+	lib/layer/itercache.c  \
+	lib/layer/static.c     \
+	lib/layer/stats.c      \
+	lib/context.c          \
+	lib/resolve.c          \
+	lib/zonecut.c          \
+	lib/rplan.c            \
+	lib/cache.c
+
+libkresolve_HEADERS := \
+	lib/layer/iterate.h    \
+	lib/layer/itercache.h  \
+	lib/layer/static.h     \
+	lib/layer/stats.h      \
+	lib/layer.h            \
+	lib/context.h          \
+	lib/resolve.h          \
+	lib/zonecut.h          \
+	lib/rplan.h            \
+	lib/cache.h
+
+# Dependencies
+libkresolve_DEPEND := libknot
+libkresolve_LIBS := $(libknot_LIBS)
+libkresolve_TARGET := -Wl,-rpath,lib -Llib -lkresolve
+
+# Make library
+$(eval $(call make_lib,libkresolve,lib))
\ No newline at end of file
diff --git a/lib/rplan.c b/lib/rplan.c
index aec9429e9..f25d4cb3e 100644
--- a/lib/rplan.c
+++ b/lib/rplan.c
@@ -14,8 +14,6 @@
     along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <sys/time.h>
-
 #include <libknot/descriptor.h>
 #include <libknot/processing/layer.h>
 #include <libknot/errcode.h>
diff --git a/lib/rplan.h b/lib/rplan.h
index 001cbbf4c..c9771b07e 100644
--- a/lib/rplan.h
+++ b/lib/rplan.h
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <sys/time.h>
+
 #include <libknot/dname.h>
 #include <libknot/internal/lists.h>
 #include <libknot/internal/namedb/namedb.h>
diff --git a/platform.mk b/platform.mk
new file mode 100644
index 000000000..b7ced0ea8
--- /dev/null
+++ b/platform.mk
@@ -0,0 +1,90 @@
+# Platform-specific
+CCLD := $(CC)
+LIBEXT := .so
+MODEXT := $(LIBEXT)
+LIBTYPE := shared
+MODTYPE := shared
+BINEXT :=
+PLATFORM = Linux
+ifeq ($(OS),Windows_NT)
+	PLATFORM := Windows
+	RM := del
+	LN := link
+	LIBEXT := .lib
+	BINEXT := .exe
+else
+	UNAME := $(shell uname -s)
+    ifeq ($(UNAME),Darwin)
+        PLATFORM := Darwin
+        LIBEXT := .dylib
+        MODTYPE := dynamiclib
+    else
+    	PLATFORM := POSIX
+		LDFLAGS += -pthread
+    endif
+endif
+
+# Silent compilation
+ifeq ($(V),1)
+	quiet = $($1)
+else
+	quiet = @echo "  $1	$2"; $($1)
+endif	
+
+%.o: %.c
+	$(call quiet,CC,$<) $(CFLAGS) -MMD -MP -c $< -o $@
+
+# Make objects and depends (name)
+define make_objs
+$(1)_OBJ := $$($(1)_SOURCES:.c=.o)
+$(1)_DEP := $$($(1)_SOURCES:.c=.d)
+-include $$($(1)_DEP)
+endef
+
+# Make target (name,path,ext,ldflags,dst)
+define make_target
+$$(eval $$(call make_objs,$(1)))
+$(1): $(2)/$(1)$(3)
+$(2)/$(1)$(3): $$($(1)_OBJ) $$($(1)_DEPEND)
+	$(call quiet,CCLD,$$@) $(CFLAGS) $$($(1)_OBJ) -o $$@ $(4) $(LDFLAGS) $$($(1)_LIBS)
+$(1)-clean:
+	$(RM) $$($(1)_OBJ) $$($(1)_DEP) $(2)/$(1)$(3)
+$(1)-install: $(2)/$(1)$(3)
+	$(INSTALL) $$^ $(PREFIX)/$(5) 
+ifneq ($$(strip $$($(1)_HEADERS)),)
+	$(INSTALL) -d $(PREFIX)/$(INCLUDEDIR)/$(1)
+	$(INSTALL) $$($(1)_HEADERS) $(PREFIX)/$(INCLUDEDIR)/$(1)
+endif
+.PHONY: $(1) $(1)-clean $(1)-install
+endef
+
+# Make targets (name,path)
+make_bin = $(call make_target,$(1),$(2),$(BINEXT),,$(BINDIR))
+make_lib = $(call make_target,$(1),$(2),$(LIBEXT),-$(LIBTYPE),$(LIBDIR))
+make_module = $(call make_target,$(1),$(2),$(MODEXT),-$(MODTYPE),$(LIBDIR))
+
+# Evaluate library
+define have_lib
+ifeq ($$(strip $$($(1)_LIBS)),)
+	HAS_$(1) := no
+else
+	HAS_$(1) := yes
+$(1):
+endif
+endef
+
+# Find library (pkg-config)
+define find_lib
+	ifeq ($$(strip $$($(1)_LIBS)),)
+		$(1)_CFLAGS := $(shell pkg-config --cflags $(1))
+		$(1)_LIBS := $(shell pkg-config --libs $(1) --silence-errors)
+	endif
+	$(call have_lib,$(1))
+endef
+
+# Find Python 
+define find_python
+	python_CFLAGS := $(shell $(PYTHON) -c "from distutils import sysconfig as c;print('-I%s' % c.get_python_inc())")
+	python_LIBS := $(shell $(PYTHON) -c "from distutils import sysconfig as c;print('-L%s -lpython%s' % (c.get_config_var('LIBDIR'), c.get_config_var('VERSION')))")
+	$(call have_lib,python)
+endef
\ No newline at end of file
diff --git a/scripts/bootstrap-depends.sh b/scripts/bootstrap-depends.sh
index 5b9ec34cc..6355bff5f 100755
--- a/scripts/bootstrap-depends.sh
+++ b/scripts/bootstrap-depends.sh
@@ -37,7 +37,6 @@ fi
 if [ ! -e ${1}/include/libknot ]; then
 	git clone https://github.com/CZNIC-Labs/knot.git || true
 	cd knot
-	git checkout resolver_improvements
 	autoreconf -i
 	./configure --prefix=${1}
 	make
@@ -60,7 +59,7 @@ fi
 
 # libuv
 if [ ! -e ${1}/include/uv.h ]; then
-	git clone https://github.com/libuv/libuv.git || true
+	git clone -b v1.3.0 https://github.com/libuv/libuv.git || true
 	cd libuv
 	sh autogen.sh
 	./configure --prefix=${1}
diff --git a/tests/test_integration.c b/tests/test_integration.c
index 6d00b7138..a0317f48b 100644
--- a/tests/test_integration.c
+++ b/tests/test_integration.c
@@ -65,7 +65,7 @@ static PyObject* init(PyObject* self, PyObject* args)
 		global_context.options |= QUERY_NO_MINIMIZE; 
 	}
 
-	return Py_BuildValue("s", PACKAGE_STRING " (integration tests)");
+	return Py_BuildValue("");
 }
 
 static PyObject* deinit(PyObject* self, PyObject* args)
diff --git a/tests/tests.mk b/tests/tests.mk
new file mode 100644
index 000000000..a42c9eb4c
--- /dev/null
+++ b/tests/tests.mk
@@ -0,0 +1,55 @@
+#
+# Unit tests
+#
+
+tests_BIN := \
+	test_context \
+	test_rplan \
+	test_cache \
+	test_resolve
+
+# Dependencies
+tests_DEPEND := libkresolve cmocka
+tests_LIBS :=  $(libkresolve_TARGET) $(libkresolve_LIBS) $(cmocka_LIBS)
+
+# Make test binaries
+define make_test
+$(1)_SOURCES := tests/$(1).c
+$(1)_LIBS := $(tests_LIBS)
+$(1)_DEPEND := $(tests_DEPEND)
+$(call make_bin,$(1),tests)
+endef
+
+$(foreach test,$(tests_BIN),$(eval $(call make_test,$(test))))
+
+#
+# Integration tests
+#
+
+# Mocked calls library
+libmock_calls_SOURCES := tests/mock_calls.c
+libmock_calls_LIBS := $(tests_LIBS) $(python_LIBS)
+libmock_calls_DEPEND := libkresolve
+$(eval $(call make_lib,libmock_calls,tests))
+
+# Python module for tests
+_test_integration_SOURCES := tests/test_integration.c
+_test_integration_LIBS := -Wl,-rpath,tests -Ltests -lmock_calls $(libmock_calls_LIBS)
+_test_integration_DEPEND := libmock_calls
+$(eval $(call make_module,_test_integration,tests))
+
+# Preload mock library
+ifeq ($(PLATFORM),Darwin)
+	preload_libs := @DYLD_FORCE_FLAT_NAMESPACE=1 DYLD_INSERT_LIBRARIES=tests/libmock_calls$(LIBEXT)
+else
+	preload_libs := @LD_PRELOAD=tests/libmock_calls$(LIBEXT)
+endif
+
+# Targets
+.PHONY: tests-check-integration tests-check-unit tests-check tests-clean
+tests-check-integration: libmock_calls _test_integration
+	$(call preload_libs) tests/test_integration.py tests/testdata
+tests-check-unit: $(tests_BIN)
+	tests/runtests -b tests $^	
+tests-check: tests-check-unit tests-check-integration
+tests-clean: $(foreach test,$(tests_BIN),$(test)-clean) libmock_calls-clean _test_integration-clean
\ No newline at end of file
-- 
GitLab