You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
7.7 KiB

  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. # You can point to a preferred python install to use by setting the following
  18. # to the point at the root directory of the python install:
  19. #
  20. # PYTHON_ROOT_DIR - The root directory of the python install
  21. #=============================================================================
  22. # Copyright 2005-2010 Kitware, Inc.
  23. # Copyright 2011 Bjoern Ricks <bjoern.ricks@gmail.com>
  24. # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
  25. #
  26. # Distributed under the OSI-approved BSD License (the "License");
  27. # see accompanying file Copyright.txt for details.
  28. #
  29. # This software is distributed WITHOUT ANY WARRANTY; without even the
  30. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  31. # See the License for more information.
  32. #=============================================================================
  33. # (To distribute this file outside of CMake, substitute the full
  34. # License text for the above reference.)
  35. unset(_Python_NAMES)
  36. set(_PYTHON1_VERSIONS 1.6 1.5)
  37. set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
  38. set(_PYTHON3_VERSIONS 3.3 3.2 3.1 3.0)
  39. if(PythonInterp_FIND_VERSION)
  40. if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
  41. string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
  42. string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
  43. list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
  44. unset(_PYTHON_FIND_OTHER_VERSIONS)
  45. if(NOT PythonInterp_FIND_VERSION_EXACT)
  46. foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
  47. if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
  48. list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
  49. endif()
  50. endforeach()
  51. endif()
  52. unset(_PYTHON_FIND_MAJ_MIN)
  53. unset(_PYTHON_FIND_MAJ)
  54. else()
  55. list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
  56. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
  57. endif()
  58. else()
  59. set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
  60. endif()
  61. list(APPEND _Python_NAMES python)
  62. # Search for the preferred executable first
  63. if( ${PYTHON_ROOT_DIR} )
  64. # Search for any of the executable names solely in the directory we've
  65. # been pointed to. Failure to find the python executable here is a fatal
  66. # fail.
  67. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES}
  68. PATHS ${PYTHON_ROOT_DIR}
  69. NO_DEFAULT_PATH )
  70. else()
  71. # If there is no specific path given, look for python in the path
  72. find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
  73. endif()
  74. # Set up the versions we know about, in the order we will search. Always add
  75. # the user supplied additional versions to the front.
  76. set(_Python_VERSIONS
  77. ${Python_ADDITIONAL_VERSIONS}
  78. ${_PYTHON_FIND_OTHER_VERSIONS}
  79. )
  80. unset(_PYTHON_FIND_OTHER_VERSIONS)
  81. unset(_PYTHON1_VERSIONS)
  82. unset(_PYTHON2_VERSIONS)
  83. unset(_PYTHON3_VERSIONS)
  84. # Search for newest python version if python executable isn't found
  85. if(NOT PYTHON_EXECUTABLE)
  86. # If using the MINGW compiler, we mustn't find the standard python
  87. # distribution because of multiple C-Runtime errors. We must instead
  88. # use the Python-a-mingw-us distribution
  89. if(MINGW)
  90. list( APPEND _Python_PPATHS ${PYTHON_ROOT_DIR} )
  91. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.9" )
  92. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.8" )
  93. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.7" )
  94. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.6" )
  95. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.5" )
  96. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.4" )
  97. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.3" )
  98. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.2" )
  99. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.1" )
  100. list( APPEND _Python_PPATHS "C:/python/${_CURRENT_VERSION}.0" )
  101. else()
  102. list( APPEND _Python_PPATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath] )
  103. endif()
  104. foreach(_CURRENT_VERSION ${_Python_VERSIONS})
  105. set(_Python_NAMES python${_CURRENT_VERSION})
  106. if(WIN32)
  107. list(APPEND _Python_NAMES python)
  108. endif()
  109. find_program(PYTHON_EXECUTABLE
  110. NAMES ${_Python_NAMES}
  111. PATHS ${_Python_PPATHS}
  112. )
  113. endforeach()
  114. endif()
  115. # determine python version string
  116. if(PYTHON_EXECUTABLE)
  117. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
  118. "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
  119. OUTPUT_VARIABLE _VERSION
  120. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  121. ERROR_QUIET)
  122. if(NOT _PYTHON_VERSION_RESULT)
  123. string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
  124. list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
  125. list(GET _VERSION 1 PYTHON_VERSION_MINOR)
  126. list(GET _VERSION 2 PYTHON_VERSION_PATCH)
  127. if(PYTHON_VERSION_PATCH EQUAL 0)
  128. # it's called "Python 2.7", not "2.7.0"
  129. string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
  130. endif()
  131. else()
  132. # sys.version predates sys.version_info, so use that
  133. execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
  134. OUTPUT_VARIABLE _VERSION
  135. RESULT_VARIABLE _PYTHON_VERSION_RESULT
  136. ERROR_QUIET)
  137. if(NOT _PYTHON_VERSION_RESULT)
  138. string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
  139. string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
  140. string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
  141. if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
  142. string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
  143. else()
  144. set(PYTHON_VERSION_PATCH "0")
  145. endif()
  146. else()
  147. # sys.version was first documented for Python 1.5, so assume
  148. # this is older.
  149. set(PYTHON_VERSION_STRING "1.4")
  150. set(PYTHON_VERSION_MAJOR "1")
  151. set(PYTHON_VERSION_MAJOR "4")
  152. set(PYTHON_VERSION_MAJOR "0")
  153. endif()
  154. endif()
  155. unset(_PYTHON_VERSION_RESULT)
  156. unset(_VERSION)
  157. endif()
  158. # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
  159. # all listed variables are TRUE
  160. include(FindPackageHandleStandardArgs)
  161. find_package_handle_standard_args(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
  162. mark_as_advanced(PYTHON_EXECUTABLE)