Skip to content

Commit 892e89c

Browse files
committed
[CMake] Set rpath for individual targets and not globally
When using the `rpath=ON` option, the global CMake variable `CMAKE_INSTALL_RPATH` is mutated, which is problematic in several corner cases. This commit suggests to append the rpaths at the target property level, and also set the rpaths for the build tree separately so they make sense. A new ROOT macro is introduced for this rpath setting, which are also used for the PyROOT libraries, which also need custom rpaths. Closes #19327 and #19134.
1 parent eb3a01b commit 892e89c

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

cmake/modules/RootBuildOptions.cmake

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,6 @@ include(RootInstallDirs)
427427
# add to RPATH any directories outside the project that are in the linker search path
428428
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
429429

430-
if(rpath)
431-
file(RELATIVE_PATH BINDIR_TO_LIBDIR "${CMAKE_INSTALL_FULL_BINDIR}" "${CMAKE_INSTALL_FULL_LIBDIR}")
432-
433-
if(APPLE)
434-
set(CMAKE_MACOSX_RPATH TRUE)
435-
set(CMAKE_INSTALL_NAME_DIR "@rpath")
436-
437-
set(_rpath_values "@loader_path" "@loader_path/${BINDIR_TO_LIBDIR}")
438-
else()
439-
set(_rpath_values "$ORIGIN" "$ORIGIN/${BINDIR_TO_LIBDIR}")
440-
endif()
441-
442-
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH-CACHED};${_rpath_values}" CACHE STRING "Install RPATH" FORCE)
443-
444-
unset(BINDIR_TO_LIBDIR)
445-
endif()
446-
447430
#---deal with the DCMAKE_IGNORE_PATH------------------------------------------------------------
448431
if(macos_native)
449432
if(APPLE)

cmake/modules/RootMacros.cmake

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,9 @@ function(ROOT_LINKER_LIBRARY library)
10281028

10291029
#----Installation details-------------------------------------------------------
10301030
if(NOT ARG_TEST AND NOT ARG_NOINSTALL AND CMAKE_LIBRARY_OUTPUT_DIRECTORY)
1031+
if(rpath)
1032+
ROOT_APPEND_LIBDIR_TO_INSTALL_RPATH(${library} ${CMAKE_INSTALL_LIBDIR})
1033+
endif()
10311034
if(ARG_CMAKENOEXPORT)
10321035
install(TARGETS ${library} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT libraries
10331036
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries
@@ -1515,6 +1518,9 @@ function(ROOT_EXECUTABLE executable)
15151518
endif()
15161519
#----Installation details------------------------------------------------------
15171520
if(NOT ARG_NOINSTALL AND CMAKE_RUNTIME_OUTPUT_DIRECTORY)
1521+
if(rpath)
1522+
ROOT_APPEND_LIBDIR_TO_INSTALL_RPATH(${executable} ${CMAKE_INSTALL_BINDIR})
1523+
endif()
15181524
if(ARG_CMAKENOEXPORT)
15191525
install(TARGETS ${executable} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT applications)
15201526
else()
@@ -2150,3 +2156,28 @@ function(generateManual name input output)
21502156

21512157
install(FILES ${output} DESTINATION ${CMAKE_INSTALL_MANDIR}/man1)
21522158
endfunction()
2159+
2160+
#----------------------------------------------------------------------------
2161+
# --- ROOT_APPEND_LIBDIR_TO_INSTALL_RPATH(target install_dir)
2162+
#
2163+
# Sets the INSTALL_RPATH for a given target so that it can find the ROOT shared
2164+
# libraries at runtime. The RPATH is set relative to the target's own location
2165+
# using $ORIGIN (or @loader_path on macOS).
2166+
#
2167+
# Arguments:
2168+
# target - The CMake target (e.g., a shared library or executable)
2169+
# install_dir - The install subdirectory relative to CMAKE_INSTALL_PREFIX
2170+
#----------------------------------------------------------------------------
2171+
function(ROOT_APPEND_LIBDIR_TO_INSTALL_RPATH target install_dir)
2172+
file(RELATIVE_PATH to_libdir "${CMAKE_INSTALL_PREFIX}/${install_dir}" "${CMAKE_INSTALL_FULL_LIBDIR}")
2173+
2174+
# New path
2175+
if(APPLE)
2176+
set(new_rpath "@loader_path/${to_libdir}")
2177+
else()
2178+
set(new_rpath "$ORIGIN/${to_libdir}")
2179+
endif()
2180+
2181+
# Append to existing RPATH
2182+
set_property(TARGET ${target} APPEND PROPERTY INSTALL_RPATH "${new_rpath}")
2183+
endfunction()

0 commit comments

Comments
 (0)