From 4d5971518de4a6cc8307d5c31567bca35072b5b6 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 5 May 2021 21:37:02 +0100 Subject: [PATCH 1/3] os/mac/keg: add change_rpath method --- Library/Homebrew/os/mac/keg.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) 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? From 8f1cd1288d1bfdd1a48470a8671f7ca940d40426 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 5 May 2021 23:43:43 +0100 Subject: [PATCH 2/3] extend/os/mac/keg_relocate: relocate rpaths on macOS --- Library/Homebrew/extend/os/mac/keg_relocate.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index 86dc1258e8..f7be065782 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| + if old_name.start_with? relocation.old_cellar + new_name = old_name.sub(relocation.old_cellar, relocation.new_cellar) + elsif old_name.start_with? relocation.old_prefix + new_name = 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 + 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. From dec7b973427e5778434078706fad9d5f44d34dfd Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Thu, 6 May 2021 11:52:18 +0100 Subject: [PATCH 3/3] keg_relocate: style improvements Co-authored-by: Mike McQuaid --- Library/Homebrew/extend/os/mac/keg_relocate.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index f7be065782..50a65f4d78 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -38,10 +38,10 @@ class Keg if ENV["HOMEBREW_RELOCATE_RPATHS"] each_rpath_for(file) do |old_name| - if old_name.start_with? relocation.old_cellar - new_name = old_name.sub(relocation.old_cellar, relocation.new_cellar) + 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 - new_name = old_name.sub(relocation.old_prefix, relocation.new_prefix) + old_name.sub(relocation.old_prefix, relocation.new_prefix) end change_rpath(old_name, new_name, file) if new_name @@ -125,7 +125,7 @@ class Keg def each_rpath_for(file, &block) rpaths = file.rpaths - rpaths.reject! { |fn| fn =~ /^@(loader|executable)_path/ } + .reject { |fn| fn =~ /^@(loader|executable)_path/ } rpaths.each(&block) end