FindPython.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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).
  13. * ``NumPy``: search for NumPy include directories.
  14. If no ``COMPONENTS`` are specified, ``Interpreter`` is assumed.
  15. To ensure consistent versions between components ``Interpreter``, ``Compiler``,
  16. ``Development`` and ``NumPy``, specify all components at the same time::
  17. find_package (Python COMPONENTS Interpreter Development)
  18. This module looks preferably for version 3 of Python. If not found, version 2
  19. is searched.
  20. To manage concurrent versions 3 and 2 of Python, use :module:`FindPython3` and
  21. :module:`FindPython2` modules rather than this one.
  22. .. note::
  23. If components ``Interpreter`` and ``Development`` are both specified, this
  24. module search only for interpreter with same platform architecture as the one
  25. defined by ``CMake`` configuration. This contraint does not apply if only
  26. ``Interpreter`` component is specified.
  27. Imported Targets
  28. ^^^^^^^^^^^^^^^^
  29. This module defines the following :ref:`Imported Targets <Imported Targets>`
  30. (when :prop_gbl:`CMAKE_ROLE` is ``PROJECT``):
  31. ``Python::Interpreter``
  32. Python interpreter. Target defined if component ``Interpreter`` is found.
  33. ``Python::Compiler``
  34. Python compiler. Target defined if component ``Compiler`` is found.
  35. ``Python::Python``
  36. Python library for Python embedding. Target defined if component
  37. ``Development`` is found.
  38. ``Python::Module``
  39. Python library for Python module. Target defined if component ``Development``
  40. is found.
  41. ``Python::NumPy``
  42. NumPy Python library. Target defined if component ``NumPy`` is found.
  43. Result Variables
  44. ^^^^^^^^^^^^^^^^
  45. This module will set the following variables in your project
  46. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  47. ``Python_FOUND``
  48. System has the Python requested components.
  49. ``Python_Interpreter_FOUND``
  50. System has the Python interpreter.
  51. ``Python_EXECUTABLE``
  52. Path to the Python interpreter.
  53. ``Python_INTERPRETER_ID``
  54. A short string unique to the interpreter. Possible values include:
  55. * Python
  56. * ActivePython
  57. * Anaconda
  58. * Canopy
  59. * IronPython
  60. ``Python_STDLIB``
  61. Standard platform independent installation directory.
  62. Information returned by
  63. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
  64. ``Python_STDARCH``
  65. Standard platform dependent installation directory.
  66. Information returned by
  67. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
  68. ``Python_SITELIB``
  69. Third-party platform independent installation directory.
  70. Information returned by
  71. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
  72. ``Python_SITEARCH``
  73. Third-party platform dependent installation directory.
  74. Information returned by
  75. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
  76. ``Python_Compiler_FOUND``
  77. System has the Python compiler.
  78. ``Python_COMPILER``
  79. Path to the Python compiler. Only offered by IronPython.
  80. ``Python_COMPILER_ID``
  81. A short string unique to the compiler. Possible values include:
  82. * IronPython
  83. ``Python_Development_FOUND``
  84. System has the Python development artifacts.
  85. ``Python_INCLUDE_DIRS``
  86. The Python include directories.
  87. ``Python_LIBRARIES``
  88. The Python libraries.
  89. ``Python_LIBRARY_DIRS``
  90. The Python library directories.
  91. ``Python_RUNTIME_LIBRARY_DIRS``
  92. The Python runtime library directories.
  93. ``Python_VERSION``
  94. Python version.
  95. ``Python_VERSION_MAJOR``
  96. Python major version.
  97. ``Python_VERSION_MINOR``
  98. Python minor version.
  99. ``Python_VERSION_PATCH``
  100. Python patch version.
  101. ``Python_NumPy_FOUND``
  102. System has the NumPy.
  103. ``Python_NumPy_INCLUDE_DIRS``
  104. The NumPy include directries.
  105. ``Python_NumPy_VERSION``
  106. The NumPy version.
  107. Hints
  108. ^^^^^
  109. ``Python_ROOT_DIR``
  110. Define the root directory of a Python installation.
  111. ``Python_USE_STATIC_LIBS``
  112. * If not defined, search for shared libraries and static libraries in that
  113. order.
  114. * If set to TRUE, search **only** for static libraries.
  115. * If set to FALSE, search **only** for shared libraries.
  116. ``Python_FIND_ABI``
  117. This variable defines which ABIs, as defined in
  118. `PEP 3149 <https://www.python.org/dev/peps/pep-3149/>`_, should be searched.
  119. .. note::
  120. This hint will be honored only when searched for ``Python`` version 3.
  121. .. note::
  122. If ``Python_FIND_ABI`` is not defined, any ABI will be searched.
  123. The ``Python_FIND_ABI`` variable is a 3-tuple specifying, in that order,
  124. ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
  125. Each element can be set to one of the following:
  126. * ``ON``: Corresponding flag is selected.
  127. * ``OFF``: Corresponding flag is not selected.
  128. * ``ANY``: The two posibilties (``ON`` and ``OFF``) will be searched.
  129. From this 3-tuple, various ABIs will be searched starting from the most
  130. specialized to the most general. Moreover, ``debug`` versions will be
  131. searched **after** ``non-debug`` ones.
  132. For example, if we have::
  133. set (Python_FIND_ABI "ON" "ANY" "ANY")
  134. The following flags combinations will be appended, in that order, to the
  135. artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
  136. And to search any possible ABIs::
  137. set (Python_FIND_ABI "ANY" "ANY" "ANY")
  138. The following combinations, in that order, will be used: ``mu``, ``m``,
  139. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
  140. .. note::
  141. This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
  142. when ``Python_FIND_ABI`` is defined, ``Python`` distributions from
  143. `python.org <https://www.python.org/>`_ will be found only if value for
  144. each flag is ``OFF`` or ``ANY``.
  145. ``Python_FIND_STRATEGY``
  146. This variable defines how lookup will be done.
  147. The ``Python_FIND_STRATEGY`` variable can be set to empty or one of the
  148. following:
  149. * ``VERSION``: Try to find the most recent version in all specified
  150. locations.
  151. This is the default if policy :policy:`CMP0094` is undefined or set to
  152. ``OLD``.
  153. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  154. constraints is founded.
  155. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  156. ``Python_FIND_REGISTRY``
  157. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  158. of preference between registry and environment variables.
  159. the ``Python_FIND_REGISTRY`` variable can be set to empty or one of the
  160. following:
  161. * ``FIRST``: Try to use registry before environment variables.
  162. This is the default.
  163. * ``LAST``: Try to use registry after environment variables.
  164. * ``NEVER``: Never try to use registry.
  165. ``Python_FIND_FRAMEWORK``
  166. On macOS the ``Python_FIND_FRAMEWORK`` variable determine the order of
  167. preference between Apple-style and unix-style package components.
  168. This variable can be set to empty or take same values as
  169. :variable:`CMAKE_FIND_FRAMEWORK` variable.
  170. .. note::
  171. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  172. If ``Python_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  173. variable will be used, if any.
  174. ``Python_FIND_VIRTUALENV``
  175. This variable defines the handling of virtual environments. It is meaningfull
  176. only when a virtual environment is active (i.e. the ``activate`` script has
  177. been evaluated). In this case, it takes precedence over
  178. ``Python_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK`` variables.
  179. The ``Python_FIND_VIRTUALENV`` variable can be set to empty or one of the
  180. following:
  181. * ``FIRST``: The virtual environment is used before any other standard
  182. paths to look-up for the interpreter. This is the default.
  183. * ``ONLY``: Only the virtual environment is used to look-up for the
  184. interpreter.
  185. * ``STANDARD``: The virtual environment is not used to look-up for the
  186. interpreter. In this case, variable ``Python_FIND_REGISTRY`` (Windows)
  187. or ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  188. ``NEVER`` to select preferably the interpreter from the virtual
  189. environment.
  190. .. note::
  191. If the component ``Development`` is requested, it is **strongly**
  192. recommended to also include the component ``Interpreter`` to get expected
  193. result.
  194. Artifacts Specification
  195. ^^^^^^^^^^^^^^^^^^^^^^^
  196. To solve special cases, it is possible to specify directly the artifacts by
  197. setting the following variables:
  198. ``Python_EXECUTABLE``
  199. The path to the interpreter.
  200. ``Python_COMPILER``
  201. The path to the compiler.
  202. ``Python_LIBRARY``
  203. The path to the library. It will be used to compute the
  204. variables ``Python_LIBRARIES``, ``Python_LIBRAY_DIRS`` and
  205. ``Python_RUNTIME_LIBRARY_DIRS``.
  206. ``Python_INCLUDE_DIR``
  207. The path to the directory of the ``Python`` headers. It will be used to
  208. compute the variable ``Python_INCLUDE_DIRS``.
  209. ``Python_NumPy_INCLUDE_DIR``
  210. The path to the directory of the ``NumPy`` headers. It will be used to
  211. compute the variable ``Python_NumPy_INCLUDE_DIRS``.
  212. .. note::
  213. All paths must be absolute. Any artifact specified with a relative path
  214. will be ignored.
  215. .. note::
  216. When an artifact is specified, all ``HINTS`` will be ignored and no search
  217. will be performed for this artifact.
  218. If more than one artifact is specified, it is the user's responsability to
  219. ensure the consistency of the various artifacts.
  220. Commands
  221. ^^^^^^^^
  222. This module defines the command ``Python_add_library`` (when
  223. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  224. :command:`add_library` and adds a dependency to target ``Python::Python`` or,
  225. when library type is ``MODULE``, to target ``Python::Module`` and takes care of
  226. Python module naming rules::
  227. Python_add_library (my_module MODULE src1.cpp)
  228. If library type is not specified, ``MODULE`` is assumed.
  229. #]=======================================================================]
  230. set (_PYTHON_PREFIX Python)
  231. if (DEFINED Python_FIND_VERSION)
  232. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  233. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  234. else()
  235. # iterate over versions in quiet and NOT required modes to avoid multiple
  236. # "Found" messages and prematurally failure.
  237. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  238. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  239. set (Python_FIND_QUIETLY TRUE)
  240. set (Python_FIND_REQUIRED FALSE)
  241. set (_Python_REQUIRED_VERSIONS 3 2)
  242. set (_Python_REQUIRED_VERSION_LAST 2)
  243. unset (_Python_INPUT_VARS)
  244. foreach (_Python_ITEM IN ITEMS Python_EXECUTABLE Python_COMPILER Python_LIBRARY
  245. Python_INCLUDE_DIR Python_NumPy_INCLUDE_DIR)
  246. if (NOT DEFINED ${_Python_ITEM})
  247. list (APPEND _Python_INPUT_VARS ${_Python_ITEM})
  248. endif()
  249. endforeach()
  250. foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
  251. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  252. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  253. if (Python_FOUND OR
  254. _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
  255. break()
  256. endif()
  257. # clean-up INPUT variables not set by the user
  258. foreach (_Python_ITEM IN LISTS _Python_INPUT_VARS)
  259. unset (${_Python_ITEM})
  260. endforeach()
  261. # clean-up some CACHE variables to ensure look-up restart from scratch
  262. foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
  263. unset (${_Python_ITEM} CACHE)
  264. endforeach()
  265. endforeach()
  266. unset (Python_FIND_VERSION)
  267. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  268. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  269. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  270. # call again validation command to get "Found" or error message
  271. find_package_handle_standard_args (Python HANDLE_COMPONENTS
  272. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  273. VERSION_VAR Python_VERSION)
  274. endif()
  275. endif()
  276. if (COMMAND __Python_add_library)
  277. macro (Python_add_library)
  278. __Python_add_library (Python ${ARGV})
  279. endmacro()
  280. endif()
  281. unset (_PYTHON_PREFIX)