From 2ce371e472ec5687882e36e0995a6a7a40b88135 Mon Sep 17 00:00:00 2001
From: Marko Petric <marko.petric@cern.ch>
Date: Tue, 17 Mar 2020 13:43:47 +0100
Subject: [PATCH] Update RPATH settings

---
 CMakeLists.txt            | 11 ++--------
 cmake/DD4hepBuild.cmake   | 45 ++++++++++++++++++++++-----------------
 cmake/MakeGaudiMap.cmake  |  7 +++++-
 cmake/run_test.sh         |  2 ++
 cmake/run_test_package.sh |  2 ++
 cmake/thisdd4hep.sh       | 36 ++++++++++++++++++-------------
 examples/CMakeLists.txt   | 19 +++++++++++++++++
 7 files changed, 77 insertions(+), 45 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index c5472e768..8a5797aa5 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -75,7 +75,8 @@ option(DD4HEP_USE_HEPMC3  "Build hepmc3 extensions"                       OFF)
 option(DD4HEP_LOAD_ASSIMP "Download and build ASSIMP from github"         OFF)
 option(BUILD_TESTING      "Enable and build tests"                        ON)
 option(BUILD_SHARED_LIBS  "If OFF build STATIC Libraries"                 ON)
-option(CMAKE_MACOSX_RPATH "Build with rpath on macos"                     ON)
+option(DD4HEP_SET_RPATH   "Link libraries with built-in RPATH (run-time search path)" ON)
+
 SET(DD4HEP_BUILD_PACKAGES "DDRec DDDetectors DDCond DDAlign DDCAD DDDigi DDG4 DDEve UtilityApps"
   CACHE STRING "List of DD4hep packages to build")
 SEPARATE_ARGUMENTS(DD4HEP_BUILD_PACKAGES)
@@ -206,14 +207,6 @@ endif()
 
 
 ENDIF(DD4HEP_USE_EXISTING_DD4HEP)
-#######################
-# Treatment for Apple #
-#######################
-
-if( APPLE )
-  set( USE_DYLD 1)
-  set(CMAKE_MACOSX_RPATH 1)
-endif()
 
 #########################
 # Configure and install #
diff --git a/cmake/DD4hepBuild.cmake b/cmake/DD4hepBuild.cmake
index 3027fe5cc..63034a8f5 100644
--- a/cmake/DD4hepBuild.cmake
+++ b/cmake/DD4hepBuild.cmake
@@ -88,26 +88,31 @@ macro(dd4hep_set_compiler_flags)
     MESSAGE( WARNING "We do not test with the ${CMAKE_CXX_COMPILER_ID} compiler, use at your own discretion" )
   endif()
 
- #rpath treatment
- if (APPLE)
-   # 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)
-
+ #---RPATH options-------------------------------------------------------------------------------
+ #  When building, don't use the install RPATH already (but later on when installing)
+ set(CMAKE_SKIP_BUILD_RPATH FALSE)         # don't skip the full RPATH for the build tree
+ set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE) # use always the build RPATH for the build tree
+ set(CMAKE_MACOSX_RPATH TRUE)              # use RPATH for MacOSX
+ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # point to directories outside the build tree to the install RPATH
+
+ # Check whether to add RPATH to the installation (the build tree always has the RPATH enabled)
+ if(APPLE)
+   set(CMAKE_INSTALL_NAME_DIR "@rpath")
+   set(CMAKE_INSTALL_RPATH "@loader_path/../lib")    # self relative LIBDIR
+   # 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 "@loader_path/../lib")
+   endif("${isSystemDir}" STREQUAL "-1")
+ elseif(DD4HEP_SET_RPATH)
+   set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib") # install LIBDIR
    # 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")
+   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")
+ else()
+   set(CMAKE_SKIP_INSTALL_RPATH TRUE)           # skip the full RPATH for the install tree
  endif()
 endmacro(dd4hep_set_compiler_flags)
 
