FindPython: locating Development.Module and SABIModule without Interpreter broken on Windows
Hi, I'm having issues using FindPython on Windows. When searching for both the Development.Module
and Development.SABIModule
components (without the Interpreter
component), FindPython fails. When searching for just one of the two, it works correctly. Please find a minimal example below.
cmake_minimum_required(VERSION 3.26)
project(find-python)
option(WITH_SABI
"Locate Development.SABIModule component in addition to Development.Module" On)
if (WITH_SABI)
# Broken
find_package(Python 3.8 REQUIRED COMPONENTS Development.Module
OPTIONAL_COMPONENTS Development.SABIModule)
else()
# Works
find_package(Python 3.8 REQUIRED COMPONENTS Development.Module)
endif()
CMake output ... (click to open)
$ cmake --version
cmake version 3.31.0
$ cmake -S . -B build -D WITH_SABI=On
-- Building for: Visual Studio 17 2022
-- The C compiler identification is MSVC 19.42.34433.0
-- The CXX compiler identification is MSVC 19.42.34433.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Tools/MSVC/14.42.34433/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Error at C:/Program Files/CMake/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:233 (message):
Could NOT find Python (missing: Python_INCLUDE_DIRS Development.Module)
(Required is at least version "3.8")
Call Stack (most recent call first):
C:/Program Files/CMake/share/cmake-3.31/Modules/FindPackageHandleStandardArgs.cmake:603 (_FPHSA_FAILURE_MESSAGE)
C:/Program Files/CMake/share/cmake-3.31/Modules/FindPython/Support.cmake:4001 (find_package_handle_standard_args)
C:/Program Files/CMake/share/cmake-3.31/Modules/FindPython.cmake:631 (include)
CMakeLists.txt:8 (find_package)
-- Configuring incomplete, errors occurred!
You can find the full output of this test project here: https://github.com/tttapa/cmake-find-python-windows-bug/actions/runs/12118393059/job/33782784883.
Adding the Interpreter
component gets around the issue, but this is not feasible when cross-compiling, which is where the problem originally came up. (Although it is not limited to cross-compilation, as demonstrated above.)
Manually setting hints/artifacts such as Python_ROOT_DIR
and Python_INCLUDE_DIR
does not seem to help.
I've had a quick look at FindPython/Support.cmake
to see what's going on, and it seems like the Python_VERSION_MAJOR
variable is empty, causing the check on line 3783 to fail, so the Python_Development.{,SABI}Module_FOUND
variables are never set, but it is unclear to me what the root cause is.