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