1#!/usr/bin/env python 2 3'''Prepare a code coverage artifact. 4 5- Collate raw profiles into one indexed profile. 6- Generate html reports for the given binaries. 7''' 8 9import argparse 10import glob 11import os 12import subprocess 13import sys 14 15def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles): 16 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]) 25 if not preserve_profiles: 26 for raw_profile in raw_profiles: 27 os.remove(raw_profile) 28 os.remove(manifest_path) 29 print 'Done!' 30 return profdata_path 31 32def prepare_html_report(host_llvm_cov, profile, report_dir, binary, 33 restricted_dirs): 34 print ':: Preparing html report for {0}...'.format(binary), 35 sys.stdout.flush() 36 binary_report_dir = os.path.join(report_dir, os.path.basename(binary)) 37 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) 42 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) 45 print 'Done!' 46 47def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries, 48 restricted_dirs): 49 for binary in binaries: 50 prepare_html_report(host_llvm_cov, profdata_path, report_dir, binary, 51 restricted_dirs) 52 53if __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') 59 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') 65 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') 70 args = parser.parse_args() 71 72 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) 81