blob: 883cdd78049bec04f604a0f44b0f94190d13bd35 [file] [log] [blame]
Vedant Kumard9aed822016-06-13 23:33:48 +00001#!/usr/bin/env python
2
Vedant Kumar74669872016-10-26 22:07:37 +00003from __future__ import print_function
4
Vedant Kumard9aed822016-06-13 23:33:48 +00005'''Prepare a code coverage artifact.
6
7- Collate raw profiles into one indexed profile.
Vedant Kumar547ebad2016-09-22 21:49:49 +00008- Generate html reports for the given binaries.
Vedant Kumar16776312017-02-09 19:37:18 +00009
10Caution: The positional arguments to this script must be specified before any
11optional arguments, such as --restrict.
Vedant Kumard9aed822016-06-13 23:33:48 +000012'''
13
14import argparse
15import glob
16import os
17import subprocess
18import sys
19
Vedant Kumar05ee94f2016-07-18 22:50:10 +000020def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
Vedant Kumar74669872016-10-26 22:07:37 +000021 print(':: Merging raw profiles...', end='')
Vedant Kumard9aed822016-06-13 23:33:48 +000022 sys.stdout.flush()
23 raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
24 manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
25 profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
26 with open(manifest_path, 'w') as manifest:
27 manifest.write('\n'.join(raw_profiles))
28 subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
29 manifest_path, '-o', profdata_path])
Vedant Kumar05ee94f2016-07-18 22:50:10 +000030 if not preserve_profiles:
31 for raw_profile in raw_profiles:
32 os.remove(raw_profile)
33 os.remove(manifest_path)
Vedant Kumar74669872016-10-26 22:07:37 +000034 print('Done!')
Vedant Kumar547ebad2016-09-22 21:49:49 +000035 return profdata_path
Vedant Kumard9aed822016-06-13 23:33:48 +000036
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000037def prepare_html_report(host_llvm_cov, profile, report_dir, binaries,
Vedant Kumar547ebad2016-09-22 21:49:49 +000038 restricted_dirs):
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000039 print(':: Preparing html report for {0}...'.format(binaries), end='')
Vedant Kumard9aed822016-06-13 23:33:48 +000040 sys.stdout.flush()
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000041 objects = []
42 for i, binary in enumerate(binaries):
43 if i == 0:
44 objects.append(binary)
45 else:
46 objects.extend(('-object', binary))
47 invocation = [host_llvm_cov, 'show'] + objects + ['-format', 'html',
48 '-instr-profile', profile, '-o', report_dir,
Vedant Kumar547ebad2016-09-22 21:49:49 +000049 '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
50 '-Xdemangler', '-n'] + restricted_dirs
51 subprocess.check_call(invocation)
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000052 with open(os.path.join(report_dir, 'summary.txt'), 'wb') as Summary:
53 subprocess.check_call([host_llvm_cov, 'report'] + objects +
54 ['-instr-profile', profile], stdout=Summary)
Vedant Kumar74669872016-10-26 22:07:37 +000055 print('Done!')
Vedant Kumard9aed822016-06-13 23:33:48 +000056
Vedant Kumar547ebad2016-09-22 21:49:49 +000057def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000058 unified_report, restricted_dirs):
59 if unified_report:
60 prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries,
Vedant Kumar547ebad2016-09-22 21:49:49 +000061 restricted_dirs)
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000062 else:
63 for binary in binaries:
64 binary_report_dir = os.path.join(report_dir,
65 os.path.basename(binary))
66 prepare_html_report(host_llvm_cov, profdata_path, binary_report_dir,
67 [binary], restricted_dirs)
Vedant Kumar05ee94f2016-07-18 22:50:10 +000068
Vedant Kumard9aed822016-06-13 23:33:48 +000069if __name__ == '__main__':
70 parser = argparse.ArgumentParser(description=__doc__)
71 parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
72 parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
73 parser.add_argument('profile_data_dir',
74 help='Path to the directory containing the raw profiles')
Vedant Kumar05ee94f2016-07-18 22:50:10 +000075 parser.add_argument('report_dir',
76 help='Path to the output directory for html reports')
Vedant Kumar273b6dc2016-10-26 22:07:35 +000077 parser.add_argument('binaries', metavar='B', type=str, nargs='*',
Vedant Kumar05ee94f2016-07-18 22:50:10 +000078 help='Path to an instrumented binary')
Vedant Kumar273b6dc2016-10-26 22:07:35 +000079 parser.add_argument('--only-merge', action='store_true',
80 help='Only merge raw profiles together, skip report '
81 'generation')
Vedant Kumar05ee94f2016-07-18 22:50:10 +000082 parser.add_argument('--preserve-profiles',
83 help='Do not delete raw profiles', action='store_true')
Vedant Kumar547ebad2016-09-22 21:49:49 +000084 parser.add_argument('--use-existing-profdata',
85 help='Specify an existing indexed profile to use')
Vedant Kumarf4df0ed2016-10-26 22:07:39 +000086 parser.add_argument('--unified-report', action='store_true',
87 help='Emit a unified report for all binaries')
Vedant Kumar547ebad2016-09-22 21:49:49 +000088 parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
89 default=[],
Vedant Kumar16776312017-02-09 19:37:18 +000090 help='Restrict the reporting to the given source paths'
91 ' (must be specified after all other positional arguments)')
Vedant Kumard9aed822016-06-13 23:33:48 +000092 args = parser.parse_args()
93
Vedant Kumar273b6dc2016-10-26 22:07:35 +000094 if args.use_existing_profdata and args.only_merge:
Vedant Kumar74669872016-10-26 22:07:37 +000095 print('--use-existing-profdata and --only-merge are incompatible')
Vedant Kumar273b6dc2016-10-26 22:07:35 +000096 exit(1)
97
Vedant Kumar547ebad2016-09-22 21:49:49 +000098 if args.use_existing_profdata:
99 profdata_path = args.use_existing_profdata
100 else:
101 profdata_path = merge_raw_profiles(args.host_llvm_profdata,
102 args.profile_data_dir,
103 args.preserve_profiles)
104
Vedant Kumarf4df0ed2016-10-26 22:07:39 +0000105 if not len(args.binaries):
106 print('No binaries specified, no work to do!')
107 exit(1)
108
Vedant Kumar273b6dc2016-10-26 22:07:35 +0000109 if not args.only_merge:
110 prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
Vedant Kumarf4df0ed2016-10-26 22:07:39 +0000111 args.binaries, args.unified_report, args.restrict)