1#!/usr/bin/env python
2
3import subprocess
4import difflib
5
6def capture_2(args0, args1):
7    import subprocess
8    p0 = subprocess.Popen(args0, stdin=None, stdout=subprocess.PIPE,
9                          stderr=subprocess.PIPE)
10    p1 = subprocess.Popen(args1, stdin=p0.stdout, stdout=subprocess.PIPE,
11                          stderr=subprocess.PIPE)
12    out,_ = p1.communicate()
13    return out
14
15def normalize_nm(data):    
16    lines = data.split('\n')
17    lines.sort()
18
19    # FIXME: Ignore common symbols for now.
20    lines = [ln for ln in lines
21             if not ln.startswith('         C')]
22
23    return lines
24
25def main():
26    import sys
27    clang = sys.argv[1]
28    flags = sys.argv[2:]
29
30    # FIXME: Relax to include undefined symbols.
31    nm_args = ["llvm-nm", "-extern-only", "-defined-only"]
32
33    llvmgcc_args = ["llvm-gcc"] + flags + ["-emit-llvm","-c","-o","-"]
34    clang_args = [clang] + flags + ["-emit-llvm","-c","-o","-"]
35
36    llvmgcc_nm = capture_2(llvmgcc_args, nm_args)
37    clang_nm = capture_2(clang_args, nm_args)
38
39    llvmgcc_nm = normalize_nm(llvmgcc_nm)
40    clang_nm = normalize_nm(clang_nm)
41
42    if llvmgcc_nm == clang_nm:
43        sys.exit(0)
44
45    print ' '.join(llvmgcc_args), '|', ' '.join(nm_args)
46    print ' '.join(clang_args), '|', ' '.join(nm_args)
47    for line in difflib.unified_diff(llvmgcc_nm, clang_nm,
48                                     fromfile="llvm-gcc symbols",
49                                     tofile="clang symbols"):
50        print line
51    sys.exit(1)
52
53if __name__ == '__main__':
54    main()
55