Ensure writability, handle errors

This commit is contained in:
Carlo Cabrera 2021-07-24 19:39:47 +08:00
parent 5e0b786da2
commit a8527f4c16
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -1582,6 +1582,10 @@ class Formula
end end
end end
# Replaces a universal binary with its native slice.
#
# If called with no parameters, does this with all compatible
# universal binaries in a {Formula}'s {Keg}.
sig { params(targets: T.nilable(T.any(Pathname, String))).void } sig { params(targets: T.nilable(T.any(Pathname, String))).void }
def deuniversalize_machos(*targets) def deuniversalize_machos(*targets)
if targets.blank? if targets.blank?
@ -1590,10 +1594,23 @@ class Formula
end end
end end
targets.each do |t| targets.each { |t| extract_slice_from(Pathname.new(t), Hardware::CPU.arch) }
macho = MachO::FatFile.new(t) end
# @private
sig { params(file: Pathname, arch: T.nilable(Symbol)).void }
def extract_slice_from(file, arch = Hardware::CPU.arch)
odebug "Extracting #{arch} slice from #{file}"
file.ensure_writable do
macho = MachO::FatFile.new(file)
native_slice = macho.extract(Hardware::CPU.arch) native_slice = macho.extract(Hardware::CPU.arch)
native_slice.write t native_slice.write file
rescue MachO::MachOBinaryError
onoe "#{file} is not a universal binary"
raise
rescue NoMethodError
onoe "#{file} does not contain an #{arch} slice"
raise
end end
end end