17ec681f3Smrg#!/usr/bin/env python3
27ec681f3Smrg
37ec681f3Smrgimport argparse
47ec681f3Smrgimport difflib
57ec681f3Smrgimport errno
67ec681f3Smrgimport os
77ec681f3Smrgimport pathlib
87ec681f3Smrgimport subprocess
97ec681f3Smrgimport sys
107ec681f3Smrg
117ec681f3Smrg# The meson version handles windows paths better, but if it's not available
127ec681f3Smrg# fall back to shlex
137ec681f3Smrgtry:
147ec681f3Smrg    from meson.mesonlib import split_args
157ec681f3Smrgexcept ImportError:
167ec681f3Smrg    from shlex import split as split_args
177ec681f3Smrg
187ec681f3Smrgparser = argparse.ArgumentParser()
197ec681f3Smrgparser.add_argument('--i965_asm',
207ec681f3Smrg                    help='path to i965_asm binary')
217ec681f3Smrgparser.add_argument('--gen_name',
227ec681f3Smrg                    help='name of the hardware generation (as understood by i965_asm)')
237ec681f3Smrgparser.add_argument('--gen_folder',
247ec681f3Smrg                    type=pathlib.Path,
257ec681f3Smrg                    help='name of the folder for the generation')
267ec681f3Smrgargs = parser.parse_args()
277ec681f3Smrg
287ec681f3Smrgwrapper = os.environ.get('MESON_EXE_WRAPPER')
297ec681f3Smrgif wrapper is not None:
307ec681f3Smrg    i965_asm = split_args(wrapper) + [args.i965_asm]
317ec681f3Smrgelse:
327ec681f3Smrg    i965_asm = [args.i965_asm]
337ec681f3Smrg
347ec681f3Smrgsuccess = True
357ec681f3Smrg
367ec681f3Smrgfor asm_file in args.gen_folder.glob('*.asm'):
377ec681f3Smrg    expected_file = asm_file.stem + '.expected'
387ec681f3Smrg    expected_path = args.gen_folder / expected_file
397ec681f3Smrg
407ec681f3Smrg    try:
417ec681f3Smrg        command = i965_asm + [
427ec681f3Smrg            '--type', 'hex',
437ec681f3Smrg            '--gen', args.gen_name,
447ec681f3Smrg            asm_file
457ec681f3Smrg        ]
467ec681f3Smrg        with subprocess.Popen(command,
477ec681f3Smrg                              stdout=subprocess.PIPE,
487ec681f3Smrg                              stderr=subprocess.DEVNULL) as cmd:
497ec681f3Smrg            lines_after = [line.decode('ascii') for line in cmd.stdout.readlines()]
507ec681f3Smrg    except OSError as e:
517ec681f3Smrg        if e.errno == errno.ENOEXEC:
527ec681f3Smrg            print('Skipping due to inability to run host binaries.',
537ec681f3Smrg                  file=sys.stderr)
547ec681f3Smrg            exit(77)
557ec681f3Smrg        raise
567ec681f3Smrg
577ec681f3Smrg    with expected_path.open() as f:
587ec681f3Smrg        lines_before = f.readlines()
597ec681f3Smrg
607ec681f3Smrg    diff = ''.join(difflib.unified_diff(lines_before, lines_after,
617ec681f3Smrg                                        expected_file, asm_file.stem + '.out'))
627ec681f3Smrg
637ec681f3Smrg    if diff:
647ec681f3Smrg        print('Output comparison for {}:'.format(asm_file.name))
657ec681f3Smrg        print(diff)
667ec681f3Smrg        success = False
677ec681f3Smrg    else:
687ec681f3Smrg        print('{} : PASS'.format(asm_file.name))
697ec681f3Smrg
707ec681f3Smrgif not success:
717ec681f3Smrg    exit(1)
72