@@ -208,6 +208,62 @@ function(_bd_ensure_sonames os name install_dir)
208208 endforeach ()
209209endfunction ()
210210
211+ # Patch RPATH so a dep point to its sibling libraries
212+ function (_bd_relocatable_elf os name install_dir depends_prefixes )
213+ if (os STREQUAL "windows" OR os STREQUAL "macos" )
214+ return ()
215+ endif ()
216+
217+ if (NOT EXISTS "${install_dir} /lib" )
218+ return ()
219+ endif ()
220+
221+ file (GLOB _shared_libs "${install_dir} /lib/*.so" "${install_dir} /lib/*.so.*" )
222+ if (NOT _shared_libs)
223+ return ()
224+ endif ()
225+
226+ find_program (PATCHELF NAMES patchelf )
227+ if (NOT PATCHELF)
228+ message (FATAL_ERROR "[${name} ] patchelf not found" )
229+ endif ()
230+
231+ get_filename_component (_install_lib "${install_dir} /lib" ABSOLUTE )
232+ set (_rpath_entries "\$ ORIGIN" )
233+ foreach (_prefix ${depends_prefixes} )
234+ if (EXISTS "${_prefix} /lib" )
235+ get_filename_component (_dep_lib "${_prefix} /lib" ABSOLUTE )
236+ file (RELATIVE_PATH _rel "${_install_lib} " "${_dep_lib} " )
237+ list (APPEND _rpath_entries "\$ ORIGIN/${_rel} " )
238+ endif ()
239+ endforeach ()
240+
241+ list (REMOVE_DUPLICATES _rpath_entries)
242+ list (JOIN _rpath_entries ":" _rpath)
243+
244+ foreach (_shared_lib ${_shared_libs} )
245+ if (IS_SYMLINK "${_shared_lib} " )
246+ continue ()
247+ endif ()
248+
249+ execute_process (COMMAND "${PATCHELF} " --print-rpath "${_shared_lib} "
250+ OUTPUT_VARIABLE _current_rpath OUTPUT_STRIP_TRAILING_WHITESPACE
251+ ERROR_QUIET RESULT_VARIABLE _result )
252+ if (NOT _result EQUAL 0)
253+ continue ()
254+ endif ()
255+ if (_current_rpath STREQUAL _rpath)
256+ continue ()
257+ endif ()
258+
259+ execute_process (COMMAND "${PATCHELF} " --set-rpath "${_rpath} " "${_shared_lib} "
260+ RESULT_VARIABLE _result )
261+ if (NOT _result EQUAL 0)
262+ message (FATAL_ERROR "[${name} ] patchelf failed for ${_shared_lib} " )
263+ endif ()
264+ endforeach ()
265+ endfunction ()
266+
211267# macOS: make installed dylibs relocatable.
212268function (_bd_relocatable_macos os install_dir )
213269 if (NOT os STREQUAL "macos" )
@@ -290,6 +346,7 @@ function(build_dep)
290346 if (_prev_sig STREQUAL "${_build_sig} " )
291347 message (STATUS "[${BD_NAME} ] up-to-date (recipe unchanged), skipping build" )
292348 _bd_ensure_sonames ("${BD_OS} " "${BD_NAME} " "${BD_INSTALL_DIR} " )
349+ _bd_relocatable_elf ("${BD_OS} " "${BD_NAME} " "${BD_INSTALL_DIR} " "${BD_DEPENDS_PREFIXES} " )
293350 _bd_relocatable_macos ("${BD_OS} " "${BD_INSTALL_DIR} " )
294351 return ()
295352 endif ()
@@ -388,6 +445,7 @@ function(build_dep)
388445
389446 # Fix-up installed libraries
390447 _bd_ensure_sonames ("${BD_OS} " "${BD_NAME} " "${INSTALL} " )
448+ _bd_relocatable_elf ("${BD_OS} " "${BD_NAME} " "${INSTALL} " "${BD_DEPENDS_PREFIXES} " )
391449 _bd_relocatable_macos ("${BD_OS} " "${INSTALL} " )
392450
393451 # Record the build signature
0 commit comments