[PATCH] D25304: cmake: Set the proper rpath in add_llvm_executable and llvm_add_library

Peter Levine via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 6 00:32:55 PDT 2016


plevine created this revision.
plevine added reviewers: beanz, mgorny.
plevine added a subscriber: llvm-commits.

LLVM sets CMAKE_INSTALL_RPATH in its top-level CMakeLists.txt to "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}". When Clang is built with LLVM, this propagates to Clang as well and ensures that even if CMAKE_INSTALL_PREFIX is set, installed binaries always find their dependent libraries.

When compiling clang as a separate build, no such rpath is set. If CMAKE_INSTALL_PREFIX is not set and libraries are installed to standard locations, this is not a problem. But when defining CMAKE_INSTALL_PREFIX, even if the same as that of LLVM, and installing and then executing binaries like clang, it results in

> error while loading shared libraries: libLLVMBPFCodeGen.so.4.0: cannot open shared object file: No such file or directory

A fix was proposed to Clang: https://reviews.llvm.org/D25267 <https://reviews.llvm.org/D25267>.  It was suggested that this problem be resolved by patching LLVM's 'add_llvm_executable' function and remove the rpath logic from the top-level CMakeLists.txt entirely.  It seems to me that such changes would also be needed in 'llvm_add_library' as rpath is relevant to shared libraries as well.

This patch is based on the rpath logic of the top-level CMakeLists.txt.  It has been tested by building LLVM with "CMAKE_INSTALL_PREFIX=/usr/lib/llvm-9999" and building Clang separately with "CMAKE_INSTALL_PREFIX=/usr/lib/clang-9999" (the version number is a Gentoo Linux convention).  Testing with `readelf -d /usr/lib/llvm-9999/bin/llvm-extract | grep 'RUNPATH'`, for instance, correctly prints:

> 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../lib64]

and `readelf -d /usr/lib/clang-9999/bin/clang++ | grep 'RUNPATH'` correctly prints

> 0x000000000000001d (RUNPATH)            Library runpath: [$ORIGIN/../lib64:/usr/lib64/llvm-9999/lib64]


https://reviews.llvm.org/D25304

Files:
  CMakeLists.txt
  cmake/modules/AddLLVM.cmake


Index: cmake/modules/AddLLVM.cmake
===================================================================
--- cmake/modules/AddLLVM.cmake
+++ cmake/modules/AddLLVM.cmake
@@ -476,6 +476,26 @@
     endif()
   endif()
 
+  if(ARG_SHARED)
+    if (APPLE)
+      set_target_properties( ${name} PROPERTIES INSTALL_NAME_DIR "@rpath" )
+      set_target_properties( ${name} PROPERTIES INSTALL_RPATH "@executable_path/../lib" )
+    else(UNIX)
+      if(NOT DEFINED CMAKE_INSTALL_RPATH)
+        set_target_properties( ${name} PROPERTIES INSTALL_RPATH "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" )
+        if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
+          set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-z,origin")
+        endif()
+      endif(NOT DEFINED CMAKE_INSTALL_RPATH)
+    endif()
+    if (${CMAKE_PROJECT_NAME} MATCHES "LLVM")
+      set_target_properties( ${name} PROPERTIES BUILD_WITH_INSTALL_RPATH ON )
+    else()
+      set_target_properties( ${name} PROPERTIES BUILD_WITH_INSTALL_RPATH OFF )
+      set_property(TARGET ${name} APPEND_STRING PROPERTY INSTALL_RPATH ":${LLVM_LIBRARY_DIR}")
+    endif()
+  endif(ARG_SHARED)
+
   if(ARG_SHARED AND UNIX)
     if(NOT APPLE AND ARG_SONAME)
       get_target_property(output_name ${name} OUTPUT_NAME)
@@ -675,6 +695,25 @@
   # macros.
   set_target_properties( ${name} PROPERTIES DEFINE_SYMBOL "" )
 
+  if (APPLE)
+    set_target_properties( ${name} PROPERTIES INSTALL_NAME_DIR "@rpath" )
+    set_target_properties( ${name} PROPERTIES INSTALL_RPATH "@executable_path/../lib" )
+  else(UNIX)
+    if(NOT DEFINED CMAKE_INSTALL_RPATH)
+      set_target_properties( ${name} PROPERTIES INSTALL_RPATH "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" )
+      if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
+        set_property(TARGET ${name} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-z,origin")
+      endif()
+    endif(NOT DEFINED CMAKE_INSTALL_RPATH)
+  endif()
+
+  if (${CMAKE_PROJECT_NAME} MATCHES "LLVM")
+    set_target_properties( ${name} PROPERTIES BUILD_WITH_INSTALL_RPATH ON )
+  else()
+    set_target_properties( ${name} PROPERTIES BUILD_WITH_INSTALL_RPATH OFF )
+    set_property(TARGET ${name} APPEND_STRING PROPERTY INSTALL_RPATH ":${LLVM_LIBRARY_DIR}")
+  endif()
+
   if (LLVM_EXPORTED_SYMBOL_FILE)
     add_llvm_symbol_exports( ${name} ${LLVM_EXPORTED_SYMBOL_FILE} )
   endif(LLVM_EXPORTED_SYMBOL_FILE)
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt
+++ CMakeLists.txt
@@ -672,20 +672,6 @@
 set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
 set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX} )
 
-set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
-if (APPLE)
-  set(CMAKE_INSTALL_NAME_DIR "@rpath")
-  set(CMAKE_INSTALL_RPATH "@executable_path/../lib")
-else(UNIX)
-  if(NOT DEFINED CMAKE_INSTALL_RPATH)
-    set(CMAKE_INSTALL_RPATH "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}")
-    if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)")
-      set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-z,origin")
-      set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,origin")
-    endif()
-  endif(NOT DEFINED CMAKE_INSTALL_RPATH)
-endif()
-
 if(APPLE AND DARWIN_LTO_LIBRARY)
   set(CMAKE_EXE_LINKER_FLAGS
     "${CMAKE_EXE_LINKER_FLAGS} -Wl,-lto_library -Wl,${DARWIN_LTO_LIBRARY}")


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25304.73731.patch
Type: text/x-patch
Size: 3409 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161006/9836c669/attachment.bin>


More information about the llvm-commits mailing list