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 | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 14 | * Example usage: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 15 | |
Max Moroz | a5a9527 | 2018-08-31 16:20:55 | [diff] [blame] | 16 | gn gen out/coverage \\ |
Abhishek Arya | 2f26118 | 2019-04-24 17:06:45 | [diff] [blame] | 17 | --args="use_clang_coverage=true is_component_build=false\\ |
| 18 | is_debug=false dcheck_always_on=true" |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 19 | gclient runhooks |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 20 | python tools/code_coverage/coverage.py crypto_unittests url_unittests \\ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 21 | -b out/coverage -o out/report -c 'out/coverage/crypto_unittests' \\ |
| 22 | -c 'out/coverage/url_unittests --gtest_filter=URLParser.PathURL' \\ |
| 23 | -f url/ -f crypto/ |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 24 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 25 | The command above builds crypto_unittests and url_unittests targets and then |
| 26 | runs them with specified command line arguments. For url_unittests, it only |
| 27 | runs the test URLParser.PathURL. The coverage report is filtered to include |
| 28 | only files and sub-directories under url/ and crypto/ directories. |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 29 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 30 | If you want to run tests that try to draw to the screen but don't have a |
| 31 | display connected, you can run tests in headless mode with xvfb. |
| 32 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 33 | * Sample flow for running a test target with xvfb (e.g. unit_tests): |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 34 | |
| 35 | python tools/code_coverage/coverage.py unit_tests -b out/coverage \\ |
| 36 | -o out/report -c 'python testing/xvfb.py out/coverage/unit_tests' |
| 37 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 38 | If you are building a fuzz target, you need to add "use_libfuzzer=true" GN |
| 39 | flag as well. |
| 40 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 41 | * Sample workflow for a fuzz target (e.g. pdfium_fuzzer): |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 42 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 43 | python tools/code_coverage/coverage.py pdfium_fuzzer \\ |
| 44 | -b out/coverage -o out/report \\ |
Max Moroz | 13c2318 | 2018-11-17 00:23:22 | [diff] [blame] | 45 | -c 'out/coverage/pdfium_fuzzer -runs=0 <corpus_dir>' \\ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 46 | -f third_party/pdfium |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 47 | |
| 48 | where: |
| 49 | <corpus_dir> - directory containing samples files for this format. |
Max Moroz | 13c2318 | 2018-11-17 00:23:22 | [diff] [blame] | 50 | |
| 51 | To learn more about generating code coverage reports for fuzz targets, see |
| 52 | https://chromium.googlesource.com/chromium/src/+/master/testing/libfuzzer/efficient_fuzzer.md#Code-Coverage |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 53 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 54 | * Sample workflow for running Blink web tests: |
| 55 | |
| 56 | python tools/code_coverage/coverage.py blink_tests \\ |
| 57 | -wt -b out/coverage -o out/report -f third_party/blink |
| 58 | |
| 59 | If you need to pass arguments to run_web_tests.py, use |
| 60 | -wt='arguments to run_web_tests.py e.g. test directories' |
| 61 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 62 | For more options, please refer to tools/code_coverage/coverage.py -h. |
Yuke Liao | 8e209fe8 | 2018-04-18 20:36:38 | [diff] [blame] | 63 | |
| 64 | For an overview of how code coverage works in Chromium, please refer to |
Yuke Liao | 43bbbcd5 | 2019-06-21 19:34:50 | [diff] [blame] | 65 | https://chromium.googlesource.com/chromium/src/+/master/docs/testing/code_coverage.md |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 66 | """ |
| 67 | |
| 68 | from __future__ import print_function |
| 69 | |
| 70 | import sys |
| 71 | |
| 72 | import argparse |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 73 | import json |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 74 | import logging |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 75 | import multiprocessing |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 76 | import os |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 77 | import re |
| 78 | import shlex |
Max Moroz | 025d895 | 2018-05-03 16:33:34 | [diff] [blame] | 79 | import shutil |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 80 | import subprocess |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 81 | import urllib2 |
| 82 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 83 | sys.path.append( |
| 84 | os.path.join( |
| 85 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'tools', |
| 86 | 'clang', 'scripts')) |
Roberto Carrillo | 3631272 | 2018-10-17 02:18:45 | [diff] [blame] | 87 | from update import LLVM_BUILD_DIR |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 88 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 89 | sys.path.append( |
| 90 | os.path.join( |
| 91 | os.path.dirname(__file__), os.path.pardir, os.path.pardir, |
| 92 | 'third_party')) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 93 | from collections import defaultdict |
| 94 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 95 | import coverage_utils |
Roberto Carrillo | 3631272 | 2018-10-17 02:18:45 | [diff] [blame] | 96 | import update_clang_coverage_tools |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 97 | |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 98 | # Absolute path to the code coverage tools binary. These paths can be |
| 99 | # overwritten by user specified coverage tool paths. |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 100 | LLVM_BIN_DIR = os.path.join(LLVM_BUILD_DIR, 'bin') |
| 101 | LLVM_COV_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-cov') |
| 102 | LLVM_PROFDATA_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-profdata') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 103 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 104 | # Absolute path to the root of the checkout. |
| 105 | SRC_ROOT_PATH = None |
| 106 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 107 | # Build directory, the value is parsed from command line arguments. |
| 108 | BUILD_DIR = None |
| 109 | |
| 110 | # Output directory for generated artifacts, the value is parsed from command |
| 111 | # line arguemnts. |
| 112 | OUTPUT_DIR = None |
| 113 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 114 | # Name of the file extension for profraw data files. |
| 115 | PROFRAW_FILE_EXTENSION = 'profraw' |
| 116 | |
| 117 | # Name of the final profdata file, and this file needs to be passed to |
| 118 | # "llvm-cov" command in order to call "llvm-cov show" to inspect the |
| 119 | # line-by-line coverage of specific files. |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 120 | PROFDATA_FILE_NAME = os.extsep.join(['coverage', 'profdata']) |
| 121 | |
| 122 | # Name of the file with summary information generated by llvm-cov export. |
| 123 | SUMMARY_FILE_NAME = os.extsep.join(['summary', 'json']) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 124 | |
| 125 | # Build arg required for generating code coverage data. |
| 126 | CLANG_COVERAGE_BUILD_ARG = 'use_clang_coverage' |
| 127 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 128 | LOGS_DIR_NAME = 'logs' |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 129 | |
| 130 | # Used to extract a mapping between directories and components. |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 131 | COMPONENT_MAPPING_URL = ( |
| 132 | 'https://storage.googleapis.com/chromium-owners/component_map.json') |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 133 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 134 | # Caches the results returned by _GetBuildArgs, don't use this variable |
| 135 | # directly, call _GetBuildArgs instead. |
| 136 | _BUILD_ARGS = None |
| 137 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 138 | # Retry failed merges. |
| 139 | MERGE_RETRIES = 3 |
| 140 | |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 141 | # Message to guide user to file a bug when everything else fails. |
| 142 | FILE_BUG_MESSAGE = ( |
| 143 | 'If it persists, please file a bug with the command you used, git revision ' |
| 144 | 'and args.gn config here: ' |
| 145 | 'https://bugs.chromium.org/p/chromium/issues/entry?' |
| 146 | 'components=Tools%3ECodeCoverage') |
| 147 | |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 148 | # String to replace with actual llvm profile path. |
| 149 | LLVM_PROFILE_FILE_PATH_SUBSTITUTION = '<llvm_profile_file_path>' |
| 150 | |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 151 | |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 152 | def _ConfigureLLVMCoverageTools(args): |
| 153 | """Configures llvm coverage tools.""" |
| 154 | if args.coverage_tools_dir: |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 155 | llvm_bin_dir = coverage_utils.GetFullPath(args.coverage_tools_dir) |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 156 | global LLVM_COV_PATH |
| 157 | global LLVM_PROFDATA_PATH |
| 158 | LLVM_COV_PATH = os.path.join(llvm_bin_dir, 'llvm-cov') |
| 159 | LLVM_PROFDATA_PATH = os.path.join(llvm_bin_dir, 'llvm-profdata') |
| 160 | else: |
Roberto Carrillo | 3631272 | 2018-10-17 02:18:45 | [diff] [blame] | 161 | update_clang_coverage_tools.DownloadCoverageToolsIfNeeded() |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 162 | |
| 163 | coverage_tools_exist = ( |
| 164 | os.path.exists(LLVM_COV_PATH) and os.path.exists(LLVM_PROFDATA_PATH)) |
| 165 | assert coverage_tools_exist, ('Cannot find coverage tools, please make sure ' |
| 166 | 'both \'%s\' and \'%s\' exist.') % ( |
| 167 | LLVM_COV_PATH, LLVM_PROFDATA_PATH) |
| 168 | |
Abhishek Arya | 2f26118 | 2019-04-24 17:06:45 | [diff] [blame] | 169 | |
Abhishek Arya | 1c97ea54 | 2018-05-10 03:53:19 | [diff] [blame] | 170 | def _GetPathWithLLVMSymbolizerDir(): |
| 171 | """Add llvm-symbolizer directory to path for symbolized stacks.""" |
| 172 | path = os.getenv('PATH') |
| 173 | dirs = path.split(os.pathsep) |
| 174 | if LLVM_BIN_DIR in dirs: |
| 175 | return path |
| 176 | |
| 177 | return path + os.pathsep + LLVM_BIN_DIR |
| 178 | |
| 179 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 180 | def _GetTargetOS(): |
| 181 | """Returns the target os specified in args.gn file. |
| 182 | |
| 183 | Returns an empty string is target_os is not specified. |
| 184 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 185 | build_args = _GetBuildArgs() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 186 | return build_args['target_os'] if 'target_os' in build_args else '' |
| 187 | |
| 188 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 189 | def _IsIOS(): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 190 | """Returns true if the target_os specified in args.gn file is ios""" |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 191 | return _GetTargetOS() == 'ios' |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 192 | |
| 193 | |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 194 | def _GeneratePerFileLineByLineCoverageInHtml(binary_paths, profdata_file_path, |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 195 | filters, ignore_filename_regex): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 196 | """Generates per file line-by-line coverage in html using 'llvm-cov show'. |
| 197 | |
| 198 | For a file with absolute path /a/b/x.cc, a html report is generated as: |
| 199 | OUTPUT_DIR/coverage/a/b/x.cc.html. An index html file is also generated as: |
| 200 | OUTPUT_DIR/index.html. |
| 201 | |
| 202 | Args: |
| 203 | binary_paths: A list of paths to the instrumented binaries. |
| 204 | profdata_file_path: A path to the profdata file. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 205 | filters: A list of directories and files to get coverage for. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 206 | """ |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 207 | # llvm-cov show [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 208 | # [[-object BIN]] [SOURCES] |
| 209 | # NOTE: For object files, the first one is specified as a positional argument, |
| 210 | # and the rest are specified as keyword argument. |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 211 | logging.debug('Generating per file line by line coverage reports using ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 212 | '"llvm-cov show" command.') |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 213 | subprocess_cmd = [ |
| 214 | LLVM_COV_PATH, 'show', '-format=html', |
| 215 | '-output-dir={}'.format(OUTPUT_DIR), |
| 216 | '-instr-profile={}'.format(profdata_file_path), binary_paths[0] |
| 217 | ] |
| 218 | subprocess_cmd.extend( |
| 219 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 220 | _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths)) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 221 | if coverage_utils.GetHostPlatform() in ['linux', 'mac']: |
Ryan Sleevi | ae19b2c3 | 2018-05-15 22:36:17 | [diff] [blame] | 222 | subprocess_cmd.extend(['-Xdemangler', 'c++filt', '-Xdemangler', '-n']) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 223 | subprocess_cmd.extend(filters) |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 224 | if ignore_filename_regex: |
| 225 | subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex) |
| 226 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 227 | subprocess.check_call(subprocess_cmd) |
Max Moroz | 025d895 | 2018-05-03 16:33:34 | [diff] [blame] | 228 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 229 | logging.debug('Finished running "llvm-cov show" command.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 230 | |
| 231 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 232 | def _GetLogsDirectoryPath(): |
| 233 | """Path to the logs directory.""" |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 234 | return os.path.join( |
| 235 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), LOGS_DIR_NAME) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 236 | |
| 237 | |
| 238 | def _GetProfdataFilePath(): |
| 239 | """Path to the resulting .profdata file.""" |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 240 | return os.path.join( |
| 241 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), |
| 242 | PROFDATA_FILE_NAME) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 243 | |
| 244 | |
| 245 | def _GetSummaryFilePath(): |
| 246 | """The JSON file that contains coverage summary written by llvm-cov export.""" |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 247 | return os.path.join( |
| 248 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), |
| 249 | SUMMARY_FILE_NAME) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 250 | |
| 251 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 252 | def _CreateCoverageProfileDataForTargets(targets, commands, jobs_count=None): |
| 253 | """Builds and runs target to generate the coverage profile data. |
| 254 | |
| 255 | Args: |
| 256 | targets: A list of targets to build with coverage instrumentation. |
| 257 | commands: A list of commands used to run the targets. |
| 258 | jobs_count: Number of jobs to run in parallel for building. If None, a |
| 259 | default value is derived based on CPUs availability. |
| 260 | |
| 261 | Returns: |
| 262 | A relative path to the generated profdata file. |
| 263 | """ |
| 264 | _BuildTargets(targets, jobs_count) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 265 | target_profdata_file_paths = _GetTargetProfDataPathsByExecutingCommands( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 266 | targets, commands) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 267 | coverage_profdata_file_path = ( |
| 268 | _CreateCoverageProfileDataFromTargetProfDataFiles( |
| 269 | target_profdata_file_paths)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 270 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 271 | for target_profdata_file_path in target_profdata_file_paths: |
| 272 | os.remove(target_profdata_file_path) |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 273 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 274 | return coverage_profdata_file_path |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 275 | |
| 276 | |
| 277 | def _BuildTargets(targets, jobs_count): |
| 278 | """Builds target with Clang coverage instrumentation. |
| 279 | |
| 280 | This function requires current working directory to be the root of checkout. |
| 281 | |
| 282 | Args: |
| 283 | targets: A list of targets to build with coverage instrumentation. |
| 284 | jobs_count: Number of jobs to run in parallel for compilation. If None, a |
| 285 | default value is derived based on CPUs availability. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 286 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 287 | logging.info('Building %s.', str(targets)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 288 | |
Max Moroz | 0657629 | 2019-01-03 19:22:52 | [diff] [blame] | 289 | subprocess_cmd = ['autoninja', '-C', BUILD_DIR] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 290 | if jobs_count is not None: |
| 291 | subprocess_cmd.append('-j' + str(jobs_count)) |
| 292 | |
| 293 | subprocess_cmd.extend(targets) |
| 294 | subprocess.check_call(subprocess_cmd) |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 295 | logging.debug('Finished building %s.', str(targets)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 296 | |
| 297 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 298 | def _GetTargetProfDataPathsByExecutingCommands(targets, commands): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 299 | """Runs commands and returns the relative paths to the profraw data files. |
| 300 | |
| 301 | Args: |
| 302 | targets: A list of targets built with coverage instrumentation. |
| 303 | commands: A list of commands used to run the targets. |
| 304 | |
| 305 | Returns: |
| 306 | A list of relative paths to the generated profraw data files. |
| 307 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 308 | logging.debug('Executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 309 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 310 | # Remove existing profraw data files. |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 311 | report_root_dir = coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR) |
| 312 | for file_or_dir in os.listdir(report_root_dir): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 313 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 314 | os.remove(os.path.join(report_root_dir, file_or_dir)) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 315 | |
| 316 | # Ensure that logs directory exists. |
| 317 | if not os.path.exists(_GetLogsDirectoryPath()): |
| 318 | os.makedirs(_GetLogsDirectoryPath()) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 319 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 320 | profdata_file_paths = [] |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 321 | |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 322 | # Run all test targets to generate profraw data files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 323 | for target, command in zip(targets, commands): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 324 | output_file_name = os.extsep.join([target + '_output', 'log']) |
| 325 | output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 326 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 327 | profdata_file_path = None |
| 328 | for _ in xrange(MERGE_RETRIES): |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 329 | logging.info('Running command: "%s", the output is redirected to "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 330 | command, output_file_path) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 331 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 332 | if _IsIOSCommand(command): |
| 333 | # On iOS platform, due to lack of write permissions, profraw files are |
| 334 | # generated outside of the OUTPUT_DIR, and the exact paths are contained |
| 335 | # in the output of the command execution. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 336 | output = _ExecuteIOSCommand(command, output_file_path) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 337 | else: |
| 338 | # On other platforms, profraw files are generated inside the OUTPUT_DIR. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 339 | output = _ExecuteCommand(target, command, output_file_path) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 340 | |
| 341 | profraw_file_paths = [] |
| 342 | if _IsIOS(): |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 343 | profraw_file_paths = [_GetProfrawDataFileByParsingOutput(output)] |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 344 | else: |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 345 | for file_or_dir in os.listdir(report_root_dir): |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 346 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 347 | profraw_file_paths.append( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 348 | os.path.join(report_root_dir, file_or_dir)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 349 | |
| 350 | assert profraw_file_paths, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 351 | 'Running target "%s" failed to generate any profraw data file, ' |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 352 | 'please make sure the binary exists, is properly instrumented and ' |
| 353 | 'does not crash. %s' % (target, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 354 | |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 355 | assert isinstance(profraw_file_paths, list), ( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 356 | 'Variable \'profraw_file_paths\' is expected to be of type \'list\', ' |
| 357 | 'but it is a %s. %s' % (type(profraw_file_paths), FILE_BUG_MESSAGE)) |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 358 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 359 | try: |
| 360 | profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles( |
| 361 | target, profraw_file_paths) |
| 362 | break |
| 363 | except Exception: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 364 | logging.info('Retrying...') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 365 | finally: |
| 366 | # Remove profraw files now so that they are not used in next iteration. |
| 367 | for profraw_file_path in profraw_file_paths: |
| 368 | os.remove(profraw_file_path) |
| 369 | |
| 370 | assert profdata_file_path, ( |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 371 | 'Failed to merge target "%s" profraw files after %d retries. %s' % |
| 372 | (target, MERGE_RETRIES, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 373 | profdata_file_paths.append(profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 374 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 375 | logging.debug('Finished executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 376 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 377 | return profdata_file_paths |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 378 | |
| 379 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 380 | def _GetEnvironmentVars(profraw_file_path): |
| 381 | """Return environment vars for subprocess, given a profraw file path.""" |
| 382 | env = os.environ.copy() |
| 383 | env.update({ |
| 384 | 'LLVM_PROFILE_FILE': profraw_file_path, |
| 385 | 'PATH': _GetPathWithLLVMSymbolizerDir() |
| 386 | }) |
| 387 | return env |
| 388 | |
| 389 | |
| 390 | def _ExecuteCommand(target, command, output_file_path): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 391 | """Runs a single command and generates a profraw data file.""" |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 392 | # Per Clang "Source-based Code Coverage" doc: |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 393 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 394 | # "%p" expands out to the process ID. It's not used by this scripts due to: |
| 395 | # 1) If a target program spawns too many processess, it may exhaust all disk |
| 396 | # space available. For example, unit_tests writes thousands of .profraw |
| 397 | # files each of size 1GB+. |
| 398 | # 2) If a target binary uses shared libraries, coverage profile data for them |
| 399 | # will be missing, resulting in incomplete coverage reports. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 400 | # |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 401 | # "%Nm" expands out to the instrumented binary's signature. When this pattern |
| 402 | # is specified, the runtime creates a pool of N raw profiles which are used |
| 403 | # for on-line profile merging. The runtime takes care of selecting a raw |
| 404 | # profile from the pool, locking it, and updating it before the program exits. |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 405 | # N must be between 1 and 9. The merge pool specifier can only occur once per |
| 406 | # filename pattern. |
| 407 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 408 | # "%1m" is used when tests run in single process, such as fuzz targets. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 409 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 410 | # For other cases, "%4m" is chosen as it creates some level of parallelism, |
| 411 | # but it's not too big to consume too much computing resource or disk space. |
| 412 | profile_pattern_string = '%1m' if _IsFuzzerTarget(target) else '%4m' |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 413 | expected_profraw_file_name = os.extsep.join( |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 414 | [target, profile_pattern_string, PROFRAW_FILE_EXTENSION]) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 415 | expected_profraw_file_path = os.path.join( |
| 416 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), |
| 417 | expected_profraw_file_name) |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 418 | command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION, |
| 419 | expected_profraw_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 420 | |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 421 | try: |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 422 | # Some fuzz targets or tests may write into stderr, redirect it as well. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 423 | with open(output_file_path, 'wb') as output_file_handle: |
| 424 | subprocess.check_call( |
| 425 | shlex.split(command), |
| 426 | stdout=output_file_handle, |
| 427 | stderr=subprocess.STDOUT, |
| 428 | env=_GetEnvironmentVars(expected_profraw_file_path)) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 429 | except subprocess.CalledProcessError as e: |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 430 | logging.warning('Command: "%s" exited with non-zero return code.', command) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 431 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 432 | return open(output_file_path, 'rb').read() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 433 | |
| 434 | |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 435 | def _IsFuzzerTarget(target): |
| 436 | """Returns true if the target is a fuzzer target.""" |
| 437 | build_args = _GetBuildArgs() |
| 438 | use_libfuzzer = ('use_libfuzzer' in build_args and |
| 439 | build_args['use_libfuzzer'] == 'true') |
| 440 | return use_libfuzzer and target.endswith('_fuzzer') |
| 441 | |
| 442 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 443 | def _ExecuteIOSCommand(command, output_file_path): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 444 | """Runs a single iOS command and generates a profraw data file. |
| 445 | |
| 446 | iOS application doesn't have write access to folders outside of the app, so |
| 447 | it's impossible to instruct the app to flush the profraw data file to the |
| 448 | desired location. The profraw data file will be generated somewhere within the |
| 449 | application's Documents folder, and the full path can be obtained by parsing |
| 450 | the output. |
| 451 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 452 | assert _IsIOSCommand(command) |
| 453 | |
| 454 | # After running tests, iossim generates a profraw data file, it won't be |
| 455 | # needed anyway, so dump it into the OUTPUT_DIR to avoid polluting the |
| 456 | # checkout. |
| 457 | iossim_profraw_file_path = os.path.join( |
| 458 | OUTPUT_DIR, os.extsep.join(['iossim', PROFRAW_FILE_EXTENSION])) |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 459 | command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION, |
| 460 | iossim_profraw_file_path) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 461 | |
| 462 | try: |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 463 | with open(output_file_path, 'wb') as output_file_handle: |
| 464 | subprocess.check_call( |
| 465 | shlex.split(command), |
| 466 | stdout=output_file_handle, |
| 467 | stderr=subprocess.STDOUT, |
| 468 | env=_GetEnvironmentVars(iossim_profraw_file_path)) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 469 | except subprocess.CalledProcessError as e: |
| 470 | # iossim emits non-zero return code even if tests run successfully, so |
| 471 | # ignore the return code. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 472 | pass |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 473 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 474 | return open(output_file_path, 'rb').read() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 475 | |
| 476 | |
| 477 | def _GetProfrawDataFileByParsingOutput(output): |
| 478 | """Returns the path to the profraw data file obtained by parsing the output. |
| 479 | |
| 480 | The output of running the test target has no format, but it is guaranteed to |
| 481 | have a single line containing the path to the generated profraw data file. |
| 482 | NOTE: This should only be called when target os is iOS. |
| 483 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 484 | assert _IsIOS() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 485 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 486 | output_by_lines = ''.join(output).splitlines() |
| 487 | profraw_file_pattern = re.compile('.*Coverage data at (.*coverage\.profraw).') |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 488 | |
| 489 | for line in output_by_lines: |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 490 | result = profraw_file_pattern.match(line) |
| 491 | if result: |
| 492 | return result.group(1) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 493 | |
| 494 | assert False, ('No profraw data file was generated, did you call ' |
| 495 | 'coverage_util::ConfigureCoverageReportPath() in test setup? ' |
| 496 | 'Please refer to base/test/test_support_ios.mm for example.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 497 | |
| 498 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 499 | def _CreateCoverageProfileDataFromTargetProfDataFiles(profdata_file_paths): |
| 500 | """Returns a relative path to coverage profdata file by merging target |
| 501 | profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 502 | |
| 503 | Args: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 504 | profdata_file_paths: A list of relative paths to the profdata data files |
| 505 | that are to be merged. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 506 | |
| 507 | Returns: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 508 | A relative path to the merged coverage profdata file. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 509 | |
| 510 | Raises: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 511 | CalledProcessError: An error occurred merging profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 512 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 513 | logging.info('Creating the coverage profile data file.') |
| 514 | logging.debug('Merging target profraw files to create target profdata file.') |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 515 | profdata_file_path = _GetProfdataFilePath() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 516 | try: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 517 | subprocess_cmd = [ |
| 518 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 519 | ] |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 520 | subprocess_cmd.extend(profdata_file_paths) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 521 | |
| 522 | output = subprocess.check_output(subprocess_cmd) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 523 | logging.debug('Merge output: %s', output) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 524 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 525 | logging.error( |
| 526 | 'Failed to merge target profdata files to create coverage profdata. %s', |
| 527 | FILE_BUG_MESSAGE) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 528 | raise error |
| 529 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 530 | logging.debug('Finished merging target profdata files.') |
| 531 | logging.info('Code coverage profile data is created as: "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 532 | profdata_file_path) |
| 533 | return profdata_file_path |
| 534 | |
| 535 | |
| 536 | def _CreateTargetProfDataFileFromProfRawFiles(target, profraw_file_paths): |
| 537 | """Returns a relative path to target profdata file by merging target |
| 538 | profraw files. |
| 539 | |
| 540 | Args: |
| 541 | profraw_file_paths: A list of relative paths to the profdata data files |
| 542 | that are to be merged. |
| 543 | |
| 544 | Returns: |
| 545 | A relative path to the merged coverage profdata file. |
| 546 | |
| 547 | Raises: |
| 548 | CalledProcessError: An error occurred merging profdata files. |
| 549 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 550 | logging.info('Creating target profile data file.') |
| 551 | logging.debug('Merging target profraw files to create target profdata file.') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 552 | profdata_file_path = os.path.join(OUTPUT_DIR, '%s.profdata' % target) |
| 553 | |
| 554 | try: |
| 555 | subprocess_cmd = [ |
| 556 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 557 | ] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 558 | subprocess_cmd.extend(profraw_file_paths) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 559 | |
| 560 | output = subprocess.check_output(subprocess_cmd) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 561 | logging.debug('Merge output: %s', output) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 562 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 563 | logging.error( |
| 564 | 'Failed to merge target profraw files to create target profdata.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 565 | raise error |
| 566 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 567 | logging.debug('Finished merging target profraw files.') |
| 568 | logging.info('Target "%s" profile data is created as: "%s".', target, |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 569 | profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 570 | return profdata_file_path |
| 571 | |
| 572 | |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 573 | def _GeneratePerFileCoverageSummary(binary_paths, profdata_file_path, filters, |
| 574 | ignore_filename_regex): |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 575 | """Generates per file coverage summary using "llvm-cov export" command.""" |
| 576 | # llvm-cov export [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 577 | # [[-object BIN]] [SOURCES]. |
| 578 | # NOTE: For object files, the first one is specified as a positional argument, |
| 579 | # and the rest are specified as keyword argument. |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 580 | logging.debug('Generating per-file code coverage summary using "llvm-cov ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 581 | 'export -summary-only" command.') |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 582 | subprocess_cmd = [ |
| 583 | LLVM_COV_PATH, 'export', '-summary-only', |
| 584 | '-instr-profile=' + profdata_file_path, binary_paths[0] |
| 585 | ] |
| 586 | subprocess_cmd.extend( |
| 587 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 588 | _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths)) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 589 | subprocess_cmd.extend(filters) |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 590 | if ignore_filename_regex: |
| 591 | subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 592 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 593 | export_output = subprocess.check_output(subprocess_cmd) |
| 594 | |
| 595 | # Write output on the disk to be used by code coverage bot. |
| 596 | with open(_GetSummaryFilePath(), 'w') as f: |
| 597 | f.write(export_output) |
| 598 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 599 | return export_output |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 600 | |
| 601 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 602 | def _AddArchArgumentForIOSIfNeeded(cmd_list, num_archs): |
| 603 | """Appends -arch arguments to the command list if it's ios platform. |
| 604 | |
| 605 | iOS binaries are universal binaries, and require specifying the architecture |
| 606 | to use, and one architecture needs to be specified for each binary. |
| 607 | """ |
| 608 | if _IsIOS(): |
| 609 | cmd_list.extend(['-arch=x86_64'] * num_archs) |
| 610 | |
| 611 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 612 | def _GetBinaryPath(command): |
| 613 | """Returns a relative path to the binary to be run by the command. |
| 614 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 615 | Currently, following types of commands are supported (e.g. url_unittests): |
| 616 | 1. Run test binary direcly: "out/coverage/url_unittests <arguments>" |
| 617 | 2. Use xvfb. |
| 618 | 2.1. "python testing/xvfb.py out/coverage/url_unittests <arguments>" |
| 619 | 2.2. "testing/xvfb.py out/coverage/url_unittests <arguments>" |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 620 | 3. Use iossim to run tests on iOS platform, please refer to testing/iossim.mm |
| 621 | for its usage. |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 622 | 3.1. "out/Coverage-iphonesimulator/iossim |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 623 | <iossim_arguments> -c <app_arguments> |
| 624 | out/Coverage-iphonesimulator/url_unittests.app" |
| 625 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 626 | Args: |
| 627 | command: A command used to run a target. |
| 628 | |
| 629 | Returns: |
| 630 | A relative path to the binary. |
| 631 | """ |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 632 | xvfb_script_name = os.extsep.join(['xvfb', 'py']) |
| 633 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 634 | command_parts = shlex.split(command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 635 | if os.path.basename(command_parts[0]) == 'python': |
| 636 | assert os.path.basename(command_parts[1]) == xvfb_script_name, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 637 | 'This tool doesn\'t understand the command: "%s".' % command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 638 | return command_parts[2] |
| 639 | |
| 640 | if os.path.basename(command_parts[0]) == xvfb_script_name: |
| 641 | return command_parts[1] |
| 642 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 643 | if _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 644 | # For a given application bundle, the binary resides in the bundle and has |
| 645 | # the same name with the application without the .app extension. |
Artem Titarenko | 2b46495 | 2018-11-07 17:22:02 | [diff] [blame] | 646 | app_path = command_parts[1].rstrip(os.path.sep) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 647 | app_name = os.path.splitext(os.path.basename(app_path))[0] |
| 648 | return os.path.join(app_path, app_name) |
| 649 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 650 | return command_parts[0] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 651 | |
| 652 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 653 | def _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 654 | """Returns true if command is used to run tests on iOS platform.""" |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 655 | return os.path.basename(shlex.split(command)[0]) == 'iossim' |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 656 | |
| 657 | |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 658 | def _VerifyTargetExecutablesAreInBuildDirectory(commands): |
| 659 | """Verifies that the target executables specified in the commands are inside |
| 660 | the given build directory.""" |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 661 | for command in commands: |
| 662 | binary_path = _GetBinaryPath(command) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 663 | binary_absolute_path = coverage_utils.GetFullPath(binary_path) |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 664 | assert binary_absolute_path.startswith(BUILD_DIR + os.sep), ( |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 665 | 'Target executable "%s" in command: "%s" is outside of ' |
| 666 | 'the given build directory: "%s".' % (binary_path, command, BUILD_DIR)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 667 | |
| 668 | |
| 669 | def _ValidateBuildingWithClangCoverage(): |
| 670 | """Asserts that targets are built with Clang coverage enabled.""" |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 671 | build_args = _GetBuildArgs() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 672 | |
| 673 | if (CLANG_COVERAGE_BUILD_ARG not in build_args or |
| 674 | build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'): |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 675 | assert False, ('\'{} = true\' is required in args.gn.' |
| 676 | ).format(CLANG_COVERAGE_BUILD_ARG) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 677 | |
| 678 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 679 | def _ValidateCurrentPlatformIsSupported(): |
| 680 | """Asserts that this script suports running on the current platform""" |
| 681 | target_os = _GetTargetOS() |
| 682 | if target_os: |
| 683 | current_platform = target_os |
| 684 | else: |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 685 | current_platform = coverage_utils.GetHostPlatform() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 686 | |
| 687 | assert current_platform in [ |
| 688 | 'linux', 'mac', 'chromeos', 'ios' |
| 689 | ], ('Coverage is only supported on linux, mac, chromeos and ios.') |
| 690 | |
| 691 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 692 | def _GetBuildArgs(): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 693 | """Parses args.gn file and returns results as a dictionary. |
| 694 | |
| 695 | Returns: |
| 696 | A dictionary representing the build args. |
| 697 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 698 | global _BUILD_ARGS |
| 699 | if _BUILD_ARGS is not None: |
| 700 | return _BUILD_ARGS |
| 701 | |
| 702 | _BUILD_ARGS = {} |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 703 | build_args_path = os.path.join(BUILD_DIR, 'args.gn') |
| 704 | assert os.path.exists(build_args_path), ('"%s" is not a build directory, ' |
| 705 | 'missing args.gn file.' % BUILD_DIR) |
| 706 | with open(build_args_path) as build_args_file: |
| 707 | build_args_lines = build_args_file.readlines() |
| 708 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 709 | for build_arg_line in build_args_lines: |
| 710 | build_arg_without_comments = build_arg_line.split('#')[0] |
| 711 | key_value_pair = build_arg_without_comments.split('=') |
| 712 | if len(key_value_pair) != 2: |
| 713 | continue |
| 714 | |
| 715 | key = key_value_pair[0].strip() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 716 | |
| 717 | # Values are wrapped within a pair of double-quotes, so remove the leading |
| 718 | # and trailing double-quotes. |
| 719 | value = key_value_pair[1].strip().strip('"') |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 720 | _BUILD_ARGS[key] = value |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 721 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 722 | return _BUILD_ARGS |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 723 | |
| 724 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 725 | def _VerifyPathsAndReturnAbsolutes(paths): |
| 726 | """Verifies that the paths specified in |paths| exist and returns absolute |
| 727 | versions. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 728 | |
| 729 | Args: |
| 730 | paths: A list of files or directories. |
| 731 | """ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 732 | absolute_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 733 | for path in paths: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 734 | absolute_path = os.path.join(SRC_ROOT_PATH, path) |
| 735 | assert os.path.exists(absolute_path), ('Path: "%s" doesn\'t exist.' % path) |
| 736 | |
| 737 | absolute_paths.append(absolute_path) |
| 738 | |
| 739 | return absolute_paths |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 740 | |
| 741 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 742 | def _GetBinaryPathsFromTargets(targets, build_dir): |
| 743 | """Return binary paths from target names.""" |
| 744 | # FIXME: Derive output binary from target build definitions rather than |
| 745 | # assuming that it is always the same name. |
| 746 | binary_paths = [] |
| 747 | for target in targets: |
| 748 | binary_path = os.path.join(build_dir, target) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 749 | if coverage_utils.GetHostPlatform() == 'win': |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 750 | binary_path += '.exe' |
| 751 | |
| 752 | if os.path.exists(binary_path): |
| 753 | binary_paths.append(binary_path) |
| 754 | else: |
| 755 | logging.warning( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 756 | 'Target binary "%s" not found in build directory, skipping.', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 757 | os.path.basename(binary_path)) |
| 758 | |
| 759 | return binary_paths |
| 760 | |
| 761 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 762 | def _GetCommandForWebTests(arguments): |
| 763 | """Return command to run for blink web tests.""" |
| 764 | command_list = [ |
| 765 | 'python', 'testing/xvfb.py', 'python', |
| 766 | 'third_party/blink/tools/run_web_tests.py', |
| 767 | '--additional-driver-flag=--no-sandbox', |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 768 | '--additional-env-var=LLVM_PROFILE_FILE=%s' % |
Robert Ma | 339fc47 | 2018-12-14 21:22:42 | [diff] [blame] | 769 | LLVM_PROFILE_FILE_PATH_SUBSTITUTION, |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 770 | '--child-processes=%d' % max(1, int(multiprocessing.cpu_count() / 2)), |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 771 | '--disable-breakpad', '--no-show-results', '--skip-failing-tests', |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 772 | '--target=%s' % os.path.basename(BUILD_DIR), '--time-out-ms=30000' |
| 773 | ] |
| 774 | if arguments.strip(): |
| 775 | command_list.append(arguments) |
| 776 | return ' '.join(command_list) |
| 777 | |
| 778 | |
| 779 | def _GetBinaryPathForWebTests(): |
| 780 | """Return binary path used to run blink web tests.""" |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 781 | host_platform = coverage_utils.GetHostPlatform() |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 782 | if host_platform == 'win': |
| 783 | return os.path.join(BUILD_DIR, 'content_shell.exe') |
| 784 | elif host_platform == 'linux': |
| 785 | return os.path.join(BUILD_DIR, 'content_shell') |
| 786 | elif host_platform == 'mac': |
| 787 | return os.path.join(BUILD_DIR, 'Content Shell.app', 'Contents', 'MacOS', |
| 788 | 'Content Shell') |
| 789 | else: |
| 790 | assert False, 'This platform is not supported for web tests.' |
| 791 | |
| 792 | |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 793 | def _SetupOutputDir(): |
| 794 | """Setup output directory.""" |
| 795 | if os.path.exists(OUTPUT_DIR): |
| 796 | shutil.rmtree(OUTPUT_DIR) |
| 797 | |
| 798 | # Creates |OUTPUT_DIR| and its platform sub-directory. |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 799 | os.makedirs(coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR)) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 800 | |
| 801 | |
Yuke Liao | abfbba4 | 2019-06-11 16:03:59 | [diff] [blame] | 802 | def _SetMacXcodePath(): |
| 803 | """Set DEVELOPER_DIR to the path to hermetic Xcode.app on Mac OS X.""" |
| 804 | if sys.platform != 'darwin': |
| 805 | return |
| 806 | |
| 807 | xcode_path = os.path.join(SRC_ROOT_PATH, 'build', 'mac_files', 'Xcode.app') |
| 808 | if os.path.exists(xcode_path): |
| 809 | os.environ['DEVELOPER_DIR'] = xcode_path |
| 810 | |
| 811 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 812 | def _ParseCommandArguments(): |
| 813 | """Adds and parses relevant arguments for tool comands. |
| 814 | |
| 815 | Returns: |
| 816 | A dictionary representing the arguments. |
| 817 | """ |
| 818 | arg_parser = argparse.ArgumentParser() |
| 819 | arg_parser.usage = __doc__ |
| 820 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 821 | arg_parser.add_argument( |
| 822 | '-b', |
| 823 | '--build-dir', |
| 824 | type=str, |
| 825 | required=True, |
| 826 | help='The build directory, the path needs to be relative to the root of ' |
| 827 | 'the checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 828 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 829 | arg_parser.add_argument( |
| 830 | '-o', |
| 831 | '--output-dir', |
| 832 | type=str, |
| 833 | required=True, |
| 834 | help='Output directory for generated artifacts.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 835 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 836 | arg_parser.add_argument( |
| 837 | '-c', |
| 838 | '--command', |
| 839 | action='append', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 840 | required=False, |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 841 | help='Commands used to run test targets, one test target needs one and ' |
| 842 | 'only one command, when specifying commands, one should assume the ' |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 843 | 'current working directory is the root of the checkout. This option is ' |
| 844 | 'incompatible with -p/--profdata-file option.') |
| 845 | |
| 846 | arg_parser.add_argument( |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 847 | '-wt', |
| 848 | '--web-tests', |
| 849 | nargs='?', |
| 850 | type=str, |
| 851 | const=' ', |
| 852 | required=False, |
| 853 | help='Run blink web tests. Support passing arguments to run_web_tests.py') |
| 854 | |
| 855 | arg_parser.add_argument( |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 856 | '-p', |
| 857 | '--profdata-file', |
| 858 | type=str, |
| 859 | required=False, |
| 860 | help='Path to profdata file to use for generating code coverage reports. ' |
| 861 | 'This can be useful if you generated the profdata file seperately in ' |
| 862 | 'your own test harness. This option is ignored if run command(s) are ' |
| 863 | 'already provided above using -c/--command option.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 864 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 865 | arg_parser.add_argument( |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 866 | '-f', |
| 867 | '--filters', |
| 868 | action='append', |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 869 | required=False, |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 870 | help='Directories or files to get code coverage for, and all files under ' |
| 871 | 'the directories are included recursively.') |
| 872 | |
| 873 | arg_parser.add_argument( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 874 | '-i', |
| 875 | '--ignore-filename-regex', |
| 876 | type=str, |
| 877 | help='Skip source code files with file paths that match the given ' |
| 878 | 'regular expression. For example, use -i=\'.*/out/.*|.*/third_party/.*\' ' |
| 879 | 'to exclude files in third_party/ and out/ folders from the report.') |
| 880 | |
| 881 | arg_parser.add_argument( |
Yuke Liao | 1b852fd | 2018-05-11 17:07:32 | [diff] [blame] | 882 | '--no-file-view', |
| 883 | action='store_true', |
| 884 | help='Don\'t generate the file view in the coverage report. When there ' |
| 885 | 'are large number of html files, the file view becomes heavy and may ' |
| 886 | 'cause the browser to freeze, and this argument comes handy.') |
| 887 | |
| 888 | arg_parser.add_argument( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 889 | '--no-component-view', |
| 890 | action='store_true', |
| 891 | help='Don\'t generate the component view in the coverage report.') |
| 892 | |
| 893 | arg_parser.add_argument( |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 894 | '--coverage-tools-dir', |
| 895 | type=str, |
| 896 | help='Path of the directory where LLVM coverage tools (llvm-cov, ' |
| 897 | 'llvm-profdata) exist. This should be only needed if you are testing ' |
| 898 | 'against a custom built clang revision. Otherwise, we pick coverage ' |
| 899 | 'tools automatically from your current source checkout.') |
| 900 | |
| 901 | arg_parser.add_argument( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 902 | '-j', |
| 903 | '--jobs', |
| 904 | type=int, |
| 905 | default=None, |
| 906 | help='Run N jobs to build in parallel. If not specified, a default value ' |
Max Moroz | 0657629 | 2019-01-03 19:22:52 | [diff] [blame] | 907 | 'will be derived based on CPUs and goma availability. Please refer to ' |
| 908 | '\'autoninja -h\' for more details.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 909 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 910 | arg_parser.add_argument( |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 911 | '-v', |
| 912 | '--verbose', |
| 913 | action='store_true', |
| 914 | help='Prints additional output for diagnostics.') |
| 915 | |
| 916 | arg_parser.add_argument( |
| 917 | '-l', '--log_file', type=str, help='Redirects logs to a file.') |
| 918 | |
| 919 | arg_parser.add_argument( |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 920 | 'targets', |
| 921 | nargs='+', |
| 922 | help='The names of the test targets to run. If multiple run commands are ' |
| 923 | 'specified using the -c/--command option, then the order of targets and ' |
| 924 | 'commands must match, otherwise coverage generation will fail.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 925 | |
| 926 | args = arg_parser.parse_args() |
| 927 | return args |
| 928 | |
| 929 | |
| 930 | def Main(): |
| 931 | """Execute tool commands.""" |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 932 | # Setup coverage binaries even when script is called with empty params. This |
| 933 | # is used by coverage bot for initial setup. |
| 934 | if len(sys.argv) == 1: |
Roberto Carrillo | 3631272 | 2018-10-17 02:18:45 | [diff] [blame] | 935 | update_clang_coverage_tools.DownloadCoverageToolsIfNeeded() |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 936 | print(__doc__) |
| 937 | return |
| 938 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 939 | # Change directory to source root to aid in relative paths calculations. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 940 | global SRC_ROOT_PATH |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 941 | SRC_ROOT_PATH = coverage_utils.GetFullPath( |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 942 | os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir)) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 943 | os.chdir(SRC_ROOT_PATH) |
Abhishek Arya | 8a0751a | 2018-05-03 18:53:11 | [diff] [blame] | 944 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 945 | args = _ParseCommandArguments() |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 946 | coverage_utils.ConfigureLogging(verbose=args.verbose, log_file=args.log_file) |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 947 | _ConfigureLLVMCoverageTools(args) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 948 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 949 | global BUILD_DIR |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 950 | BUILD_DIR = coverage_utils.GetFullPath(args.build_dir) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 951 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 952 | global OUTPUT_DIR |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 953 | OUTPUT_DIR = coverage_utils.GetFullPath(args.output_dir) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 954 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 955 | assert args.web_tests or args.command or args.profdata_file, ( |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 956 | 'Need to either provide commands to run using -c/--command option OR ' |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 957 | 'provide prof-data file as input using -p/--profdata-file option OR ' |
| 958 | 'run web tests using -wt/--run-web-tests.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 959 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 960 | assert not args.command or (len(args.targets) == len(args.command)), ( |
| 961 | 'Number of targets must be equal to the number of test commands.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 962 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 963 | assert os.path.exists(BUILD_DIR), ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 964 | 'Build directory: "%s" doesn\'t exist. ' |
| 965 | 'Please run "gn gen" to generate.' % BUILD_DIR) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 966 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 967 | _ValidateCurrentPlatformIsSupported() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 968 | _ValidateBuildingWithClangCoverage() |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 969 | |
| 970 | absolute_filter_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 971 | if args.filters: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 972 | absolute_filter_paths = _VerifyPathsAndReturnAbsolutes(args.filters) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 973 | |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 974 | _SetupOutputDir() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 975 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 976 | # Get .profdata file and list of binary paths. |
| 977 | if args.web_tests: |
| 978 | commands = [_GetCommandForWebTests(args.web_tests)] |
| 979 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 980 | args.targets, commands, args.jobs) |
| 981 | binary_paths = [_GetBinaryPathForWebTests()] |
| 982 | elif args.command: |
| 983 | for i in range(len(args.command)): |
| 984 | assert not 'run_web_tests.py' in args.command[i], ( |
| 985 | 'run_web_tests.py is not supported via --command argument. ' |
| 986 | 'Please use --run-web-tests argument instead.') |
| 987 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 988 | # A list of commands are provided. Run them to generate profdata file, and |
| 989 | # create a list of binary paths from parsing commands. |
| 990 | _VerifyTargetExecutablesAreInBuildDirectory(args.command) |
| 991 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 992 | args.targets, args.command, args.jobs) |
| 993 | binary_paths = [_GetBinaryPath(command) for command in args.command] |
| 994 | else: |
| 995 | # An input prof-data file is already provided. Just calculate binary paths. |
| 996 | profdata_file_path = args.profdata_file |
| 997 | binary_paths = _GetBinaryPathsFromTargets(args.targets, args.build_dir) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 998 | |
Erik Chen | 283b92c7 | 2019-07-22 16:37:39 | [diff] [blame^] | 999 | # If the checkout uses the hermetic xcode binaries, then otool must be |
| 1000 | # directly invoked. The indirection via /usr/bin/otool won't work unless |
| 1001 | # there's an actual system install of Xcode. |
| 1002 | otool_path = None |
| 1003 | if sys.platform == 'darwin': |
| 1004 | hermetic_otool_path = os.path.join( |
| 1005 | SRC_ROOT_PATH, 'build', 'mac_files', 'xcode_binaries', 'Contents', |
| 1006 | 'Developer', 'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', |
| 1007 | 'otool') |
| 1008 | if os.path.exists(hermetic_otool_path): |
| 1009 | otool_path = hermetic_otool_path |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1010 | binary_paths.extend( |
Erik Chen | 283b92c7 | 2019-07-22 16:37:39 | [diff] [blame^] | 1011 | coverage_utils.GetSharedLibraries(binary_paths, BUILD_DIR, otool_path)) |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 1012 | |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1013 | logging.info('Generating code coverage report in html (this can take a while ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1014 | 'depending on size of target!).') |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1015 | per_file_summary_data = _GeneratePerFileCoverageSummary( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1016 | binary_paths, profdata_file_path, absolute_filter_paths, |
| 1017 | args.ignore_filename_regex) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1018 | _GeneratePerFileLineByLineCoverageInHtml(binary_paths, profdata_file_path, |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1019 | absolute_filter_paths, |
| 1020 | args.ignore_filename_regex) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1021 | component_mappings = None |
| 1022 | if not args.no_component_view: |
| 1023 | component_mappings = json.load(urllib2.urlopen(COMPONENT_MAPPING_URL)) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1024 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1025 | # Call prepare here. |
| 1026 | processor = coverage_utils.CoverageReportPostProcessor( |
| 1027 | OUTPUT_DIR, |
| 1028 | SRC_ROOT_PATH, |
| 1029 | per_file_summary_data, |
| 1030 | no_component_view=args.no_component_view, |
| 1031 | no_file_view=args.no_file_view, |
| 1032 | component_mappings=component_mappings) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1033 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1034 | processor.PrepareHtmlReport() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1035 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1036 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1037 | if __name__ == '__main__': |
| 1038 | sys.exit(Main()) |