Skip to content

Commit

Permalink
GHI #13 Fix Windows PATH issues for tests
Browse files Browse the repository at this point in the history
Signed-off-by: doodspav <[email protected]>
  • Loading branch information
doodspav committed Mar 18, 2023
1 parent 233baaa commit 59f6e93
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ project(
LANGUAGES CXX
)

include(cmake/OptionVariables.cmake)


# ---- Dependencies ----

Expand All @@ -17,6 +19,8 @@ if(PROJECT_IS_TOP_LEVEL)
enable_testing()
endif()

# for Windows: prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
find_package(GTest REQUIRED)
include(GoogleTest)

Expand Down Expand Up @@ -45,3 +49,33 @@ gtest_add_tests(
TARGET patomic_test
TEST_LIST patomic_test_TESTS
)


# ---- Windows Path Issues ----

include(cmake/WindowsDependenciesPath.cmake)
windows_deps_path(
deps_path
patomic::patomic
GTest::gtest_main
)

# check we're on Windows, and we have SHARED_LIBRARY dependencies
if(NOT deps_path STREQUAL "" AND CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")

# set environment variable for each test ctest works automatically
if(patomic_test_SET_CTEST_PATH_ENV_WINDOWS)
foreach(test IN LISTS patomic_test_TESTS)
set_property(
TEST "${test}"
PROPERTY ENVIRONMENT "PATH=${deps_path}"
)
endforeach()
endif()

# create file with PATH contents for when not running tests through CMake
file(GENERATE
OUTPUT "${PROJECT_BINARY_DIR}/windows_dependencies_path.txt"
CONTENT "${deps_path}"
)
endif()
13 changes: 13 additions & 0 deletions test/cmake/OptionVariables.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ---- Windows Tests Path ----

# By default we set PATH for tests run with CTest on Windows in order to prevent
# linker errors.
# Due to limitations in CMake, we can only completely override the PATH, rather
# than prepend to it.
# This gives users the option to disable this behaviour.
option(
patomic_test_SET_CTEST_PATH_ENV_WINDOWS
"Set PATH environment variable for tests when run using CTest on Windows"
ON
)
mark_as_advanced(patomic_test_SET_CTEST_PATH_ENV_WINDOWS)
30 changes: 30 additions & 0 deletions test/cmake/WindowsDependenciesPath.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# ---- Windows Path Prefix ----

# Windows doesn't support rpath, so when linking dynamically the libraries need
# to either be in the same directory or on PATH.
# This function sets a variable to a GENERATOR string that may be prepended to
# path in order to find linked dependencies
# See: https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order
# Usage: windows_deps_path(<variable> <link-target>...)
function(windows_deps_path VAR)
if(NOT CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
set(${VAR} "" PARENT_SCOPE)
return()
endif()

set(path "")
set(glue "")
foreach(target IN LISTS ARGN)
get_target_property(type "${target}" TYPE)
if(type STREQUAL "SHARED_LIBRARY")
set(path "${path}${glue}$<TARGET_FILE_DIR:${target}>")
set(glue "\;") # backslash is important
endif()
endforeach()

if(NOT path STREQUAL "")
set(${VAR} "${path}" PARENT_SCOPE)
else()
set(${VAR} "" PARENT_SCOPE)
endif()
endfunction()

0 comments on commit 59f6e93

Please sign in to comment.