os/mac/mach: typed: strict
Signed-off-by: botantony <antonsm21@gmail.com>
This commit is contained in:
parent
744e2452d8
commit
289fed93b4
@ -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,26 +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)
|
if macho.is_a?(MachO::FatFile)
|
||||||
machos = macho.machos
|
machos = T.cast(macho, MachO::FatFile).machos
|
||||||
else
|
else
|
||||||
machos << macho
|
machos << macho
|
||||||
end
|
end
|
||||||
@ -68,6 +70,7 @@ 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.
|
||||||
|
sig { params(rpath: String, options: T::Boolean).void }
|
||||||
def delete_rpath(rpath, **options)
|
def delete_rpath(rpath, **options)
|
||||||
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)
|
||||||
@ -81,21 +84,25 @@ module MachOShim
|
|||||||
macho.write!
|
macho.write!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(old: String, new: String, options: T::Boolean).void }
|
||||||
def change_rpath(old, new, **options)
|
def change_rpath(old, new, **options)
|
||||||
macho.change_rpath(old, new, options)
|
macho.change_rpath(old, new, options)
|
||||||
macho.write!
|
macho.write!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(id: String, options: T::Boolean).void }
|
||||||
def change_dylib_id(id, **options)
|
def change_dylib_id(id, **options)
|
||||||
macho.change_dylib_id(id, options)
|
macho.change_dylib_id(id, options)
|
||||||
macho.write!
|
macho.write!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
sig { params(old: String, new: String, options: T::Boolean).void }
|
||||||
def change_install_name(old, new, **options)
|
def change_install_name(old, new, **options)
|
||||||
macho.change_install_name(old, new, options)
|
macho.change_install_name(old, new, options)
|
||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user