FindPythonInterp.cmake 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. 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` instead.
  10. Find python interpreter
  11. This module finds if Python interpreter is installed and determines
  12. where the executables are. This code sets the following variables:
  13. ::
  14. PYTHONINTERP_FOUND - Was the Python executable found
  15. PYTHON_EXECUTABLE - path to the Python interpreter
  16. ::
  17. PYTHON_VERSION_STRING - Python version found e.g. 2.5.2
  18. PYTHON_VERSION_MAJOR - Python major version found e.g. 2
  19. PYTHON_VERSION_MINOR - Python minor version found e.g. 5
  20. PYTHON_VERSION_PATCH - Python patch version found e.g. 2
  21. The Python_ADDITIONAL_VERSIONS variable can be used to specify a list
  22. of version numbers that should be taken into account when searching
  23. for Python. You need to set this variable before calling
  24. find_package(PythonInterp).
  25. If calling both ``find_package(PythonInterp)`` and
  26. ``find_package(PythonLibs)``, call ``find_package(PythonInterp)`` first to
  27. get the currently active Python version by default with a consistent version
  28. of PYTHON_LIBRARIES.
  29. .. note::
  30. A call to ``find_package(PythonInterp ${V})`` for python version ``V``
  31. may find a ``python`` executable with no version suffix. In this case
  32. no attempt is made to avoid python executables from other versions.
  33. Use :module:`FindPython3`, :module:`FindPython2` or :module:`FindPython`
  34. instead.
  35. #]=======================================================================]
  36. cmake_policy(GET CMP0148 _FindPythonInterp_CMP0148)
  37. if(_FindPythonInterp_CMP0148 STREQUAL "NEW")
  38. message(FATAL_ERROR "The FindPythonInterp module has been removed by policy CMP0148.")
  39. endif()
  40. if(_FindPythonInterp_testing)
  41. set(_FindPythonInterp_included TRUE)
  42. return()
  43. endif()
  44. unset(_Python_NAMES)
  45. set(_PYTHON1_VERSIONS 1.6 1.5)
  46. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  47. set(_PYTHON3_VERSIONS 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)
  48. if(PythonInterp_FIND_VERSION)
  49. if(PythonInterp_FIND_VERSION_COUNT GREATER 1)
  50. set(_PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION_MAJOR}.${PythonInterp_FIND_VERSION_MINOR}")
  51. list(APPEND _Python_NAMES
  52. python${_PYTHON_FIND_MAJ_MIN}
  53. python${PythonInterp_FIND_VERSION_MAJOR})
  54. unset(_PYTHON_FIND_OTHER_VERSIONS)
  55. if(NOT PythonInterp_FIND_VERSION_EXACT)
  56. foreach(_PYTHON_V ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  57. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  58. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  59. endif()
  60. endforeach()
  61. endif()
  62. unset(_PYTHON_FIND_MAJ_MIN)
  63. else()
  64. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION_MAJOR})
  65. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS})
  66. endif()
  67. else()
  68. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  69. endif()
  70. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  71. # Set up the versions we know about, in the order we will search. Always add
  72. # the user supplied additional versions to the front.
  73. set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS})
  74. # If FindPythonInterp has already found the major and minor version,
  75. # insert that version next to get consistent versions of the interpreter and
  76. # library.
  77. if(DEFINED PYTHONLIBS_VERSION_STRING)
  78. string(REPLACE "." ";" _PYTHONLIBS_VERSION "${PYTHONLIBS_VERSION_STRING}")
  79. list(GET _PYTHONLIBS_VERSION 0 _PYTHONLIBS_VERSION_MAJOR)
  80. list(GET _PYTHONLIBS_VERSION 1 _PYTHONLIBS_VERSION_MINOR)
  81. list(APPEND _Python_VERSIONS ${_PYTHONLIBS_VERSION_MAJOR}.${_PYTHONLIBS_VERSION_MINOR})
  82. endif()
  83. # Search for the current active python version first
  84. list(APPEND _Python_VERSIONS ";")
  85. list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS})
  86. unset(_PYTHON_FIND_OTHER_VERSIONS)
  87. unset(_PYTHON1_VERSIONS)
  88. unset(_PYTHON2_VERSIONS)
  89. unset(_PYTHON3_VERSIONS)
  90. # Search for newest python version if python executable isn't found
  91. if(NOT PYTHON_EXECUTABLE)
  92. foreach(_CURRENT_VERSION IN LISTS _Python_VERSIONS)
  93. set(_Python_NAMES python${_CURRENT_VERSION})
  94. if(CMAKE_HOST_WIN32)
  95. list(APPEND _Python_NAMES python)
  96. endif()
  97. find_program(PYTHON_EXECUTABLE
  98. NAMES ${_Python_NAMES}
  99. PATHS
  100. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  101. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  102. [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  103. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  104. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath]
  105. [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath]
  106. )
  107. endforeach()
  108. endif()
  109. # determine python version string
  110. if(PYTHON_EXECUTABLE)
  111. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  112. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  113. OUTPUT_VARIABLE _VERSION
  114. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  115. ERROR_QUIET)
  116. if(NOT _PYTHON_VERSION_RESULT)
  117. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  118. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  119. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  120. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  121. if(PYTHON_VERSION_PATCH EQUAL 0)
  122. # it's called "Python 2.7", not "2.7.0"
  123. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  124. endif()
  125. else()
  126. # sys.version predates sys.version_info, so use that
  127. # sys.version was first documented for Python 1.5, so assume version 1.4
  128. # if retrieving sys.version fails.
  129. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "try: import sys; sys.stdout.write(sys.version)\nexcept: sys.stdout.write(\"1.4.0\")"
  130. OUTPUT_VARIABLE _VERSION
  131. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  132. ERROR_QUIET)
  133. if(NOT _PYTHON_VERSION_RESULT)
  134. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  135. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  136. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  137. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.([0-9]+)")
  138. set(PYTHON_VERSION_PATCH "${CMAKE_MATCH_1}")
  139. else()
  140. set(PYTHON_VERSION_PATCH "0")
  141. endif()
  142. else()
  143. unset(PYTHON_VERSION_STRING)
  144. unset(PYTHON_VERSION_MAJOR)
  145. unset(PYTHON_VERSION_MINOR)
  146. unset(PYTHON_VERSION_PATCH)
  147. endif()
  148. endif()
  149. unset(_PYTHON_VERSION_RESULT)
  150. unset(_VERSION)
  151. endif()
  152. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  153. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  154. mark_as_advanced(PYTHON_EXECUTABLE)