Skip to content

Commit d46f332

Browse files
authored
fix: allow cmake varibles from Python calls to be cached (#2570)
1 parent fecef38 commit d46f332

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

tools/pybind11NewTools.cmake

+28-20
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,38 @@ if(NOT DEFINED ${_Python}_EXECUTABLE)
8080

8181
endif()
8282

83-
# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
84-
execute_process(
85-
COMMAND "${${_Python}_EXECUTABLE}" "-c" "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
86-
RESULT_VARIABLE PYTHON_IS_DEBUG)
83+
if(NOT DEFINED PYTHON_IS_DEBUG)
84+
# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
85+
execute_process(
86+
COMMAND "${${_Python}_EXECUTABLE}" "-c"
87+
"import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
88+
RESULT_VARIABLE _PYTHON_IS_DEBUG)
89+
set(PYTHON_IS_DEBUG
90+
"${_PYTHON_IS_DEBUG}"
91+
CACHE INTERNAL "Python debug status")
92+
endif()
8793

8894
# Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
8995
# required for PyPy3 (as of 7.3.1)
90-
execute_process(
91-
COMMAND "${${_Python}_EXECUTABLE}" "-c"
92-
"from distutils import sysconfig; print(sysconfig.get_config_var('SO'))"
93-
OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
94-
ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
95-
OUTPUT_STRIP_TRAILING_WHITESPACE)
96-
97-
if(_PYTHON_MODULE_EXTENSION STREQUAL "")
98-
message(
99-
FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
100-
"package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}")
101-
endif()
96+
if(NOT DEFINED PYTHON_MODULE_EXTENSION)
97+
execute_process(
98+
COMMAND "${${_Python}_EXECUTABLE}" "-c"
99+
"from distutils import sysconfig; print(sysconfig.get_config_var('SO'))"
100+
OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION
101+
ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR
102+
OUTPUT_STRIP_TRAILING_WHITESPACE)
103+
104+
if(_PYTHON_MODULE_EXTENSION STREQUAL "")
105+
message(
106+
FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
107+
"package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}")
108+
endif()
102109

103-
# This needs to be available for the pybind11_extension function
104-
set(PYTHON_MODULE_EXTENSION
105-
"${_PYTHON_MODULE_EXTENSION}"
106-
CACHE INTERNAL "")
110+
# This needs to be available for the pybind11_extension function
111+
set(PYTHON_MODULE_EXTENSION
112+
"${_PYTHON_MODULE_EXTENSION}"
113+
CACHE INTERNAL "")
114+
endif()
107115

108116
# Python debug libraries expose slightly different objects before 3.8
109117
# https://docs.python.org/3.6/c-api/intro.html#debugging-builds

0 commit comments

Comments
 (0)