Imported Upstream version 2.8.10.2
[platform/upstream/cmake.git] / Modules / FindPythonInterp.cmake
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 #=============================================================================
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>
21 #
22 # Distributed under the OSI-approved BSD License (the "License");
23 # see accompanying file Copyright.txt for details.
24 #
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.)
31
32 unset(_Python_NAMES)
33
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)
37
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})
48                 endif()
49              endforeach()
50         endif()
51         unset(_PYTHON_FIND_MAJ_MIN)
52         unset(_PYTHON_FIND_MAJ)
53     else()
54         list(APPEND _Python_NAMES python${PythonInterp_FIND_VERSION})
55         set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON${PythonInterp_FIND_VERSION}_VERSIONS})
56     endif()
57 else()
58     set(_PYTHON_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS} ${_PYTHON2_VERSIONS} ${_PYTHON1_VERSIONS})
59 endif()
60
61 list(APPEND _Python_NAMES python)
62
63 # Search for the current active python version first
64 find_program(PYTHON_EXECUTABLE NAMES ${_Python_NAMES})
65
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.
68 set(_Python_VERSIONS
69   ${Python_ADDITIONAL_VERSIONS}
70   ${_PYTHON_FIND_OTHER_VERSIONS}
71   )
72
73 unset(_PYTHON_FIND_OTHER_VERSIONS)
74 unset(_PYTHON1_VERSIONS)
75 unset(_PYTHON2_VERSIONS)
76 unset(_PYTHON3_VERSIONS)
77
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})
82       if(WIN32)
83         list(APPEND _Python_NAMES python)
84       endif()
85       find_program(PYTHON_EXECUTABLE
86         NAMES ${_Python_NAMES}
87         PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
88         )
89     endforeach()
90 endif()
91
92 # determine python version string
93 if(PYTHON_EXECUTABLE)
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
98                     ERROR_QUIET)
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}")
107         endif()
108     else()
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
113                         ERROR_QUIET)
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}")
120             else()
121                 set(PYTHON_VERSION_PATCH "0")
122             endif()
123         else()
124             # sys.version was first documented for Python 1.5, so assume
125             # this is older.
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")
130         endif()
131     endif()
132     unset(_PYTHON_VERSION_RESULT)
133     unset(_VERSION)
134 endif()
135
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)
140
141 mark_as_advanced(PYTHON_EXECUTABLE)