Commit 352baee2 authored by Marc Chevrier's avatar Marc Chevrier

FindPython*: New implementation for Python stuff

Fixes: #16142
parent dfc9036b
......@@ -644,6 +644,8 @@ Documentation`_ section above.
.. _`CMake Developer Standard Variable Names`:
Standard Variable Names
^^^^^^^^^^^^^^^^^^^^^^^
......
......@@ -196,6 +196,9 @@ All Modules
/module/FindPostgreSQL
/module/FindProducer
/module/FindProtobuf
/module/FindPython
/module/FindPython2
/module/FindPython3
/module/FindPythonInterp
/module/FindPythonLibs
/module/FindQt3
......
.. cmake-module:: ../../Modules/FindPython.cmake
.. cmake-module:: ../../Modules/FindPython2.cmake
.. cmake-module:: ../../Modules/FindPython3.cmake
FindPython(2|3)
---------------
* The new :module:`FindPython3` and :module:`FindPython2` modules, as well as
:module:`FindPython`, provide a new way to locate python environments.
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindPython
----------
Find Python interpreter, compiler and development environment (include
directories and libraries).
Three components are supported:
* ``Interpreter``: search for Python interpreter.
* ``Compiler``: search for Python compiler. Only offered by IronPython.
* ``Development``: search for development artifacts (include directories and
libraries).
If no ``COMPONENTS`` is specified, ``Interpreter`` is assumed.
To ensure consistent versions between components ``Interpreter``, ``Compiler``
and ``Development``, specify all components at the same time::
find_package (Python COMPONENTS Interpreter Development)
This module looks preferably for version 3 of Python. If not found, version 2
is searched.
To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
:module:`FindPython2` modules rather than this one.
Imported Targets
^^^^^^^^^^^^^^^^
This module defines the following :ref:`Imported Targets <Imported Targets>`:
``Python::Interpreter``
Python interpreter. Target defined if component ``Interpreter`` is found.
``Python::Compiler``
Python compiler. Target defined if component ``Compiler`` is found.
``Python::Python``
Python library. Target defined if component ``Development`` is found.
Result Variables
^^^^^^^^^^^^^^^^
This module will set the following variables in your project
(see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
``Python_FOUND``
System has the Python requested components.
``Python_Interpreter_FOUND``
System has the Python interpreter.
``Python_EXECUTABLE``
Path to the Python interpreter.
``Python_INTERPRETER_ID``
A short string unique to the interpreter. Possible values include:
* Python
* ActivePython
* Anaconda
* Canopy
* IronPython
``Python_STDLIB``
Standard platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
``Python_STDARCH``
Standard platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
``Python_SITELIB``
Third-party platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
``Python_SITEARCH``
Third-party platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
``Python_Compiler_FOUND``
System has the Python compiler.
``Python_COMPILER``
Path to the Python compiler. Only offered by IronPython.
``Python_COMPILER_ID``
A short string unique to the compiler. Possible values include:
* IronPython
``Python_Development_FOUND``
System has the Python development artifacts.
``Python_INCLUDE_DIRS``
The Python include directories.
``Python_LIBRARIES``
The Python libraries.
``Python_LIBRARY_DIRS``
The Python library directories.
``Python_RUNTIME_LIBRARY_DIRS``
The Python runtime library directories.
``Python_VERSION``
Python version.
``Python_VERSION_MAJOR``
Python major version.
``Python_VERSION_MINOR``
Python minor version.
``Python_VERSION_PATCH``
Python patch version.
Hints
^^^^^
``Python_ROOT_DIR``
Define the root directory of a Python installation.
``Python_USE_STATIC_LIBS``
* If not defined, search for shared libraries and static libraries in that
order.
* If set to TRUE, search **only** for static libraries.
* If set to FALSE, search **only** for shared libraries.
Commands
^^^^^^^^
This module defines the command ``Python_add_library`` which have the same
semantic as :command:`add_library` but take care of Python module naming rules
(only applied if library is of type ``MODULE``) and add dependency to target
``Python::Python``::
Python_add_library (my_module MODULE src1.cpp)
If library type is not specified, ``MODULE`` is assumed.
#]=======================================================================]
set (_PYTHON_PREFIX Python)
if (DEFINED Python_FIND_VERSION)
set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
else()
# iterate over versions in quiet and NOT required modes to avoid multiple
# "Found" messages and prematurally failure.
set (_Python_QUIETLY ${Python_FIND_QUIETLY})
set (_Python_REQUIRED ${Python_FIND_REQUIRED})
set (Python_FIND_QUIETLY TRUE)
set (Python_FIND_REQUIRED FALSE)
foreach (_Python_REQUIRED_VERSION_MAJOR IN ITEMS 3 2)
set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
if (Python_FOUND)
break()
endif()
# clean-up some CACHE variables to ensure look-up restart from scratch
foreach (_Python_ITEM IN ITEMS EXECUTABLE COMPILER
LIBRARY_RELEASE RUNTIME_LIBRARY_RELEASE
LIBRARY_DEBUG RUNTIME_LIBRARY_DEBUG
INCLUDE_DIR)
unset (Python_${_Python_ITEM} CACHE)
endforeach()
endforeach()
unset (Python_FIND_VERSION)
set (Python_FIND_QUIETLY ${_Python_QUIETLY})
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
REQUIRED_VARS ${_Python_REQUIRED_VARS}
VERSION_VAR Python_VERSION)
endif()
endif()
if (COMMAND __Python_add_library)
macro (Python_add_library)
__Python_add_library (Python ${ARGV})
endmacro()
endif()
unset (_PYTHON_PREFIX)
This diff is collapsed.
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindPython2
-----------
Find Python 2 interpreter, compiler and development environment (include
directories and libraries).
Three components are supported:
* ``Interpreter``: search for Python 2 interpreter
* ``Compiler``: search for Python 2 compiler. Only offered by IronPython.
* ``Development``: search for development artifacts (include directories and
libraries)
If no ``COMPONENTS`` is specified, ``Interpreter`` is assumed.
To ensure consistent versions between components ``Interpreter``, ``Compiler``
and ``Development``, specify all components at the same time::
find_package (Python2 COMPONENTS Interpreter Development)
This module looks only for version 2 of Python. This module can be used
concurrently with :module:`FindPython3` module to use both Python versions.
The :module:`FindPython` module can be used if Python version does not matter
for you.
Imported Targets
^^^^^^^^^^^^^^^^
This module defines the following :ref:`Imported Targets <Imported Targets>`:
``Python2::Interpreter``
Python 2 interpreter. Target defined if component ``Interpreter`` is found.
``Python2::Compiler``
Python 2 compiler. Target defined if component ``Compiler`` is found.
``Python2::Python``
Python 2 library. Target defined if component ``Development`` is found.
Result Variables
^^^^^^^^^^^^^^^^
This module will set the following variables in your project
(see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
``Python2_FOUND``
System has the Python 2 requested components.
``Python2_Interpreter_FOUND``
System has the Python 2 interpreter.
``Python2_EXECUTABLE``
Path to the Python 2 interpreter.
``Python2_INTERPRETER_ID``
A short string unique to the interpreter. Possible values include:
* Python
* ActivePython
* Anaconda
* Canopy
* IronPython
``Python2_STDLIB``
Standard platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
``Python2_STDARCH``
Standard platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
``Python2_SITELIB``
Third-party platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
``Python2_SITEARCH``
Third-party platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
``Python2_Compiler_FOUND``
System has the Python 2 compiler.
``Python2_COMPILER``
Path to the Python 2 compiler. Only offered by IronPython.
``Python2_COMPILER_ID``
A short string unique to the compiler. Possible values include:
* IronPython
``Python2_Development_FOUND``
System has the Python 2 development artifacts.
``Python2_INCLUDE_DIRS``
The Python 2 include directories.
``Python2_LIBRARIES``
The Python 2 libraries.
``Python2_LIBRARY_DIRS``
The Python 2 library directories.
``Python2_RUNTIME_LIBRARY_DIRS``
The Python 2 runtime library directories.
``Python2_VERSION``
Python 2 version.
``Python2_VERSION_MAJOR``
Python 2 major version.
``Python2_VERSION_MINOR``
Python 2 minor version.
``Python2_VERSION_PATCH``
Python 2 patch version.
Hints
^^^^^
``Python2_ROOT_DIR``
Define the root directory of a Python 2 installation.
``Python2_USE_STATIC_LIBS``
* If not defined, search for shared libraries and static libraries in that
order.
* If set to TRUE, search **only** for static libraries.
* If set to FALSE, search **only** for shared libraries.
Commands
^^^^^^^^
This module defines the command ``Python2_add_library`` which have the same
semantic as :command:`add_library` but take care of Python module naming rules
(only applied if library is of type ``MODULE``) and add dependency to target
``Python2::Python``::
Python2_add_library (my_module MODULE src1.cpp)
If library type is not specified, ``MODULE`` is assumed.
#]=======================================================================]
set (_PYTHON_PREFIX Python2)
set (_Python2_REQUIRED_VERSION_MAJOR 2)
include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
if (COMMAND __Python2_add_library)
macro (Python2_add_library)
__Python2_add_library (Python2 ${ARGV})
endmacro()
endif()
unset (_PYTHON_PREFIX)
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindPython3
-----------
Find Python 3 interpreter, compiler and development environment (include
directories and libraries).
Three components are supported:
* ``Interpreter``: search for Python 3 interpreter
* ``Compiler``: search for Python 3 compiler. Only offered by IronPython.
* ``Development``: search for development artifacts (include directories and
libraries)
If no ``COMPONENTS`` is specified, ``Interpreter`` is assumed.
To ensure consistent versions between components ``Interpreter``, ``Compiler``
and ``Development``, specify all components at the same time::
find_package (Python3 COMPONENTS Interpreter Development)
This module looks only for version 3 of Python. This module can be used
concurrently with :module:`FindPython2` module to use both Python versions.
The :module:`FindPython` module can be used if Python version does not matter
for you.
Imported Targets
^^^^^^^^^^^^^^^^
This module defines the following :ref:`Imported Targets <Imported Targets>`:
``Python3::Interpreter``
Python 3 interpreter. Target defined if component ``Interpreter`` is found.
``Python3::Compiler``
Python 3 compiler. Target defined if component ``Compiler`` is found.
``Python3::Python``
Python 3 library. Target defined if component ``Development`` is found.
Result Variables
^^^^^^^^^^^^^^^^
This module will set the following variables in your project
(see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
``Python3_FOUND``
System has the Python 3 requested components.
``Python3_Interpreter_FOUND``
System has the Python 3 interpreter.
``Python3_EXECUTABLE``
Path to the Python 3 interpreter.
``Python3_INTERPRETER_ID``
A short string unique to the interpreter. Possible values include:
* Python
* ActivePython
* Anaconda
* Canopy
* IronPython
``Python3_STDLIB``
Standard platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
``Python3_STDARCH``
Standard platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
``Python3_SITELIB``
Third-party platform independent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
``Python3_SITEARCH``
Third-party platform dependent installation directory.
Information returned by
``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
``Python3_Compiler_FOUND``
System has the Python 3 compiler.
``Python3_COMPILER``
Path to the Python 3 compiler. Only offered by IronPython.
``Python3_COMPILER_ID``
A short string unique to the compiler. Possible values include:
* IronPython
``Python3_Development_FOUND``
System has the Python 3 development artifacts.
``Python3_INCLUDE_DIRS``
The Python 3 include directories.
``Python3_LIBRARIES``
The Python 3 libraries.
``Python3_LIBRARY_DIRS``
The Python 3 library directories.
``Python3_RUNTIME_LIBRARY_DIRS``
The Python 3 runtime library directories.
``Python3_VERSION``
Python 3 version.
``Python3_VERSION_MAJOR``
Python 3 major version.
``Python3_VERSION_MINOR``
Python 3 minor version.
``Python3_VERSION_PATCH``
Python 3 patch version.
Hints
^^^^^
``Python3_ROOT_DIR``
Define the root directory of a Python 3 installation.
``Python3_USE_STATIC_LIBS``
* If not defined, search for shared libraries and static libraries in that
order.
* If set to TRUE, search **only** for static libraries.
* If set to FALSE, search **only** for shared libraries.
Commands
^^^^^^^^
This module defines the command ``Python3_add_library`` which have the same
semantic as :command:`add_library` but take care of Python module naming rules
(only applied if library is of type ``MODULE``) and add dependency to target
``Python3::Python``::
Python3_add_library (my_module MODULE src1.cpp)
If library type is not specified, ``MODULE`` is assumed.
#]=======================================================================]
set (_PYTHON_PREFIX Python3)
set (_Python3_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()
endif()
unset (_PYTHON_PREFIX)
......@@ -7,6 +7,10 @@
#
# Find python interpreter
#
# .. deprecated:: 3.12
#
# Use :module:`FindPython3`, :module:`FindPython2` or :module:`FindPython` instead.
#
# This module finds if Python interpreter is installed and determines
# where the executables are. This code sets the following variables:
#
......
......@@ -7,6 +7,10 @@
#
# Find python libraries
#
# .. deprecated:: 3.12
#
# Use :module:`FindPython3`, :module:`FindPython2` or :module:`FindPython` instead.
#
# This module finds if Python is installed and determines where the
# include files and libraries are. It also determines what the name of
# the library is. This code sets the following variables:
......
......@@ -1427,6 +1427,10 @@ ${CMake_BINARY_DIR}/bin/cmake -DDIR=dev -P ${CMake_SOURCE_DIR}/Utilities/Release
add_subdirectory(FindXercesC)
endif()
if(CMake_TEST_FindPython)
add_subdirectory(FindPython)
endif()
add_subdirectory(FindThreads)
# Matlab module
......
add_test(NAME FindPython.Python2 COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/Python2"
"${CMake_BINARY_DIR}/Tests/FindPython/Python2"
${build_generator_args}
--build-project TestPython2
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
add_test(NAME FindPython.Python2Fail COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/Python2Fail"
"${CMake_BINARY_DIR}/Tests/FindPython/Python2Fail"
${build_generator_args}
--build-project TestPython2Fail
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
set_tests_properties(FindPython.Python2Fail PROPERTIES
PASS_REGULAR_EXPRESSION "Could NOT find Python2 \\(missing: foobar\\)")
add_test(NAME FindPython.Python3 COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/Python3"
"${CMake_BINARY_DIR}/Tests/FindPython/Python3"
${build_generator_args}
--build-project TestPython3
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
add_test(NAME FindPython.Python3Fail COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/Python3Fail"
"${CMake_BINARY_DIR}/Tests/FindPython/Python3Fail"
${build_generator_args}
--build-project TestPython3Fail
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
set_tests_properties(FindPython.Python3Fail PROPERTIES
PASS_REGULAR_EXPRESSION "Could NOT find Python3 \\(missing: foobar\\)")
add_test(NAME FindPython.Python COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/Python"
"${CMake_BINARY_DIR}/Tests/FindPython/Python"
${build_generator_args}
--build-project TestPython
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
add_test(NAME FindPython.MultiplePackages COMMAND
${CMAKE_CTEST_COMMAND} -C $<CONFIGURATION>
--build-and-test
"${CMake_SOURCE_DIR}/Tests/FindPython/MultiplePackages"
"${CMake_BINARY_DIR}/Tests/FindPython/MultiplePackages"
${build_generator_args}
--build-project TestMultiplePackages
--build-options ${build_options}
--test-command ${CMAKE_CTEST_COMMAND} -V -C $<CONFIGURATION>
)
cmake_minimum_required(VERSION 3.1)
project(TestMultiplePackages C)
find_package (Python2 REQUIRED COMPONENTS Interpreter Development)
find_package (Python3 REQUIRED COMPONENTS Interpreter Development)
# Must find Python 3
find_package (Python REQUIRED)
if (NOT Python3_EXECUTABLE STREQUAL Python_EXECUTABLE)
message (FATAL_ERROR
"Python interpreters do not match:\n"
" Python_EXECUTABLE='${Python_EXECUTABLE}'\n"
" Python3_EXECUTABLE='${Python3_EXECUTABLE}'\n"
)
endif()
Python2_add_library (spam2 MODULE ../spam.c)
target_compile_definitions (spam2 PRIVATE PYTHON2)
Python3_add_library (spam3 MODULE ../spam.c)
target_compile_definitions (spam3 PRIVATE PYTHON3)
add_test (NAME python2_spam2
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
"${Python2_EXECUTABLE}" -c "import spam2; spam2.system(\"cd\")")
add_test (NAME python3_spam3
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
"${Python3_EXECUTABLE}" -c "import spam3; spam3.system(\"cd\")")
cmake_minimum_required(VERSION 3.1)
project(TestPython C)
include(CTest)
find_package(Python 3 REQUIRED COMPONENTS Interpreter Development)
if (NOT Python_FOUND)
message (FATAL_ERROR "Fail to found Python 3")
endif()
Python_add_library (spam3 MODULE ../spam.c)
target_compile_definitions (spam3 PRIVATE PYTHON3)
add_test (NAME python_spam3
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
"${Python_EXECUTABLE}" -c "import spam3; spam3.system(\"cd\")")
cmake_minimum_required(VERSION 3.1)
project(TestPython2 C)
include(CTest)
find_package(Python2 3 QUIET)
if (Python2_FOUND)
message (FATAL_ERROR "Wrong python version found: ${Python2_VERSION}")
endif()
find_package(Python2 REQUIRED COMPONENTS Interpreter Development)
if (NOT Python2_FOUND)
message (FATAL_ERROR "Fail to found Python 2")
endif()
Python2_add_library (spam2 MODULE ../spam.c)
target_compile_definitions (spam2 PRIVATE PYTHON2)
add_test (NAME python2_spam2
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam2>"
"${Python2_EXECUTABLE}" -c "import spam2; spam2.system(\"cd\")")
cmake_minimum_required(VERSION 3.1)
project(TestPython2Fail C)
include(CTest)
find_package(Python2 REQUIRED COMPONENTS Interpreter Development foobar)
Python2_add_library (spam2 MODULE ../spam.c)
target_compile_definitions (spam2 PRIVATE PYTHON2)
add_test (NAME python2_spam2
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam2>"
"${Python2_EXECUTABLE}" -c "import spam2; spam2.system(\"cd\")")
cmake_minimum_required(VERSION 3.1)
project(TestPython3 C)
include(CTest)
find_package(Python3 2 QUIET)
if (Python3_FOUND)
message (FATAL_ERROR "Wrong python version found: ${Python3_VERSION}")
endif()
find_package(Python3 REQUIRED COMPONENTS Interpreter Development)
if (NOT Python3_FOUND)
message (FATAL_ERROR "Fail to found Python 3")
endif()
Python3_add_library (spam3 MODULE ../spam.c)
target_compile_definitions (spam3 PRIVATE PYTHON3)
add_test (NAME python3_spam3
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
"${Python3_EXECUTABLE}" -c "import spam3; spam3.system(\"cd\")")
cmake_minimum_required(VERSION 3.1)
project(TestPython3Fail C)
include(CTest)
find_package(Python3 REQUIRED COMPONENTS Interpreter Development foobar)
Python3_add_library (spam3 MODULE ../spam.c)
target_compile_definitions (spam3 PRIVATE PYTHON3)
add_test (NAME python3_spam3
COMMAND "${CMAKE_COMMAND}" -E env "PYTHONPATH=$<TARGET_FILE_DIR:spam3>"
"${Python3_EXECUTABLE}" -c "import spam3; spam3.system(\"cd\")")
#include <Python.h>
static PyObject* spam_system(PyObject* self, PyObject* args)
{
const char* command;
int sts;