Skip to content

Instantly share code, notes, and snippets.

@eregon
Created November 14, 2010 23:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eregon/676232 to your computer and use it in GitHub Desktop.
Save eregon/676232 to your computer and use it in GitHub Desktop.
Fix the libraries paths used by binaries and libraries of Homebrew. Could be improved to do it recursively. Need to use HOMEBREW_PREFIX, not /usr/local directly
puts "sudo is needed, because we need to write in read-only binaries and libraries"
# should maybe try with system if not found, and in keg-only
def fix_libraries_paths(binary, recurse = false)
binary = File.expand_path binary
puts binary
libraries = `otool -L #{binary}`.lines.drop(1)
libraries = libraries.map { |library| library.split(/\s/)[1] }
libraries.select { |library| library.start_with? '/usr/local/Cellar/' }.each { |library|
lib = File.basename(library).sub(/[._-](?:\d+\.)+dylib$/, '.dylib')
org_lib = File.basename(library)
next if library.include? File.basename(binary, File.extname(binary))
good_path = "/usr/local/lib/#{lib}"
good_path_org = "/usr/local/lib/#{org_lib}"
if File.exist? good_path
system "sudo install_name_tool -change #{library} #{good_path} #{binary}"
elsif File.exist? good_path_org
system "sudo install_name_tool -change #{library} #{good_path_org} #{binary}"
else
puts "Could not find the library for #{lib} : #{library} (tried #{good_path})"
end
}
libraries.each { |library|
fix_libraries_paths(library) if library =~ %r{\A/usr/local/(?:lib|Cellar)/}
} if recurse
end
recur = ARGV.delete('-r')
fix_libraries_paths(ARGV.first, recur)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment