build(cmake): use FindPython3 instead of removed FindPythonInterp

CMake 3.27 enabled policy CMP0148, which deprecates the FindPythonInterp
and FindPythonLibs modules; CMake 4.x removes them entirely. Switch the
root find_package() to the modern FindPython3 module and alias
Python3_EXECUTABLE to the legacy PYTHON_EXECUTABLE variable so the 90+
downstream references across the tree keep working unchanged.

Signed-off-by: Ramon Roche <mrpollo@gmail.com>
This commit is contained in:
Ramon Roche 2026-04-08 20:20:29 -07:00
parent 3ab7895af7
commit c72cfe437e
No known key found for this signature in database
GPG Key ID: 275988FAE5821713

View File

@ -173,13 +173,35 @@ set(config_module_list)
set(config_kernel_list)
# Find Python
find_package(PythonInterp 3)
#
# Force FindPython3 to prefer the interpreter on PATH (and any activated
# virtualenv/conda/pyenv environment) so downstream tooling runs under
# the same Python the developer invoked the build with. On Linux these
# are already the CMake defaults; the settings exist to normalize
# behavior on macOS and Windows:
#
# - FIND_FRAMEWORK=LAST prevents FindPython3 from preferring a macOS
# Python.framework install (e.g. Homebrew's
# /opt/homebrew/Frameworks/Python.framework/...) over a pyenv shim
# or venv on PATH, which would otherwise pick an interpreter that
# lacks PX4's Python deps (kconfiglib, empy, jinja2, etc.).
# - FIND_REGISTRY=NEVER skips the Windows registry lookup for the
# same reason.
# - FIND_STRATEGY and FIND_VIRTUALENV are set explicitly to make the
# intent obvious and insulate us from future default changes.
set(Python3_FIND_STRATEGY LOCATION)
set(Python3_FIND_VIRTUALENV FIRST)
set(Python3_FIND_FRAMEWORK LAST)
set(Python3_FIND_REGISTRY NEVER)
find_package(Python3 COMPONENTS Interpreter)
# We have a custom error message to tell users how to install python3.
if(NOT PYTHONINTERP_FOUND)
if(NOT Python3_Interpreter_FOUND)
message(FATAL_ERROR "Python 3 not found. Please install Python 3:\n"
" Ubuntu: sudo apt install python3 python3-dev python3-pip\n"
" macOS: brew install python")
endif()
# Alias to the legacy variable so downstream CMakeLists keep working.
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
option(PYTHON_COVERAGE "Python code coverage" OFF)
if(PYTHON_COVERAGE)