Created
November 14, 2010 23:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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