Merge pull request #1299 from jet47:gpu-cuda-rename
[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 "${MIN_VER_PYTHON}")
16
17 unset(HAVE_SPHINX CACHE)
18
19 if(PYTHONINTERP_FOUND)
20   set(PYTHON_VERSION_MAJOR_MINOR "${PYTHON_VERSION_MAJOR}.${PYTHON_VERSION_MINOR}")
21
22   if(NOT ANDROID AND NOT IOS)
23     find_host_package(PythonLibs "${PYTHON_VERSION_STRING}" EXACT)
24   endif()
25
26   if(NOT ANDROID AND NOT IOS)
27     if(CMAKE_HOST_UNIX)
28       execute_process(COMMAND ${PYTHON_EXECUTABLE} -c "from distutils.sysconfig import *; print(get_python_lib())"
29                       RESULT_VARIABLE PYTHON_CVPY_PROCESS
30                       OUTPUT_VARIABLE PYTHON_STD_PACKAGES_PATH
31                       OUTPUT_STRIP_TRAILING_WHITESPACE)
32       if("${PYTHON_STD_PACKAGES_PATH}" MATCHES "site-packages")
33         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/site-packages")
34       else() #debian based assumed, install to the dist-packages.
35         set(_PYTHON_PACKAGES_PATH "python${PYTHON_VERSION_MAJOR_MINOR}/dist-packages")
36       endif()
37       if(EXISTS "${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX}/${PYTHON_PACKAGES_PATH}")
38         set(_PYTHON_PACKAGES_PATH "lib${LIB_SUFFIX}/${_PYTHON_PACKAGES_PATH}")
39       else()
40         set(_PYTHON_PACKAGES_PATH "lib/${_PYTHON_PACKAGES_PATH}")
41       endif()
42     elseif(CMAKE_HOST_WIN32)
43       get_filename_component(PYTHON_PATH "${PYTHON_EXECUTABLE}" PATH)
44       file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
45       if(NOT EXISTS "${PYTHON_PATH}/Lib/site-packages")
46         unset(PYTHON_PATH)
47         get_filename_component(PYTHON_PATH "[HKEY_LOCAL_MACHINE\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
48         if(NOT PYTHON_PATH)
49            get_filename_component(PYTHON_PATH "[HKEY_CURRENT_USER\\SOFTWARE\\Python\\PythonCore\\${PYTHON_VERSION_MAJOR_MINOR}\\InstallPath]" ABSOLUTE)
50         endif()
51         file(TO_CMAKE_PATH "${PYTHON_PATH}" PYTHON_PATH)
52       endif()
53       set(_PYTHON_PACKAGES_PATH "${PYTHON_PATH}/Lib/site-packages")
54     endif()
55     SET(PYTHON_PACKAGES_PATH "${_PYTHON_PACKAGES_PATH}" CACHE PATH "Where to install the python packages.")
56
57     if(NOT PYTHON_NUMPY_INCLUDE_DIRS)
58       # Attempt to discover the NumPy include directory. If this succeeds, then build python API with NumPy
59       execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c
60                         "import os; os.environ['DISTUTILS_USE_SDK']='1'; import numpy.distutils; print(os.pathsep.join(numpy.distutils.misc_util.get_numpy_include_dirs()))"
61                       RESULT_VARIABLE PYTHON_NUMPY_PROCESS
62                       OUTPUT_VARIABLE PYTHON_NUMPY_INCLUDE_DIRS
63                       OUTPUT_STRIP_TRAILING_WHITESPACE)
64
65       if(PYTHON_NUMPY_PROCESS EQUAL 0)
66         file(TO_CMAKE_PATH "${PYTHON_NUMPY_INCLUDE_DIRS}" _PYTHON_NUMPY_INCLUDE_DIRS)
67         set(PYTHON_NUMPY_INCLUDE_DIRS "${_PYTHON_NUMPY_INCLUDE_DIRS}" CACHE PATH "Path to numpy headers")
68       endif()
69     endif()
70
71     if(PYTHON_NUMPY_INCLUDE_DIRS)
72       execute_process(COMMAND "${PYTHON_EXECUTABLE}" -c "import numpy; print(numpy.version.version)"
73                       OUTPUT_VARIABLE PYTHON_NUMPY_VERSION
74                       OUTPUT_STRIP_TRAILING_WHITESPACE)
75     endif()
76   endif(NOT ANDROID AND NOT IOS)
77 endif()
78
79 if(BUILD_DOCS)
80   find_host_program(SPHINX_BUILD sphinx-build)
81   find_host_program(PLANTUML plantuml)
82   if(SPHINX_BUILD)
83       execute_process(COMMAND "${SPHINX_BUILD}"
84                       OUTPUT_QUIET
85                       ERROR_VARIABLE SPHINX_OUTPUT
86                       OUTPUT_STRIP_TRAILING_WHITESPACE)
87       if(SPHINX_OUTPUT MATCHES "Sphinx v([0-9][^ \n]*)")
88         set(SPHINX_VERSION "${CMAKE_MATCH_1}")
89         set(HAVE_SPHINX 1)
90         message(STATUS "Found Sphinx ${SPHINX_VERSION}: ${SPHINX_BUILD}")
91       endif()
92   endif()
93 endif(BUILD_DOCS)