diff --git a/CMakeLists.txt b/CMakeLists.txt index b50c631d43..23ff36d929 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,11 +63,11 @@ if(UNIX) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) # for multi-config build system (e.g. Xcode, Ninja Multi-Config) - foreach(OUTPUTCONFIG IN LISTS CMAKE_CONFIGURATION_TYPES) - string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_LIBDIR}) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_LIBDIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_BINDIR}) + foreach(OutputConfig IN LISTS CMAKE_CONFIGURATION_TYPES) + string(TOUPPER ${OutputConfig} OUTPUTCONFIG) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_LIBDIR}) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_LIBDIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR}) endforeach() else() # Currently Only support static build for windows @@ -76,11 +76,11 @@ else() set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}) # for multi-config builds (e.g. msvc) - foreach(OUTPUTCONFIG IN LISTS CMAKE_CONFIGURATION_TYPES) - string(TOUPPER ${OUTPUTCONFIG} OUTPUTCONFIG) - set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_BINDIR}) - set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_BINDIR}) - set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OUTPUTCONFIG}/${CMAKE_INSTALL_BINDIR}) + foreach(OutputConfig IN LISTS CMAKE_CONFIGURATION_TYPES) + string(TOUPPER ${OutputConfig} OUTPUTCONFIG) + set(CMAKE_LIBRARY_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR}) + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR}) + set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${OUTPUTCONFIG} ${CMAKE_BINARY_DIR}/${OutputConfig}/${CMAKE_INSTALL_BINDIR}) endforeach() endif() diff --git a/cmake/cpp.cmake b/cmake/cpp.cmake index 6313c9c273..d8ddffa292 100644 --- a/cmake/cpp.cmake +++ b/cmake/cpp.cmake @@ -141,6 +141,78 @@ if(MSVC) ) endif() +################ +## C++ Test ## +################ +# 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// +# e.g.: +# ortools_cxx_test( +# FILE_NAME +# ${PROJECT_SOURCE_DIR}/ortools/foo/foo_test.cc +# COMPONENT_NAME +# foo +# DEPS +# GTest::gmock +# GTest::gtest_main +# ) +function(ortools_cxx_test) + set(options "") + set(oneValueArgs "FILE_NAME;COMPONENT_NAME") + set(multiValueArgs "DEPS") + cmake_parse_arguments(TEST + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ${ARGN} + ) +if(NOT TEST_FILE_NAME) + message(FATAL_ERROR "no FILE_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}) + endif() + + add_executable(${TEST_NAME} ${TEST_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 + ${TEST_DEPS} + ) + + include(GNUInstallDirs) + if(APPLE) + 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}") + endif() + + if(BUILD_TESTING) + add_test( + NAME cxx_${COMPONENT_NAME}_${TEST_NAME} + COMMAND ${TEST_NAME}) + endif() + message(STATUS "Configuring test ${TEST_FILE_NAME} ...DONE") +endfunction() + ################## ## PROTO FILE ## ################## @@ -537,72 +609,6 @@ install(DIRECTORY ortools/constraint_solver/docs/ PATTERN "*.md") endif() -################ -## C++ Test ## -################ -# add_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// -# e.g.: -# add_cxx_test( -# FILE_NAME -# ${PROJECT_SOURCE_DIR}/ortools/foo/foo_test.cc -# COMPONENT_NAME -# foo -# ) -function(add_cxx_test) - set(options "") - set(oneValueArgs FILE_NAME COMPONENT_NAME) - set(multiValueArgs "") - cmake_parse_arguments(TEST - "${options}" - "${oneValueArgs}" - "${multiValueArgs}" - ${ARGN} - ) -if(NOT TEST_FILE_NAME) - message(FATAL_ERROR "no FILE_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}) - endif() - - add_executable(${TEST_NAME} ${TEST_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_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}") - endif() - - if(BUILD_TESTING) - add_test( - NAME cxx_${COMPONENT_NAME}_${TEST_NAME} - COMMAND ${TEST_NAME}) - endif() - message(STATUS "Configuring test ${TEST_FILE_NAME} ...DONE") -endfunction() - ################## ## C++ Sample ## ################## diff --git a/cmake/dependencies/CMakeLists.txt b/cmake/dependencies/CMakeLists.txt index b6ee347c95..7790c073ae 100644 --- a/cmake/dependencies/CMakeLists.txt +++ b/cmake/dependencies/CMakeLists.txt @@ -108,9 +108,9 @@ if(BUILD_Protobuf) Protobuf GIT_REPOSITORY "https://github.com/protocolbuffers/protobuf.git" GIT_TAG "v26.1" - PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/../../patches/protobuf-v26.1.patch" GIT_SHALLOW TRUE GIT_SUBMODULES "" + PATCH_COMMAND git apply --ignore-whitespace "${CMAKE_CURRENT_LIST_DIR}/../../patches/protobuf-v26.1.patch" ) FetchContent_MakeAvailable(Protobuf) list(POP_BACK CMAKE_MESSAGE_INDENT) diff --git a/cmake/java.cmake b/cmake/java.cmake index c123541ba8..02a4414031 100644 --- a/cmake/java.cmake +++ b/cmake/java.cmake @@ -285,6 +285,8 @@ add_custom_command( COMMAND ${CMAKE_COMMAND} -E touch ${JAVA_NATIVE_PROJECT_DIR}/timestamp DEPENDS ${JAVA_NATIVE_PROJECT_DIR}/pom.xml + Java${PROJECT_NAME}_proto + jni${JAVA_ARTIFACT} BYPRODUCTS ${JAVA_NATIVE_PROJECT_DIR}/target COMMENT "Generate Java native package ${JAVA_NATIVE_PROJECT} (${JAVA_NATIVE_PROJECT_DIR}/timestamp)"