mach: Avoid reopening the file for relocation
This commit allows the relocation code to perform install name and dylib ID changes without reopening the file separately.
This commit is contained in:
		
							parent
							
								
									551e5dd945
								
							
						
					
					
						commit
						d618e574fb
					
				@ -2,7 +2,10 @@ class Keg
 | 
			
		||||
  def fix_dynamic_linkage
 | 
			
		||||
    mach_o_files.each do |file|
 | 
			
		||||
      file.ensure_writable do
 | 
			
		||||
        change_dylib_id(dylib_id_for(file), file) if file.dylib?
 | 
			
		||||
        if file.dylib?
 | 
			
		||||
          @require_relocation = true
 | 
			
		||||
          file.change_dylib_id(dylib_id_for(file))
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        each_install_name_for(file) do |bad_name|
 | 
			
		||||
          # Don't fix absolute paths unless they are rooted in the build directory
 | 
			
		||||
@ -11,7 +14,9 @@ class Keg
 | 
			
		||||
                  !bad_name.start_with?(HOMEBREW_TEMP.realpath.to_s)
 | 
			
		||||
 | 
			
		||||
          new_name = fixed_name(file, bad_name)
 | 
			
		||||
          change_install_name(bad_name, new_name, file) unless new_name == bad_name
 | 
			
		||||
 | 
			
		||||
          @require_relocation = true
 | 
			
		||||
          file.change_install_name(bad_name, new_name, file)
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
@ -23,8 +28,9 @@ class Keg
 | 
			
		||||
    mach_o_files.each do |file|
 | 
			
		||||
      file.ensure_writable do
 | 
			
		||||
        if file.dylib?
 | 
			
		||||
          @require_relocation = true
 | 
			
		||||
          id = dylib_id_for(file).sub(relocation.old_prefix, relocation.new_prefix)
 | 
			
		||||
          change_dylib_id(id, file)
 | 
			
		||||
          file.change_dylib_id(id)
 | 
			
		||||
        end
 | 
			
		||||
 | 
			
		||||
        each_install_name_for(file) do |old_name|
 | 
			
		||||
@ -34,7 +40,8 @@ class Keg
 | 
			
		||||
            new_name = old_name.sub(relocation.old_prefix, relocation.new_prefix)
 | 
			
		||||
          end
 | 
			
		||||
 | 
			
		||||
          change_install_name(old_name, new_name, file) if new_name
 | 
			
		||||
          @require_relocation = true
 | 
			
		||||
          file.change_install_name(old_name, new_name) if new_name
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,6 @@ require "os/mac/xcode"
 | 
			
		||||
require "os/mac/xquartz"
 | 
			
		||||
require "os/mac/pathname"
 | 
			
		||||
require "os/mac/sdk"
 | 
			
		||||
require "os/mac/keg"
 | 
			
		||||
 | 
			
		||||
module OS
 | 
			
		||||
  module Mac
 | 
			
		||||
 | 
			
		||||
@ -1,29 +0,0 @@
 | 
			
		||||
class Keg
 | 
			
		||||
  def change_dylib_id(id, file)
 | 
			
		||||
    return if file.dylib_id == id
 | 
			
		||||
    @require_relocation = true
 | 
			
		||||
    puts "Changing dylib ID of #{file}\n  from #{file.dylib_id}\n    to #{id}" if ARGV.debug?
 | 
			
		||||
    MachO::Tools.change_dylib_id(file, id, strict: false)
 | 
			
		||||
  rescue MachO::MachOError
 | 
			
		||||
    onoe <<-EOS.undent
 | 
			
		||||
      Failed changing dylib ID of #{file}
 | 
			
		||||
        from #{file.dylib_id}
 | 
			
		||||
          to #{id}
 | 
			
		||||
    EOS
 | 
			
		||||
    raise
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def change_install_name(old, new, file)
 | 
			
		||||
    return if old == new
 | 
			
		||||
    @require_relocation = true
 | 
			
		||||
    puts "Changing install name in #{file}\n  from #{old}\n    to #{new}" if ARGV.debug?
 | 
			
		||||
    MachO::Tools.change_install_name(file, old, new, strict: false)
 | 
			
		||||
  rescue MachO::MachOError
 | 
			
		||||
    onoe <<-EOS.undent
 | 
			
		||||
      Failed changing install name in #{file}
 | 
			
		||||
        from #{old}
 | 
			
		||||
          to #{new}
 | 
			
		||||
    EOS
 | 
			
		||||
    raise
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@ -61,6 +61,34 @@ module MachOShim
 | 
			
		||||
    macho.dylib_id
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def change_dylib_id(id)
 | 
			
		||||
    return if dylib_id == id
 | 
			
		||||
    puts "Changing dylib ID of #{self}\n  from #{dylib_id}\n    to #{id}" if ARGV.debug?
 | 
			
		||||
    macho.change_dylib_id(id, strict: false)
 | 
			
		||||
    macho.write!
 | 
			
		||||
  rescue MachO::MachOError
 | 
			
		||||
    odie <<-EOS.undent
 | 
			
		||||
      Failed changing dylib ID of #{self}
 | 
			
		||||
        from #{file.dylib_id}
 | 
			
		||||
          to #{id}
 | 
			
		||||
    EOS
 | 
			
		||||
    raise
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def change_install_name(old, new)
 | 
			
		||||
    return if old == new
 | 
			
		||||
    puts "Changing install name in #{self}\n  from #{old}\n    to #{new}" if ARGV.debug?
 | 
			
		||||
    macho.change_install_name(old, new, strict: false)
 | 
			
		||||
    macho.write!
 | 
			
		||||
  rescue MachO::MachOError
 | 
			
		||||
    odie <<-EOS.undent
 | 
			
		||||
      Failed changing install name in #{self}
 | 
			
		||||
        from #{old}
 | 
			
		||||
          to #{new}
 | 
			
		||||
    EOS
 | 
			
		||||
    raise
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def archs
 | 
			
		||||
    mach_data.map { |m| m.fetch :arch }.extend(ArchitectureListExtension)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user