diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index 86dc1258e8..50a65f4d78 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -35,6 +35,18 @@ class Keg change_install_name(old_name, new_name, file) if new_name end + + if ENV["HOMEBREW_RELOCATE_RPATHS"] + each_rpath_for(file) do |old_name| + new_name = if old_name.start_with? relocation.old_cellar + old_name.sub(relocation.old_cellar, relocation.new_cellar) + elsif old_name.start_with? relocation.old_prefix + old_name.sub(relocation.old_prefix, relocation.new_prefix) + end + + change_rpath(old_name, new_name, file) if new_name + end + end end end end @@ -111,6 +123,12 @@ class Keg dylibs.each(&block) end + def each_rpath_for(file, &block) + rpaths = file.rpaths + .reject { |fn| fn =~ /^@(loader|executable)_path/ } + rpaths.each(&block) + end + def dylib_id_for(file) # The new dylib ID should have the same basename as the old dylib ID, not # the basename of the file itself. diff --git a/Library/Homebrew/os/mac/keg.rb b/Library/Homebrew/os/mac/keg.rb index 901c4dc7ee..72a3b85908 100644 --- a/Library/Homebrew/os/mac/keg.rb +++ b/Library/Homebrew/os/mac/keg.rb @@ -34,6 +34,22 @@ class Keg raise end + def change_rpath(old, new, file) + return if old == new + + @require_relocation = true + odebug "Changing rpath in #{file}\n from #{old}\n to #{new}" + MachO::Tools.change_rpath(file, old, new, strict: false) + apply_ad_hoc_signature(file) + rescue MachO::MachOError + onoe <<~EOS + Failed changing rpath in #{file} + from #{old} + to #{new} + EOS + raise + end + def apply_ad_hoc_signature(file) return if MacOS.version < :big_sur return unless Hardware::CPU.arm?