OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2015 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 """Run the given command through LLVM's coverage tools.""" | 7 """Run the given command through LLVM's coverage tools.""" |
8 | 8 |
9 | 9 |
10 import argparse | 10 import argparse |
11 import json | 11 import json |
12 import os | 12 import os |
13 import re | 13 import re |
14 import shlex | 14 import shlex |
15 import subprocess | 15 import subprocess |
16 import sys | 16 import sys |
17 | 17 |
18 | 18 |
19 BUILDTYPE = 'Coverage' | 19 BUILDTYPE = 'Coverage' |
20 OUT_DIR = os.path.realpath(os.path.join('out', BUILDTYPE)) | 20 DEFAULT_OUT_DIR = os.path.realpath(os.path.join('out', BUILDTYPE)) |
21 PROFILE_DATA = 'default.profraw' | 21 PROFILE_DATA = 'default.profraw' |
22 PROFILE_DATA_MERGED = 'prof_merged' | 22 PROFILE_DATA_MERGED = 'prof_merged' |
23 SKIA_OUT = 'SKIA_OUT' | |
23 | 24 |
24 | 25 |
25 def _fix_filename(filename): | 26 def _fix_filename(filename): |
26 """Return a filename which we can use to identify the file. | 27 """Return a filename which we can use to identify the file. |
27 | 28 |
28 The file paths printed by llvm-cov take the form: | 29 The file paths printed by llvm-cov take the form: |
29 | 30 |
30 /path/to/repo/out/dir/../../src/filename.cpp | 31 /path/to/repo/out/dir/../../src/filename.cpp |
31 | 32 |
32 And then they're truncated to 22 characters with leading ellipses: | 33 And then they're truncated to 22 characters with leading ellipses: |
(...skipping 27 matching lines...) Expand all Loading... | |
60 matched.append(f) | 61 matched.append(f) |
61 if len(matched) == 1: | 62 if len(matched) == 1: |
62 filtered.append((percent, matched[0])) | 63 filtered.append((percent, matched[0])) |
63 elif len(matched) > 1: | 64 elif len(matched) > 1: |
64 print >> sys.stderr, ('WARNING: multiple matches for %s; skipping:\n\t%s' | 65 print >> sys.stderr, ('WARNING: multiple matches for %s; skipping:\n\t%s' |
65 % (new_file, '\n\t'.join(matched))) | 66 % (new_file, '\n\t'.join(matched))) |
66 print 'Filtered out %d files.' % (len(results) - len(filtered)) | 67 print 'Filtered out %d files.' % (len(results) - len(filtered)) |
67 return filtered | 68 return filtered |
68 | 69 |
69 | 70 |
71 def _get_out_dir(): | |
72 """Determine the location for compiled binaries.""" | |
73 if os.environ.get(SKIA_OUT): | |
mtklein
2015/07/15 14:30:46
I think we can be pithier and just as clear:
ret
borenet
2015/07/15 14:42:02
Done.
| |
74 return os.path.join(os.environ[SKIA_OUT], BUILDTYPE) | |
75 return DEFAULT_OUT_DIR | |
76 | |
77 | |
70 def run_coverage(cmd): | 78 def run_coverage(cmd): |
71 """Run the given command and return per-file coverage data. | 79 """Run the given command and return per-file coverage data. |
72 | 80 |
73 Assumes that the binary has been built using llvm_coverage_build and that | 81 Assumes that the binary has been built using llvm_coverage_build and that |
74 LLVM 3.6 or newer is installed. | 82 LLVM 3.6 or newer is installed. |
75 """ | 83 """ |
76 binary_path = os.path.join(OUT_DIR, cmd[0]) | 84 binary_path = os.path.join(_get_out_dir(), cmd[0]) |
77 subprocess.call([binary_path] + cmd[1:]) | 85 subprocess.call([binary_path] + cmd[1:]) |
78 try: | 86 try: |
79 subprocess.check_call( | 87 subprocess.check_call( |
80 ['llvm-profdata', 'merge', PROFILE_DATA, | 88 ['llvm-profdata', 'merge', PROFILE_DATA, |
81 '-output=%s' % PROFILE_DATA_MERGED]) | 89 '-output=%s' % PROFILE_DATA_MERGED]) |
82 finally: | 90 finally: |
83 os.remove(PROFILE_DATA) | 91 os.remove(PROFILE_DATA) |
84 try: | 92 try: |
85 report = subprocess.check_output( | 93 report = subprocess.check_output( |
86 ['llvm-cov', 'report', '-instr-profile', PROFILE_DATA_MERGED, | 94 ['llvm-cov', 'report', '-instr-profile', PROFILE_DATA_MERGED, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 format_results = _nanobench_json(results, properties, key) | 174 format_results = _nanobench_json(results, properties, key) |
167 if args.outResultsFile: | 175 if args.outResultsFile: |
168 with open(args.outResultsFile, 'w') as f: | 176 with open(args.outResultsFile, 'w') as f: |
169 json.dump(format_results, f) | 177 json.dump(format_results, f) |
170 else: | 178 else: |
171 print json.dumps(format_results, indent=4, sort_keys=True) | 179 print json.dumps(format_results, indent=4, sort_keys=True) |
172 | 180 |
173 | 181 |
174 if __name__ == '__main__': | 182 if __name__ == '__main__': |
175 main() | 183 main() |
OLD | NEW |