extend/os/mac/keg_relocate: fix duplicate RPATH handling

ruby-macho chokes on changing duplicate RPATHs, so we need to strip the
duplicates before trying to relocate them.

This continues #11405. We need this to unblock
Homebrew/homebrew-core#91224.

While we're here, let's get rid of `HOMEBREW_RELOCATE_RPATHS`. We've
been using it for nearly a year with essentially no problems (barring
`pdnsrec`), so I think it is safe to do unconditionally.
This commit is contained in:
Carlo Cabrera 2022-01-18 15:16:01 +08:00
parent 4d0b93c3cb
commit 57fae524de
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0
2 changed files with 18 additions and 14 deletions

View File

@ -31,11 +31,9 @@ class Keg
change_install_name(old_name, new_name, file) if new_name
end
if ENV["HOMEBREW_RELOCATE_RPATHS"]
each_linkage_for(file, :rpaths) do |old_name|
new_name = relocated_name_for(old_name, relocation)
change_rpath(old_name, new_name, file) if new_name
end
each_linkage_for(file, :rpaths) do |old_name|
new_name = relocated_name_for(old_name, relocation)
change_rpath(old_name, new_name, file) if new_name
end
end
end
@ -56,11 +54,21 @@ class Keg
change_install_name(bad_name, new_name, file) unless new_name == bad_name
end
each_linkage_for(file, :rpaths) do |bad_name|
# Strip 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)
# Count duplicate rpaths. We need to keep track of this ourselves
# because the MachO data is cached and this cache is not updated
# after modification with #delete_rpath.
rpath_dupe_count = Hash.new { |h, k| h[k] = -1 }
file.rpaths.each do |rpath|
rpath_dupe_count[rpath] += 1
end
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_dupe_count[bad_name] <= 0)
rpath_dupe_count[bad_name] -= 1
delete_rpath(bad_name, file)
end
end

View File

@ -1229,11 +1229,7 @@ class FormulaInstaller
keg = Keg.new(formula.prefix)
skip_linkage = formula.bottle_specification.skip_relocation?
# TODO: Remove `with_env` when bottles are built with RPATH relocation enabled
# https://github.com/Homebrew/brew/issues/11329
with_env(HOMEBREW_RELOCATE_RPATHS: "1") do
keg.replace_placeholders_with_locations tab.changed_files, skip_linkage: skip_linkage
end
keg.replace_placeholders_with_locations tab.changed_files, skip_linkage: skip_linkage
end
sig { params(output: T.nilable(String)).void }