Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / cmake / FindEigen.cmake
1 # Ceres Solver - A fast non-linear least squares minimizer
2 # Copyright 2015 Google Inc. All rights reserved.
3 # http://ceres-solver.org/
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 # EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
39 # EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
40 # EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
41 # EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
42 # FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION: True iff the version of Eigen
43 #                                            found was built & installed /
44 #                                            exported as a CMake package.
45 #
46 # The following variables control the behaviour of this module:
47 #
48 # EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
49 #                           then prefer using an exported CMake configuration
50 #                           generated by Eigen over searching for the
51 #                           Eigen components manually.  Otherwise (FALSE)
52 #                           ignore any exported Eigen CMake configurations and
53 #                           always perform a manual search for the components.
54 #                           Default: TRUE iff user does not define this variable
55 #                           before we are called, and does NOT specify
56 #                           EIGEN_INCLUDE_DIR_HINTS, otherwise FALSE.
57 # EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
58 #                          search for eigen includes, e.g: /timbuktu/eigen3.
59 #
60 # The following variables are also defined by this module, but in line with
61 # CMake recommended FindPackage() module style should NOT be referenced directly
62 # by callers (use the plural variables detailed above instead).  These variables
63 # do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
64 # are NOT re-called (i.e. search for library is not repeated) if these variables
65 # are set with valid values _in the CMake cache_. This means that if these
66 # variables are set directly in the cache, either by the user in the CMake GUI,
67 # or by the user passing -DVAR=VALUE directives to CMake when called (which
68 # explicitly defines a cache variable), then they will be used verbatim,
69 # bypassing the HINTS variables and other hard-coded search locations.
70 #
71 # EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
72 #                    include directory of any dependencies.
73
74 # Called if we failed to find Eigen or any of it's required dependencies,
75 # unsets all public (designed to be used externally) variables and reports
76 # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
77 macro(EIGEN_REPORT_NOT_FOUND REASON_MSG)
78   unset(EIGEN_FOUND)
79   unset(EIGEN_INCLUDE_DIRS)
80   unset(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION)
81   # Make results of search visible in the CMake GUI if Eigen has not
82   # been found so that user does not have to toggle to advanced view.
83   mark_as_advanced(CLEAR EIGEN_INCLUDE_DIR)
84   # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
85   # use the camelcase library name, not uppercase.
86   if (Eigen_FIND_QUIETLY)
87     message(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
88   elseif (Eigen_FIND_REQUIRED)
89     message(FATAL_ERROR "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
90   else()
91     # Neither QUIETLY nor REQUIRED, use no priority which emits a message
92     # but continues configuration and allows generation.
93     message("-- Failed to find Eigen - " ${REASON_MSG} ${ARGN})
94   endif ()
95   return()
96 endmacro(EIGEN_REPORT_NOT_FOUND)
97
98 # Protect against any alternative find_package scripts for this library having
99 # been called previously (in a client project) which set EIGEN_FOUND, but not
100 # the other variables we require / set here which could cause the search logic
101 # here to fail.
102 unset(EIGEN_FOUND)
103
104 # -----------------------------------------------------------------
105 # By default, if the user has expressed no preference for using an exported
106 # Eigen CMake configuration over performing a search for the installed
107 # components, and has not specified any hints for the search locations, then
108 # prefer an exported configuration if available.
109 if (NOT DEFINED EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION
110     AND NOT EIGEN_INCLUDE_DIR_HINTS)
111   message(STATUS "No preference for use of exported Eigen CMake configuration "
112     "set, and no hints for include directory provided. "
113     "Defaulting to preferring an installed/exported Eigen CMake configuration "
114     "if available.")
115   set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
116 endif()
117
118 if (EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION)
119   # Try to find an exported CMake configuration for Eigen.
120   #
121   # We search twice, s/t we can invert the ordering of precedence used by
122   # find_package() for exported package build directories, and installed
123   # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7)
124   # respectively in [1].
125   #
126   # By default, exported build directories are (in theory) detected first, and
127   # this is usually the case on Windows.  However, on OS X & Linux, the install
128   # path (/usr/local) is typically present in the PATH environment variable
129   # which is checked in item 4) in [1] (i.e. before both of the above, unless
130   # NO_SYSTEM_ENVIRONMENT_PATH is passed).  As such on those OSs installed
131   # packages are usually detected in preference to exported package build
132   # directories.
133   #
134   # To ensure a more consistent response across all OSs, and as users usually
135   # want to prefer an installed version of a package over a locally built one
136   # where both exist (esp. as the exported build directory might be removed
137   # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which
138   # means any build directories exported by the user are ignored, and thus
139   # installed directories are preferred.  If this fails to find the package
140   # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any
141   # exported build directories will now be detected.
142   #
143   # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which
144   # is item 5) in [1]), to not preferentially use projects that were built
145   # recently with the CMake GUI to ensure that we always prefer an installed
146   # version if available.
147   #
148   # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package
149   find_package(Eigen3 QUIET
150                       NO_MODULE
151                       NO_CMAKE_PACKAGE_REGISTRY
152                       NO_CMAKE_BUILDS_PATH)
153   if (EIGEN3_FOUND)
154     message(STATUS "Found installed version of Eigen: ${Eigen3_DIR}")
155   else()
156     # Failed to find an installed version of Eigen, repeat search allowing
157     # exported build directories.
158     message(STATUS "Failed to find installed Eigen CMake configuration, "
159       "searching for Eigen build directories exported with CMake.")
160     # Again pass NO_CMAKE_BUILDS_PATH, as we know that Eigen is exported and
161     # do not want to treat projects built with the CMake GUI preferentially.
162     find_package(Eigen3 QUIET
163                         NO_MODULE
164                         NO_CMAKE_BUILDS_PATH)
165     if (EIGEN3_FOUND)
166       message(STATUS "Found exported Eigen build directory: ${Eigen3_DIR}")
167     endif()
168   endif()
169   if (EIGEN3_FOUND)
170     set(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION TRUE)
171     set(EIGEN_FOUND ${EIGEN3_FOUND})
172     set(EIGEN_INCLUDE_DIR "${EIGEN3_INCLUDE_DIR}" CACHE STRING
173       "Eigen include directory" FORCE)
174   else()
175     message(STATUS "Failed to find an installed/exported CMake configuration "
176       "for Eigen, will perform search for installed Eigen components.")
177   endif()
178 endif()
179
180 if (NOT EIGEN_FOUND)
181   # Search user-installed locations first, so that we prefer user installs
182   # to system installs where both exist.
183   list(APPEND EIGEN_CHECK_INCLUDE_DIRS
184     /usr/local/include
185     /usr/local/homebrew/include # Mac OS X
186     /opt/local/var/macports/software # Mac OS X.
187     /opt/local/include
188     /usr/include)
189   # Additional suffixes to try appending to each search path.
190   list(APPEND EIGEN_CHECK_PATH_SUFFIXES
191     eigen3 # Default root directory for Eigen.
192     Eigen/include/eigen3 # Windows (for C:/Program Files prefix) < 3.3
193     Eigen3/include/eigen3 ) # Windows (for C:/Program Files prefix) >= 3.3
194
195   # Search supplied hint directories first if supplied.
196   find_path(EIGEN_INCLUDE_DIR
197     NAMES Eigen/Core
198     HINTS ${EIGEN_INCLUDE_DIR_HINTS}
199     PATHS ${EIGEN_CHECK_INCLUDE_DIRS}
200     PATH_SUFFIXES ${EIGEN_CHECK_PATH_SUFFIXES})
201
202   if (NOT EIGEN_INCLUDE_DIR OR
203       NOT EXISTS ${EIGEN_INCLUDE_DIR})
204     eigen_report_not_found(
205       "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
206       "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
207   endif (NOT EIGEN_INCLUDE_DIR OR
208     NOT EXISTS ${EIGEN_INCLUDE_DIR})
209
210   # Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
211   # if called.
212   set(EIGEN_FOUND TRUE)
213 endif()
214
215 # Extract Eigen version from Eigen/src/Core/util/Macros.h
216 if (EIGEN_INCLUDE_DIR)
217   set(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
218   if (NOT EXISTS ${EIGEN_VERSION_FILE})
219     eigen_report_not_found(
220       "Could not find file: ${EIGEN_VERSION_FILE} "
221       "containing version information in Eigen install located at: "
222       "${EIGEN_INCLUDE_DIR}.")
223   else (NOT EXISTS ${EIGEN_VERSION_FILE})
224     file(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
225
226     string(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
227       EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
228     string(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
229       EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
230
231     string(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
232       EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
233     string(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
234       EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
235
236     string(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
237       EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
238     string(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
239       EIGEN_MINOR_VERSION "${EIGEN_MINOR_VERSION}")
240
241     # This is on a single line s/t CMake does not interpret it as a list of
242     # elements and insert ';' separators which would result in 3.;2.;0 nonsense.
243     set(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
244   endif (NOT EXISTS ${EIGEN_VERSION_FILE})
245 endif (EIGEN_INCLUDE_DIR)
246
247 # Set standard CMake FindPackage variables if found.
248 if (EIGEN_FOUND)
249   set(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
250 endif (EIGEN_FOUND)
251
252 # Handle REQUIRED / QUIET optional arguments and version.
253 include(FindPackageHandleStandardArgs)
254 find_package_handle_standard_args(Eigen
255   REQUIRED_VARS EIGEN_INCLUDE_DIRS
256   VERSION_VAR EIGEN_VERSION)
257
258 # Only mark internal variables as advanced if we found Eigen, otherwise
259 # leave it visible in the standard GUI for the user to set manually.
260 if (EIGEN_FOUND)
261   mark_as_advanced(FORCE EIGEN_INCLUDE_DIR
262     Eigen3_DIR) # Autogenerated by find_package(Eigen3)
263 endif (EIGEN_FOUND)