@@ -97,6 +97,8 @@ set(PYBIND11_CROSS_MODULE_GIL_TESTS
97
97
test_gil_scoped.py
98
98
)
99
99
100
+ option (DOWNLOAD_EIGEN "Download EIGEN (requires CMake 3.11+)" OFF )
101
+
100
102
# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
101
103
# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
102
104
# skip message).
@@ -105,18 +107,42 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
105
107
# Try loading via newer Eigen's Eigen3Config first (bypassing tools/FindEigen3.cmake).
106
108
# Eigen 3.3.1+ exports a cmake 3.0+ target for handling dependency requirements, but also
107
109
# produces a fatal error if loaded from a pre-3.0 cmake.
108
- if (NOT CMAKE_VERSION VERSION_LESS 3.0 )
109
- find_package (Eigen3 3.2.7 QUIET CONFIG )
110
- if (EIGEN3_FOUND )
111
- if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1 )
112
- set (PYBIND11_EIGEN_VIA_TARGET 1 )
110
+ if (DOWNLOAD_EIGEN )
111
+ if (CMAKE_VERSION VERSION_LESS 3.11 )
112
+ message (FATAL_ERROR "CMake 3.11+ required when using DOWNLOAD_EIGEN" )
113
+ endif ()
114
+
115
+ set (EIGEN3_VERSION_STRING "3.3.7" )
116
+
117
+ include (FetchContent )
118
+ FetchContent_Declare (
119
+ eigen
120
+ GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
121
+ GIT_TAG ${EIGEN3_VERSION_STRING}
122
+ )
123
+
124
+ FetchContent_GetProperties (eigen )
125
+ if (NOT eigen_POPULATED )
126
+ message (STATUS "Downloading Eigen" )
127
+ FetchContent_Populate (eigen )
128
+ endif ()
129
+
130
+ set (EIGEN3_INCLUDE_DIR ${eigen_SOURCE_DIR} )
131
+ set (EIGEN3_FOUND TRUE )
132
+ else ()
133
+ if (NOT CMAKE_VERSION VERSION_LESS 3.0 )
134
+ find_package (Eigen3 3.2.7 QUIET CONFIG )
135
+ if (EIGEN3_FOUND )
136
+ if (EIGEN3_VERSION_STRING AND NOT EIGEN3_VERSION_STRING VERSION_LESS 3.3.1 )
137
+ set (PYBIND11_EIGEN_VIA_TARGET TRUE )
138
+ endif ()
113
139
endif ()
114
140
endif ()
115
- endif ( )
116
- if ( NOT EIGEN3_FOUND )
117
- # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
118
- # tools/FindEigen3.cmake
119
- find_package ( Eigen3 3.2.7 QUIET )
141
+ if ( NOT EIGEN3_FOUND )
142
+ # Couldn't load via target, so fall back to allowing module mode finding, which will pick up
143
+ # tools/FindEigen3.cmake
144
+ find_package ( Eigen3 3.2.7 QUIET )
145
+ endif ( )
120
146
endif ()
121
147
122
148
if (EIGEN3_FOUND )
@@ -129,7 +155,7 @@ if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
129
155
message (STATUS "Building tests with Eigen v${EIGEN3_VERSION} " )
130
156
else ()
131
157
list (REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I} )
132
- message (STATUS "Building tests WITHOUT Eigen" )
158
+ message (STATUS "Building tests WITHOUT Eigen, use -DDOWNLOAD_EIGEN on CMake 3.11+ to download " )
133
159
endif ()
134
160
endif ()
135
161
0 commit comments