From fbe41703e4bf6cf57acd2e5411d8460767acef8e Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 1 Nov 2018 01:37:40 +0000 Subject: [PATCH] cmake: introduce setup_vars scripts - CMake install COMPONENT: "scripts" --- CMakeLists.txt | 5 +++ cmake/OpenCVGenSetupVars.cmake | 51 +++++++++++++++++++++++++ cmake/OpenCVUtils.cmake | 4 +- cmake/templates/setup_vars_linux.sh.in | 20 ++++++++++ cmake/templates/setup_vars_macosx.sh.in | 20 ++++++++++ cmake/templates/setup_vars_win32.cmd.in | 18 +++++++++ 6 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 cmake/OpenCVGenSetupVars.cmake create mode 100644 cmake/templates/setup_vars_linux.sh.in create mode 100644 cmake/templates/setup_vars_macosx.sh.in create mode 100644 cmake/templates/setup_vars_win32.cmd.in diff --git a/CMakeLists.txt b/CMakeLists.txt index a536b4d5c0..cfcc63f665 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -345,6 +345,7 @@ OCV_OPTION(GENERATE_ABI_DESCRIPTOR "Generate XML file for abi_compliance_chec OCV_OPTION(CV_ENABLE_INTRINSICS "Use intrinsic-based optimized code" ON ) OCV_OPTION(CV_DISABLE_OPTIMIZATION "Disable explicit optimized code (dispatched code/intrinsics/loop unrolling/etc)" OFF ) OCV_OPTION(CV_TRACE "Enable OpenCV code trace" ON) +OCV_OPTION(OPENCV_GENERATE_SETUPVARS "Generate setup_vars* scripts" ON IF (NOT ANDROID AND NOT APPLE_FRAMEWORK) ) OCV_OPTION(ENABLE_PYLINT "Add target with Pylint checks" (BUILD_DOCS OR BUILD_EXAMPLES) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) ) OCV_OPTION(ENABLE_FLAKE8 "Add target with Python flake8 checker" (BUILD_DOCS OR BUILD_EXAMPLES) IF (NOT CMAKE_CROSSCOMPILING AND NOT APPLE_FRAMEWORK) ) @@ -925,6 +926,10 @@ if(COMMAND ocv_pylint_finalize) ocv_pylint_finalize() endif() +if(OPENCV_GENERATE_SETUPVARS) + include(cmake/OpenCVGenSetupVars.cmake) +endif() + # ---------------------------------------------------------------------------- # Summary: # ---------------------------------------------------------------------------- diff --git a/cmake/OpenCVGenSetupVars.cmake b/cmake/OpenCVGenSetupVars.cmake new file mode 100644 index 0000000000..e924317871 --- /dev/null +++ b/cmake/OpenCVGenSetupVars.cmake @@ -0,0 +1,51 @@ +if(WIN32) + ocv_update(OPENCV_SETUPVARS_INSTALL_PATH ".") + ocv_update(OPENCV_SCRIPT_EXTENSION ".cmd") + ocv_update(OPENCV_SETUPVARS_TEMPLATE "setup_vars_win32.cmd.in") +else() + ocv_update(OPENCV_SETUPVARS_INSTALL_PATH "bin") + ocv_update(OPENCV_SCRIPT_EXTENSION ".sh") + if(APPLE) + ocv_update(OPENCV_SETUPVARS_TEMPLATE "setup_vars_macosx.sh.in") + else() + ocv_update(OPENCV_SETUPVARS_TEMPLATE "setup_vars_linux.sh.in") + endif() +endif() + +if(INSTALL_TO_MANGLED_PATHS) + ocv_update(OPENCV_SETUPVARS_FILENAME "setup_vars_opencv-${OPENCV_VERSION}${OPENCV_SCRIPT_EXTENSION}") +else() + ocv_update(OPENCV_SETUPVARS_FILENAME setup_vars_opencv3${OPENCV_SCRIPT_EXTENSION}) +endif() + +##### build directory +if(WIN32) + set(__build_type "${CMAKE_BUILD_TYPE}") + if(NOT __build_type) + set(__build_type "Release") # default + endif() + file(RELATIVE_PATH OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG "${OpenCV_BINARY_DIR}/" "${EXECUTABLE_OUTPUT_PATH}/${__build_type}/") +else() + file(RELATIVE_PATH OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG "${OpenCV_BINARY_DIR}/" "${LIBRARY_OUTPUT_PATH}/") +endif() +set(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "python_loader") # https://github.com/opencv/opencv/pull/12977 +configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/${OPENCV_SETUPVARS_TEMPLATE}" "${CMAKE_BINARY_DIR}/tmp/setup_vars${OPENCV_SCRIPT_EXTENSION}" @ONLY) +file(COPY "${CMAKE_BINARY_DIR}/tmp/setup_vars${OPENCV_SCRIPT_EXTENSION}" DESTINATION "${CMAKE_BINARY_DIR}" + FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) + +##### install directory +if(WIN32) + file(RELATIVE_PATH OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG + "${CMAKE_INSTALL_PREFIX}/${OPENCV_SETUPVARS_INSTALL_PATH}/" "${CMAKE_INSTALL_PREFIX}/${OPENCV_BIN_INSTALL_PATH}/") +else() + file(RELATIVE_PATH OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG + "${CMAKE_INSTALL_PREFIX}/${OPENCV_SETUPVARS_INSTALL_PATH}/" "${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}/") +endif() +file(RELATIVE_PATH OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG + "${CMAKE_INSTALL_PREFIX}/${OPENCV_SETUPVARS_INSTALL_PATH}/" "${CMAKE_INSTALL_PREFIX}/") +ocv_path_join(OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG "${OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG}" "python_loader") # https://github.com/opencv/opencv/pull/12977 +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}" + DESTINATION "${OPENCV_SETUPVARS_INSTALL_PATH}" + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE + COMPONENT scripts) diff --git a/cmake/OpenCVUtils.cmake b/cmake/OpenCVUtils.cmake index 3ae6162ba6..a07f035b3a 100644 --- a/cmake/OpenCVUtils.cmake +++ b/cmake/OpenCVUtils.cmake @@ -149,7 +149,9 @@ macro(ocv_path_join result_var P1 P2_) else() set(${result_var} "${P1}/${P2}") endif() - string(REGEX REPLACE "([/\\]?)[\\.][/\\]" "\\1" ${result_var} "${${result_var}}") + string(REPLACE "\\\\" "\\" ${result_var} "${${result_var}}") + string(REPLACE "//" "/" ${result_var} "${${result_var}}") + string(REGEX REPLACE "(^|[/\\])[\\.][/\\]" "\\1" ${result_var} "${${result_var}}") if("${${result_var}}" STREQUAL "") set(${result_var} ".") endif() diff --git a/cmake/templates/setup_vars_linux.sh.in b/cmake/templates/setup_vars_linux.sh.in new file mode 100644 index 0000000000..f7d30541c6 --- /dev/null +++ b/cmake/templates/setup_vars_linux.sh.in @@ -0,0 +1,20 @@ +#!/bin/bash + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +[[ ! "${OPENCV_QUIET}" ]] && ( echo "Setting vars for OpenCV @OPENCV_VERSION@" ) +export LD_LIBRARY_PATH="$SCRIPT_DIR/@OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG@:$LD_LIBRARY_PATH" + +if [[ ! "$OPENCV_SKIP_PYTHON" ]]; then + PYTHONPATH_OPENCV="$SCRIPT_DIR/@OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG@" + [[ ! "${OPENCV_QUIET}" ]] && ( echo "Append PYTHONPATH: ${PYTHONPATH_OPENCV}" ) + export PYTHONPATH="${PYTHONPATH_OPENCV}:$PYTHONPATH" +fi + +# Don't exec in "sourced" mode +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + if [[ $# -ne 0 ]]; then + [[ ! "${OPENCV_QUIET}" && "${OPENCV_VERBOSE}" ]] && ( echo "Executing: $*" ) + exec "$@" + fi +fi diff --git a/cmake/templates/setup_vars_macosx.sh.in b/cmake/templates/setup_vars_macosx.sh.in new file mode 100644 index 0000000000..3e85f3add2 --- /dev/null +++ b/cmake/templates/setup_vars_macosx.sh.in @@ -0,0 +1,20 @@ +#!/bin/bash + +SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" + +[[ ! "${OPENCV_QUIET}" ]] && ( echo "Setting vars for OpenCV @OPENCV_VERSION@" ) +export DYLD_LIBRARY_PATH="$SCRIPT_DIR/@OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG@:$DYLD_LIBRARY_PATH" + +if [[ ! "$OPENCV_SKIP_PYTHON" ]]; then + PYTHONPATH_OPENCV="$SCRIPT_DIR/@OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG@" + [[ ! "${OPENCV_QUIET}" ]] && ( echo "Append PYTHONPATH: ${PYTHONPATH_OPENCV}" ) + export PYTHONPATH="${PYTHONPATH_OPENCV}:$PYTHONPATH" +fi + +# Don't exec in "sourced" mode +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + if [[ $# -ne 0 ]]; then + [[ ! "${OPENCV_QUIET}" && "${OPENCV_VERBOSE}" ]] && ( echo "Executing: $*" ) + exec "$@" + fi +fi diff --git a/cmake/templates/setup_vars_win32.cmd.in b/cmake/templates/setup_vars_win32.cmd.in new file mode 100644 index 0000000000..f6722535ac --- /dev/null +++ b/cmake/templates/setup_vars_win32.cmd.in @@ -0,0 +1,18 @@ +@ECHO OFF +SETLOCAL EnableDelayedExpansion + +SET "SCRIPT_DIR=%~dp0" + +IF NOT DEFINED OPENCV_QUIET ( ECHO Setting vars for OpenCV @OPENCV_VERSION@ ) +SET "PATH=!SCRIPT_DIR!\@OPENCV_LIB_RUNTIME_DIR_RELATIVE_CMAKECONFIG@;%PATH%" + +IF NOT DEFINED OPENCV_SKIP_PYTHON ( + SET "PYTHONPATH_OPENCV=!SCRIPT_DIR!\@OPENCV_PYTHON_DIR_RELATIVE_CMAKECONFIG@" + IF NOT DEFINED OPENCV_QUIET ( ECHO Append PYTHONPATH: !PYTHONPATH_OPENCV! ) + SET "PYTHONPATH=!PYTHONPATH_OPENCV!;%PYTHONPATH%" +) + +IF NOT [%1] == [] ( + %* + EXIT /B !errorlevel! +)