LLVM Discussion Forums

Linking against LLVM on macOS and RPATH

Question: How do we set relocatable RPATH correctly during compilation of LLVM 9 on macOS 10.15 such that applications and libraries linking against shared LLVM libraries find them ?

Environment:
For our project, we build LLVM v9.0.0 on macOS 10.15.3 with SIP enabled using CMake:

cmake $SOURCEDIR/llvm \
  -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt" \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX:PATH="$INSTALLROOT" \
  -DLLVM_INSTALL_UTILS=ON \
  -DPYTHON_EXECUTABLE=$(which python3) \
  -DDEFAULT_SYSROOT=${DEFAULT_SYSROOT} \
  -DBUILD_SHARED_LIBS=ON

We link against some of the resulting shared libraries when building appache arrow which then links against our application.
When executing this application we get an RPATH related error

./o2-test-framework-ASoA 
dyld: Library not loaded: @rpath/libLLVMMCJIT.dylib
  Referenced from: /Users/alibuild/alice/sw/osx_x86-64/arrow/v0.14.1-1/lib/libgandiva.14.1.0.dylib
  Reason: image not found
zsh: abort      sw/BUILD/O2-latest/O2/stage/tests/o2-test-framework-ASoA

Inspecting the LLVM library in question yields

otool -L sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib 
sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib:
	@rpath/libLLVMMCJIT.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMExecutionEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMRuntimeDyld.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMTarget.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMObject.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMCore.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMSupport.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.0.0)
otool -l sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib | grep -A2 RPATH
          cmd LC_RPATH
      cmdsize 32
         path @loader_path/../lib (offset 12)

which makes us assume that LC_RPATH is not set correctly.

Changing the the rpath of libLLVMMCJIT.dylib results in the application finding the libraries again, so this seems to be an issue with the way how we compile LLVM.

install_name_tool -id $PWD/sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib $PWD/sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib 
alibuild@alibuildmac01wifi alice % otool -l sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib | grep -A2 RPATH
          cmd LC_RPATH
      cmdsize 32
         path @loader_path/../lib (offset 12)
otool -L sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib                 
sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib:
	/Users/alibuild/alice/sw/osx_x86-64/Clang/latest/lib/libLLVMMCJIT.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMExecutionEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMRuntimeDyld.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMTarget.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMObject.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMCore.dylib (compatibility version 0.0.0, current version 0.0.0)
	@rpath/libLLVMSupport.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 800.7.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.0.0)

How can we ensure that LLVM at build time sets the correct LC_RPATH?