blob: 179dd54871824ffc08f9c0c16a8179e687fd01e9 [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:
Marc-André Lureaubc848262023-03-02 17:18:45 +040054 # The suite name in the introspection info is "PROJECT" or "PROJECT:SUITE"
55 if ':' in s:
56 s = s.split(':')[1]
57 if s == 'slow' or s == 'thorough':
58 continue
Paolo Bonzini245dac42020-01-28 14:48:54 +010059 if s.endswith('-slow'):
60 s = s[:-5]
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050061 suites[s].speeds.append('slow')
Paolo Bonzini3e233e22021-11-09 14:13:00 +010062 if s.endswith('-thorough'):
63 s = s[:-9]
64 suites[s].speeds.append('thorough')
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050065 suites[s].deps.update(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010066
Paolo Bonzini40d9b742020-09-02 07:19:43 -040067def emit_prolog(suites, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050068 all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
69 all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
70 print()
71 print(f'all-{prefix}-targets = {all_targets}')
72 print(f'all-{prefix}-xml = {all_xml}')
73 print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
74 print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
75 print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
76 print(f'endif')
77 print(f'{prefix}-build: run-ninja')
78 print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
79 print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
80 print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
81 print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040082
Paolo Bonzini98487b92021-10-06 11:27:47 +020083def emit_suite_deps(name, suite, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050084 deps = ' '.join(suite.deps)
Paolo Bonzini9b32ba52022-05-27 16:35:44 +010085 targets = [f'{prefix}-{name}', f'{prefix}-report-{name}.junit.xml', f'{prefix}', f'{prefix}-report.junit.xml',
86 f'{prefix}-build']
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050087 print()
88 print(f'.{prefix}-{name}.deps = {deps}')
Paolo Bonzini9b32ba52022-05-27 16:35:44 +010089 for t in targets:
90 print(f'.ninja-goals.{t} += $(.{prefix}-{name}.deps)')
Paolo Bonzini98487b92021-10-06 11:27:47 +020091
92def emit_suite(name, suite, prefix):
93 emit_suite_deps(name, suite, prefix)
94 targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050095 print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
96 print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
97 print(f'endif')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040098
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040099targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
100 for t in introspect['targets']}
101
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400102testsuites = defaultdict(Suite)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400103for test in introspect['tests']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400104 process_tests(test, targets, testsuites)
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400105emit_prolog(testsuites, 'check')
106for name, suite in testsuites.items():
107 emit_suite(name, suite, 'check')
108
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400109benchsuites = defaultdict(Suite)
110for test in introspect['benchmarks']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400111 process_tests(test, targets, benchsuites)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400112emit_prolog(benchsuites, 'bench')
113for name, suite in benchsuites.items():
114 emit_suite(name, suite, 'bench')