1# Ceres Solver - A fast non-linear least squares minimizer
2# Copyright 2013 Google Inc. All rights reserved.
3# http://code.google.com/p/ceres-solver/
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions are met:
7#
8# * Redistributions of source code must retain the above copyright notice,
9#   this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright notice,
11#   this list of conditions and the following disclaimer in the documentation
12#   and/or other materials provided with the distribution.
13# * Neither the name of Google Inc. nor the names of its contributors may be
14#   used to endorse or promote products derived from this software without
15#   specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29# Author: alexs.mac@gmail.com (Alex Stewart)
30#
31
32# FindEigen.cmake - Find Eigen library, version >= 3.
33#
34# This module defines the following variables:
35#
36# EIGEN_FOUND: TRUE iff Eigen is found.
37# EIGEN_INCLUDE_DIRS: Include directories for Eigen.
38#
39# EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
40# EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
41# EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
42# EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
43#
44# The following variables control the behaviour of this module:
45#
46# EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
47#                          search for eigen includes, e.g: /timbuktu/eigen3.
48#
49# The following variables are also defined by this module, but in line with
50# CMake recommended FindPackage() module style should NOT be referenced directly
51# by callers (use the plural variables detailed above instead).  These variables
52# do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
53# are NOT re-called (i.e. search for library is not repeated) if these variables
54# are set with valid values _in the CMake cache_. This means that if these
55# variables are set directly in the cache, either by the user in the CMake GUI,
56# or by the user passing -DVAR=VALUE directives to CMake when called (which
57# explicitly defines a cache variable), then they will be used verbatim,
58# bypassing the HINTS variables and other hard-coded search locations.
59#
60# EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
61#                    include directory of any dependencies.
62
63# Called if we failed to find Eigen or any of it's required dependencies,
64# unsets all public (designed to be used externally) variables and reports
65# error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
66MACRO(EIGEN_REPORT_NOT_FOUND REASON_MSG)
67  UNSET(EIGEN_FOUND)
68  UNSET(EIGEN_INCLUDE_DIRS)
69  # Make results of search visible in the CMake GUI if Eigen has not
70  # been found so that user does not have to toggle to advanced view.
71  MARK_AS_ADVANCED(CLEAR EIGEN_INCLUDE_DIR)
72  # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
73  # use the camelcase library name, not uppercase.
74  IF (Eigen_FIND_QUIETLY)
75    MESSAGE(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
76  ELSEIF (Eigen_FIND_REQUIRED)
77    MESSAGE(FATAL_ERROR "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
78  ELSE()
79    # Neither QUIETLY nor REQUIRED, use no priority which emits a message
80    # but continues configuration and allows generation.
81    MESSAGE("-- Failed to find Eigen - " ${REASON_MSG} ${ARGN})
82  ENDIF ()
83ENDMACRO(EIGEN_REPORT_NOT_FOUND)
84
85# Search user-installed locations first, so that we prefer user installs
86# to system installs where both exist.
87#
88# TODO: Add standard Windows search locations for Eigen.
89LIST(APPEND EIGEN_CHECK_INCLUDE_DIRS
90  /usr/local/include/eigen3
91  /usr/local/homebrew/include/eigen3 # Mac OS X
92  /opt/local/var/macports/software/eigen3 # Mac OS X.
93  /opt/local/include/eigen3
94  /usr/include/eigen3)
95
96# Search supplied hint directories first if supplied.
97FIND_PATH(EIGEN_INCLUDE_DIR
98  NAMES Eigen/Core
99  PATHS ${EIGEN_INCLUDE_DIR_HINTS}
100  ${EIGEN_CHECK_INCLUDE_DIRS})
101IF (NOT EIGEN_INCLUDE_DIR OR
102    NOT EXISTS ${EIGEN_INCLUDE_DIR})
103  EIGEN_REPORT_NOT_FOUND(
104    "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
105    "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
106ENDIF (NOT EIGEN_INCLUDE_DIR OR
107       NOT EXISTS ${EIGEN_INCLUDE_DIR})
108
109# Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
110# if called.
111SET(EIGEN_FOUND TRUE)
112
113# Extract Eigen version from Eigen/src/Core/util/Macros.h
114IF (EIGEN_INCLUDE_DIR)
115  SET(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
116  IF (NOT EXISTS ${EIGEN_VERSION_FILE})
117    EIGEN_REPORT_NOT_FOUND(
118      "Could not find file: ${EIGEN_VERSION_FILE} "
119      "containing version information in Eigen install located at: "
120      "${EIGEN_INCLUDE_DIR}.")
121  ELSE (NOT EXISTS ${EIGEN_VERSION_FILE})
122    FILE(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
123
124    STRING(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
125      EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
126    STRING(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
127      EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
128
129    STRING(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
130      EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
131    STRING(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
132      EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
133
134    STRING(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
135      EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
136    STRING(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
137      EIGEN_MINOR_VERSION "${EIGEN_MINOR_VERSION}")
138
139    # This is on a single line s/t CMake does not interpret it as a list of
140    # elements and insert ';' separators which would result in 3.;2.;0 nonsense.
141    SET(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
142  ENDIF (NOT EXISTS ${EIGEN_VERSION_FILE})
143ENDIF (EIGEN_INCLUDE_DIR)
144
145# Set standard CMake FindPackage variables if found.
146IF (EIGEN_FOUND)
147  SET(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
148ENDIF (EIGEN_FOUND)
149
150# Handle REQUIRED / QUIET optional arguments and version.
151INCLUDE(FindPackageHandleStandardArgs)
152FIND_PACKAGE_HANDLE_STANDARD_ARGS(Eigen
153  REQUIRED_VARS EIGEN_INCLUDE_DIRS
154  VERSION_VAR EIGEN_VERSION)
155
156# Only mark internal variables as advanced if we found Eigen, otherwise
157# leave it visible in the standard GUI for the user to set manually.
158IF (EIGEN_FOUND)
159  MARK_AS_ADVANCED(FORCE EIGEN_INCLUDE_DIR)
160ENDIF (EIGEN_FOUND)
161