From 568bc94f3ee66bb62a31ae9d9c7f15de8c60cc92 Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Wed, 19 Jan 2022 00:34:39 +0800 Subject: [PATCH] os/mac/keg: use `MachOFile#delete_rpath` instead of `MachO::Tools` This will allow us to avoid keeping track of the number of `RPATH`s while trying to delete duplicates. See discussion at #12745. --- Library/Homebrew/extend/os/mac/keg_relocate.rb | 10 +--------- Library/Homebrew/os/mac/keg.rb | 2 +- Library/Homebrew/os/mac/mach.rb | 7 +++++++ 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index 25eae0a6cf..282d9be933 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -54,20 +54,12 @@ class Keg change_install_name(bad_name, new_name, file) unless new_name == bad_name end - # Keep track of the rpath counts for deletion of duplicates. - # We need to track this here since we cache the MachO data [0] - # and this cache is not updated after modification with #delete_rpath. - # - # [0] See os/mac/mach.rb. - rpath_count = Hash.new { |h, k| h[k] = file.rpaths.count(k) } - each_linkage_for(file, :rpaths) do |bad_name| # Strip duplicate rpaths and rpaths rooted in the build directory. next if !bad_name.start_with?(HOMEBREW_TEMP.to_s) && !bad_name.start_with?(HOMEBREW_TEMP.realpath.to_s) && - (rpath_count[bad_name] == 1) + (file.rpaths.count(bad_name) == 1) - rpath_count[bad_name] -= 1 delete_rpath(bad_name, file) end end diff --git a/Library/Homebrew/os/mac/keg.rb b/Library/Homebrew/os/mac/keg.rb index 528ff0e28a..ef4395bb66 100644 --- a/Library/Homebrew/os/mac/keg.rb +++ b/Library/Homebrew/os/mac/keg.rb @@ -52,7 +52,7 @@ class Keg def delete_rpath(rpath, file) odebug "Deleting rpath #{rpath} in #{file}" - MachO::Tools.delete_rpath(file, rpath, strict: false) + file.delete_rpath(rpath, strict: false) apply_ad_hoc_signature(file) rescue MachO::MachOError onoe <<~EOS diff --git a/Library/Homebrew/os/mac/mach.rb b/Library/Homebrew/os/mac/mach.rb index 0b4d8a0c63..bd636ffba9 100644 --- a/Library/Homebrew/os/mac/mach.rb +++ b/Library/Homebrew/os/mac/mach.rb @@ -57,6 +57,13 @@ module MachOShim end private :mach_data + # TODO: See if the `#write!` call can be delayed until + # we know we're not making any changes to the rpaths. + def delete_rpath(rpath, **options) + macho.delete_rpath(rpath, options) + macho.write! + end + def dynamically_linked_libraries(except: :none) lcs = macho.dylib_load_commands.reject { |lc| lc.type == except }