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