blob: 8b497506e64d3dfb59911f98d28a66bb3c2fe84c [file] [log] [blame]
borenet11271fe2015-07-06 07:43:58 -07001#!/usr/bin/env python
2# Copyright (c) 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6
7"""Run the given command through LLVM's coverage tools."""
8
9
10import argparse
borenet11271fe2015-07-06 07:43:58 -070011import os
borenet11271fe2015-07-06 07:43:58 -070012import subprocess
borenet11271fe2015-07-06 07:43:58 -070013
14
15BUILDTYPE = 'Coverage'
borenet11271fe2015-07-06 07:43:58 -070016PROFILE_DATA = 'default.profraw'
17PROFILE_DATA_MERGED = 'prof_merged'
borenetf0c84402015-07-15 07:43:06 -070018SKIA_OUT = 'SKIA_OUT'
borenet11271fe2015-07-06 07:43:58 -070019
20
borenetf0c84402015-07-15 07:43:06 -070021def _get_out_dir():
22 """Determine the location for compiled binaries."""
23 return os.path.join(os.environ.get(SKIA_OUT, os.path.realpath('out')),
24 BUILDTYPE)
25
26
borenet11271fe2015-07-06 07:43:58 -070027def run_coverage(cmd):
28 """Run the given command and return per-file coverage data.
29
30 Assumes that the binary has been built using llvm_coverage_build and that
31 LLVM 3.6 or newer is installed.
32 """
borenetf0c84402015-07-15 07:43:06 -070033 binary_path = os.path.join(_get_out_dir(), cmd[0])
borenet11271fe2015-07-06 07:43:58 -070034 subprocess.call([binary_path] + cmd[1:])
35 try:
36 subprocess.check_call(
37 ['llvm-profdata', 'merge', PROFILE_DATA,
38 '-output=%s' % PROFILE_DATA_MERGED])
39 finally:
40 os.remove(PROFILE_DATA)
41 try:
boreneta6ae14e2015-07-20 09:43:36 -070042 return subprocess.check_output(['llvm-cov', 'show', '-no-colors',
43 '-instr-profile', PROFILE_DATA_MERGED,
44 binary_path])
borenet11271fe2015-07-06 07:43:58 -070045 finally:
46 os.remove(PROFILE_DATA_MERGED)
borenet334e5882015-07-06 11:18:45 -070047
48
borenet11271fe2015-07-06 07:43:58 -070049def main():
borenet334e5882015-07-06 11:18:45 -070050 """Run coverage and generate a report."""
51 # Parse args.
52 parser = argparse.ArgumentParser()
53 parser.add_argument('--outResultsFile')
borenet334e5882015-07-06 11:18:45 -070054 args, cmd = parser.parse_known_args()
borenet8a955af2015-07-16 07:01:44 -070055
borenet334e5882015-07-06 11:18:45 -070056 # Run coverage.
boreneta6ae14e2015-07-20 09:43:36 -070057 report = run_coverage(cmd)
58 with open(args.outResultsFile, 'w') as f:
59 f.write(report)
borenet11271fe2015-07-06 07:43:58 -070060
61
62if __name__ == '__main__':
63 main()