FindPythonInterp.cmake 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. # Copyright 2005-2010 Kitware, Inc.
  32. # Copyright 2011 Bjoern Ricks <[email protected]>
  33. # Copyright 2012 Rolf Eike Beer <[email protected]>
  34. #
  35. # Distributed under the OSI-approved BSD License (the "License");
  36. # see accompanying file Copyright.txt for details.
  37. #
  38. # This software is distributed WITHOUT ANY WARRANTY; without even the
  39. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  40. # See the License for more information.
  41. #=============================================================================
  42. # (To distribute this file outside of CMake, substitute the full
  43. # License text for the above reference.)
  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.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. list(APPEND _Python_NAMES python)
  71. # Search for the current active python version first
  72. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  73. # Set up the versions we know about, in the order we will search. Always add
  74. # the user supplied additional versions to the front.
  75. set(_Python_VERSIONS
  76. ${Python_ADDITIONAL_VERSIONS}
  77. ${_PYTHON_FIND_OTHER_VERSIONS}
  78. )
  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 ${_Python_VERSIONS})
  86. set(_Python_NAMES python${_CURRENT_VERSION})
  87. if(WIN32)
  88. list(APPEND _Python_NAMES python)
  89. endif()
  90. find_program(PYTHON_EXECUTABLE
  91. NAMES ${_Python_NAMES}
  92. PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  93. )
  94. endforeach()
  95. endif()
  96. # determine python version string
  97. if(PYTHON_EXECUTABLE)
  98. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  99. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  100. OUTPUT_VARIABLE _VERSION
  101. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  102. ERROR_QUIET)
  103. if(NOT _PYTHON_VERSION_RESULT)
  104. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  105. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  106. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  107. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  108. if(PYTHON_VERSION_PATCH EQUAL 0)
  109. # it's called "Python 2.7", not "2.7.0"
  110. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  111. endif()
  112. else()
  113. # sys.version predates sys.version_info, so use that
  114. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
  115. OUTPUT_VARIABLE _VERSION
  116. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  117. ERROR_QUIET)
  118. if(NOT _PYTHON_VERSION_RESULT)
  119. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  120. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  121. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  122. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
  123. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
  124. else()
  125. set(PYTHON_VERSION_PATCH "0")
  126. endif()
  127. else()
  128. # sys.version was first documented for Python 1.5, so assume
  129. # this is older.
  130. set(PYTHON_VERSION_STRING "1.4")
  131. set(PYTHON_VERSION_MAJOR "1")
  132. set(PYTHON_VERSION_MAJOR "4")
  133. set(PYTHON_VERSION_MAJOR "0")
  134. endif()
  135. endif()
  136. unset(_PYTHON_VERSION_RESULT)
  137. unset(_VERSION)
  138. endif()
  139. # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
  140. # all listed variables are TRUE
  141. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  142. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  143. mark_as_advanced(PYTHON_EXECUTABLE)