diff --git a/CMakeLists.txt b/CMakeLists.txt index 3352734441..35b16a40dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,10 +112,19 @@ set(PYBIND11_INTERNALS_VERSION # when cross-compiling, unless CMAKE_CROSSCOMPILING_EMULATOR is defined. Compute the default # here, before option() caches it, so that a reconfigure keeps it (pybind11Common.cmake applies # the same default for find_package() consumers, where the option does not exist). +# +# Without CMP0190, FindPython cannot query the ABI flags of the interpreter when +# cross-compiling and drops a free-threaded ABI requested with Python_FIND_ABI, so also +# default to ON in that case. set(_pybind11_use_crosscompiling_default OFF) -if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1" AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR) - cmake_policy(GET CMP0190 _pybind11_cmp0190) - if(_pybind11_cmp0190 STREQUAL "NEW") +if(NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR) + set(_pybind11_cmp0190 "OLD") + if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1") + cmake_policy(GET CMP0190 _pybind11_cmp0190) + endif() + string(TOUPPER "${Python_FIND_ABI}" _pybind11_find_abi) + if(_pybind11_cmp0190 STREQUAL "NEW" OR _pybind11_find_abi MATCHES + "^[^;]*;[^;]*;[^;]*;(ON|TRUE|YES|Y|1)$") set(_pybind11_use_crosscompiling_default ON) endif() endif() diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index fb4f898e9c..3e1954df33 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -47,11 +47,18 @@ set(pybind11_INCLUDE_DIRS # project set explicitly (e.g. Emscripten/Pyodide defines an emulator yet still wants it ON). # This applies to find_package() consumers only; the pybind11 project itself computes the same # default for its PYBIND11_USE_CROSSCOMPILING option, which is always defined. -if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1" - AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR - AND NOT DEFINED PYBIND11_USE_CROSSCOMPILING) - cmake_policy(GET CMP0190 _pybind11_cmp0190) - if(_pybind11_cmp0190 STREQUAL "NEW") +# +# Without CMP0190, FindPython still finds the interpreter when cross-compiling, but cannot +# query its ABI flags. It then drops a free-threaded ABI requested with Python_FIND_ABI and +# rejects the "python3.Xt" headers, so also default to ON in that case. +if(NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR AND NOT DEFINED PYBIND11_USE_CROSSCOMPILING) + set(_pybind11_cmp0190 "OLD") + if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1") + cmake_policy(GET CMP0190 _pybind11_cmp0190) + endif() + string(TOUPPER "${Python_FIND_ABI}" _pybind11_find_abi) + if(_pybind11_cmp0190 STREQUAL "NEW" OR _pybind11_find_abi MATCHES + "^[^;]*;[^;]*;[^;]*;(ON|TRUE|YES|Y|1)$") set(PYBIND11_USE_CROSSCOMPILING "ON") endif() endif()