Skip to content
Snippets Groups Projects
Commit fab2cffe authored by Josh Hagins's avatar Josh Hagins
Browse files

keg_relocate: wrap relocation locations in struct

parent adc4b1f0
No related branches found
No related tags found
No related merge requests found
...@@ -17,19 +17,19 @@ class Keg ...@@ -17,19 +17,19 @@ class Keg
generic_fix_dynamic_linkage generic_fix_dynamic_linkage
end end
def relocate_dynamic_linkage(old_prefix, new_prefix, old_cellar, new_cellar) def relocate_dynamic_linkage(relocation)
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 = dylib_id_for(file).sub(relocation.old_prefix, relocation.new_prefix)
change_dylib_id(id, file) change_dylib_id(id, file)
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 if old_name.start_with? relocation.old_cellar
new_name = old_name.sub(old_cellar, new_cellar) new_name = old_name.sub(relocation.old_cellar, relocation.new_cellar)
elsif old_name.start_with? old_prefix elsif old_name.start_with? relocation.old_prefix
new_name = old_name.sub(old_prefix, new_prefix) new_name = old_name.sub(relocation.old_prefix, relocation.new_prefix)
end end
change_install_name(old_name, new_name, file) if new_name change_install_name(old_name, new_name, file) if new_name
......
...@@ -3,6 +3,14 @@ class Keg ...@@ -3,6 +3,14 @@ class Keg
CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@".freeze CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@".freeze
REPOSITORY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@".freeze REPOSITORY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@".freeze
Relocation = Struct.new(:old_prefix, :old_cellar, :old_repository,
:new_prefix, :new_cellar, :new_repository) do
# Use keyword args instead of positional args for initialization
def initialize(**kwargs)
super(*members.map { |k| kwargs[k] })
end
end
def fix_dynamic_linkage def fix_dynamic_linkage
symlink_files.each do |file| symlink_files.each do |file|
link = file.readlink link = file.readlink
...@@ -15,38 +23,49 @@ class Keg ...@@ -15,38 +23,49 @@ class Keg
end end
alias generic_fix_dynamic_linkage fix_dynamic_linkage alias generic_fix_dynamic_linkage fix_dynamic_linkage
def relocate_dynamic_linkage(_old_prefix, _new_prefix, _old_cellar, _new_cellar) def relocate_dynamic_linkage(_relocation)
[] []
end end
def replace_locations_with_placeholders def replace_locations_with_placeholders
relocate_dynamic_linkage(HOMEBREW_PREFIX.to_s, PREFIX_PLACEHOLDER, relocation = Relocation.new(
HOMEBREW_CELLAR.to_s, CELLAR_PLACEHOLDER) old_prefix: HOMEBREW_PREFIX.to_s,
replacements = { old_cellar: HOMEBREW_CELLAR.to_s,
HOMEBREW_PREFIX.to_s => PREFIX_PLACEHOLDER, old_repository: HOMEBREW_REPOSITORY.to_s,
HOMEBREW_CELLAR.to_s => CELLAR_PLACEHOLDER, new_prefix: PREFIX_PLACEHOLDER,
HOMEBREW_REPOSITORY.to_s => REPOSITORY_PLACEHOLDER, new_cellar: CELLAR_PLACEHOLDER,
} new_repository: REPOSITORY_PLACEHOLDER
replace_text_in_files(replacements) )
relocate_dynamic_linkage(relocation)
replace_text_in_files(relocation)
end end
def replace_placeholders_with_locations(files) def replace_placeholders_with_locations(files)
relocate_dynamic_linkage(PREFIX_PLACEHOLDER, HOMEBREW_PREFIX.to_s, relocation = Relocation.new(
CELLAR_PLACEHOLDER, HOMEBREW_CELLAR.to_s) old_prefix: PREFIX_PLACEHOLDER,
replacements = { old_cellar: CELLAR_PLACEHOLDER,
PREFIX_PLACEHOLDER => HOMEBREW_PREFIX.to_s, old_repository: REPOSITORY_PLACEHOLDER,
CELLAR_PLACEHOLDER => HOMEBREW_CELLAR.to_s, new_prefix: HOMEBREW_PREFIX.to_s,
REPOSITORY_PLACEHOLDER => HOMEBREW_REPOSITORY.to_s, new_cellar: HOMEBREW_CELLAR.to_s,
} new_repository: HOMEBREW_REPOSITORY.to_s
replace_text_in_files(replacements, files) )
relocate_dynamic_linkage(relocation)
replace_text_in_files(relocation, files: files)
end end
def replace_text_in_files(replacements, files = nil) def replace_text_in_files(relocation, files: nil)
files ||= text_files | libtool_files files ||= text_files | libtool_files
changed_files = [] changed_files = []
files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest| files.map(&path.method(:join)).group_by { |f| f.stat.ino }.each_value do |first, *rest|
s = first.open("rb", &:read) s = first.open("rb", &:read)
replacements = {
relocation.old_prefix => relocation.new_prefix,
relocation.old_cellar => relocation.new_cellar,
relocation.old_repository => relocation.new_repository,
}
regexp = Regexp.union(replacements.keys) regexp = Regexp.union(replacements.keys)
changed = s.gsub!(regexp, replacements) changed = s.gsub!(regexp, replacements)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment