FindPython3.cmake 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. Find Python 3 interpreter, compiler and development environment (include
  7. directories and libraries).
  8. The following components are supported:
  9. * ``Interpreter``: search for Python 3 interpreter
  10. * ``Compiler``: search for Python 3 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 (Python3 COMPONENTS Interpreter Development)
  18. This module looks only for version 3 of Python. This module can be used
  19. concurrently with :module:`FindPython2` module to use both Python versions.
  20. The :module:`FindPython` module can be used if Python version does not matter
  21. for you.
  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. ``Python3::Interpreter``
  32. Python 3 interpreter. Target defined if component ``Interpreter`` is found.
  33. ``Python3::Compiler``
  34. Python 3 compiler. Target defined if component ``Compiler`` is found.
  35. ``Python3::Python``
  36. Python 3 library for Python embedding. Target defined if component
  37. ``Development`` is found.
  38. ``Python3::Module``
  39. Python 3 library for Python module. Target defined if component
  40. ``Development`` is found.
  41. ``Python3::NumPy``
  42. NumPy library for Python 3. 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. ``Python3_FOUND``
  48. System has the Python 3 requested components.
  49. ``Python3_Interpreter_FOUND``
  50. System has the Python 3 interpreter.
  51. ``Python3_EXECUTABLE``
  52. Path to the Python 3 interpreter.
  53. ``Python3_INTERPRETER_ID``
  54. A short string unique to the interpreter. Possible values include:
  55. * Python
  56. * ActivePython
  57. * Anaconda
  58. * Canopy
  59. * IronPython
  60. ``Python3_STDLIB``
  61. Standard platform independent installation directory.
  62. Information returned by
  63. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
  64. ``Python3_STDARCH``
  65. Standard platform dependent installation directory.
  66. Information returned by
  67. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
  68. ``Python3_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. ``Python3_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. ``Python3_SOABI``
  77. Extension suffix for modules.
  78. Information returned by
  79. ``distutils.sysconfig.get_config_flag('SOABI')`` or computed from
  80. ``distutils.sysconfig.get_config_flag('EXT_SUFFIX')`` or
  81. ``python3-config --extension-suffix``.
  82. ``Python3_Compiler_FOUND``
  83. System has the Python 3 compiler.
  84. ``Python3_COMPILER``
  85. Path to the Python 3 compiler. Only offered by IronPython.
  86. ``Python3_COMPILER_ID``
  87. A short string unique to the compiler. Possible values include:
  88. * IronPython
  89. ``Python3_Development_FOUND``
  90. System has the Python 3 development artifacts.
  91. ``Python3_INCLUDE_DIRS``
  92. The Python 3 include directories.
  93. ``Python3_LIBRARIES``
  94. The Python 3 libraries.
  95. ``Python3_LIBRARY_DIRS``
  96. The Python 3 library directories.
  97. ``Python3_RUNTIME_LIBRARY_DIRS``
  98. The Python 3 runtime library directories.
  99. ``Python3_VERSION``
  100. Python 3 version.
  101. ``Python3_VERSION_MAJOR``
  102. Python 3 major version.
  103. ``Python3_VERSION_MINOR``
  104. Python 3 minor version.
  105. ``Python3_VERSION_PATCH``
  106. Python 3 patch version.
  107. ``Python3_NumPy_FOUND``
  108. System has the NumPy.
  109. ``Python3_NumPy_INCLUDE_DIRS``
  110. The NumPy include directries.
  111. ``Python3_NumPy_VERSION``
  112. The NumPy version.
  113. Hints
  114. ^^^^^
  115. ``Python3_ROOT_DIR``
  116. Define the root directory of a Python 3 installation.
  117. ``Python3_USE_STATIC_LIBS``
  118. * If not defined, search for shared libraries and static libraries in that
  119. order.
  120. * If set to TRUE, search **only** for static libraries.
  121. * If set to FALSE, search **only** for shared libraries.
  122. ``Python3_FIND_ABI``
  123. This variable defines which ABIs, as defined in
  124. `PEP 3149 <https://www.python.org/dev/peps/pep-3149/>`_, should be searched.
  125. .. note::
  126. If ``Python3_FIND_ABI`` is not defined, any ABI will be searched.
  127. The ``Python3_FIND_ABI`` variable is a 3-tuple specifying, in that order,
  128. ``pydebug`` (``d``), ``pymalloc`` (``m``) and ``unicode`` (``u``) flags.
  129. Each element can be set to one of the following:
  130. * ``ON``: Corresponding flag is selected.
  131. * ``OFF``: Corresponding flag is not selected.
  132. * ``ANY``: The two posibilties (``ON`` and ``OFF``) will be searched.
  133. From this 3-tuple, various ABIs will be searched starting from the most
  134. specialized to the most general. Moreover, ``debug`` versions will be
  135. searched **after** ``non-debug`` ones.
  136. For example, if we have::
  137. set (Python3_FIND_ABI "ON" "ANY" "ANY")
  138. The following flags combinations will be appended, in that order, to the
  139. artifact names: ``dmu``, ``dm``, ``du``, and ``d``.
  140. And to search any possible ABIs::
  141. set (Python3_FIND_ABI "ANY" "ANY" "ANY")
  142. The following combinations, in that order, will be used: ``mu``, ``m``,
  143. ``u``, ``<empty>``, ``dmu``, ``dm``, ``du`` and ``d``.
  144. .. note::
  145. This hint is useful only on ``POSIX`` systems. So, on ``Windows`` systems,
  146. when ``Python3_FIND_ABI`` is defined, ``Python`` distributions from
  147. `python.org <https://www.python.org/>`_ will be found only if value for
  148. each flag is ``OFF`` or ``ANY``.
  149. ``Python3_FIND_STRATEGY``
  150. This variable defines how lookup will be done.
  151. The ``Python3_FIND_STRATEGY`` variable can be set to one of the following:
  152. * ``VERSION``: Try to find the most recent version in all specified
  153. locations.
  154. This is the default if policy :policy:`CMP0094` is undefined or set to
  155. ``OLD``.
  156. * ``LOCATION``: Stops lookup as soon as a version satisfying version
  157. constraints is founded.
  158. This is the default if policy :policy:`CMP0094` is set to ``NEW``.
  159. ``Python3_FIND_REGISTRY``
  160. On Windows the ``Python3_FIND_REGISTRY`` variable determine the order
  161. of preference between registry and environment variables.
  162. The ``Python3_FIND_REGISTRY`` variable can be set to one of the following:
  163. * ``FIRST``: Try to use registry before environment variables.
  164. This is the default.
  165. * ``LAST``: Try to use registry after environment variables.
  166. * ``NEVER``: Never try to use registry.
  167. ``Python3_FIND_FRAMEWORK``
  168. On macOS the ``Python3_FIND_FRAMEWORK`` variable determine the order of
  169. preference between Apple-style and unix-style package components.
  170. This variable can take same values as :variable:`CMAKE_FIND_FRAMEWORK`
  171. variable.
  172. .. note::
  173. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  174. If ``Python3_FIND_FRAMEWORK`` is not defined, :variable:`CMAKE_FIND_FRAMEWORK`
  175. variable will be used, if any.
  176. ``Python3_FIND_VIRTUALENV``
  177. This variable defines the handling of virtual environments managed by
  178. ``virtualenv`` or ``conda``. It is meaningful only when a virtual environment
  179. is active (i.e. the ``activate`` script has been evaluated). In this case, it
  180. takes precedence over ``Python3_FIND_REGISTRY`` and ``CMAKE_FIND_FRAMEWORK``
  181. variables. The ``Python3_FIND_VIRTUALENV`` variable can be set to one of the
  182. following:
  183. * ``FIRST``: The virtual environment is used before any other standard
  184. paths to look-up for the interpreter. This is the default.
  185. * ``ONLY``: Only the virtual environment is used to look-up for the
  186. interpreter.
  187. * ``STANDARD``: The virtual environment is not used to look-up for the
  188. interpreter. In this case, variable ``Python3_FIND_REGISTRY`` (Windows)
  189. or ``CMAKE_FIND_FRAMEWORK`` (macOS) can be set with value ``LAST`` or
  190. ``NEVER`` to select preferably the interpreter from the virtual
  191. environment.
  192. .. note::
  193. If the component ``Development`` is requested, it is **strongly**
  194. recommended to also include the component ``Interpreter`` to get expected
  195. result.
  196. Artifacts Specification
  197. ^^^^^^^^^^^^^^^^^^^^^^^
  198. To solve special cases, it is possible to specify directly the artifacts by
  199. setting the following variables:
  200. ``Python3_EXECUTABLE``
  201. The path to the interpreter.
  202. ``Python3_COMPILER``
  203. The path to the compiler.
  204. ``Python3_LIBRARY``
  205. The path to the library. It will be used to compute the
  206. variables ``Python3_LIBRARIES``, ``Python3_LIBRAY_DIRS`` and
  207. ``Python3_RUNTIME_LIBRARY_DIRS``.
  208. ``Python3_INCLUDE_DIR``
  209. The path to the directory of the ``Python`` headers. It will be used to
  210. compute the variable ``Python3_INCLUDE_DIRS``.
  211. ``Python3_NumPy_INCLUDE_DIR``
  212. The path to the directory of the ``NumPy`` headers. It will be used to
  213. compute the variable ``Python3_NumPy_INCLUDE_DIRS``.
  214. .. note::
  215. All paths must be absolute. Any artifact specified with a relative path
  216. will be ignored.
  217. .. note::
  218. When an artifact is specified, all ``HINTS`` will be ignored and no search
  219. will be performed for this artifact.
  220. If more than one artifact is specified, it is the user's responsability to
  221. ensure the consistency of the various artifacts.
  222. By default, this module supports multiple calls in different directories of a
  223. project with different version/component requirements while providing correct
  224. and consistent results for each call. To support this behavior, ``CMake`` cache
  225. is not used in the traditional way which can be problematic for interactive
  226. specification. So, to enable also interactive specification, module behavior
  227. can be controled with the following variable:
  228. ``Python3_ARTIFACTS_INTERACTIVE``
  229. Selects the behavior of the module. This is a boolean variable:
  230. * If set to ``TRUE``: Create CMake cache entries for the above artifact
  231. specification variables so that users can edit them interactively.
  232. This disables support for multiple version/component requirements.
  233. * If set to ``FALSE`` or undefined: Enable multiple version/component
  234. requirements.
  235. Commands
  236. ^^^^^^^^
  237. This module defines the command ``Python3_add_library`` (when
  238. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  239. :command:`add_library` and adds a dependency to target ``Python3::Python`` or,
  240. when library type is ``MODULE``, to target ``Python3::Module`` and takes care
  241. of Python module naming rules::
  242. Python3_add_library (<name> [STATIC | SHARED | MODULE [WITH_SOABI]]
  243. <source1> [<source2> ...])
  244. If the library type is not specified, ``MODULE`` is assumed.
  245. For ``MODULE`` library type, if option ``WITH_SOABI`` is specified, the
  246. module suffix will include the ``Python3_SOABI`` value, if any.
  247. #]=======================================================================]
  248. set (_PYTHON_PREFIX Python3)
  249. set (_Python3_REQUIRED_VERSION_MAJOR 3)
  250. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  251. if (COMMAND __Python3_add_library)
  252. macro (Python3_add_library)
  253. __Python3_add_library (Python3 ${ARGV})
  254. endmacro()
  255. endif()
  256. unset (_PYTHON_PREFIX)