#!/usr/bin/env python

'''Prepare a code coverage artifact.

- Collate raw profiles into one indexed profile.
- Delete the raw profiles.
- Generate html reports for llvm binaries.
'''

import argparse
import glob
import os
import subprocess
import sys

def merge_raw_profiles(host_llvm_profdata, profile_data_dir, preserve_profiles):
    print ':: Merging raw profiles...',
    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])
    if not preserve_profiles:
        for raw_profile in raw_profiles:
            os.remove(raw_profile)
    os.remove(manifest_path)
    print 'Done!'

def prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary):
    print ':: Preparing html report for {0}...'.format(binary),
    sys.stdout.flush()
    binary_report_dir = os.path.join(report_dir, os.path.basename(binary))
    profile = os.path.join(profile_data_dir, 'Coverage.profdata')
    subprocess.check_call([host_llvm_cov, 'show', binary, '-format', 'html',
                           '-instr-profile', profile, '-o', binary_report_dir,
                           '-show-line-counts-or-regions',
                           '-Xdemangler', 'c++filt', '-Xdemangler', '-n'])
    with open(os.path.join(binary_report_dir, 'summary.txt'), 'wb') as Summary:
        subprocess.check_call([host_llvm_cov, 'report', binary,
                               '-instr-profile', profile], stdout=Summary)
    print 'Done!'

def prepare_html_reports(host_llvm_cov, profile_data_dir, report_dir, binaries):
    for binary in binaries:
        prepare_html_report(host_llvm_cov, profile_data_dir, report_dir, binary)

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')
    parser.add_argument('report_dir',
                       help='Path to the output directory for html reports')
    parser.add_argument('binaries', metavar='B', type=str, nargs='+',
                       help='Path to an instrumented binary')
    parser.add_argument('--preserve-profiles',
                       help='Do not delete raw profiles', action='store_true')
    args = parser.parse_args()

    merge_raw_profiles(args.host_llvm_profdata, args.profile_data_dir,
                       args.preserve_profiles)
    prepare_html_reports(args.host_llvm_cov, args.profile_data_dir,
                         args.report_dir, args.binaries)

@@ -107,35 +107,8 @@
 le.%0A
-- Delete the raw profiles.%0A
 - Ge
@@ -131,20 +131,25 @@
 rts for 
-llvm
+the given
  binarie
@@ -962,32 +962,57 @@
    print 'Done!'
+%0A    return profdata_path
 %0A%0Adef prepare_ht
@@ -1039,33 +1039,24 @@
 cov, profile
-_data_dir
 , report_dir
@@ -1063,16 +1063,57 @@
 , binary
+,%0A                        restricted_dirs
 ):%0A    p
@@ -1277,96 +1277,21 @@
     
-profile = os.path.join(profile_data_dir, 'Coverage.profdata')%0A    subprocess.check_call(
+invocation = 
 %5Bhos
@@ -1337,25 +1337,16 @@
 'html',%0A
-         
         
@@ -1425,25 +1425,16 @@
         
-  
-       
 '-show-l
@@ -1460,35 +1460,8 @@
 ns',
-%0A                          
  '-X
@@ -1482,16 +1482,34 @@
 ++filt',
+%0A                 
  '-Xdema
@@ -1521,16 +1521,71 @@
 ', '-n'%5D
+ + restricted_dirs%0A    subprocess.check_call(invocation
 )%0A    wi
@@ -1856,36 +1856,33 @@
 vm_cov, prof
-ile_
 data_
-dir
+path
 , report_dir
@@ -1891,16 +1891,58 @@
 binaries
+,%0A                         restricted_dirs
 ):%0A    f
@@ -2011,28 +2011,25 @@
 ov, prof
-ile_
 data_
-dir
+path
 , report
@@ -2040,16 +2040,61 @@
 , binary
+,%0A                            restricted_dirs
 )%0A%0Aif __
@@ -2821,83 +2821,554 @@
     
-args = parser.parse_args()%0A%0A    merge_raw_profiles(args.host_llvm_profdata,
+parser.add_argument('--use-existing-profdata',%0A                       help='Specify an existing indexed profile to use')%0A    parser.add_argument('--restrict', metavar='R', type=str, nargs='*',%0A                       default=%5B%5D,%0A                       help='Restrict the reporting to the given source paths')%0A    args = parser.parse_args()%0A%0A    if args.use_existing_profdata:%0A        profdata_path = args.use_existing_profdata%0A    else:%0A        profdata_path = merge_raw_profiles(args.host_llvm_profdata,%0A                                          
  arg
@@ -3406,24 +3406,44 @@
             
+                   
+ 
 args.preserv
@@ -3454,16 +3454,17 @@
 ofiles)%0A
+%0A
     prep
@@ -3500,33 +3500,42 @@
 vm_cov, 
-args.profile_data
+profdata_path, args.report
 _dir,%0A  
@@ -3566,31 +3566,29 @@
 rgs.
-report_dir, args.binaries
+binaries, args.restrict
 )%0A

Teach the code coverage prep script about --restrict