aboutsummaryrefslogtreecommitdiffstats
path: root/Library/Homebrew/os/mac/ruby_mach.rb
blob: 0a1565f4f808860d4030fe6898b01d882727f8e7 (plain)
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
require "vendor/macho/macho"

module RubyMachO
  # @private
  def macho
    @macho ||= begin
      MachO.open(to_s)
    end
  end

  # @private
  def mach_data
    @mach_data ||= begin
      machos = []
      mach_data = []

      if MachO::Utils.fat_magic?(macho.magic)
        machos = macho.machos
      else
        machos << macho
      end

      machos.each do |m|
        arch = case m.cputype
        when :x86_64, :i386, :ppc64 then m.cputype
        when :ppc then :ppc7400
        else :dunno
        end

        type = case m.filetype
        when :dylib, :bundle then m.filetype
        when :execute then :executable
        else :dunno
        end

        mach_data << { :arch => arch, :type => type }
      end

      mach_data
    rescue MachO::NotAMachOError
      # Silently ignore errors that indicate the file is not a Mach-O binary ...
      []
    rescue
      # ... but complain about other (parse) errors for further investigation.
      if ARGV.homebrew_developer?
        onoe "Failed to read Mach-O binary: #{self}"
        raise
      end
      []
    end
  end

  def dynamically_linked_libraries
    macho.linked_dylibs
  end

  def dylib_id
    macho.dylib_id
  end
end