Improve MacOS support

Change-Id: Id54a73c025d01ee37d2373a62506eb52a4f75d35
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 05c4abf..a888765 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -15,9 +15,9 @@
     - end_section install_linux_packages
 
   script:
-    - start_section install_rocksdb_static "Building and installing rocksdb-static"
-    - curl -L https://github.com/facebook/rocksdb/archive/refs/tags/v5.11.3.tar.gz | tar zx
-    - cd rocksdb-5.11.3
+    - start_section install_rocksdb "Building and installing rocksdb"
+    - git clone https://github.com/kupietz/rocksdb.git -b 5.11.fb --single-branch
+    - cd rocksdb
     - export PROCS=$(nproc)
     - make -j $PROCS static_lib DISABLE_WARNING_AS_ERROR=1 WARNING_FLAGS=-w
     - make install-static DISABLE_WARNING_AS_ERROR=1 WARNING_FLAGS=-w
@@ -30,7 +30,7 @@
     - strip --strip-unneeded `find -name librocksdb.so`
     - ldconfig
     - cd ..
-    - end_section rocksdb_shared
+    - end_section install_rocksdb
 
     - start_section install_collocatordb "Building and installing collocatordb"
     - mkdir -p build
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5a0f448..adc8496 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -11,21 +11,32 @@
     set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
 endif("${isSystemDir}" STREQUAL "-1")
 
-include_directories(/usr/local/include;/usr/local/kl/include)
-link_directories(/usr/local/kl/lib /usr/local/lib)
-find_library(ROCKSDB NAMES librocksdb.so.5.11)
+include_directories(/usr/local/include /opt/homebrew/include)
+link_directories(/usr/local/lib64 /usr/local/lib /usr/lib64 /usr/lib /opt/homebrew/lib)
+if (1 AND APPLE)
+       message("MacOS deteted")
+       set(LIBRT "")
+       find_library(ROCKSDB NAMES librocksdb.a)
+       set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flat_namespace")
+       set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flat_namespace")
+else()
+       set(LIBRT "rt")
+       find_library(ROCKSDB NAMES librocksdb.so.5.11)
+endif()
 find_library(ROCKSDB_STATIC librocksdb.a)
 
 enable_testing()
 add_library(collocatordb SHARED src/collocatordb.cc)
-target_link_libraries(collocatordb pthread rt snappy z bz2 lz4 zstd ${ROCKSDB} dl)
+
+target_link_libraries(collocatordb pthread ${LIBRT} snappy z bz2 lz4 zstd ${ROCKSDB} dl)
+
 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 ${ROCKSDB_STATIC} pthread rt snappy z bz2 lz4 zstd dl)
+target_link_libraries(collocatordb_static ${ROCKSDB_STATIC} pthread ${LIBRT} snappy z bz2 lz4 zstd dl)
 
 add_executable(basic_test tests/basic_test.c tests/acutest.h)
 TARGET_LINK_LIBRARIES(basic_test ${ROCKSDB} collocatordb)
@@ -40,3 +51,8 @@
                 LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
                 ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
                 PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
+
+if (1 AND APPLE)
+    add_custom_command(TARGET collocatordb POST_BUILD
+        COMMAND install_name_tool -id "/usr/local/lib/libcollocatordb.1.dylib" $<TARGET_FILE:collocatordb>)
+endif()
diff --git a/README.md b/README.md
index ba180bf..b7eb570 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@
 
 ### Install RocksDB and prerequisites
 
-* prerequisites on CentOS, FeDora, RHEL
+* on CentOS, Fedora, RHEL
 
     ```bash
     sudo yum install cmake3 snappy snappy-devel zlib zlib-devel bzip2 bzip2-devel lz4-devel libzstd-devel libomp-devel
@@ -16,22 +16,41 @@
     cd ..
     ```
 
-* prerequisites on Ubuntu, Debian
+* on Ubuntu, Debian
 
     ```bash
     sudo apt-get install cmake libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev libomp-dev
     ```
 
