Skip to content

Commit 9591cfb

Browse files
authored
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 <[email protected]> * Update pybind11NewTools.cmake --------- Signed-off-by: Henry Schreiner <[email protected]>
1 parent 6831666 commit 9591cfb

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

tools/pybind11Common.cmake

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,15 @@ if(NOT _pybind11_nopython)
218218

219219
execute_process(
220220
COMMAND
221-
${${_Python}_EXECUTABLE} -c
222-
"from pkg_resources import get_distribution; print(get_distribution('${PYPI_NAME}').version)"
221+
${${_Python}_EXECUTABLE} -c "
222+
try:
223+
from importlib.metadata import version
224+
except ImportError:
225+
from pkg_resources import get_distribution
226+
def version(s):
227+
return get_distribution(s).version
228+
print(version('${PYPI_NAME}'))
229+
"
223230
RESULT_VARIABLE RESULT_PRESENT
224231
OUTPUT_VARIABLE PKG_VERSION
225232
ERROR_QUIET)

tools/pybind11NewTools.cmake

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,23 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
3939
set(_pybind11_dev_component Development.Module OPTIONAL_COMPONENTS Development.Embed)
4040
endif()
4141

42+
# Callers need to be able to access Python_EXECUTABLE
43+
set(_pybind11_global_keyword "")
44+
if(NOT is_config AND NOT DEFINED Python_ARTIFACTS_INTERACTIVE)
45+
set(Python_ARTIFACTS_INTERACTIVE TRUE)
46+
if(NOT CMAKE_VERSION VERSION_LESS 3.24)
47+
set(_pybind11_global_keyword "GLOBAL")
48+
endif()
49+
endif()
50+
4251
find_package(Python 3.6 REQUIRED COMPONENTS Interpreter ${_pybind11_dev_component}
43-
${_pybind11_quiet})
52+
${_pybind11_quiet} ${_pybind11_global_keyword})
4453

4554
# If we are in submodule mode, export the Python targets to global targets.
4655
# If this behavior is not desired, FindPython _before_ pybind11.
47-
if(NOT is_config)
56+
if(NOT is_config
57+
AND NOT Python_ARTIFACTS_INTERACTIVE
58+
AND _pybind11_global_keyword STREQUAL "")
4859
if(TARGET Python::Python)
4960
set_property(TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE)
5061
endif()
@@ -53,6 +64,22 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
5364
set_property(TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE)
5465
endif()
5566
endif()
67+
68+
# Explicitly export version for callers (including our own functions)
69+
if(NOT is_config AND Python_ARTIFACTS_INTERACTIVE)
70+
set(Python_VERSION
71+
"${Python_VERSION}"
72+
CACHE INTERNAL "")
73+
set(Python_VERSION_MAJOR
74+
"${Python_VERSION_MAJOR}"
75+
CACHE INTERNAL "")
76+
set(Python_VERSION_MINOR
77+
"${Python_VERSION_MINOR}"
78+
CACHE INTERNAL "")
79+
set(Python_VERSION_PATCH
80+
"${Python_VERSION_PATCH}"
81+
CACHE INTERNAL "")
82+
endif()
5683
endif()
5784

5885
if(Python_FOUND)

0 commit comments

Comments
 (0)