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