Merge remote-tracking branch 'origin/2.4' into merge-2.4
[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(HAVE_SPHINX CACHE)
18 if(PYTHON_EXECUTABLE)
19   if(PYTHON_VERSION_STRING)
20     set(PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
21     set(PYTHON_VERSION_FULL "${PYTHON_VERSION_STRING}")
22   else()
23     execute_process(COMMAND ${PYTHON_EXECUTABLE} --version
24       ERROR_VARIABLE PYTHON_VERSION_FULL
25       ERROR_STRIP_TRAILING_WHITESPACE)
26
27     string(REGEX MATCH "[0-9]+.[0-9]+" PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_FULL}")
28   endif()
29
30   if("${PYTHON_VERSION_FULL}" MATCHES "[0-9]+.[0-9]+.[0-9]+")
31     set(PYTHON_VERSION_FULL "${CMAKE_MATCH_0}")
32   elseif("${PYTHON_VERSION_FULL}" MATCHES "[0-9]+.[0-9]+")
33     set(PYTHON_VERSION_FULL "${CMAKE_MATCH_0}")
34   else()
35     unset(PYTHON_VERSION_FULL)
36   endif()
37
38   if(NOT ANDROID AND NOT IOS)
39     if(CMAKE_VERSION VERSION_GREATER 2.8.8 AND PYTHON_VERSION_FULL)
40       find_host_package(PythonLibs ${PYTHON_VERSION_FULL} EXACT)
41     else()
42       find_host_package(PythonLibs ${PYTHON_VERSION_FULL})
43     endif()
44     # cmake 2.4 (at least on Ubuntu 8.04 (hardy)) don't define PYTHONLIBS_FOUND
45     if(NOT PYTHONLIBS_FOUND AND PYTHON_INCLUDE_PATH)
46       set(PYTHONLIBS_FOUND ON)
47     endif()
48   endif()
49
50   if(NOT ANDROID AND NOT IOS)
51     if(CMAKE_HOST_UNIX)
52       execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print(get_python_lib())"
53                       RESULT_VARIABLE PYTHON_CVPY_PROCESS
54                       OUTPUT_VARIABLE PYTHON_STD_PACKAGES_PATH
55                       OUTPUT_STRIP_TRAILING_WHITESPACE)
56       if("${PYTHON_STD_PACKAGES_PATH}" MATCHES "site-packages")
57         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/site-packages")
58       else() #debian based assumed, install to the dist-packages.
59         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/dist-packages")
60       endif()
61       if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${PYTHON_PACKAGES_PATH}")
62         set(_PYTHON_PACKAGES_PATH "lib${LIB_SUFFIX}/${_PYTHON_PACKAGES_PATH}")
63       else()
64         set(_PYTHON_PACKAGES_PATH "lib/${_PYTHON_PACKAGES_PATH}")
65       endif()
66     elseif(CMAKE_HOST_WIN32)
67       get_filename_component(PYTHON_PATH "${PYTHON_EXECUTABLE}" PATH)
68       file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
69       if(NOT EXISTS "${PYTHON_PATH}/Lib/site-packages")
70         unset(PYTHON_PATH)
71         get_filename_component(PYTHON_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
72         if(NOT PYTHON_PATH)
73            get_filename_component(PYTHON_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
74         endif()
75         file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
76       endif()
77       set(_PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages")
78     endif()
79     SET(PYTHON_PACKAGES_PATH "${_PYTHON_PACKAGES_PATH}" CACHE PATH "Where to install the python packages.")
80
81     if(NOT PYTHON_NUMPY_INCLUDE_DIR)
82       # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
83       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])"
84                       RESULT_VARIABLE PYTHON_NUMPY_PROCESS
85                       OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIR
86                       OUTPUT_STRIP_TRAILING_WHITESPACE)
87
88       if(PYTHON_NUMPY_PROCESS EQUAL 0)
89         file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIR}" _PYTHON_NUMPY_INCLUDE_DIR)
90         set(PYTHON_NUMPY_INCLUDE_DIR ${_PYTHON_NUMPY_INCLUDE_DIR} CACHE PATH "Path to numpy headers")
91       endif()
92     endif()
93
94     if(PYTHON_NUMPY_INCLUDE_DIR)
95       execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "import numpy; print(numpy.version.version)"
96                         RESULT_VARIABLE PYTHON_NUMPY_PROCESS
97                         OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
98                         OUTPUT_STRIP_TRAILING_WHITESPACE)
99     endif()
100   endif(NOT ANDROID AND NOT IOS)
101
102   if(BUILD_DOCS)
103     find_host_program(SPHINX_BUILD sphinx-build)
104     if(SPHINX_BUILD)
105         execute_process(COMMAND "${SPHINX_BUILD}"
106                         OUTPUT_QUIET
107                         ERROR_VARIABLE SPHINX_OUTPUT
108                         OUTPUT_STRIP_TRAILING_WHITESPACE)
109         if(SPHINX_OUTPUT MATCHES "Sphinx v([0-9][^ \n]*)")
110           set(SPHINX_VERSION "${CMAKE_MATCH_1}")
111           set(HAVE_SPHINX 1)
112           message(STATUS "Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")
113         endif()
114     endif()
115   endif(BUILD_DOCS)
116 endif(PYTHON_EXECUTABLE)