FindPythonInterp.cmake 4.8 KB

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