1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
module ArchitectureListExtension
def fat?
length > 1
end
def intel_universal?
intersects_all?(Hardware::CPU::INTEL_32BIT_ARCHS, Hardware::CPU::INTEL_64BIT_ARCHS)
end
def ppc_universal?
intersects_all?(Hardware::CPU::PPC_32BIT_ARCHS, Hardware::CPU::PPC_64BIT_ARCHS)
end
# Old-style 32-bit PPC/Intel universal, e.g. ppc7400 and i386
def cross_universal?
intersects_all?(Hardware::CPU::PPC_32BIT_ARCHS, Hardware::CPU::INTEL_32BIT_ARCHS)
end
def universal?
intel_universal? || ppc_universal? || cross_universal?
end
def ppc?
(Hardware::CPU::PPC_32BIT_ARCHS+Hardware::CPU::PPC_64BIT_ARCHS).any? {|a| self.include? a}
end
def remove_ppc!
(Hardware::CPU::PPC_32BIT_ARCHS+Hardware::CPU::PPC_64BIT_ARCHS).each {|a| self.delete a}
end
def as_arch_flags
self.collect{ |a| "-arch #{a}" }.join(' ')
end
def as_cmake_arch_flags
self.join(';')
end
protected
def intersects_all?(*set)
set.all? do |archset|
archset.any? {|a| self.include? a}
end
end
end
module MachO
OTOOL_RX = /\t(.*) \(compatibility version (?:\d+\.)*\d+, current version (?:\d+\.)*\d+\)/
# Mach-O binary methods, see:
# /usr/include/mach-o/loader.h
# /usr/include/mach-o/fat.h
def mach_data
@mach_data ||= begin
offsets = []
mach_data = []
header = read(8).unpack("N2")
case header[0]
when 0xcafebabe # universal
header[1].times do |i|
# header[1] is the number of struct fat_arch in the file.
# Each struct fat_arch is 20 bytes, and the 'offset' member
# begins 8 bytes into the struct, with an additional 8 byte
# offset due to the struct fat_header at the beginning of
# the file.
offsets << read(4, 20*i + 16).unpack("N")[0]
end
when 0xcefaedfe, 0xcffaedfe, 0xfeedface, 0xfeedfacf # Single arch
offsets << 0
when 0x7f454c46 # ELF
mach_data << { :arch => :x86_64, :type => :executable }
else
raise "Not a Mach-O binary."
end
offsets.each do |offset|
arch = case read(8, offset).unpack("N2")
when [0xcefaedfe, 0x07000000] then :i386
when [0xcffaedfe, 0x07000001] then :x86_64
when [0xfeedface, 0x00000012] then :ppc7400
when [0xfeedfacf, 0x01000012] then :ppc64
else :dunno
end
type = case read(4, offset + 12).unpack("N")[0]
when 0x00000002, 0x02000000 then :executable
when 0x00000006, 0x06000000 then :dylib
when 0x00000008, 0x08000000 then :bundle
else :dunno
end
mach_data << { :arch => arch, :type => type }
end
mach_data
rescue
[]
end
end
def archs
mach_data.map{ |m| m.fetch :arch }.extend(ArchitectureListExtension)
end
def arch
case archs.length
when 0 then :dunno
when 1 then archs.first
else :universal
end
end
def universal?
arch == :universal
end
def i386?
arch == :i386
end
def x86_64?
arch == :x86_64
end
def ppc7400?
arch == :ppc7400
end
def ppc64?
arch == :ppc64
end
def dylib?
mach_data.any? { |m| m.fetch(:type) == :dylib }
end
def mach_o_executable?
mach_data.any? { |m| m.fetch(:type) == :executable }
end
def mach_o_bundle?
mach_data.any? { |m| m.fetch(:type) == :bundle }
end
# Returns an array containing all dynamically-linked libraries, based on the
# output of otool. This returns the install names, so these are not guaranteed
# to be absolute paths.
# Returns an empty array both for software that links against no libraries,
# and for non-mach objects.
def dynamically_linked_libraries
# Use an environment variable to avoid escaping problems
ENV['HOMEBREW_MACH_O_FILE'] = expand_path.to_s
libs = `#{MacOS.locate("otool")} -L "$HOMEBREW_MACH_O_FILE"`.split("\n")
# First line is the filename
libs.shift
# For dylibs, the next line is the ID
libs.shift if dylib?
libs.map! { |lib| lib[OTOOL_RX, 1] }
libs.compact!
libs
ensure
ENV.delete 'HOMEBREW_MACH_O_FILE'
end
end
|