Skip to content

Instantly share code, notes, and snippets.

@kimushu
Last active August 29, 2015 14:16
Show Gist options
  • Save kimushu/c6395f2035376633e22e to your computer and use it in GitHub Desktop.
Save kimushu/c6395f2035376633e22e to your computer and use it in GitHub Desktop.
Dependency listing tool (for .a/.o files)
#!/usr/bin/env ruby
prefix = $0.split("/").last.sub(/[^-]+$/, "")
objs = {}
ARGV.each {|f|
n = f
%x[#{prefix}nm #{f}].split("\n").each {|line|
next if line == ""
next n = "#{f}(#$1)" if line =~ /^([^: ]+):$/
objs[n] ||= {:imports => [], :exports => [], :depends => {}, :required => {}}
if line =~ /^\s+U ([^ ]+)$/
objs[n][:imports] << $1
elsif line =~ /^[0-9a-f]+ [A-TV-Z] ([^ ]+)$/
objs[n][:exports] << $1
end
}
}
objs.each {|n,o|
o[:imports].each {|s|
objs.select {|n2,o2| o2[:exports].include?(s) }.each_key {|n3|
(o[:depends][n3] ||= []) << s
(objs[n3][:required][n] ||= []) << s
}
}
puts("#{n}:")
[{:title => "Depends on", :arrow => "=>", :data => o[:depends]},
{:title => "Required by", :arrow => "<=", :data => o[:required]}].each {|c|
puts(" #{c[:title]}")
puts(" (none)") if c[:data].empty?
c[:data].each {|n2,s2|
puts(" #{c[:arrow]} #{n2}")
puts(" (#{s2[0...2].join(", ")}#{s2.size>2 ? ", ..." : ""})")
}
}
puts
}
# vim: set et sts=2 sw=2:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment