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 10Caution: The positional arguments to this script must be specified before any 11optional arguments, such as --restrict. 12''' 13 14import argparse 15import glob 16import os 17import subprocess 18import sys 19 20def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles): 21 print(':: Merging raw profiles...', end='') 22 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]) 30 if not preserve_profiles: 31 for raw_profile in raw_profiles: 32 os.remove(raw_profile) 33 os.remove(manifest_path) 34 print('Done!') 35 return profdata_path 36 37def prepare_html_report(host_llvm_cov, profile, report_dir, binaries, 38 restricted_dirs, compilation_dir): 39 print(':: Preparing html report for {0}...'.format(binaries), end='') 40 sys.stdout.flush() 41 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, 49 '-show-line-counts-or-regions', '-Xdemangler', 'c++filt', 50 '-Xdemangler', '-n'] + restricted_dirs 51 if compilation_dir: 52 invocation += ['-compilation-dir=' + compilation_dir] 53 subprocess.check_call(invocation) 54 with open(os.path.join(report_dir, 'summary.txt'), 'wb') as Summary: 55 subprocess.check_call([host_llvm_cov, 'report'] + objects + 56 ['-instr-profile', profile] + restricted_dirs, 57 stdout=Summary) 58 print('Done!') 59 60def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries, 61 unified_report, restricted_dirs, compilation_dir): 62 if unified_report: 63 prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries, 64 restricted_dirs, compilation_dir) 65 else: 66 for binary in binaries: 67 binary_report_dir = os.path.join(report_dir, 68 os.path.basename(binary)) 69 prepare_html_report(host_llvm_cov, profdata_path, binary_report_dir, 70 [binary], restricted_dirs, compilation_dir) 71 72if __name__ == '__main__': 73 parser = argparse.ArgumentParser(description=__doc__) 74 parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata') 75 parser.add_argument('host_llvm_cov', help='Path to llvm-cov') 76 parser.add_argument('profile_data_dir', 77 help='Path to the directory containing the raw profiles') 78 parser.add_argument('report_dir', 79 help='Path to the output directory for html reports') 80 parser.add_argument('binaries', metavar='B', type=str, nargs='*', 81 help='Path to an instrumented binary') 82 parser.add_argument('--only-merge', action='store_true', 83 help='Only merge raw profiles together, skip report ' 84 'generation') 85 parser.add_argument('--preserve-profiles', 86 help='Do not delete raw profiles', action='store_true') 87 parser.add_argument('--use-existing-profdata', 88 help='Specify an existing indexed profile to use') 89 parser.add_argument('--unified-report', action='store_true', 90 help='Emit a unified report for all binaries') 91 parser.add_argument('--restrict', metavar='R', type=str, nargs='*', 92 default=[], 93 help='Restrict the reporting to the given source paths' 94 ' (must be specified after all other positional arguments)') 95 parser.add_argument('-C', '--compilation-dir', type=str, default="", 96 help='The compilation directory of the binary') 97 args = parser.parse_args() 98 99 if args.use_existing_profdata and args.only_merge: 100 print('--use-existing-profdata and --only-merge are incompatible') 101 exit(1) 102 103 if args.use_existing_profdata: 104 profdata_path = args.use_existing_profdata 105 else: 106 profdata_path = merge_raw_profiles(args.host_llvm_profdata, 107 args.profile_data_dir, 108 args.preserve_profiles) 109 110 if not len(args.binaries): 111 print('No binaries specified, no work to do!') 112 exit(1) 113 114 if not args.only_merge: 115 prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir, 116 args.binaries, args.unified_report, args.restrict, 117 args.compilation_dir) 118