From 8a94d80deb217076ef19ecdf301df2570d9d0643 Mon Sep 17 00:00:00 2001
From: mpilman <markus.pilman@snowflake.com>
Date: Wed, 6 Feb 2019 19:27:38 -0800
Subject: [PATCH] fdbservice and fdbrpc now compiling

---
 CMakeLists.txt                            |  6 +++++-
 bindings/python/CMakeLists.txt            | 22 +++++++++++++++++-----
 cmake/ConfigureCompiler.cmake             |  8 ++++++--
 fdbcli/CMakeLists.txt                     |  5 ++++-
 fdbcli/fdbcli.actor.cpp                   |  4 +---
 fdbclient/NativeAPI.actor.cpp             |  8 ++++----
 fdbclient/ThreadSafeTransaction.actor.cpp |  5 +----
 fdbrpc/CMakeLists.txt                     |  7 ++++---
 fdbserver/SimulatedCluster.actor.cpp      |  5 +----
 fdbserver/fdbserver.actor.cpp             | 13 +++++--------
 fdbservice/CMakeLists.txt                 |  9 +++++++++
 fdbservice/FDBService.cpp                 |  1 +
 flow/CMakeLists.txt                       |  4 +++-
 13 files changed, 61 insertions(+), 36 deletions(-)
 create mode 100644 fdbservice/CMakeLists.txt

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6f4cc97d24..34fd2150c7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -205,7 +205,11 @@ add_subdirectory(fdbrpc)
 add_subdirectory(fdbclient)
 add_subdirectory(fdbserver)
 add_subdirectory(fdbcli)
-add_subdirectory(fdbmonitor)
+if(NOT WIN32)
+  add_subdirectory(fdbmonitor)
+else()
+  add_subdirectory(fdbservice)
+endif()
 add_subdirectory(bindings)
 add_subdirectory(fdbbackup)
 add_subdirectory(tests)
diff --git a/bindings/python/CMakeLists.txt b/bindings/python/CMakeLists.txt
index 67196a5b73..0f07cb0f10 100644
--- a/bindings/python/CMakeLists.txt
+++ b/bindings/python/CMakeLists.txt
@@ -17,12 +17,24 @@ set(out_files "")
 foreach(src ${SRCS})
   get_filename_component(dirname ${src} DIRECTORY)
   get_filename_component(extname ${src} EXT)
+  if(NOT EXISTS ${dirname})
+    file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/bindings/python/${dirname})
+  endif()
+  set(copy_command "cp")
+  set(from_path ${CMAKE_CURRENT_SOURCE_DIR}/${src})
+  set(to_path ${CMAKE_CURRENT_BINARY_DIR}/${src})
+  if (WIN32)
+    set(copy_command "copy")
+    # copy on Windows doesn't understand '/' separators
+    string(REPLACE "/" "\\" from_path "${from_path}")
+    string(REPLACE "/" "\\" to_path "${to_path}")
+  endif()
+  message(STATUS "COPY Command: ${copy_command} ${from_path} ${to_path}")
   add_custom_command(OUTPUT ${PROJECT_BINARY_DIR}/bindings/python/${src}
-	COMMAND mkdir -p ${PROJECT_BINARY_DIR}/bindings/python/${dirname}
-	COMMAND cp ${src} ${PROJECT_BINARY_DIR}/bindings/python/${dirname}/
-	DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src}
-	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
-	COMMENT "copy ${src}")
+    COMMAND ${copy_command} ${from_path} ${to_path}
+    DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${src}
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+    COMMENT "copy ${src}")
   set(out_files "${out_files};${PROJECT_BINARY_DIR}/bindings/python/${src}")
 endforeach()
 add_custom_target(python_binding ALL DEPENDS ${out_files})
diff --git a/cmake/ConfigureCompiler.cmake b/cmake/ConfigureCompiler.cmake
index 9b070f53a3..4504c49ba2 100644
--- a/cmake/ConfigureCompiler.cmake
+++ b/cmake/ConfigureCompiler.cmake
@@ -31,7 +31,11 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
 if (NOT OPEN_FOR_IDE)
   add_definitions(-DNO_INTELLISENSE)
 endif()
-add_definitions(-DUSE_UCONTEXT)
+if(WIN32)
+  add_definitions(-DUSE_USEFIBERS)
+else()
+  add_definitions(-DUSE_UCONTEXT)
+endif()
 enable_language(ASM)
 
 include(CheckFunctionExists)
@@ -40,7 +44,7 @@ set(CMAKE_REQUIRED_LIBRARIES c)
 
 
 if(WIN32)
