From 06b85eedd9093c0d7bea7578a0d2f04b5cd2b974 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Wed, 3 Apr 2019 12:32:41 +0300
Subject: [PATCH] Updates to get most code compiling in Windows, including
 cv-node.

---
 CMakeLists.txt                              |  5 +++
 cmake/Findglog.cmake                        | 37 ---------------------
 cv-node/CMakeLists.txt                      |  2 +-
 cv-node/include/ftl/algorithms/rtcensus.hpp |  2 ++
 cv-node/src/main.cpp                        | 18 ++++++++--
 net/cpp/CMakeLists.txt                      |  4 +--
 net/cpp/include/ftl/net/dispatcher.hpp      |  5 +++
 net/cpp/include/ftl/net/p2p.hpp             |  4 +--
 net/cpp/include/ftl/uuid.hpp                |  9 +++--
 net/cpp/src/listener.cpp                    |  3 +-
 net/cpp/src/main.cpp                        |  7 ++++
 net/cpp/src/net.cpp                         |  4 ---
 net/cpp/src/socket.cpp                      |  2 +-
 net/cpp/src/ws_internal.cpp                 |  2 +-
 net/cpp/test/CMakeLists.txt                 | 14 ++++----
 net/cpp/test/net_integration.cpp            |  4 ++-
 net/cpp/test/p2p_base_unit.cpp              |  2 ++
 17 files changed, 59 insertions(+), 65 deletions(-)
 delete mode 100644 cmake/Findglog.cmake

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1a2f84925..14f6b2226 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,8 @@ find_package( URIParser REQUIRED )
 find_package( MsgPack REQUIRED )
 find_package( LibSGM )
 
+include_directories(${GLOG_INCLUDE_DIRS})
+
 # Why is this problematic on some machines?
 check_language(CUDA)
 if (CUDA_TOOLKIT_ROOT_DIR)
@@ -57,6 +59,9 @@ check_include_file("uuid/uuid.h" UUID_FOUND)
 if (NOT UUID_FOUND)
 	message(ERROR "UUID library is required")
 endif()
+find_library(UUID_LIBRARIES NAME uuid libuuid)
+else()
+
 endif()
 
 find_program(CPPCHECK_FOUND cppcheck)
diff --git a/cmake/Findglog.cmake b/cmake/Findglog.cmake
deleted file mode 100644
index 8b5883975..000000000
--- a/cmake/Findglog.cmake
+++ /dev/null
@@ -1,37 +0,0 @@
-###############################################################################
-# Find glog
-#
-
-if(WIN32)
-find_path(glog_DIR NAMES include/glog/logging.h PATHS "C:/Program Files/glog" "C:/Program Files/google-glog" "C:/Program Files (x86)/google-glog")
-set(glog_DIR ${glog_DIR})
-else()
-set(glog_DIR "")
-endif()
-
-# Find lib
-set(GLOG_FOUND FALSE CACHE BOOL "" FORCE)
-find_library(GLOG_LIBRARY
-    NAMES glog libglog
-    PATHS ${glog_DIR}
-    PATH_SUFFIXES lib/
-)
-
-# Find include
-find_path(GLOG_INCLUDE_DIRS
-    NAMES glog/logging.h
-    PATHS ${glog_DIR}
-    PATH_SUFFIXES include
-)
-
-include(FindPackageHandleStandardArgs)
-find_package_handle_standard_args(glog DEFAULT_MSG GLOG_LIBRARY GLOG_INCLUDE_DIRS)
-
-mark_as_advanced(GLOG_FOUND)
-
-if(GLOG_FOUND)
-	include_directories(${GLOG_INCLUDE_DIRS})
-    set(GLOG_FOUND TRUE CACHE BOOL "" FORCE)
-    set(GLOG_LIBRARIES ${GLOG_LIBRARY})
-    message(STATUS "Found glog")
-endif()
diff --git a/cv-node/CMakeLists.txt b/cv-node/CMakeLists.txt
index 8a77cce2b..53cfce7ab 100644
--- a/cv-node/CMakeLists.txt
+++ b/cv-node/CMakeLists.txt
@@ -47,6 +47,6 @@ set_property(TARGET cv-node PROPERTY CUDA_SEPARABLE_COMPILATION ON)
 endif()
 
 #target_include_directories(cv-node PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(cv-node net Threads::Threads ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} ${GLOG_LIBRARIES})
