Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / cmake / modules / FindPython3Interp.cmake
1 #.rst:
2 # FindPython3Interp
3 # ----------------
4 #
5 # Find python interpreter
6 #
7 # This module finds if Python interpreter is installed and determines
8 # where the executables are.  This code sets the following variables:
9 #
10 # ::
11 #
12 #   PYTHON3INTERP_FOUND         - Was the Python executable found
13 #   PYTHON3_EXECUTABLE          - path to the Python interpreter
14 #
15 #
16 #
17 # ::
18 #
19 #   PYTHON3_VERSION_STRING      - Python version found e.g. 2.5.2
20 #   PYTHON3_VERSION_MAJOR       - Python major version found e.g. 2
21 #   PYTHON3_VERSION_MINOR       - Python minor version found e.g. 5
22 #   PYTHON3_VERSION_PATCH       - Python patch version found e.g. 2
23 #
24 #
25 #
26 # The Python3_ADDITIONAL_VERSIONS variable can be used to specify a list
27 # of version numbers that should be taken into account when searching
28 # for Python.  You need to set this variable before calling
29 # find_package(Python3Interp).
30 #
31 # If calling both ``find_package(Python3Interp)`` and
32 # ``find_package(Python3Libs)``, call ``find_package(Python3Interp)`` first to
33 # get the currently active Python version by default with a consistent version
34 # of PYTHON3_LIBRARIES.
35
36 #=============================================================================
37 # Copyright 2005-2010 Kitware, Inc.
38 # Copyright 2011 Bjoern Ricks <bjoern.ricks@gmail.com>
39 # Copyright 2012 Rolf Eike Beer <eike@sf-mail.de>
40 #
41 # Redistribution and use in source and binary forms, with or without
42 # modification, are permitted provided that the following conditions
43 # are met:
44 #
45 # * Redistributions of source code must retain the above copyright
46 #   notice, this list of conditions and the following disclaimer.
47 #
48 # * Redistributions in binary form must reproduce the above copyright
49 #   notice, this list of conditions and the following disclaimer in the
50 #   documentation and/or other materials provided with the distribution.
51 #
52 # * Neither the names of Kitware, Inc., the Insight Software Consortium,
53 #   nor the names of their contributors may be used to endorse or promote
54 #   products derived from this software without specific prior written
55 #   permission.
56 #
57 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
58 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
59 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
60 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
61 # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
62 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
63 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
64 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
65 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
67 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 #=============================================================================
69
70 unset(_Python3_NAMES)
71
72 set(_PYTHON3_VERSIONS 3.6 3.5 3.4 3.3 3.2 3.1 3.0)
73
74 if(Python3Interp_FIND_VERSION)
75     if(Python3Interp_FIND_VERSION_COUNT GREATER 1)
76         set(_PYTHON3_FIND_MAJ_MIN "${Python3Interp_FIND_VERSION_MAJOR}.${Python3Interp_FIND_VERSION_MINOR}")
77         list(APPEND _Python3_NAMES
78              python${_PYTHON3_FIND_MAJ_MIN}
79              python${Python3Interp_FIND_VERSION_MAJOR})
80         unset(_PYTHON3_FIND_OTHER_VERSIONS)
81         if(NOT Python3Interp_FIND_VERSION_EXACT)
82             foreach(_PYTHON3_V ${_PYTHON${Python3Interp_FIND_VERSION_MAJOR}_VERSIONS})
83                 if(NOT _PYTHON3_V VERSION_LESS _PYTHON3_FIND_MAJ_MIN)
84                     list(APPEND _PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON3_V})
85                 endif()
86              endforeach()
87         endif()
88         unset(_PYTHON3_FIND_MAJ_MIN)
89     else()
90         list(APPEND _Python3_NAMES python${Python3Interp_FIND_VERSION_MAJOR})
91         set(_PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON${Python3Interp_FIND_VERSION_MAJOR}_VERSIONS})
92     endif()
93 else()
94     set(_PYTHON3_FIND_OTHER_VERSIONS ${_PYTHON3_VERSIONS})
95 endif()
96 find_program(PYTHON3_EXECUTABLE NAMES ${_Python3_NAMES})
97
98 # Set up the versions we know about, in the order we will search. Always add
99 # the user supplied additional versions to the front.
100 set(_Python3_VERSIONS ${Python3_ADDITIONAL_VERSIONS})
101 # If FindPython3Interp has already found the major and minor version,
102 # insert that version next to get consistent versions of the interpreter and
103 # library.
104 if(DEFINED PYTHON3LIBS_VERSION_STRING)
105   string(REPLACE "." ";" _PYTHON3LIBS_VERSION "${PYTHON3LIBS_VERSION_STRING}")
106   list(GET _PYTHON3LIBS_VERSION 0 _PYTHON3LIBS_VERSION_MAJOR)
107   list(GET _PYTHON3LIBS_VERSION 1 _PYTHON3LIBS_VERSION_MINOR)
108   list(APPEND _Python3_VERSIONS ${_PYTHON3LIBS_VERSION_MAJOR}.${_PYTHON3LIBS_VERSION_MINOR})
109 endif()
110 # Search for the current active python version first
111 list(APPEND _Python3_VERSIONS ";")
112 list(APPEND _Python3_VERSIONS ${_PYTHON3_FIND_OTHER_VERSIONS})
113
114 unset(_PYTHON3_FIND_OTHER_VERSIONS)
115 unset(_PYTHON3_VERSIONS)
116
117 # Search for newest python version if python executable isn't found
118 if(NOT PYTHON3_EXECUTABLE)
119     foreach(_CURRENT_VERSION IN LISTS _Python3_VERSIONS)
120       set(_Python3_NAMES python${_CURRENT_VERSION})
121       if(WIN32)
122         list(APPEND _Python3_NAMES python)
123       endif()
124       find_program(PYTHON3_EXECUTABLE
125         NAMES ${_Python3_NAMES}
126         PATHS [HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_CURRENT_VERSION}\\InstallPath]
127         )
128     endforeach()
129 endif()
130
131 # determine python version string
132 if(PYTHON3_EXECUTABLE)
133     execute_process(COMMAND "${PYTHON3_EXECUTABLE}" -c
134                             "import sys; sys.stdout.write(';'.join([str(x) for x in sys.version_info[:3]]))"
135                     OUTPUT_VARIABLE _VERSION)
136     string(REPLACE ";" "." PYTHON3_VERSION_STRING "${_VERSION}")
137     list(GET _VERSION 0 PYTHON3_VERSION_MAJOR)
138     list(GET _VERSION 1 PYTHON3_VERSION_MINOR)
139     list(GET _VERSION 2 PYTHON3_VERSION_PATCH)
140     unset(_VERSION)
141 endif()
142
143 # handle the QUIETLY and REQUIRED arguments and set PYTHON3INTERP_FOUND to TRUE if
144 # all listed variables are TRUE
145 include(FindPackageHandleStandardArgs)
146 FIND_PACKAGE_HANDLE_STANDARD_ARGS(Python3Interp REQUIRED_VARS PYTHON3_EXECUTABLE VERSION_VAR PYTHON3_VERSION_STRING)
147
148 mark_as_advanced(PYTHON3_EXECUTABLE)