1#!/usr/bin/env python 2 3'''Prepare a code coverage artifact. 4 5- Collate raw profiles into one indexed profile. 6- Delete the raw profiles. 7- Generate html reports for llvm binaries. 8''' 9 10import argparse 11import glob 12import os 13import subprocess 14import sys 15 16def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles): 17 print ':: Merging raw profiles...', 18 sys.stdout.flush() 19 raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw')) 20 manifest_path = os.path.join(profile_data_dir, 'profiles.manifest') 21 profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata') 22 with open(manifest_path, 'w') as manifest: 23 manifest.write('\n'.join(raw_profiles)) 24 subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f', 25 manifest_path, '-o', profdata_path]) 26 if not preserve_profiles: 27 for raw_profile in raw_profiles: 28 os.remove(raw_profile) 29 os.remove(manifest_path) 30 print 'Done!' 31 32def prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary): 33 print ':: Preparing html report for {0}...'.format(binary), 34 sys.stdout.flush() 35 binary_report_dir = os.path.join(report_dir, os.path.basename(binary)) 36 profile = os.path.join(profile_data_dir, 'Coverage.profdata') 37 subprocess.check_call([host_llvm_cov, 'show', binary, '-format', 'html', 38 '-instr-profile', profile, '-o', binary_report_dir, 39 '-show-line-counts-or-regions', 40 '-Xdemangler', 'c++filt', '-Xdemangler', '-n']) 41 with open(os.path.join(binary_report_dir, 'summary.txt'), 'wb') as Summary: 42 subprocess.check_call([host_llvm_cov, 'report', binary, 43 '-instr-profile', profile], stdout=Summary) 44 print 'Done!' 45 46def prepare_html_reports(host_llvm_cov, profile_data_dir, report_dir, binaries): 47 for binary in binaries: 48 prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary) 49 50if __name__ == '__main__': 51 parser = argparse.ArgumentParser(description=__doc__) 52 parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata') 53 parser.add_argument('host_llvm_cov', help='Path to llvm-cov') 54 parser.add_argument('profile_data_dir', 55 help='Path to the directory containing the raw profiles') 56 parser.add_argument('report_dir', 57 help='Path to the output directory for html reports') 58 parser.add_argument('binaries', metavar='B', type=str, nargs='+', 59 help='Path to an instrumented binary') 60 parser.add_argument('--preserve-profiles', 61 help='Do not delete raw profiles', action='store_true') 62 args = parser.parse_args() 63 64 merge_raw_profiles(args.host_llvm_profdata, args.profile_data_dir, 65 args.preserve_profiles) 66 prepare_html_reports(args.host_llvm_cov, args.profile_data_dir, 67 args.report_dir, args.binaries) 68