blob: 7067bdadf57b2384fa93d5482d0d5c443688d1bf [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 Bonzini3d2f73e2021-02-11 06:15:12 -050026.speed.quick = $(foreach s,$(sort $(filter-out %-slow, $1)), --suite $s)
27.speed.slow = $(foreach s,$(sort $1), --suite $s)
Paolo Bonzini245dac42020-01-28 14:48:54 +010028
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050029.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)))))
Paolo Bonzini245dac42020-01-28 14:48:54 +010034
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050035.check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
36.bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
Paolo Bonzini245dac42020-01-28 14:48:54 +010037
Paolo Bonzini40d9b742020-09-02 07:19:43 -040038introspect = json.load(sys.stdin)
Paolo Bonzini40d9b742020-09-02 07:19:43 -040039
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040040def process_tests(test, targets, suites):
Yonggang Luocb23fd42020-08-26 23:10:02 +080041 executable = test['cmd'][0]
42 try:
43 executable = os.path.relpath(executable)
44 except:
45 pass
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040046
Paolo Bonzini654d6b02021-02-09 14:59:26 +010047 deps = (targets.get(x, []) for x in test['depends'])
48 deps = itertools.chain.from_iterable(deps)
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050049 deps = list(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010050
51 test_suites = test['suite'] or ['default']
Paolo Bonzini245dac42020-01-28 14:48:54 +010052 for s in test_suites:
53 # The suite name in the introspection info is "PROJECT:SUITE"
54 s = s.split(':')[1]
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050055 if s == 'slow':
56 continue
Paolo Bonzini245dac42020-01-28 14:48:54 +010057 if s.endswith('-slow'):
58 s = s[:-5]
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050059 suites[s].speeds.append('slow')
60 suites[s].deps.update(deps)
Paolo Bonzini245dac42020-01-28 14:48:54 +010061
Paolo Bonzini40d9b742020-09-02 07:19:43 -040062def emit_prolog(suites, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050063 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/$@ .')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040077
78def emit_suite(name, suite, prefix):
Paolo Bonzini3d2f73e2021-02-11 06:15:12 -050079 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')
Paolo Bonzini40d9b742020-09-02 07:19:43 -040089
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040090targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
91 for t in introspect['targets']}
92
Paolo Bonzini40d9b742020-09-02 07:19:43 -040093testsuites = defaultdict(Suite)
Paolo Bonzini9ed72472020-09-02 07:25:19 -040094for test in introspect['tests']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040095 process_tests(test, targets, testsuites)
Paolo Bonzini40d9b742020-09-02 07:19:43 -040096emit_prolog(testsuites, 'check')
97for name, suite in testsuites.items():
98 emit_suite(name, suite, 'check')
99
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400100benchsuites = defaultdict(Suite)
101for test in introspect['benchmarks']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400102 process_tests(test, targets, benchsuites)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400103emit_prolog(benchsuites, 'bench')
104for name, suite in benchsuites.items():
105 emit_suite(name, suite, 'bench')