-  add_compile_options(/W3 /EHsc /std:c++11)
+  add_compile_options(/W3 /EHsc /std:c++14 /bigobj)
 else()
   if(USE_GOLD_LINKER)
     set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=gold -Wl,--disable-new-dtags")
diff --git a/fdbcli/CMakeLists.txt b/fdbcli/CMakeLists.txt
index 50f36cd5e7..b6e0e7aa0f 100644
--- a/fdbcli/CMakeLists.txt
+++ b/fdbcli/CMakeLists.txt
@@ -2,9 +2,12 @@ set(FDBCLI_SRCS
   fdbcli.actor.cpp
   FlowLineNoise.actor.cpp
   FlowLineNoise.h
-  linenoise/linenoise.c
   linenoise/linenoise.h)
 
+if(NOT WIN32)
+  list(APPEN FDBCLI_SRCS linenoise/linenoise.c)
+endif()
+
 actor_set(FDBCLI_BUILD "${FDBCLI_SRCS}")
 add_executable(fdbcli "${FDBCLI_BUILD}")
 actor_compile(fdbcli "${FDBCLI_SRCS}")
diff --git a/fdbcli/fdbcli.actor.cpp b/fdbcli/fdbcli.actor.cpp
index 40795fbb77..711b1a6744 100644
--- a/fdbcli/fdbcli.actor.cpp
+++ b/fdbcli/fdbcli.actor.cpp
@@ -47,9 +47,7 @@
 #include "fdbcli/linenoise/linenoise.h"
 #endif
 
-#ifndef WIN32
-#include "versions.h"
-#endif
+#include "fdbclient/versions.h"
 
 #include "flow/actorcompiler.h"  // This must be the last #include.
 
diff --git a/fdbclient/NativeAPI.actor.cpp b/fdbclient/NativeAPI.actor.cpp
index 3ed2c3e767..c9b26fe52e 100644
--- a/fdbclient/NativeAPI.actor.cpp
+++ b/fdbclient/NativeAPI.actor.cpp
@@ -35,6 +35,7 @@
 #include "fdbclient/MutationList.h"
 #include "fdbclient/CoordinationInterface.h"
 #include "fdbclient/MonitorLeader.h"
+#include "fdbclient/versions.h"
 #include "fdbrpc/TLSConnection.h"
 #include "flow/Knobs.h"
 #include "fdbclient/Knobs.h"
@@ -50,16 +51,15 @@
 #undef max
 #else
 #include <time.h>
-#include "versions.h"
 #endif
-#include "flow/actorcompiler.h"  // This must be the last #include.
+#include "flow/actorcompiler.h" // This must be the last #include.
 
 extern IRandom* trace_random;
 extern const char* getHGVersion();
 
-using std::min;
-using std::max;
 using std::make_pair;
+using std::max;
+using std::min;
 
 NetworkOptions networkOptions;
 Reference<TLSOptions> tlsOptions;
diff --git a/fdbclient/ThreadSafeTransaction.actor.cpp b/fdbclient/ThreadSafeTransaction.actor.cpp
index fdc59e2783..b23da2da3d 100644
--- a/fdbclient/ThreadSafeTransaction.actor.cpp
+++ b/fdbclient/ThreadSafeTransaction.actor.cpp
@@ -21,12 +21,9 @@
 #include "fdbclient/ThreadSafeTransaction.h"
 #include "fdbclient/ReadYourWrites.h"
 #include "fdbclient/DatabaseContext.h"
+#include "fdbclient/versions.h"
 #include <new>
 
-#ifndef WIN32
-#include "versions.h"
-#endif
-
 // Users of ThreadSafeTransaction might share Reference<ThreadSafe...> between different threads as long as they don't call addRef (e.g. C API follows this).
 // Therefore, it is unsafe to call (explicitly or implicitly) this->addRef in any of these functions.
 
