From d1a78de79431581da1c06d81f45c1eb86f62d3e4 Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Mon, 7 Oct 2024 02:46:27 +0200 Subject: [PATCH] cmake: Rework ortools_cxx_test() --- cmake/cpp.cmake | 61 ++++++++++++++++++----------------- examples/tests/CMakeLists.txt | 11 +++++-- ortools/sat/CMakeLists.txt | 14 +++++--- 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/cmake/cpp.cmake b/cmake/cpp.cmake index 4e86448b10..d586dbcdea 100644 --- a/cmake/cpp.cmake +++ b/cmake/cpp.cmake @@ -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// +# 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// - 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() ################## diff --git a/examples/tests/CMakeLists.txt b/examples/tests/CMakeLists.txt index 5d49f347e9..9ba53952a1 100644 --- a/examples/tests/CMakeLists.txt +++ b/examples/tests/CMakeLists.txt @@ -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() diff --git a/ortools/sat/CMakeLists.txt b/ortools/sat/CMakeLists.txt index c2a1d46fa2..d8e6a5d58f 100644 --- a/ortools/sat/CMakeLists.txt +++ b/ortools/sat/CMakeLists.txt @@ -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