From 22fdb11e00872a9f83fda5aeb0f8f0120391c9d5 Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Mon, 18 Aug 2025 15:02:15 +0100 Subject: [PATCH] **/keg_relocate.rb: add set Sorbet `typed: strict` Other files have been changed where required. --- Library/Homebrew/dev-cmd/bottle.rb | 2 +- .../Homebrew/extend/os/linux/keg_relocate.rb | 9 ++- .../Homebrew/extend/os/mac/keg_relocate.rb | 23 ++++++-- Library/Homebrew/formula.rb | 2 +- Library/Homebrew/formula_cellar_checks.rb | 2 +- Library/Homebrew/formula_installer.rb | 2 +- Library/Homebrew/keg_relocate.rb | 58 +++++++++++++------ 7 files changed, 72 insertions(+), 26 deletions(-) diff --git a/Library/Homebrew/dev-cmd/bottle.rb b/Library/Homebrew/dev-cmd/bottle.rb index 662739e91f..28135e3944 100644 --- a/Library/Homebrew/dev-cmd/bottle.rb +++ b/Library/Homebrew/dev-cmd/bottle.rb @@ -590,7 +590,7 @@ module Homebrew ensure ignore_interrupts do original_tab&.write - keg.replace_placeholders_with_locations changed_files unless args.skip_relocation? + keg.replace_placeholders_with_locations(changed_files) if changed_files && !args.skip_relocation? end end end diff --git a/Library/Homebrew/extend/os/linux/keg_relocate.rb b/Library/Homebrew/extend/os/linux/keg_relocate.rb index 80d5d01774..9d064b3697 100644 --- a/Library/Homebrew/extend/os/linux/keg_relocate.rb +++ b/Library/Homebrew/extend/os/linux/keg_relocate.rb @@ -1,9 +1,10 @@ -# typed: true # rubocop:todo Sorbet/StrictSigil +# typed: strict # frozen_string_literal: true require "compilers" class Keg + sig { params(relocation: Relocation, skip_protodesc_cold: T::Boolean).void } def relocate_dynamic_linkage(relocation, skip_protodesc_cold: false) # Patching the dynamic linker of glibc breaks it. return if name.match? Version.formula_optionally_versioned_regex(:glibc) @@ -17,6 +18,10 @@ class Keg end end + sig { + params(file: Pathname, old_prefix: T.any(String, Regexp), new_prefix: String, + skip_protodesc_cold: T::Boolean).returns(T::Boolean) + } def change_rpath!(file, old_prefix, new_prefix, skip_protodesc_cold: false) return false if !file.elf? || !file.dynamic_elf? @@ -59,6 +64,7 @@ class Keg true end + sig { params(options: T::Hash[Symbol, T::Boolean]).returns(T::Array[Symbol]) } def detect_cxx_stdlibs(options = {}) skip_executables = options.fetch(:skip_executables, false) results = Set.new @@ -73,6 +79,7 @@ class Keg results.to_a end + sig { returns(T::Array[Pathname]) } def elf_files hardlinks = Set.new elf_files = [] diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index a2b444134f..13c9ae62ae 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -1,4 +1,4 @@ -# typed: true # rubocop:disable Sorbet/StrictSigil +# typed: strict # frozen_string_literal: true module OS @@ -9,6 +9,7 @@ module OS requires_ancestor { ::Keg } module ClassMethods + sig { params(file: Pathname, string: String).returns(T::Array[String]) } def file_linked_libraries(file, string) # Check dynamic library linkage. Importantly, do not perform for static # libraries, which will falsely report "linkage" to themselves. @@ -20,6 +21,7 @@ module OS end end + sig { params(relocation: ::Keg::Relocation, skip_protodesc_cold: T::Boolean).void } def relocate_dynamic_linkage(relocation, skip_protodesc_cold: false) mach_o_files.each do |file| file.ensure_writable do @@ -50,6 +52,7 @@ module OS end end + sig { void } def fix_dynamic_linkage mach_o_files.each do |file| file.ensure_writable do @@ -98,6 +101,7 @@ module OS super end + sig { params(file: Pathname, target: String).returns(String) } def loader_name_for(file, target) # Use @loader_path-relative install names for other Homebrew-installed binaries. if ENV["HOMEBREW_RELOCATABLE_INSTALL_NAMES"] && target.start_with?(HOMEBREW_PREFIX) @@ -113,6 +117,7 @@ module OS # If file is a dylib or bundle itself, look for the dylib named by # bad_name relative to the lib directory, so that we can skip the more # expensive recursive search if possible. + sig { params(file: Pathname, bad_name: String).returns(String) } def fixed_name(file, bad_name) if bad_name.start_with? ::Keg::PREFIX_PLACEHOLDER bad_name.sub(::Keg::PREFIX_PLACEHOLDER, HOMEBREW_PREFIX) @@ -132,14 +137,16 @@ module OS end end - VARIABLE_REFERENCE_RX = /^@(loader_|executable_|r)path/ + VARIABLE_REFERENCE_RX = T.let(/^@(loader_|executable_|r)path/, Regexp) + sig { params(file: Pathname, linkage_type: Symbol, resolve_variable_references: T::Boolean, block: T.proc.params(arg0: String).void).void } def each_linkage_for(file, linkage_type, resolve_variable_references: false, &block) file.public_send(linkage_type, resolve_variable_references:) .grep_v(VARIABLE_REFERENCE_RX) .each(&block) end + sig { params(file: Pathname).returns(String) } def dylib_id_for(file) # Swift dylib IDs should be /usr/lib/swift return file.dylib_id if file.dylib_id.start_with?("/usr/lib/swift/libswift") @@ -167,14 +174,16 @@ module OS # `XXX.framework/XXX`, both with or without a slash-delimited prefix. FRAMEWORK_RX = %r{(?:^|/)(([^/]+)\.framework/(?:Versions/[^/]+/)?\2)$} + sig { params(bad_name: String).returns(String) } def find_dylib_suffix_from(bad_name) if (framework = bad_name.match(FRAMEWORK_RX)) - framework[1] + T.must(framework[1]) else File.basename(bad_name) end end + sig { params(bad_name: String).returns(T.nilable(Pathname)) } def find_dylib(bad_name) return unless lib.directory? @@ -182,6 +191,7 @@ module OS lib.find { |pn| break pn if pn.to_s.end_with?(suffix) } end + sig { returns(T::Array[Pathname]) } def mach_o_files hardlinks = Set.new mach_o_files = [] @@ -198,6 +208,7 @@ module OS mach_o_files end + sig { returns(::Keg::Relocation) } def prepare_relocation_to_locations relocation = super @@ -225,12 +236,14 @@ module OS relocation end + sig { returns(String) } def recursive_fgrep_args # Don't recurse into symlinks; the man page says this is the default, but # it's wrong. -O is a BSD-grep-only option. "-lrO" end + sig { returns([String, String]) } def egrep_args grep_bin = "egrep" grep_args = "--files-with-matches" @@ -239,11 +252,12 @@ module OS private - CELLAR_RX = %r{\A#{HOMEBREW_CELLAR}/(?[^/]+)/[^/]+} + CELLAR_RX = T.let(%r{\A#{HOMEBREW_CELLAR}/(?[^/]+)/[^/]+}, Regexp) private_constant :CELLAR_RX # Replace HOMEBREW_CELLAR references with HOMEBREW_PREFIX/opt references # if the Cellar reference is to a different keg. + sig { params(filename: String).returns(String) } def opt_name_for(filename) return filename unless filename.start_with?(HOMEBREW_PREFIX.to_s) return filename if filename.start_with?(path.to_s) @@ -252,6 +266,7 @@ module OS filename.sub(CELLAR_RX, "#{HOMEBREW_PREFIX}/opt/#{matches[:formula_name]}") end + sig { params(filename: String).returns(T::Boolean) } def rooted_in_build_directory?(filename) # CMake normalises `/private/tmp` to `/tmp`. # https://gitlab.kitware.com/cmake/cmake/-/issues/23251 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 072d2b0c62..2d1c3c9362 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -2024,7 +2024,7 @@ class Formula raise "No universal binaries found to deuniversalize" if targets.blank? - targets&.each do |target| + targets.compact.each do |target| extract_macho_slice_from(Pathname(target), Hardware::CPU.arch) end end diff --git a/Library/Homebrew/formula_cellar_checks.rb b/Library/Homebrew/formula_cellar_checks.rb index dbdc055302..9e63a2dd39 100644 --- a/Library/Homebrew/formula_cellar_checks.rb +++ b/Library/Homebrew/formula_cellar_checks.rb @@ -234,7 +234,7 @@ module FormulaCellarChecks keg = Keg.new(prefix) matches = [] - keg.each_unique_file_matching(HOMEBREW_SHIMS_PATH) do |f| + keg.each_unique_file_matching(HOMEBREW_SHIMS_PATH.to_s) do |f| match = f.relative_path_from(keg.to_path) next if match.to_s.match? %r{^share/doc/.+?/INFO_BIN$} diff --git a/Library/Homebrew/formula_installer.rb b/Library/Homebrew/formula_installer.rb index 6bf6be477f..b5dffb38c6 100644 --- a/Library/Homebrew/formula_installer.rb +++ b/Library/Homebrew/formula_installer.rb @@ -1591,7 +1591,7 @@ on_request: installed_on_request?, options:) keg = Keg.new(formula.prefix) skip_linkage = formula.bottle_specification.skip_relocation? - keg.replace_placeholders_with_locations(tab.changed_files, skip_linkage:) + keg.replace_placeholders_with_locations(tab.changed_files, skip_linkage:) if tab.changed_files cellar = formula.bottle_specification.tag_to_cellar(Utils::Bottles.tag) return if [:any, :any_skip_relocation].include?(cellar) diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index 3e75672cd8..fdbf38f415 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -1,23 +1,25 @@ -# typed: true # rubocop:todo Sorbet/StrictSigil +# typed: strict # frozen_string_literal: true class Keg - PREFIX_PLACEHOLDER = "@@HOMEBREW_PREFIX@@" - CELLAR_PLACEHOLDER = "@@HOMEBREW_CELLAR@@" - REPOSITORY_PLACEHOLDER = "@@HOMEBREW_REPOSITORY@@" - LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@" - PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@" - JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@" - NULL_BYTE = "\x00" - NULL_BYTE_STRING = "\\x00" + PREFIX_PLACEHOLDER = T.let("@@HOMEBREW_PREFIX@@", String) + CELLAR_PLACEHOLDER = T.let("@@HOMEBREW_CELLAR@@", String) + REPOSITORY_PLACEHOLDER = T.let("@@HOMEBREW_REPOSITORY@@", String) + LIBRARY_PLACEHOLDER = T.let("@@HOMEBREW_LIBRARY@@", String) + PERL_PLACEHOLDER = T.let("@@HOMEBREW_PERL@@", String) + JAVA_PLACEHOLDER = T.let("@@HOMEBREW_JAVA@@", String) + NULL_BYTE = T.let("\x00", String) + NULL_BYTE_STRING = T.let("\\x00", String) class Relocation - RELOCATABLE_PATH_REGEX_PREFIX = /(?:(?<=-F|-I|-L|-isystem)|(?