Suggested by Thomas Martitz, don't check for track-specific files when searching...
[kugel-rb.git] / utils / analysis / objdiff.py
blobdc51441bfa59c5b3a9812f078c315a3b7b9ced4a
1 #!/usr/bin/python
2 import sys
3 from subprocess import Popen, PIPE
5 if len(sys.argv) != 3:
6 print """%s usage:
7 %s obj1 obj2
8 Calculate per-symbol and total size differences between obj1 and obj2,
9 which may be any files that nm can read""" % ((sys.argv[0],)*2)
10 sys.exit(2)
12 obj1 = sys.argv[1]
13 obj2 = sys.argv[2]
15 def getsyms(obj):
16 proc = Popen(args=['nm', '-S', '-t', 'd', obj], stdout=PIPE, stderr=PIPE)
17 out, err = proc.communicate()
18 if err:
19 print "nm reported an error:\n"
20 print err
21 sys.exit(1)
22 d = {}
23 for l in out.splitlines():
24 l = l.strip().split()
25 if len(l) == 4:
26 d[l[3]] = int(l[1])
27 return d
29 diff = 0
31 d1 = getsyms(obj1)
32 d2 = getsyms(obj2)
33 l = [(k,v) for k,v in sorted(d1.items()) if k not in d2]
34 if l:
35 print "only in %s" % obj1
36 print ''.join(" %6d %s\n" % (v,k) for k,v in l)
37 diff -= sum(v for k,v in l)
39 l = [(k,v) for k,v in sorted(d2.items()) if k not in d1]
40 if l:
41 print "only in %s" % obj2
42 print ''.join("%6d %s\n" % (v,k) for k,v in l)
43 diff += sum(v for k,v in l)
45 l = [(k,v,d2[k]) for k,v in sorted(d1.items()) if k in d2 and d2[k] != v]
46 if l:
47 print "different sizes in %s and %s:" %(obj1, obj2)
48 print ''.join(" %6d %6d %s\n" % (v1,v2,k) for k,v1,v2 in l)
49 diff += sum(v2-v1 for k,v1,v2 in l)
51 if diff:
52 print "total size difference: %+d" % diff
53 else:
54 print "total size difference: 0"