| 1 | #! /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 | |
| 7 | from collections import defaultdict |
| 8 | import itertools |
| 9 | import json |
| 10 | import os |
| 11 | import sys |
| 12 | |
| 13 | class Suite(object): |
| 14 | def __init__(self): |
| 15 | self.deps = set() |
| 16 | self.speeds = set() |
| 17 | |
| 18 | def names(self, base): |
| 19 | return [f'{base}-{speed}' for speed in self.speeds] |
| 20 | |
| 21 | |
| 22 | print(r''' |
| 23 | SPEED = quick |
| 24 | |
| 25 | .speed.quick = $(sort $(filter-out %-slow %-thorough %-optional, $1)) |
| 26 | .speed.slow = $(sort $(filter-out %-thorough, $1)) |
| 27 | .speed.thorough = $(sort $1) |
| 28 | |
| 29 | TIMEOUT_MULTIPLIER ?= 1 |
| 30 | .mtestargs = --no-rebuild -t $(TIMEOUT_MULTIPLIER) |
| 31 | ifneq ($(SPEED), quick) |
| 32 | .mtestargs += --setup $(SPEED) |
| 33 | endif |
| 34 | .mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS))))) |
| 35 | |
| 36 | .check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs) \ |
| 37 | $(foreach s, $(sort $(.check.mtest-suites)), --suite $s) |
| 38 | .bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose \ |
| 39 | $(foreach s, $(sort $(.bench.mtest-suites)), --suite $s)''') |
| 40 | |
| 41 | introspect = json.load(sys.stdin) |
| 42 | |
| 43 | def process_tests(test, targets, suites): |
| 44 | executable = test['cmd'][0] |
| 45 | try: |
| 46 | executable = os.path.relpath(executable) |
| 47 | except: |
| 48 | pass |
| 49 | |
| 50 | deps = (targets.get(x, []) for x in test['depends']) |
| 51 | deps = itertools.chain.from_iterable(deps) |
| 52 | deps = list(deps) |
| 53 | |
| 54 | test_suites = test['suite'] or ['default'] |
| 55 | for s in test_suites: |
| 56 | # The suite name in the introspection info is "PROJECT" or "PROJECT:SUITE" |
| 57 | if ':' in s: |
| 58 | s = s.split(':')[1] |
| 59 | if s == 'slow' or s == 'thorough': |
| 60 | continue |
| 61 | suites[s].deps.update(deps) |
| 62 | if s.endswith('-slow'): |
| 63 | s = s[:-5] |
| 64 | suites[s].speeds.add('slow') |
| 65 | if s.endswith('-thorough'): |
| 66 | s = s[:-9] |
| 67 | suites[s].speeds.add('thorough') |
| 68 | |
| 69 | def target_name(suite): |
| 70 | if suite.endswith('-optional'): |
| 71 | return suite[0:-9] |
| 72 | return suite |
| 73 | |
| 74 | def emit_prolog(suites, prefix): |
| 75 | all_targets = ' '.join((f'{prefix}-{target_name(k)}' |
| 76 | for k in sorted(suites.keys()))) |
| 77 | all_xml = ' '.join((f'{prefix}-report-{target_name(k)}.junit.xml' |
| 78 | for k in sorted(suites.keys()))) |
| 79 | print() |
| 80 | print(f'all-{prefix}-targets = {all_targets}') |
| 81 | print(f'all-{prefix}-xml = {all_xml}') |
| 82 | print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)') |
| 83 | print(f'ninja-cmd-goals += $(foreach s, $(.{prefix}.mtest-suites), $(.{prefix}-$s.deps))') |
| 84 | print(f'{prefix}-build: run-ninja') |
| 85 | print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}') |
| 86 | print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)') |
| 87 | print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja') |
| 88 | print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .') |
| 89 | |
| 90 | def emit_suite(name, suite, prefix): |
| 91 | tgtname = target_name(name) |
| 92 | deps = ' '.join(sorted(suite.deps)) |
| 93 | print() |
| 94 | print(f'.{prefix}-{tgtname}.deps = {deps}') |
| 95 | print(f'.ninja-goals.check-build += $(.{prefix}-{tgtname}.deps)') |
| 96 | |
| 97 | names = ' '.join(sorted(suite.names(name))) |
| 98 | targets = f'{prefix}-{tgtname} {prefix}-report-{tgtname}.junit.xml' |
| 99 | if not name.endswith('-slow') and \ |
| 100 | not name.endswith('-thorough') and \ |
| 101 | not name.endswith('-optional'): |
| 102 | targets += f' {prefix} {prefix}-report.junit.xml' |
| 103 | print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)') |
| 104 | # for the "base" suite possibly add FOO-slow and FOO-thorough |
| 105 | print(f".{prefix}.mtest-suites += {name} $(call .speed.$(SPEED), {names})") |
| 106 | print(f'endif') |
| 107 | |
| 108 | targets = {t['id']: [os.path.relpath(f) for f in t['filename']] |
| 109 | for t in introspect['targets']} |
| 110 | |
| 111 | testsuites = defaultdict(Suite) |
| 112 | for test in introspect['tests']: |
| 113 | process_tests(test, targets, testsuites) |
| 114 | emit_prolog(testsuites, 'check') |
| 115 | for name, suite in testsuites.items(): |
| 116 | emit_suite(name, suite, 'check') |
| 117 | |
| 118 | benchsuites = defaultdict(Suite) |
| 119 | for test in introspect['benchmarks']: |
| 120 | process_tests(test, targets, benchsuites) |
| 121 | emit_prolog(benchsuites, 'bench') |
| 122 | for name, suite in benchsuites.items(): |
| 123 | emit_suite(name, suite, 'bench') |