diff --git a/CMakeLists.txt b/CMakeLists.txt index 366ad6e584..48ba1b8602 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") include(utils) set_version(VERSION) -project(ortools VERSION ${VERSION} LANGUAGES CXX) +project(ortools VERSION ${VERSION} LANGUAGES CXX C) set(PROJECT_NAMESPACE ortools) message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}") #message(STATUS "major: ${PROJECT_VERSION_MAJOR}") @@ -136,6 +136,14 @@ if(USE_SCIP) message(STATUS "Build SCIP: ${BUILD_SCIP}") endif() +CMAKE_DEPENDENT_OPTION(USE_GLPK "Use the GLPK solver" ON "BUILD_CXX" OFF) +message(STATUS "GLPK support: ${USE_GLPK}") +if(USE_GLPK) + CMAKE_DEPENDENT_OPTION(BUILD_GLPK "Build the GLPK dependency Library" OFF + "NOT BUILD_DEPS" ON) + message(STATUS "Build GLPK: ${BUILD_GLPK}") +endif() + CMAKE_DEPENDENT_OPTION(USE_COINOR "Use the COIN-OR solver" ON "BUILD_CXX" OFF) message(STATUS "COIN-OR support: ${USE_COINOR}") if(USE_COINOR) diff --git a/cmake/FindGLPK.cmake b/cmake/FindGLPK.cmake new file mode 100644 index 0000000000..12b4ec160b --- /dev/null +++ b/cmake/FindGLPK.cmake @@ -0,0 +1,85 @@ +#[=======================================================================[.rst: +FindGLPK +-------- + +This module determines the GLPK library of the system. + +IMPORTED Targets +^^^^^^^^^^^^^^^^ + +This module defines :prop_tgt:`IMPORTED` target ``GLPK::GLPK``, if +GLPK has been found. + +Result Variables +^^^^^^^^^^^^^^^^ + +This module defines the following variables: + +:: + +GLPK_FOUND - True if GLPK found. + +Hints +^^^^^ + +A user may set ``GLPK_ROOT`` to a GLPK installation root to tell this +module where to look. +#]=======================================================================] +# first specifically look for the CMake version of GLPK +find_package(GLPK QUIET NO_MODULE) +# if we found the GLPK cmake package then we are done. +if(GLPK_FOUND) + return() +endif() + +set(GLPK_FOUND FALSE) +if(CMAKE_C_COMPILER_LOADED) + include (CheckIncludeFile) + include (CheckCSourceCompiles) +elseif(CMAKE_CXX_COMPILER_LOADED) + include (CheckIncludeFileCXX) + include (CheckCXXSourceCompiles) +else() + message(FATAL_ERROR "FindGLPK only works if either C or CXX language is enabled") +endif() + +if(NOT GLPK_ROOT) + if (DEFINED ENV{GLPK_ROOT}) + set(GLPK_ROOT $ENV{GLPK_ROOT}) + else() + find_path(GLPK_PATH libglpk.a libglpk.lib + DOC "Path in which libglpk is found" + PATHS + "/usr/local/lib" + "/usr/lib" + "${CMAKE_BINARY_DIR}/dependencies/GLPK/source/w32" + "${CMAKE_BINARY_DIR}/dependencies/GLPK/source/w64" + "${CMAKE_BINARY_DIR}/dependencies/GLPK/source/src/.lib" + ) + if(GLPK_PATH) + set(GLPK_ROOT ${GLPK_PATH}) + endif() + endif() +endif() + +message(STATUS "GLPK_ROOT: ${GLPK_ROOT}") +if(NOT GLPK_ROOT) + message(FATAL_ERROR "GLPK_ROOT: not found") +else() + set(GLPK_FOUND TRUE) +endif() + +if(GLPK_FOUND AND NOT TARGET GLPK::GLPK) + add_library(GLPK::GLPK UNKNOWN IMPORTED) + if (UNIX) + set_property(TARGET GLPK::GLPK PROPERTY IMPORTED_LOCATION + ${GLPK_ROOT}/libglpk.a + ) + elseif(MSVC) + set_property(TARGET GLPK::GLPK PROPERTY IMPORTED_LOCATION + ${GLPK_ROOT}/libglpk.lib + ) + else() + message(FATAL_ERROR "OR-Tools with GLPK not supported for ${CMAKE_SYSTEM}") + endif() +endif() diff --git a/cmake/README.md b/cmake/README.md index aa946882f7..22c8b9b4b8 100644 --- a/cmake/README.md +++ b/cmake/README.md @@ -75,6 +75,9 @@ compile few of them using the options below (see [CMake Options](#cmake-options) * SCIP (`BUILD_SCIP`),
note: You can disable the support of SCIP solvers by using `-DUSE_SCIP=OFF` (`ON` by default). +* GLPK (`BUILD_GLPK`),
+ note: You can disable the support of GLPK solvers + by using `-DUSE_GLPK=OFF` (`ON` by default). * COIN-OR solvers, * COIN-OR CoinUtils (`BUILD_CoinUtils`), @@ -152,6 +155,8 @@ cmake -S. -Bbuild -LH | `BUILD_Protobuf` | OFF* | Static build the protobuf libraries
**Forced** to ON if `BUILD_DEPS=ON` | | `USE_SCIP` | ON\* | Enable SCIP support
**Forced** to OFF if `BUILD_CXX=OFF` | | `BUILD_SCIP` | OFF\* | Static build the SCIP libraries
**Forced** to ON if `USE_SCIP=ON` **and** `BUILD_DEPS=ON` | +| `USE_GLPK` | ON\* | Enable GLPK support
**Forced** to OFF if `BUILD_CXX=OFF` | +| `BUILD_GLPK` | OFF\* | Static build the GLPK libraries
**Forced** to ON if `USE_GLPK=ON` **and** `BUILD_DEPS=ON` | | `USE_COINOR` | ON\* | Enable Coin-OR support
**Forced** to OFF if `BUILD_CXX=OFF` | | `BUILD_CoinUtils` | OFF\* | Static build the CoinUtils library
**Forced** to ON if `USE_COINOR=ON` **and** `BUILD_DEPS=ON` | | `BUILD_Osi` | OFF\* | Static build the Osi library
**Forced** to ON if `USE_COINOR=ON` **and** `BUILD_DEPS=ON` | diff --git a/cmake/cpp.cmake b/cmake/cpp.cmake index 4bf7bef960..fde0011493 100644 --- a/cmake/cpp.cmake +++ b/cmake/cpp.cmake @@ -23,6 +23,9 @@ if(USE_SCIP) list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_SCIP") set(GSCIP_DIR gscip) endif() +if(USE_GLPK) + list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_GLPK") +endif() if(USE_COINOR) list(APPEND OR_TOOLS_COMPILE_DEFINITIONS "USE_CBC" # enable COIN-OR CBC support @@ -123,6 +126,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC protobuf::libprotobuf ${COINOR_DEPS} $<$:libscip> + $<$:GLPK::GLPK> $<$:CPLEX::CPLEX> $<$:XPRESS::XPRESS> Threads::Threads) diff --git a/cmake/dependencies/CMakeLists.txt b/cmake/dependencies/CMakeLists.txt index 7cf7e036a6..486c7dc961 100644 --- a/cmake/dependencies/CMakeLists.txt +++ b/cmake/dependencies/CMakeLists.txt @@ -102,6 +102,27 @@ if(BUILD_SCIP) message(CHECK_PASS "fetched") endif() +# ############################################################################## +# GLPK +# ############################################################################## +if(BUILD_GLPK) + message(CHECK_START "Fetching GLPK") + list(APPEND CMAKE_MESSAGE_INDENT " ") + set(BUILD_EXAMPLES OFF) + set(WITH_GMP OFF) + set(WITH_ODBC OFF) + set(WITH_MYSQL OFF) + + FetchContent_Declare( + glpk + GIT_REPOSITORY https://github.com/Mizux/GLPK.git + GIT_TAG 5.0 + ) + FetchContent_MakeAvailable(glpk) + list(POP_BACK CMAKE_MESSAGE_INDENT) + message(CHECK_PASS "fetched") +endif() + # ############################################################################## # Protobuf # ############################################################################## diff --git a/cmake/deps.cmake b/cmake/deps.cmake index bab76fbd1a..bf1eef5a68 100644 --- a/cmake/deps.cmake +++ b/cmake/deps.cmake @@ -54,6 +54,12 @@ if(USE_SCIP) endif() endif() +if(USE_GLPK) + if(NOT BUILD_GLPK) + find_package(GLPK REQUIRED) + endif() +endif() + if(USE_COINOR) if(NOT BUILD_CoinUtils) find_package(CoinUtils REQUIRED) diff --git a/cmake/dotnet.cmake b/cmake/dotnet.cmake index 5c1084c50e..bb99cdb6d5 100644 --- a/cmake/dotnet.cmake +++ b/cmake/dotnet.cmake @@ -81,12 +81,15 @@ endif() # CMake will remove all '-D' prefix (i.e. -DUSE_FOO become USE_FOO) #get_target_property(FLAGS ${PROJECT_NAMESPACE}::ortools COMPILE_DEFINITIONS) set(FLAGS -DUSE_BOP -DUSE_GLOP -DABSL_MUST_USE_RESULT) -if(USE_SCIP) - list(APPEND FLAGS "-DUSE_SCIP") -endif() if(USE_COINOR) list(APPEND FLAGS "-DUSE_CBC" "-DUSE_CLP") endif() +if(USE_GLPK) + list(APPEND FLAGS "-DUSE_GLPK") +endif() +if(USE_SCIP) + list(APPEND FLAGS "-DUSE_SCIP") +endif() list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}") # Needed by dotnet/CMakeLists.txt diff --git a/cmake/java.cmake b/cmake/java.cmake index dc76ddfe5a..136c692fd5 100644 --- a/cmake/java.cmake +++ b/cmake/java.cmake @@ -56,12 +56,15 @@ set(JAVA_PROJECT ortools-java) # CMake will remove all '-D' prefix (i.e. -DUSE_FOO become USE_FOO) #get_target_property(FLAGS ${PROJECT_NAMESPACE}::ortools COMPILE_DEFINITIONS) set(FLAGS -DUSE_BOP -DUSE_GLOP -DABSL_MUST_USE_RESULT) -if(USE_SCIP) - list(APPEND FLAGS "-DUSE_SCIP") -endif() if(USE_COINOR) list(APPEND FLAGS "-DUSE_CBC" "-DUSE_CLP") endif() +if(USE_GLPK) + list(APPEND FLAGS "-DUSE_GLPK") +endif() +if(USE_SCIP) + list(APPEND FLAGS "-DUSE_SCIP") +endif() list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}") # Generate Protobuf java sources diff --git a/cmake/ortoolsConfig.cmake.in b/cmake/ortoolsConfig.cmake.in index 845571c341..b1aeb4ff59 100644 --- a/cmake/ortoolsConfig.cmake.in +++ b/cmake/ortoolsConfig.cmake.in @@ -17,16 +17,11 @@ endif() if(NOT absl_FOUND) find_dependency(absl REQUIRED ${CONFIG_FLAG}) endif() + if(NOT Protobuf_FOUND AND NOT PROTOBUF_FOUND AND NOT TARGET protobuf::libprotobuf) find_dependency(Protobuf REQUIRED ${CONFIG_FLAG}) endif() -if(@USE_SCIP@) - if(NOT scip_FOUND AND NOT TARGET libscip) - find_dependency(scip REQUIRED ${CONFIG_FLAG}) - endif() -endif() - if(@USE_COINOR@) if(NOT Clp_FOUND AND NOT TARGET Coin::ClpSolver) find_dependency(Clp REQUIRED ${CONFIG_FLAG}) @@ -36,4 +31,16 @@ if(@USE_COINOR@) endif() endif() +if(@USE_GLPK@) + if(NOT GLPK_FOUND AND NOT TARGET GLPK::GLPK) + find_dependency(GLPK REQUIRED ${CONFIG_FLAG}) + endif() +endif() + +if(@USE_SCIP@) + if(NOT scip_FOUND AND NOT TARGET libscip) + find_dependency(scip REQUIRED ${CONFIG_FLAG}) + endif() +endif() + include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake") diff --git a/cmake/python.cmake b/cmake/python.cmake index 0580743abc..0c5475b565 100644 --- a/cmake/python.cmake +++ b/cmake/python.cmake @@ -149,12 +149,15 @@ add_custom_target(Py${PROJECT_NAME}_proto DEPENDS ${PROTO_PYS} ${PROJECT_NAMESPA # CMake will remove all '-D' prefix (i.e. -DUSE_FOO become USE_FOO) #get_target_property(FLAGS ${PROJECT_NAMESPACE}::ortools COMPILE_DEFINITIONS) set(FLAGS -DUSE_BOP -DUSE_GLOP -DABSL_MUST_USE_RESULT) -if(USE_SCIP) - list(APPEND FLAGS "-DUSE_SCIP") -endif() if(USE_COINOR) list(APPEND FLAGS "-DUSE_CBC" "-DUSE_CLP") endif() +if(USE_GLPK) + list(APPEND FLAGS "-DUSE_GLPK") +endif() +if(USE_SCIP) + list(APPEND FLAGS "-DUSE_SCIP") +endif() list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}") set(PYTHON_PROJECT ${PROJECT_NAME}) diff --git a/ortools/linear_solver/CMakeLists.txt b/ortools/linear_solver/CMakeLists.txt index f814b08c6a..13f7c0cf81 100644 --- a/ortools/linear_solver/CMakeLists.txt +++ b/ortools/linear_solver/CMakeLists.txt @@ -23,10 +23,11 @@ target_link_libraries(${NAME} PRIVATE absl::strings absl::str_format protobuf::libprotobuf + $<$:libscip> + $<$:GLPK::GLPK> $<$:Coin::Cbc> $<$:Coin::Clp> $<$:CPLEX::CPLEX> $<$:XPRESS::XPRESS> - $<$:libscip> ${PROJECT_NAME}::proto) #add_library(${PROJECT_NAME}::linear_solver ALIAS ${NAME})