1
0

FindPythonInterp.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. #
  20. # Distributed under the OSI-approved BSD License (the "License");
  21. # see accompanying file Copyright.txt for details.
  22. #
  23. # This software is distributed WITHOUT ANY WARRANTY; without even the
  24. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  25. # See the License for more information.
  26. #=============================================================================
  27. # (To distribute this file outside of CMake, substitute the full
  28. # License text for the above reference.)
  29. unset(_Python_NAMES)
  30. set(_PYTHON1_VERSIONS 1.6 1.5)
  31. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  32. set(_PYTHON3_VERSIONS 3.3 3.2 3.1 3.0)
  33. if(PythonInterp_FIND_VERSION)
  34. if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  35. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
  36. string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
  37. list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
  38. unset(_PYTHON_FIND_OTHER_VERSIONS)
  39. if(NOT PythonInterp_FIND_VERSION_EXACT)
  40. foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
  41. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  42. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  43. endif()
  44. endforeach()
  45. endif(NOT PythonInterp_FIND_VERSION_EXACT)
  46. unset(_PYTHON_FIND_MAJ_MIN)
  47. unset(_PYTHON_FIND_MAJ)
  48. else(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  49. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
  50. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
  51. endif(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  52. else(PythonInterp_FIND_VERSION)
  53. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  54. endif(PythonInterp_FIND_VERSION)
  55. list(APPEND _Python_NAMES python)
  56. # Search for the current active python version first
  57. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  58. # Set up the versions we know about, in the order we will search. Always add
  59. # the user supplied additional versions to the front.
  60. set(_Python_VERSIONS
  61. ${Python_ADDITIONAL_VERSIONS}
  62. ${_PYTHON_FIND_OTHER_VERSIONS}
  63. )
  64. unset(_PYTHON_FIND_OTHER_VERSIONS)
  65. unset(_PYTHON1_VERSIONS)
  66. unset(_PYTHON2_VERSIONS)
  67. unset(_PYTHON3_VERSIONS)
  68. # Search for newest python version if python executable isn't found
  69. if(NOT PYTHON_EXECUTABLE)
  70. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  71. set(_Python_NAMES python${_CURRENT_VERSION})
  72. if(WIN32)
  73. list(APPEND _Python_NAMES python)
  74. endif()
  75. find_program(PYTHON_EXECUTABLE
  76. NAMES ${_Python_NAMES}
  77. PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
  78. )
  79. endforeach()
  80. endif()
  81. # determine python version string
  82. if(PYTHON_EXECUTABLE)
  83. execute_process(COMMAND "${PYTHON_EXECUTABLE}" --version
  84. ERROR_VARIABLE _VERSION
  85. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  86. OUTPUT_QUIET
  87. ERROR_STRIP_TRAILING_WHITESPACE)
  88. if(_PYTHON_VERSION_RESULT)
  89. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -V
  90. ERROR_VARIABLE _VERSION
  91. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  92. OUTPUT_QUIET
  93. ERROR_STRIP_TRAILING_WHITESPACE)
  94. endif(_PYTHON_VERSION_RESULT)
  95. if(NOT _PYTHON_VERSION_RESULT AND _VERSION MATCHES "^Python [0-9]+\\.[0-9]+.*")
  96. string(REPLACE "Python " "" PYTHON_VERSION_STRING "${_VERSION}")
  97. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  98. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  99. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
  100. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
  101. endif()
  102. endif()
  103. unset(_PYTHON_VERSION_RESULT)
  104. unset(_VERSION)
  105. endif(PYTHON_EXECUTABLE)
  106. # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
  107. # all listed variables are TRUE
  108. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  109. FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  110. mark_as_advanced(PYTHON_EXECUTABLE)