1bf215546Sopenharmony_ci# encoding=utf-8
2bf215546Sopenharmony_ci# Copyright © 2018 Intel Corporation
3bf215546Sopenharmony_ci
4bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a copy
5bf215546Sopenharmony_ci# of this software and associated documentation files (the "Software"), to deal
6bf215546Sopenharmony_ci# in the Software without restriction, including without limitation the rights
7bf215546Sopenharmony_ci# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8bf215546Sopenharmony_ci# copies of the Software, and to permit persons to whom the Software is
9bf215546Sopenharmony_ci# furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci
11bf215546Sopenharmony_ci# The above copyright notice and this permission notice shall be included in
12bf215546Sopenharmony_ci# all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci
14bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17bf215546Sopenharmony_ci# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19bf215546Sopenharmony_ci# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20bf215546Sopenharmony_ci# SOFTWARE.
21bf215546Sopenharmony_ci
22bf215546Sopenharmony_ci"""Script to generate and run glsl optimization tests."""
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ciimport argparse
25bf215546Sopenharmony_ciimport difflib
26bf215546Sopenharmony_ciimport errno
27bf215546Sopenharmony_ciimport os
28bf215546Sopenharmony_ciimport subprocess
29bf215546Sopenharmony_ciimport sys
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ciimport sexps
32bf215546Sopenharmony_ciimport lower_jump_cases
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci# The meson version handles windows paths better, but if it's not available
35bf215546Sopenharmony_ci# fall back to shlex
36bf215546Sopenharmony_citry:
37bf215546Sopenharmony_ci    from meson.mesonlib import split_args
38bf215546Sopenharmony_ciexcept ImportError:
39bf215546Sopenharmony_ci    from shlex import split as split_args
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_cidef arg_parser():
43bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
44bf215546Sopenharmony_ci    parser.add_argument(
45bf215546Sopenharmony_ci        '--test-runner',
46bf215546Sopenharmony_ci        required=True,
47bf215546Sopenharmony_ci        help='The glsl_test binary.')
48bf215546Sopenharmony_ci    return parser.parse_args()
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_cidef compare(actual, expected):
52bf215546Sopenharmony_ci    """Compare the s-expresions and return a diff if they are different."""
53bf215546Sopenharmony_ci    actual = sexps.sort_decls(sexps.parse_sexp(actual))
54bf215546Sopenharmony_ci    expected = sexps.sort_decls(sexps.parse_sexp(expected))
55bf215546Sopenharmony_ci
56bf215546Sopenharmony_ci    if actual == expected:
57bf215546Sopenharmony_ci        return None
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci    actual = sexps.sexp_to_string(actual)
60bf215546Sopenharmony_ci    expected = sexps.sexp_to_string(expected)
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci    return difflib.unified_diff(expected.splitlines(), actual.splitlines())
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cidef get_test_runner(runner):
66bf215546Sopenharmony_ci    """Wrap the test runner in the exe wrapper if necessary."""
67bf215546Sopenharmony_ci    wrapper = os.environ.get('MESON_EXE_WRAPPER', None)
68bf215546Sopenharmony_ci    if wrapper is None:
69bf215546Sopenharmony_ci        return [runner]
70bf215546Sopenharmony_ci    return split_args(wrapper) + [runner]
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cidef main():
74bf215546Sopenharmony_ci    """Generate each test and report pass or fail."""
75bf215546Sopenharmony_ci    args = arg_parser()
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_ci    total = 0
78bf215546Sopenharmony_ci    passes = 0
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci    runner = get_test_runner(args.test_runner)
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci    for gen in lower_jump_cases.CASES:
83bf215546Sopenharmony_ci        for name, opt, source, expected in gen():
84bf215546Sopenharmony_ci            total += 1
85bf215546Sopenharmony_ci            print('{}: '.format(name), end='')
86bf215546Sopenharmony_ci            proc = subprocess.Popen(
87bf215546Sopenharmony_ci                runner + ['optpass', '--quiet', '--input-ir', opt],
88bf215546Sopenharmony_ci                stdout=subprocess.PIPE,
89bf215546Sopenharmony_ci                stderr=subprocess.PIPE,
90bf215546Sopenharmony_ci                stdin=subprocess.PIPE)
91bf215546Sopenharmony_ci            out, err = proc.communicate(source.encode('utf-8'))
92bf215546Sopenharmony_ci            out = out.decode('utf-8')
93bf215546Sopenharmony_ci            err = err.decode('utf-8')
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci            if proc.returncode == 255:
96bf215546Sopenharmony_ci                print("Test returned general error, possibly missing linker")
97bf215546Sopenharmony_ci                sys.exit(77)
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci            if err:
100bf215546Sopenharmony_ci                print('FAIL')
101bf215546Sopenharmony_ci                print('Unexpected output on stderr: {}'.format(err),
102bf215546Sopenharmony_ci                      file=sys.stdout)
103bf215546Sopenharmony_ci                continue
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci            result = compare(out, expected)
106bf215546Sopenharmony_ci            if result is not None:
107bf215546Sopenharmony_ci                print('FAIL')
108bf215546Sopenharmony_ci                for l in result:
109bf215546Sopenharmony_ci                    print(l, file=sys.stderr)
110bf215546Sopenharmony_ci            else:
111bf215546Sopenharmony_ci                print('PASS')
112bf215546Sopenharmony_ci                passes += 1
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci    print('{}/{} tests returned correct results'.format(passes, total))
115bf215546Sopenharmony_ci    exit(0 if passes == total else 1)
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ciif __name__ == '__main__':
119bf215546Sopenharmony_ci    try:
120bf215546Sopenharmony_ci        main()
121bf215546Sopenharmony_ci    except OSError as e:
122bf215546Sopenharmony_ci        if e.errno == errno.ENOEXEC:
123bf215546Sopenharmony_ci            print('Skipping due to inability to run host binaries', file=sys.stderr)
124bf215546Sopenharmony_ci            sys.exit(77)
125bf215546Sopenharmony_ci        raise
126