llvm-coverage.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 #------------------------------------------------------------------------------
3 
4 import os
5 import sys
6 import argparse
7 import glob
8 import subprocess
9 #------------------------------------------------------------------------------
10 
11 def runCmd(cmd, data=None):
12  if input is None:
13  p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
14  stdout, stderr = p.communicate()
15  else:
16  p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
17  stdout, stderr = p.communicate(input=data)
18 
19  return stdout.decode('utf-8'), stderr.decode('utf-8'), p.returncode
20 #------------------------------------------------------------------------------
21 
22 def main():
23  parser = argparse.ArgumentParser(description='llvm-coverage')
24  parser.add_argument('--insights', help='C++ Insights binary', required=True)
25  parser.add_argument('--llvm-prof-dir', help='LLVM profiles data dir', default='')
26  parser.add_argument('--llvm-prof', help='llvm-profdata binary', default='llvm-profdata')
27  parser.add_argument('--llvm-cov', help='llvm-cov binary', default='llvm-cov')
28  parser.add_argument('--format', help='Output format: html/text', default='text')
29  parser.add_argument('--output', help='Output filename', required=True)
30  parser.add_argument('args', nargs=argparse.REMAINDER)
31  args = vars(parser.parse_args())
32 
33  insightsPath = args['insights']
34  rawProfiles = glob.glob(os.path.join(args['llvm_prof_dir'], "*.profraw"))
35  profilesManifest = os.path.join(args['llvm_prof_dir'], 'profiles.manifest')
36  profilesData = os.path.join(args['llvm_prof_dir'], 'insights.profdata')
37 
38  with open(profilesManifest, "w") as manifest:
39  manifest.write("\n".join(rawProfiles))
40 
41  cmd = [args['llvm_prof'], 'merge', '-sparse', '-f', profilesManifest, '-o', profilesData]
42  stdout, stderr, returncode = runCmd(cmd)
43  print(stderr)
44 
45  action = 'show' if args['format'] != 'lcov' else 'export'
46 
47  cmd = [args['llvm_cov'], action, insightsPath, f'-instr-profile={profilesData}', f'--format={args["format"]}', '-ignore-filename-regex=build/']
48 
49  stdout, stderr, returncode = runCmd(cmd)
50  print(stderr)
51 
52  open(args['output'], 'w').write(stdout)
53 #------------------------------------------------------------------------------
54 
55 
56 sys.exit(main())
57 #------------------------------------------------------------------------------
58 
59 
def runCmd(cmd, data=None)