Optionally use exported Eigen CMake configuration if available.
- Updates FindEigen.cmake to default to an installed Eigen CMake
Configuration if available, otherwise falls back to previous behaviour
of searching for Eigen components.
- This mimics the behaviour of FindGflags.cmake & FindGlog.cmake.
Change-Id: Ifce948d554a0135ce1a0c443267c0230e516f14b
diff --git a/cmake/CeresConfig.cmake.in b/cmake/CeresConfig.cmake.in
index c225058..14ab342 100644
--- a/cmake/CeresConfig.cmake.in
+++ b/cmake/CeresConfig.cmake.in
@@ -203,8 +203,14 @@
# Eigen.
# Flag set during configuration and build of Ceres.
set(CERES_EIGEN_VERSION @EIGEN_VERSION@)
+set(EIGEN_WAS_BUILT_WITH_CMAKE @FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION@)
# Append the locations of Eigen when Ceres was built to the search path hints.
-list(APPEND EIGEN_INCLUDE_DIR_HINTS @EIGEN_INCLUDE_DIR@)
+if (EIGEN_WAS_BUILT_WITH_CMAKE)
+ set(Eigen3_DIR @Eigen3_DIR@)
+ set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
+else()
+ list(APPEND EIGEN_INCLUDE_DIR_HINTS @EIGEN_INCLUDE_DIR@)
+endif()
# Search quietly to control the timing of the error message if not found. The
# search should be for an exact match, but for usability reasons do a soft
# match and reject with an explanation below.
diff --git a/cmake/FindEigen.cmake b/cmake/FindEigen.cmake
index cdeb305..af141ef 100644
--- a/cmake/FindEigen.cmake
+++ b/cmake/FindEigen.cmake
@@ -35,14 +35,25 @@
#
# EIGEN_FOUND: TRUE iff Eigen is found.
# EIGEN_INCLUDE_DIRS: Include directories for Eigen.
-#
# EIGEN_VERSION: Extracted from Eigen/src/Core/util/Macros.h
# EIGEN_WORLD_VERSION: Equal to 3 if EIGEN_VERSION = 3.2.0
# EIGEN_MAJOR_VERSION: Equal to 2 if EIGEN_VERSION = 3.2.0
# EIGEN_MINOR_VERSION: Equal to 0 if EIGEN_VERSION = 3.2.0
+# FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION: True iff the version of Eigen
+# found was built & installed /
+# exported as a CMake package.
#
# The following variables control the behaviour of this module:
#
+# EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION: TRUE/FALSE, iff TRUE then
+# then prefer using an exported CMake configuration
+# generated by Eigen over searching for the
+# Eigen components manually. Otherwise (FALSE)
+# ignore any exported Eigen CMake configurations and
+# always perform a manual search for the components.
+# Default: TRUE iff user does not define this variable
+# before we are called, and does NOT specify
+# EIGEN_INCLUDE_DIR_HINTS, otherwise FALSE.
# EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
# search for eigen includes, e.g: /timbuktu/eigen3.
#
@@ -66,6 +77,7 @@
macro(EIGEN_REPORT_NOT_FOUND REASON_MSG)
unset(EIGEN_FOUND)
unset(EIGEN_INCLUDE_DIRS)
+ unset(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION)
# Make results of search visible in the CMake GUI if Eigen has not
# been found so that user does not have to toggle to advanced view.
mark_as_advanced(CLEAR EIGEN_INCLUDE_DIR)
@@ -89,38 +101,68 @@
# here to fail.
unset(EIGEN_FOUND)
-# Search user-installed locations first, so that we prefer user installs
-# to system installs where both exist.
-list(APPEND EIGEN_CHECK_INCLUDE_DIRS
- /usr/local/include
- /usr/local/homebrew/include # Mac OS X
- /opt/local/var/macports/software # Mac OS X.
- /opt/local/include
- /usr/include)
-# Additional suffixes to try appending to each search path.
-list(APPEND EIGEN_CHECK_PATH_SUFFIXES
- eigen3 # Default root directory for Eigen.
- Eigen/include/eigen3 # Windows (for C:/Program Files prefix) < 3.3
- Eigen3/include/eigen3 ) # Windows (for C:/Program Files prefix) >= 3.3
+# -----------------------------------------------------------------
+# By default, if the user has expressed no preference for using an exported
+# Eigen CMake configuration over performing a search for the installed
+# components, and has not specified any hints for the search locations, then
+# prefer an exported configuration if available.
+if (NOT DEFINED EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION
+ AND NOT EIGEN_INCLUDE_DIR_HINTS)
+ message(STATUS "No preference for use of exported Eigen CMake configuration "
+ "set, and no hints for include directory provided. "
+ "Defaulting to preferring an installed/exported Eigen CMake configuration "
+ "if available.")
+ set(EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION TRUE)
+endif()
-# Search supplied hint directories first if supplied.
-find_path(EIGEN_INCLUDE_DIR
- NAMES Eigen/Core
- PATHS ${EIGEN_INCLUDE_DIR_HINTS}
- ${EIGEN_CHECK_INCLUDE_DIRS}
- PATH_SUFFIXES ${EIGEN_CHECK_PATH_SUFFIXES})
+if (EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION)
+ find_package(Eigen3 QUIET)
+ if (EIGEN3_FOUND)
+ message(STATUS "Found installed version of Eigen: ${Eigen3_DIR}")
+ set(FOUND_INSTALLED_EIGEN_CMAKE_CONFIGURATION TRUE)
+ set(EIGEN_FOUND ${EIGEN3_FOUND})
+ set(EIGEN_INCLUDE_DIR "${EIGEN3_INCLUDE_DIR}" CACHE STRING
+ "Eigen include directory" FORCE)
+ else()
+ message(STATUS "Failed to find an installed/exported CMake configuration "
+ "for Eigen, will perform search for installed Eigen components.")
+ endif()
+endif()
-if (NOT EIGEN_INCLUDE_DIR OR
+if (NOT EIGEN_FOUND)
+ # Search user-installed locations first, so that we prefer user installs
+ # to system installs where both exist.
+ list(APPEND EIGEN_CHECK_INCLUDE_DIRS
+ /usr/local/include
+ /usr/local/homebrew/include # Mac OS X
+ /opt/local/var/macports/software # Mac OS X.
+ /opt/local/include
+ /usr/include)
+ # Additional suffixes to try appending to each search path.
+ list(APPEND EIGEN_CHECK_PATH_SUFFIXES
+ eigen3 # Default root directory for Eigen.
+ Eigen/include/eigen3 # Windows (for C:/Program Files prefix) < 3.3
+ Eigen3/include/eigen3 ) # Windows (for C:/Program Files prefix) >= 3.3
+
+ # Search supplied hint directories first if supplied.
+ find_path(EIGEN_INCLUDE_DIR
+ NAMES Eigen/Core
+ PATHS ${EIGEN_INCLUDE_DIR_HINTS}
+ ${EIGEN_CHECK_INCLUDE_DIRS}
+ PATH_SUFFIXES ${EIGEN_CHECK_PATH_SUFFIXES})
+
+ if (NOT EIGEN_INCLUDE_DIR OR
+ NOT EXISTS ${EIGEN_INCLUDE_DIR})
+ eigen_report_not_found(
+ "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
+ "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
+ endif (NOT EIGEN_INCLUDE_DIR OR
NOT EXISTS ${EIGEN_INCLUDE_DIR})
- eigen_report_not_found(
- "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
- "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
-endif (NOT EIGEN_INCLUDE_DIR OR
- NOT EXISTS ${EIGEN_INCLUDE_DIR})
-# Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
-# if called.
-set(EIGEN_FOUND TRUE)
+ # Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
+ # if called.
+ set(EIGEN_FOUND TRUE)
+endif()
# Extract Eigen version from Eigen/src/Core/util/Macros.h
if (EIGEN_INCLUDE_DIR)