FindPython.cmake 27 KB

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