cmake: support rpath-link linker option
authorAlexander Alekhin <alexander.a.alekhin@gmail.com>
Fri, 28 Jun 2019 22:39:38 +0000 (22:39 +0000)
committerAlexander Alekhin <alexander.a.alekhin@gmail.com>
Sat, 29 Jun 2019 11:03:45 +0000 (11:03 +0000)
- builds with CMAKE_SKIP_RPATH=ON should properly find project .so files from <build>/lib directory
- $ORIGIN doesn't work properly in cross-compilation mode

cmake/OpenCVCompilerOptions.cmake

index 23b08b2..0b9d669 100644 (file)
@@ -396,3 +396,23 @@ macro(ocv_add_modules_compiler_options)
     add_definitions(-DOPENCV_ENABLE_MEMORY_SANITIZER=1)
   endif()
 endmacro()
+
+# adjust -Wl,-rpath-link
+if(CMAKE_SKIP_RPATH)
+  if((NOT CMAKE_CROSSCOMPILING OR OPENCV_ENABLE_LINKER_RPATH_LINK_ORIGIN) AND NOT OPENCV_SKIP_LINKER_RPATH_LINK_ORIGIN)
+    if(DEFINED CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN)
+      list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN}")
+    else()
+      list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "\$ORIGIN")
+    endif()
+  elseif(NOT OPENCV_SKIP_LINKER_RPATH_LINK_BINARY_LIB)
+    list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${LIBRARY_OUTPUT_PATH}")
+  endif()
+endif()
+if(OPENCV_EXTRA_RPATH_LINK_PATH)
+  string(REPLACE ":" ";" OPENCV_EXTRA_RPATH_LINK_PATH_ "${OPENCV_EXTRA_RPATH_LINK_PATH}")
+  list(APPEND CMAKE_PLATFORM_RUNTIME_PATH ${OPENCV_EXTRA_RPATH_LINK_PATH_})
+  if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG)
+    message(WARNING "OPENCV_EXTRA_RPATH_LINK_PATH may not work properly because CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG is not defined (not supported)")
+  endif()
+endif()