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, binaries,
35                        restricted_dirs):
36    print(':: Preparing html report for {0}...'.format(binaries), end='')
37    sys.stdout.flush()
38    objects = []
39    for i, binary in enumerate(binaries):
40        if i == 0:
41            objects.append(binary)
42        else:
43            objects.extend(('-object', binary))
44    invocation = [host_llvm_cov, 'show'] + objects + ['-format', 'html',
45                  '-instr-profile', profile, '-o', report_dir,
46                  '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
47                  '-Xdemangler', '-n'] + restricted_dirs
48    subprocess.check_call(invocation)
49    with open(os.path.join(report_dir, 'summary.txt'), 'wb') as Summary:
50        subprocess.check_call([host_llvm_cov, 'report'] + objects +
51                               ['-instr-profile', profile], stdout=Summary)
52    print('Done!')
53
54def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
55                         unified_report, restricted_dirs):
56    if unified_report:
57        prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries,
58                            restricted_dirs)
59    else:
60        for binary in binaries:
61            binary_report_dir = os.path.join(report_dir,
62                                             os.path.basename(binary))
63            prepare_html_report(host_llvm_cov, profdata_path, binary_report_dir,
64                                [binary], restricted_dirs)
65
66if __name__ == '__main__':
67    parser = argparse.ArgumentParser(description=__doc__)
68    parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
69    parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
70    parser.add_argument('profile_data_dir',
71                       help='Path to the directory containing the raw profiles')
72    parser.add_argument('report_dir',
73                       help='Path to the output directory for html reports')
74    parser.add_argument('binaries', metavar='B', type=str, nargs='*',
75                       help='Path to an instrumented binary')
76    parser.add_argument('--only-merge', action='store_true',
77                        help='Only merge raw profiles together, skip report '
78                             'generation')
79    parser.add_argument('--preserve-profiles',
80                       help='Do not delete raw profiles', action='store_true')
81    parser.add_argument('--use-existing-profdata',
82                       help='Specify an existing indexed profile to use')
83    parser.add_argument('--unified-report', action='store_true',
84                       help='Emit a unified report for all binaries')
85    parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
86                       default=[],
87                       help='Restrict the reporting to the given source paths')
88    args = parser.parse_args()
89
90    if args.use_existing_profdata and args.only_merge:
91        print('--use-existing-profdata and --only-merge are incompatible')
92        exit(1)
93
94    if args.use_existing_profdata:
95        profdata_path = args.use_existing_profdata
96    else:
97        profdata_path = merge_raw_profiles(args.host_llvm_profdata,
98                                           args.profile_data_dir,
99                                           args.preserve_profiles)
100
101    if not len(args.binaries):
102        print('No binaries specified, no work to do!')
103        exit(1)
104
105    if not args.only_merge:
106        prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
107                            args.binaries, args.unified_report, args.restrict)
108