FindPythonInterp.cmake 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # - Find python interpreter
  2. # This module finds if Python interpreter is installed and determines where the
  3. # executables are. This code sets the following variables:
  4. #
  5. # PYTHONINTERP_FOUND - Was the Python executable found
  6. # PYTHON_EXECUTABLE - path to the Python interpreter
  7. #
  8. # PYTHON_VERSION_STRING - Python version found e.g. 2.5.2
  9. # PYTHON_VERSION_MAJOR - Python major version found e.g. 2
  10. # PYTHON_VERSION_MINOR - Python minor version found e.g. 5
  11. # PYTHON_VERSION_PATCH - Python patch version found e.g. 2
  12. #
  13. # Python_ADDITIONAL_VERSIONS - list of additional Python versions to search for
  14. #=============================================================================
  15. # Copyright 2005-2010 Kitware, Inc.
  16. # Copyright 2011 Bjoern Ricks <[email protected]>
  17. # Copyright 2012 Rolf Eike Beer <[email protected]>
  18. #
  19. # Distributed under the OSI-approved BSD License (the "License");
  20. # see accompanying file Copyright.txt for details.
  21. #
  22. # This software is distributed WITHOUT ANY WARRANTY; without even the
  23. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  24. # See the License for more information.
  25. #=============================================================================
  26. # (To distribute this file outside of CMake, substitute the full
  27. # License text for the above reference.)
  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.3 3.2 3.1 3.0)
  32. if(PythonInterp_FIND_VERSION)
  33. if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  34. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
  35. string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
  36. list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
  37. unset(_PYTHON_FIND_OTHER_VERSIONS)
  38. if(NOT PythonInterp_FIND_VERSION_EXACT)
  39. foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
  40. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  41. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  42. endif()
  43. endforeach()
  44. endif(NOT PythonInterp_FIND_VERSION_EXACT)
  45. unset(_PYTHON_FIND_MAJ_MIN)
  46. unset(_PYTHON_FIND_MAJ)
  47. else(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  48. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
  49. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
  50. endif(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  51. else(PythonInterp_FIND_VERSION)
  52. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  53. endif(PythonInterp_FIND_VERSION)
  54. list(APPEND _Python_NAMES python)
  55. # Search for the current active python version first
  56. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  57. # Set up the versions we know about, in the order we will search. Always add
  58. # the user supplied additional versions to the front.
  59. set(_Python_VERSIONS
  60. ${Python_ADDITIONAL_VERSIONS}
  61. ${_PYTHON_FIND_OTHER_VERSIONS}
  62. )
  63. unset(_PYTHON_FIND_OTHER_VERSIONS)
  64. unset(_PYTHON1_VERSIONS)
  65. unset(_PYTHON2_VERSIONS)
  66. unset(_PYTHON3_VERSIONS)
  67. # Search for newest python version if python executable isn't found
  68. if(NOT PYTHON_EXECUTABLE)
  69. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  70. set(_Python_NAMES python${_CURRENT_VERSION})
  71. if(WIN32)
  72. list(APPEND _Python_NAMES python)
  73. endif()
  74. find_program(PYTHON_EXECUTABLE
  75. NAMES ${_Python_NAMES}
  76. PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  77. )
  78. endforeach()
  79. endif()
  80. # determine python version string
  81. if(PYTHON_EXECUTABLE)
  82. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  83. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  84. OUTPUT_VARIABLE _VERSION
  85. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  86. ERROR_QUIET)
  87. if(NOT _PYTHON_VERSION_RESULT)
  88. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  89. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  90. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  91. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  92. if(PYTHON_VERSION_PATCH EQUAL 0)
  93. # it's called "Python 2.7", not "2.7.0"
  94. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  95. endif()
  96. else()
  97. # sys.version predates sys.version_info, so use that
  98. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
  99. OUTPUT_VARIABLE _VERSION
  100. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  101. ERROR_QUIET)
  102. if(NOT _PYTHON_VERSION_RESULT)
  103. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  104. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  105. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  106. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
  107. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
  108. else()
  109. set(PYTHON_VERSION_PATCH "0")
  110. endif()
  111. else()
  112. # sys.version was first documented for Python 1.5, so assume
  113. # this is older.
  114. set(PYTHON_VERSION_STRING "1.4")
  115. set(PYTHON_VERSION_MAJOR "1")
  116. set(PYTHON_VERSION_MAJOR "4")
  117. set(PYTHON_VERSION_MAJOR "0")
  118. endif()
  119. endif()
  120. unset(_PYTHON_VERSION_RESULT)
  121. unset(_VERSION)
  122. endif(PYTHON_EXECUTABLE)
  123. # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
  124. # all listed variables are TRUE
  125. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  126. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  127. mark_as_advanced(PYTHON_EXECUTABLE)