FindPythonInterp.cmake 6.8 KB

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