Skip to content

Commit

Permalink
cmake: Rework ortools_cxx_test()
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Oct 7, 2024
1 parent d5d6520 commit d1a78de
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
61 changes: 32 additions & 29 deletions cmake/cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -147,70 +147,73 @@ endif()
# ortools_cxx_test()
# CMake function to generate and build C++ test.
# Parameters:
# FILE_NAME: the C++ filename
# COMPONENT_NAME: name of the ortools/ subdir where the test is located
# note: automatically determined if located in ortools/<component>/
# NAME: CMake target name
# SOURCES: List of source files
# [COMPILE_DEFINITIONS]: List of private compile definitions
# [COMPILE_OPTIONS]: List of private compile options
# [LINK_LIBRARIES]: List of private libraries to use when linking
# note: ortools::ortools is always linked to the target
# [LINK_OPTIONS]: List of private link options
# e.g.:
# ortools_cxx_test(
# FILE_NAME
# ${PROJECT_SOURCE_DIR}/ortools/foo/foo_test.cc
# COMPONENT_NAME
# foo
# DEPS
# NAME
# foo_bar_test
# SOURCES
# bar_test.cc
# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_test.cc
# LINK_LIBRARIES
# GTest::gmock
# GTest::gtest_main
# )
function(ortools_cxx_test)
set(options "")
set(oneValueArgs "FILE_NAME;COMPONENT_NAME")
set(multiValueArgs "DEPS")
set(oneValueArgs "NAME")
set(multiValueArgs
"SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS")
cmake_parse_arguments(TEST
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(NOT TEST_FILE_NAME)
message(FATAL_ERROR "no FILE_NAME provided")
if(NOT TEST_NAME)
message(FATAL_ERROR "no NAME provided")
endif()
get_filename_component(TEST_NAME ${TEST_FILE_NAME} NAME_WE)

message(STATUS "Configuring test ${TEST_FILE_NAME} ...")

if(NOT TEST_COMPONENT_NAME)
# test is located in ortools/<component_name>/
get_filename_component(COMPONENT_DIR ${TEST_FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)
else()
set(COMPONENT_NAME ${TEST_COMPONENT_NAME})
if(NOT TEST_SOURCES)
message(FATAL_ERROR "no SOURCES provided")
endif()
message(STATUS "Configuring test ${TEST_NAME} ...")

add_executable(${TEST_NAME} ${TEST_FILE_NAME})
add_executable(${TEST_NAME} "")
target_sources(${TEST_NAME} PRIVATE ${TEST_SOURCES})
target_include_directories(${TEST_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(${TEST_NAME} PRIVATE ${TEST_COMPILE_DEFINITIONS})
target_compile_features(${TEST_NAME} PRIVATE cxx_std_17)
target_compile_options(${TEST_NAME} PRIVATE ${TEST_COMPILE_OPTIONS})
target_link_libraries(${TEST_NAME} PRIVATE
${PROJECT_NAMESPACE}::ortools
${TEST_DEPS}
${TEST_LINK_LIBRARIES}
)
target_link_options(${TEST_NAME} PRIVATE ${TEST_LINK_OPTIONS})

include(GNUInstallDirs)
if(APPLE)
set_target_properties(${TEST_NAME} PROPERTIES INSTALL_RPATH
"@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
set_target_properties(${TEST_NAME} PROPERTIES
INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR};@loader_path")
elseif(UNIX)
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}")
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN")
endif()

if(BUILD_TESTING)
add_test(
NAME cxx_${COMPONENT_NAME}_${TEST_NAME}
NAME cxx_${TEST_NAME}
COMMAND ${TEST_NAME})
endif()
message(STATUS "Configuring test ${TEST_FILE_NAME} ...DONE")
message(STATUS "Configuring test ${TEST_NAME} ...DONE")
endfunction()

##################
Expand Down
11 changes: 9 additions & 2 deletions examples/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@ endif()

if(BUILD_CXX_EXAMPLES)
file(GLOB CXX_SRCS "*.cc")
foreach(FILE_NAME IN LISTS CXX_SRCS)
ortools_cxx_test(FILE_NAME ${FILE_NAME})
foreach(_FULL_FILE_NAME IN LISTS CXX_SRCS)
get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE)
get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME)
ortools_cxx_test(
NAME
tests_${_NAME}
SOURCES
${_FULL_FILE_NAME}
)
endforeach()
endif()

Expand Down
14 changes: 10 additions & 4 deletions ortools/sat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@ target_link_libraries(${NAME} PRIVATE

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
foreach(FILE_NAME IN LISTS _TEST_SRCS)
foreach(_FULL_FILE_NAME IN LISTS _TEST_SRCS)
get_filename_component(_NAME ${_FULL_FILE_NAME} NAME_WE)
get_filename_component(_FILE_NAME ${_FULL_FILE_NAME} NAME)
message(WARNING "name: ${_NAME}")
message(WARNING "filename: ${_FILE_NAME}")
ortools_cxx_test(
FILE_NAME
${FILE_NAME}
DEPS
NAME
sat_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
benchmark::benchmark
GTest::gmock
GTest::gtest_main
Expand Down

0 comments on commit d1a78de

Please sign in to comment.