Merge pull request #12870 from Homebrew/revert-12534-patch_prefix
Revert "Binary patching of build prefixes"
This commit is contained in:
		
						commit
						d603fd6426
					
				@ -183,14 +183,4 @@ class Keg
 | 
				
			|||||||
    # it's wrong. -O is a BSD-grep-only option.
 | 
					    # it's wrong. -O is a BSD-grep-only option.
 | 
				
			||||||
    "-lrO"
 | 
					    "-lrO"
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					 | 
				
			||||||
  def egrep_args
 | 
					 | 
				
			||||||
    grep_bin = "egrep"
 | 
					 | 
				
			||||||
    grep_args = recursive_fgrep_args
 | 
					 | 
				
			||||||
    [grep_bin, grep_args]
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def codesign_patched_binary(binary_file)
 | 
					 | 
				
			||||||
    apply_ad_hoc_signature(binary_file)
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 | 
				
			|||||||
@ -1230,10 +1230,6 @@ class FormulaInstaller
 | 
				
			|||||||
    keg = Keg.new(formula.prefix)
 | 
					    keg = Keg.new(formula.prefix)
 | 
				
			||||||
    skip_linkage = formula.bottle_specification.skip_relocation?
 | 
					    skip_linkage = formula.bottle_specification.skip_relocation?
 | 
				
			||||||
    keg.replace_placeholders_with_locations tab.changed_files, skip_linkage: skip_linkage
 | 
					    keg.replace_placeholders_with_locations tab.changed_files, skip_linkage: skip_linkage
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return if formula.bottle_specification.skip_prefix_relocation?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    keg.relocate_build_prefix(keg, Utils::Bottles.tag.default_prefix, HOMEBREW_PREFIX)
 | 
					 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  sig { params(output: T.nilable(String)).void }
 | 
					  sig { params(output: T.nilable(String)).void }
 | 
				
			||||||
 | 
				
			|||||||
@ -8,7 +8,6 @@ class Keg
 | 
				
			|||||||
  LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@"
 | 
					  LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@"
 | 
				
			||||||
  PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@"
 | 
					  PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@"
 | 
				
			||||||
  JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@"
 | 
					  JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@"
 | 
				
			||||||
  BINARY_NULL_CHARACTER = "\x00"
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  class Relocation
 | 
					  class Relocation
 | 
				
			||||||
    extend T::Sig
 | 
					    extend T::Sig
 | 
				
			||||||
@ -164,49 +163,6 @@ class Keg
 | 
				
			|||||||
    changed_files
 | 
					    changed_files
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def relocate_build_prefix(keg, old_prefix, new_prefix)
 | 
					 | 
				
			||||||
    # Find binaries which match prefix strings.
 | 
					 | 
				
			||||||
    string_matches = Set.new
 | 
					 | 
				
			||||||
    keg.each_unique_file_matching(old_prefix) do |file|
 | 
					 | 
				
			||||||
      string_matches << file
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    binary_string_matches = Set.new
 | 
					 | 
				
			||||||
    keg.each_unique_binary_file do |file|
 | 
					 | 
				
			||||||
      binary_string_matches << file if string_matches.include?(file)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Only consider string matches which are binary files with null bytes, and remove any
 | 
					 | 
				
			||||||
    # matches which are sharballs found by text_files.
 | 
					 | 
				
			||||||
    binary_string_matches -= text_files
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Split binary by null characters into array and substitute new cellar for old cellar.
 | 
					 | 
				
			||||||
    # Null padding is added if the new string is too short.
 | 
					 | 
				
			||||||
    binary_string_matches.each do |binary_file|
 | 
					 | 
				
			||||||
      binary_file.ensure_writable do
 | 
					 | 
				
			||||||
        binary = File.binread binary_file
 | 
					 | 
				
			||||||
        puts "Replacing build prefix in: #{binary_file}"
 | 
					 | 
				
			||||||
        binary_strings = binary.split(BINARY_NULL_CHARACTER)
 | 
					 | 
				
			||||||
        match_indices = binary_strings.each_index.select { |i| binary_strings[i].include?(old_prefix) }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Only perform substitution on strings which match prefix regex.
 | 
					 | 
				
			||||||
        match_indices.each do |i|
 | 
					 | 
				
			||||||
          s = binary_strings[i]
 | 
					 | 
				
			||||||
          binary_strings[i] = s.gsub(old_prefix, new_prefix).ljust(s.size, BINARY_NULL_CHARACTER)
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        # Add back null padding at the end of the binary if needed.
 | 
					 | 
				
			||||||
        patched_binary = binary_strings.join(BINARY_NULL_CHARACTER).ljust(binary.size, BINARY_NULL_CHARACTER)
 | 
					 | 
				
			||||||
        if patched_binary.size != binary.size
 | 
					 | 
				
			||||||
          raise "Patching failed!  Original and patched binary sizes do not match."
 | 
					 | 
				
			||||||
        end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        binary_file.atomic_write patched_binary
 | 
					 | 
				
			||||||
      end
 | 
					 | 
				
			||||||
      codesign_patched_binary(binary_file)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def detect_cxx_stdlibs(_options = {})
 | 
					  def detect_cxx_stdlibs(_options = {})
 | 
				
			||||||
    []
 | 
					    []
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
@ -217,47 +173,19 @@ class Keg
 | 
				
			|||||||
  end
 | 
					  end
 | 
				
			||||||
  alias generic_recursive_fgrep_args recursive_fgrep_args
 | 
					  alias generic_recursive_fgrep_args recursive_fgrep_args
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def egrep_args
 | 
					 | 
				
			||||||
    grep_bin = "grep"
 | 
					 | 
				
			||||||
    grep_args = recursive_fgrep_args
 | 
					 | 
				
			||||||
    grep_args += "Pa"
 | 
					 | 
				
			||||||
    [grep_bin, grep_args]
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
  alias generic_egrep_args egrep_args
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def each_unique_file(io)
 | 
					 | 
				
			||||||
    hardlinks = Set.new
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    until io.eof?
 | 
					 | 
				
			||||||
      file = Pathname.new(io.readline.chomp)
 | 
					 | 
				
			||||||
      # Don't yield symlinks
 | 
					 | 
				
			||||||
      next if file.symlink?
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      # Only yield a file if it has a unique inode.
 | 
					 | 
				
			||||||
      # This makes sure we don't yield hardlinks.
 | 
					 | 
				
			||||||
      yield file if hardlinks.add? file.stat.ino
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def each_unique_file_matching(string)
 | 
					  def each_unique_file_matching(string)
 | 
				
			||||||
    Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
 | 
					    Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
 | 
				
			||||||
      each_unique_file(io)
 | 
					      hardlinks = Set.new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      until io.eof?
 | 
				
			||||||
 | 
					        file = Pathname.new(io.readline.chomp)
 | 
				
			||||||
 | 
					        next if file.symlink?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        yield file if hardlinks.add? file.stat.ino
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def each_unique_binary_file
 | 
					 | 
				
			||||||
    grep_bin, grep_args = egrep_args
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # An extra \ is needed for the null character when calling grep
 | 
					 | 
				
			||||||
    Utils.popen_read(grep_bin, grep_args, "\#{BINARY_NULL_CHARACTER}", to_s) do |io|
 | 
					 | 
				
			||||||
      each_unique_file(io)
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def codesign_patched_binary(_binary_file)
 | 
					 | 
				
			||||||
    []
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  def lib
 | 
					  def lib
 | 
				
			||||||
    path/"lib"
 | 
					    path/"lib"
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
				
			|||||||
