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 Eubanks | 97d1d4b | 2023-08-16 03:57:43 | [diff] [blame] | 366 | subprocess.check_call(subprocess_cmd, shell=os.name == 'nt') |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 367 | logging.debug('Finished building %s.', str(targets)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 368 | |
| 369 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 370 | def _GetTargetProfDataPathsByExecutingCommands(targets, commands): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 371 | """Runs commands and returns the relative paths to the profraw data files. |
| 372 | |
| 373 | Args: |
| 374 | targets: A list of targets built with coverage instrumentation. |
| 375 | commands: A list of commands used to run the targets. |
| 376 | |
| 377 | Returns: |
| 378 | A list of relative paths to the generated profraw data files. |
| 379 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 380 | logging.debug('Executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 381 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 382 | # Remove existing profraw data files. |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 383 | report_root_dir = coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR) |
| 384 | for file_or_dir in os.listdir(report_root_dir): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 385 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 386 | os.remove(os.path.join(report_root_dir, file_or_dir)) |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 387 | |
| 388 | # Ensure that logs directory exists. |
| 389 | if not os.path.exists(_GetLogsDirectoryPath()): |
| 390 | os.makedirs(_GetLogsDirectoryPath()) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 391 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 392 | profdata_file_paths = [] |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 393 | |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 394 | # Run all test targets to generate profraw data files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 395 | for target, command in zip(targets, commands): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 396 | output_file_name = os.extsep.join([target + '_output', 'log']) |
| 397 | output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 398 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 399 | profdata_file_path = None |
Prakhar | 65d6383 | 2021-06-16 23:01:37 | [diff] [blame] | 400 | for _ in range(MERGE_RETRIES): |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 401 | logging.info('Running command: "%s", the output is redirected to "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 402 | command, output_file_path) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 403 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 404 | if _IsIOSCommand(command): |
| 405 | # On iOS platform, due to lack of write permissions, profraw files are |
| 406 | # generated outside of the OUTPUT_DIR, and the exact paths are contained |
| 407 | # in the output of the command execution. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 408 | output = _ExecuteIOSCommand(command, output_file_path) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 409 | else: |
| 410 | # On other platforms, profraw files are generated inside the OUTPUT_DIR. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 411 | output = _ExecuteCommand(target, command, output_file_path) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 412 | |
| 413 | profraw_file_paths = [] |
| 414 | if _IsIOS(): |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 415 | profraw_file_paths = [_GetProfrawDataFileByParsingOutput(output)] |
Ben Joyce | 8828236 | 2021-01-29 23:53:31 | [diff] [blame] | 416 | elif _IsAndroid(): |
| 417 | android_coverage_dir = os.path.join(BUILD_DIR, 'coverage') |
| 418 | for r, _, files in os.walk(android_coverage_dir): |
| 419 | for f in files: |
| 420 | if f.endswith(PROFRAW_FILE_EXTENSION): |
| 421 | profraw_file_paths.append(os.path.join(r, f)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 422 | else: |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 423 | for file_or_dir in os.listdir(report_root_dir): |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 424 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 425 | profraw_file_paths.append( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 426 | os.path.join(report_root_dir, file_or_dir)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 427 | |
| 428 | assert profraw_file_paths, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 429 | 'Running target "%s" failed to generate any profraw data file, ' |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 430 | 'please make sure the binary exists, is properly instrumented and ' |
| 431 | 'does not crash. %s' % (target, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 432 | |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 433 | assert isinstance(profraw_file_paths, list), ( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 434 | 'Variable \'profraw_file_paths\' is expected to be of type \'list\', ' |
| 435 | 'but it is a %s. %s' % (type(profraw_file_paths), FILE_BUG_MESSAGE)) |
Yuke Liao | 9c2c70b | 2018-05-23 15:37:57 | [diff] [blame] | 436 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 437 | try: |
| 438 | profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles( |
| 439 | target, profraw_file_paths) |
| 440 | break |
| 441 | except Exception: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 442 | logging.info('Retrying...') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 443 | finally: |
| 444 | # Remove profraw files now so that they are not used in next iteration. |
| 445 | for profraw_file_path in profraw_file_paths: |
| 446 | os.remove(profraw_file_path) |
| 447 | |
| 448 | assert profdata_file_path, ( |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 449 | 'Failed to merge target "%s" profraw files after %d retries. %s' % |
| 450 | (target, MERGE_RETRIES, FILE_BUG_MESSAGE)) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 451 | profdata_file_paths.append(profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 452 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 453 | logging.debug('Finished executing the test commands.') |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 454 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 455 | return profdata_file_paths |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 456 | |
| 457 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 458 | def _GetEnvironmentVars(profraw_file_path): |
| 459 | """Return environment vars for subprocess, given a profraw file path.""" |
| 460 | env = os.environ.copy() |
| 461 | env.update({ |
| 462 | 'LLVM_PROFILE_FILE': profraw_file_path, |
| 463 | 'PATH': _GetPathWithLLVMSymbolizerDir() |
| 464 | }) |
| 465 | return env |
| 466 | |
| 467 | |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 468 | def _SplitCommand(command): |
| 469 | """Split a command string into parts in a platform-specific way.""" |
| 470 | if coverage_utils.GetHostPlatform() == 'win': |
| 471 | return command.split() |
Julia Hansbrough | 58aa7b0a | 2023-01-17 21:08:41 | [diff] [blame] | 472 | split_command = shlex.split(command) |
| 473 | # Python's subprocess does not do glob expansion, so we expand it out here. |
| 474 | new_command = [] |
| 475 | for item in split_command: |
| 476 | if '*' in item: |
| 477 | files = glob.glob(item) |
| 478 | for file in files: |
| 479 | new_command.append(file) |
| 480 | else: |
| 481 | new_command.append(item) |
| 482 | return new_command |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 483 | |
| 484 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 485 | def _ExecuteCommand(target, command, output_file_path): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 486 | """Runs a single command and generates a profraw data file.""" |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 487 | # Per Clang "Source-based Code Coverage" doc: |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 488 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 489 | # "%p" expands out to the process ID. It's not used by this scripts due to: |
| 490 | # 1) If a target program spawns too many processess, it may exhaust all disk |
| 491 | # space available. For example, unit_tests writes thousands of .profraw |
| 492 | # files each of size 1GB+. |
| 493 | # 2) If a target binary uses shared libraries, coverage profile data for them |
| 494 | # will be missing, resulting in incomplete coverage reports. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 495 | # |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 496 | # "%Nm" expands out to the instrumented binary's signature. When this pattern |
| 497 | # is specified, the runtime creates a pool of N raw profiles which are used |
| 498 | # for on-line profile merging. The runtime takes care of selecting a raw |
| 499 | # profile from the pool, locking it, and updating it before the program exits. |
Yuke Liao | d4a986520 | 2018-01-12 23:17:52 | [diff] [blame] | 500 | # N must be between 1 and 9. The merge pool specifier can only occur once per |
| 501 | # filename pattern. |
| 502 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 503 | # "%1m" is used when tests run in single process, such as fuzz targets. |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 504 | # |
Max Moroz | d73e45f | 2018-04-24 18:32:47 | [diff] [blame] | 505 | # For other cases, "%4m" is chosen as it creates some level of parallelism, |
| 506 | # but it's not too big to consume too much computing resource or disk space. |
| 507 | profile_pattern_string = '%1m' if _IsFuzzerTarget(target) else '%4m' |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 508 | expected_profraw_file_name = os.extsep.join( |
Nico Weber | 51e61c7d | 2023-12-12 17:32:46 | [diff] [blame^] | 509 | [target, profile_pattern_string, PROFRAW_FILE_EXTENSION]) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 510 | expected_profraw_file_path = os.path.join( |
| 511 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), |
| 512 | expected_profraw_file_name) |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 513 | command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION, |
| 514 | expected_profraw_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 515 | |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 516 | try: |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 517 | # Some fuzz targets or tests may write into stderr, redirect it as well. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 518 | with open(output_file_path, 'wb') as output_file_handle: |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 519 | subprocess.check_call(_SplitCommand(command), |
| 520 | stdout=output_file_handle, |
| 521 | stderr=subprocess.STDOUT, |
| 522 | env=_GetEnvironmentVars(expected_profraw_file_path)) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 523 | except subprocess.CalledProcessError as e: |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 524 | logging.warning('Command: "%s" exited with non-zero return code.', command) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 525 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 526 | return open(output_file_path, 'rb').read() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 527 | |
| 528 | |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 529 | def _IsFuzzerTarget(target): |
| 530 | """Returns true if the target is a fuzzer target.""" |
| 531 | build_args = _GetBuildArgs() |
| 532 | use_libfuzzer = ('use_libfuzzer' in build_args and |
| 533 | build_args['use_libfuzzer'] == 'true') |
Adrian Taylor | 9470000f | 2023-03-10 16:18:25 | [diff] [blame] | 534 | use_centipede = ('use_centipede' in build_args |
| 535 | and build_args['use_centipede'] == 'true') |
| 536 | return (use_libfuzzer or use_centipede) and target.endswith('_fuzzer') |
Yuke Liao | 27349c9 | 2018-03-22 21:10:01 | [diff] [blame] | 537 | |
| 538 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 539 | def _ExecuteIOSCommand(command, output_file_path): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 540 | """Runs a single iOS command and generates a profraw data file. |
| 541 | |
| 542 | iOS application doesn't have write access to folders outside of the app, so |
| 543 | it's impossible to instruct the app to flush the profraw data file to the |
| 544 | desired location. The profraw data file will be generated somewhere within the |
| 545 | application's Documents folder, and the full path can be obtained by parsing |
| 546 | the output. |
| 547 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 548 | assert _IsIOSCommand(command) |
| 549 | |
| 550 | # After running tests, iossim generates a profraw data file, it won't be |
| 551 | # needed anyway, so dump it into the OUTPUT_DIR to avoid polluting the |
| 552 | # checkout. |
| 553 | iossim_profraw_file_path = os.path.join( |
| 554 | OUTPUT_DIR, os.extsep.join(['iossim', PROFRAW_FILE_EXTENSION])) |
Abhishek Arya | bd0655d | 2018-05-21 19:55:24 | [diff] [blame] | 555 | command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION, |
| 556 | iossim_profraw_file_path) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 557 | |
| 558 | try: |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 559 | with open(output_file_path, 'wb') as output_file_handle: |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 560 | subprocess.check_call(_SplitCommand(command), |
| 561 | stdout=output_file_handle, |
| 562 | stderr=subprocess.STDOUT, |
| 563 | env=_GetEnvironmentVars(iossim_profraw_file_path)) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 564 | except subprocess.CalledProcessError as e: |
| 565 | # iossim emits non-zero return code even if tests run successfully, so |
| 566 | # ignore the return code. |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 567 | pass |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 568 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 569 | return open(output_file_path, 'rb').read() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 570 | |
| 571 | |
| 572 | def _GetProfrawDataFileByParsingOutput(output): |
| 573 | """Returns the path to the profraw data file obtained by parsing the output. |
| 574 | |
| 575 | The output of running the test target has no format, but it is guaranteed to |
| 576 | have a single line containing the path to the generated profraw data file. |
| 577 | NOTE: This should only be called when target os is iOS. |
| 578 | """ |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 579 | assert _IsIOS() |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 580 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 581 | output_by_lines = ''.join(output).splitlines() |
| 582 | profraw_file_pattern = re.compile('.*Coverage data at (.*coverage\.profraw).') |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 583 | |
| 584 | for line in output_by_lines: |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 585 | result = profraw_file_pattern.match(line) |
| 586 | if result: |
| 587 | return result.group(1) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 588 | |
| 589 | assert False, ('No profraw data file was generated, did you call ' |
| 590 | 'coverage_util::ConfigureCoverageReportPath() in test setup? ' |
| 591 | 'Please refer to base/test/test_support_ios.mm for example.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 592 | |
| 593 | |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 594 | def _CreateCoverageProfileDataFromTargetProfDataFiles(profdata_file_paths): |
| 595 | """Returns a relative path to coverage profdata file by merging target |
| 596 | profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 597 | |
| 598 | Args: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 599 | profdata_file_paths: A list of relative paths to the profdata data files |
| 600 | that are to be merged. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 601 | |
| 602 | Returns: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 603 | A relative path to the merged coverage profdata file. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 604 | |
| 605 | Raises: |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 606 | CalledProcessError: An error occurred merging profdata files. |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 607 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 608 | logging.info('Creating the coverage profile data file.') |
| 609 | logging.debug('Merging target profraw files to create target profdata file.') |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 610 | profdata_file_path = _GetProfdataFilePath() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 611 | try: |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 612 | subprocess_cmd = [ |
| 613 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 614 | ] |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 615 | subprocess_cmd.extend(profdata_file_paths) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 616 | |
| 617 | output = subprocess.check_output(subprocess_cmd) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 618 | logging.debug('Merge output: %s', output) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 619 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 620 | logging.error( |
| 621 | 'Failed to merge target profdata files to create coverage profdata. %s', |
| 622 | FILE_BUG_MESSAGE) |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 623 | raise error |
| 624 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 625 | logging.debug('Finished merging target profdata files.') |
| 626 | logging.info('Code coverage profile data is created as: "%s".', |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 627 | profdata_file_path) |
| 628 | return profdata_file_path |
| 629 | |
| 630 | |
| 631 | def _CreateTargetProfDataFileFromProfRawFiles(target, profraw_file_paths): |
| 632 | """Returns a relative path to target profdata file by merging target |
| 633 | profraw files. |
| 634 | |
| 635 | Args: |
| 636 | profraw_file_paths: A list of relative paths to the profdata data files |
| 637 | that are to be merged. |
| 638 | |
| 639 | Returns: |
| 640 | A relative path to the merged coverage profdata file. |
| 641 | |
| 642 | Raises: |
| 643 | CalledProcessError: An error occurred merging profdata files. |
| 644 | """ |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 645 | logging.info('Creating target profile data file.') |
| 646 | logging.debug('Merging target profraw files to create target profdata file.') |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 647 | profdata_file_path = os.path.join(OUTPUT_DIR, '%s.profdata' % target) |
| 648 | |
| 649 | try: |
| 650 | subprocess_cmd = [ |
| 651 | LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true' |
| 652 | ] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 653 | subprocess_cmd.extend(profraw_file_paths) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 654 | output = subprocess.check_output(subprocess_cmd) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 655 | logging.debug('Merge output: %s', output) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 656 | except subprocess.CalledProcessError as error: |
Abhishek Arya | d35de7e | 2018-05-10 22:23:04 | [diff] [blame] | 657 | logging.error( |
| 658 | 'Failed to merge target profraw files to create target profdata.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 659 | raise error |
| 660 | |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 661 | logging.debug('Finished merging target profraw files.') |
| 662 | logging.info('Target "%s" profile data is created as: "%s".', target, |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 663 | profdata_file_path) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 664 | return profdata_file_path |
| 665 | |
| 666 | |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 667 | def _GeneratePerFileCoverageSummary(binary_paths, profdata_file_path, filters, |
| 668 | ignore_filename_regex): |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 669 | """Generates per file coverage summary using "llvm-cov export" command.""" |
| 670 | # llvm-cov export [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 671 | # [[-object BIN]] [SOURCES]. |
| 672 | # NOTE: For object files, the first one is specified as a positional argument, |
| 673 | # and the rest are specified as keyword argument. |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 674 | logging.debug('Generating per-file code coverage summary using "llvm-cov ' |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 675 | 'export -summary-only" command.') |
Sajjad Mirza | 07f5233 | 2020-11-11 01:50:47 | [diff] [blame] | 676 | for path in binary_paths: |
| 677 | if not os.path.exists(path): |
| 678 | logging.error("Binary %s does not exist", path) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 679 | subprocess_cmd = [ |
| 680 | LLVM_COV_PATH, 'export', '-summary-only', |
Choongwoo Han | 5675252 | 2021-06-10 17:38:34 | [diff] [blame] | 681 | '-compilation-dir={}'.format(BUILD_DIR), |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 682 | '-instr-profile=' + profdata_file_path, binary_paths[0] |
| 683 | ] |
| 684 | subprocess_cmd.extend( |
| 685 | ['-object=' + binary_path for binary_path in binary_paths[1:]]) |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 686 | _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths)) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 687 | subprocess_cmd.extend(filters) |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 688 | if ignore_filename_regex: |
| 689 | subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 690 | |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 691 | export_output = subprocess.check_output(subprocess_cmd) |
| 692 | |
| 693 | # Write output on the disk to be used by code coverage bot. |
Prakhar | 65d6383 | 2021-06-16 23:01:37 | [diff] [blame] | 694 | with open(_GetSummaryFilePath(), 'wb') as f: |
Max Moroz | 7c5354f | 2018-05-06 00:03:48 | [diff] [blame] | 695 | f.write(export_output) |
| 696 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 697 | return export_output |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 698 | |
| 699 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 700 | def _AddArchArgumentForIOSIfNeeded(cmd_list, num_archs): |
| 701 | """Appends -arch arguments to the command list if it's ios platform. |
| 702 | |
| 703 | iOS binaries are universal binaries, and require specifying the architecture |
| 704 | to use, and one architecture needs to be specified for each binary. |
| 705 | """ |
| 706 | if _IsIOS(): |
| 707 | cmd_list.extend(['-arch=x86_64'] * num_archs) |
| 708 | |
| 709 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 710 | def _GetBinaryPath(command): |
| 711 | """Returns a relative path to the binary to be run by the command. |
| 712 | |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 713 | Currently, following types of commands are supported (e.g. url_unittests): |
| 714 | 1. Run test binary direcly: "out/coverage/url_unittests <arguments>" |
| 715 | 2. Use xvfb. |
| 716 | 2.1. "python testing/xvfb.py out/coverage/url_unittests <arguments>" |
| 717 | 2.2. "testing/xvfb.py out/coverage/url_unittests <arguments>" |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 718 | 3. Use iossim to run tests on iOS platform, please refer to testing/iossim.mm |
| 719 | for its usage. |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 720 | 3.1. "out/Coverage-iphonesimulator/iossim |
Yuke Liao | 92107f0 | 2018-03-07 01:44:37 | [diff] [blame] | 721 | <iossim_arguments> -c <app_arguments> |
| 722 | out/Coverage-iphonesimulator/url_unittests.app" |
| 723 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 724 | Args: |
| 725 | command: A command used to run a target. |
| 726 | |
| 727 | Returns: |
| 728 | A relative path to the binary. |
| 729 | """ |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 730 | xvfb_script_name = os.extsep.join(['xvfb', 'py']) |
| 731 | |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 732 | command_parts = _SplitCommand(command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 733 | if os.path.basename(command_parts[0]) == 'python': |
| 734 | assert os.path.basename(command_parts[1]) == xvfb_script_name, ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 735 | 'This tool doesn\'t understand the command: "%s".' % command) |
Yuke Liao | 545db32 | 2018-02-15 17:12:01 | [diff] [blame] | 736 | return command_parts[2] |
| 737 | |
| 738 | if os.path.basename(command_parts[0]) == xvfb_script_name: |
| 739 | return command_parts[1] |
| 740 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 741 | if _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 742 | # For a given application bundle, the binary resides in the bundle and has |
| 743 | # the same name with the application without the .app extension. |
Artem Titarenko | 2b46495 | 2018-11-07 17:22:02 | [diff] [blame] | 744 | app_path = command_parts[1].rstrip(os.path.sep) |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 745 | app_name = os.path.splitext(os.path.basename(app_path))[0] |
| 746 | return os.path.join(app_path, app_name) |
| 747 | |
Sajjad Mirza | 07f5233 | 2020-11-11 01:50:47 | [diff] [blame] | 748 | if coverage_utils.GetHostPlatform() == 'win' \ |
| 749 | and not command_parts[0].endswith('.exe'): |
| 750 | return command_parts[0] + '.exe' |
| 751 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 752 | return command_parts[0] |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 753 | |
| 754 | |
Yuke Liao | b292683 | 2018-03-02 17:34:29 | [diff] [blame] | 755 | def _IsIOSCommand(command): |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 756 | """Returns true if command is used to run tests on iOS platform.""" |
Sajjad Mirza | 0b96e00 | 2020-11-10 19:32:55 | [diff] [blame] | 757 | return os.path.basename(_SplitCommand(command)[0]) == 'iossim' |
Yuke Liao | a0c8c2f | 2018-02-28 20:14:10 | [diff] [blame] | 758 | |
| 759 | |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 760 | def _VerifyTargetExecutablesAreInBuildDirectory(commands): |
| 761 | """Verifies that the target executables specified in the commands are inside |
| 762 | the given build directory.""" |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 763 | for command in commands: |
| 764 | binary_path = _GetBinaryPath(command) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 765 | binary_absolute_path = coverage_utils.GetFullPath(binary_path) |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 766 | assert binary_absolute_path.startswith(BUILD_DIR + os.sep), ( |
Yuke Liao | 95d13d7 | 2017-12-07 18:18:50 | [diff] [blame] | 767 | 'Target executable "%s" in command: "%s" is outside of ' |
| 768 | 'the given build directory: "%s".' % (binary_path, command, BUILD_DIR)) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 769 | |
| 770 | |
| 771 | def _ValidateBuildingWithClangCoverage(): |
| 772 | """Asserts that targets are built with Clang coverage enabled.""" |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 773 | build_args = _GetBuildArgs() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 774 | |
| 775 | if (CLANG_COVERAGE_BUILD_ARG not in build_args or |
| 776 | build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'): |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 777 | assert False, ('\'{} = true\' is required in args.gn.' |
| 778 | ).format(CLANG_COVERAGE_BUILD_ARG) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 779 | |
| 780 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 781 | def _ValidateCurrentPlatformIsSupported(): |
| 782 | """Asserts that this script suports running on the current platform""" |
| 783 | target_os = _GetTargetOS() |
| 784 | if target_os: |
| 785 | current_platform = target_os |
| 786 | else: |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 787 | current_platform = coverage_utils.GetHostPlatform() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 788 | |
Ben Joyce | 8828236 | 2021-01-29 23:53:31 | [diff] [blame] | 789 | supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win'] |
| 790 | assert current_platform in supported_platforms, ('Coverage is only' |
| 791 | 'supported on %s' % |
| 792 | supported_platforms) |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 793 | |
| 794 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 795 | def _GetBuildArgs(): |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 796 | """Parses args.gn file and returns results as a dictionary. |
| 797 | |
| 798 | Returns: |
| 799 | A dictionary representing the build args. |
| 800 | """ |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 801 | global _BUILD_ARGS |
| 802 | if _BUILD_ARGS is not None: |
| 803 | return _BUILD_ARGS |
| 804 | |
| 805 | _BUILD_ARGS = {} |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 806 | build_args_path = os.path.join(BUILD_DIR, 'args.gn') |
| 807 | assert os.path.exists(build_args_path), ('"%s" is not a build directory, ' |
| 808 | 'missing args.gn file.' % BUILD_DIR) |
| 809 | with open(build_args_path) as build_args_file: |
| 810 | build_args_lines = build_args_file.readlines() |
| 811 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 812 | for build_arg_line in build_args_lines: |
| 813 | build_arg_without_comments = build_arg_line.split('#')[0] |
| 814 | key_value_pair = build_arg_without_comments.split('=') |
| 815 | if len(key_value_pair) != 2: |
| 816 | continue |
| 817 | |
| 818 | key = key_value_pair[0].strip() |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 819 | |
| 820 | # Values are wrapped within a pair of double-quotes, so remove the leading |
| 821 | # and trailing double-quotes. |
| 822 | value = key_value_pair[1].strip().strip('"') |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 823 | _BUILD_ARGS[key] = value |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 824 | |
Yuke Liao | 80afff3 | 2018-03-07 01:26:20 | [diff] [blame] | 825 | return _BUILD_ARGS |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 826 | |
| 827 | |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 828 | def _VerifyPathsAndReturnAbsolutes(paths): |
| 829 | """Verifies that the paths specified in |paths| exist and returns absolute |
| 830 | versions. |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 831 | |
| 832 | Args: |
| 833 | paths: A list of files or directories. |
| 834 | """ |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 835 | absolute_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 836 | for path in paths: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 837 | absolute_path = os.path.join(SRC_ROOT_PATH, path) |
| 838 | assert os.path.exists(absolute_path), ('Path: "%s" doesn\'t exist.' % path) |
| 839 | |
| 840 | absolute_paths.append(absolute_path) |
| 841 | |
| 842 | return absolute_paths |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 843 | |
| 844 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 845 | def _GetBinaryPathsFromTargets(targets, build_dir): |
| 846 | """Return binary paths from target names.""" |
Ben Joyce | 8828236 | 2021-01-29 23:53:31 | [diff] [blame] | 847 | # TODO(crbug.com/899974): Derive output binary from target build definitions |
| 848 | # rather than assuming that it is always the same name. |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 849 | binary_paths = [] |
| 850 | for target in targets: |
| 851 | binary_path = os.path.join(build_dir, target) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 852 | if coverage_utils.GetHostPlatform() == 'win': |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 853 | binary_path += '.exe' |
| 854 | |
| 855 | if os.path.exists(binary_path): |
| 856 | binary_paths.append(binary_path) |
| 857 | else: |
| 858 | logging.warning( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 859 | 'Target binary "%s" not found in build directory, skipping.', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 860 | os.path.basename(binary_path)) |
| 861 | |
| 862 | return binary_paths |
| 863 | |
| 864 | |
Prakhar | a641851 | 2023-05-22 17:17:45 | [diff] [blame] | 865 | def _GetCommandForWebTests(targets, arguments): |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 866 | """Return command to run for blink web tests.""" |
Prakhar | a641851 | 2023-05-22 17:17:45 | [diff] [blame] | 867 | assert len(targets) == 1, "Only one wpt target can be run" |
| 868 | target = targets[0] |
| 869 | expected_profraw_file_name = os.extsep.join( |
Nico Weber | 51e61c7d | 2023-12-12 17:32:46 | [diff] [blame^] | 870 | [target, '%2m', PROFRAW_FILE_EXTENSION]) |
Prakhar | a641851 | 2023-05-22 17:17:45 | [diff] [blame] | 871 | expected_profraw_file_path = os.path.join( |
| 872 | coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), |
| 873 | expected_profraw_file_name) |
| 874 | |
Dirk Pranke | 34c093a4 | 2021-03-25 19:19:05 | [diff] [blame] | 875 | cpu_count = multiprocessing.cpu_count() |
| 876 | if sys.platform == 'win32': |
| 877 | # TODO(crbug.com/1190269) - we can't use more than 56 |
| 878 | # cores on Windows or Python3 may hang. |
| 879 | cpu_count = min(cpu_count, 56) |
| 880 | cpu_count = max(1, cpu_count // 2) |
| 881 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 882 | command_list = [ |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 883 | 'third_party/blink/tools/run_web_tests.py', |
| 884 | '--additional-driver-flag=--no-sandbox', |
Prakhar | a641851 | 2023-05-22 17:17:45 | [diff] [blame] | 885 | '--additional-env-var=LLVM_PROFILE_FILE=%s' % expected_profraw_file_path, |
Dirk Pranke | 34c093a4 | 2021-03-25 19:19:05 | [diff] [blame] | 886 | '--child-processes=%d' % cpu_count, '--disable-breakpad', |
| 887 | '--no-show-results', '--skip-failing-tests', |
Weizhong Xia | 91b5336 | 2022-01-05 17:13:35 | [diff] [blame] | 888 | '--target=%s' % os.path.basename(BUILD_DIR), '--timeout-ms=30000' |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 889 | ] |
| 890 | if arguments.strip(): |
| 891 | command_list.append(arguments) |
| 892 | return ' '.join(command_list) |
| 893 | |
| 894 | |
Ben Joyce | 8828236 | 2021-01-29 23:53:31 | [diff] [blame] | 895 | def _GetBinaryPathsForAndroid(targets): |
| 896 | """Return binary paths used when running android tests.""" |
| 897 | # TODO(crbug.com/899974): Implement approach that doesn't assume .so file is |
| 898 | # based on the target's name. |
| 899 | android_binaries = set() |
| 900 | for target in targets: |
| 901 | so_library_path = os.path.join(BUILD_DIR, 'lib.unstripped', |
| 902 | 'lib%s__library.so' % target) |
| 903 | if os.path.exists(so_library_path): |
| 904 | android_binaries.add(so_library_path) |
| 905 | |
| 906 | return list(android_binaries) |
| 907 | |
| 908 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 909 | def _GetBinaryPathForWebTests(): |
| 910 | """Return binary path used to run blink web tests.""" |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 911 | host_platform = coverage_utils.GetHostPlatform() |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 912 | if host_platform == 'win': |
| 913 | return os.path.join(BUILD_DIR, 'content_shell.exe') |
| 914 | elif host_platform == 'linux': |
| 915 | return os.path.join(BUILD_DIR, 'content_shell') |
| 916 | elif host_platform == 'mac': |
| 917 | return os.path.join(BUILD_DIR, 'Content Shell.app', 'Contents', 'MacOS', |
| 918 | 'Content Shell') |
| 919 | else: |
| 920 | assert False, 'This platform is not supported for web tests.' |
| 921 | |
| 922 | |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 923 | def _SetupOutputDir(): |
| 924 | """Setup output directory.""" |
| 925 | if os.path.exists(OUTPUT_DIR): |
| 926 | shutil.rmtree(OUTPUT_DIR) |
| 927 | |
| 928 | # Creates |OUTPUT_DIR| and its platform sub-directory. |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 929 | os.makedirs(coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR)) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 930 | |
| 931 | |
Yuke Liao | abfbba4 | 2019-06-11 16:03:59 | [diff] [blame] | 932 | def _SetMacXcodePath(): |
| 933 | """Set DEVELOPER_DIR to the path to hermetic Xcode.app on Mac OS X.""" |
| 934 | if sys.platform != 'darwin': |
| 935 | return |
| 936 | |
| 937 | xcode_path = os.path.join(SRC_ROOT_PATH, 'build', 'mac_files', 'Xcode.app') |
| 938 | if os.path.exists(xcode_path): |
| 939 | os.environ['DEVELOPER_DIR'] = xcode_path |
| 940 | |
| 941 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 942 | def _ParseCommandArguments(): |
| 943 | """Adds and parses relevant arguments for tool comands. |
| 944 | |
| 945 | Returns: |
| 946 | A dictionary representing the arguments. |
| 947 | """ |
| 948 | arg_parser = argparse.ArgumentParser() |
| 949 | arg_parser.usage = __doc__ |
| 950 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 951 | arg_parser.add_argument( |
| 952 | '-b', |
| 953 | '--build-dir', |
| 954 | type=str, |
| 955 | required=True, |
| 956 | help='The build directory, the path needs to be relative to the root of ' |
| 957 | 'the checkout.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 958 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 959 | arg_parser.add_argument( |
| 960 | '-o', |
| 961 | '--output-dir', |
| 962 | type=str, |
| 963 | required=True, |
| 964 | help='Output directory for generated artifacts.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 965 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 966 | arg_parser.add_argument( |
| 967 | '-c', |
| 968 | '--command', |
| 969 | action='append', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 970 | required=False, |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 971 | help='Commands used to run test targets, one test target needs one and ' |
| 972 | 'only one command, when specifying commands, one should assume the ' |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 973 | 'current working directory is the root of the checkout. This option is ' |
| 974 | 'incompatible with -p/--profdata-file option.') |
| 975 | |
| 976 | arg_parser.add_argument( |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 977 | '-wt', |
| 978 | '--web-tests', |
| 979 | nargs='?', |
| 980 | type=str, |
| 981 | const=' ', |
| 982 | required=False, |
| 983 | help='Run blink web tests. Support passing arguments to run_web_tests.py') |
| 984 | |
| 985 | arg_parser.add_argument( |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 986 | '-p', |
| 987 | '--profdata-file', |
| 988 | type=str, |
Prakhar | b852780 | 2023-04-20 09:48:46 | [diff] [blame] | 989 | action='append', |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 990 | required=False, |
Prakhar | b852780 | 2023-04-20 09:48:46 | [diff] [blame] | 991 | help= |
| 992 | 'Path(s) to profdata file(s) to use for generating code coverage reports. ' |
| 993 | 'This can be useful if you generated the profdata file seperately in ' |
| 994 | 'your own test harness. This option is ignored if run command(s) are ' |
| 995 | 'already provided above using -c/--command option.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 996 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 997 | arg_parser.add_argument( |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 998 | '-f', |
| 999 | '--filters', |
| 1000 | action='append', |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1001 | required=False, |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1002 | help='Directories or files to get code coverage for, and all files under ' |
| 1003 | 'the directories are included recursively.') |
| 1004 | |
| 1005 | arg_parser.add_argument( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1006 | '-i', |
| 1007 | '--ignore-filename-regex', |
| 1008 | type=str, |
| 1009 | help='Skip source code files with file paths that match the given ' |
| 1010 | 'regular expression. For example, use -i=\'.*/out/.*|.*/third_party/.*\' ' |
| 1011 | 'to exclude files in third_party/ and out/ folders from the report.') |
| 1012 | |
| 1013 | arg_parser.add_argument( |
Yuke Liao | 1b852fd | 2018-05-11 17:07:32 | [diff] [blame] | 1014 | '--no-file-view', |
| 1015 | action='store_true', |
| 1016 | help='Don\'t generate the file view in the coverage report. When there ' |
| 1017 | 'are large number of html files, the file view becomes heavy and may ' |
| 1018 | 'cause the browser to freeze, and this argument comes handy.') |
| 1019 | |
| 1020 | arg_parser.add_argument( |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1021 | '--no-component-view', |
| 1022 | action='store_true', |
| 1023 | help='Don\'t generate the component view in the coverage report.') |
| 1024 | |
| 1025 | arg_parser.add_argument( |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 1026 | '--coverage-tools-dir', |
| 1027 | type=str, |
| 1028 | help='Path of the directory where LLVM coverage tools (llvm-cov, ' |
| 1029 | 'llvm-profdata) exist. This should be only needed if you are testing ' |
| 1030 | 'against a custom built clang revision. Otherwise, we pick coverage ' |
| 1031 | 'tools automatically from your current source checkout.') |
| 1032 | |
| 1033 | arg_parser.add_argument( |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1034 | '-j', |
| 1035 | '--jobs', |
| 1036 | type=int, |
| 1037 | default=None, |
| 1038 | 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] | 1039 | 'will be derived based on CPUs and goma availability. Please refer to ' |
| 1040 | '\'autoninja -h\' for more details.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1041 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1042 | arg_parser.add_argument( |
Sahel Sharify | 38cabdc | 2020-01-16 00:40:01 | [diff] [blame] | 1043 | '--format', |
| 1044 | type=str, |
| 1045 | default='html', |
Akekawit Jitprasert | f9cb662 | 2021-08-24 17:48:02 | [diff] [blame] | 1046 | help='Output format of the "llvm-cov show/export" command. The ' |
| 1047 | 'supported formats are "text", "html" and "lcov".') |
Sahel Sharify | 38cabdc | 2020-01-16 00:40:01 | [diff] [blame] | 1048 | |
| 1049 | arg_parser.add_argument( |
Yuke Liao | 481d348 | 2018-01-29 19:17:10 | [diff] [blame] | 1050 | '-v', |
| 1051 | '--verbose', |
| 1052 | action='store_true', |
| 1053 | help='Prints additional output for diagnostics.') |
| 1054 | |
| 1055 | arg_parser.add_argument( |
| 1056 | '-l', '--log_file', type=str, help='Redirects logs to a file.') |
| 1057 | |
| 1058 | arg_parser.add_argument( |
Abhishek Arya | c19bc5ef | 2018-05-04 22:10:02 | [diff] [blame] | 1059 | 'targets', |
| 1060 | nargs='+', |
| 1061 | help='The names of the test targets to run. If multiple run commands are ' |
| 1062 | 'specified using the -c/--command option, then the order of targets and ' |
| 1063 | 'commands must match, otherwise coverage generation will fail.') |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1064 | |
| 1065 | args = arg_parser.parse_args() |
| 1066 | return args |
| 1067 | |
| 1068 | |
| 1069 | def Main(): |
| 1070 | """Execute tool commands.""" |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 1071 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1072 | # Change directory to source root to aid in relative paths calculations. |
| 1073 | os.chdir(SRC_ROOT_PATH) |
Abhishek Arya | 8a0751a | 2018-05-03 18:53:11 | [diff] [blame] | 1074 | |
pasthana | a484411 | 2020-05-21 18:03:55 | [diff] [blame] | 1075 | # Setup coverage binaries even when script is called with empty params. This |
| 1076 | # is used by coverage bot for initial setup. |
| 1077 | if len(sys.argv) == 1: |
Choongwoo Han | bd1aa95 | 2021-06-09 22:25:38 | [diff] [blame] | 1078 | subprocess.check_call([ |
Akekawit Jitprasert | 928671e | 2021-09-20 18:40:58 | [diff] [blame] | 1079 | sys.executable, 'tools/clang/scripts/update.py', '--package', |
| 1080 | 'coverage_tools' |
Choongwoo Han | bd1aa95 | 2021-06-09 22:25:38 | [diff] [blame] | 1081 | ]) |
pasthana | a484411 | 2020-05-21 18:03:55 | [diff] [blame] | 1082 | print(__doc__) |
| 1083 | return |
| 1084 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1085 | args = _ParseCommandArguments() |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1086 | coverage_utils.ConfigureLogging(verbose=args.verbose, log_file=args.log_file) |
Yuke Liao | 082e9963 | 2018-05-18 15:40:40 | [diff] [blame] | 1087 | _ConfigureLLVMCoverageTools(args) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1088 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1089 | global BUILD_DIR |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1090 | BUILD_DIR = coverage_utils.GetFullPath(args.build_dir) |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 1091 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1092 | global OUTPUT_DIR |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1093 | OUTPUT_DIR = coverage_utils.GetFullPath(args.output_dir) |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1094 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 1095 | assert args.web_tests or args.command or args.profdata_file, ( |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1096 | 'Need to either provide commands to run using -c/--command option OR ' |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 1097 | 'provide prof-data file as input using -p/--profdata-file option OR ' |
| 1098 | 'run web tests using -wt/--run-web-tests.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1099 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1100 | assert not args.command or (len(args.targets) == len(args.command)), ( |
| 1101 | 'Number of targets must be equal to the number of test commands.') |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1102 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1103 | assert os.path.exists(BUILD_DIR), ( |
Abhishek Arya | fb70b53 | 2018-05-06 17:47:40 | [diff] [blame] | 1104 | 'Build directory: "%s" doesn\'t exist. ' |
| 1105 | 'Please run "gn gen" to generate.' % BUILD_DIR) |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1106 | |
Yuke Liao | c60b2d0 | 2018-03-02 21:40:43 | [diff] [blame] | 1107 | _ValidateCurrentPlatformIsSupported() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1108 | _ValidateBuildingWithClangCoverage() |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1109 | |
| 1110 | absolute_filter_paths = [] |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1111 | if args.filters: |
Abhishek Arya | 16f059a | 2017-12-07 17:47:32 | [diff] [blame] | 1112 | absolute_filter_paths = _VerifyPathsAndReturnAbsolutes(args.filters) |
Yuke Liao | 66da173 | 2017-12-05 22:19:42 | [diff] [blame] | 1113 | |
Abhishek Arya | e5811afa | 2018-05-24 03:56:01 | [diff] [blame] | 1114 | _SetupOutputDir() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1115 | |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 1116 | # Get .profdata file and list of binary paths. |
| 1117 | if args.web_tests: |
Prakhar | a641851 | 2023-05-22 17:17:45 | [diff] [blame] | 1118 | commands = [_GetCommandForWebTests(args.targets, args.web_tests)] |
Abhishek Arya | 0391109 | 2018-05-21 16:42:35 | [diff] [blame] | 1119 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 1120 | args.targets, commands, args.jobs) |
| 1121 | binary_paths = [_GetBinaryPathForWebTests()] |
| 1122 | elif args.command: |
| 1123 | for i in range(len(args.command)): |
| 1124 | assert not 'run_web_tests.py' in args.command[i], ( |
| 1125 | 'run_web_tests.py is not supported via --command argument. ' |
| 1126 | 'Please use --run-web-tests argument instead.') |
| 1127 | |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1128 | # A list of commands are provided. Run them to generate profdata file, and |
| 1129 | # create a list of binary paths from parsing commands. |
| 1130 | _VerifyTargetExecutablesAreInBuildDirectory(args.command) |
| 1131 | profdata_file_path = _CreateCoverageProfileDataForTargets( |
| 1132 | args.targets, args.command, args.jobs) |
| 1133 | binary_paths = [_GetBinaryPath(command) for command in args.command] |
| 1134 | else: |
Julia Hansbrough | 57bd3fc | 2023-03-30 01:47:23 | [diff] [blame] | 1135 | # An input prof-data file(s) is already provided. |
| 1136 | if len(args.profdata_file) == 1: |
| 1137 | # If it's just one input file, use as-is. |
Prakhar | f851c56 | 2023-05-23 19:32:40 | [diff] [blame] | 1138 | profdata_file_path = args.profdata_file[0] |
Julia Hansbrough | 57bd3fc | 2023-03-30 01:47:23 | [diff] [blame] | 1139 | else: |
| 1140 | # Otherwise, there are multiple profdata files and we need to merge them. |
| 1141 | profdata_file_path = _CreateCoverageProfileDataFromTargetProfDataFiles(args.profdata_file) |
| 1142 | # Since input prof-data files were provided, we only need to calculate the |
| 1143 | # binary paths from here. |
Abhishek Arya | 64636af | 2018-05-04 14:42:13 | [diff] [blame] | 1144 | binary_paths = _GetBinaryPathsFromTargets(args.targets, args.build_dir) |
Yuke Liao | ea228d0 | 2018-01-05 19:10:33 | [diff] [blame] | 1145 | |
Erik Chen | 283b92c7 | 2019-07-22 16:37:39 | [diff] [blame] | 1146 | # If the checkout uses the hermetic xcode binaries, then otool must be |
| 1147 | # directly invoked. The indirection via /usr/bin/otool won't work unless |
| 1148 | # there's an actual system install of Xcode. |
| 1149 | otool_path = None |
| 1150 | if sys.platform == 'darwin': |
| 1151 | hermetic_otool_path = os.path.join( |
| 1152 | SRC_ROOT_PATH, 'build', 'mac_files', 'xcode_binaries', 'Contents', |
| 1153 | 'Developer', 'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin', |
| 1154 | 'otool') |
| 1155 | if os.path.exists(hermetic_otool_path): |
| 1156 | otool_path = hermetic_otool_path |
Ben Joyce | 8828236 | 2021-01-29 23:53:31 | [diff] [blame] | 1157 | |
| 1158 | if _IsAndroid(): |
| 1159 | binary_paths = _GetBinaryPathsForAndroid(args.targets) |
| 1160 | elif sys.platform.startswith('linux') or sys.platform.startswith('darwin'): |
Brent McBride | b25b177a4 | 2020-05-11 18:13:06 | [diff] [blame] | 1161 | binary_paths.extend( |
| 1162 | coverage_utils.GetSharedLibraries(binary_paths, BUILD_DIR, otool_path)) |
Abhishek Arya | 78120bc | 2018-05-07 20:53:54 | [diff] [blame] | 1163 | |
Akekawit Jitprasert | f9cb662 | 2021-08-24 17:48:02 | [diff] [blame] | 1164 | assert args.format in ['html', 'lcov', 'text'], ( |
| 1165 | '%s is not a valid output format for "llvm-cov show/export". Only ' |
| 1166 | '"text", "html" and "lcov" formats are supported.' % (args.format)) |
Sahel Sharify | 38cabdc | 2020-01-16 00:40:01 | [diff] [blame] | 1167 | logging.info('Generating code coverage report in %s (this can take a while ' |
| 1168 | 'depending on size of target!).' % (args.format)) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1169 | per_file_summary_data = _GeneratePerFileCoverageSummary( |
Yuke Liao | 0e4c868 | 2018-04-18 21:06:59 | [diff] [blame] | 1170 | binary_paths, profdata_file_path, absolute_filter_paths, |
| 1171 | args.ignore_filename_regex) |
Akekawit Jitprasert | f9cb662 | 2021-08-24 17:48:02 | [diff] [blame] | 1172 | |
| 1173 | if args.format == 'lcov': |
| 1174 | _GeneratePerFileLineByLineCoverageInLcov( |
| 1175 | binary_paths, profdata_file_path, absolute_filter_paths, |
| 1176 | args.ignore_filename_regex) |
| 1177 | return |
| 1178 | |
Sahel Sharify | 38cabdc | 2020-01-16 00:40:01 | [diff] [blame] | 1179 | _GeneratePerFileLineByLineCoverageInFormat( |
| 1180 | binary_paths, profdata_file_path, absolute_filter_paths, |
| 1181 | args.ignore_filename_regex, args.format) |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1182 | component_mappings = None |
| 1183 | if not args.no_component_view: |
Choongwoo Han | bd1aa95 | 2021-06-09 22:25:38 | [diff] [blame] | 1184 | component_mappings = json.load(urlopen(COMPONENT_MAPPING_URL)) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1185 | |
Max Moroz | 1de68d7 | 2018-08-21 13:38:18 | [diff] [blame] | 1186 | # Call prepare here. |
| 1187 | processor = coverage_utils.CoverageReportPostProcessor( |
| 1188 | OUTPUT_DIR, |
| 1189 | SRC_ROOT_PATH, |
| 1190 | per_file_summary_data, |
| 1191 | no_component_view=args.no_component_view, |
| 1192 | no_file_view=args.no_file_view, |
| 1193 | component_mappings=component_mappings) |
Yuke Liao | dd1ec059 | 2018-02-02 01:26:37 | [diff] [blame] | 1194 | |
Sahel Sharify | 38cabdc | 2020-01-16 00:40:01 | [diff] [blame] | 1195 | if args.format == 'html': |
| 1196 | processor.PrepareHtmlReport() |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1197 | |
Abhishek Arya | 1ec832c | 2017-12-05 18:06:59 | [diff] [blame] | 1198 | |
Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame] | 1199 | if __name__ == '__main__': |
| 1200 | sys.exit(Main()) |