From 2a47e2c1a575a1c73d807599d8c0c8c620664700 Mon Sep 17 00:00:00 2001 From: Anshu Date: Mon, 29 Jun 2026 16:57:21 +0530 Subject: [PATCH] Merge pull request #29396 from 0AnshuAditya0:fix-macos-framework-rpath-29028 cmake: fix #29028 macOS dynamic framework RPATH for relocatable install #29396 ### Problem When building OpenCV as a dynamic framework on macOS (`-DAPPLE_FRAMEWORK=ON -DBUILD_SHARED_LIBS=ON`), installed binaries contain an absolute `LC_RPATH` pointing to the build tree, making the install non-relocatable. Moving the install directory or deploying to another Mac causes binaries to crash on launch. ### Root cause `cmake/OpenCVInstallLayout.cmake` unconditionally sets `CMAKE_INSTALL_RPATH` to `${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}` for all platforms. Additionally, `cmake/platforms/OpenCV-Darwin.cmake` was empty and missing the `@executable_path`/`@loader_path` linker flags that the iOS toolchain already sets for the same `APPLE_FRAMEWORK AND BUILD_SHARED_LIBS` case. ### Fix - Guard `CMAKE_INSTALL_RPATH` in `OpenCVInstallLayout.cmake` to use relocatable `@executable_path` tokens for Apple framework builds - Add equivalent linker flags to `OpenCV-Darwin.cmake`, mirroring `platforms/ios/cmake/Modules/Platform/iOS.cmake` Fixes #29028 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- cmake/OpenCVInstallLayout.cmake | 6 +++++- cmake/platforms/OpenCV-Darwin.cmake | 9 ++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cmake/OpenCVInstallLayout.cmake b/cmake/OpenCVInstallLayout.cmake index 25aede72c5..2f3c4d9a48 100644 --- a/cmake/OpenCVInstallLayout.cmake +++ b/cmake/OpenCVInstallLayout.cmake @@ -96,7 +96,11 @@ else() # UNIX endif() -ocv_update(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}") +if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS) + ocv_update(CMAKE_INSTALL_RPATH "@executable_path/Frameworks;@loader_path/Frameworks;@executable_path/../${OPENCV_3P_LIB_INSTALL_PATH}") +else() + ocv_update(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_LIB_INSTALL_PATH}") +endif() set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) if(INSTALL_TO_MANGLED_PATHS) diff --git a/cmake/platforms/OpenCV-Darwin.cmake b/cmake/platforms/OpenCV-Darwin.cmake index 1bb8bf6d7f..5f6c32ac4d 100644 --- a/cmake/platforms/OpenCV-Darwin.cmake +++ b/cmake/platforms/OpenCV-Darwin.cmake @@ -1 +1,8 @@ -# empty +# Additional flags for dynamic framework (macOS) +if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS) + string(APPEND CMAKE_MODULE_LINKER_FLAGS " -rpath @executable_path/Frameworks -rpath @loader_path/Frameworks") + string(APPEND CMAKE_SHARED_LINKER_FLAGS " -rpath @executable_path/Frameworks -rpath @loader_path/Frameworks") + string(APPEND CMAKE_EXE_LINKER_FLAGS " -rpath @executable_path/Frameworks -rpath @loader_path/Frameworks") + set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG 1) + set(CMAKE_INSTALL_NAME_DIR "@rpath") +endif()