Blame cmake/FindEigen.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
# FindEigen.cmake - Find Eigen library, version >= 3.
Packit ea1746
#
Packit ea1746
# This module defines the following variables:
Packit ea1746
#
Packit ea1746
# EIGEN_FOUND: TRUE iff Eigen is found.
Packit ea1746
# EIGEN_INCLUDE_DIRS: Include directories for Eigen.
Packit ea1746
# EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
Packit ea1746
# EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
Packit ea1746
# EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
Packit ea1746
# EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
Packit ea1746
# FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION: True iff the version of Eigen
Packit ea1746
#                                            found was built & installed /
Packit ea1746
#                                            exported as a CMake package.
Packit ea1746
#
Packit ea1746
# The following variables control the behaviour of this module:
Packit ea1746
#
Packit ea1746
# EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
Packit ea1746
#                           then prefer using an exported CMake configuration
Packit ea1746
#                           generated by Eigen over searching for the
Packit ea1746
#                           Eigen components manually.  Otherwise (FALSE)
Packit ea1746
#                           ignore any exported Eigen CMake configurations and
Packit ea1746
#                           always perform a manual search for the components.
Packit ea1746
#                           Default: TRUE iff user does not define this variable
Packit ea1746
#                           before we are called, and does NOT specify
Packit ea1746
#                           EIGEN_INCLUDE_DIR_HINTS, otherwise FALSE.
Packit ea1746
# EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
Packit ea1746
#                          search for eigen includes, e.g: /timbuktu/eigen3.
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
# EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
Packit ea1746
#                    include directory of any dependencies.
Packit ea1746
Packit ea1746
# Called if we failed to find Eigen 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(EIGEN_REPORT_NOT_FOUND REASON_MSG)
Packit ea1746
  unset(EIGEN_FOUND)
Packit ea1746
  unset(EIGEN_INCLUDE_DIRS)
Packit ea1746
  unset(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION)
Packit ea1746
  # Make results of search visible in the CMake GUI if Eigen has not
Packit ea1746
  # been found so that user does not have to toggle to advanced view.
Packit ea1746
  mark_as_advanced(CLEAR EIGEN_INCLUDE_DIR)
Packit ea1746
  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
Packit ea1746
  # use the camelcase library name, not uppercase.
Packit ea1746
  if (Eigen_FIND_QUIETLY)
