Switch to cmake

Change-Id: I35cb8925d2bc8441e7635f518dbcfbe0285fd942
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..4d52d56
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,33 @@
+cmake_minimum_required(VERSION 3.9)
+project(collocatordb VERSION 1.2.0 DESCRIPTION "Storing and retrieving collocation counts based on RocksDB")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fno-rtti")
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g")
+
+include(GNUInstallDirs)
+include_directories(/usr/local/include)
+find_library(ROCKSDB librocksdb.so.5.11)
+enable_testing()
+add_library(collocatordb SHARED src/collocatordb.cc)
+target_link_libraries(collocatordb pthread rt snappy z bz2 lz4 zstd ${ROCKSDB})
+set_target_properties(collocatordb PROPERTIES
+        VERSION ${PROJECT_VERSION}
+        SOVERSION 1
+        PUBLIC_HEADER src/collocatordb.h)
+
+add_library(collocatordb_static STATIC src/collocatordb.cc)
+target_link_libraries(collocatordb_static pthread rt snappy  bz2 lz4 zstd)
+
+add_executable(basic_test tests/basic_test.c tests/acutest.h)
+TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
+add_test(BASIC_TEST basic_test)
+
+configure_file(collocatordb.pc.in collocatordb.pc @ONLY)
+target_include_directories(collocatordb PRIVATE .)
+install(TARGETS collocatordb collocatordb_static
+        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+        PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+install(FILES ${CMAKE_BINARY_DIR}/collocatordb.pc
+        DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig)
+
+# Install library
+install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
diff --git a/Makefile b/Makefile
deleted file mode 100644
index 4b9a746..0000000
--- a/Makefile
+++ /dev/null
@@ -1,95 +0,0 @@
-COLLOCATDB_MAJOR=1
-COLLOCATDB_MINOR=2
-COLLOCATDB_PATCH=0
-
-INSTALL_PATH ?= /usr/local
-
-PLATFORM_CCFLAGS= -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX  -DOS_LINUX -fno-builtin-memcmp -DROCKSDB_FALLOCATE_PRESENT -DSNAPPY -DGFLAGS=1 -DZLIB -DBZIP2 -DLZ4 -DZSTD -DROCKSDB_MALLOC_USABLE_SIZE -DROCKSDB_PTHREAD_ADAPTIVE_MUTEX -DROCKSDB_BACKTRACE -DROCKSDB_RANGESYNC_PRESENT -DROCKSDB_SCHED_GETCPU_PRESENT -march=native  -DROCKSDB_SUPPORT_THREAD_LOCAL
-PLATFORM_CXXFLAGS=-std=c++11  -DROCKSDB_PLATFORM_POSIX -DROCKSDB_LIB_IO_POSIX  -DOS_LINUX -fno-builtin-memcmp -DROCKSDB_FALLOCATE_PRESENT -DSNAPPY -DGFLAGS=1 -DZLIB -DBZIP2 -DLZ4 -DZSTD -DROCKSDB_MALLOC_USABLE_SIZE -DROCKSDB_PTHREAD_ADAPTIVE_MUTEX -DROCKSDB_BACKTRACE -DROCKSDB_RANGESYNC_PRESENT -DROCKSDB_SCHED_GETCPU_PRESENT -march=native  -DROCKSDB_SUPPORT_THREAD_LOCAL
-PLATFORM=OS_LINUX
-PLATFORM_LDFLAGS= -L$(INSTALL_PATH)/lib -Wl,-rpath=$(INSTALL_PATH)/lib -lpthread -lrt -lsnappy -lz -lbz2 -llz4 -lzstd
-
-CXXFLAGS = -Wall -Wno-reorder -I/usr/local/include -g -std=c++11
-CFLAGS = -Wall -I/usr/local/rocksdb -I$(INSTALL_PATH)/include -g -std=gnu99  -O2
-
-ARFLAGS = ${EXTRA_ARFLAGS} rs
-
-ifneq ($(USE_RTTI), 1)
-	CXXFLAGS += -fno-rtti
-endif
-SOURCE_DIR := src
-BUILD_DIR := build
-BIN_DIR := bin
-LIB_DIR := lib
-LIB_SOURCES = src/collocatordb.cc
-
-LIB_OBJECTS := $(subst $(SOURCE_DIR),$(BUILD_DIR),$(LIB_SOURCES:.cc=.o))
-
-.PHONY: static_lib shared_lib install-static install-shared test
-
-all: static_lib shared_lib test
-
-install: install-static install-shared
-
-test: bin/basic_test
-
-bin/hello_world: examples/hello_world.c src/collocatordb.h build/collocatordb.o Makefile
-	mkdir -p bin
-	$(CC) $(CFLAGS) -L. -L$(INSTALL_PATH)/lib examples/hello_world.c -o$@ build/collocatordb.o --std=gnu99 -lstdc++ -lm $(PLATFORM_LDFLAGS) $(PLATFORM_CCFLAGS) $(EXEC_LDFLAGS) -lrocksdb
-
-bin/testcdb: src/testcdb.cc src/collocatordb.h build/collocatordb.o Makefile
-	$(CXX) $(CXXFLAGS) -L. -L$(INSTALL_PATH)/lib $@.cc -o$@ build/collocatordb.o -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
-
-bin/dumpllr: examples/dumpllr.cc src/collocatordb.h build/collocatordb.o Makefile
-	$(CXX) $(CXXFLAGS) -L. -L$(INSTALL_PATH)/lib $@.cc -fopenmp -o$@ build/collocatordb.o $(INSTALL_PATH)/lib/librocksdb.a $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
-
-bin/dumppmicubed: examples/dumppmicubed.cc src/collocatordb.h build/collocatordb.o Makefile
-	$(CXX) $(CXXFLAGS) -L. -L$(INSTALL_PATH)/lib $@.cc -fopenmp -o$@ build/collocatordb.o -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
-
-bin/dumpldafdiff: dumpldafdiff.cc src/collocatordb.h build/collocatordb.o Makefile
-	$(CXX) $(CXXFLAGS) -D_GLIBCXX_PARALLEL -L. -L$(INSTALL_PATH)/lib $@.cc -fopenmp -o$@ build/collocatordb.o /vol/work/kupietz/rocksdb/librocksdb.a $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
-
-bin/collocatordb: src/collocatordb.cc Makefile
-	$(CXX) $(CXXFLAGS) -L$(INSTALL_PATH)/lib $@.cc -o$@ -lrocksdb $(PLATFORM_LDFLAGS) $(PLATFORM_CXXFLAGS) $(EXEC_LDFLAGS)
-
-static_lib: lib/libcollocatordb.a
-
-shared_lib: lib/libcollocatordb.so.$(COLLOCATDB_MAJOR).$(COLLOCATDB_MINOR)
-
-lib/libcollocatordb.a: $(LIB_OBJECTS)
-	mkdir -p lib
-	$(AM_V_AR)rm -f $@
-	$(AM_V_at)$(AR) $(ARFLAGS) $@ $(LIB_OBJECTS)
-
-lib/libcollocatordb.so.$(COLLOCATDB_MAJOR).$(COLLOCATDB_MINOR): $(LIB_SOURCES)
-	$(CXX) $(CXXFLAGS) -Wall -D_GLIBCXX_PARALLEL -fopenmp -c $< -Wl,-soname=libcollocatordb.so.1 -Wl,--version-script=collocatordb.exmap -shared -fPIC -o $@
-
-build/collocatordb.o : src/collocatordb.cc
-	mkdir -p build
-	$(CXX) $(CXXFLAGS) -c $< -o$@ $(PLATFORM_CXXFLAGS)
-
-.cc.o:
-	$(CXX) $(CXXFLAGS) -c $< -o$@ $(PLATFORM_CXXFLAGS)
-
-install-static: lib/libcollocatordb.a src/collocatordb.h
-	install -d $(INSTALL_PATH)/include && \
-	install -d $(INSTALL_PATH)/lib && \
-	install -t $(INSTALL_PATH)/include -D -C -m 644  src/collocatordb.h $ && \
-	install -t $(INSTALL_PATH)/lib -C -D -m 755 lib/libcollocatordb.a
-
-install-shared: lib/libcollocatordb.so.$(COLLOCATDB_MAJOR).$(COLLOCATDB_MINOR) src/collocatordb.h
-	install -d $(INSTALL_PATH)/include && \
-	install -d $(INSTALL_PATH)/lib && \
-	install -Dt $(INSTALL_PATH)/include -C -m 644 src/collocatordb.h && \
-	install -Dt $(INSTALL_PATH)/lib -C -m 755 lib/libcollocatordb.so.$(COLLOCATDB_MAJOR).$(COLLOCATDB_MINOR) && \
-		ln -fs $(INSTALL_PATH)/lib/libcollocatordb.so.$(COLLOCATDB_MAJOR).$(COLLOCATDB_MINOR) $(INSTALL_PATH)/lib/libcollocatordb.so.$(COLLOCATDB_MAJOR) && \
-		ln -fs $(INSTALL_PATH)/lib/libcollocatordb.so.$(COLLOCATDB_MAJOR) $(INSTALL_PATH)/lib/libcollocatordb.so
-
-bin/basic_test: tests/basic_test.c build/collocatordb.o
-	mkdir -p bin
-	$(CC) $(CFLAGS) -L. -L$(INSTALL_PATH)/lib $< -o$@ build/collocatordb.o --std=gnu99 -lstdc++ -lm $(PLATFORM_LDFLAGS) $(PLATFORM_CCFLAGS) $(EXEC_LDFLAGS) -lrocksdb
-	$@
-
-clean:
-	rm -rf $(LIB_DIR) $(BUILD_DIR) $(BIN_DIR)
-	rm -f *.so* *.a *.o c_testcdb collocatordb dumpldafdiff dumppmicubed dumpllr testcdb hello_world
diff --git a/README.md b/README.md
index f89ceb6..09edb37 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
 ### Install RocksdDB and prerequisites
 * install prerequisites on CentOS, FeDora, RHEL
     ```
-    sudo yum install snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4-devel libzstd-devel libomp-devel
+    sudo yum install cmake snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4-devel libzstd-devel libomp-devel
     
     git clone https://github.com/gflags/gflags.git
     cd gflags
@@ -14,7 +14,7 @@
     ```
 * install prerequisites on Ubuntu, Debian
     ```
-    sudo apt-get install libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev libomp-dev
+    sudo apt-get install cmake libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev libomp-dev
     ```
 * install RocksDBn [v5.11.3](https://github.com/facebook/rocksdb/releases/tag/v5.11.3)
     ```
@@ -26,7 +26,12 @@
     ```
 ### Install CollocatorDB
 ```
-make && make install
+git clone ssh://korap.ids-mannheim.de:29418/private/collocatordb && scp -p -P 29418 korap.ids-mannheim.de:hooks/commit-msg "collocatordb/.git/hooks/"
+cd collocatordb
+mkdir -p build
+cd build
+cmake ..
+make && make test && sudo make install
 ```
 ## Provided API
 ```
diff --git a/collocatordb.pc.in b/collocatordb.pc.in
new file mode 100644
index 0000000..9b15f62
--- /dev/null
+++ b/collocatordb.pc.in
@@ -0,0 +1,12 @@
+prefix=@CMAKE_INSTALL_PREFIX@
+exec_prefix=@CMAKE_INSTALL_PREFIX@
+libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@
+includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@
+
+Name: @PROJECT_NAME@
+Description: @PROJECT_DESCRIPTION@
+Version: @PROJECT_VERSION@
+
+Requires:
+Libs: -L${libdir} -lmylib
+Cflags: -I${includedir}