gcov12_report.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env python3
  2. """Line counts of a coverage build made with GCC 12 (the portable one that ran in the VMs) as a gcovr style JSON report.
  3. gcovr drops some of those files and the host's gcov cannot read the data, so the Debian 12 rootfs's own gcov runs over every
  4. .gcda of the folder (tools/analysis/gcov_deb12.sh, text output with preserved paths) and the .gcov files are read here.
  5. Usage: gcov12_report.py <folder with the .gcno and .gcda files> <out.json> [source prefix, default src/WallpaperEngine/]"""
  6. import glob, json, os, re, subprocess, sys, tempfile
  7. folder, out = sys.argv[1:3]
  8. prefix = sys.argv[3] if len(sys.argv) > 3 else 'src/WallpaperEngine/'
  9. here = os.path.dirname(os.path.abspath(__file__))
  10. repo = os.path.abspath(os.path.join(here, '..', '..'))
  11. wanted = repo.replace('/', '#') + '#' + prefix.replace('/', '#')
  12. counts = {}
  13. with tempfile.TemporaryDirectory(dir=folder) as work:
  14. script = os.path.join(work, 'run.sh')
  15. open(script, 'w').write('#!/bin/bash\ncd "$1"\nfind "$2" -name "*.gcda" | while read -r f; do gcov -p -o "$(dirname "$f")" "$f" > /dev/null 2>&1; done\nexit 0\n')
  16. os.chmod(script, 0o755)
  17. env = dict(os.environ, GCOV_BIND=os.path.abspath(folder), PROOT=os.environ.get('PROOT', os.path.expanduser('~/.local/share/arch-proot/proot')))
  18. env['BINDS'] = '-b %s:%s -b %s:%s' % (repo, repo, os.path.abspath(folder), os.path.abspath(folder))
  19. subprocess.run([os.path.join(here, '..', 'distro_rootfs.sh'), 'run',
  20. os.environ.get('GCOV_ROOTFS', os.path.expanduser('~/.local/share/distro-rootfs/debian-12')), script,
  21. work, os.path.abspath(folder)], env=env, check=True)
  22. for path in glob.glob(os.path.join(work, wanted + '*.gcov')):
  23. source = None
  24. for line in open(path, errors='replace'):
  25. fields = line.rstrip('\n').split(':', 2)
  26. if len(fields) < 3:
  27. continue
  28. count, number = fields[0].strip(), fields[1].strip()
  29. if number == '0':
  30. if fields[2].startswith('Source:'):
  31. source = os.path.relpath(fields[2][7:], repo)
  32. continue
  33. if count == '-' or source is None:
  34. continue
  35. value = 0 if count[0] in '#=' else int(count.rstrip('*'))
  36. lines = counts.setdefault(source, {})
  37. lines[int(number)] = lines.get(int(number), 0) + value
  38. json.dump({'files': [{'file': name, 'lines': [{'line_number': n, 'count': c} for n, c in sorted(lines.items())]}
  39. for name, lines in sorted(counts.items())]}, open(out, 'w'))
  40. print(len(counts), 'files')