@@ -561,7 +566,7 @@ function(dd4hep_add_dictionary dictionary )
   add_custom_command(OUTPUT ${dictionary}.cxx ${output_dir}/${dictionary}_rdict.pcm
     COMMAND ${ROOT_rootcling_CMD}
     ARGS -f ${dictionary}.cxx -s ${output_dir}/${dictionary} -inlineInputHeader
-    ${ARG_OPTIONS} -std=c++${CMAKE_CXX_STANDARD}
+    ${ARG_OPTIONS}
    "$<$<BOOL:$<JOIN:${comp_defs},>>:-D$<JOIN:${comp_defs},;-D>>"
    "$<$<BOOL:$<JOIN:${inc_dirs},>>:-I$<JOIN:${inc_dirs},;-I>>"
    "$<JOIN:${headers},;>" "$<JOIN:${linkdefs},;>"
diff --git a/cmake/MakeGaudiMap.cmake b/cmake/MakeGaudiMap.cmake
index 6cd966a38..3b8bb3873 100644
--- a/cmake/MakeGaudiMap.cmake
+++ b/cmake/MakeGaudiMap.cmake
@@ -3,6 +3,11 @@ IF($ENV{VERBOSE})
   MESSAGE(" *** MakeGaudiMap.cmake run command : ${DD4HEP_LISTCOMPONENTS_CMD} -o ${rootmapfile} ${libname}
                     WORKING_DIRECTORY ${LIBRARY_LOCATION} "
     )
+  MESSAGE("DD4HEP_LIBRARY_LOCATION = ${DD4HEP_LIBRARY_LOCATION}")
+  MESSAGE("DYLD_LIBRARY_PATH = $ENV{DYLD_LIBRARY_PATH}")
+  MESSAGE("DD4HEP_LIBRARY_PATH = $ENV{DD4HEP_LIBRARY_PATH}")
+  MESSAGE("LD_LIBRARY_PATH = $ENV{LD_LIBRARY_PATH}")
+  MESSAGE("LIBRARY_LOCATION = ${LIBRARY_LOCATION}")
 ENDIF()
 
   GET_FILENAME_COMPONENT(GAUDI_LISTCOMP_INSTALL ${DD4HEP_LISTCOMPONENTS_CMD} DIRECTORY)
@@ -11,7 +16,7 @@ ENDIF()
   else()
     SET ( ENV{LD_LIBRARY_PATH} ${LIBRARY_LOCATION}:${DD4HEP_LIBRARY_LOCATION}:$ENV{LD_LIBRARY_PATH} )
   endif()
-  # EXECUTE_PROCESS( COMMAND echo LD_LIBRARY_PATH = $ENV{LD_LIBRARY_PATH} )
+
   EXECUTE_PROCESS( COMMAND 
     ${DD4HEP_LISTCOMPONENTS_CMD} -o ${rootmapfile} ${libname}
     WORKING_DIRECTORY ${LIBRARY_LOCATION}
diff --git a/cmake/run_test.sh b/cmake/run_test.sh
index eab04a6c2..40c53bd55 100755
--- a/cmake/run_test.sh
+++ b/cmake/run_test.sh
@@ -21,7 +21,9 @@ for i in "$@" ; do
     fi
 done
 
+export DYLD_LIBRARY_PATH=$DD4HEP_LIBRARY_PATH
 echo " #### LD_LIBRARY_PATH = :  ${LD_LIBRARY_PATH}"
+echo " #### DYLD_LIBRARY_PATH = :  ${DYLD_LIBRARY_PATH}"
 
 echo "---running test :  '" ${command} ${theargs} "'"
 eval ${command} ${theargs}
diff --git a/cmake/run_test_package.sh b/cmake/run_test_package.sh
index 443f25896..48b1d9d20 100755
--- a/cmake/run_test_package.sh
+++ b/cmake/run_test_package.sh
@@ -20,7 +20,9 @@ for i in "$@" ; do
     fi
 done
 
+export DYLD_LIBRARY_PATH=$DD4HEP_LIBRARY_PATH
 echo " #### LD_LIBRARY_PATH = :  ${LD_LIBRARY_PATH}"
+echo " #### DYLD_LIBRARY_PATH = :  ${DYLD_LIBRARY_PATH}"
 
 echo " ### running test :  '${command} ${theargs}'"
 exec ${command} ${theargs}
diff --git a/cmake/thisdd4hep.sh b/cmake/thisdd4hep.sh
index 31d857d4a..b3c4fc499 100644
--- a/cmake/thisdd4hep.sh
+++ b/cmake/thisdd4hep.sh
@@ -10,21 +10,22 @@
 #
 #-----------------------------------------------------------------------------
 dd4hep_parse_this()   {
-    package=${2};
-    if [ "x${1}" = "x" ]; then
-	if [ ! -f bin/this${package}.sh ]; then
-            echo ERROR: must "cd where/${package}/is" before calling ". bin/this${package}.sh" for this version of bash!;
-            return 1;
-	fi
-	THIS="${PWD}";
+    SOURCE=${1}
+    if [ "x${SOURCE}" = "x" ]; then
+        if [ -f bin/thisdd4hep.sh ]; then
+            THIS="$PWD"; export THIS
+        elif [ -f ./thisdd4hep.sh ]; then
+            THIS=$(cd ..  > /dev/null; pwd); export THIS
+        else
+            echo ERROR: must "cd where/dd4hep/is" before calling ". bin/thisdd4hep.sh" for this version of bash!
+            THIS=; export THIS
+            return 1
+        fi
     else
-	# get param to "."
-	THIS=$(dirname $(dirname ${1}));
-	#if [ ! -f ${THIS}/bin/this${package}.sh ]; then
-	#    THIS=$(dirname ${package});
-	#fi;
-    fi;
-    THIS=$(cd ${THIS} > /dev/null; pwd);
+        # get param to "."
+        thisroot=$(dirname ${SOURCE})
+        THIS=$(cd ${thisroot}/.. > /dev/null;pwd); export THIS
+    fi
 }
 #-----------------------------------------------------------------------------
 dd4hep_add_path()   {
@@ -61,7 +62,12 @@ dd4hep_add_library_path()    {
 }
 #-----------------------------------------------------------------------------
 #
-dd4hep_parse_this ${BASH_ARGV[0]} DD4hep;
+SOURCE=${BASH_ARGV[0]}
+if [ "x$SOURCE" = "x" ]; then
+    SOURCE=${(%):-%N} # for zsh
+fi
+
+dd4hep_parse_this $SOURCE;
 #
 # These 3 are the main configuration variables: ROOT, Geant4 and XercesC
 # --> LCIO & Co. are handled elsewhere!
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index ca50396e8..221f053fb 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -33,9 +33,12 @@ SET( ENV{DD4hepExamplesINSTALL} ${CMAKE_INSTALL_PREFIX} )
 IF(NOT TARGET DD4hep::DDCore)
   find_package ( DD4hep REQUIRED )
 ENDIF()
+include(DD4hepMacros) 
 
+dd4hep_set_compiler_flags()
 dd4hep_configure_output()
 
+
 #==========================================================================
 
 SET(DD4HEP_EXAMPLES "AlignDet CLICSiD ClientTests Conditions DDCMS DDCodex DDDB DDDigi DDG4 DDG4_MySensDet LHeD OpticalSurfaces Persistency DDCAD SimpleDetector"
@@ -46,3 +49,19 @@ FOREACH(DDExample IN LISTS DD4HEP_EXAMPLES)
   dd4hep_print("|> Building ${DDExample}")
   add_subdirectory(${DDExample})
 ENDFOREACH()
+
+if(APPLE)
+  SET ( ENV{DD4HEP_LIBRARY_PATH} $ENV{DYLD_LIBRARY_PATH} )
+else()
+  SET ( ENV{DD4HEP_LIBRARY_PATH} ${LD_LIBRARY_PATH} )
+  SET ( DD4HEP_LIBRARY_PATH      ${LD_LIBRARY_PATH} )
+endif()
+
+#######################
+# Treatment for Apple #
+#######################
+
+if(APPLE)
+  fill_dd4hep_library_path()
+  message(STATUS "DD4HEP_LIBRARY_PATH= $ENV{DD4HEP_LIBRARY_PATH}")
+endif()
-- 
GitLab