Merge pull request #2098 from pxli168:patch-1
[profile/ivi/opencv.git] / cmake / OpenCVDetectPython.cmake
1 if(WIN32 AND NOT PYTHON_EXECUTABLE)
2   # search for executable with the same bitness as resulting binaries
3   # standard FindPythonInterp always prefers executable from system path
4   # this is really important because we are using the interpreter for numpy search and for choosing the install location
5   foreach(_CURRENT_VERSION ${Python_ADDITIONAL_VERSIONS} 2.7 "${MIN_VER_PYTHON}")
6     find_host_program(PYTHON_EXECUTABLE
7       NAMES python${_CURRENT_VERSION} python
8       PATHS
9         [HKEY_LOCAL_MACHINE\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
10         [HKEY_CURRENT_USER\\\\SOFTWARE\\\\Python\\\\PythonCore\\\\${_CURRENT_VERSION}\\\\InstallPath]
11       NO_SYSTEM_ENVIRONMENT_PATH
12     )
13   endforeach()
14 endif()
15 find_host_package(PythonInterp 2.7)
16 if(NOT PYTHONINTERP_FOUND)
17 find_host_package(PythonInterp "${MIN_VER_PYTHON}")
18 endif()
19
20 unset(HAVE_SPHINX CACHE)
21
22 if(PYTHONINTERP_FOUND)
23   set(PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
24
25   if(NOT ANDROID AND NOT IOS)
26     ocv_check_environment_variables(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
27     # not using PYTHON_VERSION_STRING here, because it might not conform to the CMake version format
28     find_host_package(PythonLibs "${PYTHON_VERSION_MAJOR_MINOR}.${PYTHON_VERSION_PATCH}" EXACT)
29   endif()
30
31   if(NOT ANDROID AND NOT IOS)
32     if(CMAKE_HOST_UNIX)
33       execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print(get_python_lib())"
34                       RESULT_VARIABLE PYTHON_CVPY_PROCESS
35                       OUTPUT_VARIABLE PYTHON_STD_PACKAGES_PATH
36                       OUTPUT_STRIP_TRAILING_WHITESPACE)
37       if("${PYTHON_STD_PACKAGES_PATH}" MATCHES "site-packages")
38         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/site-packages")
39       else() #debian based assumed, install to the dist-packages.
40         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/dist-packages")
41       endif()
42       if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${PYTHON_PACKAGES_PATH}")
43         set(_PYTHON_PACKAGES_PATH "lib${LIB_SUFFIX}/${_PYTHON_PACKAGES_PATH}")
44       else()
45         set(_PYTHON_PACKAGES_PATH "lib/${_PYTHON_PACKAGES_PATH}")
46       endif()
47     elseif(CMAKE_HOST_WIN32)
48       get_filename_component(PYTHON_PATH "${PYTHON_EXECUTABLE}" PATH)
49       file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
50       if(NOT EXISTS "${PYTHON_PATH}/Lib/site-packages")
51         unset(PYTHON_PATH)
52         get_filename_component(PYTHON_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
53         if(NOT PYTHON_PATH)
54            get_filename_component(PYTHON_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
55         endif()
56         file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
57       endif()
58       set(_PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages")
59     endif()
60     SET(PYTHON_PACKAGES_PATH "${_PYTHON_PACKAGES_PATH}" CACHE PATH "Where to install the python packages.")
61
62     if(NOT PYTHON_NUMPY_INCLUDE_DIRS)
63       if(CMAKE_CROSSCOMPILING)
64         message(STATUS "Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV)")
65         message(STATUS "If you want to enable Python/Numpy support, set the following variables:")
66         message(STATUS "  PYTHON_INCLUDE_PATH")
67         message(STATUS "  PYTHON_LIBRARIES")
68         message(STATUS "  PYTHON_NUMPY_INCLUDE_DIRS")
69       else()
70         # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
71         execute_process(COMMAND "${PYTHON_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()))"
72                         RESULT_VARIABLE PYTHON_NUMPY_PROCESS
73                         OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIRS
74                         OUTPUT_STRIP_TRAILING_WHITESPACE)
75
76         if(NOT PYTHON_NUMPY_PROCESS EQUAL 0)
77           unset(PYTHON_NUMPY_INCLUDE_DIRS)
78         endif()
79       endif()
80     endif()
81
82     if(PYTHON_NUMPY_INCLUDE_DIRS)
83       file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIRS}" _PYTHON_NUMPY_INCLUDE_DIRS)
84       set(PYTHON_NUMPY_INCLUDE_DIRS ${_PYTHON_NUMPY_INCLUDE_DIRS} CACHE PATH "Path to numpy headers")
85       if(CMAKE_CROSSCOMPILING)
86         if(NOT PYTHON_NUMPY_VERSION)
87           set(PYTHON_NUMPY_VERSION "undefined - cannot be probed because of the cross-compilation")
88         endif()
89       else()
90         execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import numpy; print(numpy.version.version)"
91                         RESULT_VARIABLE PYTHON_NUMPY_PROCESS
92                         OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
93                         OUTPUT_STRIP_TRAILING_WHITESPACE)
94       endif()
95     endif()
96   endif(NOT ANDROID AND NOT IOS)
97 endif()
98
99 if(BUILD_DOCS)
100   find_host_program(SPHINX_BUILD sphinx-build)
101   find_host_program(PLANTUML plantuml)
102   if(SPHINX_BUILD)
103       execute_process(COMMAND "${SPHINX_BUILD}"
104                       OUTPUT_QUIET
105                       ERROR_VARIABLE SPHINX_OUTPUT
106                       OUTPUT_STRIP_TRAILING_WHITESPACE)
107       if(SPHINX_OUTPUT MATCHES "Sphinx v([0-9][^ \n]*)")
108         set(SPHINX_VERSION "${CMAKE_MATCH_1}")
109         set(HAVE_SPHINX 1)
110         message(STATUS "Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")
111       endif()
112   endif()
113 endif(BUILD_DOCS)