From 3a79e63a92febe642d903fc9af9689d2363d59ed Mon Sep 17 00:00:00 2001
From: Jon Beniston <jon@beniston.com>
Date: Fri, 26 May 2023 08:28:12 +0100
Subject: [PATCH] Enable SigMF plugins on windows

---
 CMakeLists.txt                                     |  7 +++++++
 cmake/cpack/CMakeLists.txt                         | 11 +++++++++++
 plugins/channelrx/CMakeLists.txt                   |  2 +-
 plugins/samplesource/CMakeLists.txt                |  2 +-
 plugins/samplesource/sigmffileinput/CMakeLists.txt |  9 ++++++++-
 sdrbase/CMakeLists.txt                             |  7 +++++--
 6 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6a465fa3e..983b23354 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -372,11 +372,18 @@ if(VS2019)
   set(HIDAPI_LIBRARIES "${EXTERNAL_LIBRARY_FOLDER}/hidapi/x64/hidapi.lib" CACHE INTERNAL "")
   set(HIDAPI_DLL_DIR "${EXTERNAL_LIBRARY_FOLDER}/hidapi/x64" CACHE INTERNAL "")
 
+  set(LIBSIGMF_FOUND ON CACHE INTERNAL "")
+  set(LIBSIGMF_INCLUDE_DIR "${EXTERNAL_LIBRARY_FOLDER}/libsigmf/include" CACHE INTERNAL "")
+  set(LIBSIGMF_LIBRARIES "${EXTERNAL_LIBRARY_FOLDER}/libsigmf/lib/libsigmf.lib" DbgHelp CACHE INTERNAL "")
+  set(LIBSIGMF_LIBRARIES_DEBUG "${EXTERNAL_LIBRARY_FOLDER}/libsigmf/lib/libsigmfd.lib" DbgHelp CACHE INTERNAL "")
+  set(LIBSIGMF_DLL_DIR "${EXTERNAL_LIBRARY_FOLDER}/libsigmf/lib/" CACHE INTERNAL "")
+
   # used on fixup_bundle phase
   set(WINDOWS_FIXUP_BUNDLE_LIB_DIRS
     "${EXTERNAL_LIBRARY_FOLDER}/fftw-3"
     "${EXTERNAL_LIBRARY_FOLDER}/libusb/MS64/dll"
     "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/bin"
+    "${EXTERNAL_LIBRARY_FOLDER}/libsigmf/lib"
   )
 elseif(ANDROID)
   set(EXTERNAL_LIBRARY_FOLDER "${CMAKE_SOURCE_DIR}/external/android")
diff --git a/cmake/cpack/CMakeLists.txt b/cmake/cpack/CMakeLists.txt
index 704efac23..03c1390ca 100644
--- a/cmake/cpack/CMakeLists.txt
+++ b/cmake/cpack/CMakeLists.txt
@@ -228,6 +228,14 @@ elseif(WIN32 OR MINGW)
     )
   endforeach(openssl_dll)
 
