xref: /openbmc/qemu/scripts/mtest2make.py (revision c51a3f5d)
1#! /usr/bin/env python3
2
3# Create Makefile targets to run tests, from Meson's test introspection data.
4#
5# Author: Paolo Bonzini <pbonzini@redhat.com>
6
7from collections import defaultdict
8import json
9import os
10import shlex
11import sys
12
13class Suite(object):
14    def __init__(self):
15        self.tests = list()
16        self.slow_tests = list()
17        self.executables = set()
18
19print('''
20SPEED = quick
21
22# $1 = environment, $2 = test command, $3 = test name, $4 = dir
23.test-human-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | ./scripts/tap-driver.pl --test-name="$3" $(if $(V),,--show-failures-only)
24.test-human-exitcode = $1 $(PYTHON) scripts/test-driver.py $(if $4,-C$4) $(if $(V),--verbose) -- $2 < /dev/null
25.test-tap-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | sed "s/^[a-z][a-z]* [0-9]*/& $3/" || true
26.test-tap-exitcode = printf "%s\\n" 1..1 "`$1 $(if $4,(cd $4 && $2),$2) < /dev/null > /dev/null || echo "not "`ok 1 $3"
27.test.human-print = echo $(if $(V),'$1 $2','Running test $3') &&
28.test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))}
29
30# $1 = test name, $2 = test target (human or tap)
31.test.run = $(call .test.$2-print,$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1)) $(call .test-$2-$(.test.driver.$1),$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1),$(.test.dir.$1))
32
33.test.output-format = human
34''')
35
36introspect = json.load(sys.stdin)
37i = 0
38
39def process_tests(test, suites):
40    global i
41    env = ' '.join(('%s=%s' % (shlex.quote(k), shlex.quote(v))
42                    for k, v in test['env'].items()))
43    executable = test['cmd'][0]
44    try:
45        executable = os.path.relpath(executable)
46    except:
47        pass
48    if test['workdir'] is not None:
49        try:
50            test['cmd'][0] = os.path.relpath(executable, test['workdir'])
51        except:
52            test['cmd'][0] = executable
53    else:
54        test['cmd'][0] = executable
55    cmd = ' '.join((shlex.quote(x) for x in test['cmd']))
56    driver = test['protocol'] if 'protocol' in test else 'exitcode'
57
58    i += 1
59    if test['workdir'] is not None:
60        print('.test.dir.%d := %s' % (i, shlex.quote(test['workdir'])))
61    print('.test.name.%d := %s' % (i, test['name']))
62    print('.test.driver.%d := %s' % (i, driver))
63    print('.test.env.%d := $(.test.env) %s' % (i, env))
64    print('.test.cmd.%d := %s' % (i, cmd))
65    print('.PHONY: run-test-%d' % (i,))
66    print('run-test-%d: all' % (i,))
67    print('\t@$(call .test.run,%d,$(.test.output-format))' % (i,))
68
69    test_suites = test['suite'] or ['default']
70    is_slow = any(s.endswith('-slow') for s in test_suites)
71    for s in test_suites:
72        # The suite name in the introspection info is "PROJECT:SUITE"
73        s = s.split(':')[1]
74        if s.endswith('-slow'):
75            s = s[:-5]
76        if is_slow:
77            suites[s].slow_tests.append(i)
78        else:
79            suites[s].tests.append(i)
80        suites[s].executables.add(executable)
81
82def emit_prolog(suites, prefix):
83    all_tap = ' '.join(('%s-report-%s.tap' % (prefix, k) for k in suites.keys()))
84    print('.PHONY: %s %s-report.tap %s' % (prefix, prefix, all_tap))
85    print('%s: run-tests' % (prefix,))
86    print('%s-report.tap %s: %s-report%%.tap: all' % (prefix, all_tap, prefix))
87    print('''\t$(MAKE) .test.output-format=tap --quiet -Otarget V=1 %s$* | ./scripts/tap-merge.pl | tee "$@" \\
88              | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only)''' % (prefix, ))
89
90def emit_suite(name, suite, prefix):
91    executables = ' '.join(suite.executables)
92    slow_test_numbers = ' '.join((str(x) for x in suite.slow_tests))
93    test_numbers = ' '.join((str(x) for x in suite.tests))
94    target = '%s-%s' % (prefix, name)
95    print('.test.quick.%s := %s' % (target, test_numbers))
96    print('.test.slow.%s := $(.test.quick.%s) %s' % (target, target, slow_test_numbers))
97    print('%s-build: %s' % (prefix, executables))
98    print('.PHONY: %s' % (target, ))
99    print('.PHONY: %s-report-%s.tap' % (prefix, name))
100    print('%s: run-tests' % (target, ))
101    print('ifneq ($(filter %s %s, $(MAKECMDGOALS)),)' % (target, prefix))
102    print('.tests += $(.test.$(SPEED).%s)' % (target, ))
103    print('endif')
104
105testsuites = defaultdict(Suite)
106for test in introspect['tests']:
107    process_tests(test, testsuites)
108emit_prolog(testsuites, 'check')
109for name, suite in testsuites.items():
110    emit_suite(name, suite, 'check')
111
112benchsuites = defaultdict(Suite)
113for test in introspect['benchmarks']:
114    process_tests(test, benchsuites)
115emit_prolog(benchsuites, 'bench')
116for name, suite in benchsuites.items():
117    emit_suite(name, suite, 'bench')
118
119print('run-tests: $(patsubst %, run-test-%, $(.tests))')
120