FindPython3.cmake 16 KB

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