FindPython.cmake 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file Copyright.txt or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPython
  5. ----------
  6. Find Python interpreter, compiler and development environment (include
  7. directories and libraries).
  8. The following components are supported:
  9. * ``Interpreter``: search for Python interpreter.
  10. * ``Compiler``: search for Python compiler. Only offered by IronPython.
  11. * ``Development``: search for development artifacts (include directories and
  12. libraries). This component includes two sub-components which can be specified
  13. independently:
  14. * ``Development.Module``: search for artifacts for Python module
  15. developments.
  16. * ``Development.Embed``: search for artifacts for Python embedding
  17. developments.
  18. * ``NumPy``: search for NumPy include directories.
  19. If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
  20. If component ``Development`` is specified, it implies sub-components
  21. ``Development.Module`` and ``Development.Embed``.
  22. To ensure consistent versions between components ``Interpreter``, ``Compiler``,
  23. ``Development`` (or one of its sub-components) and ``NumPy``, specify all
  24. components at the same time::
  25. find_package (Python COMPONENTS Interpreter Development)
  26. This module looks preferably for version 3 of Python. If not found, version 2
  27. is searched.
  28. To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
  29. :module:`FindPython2` modules rather than this one.
  30. .. note::
  31. If components ``Interpreter`` and ``Development`` (or one of its
  32. sub-components) are both specified, this module search only for interpreter
  33. with same platform architecture as the one defined by ``CMake``
  34. configuration. This contraint does not apply if only ``Interpreter``
  35. component is specified.
  36. Imported Targets
  37. ^^^^^^^^^^^^^^^^
  38. This module defines the following :ref:`Imported Targets <Imported Targets>`
  39. (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  40. ``Python::Interpreter``
  41. Python interpreter. Target defined if component ``Interpreter`` is found.
  42. ``Python::Compiler``
  43. Python compiler. Target defined if component ``Compiler`` is found.
  44. ``Python::Module``
  45. Python library for Python module. Target defined if component
  46. ``Development.Module`` is found.
  47. ``Python::Python``
  48. Python library for Python embedding. Target defined if component
  49. ``Development.Embed`` is found.
  50. ``Python::NumPy``
  51. NumPy Python library. Target defined if component ``NumPy`` is found.
  52. Result Variables
  53. ^^^^^^^^^^^^^^^^
  54. This module will set the following variables in your project
  55. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  56. ``Python_FOUND``
  57. System has the Python requested components.
  58. ``Python_Interpreter_FOUND``
  59. System has the Python interpreter.
  60. ``Python_EXECUTABLE``
  61. Path to the Python interpreter.
  62. ``Python_INTERPRETER_ID``
  63. A short string unique to the interpreter. Possible values include:
  64. * Python
  65. * ActivePython
  66. * Anaconda
  67. * Canopy
  68. * IronPython
  69. * PyPy
  70. ``Python_STDLIB``
  71. Standard platform independent installation directory.
  72. Information returned by
  73. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``
  74. or else ``sysconfig.get_path('stdlib')``.
  75. ``Python_STDARCH``
  76. Standard platform dependent installation directory.
  77. Information returned by
  78. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``
  79. or else ``sysconfig.get_path('platstdlib')``.
  80. ``Python_SITELIB``
  81. Third-party platform independent installation directory.
  82. Information returned by
  83. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``
  84. or else ``sysconfig.get_path('purelib')``.
  85. ``Python_SITEARCH``
  86. Third-party platform dependent installation directory.
  87. Information returned by
  88. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``
  89. or else ``sysconfig.get_path('platlib')``.
  90. ``Python_SOABI``
  91. Extension suffix for modules.
  92. Information returned by
  93. ``distutils.sysconfig.get_config_var('SOABI')`` or computed from
  94. ``distutils.sysconfig.get_config_var('EXT_SUFFIX')`` or
  95. ``python-config --extension-suffix``. If package ``distutils.sysconfig`` is
  96. not available, ``sysconfig.get_config_var('SOABI')`` or
  97. ``sysconfig.get_config_var('EXT_SUFFIX')`` are used.
  98. ``Python_Compiler_FOUND``
  99. System has the Python compiler.
  100. ``Python_COMPILER``
  101. Path to the Python compiler. Only offered by IronPython.
  102. ``Python_COMPILER_ID``
  103. A short string unique to the compiler. Possible values include:
  104. * IronPython
  105. ``Python_DOTNET_LAUNCHER``
  106. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  107. ``Python_Development_FOUND``
  108. System has the Python development artifacts.
  109. ``Python_Development.Module_FOUND``
  110. System has the Python development artifacts for Python module.
  111. ``Python_Development.Embed_FOUND``
  112. System has the Python development artifacts for Python embedding.
  113. ``Python_INCLUDE_DIRS``
  114. The Python include directories.
  115. ``Python_LIBRARIES``
  116. The Python libraries.
  117. ``Python_LIBRARY_DIRS``
  118. The Python library directories.
  119. ``Python_RUNTIME_LIBRARY_DIRS``
  120. The Python runtime library directories.
  121. ``Python_VERSION``
  122. Python version.
  123. ``Python_VERSION_MAJOR``
  124. Python major version.
  125. ``Python_VERSION_MINOR``
  126. Python minor version.
  127. ``Python_VERSION_PATCH``
  128. Python patch version.
  129. ``Python_PyPy_VERSION``
  130. Python PyPy version.
  131. ``Python_NumPy_FOUND``
  132. System has the NumPy.
  133. ``Python_NumPy_INCLUDE_DIRS``
  134. The NumPy include directries.
  135. ``Python_NumPy_VERSION``
  136. The NumPy version.
  137. Hints
  138. ^^^^^
  139. ``Python_ROOT_DIR``
  140. Define the root directory of a Python installation.
  141. ``Python_USE_STATIC_LIBS``
  142. * If not defined, search for shared libraries and static libraries in that
  143. order.
  144. * If set to TRUE, search **only** for static libraries.
  145. * If set to FALSE, search **only** for shared libraries.
  146. ``Python_FIND_ABI``
  147. This variable defines which ABIs, as defined in
  148. `PEP 3149 <https://www.python.org/dev/peps/pep-3149/>`_, should be searched.
  149. .. note::
  150. This hint will be honored only when searched for ``Python`` version 3.
  151. .. note::
  152. If ``Python_FIND_ABI`` is not defined, any ABI will be searched.
  153. The ``Python_FIND_ABI`` variable is a 3-tuple specifying, in that order,
  154. ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
  155. Each element can be set to one of the following:
  156. * ``ON``: Corresponding flag is selected.
  157. * ``OFF``: Corresponding flag is not selected.
  158. * ``ANY``: The two posibilties (``ON`` and ``OFF``) will be searched.
  159. From this 3-tuple, various ABIs will be searched starting from the most
  160. specialized to the most general. Moreover, ``debug`` versions will be
  161. searched **after** ``non-debug`` ones.
  162. For example, if we have::
  163. set (Python_FIND_ABI "ON" "ANY" "ANY")
  164. The following flags combinations will be appended, in that order, to the
  165. artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
  166. And to search any possible ABIs::
  167. set (Python_FIND_ABI "ANY" "ANY" "ANY")
  168. The following combinations, in that order, will be used: ``mu``, ``m``,
  169. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
  170. .. note::
  171. This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
  172. when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
  173. `python.org <https://www.python.org/>`_ will be found only if value for
  174. each flag is ``OFF`` or ``ANY``.
  175. ``Python_FIND_STRATEGY``
  176. This variable defines how lookup will be done.
  177. The ``Python_FIND_STRATEGY`` variable can be set to one of the following:
  178. * ``VERSION``: Try to find the most recent version in all specified
  179. locations.
  180. This is the default if policy :policy:`CMP0094` is undefined or set to
  181. ``OLD``.
  182. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  183. constraints is founded.
  184. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  185. ``Python_FIND_REGISTRY``
  186. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  187. of preference between registry and environment variables.
  188. the ``Python_FIND_REGISTRY`` variable can be set to one of the following:
  189. * ``FIRST``: Try to use registry before environment variables.
  190. This is the default.
  191. * ``LAST``: Try to use registry after environment variables.
  192. * ``NEVER``: Never try to use registry.
  193. ``Python_FIND_FRAMEWORK``
  194. On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
  195. preference between Apple-style and unix-style package components.
  196. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  197. variable.
  198. .. note::
  199. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  200. If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  201. variable will be used, if any.
  202. ``Python_FIND_VIRTUALENV``
  203. This variable defines the handling of virtual environments managed by
  204. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  205. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  206. takes precedence over ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  207. variables. The ``Python_FIND_VIRTUALENV`` variable can be set to one of the
  208. following:
  209. * ``FIRST``: The virtual environment is used before any other standard
  210. paths to look-up for the interpreter. This is the default.
  211. * ``ONLY``: Only the virtual environment is used to look-up for the
  212. interpreter.
  213. * ``STANDARD``: The virtual environment is not used to look-up for the
  214. interpreter but environment variable ``PATH`` is always considered.
  215. In this case, variable ``Python_FIND_REGISTRY`` (Windows) or
  216. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  217. ``NEVER`` to select preferably the interpreter from the virtual
  218. environment.
  219. .. note::
  220. If the component ``Development`` is requested, it is **strongly**
  221. recommended to also include the component ``Interpreter`` to get expected
  222. result.
  223. ``Python_FIND_IMPLEMENTATIONS``
  224. This variable defines, in an ordered list, the different implementations
  225. which will be searched. The ``Python_FIND_IMPLEMENTATIONS`` variable can
  226. hold the following values:
  227. * ``CPython``: this is the standard implementation. Various products, like
  228. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  229. * ``IronPython``: This implementation use the ``CSharp`` language for
  230. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  231. See `IronPython <http://ironpython.net>`_.
  232. * ``PyPy``: This implementation use ``RPython`` language and
  233. ``RPython translation toolchain`` to produce the python interpreter.
  234. See `PyPy <https://www.pypy.org>`_.
  235. The default value is:
  236. * Windows platform: ``CPython``, ``IronPython``
  237. * Other platforms: ``CPython``
  238. .. note::
  239. This hint has the lowest priority of all hints, so even if, for example,
  240. you specify ``IronPython`` first and ``CPython`` in second, a python
  241. product based on ``CPython`` can be selected because, for example with
  242. ``Python_FIND_STRATEGY=LOCATION``, each location will be search first for
  243. ``IronPython`` and second for ``CPython``.
  244. .. note::
  245. When ``IronPython`` is specified, on platforms other than ``Windows``, the
  246. ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
  247. through the ``PATH`` variable.
  248. Artifacts Specification
  249. ^^^^^^^^^^^^^^^^^^^^^^^
  250. To solve special cases, it is possible to specify directly the artifacts by
  251. setting the following variables:
  252. ``Python_EXECUTABLE``
  253. The path to the interpreter.
  254. ``Python_COMPILER``
  255. The path to the compiler.
  256. ``Python_DOTNET_LAUNCHER``
  257. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  258. ``Python_LIBRARY``
  259. The path to the library. It will be used to compute the
  260. variables ``Python_LIBRARIES``, ``Python_LIBRAY_DIRS`` and
  261. ``Python_RUNTIME_LIBRARY_DIRS``.
  262. ``Python_INCLUDE_DIR``
  263. The path to the directory of the ``Python`` headers. It will be used to
  264. compute the variable ``Python_INCLUDE_DIRS``.
  265. ``Python_NumPy_INCLUDE_DIR``
  266. The path to the directory of the ``NumPy`` headers. It will be used to
  267. compute the variable ``Python_NumPy_INCLUDE_DIRS``.
  268. .. note::
  269. All paths must be absolute. Any artifact specified with a relative path
  270. will be ignored.
  271. .. note::
  272. When an artifact is specified, all ``HINTS`` will be ignored and no search
  273. will be performed for this artifact.
  274. If more than one artifact is specified, it is the user's responsability to
  275. ensure the consistency of the various artifacts.
  276. By default, this module supports multiple calls in different directories of a
  277. project with different version/component requirements while providing correct
  278. and consistent results for each call. To support this behavior, ``CMake`` cache
  279. is not used in the traditional way which can be problematic for interactive
  280. specification. So, to enable also interactive specification, module behavior
  281. can be controled with the following variable:
  282. ``Python_ARTIFACTS_INTERACTIVE``
  283. Selects the behavior of the module. This is a boolean variable:
  284. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  285. specification variables so that users can edit them interactively.
  286. This disables support for multiple version/component requirements.
  287. * If set to ``FALSE`` or undefined: Enable multiple version/component
  288. requirements.
  289. Commands
  290. ^^^^^^^^
  291. This module defines the command ``Python_add_library`` (when
  292. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  293. :command:`add_library` and adds a dependency to target ``Python::Python`` or,
  294. when library type is ``MODULE``, to target ``Python::Module`` and takes care of
  295. Python module naming rules::
  296. Python_add_library (<name> [STATIC | SHARED | MODULE [WITH_SOABI]]
  297. <source1> [<source2> ...])
  298. If the library type is not specified, ``MODULE`` is assumed.
  299. For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
  300. module suffix will include the ``Python_SOABI`` value, if any.
  301. #]=======================================================================]
  302. set (_PYTHON_PREFIX Python)
  303. if (DEFINED Python_FIND_VERSION)
  304. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  305. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  306. else()
  307. # iterate over versions in quiet and NOT required modes to avoid multiple
  308. # "Found" messages and prematurally failure.
  309. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  310. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  311. set (Python_FIND_QUIETLY TRUE)
  312. set (Python_FIND_REQUIRED FALSE)
  313. set (_Python_REQUIRED_VERSIONS 3 2)
  314. set (_Python_REQUIRED_VERSION_LAST 2)
  315. unset (_Python_INPUT_VARS)
  316. foreach (_Python_ITEM IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
  317. Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
  318. if (NOT DEFINED ${_Python_ITEM})
  319. list (APPEND _Python_INPUT_VARS ${_Python_ITEM})
  320. endif()
  321. endforeach()
  322. foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
  323. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  324. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  325. if (Python_FOUND OR
  326. _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
  327. break()
  328. endif()
  329. # clean-up INPUT variables not set by the user
  330. foreach (_Python_ITEM IN LISTS _Python_INPUT_VARS)
  331. unset (${_Python_ITEM})
  332. endforeach()
  333. # clean-up some CACHE variables to ensure look-up restart from scratch
  334. foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
  335. unset (${_Python_ITEM} CACHE)
  336. endforeach()
  337. endforeach()
  338. unset (Python_FIND_VERSION)
  339. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  340. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  341. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  342. # call again validation command to get "Found" or error message
  343. find_package_handle_standard_args (Python HANDLE_COMPONENTS
  344. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  345. VERSION_VAR Python_VERSION)
  346. endif()
  347. endif()
  348. if (COMMAND __Python_add_library)
  349. macro (Python_add_library)
  350. __Python_add_library (Python ${ARGV})
  351. endmacro()
  352. endif()
  353. unset (_PYTHON_PREFIX)