+target_link_libraries(cv-node Threads::Threads ${OpenCV_LIBS} ${LIBSGM_LIBRARIES} ${CUDA_LIBRARIES} glog::glog ${PROJECT_BINARY_DIR}/net/cpp/Release/net.lib)
 
 
diff --git a/cv-node/include/ftl/algorithms/rtcensus.hpp b/cv-node/include/ftl/algorithms/rtcensus.hpp
index 0a7d382b6..2623e18bd 100644
--- a/cv-node/include/ftl/algorithms/rtcensus.hpp
+++ b/cv-node/include/ftl/algorithms/rtcensus.hpp
@@ -10,6 +10,8 @@
 #include <ftl/disparity.hpp>
 #include <nlohmann/json.hpp>
 
+#include <ftl/config.h>
+
 #if defined HAVE_CUDA
 #include <opencv2/core/cuda.hpp>
 #endif
diff --git a/cv-node/src/main.cpp b/cv-node/src/main.cpp
index d336bcae8..0b664073c 100644
--- a/cv-node/src/main.cpp
+++ b/cv-node/src/main.cpp
@@ -8,6 +8,7 @@
 #include <string>
 #include <map>
 #include <vector>
+#include <fstream>
 
 #include <opencv2/opencv.hpp>
 #include <ftl/local.hpp>
@@ -42,9 +43,20 @@ static json config;
  * Find and load a JSON configuration file
  */
 static bool findConfiguration(const string &file) {
-	// TODO(nick) Check other locations
-	ifstream i((file != "") ? file : FTL_LOCAL_CONFIG_ROOT "/config.json");
-	if (!i.is_open()) return false;
+	ifstream i;
+	
+	if (file != "") {
+		i.open(file);
+	}
+	if (!i.is_open()) {
+		i.open("./config.json");
+	}
+	if (!i.is_open()) {
+		i.open(FTL_LOCAL_CONFIG_ROOT "/config.json");
+	}
+	if (!i.is_open()) {
+		i.open(FTL_GLOBAL_CONFIG_ROOT "/config.json");
+	}
 	i >> config;
 	return true;
 }
