From d46f3322a45cb907777f568bb1377ab1b9457ad8 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Fri, 9 Oct 2020 17:33:45 -0400 Subject: [PATCH] fix: allow cmake varibles from Python calls to be cached (#2570) --- tools/pybind11NewTools.cmake | 48 +++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/tools/pybind11NewTools.cmake b/tools/pybind11NewTools.cmake index f772356d2d..bc9fa18690 100644 --- a/tools/pybind11NewTools.cmake +++ b/tools/pybind11NewTools.cmake @@ -80,30 +80,38 @@ if(NOT DEFINED ${_Python}_EXECUTABLE) endif() -# Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter -execute_process( - COMMAND "${${_Python}_EXECUTABLE}" "-c" "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))" - RESULT_VARIABLE PYTHON_IS_DEBUG) +if(NOT DEFINED PYTHON_IS_DEBUG) + # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter + execute_process( + COMMAND "${${_Python}_EXECUTABLE}" "-c" + "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))" + RESULT_VARIABLE _PYTHON_IS_DEBUG) + set(PYTHON_IS_DEBUG + "${_PYTHON_IS_DEBUG}" + CACHE INTERNAL "Python debug status") +endif() # Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is # required for PyPy3 (as of 7.3.1) -execute_process( - COMMAND "${${_Python}_EXECUTABLE}" "-c" - "from distutils import sysconfig; print(sysconfig.get_config_var('SO'))" - OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION - ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR - OUTPUT_STRIP_TRAILING_WHITESPACE) - -if(_PYTHON_MODULE_EXTENSION STREQUAL "") - message( - FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'" - "package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}") -endif() +if(NOT DEFINED PYTHON_MODULE_EXTENSION) + execute_process( + COMMAND "${${_Python}_EXECUTABLE}" "-c" + "from distutils import sysconfig; print(sysconfig.get_config_var('SO'))" + OUTPUT_VARIABLE _PYTHON_MODULE_EXTENSION + ERROR_VARIABLE _PYTHON_MODULE_EXTENSION_ERR + OUTPUT_STRIP_TRAILING_WHITESPACE) + + if(_PYTHON_MODULE_EXTENSION STREQUAL "") + message( + FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'" + "package is not installed. Full error message:\n${_PYTHON_MODULE_EXTENSION_ERR}") + endif() -# This needs to be available for the pybind11_extension function -set(PYTHON_MODULE_EXTENSION - "${_PYTHON_MODULE_EXTENSION}" - CACHE INTERNAL "") + # This needs to be available for the pybind11_extension function + set(PYTHON_MODULE_EXTENSION + "${_PYTHON_MODULE_EXTENSION}" + CACHE INTERNAL "") +endif() # Python debug libraries expose slightly different objects before 3.8 # https://docs.python.org/3.6/c-api/intro.html#debugging-builds