-* install [RocksDB v5.11.3](https://github.com/facebook/rocksdb/releases/tag/v5.11.3)
+* on MacOS
 
     ```bash
-    curl -L https://github.com/facebook/rocksdb/archive/refs/tags/v5.11.3.tar.gz | tar zx
-    cd rocksdb-5.11.3
-    make static_lib DISABLE_WARNING_AS_ERROR=1 && sudo make install-static DISABLE_WARNING_AS_ERROR=1
-    make shared_lib DISABLE_WARNING_AS_ERROR=1 && sudo make install-shared DISABLE_WARNING_AS_ERROR=1
+    brew install cmake snappy zlib bzip2 lz4 zstd libomp gflags
+    ```
+
+* install our fork of [RocksDB v5.11.3.fb](https://github.com/kupietz/rocksdb/tree/5.11.fb)
+  * on Linux
+
+    ```bash
+    git clone https://github.com/kupietz/rocksdb.git -b 5.11.fb --single-branch
+    cd rocksdb
+    make -j $(nproc) static_lib DISABLE_WARNING_AS_ERROR=1 && sudo make install-static DISABLE_WARNING_AS_ERROR=1
+    make -j $(nproc) shared_lib DISABLE_WARNING_AS_ERROR=1 && sudo make install-shared DISABLE_WARNING_AS_ERROR=1
+    cd build
     ldconfig
     ```
 
+  * on MacOS
+
+    ```bash
+    git clone https://github.com/kupietz/rocksdb.git -b 5.11.fb --single-branch
+    cd rocksdb
+    mkdir -f build
+    cd build
+    cmake .. -DWITH_SNAPPY=1 -DWITH_LZ4=1 -DWITH_ZLIB=1 -DWITH_GFLAGS=1 -DCMAKE_INSTALL_LIBDIR=/usr/local/lib
+    make -j $(sysctl -n hw.ncpu) && sudo make install
+    ```
+
 ### Install CollocatorDB
 
 ```bash
diff --git a/src/collocatordb.cc b/src/collocatordb.cc
index 52cbc27..2660346 100644
--- a/src/collocatordb.cc
+++ b/src/collocatordb.cc
@@ -17,6 +17,7 @@
 #include <rocksdb/merge_operator.h>
 #include <rocksdb/slice_transform.h>
 #include "merge_operators.h"
+#include "export.h"
 
 #define WINDOW_SIZE 5
 #define FREQUENCY_THRESHOLD 5
@@ -842,23 +843,23 @@
 #pragma clang diagnostic push
 #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
 #endif
-  COLLOCATORS *open_collocatordb_for_write(char *dbname) {
+  DLL_EXPORT COLLOCATORS *open_collocatordb_for_write(char *dbname) {
 		return new rocksdb::CollocatorDB(dbname, false);
 	}
 
-	COLLOCATORS *open_collocatordb(char *dbname) {
+	DLL_EXPORT COLLOCATORS *open_collocatordb(char *dbname) {
 		return new rocksdb::CollocatorDB(dbname, true);
 	}
 	
-	void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
+	DLL_EXPORT void inc_collocator(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
 		db->inc(w1, w2, dist);
 	}
 
-  void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
+  DLL_EXPORT void dump_collocators(COLLOCATORS *db, uint32_t w1, uint32_t w2, int8_t dist) {
     db->dump(w1, w2, dist);
   }
 
-  COLLOCATORS *get_collocators(COLLOCATORS *db, uint32_t w1) {
+  DLL_EXPORT COLLOCATORS *get_collocators(COLLOCATORS *db, uint32_t w1) {
     std::vector<Collocator> c = db->get_collocators(w1);
     if (c.empty())
       return NULL;
@@ -868,7 +869,7 @@
     return p;
   }
 
-  COLLOCATORS *get_collocation_scores(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
+  DLL_EXPORT COLLOCATORS *get_collocation_scores(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
     std::vector<Collocator> c = db->get_collocation_scores(w1, w2);
     if (c.empty())
       return NULL;
@@ -878,20 +879,20 @@
     return p;
   }
 
-  char *get_word(COLLOCATORS *db, uint32_t w) {
+  DLL_EXPORT char *get_word(COLLOCATORS *db, uint32_t w) {
     return strdup(db->getWord(w).c_str());
   }
 
-  void read_vocab(COLLOCATORS *db, char *fname) {
+  DLL_EXPORT void read_vocab(COLLOCATORS *db, char *fname) {
     std::string fName(fname);
     db->readVocab(fName);
   }
 
-  const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
+  DLL_EXPORT const char *get_collocators_as_json(COLLOCATORS *db, uint32_t w1) {
     return strdup(db->collocators2json(w1, db->get_collocators(w1)).c_str());
   }
 
-  const char *get_collocation_scores_as_json(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
+  DLL_EXPORT const char *get_collocation_scores_as_json(COLLOCATORS *db, uint32_t w1, uint32_t w2) {
     return strdup(db->collocators2json(w1, db->get_collocation_scores(w1, w2)).c_str());
   }
 
diff --git a/src/export.h b/src/export.h
new file mode 100644
index 0000000..b4ff5d2
--- /dev/null
+++ b/src/export.h
@@ -0,0 +1,16 @@
+// export.h
+
+#ifndef EXPORT_H
+#define EXPORT_H
+
+#ifdef _WIN32
+    #ifdef BUILDING_DLL
+        #define DLL_EXPORT __declspec(dllexport)
+    #else
+        #define DLL_EXPORT __declspec(dllimport)
+    #endif
+#else
+    #define DLL_EXPORT __attribute__((visibility("default")))
+#endif
+
+#endif // EXPORT_H