Skip to content
Snippets Groups Projects
Commit b4df6d27 authored by Martin Heistermann's avatar Martin Heistermann
Browse files

Replace Gurobi finder with much-improved version. Fixes #1.

parent 690f37fc
No related branches found
No related tags found
1 merge request!6Replace Gurobi finder with much-improved version. Fixes #1.
Pipeline #16889 failed
...@@ -3,13 +3,13 @@ ...@@ -3,13 +3,13 @@
# Targets: # Targets:
# Gurobi::GurobiC - only the C interface # Gurobi::GurobiC - only the C interface
# Gurobi::GurobiCXX - C and C++ interface # Gurobi::GurobiCXX - C and C++ interface
# Gurobi::Gurobi - same as GurobiCXX
find_path(GUROBI_HOME find_path(GUROBI_HOME
NAMES include/gurobi_c++.h NAMES include/gurobi_c++.h
PATHS PATHS
$ENV{GUROBI_HOME} $ENV{GUROBI_HOME}
"/opt/gurobi/linux64/" "/opt/gurobi/linux64/"
NO_DEFAULT_PATH # avoid finding /usr
) )
find_path(GUROBI_INCLUDE_DIR find_path(GUROBI_INCLUDE_DIR
...@@ -17,15 +17,22 @@ find_path(GUROBI_INCLUDE_DIR ...@@ -17,15 +17,22 @@ find_path(GUROBI_INCLUDE_DIR
HINTS HINTS
"${GUROBI_HOME}/include" "${GUROBI_HOME}/include"
) )
mark_as_advanced(GUROBI_INCLUDE_DIR)
set(GUROBI_BIN_DIR "${GUROBI_HOME}/bin") set(GUROBI_BIN_DIR "${GUROBI_HOME}/bin")
set(GUROBI_LIB_DIR "${GUROBI_HOME}/lib") set(GUROBI_LIB_DIR "${GUROBI_HOME}/lib")
if (WIN32)
file(GLOB GUROBI_LIBRARY_LIST
RELATIVE ${GUROBI_BIN_DIR}
${GUROBI_BIN_DIR}/gurobi*.dll
)
else()
file(GLOB GUROBI_LIBRARY_LIST file(GLOB GUROBI_LIBRARY_LIST
RELATIVE ${GUROBI_LIB_DIR} RELATIVE ${GUROBI_LIB_DIR}
${GUROBI_LIB_DIR}/libgurobi*.so ${GUROBI_LIB_DIR}/libgurobi*.so
${GUROBI_LIB_DIR}/gurobi*.lib
) )
endif()
# Ignore libgurobiXY_light.so, libgurobi.so (without version): # Ignore libgurobiXY_light.so, libgurobi.so (without version):
string(REGEX MATCHALL string(REGEX MATCHALL
...@@ -44,7 +51,7 @@ list(LENGTH GUROBI_LIBRARY_VERSIONS GUROBI_NUMVER) ...@@ -44,7 +51,7 @@ list(LENGTH GUROBI_LIBRARY_VERSIONS GUROBI_NUMVER)
#message("GUROBI LIB VERSIONS: ${GUROBI_LIBRARY_VERSIONS}") #message("GUROBI LIB VERSIONS: ${GUROBI_LIBRARY_VERSIONS}")
if (GUROBI_NUMVER EQUAL 0) if (GUROBI_NUMVER EQUAL 0)
message(STATUS "Found no Gurobi library version in ${GUROBI_LIB_DIR}.") message(STATUS "Found no Gurobi library version, GUROBI_HOME = ${GUROBI_HOME}.")
elseif (GUROBI_NUMVER EQUAL 1) elseif (GUROBI_NUMVER EQUAL 1)
list(GET GUROBI_LIBRARY_VERSIONS 0 GUROBI_LIBRARY_VERSION) list(GET GUROBI_LIBRARY_VERSIONS 0 GUROBI_LIBRARY_VERSION)
else() else()
...@@ -54,12 +61,35 @@ else() ...@@ -54,12 +61,35 @@ else()
set(GUROBI_LIBRARY_VERSION "") set(GUROBI_LIBRARY_VERSION "")
endif() endif()
if (WIN32)
find_library(GUROBI_LIBRARY
NAMES "gurobi${GUROBI_LIBRARY_VERSION}"
PATHS
${GUROBI_BIN_DIR}
)
find_library(GUROBI_IMPLIB
NAMES "gurobi${GUROBI_LIBRARY_VERSION}"
PATHS
${GUROBI_LIB_DIR}
)
mark_as_advanced(GUROBI_IMPLIB)
else ()
find_library(GUROBI_LIBRARY find_library(GUROBI_LIBRARY
NAMES "gurobi${GUROBI_LIBRARY_VERSION}" NAMES "gurobi${GUROBI_LIBRARY_VERSION}"
PATHS PATHS
${GUROBI_LIB_DIR} ${GUROBI_LIB_DIR}
) )
endif()
mark_as_advanced(GUROBI_LIBRARY)
if(GUROBI_LIBRARY AND NOT TARGET Gurobi::GurobiC)
add_library(Gurobi::GurobiC SHARED IMPORTED)
target_include_directories(Gurobi::GurobiC INTERFACE ${GUROBI_INCLUDE_DIR})
set_target_properties(Gurobi::GurobiC PROPERTIES IMPORTED_LOCATION ${GUROBI_LIBRARY})
if (GUROBI_IMPLIB)
set_target_properties(Gurobi::GurobiC PROPERTIES IMPORTED_IMPLIB ${GUROBI_IMPLIB})
endif()
endif()
# Gurobi ships with some compiled versions of its C++ library for specific # Gurobi ships with some compiled versions of its C++ library for specific
# compilers, however it also comes with the source code. We will compile # compilers, however it also comes with the source code. We will compile
...@@ -68,10 +98,10 @@ find_library(GUROBI_LIBRARY ...@@ -68,10 +98,10 @@ find_library(GUROBI_LIBRARY
# (Note: doing this is motivated by actual sometimes-subtle ABI compatibility bugs) # (Note: doing this is motivated by actual sometimes-subtle ABI compatibility bugs)
find_path(GUROBI_SRC_DIR NAMES "Model.h" PATHS "${GUROBI_HOME}/src/cpp/") find_path(GUROBI_SRC_DIR NAMES "Model.h" PATHS "${GUROBI_HOME}/src/cpp/")
mark_as_advanced(GUROBI_SRC_DIR)
file(GLOB GUROBI_CXX_SRC CONFIGURE_DEPENDS ${GUROBI_SRC_DIR}/*.cpp) file(GLOB GUROBI_CXX_SRC CONFIGURE_DEPENDS ${GUROBI_SRC_DIR}/*.cpp)
if(NOT TARGET Gurobi::GurobiCXX) if(TARGET Gurobi::GurobiC AND GUROBI_CXX_SRC AND NOT TARGET Gurobi::GurobiCXX)
if(GUROBI_CXX_SRC)
add_library(GurobiCXX STATIC EXCLUDE_FROM_ALL ${GUROBI_CXX_SRC}) add_library(GurobiCXX STATIC EXCLUDE_FROM_ALL ${GUROBI_CXX_SRC})
add_library(Gurobi::GurobiCXX ALIAS GurobiCXX) add_library(Gurobi::GurobiCXX ALIAS GurobiCXX)
...@@ -80,35 +110,16 @@ if(NOT TARGET Gurobi::GurobiCXX) ...@@ -80,35 +110,16 @@ if(NOT TARGET Gurobi::GurobiCXX)
endif() endif()
target_include_directories(GurobiCXX PUBLIC ${GUROBI_INCLUDE_DIR}) target_include_directories(GurobiCXX PUBLIC ${GUROBI_INCLUDE_DIR})
target_link_libraries(GurobiCXX PUBLIC ${GUROBI_LIBRARY}) target_link_libraries(GurobiCXX PUBLIC Gurobi::GurobiC)
# We need to be able to link this into a shared library: # We need to be able to link this into a shared library:
set_target_properties(GurobiCXX PROPERTIES POSITION_INDEPENDENT_CODE ON) set_target_properties(GurobiCXX PROPERTIES POSITION_INDEPENDENT_CODE ON)
set(GUROBI_CXX_LIBRARY $<TARGET_FILE:GurobiCXX>)
endif()
endif() endif()
# legacy support: # legacy support:
set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}") set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}")
set(GUROBI_LIBRARIES "${GUROBI_CXX_LIBRARY};${GUROBI_LIBRARY}" ) set(GUROBI_LIBRARIES Gurobi::GurobiC Gurobi::GurobiCXX)
include(FindPackageHandleStandardArgs) include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Gurobi DEFAULT_MSG find_package_handle_standard_args(Gurobi DEFAULT_MSG GUROBI_LIBRARY GUROBI_INCLUDE_DIR GUROBI_SRC_DIR)
GUROBI_LIBRARY
GUROBI_INCLUDE_DIR
GUROBI_SRC_DIR
GUROBI_CXX_LIBRARY
)
mark_as_advanced(GUROBI_INCLUDE_DIR GUROBI_LIBRARY GUROBI_CXX_LIBRARY GUROBI_BIN_DIR GUROBI_SRC_DIR)
if(GUROBI_FOUND AND NOT TARGET Gurobi::Gurobi)
add_library(Gurobi::GurobiC INTERFACE IMPORTED)
target_include_directories(Gurobi::GurobiC INTERFACE ${GUROBI_INCLUDE_DIR})
target_link_libraries(Gurobi::GurobiC INTERFACE ${GUROBI_LIBRARY})
add_library(Gurobi::Gurobi INTERFACE IMPORTED)
target_link_libraries(Gurobi::Gurobi INTERFACE Gurobi::GurobiC)
target_link_libraries(Gurobi::Gurobi INTERFACE Gurobi::GurobiCXX)
endif()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment