FindPython3.cmake 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. FindPython3
  5. -----------
  6. .. versionadded:: 3.12
  7. Find Python 3 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 3 interpreter
  15. * ``Compiler``: search for Python 3 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 3 module
  22. developments.
  23. * ``Development.Embed``: search for artifacts for Python 3 embedding
  24. developments.
  25. .. versionadded:: 3.26
  26. * ``Development.SABIModule``: search for artifacts for Python 3 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. find_package (Python3 COMPONENTS Interpreter Development)
  40. This module looks only for version 3 of Python. This module can be used
  41. concurrently with :module:`FindPython2` module to use both Python versions.
  42. The :module:`FindPython` module can be used if Python version does not matter
  43. for you.
  44. .. note::
  45. If components ``Interpreter`` and ``Development`` (or one of its
  46. sub-components) are both specified, this module search only for interpreter
  47. with same platform architecture as the one defined by CMake
  48. configuration. This constraint does not apply if only ``Interpreter``
  49. component is specified.
  50. Imported Targets
  51. ^^^^^^^^^^^^^^^^
  52. This module defines the following :ref:`Imported Targets <Imported Targets>`:
  53. .. versionchanged:: 3.14
  54. :ref:`Imported Targets <Imported Targets>` are only created when
  55. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``.
  56. ``Python3::Interpreter``
  57. Python 3 interpreter. This target is defined only if the ``Interpreter``
  58. component is found.
  59. ``Python3::InterpreterDebug``
  60. .. versionadded:: 3.30
  61. Python 3 debug interpreter. This target is defined only if the
  62. ``Interpreter`` component is found and the ``Python3_EXECUTABLE_DEBUG``
  63. variable is defined. The target is only defined on the ``Windows`` platform.
  64. ``Python3::InterpreterMultiConfig``
  65. .. versionadded:: 3.30
  66. Python 3 interpreter. The release or debug version of the interpreter will be
  67. used, based on the context (platform, configuration).
  68. This target is defined only if the ``Interpreter`` component is found
  69. ``Python3::Compiler``
  70. Python 3 compiler. This target is defined only if the ``Compiler`` component
  71. is found.
  72. ``Python3::Module``
  73. .. versionadded:: 3.15
  74. Python 3 library for Python module. Target defined if component
  75. ``Development.Module`` is found.
  76. ``Python3::SABIModule``
  77. .. versionadded:: 3.26
  78. Python 3 library for Python module using the Stable Application Binary
  79. Interface. Target defined if component ``Development.SABIModule`` is found.
  80. ``Python3::Python``
  81. Python 3 library for Python embedding. Target defined if component
  82. ``Development.Embed`` is found.
  83. ``Python3::NumPy``
  84. .. versionadded:: 3.14
  85. NumPy library for Python 3. Target defined if component ``NumPy`` is found.
  86. Result Variables
  87. ^^^^^^^^^^^^^^^^
  88. This module will set the following variables in your project
  89. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  90. ``Python3_FOUND``
  91. System has the Python 3 requested components.
  92. ``Python3_Interpreter_FOUND``
  93. System has the Python 3 interpreter.
  94. ``Python3_EXECUTABLE``
  95. Path to the Python 3 interpreter.
  96. ``Python3_EXECUTABLE_DEBUG``
  97. .. versionadded:: 3.30
  98. Path to the debug Python 3 interpreter. It is only defined on ``Windows``
  99. platform.
  100. ``Python3_INTERPRETER``
  101. .. versionadded:: 3.30
  102. Path to the Python 3 interpreter, defined as a
  103. :manual:`generator expression <cmake-generator-expressions(7)>` selecting
  104. the ``Python3_EXECUTABLE`` or ``Python3_EXECUTABLE_DEBUG`` variable based on
  105. the context (platform, configuration).
  106. ``Python3_INTERPRETER_ID``
  107. A short string unique to the interpreter. Possible values include:
  108. * Python
  109. * ActivePython
  110. * Anaconda
  111. * Canopy
  112. * IronPython
  113. * PyPy
  114. ``Python3_STDLIB``
  115. Standard platform independent installation directory.
  116. Information returned by ``sysconfig.get_path('stdlib')``.
  117. ``Python3_STDARCH``
  118. Standard platform dependent installation directory.
  119. Information returned by ``sysconfig.get_path('platstdlib')``.
  120. ``Python3_SITELIB``
  121. Third-party platform independent installation directory.
  122. Information returned by ``sysconfig.get_path('purelib')``.
  123. ``Python3_SITEARCH``
  124. Third-party platform dependent installation directory.
  125. Information returned by ``sysconfig.get_path('platlib')``.
  126. ``Python3_SOABI``
  127. .. versionadded:: 3.17
  128. Extension suffix for modules.
  129. Information computed from ``sysconfig.get_config_var('EXT_SUFFIX')`` or
  130. ``sysconfig.get_config_var('SOABI')`` or
  131. ``python3-config --extension-suffix``.
  132. ``Python3_SOSABI``
  133. .. versionadded:: 3.26
  134. Extension suffix for modules using the Stable Application Binary Interface.
  135. Information computed from ``importlib.machinery.EXTENSION_SUFFIXES`` if the
  136. COMPONENT ``Interpreter`` was specified. Otherwise, the extension is ``abi3``
  137. except for ``Windows``, ``MSYS`` and ``CYGWIN`` for which this is an empty
  138. string.
  139. ``Python3_Compiler_FOUND``
  140. System has the Python 3 compiler.
  141. ``Python3_COMPILER``
  142. Path to the Python 3 compiler. Only offered by IronPython.
  143. ``Python3_COMPILER_ID``
  144. A short string unique to the compiler. Possible values include:
  145. * IronPython
  146. ``Python3_DOTNET_LAUNCHER``
  147. .. versionadded:: 3.18
  148. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  149. ``Python3_Development_FOUND``
  150. System has the Python 3 development artifacts.
  151. ``Python3_Development.Module_FOUND``
  152. .. versionadded:: 3.18
  153. System has the Python 3 development artifacts for Python module.
  154. ``Python3_Development.SABIModule_FOUND``
  155. .. versionadded:: 3.26
  156. System has the Python 3 development artifacts for Python module using the
  157. Stable Application Binary Interface.
  158. ``Python3_Development.Embed_FOUND``
  159. .. versionadded:: 3.18
  160. System has the Python 3 development artifacts for Python embedding.
  161. ``Python3_INCLUDE_DIRS``
  162. The Python 3 include directories.
  163. ``Python3_DEBUG_POSTFIX``
  164. .. versionadded.. 3.30
  165. Postfix of debug python module. This variable can be used to define the
  166. :prop_tgt:`DEBUG_POSTFIX` target property.
  167. ``Python3_LINK_OPTIONS``
  168. .. versionadded:: 3.19
  169. The Python 3 link options. Some configurations require specific link options
  170. for a correct build and execution.
  171. ``Python3_LIBRARIES``
  172. The Python 3 libraries.
  173. ``Python3_LIBRARY_DIRS``
  174. The Python 3 library directories.
  175. ``Python3_RUNTIME_LIBRARY_DIRS``
  176. The Python 3 runtime library directories.
  177. ``Python3_SABI_LIBRARIES``
  178. .. versionadded:: 3.26
  179. The Python 3 libraries for the Stable Application Binary Interface.
  180. ``Python3_SABI_LIBRARY_DIRS``
  181. .. versionadded:: 3.26
  182. The Python 3 ``SABI`` library directories.
  183. ``Python3_RUNTIME_SABI_LIBRARY_DIRS``
  184. .. versionadded:: 3.26
  185. The Python 3 runtime ``SABI`` library directories.
  186. ``Python3_VERSION``
  187. Python 3 version.
  188. ``Python3_VERSION_MAJOR``
  189. Python 3 major version.
  190. ``Python3_VERSION_MINOR``
  191. Python 3 minor version.
  192. ``Python3_VERSION_PATCH``
  193. Python 3 patch version.
  194. ``Python3_PyPy_VERSION``
  195. .. versionadded:: 3.18
  196. Python 3 PyPy version.
  197. ``Python3_NumPy_FOUND``
  198. .. versionadded:: 3.14
  199. System has the NumPy.
  200. ``Python3_NumPy_INCLUDE_DIRS``
  201. .. versionadded:: 3.14
  202. The NumPy include directories.
  203. ``Python3_NumPy_VERSION``
  204. .. versionadded:: 3.14
  205. The NumPy version.
  206. Hints
  207. ^^^^^
  208. ``Python3_ROOT_DIR``
  209. Define the root directory of a Python 3 installation.
  210. ``Python3_USE_STATIC_LIBS``
  211. * If not defined, search for shared libraries and static libraries in that
  212. order.
  213. * If set to TRUE, search **only** for static libraries.
  214. * If set to FALSE, search **only** for shared libraries.
  215. .. note::
  216. This hint will be ignored on ``Windows`` because static libraries are not
  217. available on this platform.
  218. ``Python3_FIND_ABI``
  219. .. versionadded:: 3.16
  220. This variable defines which ABIs, as defined in :pep:`3149`, should be
  221. searched.
  222. The ``Python3_FIND_ABI`` variable is a 4-tuple specifying, in that order,
  223. ``pydebug`` (``d``), ``pymalloc`` (``m``), ``unicode`` (``u``) and
  224. ``gil_disabled`` (``t``) flags.
  225. .. versionadded:: 3.30
  226. A fourth element, specifying the ``gil_disabled`` flag (i.e. free
  227. threaded python), is added and is optional. If not specified, the value is
  228. ``OFF``.
  229. Each element can be set to one of the following:
  230. * ``ON``: Corresponding flag is selected.
  231. * ``OFF``: Corresponding flag is not selected.
  232. * ``ANY``: The two possibilities (``ON`` and ``OFF``) will be searched.
  233. .. note::
  234. If ``Python3_FIND_ABI`` is not defined, any ABI will be searched.
  235. From this 4-tuple, various ABIs will be searched starting from the most
  236. specialized to the most general. Moreover, when ``ANY`` is specified for
  237. ``pydebug`` and ``gil_disabled``, ``debug`` and ``free threaded`` versions
  238. will be searched **after** ``non-debug`` and ``non-gil-disabled`` ones.
  239. For example, if we have::
  240. set (Python3_FIND_ABI "ON" "ANY" "ANY" "ON")
  241. The following flags combinations will be appended, in that order, to the
  242. artifact names: ``tdmu``, ``tdm``, ``tdu``, and ``td``.
  243. And to search any possible ABIs::
  244. set (Python3_FIND_ABI "ANY" "ANY" "ANY" "ANY")
  245. The following combinations, in that order, will be used: ``mu``, ``m``,
  246. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du``, ``d``, ``tmu``, ``tm``, ``tu``,
  247. ``t``, ``tdmu``, ``tdm``, ``tdu``, and ``td``.
  248. .. note::
  249. This hint is useful only on ``POSIX`` systems except for the
  250. ``gil_disabled`` flag. So, on ``Windows`` systems,
  251. when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
  252. `python.org <https://www.python.org/>`_ will be found only if the value for
  253. each flag is ``OFF`` or ``ANY`` except for the fourth one
  254. (``gil_disabled``).
  255. ``Python3_FIND_STRATEGY``
  256. .. versionadded:: 3.15
  257. This variable defines how lookup will be done.
  258. The ``Python3_FIND_STRATEGY`` variable can be set to one of the following:
  259. * ``VERSION``: Try to find the most recent version in all specified
  260. locations.
  261. This is the default if policy :policy:`CMP0094` is undefined or set to
  262. ``OLD``.
  263. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  264. constraints is founded.
  265. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  266. See also ``Python3_FIND_UNVERSIONED_NAMES``.
  267. ``Python3_FIND_REGISTRY``
  268. .. versionadded:: 3.13
  269. On Windows the ``Python3_FIND_REGISTRY`` variable determine the order
  270. of preference between registry and environment variables.
  271. The ``Python3_FIND_REGISTRY`` variable can be set to one of the following:
  272. * ``FIRST``: Try to use registry before environment variables.
  273. This is the default.
  274. * ``LAST``: Try to use registry after environment variables.
  275. * ``NEVER``: Never try to use registry.
  276. ``Python3_FIND_FRAMEWORK``
  277. .. versionadded:: 3.15
  278. On macOS the ``Python3_FIND_FRAMEWORK`` variable determine the order of
  279. preference between Apple-style and unix-style package components.
  280. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  281. variable.
  282. .. note::
  283. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  284. If ``Python3_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  285. variable will be used, if any.
  286. ``Python3_FIND_VIRTUALENV``
  287. .. versionadded:: 3.15
  288. This variable defines the handling of virtual environments managed by
  289. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  290. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  291. takes precedence over ``Python3_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  292. variables. The ``Python3_FIND_VIRTUALENV`` variable can be set to one of the
  293. following:
  294. * ``FIRST``: The virtual environment is used before any other standard
  295. paths to look-up for the interpreter. This is the default.
  296. * ``ONLY``: Only the virtual environment is used to look-up for the
  297. interpreter.
  298. * ``STANDARD``: The virtual environment is not used to look-up for the
  299. interpreter but environment variable ``PATH`` is always considered.
  300. In this case, variable ``Python3_FIND_REGISTRY`` (Windows) or
  301. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  302. ``NEVER`` to select preferably the interpreter from the virtual
  303. environment.
  304. .. versionadded:: 3.17
  305. Added support for ``conda`` environments.
  306. .. note::
  307. If the component ``Development`` is requested, it is **strongly**
  308. recommended to also include the component ``Interpreter`` to get expected
  309. result.
  310. ``Python3_FIND_IMPLEMENTATIONS``
  311. .. versionadded:: 3.18
  312. This variable defines, in an ordered list, the different implementations
  313. which will be searched. The ``Python3_FIND_IMPLEMENTATIONS`` variable can
  314. hold the following values:
  315. * ``CPython``: this is the standard implementation. Various products, like
  316. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  317. * ``IronPython``: This implementation use the ``CSharp`` language for
  318. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  319. See `IronPython <https://ironpython.net>`_.
  320. * ``PyPy``: This implementation use ``RPython`` language and
  321. ``RPython translation toolchain`` to produce the python interpreter.
  322. See `PyPy <https://www.pypy.org>`_.
  323. The default value is:
  324. * Windows platform: ``CPython``, ``IronPython``
  325. * Other platforms: ``CPython``
  326. .. note::
  327. This hint has the lowest priority of all hints, so even if, for example,
  328. you specify ``IronPython`` first and ``CPython`` in second, a python
  329. product based on ``CPython`` can be selected because, for example with
  330. ``Python3_FIND_STRATEGY=LOCATION``, each location will be search first for
  331. ``IronPython`` and second for ``CPython``.
  332. .. note::
  333. When ``IronPython`` is specified, on platforms other than ``Windows``, the
  334. ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
  335. through the ``PATH`` variable.
  336. ``Python3_FIND_UNVERSIONED_NAMES``
  337. .. versionadded:: 3.20
  338. This variable defines how the generic names will be searched. Currently, it
  339. only applies to the generic names of the interpreter, namely, ``python3`` and
  340. ``python``.
  341. The ``Python3_FIND_UNVERSIONED_NAMES`` variable can be set to one of the
  342. following values:
  343. * ``FIRST``: The generic names are searched before the more specialized ones
  344. (such as ``python3.5`` for example).
  345. * ``LAST``: The generic names are searched after the more specialized ones.
  346. This is the default.
  347. * ``NEVER``: The generic name are not searched at all.
  348. See also ``Python3_FIND_STRATEGY``.
  349. Artifacts Specification
  350. ^^^^^^^^^^^^^^^^^^^^^^^
  351. .. versionadded:: 3.16
  352. To solve special cases, it is possible to specify directly the artifacts by
  353. setting the following variables:
  354. ``Python3_EXECUTABLE``
  355. The path to the interpreter.
  356. ``Python3_COMPILER``
  357. The path to the compiler.
  358. ``Python3_DOTNET_LAUNCHER``
  359. .. versionadded:: 3.18
  360. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  361. ``Python3_LIBRARY``
  362. The path to the library. It will be used to compute the
  363. variables ``Python3_LIBRARIES``, ``Python3_LIBRARY_DIRS`` and
  364. ``Python3_RUNTIME_LIBRARY_DIRS``.
  365. ``Python3_SABI_LIBRARY``
  366. .. versionadded:: 3.26
  367. The path to the library for Stable Application Binary Interface. It will be
  368. used to compute the variables ``Python3_SABI_LIBRARIES``,
  369. ``Python3_SABI_LIBRARY_DIRS`` and ``Python3_RUNTIME_SABI_LIBRARY_DIRS``.
  370. ``Python3_INCLUDE_DIR``
  371. The path to the directory of the ``Python`` headers. It will be used to
  372. compute the variable ``Python3_INCLUDE_DIRS``.
  373. ``Python3_NumPy_INCLUDE_DIR``
  374. The path to the directory of the ``NumPy`` headers. It will be used to
  375. compute the variable ``Python3_NumPy_INCLUDE_DIRS``.
  376. .. note::
  377. All paths must be absolute. Any artifact specified with a relative path
  378. will be ignored.
  379. .. note::
  380. When an artifact is specified, all ``HINTS`` will be ignored and no search
  381. will be performed for this artifact.
  382. If more than one artifact is specified, it is the user's responsibility to
  383. ensure the consistency of the various artifacts.
  384. By default, this module supports multiple calls in different directories of a
  385. project with different version/component requirements while providing correct
  386. and consistent results for each call. To support this behavior, CMake cache
  387. is not used in the traditional way which can be problematic for interactive
  388. specification. So, to enable also interactive specification, module behavior
  389. can be controlled with the following variable:
  390. ``Python3_ARTIFACTS_INTERACTIVE``
  391. .. versionadded:: 3.18
  392. Selects the behavior of the module. This is a boolean variable:
  393. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  394. specification variables so that users can edit them interactively.
  395. This disables support for multiple version/component requirements.
  396. * If set to ``FALSE`` or undefined: Enable multiple version/component
  397. requirements.
  398. Commands
  399. ^^^^^^^^
  400. This module defines the command ``Python3_add_library`` (when
  401. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  402. :command:`add_library` and adds a dependency to target ``Python3::Python`` or,
  403. when library type is ``MODULE``, to target ``Python3::Module`` or
  404. ``Python3::SABIModule`` (when ``USE_SABI`` option is specified) and takes care
  405. of Python module naming rules::
  406. Python3_add_library (<name> [STATIC | SHARED | MODULE [USE_SABI <version>] [WITH_SOABI]]
  407. <source1> [<source2> ...])
  408. If the library type is not specified, ``MODULE`` is assumed.
  409. .. versionadded:: 3.17
  410. For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
  411. module suffix will include the ``Python3_SOABI`` value, if any.
  412. .. versionadded:: 3.26
  413. For ``MODULE`` type, if the option ``USE_SABI`` is specified, the
  414. preprocessor definition ``Py_LIMITED_API`` will be specified, as ``PRIVATE``,
  415. for the target ``<name>`` with the value computed from ``<version>`` argument.
  416. The expected format for ``<version>`` is ``major[.minor]``, where each
  417. component is a numeric value. If ``minor`` component is specified, the
  418. version should be, at least, ``3.2`` which is the version where the
  419. `Stable Application Binary Interface <https://docs.python.org/3/c-api/stable.html>`_
  420. was introduced. Specifying only major version ``3`` is equivalent to ``3.2``.
  421. When option ``WITH_SOABI`` is also specified, the module suffix will include
  422. the ``Python3_SOSABI`` value, if any.
  423. .. versionadded:: 3.30
  424. For ``MODULE`` type, the :prop_tgt:`DEBUG_POSTFIX` target property is
  425. initialized with the value of ``Python3_DEBUG_POSTFIX`` variable if defined.
  426. #]=======================================================================]
  427. set (_PYTHON_PREFIX Python3)
  428. set (_Python3_REQUIRED_VERSION_MAJOR 3)
  429. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  430. if (COMMAND __Python3_add_library)
  431. macro (Python3_add_library)
  432. __Python3_add_library (Python3 ${ARGV})
  433. endmacro()
  434. endif()
  435. unset (_PYTHON_PREFIX)