1# coding=utf-8
2#
3# Copyright © 2011 Intel Corporation
4#
5# Permission is hereby granted, free of charge, to any person obtaining a
6# copy of this software and associated documentation files (the "Software"),
7# to deal in the Software without restriction, including without limitation
8# the rights to use, copy, modify, merge, publish, distribute, sublicense,
9# and/or sell copies of the Software, and to permit persons to whom the
10# Software is furnished to do so, subject to the following conditions:
11#
12# The above copyright notice and this permission notice (including the next
13# paragraph) shall be included in all copies or substantial portions of the
14# Software.
15#
16# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22# DEALINGS IN THE SOFTWARE.
23
24# Compare two files containing IR code.  Ignore formatting differences
25# and declaration order.
26
27import os
28import os.path
29import subprocess
30import sys
31import tempfile
32
33from sexps import *
34
35if len(sys.argv) != 3:
36    print 'Usage: python2 ./compare_ir.py <file1> <file2>'
37    exit(1)
38
39with open(sys.argv[1]) as f:
40    ir1 = sort_decls(parse_sexp(f.read()))
41with open(sys.argv[2]) as f:
42    ir2 = sort_decls(parse_sexp(f.read()))
43
44if ir1 == ir2:
45    exit(0)
46else:
47    file1, path1 = tempfile.mkstemp(os.path.basename(sys.argv[1]))
48    file2, path2 = tempfile.mkstemp(os.path.basename(sys.argv[2]))
49    try:
50        os.write(file1, '{0}\n'.format(sexp_to_string(ir1)))
51        os.close(file1)
52        os.write(file2, '{0}\n'.format(sexp_to_string(ir2)))
53        os.close(file2)
54        subprocess.call(['diff', '-u', path1, path2])
55    finally:
56        os.remove(path1)
57        os.remove(path2)
58    exit(1)
59