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:
5 # PYTHONINTERP_FOUND - Was the Python executable found
6 # PYTHON_EXECUTABLE - path to the Python interpreter
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
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).
17 #=============================================================================
18 # Copyright 2005-2010 Kitware, Inc.
19 # Copyright 2011 Bjoern Ricks <bjoern.ricks@gmail.com>
20 # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
22 # Distributed under the OSI-approved BSD License (the "License");
23 # see accompanying file Copyright.txt for details.
25 # This software is distributed WITHOUT ANY WARRANTY; without even the
26 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27 # See the License for more information.
28 #=============================================================================
29 # (To distribute this file outside of CMake, substitute the full
30 # License text for the above reference.)
34 set(_PYTHON1_VERSIONS 1.6 1.5)
35 set(_PYTHON2_VERSIONS 2.7 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
36 set(_PYTHON3_VERSIONS 3.3 3.2 3.1 3.0)
38 if(PythonInterp_FIND_VERSION)
39 if(PythonInterp_FIND_VERSION MATCHES "^[0-9]+\\.[0-9]+(\\.[0-9]+.*)?$")
40 string(REGEX REPLACE "^([0-9]+\\.[0-9]+).*" "\\1" _PYTHON_FIND_MAJ_MIN "${PythonInterp_FIND_VERSION}")
41 string(REGEX REPLACE "^([0-9]+).*" "\\1" _PYTHON_FIND_MAJ "${_PYTHON_FIND_MAJ_MIN}")
42 list(APPEND _Python_NAMES python${_PYTHON_FIND_MAJ_MIN} python${_PYTHON_FIND_MAJ})
43 unset(_PYTHON_FIND_OTHER_VERSIONS)
44 if(NOT PythonInterp_FIND_VERSION_EXACT)
45 foreach(_PYTHON_V ${_PYTHON${_PYTHON_FIND_MAJ}_VERSIONS})
46 if(NOT _PYTHON_V VERSION_LESS _PYTHON_FIND_MAJ_MIN)
47 list(APPEND _PYTHON_FIND_OTHER_VERSIONS ${_PYTHON_V})
51 unset(_PYTHON_FIND_MAJ_MIN)
52 unset(_PYTHON_FIND_MAJ)
54 list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
55 set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
58 set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
61 list(APPEND _Python_NAMES python)
63 # Search for the current active python version first
64 find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
66 # Set up the versions we know about, in the order we will search. Always add
67 # the user supplied additional versions to the front.
69 ${Python_ADDITIONAL_VERSIONS}
70 ${_PYTHON_FIND_OTHER_VERSIONS}
73 unset(_PYTHON_FIND_OTHER_VERSIONS)
74 unset(_PYTHON1_VERSIONS)
75 unset(_PYTHON2_VERSIONS)
76 unset(_PYTHON3_VERSIONS)
78 # Search for newest python version if python executable isn't found
79 if(NOT PYTHON_EXECUTABLE)
80 foreach(_CURRENT_VERSION ${_Python_VERSIONS})
81 set(_Python_NAMES python${_CURRENT_VERSION})
83 list(APPEND _Python_NAMES python)
85 find_program(PYTHON_EXECUTABLE
86 NAMES ${_Python_NAMES}
87 PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
92 # determine python version string
94 execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
95 "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
96 OUTPUT_VARIABLE _VERSION
97 RESULT_VARIABLE _PYTHON_VERSION_RESULT
99 if(NOT _PYTHON_VERSION_RESULT)
100 string(REPLACE ";" "." PYTHON_VERSION_STRING "${_VERSION}")
101 list(GET _VERSION 0 PYTHON_VERSION_MAJOR)
102 list(GET _VERSION 1 PYTHON_VERSION_MINOR)
103 list(GET _VERSION 2 PYTHON_VERSION_PATCH)
104 if(PYTHON_VERSION_PATCH EQUAL 0)
105 # it's called "Python 2.7", not "2.7.0"
106 string(REGEX REPLACE "\\.0$" "" PYTHON_VERSION_STRING "${PYTHON_VERSION_STRING}")
109 # sys.version predates sys.version_info, so use that
110 execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import sys; sys.stdout.write(sys.version)"
111 OUTPUT_VARIABLE _VERSION
112 RESULT_VARIABLE _PYTHON_VERSION_RESULT
114 if(NOT _PYTHON_VERSION_RESULT)
115 string(REGEX REPLACE " .*" "" PYTHON_VERSION_STRING "${_VERSION}")
116 string(REGEX REPLACE "^([0-9]+)\\.[0-9]+.*" "\\1" PYTHON_VERSION_MAJOR "${PYTHON_VERSION_STRING}")
117 string(REGEX REPLACE "^[0-9]+\\.([0-9])+.*" "\\1" PYTHON_VERSION_MINOR "${PYTHON_VERSION_STRING}")
118 if(PYTHON_VERSION_STRING MATCHES "^[0-9]+\\.[0-9]+\\.[0-9]+.*")
119 string(REGEX REPLACE "^[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" PYTHON_VERSION_PATCH "${PYTHON_VERSION_STRING}")
121 set(PYTHON_VERSION_PATCH "0")
124 # sys.version was first documented for Python 1.5, so assume
126 set(PYTHON_VERSION_STRING "1.4")
127 set(PYTHON_VERSION_MAJOR "1")
128 set(PYTHON_VERSION_MAJOR "4")
129 set(PYTHON_VERSION_MAJOR "0")
132 unset(_PYTHON_VERSION_RESULT)
136 # handle the QUIETLY and REQUIRED arguments and set PYTHONINTERP_FOUND to TRUE if
137 # all listed variables are TRUE
138 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
139 FIND_PACKAGE_HANDLE_STANDARD_ARGS(PythonInterp REQUIRED_VARS PYTHON_EXECUTABLE VERSION_VAR PYTHON_VERSION_STRING)
141 mark_as_advanced(PYTHON_EXECUTABLE)