2
0

mtest2make.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. TIMEOUT_MULTIPLIER = 1
  23. .mtestargs = --no-rebuild -t $(TIMEOUT_MULTIPLIER)
  24. ifneq ($(SPEED), quick)
  25. .mtestargs += --setup $(SPEED)
  26. endif
  27. .mtestargs += $(subst -j,--num-processes , $(filter-out -j, $(lastword -j1 $(filter -j%, $(MAKEFLAGS)))))
  28. .check.mtestargs = $(MTESTARGS) $(.mtestargs) $(if $(V),--verbose,--print-errorlogs)
  29. .bench.mtestargs = $(MTESTARGS) $(.mtestargs) --benchmark --verbose''')
  30. introspect = json.load(sys.stdin)
  31. def process_tests(test, targets, suites):
  32. executable = test['cmd'][0]
  33. try:
  34. executable = os.path.relpath(executable)
  35. except:
  36. pass
  37. deps = (targets.get(x, []) for x in test['depends'])
  38. deps = itertools.chain.from_iterable(deps)
  39. deps = list(deps)
  40. test_suites = test['suite'] or ['default']
  41. for s in test_suites:
  42. # The suite name in the introspection info is "PROJECT" or "PROJECT:SUITE"
  43. if ':' in s:
  44. s = s.split(':')[1]
  45. if s == 'slow' or s == 'thorough':
  46. continue
  47. if s.endswith('-slow'):
  48. s = s[:-5]
  49. suites[s].speeds.append('slow')
  50. if s.endswith('-thorough'):
  51. s = s[:-9]
  52. suites[s].speeds.append('thorough')
  53. suites[s].deps.update(deps)
  54. def emit_prolog(suites, prefix):
  55. all_targets = ' '.join((f'{prefix}-{k}' for k in suites.keys()))
  56. all_xml = ' '.join((f'{prefix}-report-{k}.junit.xml' for k in suites.keys()))
  57. print()
  58. print(f'all-{prefix}-targets = {all_targets}')
  59. print(f'all-{prefix}-xml = {all_xml}')
  60. print(f'.PHONY: {prefix} do-meson-{prefix} {prefix}-report.junit.xml $(all-{prefix}-targets) $(all-{prefix}-xml)')
  61. print(f'ifeq ($(filter {prefix}, $(MAKECMDGOALS)),)')
  62. print(f'.{prefix}.mtestargs += $(call .speed.$(SPEED), $(.{prefix}.mtest-suites))')
  63. print(f'endif')
  64. print(f'{prefix}-build: run-ninja')
  65. print(f'{prefix} $(all-{prefix}-targets): do-meson-{prefix}')
  66. print(f'do-meson-{prefix}: run-ninja; $(if $(MAKE.n),,+)$(MESON) test $(.{prefix}.mtestargs)')
  67. print(f'{prefix}-report.junit.xml $(all-{prefix}-xml): {prefix}-report%.junit.xml: run-ninja')
  68. print(f'\t$(MAKE) {prefix}$* MTESTARGS="$(MTESTARGS) --logbase {prefix}-report$*" && ln -f meson-logs/$@ .')
  69. def emit_suite_deps(name, suite, prefix):
  70. deps = ' '.join(suite.deps)
  71. targets = [f'{prefix}-{name}', f'{prefix}-report-{name}.junit.xml', f'{prefix}', f'{prefix}-report.junit.xml',
  72. f'{prefix}-build']
  73. print()
  74. print(f'.{prefix}-{name}.deps = {deps}')
  75. for t in targets:
  76. print(f'.ninja-goals.{t} += $(.{prefix}-{name}.deps)')
  77. def emit_suite(name, suite, prefix):
  78. emit_suite_deps(name, suite, prefix)
  79. targets = f'{prefix}-{name} {prefix}-report-{name}.junit.xml {prefix} {prefix}-report.junit.xml'
  80. print(f'ifneq ($(filter {targets}, $(MAKECMDGOALS)),)')
  81. print(f'.{prefix}.mtest-suites += ' + ' '.join(suite.names(name)))
  82. print(f'endif')
  83. targets = {t['id']: [os.path.relpath(f) for f in t['filename']]
  84. for t in introspect['targets']}
  85. testsuites = defaultdict(Suite)
  86. for test in introspect['tests']:
  87. process_tests(test, targets, testsuites)
  88. emit_prolog(testsuites, 'check')
  89. for name, suite in testsuites.items():
  90. emit_suite(name, suite, 'check')
  91. benchsuites = defaultdict(Suite)
  92. for test in introspect['benchmarks']:
  93. process_tests(test, targets, benchsuites)
  94. emit_prolog(benchsuites, 'bench')
  95. for name, suite in benchsuites.items():
  96. emit_suite(name, suite, 'bench')