FindPython3.cmake 14 KB

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