blob: 02c0453e67efd556f7d1b4b54cd29bffd98bdd09 [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):
16 self.tests = list()
17 self.slow_tests = list()
18 self.executables = set()
19
20print('''
21SPEED = quick
22
Paolo Bonzini42d729e2020-09-01 09:58:48 -040023# $1 = environment, $2 = test command, $3 = test name, $4 = dir
Thomas Huthc2f4c1a2021-02-18 18:23:13 +010024.test-human-tap = $1 $(if $4,(cd $4 && $2),$2) -m $(SPEED) < /dev/null | ./scripts/tap-driver.pl --test-name="$3" $(if $(V),,--show-failures-only)
Paolo Bonzinid322e842020-09-01 09:14:41 -040025.test-human-exitcode = $1 $(PYTHON) scripts/test-driver.py $(if $4,-C$4) $(if $(V),--verbose) -- $2 < /dev/null
Paolo Bonzini42d729e2020-09-01 09:58:48 -040026.test-tap-tap = $1 $(if $4,(cd $4 && $2),$2) < /dev/null | sed "s/^[a-z][a-z]* [0-9]*/& $3/" || true
27.test-tap-exitcode = printf "%s\\n" 1..1 "`$1 $(if $4,(cd $4 && $2),$2) < /dev/null > /dev/null || echo "not "`ok 1 $3"
Paolo Bonzini40d9b742020-09-02 07:19:43 -040028.test.human-print = echo $(if $(V),'$1 $2','Running test $3') &&
Paolo Bonzini245dac42020-01-28 14:48:54 +010029.test.env = MALLOC_PERTURB_=$${MALLOC_PERTURB_:-$$(( $${RANDOM:-0} % 255 + 1))}
30
31# $1 = test name, $2 = test target (human or tap)
Paolo Bonzini40d9b742020-09-02 07:19:43 -040032.test.run = $(call .test.$2-print,$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1)) $(call .test-$2-$(.test.driver.$1),$(.test.env.$1),$(.test.cmd.$1),$(.test.name.$1),$(.test.dir.$1))
Paolo Bonzini245dac42020-01-28 14:48:54 +010033
Paolo Bonzini40d9b742020-09-02 07:19:43 -040034.test.output-format = human
Paolo Bonzini245dac42020-01-28 14:48:54 +010035''')
36
Paolo Bonzini40d9b742020-09-02 07:19:43 -040037introspect = json.load(sys.stdin)
Paolo Bonzini245dac42020-01-28 14:48:54 +010038i = 0
Paolo Bonzini40d9b742020-09-02 07:19:43 -040039
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040040def process_tests(test, targets, suites):
Paolo Bonzini40d9b742020-09-02 07:19:43 -040041 global i
Paolo Bonzini245dac42020-01-28 14:48:54 +010042 env = ' '.join(('%s=%s' % (shlex.quote(k), shlex.quote(v))
43 for k, v in test['env'].items()))
Yonggang Luocb23fd42020-08-26 23:10:02 +080044 executable = test['cmd'][0]
45 try:
46 executable = os.path.relpath(executable)
47 except:
48 pass
Paolo Bonzini245dac42020-01-28 14:48:54 +010049 if test['workdir'] is not None:
Yonggang Luocb23fd42020-08-26 23:10:02 +080050 try:
51 test['cmd'][0] = os.path.relpath(executable, test['workdir'])
52 except:
53 test['cmd'][0] = executable
Paolo Bonzini245dac42020-01-28 14:48:54 +010054 else:
55 test['cmd'][0] = executable
Paolo Bonzini555b27a2020-09-01 09:58:48 -040056 cmd = ' '.join((shlex.quote(x) for x in test['cmd']))
Paolo Bonzini245dac42020-01-28 14:48:54 +010057 driver = test['protocol'] if 'protocol' in test else 'exitcode'
58
59 i += 1
Paolo Bonzini42d729e2020-09-01 09:58:48 -040060 if test['workdir'] is not None:
61 print('.test.dir.%d := %s' % (i, shlex.quote(test['workdir'])))
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040062
Paolo Bonzini654d6b02021-02-09 14:59:26 +010063 deps = (targets.get(x, []) for x in test['depends'])
64 deps = itertools.chain.from_iterable(deps)
Paolo Bonzini48a81fd2020-09-04 10:06:06 -040065
Paolo Bonzini245dac42020-01-28 14:48:54 +010066 print('.test.name.%d := %s' % (i, test['name']))
67 print('.test.driver.%d := %s' % (i, driver))
Paolo Bonzini555b27a2020-09-01 09:58:48 -040068 print('.test.env.%d := $(.test.env) %s' % (i, env))
Paolo Bonzini245dac42020-01-28 14:48:54 +010069 print('.test.cmd.%d := %s' % (i, cmd))
Paolo Bonzini09e93322020-08-13 09:28:11 -040070 print('.test.deps.%d := %s' % (i, ' '.join(deps)))
Paolo Bonzini40d9b742020-09-02 07:19:43 -040071 print('.PHONY: run-test-%d' % (i,))
Paolo Bonzini09e93322020-08-13 09:28:11 -040072 print('run-test-%d: $(.test.deps.%d)' % (i,i))
Paolo Bonzini40d9b742020-09-02 07:19:43 -040073 print('\t@$(call .test.run,%d,$(.test.output-format))' % (i,))
Paolo Bonzini245dac42020-01-28 14:48:54 +010074
75 test_suites = test['suite'] or ['default']
76 is_slow = any(s.endswith('-slow') for s in test_suites)
77 for s in test_suites:
78 # The suite name in the introspection info is "PROJECT:SUITE"
79 s = s.split(':')[1]
80 if s.endswith('-slow'):
81 s = s[:-5]
82 if is_slow:
83 suites[s].slow_tests.append(i)
84 else:
85 suites[s].tests.append(i)
86 suites[s].executables.add(executable)
87
Paolo Bonzini40d9b742020-09-02 07:19:43 -040088def emit_prolog(suites, prefix):
89 all_tap = ' '.join(('%s-report-%s.tap' % (prefix, k) for k in suites.keys()))
90 print('.PHONY: %s %s-report.tap %s' % (prefix, prefix, all_tap))
91 print('%s: run-tests' % (prefix,))
92 print('%s-report.tap %s: %s-report%%.tap: all' % (prefix, all_tap, prefix))
93 print('''\t$(MAKE) .test.output-format=tap --quiet -Otarget V=1 %s$* | ./scripts/tap-merge.pl | tee "$@" \\
94 | ./scripts/tap-driver.pl $(if $(V),, --show-failures-only)''' % (prefix, ))
95
96def emit_suite(name, suite, prefix):
Paolo Bonzini245dac42020-01-28 14:48:54 +010097 executables = ' '.join(suite.executables)
98 slow_test_numbers = ' '.join((str(x) for x in suite.slow_tests))
99 test_numbers = ' '.join((str(x) for x in suite.tests))
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400100 target = '%s-%s' % (prefix, name)
101 print('.test.quick.%s := %s' % (target, test_numbers))
102 print('.test.slow.%s := $(.test.quick.%s) %s' % (target, target, slow_test_numbers))
103 print('%s-build: %s' % (prefix, executables))
104 print('.PHONY: %s' % (target, ))
105 print('.PHONY: %s-report-%s.tap' % (prefix, name))
106 print('%s: run-tests' % (target, ))
107 print('ifneq ($(filter %s %s, $(MAKECMDGOALS)),)' % (target, prefix))
108 print('.tests += $(.test.$(SPEED).%s)' % (target, ))
109 print('endif')
Alex Bennée47e34242021-02-02 13:39:57 +0000110 print('all-%s-targets += %s' % (prefix, target))
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400111
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400112targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
113 for t in introspect['targets']}
114
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400115testsuites = defaultdict(Suite)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400116for test in introspect['tests']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400117 process_tests(test, targets, testsuites)
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400118emit_prolog(testsuites, 'check')
119for name, suite in testsuites.items():
120 emit_suite(name, suite, 'check')
121
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400122benchsuites = defaultdict(Suite)
123for test in introspect['benchmarks']:
Paolo Bonzini48a81fd2020-09-04 10:06:06 -0400124 process_tests(test, targets, benchsuites)
Paolo Bonzini9ed72472020-09-02 07:25:19 -0400125emit_prolog(benchsuites, 'bench')
126for name, suite in benchsuites.items():
127 emit_suite(name, suite, 'bench')
128
Paolo Bonzini40d9b742020-09-02 07:19:43 -0400129print('run-tests: $(patsubst %, run-test-%, $(.tests))')