Packit ea1746
    message(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
Packit ea1746
  elseif (Eigen_FIND_REQUIRED)
Packit ea1746
    message(FATAL_ERROR "Failed to find Eigen - " ${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 Eigen - " ${REASON_MSG} ${ARGN})
Packit ea1746
  endif ()
Packit ea1746
  return()
Packit ea1746
endmacro(EIGEN_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 EIGEN_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(EIGEN_FOUND)
Packit ea1746
Packit ea1746
# -----------------------------------------------------------------
Packit ea1746
# By default, if the user has expressed no preference for using an exported
Packit ea1746
# Eigen CMake configuration over performing a search for the installed
Packit ea1746
# components, and has not specified any hints for the search locations, then
Packit ea1746
# prefer an exported configuration if available.
Packit ea1746
if (NOT DEFINED EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION
Packit ea1746
    AND NOT EIGEN_INCLUDE_DIR_HINTS)
Packit ea1746
  message(STATUS "No preference for use of exported Eigen CMake configuration "
Packit ea1746
    "set, and no hints for include directory provided. "
Packit ea1746
    "Defaulting to preferring an installed/exported Eigen CMake configuration "
Packit ea1746
    "if available.")
Packit ea1746
  set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
Packit ea1746
endif()
Packit ea1746
Packit ea1746
if (EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION)
Packit ea1746
  # Try to find an exported CMake configuration for Eigen.
Packit ea1746
  #
Packit ea1746
  # We search twice, s/t we can invert the ordering of precedence used by
Packit ea1746
  # find_package() for exported package build directories, and installed
Packit ea1746
  # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
Packit ea1746
  # respectively in [1].
Packit ea1746
  #
Packit ea1746
  # By default, exported build directories are (in theory) detected first, and
Packit ea1746
  # this is usually the case on Windows.  However, on OS X & Linux, the install
Packit ea1746
  # path (/usr/local) is typically present in the PATH environment variable
Packit ea1746
  # which is checked in item 4) in [1] (i.e. before both of the above, unless
Packit ea1746
  # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
Packit ea1746
  # packages are usually detected in preference to exported package build
Packit ea1746
  # directories.
Packit ea1746
  #
Packit ea1746
  # To ensure a more consistent response across all OSs, and as users usually
Packit ea1746
  # want to prefer an installed version of a package over a locally built one
Packit ea1746
  # where both exist (esp. as the exported build directory might be removed
Packit ea1746
  # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
Packit ea1746
  # means any build directories exported by the user are ignored, and thus
Packit ea1746
  # installed directories are preferred.  If this fails to find the package
Packit ea1746
  # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
Packit ea1746
  # exported build directories will now be detected.
Packit ea1746
  #
Packit ea1746
  # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
Packit ea1746
  # is item 5) in [1]), to not preferentially use projects that were built
Packit ea1746
  # recently with the CMake GUI to ensure that we always prefer an installed
Packit ea1746
  # version if available.
Packit ea1746
  #
Packit ea1746
  # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
Packit ea1746
  find_package(Eigen3 QUIET
Packit ea1746
                      NO_MODULE
Packit ea1746
                      NO_CMAKE_PACKAGE_REGISTRY
Packit ea1746
                      NO_CMAKE_BUILDS_PATH)
Packit ea1746
  if (EIGEN3_FOUND)
Packit ea1746
    message(STATUS "Found installed version of Eigen: ${Eigen3_DIR}")
Packit ea1746
  else()
Packit ea1746
    # Failed to find an installed version of Eigen, repeat search allowing
Packit ea1746
    # exported build directories.
Packit ea1746
    message(STATUS "Failed to find installed Eigen CMake configuration, "
Packit ea1746
      "searching for Eigen build directories exported with CMake.")
Packit ea1746
    # Again pass NO_CMAKE_BUILDS_PATH, as we know that Eigen is exported and
Packit ea1746
    # do not want to treat projects built with the CMake GUI preferentially.
Packit ea1746
    find_package(Eigen3 QUIET
Packit ea1746
                        NO_MODULE
Packit ea1746
                        NO_CMAKE_BUILDS_PATH)
Packit ea1746
    if (EIGEN3_FOUND)
Packit ea1746
      message(STATUS "Found exported Eigen build directory: ${Eigen3_DIR}")
Packit ea1746
    endif()
Packit ea1746
  endif()
Packit ea1746
  if (EIGEN3_FOUND)
Packit ea1746
    set(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION TRUE)
Packit ea1746
    set(EIGEN_FOUND ${EIGEN3_FOUND})
Packit ea1746
    set(EIGEN_INCLUDE_DIR "${EIGEN3_INCLUDE_DIR}" CACHE STRING
Packit ea1746
      "Eigen include directory" FORCE)
Packit ea1746
  else()
Packit ea1746
    message(STATUS "Failed to find an installed/exported CMake configuration "
Packit ea1746
      "for Eigen, will perform search for installed Eigen components.")
Packit ea1746
  endif()
Packit ea1746
endif()
Packit ea1746
Packit ea1746
if (NOT EIGEN_FOUND)
Packit ea1746
  # Search user-installed locations first, so that we prefer user installs
Packit ea1746
  # to system installs where both exist.
Packit ea1746
  list(APPEND EIGEN_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
  # Additional suffixes to try appending to each search path.
Packit ea1746
  list(APPEND EIGEN_CHECK_PATH_SUFFIXES
Packit ea1746
    eigen3 # Default root directory for Eigen.
Packit ea1746
    Eigen/include/eigen3 # Windows (for C:/Program Files prefix) < 3.3
Packit ea1746
    Eigen3/include/eigen3 ) # Windows (for C:/Program Files prefix) >= 3.3
Packit ea1746
Packit ea1746
  # Search supplied hint directories first if supplied.
Packit ea1746
  find_path(EIGEN_INCLUDE_DIR
Packit ea1746
    NAMES Eigen/Core
Packit ea1746
    HINTS ${EIGEN_INCLUDE_DIR_HINTS}
Packit ea1746
    PATHS ${EIGEN_CHECK_INCLUDE_DIRS}
Packit ea1746
    PATH_SUFFIXES ${EIGEN_CHECK_PATH_SUFFIXES})
Packit ea1746
Packit ea1746
  if (NOT EIGEN_INCLUDE_DIR OR
Packit ea1746
      NOT EXISTS ${EIGEN_INCLUDE_DIR})
Packit ea1746
    eigen_report_not_found(
Packit ea1746
      "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
Packit ea1746
      "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
Packit ea1746
  endif (NOT EIGEN_INCLUDE_DIR OR
Packit ea1746
    NOT EXISTS ${EIGEN_INCLUDE_DIR})
Packit ea1746
Packit ea1746
  # Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
Packit ea1746
  # if called.
Packit ea1746
  set(EIGEN_FOUND TRUE)
Packit ea1746
endif()
Packit ea1746
Packit ea1746
# Extract Eigen version from Eigen/src/Core/util/Macros.h
Packit ea1746
if (EIGEN_INCLUDE_DIR)
Packit ea1746
  set(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
Packit ea1746
  if (NOT EXISTS ${EIGEN_VERSION_FILE})
Packit ea1746
    eigen_report_not_found(
Packit ea1746
      "Could not find file: ${EIGEN_VERSION_FILE} "
Packit ea1746
      "containing version information in Eigen install located at: "
Packit ea1746
      "${EIGEN_INCLUDE_DIR}.")
Packit ea1746
  else (NOT EXISTS ${EIGEN_VERSION_FILE})
Packit ea1746
    file(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
Packit ea1746
      EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
Packit ea1746
      EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
Packit ea1746
      EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
Packit ea1746
      EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
Packit ea1746
Packit ea1746
    string(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
Packit ea1746
      EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
Packit ea1746
    string(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
Packit ea1746
      EIGEN_MINOR_VERSION "${EIGEN_MINOR_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.;2.;0 nonsense.
Packit ea1746
    set(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
Packit ea1746
  endif (NOT EXISTS ${EIGEN_VERSION_FILE})
Packit ea1746
endif (EIGEN_INCLUDE_DIR)
Packit ea1746
Packit ea1746
# Set standard CMake FindPackage variables if found.
Packit ea1746
if (EIGEN_FOUND)
Packit ea1746
  set(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
Packit ea1746
endif (EIGEN_FOUND)
Packit ea1746
Packit ea1746
# Handle REQUIRED / QUIET optional arguments and version.
Packit ea1746
include(FindPackageHandleStandardArgs)
Packit ea1746
find_package_handle_standard_args(Eigen
Packit ea1746
  REQUIRED_VARS EIGEN_INCLUDE_DIRS
Packit ea1746
  VERSION_VAR EIGEN_VERSION)
Packit ea1746
Packit ea1746
# Only mark internal variables as advanced if we found Eigen, otherwise
Packit ea1746
# leave it visible in the standard GUI for the user to set manually.
Packit ea1746
if (EIGEN_FOUND)
Packit ea1746
  mark_as_advanced(FORCE EIGEN_INCLUDE_DIR
Packit ea1746
    Eigen3_DIR) # Autogenerated by find_package(Eigen3)
Packit ea1746
endif (EIGEN_FOUND)