blob: 9708a372b75395890481639a4752f7de10f1ab80 [file] [log] [blame]
Vedant Kumard9aed822016-06-13 23:33:48 +00001#!/usr/bin/env python
2
3'''Prepare a code coverage artifact.
4
5- Collate raw profiles into one indexed profile.
6- Delete the raw profiles.
Vedant Kumar05ee94f2016-07-18 22:50:10 +00007- Generate html reports for llvm binaries.
Vedant Kumard9aed822016-06-13 23:33:48 +00008'''
9
10import argparse
11import glob
12import os
13import subprocess
14import sys
15
Vedant Kumar05ee94f2016-07-18 22:50:10 +000016def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
Vedant Kumard9aed822016-06-13 23:33:48 +000017 print ':: Merging raw profiles...',
18 sys.stdout.flush()
19 raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
20 manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
21 profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
22 with open(manifest_path, 'w') as manifest:
23 manifest.write('\n'.join(raw_profiles))
24 subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
25 manifest_path, '-o', profdata_path])
Vedant Kumar05ee94f2016-07-18 22:50:10 +000026 if not preserve_profiles:
27 for raw_profile in raw_profiles:
28 os.remove(raw_profile)
29 os.remove(manifest_path)
Vedant Kumard9aed822016-06-13 23:33:48 +000030 print 'Done!'
31
Vedant Kumar05ee94f2016-07-18 22:50:10 +000032def prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary):
33 print ':: Preparing html report for {0}...'.format(binary),
Vedant Kumard9aed822016-06-13 23:33:48 +000034 sys.stdout.flush()
Vedant Kumar05ee94f2016-07-18 22:50:10 +000035 binary_report_dir = os.path.join(report_dir, os.path.basename(binary))
36 profile = os.path.join(profile_data_dir, 'Coverage.profdata')
37 subprocess.check_call([host_llvm_cov, 'show', binary, '-format', 'html',
38 '-instr-profile', profile, '-o', binary_report_dir,
39 '-show-line-counts-or-regions',
40 '-Xdemangler', 'c++filt', '-Xdemangler', '-n'])
41 with open(os.path.join(binary_report_dir, 'summary.txt'), 'wb') as Summary:
42 subprocess.check_call([host_llvm_cov, 'report', binary,
43 '-instr-profile', profile], stdout=Summary)
Vedant Kumard9aed822016-06-13 23:33:48 +000044 print 'Done!'
45
Vedant Kumar05ee94f2016-07-18 22:50:10 +000046def prepare_html_reports(host_llvm_cov, profile_data_dir, report_dir, binaries):
47 for binary in binaries:
48 prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary)
49
Vedant Kumard9aed822016-06-13 23:33:48 +000050if __name__ == '__main__':
51 parser = argparse.ArgumentParser(description=__doc__)
52 parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
53 parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
54 parser.add_argument('profile_data_dir',
55 help='Path to the directory containing the raw profiles')
Vedant Kumar05ee94f2016-07-18 22:50:10 +000056 parser.add_argument('report_dir',
57 help='Path to the output directory for html reports')
58 parser.add_argument('binaries', metavar='B', type=str, nargs='+',
59 help='Path to an instrumented binary')
60 parser.add_argument('--preserve-profiles',
61 help='Do not delete raw profiles', action='store_true')
Vedant Kumard9aed822016-06-13 23:33:48 +000062 args = parser.parse_args()
63
Vedant Kumar05ee94f2016-07-18 22:50:10 +000064 merge_raw_profiles(args.host_llvm_profdata, args.profile_data_dir,
65 args.preserve_profiles)
66 prepare_html_reports(args.host_llvm_cov, args.profile_data_dir,
67 args.report_dir, args.binaries)