@@ -32,6 +32,13 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
3232 set (Python_ROOT_DIR "$ENV{pythonLocation} " )
3333 endif ()
3434
35+ # Interpreter should not be found when cross-compiling
36+ if (_PYBIND11_CROSSCOMPILING)
37+ set (_pybind11_interp_component "" )
38+ else ()
39+ set (_pybind11_interp_component Interpreter)
40+ endif ()
41+
3542 # Development.Module support (required for manylinux) started in 3.18
3643 if (CMAKE_VERSION VERSION_LESS 3.18)
3744 set (_pybind11_dev_component Development)
@@ -48,8 +55,9 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
4855 endif ()
4956 endif ()
5057
51- find_package (Python 3.6 REQUIRED COMPONENTS Interpreter ${_pybind11_dev_component}
52- ${_pybind11_quiet} ${_pybind11_global_keyword} )
58+ find_package (
59+ Python 3.6 REQUIRED COMPONENTS ${_pybind11_interp_component} ${_pybind11_dev_component}
60+ ${_pybind11_quiet} ${_pybind11_global_keyword} )
5361
5462 # If we are in submodule mode, export the Python targets to global targets.
5563 # If this behavior is not desired, FindPython _before_ pybind11.
@@ -59,7 +67,9 @@ if(NOT Python_FOUND AND NOT Python3_FOUND)
5967 if (TARGET Python::Python)
6068 set_property (TARGET Python::Python PROPERTY IMPORTED_GLOBAL TRUE )
6169 endif ()
62- set_property (TARGET Python::Interpreter PROPERTY IMPORTED_GLOBAL TRUE )
70+ if (TARGET Python::Interpreter)
71+ set_property (TARGET Python::Interpreter PROPERTY IMPORTED_GLOBAL TRUE )
72+ endif ()
6373 if (TARGET Python::Module)
6474 set_property (TARGET Python::Module PROPERTY IMPORTED_GLOBAL TRUE )
6575 endif ()
@@ -100,69 +110,89 @@ if(PYBIND11_MASTER_PROJECT)
100110 endif ()
101111endif ()
102112
103- # If a user finds Python, they may forget to include the Interpreter component
104- # and the following two steps require it. It is highly recommended by CMake
105- # when finding development libraries anyway, so we will require it.
106- if (NOT DEFINED ${_Python} _EXECUTABLE)
107- message (
108- FATAL_ERROR
109- "${_Python} was found without the Interpreter component. Pybind11 requires this component." )
110-
111- endif ()
112-
113- if (DEFINED PYBIND11_PYTHON_EXECUTABLE_LAST AND NOT ${_Python} _EXECUTABLE STREQUAL
114- PYBIND11_PYTHON_EXECUTABLE_LAST)
115- # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
116- unset (PYTHON_IS_DEBUG CACHE )
117- unset (PYTHON_MODULE_EXTENSION CACHE )
118- endif ()
119-
120- set (PYBIND11_PYTHON_EXECUTABLE_LAST
121- "${${_Python} _EXECUTABLE}"
122- CACHE INTERNAL "Python executable during the last CMake run" )
123-
124- if (NOT DEFINED PYTHON_IS_DEBUG)
125- # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
126- execute_process (
127- COMMAND "${${_Python} _EXECUTABLE}" "-c"
128- "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
129- RESULT_VARIABLE _PYTHON_IS_DEBUG)
130- set (PYTHON_IS_DEBUG
131- "${_PYTHON_IS_DEBUG} "
132- CACHE INTERNAL "Python debug status" )
133- endif ()
134-
135- # Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
136- # required for PyPy3 (as of 7.3.1)
137- if (NOT DEFINED PYTHON_MODULE_EXTENSION OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
138- execute_process (
139- COMMAND
140- "${${_Python} _EXECUTABLE}" "-c"
141- "import sys, importlib; s = importlib.import_module('distutils.sysconfig' if sys.version_info < (3, 10) else 'sysconfig'); print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
142- OUTPUT_VARIABLE _PYTHON_MODULE_EXT_SUFFIX
143- ERROR_VARIABLE _PYTHON_MODULE_EXT_SUFFIX_ERR
144- OUTPUT_STRIP_TRAILING_WHITESPACE)
145-
146- if (_PYTHON_MODULE_EXT_SUFFIX STREQUAL "" )
113+ if (NOT _PYBIND11_CROSSCOMPILING)
114+ # If a user finds Python, they may forget to include the Interpreter component
115+ # and the following two steps require it. It is highly recommended by CMake
116+ # when finding development libraries anyway, so we will require it.
117+ if (NOT DEFINED ${_Python} _EXECUTABLE)
147118 message (
148- FATAL_ERROR "pybind11 could not query the module file extension, likely the 'distutils'"
149- "package is not installed. Full error message: \n ${_PYTHON_MODULE_EXT_SUFFIX_ERR} "
119+ FATAL_ERROR
120+ " ${_Python} was found without the Interpreter component. Pybind11 requires this component. "
150121 )
122+
151123 endif ()
152124
153- # This needs to be available for the pybind11_extension function
154- if (NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
155- get_filename_component (_PYTHON_MODULE_DEBUG_POSTFIX "${_PYTHON_MODULE_EXT_SUFFIX} " NAME_WE )
156- set (PYTHON_MODULE_DEBUG_POSTFIX
157- "${_PYTHON_MODULE_DEBUG_POSTFIX} "
158- CACHE INTERNAL "" )
125+ if (DEFINED PYBIND11_PYTHON_EXECUTABLE_LAST AND NOT ${_Python} _EXECUTABLE STREQUAL
126+ PYBIND11_PYTHON_EXECUTABLE_LAST)
127+ # Detect changes to the Python version/binary in subsequent CMake runs, and refresh config if needed
128+ unset (PYTHON_IS_DEBUG CACHE )
129+ unset (PYTHON_MODULE_EXTENSION CACHE )
159130 endif ()
160131
161- if (NOT DEFINED PYTHON_MODULE_EXTENSION)
162- get_filename_component (_PYTHON_MODULE_EXTENSION "${_PYTHON_MODULE_EXT_SUFFIX} " EXT )
163- set (PYTHON_MODULE_EXTENSION
164- "${_PYTHON_MODULE_EXTENSION} "
165- CACHE INTERNAL "" )
132+ set (PYBIND11_PYTHON_EXECUTABLE_LAST
133+ "${${_Python} _EXECUTABLE}"
134+ CACHE INTERNAL "Python executable during the last CMake run" )
135+
136+ if (NOT DEFINED PYTHON_IS_DEBUG)
137+ # Debug check - see https://stackoverflow.com/questions/646518/python-how-to-detect-debug-Interpreter
138+ execute_process (
139+ COMMAND "${${_Python} _EXECUTABLE}" "-c"
140+ "import sys; sys.exit(hasattr(sys, 'gettotalrefcount'))"
141+ RESULT_VARIABLE _PYTHON_IS_DEBUG)
142+ set (PYTHON_IS_DEBUG
143+ "${_PYTHON_IS_DEBUG} "
144+ CACHE INTERNAL "Python debug status" )
145+ endif ()
146+
147+ # Get the suffix - SO is deprecated, should use EXT_SUFFIX, but this is
148+ # required for PyPy3 (as of 7.3.1)
149+ if (NOT DEFINED PYTHON_MODULE_EXTENSION OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
150+ execute_process (
151+ COMMAND
152+ "${${_Python} _EXECUTABLE}" "-c"
153+ "import sys, importlib; s = importlib.import_module('distutils.sysconfig' if sys.version_info < (3, 10) else 'sysconfig'); print(s.get_config_var('EXT_SUFFIX') or s.get_config_var('SO'))"
154+ OUTPUT_VARIABLE _PYTHON_MODULE_EXT_SUFFIX
155+ ERROR_VARIABLE _PYTHON_MODULE_EXT_SUFFIX_ERR
156+ OUTPUT_STRIP_TRAILING_WHITESPACE)
157+
158+ if (_PYTHON_MODULE_EXT_SUFFIX STREQUAL "" )
159+ message (
160+ FATAL_ERROR
161+ "pybind11 could not query the module file extension, likely the 'distutils'"
162+ "package is not installed. Full error message:\n ${_PYTHON_MODULE_EXT_SUFFIX_ERR} " )
163+ endif ()
164+
165+ # This needs to be available for the pybind11_extension function
166+ if (NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
167+ get_filename_component (_PYTHON_MODULE_DEBUG_POSTFIX "${_PYTHON_MODULE_EXT_SUFFIX} " NAME_WE )
168+ set (PYTHON_MODULE_DEBUG_POSTFIX
169+ "${_PYTHON_MODULE_DEBUG_POSTFIX} "
170+ CACHE INTERNAL "" )
171+ endif ()
172+
173+ if (NOT DEFINED PYTHON_MODULE_EXTENSION)
174+ get_filename_component (_PYTHON_MODULE_EXTENSION "${_PYTHON_MODULE_EXT_SUFFIX} " EXT )
175+ set (PYTHON_MODULE_EXTENSION
176+ "${_PYTHON_MODULE_EXTENSION} "
177+ CACHE INTERNAL "" )
178+ endif ()
179+ endif ()
180+ else ()
181+ if (NOT DEFINED PYTHON_IS_DEBUG
182+ OR NOT DEFINED PYTHON_MODULE_EXTENSION
183+ OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
184+ include ("${CMAKE_CURRENT_LIST_DIR} /pybind11GuessPythonExtSuffix.cmake" )
185+ pybind11_guess_python_module_extension("${_Python} " )
186+ endif ()
187+ # When cross-compiling, we cannot query the Python interpreter, so we require
188+ # the user to set these variables explicitly.
189+ if (NOT DEFINED PYTHON_IS_DEBUG
190+ OR NOT DEFINED PYTHON_MODULE_EXTENSION
191+ OR NOT DEFINED PYTHON_MODULE_DEBUG_POSTFIX )
192+ message (
193+ FATAL_ERROR
194+ "When cross-compiling, you should set the PYTHON_IS_DEBUG, PYTHON_MODULE_EXTENSION and PYTHON_MODULE_DEBUG_POSTFIX \
195+ variables appropriately before loading pybind11 (e.g. in your CMake toolchain file)" )
166196 endif ()
167197endif ()
168198
0 commit comments