1# Distributed under the OSI-approved BSD 3-Clause License. See accompanying 2# file Copyright.txt or https://cmake.org/licensing for details. 3 4#[=======================================================================[.rst: 5FindPythonInterp 6---------------- 7 8.. deprecated:: 3.12 9 10 Use :module:`FindPython3`, :module:`FindPython2` or :module:`FindPython` instead. 11 12Find python interpreter 13 14This module finds if Python interpreter is installed and determines 15where the executables are. This code sets the following variables: 16 17:: 18 19 PYTHONINTERP_FOUND - Was the Python executable found 20 PYTHON_EXECUTABLE - path to the Python interpreter 21 22 23 24:: 25 26 PYTHON_VERSION_STRING - Python version found e.g. 2.5.2 27 PYTHON_VERSION_MAJOR - Python major version found e.g. 2 28 PYTHON_VERSION_MINOR - Python minor version found e.g. 5 29 PYTHON_VERSION_PATCH - Python patch version found e.g. 2 30 31 32 33The Python_ADDITIONAL_VERSIONS variable can be used to specify a list 34of version numbers that should be taken into account when searching 35for Python. You need to set this variable before calling 36find_package(PythonInterp). 37 38If calling both ``find_package(PythonInterp)`` and 39``find_package(PythonLibs)``, call ``find_package(PythonInterp)`` first to 40get the currently active Python version by default with a consistent version 41of PYTHON_LIBRARIES. 42 43.. note:: 44 45 A call to ``find_package(PythonInterp ${V})`` for python version ``V`` 46 may find a ``python`` executable with no version suffix. In this case 47 no attempt is made to avoid python executables from other versions. 48 Use :module:`FindPython3`, :module:`FindPython2` or :module:`FindPython` 49 instead. 50 51#]=======================================================================] 52 53unset(_Python_NAMES) 54 55set(_PYTHON1_VERSIONS 1.6 1.5) 56set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0) 57set(_PYTHON3_VERSIONS 3.10 3.9 3.8 3.7 3.6 3.5 3.4 3.3 3.2 3.1 3.0) 58 59if(PythonInterp_FIND_VERSION) 60 if(PythonInterp_FIND_VERSION_COUNT GREATER 1) 61 set(_PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION_MAJOR}.${PythonInterp_FIND_VERSION_MINOR}") 62 list(APPEND _Python_NAMES 63 python${_PYTHON_FIND_MAJ_MIN} 64 python${PythonInterp_FIND_VERSION_MAJOR}) 65 unset(_PYTHON_FIND_OTHER_VERSIONS) 66 if(NOT PythonInterp_FIND_VERSION_EXACT) 67 foreach(_PYTHON_V ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS}) 68 if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN) 69 list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V}) 70 endif() 71 endforeach() 72 endif() 73 unset(_PYTHON_FIND_MAJ_MIN) 74 else() 75 list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION_MAJOR}) 76 set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION_MAJOR}_VERSIONS}) 77 endif() 78else() 79 set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS}) 80endif() 81find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES}) 82 83# Set up the versions we know about, in the order we will search. Always add 84# the user supplied additional versions to the front. 85set(_Python_VERSIONS ${Python_ADDITIONAL_VERSIONS}) 86# If FindPythonInterp has already found the major and minor version, 87# insert that version next to get consistent versions of the interpreter and 88# library. 89if(DEFINED PYTHONLIBS_VERSION_STRING) 90 string(REPLACE "." ";" _PYTHONLIBS_VERSION "${PYTHONLIBS_VERSION_STRING}") 91 list(GET _PYTHONLIBS_VERSION 0 _PYTHONLIBS_VERSION_MAJOR) 92 list(GET _PYTHONLIBS_VERSION 1 _PYTHONLIBS_VERSION_MINOR) 93 list(APPEND _Python_VERSIONS ${_PYTHONLIBS_VERSION_MAJOR}.${_PYTHONLIBS_VERSION_MINOR}) 94endif() 95# Search for the current active python version first 96list(APPEND _Python_VERSIONS ";") 97list(APPEND _Python_VERSIONS ${_PYTHON_FIND_OTHER_VERSIONS}) 98 99unset(_PYTHON_FIND_OTHER_VERSIONS) 100unset(_PYTHON1_VERSIONS) 101unset(_PYTHON2_VERSIONS) 102unset(_PYTHON3_VERSIONS) 103 104# Search for newest python version if python executable isn't found 105if(NOT PYTHON_EXECUTABLE) 106 foreach(_CURRENT_VERSION IN LISTS _Python_VERSIONS) 107 set(_Python_NAMES python${_CURRENT_VERSION}) 108 if(CMAKE_HOST_WIN32) 109 list(APPEND _Python_NAMES python) 110 endif() 111 find_program(PYTHON_EXECUTABLE 112 NAMES ${_Python_NAMES} 113 PATHS 114 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath] 115 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath] 116 [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath] 117 [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath] 118 [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-32\\InstallPath] 119 [HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}-64\\InstallPath] 120 ) 121 endforeach() 122endif() 123 124# determine python version string 125if(PYTHON_EXECUTABLE) 126 execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c 127 "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))" 128 OUTPUT_VARIABLE _VERSION 129 RESULT_VARIABLE _PYTHON_VERSION_RESULT 130 ERROR_QUIET) 131 if(NOT _PYTHON_VERSION_RESULT) 132 string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}") 133 list(GET _VERSION 0 PYTHON_VERSION_MAJOR) 134 list(GET _VERSION 1 PYTHON_VERSION_MINOR) 135 list(GET _VERSION 2 PYTHON_VERSION_PATCH) 136 if(PYTHON_VERSION_PATCH EQUAL 0) 137 # it's called "Python 2.7", not "2.7.0" 138 string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}") 139 endif() 140 else() 141 # sys.version predates sys.version_info, so use that 142 # sys.version was first documented for Python 1.5, so assume version 1.4 143 # if retrieving sys.version fails. 144 execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "try: import sys; sys.stdout.write(sys.version)\nexcept: sys.stdout.write(\"1.4.0\")" 145 OUTPUT_VARIABLE _VERSION 146 RESULT_VARIABLE _PYTHON_VERSION_RESULT 147 ERROR_QUIET) 148 if(NOT _PYTHON_VERSION_RESULT) 149 string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}") 150 string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}") 151 string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}") 152 if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.([0-9]+)") 153 set(PYTHON_VERSION_PATCH "${CMAKE_MATCH_1}") 154 else() 155 set(PYTHON_VERSION_PATCH "0") 156 endif() 157 else() 158 unset(PYTHON_VERSION_STRING) 159 unset(PYTHON_VERSION_MAJOR) 160 unset(PYTHON_VERSION_MINOR) 161 unset(PYTHON_VERSION_PATCH) 162 endif() 163 endif() 164 unset(_PYTHON_VERSION_RESULT) 165 unset(_VERSION) 166endif() 167 168include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake) 169FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING) 170 171mark_as_advanced(PYTHON_EXECUTABLE) 172