Skip to content

Commit

Permalink
cmake: Rework add_cxx_test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Nov 6, 2023
1 parent 95a6f84 commit a33bf9a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cmake/cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -590,25 +590,32 @@ endfunction()
# Parameters:
# the C++ filename
# e.g.:
# add_cxx_test(foo.cc)
# add_cxx_test(foo_test.cc)
function(add_cxx_test FILE_NAME)
message(STATUS "Configuring test ${FILE_NAME}: ...")
get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)

add_executable(${TEST_NAME} ${FILE_NAME})
target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(${TEST_NAME} PRIVATE cxx_std_17)
target_link_libraries(${TEST_NAME} PRIVATE
${PROJECT_NAMESPACE}::ortools
)

include(GNUInstallDirs)
if(APPLE)
set(CMAKE_INSTALL_RPATH
set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH
"@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
elseif(UNIX)
set(CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$ORIGIN/../lib64:$ORIGIN/../lib:$ORIGIN")
cmake_path(RELATIVE_PATH CMAKE_INSTALL_FULL_LIBDIR
BASE_DIRECTORY ${CMAKE_INSTALL_FULL_BINDIR}
OUTPUT_VARIABLE libdir_relative_path)
set_target_properties(${TEST_NAME} PROPERTIES
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}")
endif()

add_executable(${TEST_NAME} ${FILE_NAME})
target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_features(${TEST_NAME} PRIVATE cxx_std_17)
target_link_libraries(${TEST_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools)

if(BUILD_TESTING)
add_test(NAME cxx_${COMPONENT_NAME}_${TEST_NAME} COMMAND ${TEST_NAME})
endif()
Expand Down

0 comments on commit a33bf9a

Please sign in to comment.