Skip to content

Commit de7ab58

Browse files
Use FetchContent_Populate for Eigen to avoid target collision
Eigen's unsupported/test/ creates a custom target named "autodiff" which collides with the real autodiff library when both are added via FetchContent_MakeAvailable. Since Eigen is header-only, use FetchContent_Populate to download without add_subdirectory, then create the Eigen3::Eigen target manually.
1 parent 8c0a7b3 commit de7ab58

1 file changed

Lines changed: 14 additions & 15 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,21 @@ if(NOT Eigen3_FOUND)
7272
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
7373
GIT_TAG 3.4.0
7474
)
75-
set(BUILD_TESTING OFF CACHE BOOL "" FORCE)
76-
set(EIGEN_BUILD_DOC OFF CACHE BOOL "" FORCE)
77-
set(EIGEN_BUILD_TESTING OFF CACHE BOOL "" FORCE)
78-
FetchContent_MakeAvailable(eigen)
79-
80-
# Point Eigen3_DIR to the build directory where Eigen3Config.cmake is generated
81-
# so that downstream find_package(Eigen3) calls (e.g., from autodiff) can find it
82-
set(Eigen3_DIR "${eigen_BINARY_DIR}" CACHE PATH "Path to Eigen3Config.cmake" FORCE)
83-
84-
# Ensure Eigen3::Eigen target exists for direct usage
85-
if(NOT TARGET Eigen3::Eigen)
86-
add_library(Eigen3::Eigen INTERFACE IMPORTED)
87-
set_target_properties(Eigen3::Eigen PROPERTIES
88-
INTERFACE_INCLUDE_DIRECTORIES "${eigen_SOURCE_DIR}"
89-
)
75+
# Use FetchContent_Populate (not MakeAvailable) to download Eigen without
76+
# adding it as a subdirectory. Eigen is header-only so no build step is
77+
# needed, and add_subdirectory pulls in unsupported/test which creates a
78+
# custom target called "autodiff" that collides with the real autodiff lib.
79+
FetchContent_GetProperties(eigen)
80+
if(NOT eigen_POPULATED)
81+
FetchContent_Populate(eigen)
9082
endif()
83+
84+
# Create the canonical Eigen3::Eigen target for our project and autodiff
85+
add_library(Eigen3::Eigen INTERFACE IMPORTED)
86+
set_target_properties(Eigen3::Eigen PROPERTIES
87+
INTERFACE_INCLUDE_DIRECTORIES "${eigen_SOURCE_DIR}"
88+
)
89+
set(Eigen3_FOUND TRUE CACHE BOOL "" FORCE)
9190
endif()
9291

9392
if (CDDP_CPP_CASADI)

0 commit comments

Comments
 (0)