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 The following 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
17 * ``NumPy``: search for NumPy include directories.
19 If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
21 To ensure consistent versions between components ``Interpreter``, ``Compiler``,
22 ``Development`` and ``NumPy``, specify all components at the same time::
24 find_package (Python COMPONENTS Interpreter Development)
26 This module looks preferably for version 3 of Python. If not found, version 2
28 To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
29 :module:`FindPython2` modules rather than this one.
33 If components ``Interpreter`` and ``Development`` are both specified, this
34 module search only for interpreter with same platform architecture as the one
35 defined by ``CMake`` configuration. This contraint does not apply if only
36 ``Interpreter`` component is specified.
41 This module defines the following :ref:`Imported Targets <Imported Targets>`
42 (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
44 ``Python::Interpreter``
45 Python interpreter. Target defined if component ``Interpreter`` is found.
47 Python compiler. Target defined if component ``Compiler`` is found.
49 Python library for Python embedding. Target defined if component
50 ``Development`` is found.
52 Python library for Python module. Target defined if component ``Development``
55 NumPy Python library. Target defined if component ``NumPy`` is found.
60 This module will set the following variables in your project
61 (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
64 System has the Python requested components.
65 ``Python_Interpreter_FOUND``
66 System has the Python interpreter.
68 Path to the Python interpreter.
69 ``Python_INTERPRETER_ID``
70 A short string unique to the interpreter. Possible values include:
77 Standard platform independent installation directory.
79 Information returned by
80 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
82 Standard platform dependent installation directory.
84 Information returned by
85 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
87 Third-party platform independent installation directory.
89 Information returned by
90 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
92 Third-party platform dependent installation directory.
94 Information returned by
95 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
97 Extension suffix for modules.
99 Information returned by
100 ``distutils.sysconfig.get_config_flag('SOABI')`` or computed from
101 ``distutils.sysconfig.get_config_flag('EXT_SUFFIX')`` or
102 ``python-config --extension-suffix``.
103 ``Python_Compiler_FOUND``
104 System has the Python compiler.
106 Path to the Python compiler. Only offered by IronPython.
107 ``Python_COMPILER_ID``
108 A short string unique to the compiler. Possible values include:
110 ``Python_Development_FOUND``
111 System has the Python development artifacts.
112 ``Python_INCLUDE_DIRS``
113 The Python include directories.
115 The Python libraries.
116 ``Python_LIBRARY_DIRS``
117 The Python library directories.
118 ``Python_RUNTIME_LIBRARY_DIRS``
119 The Python runtime library directories.
122 ``Python_VERSION_MAJOR``
123 Python major version.
124 ``Python_VERSION_MINOR``
125 Python minor version.
126 ``Python_VERSION_PATCH``
127 Python patch version.
128 ``Python_NumPy_FOUND``
129 System has the NumPy.
130 ``Python_NumPy_INCLUDE_DIRS``
131 The NumPy include directries.
132 ``Python_NumPy_VERSION``
139 Define the root directory of a Python installation.
141 ``Python_USE_STATIC_LIBS``
142 * If not defined, search for shared libraries and static libraries in that
144 * If set to TRUE, search **only** for static libraries.
145 * If set to FALSE, search **only** for shared libraries.
148 This variable defines which ABIs, as defined in
149 `PEP 3149 <https://www.python.org/dev/peps/pep-3149/>`_, should be searched.
153 This hint will be honored only when searched for ``Python`` version 3.
157 If ``Python_FIND_ABI`` is not defined, any ABI will be searched.
159 The ``Python_FIND_ABI`` variable is a 3-tuple specifying, in that order,
160 ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
161 Each element can be set to one of the following:
163 * ``ON``: Corresponding flag is selected.
164 * ``OFF``: Corresponding flag is not selected.
165 * ``ANY``: The two posibilties (``ON`` and ``OFF``) will be searched.
167 From this 3-tuple, various ABIs will be searched starting from the most
168 specialized to the most general. Moreover, ``debug`` versions will be
169 searched **after** ``non-debug`` ones.
171 For example, if we have::
173 set (Python_FIND_ABI "ON" "ANY" "ANY")
175 The following flags combinations will be appended, in that order, to the
176 artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
178 And to search any possible ABIs::
180 set (Python_FIND_ABI "ANY" "ANY" "ANY")
182 The following combinations, in that order, will be used: ``mu``, ``m``,
183 ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
187 This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
188 when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
189 `python.org <https://www.python.org/>`_ will be found only if value for
190 each flag is ``OFF`` or ``ANY``.
192 ``Python_FIND_STRATEGY``
193 This variable defines how lookup will be done.
194 The ``Python_FIND_STRATEGY`` variable can be set to one of the following:
196 * ``VERSION``: Try to find the most recent version in all specified
198 This is the default if policy :policy:`CMP0094` is undefined or set to
200 * ``LOCATION``: Stops lookup as soon as a version satisfying version
201 constraints is founded.
202 This is the default if policy :policy:`CMP0094` is set to ``NEW``.
204 ``Python_FIND_REGISTRY``
205 On Windows the ``Python_FIND_REGISTRY`` variable determine the order
206 of preference between registry and environment variables.
207 the ``Python_FIND_REGISTRY`` variable can be set to one of the following:
209 * ``FIRST``: Try to use registry before environment variables.
211 * ``LAST``: Try to use registry after environment variables.
212 * ``NEVER``: Never try to use registry.
214 ``Python_FIND_FRAMEWORK``
215 On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
216 preference between Apple-style and unix-style package components.
217 This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
222 Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
224 If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
225 variable will be used, if any.
227 ``Python_FIND_VIRTUALENV``
228 This variable defines the handling of virtual environments managed by
229 ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
230 is active (i.e. the ``activate`` script has been evaluated). In this case, it
231 takes precedence over ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
232 variables. The ``Python_FIND_VIRTUALENV`` variable can be set to one of the
235 * ``FIRST``: The virtual environment is used before any other standard
236 paths to look-up for the interpreter. This is the default.
237 * ``ONLY``: Only the virtual environment is used to look-up for the
239 * ``STANDARD``: The virtual environment is not used to look-up for the
240 interpreter. In this case, variable ``Python_FIND_REGISTRY`` (Windows)
241 or ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
242 ``NEVER`` to select preferably the interpreter from the virtual
247 If the component ``Development`` is requested, it is **strongly**
248 recommended to also include the component ``Interpreter`` to get expected
251 Artifacts Specification
252 ^^^^^^^^^^^^^^^^^^^^^^^
254 To solve special cases, it is possible to specify directly the artifacts by
255 setting the following variables:
257 ``Python_EXECUTABLE``
258 The path to the interpreter.
261 The path to the compiler.
264 The path to the library. It will be used to compute the
265 variables ``Python_LIBRARIES``, ``Python_LIBRAY_DIRS`` and
266 ``Python_RUNTIME_LIBRARY_DIRS``.
268 ``Python_INCLUDE_DIR``
269 The path to the directory of the ``Python`` headers. It will be used to
270 compute the variable ``Python_INCLUDE_DIRS``.
272 ``Python_NumPy_INCLUDE_DIR``
273 The path to the directory of the ``NumPy`` headers. It will be used to
274 compute the variable ``Python_NumPy_INCLUDE_DIRS``.
278 All paths must be absolute. Any artifact specified with a relative path
283 When an artifact is specified, all ``HINTS`` will be ignored and no search
284 will be performed for this artifact.
286 If more than one artifact is specified, it is the user's responsability to
287 ensure the consistency of the various artifacts.
292 This module defines the command ``Python_add_library`` (when
293 :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
294 :command:`add_library` and adds a dependency to target ``Python::Python`` or,
295 when library type is ``MODULE``, to target ``Python::Module`` and takes care of
296 Python module naming rules::
298 Python_add_library (<name> [STATIC | SHARED | MODULE [WITH_SOABI]]
299 <source1> [<source2> ...])
301 If the library type is not specified, ``MODULE`` is assumed.
303 For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
304 module suffix will include the ``Python_SOABI`` value, if any.
305 #]=======================================================================]
308 set (_PYTHON_PREFIX Python)
310 if (DEFINED Python_FIND_VERSION)
311 set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
313 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
315 # iterate over versions in quiet and NOT required modes to avoid multiple
316 # "Found" messages and prematurally failure.
317 set (_Python_QUIETLY ${Python_FIND_QUIETLY})
318 set (_Python_REQUIRED ${Python_FIND_REQUIRED})
319 set (Python_FIND_QUIETLY TRUE)
320 set (Python_FIND_REQUIRED FALSE)
322 set (_Python_REQUIRED_VERSIONS 3 2)
323 set (_Python_REQUIRED_VERSION_LAST 2)
325 unset (_Python_INPUT_VARS)
326 foreach (_Python_ITEM IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
327 Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
328 if (NOT DEFINED ${_Python_ITEM})
329 list (APPEND _Python_INPUT_VARS ${_Python_ITEM})
333 foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
334 set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
335 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
337 _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
340 # clean-up INPUT variables not set by the user
341 foreach (_Python_ITEM IN LISTS _Python_INPUT_VARS)
342 unset (${_Python_ITEM})
344 # clean-up some CACHE variables to ensure look-up restart from scratch
345 foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
346 unset (${_Python_ITEM} CACHE)
350 unset (Python_FIND_VERSION)
352 set (Python_FIND_QUIETLY ${_Python_QUIETLY})
353 set (Python_FIND_REQUIRED ${_Python_REQUIRED})
354 if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
355 # call again validation command to get "Found" or error message
356 find_package_handle_standard_args (Python HANDLE_COMPONENTS
357 REQUIRED_VARS ${_Python_REQUIRED_VARS}
358 VERSION_VAR Python_VERSION)
362 if (COMMAND __Python_add_library)
363 macro (Python_add_library)
364 __Python_add_library (Python ${ARGV})
368 unset (_PYTHON_PREFIX)