@@ -45,8 +45,13 @@ if(NOT pybind11_FIND_QUIETLY)
45
45
message (STATUS "pybind11 v${pybind11_VERSION} ${pybind11_VERSION_TYPE} " )
46
46
endif ()
47
47
48
+ # Avoid infinite recursion if tests include this as a subdirectory
49
+ if (DEFINED PYBIND11_MASTER_PROJECT)
50
+ set (PYBIND11_TEST OFF )
51
+ endif ()
52
+
48
53
# Check if pybind11 is being used directly or via add_subdirectory
49
- if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR )
54
+ if (CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR AND NOT DEFINED PYBIND11_MASTER_PROJECT )
50
55
### Warn if not an out-of-source builds
51
56
if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR )
52
57
set (lines
@@ -161,12 +166,24 @@ endif()
161
166
# You can also place ifs *in* the Config.in, but not here.
162
167
163
168
# This section builds targets, but does *not* touch Python
164
-
165
- # Build the headers-only target (no Python included):
166
- # (long name used here to keep this from clashing in subdirectory mode)
167
- add_library (pybind11_headers INTERFACE )
168
- add_library (pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target
169
- add_library (pybind11::headers ALIAS pybind11_headers) # easier to use/remember
169
+ # Non-IMPORT targets cannot be defined twice
170
+ if (NOT TARGET pybind11_headers)
171
+ # Build the headers-only target (no Python included):
172
+ # (long name used here to keep this from clashing in subdirectory mode)
173
+ add_library (pybind11_headers INTERFACE )
174
+ add_library (pybind11::pybind11_headers ALIAS pybind11_headers) # to match exported target
175
+ add_library (pybind11::headers ALIAS pybind11_headers) # easier to use/remember
176
+
177
+ target_include_directories (
178
+ pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${pybind11_INCLUDE_DIR} >
179
+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
180
+
181
+ target_compile_features (pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals
182
+ cxx_right_angle_brackets)
183
+ else ()
184
+ # It is invalid to install a target twice, too.
185
+ set (PYBIND11_INSTALL OFF )
186
+ endif ()
170
187
171
188
include ("${CMAKE_CURRENT_SOURCE_DIR} /tools/pybind11Common.cmake" )
172
189
@@ -177,14 +194,6 @@ elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
177
194
file (RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS} )
178
195
endif ()
179
196
180
- # Fill in headers target
181
- target_include_directories (
182
- pybind11_headers ${pybind11_system} INTERFACE $<BUILD_INTERFACE:${pybind11_INCLUDE_DIR} >
183
- $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >)
184
-
185
- target_compile_features (pybind11_headers INTERFACE cxx_inheriting_constructors cxx_user_literals
186
- cxx_right_angle_brackets)
187
-
188
197
if (PYBIND11_INSTALL)
189
198
install (DIRECTORY ${pybind11_INCLUDE_DIR} /pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
190
199
set (PYBIND11_CMAKECONFIG_INSTALL_DIR
0 commit comments