From a4b0f85d491f5331cd9197327c8cd6bdbf2a5a1e Mon Sep 17 00:00:00 2001 From: Alison Felizzi Date: Mon, 24 Jan 2022 02:44:54 +0000 Subject: [PATCH] Support linking tests with relative RPATH This change adds support for linking our test executables with a relative RPATH to the WiredTiger library, via a new configuration option 'LINK_TESTS_WITH_RELATIVE_RPATH'. This being supported on Linux and Darwin platforms. Generally we expect to execute the test binaries under the CMake build path and don't intend to install & distribute our test under other paths. Hence it should be OK to encode a relative RPATH for our test executables. --- test/CMakeLists.txt | 11 +++++++++++ test/ctest_helpers.cmake | 23 +++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ce81f1488..0dd4119f0 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,6 +2,17 @@ project(test) include(ctest_helpers.cmake) +# Generally its appropriate to use this configuration when we expect to execute +# the test binaries under the CMake build path. This should usually be the case +# as we don't intend to install & distribute our test binaries. +config_bool( + LINK_TESTS_WITH_RELATIVE_RPATH + "Link our WiredTiger test executables with a relative runpath \ + to the WiredTiger library." + DEFAULT OFF + DEPENDS "WT_LINUX OR WT_DARWIN" +) + if(WT_WIN) # Compile the windows shim library. add_subdirectory(windows) diff --git a/test/ctest_helpers.cmake b/test/ctest_helpers.cmake index 23b00f930..0d5860f60 100644 --- a/test/ctest_helpers.cmake +++ b/test/ctest_helpers.cmake @@ -77,6 +77,29 @@ function(create_test_executable target) target_link_libraries(${target} ${CREATE_TEST_LIBS}) endif() + if(LINK_TESTS_WITH_RELATIVE_RPATH) + # If enabled, additionally link the final test executable with a + # relative runpath to the top-level build directory. This being the + # build location of the WiredTiger library. + set(origin_linker_variable) + if(WT_LINUX) + set(origin_linker_variable "\\$ORIGIN") + elseif(WT_DARWIN) + set(origin_linker_variable "@loader_path") + else() + # Sanity check. We generally don't expect to enter this path, the + # 'LINK_TESTS_WITH_RELATIVE_RPATH' configuration should be bound to only + # WT_LINUX and WT_DARWIN platforms. + message(FATAL_ERROR "Unable to use 'LINK_TESTS_WITH_RELATIVE_RPATH': Unknown platform") + endif() + file(RELATIVE_PATH relative_rpath ${test_binary_dir} ${CMAKE_BINARY_DIR}) + set_target_properties(${target} + PROPERTIES + BUILD_WITH_INSTALL_RPATH FALSE + LINK_FLAGS "-Wl,-rpath,${origin_linker_variable}/${relative_rpath}" + ) + endif() + # If compiling for windows, additionally link in the shim library. if(WT_WIN) target_include_directories( -- 2.17.1