From 45963b2064d91972b51194f868045de0df742396 Mon Sep 17 00:00:00 2001
From: Ulrich Mohr <mohr@irs.uni-stuttgart.de>
Date: Fri, 27 Jan 2023 16:41:04 +0100
Subject: [PATCH] tuning win build

---
 CMakeLists.txt                                | 10 ++++----
 src/fsfw/globalfunctions/DleParser.cpp        |  1 +
 .../windows/cmake/x64-windows-toolchain       | 23 +++++++++++++++++++
 3 files changed, 29 insertions(+), 5 deletions(-)
 create mode 100644 unittests/testcfg/windows/cmake/x64-windows-toolchain

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 79549a4af..8c20979d4 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,6 +64,11 @@ elseif(${CMAKE_CXX_STANDARD} LESS 17)
       "${MSG_PREFIX} Compiling the FSFW requires a minimum of C++17 support")
 endif()
 
+if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
+  # Manually tweak MSVC to emit (about) the same warnings as clang and gcc on linux
+  add_compile_options("/permissive-" /wd4267 /wd4244 /wd4244 /wd4305 /wd4805 /wd4267 /wd4646 /wd4065 "/Dand=&&" "/Dor=||" "/Dnot=!")
+endif()
+
 set(FSFW_SOURCES_DIR "${CMAKE_SOURCE_DIR}/src/fsfw")
 
 set(FSFW_ETL_LIB_NAME etl)
@@ -476,11 +481,6 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
   set(FSFW_WARNING_FLAGS -Weverything -Wno-gnu-anonymous-struct)
 endif()
 
-if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
-  set(COMPILER_FLAGS "/permissive-" "/Wall")
-  add_compile_definitions(NOMINMAX not=! and=&& or=||)
-endif()
-
 # Required include paths to compile the FSFW
 target_include_directories(
   ${LIB_FSFW_NAME} INTERFACE ${CMAKE_SOURCE_DIR} ${FSFW_CONFIG_PATH_ABSOLUTE}
diff --git a/src/fsfw/globalfunctions/DleParser.cpp b/src/fsfw/globalfunctions/DleParser.cpp
index cc695babe..8f764f648 100644
--- a/src/fsfw/globalfunctions/DleParser.cpp
+++ b/src/fsfw/globalfunctions/DleParser.cpp
@@ -52,6 +52,7 @@ ReturnValue_t DleParser::parseRingBuf(size_t& readSize) {
         // without skipping the STX
         readSize = vectorIdx;
         ErrorInfo info;
+        info.len = vectorIdx;
         setErrorContext(ErrorTypes::CONSECUTIVE_STX_CHARS, info);
         return POSSIBLE_PACKET_LOSS;
       }
diff --git a/unittests/testcfg/windows/cmake/x64-windows-toolchain b/unittests/testcfg/windows/cmake/x64-windows-toolchain
new file mode 100644
index 000000000..f62b0f64f
--- /dev/null
+++ b/unittests/testcfg/windows/cmake/x64-windows-toolchain
@@ -0,0 +1,23 @@
+set(CMAKE_SYSTEM_NAME Windows)
+
+# which compilers to use for C and C++
+set(CMAKE_C_COMPILER   /opt/msvc/bin/x64/cl)
+set(CMAKE_CXX_COMPILER /opt/msvc/bin/x64/cl)
+
+# built in tests fail
+set(CMAKE_C_COMPILER_WORKS 1)
+set(CMAKE_CXX_COMPILER_WORKS 1)
+
+
+# adjust the default behavior of the FIND_XXX() commands:
+# search programs in the host environment
+set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM ONLY)
+
+# search headers and libraries in the target environment
+set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
+set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
+
+# ignore host installed libraries
+# makes find_package() ignore the <package>Config.cmake files
+set(CMAKE_IGNORE_PATH /usr/local)