Merge pull request #20660 from botantony/os/mac-typed

os/mac/*: `typed: strict`
This commit is contained in:
Mike McQuaid 2025-09-10 15:36:23 +00:00 committed by GitHub
commit 3023e6dcad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 62 additions and 28 deletions

View File

@ -1,7 +1,14 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
class Keg class Keg
sig { params(path: Pathname).void }
def initialize(path)
super
@require_relocation = T.let(false, T::Boolean)
end
sig { params(id: String, file: Pathname).returns(T::Boolean) } sig { params(id: String, file: Pathname).returns(T::Boolean) }
def change_dylib_id(id, file) def change_dylib_id(id, file)
return false if file.dylib_id == id return false if file.dylib_id == id
@ -36,6 +43,7 @@ class Keg
raise raise
end end
sig { params(old: String, new: String, file: Pathname).returns(T::Boolean) }
def change_rpath(old, new, file) def change_rpath(old, new, file)
return false if old == new return false if old == new

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "macho" require "macho"
@ -12,25 +12,28 @@ module MachOShim
delegate [:dylib_id] => :macho delegate [:dylib_id] => :macho
sig { params(args: T.untyped).void }
def initialize(*args) def initialize(*args)
@macho = T.let(nil, T.nilable(MachO::MachOFile)) @macho = T.let(nil, T.nilable(T.any(MachO::MachOFile, MachO::FatFile)))
@mach_data = T.let(nil, T.nilable(T::Array[T::Hash[Symbol, T.untyped]])) @mach_data = T.let(nil, T.nilable(T::Array[T::Hash[Symbol, Symbol]]))
super super
end end
sig { returns(T.any(MachO::MachOFile, MachO::FatFile)) }
def macho def macho
@macho ||= MachO.open(to_s) @macho ||= MachO.open(to_s)
end end
private :macho private :macho
sig { returns(T::Array[T::Hash[Symbol, T.untyped]]) } sig { returns(T::Array[T::Hash[Symbol, Symbol]]) }
def mach_data def mach_data
@mach_data ||= begin @mach_data ||= begin
machos = [] machos = []
mach_data = [] mach_data = []
if MachO::Utils.fat_magic?(macho.magic) case (macho = self.macho)
when MachO::FatFile
machos = macho.machos machos = macho.machos
else else
machos << macho machos << macho
@ -68,34 +71,38 @@ module MachOShim
# TODO: See if the `#write!` call can be delayed until # TODO: See if the `#write!` call can be delayed until
# we know we're not making any changes to the rpaths. # we know we're not making any changes to the rpaths.
def delete_rpath(rpath, **options) sig { params(rpath: String, strict: T::Boolean).void }
def delete_rpath(rpath, strict: true)
candidates = rpaths(resolve_variable_references: false).select do |r| candidates = rpaths(resolve_variable_references: false).select do |r|
resolve_variable_name(r) == resolve_variable_name(rpath) resolve_variable_name(r) == resolve_variable_name(rpath)
end end
# Delete the last instance to avoid changing the order in which rpaths are searched. # Delete the last instance to avoid changing the order in which rpaths are searched.
rpath_to_delete = candidates.last rpath_to_delete = candidates.last
options[:last] = true
macho.delete_rpath(rpath_to_delete, options) macho.delete_rpath(rpath_to_delete, { last: true, strict: })
macho.write! macho.write!
end end
def change_rpath(old, new, **options) sig { params(old: String, new: String, uniq: T::Boolean, last: T::Boolean, strict: T::Boolean).void }
macho.change_rpath(old, new, options) def change_rpath(old, new, uniq: false, last: false, strict: true)
macho.change_rpath(old, new, { uniq:, last:, strict: })
macho.write! macho.write!
end end
def change_dylib_id(id, **options) sig { params(id: String, strict: T::Boolean).void }
macho.change_dylib_id(id, options) def change_dylib_id(id, strict: true)
macho.change_dylib_id(id, { strict: })
macho.write! macho.write!
end end
def change_install_name(old, new, **options) sig { params(old: String, new: String, strict: T::Boolean).void }
macho.change_install_name(old, new, options) def change_install_name(old, new, strict: true)
macho.change_install_name(old, new, { strict: })
macho.write! macho.write!
end end
sig { params(except: Symbol, resolve_variable_references: T::Boolean).returns(T::Array[String]) }
def dynamically_linked_libraries(except: :none, resolve_variable_references: true) def dynamically_linked_libraries(except: :none, resolve_variable_references: true)
lcs = macho.dylib_load_commands lcs = macho.dylib_load_commands
lcs.reject! { |lc| lc.flag?(except) } if except != :none lcs.reject! { |lc| lc.flag?(except) } if except != :none
@ -105,6 +112,7 @@ module MachOShim
names names
end end
sig { params(resolve_variable_references: T::Boolean).returns(T::Array[String]) }
def rpaths(resolve_variable_references: true) def rpaths(resolve_variable_references: true)
names = macho.rpaths names = macho.rpaths
# Don't recursively resolve rpaths to avoid infinite loops. # Don't recursively resolve rpaths to avoid infinite loops.
@ -113,11 +121,12 @@ module MachOShim
names names
end end
sig { params(name: String, resolve_rpaths: T::Boolean).returns(String) }
def resolve_variable_name(name, resolve_rpaths: true) def resolve_variable_name(name, resolve_rpaths: true)
if name.start_with? "@loader_path" if name.start_with? "@loader_path"
Pathname(name.sub("@loader_path", dirname)).cleanpath.to_s Pathname(name.sub("@loader_path", dirname.to_s)).cleanpath.to_s
elsif name.start_with?("@executable_path") && binary_executable? elsif name.start_with?("@executable_path") && binary_executable?
Pathname(name.sub("@executable_path", dirname)).cleanpath.to_s Pathname(name.sub("@executable_path", dirname.to_s)).cleanpath.to_s
elsif resolve_rpaths && name.start_with?("@rpath") && (target = resolve_rpath(name)).present? elsif resolve_rpaths && name.start_with?("@rpath") && (target = resolve_rpath(name)).present?
target target
else else
@ -125,6 +134,7 @@ module MachOShim
end end
end end
sig { params(name: String).returns(T.nilable(String)) }
def resolve_rpath(name) def resolve_rpath(name)
target = T.let(nil, T.nilable(String)) target = T.let(nil, T.nilable(String))
return unless rpaths(resolve_variable_references: true).find do |rpath| return unless rpaths(resolve_variable_references: true).find do |rpath|
@ -134,48 +144,58 @@ module MachOShim
target target
end end
sig { returns(T::Array[Symbol]) }
def archs def archs
mach_data.map { |m| m.fetch :arch } mach_data.map { |m| m.fetch :arch }
end end
sig { returns(Symbol) }
def arch def arch
case archs.length case archs.length
when 0 then :dunno when 0 then :dunno
when 1 then archs.first when 1 then archs.fetch(0)
else :universal else :universal
end end
end end
sig { returns(T::Boolean) }
def universal? def universal?
arch == :universal arch == :universal
end end
sig { returns(T::Boolean) }
def i386? def i386?
arch == :i386 arch == :i386
end end
sig { returns(T::Boolean) }
def x86_64? def x86_64?
arch == :x86_64 arch == :x86_64
end end
sig { returns(T::Boolean) }
def ppc7400? def ppc7400?
arch == :ppc7400 arch == :ppc7400
end end
sig { returns(T::Boolean) }
def ppc64? def ppc64?
arch == :ppc64 arch == :ppc64
end end
sig { returns(T::Boolean) }
def dylib? def dylib?
mach_data.any? { |m| m.fetch(:type) == :dylib } mach_data.any? { |m| m.fetch(:type) == :dylib }
end end
sig { returns(T::Boolean) }
def mach_o_executable? def mach_o_executable?
mach_data.any? { |m| m.fetch(:type) == :executable } mach_data.any? { |m| m.fetch(:type) == :executable }
end end
alias binary_executable? mach_o_executable? alias binary_executable? mach_o_executable?
sig { returns(T::Boolean) }
def mach_o_bundle? def mach_o_bundle?
mach_data.any? { |m| m.fetch(:type) == :bundle } mach_data.any? { |m| m.fetch(:type) == :bundle }
end end

View File

@ -1,4 +1,4 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
require "system_command" require "system_command"
@ -22,7 +22,7 @@ module OS
sig { params(version: MacOSVersion, path: T.any(String, Pathname), source: Symbol).void } sig { params(version: MacOSVersion, path: T.any(String, Pathname), source: Symbol).void }
def initialize(version, path, source) def initialize(version, path, source)
@version = version @version = version
@path = Pathname.new(path) @path = T.let(Pathname(path), Pathname)
@source = source @source = source
end end
end end
@ -36,6 +36,12 @@ module OS
class NoSDKError < StandardError; end class NoSDKError < StandardError; end
sig { void }
def initialize
@all_sdks = T.let(nil, T.nilable(T::Array[SDK]))
@sdk_prefix = T.let(nil, T.nilable(String))
end
sig { params(version: MacOSVersion).returns(SDK) } sig { params(version: MacOSVersion).returns(SDK) }
def sdk_for(version) def sdk_for(version)
sdk = all_sdks.find { |s| s.version == version } sdk = all_sdks.find { |s| s.version == version }

View File

@ -1,11 +1,11 @@
# typed: true # rubocop:todo Sorbet/StrictSigil # typed: strict
# frozen_string_literal: true # frozen_string_literal: true
module OS module OS
module Mac module Mac
# Helper module for querying Xcode information. # Helper module for querying Xcode information.
module Xcode module Xcode
DEFAULT_BUNDLE_PATH = Pathname("/Applications/Xcode.app").freeze DEFAULT_BUNDLE_PATH = T.let(Pathname("/Applications/Xcode.app").freeze, Pathname)
BUNDLE_ID = "com.apple.dt.Xcode" BUNDLE_ID = "com.apple.dt.Xcode"
OLD_BUNDLE_ID = "com.apple.Xcode" OLD_BUNDLE_ID = "com.apple.Xcode"
APPLE_DEVELOPER_DOWNLOAD_URL = "https://developer.apple.com/download/all/" APPLE_DEVELOPER_DOWNLOAD_URL = "https://developer.apple.com/download/all/"
@ -98,7 +98,7 @@ module OS
# directory or nil if Xcode.app is not installed. # directory or nil if Xcode.app is not installed.
sig { returns(T.nilable(Pathname)) } sig { returns(T.nilable(Pathname)) }
def self.prefix def self.prefix
@prefix ||= begin @prefix ||= T.let(begin
dir = MacOS.active_developer_dir dir = MacOS.active_developer_dir
if dir.empty? || dir == CLT::PKG_PATH || !File.directory?(dir) if dir.empty? || dir == CLT::PKG_PATH || !File.directory?(dir)
@ -108,7 +108,7 @@ module OS
# Use cleanpath to avoid pathological trailing slash # Use cleanpath to avoid pathological trailing slash
Pathname.new(dir).cleanpath Pathname.new(dir).cleanpath
end end
end end, T.nilable(Pathname))
end end
sig { returns(Pathname) } sig { returns(Pathname) }
@ -134,7 +134,7 @@ module OS
sig { returns(XcodeSDKLocator) } sig { returns(XcodeSDKLocator) }
def self.sdk_locator def self.sdk_locator
@sdk_locator ||= XcodeSDKLocator.new @sdk_locator ||= T.let(XcodeSDKLocator.new, T.nilable(OS::Mac::XcodeSDKLocator))
end end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) } sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) }
@ -183,7 +183,7 @@ module OS
# may return a version string # may return a version string
# that is guessed based on the compiler, so do not # that is guessed based on the compiler, so do not
# use it in order to check if Xcode is installed. # use it in order to check if Xcode is installed.
if @version ||= detect_version if @version ||= T.let(detect_version, T.nilable(String))
::Version.new @version ::Version.new @version
else else
::Version::NULL ::Version::NULL
@ -293,7 +293,7 @@ module OS
sig { returns(CLTSDKLocator) } sig { returns(CLTSDKLocator) }
def self.sdk_locator def self.sdk_locator
@sdk_locator ||= CLTSDKLocator.new @sdk_locator ||= T.let(CLTSDKLocator.new, T.nilable(OS::Mac::CLTSDKLocator))
end end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) } sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) }
@ -444,7 +444,7 @@ module OS
# @api internal # @api internal
sig { returns(::Version) } sig { returns(::Version) }
def self.version def self.version
if @version ||= detect_version if @version ||= T.let(detect_version, T.nilable(String))
::Version.new @version ::Version.new @version
else else
::Version::NULL ::Version::NULL