diff --git a/cmake/cpp.cmake b/cmake/cpp.cmake index 8b39735dbe..0de02f651b 100644 --- a/cmake/cpp.cmake +++ b/cmake/cpp.cmake @@ -505,82 +505,9 @@ install(DIRECTORY ortools/constraint_solver/docs/ PATTERN "*.md") endif() -############################ -## Samples/Examples/Tests ## -############################ -# add_cxx_sample() -# CMake function to generate and build C++ sample. -# Parameters: -# the C++ filename -# e.g.: -# add_cxx_sample(foo.cc) -function(add_cxx_sample FILE_NAME) - message(STATUS "Configuring sample ${FILE_NAME}: ...") - get_filename_component(SAMPLE_NAME ${FILE_NAME} NAME_WE) - get_filename_component(SAMPLE_DIR ${FILE_NAME} DIRECTORY) - get_filename_component(COMPONENT_DIR ${SAMPLE_DIR} DIRECTORY) - get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME) - - add_executable(${SAMPLE_NAME} ${FILE_NAME}) - target_include_directories(${SAMPLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - target_compile_features(${SAMPLE_NAME} PRIVATE cxx_std_17) - target_link_libraries(${SAMPLE_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools) - - include(GNUInstallDirs) - if(APPLE) - set_target_properties(${SAMPLE_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(${SAMPLE_NAME} PROPERTIES - INSTALL_RPATH "$ORIGIN/${libdir_relative_path}") - endif() - install(TARGETS ${SAMPLE_NAME}) - - if(BUILD_TESTING) - add_test(NAME cxx_${COMPONENT_NAME}_${SAMPLE_NAME} COMMAND ${SAMPLE_NAME}) - endif() - message(STATUS "Configuring sample ${FILE_NAME}: ...DONE") -endfunction() - -# add_cxx_example() -# CMake function to generate and build C++ example. -# Parameters: -# the C++ filename -# e.g.: -# add_cxx_example(foo.cc) -function(add_cxx_example FILE_NAME) - message(STATUS "Configuring example ${FILE_NAME}: ...") - get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE) - get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY) - get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME) - - add_executable(${EXAMPLE_NAME} ${FILE_NAME}) - target_include_directories(${EXAMPLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - target_compile_features(${EXAMPLE_NAME} PRIVATE cxx_std_17) - target_link_libraries(${EXAMPLE_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools) - - include(GNUInstallDirs) - if(APPLE) - set_target_properties(${EXAMPLE_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(${EXAMPLE_NAME} PROPERTIES - INSTALL_RPATH "$ORIGIN/${libdir_relative_path}") - endif() - install(TARGETS ${EXAMPLE_NAME}) - - if(BUILD_TESTING) - add_test(NAME cxx_${COMPONENT_NAME}_${EXAMPLE_NAME} COMMAND ${EXAMPLE_NAME}) - endif() - message(STATUS "Configuring example ${FILE_NAME}: ...DONE") -endfunction() - +################ +## C++ Test ## +################ # add_cxx_test() # CMake function to generate and build C++ test. # Parameters: @@ -617,3 +544,108 @@ function(add_cxx_test FILE_NAME) endif() message(STATUS "Configuring test ${FILE_NAME}: ...DONE") endfunction() + +################## +## C++ Sample ## +################## +# add_cxx_sample() +# CMake function to generate and build C++ sample. +# 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//samples/ +# e.g.: +# add_cxx_sample( +# FILE_NAME +# ${PROJECT_SOURCE_DIR}/ortools/foo/sample/bar.cc +# COMPONENT_NAME +# foo +# ) +function(add_cxx_sample) + set(options "") + set(oneValueArgs FILE_NAME COMPONENT_NAME) + set(multiValueArgs "") + cmake_parse_arguments(SAMPLE + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) + if(NOT SAMPLE_FILE_NAME) + message(FATAL_ERROR "no FILE_NAME provided") + endif() + get_filename_component(SAMPLE_NAME ${SAMPLE_FILE_NAME} NAME_WE) + + message(STATUS "Configuring sample ${SAMPLE_FILE_NAME} ...") + + if(NOT SAMPLE_COMPONENT_NAME) + # sample is located in ortools//sample/ + get_filename_component(SAMPLE_DIR ${SAMPLE_FILE_NAME} DIRECTORY) + get_filename_component(COMPONENT_DIR ${SAMPLE_DIR} DIRECTORY) + get_filename_component(SAMPLE_COMPONENT_NAME ${COMPONENT_DIR} NAME) + endif() + + add_executable(${SAMPLE_NAME} ${SAMPLE_FILE_NAME}) + target_include_directories(${SAMPLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + target_compile_features(${SAMPLE_NAME} PRIVATE cxx_std_17) + target_link_libraries(${SAMPLE_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools) + + include(GNUInstallDirs) + if(APPLE) + set_target_properties(${SAMPLE_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(${SAMPLE_NAME} PROPERTIES + INSTALL_RPATH "$ORIGIN/${libdir_relative_path}") + endif() + install(TARGETS ${SAMPLE_NAME}) + + if(BUILD_TESTING) + add_test( + NAME cxx_${SAMPLE_COMPONENT_NAME}_${SAMPLE_NAME} + COMMAND ${SAMPLE_NAME}) + endif() + message(STATUS "Configuring sample ${SAMPLE_FILE_NAME} ...DONE") +endfunction() + +################### +## C++ Example ## +################### +# add_cxx_example() +# CMake function to generate and build C++ example. +# Parameters: +# the C++ filename +# e.g.: +# add_cxx_example(foo.cc) +function(add_cxx_example FILE_NAME) + message(STATUS "Configuring example ${FILE_NAME}: ...") + get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE) + get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY) + get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME) + + add_executable(${EXAMPLE_NAME} ${FILE_NAME}) + target_include_directories(${EXAMPLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + target_compile_features(${EXAMPLE_NAME} PRIVATE cxx_std_17) + target_link_libraries(${EXAMPLE_NAME} PRIVATE ${PROJECT_NAMESPACE}::ortools) + + include(GNUInstallDirs) + if(APPLE) + set_target_properties(${EXAMPLE_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(${EXAMPLE_NAME} PROPERTIES + INSTALL_RPATH "$ORIGIN/${libdir_relative_path}") + endif() + install(TARGETS ${EXAMPLE_NAME}) + + if(BUILD_TESTING) + add_test(NAME cxx_${COMPONENT_NAME}_${EXAMPLE_NAME} COMMAND ${EXAMPLE_NAME}) + endif() + message(STATUS "Configuring example ${FILE_NAME}: ...DONE") +endfunction() diff --git a/ortools/algorithms/samples/CMakeLists.txt b/ortools/algorithms/samples/CMakeLists.txt index 7661012ddd..d9d3aeb26c 100644 --- a/ortools/algorithms/samples/CMakeLists.txt +++ b/ortools/algorithms/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/constraint_solver/samples/CMakeLists.txt b/ortools/constraint_solver/samples/CMakeLists.txt index 7661012ddd..d9d3aeb26c 100644 --- a/ortools/constraint_solver/samples/CMakeLists.txt +++ b/ortools/constraint_solver/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/glop/samples/CMakeLists.txt b/ortools/glop/samples/CMakeLists.txt index 5d56ffaa94..9dda24c42d 100644 --- a/ortools/glop/samples/CMakeLists.txt +++ b/ortools/glop/samples/CMakeLists.txt @@ -39,6 +39,6 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/graph/samples/CMakeLists.txt b/ortools/graph/samples/CMakeLists.txt index 7661012ddd..d9d3aeb26c 100644 --- a/ortools/graph/samples/CMakeLists.txt +++ b/ortools/graph/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/linear_solver/samples/CMakeLists.txt b/ortools/linear_solver/samples/CMakeLists.txt index 7661012ddd..d9d3aeb26c 100644 --- a/ortools/linear_solver/samples/CMakeLists.txt +++ b/ortools/linear_solver/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/math_opt/samples/cpp/CMakeLists.txt b/ortools/math_opt/samples/cpp/CMakeLists.txt index aad89a9e62..d2e7634270 100644 --- a/ortools/math_opt/samples/cpp/CMakeLists.txt +++ b/ortools/math_opt/samples/cpp/CMakeLists.txt @@ -28,6 +28,8 @@ if(BUILD_CXX_SAMPLES) foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample( + FILE_NAME ${SAMPLE} + COMPONENT_NAME math_opt) endforeach() endif() diff --git a/ortools/pdlp/samples/CMakeLists.txt b/ortools/pdlp/samples/CMakeLists.txt index 0813022dcf..ff66fea4c0 100644 --- a/ortools/pdlp/samples/CMakeLists.txt +++ b/ortools/pdlp/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif() diff --git a/ortools/sat/samples/CMakeLists.txt b/ortools/sat/samples/CMakeLists.txt index 7661012ddd..d9d3aeb26c 100644 --- a/ortools/sat/samples/CMakeLists.txt +++ b/ortools/sat/samples/CMakeLists.txt @@ -18,7 +18,7 @@ endif() if(BUILD_CXX_SAMPLES) file(GLOB CXX_SRCS "*.cc") foreach(SAMPLE IN LISTS CXX_SRCS) - add_cxx_sample(${SAMPLE}) + add_cxx_sample(FILE_NAME ${SAMPLE}) endforeach() endif()