From 70fa8799952e905875c09f91927fe9daa42e80bd Mon Sep 17 00:00:00 2001
From: Sergey Yakubov <sergey.yakubov@desy.de>
Date: Wed, 20 Dec 2017 17:12:27 +0100
Subject: [PATCH] start adapting for windows

---
 CMakeLists.txt                                |  1 +
 CMakeModules/testing_cpp.cmake                | 12 +++++-
 common/cpp/CMakeLists.txt                     |  6 +--
 common/cpp/include/system_wrappers/io.h       |  8 ++++
 common/cpp/src/system_io.cpp                  | 10 ++++-
 common/cpp/src/system_io_windows.cpp          | 43 +++++++++++++++++++
 examples/worker/CMakeLists.txt                |  2 +
 examples/worker/process_folder/CMakeLists.txt |  2 +-
 producer/api/CMakeLists.txt                   |  2 +-
 tests/CMakeLists.txt                          |  1 +
 tests/common/cpp/CMakeLists.txt               |  2 +-
 .../worker/connect_multithread/CMakeLists.txt |  2 +-
 tests/worker/next_multithread/CMakeLists.txt  |  2 +-
 worker/api/cpp/CMakeLists.txt                 |  4 +-
 worker/api/cpp/src/folder_data_broker.cpp     |  2 +-
 worker/api/cpp/src/folder_data_broker.h       |  2 +-
 16 files changed, 87 insertions(+), 14 deletions(-)
 create mode 100644 common/cpp/src/system_io_windows.cpp

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 4f0a10ab5..9fa7ba331 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1,5 +1,6 @@
 cmake_minimum_required(VERSION 2.8)
 set(CMAKE_CXX_STANDARD 11)
+set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
 
 project(HIDRA2)
 
diff --git a/CMakeModules/testing_cpp.cmake b/CMakeModules/testing_cpp.cmake
index 3cb0222d5..2702cef34 100644
--- a/CMakeModules/testing_cpp.cmake
+++ b/CMakeModules/testing_cpp.cmake
@@ -4,13 +4,23 @@ if (BUILD_TESTS)
     find_package(Threads)
     find_program(MEMORYCHECK_COMMAND valgrind)
     set(MEMORYCHECK_COMMAND_OPTIONS "--trace-children=yes --leak-check=full --error-exitcode=1")
+    set (gtest_SOURCE_DIR "C:/googletest")
 endif ()
 
 function(gtest target test_source_files test_libraries)
     if (BUILD_TESTS)
         include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
+        link_directories(${gtest_SOURCE_DIR}/lib)
         add_executable(test-${target} ${test_source_files})
