Merge pull request #13458 from alalek:fix_python_install_path
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 18 Dec 2018 11:46:02 +0000 (11:46 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Tue, 18 Dec 2018 11:46:03 +0000 (11:46 +0000)
CMakeLists.txt
cmake/OpenCVGenSetupVars.cmake
modules/python/CMakeLists.txt
modules/python/common.cmake
modules/python/python_loader.cmake
modules/python/standalone.cmake

index fcfef89..f17220b 100644 (file)
@@ -608,7 +608,7 @@ else()
     endif()
   endif()
   ocv_update(OPENCV_INCLUDE_INSTALL_PATH "include")
-  ocv_update(OPENCV_PYTHON_INSTALL_PATH  "python")
+  #ocv_update(OPENCV_PYTHON_INSTALL_PATH  "python")  # no default value, see https://github.com/opencv/opencv/issues/13202
 endif()
 
 ocv_update(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}")
index 7d49b8c..61d0088 100644 (file)
@@ -43,11 +43,24 @@ else()
 endif()
 file(RELATIVE_PATH OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG
     "${CMAKE_INSTALL_PREFIX}/${OPENCV_SETUPVARS_INSTALL_PATH}/" "${CMAKE_INSTALL_PREFIX}/")
-if(IS_ABSOLUTE "${OPENCV_PYTHON_INSTALL_PATH}")
-  set(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${OPENCV_PYTHON_INSTALL_PATH}")
-  message(WARNING "CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install directory")
+if(DEFINED OPENCV_PYTHON_INSTALL_PATH)
+  set(__python_path "${OPENCV_PYTHON_INSTALL_PATH}")
+elseif(DEFINED OPENCV_PYTHON_INSTALL_PATH_SETUPVARS)
+  set(__python_path "${OPENCV_PYTHON_INSTALL_PATH_SETUPVARS}")
+endif()
+if(DEFINED __python_path)
+  if(IS_ABSOLUTE "${__python_path}")
+    set(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${__python_path}")
+    message(WARNING "CONFIGURATION IS NOT SUPPORTED: validate setupvars script in install directory")
+  else()
+    ocv_path_join(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG}" "${__python_path}")
+  endif()
 else()
-  ocv_path_join(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG}" "${OPENCV_PYTHON_INSTALL_PATH}")
+  if(DEFINED OPENCV_PYTHON3_INSTALL_PATH)
+    ocv_path_join(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG}" "${OPENCV_PYTHON3_INSTALL_PATH}")
+  else()
+    set(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "python_loader_is_not_installed")
+  endif()
 endif()
 configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/${OPENCV_SETUPVARS_TEMPLATE}" "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/${OPENCV_SETUPVARS_FILENAME}" @ONLY)
 install(FILES "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/${OPENCV_SETUPVARS_FILENAME}"
index 2787428..fbf01d6 100644 (file)
@@ -20,6 +20,7 @@ add_subdirectory(bindings)
 
 if(NOT OPENCV_SKIP_PYTHON_LOADER)
   include("./python_loader.cmake")
+  message(STATUS "OpenCV Python: during development append to PYTHONPATH: ${CMAKE_BINARY_DIR}/python_loader")
 endif()
 
 if(__disable_python2)
index 4b4eaa6..c65cddc 100644 (file)
@@ -120,6 +120,21 @@ if(NOT OPENCV_SKIP_PYTHON_LOADER)
   set(__python_loader_subdir "cv2/")
 endif()
 
+if(NOT " ${PYTHON}" STREQUAL " PYTHON"
+    AND NOT DEFINED OPENCV_PYTHON_INSTALL_PATH
+)
+  if(DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
+    set(OPENCV_PYTHON_INSTALL_PATH "${OPENCV_${PYTHON}_INSTALL_PATH}")
+  elseif(NOT OPENCV_SKIP_PYTHON_LOADER)
+    set(OPENCV_PYTHON_INSTALL_PATH "${${PYTHON}_PACKAGES_PATH}")
+  endif()
+endif()
+
+if(NOT OPENCV_SKIP_PYTHON_LOADER AND DEFINED OPENCV_PYTHON_INSTALL_PATH)
+  include("${CMAKE_CURRENT_LIST_DIR}/python_loader.cmake")
+  set(OPENCV_PYTHON_INSTALL_PATH_SETUPVARS "${OPENCV_PYTHON_INSTALL_PATH}" CACHE INTERNAL "")
+endif()
+
 if(NOT " ${PYTHON}" STREQUAL " PYTHON" AND DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
   set(__python_binary_install_path "${OPENCV_${PYTHON}_INSTALL_PATH}")
 elseif(OPENCV_SKIP_PYTHON_LOADER AND DEFINED ${PYTHON}_PACKAGES_PATH)
index 59ce8e5..663be5c 100644 (file)
@@ -2,20 +2,24 @@ ocv_assert(NOT OPENCV_SKIP_PYTHON_LOADER)
 
 set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}")
 
-ocv_assert(DEFINED OPENCV_PYTHON_INSTALL_PATH)
 if(OpenCV_FOUND)
   set(__loader_path "${OpenCV_BINARY_DIR}/python_loader")
+  message(STATUS "OpenCV Python: during development append to PYTHONPATH: ${__loader_path}")
 else()
   set(__loader_path "${CMAKE_BINARY_DIR}/python_loader")
 endif()
 
 set(__python_loader_install_tmp_path "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/python_loader/")
-if(IS_ABSOLUTE "${OPENCV_PYTHON_INSTALL_PATH}")
-  set(OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}/")
-  set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "'${CMAKE_INSTALL_PREFIX}'")
+if(DEFINED OPENCV_PYTHON_INSTALL_PATH)
+  if(IS_ABSOLUTE "${OPENCV_PYTHON_INSTALL_PATH}")
+    set(OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}/")
+    set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "'${CMAKE_INSTALL_PREFIX}'")
+  else()
+    file(RELATIVE_PATH OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}/${OPENCV_PYTHON_INSTALL_PATH}/cv2" ${CMAKE_INSTALL_PREFIX})
+    set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "os.path.join(LOADER_DIR, '${OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE}')")
+  endif()
 else()
-  file(RELATIVE_PATH OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE "${CMAKE_INSTALL_PREFIX}/${OPENCV_PYTHON_INSTALL_PATH}/cv2" ${CMAKE_INSTALL_PREFIX})
-  set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "os.path.join(LOADER_DIR, '${OpenCV_PYTHON_INSTALL_PATH_RELATIVE_CONFIGCMAKE}')")
+  set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "os.path.join(LOADER_DIR, 'not_installed')")
 endif()
 
 set(PYTHON_LOADER_FILES
@@ -25,7 +29,13 @@ set(PYTHON_LOADER_FILES
 foreach(fname ${PYTHON_LOADER_FILES})
   get_filename_component(__dir "${fname}" DIRECTORY)
   file(COPY "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${__loader_path}/${__dir}")
-  install(FILES "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/${__dir}" COMPONENT python)
+  if(fname STREQUAL "setup.py")
+    if(OPENCV_PYTHON_SETUP_PY_INSTALL_PATH)
+      install(FILES "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${OPENCV_PYTHON_SETUP_PY_INSTALL_PATH}" COMPONENT python)
+    endif()
+  elseif(DEFINED OPENCV_PYTHON_INSTALL_PATH)
+    install(FILES "${PYTHON_SOURCE_DIR}/package/${fname}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/${__dir}" COMPONENT python)
+  endif()
 endforeach()
 
 if(NOT OpenCV_FOUND)  # Ignore "standalone" builds of Python bindings
@@ -41,14 +51,15 @@ if(NOT OpenCV_FOUND)  # Ignore "standalone" builds of Python bindings
   string(REPLACE ";" ",\n    " CMAKE_PYTHON_BINARIES_PATH "${CMAKE_PYTHON_BINARIES_PATH}")
   configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__loader_path}/cv2/config.py" @ONLY)
 
-  if(WIN32)
-    list(APPEND CMAKE_PYTHON_BINARIES_INSTALL_PATH "os.path.join(${CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE}, '${OPENCV_BIN_INSTALL_PATH}')")
-  else()
-    list(APPEND CMAKE_PYTHON_BINARIES_INSTALL_PATH "os.path.join(${CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE}, '${OPENCV_LIB_INSTALL_PATH}')")
+  # install
+  if(DEFINED OPENCV_PYTHON_INSTALL_PATH)
+    if(WIN32)
+      list(APPEND CMAKE_PYTHON_BINARIES_INSTALL_PATH "os.path.join(${CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE}, '${OPENCV_BIN_INSTALL_PATH}')")
+    else()
+      list(APPEND CMAKE_PYTHON_BINARIES_INSTALL_PATH "os.path.join(${CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE}, '${OPENCV_LIB_INSTALL_PATH}')")
+    endif()
+    string(REPLACE ";" ",\n    " CMAKE_PYTHON_BINARIES_PATH "${CMAKE_PYTHON_BINARIES_INSTALL_PATH}")
+    configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__python_loader_install_tmp_path}/cv2/config.py" @ONLY)
+    install(FILES "${__python_loader_install_tmp_path}/cv2/config.py" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
   endif()
-  string(REPLACE ";" ",\n    " CMAKE_PYTHON_BINARIES_PATH "${CMAKE_PYTHON_BINARIES_INSTALL_PATH}")
-  configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__python_loader_install_tmp_path}/cv2/config.py" @ONLY)
-  install(FILES "${__python_loader_install_tmp_path}/cv2/config.py" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
-
-  message(STATUS "OpenCV Python: during development append to PYTHONPATH: ${__loader_path}")
 endif()
index 5848151..a27bba0 100644 (file)
@@ -3,7 +3,13 @@ if(NOT DEFINED OpenCV_BINARY_DIR)
 endif()
 include("${OpenCV_BINARY_DIR}/opencv_python_config.cmake")
 if(NOT DEFINED OpenCV_SOURCE_DIR)
-  message(FATAL_ERROR "Missing define of OpenCV_SOURCE_DIR")
+  message(FATAL_ERROR "Missing OpenCV_SOURCE_DIR")
+endif()
+if(NOT OPENCV_PYTHON_INSTALL_PATH)
+  if(NOT DEFINED OPENCV_PYTHON_STANDALONE_INSTALL_PATH)
+    message(FATAL_ERROR "Missing OPENCV_PYTHON_STANDALONE_INSTALL_PATH / OPENCV_PYTHON_INSTALL_PATH")
+  endif()
+  set(OPENCV_PYTHON_INSTALL_PATH "${OPENCV_PYTHON_STANDALONE_INSTALL_PATH}")
 endif()
 
 include("${OpenCV_SOURCE_DIR}/cmake/OpenCVUtils.cmake")