Skip to content

Commit

Permalink
Refs #18687. Fix execution of tests on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: Ricardo González Moreno <[email protected]>
  • Loading branch information
richiware committed Jul 4, 2023
1 parent 6d2486f commit ce57d89
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
15 changes: 15 additions & 0 deletions cmake/testing/GoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
include(GoogleTest)

function(gtest_discover_tests TARGET)
if (WIN32)
add_custom_command(
TARGET ${TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E echo "set(_target_runtime_dlls $<TARGET_RUNTIME_DLLS:${TARGET}>)" > "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_runtime_dlls_$<CONFIG>.cmake"
COMMAND ${CMAKE_COMMAND} -DTARGET=${TARGET} -DCONFIG=$<CONFIG> -DFILE_TO_PROCESS="${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_runtime_dlls_$<CONFIG>.cmake" -P ${CMAKE_SOURCE_DIR}/cmake/testing/generate_google_test_win_wrapper.cmake
COMMAND_EXPAND_LISTS
)

set(CMAKE_COMMAND "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_win_wrapper_$<CONFIG>.bat")
endif()
_gtest_discover_tests(${TARGET} ${ARGN})
endfunction()
28 changes: 28 additions & 0 deletions cmake/testing/generate_google_test_win_wrapper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
if (NOT DEFINED TARGET)
message(FATAL_ERROR "This scrips needs TARGET variable set")
endif()
if (NOT DEFINED CONFIG)
message(FATAL_ERROR "This scrips needs CONFIG variable set")
endif()
if (NOT DEFINED FILE_TO_PROCESS)
message(FATAL_ERROR "This scrips needs FILE_TO_PROCESS variable set")
endif()

include(${FILE_TO_PROCESS})

set(_path "")
foreach(_runtime_dll ${_target_runtime_dlls})
cmake_path(GET _runtime_dll PARENT_PATH _runtime_dll_path)
cmake_path(NATIVE_PATH _runtime_dll_path _runtime_dll_path_native)
list(APPEND _path "${_runtime_dll_path_native}")
endforeach()

list(REMOVE_DUPLICATES _path)

cmake_path(NATIVE_PATH CMAKE_COMMAND _cmake_command)
file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_win_wrapper_${CONFIG}.bat" "
@ECHO OFF
set \"PATH=${_path};%PATH%\"
\"${_cmake_command}\" %*
")

2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

find_package(GTest CONFIG REQUIRED)
include(GoogleTest)
include(${PROJECT_SOURCE_DIR}/cmake/testing/GoogleTest.cmake)

###############################################################################
# Unit tests
Expand Down

0 comments on commit ce57d89

Please sign in to comment.