From 9591cfb0b8dcf2aa213c8a44ac2c8b7eefa95342 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Thu, 16 Nov 2023 00:54:47 -0500 Subject: [PATCH] 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 * Update pybind11NewTools.cmake --------- Signed-off-by: Henry Schreiner --- tools/pybind11Common.cmake | 11 +++++++++-- tools/pybind11NewTools.cmake | 31 +++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index f921970b6a..50b15e11b6 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -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) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index 9106762fc2..4b69486311 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -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)