Skip to content

Commit

Permalink
cmake: Add MathOpt tests to build (#4402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Oct 7, 2024
1 parent 391c782 commit f38eda1
Show file tree
Hide file tree
Showing 9 changed files with 548 additions and 0 deletions.
84 changes: 84 additions & 0 deletions cmake/cpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,90 @@ function(ortools_cxx_test)
message(STATUS "Configuring test ${TEST_NAME} ...DONE")
endfunction()

###################
## C++ Library ##
###################
# ortools_cxx_library()
# CMake function to generate and build C++ library.
# Parameters:
# NAME: CMake target name
# SOURCES: List of source files
# [TYPE]: SHARED or STATIC
# [COMPILE_DEFINITIONS]: List of private compile definitions
# [COMPILE_OPTIONS]: List of private compile options
# [LINK_LIBRARIES]: List of **public** 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_library(
# NAME
# foo_bar_library
# SOURCES
# bar_library.cc
# ${PROJECT_SOURCE_DIR}/ortools/foo/bar_library.cc
# TYPE
# SHARED
# LINK_LIBRARIES
# GTest::gmock
# GTest::gtest_main
# TESTING
# )
function(ortools_cxx_library)
set(options "TESTING")
set(oneValueArgs "NAME;TYPE")
set(multiValueArgs
"SOURCES;COMPILE_DEFINITIONS;COMPILE_OPTIONS;LINK_LIBRARIES;LINK_OPTIONS")
cmake_parse_arguments(LIBRARY
"${options}"
"${oneValueArgs}"
"${multiValueArgs}"
${ARGN}
)
if(LIBRARY_TESTING AND NOT BUILD_TESTING)
return()
endif()

if(NOT LIBRARY_NAME)
message(FATAL_ERROR "no NAME provided")
endif()
if(NOT LIBRARY_SOURCES)
message(FATAL_ERROR "no SOURCES provided")
endif()
message(STATUS "Configuring library ${LIBRARY_NAME} ...")

add_library(${LIBRARY_NAME} ${LIBRARY_TYPE} "")
target_sources(${LIBRARY_NAME} PRIVATE ${LIBRARY_SOURCES})
target_include_directories(${LIBRARY_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_DEFINITIONS})
target_compile_features(${LIBRARY_NAME} PRIVATE cxx_std_17)
target_compile_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_COMPILE_OPTIONS})
target_link_libraries(${LIBRARY_NAME} PUBLIC
${PROJECT_NAMESPACE}::ortools
${LIBRARY_LINK_LIBRARIES}
)
target_link_options(${LIBRARY_NAME} PRIVATE ${LIBRARY_LINK_OPTIONS})

include(GNUInstallDirs)
if(APPLE)
set_target_properties(${LIBRARY_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(${LIBRARY_NAME} PROPERTIES
INSTALL_RPATH "$ORIGIN/${libdir_relative_path}:$ORIGIN")
endif()
add_library(${PROJECT_NAMESPACE}::${LIBRARY_NAME} ALIAS ${LIBRARY_NAME})

if(BUILD_TESTING)
add_test(
NAME cxx_${LIBRARY_NAME}
COMMAND ${LIBRARY_NAME})
endif()
message(STATUS "Configuring library ${LIBRARY_NAME} ...DONE")
endfunction()

##################
## PROTO FILE ##
##################
Expand Down
1 change: 1 addition & 0 deletions ortools/math_opt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ add_subdirectory(constraints)
add_subdirectory(cpp)
add_subdirectory(io)
add_subdirectory(labs)
add_subdirectory(solver_tests)
add_subdirectory(solvers)
add_subdirectory(storage)
add_subdirectory(validators)
Expand Down
18 changes: 18 additions & 0 deletions ortools/math_opt/constraints/indicator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,21 @@ target_link_libraries(${NAME} PRIVATE
absl::strings
${PROJECT_NAMESPACE}::math_opt_proto)
#install(TARGETS ${NAME} EXPORT ${PROJECT_NAME}Targets)

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
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)
ortools_cxx_test(
NAME
math_opt_constraints_indicator_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
#benchmark::benchmark
GTest::gmock
GTest::gtest_main
)
endforeach()
endif()
18 changes: 18 additions & 0 deletions ortools/math_opt/constraints/quadratic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC
target_link_libraries(${NAME} PRIVATE
absl::strings
${PROJECT_NAMESPACE}::math_opt_proto)

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
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)
ortools_cxx_test(
NAME
math_opt_constraints_quadratic_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
#benchmark::benchmark
GTest::gmock
GTest::gtest_main
)
endforeach()
endif()
18 changes: 18 additions & 0 deletions ortools/math_opt/constraints/second_order_cone/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC
target_link_libraries(${NAME} PRIVATE
absl::strings
${PROJECT_NAMESPACE}::math_opt_proto)

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
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)
ortools_cxx_test(
NAME
math_opt_constraints_second_order_cone_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
#benchmark::benchmark
GTest::gmock
GTest::gtest_main
)
endforeach()
endif()
18 changes: 18 additions & 0 deletions ortools/math_opt/constraints/sos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC
target_link_libraries(${NAME} PRIVATE
absl::strings
${PROJECT_NAMESPACE}::math_opt_proto)

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
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)
ortools_cxx_test(
NAME
math_opt_constraints_sos_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
#benchmark::benchmark
GTest::gmock
GTest::gtest_main
)
endforeach()
endif()
18 changes: 18 additions & 0 deletions ortools/math_opt/constraints/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,21 @@ target_include_directories(${NAME} PUBLIC
target_link_libraries(${NAME} PRIVATE
absl::strings
${PROJECT_NAMESPACE}::math_opt_proto)

if(BUILD_TESTING)
file(GLOB _TEST_SRCS "*_test.cc")
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)
ortools_cxx_test(
NAME
math_opt_constraints_util_${_NAME}
SOURCES
${_FILE_NAME}
LINK_LIBRARIES
#benchmark::benchmark
GTest::gmock
GTest::gtest_main
)
endforeach()
endif()
Loading

0 comments on commit f38eda1

Please sign in to comment.