1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find Python interpreter, compiler and development environment (include
9 directories and libraries).
11 Three components are supported:
13 * ``Interpreter``: search for Python interpreter.
14 * ``Compiler``: search for Python compiler. Only offered by IronPython.
15 * ``Development``: search for development artifacts (include directories and
18 If no ``COMPONENTS`` is specified, ``Interpreter`` is assumed.
20 To ensure consistent versions between components ``Interpreter``, ``Compiler``
21 and ``Development``, specify all components at the same time::
23 find_package (Python COMPONENTS Interpreter Development)
25 This module looks preferably for version 3 of Python. If not found, version 2
27 To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
28 :module:`FindPython2` modules rather than this one.
33 This module defines the following :ref:`Imported Targets <Imported Targets>`:
35 ``Python::Interpreter``
36 Python interpreter. Target defined if component ``Interpreter`` is found.
38 Python compiler. Target defined if component ``Compiler`` is found.
40 Python library. Target defined if component ``Development`` is found.
45 This module will set the following variables in your project
46 (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
49 System has the Python requested components.
50 ``Python_Interpreter_FOUND``
51 System has the Python interpreter.
53 Path to the Python interpreter.
54 ``Python_INTERPRETER_ID``
55 A short string unique to the interpreter. Possible values include:
62 Standard platform independent installation directory.
64 Information returned by
65 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
67 Standard platform dependent installation directory.
69 Information returned by
70 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
72 Third-party platform independent installation directory.
74 Information returned by
75 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
77 Third-party platform dependent installation directory.
79 Information returned by
80 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
81 ``Python_Compiler_FOUND``
82 System has the Python compiler.
84 Path to the Python compiler. Only offered by IronPython.
85 ``Python_COMPILER_ID``
86 A short string unique to the compiler. Possible values include:
88 ``Python_Development_FOUND``
89 System has the Python development artifacts.
90 ``Python_INCLUDE_DIRS``
91 The Python include directories.
94 ``Python_LIBRARY_DIRS``
95 The Python library directories.
96 ``Python_RUNTIME_LIBRARY_DIRS``
97 The Python runtime library directories.
100 ``Python_VERSION_MAJOR``
101 Python major version.
102 ``Python_VERSION_MINOR``
103 Python minor version.
104 ``Python_VERSION_PATCH``
105 Python patch version.
111 Define the root directory of a Python installation.
113 ``Python_USE_STATIC_LIBS``
114 * If not defined, search for shared libraries and static libraries in that
116 * If set to TRUE, search **only** for static libraries.
117 * If set to FALSE, search **only** for shared libraries.
122 This module defines the command ``Python_add_library`` which have the same
123 semantic as :command:`add_library` but take care of Python module naming rules
124 (only applied if library is of type ``MODULE``) and add dependency to target
127 Python_add_library (my_module MODULE src1.cpp)
129 If library type is not specified, ``MODULE`` is assumed.
130 #]=======================================================================]
133 set (_PYTHON_PREFIX Python)
135 if (DEFINED Python_FIND_VERSION)
136 set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
138 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
140 # iterate over versions in quiet and NOT required modes to avoid multiple
141 # "Found" messages and prematurally failure.
142 set (_Python_QUIETLY ${Python_FIND_QUIETLY})
143 set (_Python_REQUIRED ${Python_FIND_REQUIRED})
144 set (Python_FIND_QUIETLY TRUE)
145 set (Python_FIND_REQUIRED FALSE)
147 set (_Python_REQUIRED_VERSIONS 3 2)
148 set (_Python_REQUIRED_VERSION_LAST 2)
150 foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
151 set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
152 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
154 _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
157 # clean-up some CACHE variables to ensure look-up restart from scratch
158 foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
159 unset (${_Python_ITEM} CACHE)
163 unset (Python_FIND_VERSION)
165 set (Python_FIND_QUIETLY ${_Python_QUIETLY})
166 set (Python_FIND_REQUIRED ${_Python_REQUIRED})
167 if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
168 # call again validation command to get "Found" or error message
169 find_package_handle_standard_args (Python HANDLE_COMPONENTS
170 REQUIRED_VARS ${_Python_REQUIRED_VARS}
171 VERSION_VAR Python_VERSION)
175 if (COMMAND __Python_add_library)
176 macro (Python_add_library)
177 __Python_add_library (Python ${ARGV})
181 unset (_PYTHON_PREFIX)