Module: MachOShim Private

Extended by:
T::Helpers
Defined in:
os/mac/mach.rb,
extend/pathname.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Pathname extension for dealing with Mach-O files.

Instance Method Summary collapse

Instance Method Details

#archSymbol

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



165
166
167
168
169
170
171
# File 'os/mac/mach.rb', line 165

def arch
  case archs.length
  when 0 then :dunno
  when 1 then archs.fetch(0)
  else :universal
  end
end

#archsArray<Symbol>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



160
161
162
# File 'os/mac/mach.rb', line 160

def archs
  mach_data.map { |m| m.fetch :arch }
end

#change_dylib_id(id, strict: true, write: true) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • id (String)
  • strict (Boolean) (defaults to: true)
  • write (Boolean) (defaults to: true)


100
101
102
103
# File 'os/mac/mach.rb', line 100

def change_dylib_id(id, strict: true, write: true)
  macho.change_dylib_id(id, { strict: })
  macho.write! if write
end

#change_install_name(old, new, strict: true, write: true) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • old (String)
  • new (String)
  • strict (Boolean) (defaults to: true)
  • write (Boolean) (defaults to: true)


106
107
108
109
# File 'os/mac/mach.rb', line 106

def change_install_name(old, new, strict: true, write: true)
  macho.change_install_name(old, new, { strict: })
  macho.write! if write
end

#change_rpath(old, new, uniq: false, last: false, strict: true, write: true) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Parameters:

  • old (String)
  • new (String)
  • uniq (Boolean) (defaults to: false)
  • last (Boolean) (defaults to: false)
  • strict (Boolean) (defaults to: true)
  • write (Boolean) (defaults to: true)


94
95
96
97
# File 'os/mac/mach.rb', line 94

def change_rpath(old, new, uniq: false, last: false, strict: true, write: true)
  macho.change_rpath(old, new, { uniq:, last:, strict: })
  macho.write! if write
end

#delete_rpath(rpath, strict: true, write: true) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns the deleted rpath, or nil when there's nothing to delete.

Parameters:

  • rpath (String)
  • strict (Boolean) (defaults to: true)
  • write (Boolean) (defaults to: true)

Returns:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'os/mac/mach.rb', line 78

def delete_rpath(rpath, strict: true, write: true)
  candidates = rpaths(resolve_variable_references: false).select do |r|
    resolve_variable_name(r) == resolve_variable_name(rpath)
  end

  # Delete the last instance to avoid changing the order in which rpaths are searched.
  rpath_to_delete = candidates.last
  # Avoid writing the whole binary back to disk when there's nothing to delete.
  return if rpath_to_delete.nil?

  macho.delete_rpath(rpath_to_delete, { last: true, strict: })
  macho.write! if write
  rpath_to_delete
end

#dylib?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


199
200
201
# File 'os/mac/mach.rb', line 199

def dylib?
  mach_data.any? { |m| m.fetch(:type) == :dylib }
end

#dylib_idString?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



24
# File 'os/mac/mach.rb', line 24

def dylib_id = macho.dylib_id

#dynamically_linked_libraries(except: :none, resolve_variable_references: true) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • except (Symbol) (defaults to: :none)
  • resolve_variable_references (Boolean) (defaults to: true)

Returns:



118
119
120
121
122
123
124
125
# File 'os/mac/mach.rb', line 118

def dynamically_linked_libraries(except: :none, resolve_variable_references: true)
  lcs = macho.dylib_load_commands
  lcs.reject! { |lc| lc.flag?(except) } if except != :none
  names = lcs.map { |lc| lc.name.to_s }.uniq
  names.map! { resolve_variable_name(it) } if resolve_variable_references

  names
end

#i386?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


179
180
181
# File 'os/mac/mach.rb', line 179

def i386?
  arch == :i386
end

#initialize(*args) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • args (T.untyped)


14
15
16
17
18
19
20
21
# File 'os/mac/mach.rb', line 14

def initialize(*args)
  require "macho"

  @macho = T.let(nil, T.nilable(MachOFile))
  @mach_data = T.let(nil, T.nilable(T::Array[T::Hash[Symbol, Symbol]]))

  super
end

#mach_o_bundle?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


211
212
213
# File 'os/mac/mach.rb', line 211

def mach_o_bundle?
  mach_data.any? { |m| m.fetch(:type) == :bundle }
end

#mach_o_executable?Boolean Also known as: binary_executable?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


204
205
206
# File 'os/mac/mach.rb', line 204

def mach_o_executable?
  mach_data.any? { |m| m.fetch(:type) == :executable }
end

#ppc64?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


194
195
196
# File 'os/mac/mach.rb', line 194

def ppc64?
  arch == :ppc64
end

#ppc7400?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


189
190
191
# File 'os/mac/mach.rb', line 189

def ppc7400?
  arch == :ppc7400
end

#resolve_rpath(name) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



150
151
152
153
154
155
156
157
# File 'os/mac/mach.rb', line 150

def resolve_rpath(name)
  target = T.let(nil, T.nilable(String))
  return unless rpaths(resolve_variable_references: true).find do |rpath|
    File.exist?(target = File.join(rpath, name.delete_prefix("@rpath")))
  end

  target
end

#resolve_variable_name(name, resolve_rpaths: true) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • name (String)
  • resolve_rpaths (Boolean) (defaults to: true)

Returns:



137
138
139
140
141
142
143
144
145
146
147
# File 'os/mac/mach.rb', line 137

def resolve_variable_name(name, resolve_rpaths: true)
  if name.start_with? "@loader_path"
    Pathname(name.sub("@loader_path", dirname.to_s)).cleanpath.to_s
  elsif name.start_with?("@executable_path") && binary_executable?
    Pathname(name.sub("@executable_path", dirname.to_s)).cleanpath.to_s
  elsif resolve_rpaths && name.start_with?("@rpath") && (target = resolve_rpath(name)).present?
    target
  else
    name
  end
end

#rpaths(resolve_variable_references: true) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • resolve_variable_references (Boolean) (defaults to: true)

Returns:



128
129
130
131
132
133
134
# File 'os/mac/mach.rb', line 128

def rpaths(resolve_variable_references: true)
  names = macho.rpaths
  # Don't recursively resolve rpaths to avoid infinite loops.
  names.map! { |name| resolve_variable_name(name, resolve_rpaths: false) } if resolve_variable_references

  names
end

#save_changesvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.

Persist edits made with write: false in a single write per file.



113
114
115
# File 'os/mac/mach.rb', line 113

def save_changes
  macho.write!
end

#universal?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


174
175
176
# File 'os/mac/mach.rb', line 174

def universal?
  arch == :universal
end

#x86_64?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


184
185
186
# File 'os/mac/mach.rb', line 184

def x86_64?
  arch == :x86_64
end