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 |
Yuke Liao | ab9c44e | 2018-02-21 00:24:40 | [diff] [blame] | 11 | "use_clang_coverage=true" and "is_component_build=false" GN flags to args.gn |
| 12 | file in your build output directory (e.g. out/coverage). |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 13 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 14 | Example usage: |
| 15 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 16 | gn gen out/coverage --args='use_clang_coverage=true is_component_build=false' |
| 17 | gclient runhooks |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 18 | python tools/code_coverage/coverage.py crypto_unittests url_unittests \\ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 19 | -b out/coverage -o out/report -c 'out/coverage/crypto_unittests' \\ |
| 20 | -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \\ |
| 21 | -f url/ -f crypto/ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 22 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 23 | The command above builds crypto_unittests and url_unittests targets and then |
| 24 | runs them with specified command line arguments. For url_unittests, it only |
| 25 | runs the test URLParser.PathURL. The coverage report is filtered to include |
| 26 | only files and sub-directories under url/ and crypto/ directories. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 27 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 28 | If you want to run tests that try to draw to the screen but don't have a |
| 29 | display connected, you can run tests in headless mode with xvfb. |
| 30 | |
| 31 | Sample flow for running a test target with xvfb (e.g. unit_tests): |
| 32 | |
| 33 | python tools/code_coverage/coverage.py unit_tests -b out/coverage \\ |
| 34 | -o out/report -c 'python testing/xvfb.py out/coverage/unit_tests' |
| 35 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 36 | If you are building a fuzz target, you need to add "use_libfuzzer=true" GN |
| 37 | flag as well. |
| 38 | |
| 39 | Sample workflow for a fuzz target (e.g. pdfium_fuzzer): |
| 40 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 41 | python tools/code_coverage/coverage.py pdfium_fuzzer \\ |
| 42 | -b out/coverage -o out/report \\ |
| 43 | -c 'out/coverage/pdfium_fuzzer -runs=<runs> <corpus_dir>' \\ |
| 44 | -f third_party/pdfium |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 45 | |
| 46 | where: |
| 47 | <corpus_dir> - directory containing samples files for this format. |
| 48 | <runs> - number of times to fuzz target function. Should be 0 when you just |
| 49 | want to see the coverage on corpus and don't want to fuzz at all. |
| 50 | |
| 51 | For more options, please refer to tools/code_coverage/coverage.py -h. |
Yuke Liao | 8e209fe8 | 2018-04-18 20:36:38 | [diff] [blame] | 52 | |
| 53 | For an overview of how code coverage works in Chromium, please refer to |
| 54 | https://chromium.googlesource.com/chromium/src/+/master/docs/code_coverage.md |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 55 | """ |
| 56 | |
| 57 | from __future__ import print_function |
| 58 | |
| 59 | import sys |
| 60 | |
| 61 | import argparse |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 62 | import json |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 63 | import logging |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 64 | import os |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 65 | import re |
| 66 | import shlex |
Max Moroz | 025d895 | 2018-05-03 16:33:34 | [diff] [blame] | 67 | import shutil |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 68 | import subprocess |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 69 | import urllib2 |
| 70 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 71 | sys.path.append( |
| 72 | os.path.join( |
| 73 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'tools', |
| 74 | 'clang', 'scripts')) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 75 | import update as clang_update |
| 76 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 77 | sys.path.append( |
| 78 | os.path.join( |
| 79 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, |
| 80 | 'third_party')) |
| 81 | import jinja2 |
| 82 | from collections import defaultdict |
| 83 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 84 | # Absolute path to the root of the checkout. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 85 | SRC_ROOT_PATH = os.path.abspath( |
| 86 | os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 87 | |
| 88 | # Absolute path to the code coverage tools binary. |
| 89 | LLVM_BUILD_DIR = clang_update.LLVM_BUILD_DIR |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 90 | LLVM_BIN_DIR = os.path.join(LLVM_BUILD_DIR, 'bin') |
| 91 | LLVM_COV_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-cov') |
| 92 | LLVM_PROFDATA_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-profdata') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 93 | |
| 94 | # Build directory, the value is parsed from command line arguments. |
| 95 | BUILD_DIR = None |
| 96 | |
| 97 | # Output directory for generated artifacts, the value is parsed from command |
| 98 | # line arguemnts. |
| 99 | OUTPUT_DIR = None |
| 100 | |
| 101 | # Default number of jobs used to build when goma is configured and enabled. |
| 102 | DEFAULT_GOMA_JOBS = 100 |
| 103 | |
| 104 | # Name of the file extension for profraw data files. |
| 105 | PROFRAW_FILE_EXTENSION = 'profraw' |
| 106 | |
| 107 | # Name of the final profdata file, and this file needs to be passed to |
| 108 | # "llvm-cov" command in order to call "llvm-cov show" to inspect the |
| 109 | # line-by-line coverage of specific files. |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 110 | PROFDATA_FILE_NAME = os.extsep.join(['coverage', 'profdata']) |
| 111 | |
| 112 | # Name of the file with summary information generated by llvm-cov export. |
| 113 | SUMMARY_FILE_NAME = os.extsep.join(['summary', 'json']) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 114 | |
| 115 | # Build arg required for generating code coverage data. |
| 116 | CLANG_COVERAGE_BUILD_ARG = 'use_clang_coverage' |
| 117 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 118 | # The default name of the html coverage report for a directory. |
| 119 | DIRECTORY_COVERAGE_HTML_REPORT_NAME = os.extsep.join(['report', 'html']) |
| 120 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 121 | # Name of the html index files for different views. |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 122 | COMPONENT_VIEW_INDEX_FILE = os.extsep.join(['component_view_index', 'html']) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 123 | DIRECTORY_VIEW_INDEX_FILE = os.extsep.join(['directory_view_index', 'html']) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 124 | FILE_VIEW_INDEX_FILE = os.extsep.join(['file_view_index', 'html']) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 125 | INDEX_HTML_FILE = os.extsep.join(['index', 'html']) |
| 126 | |
| 127 | LOGS_DIR_NAME = 'logs' |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 128 | |
| 129 | # Used to extract a mapping between directories and components. |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 130 | COMPONENT_MAPPING_URL = ( |
| 131 | 'https://storage.googleapis.com/chromium-owners/component_map.json') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 132 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 133 | # Caches the results returned by _GetBuildArgs, don't use this variable |
| 134 | # directly, call _GetBuildArgs instead. |
| 135 | _BUILD_ARGS = None |
| 136 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 137 | # Retry failed merges. |
| 138 | MERGE_RETRIES = 3 |
| 139 | |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 140 | # Message to guide user to file a bug when everything else fails. |
| 141 | FILE_BUG_MESSAGE = ( |
| 142 | 'If it persists, please file a bug with the command you used, git revision ' |
| 143 | 'and args.gn config here: ' |
| 144 | 'https://bugs.chromium.org/p/chromium/issues/entry?' |
| 145 | 'components=Tools%3ECodeCoverage') |
| 146 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 147 | |
| 148 | class _CoverageSummary(object): |
| 149 | """Encapsulates coverage summary representation.""" |
| 150 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 151 | def __init__(self, |
| 152 | regions_total=0, |
| 153 | regions_covered=0, |
| 154 | functions_total=0, |
| 155 | functions_covered=0, |
| 156 | lines_total=0, |
| 157 | lines_covered=0): |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 158 | """Initializes _CoverageSummary object.""" |
| 159 | self._summary = { |
| 160 | 'regions': { |
| 161 | 'total': regions_total, |
| 162 | 'covered': regions_covered |
| 163 | }, |
| 164 | 'functions': { |
| 165 | 'total': functions_total, |
| 166 | 'covered': functions_covered |
| 167 | }, |
| 168 | 'lines': { |
| 169 | 'total': lines_total, |
| 170 | 'covered': lines_covered |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | def Get(self): |
| 175 | """Returns summary as a dictionary.""" |
| 176 | return self._summary |
| 177 | |
| 178 | def AddSummary(self, other_summary): |
| 179 | """Adds another summary to this one element-wise.""" |
| 180 | for feature in self._summary: |
| 181 | self._summary[feature]['total'] += other_summary.Get()[feature]['total'] |
| 182 | self._summary[feature]['covered'] += other_summary.Get()[feature][ |
| 183 | 'covered'] |
| 184 | |
| 185 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 186 | class _CoverageReportHtmlGenerator(object): |
| 187 | """Encapsulates coverage html report generation. |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 188 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 189 | The generated html has a table that contains links to other coverage reports. |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 190 | """ |
| 191 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 192 | def __init__(self, output_path, table_entry_type): |
| 193 | """Initializes _CoverageReportHtmlGenerator object. |
| 194 | |
| 195 | Args: |
| 196 | output_path: Path to the html report that will be generated. |
| 197 | table_entry_type: Type of the table entries to be displayed in the table |
| 198 | header. For example: 'Path', 'Component'. |
| 199 | """ |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 200 | css_file_name = os.extsep.join(['style', 'css']) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 201 | css_absolute_path = os.path.join(OUTPUT_DIR, css_file_name) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 202 | assert os.path.exists(css_absolute_path), ( |
| 203 | 'css file doesn\'t exit. Please make sure "llvm-cov show -format=html" ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 204 | 'is called first, and the css file is generated at: "%s".' % |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 205 | css_absolute_path) |
| 206 | |
| 207 | self._css_absolute_path = css_absolute_path |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 208 | self._output_path = output_path |
| 209 | self._table_entry_type = table_entry_type |
| 210 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 211 | self._table_entries = [] |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 212 | self._total_entry = {} |
Abhishek Arya | 302b67a | 2018-05-10 19:43:23 | [diff] [blame] | 213 | |
| 214 | source_dir = os.path.dirname(os.path.realpath(__file__)) |
| 215 | template_dir = os.path.join(source_dir, 'html_templates') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 216 | |
| 217 | jinja_env = jinja2.Environment( |
| 218 | loader=jinja2.FileSystemLoader(template_dir), trim_blocks=True) |
| 219 | self._header_template = jinja_env.get_template('header.html') |
| 220 | self._table_template = jinja_env.get_template('table.html') |
| 221 | self._footer_template = jinja_env.get_template('footer.html') |
Abhishek Arya | 302b67a | 2018-05-10 19:43:23 | [diff] [blame] | 222 | |
Abhishek Arya | 865fffd | 2018-05-08 22:16:01 | [diff] [blame] | 223 | self._style_overrides = open( |
Abhishek Arya | 302b67a | 2018-05-10 19:43:23 | [diff] [blame] | 224 | os.path.join(source_dir, 'static', 'css', 'style.css')).read() |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 225 | |
| 226 | def AddLinkToAnotherReport(self, html_report_path, name, summary): |
| 227 | """Adds a link to another html report in this report. |
| 228 | |
| 229 | The link to be added is assumed to be an entry in this directory. |
| 230 | """ |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 231 | # Use relative paths instead of absolute paths to make the generated reports |
| 232 | # portable. |
| 233 | html_report_relative_path = _GetRelativePathToDirectoryOfFile( |
| 234 | html_report_path, self._output_path) |
| 235 | |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 236 | table_entry = self._CreateTableEntryFromCoverageSummary( |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 237 | summary, html_report_relative_path, name, |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 238 | os.path.basename(html_report_path) == |
| 239 | DIRECTORY_COVERAGE_HTML_REPORT_NAME) |
| 240 | self._table_entries.append(table_entry) |
| 241 | |
| 242 | def CreateTotalsEntry(self, summary): |
Yuke Liao | a785f4d3 | 2018-02-13 21:41:35 | [diff] [blame] | 243 | """Creates an entry corresponds to the 'Totals' row in the html report.""" |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 244 | self._total_entry = self._CreateTableEntryFromCoverageSummary(summary) |
| 245 | |
| 246 | def _CreateTableEntryFromCoverageSummary(self, |
| 247 | summary, |
| 248 | href=None, |
| 249 | name=None, |
| 250 | is_dir=None): |
| 251 | """Creates an entry to display in the html report.""" |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 252 | assert (href is None and name is None and is_dir is None) or ( |
| 253 | href is not None and name is not None and is_dir is not None), ( |
| 254 | 'The only scenario when href or name or is_dir can be None is when ' |
Yuke Liao | a785f4d3 | 2018-02-13 21:41:35 | [diff] [blame] | 255 | 'creating an entry for the Totals row, and in that case, all three ' |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 256 | 'attributes must be None.') |
| 257 | |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 258 | entry = {} |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 259 | if href is not None: |
| 260 | entry['href'] = href |
| 261 | if name is not None: |
| 262 | entry['name'] = name |
| 263 | if is_dir is not None: |
| 264 | entry['is_dir'] = is_dir |
| 265 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 266 | summary_dict = summary.Get() |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 267 | for feature in summary_dict: |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 268 | if summary_dict[feature]['total'] == 0: |
| 269 | percentage = 0.0 |
| 270 | else: |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 271 | percentage = float(summary_dict[feature] |
| 272 | ['covered']) / summary_dict[feature]['total'] * 100 |
Yuke Liao | a785f4d3 | 2018-02-13 21:41:35 | [diff] [blame] | 273 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 274 | color_class = self._GetColorClass(percentage) |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 275 | entry[feature] = { |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 276 | 'total': summary_dict[feature]['total'], |
| 277 | 'covered': summary_dict[feature]['covered'], |
Yuke Liao | a785f4d3 | 2018-02-13 21:41:35 | [diff] [blame] | 278 | 'percentage': '{:6.2f}'.format(percentage), |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 279 | 'color_class': color_class |
| 280 | } |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 281 | |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 282 | return entry |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 283 | |
| 284 | def _GetColorClass(self, percentage): |
| 285 | """Returns the css color class based on coverage percentage.""" |
| 286 | if percentage >= 0 and percentage < 80: |
| 287 | return 'red' |
| 288 | if percentage >= 80 and percentage < 100: |
| 289 | return 'yellow' |
| 290 | if percentage == 100: |
| 291 | return 'green' |
| 292 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 293 | assert False, 'Invalid coverage percentage: "%d".' % percentage |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 294 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 295 | def WriteHtmlCoverageReport(self): |
| 296 | """Writes html coverage report. |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 297 | |
| 298 | In the report, sub-directories are displayed before files and within each |
| 299 | category, entries are sorted alphabetically. |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 300 | """ |
| 301 | |
| 302 | def EntryCmp(left, right): |
| 303 | """Compare function for table entries.""" |
| 304 | if left['is_dir'] != right['is_dir']: |
| 305 | return -1 if left['is_dir'] == True else 1 |
| 306 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 307 | return -1 if left['name'] < right['name'] else 1 |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 308 | |
| 309 | self._table_entries = sorted(self._table_entries, cmp=EntryCmp) |
| 310 | |
| 311 | css_path = os.path.join(OUTPUT_DIR, os.extsep.join(['style', 'css'])) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 312 | |
| 313 | directory_view_path = _GetDirectoryViewPath() |
| 314 | component_view_path = _GetComponentViewPath() |
| 315 | file_view_path = _GetFileViewPath() |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 316 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 317 | html_header = self._header_template.render( |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 318 | css_path=_GetRelativePathToDirectoryOfFile(css_path, self._output_path), |
| 319 | directory_view_href=_GetRelativePathToDirectoryOfFile( |
| 320 | directory_view_path, self._output_path), |
| 321 | component_view_href=_GetRelativePathToDirectoryOfFile( |
| 322 | component_view_path, self._output_path), |
| 323 | file_view_href=_GetRelativePathToDirectoryOfFile( |
Abhishek Arya | 865fffd | 2018-05-08 22:16:01 | [diff] [blame] | 324 | file_view_path, self._output_path), |
| 325 | style_overrides=self._style_overrides) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 326 | |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 327 | html_table = self._table_template.render( |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 328 | entries=self._table_entries, |
| 329 | total_entry=self._total_entry, |
| 330 | table_entry_type=self._table_entry_type) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 331 | html_footer = self._footer_template.render() |
| 332 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 333 | with open(self._output_path, 'w') as html_file: |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 334 | html_file.write(html_header + html_table + html_footer) |
| 335 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 336 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 337 | def _ConfigureLogging(args): |
| 338 | """Configures logging settings for later use.""" |
| 339 | log_level = logging.DEBUG if args.verbose else logging.INFO |
| 340 | log_format = '[%(asctime)s %(levelname)s] %(message)s' |
| 341 | log_file = args.log_file if args.log_file else None |
| 342 | logging.basicConfig(filename=log_file, level=log_level, format=log_format) |
| 343 | |
| 344 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 345 | def _GetSharedLibraries(binary_paths): |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 346 | """Returns list of shared libraries used by specified binaries.""" |
| 347 | logging.info('Finding shared libraries for targets (if any).') |
| 348 | shared_libraries = [] |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 349 | cmd = [] |
| 350 | shared_library_re = None |
| 351 | |
| 352 | if sys.platform.startswith('linux'): |
| 353 | cmd.extend(['ldd']) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 354 | shared_library_re = re.compile(r'.*\.so\s=>\s(.*' + BUILD_DIR + |
| 355 | r'.*\.so)\s.*') |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 356 | elif sys.platform.startswith('darwin'): |
| 357 | cmd.extend(['otool', '-L']) |
| 358 | shared_library_re = re.compile(r'\s+(@rpath/.*\.dylib)\s.*') |
| 359 | else: |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 360 | assert False, 'Cannot detect shared libraries used by the given targets.' |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 361 | |
| 362 | assert shared_library_re is not None |
| 363 | |
| 364 | cmd.extend(binary_paths) |
| 365 | output = subprocess.check_output(cmd) |
| 366 | |
| 367 | for line in output.splitlines(): |
| 368 | m = shared_library_re.match(line) |
| 369 | if not m: |
| 370 | continue |
| 371 | |
| 372 | shared_library_path = m.group(1) |
| 373 | if sys.platform.startswith('darwin'): |
| 374 | # otool outputs "@rpath" macro instead of the dirname of the given binary. |
| 375 | shared_library_path = shared_library_path.replace('@rpath', BUILD_DIR) |
| 376 | |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 377 | if shared_library_path in shared_libraries: |
| 378 | continue |
| 379 | |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 380 | assert os.path.exists(shared_library_path), ('Shared library "%s" used by ' |
| 381 | 'the given target(s) does not ' |
| 382 | 'exist.' % shared_library_path) |
| 383 | with open(shared_library_path) as f: |
| 384 | data = f.read() |
| 385 | |
| 386 | # Do not add non-instrumented libraries. Otherwise, llvm-cov errors outs. |
| 387 | if '__llvm_cov' in data: |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 388 | shared_libraries.append(shared_library_path) |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 389 | |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 390 | logging.debug('Found shared libraries (%d): %s.', len(shared_libraries), |
| 391 | shared_libraries) |
| 392 | logging.info('Finished finding shared libraries for targets.') |
| 393 | return shared_libraries |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 394 | |
| 395 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 396 | def _GetHostPlatform(): |
| 397 | """Returns the host platform. |
| 398 | |
| 399 | This is separate from the target platform/os that coverage is running for. |
| 400 | """ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 401 | if sys.platform == 'win32' or sys.platform == 'cygwin': |
| 402 | return 'win' |
| 403 | if sys.platform.startswith('linux'): |
| 404 | return 'linux' |
| 405 | else: |
| 406 | assert sys.platform == 'darwin' |
| 407 | return 'mac' |
| 408 | |
| 409 | |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 410 | def _GetPathWithLLVMSymbolizerDir(): |
| 411 | """Add llvm-symbolizer directory to path for symbolized stacks.""" |
| 412 | path = os.getenv('PATH') |
| 413 | dirs = path.split(os.pathsep) |
| 414 | if LLVM_BIN_DIR in dirs: |
| 415 | return path |
| 416 | |
| 417 | return path + os.pathsep + LLVM_BIN_DIR |
| 418 | |
| 419 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 420 | def _GetTargetOS(): |
| 421 | """Returns the target os specified in args.gn file. |
| 422 | |
| 423 | Returns an empty string is target_os is not specified. |
| 424 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 425 | build_args = _GetBuildArgs() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 426 | return build_args['target_os'] if 'target_os' in build_args else '' |
| 427 | |
| 428 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 429 | def _IsIOS(): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 430 | """Returns true if the target_os specified in args.gn file is ios""" |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 431 | return _GetTargetOS() == 'ios' |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 432 | |
| 433 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 434 | # TODO(crbug.com/759794): remove this function once tools get included to |
| 435 | # Clang bundle: |
| 436 | # https://chromium-review.googlesource.com/c/chromium/src/+/688221 |
| 437 | def DownloadCoverageToolsIfNeeded(): |
| 438 | """Temporary solution to download llvm-profdata and llvm-cov tools.""" |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 439 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 440 | def _GetRevisionFromStampFile(stamp_file_path): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 441 | """Returns a pair of revision number by reading the build stamp file. |
| 442 | |
| 443 | Args: |
| 444 | stamp_file_path: A path the build stamp file created by |
| 445 | tools/clang/scripts/update.py. |
| 446 | Returns: |
| 447 | A pair of integers represeting the main and sub revision respectively. |
| 448 | """ |
| 449 | if not os.path.exists(stamp_file_path): |
| 450 | return 0, 0 |
| 451 | |
| 452 | with open(stamp_file_path) as stamp_file: |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 453 | stamp_file_line = stamp_file.readline() |
| 454 | if ',' in stamp_file_line: |
| 455 | package_version = stamp_file_line.rstrip().split(',')[0] |
| 456 | else: |
| 457 | package_version = stamp_file_line.rstrip() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 458 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 459 | clang_revision_str, clang_sub_revision_str = package_version.split('-') |
| 460 | return int(clang_revision_str), int(clang_sub_revision_str) |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 461 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 462 | host_platform = _GetHostPlatform() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 463 | clang_revision, clang_sub_revision = _GetRevisionFromStampFile( |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 464 | clang_update.STAMP_FILE) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 465 | |
| 466 | coverage_revision_stamp_file = os.path.join( |
| 467 | os.path.dirname(clang_update.STAMP_FILE), 'cr_coverage_revision') |
| 468 | coverage_revision, coverage_sub_revision = _GetRevisionFromStampFile( |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 469 | coverage_revision_stamp_file) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 470 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 471 | has_coverage_tools = ( |
| 472 | os.path.exists(LLVM_COV_PATH) and os.path.exists(LLVM_PROFDATA_PATH)) |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 473 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 474 | if (has_coverage_tools and coverage_revision == clang_revision and |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 475 | coverage_sub_revision == clang_sub_revision): |
| 476 | # LLVM coverage tools are up to date, bail out. |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 477 | return |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 478 | |
| 479 | package_version = '%d-%d' % (clang_revision, clang_sub_revision) |
| 480 | coverage_tools_file = 'llvm-code-coverage-%s.tgz' % package_version |
| 481 | |
| 482 | # The code bellow follows the code from tools/clang/scripts/update.py. |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 483 | if host_platform == 'mac': |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 484 | coverage_tools_url = clang_update.CDS_URL + '/Mac/' + coverage_tools_file |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 485 | elif host_platform == 'linux': |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 486 | coverage_tools_url = ( |
| 487 | clang_update.CDS_URL + '/Linux_x64/' + coverage_tools_file) |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 488 | else: |
| 489 | assert host_platform == 'win' |
| 490 | coverage_tools_url = (clang_update.CDS_URL + '/Win/' + coverage_tools_file) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 491 | |
| 492 | try: |
| 493 | clang_update.DownloadAndUnpack(coverage_tools_url, |
| 494 | clang_update.LLVM_BUILD_DIR) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 495 | with open(coverage_revision_stamp_file, 'w') as file_handle: |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 496 | file_handle.write('%s,%s' % (package_version, host_platform)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 497 | file_handle.write('\n') |
| 498 | except urllib2.URLError: |
| 499 | raise Exception( |
| 500 | 'Failed to download coverage tools: %s.' % coverage_tools_url) |
| 501 | |
| 502 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 503 | def _GeneratePerFileLineByLineCoverageInHtml(binary_paths, profdata_file_path, |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 504 | filters, ignore_filename_regex): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 505 | """Generates per file line-by-line coverage in html using 'llvm-cov show'. |
| 506 | |
| 507 | For a file with absolute path /a/b/x.cc, a html report is generated as: |
| 508 | OUTPUT_DIR/coverage/a/b/x.cc.html. An index html file is also generated as: |
| 509 | OUTPUT_DIR/index.html. |
| 510 | |
| 511 | Args: |
| 512 | binary_paths: A list of paths to the instrumented binaries. |
| 513 | profdata_file_path: A path to the profdata file. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 514 | filters: A list of directories and files to get coverage for. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 515 | """ |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 516 | # llvm-cov show [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 517 | # [[-object BIN]] [SOURCES] |
| 518 | # NOTE: For object files, the first one is specified as a positional argument, |
| 519 | # and the rest are specified as keyword argument. |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 520 | logging.debug('Generating per file line by line coverage reports using ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 521 | '"llvm-cov show" command.') |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 522 | subprocess_cmd = [ |
| 523 | LLVM_COV_PATH, 'show', '-format=html', |
| 524 | '-output-dir={}'.format(OUTPUT_DIR), |
| 525 | '-instr-profile={}'.format(profdata_file_path), binary_paths[0] |
| 526 | ] |
| 527 | subprocess_cmd.extend( |
| 528 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 529 | _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths)) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 530 | subprocess_cmd.extend(filters) |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 531 | if ignore_filename_regex: |
| 532 | subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex) |
| 533 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 534 | subprocess.check_call(subprocess_cmd) |
Max Moroz | 025d895 | 2018-05-03 16:33:34 | [diff] [blame] | 535 | |
| 536 | # llvm-cov creates "coverage" subdir in the output dir. We would like to use |
| 537 | # the platform name instead, as it simplifies the report dir structure when |
| 538 | # the same report is generated for different platforms. |
| 539 | default_report_subdir_path = os.path.join(OUTPUT_DIR, 'coverage') |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 540 | platform_report_subdir_path = _GetCoverageReportRootDirPath() |
| 541 | _MergeTwoDirectories(default_report_subdir_path, platform_report_subdir_path) |
Max Moroz | 025d895 | 2018-05-03 16:33:34 | [diff] [blame] | 542 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 543 | logging.debug('Finished running "llvm-cov show" command.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 544 | |
| 545 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 546 | def _GenerateFileViewHtmlIndexFile(per_file_coverage_summary): |
| 547 | """Generates html index file for file view.""" |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 548 | file_view_index_file_path = _GetFileViewPath() |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 549 | logging.debug('Generating file view html index file as: "%s".', |
| 550 | file_view_index_file_path) |
| 551 | html_generator = _CoverageReportHtmlGenerator(file_view_index_file_path, |
| 552 | 'Path') |
| 553 | totals_coverage_summary = _CoverageSummary() |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 554 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 555 | for file_path in per_file_coverage_summary: |
| 556 | totals_coverage_summary.AddSummary(per_file_coverage_summary[file_path]) |
| 557 | |
| 558 | html_generator.AddLinkToAnotherReport( |
| 559 | _GetCoverageHtmlReportPathForFile(file_path), |
| 560 | os.path.relpath(file_path, SRC_ROOT_PATH), |
| 561 | per_file_coverage_summary[file_path]) |
| 562 | |
| 563 | html_generator.CreateTotalsEntry(totals_coverage_summary) |
| 564 | html_generator.WriteHtmlCoverageReport() |
| 565 | logging.debug('Finished generating file view html index file.') |
| 566 | |
| 567 | |
| 568 | def _CalculatePerDirectoryCoverageSummary(per_file_coverage_summary): |
| 569 | """Calculates per directory coverage summary.""" |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 570 | logging.debug('Calculating per-directory coverage summary.') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 571 | per_directory_coverage_summary = defaultdict(lambda: _CoverageSummary()) |
| 572 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 573 | for file_path in per_file_coverage_summary: |
| 574 | summary = per_file_coverage_summary[file_path] |
| 575 | parent_dir = os.path.dirname(file_path) |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 576 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 577 | while True: |
| 578 | per_directory_coverage_summary[parent_dir].AddSummary(summary) |
| 579 | |
| 580 | if parent_dir == SRC_ROOT_PATH: |
| 581 | break |
| 582 | parent_dir = os.path.dirname(parent_dir) |
| 583 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 584 | logging.debug('Finished calculating per-directory coverage summary.') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 585 | return per_directory_coverage_summary |
| 586 | |
| 587 | |
| 588 | def _GeneratePerDirectoryCoverageInHtml(per_directory_coverage_summary, |
| 589 | per_file_coverage_summary): |
| 590 | """Generates per directory coverage breakdown in html.""" |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 591 | logging.debug('Writing per-directory coverage html reports.') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 592 | for dir_path in per_directory_coverage_summary: |
| 593 | _GenerateCoverageInHtmlForDirectory( |
| 594 | dir_path, per_directory_coverage_summary, per_file_coverage_summary) |
| 595 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 596 | logging.debug('Finished writing per-directory coverage html reports.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 597 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 598 | |
| 599 | def _GenerateCoverageInHtmlForDirectory( |
| 600 | dir_path, per_directory_coverage_summary, per_file_coverage_summary): |
| 601 | """Generates coverage html report for a single directory.""" |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 602 | html_generator = _CoverageReportHtmlGenerator( |
| 603 | _GetCoverageHtmlReportPathForDirectory(dir_path), 'Path') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 604 | |
| 605 | for entry_name in os.listdir(dir_path): |
| 606 | entry_path = os.path.normpath(os.path.join(dir_path, entry_name)) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 607 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 608 | if entry_path in per_file_coverage_summary: |
| 609 | entry_html_report_path = _GetCoverageHtmlReportPathForFile(entry_path) |
| 610 | entry_coverage_summary = per_file_coverage_summary[entry_path] |
| 611 | elif entry_path in per_directory_coverage_summary: |
| 612 | entry_html_report_path = _GetCoverageHtmlReportPathForDirectory( |
| 613 | entry_path) |
| 614 | entry_coverage_summary = per_directory_coverage_summary[entry_path] |
| 615 | else: |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 616 | # Any file without executable lines shouldn't be included into the report. |
| 617 | # For example, OWNER and README.md files. |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 618 | continue |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 619 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 620 | html_generator.AddLinkToAnotherReport(entry_html_report_path, |
| 621 | os.path.basename(entry_path), |
| 622 | entry_coverage_summary) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 623 | |
Yuke Liao | d54030e | 2018-01-08 17:34:12 | [diff] [blame] | 624 | html_generator.CreateTotalsEntry(per_directory_coverage_summary[dir_path]) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 625 | html_generator.WriteHtmlCoverageReport() |
| 626 | |
| 627 | |
| 628 | def _GenerateDirectoryViewHtmlIndexFile(): |
| 629 | """Generates the html index file for directory view. |
| 630 | |
| 631 | Note that the index file is already generated under SRC_ROOT_PATH, so this |
| 632 | file simply redirects to it, and the reason of this extra layer is for |
| 633 | structural consistency with other views. |
| 634 | """ |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 635 | directory_view_index_file_path = _GetDirectoryViewPath() |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 636 | logging.debug('Generating directory view html index file as: "%s".', |
| 637 | directory_view_index_file_path) |
| 638 | src_root_html_report_path = _GetCoverageHtmlReportPathForDirectory( |
| 639 | SRC_ROOT_PATH) |
| 640 | _WriteRedirectHtmlFile(directory_view_index_file_path, |
| 641 | src_root_html_report_path) |
| 642 | logging.debug('Finished generating directory view html index file.') |
| 643 | |
| 644 | |
| 645 | def _CalculatePerComponentCoverageSummary(component_to_directories, |
| 646 | per_directory_coverage_summary): |
| 647 | """Calculates per component coverage summary.""" |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 648 | logging.debug('Calculating per-component coverage summary.') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 649 | per_component_coverage_summary = defaultdict(lambda: _CoverageSummary()) |
| 650 | |
| 651 | for component in component_to_directories: |
| 652 | for directory in component_to_directories[component]: |
| 653 | absolute_directory_path = os.path.abspath(directory) |
| 654 | if absolute_directory_path in per_directory_coverage_summary: |
| 655 | per_component_coverage_summary[component].AddSummary( |
| 656 | per_directory_coverage_summary[absolute_directory_path]) |
| 657 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 658 | logging.debug('Finished calculating per-component coverage summary.') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 659 | return per_component_coverage_summary |
| 660 | |
| 661 | |
| 662 | def _ExtractComponentToDirectoriesMapping(): |
| 663 | """Returns a mapping from components to directories.""" |
| 664 | component_mappings = json.load(urllib2.urlopen(COMPONENT_MAPPING_URL)) |
| 665 | directory_to_component = component_mappings['dir-to-component'] |
| 666 | |
| 667 | component_to_directories = defaultdict(list) |
| 668 | for directory in directory_to_component: |
| 669 | component = directory_to_component[directory] |
| 670 | component_to_directories[component].append(directory) |
| 671 | |
| 672 | return component_to_directories |
| 673 | |
| 674 | |
| 675 | def _GeneratePerComponentCoverageInHtml(per_component_coverage_summary, |
| 676 | component_to_directories, |
| 677 | per_directory_coverage_summary): |
| 678 | """Generates per-component coverage reports in html.""" |
| 679 | logging.debug('Writing per-component coverage html reports.') |
| 680 | for component in per_component_coverage_summary: |
| 681 | _GenerateCoverageInHtmlForComponent( |
| 682 | component, per_component_coverage_summary, component_to_directories, |
| 683 | per_directory_coverage_summary) |
| 684 | |
| 685 | logging.debug('Finished writing per-component coverage html reports.') |
| 686 | |
| 687 | |
| 688 | def _GenerateCoverageInHtmlForComponent( |
| 689 | component_name, per_component_coverage_summary, component_to_directories, |
| 690 | per_directory_coverage_summary): |
| 691 | """Generates coverage html report for a component.""" |
| 692 | component_html_report_path = _GetCoverageHtmlReportPathForComponent( |
| 693 | component_name) |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 694 | component_html_report_dir = os.path.dirname(component_html_report_path) |
| 695 | if not os.path.exists(component_html_report_dir): |
| 696 | os.makedirs(component_html_report_dir) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 697 | |
| 698 | html_generator = _CoverageReportHtmlGenerator(component_html_report_path, |
| 699 | 'Path') |
| 700 | |
| 701 | for dir_path in component_to_directories[component_name]: |
| 702 | dir_absolute_path = os.path.abspath(dir_path) |
| 703 | if dir_absolute_path not in per_directory_coverage_summary: |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 704 | # Any directory without an excercised file shouldn't be included into the |
| 705 | # report. |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 706 | continue |
| 707 | |
| 708 | html_generator.AddLinkToAnotherReport( |
| 709 | _GetCoverageHtmlReportPathForDirectory(dir_path), |
| 710 | os.path.relpath(dir_path, SRC_ROOT_PATH), |
| 711 | per_directory_coverage_summary[dir_absolute_path]) |
| 712 | |
| 713 | html_generator.CreateTotalsEntry( |
| 714 | per_component_coverage_summary[component_name]) |
| 715 | html_generator.WriteHtmlCoverageReport() |
| 716 | |
| 717 | |
| 718 | def _GenerateComponentViewHtmlIndexFile(per_component_coverage_summary): |
| 719 | """Generates the html index file for component view.""" |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 720 | component_view_index_file_path = _GetComponentViewPath() |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 721 | logging.debug('Generating component view html index file as: "%s".', |
| 722 | component_view_index_file_path) |
| 723 | html_generator = _CoverageReportHtmlGenerator(component_view_index_file_path, |
| 724 | 'Component') |
| 725 | totals_coverage_summary = _CoverageSummary() |
| 726 | |
| 727 | for component in per_component_coverage_summary: |
| 728 | totals_coverage_summary.AddSummary( |
| 729 | per_component_coverage_summary[component]) |
| 730 | |
| 731 | html_generator.AddLinkToAnotherReport( |
| 732 | _GetCoverageHtmlReportPathForComponent(component), component, |
| 733 | per_component_coverage_summary[component]) |
| 734 | |
| 735 | html_generator.CreateTotalsEntry(totals_coverage_summary) |
| 736 | html_generator.WriteHtmlCoverageReport() |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 737 | logging.debug('Finished generating component view html index file.') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 738 | |
| 739 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 740 | def _MergeTwoDirectories(src_path, dst_path): |
| 741 | """Merge src_path directory into dst_path directory.""" |
| 742 | for filename in os.listdir(src_path): |
| 743 | dst_path = os.path.join(dst_path, filename) |
| 744 | if os.path.exists(dst_path): |
| 745 | shutil.rmtree(dst_path) |
| 746 | os.rename(os.path.join(src_path, filename), dst_path) |
| 747 | shutil.rmtree(src_path) |
| 748 | |
| 749 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 750 | def _OverwriteHtmlReportsIndexFile(): |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 751 | """Overwrites the root index file to redirect to the default view.""" |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 752 | html_index_file_path = _GetHtmlIndexPath() |
| 753 | directory_view_index_file_path = _GetDirectoryViewPath() |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 754 | _WriteRedirectHtmlFile(html_index_file_path, directory_view_index_file_path) |
| 755 | |
| 756 | |
| 757 | def _WriteRedirectHtmlFile(from_html_path, to_html_path): |
| 758 | """Writes a html file that redirects to another html file.""" |
| 759 | to_html_relative_path = _GetRelativePathToDirectoryOfFile( |
| 760 | to_html_path, from_html_path) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 761 | content = (""" |
| 762 | <!DOCTYPE html> |
| 763 | <html> |
| 764 | <head> |
| 765 | <!-- HTML meta refresh URL redirection --> |
| 766 | <meta http-equiv="refresh" content="0; url=%s"> |
| 767 | </head> |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 768 | </html>""" % to_html_relative_path) |
| 769 | with open(from_html_path, 'w') as f: |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 770 | f.write(content) |
| 771 | |
| 772 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 773 | def _CleanUpOutputDir(): |
| 774 | """Perform a cleanup of the output dir.""" |
| 775 | # Remove the default index.html file produced by llvm-cov. |
| 776 | index_path = os.path.join(OUTPUT_DIR, INDEX_HTML_FILE) |
| 777 | if os.path.exists(index_path): |
| 778 | os.remove(index_path) |
| 779 | |
| 780 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 781 | def _GetCoverageHtmlReportPathForFile(file_path): |
| 782 | """Given a file path, returns the corresponding html report path.""" |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 783 | assert os.path.isfile(file_path), '"%s" is not a file.' % file_path |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 784 | html_report_path = os.extsep.join([os.path.abspath(file_path), 'html']) |
| 785 | |
| 786 | # '+' is used instead of os.path.join because both of them are absolute paths |
| 787 | # and os.path.join ignores the first path. |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 788 | # TODO(crbug.com/809150): Think of a generic cross platform fix (Windows). |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 789 | return _GetCoverageReportRootDirPath() + html_report_path |
| 790 | |
| 791 | |
| 792 | def _GetCoverageHtmlReportPathForDirectory(dir_path): |
| 793 | """Given a directory path, returns the corresponding html report path.""" |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 794 | assert os.path.isdir(dir_path), '"%s" is not a directory.' % dir_path |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 795 | html_report_path = os.path.join( |
| 796 | os.path.abspath(dir_path), DIRECTORY_COVERAGE_HTML_REPORT_NAME) |
| 797 | |
| 798 | # '+' is used instead of os.path.join because both of them are absolute paths |
| 799 | # and os.path.join ignores the first path. |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 800 | # TODO(crbug.com/809150): Think of a generic cross platform fix (Windows). |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 801 | return _GetCoverageReportRootDirPath() + html_report_path |
| 802 | |
| 803 | |
| 804 | def _GetCoverageHtmlReportPathForComponent(component_name): |
| 805 | """Given a component, returns the corresponding html report path.""" |
| 806 | component_file_name = component_name.lower().replace('>', '-') |
| 807 | html_report_name = os.extsep.join([component_file_name, 'html']) |
| 808 | return os.path.join(_GetCoverageReportRootDirPath(), 'components', |
| 809 | html_report_name) |
| 810 | |
| 811 | |
| 812 | def _GetCoverageReportRootDirPath(): |
| 813 | """The root directory that contains all generated coverage html reports.""" |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 814 | return os.path.join(OUTPUT_DIR, _GetHostPlatform()) |
| 815 | |
| 816 | |
| 817 | def _GetComponentViewPath(): |
| 818 | """Path to the HTML file for the component view.""" |
| 819 | return os.path.join(_GetCoverageReportRootDirPath(), |
| 820 | COMPONENT_VIEW_INDEX_FILE) |
| 821 | |
| 822 | |
| 823 | def _GetDirectoryViewPath(): |
| 824 | """Path to the HTML file for the directory view.""" |
| 825 | return os.path.join(_GetCoverageReportRootDirPath(), |
| 826 | DIRECTORY_VIEW_INDEX_FILE) |
| 827 | |
| 828 | |
| 829 | def _GetFileViewPath(): |
| 830 | """Path to the HTML file for the file view.""" |
| 831 | return os.path.join(_GetCoverageReportRootDirPath(), FILE_VIEW_INDEX_FILE) |
| 832 | |
| 833 | |
| 834 | def _GetLogsDirectoryPath(): |
| 835 | """Path to the logs directory.""" |
| 836 | return os.path.join(_GetCoverageReportRootDirPath(), LOGS_DIR_NAME) |
| 837 | |
| 838 | |
| 839 | def _GetHtmlIndexPath(): |
| 840 | """Path to the main HTML index file.""" |
| 841 | return os.path.join(_GetCoverageReportRootDirPath(), INDEX_HTML_FILE) |
| 842 | |
| 843 | |
| 844 | def _GetProfdataFilePath(): |
| 845 | """Path to the resulting .profdata file.""" |
| 846 | return os.path.join(_GetCoverageReportRootDirPath(), PROFDATA_FILE_NAME) |
| 847 | |
| 848 | |
| 849 | def _GetSummaryFilePath(): |
| 850 | """The JSON file that contains coverage summary written by llvm-cov export.""" |
| 851 | return os.path.join(_GetCoverageReportRootDirPath(), SUMMARY_FILE_NAME) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 852 | |
| 853 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 854 | def _CreateCoverageProfileDataForTargets(targets, commands, jobs_count=None): |
| 855 | """Builds and runs target to generate the coverage profile data. |
| 856 | |
| 857 | Args: |
| 858 | targets: A list of targets to build with coverage instrumentation. |
| 859 | commands: A list of commands used to run the targets. |
| 860 | jobs_count: Number of jobs to run in parallel for building. If None, a |
| 861 | default value is derived based on CPUs availability. |
| 862 | |
| 863 | Returns: |
| 864 | A relative path to the generated profdata file. |
| 865 | """ |
| 866 | _BuildTargets(targets, jobs_count) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 867 | target_profdata_file_paths = _GetTargetProfDataPathsByExecutingCommands( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 868 | targets, commands) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 869 | coverage_profdata_file_path = ( |
| 870 | _CreateCoverageProfileDataFromTargetProfDataFiles( |
| 871 | target_profdata_file_paths)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 872 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 873 | for target_profdata_file_path in target_profdata_file_paths: |
| 874 | os.remove(target_profdata_file_path) |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 875 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 876 | return coverage_profdata_file_path |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 877 | |
| 878 | |
| 879 | def _BuildTargets(targets, jobs_count): |
| 880 | """Builds target with Clang coverage instrumentation. |
| 881 | |
| 882 | This function requires current working directory to be the root of checkout. |
| 883 | |
| 884 | Args: |
| 885 | targets: A list of targets to build with coverage instrumentation. |
| 886 | jobs_count: Number of jobs to run in parallel for compilation. If None, a |
| 887 | default value is derived based on CPUs availability. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 888 | """ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 889 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 890 | def _IsGomaConfigured(): |
| 891 | """Returns True if goma is enabled in the gn build args. |
| 892 | |
| 893 | Returns: |
| 894 | A boolean indicates whether goma is configured for building or not. |
| 895 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 896 | build_args = _GetBuildArgs() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 897 | return 'use_goma' in build_args and build_args['use_goma'] == 'true' |
| 898 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 899 | logging.info('Building %s.', str(targets)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 900 | if jobs_count is None and _IsGomaConfigured(): |
| 901 | jobs_count = DEFAULT_GOMA_JOBS |
| 902 | |
| 903 | subprocess_cmd = ['ninja', '-C', BUILD_DIR] |
| 904 | if jobs_count is not None: |
| 905 | subprocess_cmd.append('-j' + str(jobs_count)) |
| 906 | |
| 907 | subprocess_cmd.extend(targets) |
| 908 | subprocess.check_call(subprocess_cmd) |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 909 | logging.debug('Finished building %s.', str(targets)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 910 | |
| 911 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 912 | def _GetTargetProfDataPathsByExecutingCommands(targets, commands): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 913 | """Runs commands and returns the relative paths to the profraw data files. |
| 914 | |
| 915 | Args: |
| 916 | targets: A list of targets built with coverage instrumentation. |
| 917 | commands: A list of commands used to run the targets. |
| 918 | |
| 919 | Returns: |
| 920 | A list of relative paths to the generated profraw data files. |
| 921 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 922 | logging.debug('Executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 923 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 924 | # Remove existing profraw data files. |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 925 | for file_or_dir in os.listdir(_GetCoverageReportRootDirPath()): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 926 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 927 | os.remove(os.path.join(_GetCoverageReportRootDirPath(), file_or_dir)) |
| 928 | |
| 929 | # Ensure that logs directory exists. |
| 930 | if not os.path.exists(_GetLogsDirectoryPath()): |
| 931 | os.makedirs(_GetLogsDirectoryPath()) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 932 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 933 | profdata_file_paths = [] |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 934 | |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 935 | # Run all test targets to generate profraw data files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 936 | for target, command in zip(targets, commands): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 937 | output_file_name = os.extsep.join([target + '_output', 'log']) |
| 938 | output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 939 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 940 | profdata_file_path = None |
| 941 | for _ in xrange(MERGE_RETRIES): |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 942 | logging.info('Running command: "%s", the output is redirected to "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 943 | command, output_file_path) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 944 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 945 | if _IsIOSCommand(command): |
| 946 | # On iOS platform, due to lack of write permissions, profraw files are |
| 947 | # generated outside of the OUTPUT_DIR, and the exact paths are contained |
| 948 | # in the output of the command execution. |
| 949 | output = _ExecuteIOSCommand(target, command) |
| 950 | else: |
| 951 | # On other platforms, profraw files are generated inside the OUTPUT_DIR. |
| 952 | output = _ExecuteCommand(target, command) |
| 953 | |
| 954 | with open(output_file_path, 'w') as output_file: |
| 955 | output_file.write(output) |
| 956 | |
| 957 | profraw_file_paths = [] |
| 958 | if _IsIOS(): |
| 959 | profraw_file_paths = _GetProfrawDataFileByParsingOutput(output) |
| 960 | else: |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 961 | for file_or_dir in os.listdir(_GetCoverageReportRootDirPath()): |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 962 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 963 | profraw_file_paths.append( |
| 964 | os.path.join(_GetCoverageReportRootDirPath(), file_or_dir)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 965 | |
| 966 | assert profraw_file_paths, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 967 | 'Running target "%s" failed to generate any profraw data file, ' |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 968 | 'please make sure the binary exists, is properly instrumented and ' |
| 969 | 'does not crash. %s' % (target, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 970 | |
| 971 | try: |
| 972 | profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles( |
| 973 | target, profraw_file_paths) |
| 974 | break |
| 975 | except Exception: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 976 | logging.info('Retrying...') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 977 | finally: |
| 978 | # Remove profraw files now so that they are not used in next iteration. |
| 979 | for profraw_file_path in profraw_file_paths: |
| 980 | os.remove(profraw_file_path) |
| 981 | |
| 982 | assert profdata_file_path, ( |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 983 | 'Failed to merge target "%s" profraw files after %d retries. %s' % |
| 984 | (target, MERGE_RETRIES, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 985 | profdata_file_paths.append(profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 986 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 987 | logging.debug('Finished executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 988 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 989 | return profdata_file_paths |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 990 | |
| 991 | |
| 992 | def _ExecuteCommand(target, command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 993 | """Runs a single command and generates a profraw data file.""" |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 994 | # Per Clang "Source-based Code Coverage" doc: |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 995 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 996 | # "%p" expands out to the process ID. It's not used by this scripts due to: |
| 997 | # 1) If a target program spawns too many processess, it may exhaust all disk |
| 998 | # space available. For example, unit_tests writes thousands of .profraw |
| 999 | # files each of size 1GB+. |
| 1000 | # 2) If a target binary uses shared libraries, coverage profile data for them |
| 1001 | # will be missing, resulting in incomplete coverage reports. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 1002 | # |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 1003 | # "%Nm" expands out to the instrumented binary's signature. When this pattern |
| 1004 | # is specified, the runtime creates a pool of N raw profiles which are used |
| 1005 | # for on-line profile merging. The runtime takes care of selecting a raw |
| 1006 | # profile from the pool, locking it, and updating it before the program exits. |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 1007 | # N must be between 1 and 9. The merge pool specifier can only occur once per |
| 1008 | # filename pattern. |
| 1009 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 1010 | # "%1m" is used when tests run in single process, such as fuzz targets. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 1011 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 1012 | # For other cases, "%4m" is chosen as it creates some level of parallelism, |
| 1013 | # but it's not too big to consume too much computing resource or disk space. |
| 1014 | profile_pattern_string = '%1m' if _IsFuzzerTarget(target) else '%4m' |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1015 | expected_profraw_file_name = os.extsep.join( |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 1016 | [target, profile_pattern_string, PROFRAW_FILE_EXTENSION]) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1017 | expected_profraw_file_path = os.path.join(_GetCoverageReportRootDirPath(), |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1018 | expected_profraw_file_name) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1019 | |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1020 | try: |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1021 | # Some fuzz targets or tests may write into stderr, redirect it as well. |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1022 | output = subprocess.check_output( |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1023 | shlex.split(command), |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1024 | stderr=subprocess.STDOUT, |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 1025 | env={ |
| 1026 | 'LLVM_PROFILE_FILE': expected_profraw_file_path, |
| 1027 | 'PATH': _GetPathWithLLVMSymbolizerDir() |
| 1028 | }) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1029 | except subprocess.CalledProcessError as e: |
| 1030 | output = e.output |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 1031 | logging.warning( |
| 1032 | 'Command: "%s" exited with non-zero return code. Output:\n%s', command, |
| 1033 | output) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1034 | |
| 1035 | return output |
| 1036 | |
| 1037 | |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 1038 | def _IsFuzzerTarget(target): |
| 1039 | """Returns true if the target is a fuzzer target.""" |
| 1040 | build_args = _GetBuildArgs() |
| 1041 | use_libfuzzer = ('use_libfuzzer' in build_args and |
| 1042 | build_args['use_libfuzzer'] == 'true') |
| 1043 | return use_libfuzzer and target.endswith('_fuzzer') |
| 1044 | |
| 1045 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1046 | def _ExecuteIOSCommand(target, command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1047 | """Runs a single iOS command and generates a profraw data file. |
| 1048 | |
| 1049 | iOS application doesn't have write access to folders outside of the app, so |
| 1050 | it's impossible to instruct the app to flush the profraw data file to the |
| 1051 | desired location. The profraw data file will be generated somewhere within the |
| 1052 | application's Documents folder, and the full path can be obtained by parsing |
| 1053 | the output. |
| 1054 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1055 | assert _IsIOSCommand(command) |
| 1056 | |
| 1057 | # After running tests, iossim generates a profraw data file, it won't be |
| 1058 | # needed anyway, so dump it into the OUTPUT_DIR to avoid polluting the |
| 1059 | # checkout. |
| 1060 | iossim_profraw_file_path = os.path.join( |
| 1061 | OUTPUT_DIR, os.extsep.join(['iossim', PROFRAW_FILE_EXTENSION])) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1062 | |
| 1063 | try: |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1064 | output = subprocess.check_output( |
| 1065 | shlex.split(command), |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 1066 | env={ |
| 1067 | 'LLVM_PROFILE_FILE': iossim_profraw_file_path, |
| 1068 | 'PATH': _GetPathWithLLVMSymbolizerDir() |
| 1069 | }) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1070 | except subprocess.CalledProcessError as e: |
| 1071 | # iossim emits non-zero return code even if tests run successfully, so |
| 1072 | # ignore the return code. |
| 1073 | output = e.output |
| 1074 | |
| 1075 | return output |
| 1076 | |
| 1077 | |
| 1078 | def _GetProfrawDataFileByParsingOutput(output): |
| 1079 | """Returns the path to the profraw data file obtained by parsing the output. |
| 1080 | |
| 1081 | The output of running the test target has no format, but it is guaranteed to |
| 1082 | have a single line containing the path to the generated profraw data file. |
| 1083 | NOTE: This should only be called when target os is iOS. |
| 1084 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1085 | assert _IsIOS() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1086 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1087 | output_by_lines = ''.join(output).splitlines() |
| 1088 | profraw_file_pattern = re.compile('.*Coverage data at (.*coverage\.profraw).') |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1089 | |
| 1090 | for line in output_by_lines: |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1091 | result = profraw_file_pattern.match(line) |
| 1092 | if result: |
| 1093 | return result.group(1) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1094 | |
| 1095 | assert False, ('No profraw data file was generated, did you call ' |
| 1096 | 'coverage_util::ConfigureCoverageReportPath() in test setup? ' |
| 1097 | 'Please refer to base/test/test_support_ios.mm for example.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1098 | |
| 1099 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1100 | def _CreateCoverageProfileDataFromTargetProfDataFiles(profdata_file_paths): |
| 1101 | """Returns a relative path to coverage profdata file by merging target |
| 1102 | profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1103 | |
| 1104 | Args: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1105 | profdata_file_paths: A list of relative paths to the profdata data files |
| 1106 | that are to be merged. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1107 | |
| 1108 | Returns: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1109 | A relative path to the merged coverage profdata file. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1110 | |
| 1111 | Raises: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1112 | CalledProcessError: An error occurred merging profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1113 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1114 | logging.info('Creating the coverage profile data file.') |
| 1115 | logging.debug('Merging target profraw files to create target profdata file.') |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1116 | profdata_file_path = _GetProfdataFilePath() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1117 | try: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1118 | subprocess_cmd = [ |
| 1119 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 1120 | ] |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1121 | subprocess_cmd.extend(profdata_file_paths) |
| 1122 | subprocess.check_call(subprocess_cmd) |
| 1123 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 1124 | logging.error( |
| 1125 | 'Failed to merge target profdata files to create coverage profdata. %s', |
| 1126 | FILE_BUG_MESSAGE) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1127 | raise error |
| 1128 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1129 | logging.debug('Finished merging target profdata files.') |
| 1130 | logging.info('Code coverage profile data is created as: "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1131 | profdata_file_path) |
| 1132 | return profdata_file_path |
| 1133 | |
| 1134 | |
| 1135 | def _CreateTargetProfDataFileFromProfRawFiles(target, profraw_file_paths): |
| 1136 | """Returns a relative path to target profdata file by merging target |
| 1137 | profraw files. |
| 1138 | |
| 1139 | Args: |
| 1140 | profraw_file_paths: A list of relative paths to the profdata data files |
| 1141 | that are to be merged. |
| 1142 | |
| 1143 | Returns: |
| 1144 | A relative path to the merged coverage profdata file. |
| 1145 | |
| 1146 | Raises: |
| 1147 | CalledProcessError: An error occurred merging profdata files. |
| 1148 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1149 | logging.info('Creating target profile data file.') |
| 1150 | logging.debug('Merging target profraw files to create target profdata file.') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1151 | profdata_file_path = os.path.join(OUTPUT_DIR, '%s.profdata' % target) |
| 1152 | |
| 1153 | try: |
| 1154 | subprocess_cmd = [ |
| 1155 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 1156 | ] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1157 | subprocess_cmd.extend(profraw_file_paths) |
| 1158 | subprocess.check_call(subprocess_cmd) |
| 1159 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame^] | 1160 | logging.error( |
| 1161 | 'Failed to merge target profraw files to create target profdata.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1162 | raise error |
| 1163 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1164 | logging.debug('Finished merging target profraw files.') |
| 1165 | logging.info('Target "%s" profile data is created as: "%s".', target, |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1166 | profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1167 | return profdata_file_path |
| 1168 | |
| 1169 | |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1170 | def _GeneratePerFileCoverageSummary(binary_paths, profdata_file_path, filters, |
| 1171 | ignore_filename_regex): |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1172 | """Generates per file coverage summary using "llvm-cov export" command.""" |
| 1173 | # llvm-cov export [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 1174 | # [[-object BIN]] [SOURCES]. |
| 1175 | # NOTE: For object files, the first one is specified as a positional argument, |
| 1176 | # and the rest are specified as keyword argument. |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1177 | logging.debug('Generating per-file code coverage summary using "llvm-cov ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1178 | 'export -summary-only" command.') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1179 | subprocess_cmd = [ |
| 1180 | LLVM_COV_PATH, 'export', '-summary-only', |
| 1181 | '-instr-profile=' + profdata_file_path, binary_paths[0] |
| 1182 | ] |
| 1183 | subprocess_cmd.extend( |
| 1184 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1185 | _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths)) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1186 | subprocess_cmd.extend(filters) |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1187 | if ignore_filename_regex: |
| 1188 | subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1189 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1190 | export_output = subprocess.check_output(subprocess_cmd) |
| 1191 | |
| 1192 | # Write output on the disk to be used by code coverage bot. |
| 1193 | with open(_GetSummaryFilePath(), 'w') as f: |
| 1194 | f.write(export_output) |
| 1195 | |
| 1196 | json_output = json.loads(export_output) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1197 | assert len(json_output['data']) == 1 |
| 1198 | files_coverage_data = json_output['data'][0]['files'] |
| 1199 | |
| 1200 | per_file_coverage_summary = {} |
| 1201 | for file_coverage_data in files_coverage_data: |
| 1202 | file_path = file_coverage_data['filename'] |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1203 | assert file_path.startswith(SRC_ROOT_PATH + os.sep), ( |
| 1204 | 'File path "%s" in coverage summary is outside source checkout.' % |
| 1205 | file_path) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1206 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1207 | summary = file_coverage_data['summary'] |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1208 | if summary['lines']['count'] == 0: |
| 1209 | continue |
| 1210 | |
| 1211 | per_file_coverage_summary[file_path] = _CoverageSummary( |
| 1212 | regions_total=summary['regions']['count'], |
| 1213 | regions_covered=summary['regions']['covered'], |
| 1214 | functions_total=summary['functions']['count'], |
| 1215 | functions_covered=summary['functions']['covered'], |
| 1216 | lines_total=summary['lines']['count'], |
| 1217 | lines_covered=summary['lines']['covered']) |
| 1218 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1219 | logging.debug('Finished generating per-file code coverage summary.') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1220 | return per_file_coverage_summary |
| 1221 | |
| 1222 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1223 | def _AddArchArgumentForIOSIfNeeded(cmd_list, num_archs): |
| 1224 | """Appends -arch arguments to the command list if it's ios platform. |
| 1225 | |
| 1226 | iOS binaries are universal binaries, and require specifying the architecture |
| 1227 | to use, and one architecture needs to be specified for each binary. |
| 1228 | """ |
| 1229 | if _IsIOS(): |
| 1230 | cmd_list.extend(['-arch=x86_64'] * num_archs) |
| 1231 | |
| 1232 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1233 | def _GetBinaryPath(command): |
| 1234 | """Returns a relative path to the binary to be run by the command. |
| 1235 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 1236 | Currently, following types of commands are supported (e.g. url_unittests): |
| 1237 | 1. Run test binary direcly: "out/coverage/url_unittests <arguments>" |
| 1238 | 2. Use xvfb. |
| 1239 | 2.1. "python testing/xvfb.py out/coverage/url_unittests <arguments>" |
| 1240 | 2.2. "testing/xvfb.py out/coverage/url_unittests <arguments>" |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 1241 | 3. Use iossim to run tests on iOS platform, please refer to testing/iossim.mm |
| 1242 | for its usage. |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1243 | 3.1. "out/Coverage-iphonesimulator/iossim |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 1244 | <iossim_arguments> -c <app_arguments> |
| 1245 | out/Coverage-iphonesimulator/url_unittests.app" |
| 1246 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 1247 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1248 | Args: |
| 1249 | command: A command used to run a target. |
| 1250 | |
| 1251 | Returns: |
| 1252 | A relative path to the binary. |
| 1253 | """ |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 1254 | xvfb_script_name = os.extsep.join(['xvfb', 'py']) |
| 1255 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1256 | command_parts = shlex.split(command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 1257 | if os.path.basename(command_parts[0]) == 'python': |
| 1258 | assert os.path.basename(command_parts[1]) == xvfb_script_name, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1259 | 'This tool doesn\'t understand the command: "%s".' % command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 1260 | return command_parts[2] |
| 1261 | |
| 1262 | if os.path.basename(command_parts[0]) == xvfb_script_name: |
| 1263 | return command_parts[1] |
| 1264 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1265 | if _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1266 | # For a given application bundle, the binary resides in the bundle and has |
| 1267 | # the same name with the application without the .app extension. |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 1268 | app_path = command_parts[-1].rstrip(os.path.sep) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1269 | app_name = os.path.splitext(os.path.basename(app_path))[0] |
| 1270 | return os.path.join(app_path, app_name) |
| 1271 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1272 | return command_parts[0] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1273 | |
| 1274 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1275 | def _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1276 | """Returns true if command is used to run tests on iOS platform.""" |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 1277 | return os.path.basename(shlex.split(command)[0]) == 'iossim' |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 1278 | |
| 1279 | |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 1280 | def _VerifyTargetExecutablesAreInBuildDirectory(commands): |
| 1281 | """Verifies that the target executables specified in the commands are inside |
| 1282 | the given build directory.""" |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1283 | for command in commands: |
| 1284 | binary_path = _GetBinaryPath(command) |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 1285 | binary_absolute_path = os.path.abspath(os.path.normpath(binary_path)) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1286 | assert binary_absolute_path.startswith(BUILD_DIR), ( |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 1287 | 'Target executable "%s" in command: "%s" is outside of ' |
| 1288 | 'the given build directory: "%s".' % (binary_path, command, BUILD_DIR)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1289 | |
| 1290 | |
| 1291 | def _ValidateBuildingWithClangCoverage(): |
| 1292 | """Asserts that targets are built with Clang coverage enabled.""" |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 1293 | build_args = _GetBuildArgs() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1294 | |
| 1295 | if (CLANG_COVERAGE_BUILD_ARG not in build_args or |
| 1296 | build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'): |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1297 | assert False, ('\'{} = true\' is required in args.gn.' |
| 1298 | ).format(CLANG_COVERAGE_BUILD_ARG) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1299 | |
| 1300 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1301 | def _ValidateCurrentPlatformIsSupported(): |
| 1302 | """Asserts that this script suports running on the current platform""" |
| 1303 | target_os = _GetTargetOS() |
| 1304 | if target_os: |
| 1305 | current_platform = target_os |
| 1306 | else: |
| 1307 | current_platform = _GetHostPlatform() |
| 1308 | |
| 1309 | assert current_platform in [ |
| 1310 | 'linux', 'mac', 'chromeos', 'ios' |
| 1311 | ], ('Coverage is only supported on linux, mac, chromeos and ios.') |
| 1312 | |
| 1313 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 1314 | def _GetBuildArgs(): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1315 | """Parses args.gn file and returns results as a dictionary. |
| 1316 | |
| 1317 | Returns: |
| 1318 | A dictionary representing the build args. |
| 1319 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 1320 | global _BUILD_ARGS |
| 1321 | if _BUILD_ARGS is not None: |
| 1322 | return _BUILD_ARGS |
| 1323 | |
| 1324 | _BUILD_ARGS = {} |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1325 | build_args_path = os.path.join(BUILD_DIR, 'args.gn') |
| 1326 | assert os.path.exists(build_args_path), ('"%s" is not a build directory, ' |
| 1327 | 'missing args.gn file.' % BUILD_DIR) |
| 1328 | with open(build_args_path) as build_args_file: |
| 1329 | build_args_lines = build_args_file.readlines() |
| 1330 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1331 | for build_arg_line in build_args_lines: |
| 1332 | build_arg_without_comments = build_arg_line.split('#')[0] |
| 1333 | key_value_pair = build_arg_without_comments.split('=') |
| 1334 | if len(key_value_pair) != 2: |
| 1335 | continue |
| 1336 | |
| 1337 | key = key_value_pair[0].strip() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1338 | |
| 1339 | # Values are wrapped within a pair of double-quotes, so remove the leading |
| 1340 | # and trailing double-quotes. |
| 1341 | value = key_value_pair[1].strip().strip('"') |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 1342 | _BUILD_ARGS[key] = value |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1343 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 1344 | return _BUILD_ARGS |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1345 | |
| 1346 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1347 | def _VerifyPathsAndReturnAbsolutes(paths): |
| 1348 | """Verifies that the paths specified in |paths| exist and returns absolute |
| 1349 | versions. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1350 | |
| 1351 | Args: |
| 1352 | paths: A list of files or directories. |
| 1353 | """ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1354 | absolute_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1355 | for path in paths: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1356 | absolute_path = os.path.join(SRC_ROOT_PATH, path) |
| 1357 | assert os.path.exists(absolute_path), ('Path: "%s" doesn\'t exist.' % path) |
| 1358 | |
| 1359 | absolute_paths.append(absolute_path) |
| 1360 | |
| 1361 | return absolute_paths |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1362 | |
| 1363 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1364 | def _GetRelativePathToDirectoryOfFile(target_path, base_path): |
| 1365 | """Returns a target path relative to the directory of base_path. |
| 1366 | |
| 1367 | This method requires base_path to be a file, otherwise, one should call |
| 1368 | os.path.relpath directly. |
| 1369 | """ |
| 1370 | assert os.path.dirname(base_path) != base_path, ( |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 1371 | 'Base path: "%s" is a directory, please call os.path.relpath directly.' % |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1372 | base_path) |
Yuke Liao | c7e60714 | 2018-02-05 20:26:14 | [diff] [blame] | 1373 | base_dir = os.path.dirname(base_path) |
| 1374 | return os.path.relpath(target_path, base_dir) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1375 | |
| 1376 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1377 | def _GetBinaryPathsFromTargets(targets, build_dir): |
| 1378 | """Return binary paths from target names.""" |
| 1379 | # FIXME: Derive output binary from target build definitions rather than |
| 1380 | # assuming that it is always the same name. |
| 1381 | binary_paths = [] |
| 1382 | for target in targets: |
| 1383 | binary_path = os.path.join(build_dir, target) |
| 1384 | if _GetHostPlatform() == 'win': |
| 1385 | binary_path += '.exe' |
| 1386 | |
| 1387 | if os.path.exists(binary_path): |
| 1388 | binary_paths.append(binary_path) |
| 1389 | else: |
| 1390 | logging.warning( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1391 | 'Target binary "%s" not found in build directory, skipping.', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1392 | os.path.basename(binary_path)) |
| 1393 | |
| 1394 | return binary_paths |
| 1395 | |
| 1396 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1397 | def _ParseCommandArguments(): |
| 1398 | """Adds and parses relevant arguments for tool comands. |
| 1399 | |
| 1400 | Returns: |
| 1401 | A dictionary representing the arguments. |
| 1402 | """ |
| 1403 | arg_parser = argparse.ArgumentParser() |
| 1404 | arg_parser.usage = __doc__ |
| 1405 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1406 | arg_parser.add_argument( |
| 1407 | '-b', |
| 1408 | '--build-dir', |
| 1409 | type=str, |
| 1410 | required=True, |
| 1411 | help='The build directory, the path needs to be relative to the root of ' |
| 1412 | 'the checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1413 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1414 | arg_parser.add_argument( |
| 1415 | '-o', |
| 1416 | '--output-dir', |
| 1417 | type=str, |
| 1418 | required=True, |
| 1419 | help='Output directory for generated artifacts.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1420 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1421 | arg_parser.add_argument( |
| 1422 | '-c', |
| 1423 | '--command', |
| 1424 | action='append', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1425 | required=False, |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1426 | help='Commands used to run test targets, one test target needs one and ' |
| 1427 | 'only one command, when specifying commands, one should assume the ' |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1428 | 'current working directory is the root of the checkout. This option is ' |
| 1429 | 'incompatible with -p/--profdata-file option.') |
| 1430 | |
| 1431 | arg_parser.add_argument( |
| 1432 | '-p', |
| 1433 | '--profdata-file', |
| 1434 | type=str, |
| 1435 | required=False, |
| 1436 | help='Path to profdata file to use for generating code coverage reports. ' |
| 1437 | 'This can be useful if you generated the profdata file seperately in ' |
| 1438 | 'your own test harness. This option is ignored if run command(s) are ' |
| 1439 | 'already provided above using -c/--command option.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1440 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1441 | arg_parser.add_argument( |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1442 | '-f', |
| 1443 | '--filters', |
| 1444 | action='append', |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1445 | required=False, |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1446 | help='Directories or files to get code coverage for, and all files under ' |
| 1447 | 'the directories are included recursively.') |
| 1448 | |
| 1449 | arg_parser.add_argument( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1450 | '-i', |
| 1451 | '--ignore-filename-regex', |
| 1452 | type=str, |
| 1453 | help='Skip source code files with file paths that match the given ' |
| 1454 | 'regular expression. For example, use -i=\'.*/out/.*|.*/third_party/.*\' ' |
| 1455 | 'to exclude files in third_party/ and out/ folders from the report.') |
| 1456 | |
| 1457 | arg_parser.add_argument( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1458 | '-j', |
| 1459 | '--jobs', |
| 1460 | type=int, |
| 1461 | default=None, |
| 1462 | help='Run N jobs to build in parallel. If not specified, a default value ' |
| 1463 | 'will be derived based on CPUs availability. Please refer to ' |
| 1464 | '\'ninja -h\' for more details.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1465 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1466 | arg_parser.add_argument( |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1467 | '-v', |
| 1468 | '--verbose', |
| 1469 | action='store_true', |
| 1470 | help='Prints additional output for diagnostics.') |
| 1471 | |
| 1472 | arg_parser.add_argument( |
| 1473 | '-l', '--log_file', type=str, help='Redirects logs to a file.') |
| 1474 | |
| 1475 | arg_parser.add_argument( |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1476 | 'targets', |
| 1477 | nargs='+', |
| 1478 | help='The names of the test targets to run. If multiple run commands are ' |
| 1479 | 'specified using the -c/--command option, then the order of targets and ' |
| 1480 | 'commands must match, otherwise coverage generation will fail.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1481 | |
| 1482 | args = arg_parser.parse_args() |
| 1483 | return args |
| 1484 | |
| 1485 | |
| 1486 | def Main(): |
| 1487 | """Execute tool commands.""" |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1488 | # Change directory to source root to aid in relative paths calculations. |
| 1489 | os.chdir(SRC_ROOT_PATH) |
Abhishek Arya | 8a0751a | 2018-05-03 18:53:11 | [diff] [blame] | 1490 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1491 | # Setup coverage binaries even when script is called with empty params. This |
| 1492 | # is used by coverage bot for initial setup. |
Abhishek Arya | 8a0751a | 2018-05-03 18:53:11 | [diff] [blame] | 1493 | DownloadCoverageToolsIfNeeded() |
| 1494 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1495 | args = _ParseCommandArguments() |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1496 | _ConfigureLogging(args) |
| 1497 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1498 | global BUILD_DIR |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1499 | BUILD_DIR = os.path.abspath(args.build_dir) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1500 | global OUTPUT_DIR |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1501 | OUTPUT_DIR = os.path.abspath(args.output_dir) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1502 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1503 | assert args.command or args.profdata_file, ( |
| 1504 | 'Need to either provide commands to run using -c/--command option OR ' |
| 1505 | 'provide prof-data file as input using -p/--profdata-file option.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1506 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1507 | assert not args.command or (len(args.targets) == len(args.command)), ( |
| 1508 | 'Number of targets must be equal to the number of test commands.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1509 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1510 | assert os.path.exists(BUILD_DIR), ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1511 | 'Build directory: "%s" doesn\'t exist. ' |
| 1512 | 'Please run "gn gen" to generate.' % BUILD_DIR) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1513 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1514 | _ValidateCurrentPlatformIsSupported() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1515 | _ValidateBuildingWithClangCoverage() |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1516 | |
| 1517 | absolute_filter_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1518 | if args.filters: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1519 | absolute_filter_paths = _VerifyPathsAndReturnAbsolutes(args.filters) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1520 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1521 | if not os.path.exists(_GetCoverageReportRootDirPath()): |
| 1522 | os.makedirs(_GetCoverageReportRootDirPath()) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1523 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1524 | # Get profdate file and list of binary paths. |
| 1525 | if args.command: |
| 1526 | # A list of commands are provided. Run them to generate profdata file, and |
| 1527 | # create a list of binary paths from parsing commands. |
| 1528 | _VerifyTargetExecutablesAreInBuildDirectory(args.command) |
| 1529 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 1530 | args.targets, args.command, args.jobs) |
| 1531 | binary_paths = [_GetBinaryPath(command) for command in args.command] |
| 1532 | else: |
| 1533 | # An input prof-data file is already provided. Just calculate binary paths. |
| 1534 | profdata_file_path = args.profdata_file |
| 1535 | binary_paths = _GetBinaryPathsFromTargets(args.targets, args.build_dir) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1536 | |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 1537 | binary_paths.extend(_GetSharedLibraries(binary_paths)) |
| 1538 | |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1539 | logging.info('Generating code coverage report in html (this can take a while ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1540 | 'depending on size of target!).') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1541 | per_file_coverage_summary = _GeneratePerFileCoverageSummary( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1542 | binary_paths, profdata_file_path, absolute_filter_paths, |
| 1543 | args.ignore_filename_regex) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1544 | _GeneratePerFileLineByLineCoverageInHtml(binary_paths, profdata_file_path, |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1545 | absolute_filter_paths, |
| 1546 | args.ignore_filename_regex) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1547 | _GenerateFileViewHtmlIndexFile(per_file_coverage_summary) |
| 1548 | |
| 1549 | per_directory_coverage_summary = _CalculatePerDirectoryCoverageSummary( |
| 1550 | per_file_coverage_summary) |
| 1551 | _GeneratePerDirectoryCoverageInHtml(per_directory_coverage_summary, |
| 1552 | per_file_coverage_summary) |
| 1553 | _GenerateDirectoryViewHtmlIndexFile() |
| 1554 | |
| 1555 | component_to_directories = _ExtractComponentToDirectoriesMapping() |
| 1556 | per_component_coverage_summary = _CalculatePerComponentCoverageSummary( |
| 1557 | component_to_directories, per_directory_coverage_summary) |
| 1558 | _GeneratePerComponentCoverageInHtml(per_component_coverage_summary, |
| 1559 | component_to_directories, |
| 1560 | per_directory_coverage_summary) |
| 1561 | _GenerateComponentViewHtmlIndexFile(per_component_coverage_summary) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1562 | |
| 1563 | # The default index file is generated only for the list of source files, needs |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1564 | # to overwrite it to display per directory coverage view by default. |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1565 | _OverwriteHtmlReportsIndexFile() |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1566 | _CleanUpOutputDir() |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1567 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 1568 | html_index_file_path = 'file://' + os.path.abspath(_GetHtmlIndexPath()) |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1569 | logging.info('Index file for html report is generated as: "%s".', |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1570 | html_index_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1571 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1572 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1573 | if __name__ == '__main__': |
| 1574 | sys.exit(Main()) |