From a867d6f2f57a3a1f0c824ca33b189b82e651f353 Mon Sep 17 00:00:00 2001
From: Marko Petric <marko.petric@cern.ch>
Date: Wed, 30 Nov 2016 09:32:20 +0100
Subject: [PATCH] proper cmake rpath treatment

---
 cmake/DD4hepBuild.cmake | 26 +++++++++++++++++++-------
 examples/CMakeLists.txt |  4 ++++
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index aa41a08d8..4d9428b38 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -58,13 +58,25 @@ macro(dd4hep_set_compiler_flags)
    set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
 #   All this simply does not work!
    if ( APPLE )
-     set(CMAKE_SKIP_BUILD_RPATH            FALSE)
-     set(CMAKE_BUILD_WITH_INSTALL_RPATH    FALSE)
-     set(CMAKE_INSTALL_RPATH               "${CMAKE_BINARY_DIR}/lib")
-     set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
-#     set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-rpath ${CMAKE_BINARY_DIR}/lib")
-#     message( STATUS "Apple settings: CMAKE_INSTALL_RPATH:       ${CMAKE_INSTALL_RPATH}" )
-#     message( STATUS "Apple settings: CMAKE_SHARED_LINKER_FLAGS: ${CMAKE_SHARED_LINKER_FLAGS}" )
+     # use, i.e. don't skip the full RPATH for the build tree
+     SET(CMAKE_SKIP_BUILD_RPATH  FALSE)
+
+     # when building, don't use the install RPATH already
+     # (but later on when installing)
+     SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
+
+     SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
+
+     # add the automatically determined parts of the RPATH
+     # which point to directories outside the build tree to the install RPATH
+     SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
+
+
+     # the RPATH to be used when installing, but only if it's not a system directory
+     LIST(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
+     IF("${isSystemDir}" STREQUAL "-1")
+       SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
+     ENDIF("${isSystemDir}" STREQUAL "-1")
    endif()
  endif()
 endmacro(dd4hep_set_compiler_flags)
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 0a5d32e4d..a4e14832d 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -24,9 +24,13 @@
 cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
 option(BUILD_TESTING "Enable and build tests" ON)
 option(CMAKE_MACOSX_RPATH "Build with rpath on macos" ON)
+
 #
 find_package ( DD4hep REQUIRED )
 include ( ${DD4hep_DIR}/cmake/DD4hep.cmake )
+include ( ${DD4hep_DIR}/cmake/DD4hepBuild.cmake )
+#
+dd4hep_set_compiler_flags()
 #
 find_package ( ROOT REQUIRED COMPONENTS Geom GenVector )
 dd4hep_set_compiler_flags()
-- 
GitLab