1 # Find specified Python version
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(WIN32 AND NOT ${executable})
30 # search for executable with the same bitness as resulting binaries
31 # standard FindPythonInterp always prefers executable from system path
32 # this is really important because we are using the interpreter for numpy search and for choosing the install location
33 foreach(_CURRENT_VERSION ${Python_ADDITIONAL_VERSIONS} "${preferred_version}" "${min_version}")
34 find_host_program(executable
35 NAMES python${_CURRENT_VERSION} python
37 [HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
38 [HKEY_CURRENT_USER\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
39 NO_SYSTEM_ENVIRONMENT_PATH
44 find_host_package(PythonInterp "${preferred_version}")
45 if(NOT PYTHONINTERP_FOUND)
46 find_host_package(PythonInterp "${min_version}")
49 if(PYTHONINTERP_FOUND)
51 set(_found ${PYTHONINTERP_FOUND})
52 set(_executable ${PYTHON_EXECUTABLE})
53 set(_version_string ${PYTHON_VERSION_STRING})
54 set(_version_major ${PYTHON_VERSION_MAJOR})
55 set(_version_minor ${PYTHON_VERSION_MINOR})
56 set(_version_patch ${PYTHON_VERSION_PATCH})
58 # Clear find_host_package side effects
59 unset(PYTHONINTERP_FOUND)
60 unset(PYTHON_EXECUTABLE CACHE)
61 unset(PYTHON_VERSION_STRING)
62 unset(PYTHON_VERSION_MAJOR)
63 unset(PYTHON_VERSION_MINOR)
64 unset(PYTHON_VERSION_PATCH)
68 set(_version_major_minor "${_version_major}.${_version_minor}")
70 if(NOT ANDROID AND NOT IOS)
71 ocv_check_environment_variables(${library_env} ${include_dir_env})
73 set(PYTHON_LIBRARY "${${library_env}}")
76 set(PYTHON_INCLUDE_DIR "${${include_dir_env}}")
79 # not using _version_string here, because it might not conform to the CMake version format
80 find_host_package(PythonLibs "${_version_major_minor}.${_version_patch}" EXACT)
84 set(_libs_found ${PYTHONLIBS_FOUND})
85 set(_libraries ${PYTHON_LIBRARIES})
86 set(_include_path ${PYTHON_INCLUDE_PATH})
87 set(_include_dirs ${PYTHON_INCLUDE_DIRS})
88 set(_debug_libraries ${PYTHON_DEBUG_LIBRARIES})
89 set(_libs_version_string ${PYTHONLIBS_VERSION_STRING})
90 set(_debug_library ${PYTHON_DEBUG_LIBRARY})
91 set(_library ${PYTHON_LIBRARY})
92 set(_library_debug ${PYTHON_LIBRARY_DEBUG})
93 set(_library_release ${PYTHON_LIBRARY_RELEASE})
94 set(_include_dir ${PYTHON_INCLUDE_DIR})
95 set(_include_dir2 ${PYTHON_INCLUDE_DIR2})
97 # Clear find_host_package side effects
98 unset(PYTHONLIBS_FOUND)
99 unset(PYTHON_LIBRARIES)
100 unset(PYTHON_INCLUDE_PATH)
101 unset(PYTHON_INCLUDE_DIRS)
102 unset(PYTHON_DEBUG_LIBRARIES)
103 unset(PYTHONLIBS_VERSION_STRING)
104 unset(PYTHON_DEBUG_LIBRARY CACHE)
105 unset(PYTHON_LIBRARY)
106 unset(PYTHON_LIBRARY_DEBUG)
107 unset(PYTHON_LIBRARY_RELEASE)
108 unset(PYTHON_LIBRARY CACHE)
109 unset(PYTHON_LIBRARY_DEBUG CACHE)
110 unset(PYTHON_LIBRARY_RELEASE CACHE)
111 unset(PYTHON_INCLUDE_DIR CACHE)
112 unset(PYTHON_INCLUDE_DIR2 CACHE)
116 if(NOT ANDROID AND NOT IOS)
118 execute_process(COMMAND ${_executable} -c "from distutils.sysconfig import *; print(get_python_lib())"
119 RESULT_VARIABLE _cvpy_process
120 OUTPUT_VARIABLE _std_packages_path
121 OUTPUT_STRIP_TRAILING_WHITESPACE)
122 if("${_std_packages_path}" MATCHES "site-packages")
123 set(_packages_path "python${_version_major_minor}/site-packages")
124 else() #debian based assumed, install to the dist-packages.
125 set(_packages_path "python${_version_major_minor}/dist-packages")
127 if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${${packages_path}}")
128 set(_packages_path "lib${LIB_SUFFIX}/${_packages_path}")
130 set(_packages_path "lib/${_packages_path}")
132 elseif(CMAKE_HOST_WIN32)
133 get_filename_component(_path "${_executable}" PATH)
134 file(TO_CMAKE_PATH "${_path}" _path)
135 if(NOT EXISTS "${_path}/Lib/site-packages")
137 get_filename_component(_path "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${_version_major_minor}\\InstallPath]" ABSOLUTE)
139 get_filename_component(_path "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${_version_major_minor}\\InstallPath]" ABSOLUTE)
141 file(TO_CMAKE_PATH "${_path}" _path)
143 set(_packages_path "${_path}/Lib/site-packages")
147 set(_numpy_include_dirs ${${numpy_include_dirs}})
149 if(NOT _numpy_include_dirs)
150 if(CMAKE_CROSSCOMPILING)
151 message(STATUS "Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV)")
152 message(STATUS "If you want to enable Python/Numpy support, set the following variables:")
153 message(STATUS " PYTHON2_INCLUDE_PATH")
154 message(STATUS " PYTHON2_LIBRARIES")
155 message(STATUS " PYTHON2_NUMPY_INCLUDE_DIRS")
156 message(STATUS " PYTHON3_INCLUDE_PATH")
157 message(STATUS " PYTHON3_LIBRARIES")
158 message(STATUS " PYTHON3_NUMPY_INCLUDE_DIRS")
160 # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
161 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()))"
162 RESULT_VARIABLE _numpy_process
163 OUTPUT_VARIABLE _numpy_include_dirs
164 OUTPUT_STRIP_TRAILING_WHITESPACE)
166 if(NOT _numpy_process EQUAL 0)
167 unset(_numpy_include_dirs)
172 if(_numpy_include_dirs)
173 file(TO_CMAKE_PATH "${_numpy_include_dirs}" _numpy_include_dirs)
174 if(CMAKE_CROSSCOMPILING)
175 if(NOT _numpy_version)
176 set(_numpy_version "undefined - cannot be probed because of the cross-compilation")
179 execute_process(COMMAND "${_executable}" -c "import numpy; print(numpy.version.version)"
180 RESULT_VARIABLE _numpy_process
181 OUTPUT_VARIABLE _numpy_version
182 OUTPUT_STRIP_TRAILING_WHITESPACE)
185 endif(NOT ANDROID AND NOT IOS)
188 # Export return values
189 set(${found} "${_found}" PARENT_SCOPE)
190 set(${executable} "${_executable}" CACHE FILEPATH "Path to Python interpretor")
191 set(${version_string} "${_version_string}" PARENT_SCOPE)
192 set(${version_major} "${_version_major}" PARENT_SCOPE)
193 set(${version_minor} "${_version_minor}" PARENT_SCOPE)
194 set(${libs_found} "${_libs_found}" PARENT_SCOPE)
195 set(${libs_version_string} "${_libs_version_string}" PARENT_SCOPE)
196 set(${libraries} "${_libraries}" PARENT_SCOPE)
197 set(${library} "${_library}" CACHE FILEPATH "Path to Python library")
198 set(${debug_libraries} "${_debug_libraries}" PARENT_SCOPE)
199 set(${debug_library} "${_debug_library}" CACHE FILEPATH "Path to Python debug")
200 set(${include_path} "${_include_path}" PARENT_SCOPE)
201 set(${include_dir} "${_include_dir}" CACHE PATH "Python include dir")
202 set(${include_dir2} "${_include_dir2}" CACHE PATH "Python include dir 2")
203 set(${packages_path} "${_packages_path}" CACHE PATH "Where to install the python packages.")
204 set(${numpy_include_dirs} ${_numpy_include_dirs} CACHE PATH "Path to numpy headers")
205 set(${numpy_version} "${_numpy_version}" PARENT_SCOPE)
206 endfunction(find_python)
208 find_python(2.7 "${MIN_VER_PYTHON2}" PYTHON2_LIBRARY PYTHON2_INCLUDE_DIR
209 PYTHON2INTERP_FOUND PYTHON2_EXECUTABLE PYTHON2_VERSION_STRING
210 PYTHON2_VERSION_MAJOR PYTHON2_VERSION_MINOR PYTHON2LIBS_FOUND
211 PYTHON2LIBS_VERSION_STRING PYTHON2_LIBRARIES PYTHON2_LIBRARY
212 PYTHON2_DEBUG_LIBRARIES PYTHON2_LIBRARY_DEBUG PYTHON2_INCLUDE_PATH
213 PYTHON2_INCLUDE_DIR PYTHON2_INCLUDE_DIR2 PYTHON2_PACKAGES_PATH
214 PYTHON2_NUMPY_INCLUDE_DIRS PYTHON2_NUMPY_VERSION)
216 find_python(3.4 "${MIN_VER_PYTHON3}" PYTHON3_LIBRARY PYTHON3_INCLUDE_DIR
217 PYTHON3INTERP_FOUND PYTHON3_EXECUTABLE PYTHON3_VERSION_STRING
218 PYTHON3_VERSION_MAJOR PYTHON3_VERSION_MINOR PYTHON3LIBS_FOUND
219 PYTHON3LIBS_VERSION_STRING PYTHON3_LIBRARIES PYTHON3_LIBRARY
220 PYTHON3_DEBUG_LIBRARIES PYTHON3_LIBRARY_DEBUG PYTHON3_INCLUDE_PATH
221 PYTHON3_INCLUDE_DIR PYTHON3_INCLUDE_DIR2 PYTHON3_PACKAGES_PATH
222 PYTHON3_NUMPY_INCLUDE_DIRS PYTHON3_NUMPY_VERSION)
224 # Use Python 2 as default Python interpreter
225 if(PYTHON2LIBS_FOUND)
226 set(PYTHON_DEFAULT_AVAILABLE "TRUE")
227 set(PYTHON_DEFAULT_EXECUTABLE "${PYTHON2_EXECUTABLE}")
230 unset(HAVE_SPHINX CACHE)
233 find_host_program(SPHINX_BUILD sphinx-build)
234 find_host_program(PLANTUML plantuml)
236 execute_process(COMMAND "${SPHINX_BUILD}"
238 ERROR_VARIABLE SPHINX_OUTPUT
239 OUTPUT_STRIP_TRAILING_WHITESPACE)
240 if(SPHINX_OUTPUT MATCHES "Sphinx v([0-9][^ \n]*)")
241 set(SPHINX_VERSION "${CMAKE_MATCH_1}")
243 message(STATUS "Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")