Merge pull request #11882 from alalek:videoio_vfw_lower_priority
[platform/upstream/opencv.git] / cmake / OpenCVDetectPython.cmake
1 # Find specified Python version
2 # Arguments:
3 #   preferred_version (value): Version to check for first
4 #   min_version (value): Minimum supported version
5 #   library_env (value): Name of Python library ENV variable to check
6 #   include_dir_env (value): Name of Python include directory ENV variable to check
7 #   found (variable): Set if interpreter found
8 #   executable (variable): Output of executable found
9 #   version_string (variable): Output of found version
10 #   version_major (variable): Output of found major version
11 #   version_minor (variable): Output of found minor version
12 #   libs_found (variable): Set if libs found
13 #   libs_version_string (variable): Output of found libs version
14 #   libraries (variable): Output of found Python libraries
15 #   library (variable): Output of found Python library
16 #   debug_libraries (variable): Output of found Python debug libraries
17 #   debug_library (variable): Output of found Python debug library
18 #   include_path (variable): Output of found Python include path
19 #   include_dir (variable): Output of found Python include dir
20 #   include_dir2 (variable): Output of found Python include dir2
21 #   packages_path (variable): Output of found Python packages path
22 #   numpy_include_dirs (variable): Output of found Python Numpy include dirs
23 #   numpy_version (variable): Output of found Python Numpy version
24 function(find_python preferred_version min_version library_env include_dir_env
25          found executable version_string version_major version_minor
26          libs_found libs_version_string libraries library debug_libraries
27          debug_library include_path include_dir include_dir2 packages_path
28          numpy_include_dirs numpy_version)
29 if(NOT ${found})
30   if(" ${executable}" STREQUAL " PYTHON_EXECUTABLE")
31     set(__update_python_vars 0)
32   else()
33     set(__update_python_vars 1)
34   endif()
35
36   ocv_check_environment_variables(${executable})
37   if(${executable})
38     set(PYTHON_EXECUTABLE "${${executable}}")
39   endif()
40
41   if(WIN32 AND NOT ${executable})
42     # search for executable with the same bitness as resulting binaries
43     # standard FindPythonInterp always prefers executable from system path
44     # this is really important because we are using the interpreter for numpy search and for choosing the install location
45     foreach(_CURRENT_VERSION ${Python_ADDITIONAL_VERSIONS} "${preferred_version}" "${min_version}")
46       find_host_program(PYTHON_EXECUTABLE
47         NAMES python${_CURRENT_VERSION} python
48         PATHS
49           [HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
50           [HKEY_CURRENT_USER\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
51         NO_SYSTEM_ENVIRONMENT_PATH
52       )
53     endforeach()
54   endif()
55
56   string(REGEX MATCH "^[0-9]+" _preferred_version_major "${preferred_version}")
57
58   find_host_package(PythonInterp "${preferred_version}")
59   if(NOT PYTHONINTERP_FOUND)
60     find_host_package(PythonInterp "${min_version}")
61   endif()
62
63   if(PYTHONINTERP_FOUND)
64     # Check if python major version is correct
65     if("${_preferred_version_major}" STREQUAL "" OR "${_preferred_version_major}" STREQUAL "${PYTHON_VERSION_MAJOR}")
66       # Copy outputs
67       set(_found ${PYTHONINTERP_FOUND})
68       set(_executable ${PYTHON_EXECUTABLE})
69       set(_version_string ${PYTHON_VERSION_STRING})
70       set(_version_major ${PYTHON_VERSION_MAJOR})
71       set(_version_minor ${PYTHON_VERSION_MINOR})
72       set(_version_patch ${PYTHON_VERSION_PATCH})
73     endif()
74   endif()
75
76   if(__update_python_vars)
77     # Clear find_host_package side effects
78     unset(PYTHONINTERP_FOUND)
79     unset(PYTHON_EXECUTABLE CACHE)
80     unset(PYTHON_VERSION_STRING)
81     unset(PYTHON_VERSION_MAJOR)
82     unset(PYTHON_VERSION_MINOR)
83     unset(PYTHON_VERSION_PATCH)
84   endif()
85
86   if(_found)
87     set(_version_major_minor "${_version_major}.${_version_minor}")
88
89     if(NOT ANDROID AND NOT APPLE_FRAMEWORK)
90       ocv_check_environment_variables(${library_env} ${include_dir_env})
91       if(NOT ${${library_env}} STREQUAL "")
92           set(PYTHON_LIBRARY "${${library_env}}")
93       endif()
94       if(NOT ${${include_dir_env}} STREQUAL "")
95           set(PYTHON_INCLUDE_DIR "${${include_dir_env}}")
96       endif()
97
98       # not using _version_string here, because it might not conform to the CMake version format
99       if(CMAKE_CROSSCOMPILING)
100         # builder version can differ from target, matching base version (e.g. 2.7)
101         find_package(PythonLibs "${_version_major_minor}")
102       else()
103         find_package(PythonLibs "${_version_major_minor}.${_version_patch}" EXACT)
104       endif()
105
106       if(PYTHONLIBS_FOUND)
107         # Copy outputs
108         set(_libs_found ${PYTHONLIBS_FOUND})
109         set(_libraries ${PYTHON_LIBRARIES})
110         set(_include_path ${PYTHON_INCLUDE_PATH})
111         set(_include_dirs ${PYTHON_INCLUDE_DIRS})
112         set(_debug_libraries ${PYTHON_DEBUG_LIBRARIES})
113         set(_libs_version_string ${PYTHONLIBS_VERSION_STRING})
114         set(_debug_library ${PYTHON_DEBUG_LIBRARY})
115         set(_library ${PYTHON_LIBRARY})
116         set(_library_debug ${PYTHON_LIBRARY_DEBUG})
117         set(_library_release ${PYTHON_LIBRARY_RELEASE})
118         set(_include_dir ${PYTHON_INCLUDE_DIR})
119         set(_include_dir2 ${PYTHON_INCLUDE_DIR2})
120       endif()
121       if(__update_python_vars)
122         # Clear find_package side effects
123         unset(PYTHONLIBS_FOUND)
124         unset(PYTHON_LIBRARIES)
125         unset(PYTHON_INCLUDE_PATH)
126         unset(PYTHON_INCLUDE_DIRS)
127         unset(PYTHON_DEBUG_LIBRARIES)
128         unset(PYTHONLIBS_VERSION_STRING)
129         unset(PYTHON_DEBUG_LIBRARY CACHE)
130         unset(PYTHON_LIBRARY)
131         unset(PYTHON_LIBRARY_DEBUG)
132         unset(PYTHON_LIBRARY_RELEASE)
133         unset(PYTHON_LIBRARY CACHE)
134         unset(PYTHON_LIBRARY_DEBUG CACHE)
135         unset(PYTHON_LIBRARY_RELEASE CACHE)
136         unset(PYTHON_INCLUDE_DIR CACHE)
137         unset(PYTHON_INCLUDE_DIR2 CACHE)
138       endif()
139     endif()
140
141     if(NOT ANDROID AND NOT IOS)
142       if(CMAKE_HOST_UNIX)
143         execute_process(COMMAND ${_executable} -c "from distutils.sysconfig import *; print(get_python_lib())"
144                         RESULT_VARIABLE _cvpy_process
145                         OUTPUT_VARIABLE _std_packages_path
146                         OUTPUT_STRIP_TRAILING_WHITESPACE)
147         if("${_std_packages_path}" MATCHES "site-packages")
148           set(_packages_path "python${_version_major_minor}/site-packages")
149         else() #debian based assumed, install to the dist-packages.
150           set(_packages_path "python${_version_major_minor}/dist-packages")
151         endif()
152         if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${${packages_path}}")
153           set(_packages_path "lib${LIB_SUFFIX}/${_packages_path}")
154         else()
155           set(_packages_path "lib/${_packages_path}")
156         endif()
157       elseif(CMAKE_HOST_WIN32)
158         get_filename_component(_path "${_executable}" PATH)
159         file(TO_CMAKE_PATH "${_path}" _path)
160         if(NOT EXISTS "${_path}/Lib/site-packages")
161           unset(_path)
162           get_filename_component(_path "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_version_major_minor}\\InstallPath]" ABSOLUTE)
163           if(NOT _path)
164              get_filename_component(_path "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_version_major_minor}\\InstallPath]" ABSOLUTE)
165           endif()
166           file(TO_CMAKE_PATH "${_path}" _path)
167         endif()
168         set(_packages_path "${_path}/Lib/site-packages")
169         unset(_path)
170       endif()
171
172       set(_numpy_include_dirs "${${numpy_include_dirs}}")
173
174       if(NOT _numpy_include_dirs)
175         if(CMAKE_CROSSCOMPILING)
176           message(STATUS "Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV)")
177           message(STATUS "If you want to enable Python/Numpy support, set the following variables:")
178           message(STATUS "  PYTHON2_INCLUDE_PATH")
179           message(STATUS "  PYTHON2_LIBRARIES (optional on Unix-like systems)")
180           message(STATUS "  PYTHON2_NUMPY_INCLUDE_DIRS")
181           message(STATUS "  PYTHON3_INCLUDE_PATH")
182           message(STATUS "  PYTHON3_LIBRARIES (optional on Unix-like systems)")
183           message(STATUS "  PYTHON3_NUMPY_INCLUDE_DIRS")
184         else()
185           # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
186           execute_process(COMMAND "${_executable}" -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print(os.pathsep.join(numpy.distutils.misc_util.get_numpy_include_dirs()))"
187                           RESULT_VARIABLE _numpy_process
188                           OUTPUT_VARIABLE _numpy_include_dirs
189                           OUTPUT_STRIP_TRAILING_WHITESPACE)
190
191           if(NOT _numpy_process EQUAL 0)
192               unset(_numpy_include_dirs)
193           endif()
194         endif()
195       endif()
196
197       if(_numpy_include_dirs)
198         file(TO_CMAKE_PATH "${_numpy_include_dirs}" _numpy_include_dirs)
199         if(CMAKE_CROSSCOMPILING)
200           if(NOT _numpy_version)
201             set(_numpy_version "undefined - cannot be probed because of the cross-compilation")
202           endif()
203         else()
204           execute_process(COMMAND "${_executable}" -c "import numpy; print(numpy.version.version)"
205                           RESULT_VARIABLE _numpy_process
206                           OUTPUT_VARIABLE _numpy_version
207                           OUTPUT_STRIP_TRAILING_WHITESPACE)
208         endif()
209       endif()
210     endif(NOT ANDROID AND NOT IOS)
211   endif()
212
213   # Export return values
214   set(${found} "${_found}" CACHE INTERNAL "")
215   set(${executable} "${_executable}" CACHE FILEPATH "Path to Python interpretor")
216   set(${version_string} "${_version_string}" CACHE INTERNAL "")
217   set(${version_major} "${_version_major}" CACHE INTERNAL "")
218   set(${version_minor} "${_version_minor}" CACHE INTERNAL "")
219   set(${libs_found} "${_libs_found}" CACHE INTERNAL "")
220   set(${libs_version_string} "${_libs_version_string}" CACHE INTERNAL "")
221   set(${libraries} "${_libraries}" CACHE INTERNAL "Python libraries")
222   set(${library} "${_library}" CACHE FILEPATH "Path to Python library")
223   set(${debug_libraries} "${_debug_libraries}" CACHE INTERNAL "")
224   set(${debug_library} "${_debug_library}" CACHE FILEPATH "Path to Python debug")
225   set(${include_path} "${_include_path}" CACHE INTERNAL "")
226   set(${include_dir} "${_include_dir}" CACHE PATH "Python include dir")
227   set(${include_dir2} "${_include_dir2}" CACHE PATH "Python include dir 2")
228   set(${packages_path} "${_packages_path}" CACHE PATH "Where to install the python packages.")
229   set(${numpy_include_dirs} ${_numpy_include_dirs} CACHE PATH "Path to numpy headers")
230   set(${numpy_version} "${_numpy_version}" CACHE INTERNAL "")
231 endif()
232 endfunction(find_python)
233
234 if(OPENCV_PYTHON_SKIP_DETECTION)
235   return()
236 endif()
237
238 find_python(2.7 "${MIN_VER_PYTHON2}" PYTHON2_LIBRARY PYTHON2_INCLUDE_DIR
239     PYTHON2INTERP_FOUND PYTHON2_EXECUTABLE PYTHON2_VERSION_STRING
240     PYTHON2_VERSION_MAJOR PYTHON2_VERSION_MINOR PYTHON2LIBS_FOUND
241     PYTHON2LIBS_VERSION_STRING PYTHON2_LIBRARIES PYTHON2_LIBRARY
242     PYTHON2_DEBUG_LIBRARIES PYTHON2_LIBRARY_DEBUG PYTHON2_INCLUDE_PATH
243     PYTHON2_INCLUDE_DIR PYTHON2_INCLUDE_DIR2 PYTHON2_PACKAGES_PATH
244     PYTHON2_NUMPY_INCLUDE_DIRS PYTHON2_NUMPY_VERSION)
245
246 find_python(3.4 "${MIN_VER_PYTHON3}" PYTHON3_LIBRARY PYTHON3_INCLUDE_DIR
247     PYTHON3INTERP_FOUND PYTHON3_EXECUTABLE PYTHON3_VERSION_STRING
248     PYTHON3_VERSION_MAJOR PYTHON3_VERSION_MINOR PYTHON3LIBS_FOUND
249     PYTHON3LIBS_VERSION_STRING PYTHON3_LIBRARIES PYTHON3_LIBRARY
250     PYTHON3_DEBUG_LIBRARIES PYTHON3_LIBRARY_DEBUG PYTHON3_INCLUDE_PATH
251     PYTHON3_INCLUDE_DIR PYTHON3_INCLUDE_DIR2 PYTHON3_PACKAGES_PATH
252     PYTHON3_NUMPY_INCLUDE_DIRS PYTHON3_NUMPY_VERSION)
253
254
255 if(PYTHON_DEFAULT_EXECUTABLE)
256     set(PYTHON_DEFAULT_AVAILABLE "TRUE")
257 elseif(PYTHON2INTERP_FOUND) # Use Python 2 as default Python interpreter
258     set(PYTHON_DEFAULT_AVAILABLE "TRUE")
259     set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
260 elseif(PYTHON3INTERP_FOUND) # Use Python 3 as fallback Python interpreter (if there is no Python 2)
261     set(PYTHON_DEFAULT_AVAILABLE "TRUE")
262     set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON3_EXECUTABLE}")
263 endif()