Better support for subprojects

Describe the feature you would like to be implemented.

Better support for subprojects

Would such a feature be useful for other users? Why?

With a package manager like CPM, or just using FetchContent or even add_subdirectory, a user will ran into random weird troubles, since Eigen3 hardly relies on default CMAKE variables like BUILD_TESTING. A better support will make it possible, to use eigen3 with a package manager.

All trouble making default CMAKE variables should be moved into a subproject guard.

Any hints on how to implement the requested feature?

See below

Additional resources

Example to fix tests:

  • search and replace all BUILD_TESTING with EIGEN3_BUILD_TESTING
  • set EIGEN3_BUILD_TESTING to BUILD_TESTING, when Eigen3 is not a subproject

Line 470, CMakeLists.txt

option(BUILD_TESTING "Enable creation of tests." OFF)
if(CMAKE_VERSION VERSION_LESS 3.21 AND ${CMAKE_SOURCE_DIR} EQUALS ${CMAKE_CURRENT_SOURCE_DIR})
    set(EIGEN3_BUILD_TESTING ${BUILD_TESTING} CACHE BOOL "Enable creation of eigen tests.")
elseif(${EIGEN3_IS_TOP_LEVEL})
    set(EIGEN3_BUILD_TESTING ${BUILD_TESTING} CACHE BOOL "Enable creation of eigen tests.")
else()
    option(EIGEN3_BUILD_TESTING "Enable creation of eigen tests." OFF)
endif()
Edited by Fabian Keßler