BundleUtilities macOS: failure resolving library with @rpath when LC_RPATH isn't set
Example: Create an application bundle linking to sfml-network from Homebrew, and try to run fixup_bundle
on it, like so:
cmake_minimum_required(VERSION 3.7)
project(foo)
find_library(SFML_NETWORK sfml-network)
add_executable(foo foo.c)
set_target_properties(foo PROPERTIES MACOSX_BUNDLE YES)
target_link_libraries(foo PRIVATE "${SFML_NETWORK}")
install(TARGETS foo DESTINATION .)
install(CODE "
set(BU_CHMOD_BUNDLE_ITEMS ON)
include(BundleUtilities)
fixup_bundle(\"${CMAKE_INSTALL_PREFIX}/foo.app\" \"\" \"\")
")
Unfortunately, fixup_bundle fails, because sfml-network links to sfml-system using an rpath:
$ ninja install -v
[2/3] cmake -P cmake_install.cmake
-- Install configuration: ""
-- Installing: foo.app
-- Installing: foo.app/Contents
-- Installing: foo.app/Contents/Info.plist
-- Installing: foo.app/Contents/MacOS
-- Installing: foo.app/Contents/MacOS/foo
-- fixup_bundle
-- app='root/foo.app'
-- libs=''
-- dirs=''
-- ignoreItems=''
-- fixup_bundle: preparing...
--
warning: cannot resolve item '@rpath/libsfml-system.2.4.dylib'
possible problems:
need more directories?
need to use InstallRequiredSystemLibraries?
run in install tree instead of build tree?
-- warning: gp_resolved_file_type non-absolute file '@rpath/libsfml-system.2.4.dylib' returning type 'other' -- possibly incorrect
--
warning: cannot resolve item '@rpath/libsfml-system.2.4.dylib'
possible problems:
need more directories?
need to use InstallRequiredSystemLibraries?
run in install tree instead of build tree?
warning: target '@rpath/libsfml-system.2.4.dylib' is not absolute...
warning: target '@rpath/libsfml-system.2.4.dylib' does not exist...
CMake Error at /usr/local/Cellar/cmake/3.7.1/share/cmake/Modules/GetPrerequisites.cmake:818 (message):
/usr/bin/otool failed: 1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump:
'@rpath/libsfml-system.2.4.dylib': No such file or directory.
fatal error:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/otool:
internal objdump command failed
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.7.1/share/cmake/Modules/GetPrerequisites.cmake:943 (get_prerequisites)
/usr/local/Cellar/cmake/3.7.1/share/cmake/Modules/GetPrerequisites.cmake:943 (get_prerequisites)
/usr/local/Cellar/cmake/3.7.1/share/cmake/Modules/BundleUtilities.cmake:618 (get_prerequisites)
/usr/local/Cellar/cmake/3.7.1/share/cmake/Modules/BundleUtilities.cmake:848 (get_bundle_keys)
cmake_install.cmake:38 (fixup_bundle)
FAILED: CMakeFiles/install.util
$ otool -L /usr/local/lib/libsfml-network.dylib
/usr/local/lib/libsfml-network.dylib:
/usr/local/opt/sfml/lib/libsfml-network.2.4.dylib (compatibility version 2.4.0, current version 2.4.0)
@rpath/libsfml-system.2.4.dylib (compatibility version 2.4.0, current version 2.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 120.1.0)
This can be fixed by passing -DCMAKE_INSTALL_RPATH=/usr/local/opt/sfml/lib
, but since the macOS system dyld works regardless so should BundleUtilities.