From 61e900b4057dff9c9a5d7e8b0a2cffd2e861414b Mon Sep 17 00:00:00 2001 From: Chris Bieneman Date: Tue, 1 Nov 2016 17:44:58 +0000 Subject: [PATCH] [CMake] Fix rpath construction for out-of-tree builds MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This patch was produced in conjunction with Michał Górny. It should resolve the issues that were trying to be solved by D25304. This moves rpath handling into `llvm_add_library` and `add_llvm_executable` so that it is available to all projects using AddLLVM whether built in-tree or out-of-tree. llvm-svn: 285714 --- llvm/CMakeLists.txt | 14 -------------- llvm/cmake/modules/AddLLVM.cmake | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/llvm/CMakeLists.txt b/llvm/CMakeLists.txt index bb1055a..1b7fe83 100644 --- a/llvm/CMakeLists.txt +++ b/llvm/CMakeLists.txt @@ -677,20 +677,6 @@ set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/bin ) 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}") diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake index b3105e0..db2f31c 100644 --- a/llvm/cmake/modules/AddLLVM.cmake +++ b/llvm/cmake/modules/AddLLVM.cmake @@ -415,6 +415,9 @@ function(llvm_add_library name) elseif(ARG_SHARED) add_windows_version_resource_file(ALL_FILES ${ALL_FILES}) add_library(${name} SHARED ${ALL_FILES}) + + llvm_setup_rpath(${name}) + else() add_library(${name} STATIC ${ALL_FILES}) endif() @@ -659,6 +662,8 @@ macro(add_llvm_executable name) add_executable(${name} ${ALL_FILES}) endif() + llvm_setup_rpath(${name}) + if(DEFINED windows_resource_file) set_windows_version_resource_properties(${name} ${windows_resource_file}) endif() @@ -1317,3 +1322,35 @@ function(llvm_externalize_debuginfo name) message(FATAL_ERROR "LLVM_EXTERNALIZE_DEBUGINFO isn't implemented for non-darwin platforms!") endif() endfunction() + +function(llvm_setup_rpath name) + if(LLVM_INSTALL_PREFIX AND NOT (LLVM_INSTALL_PREFIX STREQUAL CMAKE_INSTALL_PREFIX)) + set(extra_libdir ${LLVM_LIBRARY_DIR}) + elseif(LLVM_BUILD_LIBRARY_DIR) + set(extra_libdir ${LLVM_LIBRARY_DIR}) + endif() + + if (APPLE) + set(_install_name_dir INSTALL_NAME_DIR "@rpath") + set(_install_rpath "@loader_path/../lib" ${extra_libdir}) + elseif(UNIX) + if(NOT DEFINED CMAKE_INSTALL_RPATH) + set(_install_rpath "\$ORIGIN/../lib${LLVM_LIBDIR_SUFFIX}" ${extra_libdir}) + if(${CMAKE_SYSTEM_NAME} MATCHES "(FreeBSD|DragonFly)") + set_property(TARGET ${name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-z,origin ") + elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux" AND NOT LLVM_LINKER_IS_GOLD) + # $ORIGIN is not interpreted at link time by ld.bfd + set_property(TARGET ${name} APPEND_STRING PROPERTY + LINK_FLAGS " -Wl,-rpath-link,${LLVM_LIBRARY_OUTPUT_INTDIR} ") + endif() + endif() + else() + return() + endif() + + set_target_properties(${name} PROPERTIES + BUILD_WITH_INSTALL_RPATH On + INSTALL_RPATH "${_install_rpath}" + ${_install_name_dir}) +endfunction() -- 2.7.4