FindPython: Add version range support

Fixes: #21107
This commit is contained in:
Marc Chevrier
2020-09-21 19:31:43 +02:00
parent f002c1cfc7
commit e4b7049230
7 changed files with 491 additions and 142 deletions

View File

@@ -10,6 +10,10 @@ FindPython
Find Python interpreter, compiler and development environment (include
directories and libraries).
When a version is requested, it can be specified as a simple value or as a
range. For a detailed description of version range usage and capabilities,
refer to the :command:`find_package` command.
The following components are supported:
* ``Interpreter``: search for Python interpreter.
@@ -387,13 +391,39 @@ module suffix will include the ``Python_SOABI`` value, if any.
#]=======================================================================]
cmake_policy(PUSH)
# numbers and boolean constants
cmake_policy (SET CMP0012 NEW)
set (_PYTHON_PREFIX Python)
unset (_Python_REQUIRED_VERSION_MAJOR)
unset (_Python_REQUIRED_VERSIONS)
if (DEFINED Python_FIND_VERSION)
if (Python_FIND_VERSION_RANGE)
# compute list of major versions
foreach (_Python_MAJOR IN ITEMS 3 2)
if (_Python_MAJOR VERSION_GREATER_EQUAL Python_FIND_VERSION_MIN_MAJOR
AND ((Python_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _Python_MAJOR VERSION_LESS_EQUAL Python_FIND_VERSION_MAX)
OR (Python_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _Python_MAJOR VERSION_LESS Python_FIND_VERSION_MAX)))
list (APPEND _Python_REQUIRED_VERSIONS ${_Python_MAJOR})
endif()
endforeach()
list (LENGTH _Python_REQUIRED_VERSIONS _Python_VERSION_COUNT)
if (_Python_VERSION_COUNT EQUAL 0)
unset (_Python_REQUIRED_VERSIONS)
elseif (_Python_VERSION_COUNT EQUAL 1)
set (_Python_REQUIRED_VERSION_MAJOR ${_Python_REQUIRED_VERSIONS})
endif()
elseif (DEFINED Python_FIND_VERSION)
set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
else()
set (_Python_REQUIRED_VERSIONS 3 2)
endif()
if (_Python_REQUIRED_VERSION_MAJOR)
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
elseif (_Python_REQUIRED_VERSIONS)
# iterate over versions in quiet and NOT required modes to avoid multiple
# "Found" messages and prematurally failure.
set (_Python_QUIETLY ${Python_FIND_QUIETLY})
@@ -401,7 +431,6 @@ else()
set (Python_FIND_QUIETLY TRUE)
set (Python_FIND_REQUIRED FALSE)
set (_Python_REQUIRED_VERSIONS 3 2)
set (_Python_REQUIRED_VERSION_LAST 2)
unset (_Python_INPUT_VARS)
@@ -435,10 +464,21 @@ else()
set (Python_FIND_REQUIRED ${_Python_REQUIRED})
if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
# call again validation command to get "Found" or error message
find_package_handle_standard_args (Python HANDLE_COMPONENTS
find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
REQUIRED_VARS ${_Python_REQUIRED_VARS}
VERSION_VAR Python_VERSION)
endif()
else()
# supported versions not in the specified range. Call final check
if (NOT Python_FIND_COMPONENTS)
set (Python_FIND_COMPONENTS Interpreter)
set (Python_FIND_REQUIRED_Interpreter TRUE)
endif()
include (${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
VERSION_VAR Python_VERSION
REASON_FAILURE_MESSAGE "Version range specified \"${Python_FIND_VERSION_RANGE}\" does not include supported versions")
endif()
if (COMMAND __Python_add_library)
@@ -448,3 +488,5 @@ if (COMMAND __Python_add_library)
endif()
unset (_PYTHON_PREFIX)
cmake_policy(POP)