@ -347,11 +347,6 @@ class Bottle
 | 
				
			|||||||
    @spec.compatible_locations?(tag: @tag)
 | 
					    @spec.compatible_locations?(tag: @tag)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  # Should the build prefix be relocated?
 | 
					 | 
				
			||||||
  def skip_prefix_relocation?
 | 
					 | 
				
			||||||
    @spec.skip_prefix_relocation?(tag: @tag)
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  # Does the bottle need to be relocated?
 | 
					  # Does the bottle need to be relocated?
 | 
				
			||||||
  def skip_relocation?
 | 
					  def skip_relocation?
 | 
				
			||||||
    @spec.skip_relocation?(tag: @tag)
 | 
					    @spec.skip_relocation?(tag: @tag)
 | 
				
			||||||
@ -478,8 +473,6 @@ class Bottle
 | 
				
			|||||||
end
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class BottleSpecification
 | 
					class BottleSpecification
 | 
				
			||||||
  RELOCATABLE_CELLARS = [:any, :any_skip_relocation].freeze
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  extend T::Sig
 | 
					  extend T::Sig
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  attr_rw :rebuild
 | 
					  attr_rw :rebuild
 | 
				
			||||||
@ -520,30 +513,7 @@ class BottleSpecification
 | 
				
			|||||||
      tag.default_cellar
 | 
					      tag.default_cellar
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return true if RELOCATABLE_CELLARS.include?(cellar)
 | 
					    return true if [:any, :any_skip_relocation].include?(cellar)
 | 
				
			||||||
 | 
					 | 
				
			||||||
    prefix = Pathname(cellar).parent.to_s
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    cellar_relocatable = cellar.size >= HOMEBREW_CELLAR.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"]
 | 
					 | 
				
			||||||
    prefix_relocatable = prefix.size >= HOMEBREW_PREFIX.to_s.size && ENV["HOMEBREW_RELOCATE_BUILD_PREFIX"]
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    compatible_cellar = cellar == HOMEBREW_CELLAR.to_s || cellar_relocatable
 | 
					 | 
				
			||||||
    compatible_prefix = prefix == HOMEBREW_PREFIX.to_s || prefix_relocatable
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    compatible_cellar && compatible_prefix
 | 
					 | 
				
			||||||
  end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  # Should the build prefix for the {Bottle} this {BottleSpecification} belongs to be relocated?
 | 
					 | 
				
			||||||
  sig { params(tag: Utils::Bottles::Tag).returns(T::Boolean) }
 | 
					 | 
				
			||||||
  def skip_prefix_relocation?(tag: Utils::Bottles.tag)
 | 
					 | 
				
			||||||
    spec = collector.specification_for(tag)
 | 
					 | 
				
			||||||
    cellar = if spec.present?
 | 
					 | 
				
			||||||
      spec.cellar
 | 
					 | 
				
			||||||
    else
 | 
					 | 
				
			||||||
      tag.default_cellar
 | 
					 | 
				
			||||||
    end
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return true if RELOCATABLE_CELLARS.include?(cellar)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    prefix = Pathname(cellar).parent.to_s
 | 
					    prefix = Pathname(cellar).parent.to_s
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user