summaryrefslogtreecommitdiff
path: root/Modules/FindPython.cmake
diff options
context:
space:
mode:
authorMarc Chevrier <marc.chevrier@gmail.com>2020-09-21 19:31:43 +0200
committerMarc Chevrier <marc.chevrier@gmail.com>2020-09-30 14:30:23 +0200
commite4b7049230afabd7f2cc0186f26176b103682100 (patch)
tree57c99c4d31e0a903df80701e7526e728db1697db /Modules/FindPython.cmake
parentf002c1cfc7f66edb9c9821524671574c23f92cd2 (diff)
downloadcmake-e4b7049230afabd7f2cc0186f26176b103682100.tar.gz
FindPython: Add version range support
Fixes: #21107
Diffstat (limited to 'Modules/FindPython.cmake')
-rw-r--r--Modules/FindPython.cmake52
1 files changed, 47 insertions, 5 deletions
diff --git a/Modules/FindPython.cmake b/Modules/FindPython.cmake
index 584f64d3e2..2d13f48162 100644
--- a/Modules/FindPython.cmake
+++ b/Modules/FindPython.cmake
@@ -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.
#]=======================================================================]
-set (_PYTHON_PREFIX Python)
+cmake_policy(PUSH)
+# numbers and boolean constants
+cmake_policy (SET CMP0012 NEW)
-if (DEFINED Python_FIND_VERSION)
+
+set (_PYTHON_PREFIX Python)
+unset (_Python_REQUIRED_VERSION_MAJOR)
+unset (_Python_REQUIRED_VERSIONS)
+
+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})
+else()
+ set (_Python_REQUIRED_VERSIONS 3 2)
+endif()
+if (_Python_REQUIRED_VERSION_MAJOR)
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
-else()
+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)