-        target_link_libraries(test-${target} gtest gmock gtest_main ${CMAKE_THREAD_LIBS_INIT})
+
+        IF(WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
+            set(GTEST_LIBS gtestd gtest_maind gmockd)
+        ELSE()
+            set(GTEST_LIBS gtest gmock gtest_main)
+        ENDIF(WIN32 AND ${CMAKE_BUILD_TYPE} STREQUAL "Debug")
+        target_link_libraries(test-${target} ${GTEST_LIBS} ${CMAKE_THREAD_LIBS_INIT})
+#        set_target_properties(test-${target} PROPERTIES COMPILE_FLAGS "-DGTEST_LINKED_AS_SHARED_LIBRARY=1")
+
         if (NOT ${test_libraries} STREQUAL "")
             target_link_libraries(test-${target} ${test_libraries})
         endif ()
diff --git a/common/cpp/CMakeLists.txt b/common/cpp/CMakeLists.txt
index 9fef72369..0cf8083f6 100644
--- a/common/cpp/CMakeLists.txt
+++ b/common/cpp/CMakeLists.txt
@@ -6,7 +6,7 @@ set(SOURCE_FILES
         include/system_wrappers/system_io.h
         src/system_io.cpp)
 IF(WIN32)
-    # Do windows specific includes
+    set(SOURCE_FILES ${SOURCE_FILES} src/system_io_windows.cpp)
 ELSEIF(UNIX)
     set(SOURCE_FILES ${SOURCE_FILES} src/system_io_linux.cpp)
 ENDIF(WIN32)
@@ -15,7 +15,7 @@ ENDIF(WIN32)
 ################################
 # Library
 ################################
-#add_library(${TARGET_NAME} SHARED STATIC ${SOURCE_FILES})
-add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
+add_library(${TARGET_NAME} STATIC ${SOURCE_FILES})
+#add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
 target_include_directories(${TARGET_NAME} PUBLIC include)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
diff --git a/common/cpp/include/system_wrappers/io.h b/common/cpp/include/system_wrappers/io.h
index 059b376c5..ad34debca 100644
--- a/common/cpp/include/system_wrappers/io.h
+++ b/common/cpp/include/system_wrappers/io.h
@@ -9,6 +9,14 @@
 
 #include "common/file_info.h"
 
+#ifdef __linux__
+#include <unistd.h>
+#elif defined(_MSC_VER)
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#endif
+
+
 namespace hidra2 {
 
 
diff --git a/common/cpp/src/system_io.cpp b/common/cpp/src/system_io.cpp
index c92dbb0f1..f445aca17 100644
--- a/common/cpp/src/system_io.cpp
+++ b/common/cpp/src/system_io.cpp
@@ -1,7 +1,15 @@
 #include <fcntl.h>
-#include <unistd.h>
 #include <iostream>
 
+#ifdef __linux__
+#include <unistd.h>
+#elif defined(_MSC_VER)
+#include <BaseTsd.h>
+typedef SSIZE_T ssize_t;
+#endif
+
+#include <io.h>
+
 #include <system_wrappers/system_io.h>
 
 namespace hidra2 {
diff --git a/common/cpp/src/system_io_windows.cpp b/common/cpp/src/system_io_windows.cpp
new file mode 100644
index 000000000..c013110c2
--- /dev/null
+++ b/common/cpp/src/system_io_windows.cpp
@@ -0,0 +1,43 @@
+#include "system_wrappers/system_io.h"
+
+#include <cstring>
+
+//#include <dirent.h>
+
+#include <sys/stat.h>
+#include <algorithm>
+
+#include <cerrno>
+
+using std::string;
+using std::vector;
+using std::chrono::system_clock;
+
+namespace hidra2 {
+
+IOErrors IOErrorFromErrno() {
+    IOErrors err;
+    switch (errno) {
+    case 0:
+        err = IOErrors::NO_ERROR;
+        break;
+    case ENOENT:
+    case ENOTDIR:
+        err = IOErrors::FILE_NOT_FOUND;
+        break;
+    case EACCES:
+        err = IOErrors::PERMISSIONS_DENIED;
+        break;
+    default:
+        err = IOErrors::UNKWOWN_ERROR;
+        break;
+    }
+    return err;
+}
+
+std::vector<FileInfo> SystemIO::FilesInFolder(const string& folder, IOErrors* err) {
+    std::vector<FileInfo> files{};
+    return files;
+}
+
+}
diff --git a/examples/worker/CMakeLists.txt b/examples/worker/CMakeLists.txt
index 29337dc4b..6e1b685be 100644
--- a/examples/worker/CMakeLists.txt
+++ b/examples/worker/CMakeLists.txt
@@ -1,3 +1,5 @@
+find_package(Threads)
+
 add_subdirectory(process_folder)
 
 
diff --git a/examples/worker/process_folder/CMakeLists.txt b/examples/worker/process_folder/CMakeLists.txt
index eeb7f2dbc..4bfa9c3d6 100644
--- a/examples/worker/process_folder/CMakeLists.txt
+++ b/examples/worker/process_folder/CMakeLists.txt
@@ -9,7 +9,7 @@ set(SOURCE_FILES process_folder.cpp)
 #set(CMAKE_FIND_STATIC FIRST)
 #endif()
 add_executable(${TARGET_NAME} ${SOURCE_FILES})
-target_link_libraries(${TARGET_NAME} common worker-api pthread)
+target_link_libraries(${TARGET_NAME} common worker-api ${CMAKE_THREAD_LIBS_INIT})
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 #set_target_properties(${TARGET_NAME} PROPERTIES LINK_FLAGS "-static-libstdc++")
 
diff --git a/producer/api/CMakeLists.txt b/producer/api/CMakeLists.txt
index 40d5bc8d8..b09e6b457 100644
--- a/producer/api/CMakeLists.txt
+++ b/producer/api/CMakeLists.txt
@@ -5,7 +5,7 @@ set(SOURCE_FILES src/producer/producer.cpp include/producer/producer.h)
 ################################
 # Library
 ################################
-add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
+add_library(${TARGET_NAME} STATIC ${SOURCE_FILES})
 target_include_directories(${TARGET_NAME} PUBLIC include)
 target_link_libraries(${TARGET_NAME} common)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 4eea8d27a..59f4f8214 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,4 +1,5 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 3.7) # needed for fixtures
+find_package(Threads)
 
 add_subdirectory(common/cpp)
 add_subdirectory(system_io)
diff --git a/tests/common/cpp/CMakeLists.txt b/tests/common/cpp/CMakeLists.txt
index 1857e8929..1eec82d9f 100644
--- a/tests/common/cpp/CMakeLists.txt
+++ b/tests/common/cpp/CMakeLists.txt
@@ -5,6 +5,6 @@ set(SOURCE_FILES
 ################################
 # Library
 ################################
-add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
+add_library(${TARGET_NAME} STATIC ${SOURCE_FILES})
 target_include_directories(${TARGET_NAME} PUBLIC include)
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
diff --git a/tests/worker/connect_multithread/CMakeLists.txt b/tests/worker/connect_multithread/CMakeLists.txt
index b4baf2ec9..5d7cc640f 100644
--- a/tests/worker/connect_multithread/CMakeLists.txt
+++ b/tests/worker/connect_multithread/CMakeLists.txt
@@ -6,7 +6,7 @@ set(SOURCE_FILES content_multithread.cpp)
 # Executable and link
 ################################
 add_executable(${TARGET_NAME} ${SOURCE_FILES})
-target_link_libraries(${TARGET_NAME} test_common common worker-api pthread)
+target_link_libraries(${TARGET_NAME} test_common common worker-api ${CMAKE_THREAD_LIBS_INIT})
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 
 ################################
diff --git a/tests/worker/next_multithread/CMakeLists.txt b/tests/worker/next_multithread/CMakeLists.txt
index 2e4fc652f..48cab6201 100644
--- a/tests/worker/next_multithread/CMakeLists.txt
+++ b/tests/worker/next_multithread/CMakeLists.txt
@@ -6,7 +6,7 @@ set(SOURCE_FILES next_multithread.cpp)
 # Executable and link
 ################################
 add_executable(${TARGET_NAME} ${SOURCE_FILES})
-target_link_libraries(${TARGET_NAME} test_common common worker-api pthread)
+target_link_libraries(${TARGET_NAME} test_common common worker-api  ${CMAKE_THREAD_LIBS_INIT})
 set_target_properties(${TARGET_NAME} PROPERTIES LINKER_LANGUAGE CXX)
 
 ################################
diff --git a/worker/api/cpp/CMakeLists.txt b/worker/api/cpp/CMakeLists.txt
index bb138ff79..da84ebcfb 100644
--- a/worker/api/cpp/CMakeLists.txt
+++ b/worker/api/cpp/CMakeLists.txt
@@ -6,8 +6,8 @@ set(SOURCE_FILES include/worker/data_broker.h src/data_broker.cpp src/folder_dat
 ################################
 # Library
 ################################
-add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
-#add_library(${TARGET_NAME} SHARED STATIC ${SOURCE_FILES})
+#add_library(${TARGET_NAME} SHARED ${SOURCE_FILES})
+add_library(${TARGET_NAME} STATIC ${SOURCE_FILES})
 
 target_include_directories(${TARGET_NAME} PUBLIC include)
 target_link_libraries(${TARGET_NAME} common)
diff --git a/worker/api/cpp/src/folder_data_broker.cpp b/worker/api/cpp/src/folder_data_broker.cpp
index 2696f73eb..b989170db 100644
--- a/worker/api/cpp/src/folder_data_broker.cpp
+++ b/worker/api/cpp/src/folder_data_broker.cpp
@@ -58,7 +58,7 @@ WorkerErrorCode FolderDataBroker::CanGetData(FileInfo* info, FileData* data, int
         return WorkerErrorCode::WRONG_INPUT;
     }
 
-    if (nfile >= filelist_.size()) {
+    if (nfile >= (int) filelist_.size()) {
         return WorkerErrorCode::NO_DATA;
     }
     return WorkerErrorCode::OK;
diff --git a/worker/api/cpp/src/folder_data_broker.h b/worker/api/cpp/src/folder_data_broker.h
index 5fd4f08e4..27d960172 100644
--- a/worker/api/cpp/src/folder_data_broker.h
+++ b/worker/api/cpp/src/folder_data_broker.h
@@ -20,7 +20,7 @@ class FolderDataBroker final : public hidra2::DataBroker {
 
   private:
     bool is_connected_;
-    int64_t current_file_;
+    int current_file_;
     std::string base_path_;
     std::vector<FileInfo>  filelist_;
     WorkerErrorCode CanGetData(FileInfo* info, FileData* data, int nfile) const;
-- 
GitLab