fix(cmake): findpython issues and 3.12 support for pybind11_find_import (#4941)

* fix(cmake): findpython issues and 3.12 support for pybind11_find_import

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>

* Update pybind11NewTools.cmake

---------

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
Henry Schreiner 2023-11-16 00:54:47 -05:00 committed by GitHub
parent 6831666f5c
commit 9591cfb0b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 4 deletions

View File

@ -218,8 +218,15 @@ if(NOT _pybind11_nopython)
execute_process(
COMMAND
${${_Python}_EXECUTABLE} -c
"from pkg_resources import get_distribution; print(get_distribution('${PYPI_NAME}').version)"
${${_Python}_EXECUTABLE} -c "
try:
from importlib.metadata import version
except ImportError:
from pkg_resources import get_distribution
def version(s):
return get_distribution(s).version
print(version('${PYPI_NAME}'))
"
RESULT_VARIABLE RESULT_PRESENT
OUTPUT_VARIABLE PKG_VERSION
ERROR_QUIET)

View File

@ -39,12 +39,23 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
set(_pybind11_dev_component Development.Module OPTIONAL_COMPONENTS Development.Embed)
endif()
# Callers need to be able to access Python_EXECUTABLE
set(_pybind11_global_keyword "")
if(NOT is_config AND NOT DEFINED Python_ARTIFACTS_INTERACTIVE)
set(Python_ARTIFACTS_INTERACTIVE TRUE)
if(NOT CMAKE_VERSION VERSION_LESS 3.24)
set(_pybind11_global_keyword "GLOBAL")
endif()
endif()
find_package(Python 3.6 REQUIRED COMPONENTS Interpreter ${_pybind11_dev_component}
${_pybind11_quiet})
${_pybind11_quiet} ${_pybind11_global_keyword})
# If we are in submodule mode, export the Python targets to global targets.
# If this behavior is not desired, FindPython _before_ pybind11.
if(NOT is_config)
if(NOT is_config
AND NOT Python_ARTIFACTS_INTERACTIVE
AND _pybind11_global_keyword STREQUAL "")
if(TARGET Python::Python)
set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
endif()
@ -53,6 +64,22 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
set_property(TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE)
endif()
endif()
# Explicitly export version for callers (including our own functions)
if(NOT is_config AND Python_ARTIFACTS_INTERACTIVE)
set(Python_VERSION
"${Python_VERSION}"
CACHE INTERNAL "")
set(Python_VERSION_MAJOR
"${Python_VERSION_MAJOR}"
CACHE INTERNAL "")
set(Python_VERSION_MINOR
"${Python_VERSION_MINOR}"
CACHE INTERNAL "")
set(Python_VERSION_PATCH
"${Python_VERSION_PATCH}"
CACHE INTERNAL "")
endif()
endif()
if(Python_FOUND)