Merge pull request #11358 from carlocab/keg-relocate-refactor

extend/os/mac/keg_relocate: refactor `relocate_dynamic_linkage`
This commit is contained in:
Carlo Cabrera 2021-05-11 19:22:45 +01:00 committed by GitHub
commit ef000d0c3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,34 +19,21 @@ class Keg
undef relocate_dynamic_linkage undef relocate_dynamic_linkage
def relocate_dynamic_linkage(relocation) def relocate_dynamic_linkage(relocation)
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
mach_o_files.each do |file| mach_o_files.each do |file|
file.ensure_writable do file.ensure_writable do
if file.dylib? if file.dylib?
id = dylib_id_for(file).sub(old_prefix, new_prefix) id = relocated_name_for(dylib_id_for(file), relocation)
change_dylib_id(id, file) change_dylib_id(id, file) if id
end end
each_install_name_for(file) do |old_name| each_install_name_for(file) do |old_name|
if old_name.start_with? old_cellar new_name = relocated_name_for(old_name, relocation)
new_name = old_name.sub(old_cellar, new_cellar)
elsif old_name.start_with? old_prefix
new_name = old_name.sub(old_prefix, new_prefix)
end
change_install_name(old_name, new_name, file) if new_name change_install_name(old_name, new_name, file) if new_name
end end
if ENV["HOMEBREW_RELOCATE_RPATHS"] if ENV["HOMEBREW_RELOCATE_RPATHS"]
each_rpath_for(file) do |old_name| each_rpath_for(file) do |old_name|
new_name = if old_name.start_with? old_cellar new_name = relocated_name_for(old_name, relocation)
old_name.sub(old_cellar, new_cellar)
elsif old_name.start_with? old_prefix
old_name.sub(old_prefix, new_prefix)
end
change_rpath(old_name, new_name, file) if new_name change_rpath(old_name, new_name, file) if new_name
end end
end end
@ -140,6 +127,17 @@ class Keg
(opt_record/relative_dirname/basename).to_s (opt_record/relative_dirname/basename).to_s
end end
def relocated_name_for(old_name, relocation)
old_prefix, new_prefix = relocation.replacement_pair_for(:prefix)
old_cellar, new_cellar = relocation.replacement_pair_for(:cellar)
if old_name.start_with? old_cellar
old_name.sub(old_cellar, new_cellar)
elsif old_name.start_with? old_prefix
old_name.sub(old_prefix, new_prefix)
end
end
# Matches framework references like `XXX.framework/Versions/YYY/XXX` and # Matches framework references like `XXX.framework/Versions/YYY/XXX` and
# `XXX.framework/XXX`, both with or without a slash-delimited prefix. # `XXX.framework/XXX`, both with or without a slash-delimited prefix.
FRAMEWORK_RX = %r{(?:^|/)(([^/]+)\.framework/(?:Versions/[^/]+/)?\2)$}.freeze FRAMEWORK_RX = %r{(?:^|/)(([^/]+)\.framework/(?:Versions/[^/]+/)?\2)$}.freeze