blob: 2ef375fc6fbe76a3b7a0a10600faaa3d7378bae2 [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
Dmitry Frolov94aa1a02024-11-13 12:43:40 +030030TIMEOUT_MULTIPLIER ?= 1
Daniel P. Berrangé41563252023-12-15 08:03:57 +010031.mtestargs = --no-rebuild -t $(TIMEOUT_MULTIPLIER)
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050032ifneq ($(SPEED), quick)
33.mtestargs += --setup $(SPEED)
34endif
35.mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS)))))
Paolo Bonzini245dac42020-01-28 14:48:54 +010036
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050037.check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
38.bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
Paolo Bonzini245dac42020-01-28 14:48:54 +010039
Paolo Bonzini40d9b742020-09-02 07:19:43 -040040introspect = json.load(sys.stdin)
Paolo Bonzini40d9b742020-09-02 07:19:43 -040041
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040042def process_tests(test, targets, suites):
Yonggang Luocb23fd42020-08-26 23:10:02 +080043 executable = test['cmd'][0]
44 try:
45 executable = os.path.relpath(executable)
46 except:
47 pass
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040048
Paolo Bonzini654d6b02021-02-09 14:59:26 +010049 deps = (targets.get(x, []) for x in test['depends'])
50 deps = itertools.chain.from_iterable(deps)
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050051 deps = list(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010052
53 test_suites = test['suite'] or ['default']
Paolo Bonzini245dac42020-01-28 14:48:54 +010054 for s in test_suites:
Marc-André Lureaubc848262023-03-02 17:18:45 +040055 # The suite name in the introspection info is "PROJECT" or "PROJECT:SUITE"
56 if ':' in s:
57 s = s.split(':')[1]
58 if s == 'slow' or s == 'thorough':
59 continue
Paolo Bonzini245dac42020-01-28 14:48:54 +010060 if s.endswith('-slow'):
61 s = s[:-5]
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050062 suites[s].speeds.append('slow')
Paolo Bonzini3e233e22021-11-09 14:13:00 +010063 if s.endswith('-thorough'):
64 s = s[:-9]
65 suites[s].speeds.append('thorough')
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050066 suites[s].deps.update(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010067
Paolo Bonzini40d9b742020-09-02 07:19:43 -040068def emit_prolog(suites, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050069 all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
70 all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
71 print()
72 print(f'all-{prefix}-targets = {all_targets}')
73 print(f'all-{prefix}-xml = {all_xml}')
74 print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
75 print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
76 print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
77 print(f'endif')
78 print(f'{prefix}-build: run-ninja')
79 print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
80 print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
81 print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
82 print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040083
Paolo Bonzini98487b92021-10-06 11:27:47 +020084def emit_suite_deps(name, suite, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050085 deps = ' '.join(suite.deps)
Paolo Bonzini9b32ba52022-05-27 16:35:44 +010086 targets = [f'{prefix}-{name}', f'{prefix}-report-{name}.junit.xml', f'{prefix}', f'{prefix}-report.junit.xml',
87 f'{prefix}-build']
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050088 print()
89 print(f'.{prefix}-{name}.deps = {deps}')
Paolo Bonzini9b32ba52022-05-27 16:35:44 +010090 for t in targets:
91 print(f'.ninja-goals.{t} += $(.{prefix}-{name}.deps)')
Paolo Bonzini98487b92021-10-06 11:27:47 +020092
93def emit_suite(name, suite, prefix):
94 emit_suite_deps(name, suite, prefix)
95 targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050096 print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
97 print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
98 print(f'endif')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040099
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400100targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
101 for t in introspect['targets']}
102
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400103testsuites = defaultdict(Suite)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400104for test in introspect['tests']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400105 process_tests(test, targets, testsuites)
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400106emit_prolog(testsuites, 'check')
107for name, suite in testsuites.items():
108 emit_suite(name, suite, 'check')
109
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400110benchsuites = defaultdict(Suite)
111for test in introspect['benchmarks']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400112 process_tests(test, targets, benchsuites)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400113emit_prolog(benchsuites, 'bench')
114for name, suite in benchsuites.items():
115 emit_suite(name, suite, 'bench')