Merge pull request #11756 from carlocab/deuniversalize-machos

formula: add `deuniversalize_machos` method
This commit is contained in:
Carlo Cabrera 2021-08-18 02:28:34 +08:00 committed by GitHub
commit 0ae032ad2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -4,7 +4,9 @@
class Formula
undef shared_library
undef rpath
undef deuniversalize_machos
sig { params(name: String, version: T.nilable(T.any(String, Integer))).returns(String) }
def shared_library(name, version = nil)
suffix = if version == "*" || (name == "*" && version.blank?)
"{,.*}"
@ -14,10 +16,14 @@ class Formula
"#{name}.so#{suffix}"
end
sig { returns(String) }
def rpath
"'$ORIGIN/../lib'"
end
sig { params(targets: T.nilable(T.any(Pathname, String))).void }
def deuniversalize_machos(*targets); end
class << self
undef ignore_missing_libraries

View File

@ -1589,6 +1589,38 @@ 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)
targets = nil if targets.blank?
targets ||= any_installed_keg.mach_o_files.select do |file|
file.arch == :universal && file.archs.include?(Hardware::CPU.arch)
end
targets.each { |t| extract_macho_slice_from(Pathname.new(t), Hardware::CPU.arch) }
end
# @private
sig { params(file: Pathname, arch: T.nilable(Symbol)).void }
def extract_macho_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 file
MachO.codesign! file if Hardware::CPU.arm?
rescue MachO::MachOBinaryError
onoe "#{file} is not a universal binary"
raise
rescue NoMethodError
onoe "#{file} does not contain an #{arch} slice"
raise
end
end
# an array of all core {Formula} names
# @private
def self.core_names