FindPythonInterp.cmake 6.1 KB

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