FindEigen.cmake 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. # FindEigen.cmake - Find Eigen library, version >= 3.
  32. #
  33. # This module defines the following variables:
  34. #
  35. # EIGEN_FOUND: TRUE iff Eigen is found.
  36. # EIGEN_INCLUDE_DIRS: Include directories for Eigen.
  37. #
  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. #
  43. # The following variables control the behaviour of this module:
  44. #
  45. # EIGEN_INCLUDE_DIR_HINTS: List of additional directories in which to
  46. # search for eigen includes, e.g: /timbuktu/eigen3.
  47. #
  48. # The following variables are also defined by this module, but in line with
  49. # CMake recommended FindPackage() module style should NOT be referenced directly
  50. # by callers (use the plural variables detailed above instead). These variables
  51. # do however affect the behaviour of the module via FIND_[PATH/LIBRARY]() which
  52. # are NOT re-called (i.e. search for library is not repeated) if these variables
  53. # are set with valid values _in the CMake cache_. This means that if these
  54. # variables are set directly in the cache, either by the user in the CMake GUI,
  55. # or by the user passing -DVAR=VALUE directives to CMake when called (which
  56. # explicitly defines a cache variable), then they will be used verbatim,
  57. # bypassing the HINTS variables and other hard-coded search locations.
  58. #
  59. # EIGEN_INCLUDE_DIR: Include directory for CXSparse, not including the
  60. # include directory of any dependencies.
  61. # Called if we failed to find Eigen or any of it's required dependencies,
  62. # unsets all public (designed to be used externally) variables and reports
  63. # error message at priority depending upon [REQUIRED/QUIET/<NONE>] argument.
  64. MACRO(EIGEN_REPORT_NOT_FOUND REASON_MSG)
  65. UNSET(EIGEN_FOUND)
  66. UNSET(EIGEN_INCLUDE_DIRS)
  67. # Make results of search visible in the CMake GUI if Eigen has not
  68. # been found so that user does not have to toggle to advanced view.
  69. MARK_AS_ADVANCED(CLEAR EIGEN_INCLUDE_DIR)
  70. # Note <package>_FIND_[REQUIRED/QUIETLY] variables defined by FindPackage()
  71. # use the camelcase library name, not uppercase.
  72. IF (Eigen_FIND_QUIETLY)
  73. MESSAGE(STATUS "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
  74. ELSEIF (Eigen_FIND_REQUIRED)
  75. MESSAGE(FATAL_ERROR "Failed to find Eigen - " ${REASON_MSG} ${ARGN})
  76. ELSE()
  77. # Neither QUIETLY nor REQUIRED, use no priority which emits a message
  78. # but continues configuration and allows generation.
  79. MESSAGE("-- Failed to find Eigen - " ${REASON_MSG} ${ARGN})
  80. ENDIF ()
  81. ENDMACRO(EIGEN_REPORT_NOT_FOUND)
  82. # Search user-installed locations first, so that we prefer user installs
  83. # to system installs where both exist.
  84. #
  85. # TODO: Add standard Windows search locations for Eigen.
  86. LIST(APPEND EIGEN_CHECK_INCLUDE_DIRS
  87. /usr/local/include/eigen3
  88. /usr/local/homebrew/include/eigen3 # Mac OS X
  89. /opt/local/var/macports/software/eigen3 # Mac OS X.
  90. /opt/local/include/eigen3
  91. /usr/include/eigen3)
  92. # Search supplied hint directories first if supplied.
  93. FIND_PATH(EIGEN_INCLUDE_DIR
  94. NAMES Eigen/Core
  95. PATHS ${EIGEN_INCLUDE_DIR_HINTS}
  96. ${EIGEN_CHECK_INCLUDE_DIRS})
  97. IF (NOT EIGEN_INCLUDE_DIR OR
  98. NOT EXISTS ${EIGEN_INCLUDE_DIR})
  99. EIGEN_REPORT_NOT_FOUND(
  100. "Could not find eigen3 include directory, set EIGEN_INCLUDE_DIR to "
  101. "path to eigen3 include directory, e.g. /usr/local/include/eigen3.")
  102. ENDIF (NOT EIGEN_INCLUDE_DIR OR
  103. NOT EXISTS ${EIGEN_INCLUDE_DIR})
  104. # Mark internally as found, then verify. EIGEN_REPORT_NOT_FOUND() unsets
  105. # if called.
  106. SET(EIGEN_FOUND TRUE)
  107. # Extract Eigen version from Eigen/src/Core/util/Macros.h
  108. IF (EIGEN_INCLUDE_DIR)
  109. SET(EIGEN_VERSION_FILE ${EIGEN_INCLUDE_DIR}/Eigen/src/Core/util/Macros.h)
  110. IF (NOT EXISTS ${EIGEN_VERSION_FILE})
  111. EIGEN_REPORT_NOT_FOUND(
  112. "Could not find file: ${EIGEN_VERSION_FILE} "
  113. "containing version information in Eigen install located at: "
  114. "${EIGEN_INCLUDE_DIR}.")
  115. ELSE (NOT EXISTS ${EIGEN_VERSION_FILE})
  116. FILE(READ ${EIGEN_VERSION_FILE} EIGEN_VERSION_FILE_CONTENTS)
  117. STRING(REGEX MATCH "#define EIGEN_WORLD_VERSION [0-9]+"
  118. EIGEN_WORLD_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
  119. STRING(REGEX REPLACE "#define EIGEN_WORLD_VERSION ([0-9]+)" "\\1"
  120. EIGEN_WORLD_VERSION "${EIGEN_WORLD_VERSION}")
  121. STRING(REGEX MATCH "#define EIGEN_MAJOR_VERSION [0-9]+"
  122. EIGEN_MAJOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
  123. STRING(REGEX REPLACE "#define EIGEN_MAJOR_VERSION ([0-9]+)" "\\1"
  124. EIGEN_MAJOR_VERSION "${EIGEN_MAJOR_VERSION}")
  125. STRING(REGEX MATCH "#define EIGEN_MINOR_VERSION [0-9]+"
  126. EIGEN_MINOR_VERSION "${EIGEN_VERSION_FILE_CONTENTS}")
  127. STRING(REGEX REPLACE "#define EIGEN_MINOR_VERSION ([0-9]+)" "\\1"
  128. EIGEN_MINOR_VERSION "${EIGEN_MINOR_VERSION}")
  129. # This is on a single line s/t CMake does not interpret it as a list of
  130. # elements and insert ';' separators which would result in 3.;2.;0 nonsense.
  131. SET(EIGEN_VERSION "${EIGEN_WORLD_VERSION}.${EIGEN_MAJOR_VERSION}.${EIGEN_MINOR_VERSION}")
  132. ENDIF (NOT EXISTS ${EIGEN_VERSION_FILE})
  133. ENDIF (EIGEN_INCLUDE_DIR)
  134. # Set standard CMake FindPackage variables if found.
  135. IF (EIGEN_FOUND)
  136. SET(EIGEN_INCLUDE_DIRS ${EIGEN_INCLUDE_DIR})
  137. ENDIF (EIGEN_FOUND)
  138. # Handle REQUIRED / QUIET optional arguments and version.
  139. INCLUDE(FindPackageHandleStandardArgs)
  140. FIND_PACKAGE_HANDLE_STANDARD_ARGS(Eigen
  141. REQUIRED_VARS EIGEN_INCLUDE_DIRS
  142. VERSION_VAR EIGEN_VERSION)
  143. # Only mark internal variables as advanced if we found Eigen, otherwise
  144. # leave it visible in the standard GUI for the user to set manually.
  145. IF (EIGEN_FOUND)
  146. MARK_AS_ADVANCED(FORCE EIGEN_INCLUDE_DIR)
  147. ENDIF (EIGEN_FOUND)