From 145d4b1e3ba3528300ac41be09ecd624af412ed3 Mon Sep 17 00:00:00 2001 From: Chen Jiali Date: Wed, 11 Sep 2024 20:02:06 +0800 Subject: [PATCH] OpenCVDetectPython.cmake: prefer using FindPython Reason for change: 1. PythonInterp will misdetect Python3 as Python2 in distributions using newer versions of CMake: ``` -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.11.2", minimum required is "2.7") -- Found PythonLibs: /usr/lib/aarch64-linux-gnu/libpython3.11.so (found suitable exact version "3.11.2") -- Found PythonInterp: /usr/bin/python3 (found suitable version "3.11.2", minimum required is "3") -- Found PythonLibs: /usr/lib/aarch64-linux-gnu/libpython3.11.so (found suitable exact version "3.11.2") ``` 2. PythonInterp will be removed soon and unusable: https://cmake.org/cmake/help/latest/module/FindPythonInterp.html, for Debian that is Debian 13 Signed-off-by: Chen Jiali --- cmake/modules/OpenCVDetectPython.cmake | 64 ++++++++++++++++++-------- 1 file changed, 45 insertions(+), 19 deletions(-) diff --git a/cmake/modules/OpenCVDetectPython.cmake b/cmake/modules/OpenCVDetectPython.cmake index c0441c458..5f3db2d89 100644 --- a/cmake/modules/OpenCVDetectPython.cmake +++ b/cmake/modules/OpenCVDetectPython.cmake @@ -29,29 +29,55 @@ function(find_python preferred_version min_version library_env include_dir_env if(NOT ${found}) if(${executable}) set(PYTHON_EXECUTABLE "${${executable}}") + set(Python_EXECUTABLE "${${executable}}") endif() - find_package(PythonInterp "${preferred_version}") - if(NOT PYTHONINTERP_FOUND) - find_package(PythonInterp "${min_version}") - endif() + if(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.12") + find_package(Python "${preferred_version}" COMPONENTS Interpreter) + if(NOT Python_Interpreter_FOUND) + find_package(Python "${min_version}" COMPONENTS Interpreter) + endif() - if(PYTHONINTERP_FOUND) - # Copy outputs - set(_found ${PYTHONINTERP_FOUND}) - set(_executable ${PYTHON_EXECUTABLE}) - set(_version_string ${PYTHON_VERSION_STRING}) - set(_version_major ${PYTHON_VERSION_MAJOR}) - set(_version_minor ${PYTHON_VERSION_MINOR}) - set(_version_patch ${PYTHON_VERSION_PATCH}) + if(Python_Interpreter_FOUND) + # Copy outputs + set(_found ${Python_Interpreter_FOUND}) + set(_executable ${Python_EXECUTABLE}) + set(_version_string ${Python_VERSION}) + set(_version_major ${Python_VERSION_MAJOR}) + set(_version_minor ${Python_VERSION_MINOR}) + set(_version_patch ${Python_VERSION_PATCH}) - # Clear find_host_package side effects - unset(PYTHONINTERP_FOUND) - unset(PYTHON_EXECUTABLE CACHE) - unset(PYTHON_VERSION_STRING) - unset(PYTHON_VERSION_MAJOR) - unset(PYTHON_VERSION_MINOR) - unset(PYTHON_VERSION_PATCH) + # Clear find_host_package side effects + unset(Python_Interpreter_FOUND) + unset(Python_EXECUTABLE CACHE) + unset(Python_VERSION) + unset(Python_VERSION_MAJOR) + unset(Python_VERSION_MINOR) + unset(Python_VERSION_PATCH) + endif() + else() + find_package(PythonInterp "${preferred_version}") + if(NOT PYTHONINTERP_FOUND) + find_package(PythonInterp "${min_version}") + endif() + + if(PYTHONINTERP_FOUND) + # Copy outputs + set(_found ${PYTHONINTERP_FOUND}) + set(_executable ${PYTHON_EXECUTABLE}) + set(_version_string ${PYTHON_VERSION_STRING}) + set(_version_major ${PYTHON_VERSION_MAJOR}) + set(_version_minor ${PYTHON_VERSION_MINOR}) + set(_version_patch ${PYTHON_VERSION_PATCH}) + + # Clear find_host_package side effects + unset(PYTHONINTERP_FOUND) + unset(PYTHON_EXECUTABLE CACHE) + unset(PYTHON_VERSION_STRING) + unset(PYTHON_VERSION_MAJOR) + unset(PYTHON_VERSION_MINOR) + unset(PYTHON_VERSION_PATCH) + endif() endif() if(_found)