mtest2make.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #! /usr/bin/env python3
  2. # Create Makefile targets to run tests, from Meson's test introspection data.
  3. #
  4. # Author: Paolo Bonzini <pbonzini@redhat.com>
  5. from collections import defaultdict
  6. import itertools
  7. import json
  8. import os
  9. import shlex
  10. import sys
  11. class Suite(object):
  12. def __init__(self):
  13. self.deps = set()
  14. self.speeds = ['quick']
  15. def names(self, base):
  16. return [base if speed == 'quick' else f'{base}-{speed}' for speed in self.speeds]
  17. print('''
  18. SPEED = quick
  19. .speed.quick = $(foreach s,$(sort $(filter-out %-slow %-thorough, $1)), --suite $s)
  20. .speed.slow = $(foreach s,$(sort $(filter-out %-thorough, $1)), --suite $s)
  21. .speed.thorough = $(foreach s,$(sort $1), --suite $s)
  22. .mtestargs = --no-rebuild -t 0
  23. ifneq ($(SPEED), quick)
  24. .mtestargs += --setup $(SPEED)
  25. endif
  26. .mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS)))))
  27. .check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
  28. .bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
  29. introspect = json.load(sys.stdin)
  30. def process_tests(test, targets, suites):
  31. executable = test['cmd'][0]
  32. try:
  33. executable = os.path.relpath(executable)
  34. except:
  35. pass
  36. deps = (targets.get(x, []) for x in test['depends'])
  37. deps = itertools.chain.from_iterable(deps)
  38. deps = list(deps)
  39. test_suites = test['suite'] or ['default']
  40. for s in test_suites:
  41. # The suite name in the introspection info is "PROJECT:SUITE"
  42. s = s.split(':')[1]
  43. if s == 'slow' or s == 'thorough':
  44. continue
  45. if s.endswith('-slow'):
  46. s = s[:-5]
  47. suites[s].speeds.append('slow')
  48. if s.endswith('-thorough'):
  49. s = s[:-9]
  50. suites[s].speeds.append('thorough')
  51. suites[s].deps.update(deps)
  52. def emit_prolog(suites, prefix):
  53. all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
  54. all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
  55. print()
  56. print(f'all-{prefix}-targets = {all_targets}')
  57. print(f'all-{prefix}-xml = {all_xml}')
  58. print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
  59. print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
  60. print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
  61. print(f'endif')
  62. print(f'{prefix}-build: run-ninja')
  63. print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
  64. print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
  65. print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
  66. print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
  67. def emit_suite_deps(name, suite, prefix):
  68. deps = ' '.join(suite.deps)
  69. targets = [f'{prefix}-{name}', f'{prefix}-report-{name}.junit.xml', f'{prefix}', f'{prefix}-report.junit.xml',
  70. f'{prefix}-build']
  71. print()
  72. print(f'.{prefix}-{name}.deps = {deps}')
  73. for t in targets:
  74. print(f'.ninja-goals.{t} += $(.{prefix}-{name}.deps)')
  75. def emit_suite(name, suite, prefix):
  76. emit_suite_deps(name, suite, prefix)
  77. targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
  78. print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
  79. print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
  80. print(f'endif')
  81. targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
  82. for t in introspect['targets']}
  83. testsuites = defaultdict(Suite)
  84. for test in introspect['tests']:
  85. process_tests(test, targets, testsuites)
  86. emit_prolog(testsuites, 'check')
  87. for name, suite in testsuites.items():
  88. emit_suite(name, suite, 'check')
  89. benchsuites = defaultdict(Suite)
  90. for test in introspect['benchmarks']:
  91. process_tests(test, targets, benchsuites)
  92. emit_prolog(benchsuites, 'bench')
  93. for name, suite in benchsuites.items():
  94. emit_suite(name, suite, 'bench')