FindPython.cmake 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. Three 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`` is 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. Target defined if component ``Development`` is found.
  37. ``Python::NumPy``
  38. NumPy Python library. Target defined if component ``NumPy`` is found.
  39. Result Variables
  40. ^^^^^^^^^^^^^^^^
  41. This module will set the following variables in your project
  42. (see :ref:`Standard Variable Names <CMake Developer Standard Variable Names>`):
  43. ``Python_FOUND``
  44. System has the Python requested components.
  45. ``Python_Interpreter_FOUND``
  46. System has the Python interpreter.
  47. ``Python_EXECUTABLE``
  48. Path to the Python interpreter.
  49. ``Python_INTERPRETER_ID``
  50. A short string unique to the interpreter. Possible values include:
  51. * Python
  52. * ActivePython
  53. * Anaconda
  54. * Canopy
  55. * IronPython
  56. ``Python_STDLIB``
  57. Standard platform independent installation directory.
  58. Information returned by
  59. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=True)``.
  60. ``Python_STDARCH``
  61. Standard platform dependent installation directory.
  62. Information returned by
  63. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=True)``.
  64. ``Python_SITELIB``
  65. Third-party platform independent installation directory.
  66. Information returned by
  67. ``distutils.sysconfig.get_python_lib(plat_specific=False,standard_lib=False)``.
  68. ``Python_SITEARCH``
  69. Third-party platform dependent installation directory.
  70. Information returned by
  71. ``distutils.sysconfig.get_python_lib(plat_specific=True,standard_lib=False)``.
  72. ``Python_Compiler_FOUND``
  73. System has the Python compiler.
  74. ``Python_COMPILER``
  75. Path to the Python compiler. Only offered by IronPython.
  76. ``Python_COMPILER_ID``
  77. A short string unique to the compiler. Possible values include:
  78. * IronPython
  79. ``Python_Development_FOUND``
  80. System has the Python development artifacts.
  81. ``Python_INCLUDE_DIRS``
  82. The Python include directories.
  83. ``Python_LIBRARIES``
  84. The Python libraries.
  85. ``Python_LIBRARY_DIRS``
  86. The Python library directories.
  87. ``Python_RUNTIME_LIBRARY_DIRS``
  88. The Python runtime library directories.
  89. ``Python_VERSION``
  90. Python version.
  91. ``Python_VERSION_MAJOR``
  92. Python major version.
  93. ``Python_VERSION_MINOR``
  94. Python minor version.
  95. ``Python_VERSION_PATCH``
  96. Python patch version.
  97. ``Python_NumPy_FOUND``
  98. System has the NumPy.
  99. ``Python_NumPy_INCLUDE_DIRS``
  100. The NumPy include directries.
  101. ``Python_NumPy_VERSION``
  102. The NumPy version.
  103. Hints
  104. ^^^^^
  105. ``Python_ROOT_DIR``
  106. Define the root directory of a Python installation.
  107. ``Python_USE_STATIC_LIBS``
  108. * If not defined, search for shared libraries and static libraries in that
  109. order.
  110. * If set to TRUE, search **only** for static libraries.
  111. * If set to FALSE, search **only** for shared libraries.
  112. ``Python_FIND_REGISTRY``
  113. On Windows the ``Python_FIND_REGISTRY`` variable determine the order
  114. of preference between registry and environment variables.
  115. the ``Python_FIND_REGISTRY`` variable can be set to empty or one of the
  116. following:
  117. * ``FIRST``: Try to use registry before environment variables.
  118. This is the default.
  119. * ``LAST``: Try to use registry after environment variables.
  120. * ``NEVER``: Never try to use registry.
  121. ``CMAKE_FIND_FRAMEWORK``
  122. On OS X the :variable:`CMAKE_FIND_FRAMEWORK` variable determine the order of
  123. preference between Apple-style and unix-style package components.
  124. .. note::
  125. Value ``ONLY`` is not supported so ``FIRST`` will be used instead.
  126. .. note::
  127. If a Python virtual environment is configured, set variable
  128. ``Python_FIND_REGISTRY`` (Windows) or ``CMAKE_FIND_FRAMEWORK`` (macOS) with
  129. value ``LAST`` or ``NEVER`` to select it preferably.
  130. Commands
  131. ^^^^^^^^
  132. This module defines the command ``Python_add_library`` (when
  133. :prop_gbl:`CMAKE_ROLE` is ``PROJECT``), which has the same semantics as
  134. :command:`add_library`, but takes care of Python module naming rules
  135. (only applied if library is of type ``MODULE``), and adds a dependency to target
  136. ``Python::Python``::
  137. Python_add_library (my_module MODULE src1.cpp)
  138. If library type is not specified, ``MODULE`` is assumed.
  139. #]=======================================================================]
  140. set (_PYTHON_PREFIX Python)
  141. if (DEFINED Python_FIND_VERSION)
  142. set (_Python_REQUIRED_VERSION_MAJOR ${Python_FIND_VERSION_MAJOR})
  143. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  144. else()
  145. # iterate over versions in quiet and NOT required modes to avoid multiple
  146. # "Found" messages and prematurally failure.
  147. set (_Python_QUIETLY ${Python_FIND_QUIETLY})
  148. set (_Python_REQUIRED ${Python_FIND_REQUIRED})
  149. set (Python_FIND_QUIETLY TRUE)
  150. set (Python_FIND_REQUIRED FALSE)
  151. set (_Python_REQUIRED_VERSIONS 3 2)
  152. set (_Python_REQUIRED_VERSION_LAST 2)
  153. foreach (_Python_REQUIRED_VERSION_MAJOR IN LISTS _Python_REQUIRED_VERSIONS)
  154. set (Python_FIND_VERSION ${_Python_REQUIRED_VERSION_MAJOR})
  155. include (${CMAKE_CURRENT_LIST_DIR}/FindPython/Support.cmake)
  156. if (Python_FOUND OR
  157. _Python_REQUIRED_VERSION_MAJOR EQUAL _Python_REQUIRED_VERSION_LAST)
  158. break()
  159. endif()
  160. # clean-up some CACHE variables to ensure look-up restart from scratch
  161. foreach (_Python_ITEM IN LISTS _Python_CACHED_VARS)
  162. unset (${_Python_ITEM} CACHE)
  163. endforeach()
  164. endforeach()
  165. unset (Python_FIND_VERSION)
  166. set (Python_FIND_QUIETLY ${_Python_QUIETLY})
  167. set (Python_FIND_REQUIRED ${_Python_REQUIRED})
  168. if (Python_FIND_REQUIRED OR NOT Python_FIND_QUIETLY)
  169. # call again validation command to get "Found" or error message
  170. find_package_handle_standard_args (Python HANDLE_COMPONENTS
  171. REQUIRED_VARS ${_Python_REQUIRED_VARS}
  172. VERSION_VAR Python_VERSION)
  173. endif()
  174. endif()
  175. if (COMMAND __Python_add_library)
  176. macro (Python_add_library)
  177. __Python_add_library (Python ${ARGV})
  178. endmacro()
  179. endif()
  180. unset (_PYTHON_PREFIX)