HandleEigen.cmake 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. ###############################################################################
  2. # Option for using system Eigen or GTSAM-bundled Eigen
  3. option(GTSAM_USE_SYSTEM_EIGEN "Find and use system-installed Eigen. If 'off', use the one bundled with GTSAM" OFF)
  4. if(NOT GTSAM_USE_SYSTEM_EIGEN)
  5. # This option only makes sense if using the embedded copy of Eigen, it is
  6. # used to decide whether to *install* the "unsupported" module:
  7. option(GTSAM_WITH_EIGEN_UNSUPPORTED "Install Eigen's unsupported modules" OFF)
  8. endif()
  9. # Switch for using system Eigen or GTSAM-bundled Eigen
  10. if(GTSAM_USE_SYSTEM_EIGEN)
  11. find_package(Eigen3 REQUIRED)
  12. # Use generic Eigen include paths e.g. <Eigen/Core>
  13. set(GTSAM_EIGEN_INCLUDE_FOR_INSTALL "${EIGEN3_INCLUDE_DIR}")
  14. # check if MKL is also enabled - can have one or the other, but not both!
  15. # Note: Eigen >= v3.2.5 includes our patches
  16. if(EIGEN_USE_MKL_ALL AND (EIGEN3_VERSION VERSION_LESS 3.2.5))
  17. message(FATAL_ERROR "MKL requires at least Eigen 3.2.5, and your system appears to have an older version. Disable GTSAM_USE_SYSTEM_EIGEN to use GTSAM's copy of Eigen, or disable GTSAM_WITH_EIGEN_MKL")
  18. endif()
  19. # Check for Eigen version which doesn't work with MKL
  20. # See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1527 for details.
  21. if(EIGEN_USE_MKL_ALL AND (EIGEN3_VERSION VERSION_EQUAL 3.3.4))
  22. message(FATAL_ERROR "MKL does not work with Eigen 3.3.4 because of a bug in Eigen. See http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1527. Disable GTSAM_USE_SYSTEM_EIGEN to use GTSAM's copy of Eigen, disable GTSAM_WITH_EIGEN_MKL, or upgrade/patch your installation of Eigen.")
  23. endif()
  24. # The actual include directory (for BUILD cmake target interface):
  25. set(GTSAM_EIGEN_INCLUDE_FOR_BUILD "${EIGEN3_INCLUDE_DIR}")
  26. else()
  27. # Use bundled Eigen include path.
  28. # Clear any variables set by FindEigen3
  29. if(EIGEN3_INCLUDE_DIR)
  30. set(EIGEN3_INCLUDE_DIR NOTFOUND CACHE STRING "" FORCE)
  31. endif()
  32. # set full path to be used by external projects
  33. # this will be added to GTSAM_INCLUDE_DIR by gtsam_extra.cmake.in
  34. set(GTSAM_EIGEN_INCLUDE_FOR_INSTALL "include/gtsam/3rdparty/Eigen/")
  35. # The actual include directory (for BUILD cmake target interface):
  36. set(GTSAM_EIGEN_INCLUDE_FOR_BUILD "${GTSAM_SOURCE_DIR}/gtsam/3rdparty/Eigen/")
  37. endif()
  38. # Detect Eigen version:
  39. set(EIGEN_VER_H "${GTSAM_EIGEN_INCLUDE_FOR_BUILD}/Eigen/src/Core/util/Macros.h")
  40. if (EXISTS ${EIGEN_VER_H})
  41. file(READ "${EIGEN_VER_H}" STR_EIGEN_VERSION)
  42. # Extract the Eigen version from the Macros.h file, lines "#define EIGEN_WORLD_VERSION XX", etc...
  43. string(REGEX MATCH "EIGEN_WORLD_VERSION[ ]+[0-9]+" GTSAM_EIGEN_VERSION_WORLD "${STR_EIGEN_VERSION}")
  44. string(REGEX MATCH "[0-9]+" GTSAM_EIGEN_VERSION_WORLD "${GTSAM_EIGEN_VERSION_WORLD}")
  45. string(REGEX MATCH "EIGEN_MAJOR_VERSION[ ]+[0-9]+" GTSAM_EIGEN_VERSION_MAJOR "${STR_EIGEN_VERSION}")
  46. string(REGEX MATCH "[0-9]+" GTSAM_EIGEN_VERSION_MAJOR "${GTSAM_EIGEN_VERSION_MAJOR}")
  47. string(REGEX MATCH "EIGEN_MINOR_VERSION[ ]+[0-9]+" GTSAM_EIGEN_VERSION_MINOR "${STR_EIGEN_VERSION}")
  48. string(REGEX MATCH "[0-9]+" GTSAM_EIGEN_VERSION_MINOR "${GTSAM_EIGEN_VERSION_MINOR}")
  49. set(GTSAM_EIGEN_VERSION "${GTSAM_EIGEN_VERSION_WORLD}.${GTSAM_EIGEN_VERSION_MAJOR}.${GTSAM_EIGEN_VERSION_MINOR}")
  50. message(STATUS "Found Eigen version: ${GTSAM_EIGEN_VERSION}")
  51. else()
  52. message(WARNING "Cannot determine Eigen version, missing file: `${EIGEN_VER_H}`")
  53. endif ()
  54. if (MSVC)
  55. if (BUILD_SHARED_LIBS)
  56. # mute eigen static assert to avoid errors in shared lib
  57. list_append_cache(GTSAM_COMPILE_DEFINITIONS_PUBLIC EIGEN_NO_STATIC_ASSERT)
  58. endif()
  59. list_append_cache(GTSAM_COMPILE_OPTIONS_PRIVATE "/wd4244") # Disable loss of precision which is thrown all over our Eigen
  60. endif()