diff --git a/cmake_modules/FindEigen.cmake b/cmake_modules/FindEigen.cmake index 5d8ecdac63aa87c1b43e37f64d10702884e876b6..d44ea8903d4c5a72af468d603dc1d8e5a6bbf542 100644 --- a/cmake_modules/FindEigen.cmake +++ b/cmake_modules/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) @@ -83,39 +95,122 @@ macro(EIGEN_REPORT_NOT_FOUND REASON_MSG) return() endmacro(EIGEN_REPORT_NOT_FOUND) -# Search user-installed locations first, so that we prefer user installs -# to system installs where both exist. -# -# TODO: Add standard Windows search locations for Eigen. -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). - -# 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 +# Protect against any alternative find_package scripts for this library having +# been called previously (in a client project) which set EIGEN_FOUND, but not +# the other variables we require / set here which could cause the search logic +# here to fail. +unset(EIGEN_FOUND) + +# ----------------------------------------------------------------- +# 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() + +if (EIGEN_PREFER_EXPORTED_EIGEN_CMAKE_CONFIGURATION) + # Try to find an exported CMake configuration for Eigen. + # + # We search twice, s/t we can invert the ordering of precedence used by + # find_package() for exported package build directories, and installed + # packages (found via CMAKE_SYSTEM_PREFIX_PATH), listed as items 6) and 7) + # respectively in [1]. + # + # By default, exported build directories are (in theory) detected first, and + # this is usually the case on Windows. However, on OS X & Linux, the install + # path (/usr/local) is typically present in the PATH environment variable + # which is checked in item 4) in [1] (i.e. before both of the above, unless + # NO_SYSTEM_ENVIRONMENT_PATH is passed). As such on those OSs installed + # packages are usually detected in preference to exported package build + # directories. + # + # To ensure a more consistent response across all OSs, and as users usually + # want to prefer an installed version of a package over a locally built one + # where both exist (esp. as the exported build directory might be removed + # after installation), we first search with NO_CMAKE_PACKAGE_REGISTRY which + # means any build directories exported by the user are ignored, and thus + # installed directories are preferred. If this fails to find the package + # we then research again, but without NO_CMAKE_PACKAGE_REGISTRY, so any + # exported build directories will now be detected. + # + # To prevent confusion on Windows, we also pass NO_CMAKE_BUILDS_PATH (which + # is item 5) in [1]), to not preferentially use projects that were built + # recently with the CMake GUI to ensure that we always prefer an installed + # version if available. + # + # [1] http://www.cmake.org/cmake/help/v2.8.11/cmake.html#command:find_package + find_package(Eigen3 QUIET + NO_MODULE + NO_CMAKE_PACKAGE_REGISTRY + NO_CMAKE_BUILDS_PATH) + if (EIGEN3_FOUND) + message(STATUS "Found installed version of Eigen: ${Eigen3_DIR}") + else() + # Failed to find an installed version of Eigen, repeat search allowing + # exported build directories. + message(STATUS "Failed to find installed Eigen CMake configuration, " + "searching for Eigen build directories exported with CMake.") + # Again pass NO_CMAKE_BUILDS_PATH, as we know that Eigen is exported and + # do not want to treat projects built with the CMake GUI preferentially. + find_package(Eigen3 QUIET + NO_MODULE + NO_CMAKE_BUILDS_PATH) + if (EIGEN3_FOUND) + message(STATUS "Found exported Eigen build directory: ${Eigen3_DIR}") + endif() + endif() + if (EIGEN3_FOUND) + 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_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 + HINTS ${EIGEN_INCLUDE_DIR_HINTS} + PATHS ${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) @@ -163,5 +258,6 @@ find_package_handle_standard_args(Eigen # Only mark internal variables as advanced if we found Eigen, otherwise # leave it visible in the standard GUI for the user to set manually. if (EIGEN_FOUND) - mark_as_advanced(FORCE EIGEN_INCLUDE_DIR) -endif (EIGEN_FOUND) + mark_as_advanced(FORCE EIGEN_INCLUDE_DIR + Eigen3_DIR) # Autogenerated by find_package(Eigen3) +endif (EIGEN_FOUND) \ No newline at end of file diff --git a/cmake_modules/wolfConfig.cmake b/cmake_modules/wolfConfig.cmake index 877b55b41d3b648c87d563945c8fad81668ba1d1..4e15cc1d44b19135e96a1c67dd0f8e187982734f 100644 --- a/cmake_modules/wolfConfig.cmake +++ b/cmake_modules/wolfConfig.cmake @@ -143,6 +143,7 @@ list(APPEND wolf_INCLUDE_DIRS ${wolf_INCLUDE_DIR}) set(wolf_VERSION 0.0.1) # Eigen. + # Flag set during configuration and build of wolf. set(wolf_EIGEN_VERSION @EIGEN_VERSION@) # Append the locations of Eigen when wolf was built to the search path hints. @@ -150,8 +151,12 @@ list(APPEND EIGEN_INCLUDE_DIR_HINTS /usr/include/eigen3) # 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. -find_package(Eigen ${wolf_EIGEN_VERSION} QUIET) -if (EIGEN_FOUND) + +find_package(Eigen3 ${wolf_EIGEN_VERSION} QUIET) + +# Flag set with currently found Eigen version. +set(EIGEN_VERSION @EIGEN_VERSION@) +if (EIGEN3_FOUND) if (NOT EIGEN_VERSION VERSION_EQUAL wolf_EIGEN_VERSION) # CMake's VERSION check in FIND_PACKAGE() will accept any version >= the # specified version. However, only version = is supported. Improve @@ -164,13 +169,13 @@ if (EIGEN_FOUND) "for more details") endif () message(STATUS "Found required wolf dependency: " - "Eigen version ${wolf_EIGEN_VERSION} in ${EIGEN_INCLUDE_DIRS}") -else (EIGEN_FOUND) + "Eigen version ${wolf_EIGEN_VERSION} in ${EIGEN3_INCLUDE_DIR}") +else (EIGEN3_FOUND) wolf_report_not_found("Missing required wolf " "dependency: Eigen version ${wolf_EIGEN_VERSION}, please set " - "EIGEN_INCLUDE_DIR.") -endif (EIGEN_FOUND) -list(APPEND wolf_INCLUDE_DIRS ${EIGEN_INCLUDE_DIRS}) + "EIGEN3_INCLUDE_DIR.") +endif (EIGEN3_FOUND) +list(APPEND wolf_INCLUDE_DIRS ${EIGEN3_INCLUDE_DIR}) # Import exported wolf targets, if they have not already been imported. if (NOT TARGET wolf AND NOT wolf_BINARY_DIR) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 152595422b16a653d70bfa7600d6fe0a62b305b0..19929c49c1de4ca08e32feaee6dc89f49ffaa8ce 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -29,7 +29,7 @@ ENDIF(BUILD_EXAMPLES OR BUILD_TESTS) #find dependencies. -FIND_PACKAGE(Eigen 3.2.92 REQUIRED) +FIND_PACKAGE(Eigen3 3.2.92 REQUIRED) FIND_PACKAGE(Threads REQUIRED) @@ -144,7 +144,7 @@ ELSE (SPDLOG_INCLUDE_DIR) MESSAGE(FATAL_ERROR "Could not find spdlog") ENDIF (SPDLOG_INCLUDE_DIR) -INCLUDE_DIRECTORIES(${EIGEN_INCLUDE_DIRS}) +INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIRS}) IF(Ceres_FOUND) INCLUDE_DIRECTORIES(${CERES_INCLUDE_DIRS})