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:
10 Find Python interpreter, compiler and development environment (include
11 directories and libraries).
13 .. versionadded:: 3.19
14 When a version is requested, it can be specified as a simple value or as a
15 range. For a detailed description of version range usage and capabilities,
16 refer to the :command:`find_package` command.
18 The following components are supported:
20 * ``Interpreter``: search for Python interpreter.
21 * ``Compiler``: search for Python compiler. Only offered by IronPython.
22 * ``Development``: search for development artifacts (include directories and
25 .. versionadded:: 3.18
26 This component includes two sub-components which can be specified
29 * ``Development.Module``: search for artifacts for Python module
31 * ``Development.Embed``: search for artifacts for Python embedding
34 * ``NumPy``: search for NumPy include directories.
36 .. versionadded:: 3.14
37 Added the ``NumPy`` component.
39 If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
41 If component ``Development`` is specified, it implies sub-components
42 ``Development.Module`` and ``Development.Embed``.
44 To ensure consistent versions between components ``Interpreter``, ``Compiler``,
45 ``Development`` (or one of its sub-components) and ``NumPy``, specify all
46 components at the same time::
48 find_package (Python COMPONENTS Interpreter Development)
50 This module looks preferably for version 3 of Python. If not found, version 2
52 To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
53 :module:`FindPython2` modules rather than this one.
57 If components ``Interpreter`` and ``Development`` (or one of its
58 sub-components) are both specified, this module search only for interpreter
59 with same platform architecture as the one defined by ``CMake``
60 configuration. This constraint does not apply if only ``Interpreter``
61 component is specified.
66 This module defines the following :ref:`Imported Targets <Imported Targets>`:
68 .. versionchanged:: 3.14
69 :ref:`Imported Targets <Imported Targets>` are only created when
70 :prop_gbl:`CMAKE_ROLE` is ``PROJECT``.
72 ``Python::Interpreter``
73 Python interpreter. Target defined if component ``Interpreter`` is found.
75 Python compiler. Target defined if component ``Compiler`` is found.
78 .. versionadded:: 3.15
80 Python library for Python module. Target defined if component
81 ``Development.Module`` is found.
84 Python library for Python embedding. Target defined if component
85 ``Development.Embed`` is found.
88 .. versionadded:: 3.14
90 NumPy Python library. Target defined if component ``NumPy`` is found.
95 This module will set the following variables in your project
96 (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
99 System has the Python requested components.
100 ``Python_Interpreter_FOUND``
101 System has the Python interpreter.
102 ``Python_EXECUTABLE``
103 Path to the Python interpreter.
104 ``Python_INTERPRETER_ID``
105 A short string unique to the interpreter. Possible values include:
113 Standard platform independent installation directory.
115 Information returned by
116 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``
117 or else ``sysconfig.get_path('stdlib')``.
119 Standard platform dependent installation directory.
121 Information returned by
122 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``
123 or else ``sysconfig.get_path('platstdlib')``.
125 Third-party platform independent installation directory.
127 Information returned by
128 ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``
129 or else ``sysconfig.get_path('purelib')``.
131 Third-party platform dependent installation directory.
133 Information returned by
134 ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``
135 or else ``sysconfig.get_path('platlib')``.
138 .. versionadded:: 3.17
140 Extension suffix for modules.
142 Information returned by
143 ``distutils.sysconfig.get_config_var('SOABI')`` or computed from
144 ``distutils.sysconfig.get_config_var('EXT_SUFFIX')`` or
145 ``python-config --extension-suffix``. If package ``distutils.sysconfig`` is
146 not available, ``sysconfig.get_config_var('SOABI')`` or
147 ``sysconfig.get_config_var('EXT_SUFFIX')`` are used.
149 ``Python_Compiler_FOUND``
150 System has the Python compiler.
152 Path to the Python compiler. Only offered by IronPython.
153 ``Python_COMPILER_ID``
154 A short string unique to the compiler. Possible values include:
157 ``Python_DOTNET_LAUNCHER``
158 .. versionadded:: 3.18
160 The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
162 ``Python_Development_FOUND``
163 System has the Python development artifacts.
165 ``Python_Development.Module_FOUND``
166 .. versionadded:: 3.18
168 System has the Python development artifacts for Python module.
170 ``Python_Development.Embed_FOUND``
171 .. versionadded:: 3.18
173 System has the Python development artifacts for Python embedding.
175 ``Python_INCLUDE_DIRS``
177 The Python include directories.
179 ``Python_LINK_OPTIONS``
180 .. versionadded:: 3.19
182 The Python link options. Some configurations require specific link options
183 for a correct build and execution.
186 The Python libraries.
187 ``Python_LIBRARY_DIRS``
188 The Python library directories.
189 ``Python_RUNTIME_LIBRARY_DIRS``
190 The Python runtime library directories.
193 ``Python_VERSION_MAJOR``
194 Python major version.
195 ``Python_VERSION_MINOR``
196 Python minor version.
197 ``Python_VERSION_PATCH``
198 Python patch version.
200 ``Python_PyPy_VERSION``
201 .. versionadded:: 3.18
205 ``Python_NumPy_FOUND``
206 .. versionadded:: 3.14
208 System has the NumPy.
210 ``Python_NumPy_INCLUDE_DIRS``
211 .. versionadded:: 3.14
213 The NumPy include directories.
215 ``Python_NumPy_VERSION``
216 .. versionadded:: 3.14
224 Define the root directory of a Python installation.
226 ``Python_USE_STATIC_LIBS``
227 * If not defined, search for shared libraries and static libraries in that
229 * If set to TRUE, search **only** for static libraries.
230 * If set to FALSE, search **only** for shared libraries.
234 This hint will be ignored on ``Windows`` because static libraries are not
235 available on this platform.
238 .. versionadded:: 3.16
240 This variable defines which ABIs, as defined in :pep:`3149`, should be
245 This hint will be honored only when searched for ``Python`` version 3.
249 If ``Python_FIND_ABI`` is not defined, any ABI will be searched.
251 The ``Python_FIND_ABI`` variable is a 3-tuple specifying, in that order,
252 ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
253 Each element can be set to one of the following:
255 * ``ON``: Corresponding flag is selected.
256 * ``OFF``: Corresponding flag is not selected.
257 * ``ANY``: The two possibilities (``ON`` and ``OFF``) will be searched.
259 From this 3-tuple, various ABIs will be searched starting from the most
260 specialized to the most general. Moreover, ``debug`` versions will be
261 searched **after** ``non-debug`` ones.
263 For example, if we have::
265 set (Python_FIND_ABI "ON" "ANY" "ANY")
267 The following flags combinations will be appended, in that order, to the
268 artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
270 And to search any possible ABIs::
272 set (Python_FIND_ABI "ANY" "ANY" "ANY")
274 The following combinations, in that order, will be used: ``mu``, ``m``,
275 ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
279 This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
280 when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
281 `python.org <https://www.python.org/>`_ will be found only if value for
282 each flag is ``OFF`` or ``ANY``.
284 ``Python_FIND_STRATEGY``
285 .. versionadded:: 3.15
287 This variable defines how lookup will be done.
288 The ``Python_FIND_STRATEGY`` variable can be set to one of the following:
290 * ``VERSION``: Try to find the most recent version in all specified
292 This is the default if policy :policy:`CMP0094` is undefined or set to
294 * ``LOCATION``: Stops lookup as soon as a version satisfying version
295 constraints is founded.
296 This is the default if policy :policy:`CMP0094` is set to ``NEW``.
298 ``Python_FIND_REGISTRY``
299 .. versionadded:: 3.13
301 On Windows the ``Python_FIND_REGISTRY`` variable determine the order
302 of preference between registry and environment variables.
303 the ``Python_FIND_REGISTRY`` variable can be set to one of the following:
305 * ``FIRST``: Try to use registry before environment variables.
307 * ``LAST``: Try to use registry after environment variables.
308 * ``NEVER``: Never try to use registry.
310 ``Python_FIND_FRAMEWORK``
311 .. versionadded:: 3.15
313 On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
314 preference between Apple-style and unix-style package components.
315 This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
320 Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
322 If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
323 variable will be used, if any.
325 ``Python_FIND_VIRTUALENV``
326 .. versionadded:: 3.15
328 This variable defines the handling of virtual environments managed by
329 ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
330 is active (i.e. the ``activate`` script has been evaluated). In this case, it
331 takes precedence over ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
332 variables. The ``Python_FIND_VIRTUALENV`` variable can be set to one of the
335 * ``FIRST``: The virtual environment is used before any other standard
336 paths to look-up for the interpreter. This is the default.
337 * ``ONLY``: Only the virtual environment is used to look-up for the
339 * ``STANDARD``: The virtual environment is not used to look-up for the
340 interpreter but environment variable ``PATH`` is always considered.
341 In this case, variable ``Python_FIND_REGISTRY`` (Windows) or
342 ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
343 ``NEVER`` to select preferably the interpreter from the virtual
346 .. versionadded:: 3.17
347 Added support for ``conda`` environments.
351 If the component ``Development`` is requested, it is **strongly**
352 recommended to also include the component ``Interpreter`` to get expected
355 ``Python_FIND_IMPLEMENTATIONS``
356 .. versionadded:: 3.18
358 This variable defines, in an ordered list, the different implementations
359 which will be searched. The ``Python_FIND_IMPLEMENTATIONS`` variable can
360 hold the following values:
362 * ``CPython``: this is the standard implementation. Various products, like
363 ``Anaconda`` or ``ActivePython``, rely on this implementation.
364 * ``IronPython``: This implementation use the ``CSharp`` language for
365 ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
366 See `IronPython <https://ironpython.net>`_.
367 * ``PyPy``: This implementation use ``RPython`` language and
368 ``RPython translation toolchain`` to produce the python interpreter.
369 See `PyPy <https://www.pypy.org>`_.
371 The default value is:
373 * Windows platform: ``CPython``, ``IronPython``
374 * Other platforms: ``CPython``
378 This hint has the lowest priority of all hints, so even if, for example,
379 you specify ``IronPython`` first and ``CPython`` in second, a python
380 product based on ``CPython`` can be selected because, for example with
381 ``Python_FIND_STRATEGY=LOCATION``, each location will be search first for
382 ``IronPython`` and second for ``CPython``.
386 When ``IronPython`` is specified, on platforms other than ``Windows``, the
387 ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
388 through the ``PATH`` variable.
390 ``Python_FIND_UNVERSIONED_NAMES``
391 .. versionadded:: 3.20
393 This variable defines how the generic names will be searched. Currently, it
394 only applies to the generic names of the interpreter, namely, ``python3`` or
395 ``python2`` and ``python``.
396 The ``Python_FIND_UNVERSIONED_NAMES`` variable can be set to one of the
399 * ``FIRST``: The generic names are searched before the more specialized ones
400 (such as ``python2.5`` for example).
401 * ``LAST``: The generic names are searched after the more specialized ones.
403 * ``NEVER``: The generic name are not searched at all.
405 Artifacts Specification
406 ^^^^^^^^^^^^^^^^^^^^^^^
408 .. versionadded:: 3.16
410 To solve special cases, it is possible to specify directly the artifacts by
411 setting the following variables:
413 ``Python_EXECUTABLE``
414 The path to the interpreter.
417 The path to the compiler.
419 ``Python_DOTNET_LAUNCHER``
420 .. versionadded:: 3.18
422 The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
425 The path to the library. It will be used to compute the
426 variables ``Python_LIBRARIES``, ``Python_LIBRARY_DIRS`` and
427 ``Python_RUNTIME_LIBRARY_DIRS``.
429 ``Python_INCLUDE_DIR``
430 The path to the directory of the ``Python`` headers. It will be used to
431 compute the variable ``Python_INCLUDE_DIRS``.
433 ``Python_NumPy_INCLUDE_DIR``
434 The path to the directory of the ``NumPy`` headers. It will be used to
435 compute the variable ``Python_NumPy_INCLUDE_DIRS``.
439 All paths must be absolute. Any artifact specified with a relative path
444 When an artifact is specified, all ``HINTS`` will be ignored and no search
445 will be performed for this artifact.
447 If more than one artifact is specified, it is the user's responsibility to
448 ensure the consistency of the various artifacts.
450 By default, this module supports multiple calls in different directories of a
451 project with different version/component requirements while providing correct
452 and consistent results for each call. To support this behavior, ``CMake`` cache
453 is not used in the traditional way which can be problematic for interactive
454 specification. So, to enable also interactive specification, module behavior
455 can be controlled with the following variable:
457 ``Python_ARTIFACTS_INTERACTIVE``
458 .. versionadded:: 3.18
460 Selects the behavior of the module. This is a boolean variable:
462 * If set to ``TRUE``: Create CMake cache entries for the above artifact
463 specification variables so that users can edit them interactively.
464 This disables support for multiple version/component requirements.
465 * If set to ``FALSE`` or undefined: Enable multiple version/component
471 This module defines the command ``Python_add_library`` (when
472 :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
473 :command:`add_library` and adds a dependency to target ``Python::Python`` or,
474 when library type is ``MODULE``, to target ``Python::Module`` and takes care of
475 Python module naming rules::
477 Python_add_library (<name> [STATIC | SHARED | MODULE [WITH_SOABI]]
478 <source1> [<source2> ...])
480 If the library type is not specified, ``MODULE`` is assumed.
482 .. versionadded:: 3.17
483 For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
484 module suffix will include the ``Python_SOABI`` value, if any.
485 #]=======================================================================]
489 # numbers and boolean constants
490 cmake_policy (SET CMP0012 NEW)
493 set (_PYTHON_PREFIX Python)
494 unset (_Python_REQUIRED_VERSION_MAJOR)
495 unset (_Python_REQUIRED_VERSIONS)
497 if (Python_FIND_VERSION_RANGE)
498 # compute list of major versions
499 foreach (_Python_MAJOR IN ITEMS 3 2)
500 if (_Python_MAJOR VERSION_GREATER_EQUAL Python_FIND_VERSION_MIN_MAJOR
501 AND ((Python_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _Python_MAJOR VERSION_LESS_EQUAL Python_FIND_VERSION_MAX)
502 OR (Python_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _Python_MAJOR VERSION_LESS Python_FIND_VERSION_MAX)))
503 list (APPEND _Python_REQUIRED_VERSIONS ${_Python_MAJOR})
506 list (LENGTH _Python_REQUIRED_VERSIONS _Python_VERSION_COUNT)
507 if (_Python_VERSION_COUNT EQUAL 0)
508 unset (_Python_REQUIRED_VERSIONS)
509 elseif (_Python_VERSION_COUNT EQUAL 1)
510 set (_Python_REQUIRED_VERSION_MAJOR ${_Python_REQUIRED_VERSIONS})
512 elseif (DEFINED Python_FIND_VERSION)
513 set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
515 set (_Python_REQUIRED_VERSIONS 3 2)
518 if (_Python_REQUIRED_VERSION_MAJOR)
519 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
520 elseif (_Python_REQUIRED_VERSIONS)
521 # iterate over versions in quiet and NOT required modes to avoid multiple
522 # "Found" messages and prematurally failure.
523 set (_Python_QUIETLY ${Python_FIND_QUIETLY})
524 set (_Python_REQUIRED ${Python_FIND_REQUIRED})
525 set (Python_FIND_QUIETLY TRUE)
526 set (Python_FIND_REQUIRED FALSE)
528 set (_Python_REQUIRED_VERSION_LAST 2)
530 unset (_Python_INPUT_VARS)
531 foreach (_Python_ITEM IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
532 Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
533 if (NOT DEFINED ${_Python_ITEM})
534 list (APPEND _Python_INPUT_VARS ${_Python_ITEM})
538 foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
539 set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
540 include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
542 _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
545 # clean-up INPUT variables not set by the user
546 foreach (_Python_ITEM IN LISTS _Python_INPUT_VARS)
547 unset (${_Python_ITEM})
549 # clean-up some CACHE variables to ensure look-up restart from scratch
550 foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
551 unset (${_Python_ITEM} CACHE)
555 unset (Python_FIND_VERSION)
557 set (Python_FIND_QUIETLY ${_Python_QUIETLY})
558 set (Python_FIND_REQUIRED ${_Python_REQUIRED})
559 if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
560 # call again validation command to get "Found" or error message
561 find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
562 REQUIRED_VARS ${_Python_REQUIRED_VARS}
563 VERSION_VAR Python_VERSION)
566 # supported versions not in the specified range. Call final check
567 if (NOT Python_FIND_COMPONENTS)
568 set (Python_FIND_COMPONENTS Interpreter)
569 set (Python_FIND_REQUIRED_Interpreter TRUE)
572 include (${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
573 find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
574 VERSION_VAR Python_VERSION
575 REASON_FAILURE_MESSAGE "Version range specified \"${Python_FIND_VERSION_RANGE}\" does not include supported versions")
578 if (COMMAND __Python_add_library)
579 macro (Python_add_library)
580 __Python_add_library (Python ${ARGV})
584 unset (_PYTHON_PREFIX)