fix(cmake): unset stale PYTHON_MODULE_DEBUG_POSTFIX and correct USE_PYTHON_INCLUDE_DIR variable (#6086)
When the Python executable changes between CMake runs,
PYTHON_MODULE_DEBUG_POSTFIX was not being unset alongside PYTHON_IS_DEBUG
and PYTHON_MODULE_EXTENSION, allowing a stale postfix to survive interpreter
changes.
In the USE_PYTHON_INCLUDE_DIR block, the elseif branch checked
PYTHON_INCLUDE_DIR (singular) but expanded PYTHON_INCLUDE_DIRS (plural),
always silently resolving to an empty string. Correct it to expand
${PYTHON_INCLUDE_DIR}. Also add a Python3_INCLUDE_DIRS branch so the
option works when pybind11NewTools resolves Python via the Python3
find-package component.
Assisted-by: ClaudeCode:claude-fable-5diff --git a/CMakeLists.txt b/CMakeLists.txt
index bca0c22..3abb3dc 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -304,8 +304,10 @@
# Relative directory setting
if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS)
file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS})
+elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED Python3_INCLUDE_DIRS)
+ file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python3_INCLUDE_DIRS})
elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR)
- file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS})
+ file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIR})
endif()
if(PYBIND11_INSTALL)
diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake
index 5ab3142..568b0b0 100644
--- a/tools/pybind11NewTools.cmake
+++ b/tools/pybind11NewTools.cmake
@@ -112,6 +112,7 @@
# Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
unset(PYTHON_IS_DEBUG CACHE)
unset(PYTHON_MODULE_EXTENSION CACHE)
+ unset(PYTHON_MODULE_DEBUG_POSTFIX CACHE)
endif()
set(PYBIND11_PYTHON_EXECUTABLE_LAST