-
Notifications
You must be signed in to change notification settings - Fork 2.2k
CMake: react to python version changes #3299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The new FindPython-based variant of the CMake scripts caches information about the chosen Python version that can become stale. For example, suppose I configure a simple pybind11-based project as follows ``` cmake -S . -B build -GNinja -DPython_ROOT=<path to python 3.8> ``` which will generate `my_extension.cpython-38-x86_64-linux-gnu.so`. A subsequent change to the python version like ``` cmake -S . -B build -GNinja -DPython_ROOT=<path to python 3.9> ``` does not update all necessary build system information. In particular, the compiled file is still called `my_extension.cpython-38-x86_64-linux-gnu.so`. This commit fixes the problem by detecting changes in `Python_EXECUTABLE` and re-running Python as needed. Note that the previous way of detecting Python does not seem to be affected, it always specifies the right suffix.
for more information, see https://pre-commit.ci
Amazing! (the pre-commit fixes :D) |
tools/pybind11NewTools.cmake
Outdated
if(NOT ${_Python}_EXECUTABLE STREQUAL ${_Python}_EXECUTABLE_LAST) | ||
# 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) | ||
set(${_Python}_EXECUTABLE_LAST |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(NOT ${_Python}_EXECUTABLE STREQUAL ${_Python}_EXECUTABLE_LAST) | |
# 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) | |
set(${_Python}_EXECUTABLE_LAST | |
if(NOT ${_Python}_EXECUTABLE STREQUAL PYTHON_EXECUTABLE_LAST) | |
# 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) | |
set(PYTHON_EXECUTABLE_LAST |
If you change from Python2 to Python3, then this should still be regenerated, since these variables are not tied to ${_Python}
. (_PYTHON_EXECUTABLE_LAST
would also be fine as a name)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree with @henryiii, otherwise this looks good.
(Thanks for the feedback, I plan to merge this once CI passes.) |
fd45b47
to
d8cca11
Compare
Thanks, I wondered how this actually managed to pass my (local) testing. Now it makes sense, I'm again removing the |
This is great. May I suggest to use a prefixed name, e.g. PYBIND11_PYTHON_EXECUTABLE_LAST, instead? This makes it easier to identify in bigger trees and avoids collisions. |
Description
The new FindPython-based variant of the CMake scripts caches information
about the chosen Python version that can become stale. For example,
suppose I configure a simple pybind11-based project as follows
which will generate
my_extension.cpython-38-x86_64-linux-gnu.so
.A subsequent change to the python version like
does not update all necessary build system information. In particular,
the compiled file is still called
my_extension.cpython-38-x86_64-linux-gnu.so
.This commit fixes the problem by detecting changes in
Python_EXECUTABLE
and re-running Python as needed.Note that the previous way of detecting Python does not seem to be
affected, it always specifies the right suffix.
Suggested changelog entry: