Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1 | #!/usr/bin/python |
| 2 | # Copyright 2017 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. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 5 | """This script helps to generate code coverage report. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 6 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 7 | It uses Clang Source-based Code Coverage - |
| 8 | https://clang.llvm.org/docs/SourceBasedCodeCoverage.html |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 9 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 10 | In order to generate code coverage report, you need to first add |
| 11 | "use_clang_coverage=true" GN flag to args.gn file in your build |
| 12 | output directory (e.g. out/coverage). |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 13 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 14 | It is recommended to add "is_component_build=false" flag as well because: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 15 | 1. It is incompatible with other sanitizer flags (like "is_asan", "is_msan") |
| 16 | and others like "optimize_for_fuzzing". |
| 17 | 2. If it is not set explicitly, "is_debug" overrides it to true. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 18 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 19 | Example usage: |
| 20 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 21 | gn gen out/coverage --args='use_clang_coverage=true is_component_build=false' |
| 22 | gclient runhooks |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 23 | python tools/code_coverage/coverage.py crypto_unittests url_unittests \\ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 24 | -b out/coverage -o out/report -c 'out/coverage/crypto_unittests' \\ |
| 25 | -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \\ |
| 26 | -f url/ -f crypto/ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 27 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 28 | The command above builds crypto_unittests and url_unittests targets and then |
| 29 | runs them with specified command line arguments. For url_unittests, it only |
| 30 | runs the test URLParser.PathURL. The coverage report is filtered to include |
| 31 | only files and sub-directories under url/ and crypto/ directories. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 32 | |
| 33 | If you are building a fuzz target, you need to add "use_libfuzzer=true" GN |
| 34 | flag as well. |
| 35 | |
| 36 | Sample workflow for a fuzz target (e.g. pdfium_fuzzer): |
| 37 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 38 | python tools/code_coverage/coverage.py pdfium_fuzzer \\ |
| 39 | -b out/coverage -o out/report \\ |
| 40 | -c 'out/coverage/pdfium_fuzzer -runs=<runs> <corpus_dir>' \\ |
| 41 | -f third_party/pdfium |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 42 | |
| 43 | where: |
| 44 | <corpus_dir> - directory containing samples files for this format. |
| 45 | <runs> - number of times to fuzz target function. Should be 0 when you just |
| 46 | want to see the coverage on corpus and don't want to fuzz at all. |
| 47 | |
| 48 | For more options, please refer to tools/code_coverage/coverage.py -h. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 49 | """ |
| 50 | |
| 51 | from __future__ import print_function |
| 52 | |
| 53 | import sys |
| 54 | |
| 55 | import argparse |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 56 | import json |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 57 | import os |
| 58 | import subprocess |
| 59 | import threading |
| 60 | import urllib2 |
| 61 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 62 | sys.path.append( |
| 63 | os.path.join( |
| 64 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'tools', |
| 65 | 'clang', 'scripts')) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 66 | import update as clang_update |
| 67 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 68 | sys.path.append( |
| 69 | os.path.join( |
| 70 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, |
| 71 | 'third_party')) |
| 72 | import jinja2 |
| 73 | from collections import defaultdict |
| 74 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 75 | # Absolute path to the root of the checkout. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 76 | SRC_ROOT_PATH = os.path.abspath( |
| 77 | os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 78 | |
| 79 | # Absolute path to the code coverage tools binary. |
| 80 | LLVM_BUILD_DIR = clang_update.LLVM_BUILD_DIR |
| 81 | LLVM_COV_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-cov') |
| 82 | LLVM_PROFDATA_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-profdata') |
| 83 | |
| 84 | # Build directory, the value is parsed from command line arguments. |
| 85 | BUILD_DIR = None |
| 86 | |
| 87 | # Output directory for generated artifacts, the value is parsed from command |
| 88 | # line arguemnts. |
| 89 | OUTPUT_DIR = None |
| 90 | |
| 91 | # Default number of jobs used to build when goma is configured and enabled. |
| 92 | DEFAULT_GOMA_JOBS = 100 |
| 93 | |
| 94 | # Name of the file extension for profraw data files. |
| 95 | PROFRAW_FILE_EXTENSION = 'profraw' |
| 96 | |
| 97 | # Name of the final profdata file, and this file needs to be passed to |
| 98 | # "llvm-cov" command in order to call "llvm-cov show" to inspect the |
| 99 | # line-by-line coverage of specific files. |
| 100 | PROFDATA_FILE_NAME = 'coverage.profdata' |
| 101 | |
| 102 | # Build arg required for generating code coverage data. |
| 103 | CLANG_COVERAGE_BUILD_ARG = 'use_clang_coverage' |
| 104 | |
| 105 | # A set of targets that depend on target "testing/gtest", this set is generated |
| 106 | # by 'gn refs "testing/gtest"', and it is lazily initialized when needed. |
| 107 | GTEST_TARGET_NAMES = None |
| 108 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 109 | # The default name of the html coverage report for a directory. |
| 110 | DIRECTORY_COVERAGE_HTML_REPORT_NAME = os.extsep.join(['report', 'html']) |
| 111 | |
| 112 | |
| 113 | class _CoverageSummary(object): |
| 114 | """Encapsulates coverage summary representation.""" |
| 115 | |
| 116 | def __init__(self, regions_total, regions_covered, functions_total, |
| 117 | functions_covered, lines_total, lines_covered): |
| 118 | """Initializes _CoverageSummary object.""" |
| 119 | self._summary = { |
| 120 | 'regions': { |
| 121 | 'total': regions_total, |
| 122 | 'covered': regions_covered |
| 123 | }, |
| 124 | 'functions': { |
| 125 | 'total': functions_total, |
| 126 | 'covered': functions_covered |
| 127 | }, |
| 128 | 'lines': { |
| 129 | 'total': lines_total, |
| 130 | 'covered': lines_covered |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | def Get(self): |
| 135 | """Returns summary as a dictionary.""" |
| 136 | return self._summary |
| 137 | |
| 138 | def AddSummary(self, other_summary): |
| 139 | """Adds another summary to this one element-wise.""" |
| 140 | for feature in self._summary: |
| 141 | self._summary[feature]['total'] += other_summary.Get()[feature]['total'] |
| 142 | self._summary[feature]['covered'] += other_summary.Get()[feature][ |
| 143 | 'covered'] |
| 144 | |
| 145 | |
| 146 | class _DirectoryCoverageReportHtmlGenerator(object): |
| 147 | """Encapsulates coverage html report generation for a directory. |
| 148 | |
| 149 | The generated html has a table that contains links to the coverage report of |
| 150 | its sub-directories and files. Please refer to ./directory_example_report.html |
| 151 | for an example of the generated html file. |
| 152 | """ |
| 153 | |
| 154 | def __init__(self): |
| 155 | """Initializes _DirectoryCoverageReportHtmlGenerator object.""" |
| 156 | css_file_name = os.extsep.join(['style', 'css']) |
| 157 | css_absolute_path = os.path.abspath(os.path.join(OUTPUT_DIR, css_file_name)) |
| 158 | assert os.path.exists(css_absolute_path), ( |
| 159 | 'css file doesn\'t exit. Please make sure "llvm-cov show -format=html" ' |
| 160 | 'is called first, and the css file is generated at: "%s"' % |
| 161 | css_absolute_path) |
| 162 | |
| 163 | self._css_absolute_path = css_absolute_path |
| 164 | self._table_entries = [] |
| 165 | template_dir = os.path.join( |
| 166 | os.path.dirname(os.path.realpath(__file__)), 'html_templates') |
| 167 | |
| 168 | jinja_env = jinja2.Environment( |
| 169 | loader=jinja2.FileSystemLoader(template_dir), trim_blocks=True) |
| 170 | self._header_template = jinja_env.get_template('header.html') |
| 171 | self._table_template = jinja_env.get_template('table.html') |
| 172 | self._footer_template = jinja_env.get_template('footer.html') |
| 173 | |
| 174 | def AddLinkToAnotherReport(self, html_report_path, name, summary): |
| 175 | """Adds a link to another html report in this report. |
| 176 | |
| 177 | The link to be added is assumed to be an entry in this directory. |
| 178 | """ |
| 179 | table_entry = { |
| 180 | 'href': |
| 181 | html_report_path, |
| 182 | 'name': |
| 183 | name, |
| 184 | 'is_dir': |
| 185 | os.path.basename(html_report_path) == |
| 186 | DIRECTORY_COVERAGE_HTML_REPORT_NAME |
| 187 | } |
| 188 | summary_dict = summary.Get() |
| 189 | for feature in summary_dict.keys(): |
| 190 | percentage = round((float(summary_dict[feature]['covered'] |
| 191 | ) / summary_dict[feature]['total']) * 100, 2) |
| 192 | color_class = self._GetColorClass(percentage) |
| 193 | table_entry[feature] = { |
| 194 | 'total': summary_dict[feature]['total'], |
| 195 | 'covered': summary_dict[feature]['covered'], |
| 196 | 'percentage': percentage, |
| 197 | 'color_class': color_class |
| 198 | } |
| 199 | self._table_entries.append(table_entry) |
| 200 | |
| 201 | def _GetColorClass(self, percentage): |
| 202 | """Returns the css color class based on coverage percentage.""" |
| 203 | if percentage >= 0 and percentage < 80: |
| 204 | return 'red' |
| 205 | if percentage >= 80 and percentage < 100: |
| 206 | return 'yellow' |
| 207 | if percentage == 100: |
| 208 | return 'green' |
| 209 | |
| 210 | assert False, 'Invalid coverage percentage: "%d"' % percentage |
| 211 | |
| 212 | def WriteHtmlCoverageReport(self, output_path): |
| 213 | """Write html coverage report for the directory. |
| 214 | |
| 215 | In the report, sub-directories are displayed before files and within each |
| 216 | category, entries are sorted alphabetically. |
| 217 | |
| 218 | Args: |
| 219 | output_path: A path to the html report. |
| 220 | """ |
| 221 | |
| 222 | def EntryCmp(left, right): |
| 223 | """Compare function for table entries.""" |
| 224 | if left['is_dir'] != right['is_dir']: |
| 225 | return -1 if left['is_dir'] == True else 1 |
| 226 | |
| 227 | return left['name'] < right['name'] |
| 228 | |
| 229 | self._table_entries = sorted(self._table_entries, cmp=EntryCmp) |
| 230 | |
| 231 | css_path = os.path.join(OUTPUT_DIR, os.extsep.join(['style', 'css'])) |
| 232 | html_header = self._header_template.render( |
| 233 | css_path=os.path.relpath(css_path, os.path.dirname(output_path))) |
| 234 | html_table = self._table_template.render(entries=self._table_entries) |
| 235 | html_footer = self._footer_template.render() |
| 236 | |
| 237 | with open(output_path, 'w') as html_file: |
| 238 | html_file.write(html_header + html_table + html_footer) |
| 239 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 240 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 241 | def _GetPlatform(): |
| 242 | """Returns current running platform.""" |
| 243 | if sys.platform == 'win32' or sys.platform == 'cygwin': |
| 244 | return 'win' |
| 245 | if sys.platform.startswith('linux'): |
| 246 | return 'linux' |
| 247 | else: |
| 248 | assert sys.platform == 'darwin' |
| 249 | return 'mac' |
| 250 | |
| 251 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 252 | # TODO(crbug.com/759794): remove this function once tools get included to |
| 253 | # Clang bundle: |
| 254 | # https://chromium-review.googlesource.com/c/chromium/src/+/688221 |
| 255 | def DownloadCoverageToolsIfNeeded(): |
| 256 | """Temporary solution to download llvm-profdata and llvm-cov tools.""" |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 257 | |
| 258 | def _GetRevisionFromStampFile(stamp_file_path, platform): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 259 | """Returns a pair of revision number by reading the build stamp file. |
| 260 | |
| 261 | Args: |
| 262 | stamp_file_path: A path the build stamp file created by |
| 263 | tools/clang/scripts/update.py. |
| 264 | Returns: |
| 265 | A pair of integers represeting the main and sub revision respectively. |
| 266 | """ |
| 267 | if not os.path.exists(stamp_file_path): |
| 268 | return 0, 0 |
| 269 | |
| 270 | with open(stamp_file_path) as stamp_file: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 271 | for stamp_file_line in stamp_file.readlines(): |
| 272 | if ',' in stamp_file_line: |
| 273 | package_version, target_os = stamp_file_line.rstrip().split(',') |
| 274 | else: |
| 275 | package_version = stamp_file_line.rstrip() |
| 276 | target_os = '' |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 277 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 278 | if target_os and platform != target_os: |
| 279 | continue |
| 280 | |
| 281 | clang_revision_str, clang_sub_revision_str = package_version.split('-') |
| 282 | return int(clang_revision_str), int(clang_sub_revision_str) |
| 283 | |
| 284 | assert False, 'Coverage is only supported on target_os - linux, mac.' |
| 285 | |
| 286 | platform = _GetPlatform() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 287 | clang_revision, clang_sub_revision = _GetRevisionFromStampFile( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 288 | clang_update.STAMP_FILE, platform) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 289 | |
| 290 | coverage_revision_stamp_file = os.path.join( |
| 291 | os.path.dirname(clang_update.STAMP_FILE), 'cr_coverage_revision') |
| 292 | coverage_revision, coverage_sub_revision = _GetRevisionFromStampFile( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 293 | coverage_revision_stamp_file, platform) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 294 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 295 | has_coverage_tools = ( |
| 296 | os.path.exists(LLVM_COV_PATH) and os.path.exists(LLVM_PROFDATA_PATH)) |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 297 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 298 | if (has_coverage_tools and coverage_revision == clang_revision and |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 299 | coverage_sub_revision == clang_sub_revision): |
| 300 | # LLVM coverage tools are up to date, bail out. |
| 301 | return clang_revision |
| 302 | |
| 303 | package_version = '%d-%d' % (clang_revision, clang_sub_revision) |
| 304 | coverage_tools_file = 'llvm-code-coverage-%s.tgz' % package_version |
| 305 | |
| 306 | # The code bellow follows the code from tools/clang/scripts/update.py. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 307 | if platform == 'mac': |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 308 | coverage_tools_url = clang_update.CDS_URL + '/Mac/' + coverage_tools_file |
| 309 | else: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 310 | assert platform == 'linux' |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 311 | coverage_tools_url = ( |
| 312 | clang_update.CDS_URL + '/Linux_x64/' + coverage_tools_file) |
| 313 | |
| 314 | try: |
| 315 | clang_update.DownloadAndUnpack(coverage_tools_url, |
| 316 | clang_update.LLVM_BUILD_DIR) |
| 317 | print('Coverage tools %s unpacked' % package_version) |
| 318 | with open(coverage_revision_stamp_file, 'w') as file_handle: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 319 | file_handle.write('%s,%s' % (package_version, platform)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 320 | file_handle.write('\n') |
| 321 | except urllib2.URLError: |
| 322 | raise Exception( |
| 323 | 'Failed to download coverage tools: %s.' % coverage_tools_url) |
| 324 | |
| 325 | |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 326 | def _GenerateLineByLineFileCoverageInHtml(binary_paths, profdata_file_path, |
| 327 | filters): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 328 | """Generates per file line-by-line coverage in html using 'llvm-cov show'. |
| 329 | |
| 330 | For a file with absolute path /a/b/x.cc, a html report is generated as: |
| 331 | OUTPUT_DIR/coverage/a/b/x.cc.html. An index html file is also generated as: |
| 332 | OUTPUT_DIR/index.html. |
| 333 | |
| 334 | Args: |
| 335 | binary_paths: A list of paths to the instrumented binaries. |
| 336 | profdata_file_path: A path to the profdata file. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 337 | filters: A list of directories and files to get coverage for. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 338 | """ |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 339 | # llvm-cov show [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 340 | # [[-object BIN]] [SOURCES] |
| 341 | # NOTE: For object files, the first one is specified as a positional argument, |
| 342 | # and the rest are specified as keyword argument. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 343 | subprocess_cmd = [ |
| 344 | LLVM_COV_PATH, 'show', '-format=html', |
| 345 | '-output-dir={}'.format(OUTPUT_DIR), |
| 346 | '-instr-profile={}'.format(profdata_file_path), binary_paths[0] |
| 347 | ] |
| 348 | subprocess_cmd.extend( |
| 349 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 350 | subprocess_cmd.extend(filters) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 351 | |
| 352 | subprocess.check_call(subprocess_cmd) |
| 353 | |
| 354 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 355 | def _GeneratePerDirectoryCoverageInHtml(binary_paths, profdata_file_path, |
| 356 | filters): |
| 357 | """Generates coverage breakdown per directory.""" |
| 358 | per_file_coverage_summary = _GeneratePerFileCoverageSummary( |
| 359 | binary_paths, profdata_file_path, filters) |
| 360 | |
| 361 | per_directory_coverage_summary = defaultdict( |
| 362 | lambda: _CoverageSummary(0, 0, 0, 0, 0, 0)) |
| 363 | |
| 364 | # Calculate per directory code coverage summaries. |
| 365 | for file_path in per_file_coverage_summary: |
| 366 | summary = per_file_coverage_summary[file_path] |
| 367 | parent_dir = os.path.dirname(file_path) |
| 368 | while True: |
| 369 | per_directory_coverage_summary[parent_dir].AddSummary(summary) |
| 370 | |
| 371 | if parent_dir == SRC_ROOT_PATH: |
| 372 | break |
| 373 | parent_dir = os.path.dirname(parent_dir) |
| 374 | |
| 375 | for dir_path in per_directory_coverage_summary: |
| 376 | _GenerateCoverageInHtmlForDirectory( |
| 377 | dir_path, per_directory_coverage_summary, per_file_coverage_summary) |
| 378 | |
| 379 | |
| 380 | def _GenerateCoverageInHtmlForDirectory( |
| 381 | dir_path, per_directory_coverage_summary, per_file_coverage_summary): |
| 382 | """Generates coverage html report for a single directory.""" |
| 383 | html_generator = _DirectoryCoverageReportHtmlGenerator() |
| 384 | |
| 385 | for entry_name in os.listdir(dir_path): |
| 386 | entry_path = os.path.normpath(os.path.join(dir_path, entry_name)) |
| 387 | entry_html_report_path = _GetCoverageHtmlReportPath(entry_path) |
| 388 | |
| 389 | # Use relative paths instead of absolute paths to make the generated |
| 390 | # reports portable. |
| 391 | html_report_path = _GetCoverageHtmlReportPath(dir_path) |
| 392 | entry_html_report_relative_path = os.path.relpath( |
| 393 | entry_html_report_path, os.path.dirname(html_report_path)) |
| 394 | |
| 395 | if entry_path in per_directory_coverage_summary: |
| 396 | html_generator.AddLinkToAnotherReport( |
| 397 | entry_html_report_relative_path, os.path.basename(entry_path), |
| 398 | per_directory_coverage_summary[entry_path]) |
| 399 | elif entry_path in per_file_coverage_summary: |
| 400 | html_generator.AddLinkToAnotherReport( |
| 401 | entry_html_report_relative_path, os.path.basename(entry_path), |
| 402 | per_file_coverage_summary[entry_path]) |
| 403 | |
| 404 | html_generator.WriteHtmlCoverageReport(html_report_path) |
| 405 | |
| 406 | |
| 407 | def _OverwriteHtmlReportsIndexFile(): |
| 408 | """Overwrites the index file to link to the source root directory report.""" |
| 409 | html_index_file_path = os.path.join(OUTPUT_DIR, |
| 410 | os.extsep.join(['index', 'html'])) |
| 411 | src_root_html_report_path = _GetCoverageHtmlReportPath(SRC_ROOT_PATH) |
| 412 | src_root_html_report_relative_path = os.path.relpath( |
| 413 | src_root_html_report_path, os.path.dirname(html_index_file_path)) |
| 414 | content = (""" |
| 415 | <!DOCTYPE html> |
| 416 | <html> |
| 417 | <head> |
| 418 | <!-- HTML meta refresh URL redirection --> |
| 419 | <meta http-equiv="refresh" content="0; url=%s"> |
| 420 | </head> |
| 421 | </html>""" % src_root_html_report_relative_path) |
| 422 | with open(html_index_file_path, 'w') as f: |
| 423 | f.write(content) |
| 424 | |
| 425 | |
| 426 | def _GetCoverageHtmlReportPath(file_or_dir_path): |
| 427 | """Given a file or directory, returns the corresponding html report path.""" |
| 428 | html_path = ( |
| 429 | os.path.join(os.path.abspath(OUTPUT_DIR), 'coverage') + |
| 430 | os.path.abspath(file_or_dir_path)) |
| 431 | if os.path.isdir(file_or_dir_path): |
| 432 | return os.path.join(html_path, DIRECTORY_COVERAGE_HTML_REPORT_NAME) |
| 433 | else: |
| 434 | return os.extsep.join([html_path, 'html']) |
| 435 | |
| 436 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 437 | def _CreateCoverageProfileDataForTargets(targets, commands, jobs_count=None): |
| 438 | """Builds and runs target to generate the coverage profile data. |
| 439 | |
| 440 | Args: |
| 441 | targets: A list of targets to build with coverage instrumentation. |
| 442 | commands: A list of commands used to run the targets. |
| 443 | jobs_count: Number of jobs to run in parallel for building. If None, a |
| 444 | default value is derived based on CPUs availability. |
| 445 | |
| 446 | Returns: |
| 447 | A relative path to the generated profdata file. |
| 448 | """ |
| 449 | _BuildTargets(targets, jobs_count) |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 450 | profraw_file_paths = _GetProfileRawDataPathsByExecutingCommands( |
| 451 | targets, commands) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 452 | profdata_file_path = _CreateCoverageProfileDataFromProfRawData( |
| 453 | profraw_file_paths) |
| 454 | |
| 455 | return profdata_file_path |
| 456 | |
| 457 | |
| 458 | def _BuildTargets(targets, jobs_count): |
| 459 | """Builds target with Clang coverage instrumentation. |
| 460 | |
| 461 | This function requires current working directory to be the root of checkout. |
| 462 | |
| 463 | Args: |
| 464 | targets: A list of targets to build with coverage instrumentation. |
| 465 | jobs_count: Number of jobs to run in parallel for compilation. If None, a |
| 466 | default value is derived based on CPUs availability. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 467 | """ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 468 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 469 | def _IsGomaConfigured(): |
| 470 | """Returns True if goma is enabled in the gn build args. |
| 471 | |
| 472 | Returns: |
| 473 | A boolean indicates whether goma is configured for building or not. |
| 474 | """ |
| 475 | build_args = _ParseArgsGnFile() |
| 476 | return 'use_goma' in build_args and build_args['use_goma'] == 'true' |
| 477 | |
| 478 | print('Building %s' % str(targets)) |
| 479 | |
| 480 | if jobs_count is None and _IsGomaConfigured(): |
| 481 | jobs_count = DEFAULT_GOMA_JOBS |
| 482 | |
| 483 | subprocess_cmd = ['ninja', '-C', BUILD_DIR] |
| 484 | if jobs_count is not None: |
| 485 | subprocess_cmd.append('-j' + str(jobs_count)) |
| 486 | |
| 487 | subprocess_cmd.extend(targets) |
| 488 | subprocess.check_call(subprocess_cmd) |
| 489 | |
| 490 | |
| 491 | def _GetProfileRawDataPathsByExecutingCommands(targets, commands): |
| 492 | """Runs commands and returns the relative paths to the profraw data files. |
| 493 | |
| 494 | Args: |
| 495 | targets: A list of targets built with coverage instrumentation. |
| 496 | commands: A list of commands used to run the targets. |
| 497 | |
| 498 | Returns: |
| 499 | A list of relative paths to the generated profraw data files. |
| 500 | """ |
| 501 | # Remove existing profraw data files. |
| 502 | for file_or_dir in os.listdir(OUTPUT_DIR): |
| 503 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
| 504 | os.remove(os.path.join(OUTPUT_DIR, file_or_dir)) |
| 505 | |
| 506 | # Run different test targets in parallel to generate profraw data files. |
| 507 | threads = [] |
| 508 | for target, command in zip(targets, commands): |
| 509 | thread = threading.Thread(target=_ExecuteCommand, args=(target, command)) |
| 510 | thread.start() |
| 511 | threads.append(thread) |
| 512 | for thread in threads: |
| 513 | thread.join() |
| 514 | |
| 515 | profraw_file_paths = [] |
| 516 | for file_or_dir in os.listdir(OUTPUT_DIR): |
| 517 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
| 518 | profraw_file_paths.append(os.path.join(OUTPUT_DIR, file_or_dir)) |
| 519 | |
| 520 | # Assert one target/command generates at least one profraw data file. |
| 521 | for target in targets: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 522 | assert any( |
| 523 | os.path.basename(profraw_file).startswith(target) |
| 524 | for profraw_file in profraw_file_paths), ( |
| 525 | 'Running target: %s failed to generate any profraw data file, ' |
| 526 | 'please make sure the binary exists and is properly instrumented.' % |
| 527 | target) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 528 | |
| 529 | return profraw_file_paths |
| 530 | |
| 531 | |
| 532 | def _ExecuteCommand(target, command): |
| 533 | """Runs a single command and generates a profraw data file. |
| 534 | |
| 535 | Args: |
| 536 | target: A target built with coverage instrumentation. |
| 537 | command: A command used to run the target. |
| 538 | """ |
| 539 | if _IsTargetGTestTarget(target): |
| 540 | # This test argument is required and only required for gtest unit test |
| 541 | # targets because by default, they run tests in parallel, and that won't |
| 542 | # generated code coverage data correctly. |
| 543 | command += ' --test-launcher-jobs=1' |
| 544 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 545 | expected_profraw_file_name = os.extsep.join( |
| 546 | [target, '%p', PROFRAW_FILE_EXTENSION]) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 547 | expected_profraw_file_path = os.path.join(OUTPUT_DIR, |
| 548 | expected_profraw_file_name) |
| 549 | output_file_name = os.extsep.join([target + '_output', 'txt']) |
| 550 | output_file_path = os.path.join(OUTPUT_DIR, output_file_name) |
| 551 | |
| 552 | print('Running command: "%s", the output is redirected to "%s"' % |
| 553 | (command, output_file_path)) |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 554 | output = subprocess.check_output( |
| 555 | command.split(), env={ |
| 556 | 'LLVM_PROFILE_FILE': expected_profraw_file_path |
| 557 | }) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 558 | with open(output_file_path, 'w') as output_file: |
| 559 | output_file.write(output) |
| 560 | |
| 561 | |
| 562 | def _CreateCoverageProfileDataFromProfRawData(profraw_file_paths): |
| 563 | """Returns a relative path to the profdata file by merging profraw data files. |
| 564 | |
| 565 | Args: |
| 566 | profraw_file_paths: A list of relative paths to the profraw data files that |
| 567 | are to be merged. |
| 568 | |
| 569 | Returns: |
| 570 | A relative path to the generated profdata file. |
| 571 | |
| 572 | Raises: |
| 573 | CalledProcessError: An error occurred merging profraw data files. |
| 574 | """ |
| 575 | print('Creating the profile data file') |
| 576 | |
| 577 | profdata_file_path = os.path.join(OUTPUT_DIR, PROFDATA_FILE_NAME) |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 578 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 579 | try: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 580 | subprocess_cmd = [ |
| 581 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 582 | ] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 583 | subprocess_cmd.extend(profraw_file_paths) |
| 584 | subprocess.check_call(subprocess_cmd) |
| 585 | except subprocess.CalledProcessError as error: |
| 586 | print('Failed to merge profraw files to create profdata file') |
| 587 | raise error |
| 588 | |
| 589 | return profdata_file_path |
| 590 | |
| 591 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 592 | def _GeneratePerFileCoverageSummary(binary_paths, profdata_file_path, filters): |
| 593 | """Generates per file coverage summary using "llvm-cov export" command.""" |
| 594 | # llvm-cov export [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 595 | # [[-object BIN]] [SOURCES]. |
| 596 | # NOTE: For object files, the first one is specified as a positional argument, |
| 597 | # and the rest are specified as keyword argument. |
| 598 | subprocess_cmd = [ |
| 599 | LLVM_COV_PATH, 'export', '-summary-only', |
| 600 | '-instr-profile=' + profdata_file_path, binary_paths[0] |
| 601 | ] |
| 602 | subprocess_cmd.extend( |
| 603 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
| 604 | subprocess_cmd.extend(filters) |
| 605 | |
| 606 | json_output = json.loads(subprocess.check_output(subprocess_cmd)) |
| 607 | assert len(json_output['data']) == 1 |
| 608 | files_coverage_data = json_output['data'][0]['files'] |
| 609 | |
| 610 | per_file_coverage_summary = {} |
| 611 | for file_coverage_data in files_coverage_data: |
| 612 | file_path = file_coverage_data['filename'] |
| 613 | summary = file_coverage_data['summary'] |
| 614 | |
| 615 | # TODO(crbug.com/797345): Currently, [SOURCES] parameter doesn't apply to |
| 616 | # llvm-cov export command, so work it around by manually filter the paths. |
| 617 | # Remove this logic once the bug is fixed and clang has rolled past it. |
| 618 | if filters and not any( |
| 619 | os.path.abspath(file_path).startswith(os.path.abspath(filter)) |
| 620 | for filter in filters): |
| 621 | continue |
| 622 | |
| 623 | if summary['lines']['count'] == 0: |
| 624 | continue |
| 625 | |
| 626 | per_file_coverage_summary[file_path] = _CoverageSummary( |
| 627 | regions_total=summary['regions']['count'], |
| 628 | regions_covered=summary['regions']['covered'], |
| 629 | functions_total=summary['functions']['count'], |
| 630 | functions_covered=summary['functions']['covered'], |
| 631 | lines_total=summary['lines']['count'], |
| 632 | lines_covered=summary['lines']['covered']) |
| 633 | |
| 634 | return per_file_coverage_summary |
| 635 | |
| 636 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 637 | def _GetBinaryPath(command): |
| 638 | """Returns a relative path to the binary to be run by the command. |
| 639 | |
| 640 | Args: |
| 641 | command: A command used to run a target. |
| 642 | |
| 643 | Returns: |
| 644 | A relative path to the binary. |
| 645 | """ |
| 646 | return command.split()[0] |
| 647 | |
| 648 | |
| 649 | def _IsTargetGTestTarget(target): |
| 650 | """Returns True if the target is a gtest target. |
| 651 | |
| 652 | Args: |
| 653 | target: A target built with coverage instrumentation. |
| 654 | |
| 655 | Returns: |
| 656 | A boolean value indicates whether the target is a gtest target. |
| 657 | """ |
| 658 | global GTEST_TARGET_NAMES |
| 659 | if GTEST_TARGET_NAMES is None: |
| 660 | output = subprocess.check_output(['gn', 'refs', BUILD_DIR, 'testing/gtest']) |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 661 | list_of_gtest_targets = [ |
| 662 | gtest_target for gtest_target in output.splitlines() if gtest_target |
| 663 | ] |
| 664 | GTEST_TARGET_NAMES = set( |
| 665 | [gtest_target.split(':')[1] for gtest_target in list_of_gtest_targets]) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 666 | |
| 667 | return target in GTEST_TARGET_NAMES |
| 668 | |
| 669 | |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 670 | def _VerifyTargetExecutablesAreInBuildDirectory(commands): |
| 671 | """Verifies that the target executables specified in the commands are inside |
| 672 | the given build directory.""" |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 673 | for command in commands: |
| 674 | binary_path = _GetBinaryPath(command) |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 675 | binary_absolute_path = os.path.abspath(os.path.normpath(binary_path)) |
| 676 | assert binary_absolute_path.startswith(os.path.abspath(BUILD_DIR)), ( |
| 677 | 'Target executable "%s" in command: "%s" is outside of ' |
| 678 | 'the given build directory: "%s".' % (binary_path, command, BUILD_DIR)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 679 | |
| 680 | |
| 681 | def _ValidateBuildingWithClangCoverage(): |
| 682 | """Asserts that targets are built with Clang coverage enabled.""" |
| 683 | build_args = _ParseArgsGnFile() |
| 684 | |
| 685 | if (CLANG_COVERAGE_BUILD_ARG not in build_args or |
| 686 | build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'): |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 687 | assert False, ('\'{} = true\' is required in args.gn.' |
| 688 | ).format(CLANG_COVERAGE_BUILD_ARG) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 689 | |
| 690 | |
| 691 | def _ParseArgsGnFile(): |
| 692 | """Parses args.gn file and returns results as a dictionary. |
| 693 | |
| 694 | Returns: |
| 695 | A dictionary representing the build args. |
| 696 | """ |
| 697 | build_args_path = os.path.join(BUILD_DIR, 'args.gn') |
| 698 | assert os.path.exists(build_args_path), ('"%s" is not a build directory, ' |
| 699 | 'missing args.gn file.' % BUILD_DIR) |
| 700 | with open(build_args_path) as build_args_file: |
| 701 | build_args_lines = build_args_file.readlines() |
| 702 | |
| 703 | build_args = {} |
| 704 | for build_arg_line in build_args_lines: |
| 705 | build_arg_without_comments = build_arg_line.split('#')[0] |
| 706 | key_value_pair = build_arg_without_comments.split('=') |
| 707 | if len(key_value_pair) != 2: |
| 708 | continue |
| 709 | |
| 710 | key = key_value_pair[0].strip() |
| 711 | value = key_value_pair[1].strip() |
| 712 | build_args[key] = value |
| 713 | |
| 714 | return build_args |
| 715 | |
| 716 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 717 | def _VerifyPathsAndReturnAbsolutes(paths): |
| 718 | """Verifies that the paths specified in |paths| exist and returns absolute |
| 719 | versions. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 720 | |
| 721 | Args: |
| 722 | paths: A list of files or directories. |
| 723 | """ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 724 | absolute_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 725 | for path in paths: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 726 | absolute_path = os.path.join(SRC_ROOT_PATH, path) |
| 727 | assert os.path.exists(absolute_path), ('Path: "%s" doesn\'t exist.' % path) |
| 728 | |
| 729 | absolute_paths.append(absolute_path) |
| 730 | |
| 731 | return absolute_paths |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 732 | |
| 733 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 734 | def _ParseCommandArguments(): |
| 735 | """Adds and parses relevant arguments for tool comands. |
| 736 | |
| 737 | Returns: |
| 738 | A dictionary representing the arguments. |
| 739 | """ |
| 740 | arg_parser = argparse.ArgumentParser() |
| 741 | arg_parser.usage = __doc__ |
| 742 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 743 | arg_parser.add_argument( |
| 744 | '-b', |
| 745 | '--build-dir', |
| 746 | type=str, |
| 747 | required=True, |
| 748 | help='The build directory, the path needs to be relative to the root of ' |
| 749 | 'the checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 750 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 751 | arg_parser.add_argument( |
| 752 | '-o', |
| 753 | '--output-dir', |
| 754 | type=str, |
| 755 | required=True, |
| 756 | help='Output directory for generated artifacts.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 757 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 758 | arg_parser.add_argument( |
| 759 | '-c', |
| 760 | '--command', |
| 761 | action='append', |
| 762 | required=True, |
| 763 | help='Commands used to run test targets, one test target needs one and ' |
| 764 | 'only one command, when specifying commands, one should assume the ' |
| 765 | 'current working directory is the root of the checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 766 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 767 | arg_parser.add_argument( |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 768 | '-f', |
| 769 | '--filters', |
| 770 | action='append', |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 771 | required=False, |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 772 | help='Directories or files to get code coverage for, and all files under ' |
| 773 | 'the directories are included recursively.') |
| 774 | |
| 775 | arg_parser.add_argument( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 776 | '-j', |
| 777 | '--jobs', |
| 778 | type=int, |
| 779 | default=None, |
| 780 | help='Run N jobs to build in parallel. If not specified, a default value ' |
| 781 | 'will be derived based on CPUs availability. Please refer to ' |
| 782 | '\'ninja -h\' for more details.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 783 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 784 | arg_parser.add_argument( |
| 785 | 'targets', nargs='+', help='The names of the test targets to run.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 786 | |
| 787 | args = arg_parser.parse_args() |
| 788 | return args |
| 789 | |
| 790 | |
| 791 | def Main(): |
| 792 | """Execute tool commands.""" |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 793 | assert _GetPlatform() in ['linux', 'mac'], ( |
| 794 | 'Coverage is only supported on linux and mac platforms.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 795 | assert os.path.abspath(os.getcwd()) == SRC_ROOT_PATH, ('This script must be ' |
| 796 | 'called from the root ' |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 797 | 'of checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 798 | DownloadCoverageToolsIfNeeded() |
| 799 | |
| 800 | args = _ParseCommandArguments() |
| 801 | global BUILD_DIR |
| 802 | BUILD_DIR = args.build_dir |
| 803 | global OUTPUT_DIR |
| 804 | OUTPUT_DIR = args.output_dir |
| 805 | |
| 806 | assert len(args.targets) == len(args.command), ('Number of targets must be ' |
| 807 | 'equal to the number of test ' |
| 808 | 'commands.') |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 809 | assert os.path.exists(BUILD_DIR), ( |
| 810 | 'Build directory: {} doesn\'t exist. ' |
| 811 | 'Please run "gn gen" to generate.').format(BUILD_DIR) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 812 | _ValidateBuildingWithClangCoverage() |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 813 | _VerifyTargetExecutablesAreInBuildDirectory(args.command) |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 814 | |
| 815 | absolute_filter_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 816 | if args.filters: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 817 | absolute_filter_paths = _VerifyPathsAndReturnAbsolutes(args.filters) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 818 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 819 | if not os.path.exists(OUTPUT_DIR): |
| 820 | os.makedirs(OUTPUT_DIR) |
| 821 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 822 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 823 | args.targets, args.command, args.jobs) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 824 | binary_paths = [_GetBinaryPath(command) for command in args.command] |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 825 | |
| 826 | print('Generating code coverage report in html (this can take a while ' |
| 827 | 'depending on size of target!)') |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 828 | _GenerateLineByLineFileCoverageInHtml(binary_paths, profdata_file_path, |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 829 | absolute_filter_paths) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame^] | 830 | _GeneratePerDirectoryCoverageInHtml(binary_paths, profdata_file_path, |
| 831 | absolute_filter_paths) |
| 832 | |
| 833 | # The default index file is generated only for the list of source files, needs |
| 834 | # to overwrite it to display per directory code coverage breakdown. |
| 835 | _OverwriteHtmlReportsIndexFile() |
| 836 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 837 | html_index_file_path = 'file://' + os.path.abspath( |
| 838 | os.path.join(OUTPUT_DIR, 'index.html')) |
| 839 | print('\nCode coverage profile data is created as: %s' % profdata_file_path) |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 840 | print('Index file for html report is generated as: %s' % html_index_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 841 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 842 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 843 | if __name__ == '__main__': |
| 844 | sys.exit(Main()) |