Commit 2679675f authored by Martin Heistermann's avatar Martin Heistermann

CMake: rewrite gurobi finder:

- find any version of GUROBI based on the GUROBI_HOME env var (suggested
by Gurobi docs).
- compile C++ library ourselves to avoid toolchain/ABI mismatches
parent d24eed09
Pipeline #12047 passed with stages
in 12 minutes and 14 seconds
# - Try to find GUROBI
# GUROBI_BASE - The libraries needed to use Gurobi
# Once done this will define
# GUROBI_FOUND - System has Gurobi
# GUROBI_INCLUDE_DIRS - The Gurobi include directories
# GUROBI_LIBRARIES - The libraries needed to use Gurobi
set (GUROBI_ENABLE OFF CACHE BOOL "Enable gurobi?")
set(GUROBI_ENABLE OFF CACHE BOOL "Enable gurobi?")
if ( GUROBI_ENABLE )
set (GUROBI_BASE "c:" CACHE PATH "Base path of your gurobi installation")
if (GUROBI_ENABLE)
if (GUROBI_INCLUDE_DIR)
# in cache already
set(GUROBI_FOUND TRUE)
set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}" )
set(GUROBI_LIBRARIES "${GUROBI_CXX_LIBRARY};${GUROBI_LIBRARY}" )
else (GUROBI_INCLUDE_DIR)
set(GUROBI_BASE $ENV{GUROBI_HOME} CACHE PATH "GUROBI root directory.")
find_path(GUROBI_INCLUDE_DIR
find_path(GUROBI_INCLUDE_DIR
NAMES gurobi_c++.h
PATHS "$ENV{GUROBI_HOME}/include"
"/Library/gurobi502/mac64/include"
"/Library/gurobi562/mac64/include"
"C:\\libs\\gurobi502\\include"
"C:\\libs\\gurobi562\\include"
"${GUROBI_BASE}/include"
PATHS
"${GUROBI_BASE}/include"
"$ENV{GUROBI_HOME}/include"
)
find_library( GUROBI_LIBRARY
NAMES gurobi
gurobi60
gurobi56
gurobi55
gurobi51
gurobi50
gurobi46
gurobi45
PATHS "$ENV{GUROBI_HOME}/lib"
"/Library/gurobi562/mac64/lib"
"/Library/gurobi502/mac64/lib"
"C:\\libs\\gurobi562\\lib"
"C:\\libs\\gurobi502\\lib"
"${GUROBI_BASE}/lib"
)
get_filename_component(GUROBI_LIB_DIR "${GUROBI_INCLUDE_DIR}/../lib" ABSOLUTE)
# GUROBI_BIN_DIR is needed on windows, where it contains the .dll
get_filename_component(GUROBI_BIN_DIR "${GUROBI_INCLUDE_DIR}/../bin" ABSOLUTE)
get_filename_component(GUROBI_SRC_DIR "${GUROBI_INCLUDE_DIR}/../src" ABSOLUTE)
file(GLOB GUROBI_LIBRARY_LIST
RELATIVE ${GUROBI_LIB_DIR}
${GUROBI_LIB_DIR}/libgurobi*.so
${GUROBI_LIB_DIR}/libgurobi*.dll)
# Ignore libgurobiXY_light.so, libgurobi.so (without version):
string(REGEX MATCHALL
"libgurobi([0-9]+)\\..*"
GUROBI_LIBRARY_LIST
"${GUROBI_LIBRARY_LIST}"
)
string(REGEX REPLACE
"libgurobi([0-9]+)\\..*"
"\\1"
GUROBI_LIBRARY_VERSIONS
"${GUROBI_LIBRARY_LIST}")
list(LENGTH GUROBI_LIBRARY_VERSIONS GUROBI_NUMVER)
#message("GUROBI LIB VERSIONS: ${GUROBI_LIBRARY_VERSIONS}")
if (GUROBI_NUMVER EQUAL 1)
list(GET GUROBI_LIBRARY_VERSIONS 0 GUROBI_LIBRARY_VERSION)
set(GUROBI_LIBRARY_NAME "gurobi${GUROBI_LIBRARY_VERSION}")
else()
# none or more than one versioned library -let's try without suffix,
# maybe the user added a symlink to the desired library
set(GUROBI_LIBRARY_NAME "gurobi")
endif()
#message("GUROBI LIB NAME: ${GUROBI_LIBRARY_NAME}")
find_library(GUROBI_LIBRARY
NAMES ${GUROBI_LIBRARY_NAME}
PATHS
"${GUROBI_BASE}/lib"
"$ENV{GUROBI_HOME}/lib"
)
# Gurobi ships with some compiled versions of its C++ library for specific
# compilers, however it also comes with the source code. We will compile
# the source code outselves -- this is much safer, as it guarantees the same
# compiler version and flags.
# (Note: doing this is motivated by actual sometimes-subtle ABI compatibility bugs)
# The old behaviour can be enabled with GUROBI_USE_PRECOMPILED_CXX)
option(GUROBI_USE_PRECOMPILED_CXX "Use precompiled C++ libraries instead of building it ourselves. Not recommended." OFF)
mark_as_advanced(GUROBI_USE_PRECOMPILED_CXX)
if(GUROBI_USE_PRECOMPILED_CXX)
if ( CMAKE_GENERATOR MATCHES "^Visual Studio 12.*Win64" )
SET(GUROBI_LIB_NAME "gurobi_c++md2013")
endif()
find_library( GUROBI_CXX_LIBRARY
NAMES gurobi_c++
${GUROBI_LIB_NAME}
PATHS "$ENV{GUROBI_HOME}/lib"
"/Library/gurobi562/mac64/lib"
"/Library/gurobi502/mac64/lib"
"C:\\libs\\gurobi562\\lib"
"C:\\libs\\gurobi502\\lib"
"${GUROBI_BASE}/lib"
)
# Binary dir for DLLs
find_path(GUROBI_BIN_DIR
NAMES "gurobi60.dll"
PATHS "${GUROBI_INCLUDE_DIR}/../bin"
"${GUROBI_BASE}/bin"
DOC "Directory containing the GUROBI DLLs"
)
find_library(GUROBI_CXX_LIBRARY
NAMES gurobi_c++
${GUROBI_LIB_NAME}
PATHS "$ENV{GUROBI_HOME}/lib"
"${GUROBI_BASE}/lib"
)
else()
file(GLOB GUROBI_CXX_SRC CONFIGURE_DEPENDS ${GUROBI_SRC_DIR}/cpp/*.cpp)
if(NOT GUROBI_CXX_SRC)
message(FATAL_ERROR "could not find gurobi c++ sources in GUROBI_SRC_DIR=${GUROBI_SRC_DIR}/cpp/.")
endif()
add_library(GurobiCXX STATIC ${GUROBI_CXX_SRC})
target_include_directories(GurobiCXX PUBLIC ${GUROBI_INCLUDE_DIR})
# We need to be able to link this into a shared library:
set_target_properties(GurobiCXX PROPERTIES POSITION_INDEPENDENT_CODE ON)
set(GUROBI_CXX_LIBRARY $<TARGET_FILE:GurobiCXX>)
endif(GUROBI_USE_PRECOMPILED_CXX)
set(GUROBI_INCLUDE_DIRS "${GUROBI_INCLUDE_DIR}" )
set(GUROBI_LIBRARIES "${GUROBI_CXX_LIBRARY};${GUROBI_LIBRARY}" )
......@@ -79,13 +102,10 @@ set(GUROBI_LIBRARIES "${GUROBI_CXX_LIBRARY};${GUROBI_LIBRARY}" )
# set(GUROBI_COMPILER_FLAGS "-DIL_STD" CACHE STRING "Gurobi Compiler Flags")
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBCPLEX_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(GUROBI DEFAULT_MSG
GUROBI_CXX_LIBRARY GUROBI_LIBRARY GUROBI_INCLUDE_DIR)
mark_as_advanced(GUROBI_INCLUDE_DIR GUROBI_LIBRARY GUROBI_CXX_LIBRARY GUROBI_BIN_DIR )
endif(GUROBI_INCLUDE_DIR)
endif(GUROBI_ENABLE)
endif()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment