2016-06-14 01:33:48 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-10-27 00:07:37 +02:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2016-06-14 01:33:48 +02:00
|
|
|
'''Prepare a code coverage artifact.
|
|
|
|
|
|
|
|
- Collate raw profiles into one indexed profile.
|
2016-09-22 23:49:49 +02:00
|
|
|
- Generate html reports for the given binaries.
|
2017-02-09 20:37:18 +01:00
|
|
|
|
|
|
|
Caution: The positional arguments to this script must be specified before any
|
|
|
|
optional arguments, such as --restrict.
|
2016-06-14 01:33:48 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import glob
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2016-07-19 00:50:10 +02:00
|
|
|
def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
|
2016-10-27 00:07:37 +02:00
|
|
|
print(':: Merging raw profiles...', end='')
|
2016-06-14 01:33:48 +02:00
|
|
|
sys.stdout.flush()
|
|
|
|
raw_profiles = glob.glob(os.path.join(profile_data_dir, '*.profraw'))
|
|
|
|
manifest_path = os.path.join(profile_data_dir, 'profiles.manifest')
|
|
|
|
profdata_path = os.path.join(profile_data_dir, 'Coverage.profdata')
|
|
|
|
with open(manifest_path, 'w') as manifest:
|
|
|
|
manifest.write('\n'.join(raw_profiles))
|
|
|
|
subprocess.check_call([host_llvm_profdata, 'merge', '-sparse', '-f',
|
|
|
|
manifest_path, '-o', profdata_path])
|
2016-07-19 00:50:10 +02:00
|
|
|
if not preserve_profiles:
|
|
|
|
for raw_profile in raw_profiles:
|
|
|
|
os.remove(raw_profile)
|
|
|
|
os.remove(manifest_path)
|
2016-10-27 00:07:37 +02:00
|
|
|
print('Done!')
|
2016-09-22 23:49:49 +02:00
|
|
|
return profdata_path
|
2016-06-14 01:33:48 +02:00
|
|
|
|
2016-10-27 00:07:39 +02:00
|
|
|
def prepare_html_report(host_llvm_cov, profile, report_dir, binaries,
|
2016-09-22 23:49:49 +02:00
|
|
|
restricted_dirs):
|
2016-10-27 00:07:39 +02:00
|
|
|
print(':: Preparing html report for {0}...'.format(binaries), end='')
|
2016-06-14 01:33:48 +02:00
|
|
|
sys.stdout.flush()
|
2016-10-27 00:07:39 +02:00
|
|
|
objects = []
|
|
|
|
for i, binary in enumerate(binaries):
|
|
|
|
if i == 0:
|
|
|
|
objects.append(binary)
|
|
|
|
else:
|
|
|
|
objects.extend(('-object', binary))
|
|
|
|
invocation = [host_llvm_cov, 'show'] + objects + ['-format', 'html',
|
|
|
|
'-instr-profile', profile, '-o', report_dir,
|
2016-09-22 23:49:49 +02:00
|
|
|
'-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
|
|
|
|
'-Xdemangler', '-n'] + restricted_dirs
|
|
|
|
subprocess.check_call(invocation)
|
2016-10-27 00:07:39 +02:00
|
|
|
with open(os.path.join(report_dir, 'summary.txt'), 'wb') as Summary:
|
|
|
|
subprocess.check_call([host_llvm_cov, 'report'] + objects +
|
2018-10-11 06:00:51 +02:00
|
|
|
['-instr-profile', profile] + restricted_dirs,
|
|
|
|
stdout=Summary)
|
2016-10-27 00:07:37 +02:00
|
|
|
print('Done!')
|
2016-06-14 01:33:48 +02:00
|
|
|
|
2016-09-22 23:49:49 +02:00
|
|
|
def prepare_html_reports(host_llvm_cov, profdata_path, report_dir, binaries,
|
2016-10-27 00:07:39 +02:00
|
|
|
unified_report, restricted_dirs):
|
|
|
|
if unified_report:
|
|
|
|
prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries,
|
2016-09-22 23:49:49 +02:00
|
|
|
restricted_dirs)
|
2016-10-27 00:07:39 +02:00
|
|
|
else:
|
|
|
|
for binary in binaries:
|
|
|
|
binary_report_dir = os.path.join(report_dir,
|
|
|
|
os.path.basename(binary))
|
|
|
|
prepare_html_report(host_llvm_cov, profdata_path, binary_report_dir,
|
|
|
|
[binary], restricted_dirs)
|
2016-07-19 00:50:10 +02:00
|
|
|
|
2016-06-14 01:33:48 +02:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
parser.add_argument('host_llvm_profdata', help='Path to llvm-profdata')
|
|
|
|
parser.add_argument('host_llvm_cov', help='Path to llvm-cov')
|
|
|
|
parser.add_argument('profile_data_dir',
|
|
|
|
help='Path to the directory containing the raw profiles')
|
2016-07-19 00:50:10 +02:00
|
|
|
parser.add_argument('report_dir',
|
|
|
|
help='Path to the output directory for html reports')
|
2016-10-27 00:07:35 +02:00
|
|
|
parser.add_argument('binaries', metavar='B', type=str, nargs='*',
|
2016-07-19 00:50:10 +02:00
|
|
|
help='Path to an instrumented binary')
|
2016-10-27 00:07:35 +02:00
|
|
|
parser.add_argument('--only-merge', action='store_true',
|
|
|
|
help='Only merge raw profiles together, skip report '
|
|
|
|
'generation')
|
2016-07-19 00:50:10 +02:00
|
|
|
parser.add_argument('--preserve-profiles',
|
|
|
|
help='Do not delete raw profiles', action='store_true')
|
2016-09-22 23:49:49 +02:00
|
|
|
parser.add_argument('--use-existing-profdata',
|
|
|
|
help='Specify an existing indexed profile to use')
|
2016-10-27 00:07:39 +02:00
|
|
|
parser.add_argument('--unified-report', action='store_true',
|
|
|
|
help='Emit a unified report for all binaries')
|
2016-09-22 23:49:49 +02:00
|
|
|
parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
|
|
|
|
default=[],
|
2017-02-09 20:37:18 +01:00
|
|
|
help='Restrict the reporting to the given source paths'
|
|
|
|
' (must be specified after all other positional arguments)')
|
2016-06-14 01:33:48 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2016-10-27 00:07:35 +02:00
|
|
|
if args.use_existing_profdata and args.only_merge:
|
2016-10-27 00:07:37 +02:00
|
|
|
print('--use-existing-profdata and --only-merge are incompatible')
|
2016-10-27 00:07:35 +02:00
|
|
|
exit(1)
|
|
|
|
|
2016-09-22 23:49:49 +02:00
|
|
|
if args.use_existing_profdata:
|
|
|
|
profdata_path = args.use_existing_profdata
|
|
|
|
else:
|
|
|
|
profdata_path = merge_raw_profiles(args.host_llvm_profdata,
|
|
|
|
args.profile_data_dir,
|
|
|
|
args.preserve_profiles)
|
|
|
|
|
2016-10-27 00:07:39 +02:00
|
|
|
if not len(args.binaries):
|
|
|
|
print('No binaries specified, no work to do!')
|
|
|
|
exit(1)
|
|
|
|
|
2016-10-27 00:07:35 +02:00
|
|
|
if not args.only_merge:
|
|
|
|
prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
|
2016-10-27 00:07:39 +02:00
|
|
|
args.binaries, args.unified_report, args.restrict)
|