Skip to content

Commit ffb113d

Browse files
authored
fix: regression with installed pybind11 overriding local one (#2716)
* fix: regression with installed pybind11 overriding discovered one Closes #2709 * docs: wording incorrect
1 parent 499fcd5 commit ffb113d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

tools/pybind11NewTools.cmake

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,20 @@ endif()
127127
# Check on every access - since Python2 and Python3 could have been used - do nothing in that case.
128128

129129
if(DEFINED ${_Python}_INCLUDE_DIRS)
130+
# Only add Python for build - must be added during the import for config
131+
# since it has to be re-discovered.
132+
#
133+
# This needs to be a target to be included after the local pybind11
134+
# directory, just in case there there is an installed pybind11 sitting
135+
# next to Python's includes. It also ensures Python is a SYSTEM library.
136+
add_library(pybind11::python_headers INTERFACE IMPORTED)
137+
set_property(
138+
TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
139+
"$<BUILD_INTERFACE:${${_Python}_INCLUDE_DIRS}>")
130140
set_property(
131141
TARGET pybind11::pybind11
132142
APPEND
133-
PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${${_Python}_INCLUDE_DIRS}>)
143+
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
134144
set(pybind11_INCLUDE_DIRS
135145
"${pybind11_INCLUDE_DIR}" "${${_Python}_INCLUDE_DIRS}"
136146
CACHE INTERNAL "Directories where pybind11 and possibly Python headers are located")

tools/pybind11Tools.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,19 @@ if(PYBIND11_MASTER_PROJECT)
8181
endif()
8282
endif()
8383

84-
# Only add Python for build - must be added during the import for config since it has to be re-discovered.
84+
# Only add Python for build - must be added during the import for config since
85+
# it has to be re-discovered.
86+
#
87+
# This needs to be an target to it is included after the local pybind11
88+
# directory, just in case there are multiple versions of pybind11, we want the
89+
# one we expect.
90+
add_library(pybind11::python_headers INTERFACE IMPORTED)
91+
set_property(TARGET pybind11::python_headers PROPERTY INTERFACE_INCLUDE_DIRECTORIES
92+
"$<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>")
8593
set_property(
8694
TARGET pybind11::pybind11
8795
APPEND
88-
PROPERTY INTERFACE_INCLUDE_DIRECTORIES $<BUILD_INTERFACE:${PYTHON_INCLUDE_DIRS}>)
96+
PROPERTY INTERFACE_LINK_LIBRARIES pybind11::python_headers)
8997

9098
set(pybind11_INCLUDE_DIRS
9199
"${pybind11_INCLUDE_DIR}" "${PYTHON_INCLUDE_DIRS}"

0 commit comments

Comments
 (0)