1#!/usr/bin/env python
2
3from __future__ import print_function
4
5'''Prepare a code coverage artifact.
6
7- Collate raw profiles into one indexed profile.
8- Generate html reports for the given binaries.
9'''
10
11import argparse
12import glob
13import os
14import subprocess
15import sys
16
17def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
18    print(':: Merging raw profiles...', end='')
19    sys.stdout.flush()
20    raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
21    manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
22    profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
23    with open(manifest_path, 'w') as manifest:
24        manifest.write('\n'.join(raw_profiles))
25    subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
26                           manifest_path, '-o', profdata_path])
27    if not preserve_profiles:
28        for raw_profile in raw_profiles:
29            os.remove(raw_profile)
30    os.remove(manifest_path)
31    print('Done!')
32    return profdata_path
33
34def prepare_html_report(host_llvm_cov, profile, report_dir, binary,
35                        restricted_dirs):
36    print(':: Preparing html report for {0}...'.format(binary), end='')
37    sys.stdout.flush()
38    binary_report_dir = os.path.join(report_dir, os.path.basename(binary))
39    invocation = [host_llvm_cov, 'show', binary, '-format', 'html',
40                  '-instr-profile', profile, '-o', binary_report_dir,
41                  '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
42                  '-Xdemangler', '-n'] + restricted_dirs
43    subprocess.check_call(invocation)
44    with open(os.path.join(binary_report_dir, 'summary.txt'), 'wb') as Summary:
45        subprocess.check_call([host_llvm_cov, 'report', binary,
46                               '-instr-profile', profile], stdout=Summary)
47    print('Done!')
48
49def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
50                         restricted_dirs):
51    for binary in binaries:
52        prepare_html_report(host_llvm_cov, profdata_path, report_dir, binary,
53                            restricted_dirs)
54
55if __name__ == '__main__':
56    parser = argparse.ArgumentParser(description=__doc__)
57    parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
58    parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
59    parser.add_argument('profile_data_dir',
60                       help='Path to the directory containing the raw profiles')
61    parser.add_argument('report_dir',
62                       help='Path to the output directory for html reports')
63    parser.add_argument('binaries', metavar='B', type=str, nargs='*',
64                       help='Path to an instrumented binary')
65    parser.add_argument('--only-merge', action='store_true',
66                        help='Only merge raw profiles together, skip report '
67                             'generation')
68    parser.add_argument('--preserve-profiles',
69                       help='Do not delete raw profiles', action='store_true')
70    parser.add_argument('--use-existing-profdata',
71                       help='Specify an existing indexed profile to use')
72    parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
73                       default=[],
74                       help='Restrict the reporting to the given source paths')
75    args = parser.parse_args()
76
77    if args.use_existing_profdata and args.only_merge:
78        print('--use-existing-profdata and --only-merge are incompatible')
79        exit(1)
80
81    if args.use_existing_profdata:
82        profdata_path = args.use_existing_profdata
83    else:
84        profdata_path = merge_raw_profiles(args.host_llvm_profdata,
85                                           args.profile_data_dir,
86                                           args.preserve_profiles)
87
88    if not args.only_merge:
89        prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
90                            args.binaries, args.restrict)
91