Skip to content

Commit 2b6e935

Browse files
committed
Fixed the unit test build on Windows by copying the DLLs to the unit test dir
1 parent 04e6441 commit 2b6e935

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

test/unit/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,40 @@ if(PAHO_BUILD_SHARED)
8383
endif()
8484
endif()
8585

86+
# On Windows with shared libraries, copy DLLs next to the test binary so that
87+
# it can run (both for catch_discover_tests and when running ctest directly).
88+
if(WIN32 AND PAHO_BUILD_SHARED)
89+
if(CMAKE_VERSION VERSION_GREATER_EQUAL "3.21")
90+
add_custom_command(TARGET unit_tests POST_BUILD
91+
COMMAND ${CMAKE_COMMAND} -E copy -t $<TARGET_FILE_DIR:unit_tests>
92+
$<TARGET_RUNTIME_DLLS:unit_tests>
93+
COMMAND_EXPAND_LISTS
94+
)
95+
else()
96+
# Fallback for CMake < 3.21: copy known DLL targets manually.
97+
set(_paho_dlls $<TARGET_FILE:paho-mqttpp3-shared>)
98+
if(PAHO_WITH_MQTT_C)
99+
if(PAHO_WITH_SSL)
100+
list(APPEND _paho_dlls $<TARGET_FILE:paho-mqtt3as>)
101+
else()
102+
list(APPEND _paho_dlls $<TARGET_FILE:paho-mqtt3a>)
103+
endif()
104+
else()
105+
message(WARNING
106+
"CMake 3.21+ is recommended to automatically copy all runtime "
107+
"DLL dependencies for unit tests. With CMake ${CMAKE_VERSION}, "
108+
"only the paho-mqttpp3 DLL will be copied automatically. "
109+
"Ensure the paho MQTT C DLLs are in PATH for tests to run."
110+
)
111+
endif()
112+
add_custom_command(TARGET unit_tests POST_BUILD
113+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
114+
${_paho_dlls}
115+
$<TARGET_FILE_DIR:unit_tests>
116+
)
117+
endif()
118+
endif()
119+
86120
include(CTest)
87121
include(Catch)
88122

0 commit comments

Comments
 (0)