Blame cmake/FindCXSparse.cmake

Packit ea1746
# Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
# Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
# http://ceres-solver.org/
Packit ea1746
#
Packit ea1746
# Redistribution and use in source and binary forms, with or without
Packit ea1746
# modification, are permitted provided that the following conditions are met:
Packit ea1746
#
Packit ea1746
# * Redistributions of source code must retain the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer.
Packit ea1746
# * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
#   this list of conditions and the following disclaimer in the documentation
Packit ea1746
#   and/or other materials provided with the distribution.
Packit ea1746
# * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
#   used to endorse or promote products derived from this software without
Packit ea1746
#   specific prior written permission.
Packit ea1746
#
Packit ea1746
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
# POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
#
Packit ea1746
# Author: alexs.mac@gmail.com (Alex Stewart)
Packit ea1746
#
Packit ea1746
Packit ea1746
# FindCXSparse.cmake - Find CXSparse libraries & dependencies.
Packit ea1746
#
Packit ea1746
# This module defines the following variables which should be referenced
Packit ea1746
# by the caller to use the library.
Packit ea1746
#
Packit ea1746
# CXSPARSE_FOUND: TRUE iff CXSparse and all dependencies have been found.
Packit ea1746
# CXSPARSE_INCLUDE_DIRS: Include directories for CXSparse.
Packit ea1746
# CXSPARSE_LIBRARIES: Libraries for CXSparse and all dependencies.
Packit ea1746
#
Packit ea1746
# CXSPARSE_VERSION: Extracted from cs.h.
Packit ea1746
# CXSPARSE_MAIN_VERSION: Equal to 3 if CXSPARSE_VERSION = 3.1.2
Packit ea1746
# CXSPARSE_SUB_VERSION: Equal to 1 if CXSPARSE_VERSION = 3.1.2
Packit ea1746
# CXSPARSE_SUBSUB_VERSION: Equal to 2 if CXSPARSE_VERSION = 3.1.2
Packit ea1746
#
Packit ea1746
# The following variables control the behaviour of this module:
Packit ea1746
#
Packit ea1746
# CXSPARSE_INCLUDE_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                             search for CXSparse includes,
Packit ea1746
#                             e.g: /timbuktu/include.
Packit ea1746
# CXSPARSE_LIBRARY_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                             search for CXSparse libraries, e.g: /timbuktu/lib.
Packit ea1746
#
Packit ea1746
# The following variables are also defined by this module, but in line with
Packit ea1746
# CMake recommended FindPackage() module style should NOT be referenced directly
Packit ea1746
# by callers (use the plural variables detailed above instead).  These variables
Packit ea1746
# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
Packit ea1746
# are NOT re-called (i.e. search for library is not repeated) if these variables
Packit ea1746
# are set with valid values _in the CMake cache_. This means that if these
Packit ea1746
# variables are set directly in the cache, either by the user in the CMake GUI,
Packit ea1746
# or by the user passing -DVAR=VALUE directives to CMake when called (which
Packit ea1746
# explicitly defines a cache variable), then they will be used verbatim,
Packit ea1746
# bypassing the HINTS variables and other hard-coded search locations.
Packit ea1746
#
Packit ea1746
# CXSPARSE_INCLUDE_DIR: Include directory for CXSparse, not including the
Packit ea1746
#                       include directory of any dependencies.
Packit ea1746
# CXSPARSE_LIBRARY: CXSparse library, not including the libraries of any
Packit ea1746
#                   dependencies.
Packit ea1746
Packit ea1746
# Reset CALLERS_CMAKE_FIND_LIBRARY_PREFIXES to its value when
Packit ea1746
# FindCXSparse was invoked.
Packit ea1746
macro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
Packit ea1746
  if (MSVC)
