Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Vedant Kumar | 7466987 | 2016-10-26 22:07:37 +0000 | [diff] [blame] | 3 | from __future__ import print_function |
| 4 | |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 5 | '''Prepare a code coverage artifact. |
| 6 | |
| 7 | - Collate raw profiles into one indexed profile. |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 8 | - Generate html reports for the given binaries. |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 9 | ''' |
| 10 | |
| 11 | import argparse |
| 12 | import glob |
| 13 | import os |
| 14 | import subprocess |
| 15 | import sys |
| 16 | |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 17 | def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles): |
Vedant Kumar | 7466987 | 2016-10-26 22:07:37 +0000 | [diff] [blame] | 18 | print(':: Merging raw profiles...', end='') |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 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]) |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 27 | if not preserve_profiles: |
| 28 | for raw_profile in raw_profiles: |
| 29 | os.remove(raw_profile) |
| 30 | os.remove(manifest_path) |
Vedant Kumar | 7466987 | 2016-10-26 22:07:37 +0000 | [diff] [blame] | 31 | print('Done!') |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 32 | return profdata_path |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 33 | |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 34 | def prepare_html_report(host_llvm_cov, profile, report_dir, binaries, |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 35 | restricted_dirs): |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 36 | print(':: Preparing html report for {0}...'.format(binaries), end='') |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 37 | sys.stdout.flush() |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 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, |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 46 | '-show-line-counts-or-regions', '-Xdemangler', 'c++filt', |
| 47 | '-Xdemangler', '-n'] + restricted_dirs |
| 48 | subprocess.check_call(invocation) |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 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) |
Vedant Kumar | 7466987 | 2016-10-26 22:07:37 +0000 | [diff] [blame] | 52 | print('Done!') |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 53 | |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 54 | def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries, |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 55 | unified_report, restricted_dirs): |
| 56 | if unified_report: |
| 57 | prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries, |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 58 | restricted_dirs) |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 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) |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 65 | |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 66 | if __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') |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 72 | parser.add_argument('report_dir', |
| 73 | help='Path to the output directory for html reports') |
Vedant Kumar | 273b6dc | 2016-10-26 22:07:35 +0000 | [diff] [blame] | 74 | parser.add_argument('binaries', metavar='B', type=str, nargs='*', |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 75 | help='Path to an instrumented binary') |
Vedant Kumar | 273b6dc | 2016-10-26 22:07:35 +0000 | [diff] [blame] | 76 | parser.add_argument('--only-merge', action='store_true', |
| 77 | help='Only merge raw profiles together, skip report ' |
| 78 | 'generation') |
Vedant Kumar | 05ee94f | 2016-07-18 22:50:10 +0000 | [diff] [blame] | 79 | parser.add_argument('--preserve-profiles', |
| 80 | help='Do not delete raw profiles', action='store_true') |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 81 | parser.add_argument('--use-existing-profdata', |
| 82 | help='Specify an existing indexed profile to use') |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 83 | parser.add_argument('--unified-report', action='store_true', |
| 84 | help='Emit a unified report for all binaries') |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 85 | parser.add_argument('--restrict', metavar='R', type=str, nargs='*', |
| 86 | default=[], |
| 87 | help='Restrict the reporting to the given source paths') |
Vedant Kumar | d9aed82 | 2016-06-13 23:33:48 +0000 | [diff] [blame] | 88 | args = parser.parse_args() |
| 89 | |
Vedant Kumar | 273b6dc | 2016-10-26 22:07:35 +0000 | [diff] [blame] | 90 | if args.use_existing_profdata and args.only_merge: |
Vedant Kumar | 7466987 | 2016-10-26 22:07:37 +0000 | [diff] [blame] | 91 | print('--use-existing-profdata and --only-merge are incompatible') |
Vedant Kumar | 273b6dc | 2016-10-26 22:07:35 +0000 | [diff] [blame] | 92 | exit(1) |
| 93 | |
Vedant Kumar | 547ebad | 2016-09-22 21:49:49 +0000 | [diff] [blame] | 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 | |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 101 | if not len(args.binaries): |
| 102 | print('No binaries specified, no work to do!') |
| 103 | exit(1) |
| 104 | |
Vedant Kumar | 273b6dc | 2016-10-26 22:07:35 +0000 | [diff] [blame] | 105 | if not args.only_merge: |
| 106 | prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir, |
Vedant Kumar | f4df0ed | 2016-10-26 22:07:39 +0000 | [diff] [blame^] | 107 | args.binaries, args.unified_report, args.restrict) |