Skip to content

Instantly share code, notes, and snippets.

@suzumura-ss
Created July 17, 2015 05:46
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 suzumura-ss/01d6e0f42234d3110ea1 to your computer and use it in GitHub Desktop.
Save suzumura-ss/01d6e0f42234d3110ea1 to your computer and use it in GitHub Desktop.
shared libraryの参照パスを @executable_path/ 直下に再帰的に更新
#!/usr/bin/env ruby
class ModifyPath
def initialize(exe)
@exe = exe
@dylibs = `otool -L #{exe}`.each_line.map{|line|
$1 if line=~%r{\t([^/@].+\.dylib) .*$}
}.compact.delete_if{|dy|
File.basename(dy)==File.basename(exe) or !File.exist?(File.basename(exe))
}
end
def modify!
unless @dylibs.empty?
opt = @dylibs.map{|dy| "-change #{dy} @executable_path/#{File.basename(dy)}"}.join(' ')
system "install_name_tool #{opt} #{@exe} && otool -L #{@exe}"
end
end
def modify_recursive!(basedir = File.dirname(@exe))
modify!
@dylibs.each{|dy|
path = File.join(basedir, File.basename(dy))
ModifyPath.new(path).modify_recursive!(basedir)
}
end
end
# カレントディレクトリに実行ファイルと参照されるdylibを置いて
# ./dylib_patch.rb <実行ファイル又はdylib>
ModifyPath.new(ARGV[0]).modify_recursive!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment