xref: /qemu/scripts/mtest2make.py (revision b83a80e8)
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 itertools
9import json
10import os
11import shlex
12import sys
13
14class Suite(object):
15    def __init__(self):
16        self.deps = set()
17        self.speeds = ['quick']
18
19    def names(self, base):
20        return [base if speed == 'quick' else f'{base}-{speed}' for speed in self.speeds]
21
22
23print('''
24SPEED = quick
25
26.speed.quick = $(foreach s,$(sort $(filter-out %-slow, $1)), --suite $s)
27.speed.slow = $(foreach s,$(sort $1), --suite $s)
28
29.mtestargs = --no-rebuild -t 0
30ifneq ($(SPEED), quick)
31.mtestargs += --setup $(SPEED)
32endif
33.mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS)))))
34
35.check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
36.bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
37
38introspect = json.load(sys.stdin)
39
40def process_tests(test, targets, suites):
41    executable = test['cmd'][0]
42    try:
43        executable = os.path.relpath(executable)
44    except:
45        pass
46
47    deps = (targets.get(x, []) for x in test['depends'])
48    deps = itertools.chain.from_iterable(deps)
49    deps = list(deps)
50
51    test_suites = test['suite'] or ['default']
52    for s in test_suites:
53        # The suite name in the introspection info is "PROJECT:SUITE"
54        s = s.split(':')[1]
55        if s == 'slow':
56            continue
57        if s.endswith('-slow'):
58            s = s[:-5]
59            suites[s].speeds.append('slow')
60        suites[s].deps.update(deps)
61
62def emit_prolog(suites, prefix):
63    all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
64    all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
65    print()
66    print(f'all-{prefix}-targets = {all_targets}')
67    print(f'all-{prefix}-xml = {all_xml}')
68    print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
69    print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
70    print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
71    print(f'endif')
72    print(f'{prefix}-build: run-ninja')
73    print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
74    print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
75    print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
76    print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
77
78def emit_suite(name, suite, prefix):
79    deps = ' '.join(suite.deps)
80    targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
81    print()
82    print(f'.{prefix}-{name}.deps = {deps}')
83    print(f'ifneq ($(filter {prefix}-build {targets}, $(MAKECMDGOALS)),)')
84    print(f'.{prefix}.build-suites += {name}')
85    print(f'endif')
86    print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
87    print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
88    print(f'endif')
89
90targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
91           for t in introspect['targets']}
92
93testsuites = defaultdict(Suite)
94for test in introspect['tests']:
95    process_tests(test, targets, testsuites)
96emit_prolog(testsuites, 'check')
97for name, suite in testsuites.items():
98    emit_suite(name, suite, 'check')
99
100benchsuites = defaultdict(Suite)
101for test in introspect['benchmarks']:
102    process_tests(test, targets, benchsuites)
103emit_prolog(benchsuites, 'bench')
104for name, suite in benchsuites.items():
105    emit_suite(name, suite, 'bench')
106