FindPython: add support for multiple searches in same directory

In some situations, like cross-compilation, it can be required to search for
the host python interpreter as well as the cross-compilation development
artifacts.
By managing different prefixes for the artifacts, multiple and independent
searches can be achieved.
This commit is contained in:
Marc Chevrier
2025-01-05 17:27:42 +01:00
parent ef6f5774fa
commit 9b0510fa57
11 changed files with 468 additions and 154 deletions

View File

@@ -557,6 +557,31 @@ can be controlled with the following variable:
* If set to ``FALSE`` or undefined: Enable multiple version/component
requirements.
``Python3_ARTIFACTS_PREFIX``
.. versionadded:: 3.32
Define a custom prefix which will be used for the definition of all the
result variables, targets, and commands. By using this variable, this module
supports multiple calls in the same directory with different
version/component requirements.
For example, in case of cross-compilation, development components are needed
but the native python interpreter can also be required:
.. code-block:: cmake
find_package(Python3 COMPONENTS Development)
set(Python3_ARTIFACTS_PREFIX "_HOST")
find_package(Python3 COMPONENTS Interpreter)
# Here Python3_HOST_EXECUTABLE and Python3_HOST::Interpreter artifacts are defined
.. note::
For consistency with standard behavior of modules, the various standard
``_FOUND`` variables (i.e. without the custom prefix) are also defined by
each call to the :command:`find_package` command.
Commands
^^^^^^^^
@@ -595,16 +620,23 @@ If the library type is not specified, ``MODULE`` is assumed.
#]=======================================================================]
set (_PYTHON_PREFIX Python3)
set (_PYTHON_BASE Python3)
if(${_PYTHON_BASE}_ARTIFACTS_PREFIX)
set(_PYTHON_PREFIX "${_PYTHON_BASE}${${_PYTHON_BASE}_ARTIFACTS_PREFIX}")
else()
set(_PYTHON_PREFIX "${_PYTHON_BASE}")
endif()
set (_Python3_REQUIRED_VERSION_MAJOR 3)
set (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR 3)
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
if (COMMAND __Python3_add_library)
macro (Python3_add_library)
__Python3_add_library (Python3 ${ARGV})
endmacro()
if (COMMAND __${_PYTHON_PREFIX}_add_library AND NOT COMMAND ${_PYTHON_PREFIX}_add_library)
cmake_language(EVAL CODE
"macro (${_PYTHON_PREFIX}_add_library)
__${_PYTHON_PREFIX}_add_library (${_PYTHON_PREFIX} \${ARGV})
endmacro()")
endif()
unset (_PYTHON_BASE)
unset (_PYTHON_PREFIX)