Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 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. |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 7 | - Generate html reports for llvm binaries. |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 8 | ''' |
| 9 | |
| 10 | import argparse |
| 11 | import glob |
| 12 | import os |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 16 | def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles): |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 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]) |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 26 | if not preserve_profiles: |
| 27 | for raw_profile in raw_profiles: |
| 28 | os.remove(raw_profile) |
| 29 | os.remove(manifest_path) |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 30 | print 'Done!' |
| 31 | |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 32 | def prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary): |
| 33 | print ':: Preparing html report for {0}...'.format(binary), |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 34 | sys.stdout.flush() |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 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) |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 44 | print 'Done!' |
| 45 | |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 46 | def 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 | |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 50 | if __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') |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 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') |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 62 | args = parser.parse_args() |
| 63 | |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame^] | 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) |