diff --git a/fdbrpc/CMakeLists.txt b/fdbrpc/CMakeLists.txt
index dcea3cea4f..bd14736152 100644
--- a/fdbrpc/CMakeLists.txt
+++ b/fdbrpc/CMakeLists.txt
@@ -33,9 +33,7 @@ set(FDBRPC_SRCS
   TraceFileIO.cpp
   # C files
   libcoroutine/Common.c
-  libcoroutine/context.c
   libcoroutine/Coro.c
-  libeio/eio.c
   zlib/adler32.c
   zlib/crc32.c
   zlib/deflate.c
@@ -51,7 +49,10 @@ set(FDBRPC_SRCS
   zlib/zutil.c)
 
 if(APPLE)
-  list(APPEND FDBRPC_SRCS libcoroutine/asm.S libcoroutine/context.c)
+  list(APPEND FDBRPC_SRCS libcoroutine/asm.S)
+endif()
+if(NOT WIN32)
+  list(APPEND FDBRPC_SRCS libcoroutine/context.c libeio/eio.c)
 endif()
 
 actor_set(FDBRPC_BUILD "${FDBRPC_SRCS}")
diff --git a/fdbserver/SimulatedCluster.actor.cpp b/fdbserver/SimulatedCluster.actor.cpp
index fff51e8ad0..9db3eb8206 100644
--- a/fdbserver/SimulatedCluster.actor.cpp
+++ b/fdbserver/SimulatedCluster.actor.cpp
@@ -34,10 +34,7 @@
 #include "fdbclient/ManagementAPI.h"
 #include "fdbclient/NativeAPI.h"
 #include "fdbclient/BackupAgent.h"
-
-#ifndef WIN32
-#include "versions.h"
-#endif
+#include "fdbclient/versions.h"
 #include "flow/actorcompiler.h"  // This must be the last #include.
 
 #undef max
diff --git a/fdbserver/fdbserver.actor.cpp b/fdbserver/fdbserver.actor.cpp
index b4b4ca00b0..ce19ffe311 100644
--- a/fdbserver/fdbserver.actor.cpp
+++ b/fdbserver/fdbserver.actor.cpp
@@ -40,12 +40,6 @@
 #include <stdio.h>
 #include <fstream>
 #include "fdbserver/pubsub.h"
-#ifdef _WIN32
-#define WIN32_LEAN_AND_MEAN
-#include <Windows.h>
-#undef min
-#undef max
-#endif
 #include "fdbserver/SimulatedCluster.h"
 #include "fdbserver/TesterInterface.h"
 #include "fdbserver/workloads/workloads.h"
@@ -56,6 +50,7 @@
 #include "fdbrpc/Platform.h"
 #include "fdbserver/CoroFlow.h"
 #include "flow/SignalSafeUnwind.h"
+#include "fdbclient/versions.h"
 
 #define BOOST_DATE_TIME_NO_LIB
 #include <boost/interprocess/managed_shared_memory.hpp>
@@ -68,8 +63,10 @@
 #endif
 #endif
 
-#ifndef WIN32
-#include "versions.h"
+#ifdef WIN32
+#define NOMINMAX
+#define WIN32_LEAN_AND_MEAN
+#include <Windows.h>
 #endif
 
 #include "flow/SimpleOpt.h"
diff --git a/fdbservice/CMakeLists.txt b/fdbservice/CMakeLists.txt
new file mode 100644
index 0000000000..f3a86929b1
--- /dev/null
+++ b/fdbservice/CMakeLists.txt
@@ -0,0 +1,9 @@
+set(FDBSERVICE_SRCS FDBService.cpp ServiceBase.cpp)
+
+add_executable(fdbservice ${FDBSERVICE_SRCS})
+#
+# FIXME: This include directory is an ugly hack. We probably want to fix this
+# as soon as we get rid of the old build system
+target_include_directories(fdbservice PRIVATE ${CMAKE_BINARY_DIR}/fdbclient)
+
+install(TARGETS fdbservice DESTINATION "${FDB_LIB_NOSUFFIX}/foundationdb" COMPONENT server)
diff --git a/fdbservice/FDBService.cpp b/fdbservice/FDBService.cpp
index 1105497323..8efee25593 100644
--- a/fdbservice/FDBService.cpp
+++ b/fdbservice/FDBService.cpp
@@ -30,6 +30,7 @@
 
 #include "..\flow\SimpleOpt.h"
 #include "..\fdbmonitor\SimpleIni.h"
+#include "fdbclient/versions.h"
 
 // For PathFileExists
 #include "Shlwapi.h"
diff --git a/flow/CMakeLists.txt b/flow/CMakeLists.txt
index 35ad067a99..fdb47db93c 100644
--- a/flow/CMakeLists.txt
+++ b/flow/CMakeLists.txt
@@ -82,8 +82,10 @@ add_library(flow STATIC ${FLOW_BUILD})
 actor_compile(flow "${FLOW_SRCS}")
 target_include_directories(flow SYSTEM PUBLIC ${CMAKE_THREAD_LIBS_INIT})
 target_include_directories(flow PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR})
-if (NOT APPLE)
+if (NOT APPLE AND NOT WIN32)
   set (FLOW_LIBS ${FLOW_LIBS} rt)
+elseif(WIN32)
+  target_link_libraries(flow PUBLIC winmm.lib)
 endif()
 target_link_libraries(flow PRIVATE ${FLOW_LIBS})
 target_link_libraries(flow PUBLIC boost_target Threads::Threads ${CMAKE_DL_LIBS})