Packit ea1746
    set(CMAKE_FIND_LIBRARY_PREFIXES "${CALLERS_CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
  endif (MSVC)
Packit ea1746
endmacro(CXSPARSE_RESET_FIND_LIBRARY_PREFIX)
Packit ea1746
Packit ea1746
# Called if we failed to find CXSparse or any of it's required dependencies,
Packit ea1746
# unsets all public (designed to be used externally) variables and reports
Packit ea1746
# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
Packit ea1746
macro(CXSPARSE_REPORT_NOT_FOUND REASON_MSG)
Packit ea1746
  unset(CXSPARSE_FOUND)
Packit ea1746
  unset(CXSPARSE_INCLUDE_DIRS)
Packit ea1746
  unset(CXSPARSE_LIBRARIES)
Packit ea1746
  # Make results of search visible in the CMake GUI if CXSparse has not
Packit ea1746
  # been found so that user does not have to toggle to advanced view.
Packit ea1746
  mark_as_advanced(CLEAR CXSPARSE_INCLUDE_DIR
Packit ea1746
                         CXSPARSE_LIBRARY)
Packit ea1746
Packit ea1746
  cxsparse_reset_find_library_prefix()
Packit ea1746
Packit ea1746
  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
Packit ea1746
  # use the camelcase library name, not uppercase.
Packit ea1746
  if (CXSparse_FIND_QUIETLY)
Packit ea1746
    message(STATUS "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
Packit ea1746
  elseif (CXSparse_FIND_REQUIRED)
Packit ea1746
    message(FATAL_ERROR "Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
Packit ea1746
  else()
Packit ea1746
    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
Packit ea1746
    # but continues configuration and allows generation.
Packit ea1746
    message("-- Failed to find CXSparse - " ${REASON_MSG} ${ARGN})
Packit ea1746
  endif ()
Packit ea1746
  return()
Packit ea1746
endmacro(CXSPARSE_REPORT_NOT_FOUND)
Packit ea1746
Packit ea1746
# Protect against any alternative find_package scripts for this library having
Packit ea1746
# been called previously (in a client project) which set CXSPARSE_FOUND, but not
Packit ea1746
# the other variables we require / set here which could cause the search logic
Packit ea1746
# here to fail.
Packit ea1746
unset(CXSPARSE_FOUND)
Packit ea1746
Packit ea1746
# Handle possible presence of lib prefix for libraries on MSVC, see
Packit ea1746
# also CXSPARSE_RESET_FIND_LIBRARY_PREFIX().
Packit ea1746
if (MSVC)
Packit ea1746
  # Preserve the caller's original values for CMAKE_FIND_LIBRARY_PREFIXES
Packit ea1746
  # s/t we can set it back before returning.
Packit ea1746
  set(CALLERS_CMAKE_FIND_LIBRARY_PREFIXES "${CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
  # The empty string in this list is important, it represents the case when
Packit ea1746
  # the libraries have no prefix (shared libraries / DLLs).
Packit ea1746
  set(CMAKE_FIND_LIBRARY_PREFIXES "lib" "" "${CMAKE_FIND_LIBRARY_PREFIXES}")
Packit ea1746
endif (MSVC)
Packit ea1746
Packit ea1746
# Search user-installed locations first, so that we prefer user installs
Packit ea1746
# to system installs where both exist.
Packit ea1746
#
Packit ea1746
# TODO: Add standard Windows search locations for CXSparse.
Packit ea1746
list(APPEND CXSPARSE_CHECK_INCLUDE_DIRS
Packit ea1746
  /usr/local/include
Packit ea1746
  /usr/local/homebrew/include # Mac OS X
Packit ea1746
  /opt/local/var/macports/software # Mac OS X.
Packit ea1746
  /opt/local/include
Packit ea1746
  /usr/include)
Packit ea1746
list(APPEND CXSPARSE_CHECK_LIBRARY_DIRS
Packit ea1746
  /usr/local/lib
Packit ea1746
  /usr/local/homebrew/lib # Mac OS X.
Packit ea1746
  /opt/local/lib
Packit ea1746
  /usr/lib)
Packit ea1746
# Additional suffixes to try appending to each search path.
Packit ea1746
list(APPEND CXSPARSE_CHECK_PATH_SUFFIXES
Packit ea1746
  suitesparse) # Linux/Windows
Packit ea1746
Packit ea1746
# Search supplied hint directories first if supplied.
Packit ea1746
find_path(CXSPARSE_INCLUDE_DIR
Packit ea1746
  NAMES cs.h
Packit ea1746
  HINTS ${CXSPARSE_INCLUDE_DIR_HINTS}
Packit ea1746
  PATHS ${CXSPARSE_CHECK_INCLUDE_DIRS}
Packit ea1746
  PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
Packit ea1746
if (NOT CXSPARSE_INCLUDE_DIR OR
Packit ea1746
    NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
Packit ea1746
  cxsparse_report_not_found(
Packit ea1746
    "Could not find CXSparse include directory, set CXSPARSE_INCLUDE_DIR "
Packit ea1746
    "to directory containing cs.h")
Packit ea1746
endif (NOT CXSPARSE_INCLUDE_DIR OR
Packit ea1746
       NOT EXISTS ${CXSPARSE_INCLUDE_DIR})
Packit ea1746
Packit ea1746
find_library(CXSPARSE_LIBRARY NAMES cxsparse
Packit ea1746
  HINTS ${CXSPARSE_LIBRARY_DIR_HINTS}
Packit ea1746
  PATHS ${CXSPARSE_CHECK_LIBRARY_DIRS}
Packit ea1746
  PATH_SUFFIXES ${CXSPARSE_CHECK_PATH_SUFFIXES})
Packit ea1746
if (NOT CXSPARSE_LIBRARY OR
Packit ea1746
    NOT EXISTS ${CXSPARSE_LIBRARY})
Packit ea1746
  cxsparse_report_not_found(
Packit ea1746
    "Could not find CXSparse library, set CXSPARSE_LIBRARY "
Packit ea1746
    "to full path to libcxsparse.")
Packit ea1746
endif (NOT CXSPARSE_LIBRARY OR
Packit ea1746
       NOT EXISTS ${CXSPARSE_LIBRARY})
Packit ea1746
Packit ea1746
# Mark internally as found, then verify. CXSPARSE_REPORT_NOT_FOUND() unsets
Packit ea1746
# if called.
Packit ea1746
set(CXSPARSE_FOUND TRUE)
Packit ea1746
Packit ea1746
# Extract CXSparse version from cs.h
Packit ea1746
if (CXSPARSE_INCLUDE_DIR)
Packit ea1746
  set(CXSPARSE_VERSION_FILE ${CXSPARSE_INCLUDE_DIR}/cs.h)
Packit ea1746
  if (NOT EXISTS ${CXSPARSE_VERSION_FILE})
Packit ea1746
    cxsparse_report_not_found(
Packit ea1746
      "Could not find file: ${CXSPARSE_VERSION_FILE} "
Packit ea1746
      "containing version information in CXSparse install located at: "
Packit ea1746
      "${CXSPARSE_INCLUDE_DIR}.")
Packit ea1746
  else (NOT EXISTS ${CXSPARSE_VERSION_FILE})
Packit ea1746
    file(READ ${CXSPARSE_INCLUDE_DIR}/cs.h CXSPARSE_VERSION_FILE_CONTENTS)
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define CS_VER [0-9]+"
Packit ea1746
      CXSPARSE_MAIN_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define CS_VER ([0-9]+)" "\\1"
Packit ea1746
      CXSPARSE_MAIN_VERSION "${CXSPARSE_MAIN_VERSION}")
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define CS_SUBVER [0-9]+"
Packit ea1746
      CXSPARSE_SUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define CS_SUBVER ([0-9]+)" "\\1"
Packit ea1746
      CXSPARSE_SUB_VERSION "${CXSPARSE_SUB_VERSION}")
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define CS_SUBSUB [0-9]+"
Packit ea1746
      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define CS_SUBSUB ([0-9]+)" "\\1"
Packit ea1746
      CXSPARSE_SUBSUB_VERSION "${CXSPARSE_SUBSUB_VERSION}")
Packit ea1746
Packit ea1746
    # This is on a single line s/t CMake does not interpret it as a list of
Packit ea1746
    # elements and insert ';' separators which would result in 3.;1.;2 nonsense.
Packit ea1746
    set(CXSPARSE_VERSION "${CXSPARSE_MAIN_VERSION}.${CXSPARSE_SUB_VERSION}.${CXSPARSE_SUBSUB_VERSION}")
Packit ea1746
  endif (NOT EXISTS ${CXSPARSE_VERSION_FILE})
Packit ea1746
endif (CXSPARSE_INCLUDE_DIR)
Packit ea1746
Packit ea1746
# Catch the case when the caller has set CXSPARSE_LIBRARY in the cache / GUI and
Packit ea1746
# thus FIND_LIBRARY was not called, but specified library is invalid, otherwise
Packit ea1746
# we would report CXSparse as found.
Packit ea1746
# TODO: This regex for CXSparse library is pretty primitive, we use lowercase
Packit ea1746
#       for comparison to handle Windows using CamelCase library names, could
Packit ea1746
#       this check be better?
Packit ea1746
string(TOLOWER "${CXSPARSE_LIBRARY}" LOWERCASE_CXSPARSE_LIBRARY)
Packit ea1746
if (CXSPARSE_LIBRARY AND
Packit ea1746
    EXISTS ${CXSPARSE_LIBRARY} AND
Packit ea1746
    NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
Packit ea1746
  cxsparse_report_not_found(
Packit ea1746
    "Caller defined CXSPARSE_LIBRARY: "
Packit ea1746
    "${CXSPARSE_LIBRARY} does not match CXSparse.")
Packit ea1746
endif (CXSPARSE_LIBRARY AND
Packit ea1746
       EXISTS ${CXSPARSE_LIBRARY} AND
Packit ea1746
       NOT "${LOWERCASE_CXSPARSE_LIBRARY}" MATCHES ".*cxsparse[^/]*")
Packit ea1746
Packit ea1746
# Set standard CMake FindPackage variables if found.
Packit ea1746
if (CXSPARSE_FOUND)
Packit ea1746
  set(CXSPARSE_INCLUDE_DIRS ${CXSPARSE_INCLUDE_DIR})
Packit ea1746
  set(CXSPARSE_LIBRARIES ${CXSPARSE_LIBRARY})
Packit ea1746
endif (CXSPARSE_FOUND)
Packit ea1746
Packit ea1746
cxsparse_reset_find_library_prefix()
Packit ea1746
Packit ea1746
# Handle REQUIRED / QUIET optional arguments and version.
Packit ea1746
include(FindPackageHandleStandardArgs)
Packit ea1746
find_package_handle_standard_args(CXSparse
Packit ea1746
  REQUIRED_VARS CXSPARSE_INCLUDE_DIRS CXSPARSE_LIBRARIES
Packit ea1746
  VERSION_VAR CXSPARSE_VERSION)
Packit ea1746
Packit ea1746
# Only mark internal variables as advanced if we found CXSparse, otherwise
Packit ea1746
# leave them visible in the standard GUI for the user to set manually.
Packit ea1746
if (CXSPARSE_FOUND)
Packit ea1746
  mark_as_advanced(FORCE CXSPARSE_INCLUDE_DIR
Packit ea1746
                         CXSPARSE_LIBRARY)
Packit ea1746
endif (CXSPARSE_FOUND)