#!/usr/bin/env python

from __future__ import print_function

'''Prepare a code coverage artifact.

- Collate raw profiles into one indexed profile.
- Generate html reports for the given 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...', end='')
    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!')
    return profdata_path

def prepare_html_report(host_llvm_cov, profile, report_dir, binary,
                        restricted_dirs):
    print(':: Preparing html report for {0}...'.format(binary), end='')
    sys.stdout.flush()
    binary_report_dir = os.path.join(report_dir, os.path.basename(binary))
    invocation = [host_llvm_cov, 'show', binary, '-format', 'html',
                  '-instr-profile', profile, '-o', binary_report_dir,
                  '-show-line-counts-or-regions', '-Xdemangler', 'c++filt',
                  '-Xdemangler', '-n'] + restricted_dirs
    subprocess.check_call(invocation)
    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, profdata_path, report_dir, binaries,
                         restricted_dirs):
    for binary in binaries:
        prepare_html_report(host_llvm_cov, profdata_path, report_dir, binary,
                            restricted_dirs)

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('--only-merge', action='store_true',
                        help='Only merge raw profiles together, skip report '
                             'generation')
    parser.add_argument('--preserve-profiles',
                       help='Do not delete raw profiles', action='store_true')
    parser.add_argument('--use-existing-profdata',
                       help='Specify an existing indexed profile to use')
    parser.add_argument('--restrict', metavar='R', type=str, nargs='*',
                       default=[],
                       help='Restrict the reporting to the given source paths')
    args = parser.parse_args()

    if args.use_existing_profdata and args.only_merge:
        print('--use-existing-profdata and --only-merge are incompatible')
        exit(1)

    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)

    if not args.only_merge:
        prepare_html_reports(args.host_llvm_cov, profdata_path, args.report_dir,
                            args.binaries, args.restrict)

@@ -1102,33 +1102,35 @@
 eport_dir, binar
-y
+ies
 ,%0A              
@@ -1217,17 +1217,19 @@
 at(binar
-y
+ies
 ), end='
@@ -1262,70 +1262,170 @@
     
-binary_report_dir = os.path.join(report_dir, os.path.basename(
+objects = %5B%5D%0A    for i, binary in enumerate(binaries):%0A        if i == 0:%0A            objects.append(binary)%0A        else:%0A            objects.extend(('-object', 
 bina
@@ -1472,18 +1472,23 @@
 how'
-, binary, 
+%5D + objects + %5B
 '-fo
@@ -1553,23 +1553,16 @@
 , '-o', 
-binary_
 report_d
@@ -1767,15 +1767,8 @@
 oin(
-binary_
 repo
@@ -1863,25 +1863,29 @@
 'report'
-, binary,
+%5D + objects +
 %0A       
@@ -1900,32 +1900,33 @@
                 
+%5B
 '-instr-profile'
@@ -2066,32 +2066,48 @@
                 
+ unified_report,
  restricted_dirs
@@ -2117,32 +2117,326 @@
     
-for binary in binaries:%0A
+if unified_report:%0A        prepare_html_report(host_llvm_cov, profdata_path, report_dir, binaries,%0A                            restricted_dirs)%0A    else:%0A        for binary in binaries:%0A            binary_report_dir = os.path.join(report_dir,%0A                                             os.path.basename(binary))%0A    
     
@@ -2481,32 +2481,39 @@
  profdata_path, 
+binary_
 report_dir, bina
@@ -2511,16 +2511,8 @@
 dir,
- binary,
 %0A   
@@ -2527,32 +2527,46 @@
                 
+     %5Bbinary%5D,
  restricted_dirs
@@ -3596,24 +3596,159 @@
 le to use')%0A
+    parser.add_argument('--unified-report', action='store_true',%0A                       help='Emit a unified report for all binaries')%0A
     parser.a
@@ -4399,16 +4399,119 @@
 files)%0A%0A
+    if not len(args.binaries):%0A        print('No binaries specified, no work to do!')%0A        exit(1)%0A%0A
     if n
@@ -4653,16 +4653,37 @@
 inaries,
+ args.unified_report,
  args.re

Add a '--unified-report' option to the code coverage prep script