From e452f6e2cf596c34e231b56803f739a878530ad9 Mon Sep 17 00:00:00 2001 From: Marc Chevrier Date: Thu, 29 Oct 2020 17:02:55 +0100 Subject: FindPython: Adds control over artifact names to search Fixes: #21371 --- .../dev/FindPython-FIND_UNVERSIONED_NAMES.rst | 6 ++ Modules/FindPython.cmake | 16 ++++++ Modules/FindPython/Support.cmake | 20 ++++++- Modules/FindPython2.cmake | 16 ++++++ Modules/FindPython3.cmake | 16 ++++++ Tests/FindPython/CMakeLists.txt | 12 ++++ Tests/FindPython/UnversionedNames/CMakeLists.txt | 66 ++++++++++++++++++++++ 7 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 Help/release/dev/FindPython-FIND_UNVERSIONED_NAMES.rst create mode 100644 Tests/FindPython/UnversionedNames/CMakeLists.txt diff --git a/Help/release/dev/FindPython-FIND_UNVERSIONED_NAMES.rst b/Help/release/dev/FindPython-FIND_UNVERSIONED_NAMES.rst new file mode 100644 index 0000000..a0b5838 --- /dev/null +++ b/Help/release/dev/FindPython-FIND_UNVERSIONED_NAMES.rst @@ -0,0 +1,6 @@ +FindPython-FIND_UNVERSIONED_NAMES +--------------------------------- + +* Modules :module:`FindPython3`, :module:`FindPython2` and :module:`FindPython` + gain the capability to control how interpreter unversioned names are + searched. diff --git a/Modules/FindPython.cmake b/Modules/FindPython.cmake index 5fc6a3b..e842e6b 100644 --- a/Modules/FindPython.cmake +++ b/Modules/FindPython.cmake @@ -318,6 +318,22 @@ Hints ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available through the ``PATH`` variable. +``Python_FIND_UNVERSIONED_NAMES`` + + .. versionadded:: 3.20 + + This variable defines how the generic names will be searched. Currently, it + only applies to the generic names of the interpreter, namely, ``python3`` or + ``python2`` and ``python``. + The ``Python_FIND_UNVERSIONED_NAMES`` variable can be set to one of the + following values: + + * ``FIRST``: The generic names are searched before the more specialized ones + (such as ``python2.5`` for example). + * ``LAST``: The generic names are searched after the more specialized ones. + This is the default. + * ``NEVER``: The generic name are not searched at all. + Artifacts Specification ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Modules/FindPython/Support.cmake b/Modules/FindPython/Support.cmake index 2b911ae..0e76468 100644 --- a/Modules/FindPython/Support.cmake +++ b/Modules/FindPython/Support.cmake @@ -337,6 +337,9 @@ function (_PYTHON_GET_NAMES _PYTHON_PGN_NAMES) foreach (implementation IN LISTS _PGN_IMPLEMENTATIONS) if (implementation STREQUAL "CPython") + if (_PGN_INTERPRETER AND _${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES STREQUAL "FIRST") + list (APPEND names python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR} python) + endif() foreach (version IN LISTS _PGN_VERSION) if (_PGN_WIN32) string (REPLACE "." "" version_no_dots ${version}) @@ -386,7 +389,7 @@ function (_PYTHON_GET_NAMES _PYTHON_PGN_NAMES) endif() endif() endforeach() - if (_PGN_INTERPRETER) + if (_PGN_INTERPRETER AND _${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES STREQUAL "LAST") list (APPEND names python${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR} python) endif() elseif (implementation STREQUAL "IronPython") @@ -1364,9 +1367,22 @@ else() endif() +# Python naming handling +if (DEFINED ${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES) + if (NOT ${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES MATCHES "^(FIRST|LAST|NEVER)$") + message (AUTHOR_WARNING "Find${_PYTHON_PREFIX}: ${_${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES}: invalid value for '${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES'. 'FIRST', 'LAST' or 'NEVER' expected. 'LAST' will be used instead.") + set (_${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES LAST) + else() + set (_${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES ${${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES}) + endif() +else() + set (_${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES LAST) +endif() + + # Compute search signature # This signature will be used to check validity of cached variables on new search -set (_${_PYTHON_PREFIX}_SIGNATURE "${${_PYTHON_PREFIX}_ROOT_DIR}:${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS}:${_${_PYTHON_PREFIX}_FIND_STRATEGY}:${${_PYTHON_PREFIX}_FIND_VIRTUALENV}") +set (_${_PYTHON_PREFIX}_SIGNATURE "${${_PYTHON_PREFIX}_ROOT_DIR}:${_${_PYTHON_PREFIX}_FIND_IMPLEMENTATIONS}:${_${_PYTHON_PREFIX}_FIND_STRATEGY}:${${_PYTHON_PREFIX}_FIND_VIRTUALENV}${_${_PYTHON_PREFIX}_FIND_UNVERSIONED_NAMES}") if (NOT WIN32) string (APPEND _${_PYTHON_PREFIX}_SIGNATURE ":${${_PYTHON_PREFIX}_USE_STATIC_LIBS}:") endif() diff --git a/Modules/FindPython2.cmake b/Modules/FindPython2.cmake index 9cd22e1..5277e33 100644 --- a/Modules/FindPython2.cmake +++ b/Modules/FindPython2.cmake @@ -265,6 +265,22 @@ Hints ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available through the ``PATH`` variable. +``Python2_FIND_UNVERSIONED_NAMES`` + + .. versionadded:: 3.20 + + This variable defines how the generic names will be searched. Currently, it + only applies to the generic names of the interpreter, namely, ``python2`` and + ``python``. + The ``Python2_FIND_UNVERSIONED_NAMES`` variable can be set to one of the + following values: + + * ``FIRST``: The generic names are searched before the more specialized ones + (such as ``python2.5`` for example). + * ``LAST``: The generic names are searched after the more specialized ones. + This is the default. + * ``NEVER``: The generic name are not searched at all. + Artifacts Specification ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Modules/FindPython3.cmake b/Modules/FindPython3.cmake index c79d482..2bd4d76 100644 --- a/Modules/FindPython3.cmake +++ b/Modules/FindPython3.cmake @@ -315,6 +315,22 @@ Hints ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available through the ``PATH`` variable. +``Python3_FIND_UNVERSIONED_NAMES`` + + .. versionadded:: 3.20 + + This variable defines how the generic names will be searched. Currently, it + only applies to the generic names of the interpreter, namely, ``python3`` and + ``python``. + The ``Python3_FIND_UNVERSIONED_NAMES`` variable can be set to one of the + following values: + + * ``FIRST``: The generic names are searched before the more specialized ones + (such as ``python3.5`` for example). + * ``LAST``: The generic names are searched after the more specialized ones. + This is the default. + * ``NEVER``: The generic name are not searched at all. + Artifacts Specification ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Tests/FindPython/CMakeLists.txt b/Tests/FindPython/CMakeLists.txt index 44484c3..520ba9e 100644 --- a/Tests/FindPython/CMakeLists.txt +++ b/Tests/FindPython/CMakeLists.txt @@ -451,6 +451,18 @@ if(CMake_TEST_FindPython) --test-command ${CMAKE_CTEST_COMMAND} -V -C $ ) endif() + + if (CMAKE_SYSTEM_NAME STREQUAL "Linux") + add_test(NAME FindPython.UnversionedNames COMMAND + ${CMAKE_CTEST_COMMAND} -C $ + --build-and-test + "${CMake_SOURCE_DIR}/Tests/FindPython/UnversionedNames" + "${CMake_BINARY_DIR}/Tests/FindPython/UnversionedNames" + ${build_generator_args} + --build-project UnversionedNames + --build-options ${build_options} + ) + endif() endif() if(CMake_TEST_FindPython_NumPy) diff --git a/Tests/FindPython/UnversionedNames/CMakeLists.txt b/Tests/FindPython/UnversionedNames/CMakeLists.txt new file mode 100644 index 0000000..597bd4e --- /dev/null +++ b/Tests/FindPython/UnversionedNames/CMakeLists.txt @@ -0,0 +1,66 @@ +cmake_minimum_required(VERSION 3.19...3.20) + +project(UnversionedNames LANGUAGES NONE) + +# check if it is possible to find python with a generic name +find_program(UNVERSIONED_Python3 NAMES python3) + +if (NOT UNVERSIONED_Python3) + # no generic name available + # test cannot be done + return() +endif() + +# search with default configuration +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +if (Python3_EXECUTABLE STREQUAL UNVERSIONED_Python3) + # default configuration pick-up the generic name + # test cannot be completed + return() +endif() + +unset(Python3_EXECUTABLE) +# Force now to search first for generic name +set(Python3_FIND_UNVERSIONED_NAMES FIRST) + +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +if (NOT Python3_EXECUTABLE STREQUAL UNVERSIONED_Python3) + message(SEND_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${UNVERSIONED_Python3}") +endif() + +# To check value 'NEVER", creates directory holding a symlink to the generic name +file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/bin") +file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin") +file(CREATE_LINK "${UNVERSIONED_Python3}" "${CMAKE_CURRENT_BINARY_DIR}/bin/python3" SYMBOLIC) + +unset(Python3_EXECUTABLE) +set(Python3_FIND_UNVERSIONED_NAMES FIRST) +set(Python3_ROOT_DIR "${CMAKE_CURRENT_BINARY_DIR}") +# First search: generic name must be found +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +if (NOT Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3") + message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${CMAKE_CURRENT_BINARY_DIR}/bin/python3") +endif() + +unset(Python3_EXECUTABLE) +set(Python3_FIND_UNVERSIONED_NAMES LAST) + +# Second search: generic name must be found +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +if (NOT Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3") + message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE} instead of ${CMAKE_CURRENT_BINARY_DIR}/bin/python3") +endif() + +unset(Python3_EXECUTABLE) +set(Python3_FIND_UNVERSIONED_NAMES NEVER) + +# Third search: generic name must NOT be found +find_package(Python3 REQUIRED COMPONENTS Interpreter) + +if (Python3_EXECUTABLE STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/bin/python3") + message(FATAL_ERROR "Found unexpected interpreter ${Python3_EXECUTABLE}") +endif() -- cgit v0.12