FindPythonInterp.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindPythonInterp
  5. ----------------
  6. .. versionchanged:: 3.27
  7. This module is available only if policy :policy:`CMP0148` is not set to ``NEW``.
  8. .. deprecated:: 3.12
  9. Use :module:`FindPython3`, :module:`FindPython2`, or :module:`FindPython`
  10. instead.
  11. Finds the Python interpreter and determines the location of its executable:
  12. .. code-block:: cmake
  13. find_package(PythonInterp [<version>] [...])
  14. .. note::
  15. When using both this and the :module:`FindPythonLibs` module, call
  16. ``find_package(PythonInterp)`` before ``find_package(PythonLibs)``. This
  17. ensures that the detected interpreter version is used to guide the selection
  18. of compatible libraries, resulting in a consistent ``PYTHON_LIBRARIES`` value.
  19. .. note::
  20. A call to ``find_package(PythonInterp ${V})`` for Python version ``V`` may
  21. find a ``python`` executable with no version suffix. In this case no attempt
  22. is made to avoid Python executables from other versions. Use
  23. :module:`FindPython3`, :module:`FindPython2`, or :module:`FindPython` instead.
  24. Result Variables
  25. ^^^^^^^^^^^^^^^^
  26. This module defines the following variables:
  27. ``PythonInterp_FOUND``
  28. .. versionadded:: 3.3
  29. Boolean indicating whether the (requested version of) Python executable
  30. was found.
  31. ``PYTHON_VERSION_STRING``
  32. Python version found (e.g., ``2.5.2``).
  33. ``PYTHON_VERSION_MAJOR``
  34. Python major version found (e.g., ``2``).
  35. ``PYTHON_VERSION_MINOR``
  36. Python minor version found (e.g., ``5``).
  37. ``PYTHON_VERSION_PATCH``
  38. Python patch version found (e.g., ``2``).
  39. Cache Variables
  40. ^^^^^^^^^^^^^^^
  41. The following cache variables may also be set:
  42. ``PYTHON_EXECUTABLE``
  43. The path to the Python interpreter.
  44. Hints
  45. ^^^^^
  46. This module accepts the following variables before calling
  47. ``find_package(PythonInterp)``:
  48. ``Python_ADDITIONAL_VERSIONS``
  49. This variable can be used to specify a list of version numbers that should be
  50. taken into account when searching for Python.
  51. Deprecated Variables
  52. ^^^^^^^^^^^^^^^^^^^^
  53. The following variables are provided for backward compatibility:
  54. ``PYTHONINTERP_FOUND``
  55. .. deprecated:: 3.12
  56. Use ``PythonInterp_FOUND``, which has the same value.
  57. Boolean indicating whether the (requested version of) Python executable
  58. was found.
  59. Examples
  60. ^^^^^^^^
  61. Finding the Python interpreter in earlier versions of CMake:
  62. .. code-block:: cmake
  63. find_package(PythonInterp)
  64. execute_process(COMMAND ${PYTHON_EXECUTABLE} --help)
  65. Starting with CMake 3.12, the Python interpreter can be found using the
  66. :module:`FindPython` module. The equivalent example using the modern approach
  67. is:
  68. .. code-block:: cmake
  69. find_package(Python)
  70. execute_process(COMMAND ${Python_EXECUTABLE} --help)
  71. #]=======================================================================]
  72. cmake_policy(GET CMP0148 _FindPythonInterp_CMP0148)
  73. if(_FindPythonInterp_CMP0148 STREQUAL "NEW")
  74. message(FATAL_ERROR "The FindPythonInterp module has been removed by policy CMP0148.")
  75. endif()
  76. if(_FindPythonInterp_testing)
  77. set(_FindPythonInterp_included TRUE)
  78. return()
  79. endif()
  80. unset(_Python_NAMES)
  81. set(_PYTHON1_VERSIONS 1.6 1.5)
  82. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  83. set(_PYTHON3_VERSIONS 3.14 3.13 3.12 3.11 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
  84. if(PythonInterp_FIND_VERSION)
  85. if(PythonInterp_FIND_VERSION_COUNT GREATER 1)
  86. set(_PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION_MAJOR}.${PythonInterp_FIND_VERSION_MINOR}")
  87. list(APPEND _Python_NAMES
  88. python${_PYTHON_FIND_MAJ_MIN}
  89. python${PythonInterp_FIND_VERSION_MAJOR})
  90. unset(_PYTHON_FIND_OTHER_VERSIONS)
  91. if(NOT PythonInterp_FIND_VERSION_EXACT)
  92. foreach(_PYTHON_V ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  93. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  94. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  95. endif()
  96. endforeach()
  97. endif()
  98. unset(_PYTHON_FIND_MAJ_MIN)
  99. else()
  100. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION_MAJOR})
  101. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  102. endif()
  103. else()
  104. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  105. endif()
  106. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  107. # Set up the versions we know about, in the order we will search. Always add
  108. # the user supplied additional versions to the front.
  109. set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS})
  110. # If FindPythonInterp has already found the major and minor version,
  111. # insert that version next to get consistent versions of the interpreter and
  112. # library.
  113. if(DEFINED PYTHONLIBS_VERSION_STRING)
  114. string(REPLACE "." ";" _PYTHONLIBS_VERSION "${PYTHONLIBS_VERSION_STRING}")
  115. list(GET _PYTHONLIBS_VERSION 0 _PYTHONLIBS_VERSION_MAJOR)
  116. list(GET _PYTHONLIBS_VERSION 1 _PYTHONLIBS_VERSION_MINOR)
  117. list(APPEND _Python_VERSIONS ${_PYTHONLIBS_VERSION_MAJOR}.${_PYTHONLIBS_VERSION_MINOR})
  118. endif()
  119. # Search for the current active python version first
  120. list(APPEND _Python_VERSIONS ";")
  121. list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS})
  122. unset(_PYTHON_FIND_OTHER_VERSIONS)
  123. unset(_PYTHON1_VERSIONS)
  124. unset(_PYTHON2_VERSIONS)
  125. unset(_PYTHON3_VERSIONS)
  126. # Search for newest python version if python executable isn't found
  127. if(NOT PYTHON_EXECUTABLE)
  128. foreach(_CURRENT_VERSION IN LISTS _Python_VERSIONS)
  129. set(_Python_NAMES python${_CURRENT_VERSION})
  130. if(CMAKE_HOST_WIN32)
  131. list(APPEND _Python_NAMES python)
  132. endif()
  133. find_program(PYTHON_EXECUTABLE
  134. NAMES ${_Python_NAMES}
  135. PATHS
  136. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  137. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  138. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  139. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  140. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  141. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  142. )
  143. endforeach()
  144. endif()
  145. # determine python version string
  146. if(PYTHON_EXECUTABLE)
  147. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  148. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  149. OUTPUT_VARIABLE _VERSION
  150. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  151. ERROR_QUIET)
  152. if(NOT _PYTHON_VERSION_RESULT)
  153. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  154. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  155. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  156. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  157. if(PYTHON_VERSION_PATCH EQUAL 0)
  158. # it's called "Python 2.7", not "2.7.0"
  159. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  160. endif()
  161. else()
  162. # sys.version predates sys.version_info, so use that
  163. # sys.version was first documented for Python 1.5, so assume version 1.4
  164. # if retrieving sys.version fails.
  165. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "try: import sys; sys.stdout.write(sys.version)\nexcept: sys.stdout.write(\"1.4.0\")"
  166. OUTPUT_VARIABLE _VERSION
  167. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  168. ERROR_QUIET)
  169. if(NOT _PYTHON_VERSION_RESULT)
  170. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  171. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  172. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  173. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.([0-9]+)")
  174. set(PYTHON_VERSION_PATCH "${CMAKE_MATCH_1}")
  175. else()
  176. set(PYTHON_VERSION_PATCH "0")
  177. endif()
  178. else()
  179. unset(PYTHON_VERSION_STRING)
  180. unset(PYTHON_VERSION_MAJOR)
  181. unset(PYTHON_VERSION_MINOR)
  182. unset(PYTHON_VERSION_PATCH)
  183. endif()
  184. endif()
  185. unset(_PYTHON_VERSION_RESULT)
  186. unset(_VERSION)
  187. endif()
  188. include(FindPackageHandleStandardArgs)
  189. find_package_handle_standard_args(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  190. mark_as_advanced(PYTHON_EXECUTABLE)