FindPython3.cmake 13 KB

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