FindPython.cmake 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPython
  5. ----------
  6. .. versionadded:: 3.12
  7. Find Python interpreter, compiler and development environment (include
  8. directories and libraries).
  9. .. versionadded:: 3.19
  10. When a version is requested, it can be specified as a simple value or as a
  11. range. For a detailed description of version range usage and capabilities,
  12. refer to the :command:`find_package` command.
  13. The following components are supported:
  14. * ``Interpreter``: search for Python interpreter.
  15. * ``Compiler``: search for Python compiler. Only offered by IronPython.
  16. * ``Development``: search for development artifacts (include directories and
  17. libraries).
  18. .. versionadded:: 3.18
  19. This component includes two sub-components which can be specified
  20. independently:
  21. * ``Development.Module``: search for artifacts for Python module
  22. developments.
  23. * ``Development.Embed``: search for artifacts for Python embedding
  24. developments.
  25. .. versionadded:: 3.26
  26. * ``Development.SABIModule``: search for artifacts for Python module
  27. developments using the
  28. `Stable Application Binary Interface <https://docs.python.org/3/c-api/stable.html>`_.
  29. This component is available only for version ``3.2`` and upper.
  30. * ``NumPy``: search for NumPy include directories.
  31. .. versionadded:: 3.14
  32. Added the ``NumPy`` component.
  33. If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
  34. If component ``Development`` is specified, it implies sub-components
  35. ``Development.Module`` and ``Development.Embed``.
  36. To ensure consistent versions between components ``Interpreter``, ``Compiler``,
  37. ``Development`` (or one of its sub-components) and ``NumPy``, specify all
  38. components at the same time:
  39. .. code-block:: cmake
  40. find_package (Python COMPONENTS Interpreter Development)
  41. This module looks preferably for version 3 of Python. If not found, version 2
  42. is searched.
  43. To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
  44. :module:`FindPython2` modules rather than this one.
  45. .. note::
  46. If components ``Interpreter`` and ``Development`` (or one of its
  47. sub-components) are both specified, this module search only for interpreter
  48. with same platform architecture as the one defined by CMake
  49. configuration. This constraint does not apply if only ``Interpreter``
  50. component is specified.
  51. Imported Targets
  52. ^^^^^^^^^^^^^^^^
  53. This module defines the following :ref:`Imported Targets <Imported Targets>`:
  54. .. versionchanged:: 3.14
  55. :ref:`Imported Targets <Imported Targets>` are only created when
  56. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``.
  57. ``Python::Interpreter``
  58. Python interpreter. This target is defined only if the ``Interpreter``
  59. component is found.
  60. ``Python::InterpreterDebug``
  61. .. versionadded:: 3.30
  62. Python debug interpreter. This target is defined only if the ``Interpreter``
  63. component is found and the ``Python_EXECUTABLE_DEBUG`` variable is defined.
  64. The target is only defined on the ``Windows`` platform.
  65. ``Python::InterpreterMultiConfig``
  66. .. versionadded:: 3.30
  67. Python interpreter. The release or debug version of the interpreter will be
  68. used, based on the context (platform, configuration).
  69. This target is defined only if the ``Interpreter`` component is found
  70. ``Python::Compiler``
  71. Python compiler. This target is defined only if the ``Compiler`` component is
  72. found.
  73. ``Python::Module``
  74. .. versionadded:: 3.15
  75. Python library for Python module. Target defined if component
  76. ``Development.Module`` is found.
  77. ``Python::SABIModule``
  78. .. versionadded:: 3.26
  79. Python library for Python module using the Stable Application Binary
  80. Interface. Target defined if component ``Development.SABIModule`` is found.
  81. ``Python::Python``
  82. Python library for Python embedding. Target defined if component
  83. ``Development.Embed`` is found.
  84. ``Python::NumPy``
  85. .. versionadded:: 3.14
  86. NumPy Python library. Target defined if component ``NumPy`` is found.
  87. Result Variables
  88. ^^^^^^^^^^^^^^^^
  89. This module will set the following variables in your project
  90. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  91. ``Python_FOUND``
  92. System has the Python requested components.
  93. ``Python_Interpreter_FOUND``
  94. System has the Python interpreter.
  95. ``Python_EXECUTABLE``
  96. Path to the Python interpreter.
  97. ``Python_EXECUTABLE_DEBUG``
  98. .. versionadded:: 3.30
  99. Path to the debug Python interpreter. It is only defined on the ``Windows``
  100. platform.
  101. ``Python_INTERPRETER``
  102. .. versionadded:: 3.30
  103. Path to the Python interpreter, defined as a
  104. :manual:`generator expression <cmake-generator-expressions(7)>` selecting
  105. the ``Python_EXECUTABLE`` or ``Python_EXECUTABLE_DEBUG`` variable based on
  106. the context (platform, configuration).
  107. ``Python_INTERPRETER_ID``
  108. A short string unique to the interpreter. Possible values include:
  109. * Python
  110. * ActivePython
  111. * Anaconda
  112. * Canopy
  113. * IronPython
  114. * PyPy
  115. ``Python_STDLIB``
  116. Standard platform independent installation directory.
  117. Information returned by ``sysconfig.get_path('stdlib')``.
  118. ``Python_STDARCH``
  119. Standard platform dependent installation directory.
  120. Information returned by ``sysconfig.get_path('platstdlib')``.
  121. ``Python_SITELIB``
  122. Third-party platform independent installation directory.
  123. Information returned by ``sysconfig.get_path('purelib')``.
  124. ``Python_SITEARCH``
  125. Third-party platform dependent installation directory.
  126. Information returned by ``sysconfig.get_path('platlib')``.
  127. ``Python_SOABI``
  128. .. versionadded:: 3.17
  129. Extension suffix for modules.
  130. Information computed from ``sysconfig.get_config_var('EXT_SUFFIX')`` or
  131. ``sysconfig.get_config_var('SOABI')`` or
  132. ``python3-config --extension-suffix``.
  133. ``Python_SOSABI``
  134. .. versionadded:: 3.26
  135. Extension suffix for modules using the Stable Application Binary Interface.
  136. Information computed from ``importlib.machinery.EXTENSION_SUFFIXES`` if the
  137. COMPONENT ``Interpreter`` was specified. Otherwise, the extension is ``abi3``
  138. except for ``Windows``, ``MSYS`` and ``CYGWIN`` for which this is an empty
  139. string.
  140. ``Python_Compiler_FOUND``
  141. System has the Python compiler.
  142. ``Python_COMPILER``
  143. Path to the Python compiler. Only offered by IronPython.
  144. ``Python_COMPILER_ID``
  145. A short string unique to the compiler. Possible values include:
  146. * IronPython
  147. ``Python_DOTNET_LAUNCHER``
  148. .. versionadded:: 3.18
  149. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  150. ``Python_Development_FOUND``
  151. System has the Python development artifacts.
  152. ``Python_Development.Module_FOUND``
  153. .. versionadded:: 3.18
  154. System has the Python development artifacts for Python module.
  155. ``Python_Development.SABIModule_FOUND``
  156. .. versionadded:: 3.26
  157. System has the Python development artifacts for Python module using the
  158. Stable Application Binary Interface.
  159. ``Python_Development.Embed_FOUND``
  160. .. versionadded:: 3.18
  161. System has the Python development artifacts for Python embedding.
  162. ``Python_INCLUDE_DIRS``
  163. The Python include directories.
  164. ``Python_DEFINITIONS``
  165. .. versionadded:: 3.30.3
  166. The Python preprocessor definitions.
  167. ``Python_DEBUG_POSTFIX``
  168. .. versionadded:: 3.30
  169. Postfix of debug python module. This variable can be used to define the
  170. :prop_tgt:`DEBUG_POSTFIX` target property.
  171. ``Python_LINK_OPTIONS``
  172. .. versionadded:: 3.19
  173. The Python link options. Some configurations require specific link options
  174. for a correct build and execution.
  175. ``Python_LIBRARIES``
  176. The Python libraries.
  177. ``Python_LIBRARY_DIRS``
  178. The Python library directories.
  179. ``Python_RUNTIME_LIBRARY_DIRS``
  180. The Python runtime library directories.
  181. ``Python_SABI_LIBRARIES``
  182. .. versionadded:: 3.26
  183. The Python libraries for the Stable Application Binary Interface.
  184. ``Python_SABI_LIBRARY_DIRS``
  185. .. versionadded:: 3.26
  186. The Python ``SABI`` library directories.
  187. ``Python_RUNTIME_SABI_LIBRARY_DIRS``
  188. .. versionadded:: 3.26
  189. The Python runtime ``SABI`` library directories.
  190. ``Python_VERSION``
  191. Python version.
  192. ``Python_VERSION_MAJOR``
  193. Python major version.
  194. ``Python_VERSION_MINOR``
  195. Python minor version.
  196. ``Python_VERSION_PATCH``
  197. Python patch version.
  198. ``Python_PyPy_VERSION``
  199. .. versionadded:: 3.18
  200. Python PyPy version.
  201. ``Python_NumPy_FOUND``
  202. .. versionadded:: 3.14
  203. System has the NumPy.
  204. ``Python_NumPy_INCLUDE_DIRS``
  205. .. versionadded:: 3.14
  206. The NumPy include directories.
  207. ``Python_NumPy_VERSION``
  208. .. versionadded:: 3.14
  209. The NumPy version.
  210. Hints
  211. ^^^^^
  212. ``Python_ROOT_DIR``
  213. Define the root directory of a Python installation.
  214. ``Python_USE_STATIC_LIBS``
  215. * If not defined, search for shared libraries and static libraries in that
  216. order.
  217. * If set to TRUE, search **only** for static libraries.
  218. * If set to FALSE, search **only** for shared libraries.
  219. .. note::
  220. This hint will be ignored on ``Windows`` because static libraries are not
  221. available on this platform.
  222. ``Python_FIND_ABI``
  223. .. versionadded:: 3.16
  224. This variable defines which ABIs, as defined in :pep:`3149`, should be
  225. searched.
  226. .. note::
  227. This hint will be honored only when searched for ``Python`` version 3.
  228. The ``Python_FIND_ABI`` variable is a 4-tuple specifying, in that order,
  229. ``pydebug`` (``d``), ``pymalloc`` (``m``), ``unicode`` (``u``) and
  230. ``gil_disabled`` (``t``) flags.
  231. .. versionadded:: 3.30
  232. A fourth element, specifying the ``gil_disabled`` flag (i.e. free
  233. threaded python), is added and is optional. If not specified, the value is
  234. ``OFF``.
  235. Each element can be set to one of the following:
  236. * ``ON``: Corresponding flag is selected.
  237. * ``OFF``: Corresponding flag is not selected.
  238. * ``ANY``: The two possibilities (``ON`` and ``OFF``) will be searched.
  239. .. note::
  240. If ``Python3_FIND_ABI`` is not defined, any ABI, excluding the
  241. ``gil_disabled`` flag, will be searched.
  242. From this 4-tuple, various ABIs will be searched starting from the most
  243. specialized to the most general. Moreover, when ``ANY`` is specified for
  244. ``pydebug`` and ``gil_disabled``, ``debug`` and ``free threaded`` versions
  245. will be searched **after** ``non-debug`` and ``non-gil-disabled`` ones.
  246. For example, if we have:
  247. .. code-block:: cmake
  248. set (Python_FIND_ABI "ON" "ANY" "ANY" "ON")
  249. The following flags combinations will be appended, in that order, to the
  250. artifact names: ``tdmu``, ``tdm``, ``tdu``, and ``td``.
  251. And to search any possible ABIs:
  252. .. code-block:: cmake
  253. set (Python_FIND_ABI "ANY" "ANY" "ANY" "ANY")
  254. The following combinations, in that order, will be used: ``mu``, ``m``,
  255. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du``, ``d``, ``tmu``, ``tm``, ``tu``,
  256. ``t``, ``tdmu``, ``tdm``, ``tdu``, and ``td``.
  257. .. note::
  258. This hint is useful only on ``POSIX`` systems except for the
  259. ``gil_disabled`` flag. So, on ``Windows`` systems,
  260. when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
  261. `python.org <https://www.python.org/>`_ will be found only if the value for
  262. each flag is ``OFF`` or ``ANY`` except for the fourth one
  263. (``gil_disabled``).
  264. ``Python_FIND_STRATEGY``
  265. .. versionadded:: 3.15
  266. This variable defines how lookup will be done.
  267. The ``Python_FIND_STRATEGY`` variable can be set to one of the following:
  268. * ``VERSION``: Try to find the most recent version in all specified
  269. locations.
  270. This is the default if policy :policy:`CMP0094` is undefined or set to
  271. ``OLD``.
  272. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  273. constraints is founded.
  274. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  275. See also ``Python_FIND_UNVERSIONED_NAMES``.
  276. ``Python_FIND_REGISTRY``
  277. .. versionadded:: 3.13
  278. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  279. of preference between registry and environment variables.
  280. the ``Python_FIND_REGISTRY`` variable can be set to one of the following:
  281. * ``FIRST``: Try to use registry before environment variables.
  282. This is the default.
  283. * ``LAST``: Try to use registry after environment variables.
  284. * ``NEVER``: Never try to use registry.
  285. ``Python_FIND_FRAMEWORK``
  286. .. versionadded:: 3.15
  287. On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
  288. preference between Apple-style and unix-style package components.
  289. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  290. variable.
  291. .. note::
  292. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  293. If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  294. variable will be used, if any.
  295. ``Python_FIND_VIRTUALENV``
  296. .. versionadded:: 3.15
  297. This variable defines the handling of virtual environments managed by
  298. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  299. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  300. takes precedence over ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  301. variables. The ``Python_FIND_VIRTUALENV`` variable can be set to one of the
  302. following:
  303. * ``FIRST``: The virtual environment is used before any other standard
  304. paths to look-up for the interpreter. This is the default.
  305. * ``ONLY``: Only the virtual environment is used to look-up for the
  306. interpreter.
  307. * ``STANDARD``: The virtual environment is not used to look-up for the
  308. interpreter but environment variable ``PATH`` is always considered.
  309. In this case, variable ``Python_FIND_REGISTRY`` (Windows) or
  310. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  311. ``NEVER`` to select preferably the interpreter from the virtual
  312. environment.
  313. .. versionadded:: 3.17
  314. Added support for ``conda`` environments.
  315. .. note::
  316. If the component ``Development`` is requested (or one of its
  317. sub-components) and is not found or the wrong artifacts are returned,
  318. including also the component ``Interpreter`` may be helpful.
  319. ``Python_FIND_IMPLEMENTATIONS``
  320. .. versionadded:: 3.18
  321. This variable defines, in an ordered list, the different implementations
  322. which will be searched. The ``Python_FIND_IMPLEMENTATIONS`` variable can
  323. hold the following values:
  324. * ``CPython``: this is the standard implementation. Various products, like
  325. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  326. * ``IronPython``: This implementation use the ``CSharp`` language for
  327. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  328. See `IronPython <https://ironpython.net>`_.
  329. * ``PyPy``: This implementation use ``RPython`` language and
  330. ``RPython translation toolchain`` to produce the python interpreter.
  331. See `PyPy <https://pypy.org>`_.
  332. The default value is:
  333. * Windows platform: ``CPython``, ``IronPython``
  334. * Other platforms: ``CPython``
  335. .. note::
  336. This hint has the lowest priority of all hints, so even if, for example,
  337. you specify ``IronPython`` first and ``CPython`` in second, a python
  338. product based on ``CPython`` can be selected because, for example with
  339. ``Python_FIND_STRATEGY=LOCATION``, each location will be search first for
  340. ``IronPython`` and second for ``CPython``.
  341. .. note::
  342. When ``IronPython`` is specified, on platforms other than ``Windows``, the
  343. ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
  344. through the ``PATH`` variable.
  345. ``Python_FIND_UNVERSIONED_NAMES``
  346. .. versionadded:: 3.20
  347. This variable defines how the generic names will be searched. Currently, it
  348. only applies to the generic names of the interpreter, namely, ``python3`` or
  349. ``python2`` and ``python``.
  350. The ``Python_FIND_UNVERSIONED_NAMES`` variable can be set to one of the
  351. following values:
  352. * ``FIRST``: The generic names are searched before the more specialized ones
  353. (such as ``python2.5`` for example).
  354. * ``LAST``: The generic names are searched after the more specialized ones.
  355. This is the default.
  356. * ``NEVER``: The generic name are not searched at all.
  357. See also ``Python_FIND_STRATEGY``.
  358. Artifacts Specification
  359. ^^^^^^^^^^^^^^^^^^^^^^^
  360. .. versionadded:: 3.16
  361. To solve special cases, it is possible to specify directly the artifacts by
  362. setting the following variables:
  363. ``Python_EXECUTABLE``
  364. The path to the interpreter.
  365. ``Python_COMPILER``
  366. The path to the compiler.
  367. ``Python_DOTNET_LAUNCHER``
  368. .. versionadded:: 3.18
  369. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  370. ``Python_LIBRARY``
  371. The path to the library. It will be used to compute the
  372. variables ``Python_LIBRARIES``, ``Python_LIBRARY_DIRS`` and
  373. ``Python_RUNTIME_LIBRARY_DIRS``.
  374. ``Python_SABI_LIBRARY``
  375. .. versionadded:: 3.26
  376. The path to the library for Stable Application Binary Interface. It will be
  377. used to compute the variables ``Python_SABI_LIBRARIES``,
  378. ``Python_SABI_LIBRARY_DIRS`` and ``Python_RUNTIME_SABI_LIBRARY_DIRS``.
  379. ``Python_INCLUDE_DIR``
  380. The path to the directory of the ``Python`` headers. It will be used to
  381. compute the variable ``Python_INCLUDE_DIRS``.
  382. ``Python_NumPy_INCLUDE_DIR``
  383. The path to the directory of the ``NumPy`` headers. It will be used to
  384. compute the variable ``Python_NumPy_INCLUDE_DIRS``.
  385. .. note::
  386. All paths must be absolute. Any artifact specified with a relative path
  387. will be ignored.
  388. .. note::
  389. When an artifact is specified, all ``HINTS`` will be ignored and no search
  390. will be performed for this artifact.
  391. If more than one artifact is specified, it is the user's responsibility to
  392. ensure the consistency of the various artifacts.
  393. By default, this module supports multiple calls in different directories of a
  394. project with different version/component requirements while providing correct
  395. and consistent results for each call. To support this behavior, CMake cache
  396. is not used in the traditional way which can be problematic for interactive
  397. specification. So, to enable also interactive specification, module behavior
  398. can be controlled with the following variable:
  399. ``Python_ARTIFACTS_INTERACTIVE``
  400. .. versionadded:: 3.18
  401. Selects the behavior of the module. This is a boolean variable:
  402. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  403. specification variables so that users can edit them interactively.
  404. This disables support for multiple version/component requirements.
  405. * If set to ``FALSE`` or undefined: Enable multiple version/component
  406. requirements.
  407. ``Python_ARTIFACTS_PREFIX``
  408. .. versionadded:: 4.0
  409. Define a custom prefix which will be used for the definition of all the
  410. result variables, targets, and commands. By using this variable, this module
  411. supports multiple calls in the same directory with different
  412. version/component requirements.
  413. For example, in case of cross-compilation, development components are needed
  414. but the native python interpreter can also be required:
  415. .. code-block:: cmake
  416. find_package(Python COMPONENTS Development)
  417. set(Python_ARTIFACTS_PREFIX "_HOST")
  418. find_package(Python COMPONENTS Interpreter)
  419. # Here Python_HOST_EXECUTABLE and Python_HOST::Interpreter artifacts are defined
  420. .. note::
  421. For consistency with standard behavior of modules, the various standard
  422. ``_FOUND`` variables (i.e. without the custom prefix) are also defined by
  423. each call to the :command:`find_package` command.
  424. Commands
  425. ^^^^^^^^
  426. This module defines the command ``Python_add_library`` (when
  427. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  428. :command:`add_library` and adds a dependency to target ``Python::Python`` or,
  429. when library type is ``MODULE``, to target ``Python::Module`` or
  430. ``Python::SABIModule`` (when ``USE_SABI`` option is specified) and takes care
  431. of Python module naming rules:
  432. .. code-block:: cmake
  433. Python_add_library (<name> [STATIC | SHARED | MODULE [USE_SABI <version>] [WITH_SOABI]]
  434. <source1> [<source2> ...])
  435. If the library type is not specified, ``MODULE`` is assumed.
  436. .. versionadded:: 3.17
  437. For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
  438. module suffix will include the ``Python_SOABI`` value, if any.
  439. .. versionadded:: 3.26
  440. For ``MODULE`` type, if the option ``USE_SABI`` is specified, the
  441. preprocessor definition ``Py_LIMITED_API`` will be specified, as ``PRIVATE``,
  442. for the target ``<name>`` with the value computed from ``<version>`` argument.
  443. The expected format for ``<version>`` is ``major[.minor]``, where each
  444. component is a numeric value. If ``minor`` component is specified, the
  445. version should be, at least, ``3.2`` which is the version where the
  446. `Stable Application Binary Interface <https://docs.python.org/3/c-api/stable.html>`_
  447. was introduced. Specifying only major version ``3`` is equivalent to ``3.2``.
  448. When option ``WITH_SOABI`` is also specified, the module suffix will include
  449. the ``Python_SOSABI`` value, if any.
  450. .. versionadded:: 3.30
  451. For ``MODULE`` type, the :prop_tgt:`DEBUG_POSTFIX` target property is
  452. initialized with the value of ``Python_DEBUG_POSTFIX`` variable if defined.
  453. #]=======================================================================]
  454. cmake_policy(PUSH)
  455. # foreach loop variable scope
  456. cmake_policy (SET CMP0124 NEW)
  457. set (_PYTHON_BASE Python)
  458. if(${_PYTHON_BASE}_ARTIFACTS_PREFIX)
  459. set(_PYTHON_PREFIX "${_PYTHON_BASE}${${_PYTHON_BASE}_ARTIFACTS_PREFIX}")
  460. else()
  461. set(_PYTHON_PREFIX "${_PYTHON_BASE}")
  462. endif()
  463. unset (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
  464. unset (_${_PYTHON_PREFIX}_REQUIRED_VERSIONS)
  465. if (Python_FIND_VERSION_RANGE)
  466. # compute list of major versions
  467. foreach (version_major IN ITEMS 3 2)
  468. if (version_major VERSION_GREATER_EQUAL Python_FIND_VERSION_MIN_MAJOR
  469. AND ((Python_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND version_major VERSION_LESS_EQUAL Python_FIND_VERSION_MAX)
  470. OR (Python_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND version_major VERSION_LESS Python_FIND_VERSION_MAX)))
  471. list (APPEND _${_PYTHON_PREFIX}_REQUIRED_VERSIONS ${version_major})
  472. endif()
  473. endforeach()
  474. list (LENGTH _${_PYTHON_PREFIX}_REQUIRED_VERSIONS _${_PYTHON_PREFIX}_VERSION_COUNT)
  475. if (_${_PYTHON_PREFIX}_VERSION_COUNT EQUAL 0)
  476. unset (_${_PYTHON_PREFIX}_REQUIRED_VERSIONS)
  477. elseif (_${_PYTHON_PREFIX}_VERSION_COUNT EQUAL 1)
  478. set (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR ${_${_PYTHON_PREFIX}_REQUIRED_VERSIONS})
  479. endif()
  480. elseif (DEFINED Python_FIND_VERSION)
  481. set (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  482. else()
  483. set (_${_PYTHON_PREFIX}_REQUIRED_VERSIONS 3 2)
  484. endif()
  485. if (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR)
  486. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  487. elseif (_${_PYTHON_PREFIX}_REQUIRED_VERSIONS)
  488. # iterate over versions in quiet and NOT required modes to avoid multiple
  489. # "Found" messages and prematurally failure.
  490. set (_${_PYTHON_PREFIX}_QUIETLY ${Python_FIND_QUIETLY})
  491. set (_${_PYTHON_PREFIX}_REQUIRED ${Python_FIND_REQUIRED})
  492. set (Python_FIND_QUIETLY TRUE)
  493. set (Python_FIND_REQUIRED FALSE)
  494. set (_${_PYTHON_PREFIX}_REQUIRED_VERSION_LAST 2)
  495. unset (_${_PYTHON_PREFIX}_INPUT_VARS)
  496. foreach (item IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
  497. Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
  498. if (NOT DEFINED ${item})
  499. list (APPEND _${_PYTHON_PREFIX}_INPUT_VARS ${item})
  500. endif()
  501. endforeach()
  502. foreach (_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR IN LISTS _${_PYTHON_PREFIX}_REQUIRED_VERSIONS)
  503. set (Python_FIND_VERSION ${_${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR})
  504. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  505. if (Python_FOUND OR
  506. _${_PYTHON_PREFIX}_REQUIRED_VERSION_MAJOR EQUAL _${_PYTHON_PREFIX}_REQUIRED_VERSION_LAST)
  507. break()
  508. endif()
  509. # clean-up INPUT variables not set by the user
  510. foreach (item IN LISTS _${_PYTHON_PREFIX}_INPUT_VARS)
  511. unset (${item})
  512. endforeach()
  513. # clean-up some CACHE variables to ensure look-up restart from scratch
  514. foreach (item IN LISTS _${_PYTHON_PREFIX}_CACHED_VARS)
  515. unset (${item} CACHE)
  516. endforeach()
  517. endforeach()
  518. unset (Python_FIND_VERSION)
  519. set (Python_FIND_QUIETLY ${_${_PYTHON_PREFIX}_QUIETLY})
  520. set (Python_FIND_REQUIRED ${_${_PYTHON_PREFIX}_REQUIRED})
  521. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  522. # call again validation command to get "Found" or error message
  523. find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
  524. REQUIRED_VARS ${_${_PYTHON_PREFIX}_REQUIRED_VARS}
  525. VERSION_VAR Python_VERSION)
  526. endif()
  527. else()
  528. # supported versions not in the specified range. Call final check
  529. if (NOT Python_FIND_COMPONENTS)
  530. set (Python_FIND_COMPONENTS Interpreter)
  531. set (Python_FIND_REQUIRED_Interpreter TRUE)
  532. endif()
  533. include(FindPackageHandleStandardArgs)
  534. find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
  535. VERSION_VAR Python_VERSION
  536. REASON_FAILURE_MESSAGE "Version range specified \"${Python_FIND_VERSION_RANGE}\" does not include supported versions")
  537. endif()
  538. if (COMMAND __${_PYTHON_PREFIX}_add_library AND NOT COMMAND ${_PYTHON_PREFIX}_add_library)
  539. cmake_language(EVAL CODE
  540. "macro (${_PYTHON_PREFIX}_add_library)
  541. __${_PYTHON_PREFIX}_add_library (${_PYTHON_PREFIX} \${ARGV})
  542. endmacro()")
  543. endif()
  544. unset (_PYTHON_BASE)
  545. unset (_PYTHON_PREFIX)
  546. cmake_policy(POP)