Merge pull request #2016 from pemmanuelviel:kmeansppSquareDist
[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 2.6 2.5 2.4 2.3 2.2 2.1 2.0)
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.0)
16
17 unset(PYTHON_USE_NUMPY CACHE)
18 unset(HAVE_SPHINX CACHE)
19 if(PYTHON_EXECUTABLE)
20   if(PYTHON_VERSION_STRING)
21     set(PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
22     set(PYTHON_VERSION_FULL "${PYTHON_VERSION_STRING}")
23   else()
24     execute_process(COMMAND ${PYTHON_EXECUTABLE} --version
25       ERROR_VARIABLE PYTHON_VERSION_FULL
26       ERROR_STRIP_TRAILING_WHITESPACE)
27
28     string(REGEX MATCH "[0-9]+.[0-9]+" PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_FULL}")
29   endif()
30
31   if("${PYTHON_VERSION_FULL}" MATCHES "[0-9]+.[0-9]+.[0-9]+")
32     set(PYTHON_VERSION_FULL "${CMAKE_MATCH_0}")
33   elseif("${PYTHON_VERSION_FULL}" MATCHES "[0-9]+.[0-9]+")
34     set(PYTHON_VERSION_FULL "${CMAKE_MATCH_0}")
35   else()
36     unset(PYTHON_VERSION_FULL)
37   endif()
38
39   if(NOT ANDROID AND NOT IOS)
40     ocv_check_environment_variables(PYTHON_LIBRARY PYTHON_INCLUDE_DIR)
41     if(CMAKE_VERSION VERSION_GREATER 2.8.8 AND PYTHON_VERSION_FULL)
42       find_host_package(PythonLibs ${PYTHON_VERSION_FULL} EXACT)
43     else()
44       find_host_package(PythonLibs ${PYTHON_VERSION_FULL})
45     endif()
46     # cmake 2.4 (at least on Ubuntu 8.04 (hardy)) don't define PYTHONLIBS_FOUND
47     if(NOT PYTHONLIBS_FOUND AND PYTHON_INCLUDE_PATH)
48       set(PYTHONLIBS_FOUND ON)
49     endif()
50   endif()
51
52   if(NOT ANDROID AND NOT IOS)
53     if(CMAKE_HOST_UNIX)
54       execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print get_python_lib()"
55                       RESULT_VARIABLE PYTHON_CVPY_PROCESS
56                       OUTPUT_VARIABLE PYTHON_STD_PACKAGES_PATH
57                       OUTPUT_STRIP_TRAILING_WHITESPACE)
58       if("${PYTHON_STD_PACKAGES_PATH}" MATCHES "site-packages")
59         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/site-packages")
60       else() #debian based assumed, install to the dist-packages.
61         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/dist-packages")
62       endif()
63       if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${PYTHON_PACKAGES_PATH}")
64         set(_PYTHON_PACKAGES_PATH "lib${LIB_SUFFIX}/${_PYTHON_PACKAGES_PATH}")
65       else()
66         set(_PYTHON_PACKAGES_PATH "lib/${_PYTHON_PACKAGES_PATH}")
67       endif()
68     elseif(CMAKE_HOST_WIN32)
69       get_filename_component(PYTHON_PATH "${PYTHON_EXECUTABLE}" PATH)
70       file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
71       if(NOT EXISTS "${PYTHON_PATH}/Lib/site-packages")
72         unset(PYTHON_PATH)
73         get_filename_component(PYTHON_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
74         if(NOT PYTHON_PATH)
75            get_filename_component(PYTHON_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
76         endif()
77         file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
78       endif()
79       set(_PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages")
80     endif()
81     SET(PYTHON_PACKAGES_PATH "${_PYTHON_PACKAGES_PATH}" CACHE PATH "Where to install the python packages.")
82
83     if(NOT PYTHON_NUMPY_INCLUDE_DIR)
84       if(CMAKE_CROSSCOMPILING)
85         message(STATUS "Cannot probe for Python/Numpy support (because we are cross-compiling OpenCV)")
86         message(STATUS "If you want to enable Python/Numpy support, set the following variables:")
87         message(STATUS "  PYTHON_INCLUDE_PATH")
88         message(STATUS "  PYTHON_LIBRARIES")
89         message(STATUS "  PYTHON_NUMPY_INCLUDE_DIR")
90       else()
91         # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
92         execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print numpy.distutils.misc_util.get_numpy_include_dirs()[0]"
93                         RESULT_VARIABLE PYTHON_NUMPY_PROCESS
94                         OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIR
95                         OUTPUT_STRIP_TRAILING_WHITESPACE)
96
97         if(NOT PYTHON_NUMPY_PROCESS EQUAL 0)
98           unset(PYTHON_NUMPY_INCLUDE_DIR)
99         endif()
100       endif()
101     endif()
102
103     if(PYTHON_NUMPY_INCLUDE_DIR)
104       file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIR}" _PYTHON_NUMPY_INCLUDE_DIR)
105       set(PYTHON_NUMPY_INCLUDE_DIR ${_PYTHON_NUMPY_INCLUDE_DIR} CACHE PATH "Path to numpy headers")
106       set(PYTHON_USE_NUMPY TRUE)
107       if(CMAKE_CROSSCOMPILING)
108         if(NOT PYTHON_NUMPY_VERSION)
109           set(PYTHON_NUMPY_VERSION "undefined - cannot be probed because of the cross-compilation")
110         endif()
111       else()
112         execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print numpy.version.version"
113                         RESULT_VARIABLE PYTHON_NUMPY_PROCESS
114                         OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
115                         OUTPUT_STRIP_TRAILING_WHITESPACE)
116       endif()
117     endif()
118   endif(NOT ANDROID AND NOT IOS)
119
120   if(BUILD_DOCS)
121     find_host_program(SPHINX_BUILD sphinx-build)
122     if(SPHINX_BUILD)
123         execute_process(COMMAND "${SPHINX_BUILD}"
124                         OUTPUT_QUIET
125                         ERROR_VARIABLE SPHINX_OUTPUT
126                         OUTPUT_STRIP_TRAILING_WHITESPACE)
127         if(SPHINX_OUTPUT MATCHES "Sphinx v([0-9][^ \n]*)")
128           set(SPHINX_VERSION "${CMAKE_MATCH_1}")
129           set(HAVE_SPHINX 1)
130           message(STATUS "Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")
131         endif()
132     endif()
133   endif(BUILD_DOCS)
134 endif(PYTHON_EXECUTABLE)