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