diff --git a/net/cpp/CMakeLists.txt b/net/cpp/CMakeLists.txt
index 1f80ac614..0031ab45f 100644
--- a/net/cpp/CMakeLists.txt
+++ b/net/cpp/CMakeLists.txt
@@ -20,10 +20,10 @@ set(NETSOURCE
 check_function_exists(uriParseSingleUriA HAVE_URIPARSESINGLE)
 
 add_library(net ${NETSOURCE})
-target_link_libraries(net pthread)
+target_link_libraries(net Threads::Threads glog::glog)
 
 add_executable(net-cli src/main.cpp)
-target_link_libraries(net-cli "${PROJECT_BINARY_DIR}/net/cpp/libnet.a" ${GLOG_LIBRARIES} ${URIPARSER_LIBRARIES} pthread readline uuid)
+target_link_libraries(net-cli "${PROJECT_BINARY_DIR}/net/cpp/libnet.a" glog::glog ${URIPARSER_LIBRARIES} Threads::Threads ${READLINE_LIBRARIES} ${UUID_LIBRARIES})
 add_dependencies(net-cli net)
 
 ADD_SUBDIRECTORY(test)
diff --git a/net/cpp/include/ftl/net/dispatcher.hpp b/net/cpp/include/ftl/net/dispatcher.hpp
index 3d9465991..c80849e7d 100644
--- a/net/cpp/include/ftl/net/dispatcher.hpp
+++ b/net/cpp/include/ftl/net/dispatcher.hpp
@@ -2,6 +2,11 @@
 #define _FTL_NET_DISPATCHER_HPP_
 
 #include <ftl/net/func_traits.hpp>
+
+#ifdef _MSC_VER
+#include <msgpack_optional.hpp>
+#endif
+
 #include <msgpack.hpp>
 #include <memory>
 #include <tuple>
diff --git a/net/cpp/include/ftl/net/p2p.hpp b/net/cpp/include/ftl/net/p2p.hpp
index f9547dbde..8c956fd26 100644
--- a/net/cpp/include/ftl/net/p2p.hpp
+++ b/net/cpp/include/ftl/net/p2p.hpp
@@ -1,6 +1,8 @@
 #ifndef _FTL_NET_P2P_HPP_
 #define _FTL_NET_P2P_HPP_
 
+#include <ftl/net/protocol.hpp>
+#include <ftl/net/socket.hpp>
 #include <ftl/uuid.hpp>
 #include <optional>
 #include <string>
@@ -8,8 +10,6 @@
 #include <chrono>
 #include <vector>
 #include <memory>
-#include <ftl/net/protocol.hpp>
-#include <ftl/net/socket.hpp>
 
 namespace ftl {
 namespace net {
diff --git a/net/cpp/include/ftl/uuid.hpp b/net/cpp/include/ftl/uuid.hpp
index 4a042c9cd..76fae7629 100644
--- a/net/cpp/include/ftl/uuid.hpp
+++ b/net/cpp/include/ftl/uuid.hpp
@@ -20,7 +20,7 @@ namespace ftl {
 		public:
 		UUID() {
 #ifdef WIN32
-			::UuidCreate(&uuid_);
+			::UuidCreate(&guid_);
 #else
 			uuid_generate(uuid_);
 #endif
@@ -50,7 +50,7 @@ namespace ftl {
 		std::string to_string() const {
 			char b[37];
 #ifdef WIN32
-			UuidToString(&uuid_, (RPC_CSTR*)b);
+			UuidToString(&guid_, (RPC_CSTR*)b);
 #else
 			uuid_unparse(uuid_, b);
 #endif
@@ -62,7 +62,10 @@ namespace ftl {
 		
 		private:
 #ifdef WIN32
-		_GUID uuid_;
+		union {
+			_GUID guid_;
+			unsigned char uuid_[16];
+		};
 #else
 		unsigned char uuid_[16];
 #endif
diff --git a/net/cpp/src/listener.cpp b/net/cpp/src/listener.cpp
index dcfe31bf0..4a9deb085 100644
--- a/net/cpp/src/listener.cpp
+++ b/net/cpp/src/listener.cpp
@@ -19,10 +19,9 @@
 #endif
 
 #ifdef WIN32
+#include <winsock2.h>
 #include <windows.h>
-#include <winsock.h>
 typedef int socklen_t;
-#define MSG_WAITALL 0
 #endif
 
 using namespace ftl;
diff --git a/net/cpp/src/main.cpp b/net/cpp/src/main.cpp
index 34c1a1d73..74730cbdd 100644
--- a/net/cpp/src/main.cpp
+++ b/net/cpp/src/main.cpp
@@ -5,7 +5,14 @@
 #include <ftl/net/socket.hpp>
 #include <memory>
 #include <thread>
+
+#ifndef WIN32
 #include <readline/readline.h>
+#endif
+
+#ifdef WIN32
+#pragma comment(lib, "Ws2_32.lib")
+#endif
 
 using std::string;
 using std::shared_ptr;
diff --git a/net/cpp/src/net.cpp b/net/cpp/src/net.cpp
index 9aae938f8..3ea57c1c4 100644
--- a/net/cpp/src/net.cpp
+++ b/net/cpp/src/net.cpp
@@ -10,10 +10,6 @@
 #include <iostream>
 #include <chrono>
 
-#ifdef WIN32
-#pragma comment(lib, "Ws2_32.lib")
-#endif
-
 using namespace std;
 using namespace std::chrono;
 using ftl::net::Listener;
diff --git a/net/cpp/src/socket.cpp b/net/cpp/src/socket.cpp
index 1f2f13641..252a4d0a3 100644
--- a/net/cpp/src/socket.cpp
+++ b/net/cpp/src/socket.cpp
@@ -19,9 +19,9 @@
 #endif
 
 #ifdef WIN32
-#include <windows.h>
 #include <winsock2.h>
 #include <Ws2tcpip.h>
+#include <windows.h>
 #endif
 
 #include <iostream>
diff --git a/net/cpp/src/ws_internal.cpp b/net/cpp/src/ws_internal.cpp
index 5b1aece52..1b8038d52 100644
--- a/net/cpp/src/ws_internal.cpp
+++ b/net/cpp/src/ws_internal.cpp
@@ -21,9 +21,9 @@
 #endif
 
 #ifdef WIN32
-#include <windows.h>
 #include <winsock2.h>
 #include <Ws2tcpip.h>
+#include <windows.h>
 #endif
 
 #include <string>
diff --git a/net/cpp/test/CMakeLists.txt b/net/cpp/test/CMakeLists.txt
index 89a38738a..391ad05e3 100644
--- a/net/cpp/test/CMakeLists.txt
+++ b/net/cpp/test/CMakeLists.txt
@@ -2,8 +2,7 @@ add_executable(protocol_unit
 	./tests.cpp
 	./protocol_unit.cpp
 )
-target_include_directories(protocol_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(protocol_unit ${GLOG_LIBRARIES})
+target_link_libraries(protocol_unit glog::glog)
 
 add_executable(socket_unit
 	./tests.cpp
@@ -11,13 +10,13 @@ add_executable(socket_unit
 	./socket_unit.cpp
 )
 target_include_directories(socket_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(socket_unit ${URIPARSER_LIBRARIES} ${GLOG_LIBRARIES})
+target_link_libraries(socket_unit ${URIPARSER_LIBRARIES} glog::glog)
 
 add_executable(uri_unit
 	./tests.cpp
 	./uri_unit.cpp
 )
-target_include_directories(uri_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
+
 target_link_libraries(uri_unit ${URIPARSER_LIBRARIES})
 
 add_executable(p2p_base_unit
@@ -31,8 +30,8 @@ add_executable(p2p_base_unit
 	../src/listener.cpp
 	../src/ws_internal.cpp
 )
-target_include_directories(p2p_base_unit PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(p2p_base_unit ${URIPARSER_LIBRARIES} gflags ${GLOG_LIBRARIES} uuid)
+
+target_link_libraries(p2p_base_unit ${URIPARSER_LIBRARIES} gflags glog::glog ${UUID_LIBRARIES})
 
 add_executable(net_integration
 	./tests.cpp
@@ -44,8 +43,7 @@ add_executable(net_integration
 	../src/net.cpp
 	../src/ws_internal.cpp
 )
-target_include_directories(net_integration PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(net_integration ${URIPARSER_LIBRARIES} ${GLOG_LIBRARIES})
+target_link_libraries(net_integration ${URIPARSER_LIBRARIES} glog::glog)
 
 add_test(URIUnitTest uri_unit)
 add_test(ProtocolUnitTest protocol_unit)
diff --git a/net/cpp/test/net_integration.cpp b/net/cpp/test/net_integration.cpp
index ea915da0b..38712c5c0 100644
--- a/net/cpp/test/net_integration.cpp
+++ b/net/cpp/test/net_integration.cpp
@@ -24,9 +24,11 @@ using std::shared_ptr;
 #endif
 
 #ifdef WIN32
-#include <windows.h>
 #include <winsock2.h>
 #include <Ws2tcpip.h>
+#include <windows.h>
+
+#pragma comment(lib, "Ws2_32.lib")
 #endif
 
 static int ssock = INVALID_SOCKET;
diff --git a/net/cpp/test/p2p_base_unit.cpp b/net/cpp/test/p2p_base_unit.cpp
index 656f8ddc7..ee360c553 100644
--- a/net/cpp/test/p2p_base_unit.cpp
+++ b/net/cpp/test/p2p_base_unit.cpp
@@ -7,6 +7,8 @@
 
 #ifndef WIN32
 #include <sys/select.h>
+#else
+typedef int ssize_t;
 #endif
 
 using ftl::net::Dispatcher;
-- 
GitLab