FindPythonInterp.cmake 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. Boolean indicating whether the (requested version of) Python executable is
  29. found. For backward compatibility, the ``PYTHONINTERP_FOUND`` variable is
  30. also set to the same value.
  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. Examples
  52. ^^^^^^^^
  53. Finding the Python interpreter in earlier versions of CMake:
  54. .. code-block:: cmake
  55. find_package(PythonInterp)
  56. execute_process(COMMAND ${PYTHON_EXECUTABLE} --help)
  57. Starting with CMake 3.12, the Python interpreter can be found using the
  58. :module:`FindPython` module. The equivalent example using the modern approach
  59. is:
  60. .. code-block:: cmake
  61. find_package(Python)
  62. execute_process(COMMAND ${Python_EXECUTABLE} --help)
  63. #]=======================================================================]
  64. cmake_policy(GET CMP0148 _FindPythonInterp_CMP0148)
  65. if(_FindPythonInterp_CMP0148 STREQUAL "NEW")
  66. message(FATAL_ERROR "The FindPythonInterp module has been removed by policy CMP0148.")
  67. endif()
  68. if(_FindPythonInterp_testing)
  69. set(_FindPythonInterp_included TRUE)
  70. return()
  71. endif()
  72. unset(_Python_NAMES)
  73. set(_PYTHON1_VERSIONS 1.6 1.5)
  74. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  75. 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)
  76. if(PythonInterp_FIND_VERSION)
  77. if(PythonInterp_FIND_VERSION_COUNT GREATER 1)
  78. set(_PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION_MAJOR}.${PythonInterp_FIND_VERSION_MINOR}")
  79. list(APPEND _Python_NAMES
  80. python${_PYTHON_FIND_MAJ_MIN}
  81. python${PythonInterp_FIND_VERSION_MAJOR})
  82. unset(_PYTHON_FIND_OTHER_VERSIONS)
  83. if(NOT PythonInterp_FIND_VERSION_EXACT)
  84. foreach(_PYTHON_V ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  85. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  86. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  87. endif()
  88. endforeach()
  89. endif()
  90. unset(_PYTHON_FIND_MAJ_MIN)
  91. else()
  92. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION_MAJOR})
  93. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  94. endif()
  95. else()
  96. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  97. endif()
  98. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  99. # Set up the versions we know about, in the order we will search. Always add
  100. # the user supplied additional versions to the front.
  101. set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS})
  102. # If FindPythonInterp has already found the major and minor version,
  103. # insert that version next to get consistent versions of the interpreter and
  104. # library.
  105. if(DEFINED PYTHONLIBS_VERSION_STRING)
  106. string(REPLACE "." ";" _PYTHONLIBS_VERSION "${PYTHONLIBS_VERSION_STRING}")
  107. list(GET _PYTHONLIBS_VERSION 0 _PYTHONLIBS_VERSION_MAJOR)
  108. list(GET _PYTHONLIBS_VERSION 1 _PYTHONLIBS_VERSION_MINOR)
  109. list(APPEND _Python_VERSIONS ${_PYTHONLIBS_VERSION_MAJOR}.${_PYTHONLIBS_VERSION_MINOR})
  110. endif()
  111. # Search for the current active python version first
  112. list(APPEND _Python_VERSIONS ";")
  113. list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS})
  114. unset(_PYTHON_FIND_OTHER_VERSIONS)
  115. unset(_PYTHON1_VERSIONS)
  116. unset(_PYTHON2_VERSIONS)
  117. unset(_PYTHON3_VERSIONS)
  118. # Search for newest python version if python executable isn't found
  119. if(NOT PYTHON_EXECUTABLE)
  120. foreach(_CURRENT_VERSION IN LISTS _Python_VERSIONS)
  121. set(_Python_NAMES python${_CURRENT_VERSION})
  122. if(CMAKE_HOST_WIN32)
  123. list(APPEND _Python_NAMES python)
  124. endif()
  125. find_program(PYTHON_EXECUTABLE
  126. NAMES ${_Python_NAMES}
  127. PATHS
  128. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  129. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  130. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  131. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  132. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  133. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  134. )
  135. endforeach()
  136. endif()
  137. # determine python version string
  138. if(PYTHON_EXECUTABLE)
  139. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  140. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  141. OUTPUT_VARIABLE _VERSION
  142. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  143. ERROR_QUIET)
  144. if(NOT _PYTHON_VERSION_RESULT)
  145. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  146. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  147. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  148. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  149. if(PYTHON_VERSION_PATCH EQUAL 0)
  150. # it's called "Python 2.7", not "2.7.0"
  151. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  152. endif()
  153. else()
  154. # sys.version predates sys.version_info, so use that
  155. # sys.version was first documented for Python 1.5, so assume version 1.4
  156. # if retrieving sys.version fails.
  157. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "try: import sys; sys.stdout.write(sys.version)\nexcept: sys.stdout.write(\"1.4.0\")"
  158. OUTPUT_VARIABLE _VERSION
  159. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  160. ERROR_QUIET)
  161. if(NOT _PYTHON_VERSION_RESULT)
  162. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  163. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  164. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  165. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.([0-9]+)")
  166. set(PYTHON_VERSION_PATCH "${CMAKE_MATCH_1}")
  167. else()
  168. set(PYTHON_VERSION_PATCH "0")
  169. endif()
  170. else()
  171. unset(PYTHON_VERSION_STRING)
  172. unset(PYTHON_VERSION_MAJOR)
  173. unset(PYTHON_VERSION_MINOR)
  174. unset(PYTHON_VERSION_PATCH)
  175. endif()
  176. endif()
  177. unset(_PYTHON_VERSION_RESULT)
  178. unset(_VERSION)
  179. endif()
  180. include(FindPackageHandleStandardArgs)
  181. find_package_handle_standard_args(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  182. mark_as_advanced(PYTHON_EXECUTABLE)