FindPythonInterp.cmake 6.7 KB

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