FindPython.cmake 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. .. versionadded:: 3.12
  7. Find Python 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 interpreter.
  15. * ``Compiler``: search for Python 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 module
  22. developments.
  23. * ``Development.Embed``: search for artifacts for Python 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 (Python COMPONENTS Interpreter Development)
  35. This module looks preferably for version 3 of Python. If not found, version 2
  36. is searched.
  37. To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
  38. :module:`FindPython2` modules rather than this one.
  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. ``Python::Interpreter``
  52. Python interpreter. Target defined if component ``Interpreter`` is found.
  53. ``Python::Compiler``
  54. Python compiler. Target defined if component ``Compiler`` is found.
  55. ``Python::Module``
  56. .. versionadded:: 3.15
  57. Python library for Python module. Target defined if component
  58. ``Development.Module`` is found.
  59. ``Python::Python``
  60. Python library for Python embedding. Target defined if component
  61. ``Development.Embed`` is found.
  62. ``Python::NumPy``
  63. .. versionadded:: 3.14
  64. NumPy Python library. 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. ``Python_FOUND``
  70. System has the Python requested components.
  71. ``Python_Interpreter_FOUND``
  72. System has the Python interpreter.
  73. ``Python_EXECUTABLE``
  74. Path to the Python interpreter.
  75. ``Python_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. ``Python_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. ``Python_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. ``Python_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. ``Python_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. ``Python_SOABI``
  104. .. versionadded:: 3.17
  105. Extension suffix for modules.
  106. Information computed from ``distutils.sysconfig.get_config_var('EXT_SUFFIX')``
  107. or ``distutils.sysconfig.get_config_var('SOABI')`` or
  108. ``python3-config --extension-suffix``. If package ``distutils.sysconfig`` is
  109. not available, ``sysconfig.get_config_var('EXT_SUFFIX')`` or
  110. ``sysconfig.get_config_var('SOABI')`` are used.
  111. ``Python_Compiler_FOUND``
  112. System has the Python compiler.
  113. ``Python_COMPILER``
  114. Path to the Python compiler. Only offered by IronPython.
  115. ``Python_COMPILER_ID``
  116. A short string unique to the compiler. Possible values include:
  117. * IronPython
  118. ``Python_DOTNET_LAUNCHER``
  119. .. versionadded:: 3.18
  120. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  121. ``Python_Development_FOUND``
  122. System has the Python development artifacts.
  123. ``Python_Development.Module_FOUND``
  124. .. versionadded:: 3.18
  125. System has the Python development artifacts for Python module.
  126. ``Python_Development.Embed_FOUND``
  127. .. versionadded:: 3.18
  128. System has the Python development artifacts for Python embedding.
  129. ``Python_INCLUDE_DIRS``
  130. The Python include directories.
  131. ``Python_LINK_OPTIONS``
  132. .. versionadded:: 3.19
  133. The Python link options. Some configurations require specific link options
  134. for a correct build and execution.
  135. ``Python_LIBRARIES``
  136. The Python libraries.
  137. ``Python_LIBRARY_DIRS``
  138. The Python library directories.
  139. ``Python_RUNTIME_LIBRARY_DIRS``
  140. The Python runtime library directories.
  141. ``Python_VERSION``
  142. Python version.
  143. ``Python_VERSION_MAJOR``
  144. Python major version.
  145. ``Python_VERSION_MINOR``
  146. Python minor version.
  147. ``Python_VERSION_PATCH``
  148. Python patch version.
  149. ``Python_PyPy_VERSION``
  150. .. versionadded:: 3.18
  151. Python PyPy version.
  152. ``Python_NumPy_FOUND``
  153. .. versionadded:: 3.14
  154. System has the NumPy.
  155. ``Python_NumPy_INCLUDE_DIRS``
  156. .. versionadded:: 3.14
  157. The NumPy include directories.
  158. ``Python_NumPy_VERSION``
  159. .. versionadded:: 3.14
  160. The NumPy version.
  161. Hints
  162. ^^^^^
  163. ``Python_ROOT_DIR``
  164. Define the root directory of a Python installation.
  165. ``Python_USE_STATIC_LIBS``
  166. * If not defined, search for shared libraries and static libraries in that
  167. order.
  168. * If set to TRUE, search **only** for static libraries.
  169. * If set to FALSE, search **only** for shared libraries.
  170. .. note::
  171. This hint will be ignored on ``Windows`` because static libraries are not
  172. available on this platform.
  173. ``Python_FIND_ABI``
  174. .. versionadded:: 3.16
  175. This variable defines which ABIs, as defined in :pep:`3149`, should be
  176. searched.
  177. .. note::
  178. This hint will be honored only when searched for ``Python`` version 3.
  179. .. note::
  180. If ``Python_FIND_ABI`` is not defined, any ABI will be searched.
  181. The ``Python_FIND_ABI`` variable is a 3-tuple specifying, in that order,
  182. ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
  183. Each element can be set to one of the following:
  184. * ``ON``: Corresponding flag is selected.
  185. * ``OFF``: Corresponding flag is not selected.
  186. * ``ANY``: The two possibilities (``ON`` and ``OFF``) will be searched.
  187. From this 3-tuple, various ABIs will be searched starting from the most
  188. specialized to the most general. Moreover, ``debug`` versions will be
  189. searched **after** ``non-debug`` ones.
  190. For example, if we have::
  191. set (Python_FIND_ABI "ON" "ANY" "ANY")
  192. The following flags combinations will be appended, in that order, to the
  193. artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
  194. And to search any possible ABIs::
  195. set (Python_FIND_ABI "ANY" "ANY" "ANY")
  196. The following combinations, in that order, will be used: ``mu``, ``m``,
  197. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
  198. .. note::
  199. This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
  200. when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
  201. `python.org <https://www.python.org/>`_ will be found only if value for
  202. each flag is ``OFF`` or ``ANY``.
  203. ``Python_FIND_STRATEGY``
  204. .. versionadded:: 3.15
  205. This variable defines how lookup will be done.
  206. The ``Python_FIND_STRATEGY`` variable can be set to one of the following:
  207. * ``VERSION``: Try to find the most recent version in all specified
  208. locations.
  209. This is the default if policy :policy:`CMP0094` is undefined or set to
  210. ``OLD``.
  211. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  212. constraints is founded.
  213. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  214. ``Python_FIND_REGISTRY``
  215. .. versionadded:: 3.13
  216. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  217. of preference between registry and environment variables.
  218. the ``Python_FIND_REGISTRY`` variable can be set to one of the following:
  219. * ``FIRST``: Try to use registry before environment variables.
  220. This is the default.
  221. * ``LAST``: Try to use registry after environment variables.
  222. * ``NEVER``: Never try to use registry.
  223. ``Python_FIND_FRAMEWORK``
  224. .. versionadded:: 3.15
  225. On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
  226. preference between Apple-style and unix-style package components.
  227. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  228. variable.
  229. .. note::
  230. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  231. If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  232. variable will be used, if any.
  233. ``Python_FIND_VIRTUALENV``
  234. .. versionadded:: 3.15
  235. This variable defines the handling of virtual environments managed by
  236. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  237. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  238. takes precedence over ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  239. variables. The ``Python_FIND_VIRTUALENV`` variable can be set to one of the
  240. following:
  241. * ``FIRST``: The virtual environment is used before any other standard
  242. paths to look-up for the interpreter. This is the default.
  243. * ``ONLY``: Only the virtual environment is used to look-up for the
  244. interpreter.
  245. * ``STANDARD``: The virtual environment is not used to look-up for the
  246. interpreter but environment variable ``PATH`` is always considered.
  247. In this case, variable ``Python_FIND_REGISTRY`` (Windows) or
  248. ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  249. ``NEVER`` to select preferably the interpreter from the virtual
  250. environment.
  251. .. versionadded:: 3.17
  252. Added support for ``conda`` environments.
  253. .. note::
  254. If the component ``Development`` is requested, it is **strongly**
  255. recommended to also include the component ``Interpreter`` to get expected
  256. result.
  257. ``Python_FIND_IMPLEMENTATIONS``
  258. .. versionadded:: 3.18
  259. This variable defines, in an ordered list, the different implementations
  260. which will be searched. The ``Python_FIND_IMPLEMENTATIONS`` variable can
  261. hold the following values:
  262. * ``CPython``: this is the standard implementation. Various products, like
  263. ``Anaconda`` or ``ActivePython``, rely on this implementation.
  264. * ``IronPython``: This implementation use the ``CSharp`` language for
  265. ``.NET Framework`` on top of the `Dynamic Language Runtime` (``DLR``).
  266. See `IronPython <https://ironpython.net>`_.
  267. * ``PyPy``: This implementation use ``RPython`` language and
  268. ``RPython translation toolchain`` to produce the python interpreter.
  269. See `PyPy <https://www.pypy.org>`_.
  270. The default value is:
  271. * Windows platform: ``CPython``, ``IronPython``
  272. * Other platforms: ``CPython``
  273. .. note::
  274. This hint has the lowest priority of all hints, so even if, for example,
  275. you specify ``IronPython`` first and ``CPython`` in second, a python
  276. product based on ``CPython`` can be selected because, for example with
  277. ``Python_FIND_STRATEGY=LOCATION``, each location will be search first for
  278. ``IronPython`` and second for ``CPython``.
  279. .. note::
  280. When ``IronPython`` is specified, on platforms other than ``Windows``, the
  281. ``.Net`` interpreter (i.e. ``mono`` command) is expected to be available
  282. through the ``PATH`` variable.
  283. ``Python_FIND_UNVERSIONED_NAMES``
  284. .. versionadded:: 3.20
  285. This variable defines how the generic names will be searched. Currently, it
  286. only applies to the generic names of the interpreter, namely, ``python3`` or
  287. ``python2`` and ``python``.
  288. The ``Python_FIND_UNVERSIONED_NAMES`` variable can be set to one of the
  289. following values:
  290. * ``FIRST``: The generic names are searched before the more specialized ones
  291. (such as ``python2.5`` for example).
  292. * ``LAST``: The generic names are searched after the more specialized ones.
  293. This is the default.
  294. * ``NEVER``: The generic name are not searched at all.
  295. Artifacts Specification
  296. ^^^^^^^^^^^^^^^^^^^^^^^
  297. .. versionadded:: 3.16
  298. To solve special cases, it is possible to specify directly the artifacts by
  299. setting the following variables:
  300. ``Python_EXECUTABLE``
  301. The path to the interpreter.
  302. ``Python_COMPILER``
  303. The path to the compiler.
  304. ``Python_DOTNET_LAUNCHER``
  305. .. versionadded:: 3.18
  306. The ``.Net`` interpreter. Only used by ``IronPython`` implementation.
  307. ``Python_LIBRARY``
  308. The path to the library. It will be used to compute the
  309. variables ``Python_LIBRARIES``, ``Python_LIBRARY_DIRS`` and
  310. ``Python_RUNTIME_LIBRARY_DIRS``.
  311. ``Python_INCLUDE_DIR``
  312. The path to the directory of the ``Python`` headers. It will be used to
  313. compute the variable ``Python_INCLUDE_DIRS``.
  314. ``Python_NumPy_INCLUDE_DIR``
  315. The path to the directory of the ``NumPy`` headers. It will be used to
  316. compute the variable ``Python_NumPy_INCLUDE_DIRS``.
  317. .. note::
  318. All paths must be absolute. Any artifact specified with a relative path
  319. will be ignored.
  320. .. note::
  321. When an artifact is specified, all ``HINTS`` will be ignored and no search
  322. will be performed for this artifact.
  323. If more than one artifact is specified, it is the user's responsibility to
  324. ensure the consistency of the various artifacts.
  325. By default, this module supports multiple calls in different directories of a
  326. project with different version/component requirements while providing correct
  327. and consistent results for each call. To support this behavior, CMake cache
  328. is not used in the traditional way which can be problematic for interactive
  329. specification. So, to enable also interactive specification, module behavior
  330. can be controlled with the following variable:
  331. ``Python_ARTIFACTS_INTERACTIVE``
  332. .. versionadded:: 3.18
  333. Selects the behavior of the module. This is a boolean variable:
  334. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  335. specification variables so that users can edit them interactively.
  336. This disables support for multiple version/component requirements.
  337. * If set to ``FALSE`` or undefined: Enable multiple version/component
  338. requirements.
  339. Commands
  340. ^^^^^^^^
  341. This module defines the command ``Python_add_library`` (when
  342. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  343. :command:`add_library` and adds a dependency to target ``Python::Python`` or,
  344. when library type is ``MODULE``, to target ``Python::Module`` and takes care of
  345. Python module naming rules::
  346. Python_add_library (<name> [STATIC | SHARED | MODULE [WITH_SOABI]]
  347. <source1> [<source2> ...])
  348. If the library type is not specified, ``MODULE`` is assumed.
  349. .. versionadded:: 3.17
  350. For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
  351. module suffix will include the ``Python_SOABI`` value, if any.
  352. #]=======================================================================]
  353. cmake_policy(PUSH)
  354. # numbers and boolean constants
  355. cmake_policy (SET CMP0012 NEW)
  356. set (_PYTHON_PREFIX Python)
  357. unset (_Python_REQUIRED_VERSION_MAJOR)
  358. unset (_Python_REQUIRED_VERSIONS)
  359. if (Python_FIND_VERSION_RANGE)
  360. # compute list of major versions
  361. foreach (_Python_MAJOR IN ITEMS 3 2)
  362. if (_Python_MAJOR VERSION_GREATER_EQUAL Python_FIND_VERSION_MIN_MAJOR
  363. AND ((Python_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _Python_MAJOR VERSION_LESS_EQUAL Python_FIND_VERSION_MAX)
  364. OR (Python_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _Python_MAJOR VERSION_LESS Python_FIND_VERSION_MAX)))
  365. list (APPEND _Python_REQUIRED_VERSIONS ${_Python_MAJOR})
  366. endif()
  367. endforeach()
  368. list (LENGTH _Python_REQUIRED_VERSIONS _Python_VERSION_COUNT)
  369. if (_Python_VERSION_COUNT EQUAL 0)
  370. unset (_Python_REQUIRED_VERSIONS)
  371. elseif (_Python_VERSION_COUNT EQUAL 1)
  372. set (_Python_REQUIRED_VERSION_MAJOR ${_Python_REQUIRED_VERSIONS})
  373. endif()
  374. elseif (DEFINED Python_FIND_VERSION)
  375. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  376. else()
  377. set (_Python_REQUIRED_VERSIONS 3 2)
  378. endif()
  379. if (_Python_REQUIRED_VERSION_MAJOR)
  380. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  381. elseif (_Python_REQUIRED_VERSIONS)
  382. # iterate over versions in quiet and NOT required modes to avoid multiple
  383. # "Found" messages and prematurally failure.
  384. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  385. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  386. set (Python_FIND_QUIETLY TRUE)
  387. set (Python_FIND_REQUIRED FALSE)
  388. set (_Python_REQUIRED_VERSION_LAST 2)
  389. unset (_Python_INPUT_VARS)
  390. foreach (_Python_ITEM IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
  391. Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
  392. if (NOT DEFINED ${_Python_ITEM})
  393. list (APPEND _Python_INPUT_VARS ${_Python_ITEM})
  394. endif()
  395. endforeach()
  396. foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
  397. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  398. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  399. if (Python_FOUND OR
  400. _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
  401. break()
  402. endif()
  403. # clean-up INPUT variables not set by the user
  404. foreach (_Python_ITEM IN LISTS _Python_INPUT_VARS)
  405. unset (${_Python_ITEM})
  406. endforeach()
  407. # clean-up some CACHE variables to ensure look-up restart from scratch
  408. foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
  409. unset (${_Python_ITEM} CACHE)
  410. endforeach()
  411. endforeach()
  412. unset (Python_FIND_VERSION)
  413. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  414. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  415. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  416. # call again validation command to get "Found" or error message
  417. find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
  418. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  419. VERSION_VAR Python_VERSION)
  420. endif()
  421. else()
  422. # supported versions not in the specified range. Call final check
  423. if (NOT Python_FIND_COMPONENTS)
  424. set (Python_FIND_COMPONENTS Interpreter)
  425. set (Python_FIND_REQUIRED_Interpreter TRUE)
  426. endif()
  427. include (${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  428. find_package_handle_standard_args (Python HANDLE_COMPONENTS HANDLE_VERSION_RANGE
  429. VERSION_VAR Python_VERSION
  430. REASON_FAILURE_MESSAGE "Version range specified \"${Python_FIND_VERSION_RANGE}\" does not include supported versions")
  431. endif()
  432. if (COMMAND __Python_add_library)
  433. macro (Python_add_library)
  434. __Python_add_library (Python ${ARGV})
  435. endmacro()
  436. endif()
  437. unset (_PYTHON_PREFIX)
  438. cmake_policy(POP)