aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/llvm_coverage_run.py
blob: 8b497506e64d3dfb59911f98d28a66bb3c2fe84c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# Copyright (c) 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.


"""Run the given command through LLVM's coverage tools."""


import argparse
import os
import subprocess


BUILDTYPE = 'Coverage'
PROFILE_DATA = 'default.profraw'
PROFILE_DATA_MERGED = 'prof_merged'
SKIA_OUT = 'SKIA_OUT'


def _get_out_dir():
  """Determine the location for compiled binaries."""
  return os.path.join(os.environ.get(SKIA_OUT, os.path.realpath('out')),
                      BUILDTYPE)


def run_coverage(cmd):
  """Run the given command and return per-file coverage data.

  Assumes that the binary has been built using llvm_coverage_build and that
  LLVM 3.6 or newer is installed.
  """
  binary_path = os.path.join(_get_out_dir(), cmd[0])
  subprocess.call([binary_path] + cmd[1:])
  try:
    subprocess.check_call(
        ['llvm-profdata', 'merge', PROFILE_DATA,
         '-output=%s' % PROFILE_DATA_MERGED])
  finally:
    os.remove(PROFILE_DATA)
  try:
    return subprocess.check_output(['llvm-cov', 'show', '-no-colors',
                                    '-instr-profile', PROFILE_DATA_MERGED,
                                    binary_path])
  finally:
    os.remove(PROFILE_DATA_MERGED)


def main():
  """Run coverage and generate a report."""
  # Parse args.
  parser = argparse.ArgumentParser()
  parser.add_argument('--outResultsFile')
  args, cmd = parser.parse_known_args()

  # Run coverage.
  report = run_coverage(cmd)
  with open(args.outResultsFile, 'w') as f:
    f.write(report)


if __name__ == '__main__':
  main()