FindPythonInterp.cmake 6.6 KB

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