blob: 4d542e8aaa0c4a28d3d1776c164c0a1e65807e68 [file] [log] [blame]
Paolo Bonzini245dac42020-01-28 14:48:54 +01001#! /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
Paolo Bonzini48a81fd2020-09-04 10:06:06 -04008import itertools
Paolo Bonzini245dac42020-01-28 14:48:54 +01009import json
10import os
11import shlex
12import sys
13
14class Suite(object):
15 def __init__(self):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050016 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
Paolo Bonzini245dac42020-01-28 14:48:54 +010022
23print('''
24SPEED = quick
25
Paolo Bonzini3e233e22021-11-09 14:13:00 +010026.speed.quick = $(foreach s,$(sort $(filter-out %-slow %-thorough, $1)), --suite $s)
27.speed.slow = $(foreach s,$(sort $(filter-out %-thorough, $1)), --suite $s)
28.speed.thorough = $(foreach s,$(sort $1), --suite $s)
Paolo Bonzini245dac42020-01-28 14:48:54 +010029
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050030.mtestargs = --no-rebuild -t 0
31ifneq ($(SPEED), quick)
32.mtestargs += --setup $(SPEED)
33endif
34.mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS)))))
Paolo Bonzini245dac42020-01-28 14:48:54 +010035
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050036.check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
37.bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
Paolo Bonzini245dac42020-01-28 14:48:54 +010038
Paolo Bonzini40d9b742020-09-02 07:19:43 -040039introspect = json.load(sys.stdin)
Paolo Bonzini40d9b742020-09-02 07:19:43 -040040
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040041def process_tests(test, targets, suites):
Yonggang Luocb23fd42020-08-26 23:10:02 +080042 executable = test['cmd'][0]
43 try:
44 executable = os.path.relpath(executable)
45 except:
46 pass
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040047
Paolo Bonzini654d6b02021-02-09 14:59:26 +010048 deps = (targets.get(x, []) for x in test['depends'])
49 deps = itertools.chain.from_iterable(deps)
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050050 deps = list(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010051
52 test_suites = test['suite'] or ['default']
Paolo Bonzini245dac42020-01-28 14:48:54 +010053 for s in test_suites:
54 # The suite name in the introspection info is "PROJECT:SUITE"
55 s = s.split(':')[1]
Paolo Bonzini3e233e22021-11-09 14:13:00 +010056 if s == 'slow' or s == 'thorough':
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050057 continue
Paolo Bonzini245dac42020-01-28 14:48:54 +010058 if s.endswith('-slow'):
59 s = s[:-5]
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050060 suites[s].speeds.append('slow')
Paolo Bonzini3e233e22021-11-09 14:13:00 +010061 if s.endswith('-thorough'):
62 s = s[:-9]
63 suites[s].speeds.append('thorough')
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050064 suites[s].deps.update(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010065
Paolo Bonzini40d9b742020-09-02 07:19:43 -040066def emit_prolog(suites, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050067 all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
68 all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
69 print()
70 print(f'all-{prefix}-targets = {all_targets}')
71 print(f'all-{prefix}-xml = {all_xml}')
72 print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
73 print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
74 print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
75 print(f'endif')
76 print(f'{prefix}-build: run-ninja')
77 print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
78 print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
79 print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
80 print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040081
Paolo Bonzini98487b92021-10-06 11:27:47 +020082def emit_suite_deps(name, suite, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050083 deps = ' '.join(suite.deps)
84 targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
85 print()
86 print(f'.{prefix}-{name}.deps = {deps}')
87 print(f'ifneq ($(filter {prefix}-build {targets}, $(MAKECMDGOALS)),)')
88 print(f'.{prefix}.build-suites += {name}')
89 print(f'endif')
Paolo Bonzini98487b92021-10-06 11:27:47 +020090
91def emit_suite(name, suite, prefix):
92 emit_suite_deps(name, suite, prefix)
93 targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050094 print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
95 print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
96 print(f'endif')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040097
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040098targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
99 for t in introspect['targets']}
100
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400101testsuites = defaultdict(Suite)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400102for test in introspect['tests']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400103 process_tests(test, targets, testsuites)
Paolo Bonzini98487b92021-10-06 11:27:47 +0200104# HACK: check-block is a separate target so that it runs with --verbose;
105# only write the dependencies
106emit_suite_deps('block', testsuites['block'], 'check')
107del testsuites['block']
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400108emit_prolog(testsuites, 'check')
109for name, suite in testsuites.items():
110 emit_suite(name, suite, 'check')
111
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400112benchsuites = defaultdict(Suite)
113for test in introspect['benchmarks']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400114 process_tests(test, targets, benchsuites)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400115emit_prolog(benchsuites, 'bench')
116for name, suite in benchsuites.items():
117 emit_suite(name, suite, 'bench')