From a8527f4c16d7df419f6539fd583302635ec98a4a Mon Sep 17 00:00:00 2001 From: Carlo Cabrera <30379873+carlocab@users.noreply.github.com> Date: Sat, 24 Jul 2021 19:39:47 +0800 Subject: [PATCH] Ensure writability, handle errors --- Library/Homebrew/formula.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 3ce605123b..5081cc20bb 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -1582,6 +1582,10 @@ class Formula 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 } def deuniversalize_machos(*targets) if targets.blank? @@ -1590,10 +1594,23 @@ class Formula end end - targets.each do |t| - macho = MachO::FatFile.new(t) + targets.each { |t| extract_slice_from(Pathname.new(t), Hardware::CPU.arch) } + 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.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