mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-15 19:56:59 +08:00
archs_for_command: use new Mach-O Pathname methods
- Reimplement archs_for_command on top of the new Mach-O methods - Move ArchitectureListExtension to mach.rb - Add a test for the ArchitectureListExtension Signed-off-by: Jack Nagel <jacknagel@gmail.com>
This commit is contained in:
parent
7bb1894df5
commit
53ce9dba53
@ -1,3 +1,18 @@
|
||||
module ArchitectureListExtension
|
||||
def universal?
|
||||
self.include? :i386 and self.include? :x86_64
|
||||
end
|
||||
|
||||
def remove_ppc!
|
||||
self.delete :ppc7400
|
||||
self.delete :ppc64
|
||||
end
|
||||
|
||||
def as_arch_flags
|
||||
self.collect{ |a| "-arch #{a}" }.join(' ')
|
||||
end
|
||||
end
|
||||
|
||||
module MachO
|
||||
# Mach-O binary methods, see:
|
||||
# /usr/include/mach-o/loader.h
|
||||
@ -52,7 +67,7 @@ module MachO
|
||||
end
|
||||
|
||||
def archs
|
||||
mach_data.map{ |m| m.fetch :arch }
|
||||
mach_data.map{ |m| m.fetch :arch }.extend(ArchitectureListExtension)
|
||||
end
|
||||
|
||||
def arch
|
||||
|
@ -77,6 +77,21 @@ class MachOPathnameTests < Test::Unit::TestCase
|
||||
assert_no_match /Mach-O [^ ]* ?executable/,
|
||||
`/usr/bin/file -h '#{pn}'`.chomp
|
||||
end
|
||||
|
||||
def test_architecture_list_extension
|
||||
archs = [:i386, :x86_64, :ppc7400, :ppc64]
|
||||
archs.extend(ArchitectureListExtension)
|
||||
assert archs.universal?
|
||||
archs.remove_ppc!
|
||||
assert_equal 2, archs.length
|
||||
assert_match /-arch i386/, archs.as_arch_flags
|
||||
assert_match /-arch x86_64/, archs.as_arch_flags
|
||||
|
||||
pn = Pathname.new("#{TEST_FOLDER}/mach/fat.dylib")
|
||||
assert pn.archs.universal?
|
||||
assert_match /-arch i386/, pn.archs.as_arch_flags
|
||||
assert_match /-arch x86_64/, pn.archs.as_arch_flags
|
||||
end
|
||||
end
|
||||
|
||||
class TextExecutableTests < Test::Unit::TestCase
|
||||
|
@ -16,19 +16,19 @@ class UtilTests < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
def test_arch_for_command
|
||||
arches=archs_for_command '/usr/bin/svn'
|
||||
archs = archs_for_command '/usr/bin/svn'
|
||||
if `sw_vers -productVersion` =~ /10\.(\d+)/ and $1.to_i >= 7
|
||||
assert_equal 2, arches.length
|
||||
assert arches.include?(:x86_64)
|
||||
assert_equal 2, archs.length
|
||||
assert archs.include?(:x86_64)
|
||||
elsif `sw_vers -productVersion` =~ /10\.(\d+)/ and $1.to_i == 6
|
||||
assert_equal 3, arches.length
|
||||
assert arches.include?(:x86_64)
|
||||
assert arches.include?(:ppc7400)
|
||||
assert_equal 3, archs.length
|
||||
assert archs.include?(:x86_64)
|
||||
assert archs.include?(:ppc7400)
|
||||
else
|
||||
assert_equal 2, arches.length
|
||||
assert arches.include?(:ppc7400)
|
||||
assert_equal 2, archs.length
|
||||
assert archs.include?(:ppc7400)
|
||||
end
|
||||
assert arches.include?(:i386)
|
||||
assert archs.include?(:i386)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -186,43 +186,10 @@ def gzip *paths
|
||||
end
|
||||
end
|
||||
|
||||
module ArchitectureListExtension
|
||||
def universal?
|
||||
self.include? :i386 and self.include? :x86_64
|
||||
end
|
||||
|
||||
def remove_ppc!
|
||||
self.delete :ppc7400
|
||||
self.delete :ppc64
|
||||
end
|
||||
|
||||
def as_arch_flags
|
||||
self.collect{ |a| "-arch #{a}" }.join(' ')
|
||||
end
|
||||
end
|
||||
|
||||
# Returns array of architectures that the given command or library is built for.
|
||||
def archs_for_command cmd
|
||||
cmd = cmd.to_s # If we were passed a Pathname, turn it into a string.
|
||||
cmd = `/usr/bin/which #{cmd}` unless Pathname.new(cmd).absolute?
|
||||
cmd.gsub! ' ', '\\ ' # Escape spaces in the filename.
|
||||
|
||||
lines = `/usr/bin/file -L #{cmd}`
|
||||
archs = lines.to_a.inject([]) do |archs, line|
|
||||
case line
|
||||
when /Mach-O (executable|dynamically linked shared library) ppc/
|
||||
archs << :ppc7400
|
||||
when /Mach-O 64-bit (executable|dynamically linked shared library) ppc64/
|
||||
archs << :ppc64
|
||||
when /Mach-O (executable|dynamically linked shared library) i386/
|
||||
archs << :i386
|
||||
when /Mach-O 64-bit (executable|dynamically linked shared library) x86_64/
|
||||
archs << :x86_64
|
||||
else
|
||||
archs
|
||||
end
|
||||
end
|
||||
archs.extend(ArchitectureListExtension)
|
||||
cmd = which(cmd) unless Pathname.new(cmd).absolute?
|
||||
Pathname.new(cmd).archs
|
||||
end
|
||||
|
||||
def inreplace path, before=nil, after=nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user