+  file(GLOB LIBSIGMF_DLLS "${LIBSIGMF_DLL_DIR}/libsigmf*${CMAKE_SHARED_LIBRARY_SUFFIX}")
+  foreach(libsigmf_dll ${LIBSIGMF_DLLS})
+    get_filename_component(libsigmf_dll_name "${libsigmf_dll}"  NAME)
+    add_custom_target(copy_libsigmf_${libsigmf_dll_name} ALL
+      COMMAND ${CMAKE_COMMAND} -E copy_if_different "${libsigmf_dll}" "${SDRANGEL_BINARY_BIN_DIR}/"
+    )
+  endforeach(libsigmf_dll)
+
   # TODO we need a way to fixup_bundle() on the build bin/ directory without call install
   if(BUILD_GUI)
     install(CODE "
@@ -284,6 +292,9 @@ elseif(WIN32 OR MINGW)
     # OpenSSL
     install(DIRECTORY "${OPENSSL_DLL_DIR}/" DESTINATION "${INSTALL_LIB_DIR}"
       FILES_MATCHING PATTERN "*${CMAKE_SHARED_LIBRARY_SUFFIX}")
+    # libsigmf
+    install(DIRECTORY "${LIBSIGMF_DLL_DIR}/" DESTINATION "${INSTALL_LIB_DIR}"
+      FILES_MATCHING PATTERN "*${CMAKE_SHARED_LIBRARY_SUFFIX}")
 
 
     install(CODE "
diff --git a/plugins/channelrx/CMakeLists.txt b/plugins/channelrx/CMakeLists.txt
index 2c156b81b..9f3ace22d 100644
--- a/plugins/channelrx/CMakeLists.txt
+++ b/plugins/channelrx/CMakeLists.txt
@@ -101,7 +101,7 @@ if (ENABLE_CHANNELRX_DEMODM17 AND CODEC2_FOUND)
     add_subdirectory(demodm17)
 endif()
 
-if (ENABLE_CHANNELRX_SIGMFFILESINK AND LIBSIGMF_FOUND AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+if (ENABLE_CHANNELRX_SIGMFFILESINK AND LIBSIGMF_FOUND)
     add_subdirectory(sigmffilesink)
 endif()
 
diff --git a/plugins/samplesource/CMakeLists.txt b/plugins/samplesource/CMakeLists.txt
index 0f5f805a4..8583830cd 100644
--- a/plugins/samplesource/CMakeLists.txt
+++ b/plugins/samplesource/CMakeLists.txt
@@ -62,7 +62,7 @@ if(ENABLE_SOAPYSDR AND SOAPYSDR_FOUND)
     add_subdirectory(soapysdrinput)
 endif()
 
-if(LIBSIGMF_FOUND AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+if(LIBSIGMF_FOUND)
     add_subdirectory(sigmffileinput)
 endif()
 
diff --git a/plugins/samplesource/sigmffileinput/CMakeLists.txt b/plugins/samplesource/sigmffileinput/CMakeLists.txt
index efab2460b..826b3d149 100644
--- a/plugins/samplesource/sigmffileinput/CMakeLists.txt
+++ b/plugins/samplesource/sigmffileinput/CMakeLists.txt
@@ -19,6 +19,7 @@ set(sigmffileinput_HEADERS
 include_directories(
     ${CMAKE_SOURCE_DIR}/swagger/sdrangel/code/qt5/client
     ${LIBSIGMF_INCLUDE_DIR}
+    ${Boost_INCLUDE_DIRS}
 )
 
 if(NOT SERVER_MODE)
@@ -49,14 +50,20 @@ add_library(${TARGET_NAME} SHARED
 	${sigmffileinput_SOURCES}
 )
 
+if (NOT WIN32)
+    link_directories(${Boost_LIBRARY_DIRS})
+endif()
+
 target_link_libraries(${TARGET_NAME}
+    Boost::disable_autolinking
     Qt::Core
     ${TARGET_LIB}
 	sdrbase
 	${TARGET_LIB_GUI}
     swagger
-    ${LIBSIGMF_LIBRARIES}
 )
+target_link_libraries(${TARGET_NAME} optimized ${LIBSIGMF_LIBRARIES})
+target_link_libraries(${TARGET_NAME} debug ${LIBSIGMF_LIBRARIES_DEBUG})
 
 install(TARGETS ${TARGET_NAME} DESTINATION ${INSTALL_FOLDER})
 
diff --git a/sdrbase/CMakeLists.txt b/sdrbase/CMakeLists.txt
index eca2ad3c4..61b1e4f99 100644
--- a/sdrbase/CMakeLists.txt
+++ b/sdrbase/CMakeLists.txt
@@ -32,7 +32,7 @@ else(FFTW3F_FOUND)
     add_definitions(-DUSE_KISSFFT)
 endif(FFTW3F_FOUND)
 
-if (LIBSIGMF_FOUND AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
+if (LIBSIGMF_FOUND)
     set(sdrbase_SOURCES
         ${sdrbase_SOURCES}
         dsp/sigmffilerecord.cpp
@@ -43,7 +43,6 @@ if (LIBSIGMF_FOUND AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
         dsp/sigmffilerecord.h
     )
     include_directories(${LIBSIGMF_INCLUDE_DIR})
-    set(sdrbase_LIBSIGMF_LIB ${LIBSIGMF_LIBRARIES})
 endif()
 
 if (LIBSERIALDV_FOUND)
@@ -519,6 +518,10 @@ target_link_libraries(sdrbase
     qrtplib
     swagger
 )
+if (LIBSIGMF_FOUND)
+    target_link_libraries(sdrbase optimized ${LIBSIGMF_LIBRARIES})
+    target_link_libraries(sdrbase debug ${LIBSIGMF_LIBRARIES_DEBUG})
+endif()
 if (Qt6_FOUND)
     target_link_libraries(sdrbase
         Qt::Core5Compat