blob: ae7d4fe18aa144534dae38b62ba204aba49ff9a5 [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.
Vedant Kumar547ebad2016-09-22 21:49:49 +00006- Generate html reports for the given binaries.
Vedant Kumard9aed822016-06-13 23:33:48 +00007'''
8
9import argparse
10import glob
11import os
12import subprocess
13import sys
14
Vedant Kumar05ee94f2016-07-18 22:50:10 +000015def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
Vedant Kumard9aed822016-06-13 23:33:48 +000016 print ':: Merging raw profiles...',
17 sys.stdout.flush()
18 raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
19 manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
20 profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
21 with open(manifest_path, 'w') as manifest:
22 manifest.write('\n'.join(raw_profiles))
23 subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
24 manifest_path, '-o', profdata_path])
Vedant Kumar05ee94f2016-07-18 22:50:10 +000025 if not preserve_profiles:
26 for raw_profile in raw_profiles:
27 os.remove(raw_profile)
28 os.remove(manifest_path)
Vedant Kumard9aed822016-06-13 23:33:48 +000029 print 'Done!'
Vedant Kumar547ebad2016-09-22 21:49:49 +000030 return profdata_path
Vedant Kumard9aed822016-06-13 23:33:48 +000031
Vedant Kumar547ebad2016-09-22 21:49:49 +000032def prepare_html_report(host_llvm_cov, profile, report_dir, binary,
33 restricted_dirs):
Vedant Kumar05ee94f2016-07-18 22:50:10 +000034 print ':: Preparing html report for {0}...'.format(binary),
Vedant Kumard9aed822016-06-13 23:33:48 +000035 sys.stdout.flush()
Vedant Kumar05ee94f2016-07-18 22:50:10 +000036 binary_report_dir = os.path.join(report_dir, os.path.basename(binary))
Vedant Kumar547ebad2016-09-22 21:49:49 +000037 invocation = [host_llvm_cov, 'show', binary, '-format', 'html',
38 '-instr-profile', profile, '-o', binary_report_dir,
39 '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
40 '-Xdemangler', '-n'] + restricted_dirs
41 subprocess.check_call(invocation)
Vedant Kumar05ee94f2016-07-18 22:50:10 +000042 with open(os.path.join(binary_report_dir, 'summary.txt'), 'wb') as Summary:
43 subprocess.check_call([host_llvm_cov, 'report', binary,
44 '-instr-profile', profile], stdout=Summary)
Vedant Kumard9aed822016-06-13 23:33:48 +000045 print 'Done!'
46
Vedant Kumar547ebad2016-09-22 21:49:49 +000047def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
48 restricted_dirs):
Vedant Kumar05ee94f2016-07-18 22:50:10 +000049 for binary in binaries:
Vedant Kumar547ebad2016-09-22 21:49:49 +000050 prepare_html_report(host_llvm_cov, profdata_path, report_dir, binary,
51 restricted_dirs)
Vedant Kumar05ee94f2016-07-18 22:50:10 +000052
Vedant Kumard9aed822016-06-13 23:33:48 +000053if __name__ == '__main__':
54 parser = argparse.ArgumentParser(description=__doc__)
55 parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
56 parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
57 parser.add_argument('profile_data_dir',
58 help='Path to the directory containing the raw profiles')
Vedant Kumar05ee94f2016-07-18 22:50:10 +000059 parser.add_argument('report_dir',
60 help='Path to the output directory for html reports')
61 parser.add_argument('binaries', metavar='B', type=str, nargs='+',
62 help='Path to an instrumented binary')
63 parser.add_argument('--preserve-profiles',
64 help='Do not delete raw profiles', action='store_true')
Vedant Kumar547ebad2016-09-22 21:49:49 +000065 parser.add_argument('--use-existing-profdata',
66 help='Specify an existing indexed profile to use')
67 parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
68 default=[],
69 help='Restrict the reporting to the given source paths')
Vedant Kumard9aed822016-06-13 23:33:48 +000070 args = parser.parse_args()
71
Vedant Kumar547ebad2016-09-22 21:49:49 +000072 if args.use_existing_profdata:
73 profdata_path = args.use_existing_profdata
74 else:
75 profdata_path = merge_raw_profiles(args.host_llvm_profdata,
76 args.profile_data_dir,
77 args.preserve_profiles)
78
79 prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
80 args.binaries, args.restrict)