blob: 9af3f2cebd04d4c71f57f1d459ab5b6ec8ad58e2 [file] [log] [blame]
Dirk Pranke4d164bb2021-03-24 06:52:401#!/usr/bin/env python
Yuke Liao506e8822017-12-04 16:52:542# Copyright 2017 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
Abhishek Arya1ec832c2017-12-05 18:06:595"""This script helps to generate code coverage report.
Yuke Liao506e8822017-12-04 16:52:546
Abhishek Arya1ec832c2017-12-05 18:06:597 It uses Clang Source-based Code Coverage -
8 https://clang.llvm.org/docs/SourceBasedCodeCoverage.html
Yuke Liao506e8822017-12-04 16:52:549
Abhishek Arya16f059a2017-12-07 17:47:3210 In order to generate code coverage report, you need to first add
Yuke Liaoab9c44e2018-02-21 00:24:4011 "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 Liao506e8822017-12-04 16:52:5413
Abhishek Arya03911092018-05-21 16:42:3514 * Example usage:
Abhishek Arya1ec832c2017-12-05 18:06:5915
Max Moroza5a95272018-08-31 16:20:5516 gn gen out/coverage \\
Abhishek Arya2f261182019-04-24 17:06:4517 --args="use_clang_coverage=true is_component_build=false\\
18 is_debug=false dcheck_always_on=true"
Abhishek Arya16f059a2017-12-07 17:47:3219 gclient runhooks
Abhishek Arya1ec832c2017-12-05 18:06:5920 python tools/code_coverage/coverage.py crypto_unittests url_unittests \\
Abhishek Arya16f059a2017-12-07 17:47:3221 -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 Arya1ec832c2017-12-05 18:06:5924
Abhishek Arya16f059a2017-12-07 17:47:3225 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 Arya1ec832c2017-12-05 18:06:5929
Yuke Liao545db322018-02-15 17:12:0130 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 Arya03911092018-05-21 16:42:3533 * Sample flow for running a test target with xvfb (e.g. unit_tests):
Yuke Liao545db322018-02-15 17:12:0134
35 python tools/code_coverage/coverage.py unit_tests -b out/coverage \\
36 -o out/report -c 'python testing/xvfb.py out/coverage/unit_tests'
37
Abhishek Arya1ec832c2017-12-05 18:06:5938 If you are building a fuzz target, you need to add "use_libfuzzer=true" GN
39 flag as well.
40
Abhishek Arya03911092018-05-21 16:42:3541 * Sample workflow for a fuzz target (e.g. pdfium_fuzzer):
Abhishek Arya1ec832c2017-12-05 18:06:5942
Abhishek Arya16f059a2017-12-07 17:47:3243 python tools/code_coverage/coverage.py pdfium_fuzzer \\
44 -b out/coverage -o out/report \\
Max Moroz13c23182018-11-17 00:23:2245 -c 'out/coverage/pdfium_fuzzer -runs=0 <corpus_dir>' \\
Abhishek Arya16f059a2017-12-07 17:47:3246 -f third_party/pdfium
Abhishek Arya1ec832c2017-12-05 18:06:5947
48 where:
49 <corpus_dir> - directory containing samples files for this format.
Max Moroz13c23182018-11-17 00:23:2250
51 To learn more about generating code coverage reports for fuzz targets, see
John Palmerab8812a2021-05-21 17:03:4352 https://chromium.googlesource.com/chromium/src/+/main/testing/libfuzzer/efficient_fuzzer.md#Code-Coverage
Abhishek Arya1ec832c2017-12-05 18:06:5953
Abhishek Arya03911092018-05-21 16:42:3554 * Sample workflow for running Blink web tests:
55
56 python tools/code_coverage/coverage.py blink_tests \\
57 -wt -b out/coverage -o out/report -f third_party/blink
58
59 If you need to pass arguments to run_web_tests.py, use
60 -wt='arguments to run_web_tests.py e.g. test directories'
61
Abhishek Arya1ec832c2017-12-05 18:06:5962 For more options, please refer to tools/code_coverage/coverage.py -h.
Yuke Liao8e209fe82018-04-18 20:36:3863
64 For an overview of how code coverage works in Chromium, please refer to
John Palmerab8812a2021-05-21 17:03:4365 https://chromium.googlesource.com/chromium/src/+/main/docs/testing/code_coverage.md
Yuke Liao506e8822017-12-04 16:52:5466"""
67
68from __future__ import print_function
69
70import sys
71
72import argparse
Yuke Liaoea228d02018-01-05 19:10:3373import json
Yuke Liao481d3482018-01-29 19:17:1074import logging
Abhishek Arya03911092018-05-21 16:42:3575import multiprocessing
Yuke Liao506e8822017-12-04 16:52:5476import os
Sajjad Mirza0b96e002020-11-10 19:32:5577import platform
Yuke Liaob2926832018-03-02 17:34:2978import re
79import shlex
Max Moroz025d8952018-05-03 16:33:3480import shutil
Yuke Liao506e8822017-12-04 16:52:5481import subprocess
Yuke Liao506e8822017-12-04 16:52:5482import urllib2
83
Choongwoo Hanbd1aa952021-06-09 22:25:3884import six
85
86if six.PY2:
87 from urllib2 import urlopen
88else:
89 from urllib.request import urlopen
90
Abhishek Arya1ec832c2017-12-05 18:06:5991sys.path.append(
92 os.path.join(
Yuke Liaoea228d02018-01-05 19:10:3393 os.path.dirname(__file__), os.path.pardir, os.path.pardir,
94 'third_party'))
Yuke Liaoea228d02018-01-05 19:10:3395from collections import defaultdict
96
Max Moroz1de68d72018-08-21 13:38:1897import coverage_utils
98
Yuke Liao082e99632018-05-18 15:40:4099# Absolute path to the code coverage tools binary. These paths can be
100# overwritten by user specified coverage tool paths.
pasthanab37d5bfd2020-05-28 12:18:31101# Absolute path to the root of the checkout.
102SRC_ROOT_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),
103 os.path.pardir, os.path.pardir)
104LLVM_BIN_DIR = os.path.join(
105 os.path.join(SRC_ROOT_PATH, 'third_party', 'llvm-build', 'Release+Asserts'),
106 'bin')
Abhishek Arya1c97ea542018-05-10 03:53:19107LLVM_COV_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-cov')
108LLVM_PROFDATA_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-profdata')
Yuke Liao506e8822017-12-04 16:52:54109
Abhishek Arya03911092018-05-21 16:42:35110
Yuke Liao506e8822017-12-04 16:52:54111# Build directory, the value is parsed from command line arguments.
112BUILD_DIR = None
113
114# Output directory for generated artifacts, the value is parsed from command
115# line arguemnts.
116OUTPUT_DIR = None
117
Yuke Liao506e8822017-12-04 16:52:54118# Name of the file extension for profraw data files.
119PROFRAW_FILE_EXTENSION = 'profraw'
120
121# Name of the final profdata file, and this file needs to be passed to
122# "llvm-cov" command in order to call "llvm-cov show" to inspect the
123# line-by-line coverage of specific files.
Max Moroz7c5354f2018-05-06 00:03:48124PROFDATA_FILE_NAME = os.extsep.join(['coverage', 'profdata'])
125
126# Name of the file with summary information generated by llvm-cov export.
127SUMMARY_FILE_NAME = os.extsep.join(['summary', 'json'])
Yuke Liao506e8822017-12-04 16:52:54128
129# Build arg required for generating code coverage data.
130CLANG_COVERAGE_BUILD_ARG = 'use_clang_coverage'
131
Max Moroz7c5354f2018-05-06 00:03:48132LOGS_DIR_NAME = 'logs'
Yuke Liaodd1ec0592018-02-02 01:26:37133
134# Used to extract a mapping between directories and components.
Abhishek Arya1c97ea542018-05-10 03:53:19135COMPONENT_MAPPING_URL = (
136 'https://storage.googleapis.com/chromium-owners/component_map.json')
Yuke Liaodd1ec0592018-02-02 01:26:37137
Yuke Liao80afff32018-03-07 01:26:20138# Caches the results returned by _GetBuildArgs, don't use this variable
139# directly, call _GetBuildArgs instead.
140_BUILD_ARGS = None
141
Abhishek Aryac19bc5ef2018-05-04 22:10:02142# Retry failed merges.
143MERGE_RETRIES = 3
144
Abhishek Aryad35de7e2018-05-10 22:23:04145# Message to guide user to file a bug when everything else fails.
146FILE_BUG_MESSAGE = (
147 'If it persists, please file a bug with the command you used, git revision '
148 'and args.gn config here: '
149 'https://bugs.chromium.org/p/chromium/issues/entry?'
Yuke Liao03c644072019-07-30 18:33:40150 'components=Infra%3ETest%3ECodeCoverage')
Abhishek Aryad35de7e2018-05-10 22:23:04151
Abhishek Aryabd0655d2018-05-21 19:55:24152# String to replace with actual llvm profile path.
153LLVM_PROFILE_FILE_PATH_SUBSTITUTION = '<llvm_profile_file_path>'
154
Yuke Liao082e99632018-05-18 15:40:40155def _ConfigureLLVMCoverageTools(args):
156 """Configures llvm coverage tools."""
157 if args.coverage_tools_dir:
Max Moroz1de68d72018-08-21 13:38:18158 llvm_bin_dir = coverage_utils.GetFullPath(args.coverage_tools_dir)
Yuke Liao082e99632018-05-18 15:40:40159 global LLVM_COV_PATH
160 global LLVM_PROFDATA_PATH
161 LLVM_COV_PATH = os.path.join(llvm_bin_dir, 'llvm-cov')
162 LLVM_PROFDATA_PATH = os.path.join(llvm_bin_dir, 'llvm-profdata')
163 else:
Choongwoo Hanbd1aa952021-06-09 22:25:38164 subprocess.check_call([
165 'python', 'tools/clang/scripts/update.py', '--package', 'coverage_tools'
166 ])
Brent McBrideb25b177a42020-05-11 18:13:06167
168 if coverage_utils.GetHostPlatform() == 'win':
169 LLVM_COV_PATH += '.exe'
170 LLVM_PROFDATA_PATH += '.exe'
Yuke Liao082e99632018-05-18 15:40:40171
172 coverage_tools_exist = (
173 os.path.exists(LLVM_COV_PATH) and os.path.exists(LLVM_PROFDATA_PATH))
174 assert coverage_tools_exist, ('Cannot find coverage tools, please make sure '
175 'both \'%s\' and \'%s\' exist.') % (
176 LLVM_COV_PATH, LLVM_PROFDATA_PATH)
177
Abhishek Arya2f261182019-04-24 17:06:45178
Abhishek Arya1c97ea542018-05-10 03:53:19179def _GetPathWithLLVMSymbolizerDir():
180 """Add llvm-symbolizer directory to path for symbolized stacks."""
181 path = os.getenv('PATH')
182 dirs = path.split(os.pathsep)
183 if LLVM_BIN_DIR in dirs:
184 return path
185
186 return path + os.pathsep + LLVM_BIN_DIR
187
188
Yuke Liaoc60b2d02018-03-02 21:40:43189def _GetTargetOS():
190 """Returns the target os specified in args.gn file.
191
192 Returns an empty string is target_os is not specified.
193 """
Yuke Liao80afff32018-03-07 01:26:20194 build_args = _GetBuildArgs()
Yuke Liaoc60b2d02018-03-02 21:40:43195 return build_args['target_os'] if 'target_os' in build_args else ''
196
197
Ben Joyce88282362021-01-29 23:53:31198def _IsAndroid():
199 """Returns true if the target_os specified in args.gn file is android"""
200 return _GetTargetOS() == 'android'
201
202
Yuke Liaob2926832018-03-02 17:34:29203def _IsIOS():
Yuke Liaoa0c8c2f2018-02-28 20:14:10204 """Returns true if the target_os specified in args.gn file is ios"""
Yuke Liaoc60b2d02018-03-02 21:40:43205 return _GetTargetOS() == 'ios'
Yuke Liaoa0c8c2f2018-02-28 20:14:10206
207
Sahel Sharify38cabdc2020-01-16 00:40:01208def _GeneratePerFileLineByLineCoverageInFormat(binary_paths, profdata_file_path,
209 filters, ignore_filename_regex,
210 output_format):
211 """Generates per file line-by-line coverage in html or text using
212 'llvm-cov show'.
Yuke Liao506e8822017-12-04 16:52:54213
Sahel Sharify38cabdc2020-01-16 00:40:01214 For a file with absolute path /a/b/x.cc, a html/txt report is generated as:
215 OUTPUT_DIR/coverage/a/b/x.cc.[html|txt]. For html format, an index html file
216 is also generated as: OUTPUT_DIR/index.html.
Yuke Liao506e8822017-12-04 16:52:54217
218 Args:
219 binary_paths: A list of paths to the instrumented binaries.
220 profdata_file_path: A path to the profdata file.
Yuke Liao66da1732017-12-05 22:19:42221 filters: A list of directories and files to get coverage for.
Sahel Sharify38cabdc2020-01-16 00:40:01222 ignore_filename_regex: A regular expression for skipping source code files
223 with certain file paths.
224 output_format: The output format of generated report files.
Yuke Liao506e8822017-12-04 16:52:54225 """
Yuke Liao506e8822017-12-04 16:52:54226 # llvm-cov show [options] -instr-profile PROFILE BIN [-object BIN,...]
227 # [[-object BIN]] [SOURCES]
228 # NOTE: For object files, the first one is specified as a positional argument,
229 # and the rest are specified as keyword argument.
Yuke Liao481d3482018-01-29 19:17:10230 logging.debug('Generating per file line by line coverage reports using '
Abhishek Aryafb70b532018-05-06 17:47:40231 '"llvm-cov show" command.')
Sahel Sharify38cabdc2020-01-16 00:40:01232
Abhishek Arya1ec832c2017-12-05 18:06:59233 subprocess_cmd = [
Sahel Sharify38cabdc2020-01-16 00:40:01234 LLVM_COV_PATH, 'show', '-format={}'.format(output_format),
Abhishek Arya1ec832c2017-12-05 18:06:59235 '-output-dir={}'.format(OUTPUT_DIR),
236 '-instr-profile={}'.format(profdata_file_path), binary_paths[0]
237 ]
238 subprocess_cmd.extend(
239 ['-object=' + binary_path for binary_path in binary_paths[1:]])
Yuke Liaob2926832018-03-02 17:34:29240 _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths))
Max Moroz1de68d72018-08-21 13:38:18241 if coverage_utils.GetHostPlatform() in ['linux', 'mac']:
Ryan Sleeviae19b2c32018-05-15 22:36:17242 subprocess_cmd.extend(['-Xdemangler', 'c++filt', '-Xdemangler', '-n'])
Yuke Liao66da1732017-12-05 22:19:42243 subprocess_cmd.extend(filters)
Yuke Liao0e4c8682018-04-18 21:06:59244 if ignore_filename_regex:
245 subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex)
246
Yuke Liao506e8822017-12-04 16:52:54247 subprocess.check_call(subprocess_cmd)
Max Moroz025d8952018-05-03 16:33:34248
Abhishek Aryafb70b532018-05-06 17:47:40249 logging.debug('Finished running "llvm-cov show" command.')
Yuke Liao506e8822017-12-04 16:52:54250
251
Max Moroz7c5354f2018-05-06 00:03:48252def _GetLogsDirectoryPath():
253 """Path to the logs directory."""
Max Moroz1de68d72018-08-21 13:38:18254 return os.path.join(
255 coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR), LOGS_DIR_NAME)
Max Moroz7c5354f2018-05-06 00:03:48256
257
258def _GetProfdataFilePath():
259 """Path to the resulting .profdata file."""
Max Moroz1de68d72018-08-21 13:38:18260 return os.path.join(
261 coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR),
262 PROFDATA_FILE_NAME)
Max Moroz7c5354f2018-05-06 00:03:48263
264
265def _GetSummaryFilePath():
266 """The JSON file that contains coverage summary written by llvm-cov export."""
Max Moroz1de68d72018-08-21 13:38:18267 return os.path.join(
268 coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR),
269 SUMMARY_FILE_NAME)
Yuke Liaoea228d02018-01-05 19:10:33270
271
Yuke Liao506e8822017-12-04 16:52:54272def _CreateCoverageProfileDataForTargets(targets, commands, jobs_count=None):
273 """Builds and runs target to generate the coverage profile data.
274
275 Args:
276 targets: A list of targets to build with coverage instrumentation.
277 commands: A list of commands used to run the targets.
278 jobs_count: Number of jobs to run in parallel for building. If None, a
279 default value is derived based on CPUs availability.
280
281 Returns:
282 A relative path to the generated profdata file.
283 """
284 _BuildTargets(targets, jobs_count)
Abhishek Aryac19bc5ef2018-05-04 22:10:02285 target_profdata_file_paths = _GetTargetProfDataPathsByExecutingCommands(
Abhishek Arya1ec832c2017-12-05 18:06:59286 targets, commands)
Abhishek Aryac19bc5ef2018-05-04 22:10:02287 coverage_profdata_file_path = (
288 _CreateCoverageProfileDataFromTargetProfDataFiles(
289 target_profdata_file_paths))
Yuke Liao506e8822017-12-04 16:52:54290
Abhishek Aryac19bc5ef2018-05-04 22:10:02291 for target_profdata_file_path in target_profdata_file_paths:
292 os.remove(target_profdata_file_path)
Yuke Liaod4a9865202018-01-12 23:17:52293
Abhishek Aryac19bc5ef2018-05-04 22:10:02294 return coverage_profdata_file_path
Yuke Liao506e8822017-12-04 16:52:54295
296
297def _BuildTargets(targets, jobs_count):
298 """Builds target with Clang coverage instrumentation.
299
300 This function requires current working directory to be the root of checkout.
301
302 Args:
303 targets: A list of targets to build with coverage instrumentation.
304 jobs_count: Number of jobs to run in parallel for compilation. If None, a
305 default value is derived based on CPUs availability.
Yuke Liao506e8822017-12-04 16:52:54306 """
Abhishek Aryafb70b532018-05-06 17:47:40307 logging.info('Building %s.', str(targets))
Brent McBrideb25b177a42020-05-11 18:13:06308 autoninja = 'autoninja'
309 if coverage_utils.GetHostPlatform() == 'win':
310 autoninja += '.bat'
Yuke Liao506e8822017-12-04 16:52:54311
Brent McBrideb25b177a42020-05-11 18:13:06312 subprocess_cmd = [autoninja, '-C', BUILD_DIR]
Yuke Liao506e8822017-12-04 16:52:54313 if jobs_count is not None:
314 subprocess_cmd.append('-j' + str(jobs_count))
315
316 subprocess_cmd.extend(targets)
317 subprocess.check_call(subprocess_cmd)
Abhishek Aryafb70b532018-05-06 17:47:40318 logging.debug('Finished building %s.', str(targets))
Yuke Liao506e8822017-12-04 16:52:54319
320
Abhishek Aryac19bc5ef2018-05-04 22:10:02321def _GetTargetProfDataPathsByExecutingCommands(targets, commands):
Yuke Liao506e8822017-12-04 16:52:54322 """Runs commands and returns the relative paths to the profraw data files.
323
324 Args:
325 targets: A list of targets built with coverage instrumentation.
326 commands: A list of commands used to run the targets.
327
328 Returns:
329 A list of relative paths to the generated profraw data files.
330 """
Abhishek Aryafb70b532018-05-06 17:47:40331 logging.debug('Executing the test commands.')
Yuke Liao481d3482018-01-29 19:17:10332
Yuke Liao506e8822017-12-04 16:52:54333 # Remove existing profraw data files.
Max Moroz1de68d72018-08-21 13:38:18334 report_root_dir = coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR)
335 for file_or_dir in os.listdir(report_root_dir):
Yuke Liao506e8822017-12-04 16:52:54336 if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
Max Moroz1de68d72018-08-21 13:38:18337 os.remove(os.path.join(report_root_dir, file_or_dir))
Max Moroz7c5354f2018-05-06 00:03:48338
339 # Ensure that logs directory exists.
340 if not os.path.exists(_GetLogsDirectoryPath()):
341 os.makedirs(_GetLogsDirectoryPath())
Yuke Liao506e8822017-12-04 16:52:54342
Abhishek Aryac19bc5ef2018-05-04 22:10:02343 profdata_file_paths = []
Yuke Liaoa0c8c2f2018-02-28 20:14:10344
Yuke Liaod4a9865202018-01-12 23:17:52345 # Run all test targets to generate profraw data files.
Yuke Liao506e8822017-12-04 16:52:54346 for target, command in zip(targets, commands):
Max Moroz7c5354f2018-05-06 00:03:48347 output_file_name = os.extsep.join([target + '_output', 'log'])
348 output_file_path = os.path.join(_GetLogsDirectoryPath(), output_file_name)
Yuke Liaoa0c8c2f2018-02-28 20:14:10349
Abhishek Aryac19bc5ef2018-05-04 22:10:02350 profdata_file_path = None
351 for _ in xrange(MERGE_RETRIES):
Abhishek Aryafb70b532018-05-06 17:47:40352 logging.info('Running command: "%s", the output is redirected to "%s".',
Abhishek Aryac19bc5ef2018-05-04 22:10:02353 command, output_file_path)
Yuke Liaoa0c8c2f2018-02-28 20:14:10354
Abhishek Aryac19bc5ef2018-05-04 22:10:02355 if _IsIOSCommand(command):
356 # On iOS platform, due to lack of write permissions, profraw files are
357 # generated outside of the OUTPUT_DIR, and the exact paths are contained
358 # in the output of the command execution.
Abhishek Arya03911092018-05-21 16:42:35359 output = _ExecuteIOSCommand(command, output_file_path)
Abhishek Aryac19bc5ef2018-05-04 22:10:02360 else:
361 # On other platforms, profraw files are generated inside the OUTPUT_DIR.
Abhishek Arya03911092018-05-21 16:42:35362 output = _ExecuteCommand(target, command, output_file_path)
Abhishek Aryac19bc5ef2018-05-04 22:10:02363
364 profraw_file_paths = []
365 if _IsIOS():
Yuke Liao9c2c70b2018-05-23 15:37:57366 profraw_file_paths = [_GetProfrawDataFileByParsingOutput(output)]
Ben Joyce88282362021-01-29 23:53:31367 elif _IsAndroid():
368 android_coverage_dir = os.path.join(BUILD_DIR, 'coverage')
369 for r, _, files in os.walk(android_coverage_dir):
370 for f in files:
371 if f.endswith(PROFRAW_FILE_EXTENSION):
372 profraw_file_paths.append(os.path.join(r, f))
Abhishek Aryac19bc5ef2018-05-04 22:10:02373 else:
Max Moroz1de68d72018-08-21 13:38:18374 for file_or_dir in os.listdir(report_root_dir):
Abhishek Aryac19bc5ef2018-05-04 22:10:02375 if file_or_dir.endswith(PROFRAW_FILE_EXTENSION):
Max Moroz7c5354f2018-05-06 00:03:48376 profraw_file_paths.append(
Max Moroz1de68d72018-08-21 13:38:18377 os.path.join(report_root_dir, file_or_dir))
Abhishek Aryac19bc5ef2018-05-04 22:10:02378
379 assert profraw_file_paths, (
Abhishek Aryafb70b532018-05-06 17:47:40380 'Running target "%s" failed to generate any profraw data file, '
Abhishek Aryad35de7e2018-05-10 22:23:04381 'please make sure the binary exists, is properly instrumented and '
382 'does not crash. %s' % (target, FILE_BUG_MESSAGE))
Abhishek Aryac19bc5ef2018-05-04 22:10:02383
Yuke Liao9c2c70b2018-05-23 15:37:57384 assert isinstance(profraw_file_paths, list), (
Max Moroz1de68d72018-08-21 13:38:18385 'Variable \'profraw_file_paths\' is expected to be of type \'list\', '
386 'but it is a %s. %s' % (type(profraw_file_paths), FILE_BUG_MESSAGE))
Yuke Liao9c2c70b2018-05-23 15:37:57387
Abhishek Aryac19bc5ef2018-05-04 22:10:02388 try:
389 profdata_file_path = _CreateTargetProfDataFileFromProfRawFiles(
390 target, profraw_file_paths)
391 break
392 except Exception:
Abhishek Aryad35de7e2018-05-10 22:23:04393 logging.info('Retrying...')
Abhishek Aryac19bc5ef2018-05-04 22:10:02394 finally:
395 # Remove profraw files now so that they are not used in next iteration.
396 for profraw_file_path in profraw_file_paths:
397 os.remove(profraw_file_path)
398
399 assert profdata_file_path, (
Abhishek Aryad35de7e2018-05-10 22:23:04400 'Failed to merge target "%s" profraw files after %d retries. %s' %
401 (target, MERGE_RETRIES, FILE_BUG_MESSAGE))
Abhishek Aryac19bc5ef2018-05-04 22:10:02402 profdata_file_paths.append(profdata_file_path)
Yuke Liao506e8822017-12-04 16:52:54403
Abhishek Aryafb70b532018-05-06 17:47:40404 logging.debug('Finished executing the test commands.')
Yuke Liao481d3482018-01-29 19:17:10405
Abhishek Aryac19bc5ef2018-05-04 22:10:02406 return profdata_file_paths
Yuke Liao506e8822017-12-04 16:52:54407
408
Abhishek Arya03911092018-05-21 16:42:35409def _GetEnvironmentVars(profraw_file_path):
410 """Return environment vars for subprocess, given a profraw file path."""
411 env = os.environ.copy()
412 env.update({
413 'LLVM_PROFILE_FILE': profraw_file_path,
414 'PATH': _GetPathWithLLVMSymbolizerDir()
415 })
416 return env
417
418
Sajjad Mirza0b96e002020-11-10 19:32:55419def _SplitCommand(command):
420 """Split a command string into parts in a platform-specific way."""
421 if coverage_utils.GetHostPlatform() == 'win':
422 return command.split()
423 return shlex.split(command)
424
425
Abhishek Arya03911092018-05-21 16:42:35426def _ExecuteCommand(target, command, output_file_path):
Yuke Liaoa0c8c2f2018-02-28 20:14:10427 """Runs a single command and generates a profraw data file."""
Yuke Liaod4a9865202018-01-12 23:17:52428 # Per Clang "Source-based Code Coverage" doc:
Yuke Liao27349c92018-03-22 21:10:01429 #
Max Morozd73e45f2018-04-24 18:32:47430 # "%p" expands out to the process ID. It's not used by this scripts due to:
431 # 1) If a target program spawns too many processess, it may exhaust all disk
432 # space available. For example, unit_tests writes thousands of .profraw
433 # files each of size 1GB+.
434 # 2) If a target binary uses shared libraries, coverage profile data for them
435 # will be missing, resulting in incomplete coverage reports.
Yuke Liao27349c92018-03-22 21:10:01436 #
Yuke Liaod4a9865202018-01-12 23:17:52437 # "%Nm" expands out to the instrumented binary's signature. When this pattern
438 # is specified, the runtime creates a pool of N raw profiles which are used
439 # for on-line profile merging. The runtime takes care of selecting a raw
440 # profile from the pool, locking it, and updating it before the program exits.
Yuke Liaod4a9865202018-01-12 23:17:52441 # N must be between 1 and 9. The merge pool specifier can only occur once per
442 # filename pattern.
443 #
Max Morozd73e45f2018-04-24 18:32:47444 # "%1m" is used when tests run in single process, such as fuzz targets.
Yuke Liao27349c92018-03-22 21:10:01445 #
Max Morozd73e45f2018-04-24 18:32:47446 # For other cases, "%4m" is chosen as it creates some level of parallelism,
447 # but it's not too big to consume too much computing resource or disk space.
448 profile_pattern_string = '%1m' if _IsFuzzerTarget(target) else '%4m'
Abhishek Arya1ec832c2017-12-05 18:06:59449 expected_profraw_file_name = os.extsep.join(
Yuke Liao27349c92018-03-22 21:10:01450 [target, profile_pattern_string, PROFRAW_FILE_EXTENSION])
Max Moroz1de68d72018-08-21 13:38:18451 expected_profraw_file_path = os.path.join(
452 coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR),
453 expected_profraw_file_name)
Abhishek Aryabd0655d2018-05-21 19:55:24454 command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION,
455 expected_profraw_file_path)
Yuke Liao506e8822017-12-04 16:52:54456
Yuke Liaoa0c8c2f2018-02-28 20:14:10457 try:
Max Moroz7c5354f2018-05-06 00:03:48458 # Some fuzz targets or tests may write into stderr, redirect it as well.
Abhishek Arya03911092018-05-21 16:42:35459 with open(output_file_path, 'wb') as output_file_handle:
Sajjad Mirza0b96e002020-11-10 19:32:55460 subprocess.check_call(_SplitCommand(command),
461 stdout=output_file_handle,
462 stderr=subprocess.STDOUT,
463 env=_GetEnvironmentVars(expected_profraw_file_path))
Yuke Liaoa0c8c2f2018-02-28 20:14:10464 except subprocess.CalledProcessError as e:
Abhishek Arya03911092018-05-21 16:42:35465 logging.warning('Command: "%s" exited with non-zero return code.', command)
Yuke Liaoa0c8c2f2018-02-28 20:14:10466
Abhishek Arya03911092018-05-21 16:42:35467 return open(output_file_path, 'rb').read()
Yuke Liaoa0c8c2f2018-02-28 20:14:10468
469
Yuke Liao27349c92018-03-22 21:10:01470def _IsFuzzerTarget(target):
471 """Returns true if the target is a fuzzer target."""
472 build_args = _GetBuildArgs()
473 use_libfuzzer = ('use_libfuzzer' in build_args and
474 build_args['use_libfuzzer'] == 'true')
475 return use_libfuzzer and target.endswith('_fuzzer')
476
477
Abhishek Arya03911092018-05-21 16:42:35478def _ExecuteIOSCommand(command, output_file_path):
Yuke Liaoa0c8c2f2018-02-28 20:14:10479 """Runs a single iOS command and generates a profraw data file.
480
481 iOS application doesn't have write access to folders outside of the app, so
482 it's impossible to instruct the app to flush the profraw data file to the
483 desired location. The profraw data file will be generated somewhere within the
484 application's Documents folder, and the full path can be obtained by parsing
485 the output.
486 """
Yuke Liaob2926832018-03-02 17:34:29487 assert _IsIOSCommand(command)
488
489 # After running tests, iossim generates a profraw data file, it won't be
490 # needed anyway, so dump it into the OUTPUT_DIR to avoid polluting the
491 # checkout.
492 iossim_profraw_file_path = os.path.join(
493 OUTPUT_DIR, os.extsep.join(['iossim', PROFRAW_FILE_EXTENSION]))
Abhishek Aryabd0655d2018-05-21 19:55:24494 command = command.replace(LLVM_PROFILE_FILE_PATH_SUBSTITUTION,
495 iossim_profraw_file_path)
Yuke Liaoa0c8c2f2018-02-28 20:14:10496
497 try:
Abhishek Arya03911092018-05-21 16:42:35498 with open(output_file_path, 'wb') as output_file_handle:
Sajjad Mirza0b96e002020-11-10 19:32:55499 subprocess.check_call(_SplitCommand(command),
500 stdout=output_file_handle,
501 stderr=subprocess.STDOUT,
502 env=_GetEnvironmentVars(iossim_profraw_file_path))
Yuke Liaoa0c8c2f2018-02-28 20:14:10503 except subprocess.CalledProcessError as e:
504 # iossim emits non-zero return code even if tests run successfully, so
505 # ignore the return code.
Abhishek Arya03911092018-05-21 16:42:35506 pass
Yuke Liaoa0c8c2f2018-02-28 20:14:10507
Abhishek Arya03911092018-05-21 16:42:35508 return open(output_file_path, 'rb').read()
Yuke Liaoa0c8c2f2018-02-28 20:14:10509
510
511def _GetProfrawDataFileByParsingOutput(output):
512 """Returns the path to the profraw data file obtained by parsing the output.
513
514 The output of running the test target has no format, but it is guaranteed to
515 have a single line containing the path to the generated profraw data file.
516 NOTE: This should only be called when target os is iOS.
517 """
Yuke Liaob2926832018-03-02 17:34:29518 assert _IsIOS()
Yuke Liaoa0c8c2f2018-02-28 20:14:10519
Yuke Liaob2926832018-03-02 17:34:29520 output_by_lines = ''.join(output).splitlines()
521 profraw_file_pattern = re.compile('.*Coverage data at (.*coverage\.profraw).')
Yuke Liaoa0c8c2f2018-02-28 20:14:10522
523 for line in output_by_lines:
Yuke Liaob2926832018-03-02 17:34:29524 result = profraw_file_pattern.match(line)
525 if result:
526 return result.group(1)
Yuke Liaoa0c8c2f2018-02-28 20:14:10527
528 assert False, ('No profraw data file was generated, did you call '
529 'coverage_util::ConfigureCoverageReportPath() in test setup? '
530 'Please refer to base/test/test_support_ios.mm for example.')
Yuke Liao506e8822017-12-04 16:52:54531
532
Abhishek Aryac19bc5ef2018-05-04 22:10:02533def _CreateCoverageProfileDataFromTargetProfDataFiles(profdata_file_paths):
534 """Returns a relative path to coverage profdata file by merging target
535 profdata files.
Yuke Liao506e8822017-12-04 16:52:54536
537 Args:
Abhishek Aryac19bc5ef2018-05-04 22:10:02538 profdata_file_paths: A list of relative paths to the profdata data files
539 that are to be merged.
Yuke Liao506e8822017-12-04 16:52:54540
541 Returns:
Abhishek Aryac19bc5ef2018-05-04 22:10:02542 A relative path to the merged coverage profdata file.
Yuke Liao506e8822017-12-04 16:52:54543
544 Raises:
Abhishek Aryac19bc5ef2018-05-04 22:10:02545 CalledProcessError: An error occurred merging profdata files.
Yuke Liao506e8822017-12-04 16:52:54546 """
Abhishek Aryafb70b532018-05-06 17:47:40547 logging.info('Creating the coverage profile data file.')
548 logging.debug('Merging target profraw files to create target profdata file.')
Max Moroz7c5354f2018-05-06 00:03:48549 profdata_file_path = _GetProfdataFilePath()
Yuke Liao506e8822017-12-04 16:52:54550 try:
Abhishek Arya1ec832c2017-12-05 18:06:59551 subprocess_cmd = [
552 LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true'
553 ]
Abhishek Aryac19bc5ef2018-05-04 22:10:02554 subprocess_cmd.extend(profdata_file_paths)
Abhishek Aryae5811afa2018-05-24 03:56:01555
556 output = subprocess.check_output(subprocess_cmd)
Max Moroz1de68d72018-08-21 13:38:18557 logging.debug('Merge output: %s', output)
Abhishek Aryac19bc5ef2018-05-04 22:10:02558 except subprocess.CalledProcessError as error:
Abhishek Aryad35de7e2018-05-10 22:23:04559 logging.error(
560 'Failed to merge target profdata files to create coverage profdata. %s',
561 FILE_BUG_MESSAGE)
Abhishek Aryac19bc5ef2018-05-04 22:10:02562 raise error
563
Abhishek Aryafb70b532018-05-06 17:47:40564 logging.debug('Finished merging target profdata files.')
565 logging.info('Code coverage profile data is created as: "%s".',
Abhishek Aryac19bc5ef2018-05-04 22:10:02566 profdata_file_path)
567 return profdata_file_path
568
569
570def _CreateTargetProfDataFileFromProfRawFiles(target, profraw_file_paths):
571 """Returns a relative path to target profdata file by merging target
572 profraw files.
573
574 Args:
575 profraw_file_paths: A list of relative paths to the profdata data files
576 that are to be merged.
577
578 Returns:
579 A relative path to the merged coverage profdata file.
580
581 Raises:
582 CalledProcessError: An error occurred merging profdata files.
583 """
Abhishek Aryafb70b532018-05-06 17:47:40584 logging.info('Creating target profile data file.')
585 logging.debug('Merging target profraw files to create target profdata file.')
Abhishek Aryac19bc5ef2018-05-04 22:10:02586 profdata_file_path = os.path.join(OUTPUT_DIR, '%s.profdata' % target)
587
588 try:
589 subprocess_cmd = [
590 LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, '-sparse=true'
591 ]
Yuke Liao506e8822017-12-04 16:52:54592 subprocess_cmd.extend(profraw_file_paths)
Abhishek Aryae5811afa2018-05-24 03:56:01593 output = subprocess.check_output(subprocess_cmd)
Max Moroz1de68d72018-08-21 13:38:18594 logging.debug('Merge output: %s', output)
Yuke Liao506e8822017-12-04 16:52:54595 except subprocess.CalledProcessError as error:
Abhishek Aryad35de7e2018-05-10 22:23:04596 logging.error(
597 'Failed to merge target profraw files to create target profdata.')
Yuke Liao506e8822017-12-04 16:52:54598 raise error
599
Abhishek Aryafb70b532018-05-06 17:47:40600 logging.debug('Finished merging target profraw files.')
601 logging.info('Target "%s" profile data is created as: "%s".', target,
Yuke Liao481d3482018-01-29 19:17:10602 profdata_file_path)
Yuke Liao506e8822017-12-04 16:52:54603 return profdata_file_path
604
605
Yuke Liao0e4c8682018-04-18 21:06:59606def _GeneratePerFileCoverageSummary(binary_paths, profdata_file_path, filters,
607 ignore_filename_regex):
Yuke Liaoea228d02018-01-05 19:10:33608 """Generates per file coverage summary using "llvm-cov export" command."""
609 # llvm-cov export [options] -instr-profile PROFILE BIN [-object BIN,...]
610 # [[-object BIN]] [SOURCES].
611 # NOTE: For object files, the first one is specified as a positional argument,
612 # and the rest are specified as keyword argument.
Yuke Liao481d3482018-01-29 19:17:10613 logging.debug('Generating per-file code coverage summary using "llvm-cov '
Abhishek Aryafb70b532018-05-06 17:47:40614 'export -summary-only" command.')
Sajjad Mirza07f52332020-11-11 01:50:47615 for path in binary_paths:
616 if not os.path.exists(path):
617 logging.error("Binary %s does not exist", path)
Yuke Liaoea228d02018-01-05 19:10:33618 subprocess_cmd = [
619 LLVM_COV_PATH, 'export', '-summary-only',
620 '-instr-profile=' + profdata_file_path, binary_paths[0]
621 ]
622 subprocess_cmd.extend(
623 ['-object=' + binary_path for binary_path in binary_paths[1:]])
Yuke Liaob2926832018-03-02 17:34:29624 _AddArchArgumentForIOSIfNeeded(subprocess_cmd, len(binary_paths))
Yuke Liaoea228d02018-01-05 19:10:33625 subprocess_cmd.extend(filters)
Yuke Liao0e4c8682018-04-18 21:06:59626 if ignore_filename_regex:
627 subprocess_cmd.append('-ignore-filename-regex=%s' % ignore_filename_regex)
Yuke Liaoea228d02018-01-05 19:10:33628
Max Moroz7c5354f2018-05-06 00:03:48629 export_output = subprocess.check_output(subprocess_cmd)
630
631 # Write output on the disk to be used by code coverage bot.
632 with open(_GetSummaryFilePath(), 'w') as f:
633 f.write(export_output)
634
Max Moroz1de68d72018-08-21 13:38:18635 return export_output
Yuke Liaoea228d02018-01-05 19:10:33636
637
Yuke Liaob2926832018-03-02 17:34:29638def _AddArchArgumentForIOSIfNeeded(cmd_list, num_archs):
639 """Appends -arch arguments to the command list if it's ios platform.
640
641 iOS binaries are universal binaries, and require specifying the architecture
642 to use, and one architecture needs to be specified for each binary.
643 """
644 if _IsIOS():
645 cmd_list.extend(['-arch=x86_64'] * num_archs)
646
647
Yuke Liao506e8822017-12-04 16:52:54648def _GetBinaryPath(command):
649 """Returns a relative path to the binary to be run by the command.
650
Yuke Liao545db322018-02-15 17:12:01651 Currently, following types of commands are supported (e.g. url_unittests):
652 1. Run test binary direcly: "out/coverage/url_unittests <arguments>"
653 2. Use xvfb.
654 2.1. "python testing/xvfb.py out/coverage/url_unittests <arguments>"
655 2.2. "testing/xvfb.py out/coverage/url_unittests <arguments>"
Yuke Liao92107f02018-03-07 01:44:37656 3. Use iossim to run tests on iOS platform, please refer to testing/iossim.mm
657 for its usage.
Yuke Liaoa0c8c2f2018-02-28 20:14:10658 3.1. "out/Coverage-iphonesimulator/iossim
Yuke Liao92107f02018-03-07 01:44:37659 <iossim_arguments> -c <app_arguments>
660 out/Coverage-iphonesimulator/url_unittests.app"
661
Yuke Liao506e8822017-12-04 16:52:54662 Args:
663 command: A command used to run a target.
664
665 Returns:
666 A relative path to the binary.
667 """
Yuke Liao545db322018-02-15 17:12:01668 xvfb_script_name = os.extsep.join(['xvfb', 'py'])
669
Sajjad Mirza0b96e002020-11-10 19:32:55670 command_parts = _SplitCommand(command)
Yuke Liao545db322018-02-15 17:12:01671 if os.path.basename(command_parts[0]) == 'python':
672 assert os.path.basename(command_parts[1]) == xvfb_script_name, (
Abhishek Aryafb70b532018-05-06 17:47:40673 'This tool doesn\'t understand the command: "%s".' % command)
Yuke Liao545db322018-02-15 17:12:01674 return command_parts[2]
675
676 if os.path.basename(command_parts[0]) == xvfb_script_name:
677 return command_parts[1]
678
Yuke Liaob2926832018-03-02 17:34:29679 if _IsIOSCommand(command):
Yuke Liaoa0c8c2f2018-02-28 20:14:10680 # For a given application bundle, the binary resides in the bundle and has
681 # the same name with the application without the .app extension.
Artem Titarenko2b464952018-11-07 17:22:02682 app_path = command_parts[1].rstrip(os.path.sep)
Yuke Liaoa0c8c2f2018-02-28 20:14:10683 app_name = os.path.splitext(os.path.basename(app_path))[0]
684 return os.path.join(app_path, app_name)
685
Sajjad Mirza07f52332020-11-11 01:50:47686 if coverage_utils.GetHostPlatform() == 'win' \
687 and not command_parts[0].endswith('.exe'):
688 return command_parts[0] + '.exe'
689
Yuke Liaob2926832018-03-02 17:34:29690 return command_parts[0]
Yuke Liao506e8822017-12-04 16:52:54691
692
Yuke Liaob2926832018-03-02 17:34:29693def _IsIOSCommand(command):
Yuke Liaoa0c8c2f2018-02-28 20:14:10694 """Returns true if command is used to run tests on iOS platform."""
Sajjad Mirza0b96e002020-11-10 19:32:55695 return os.path.basename(_SplitCommand(command)[0]) == 'iossim'
Yuke Liaoa0c8c2f2018-02-28 20:14:10696
697
Yuke Liao95d13d72017-12-07 18:18:50698def _VerifyTargetExecutablesAreInBuildDirectory(commands):
699 """Verifies that the target executables specified in the commands are inside
700 the given build directory."""
Yuke Liao506e8822017-12-04 16:52:54701 for command in commands:
702 binary_path = _GetBinaryPath(command)
Max Moroz1de68d72018-08-21 13:38:18703 binary_absolute_path = coverage_utils.GetFullPath(binary_path)
Abhishek Arya03911092018-05-21 16:42:35704 assert binary_absolute_path.startswith(BUILD_DIR + os.sep), (
Yuke Liao95d13d72017-12-07 18:18:50705 'Target executable "%s" in command: "%s" is outside of '
706 'the given build directory: "%s".' % (binary_path, command, BUILD_DIR))
Yuke Liao506e8822017-12-04 16:52:54707
708
709def _ValidateBuildingWithClangCoverage():
710 """Asserts that targets are built with Clang coverage enabled."""
Yuke Liao80afff32018-03-07 01:26:20711 build_args = _GetBuildArgs()
Yuke Liao506e8822017-12-04 16:52:54712
713 if (CLANG_COVERAGE_BUILD_ARG not in build_args or
714 build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'):
Abhishek Arya1ec832c2017-12-05 18:06:59715 assert False, ('\'{} = true\' is required in args.gn.'
716 ).format(CLANG_COVERAGE_BUILD_ARG)
Yuke Liao506e8822017-12-04 16:52:54717
718
Yuke Liaoc60b2d02018-03-02 21:40:43719def _ValidateCurrentPlatformIsSupported():
720 """Asserts that this script suports running on the current platform"""
721 target_os = _GetTargetOS()
722 if target_os:
723 current_platform = target_os
724 else:
Max Moroz1de68d72018-08-21 13:38:18725 current_platform = coverage_utils.GetHostPlatform()
Yuke Liaoc60b2d02018-03-02 21:40:43726
Ben Joyce88282362021-01-29 23:53:31727 supported_platforms = ['android', 'chromeos', 'ios', 'linux', 'mac', 'win']
728 assert current_platform in supported_platforms, ('Coverage is only'
729 'supported on %s' %
730 supported_platforms)
Yuke Liaoc60b2d02018-03-02 21:40:43731
732
Yuke Liao80afff32018-03-07 01:26:20733def _GetBuildArgs():
Yuke Liao506e8822017-12-04 16:52:54734 """Parses args.gn file and returns results as a dictionary.
735
736 Returns:
737 A dictionary representing the build args.
738 """
Yuke Liao80afff32018-03-07 01:26:20739 global _BUILD_ARGS
740 if _BUILD_ARGS is not None:
741 return _BUILD_ARGS
742
743 _BUILD_ARGS = {}
Yuke Liao506e8822017-12-04 16:52:54744 build_args_path = os.path.join(BUILD_DIR, 'args.gn')
745 assert os.path.exists(build_args_path), ('"%s" is not a build directory, '
746 'missing args.gn file.' % BUILD_DIR)
747 with open(build_args_path) as build_args_file:
748 build_args_lines = build_args_file.readlines()
749
Yuke Liao506e8822017-12-04 16:52:54750 for build_arg_line in build_args_lines:
751 build_arg_without_comments = build_arg_line.split('#')[0]
752 key_value_pair = build_arg_without_comments.split('=')
753 if len(key_value_pair) != 2:
754 continue
755
756 key = key_value_pair[0].strip()
Yuke Liaoc60b2d02018-03-02 21:40:43757
758 # Values are wrapped within a pair of double-quotes, so remove the leading
759 # and trailing double-quotes.
760 value = key_value_pair[1].strip().strip('"')
Yuke Liao80afff32018-03-07 01:26:20761 _BUILD_ARGS[key] = value
Yuke Liao506e8822017-12-04 16:52:54762
Yuke Liao80afff32018-03-07 01:26:20763 return _BUILD_ARGS
Yuke Liao506e8822017-12-04 16:52:54764
765
Abhishek Arya16f059a2017-12-07 17:47:32766def _VerifyPathsAndReturnAbsolutes(paths):
767 """Verifies that the paths specified in |paths| exist and returns absolute
768 versions.
Yuke Liao66da1732017-12-05 22:19:42769
770 Args:
771 paths: A list of files or directories.
772 """
Abhishek Arya16f059a2017-12-07 17:47:32773 absolute_paths = []
Yuke Liao66da1732017-12-05 22:19:42774 for path in paths:
Abhishek Arya16f059a2017-12-07 17:47:32775 absolute_path = os.path.join(SRC_ROOT_PATH, path)
776 assert os.path.exists(absolute_path), ('Path: "%s" doesn\'t exist.' % path)
777
778 absolute_paths.append(absolute_path)
779
780 return absolute_paths
Yuke Liao66da1732017-12-05 22:19:42781
782
Abhishek Arya64636af2018-05-04 14:42:13783def _GetBinaryPathsFromTargets(targets, build_dir):
784 """Return binary paths from target names."""
Ben Joyce88282362021-01-29 23:53:31785 # TODO(crbug.com/899974): Derive output binary from target build definitions
786 # rather than assuming that it is always the same name.
Abhishek Arya64636af2018-05-04 14:42:13787 binary_paths = []
788 for target in targets:
789 binary_path = os.path.join(build_dir, target)
Max Moroz1de68d72018-08-21 13:38:18790 if coverage_utils.GetHostPlatform() == 'win':
Abhishek Arya64636af2018-05-04 14:42:13791 binary_path += '.exe'
792
793 if os.path.exists(binary_path):
794 binary_paths.append(binary_path)
795 else:
796 logging.warning(
Abhishek Aryafb70b532018-05-06 17:47:40797 'Target binary "%s" not found in build directory, skipping.',
Abhishek Arya64636af2018-05-04 14:42:13798 os.path.basename(binary_path))
799
800 return binary_paths
801
802
Abhishek Arya03911092018-05-21 16:42:35803def _GetCommandForWebTests(arguments):
804 """Return command to run for blink web tests."""
Dirk Pranke34c093a42021-03-25 19:19:05805 cpu_count = multiprocessing.cpu_count()
806 if sys.platform == 'win32':
807 # TODO(crbug.com/1190269) - we can't use more than 56
808 # cores on Windows or Python3 may hang.
809 cpu_count = min(cpu_count, 56)
810 cpu_count = max(1, cpu_count // 2)
811
Abhishek Arya03911092018-05-21 16:42:35812 command_list = [
813 'python', 'testing/xvfb.py', 'python',
814 'third_party/blink/tools/run_web_tests.py',
815 '--additional-driver-flag=--no-sandbox',
Abhishek Aryabd0655d2018-05-21 19:55:24816 '--additional-env-var=LLVM_PROFILE_FILE=%s' %
Robert Ma339fc472018-12-14 21:22:42817 LLVM_PROFILE_FILE_PATH_SUBSTITUTION,
Dirk Pranke34c093a42021-03-25 19:19:05818 '--child-processes=%d' % cpu_count, '--disable-breakpad',
819 '--no-show-results', '--skip-failing-tests',
Abhishek Arya03911092018-05-21 16:42:35820 '--target=%s' % os.path.basename(BUILD_DIR), '--time-out-ms=30000'
821 ]
822 if arguments.strip():
823 command_list.append(arguments)
824 return ' '.join(command_list)
825
826
Ben Joyce88282362021-01-29 23:53:31827def _GetBinaryPathsForAndroid(targets):
828 """Return binary paths used when running android tests."""
829 # TODO(crbug.com/899974): Implement approach that doesn't assume .so file is
830 # based on the target's name.
831 android_binaries = set()
832 for target in targets:
833 so_library_path = os.path.join(BUILD_DIR, 'lib.unstripped',
834 'lib%s__library.so' % target)
835 if os.path.exists(so_library_path):
836 android_binaries.add(so_library_path)
837
838 return list(android_binaries)
839
840
Abhishek Arya03911092018-05-21 16:42:35841def _GetBinaryPathForWebTests():
842 """Return binary path used to run blink web tests."""
Max Moroz1de68d72018-08-21 13:38:18843 host_platform = coverage_utils.GetHostPlatform()
Abhishek Arya03911092018-05-21 16:42:35844 if host_platform == 'win':
845 return os.path.join(BUILD_DIR, 'content_shell.exe')
846 elif host_platform == 'linux':
847 return os.path.join(BUILD_DIR, 'content_shell')
848 elif host_platform == 'mac':
849 return os.path.join(BUILD_DIR, 'Content Shell.app', 'Contents', 'MacOS',
850 'Content Shell')
851 else:
852 assert False, 'This platform is not supported for web tests.'
853
854
Abhishek Aryae5811afa2018-05-24 03:56:01855def _SetupOutputDir():
856 """Setup output directory."""
857 if os.path.exists(OUTPUT_DIR):
858 shutil.rmtree(OUTPUT_DIR)
859
860 # Creates |OUTPUT_DIR| and its platform sub-directory.
Max Moroz1de68d72018-08-21 13:38:18861 os.makedirs(coverage_utils.GetCoverageReportRootDirPath(OUTPUT_DIR))
Abhishek Aryae5811afa2018-05-24 03:56:01862
863
Yuke Liaoabfbba42019-06-11 16:03:59864def _SetMacXcodePath():
865 """Set DEVELOPER_DIR to the path to hermetic Xcode.app on Mac OS X."""
866 if sys.platform != 'darwin':
867 return
868
869 xcode_path = os.path.join(SRC_ROOT_PATH, 'build', 'mac_files', 'Xcode.app')
870 if os.path.exists(xcode_path):
871 os.environ['DEVELOPER_DIR'] = xcode_path
872
873
Yuke Liao506e8822017-12-04 16:52:54874def _ParseCommandArguments():
875 """Adds and parses relevant arguments for tool comands.
876
877 Returns:
878 A dictionary representing the arguments.
879 """
880 arg_parser = argparse.ArgumentParser()
881 arg_parser.usage = __doc__
882
Abhishek Arya1ec832c2017-12-05 18:06:59883 arg_parser.add_argument(
884 '-b',
885 '--build-dir',
886 type=str,
887 required=True,
888 help='The build directory, the path needs to be relative to the root of '
889 'the checkout.')
Yuke Liao506e8822017-12-04 16:52:54890
Abhishek Arya1ec832c2017-12-05 18:06:59891 arg_parser.add_argument(
892 '-o',
893 '--output-dir',
894 type=str,
895 required=True,
896 help='Output directory for generated artifacts.')
Yuke Liao506e8822017-12-04 16:52:54897
Abhishek Arya1ec832c2017-12-05 18:06:59898 arg_parser.add_argument(
899 '-c',
900 '--command',
901 action='append',
Abhishek Arya64636af2018-05-04 14:42:13902 required=False,
Abhishek Arya1ec832c2017-12-05 18:06:59903 help='Commands used to run test targets, one test target needs one and '
904 'only one command, when specifying commands, one should assume the '
Abhishek Arya64636af2018-05-04 14:42:13905 'current working directory is the root of the checkout. This option is '
906 'incompatible with -p/--profdata-file option.')
907
908 arg_parser.add_argument(
Abhishek Arya03911092018-05-21 16:42:35909 '-wt',
910 '--web-tests',
911 nargs='?',
912 type=str,
913 const=' ',
914 required=False,
915 help='Run blink web tests. Support passing arguments to run_web_tests.py')
916
917 arg_parser.add_argument(
Abhishek Arya64636af2018-05-04 14:42:13918 '-p',
919 '--profdata-file',
920 type=str,
921 required=False,
922 help='Path to profdata file to use for generating code coverage reports. '
923 'This can be useful if you generated the profdata file seperately in '
924 'your own test harness. This option is ignored if run command(s) are '
925 'already provided above using -c/--command option.')
Yuke Liao506e8822017-12-04 16:52:54926
Abhishek Arya1ec832c2017-12-05 18:06:59927 arg_parser.add_argument(
Yuke Liao66da1732017-12-05 22:19:42928 '-f',
929 '--filters',
930 action='append',
Abhishek Arya16f059a2017-12-07 17:47:32931 required=False,
Yuke Liao66da1732017-12-05 22:19:42932 help='Directories or files to get code coverage for, and all files under '
933 'the directories are included recursively.')
934
935 arg_parser.add_argument(
Yuke Liao0e4c8682018-04-18 21:06:59936 '-i',
937 '--ignore-filename-regex',
938 type=str,
939 help='Skip source code files with file paths that match the given '
940 'regular expression. For example, use -i=\'.*/out/.*|.*/third_party/.*\' '
941 'to exclude files in third_party/ and out/ folders from the report.')
942
943 arg_parser.add_argument(
Yuke Liao1b852fd2018-05-11 17:07:32944 '--no-file-view',
945 action='store_true',
946 help='Don\'t generate the file view in the coverage report. When there '
947 'are large number of html files, the file view becomes heavy and may '
948 'cause the browser to freeze, and this argument comes handy.')
949
950 arg_parser.add_argument(
Max Moroz1de68d72018-08-21 13:38:18951 '--no-component-view',
952 action='store_true',
953 help='Don\'t generate the component view in the coverage report.')
954
955 arg_parser.add_argument(
Yuke Liao082e99632018-05-18 15:40:40956 '--coverage-tools-dir',
957 type=str,
958 help='Path of the directory where LLVM coverage tools (llvm-cov, '
959 'llvm-profdata) exist. This should be only needed if you are testing '
960 'against a custom built clang revision. Otherwise, we pick coverage '
961 'tools automatically from your current source checkout.')
962
963 arg_parser.add_argument(
Abhishek Arya1ec832c2017-12-05 18:06:59964 '-j',
965 '--jobs',
966 type=int,
967 default=None,
968 help='Run N jobs to build in parallel. If not specified, a default value '
Max Moroz06576292019-01-03 19:22:52969 'will be derived based on CPUs and goma availability. Please refer to '
970 '\'autoninja -h\' for more details.')
Yuke Liao506e8822017-12-04 16:52:54971
Abhishek Arya1ec832c2017-12-05 18:06:59972 arg_parser.add_argument(
Sahel Sharify38cabdc2020-01-16 00:40:01973 '--format',
974 type=str,
975 default='html',
976 help='Output format of the "llvm-cov show" command. The supported '
977 'formats are "text" and "html".')
978
979 arg_parser.add_argument(
Yuke Liao481d3482018-01-29 19:17:10980 '-v',
981 '--verbose',
982 action='store_true',
983 help='Prints additional output for diagnostics.')
984
985 arg_parser.add_argument(
986 '-l', '--log_file', type=str, help='Redirects logs to a file.')
987
988 arg_parser.add_argument(
Abhishek Aryac19bc5ef2018-05-04 22:10:02989 'targets',
990 nargs='+',
991 help='The names of the test targets to run. If multiple run commands are '
992 'specified using the -c/--command option, then the order of targets and '
993 'commands must match, otherwise coverage generation will fail.')
Yuke Liao506e8822017-12-04 16:52:54994
995 args = arg_parser.parse_args()
996 return args
997
998
999def Main():
1000 """Execute tool commands."""
Yuke Liao082e99632018-05-18 15:40:401001
Abhishek Arya64636af2018-05-04 14:42:131002 # Change directory to source root to aid in relative paths calculations.
1003 os.chdir(SRC_ROOT_PATH)
Abhishek Arya8a0751a2018-05-03 18:53:111004
pasthanaa4844112020-05-21 18:03:551005 # Setup coverage binaries even when script is called with empty params. This
1006 # is used by coverage bot for initial setup.
1007 if len(sys.argv) == 1:
Choongwoo Hanbd1aa952021-06-09 22:25:381008 subprocess.check_call([
1009 'python', 'tools/clang/scripts/update.py', '--package', 'coverage_tools'
1010 ])
pasthanaa4844112020-05-21 18:03:551011 print(__doc__)
1012 return
1013
Yuke Liao506e8822017-12-04 16:52:541014 args = _ParseCommandArguments()
Max Moroz1de68d72018-08-21 13:38:181015 coverage_utils.ConfigureLogging(verbose=args.verbose, log_file=args.log_file)
Yuke Liao082e99632018-05-18 15:40:401016 _ConfigureLLVMCoverageTools(args)
Abhishek Arya64636af2018-05-04 14:42:131017
Yuke Liao506e8822017-12-04 16:52:541018 global BUILD_DIR
Max Moroz1de68d72018-08-21 13:38:181019 BUILD_DIR = coverage_utils.GetFullPath(args.build_dir)
Abhishek Aryae5811afa2018-05-24 03:56:011020
Yuke Liao506e8822017-12-04 16:52:541021 global OUTPUT_DIR
Max Moroz1de68d72018-08-21 13:38:181022 OUTPUT_DIR = coverage_utils.GetFullPath(args.output_dir)
Yuke Liao506e8822017-12-04 16:52:541023
Abhishek Arya03911092018-05-21 16:42:351024 assert args.web_tests or args.command or args.profdata_file, (
Abhishek Arya64636af2018-05-04 14:42:131025 'Need to either provide commands to run using -c/--command option OR '
Abhishek Arya03911092018-05-21 16:42:351026 'provide prof-data file as input using -p/--profdata-file option OR '
1027 'run web tests using -wt/--run-web-tests.')
Yuke Liaoc60b2d02018-03-02 21:40:431028
Abhishek Arya64636af2018-05-04 14:42:131029 assert not args.command or (len(args.targets) == len(args.command)), (
1030 'Number of targets must be equal to the number of test commands.')
Yuke Liaoc60b2d02018-03-02 21:40:431031
Abhishek Arya1ec832c2017-12-05 18:06:591032 assert os.path.exists(BUILD_DIR), (
Abhishek Aryafb70b532018-05-06 17:47:401033 'Build directory: "%s" doesn\'t exist. '
1034 'Please run "gn gen" to generate.' % BUILD_DIR)
Abhishek Arya64636af2018-05-04 14:42:131035
Yuke Liaoc60b2d02018-03-02 21:40:431036 _ValidateCurrentPlatformIsSupported()
Yuke Liao506e8822017-12-04 16:52:541037 _ValidateBuildingWithClangCoverage()
Abhishek Arya16f059a2017-12-07 17:47:321038
1039 absolute_filter_paths = []
Yuke Liao66da1732017-12-05 22:19:421040 if args.filters:
Abhishek Arya16f059a2017-12-07 17:47:321041 absolute_filter_paths = _VerifyPathsAndReturnAbsolutes(args.filters)
Yuke Liao66da1732017-12-05 22:19:421042
Abhishek Aryae5811afa2018-05-24 03:56:011043 _SetupOutputDir()
Yuke Liao506e8822017-12-04 16:52:541044
Abhishek Arya03911092018-05-21 16:42:351045 # Get .profdata file and list of binary paths.
1046 if args.web_tests:
1047 commands = [_GetCommandForWebTests(args.web_tests)]
1048 profdata_file_path = _CreateCoverageProfileDataForTargets(
1049 args.targets, commands, args.jobs)
1050 binary_paths = [_GetBinaryPathForWebTests()]
1051 elif args.command:
1052 for i in range(len(args.command)):
1053 assert not 'run_web_tests.py' in args.command[i], (
1054 'run_web_tests.py is not supported via --command argument. '
1055 'Please use --run-web-tests argument instead.')
1056
Abhishek Arya64636af2018-05-04 14:42:131057 # A list of commands are provided. Run them to generate profdata file, and
1058 # create a list of binary paths from parsing commands.
1059 _VerifyTargetExecutablesAreInBuildDirectory(args.command)
1060 profdata_file_path = _CreateCoverageProfileDataForTargets(
1061 args.targets, args.command, args.jobs)
1062 binary_paths = [_GetBinaryPath(command) for command in args.command]
1063 else:
1064 # An input prof-data file is already provided. Just calculate binary paths.
1065 profdata_file_path = args.profdata_file
1066 binary_paths = _GetBinaryPathsFromTargets(args.targets, args.build_dir)
Yuke Liaoea228d02018-01-05 19:10:331067
Erik Chen283b92c72019-07-22 16:37:391068 # If the checkout uses the hermetic xcode binaries, then otool must be
1069 # directly invoked. The indirection via /usr/bin/otool won't work unless
1070 # there's an actual system install of Xcode.
1071 otool_path = None
1072 if sys.platform == 'darwin':
1073 hermetic_otool_path = os.path.join(
1074 SRC_ROOT_PATH, 'build', 'mac_files', 'xcode_binaries', 'Contents',
1075 'Developer', 'Toolchains', 'XcodeDefault.xctoolchain', 'usr', 'bin',
1076 'otool')
1077 if os.path.exists(hermetic_otool_path):
1078 otool_path = hermetic_otool_path
Ben Joyce88282362021-01-29 23:53:311079
1080 if _IsAndroid():
1081 binary_paths = _GetBinaryPathsForAndroid(args.targets)
1082 elif sys.platform.startswith('linux') or sys.platform.startswith('darwin'):
Brent McBrideb25b177a42020-05-11 18:13:061083 binary_paths.extend(
1084 coverage_utils.GetSharedLibraries(binary_paths, BUILD_DIR, otool_path))
Abhishek Arya78120bc2018-05-07 20:53:541085
Sahel Sharify38cabdc2020-01-16 00:40:011086 assert args.format == 'html' or args.format == 'text', (
1087 '%s is not a valid output format for "llvm-cov show". Only "text" and '
1088 '"html" formats are supported.' % (args.format))
1089 logging.info('Generating code coverage report in %s (this can take a while '
1090 'depending on size of target!).' % (args.format))
Max Moroz1de68d72018-08-21 13:38:181091 per_file_summary_data = _GeneratePerFileCoverageSummary(
Yuke Liao0e4c8682018-04-18 21:06:591092 binary_paths, profdata_file_path, absolute_filter_paths,
1093 args.ignore_filename_regex)
Sahel Sharify38cabdc2020-01-16 00:40:011094 _GeneratePerFileLineByLineCoverageInFormat(
1095 binary_paths, profdata_file_path, absolute_filter_paths,
1096 args.ignore_filename_regex, args.format)
Max Moroz1de68d72018-08-21 13:38:181097 component_mappings = None
1098 if not args.no_component_view:
Choongwoo Hanbd1aa952021-06-09 22:25:381099 component_mappings = json.load(urlopen(COMPONENT_MAPPING_URL))
Yuke Liaodd1ec0592018-02-02 01:26:371100
Max Moroz1de68d72018-08-21 13:38:181101 # Call prepare here.
1102 processor = coverage_utils.CoverageReportPostProcessor(
1103 OUTPUT_DIR,
1104 SRC_ROOT_PATH,
1105 per_file_summary_data,
1106 no_component_view=args.no_component_view,
1107 no_file_view=args.no_file_view,
1108 component_mappings=component_mappings)
Yuke Liaodd1ec0592018-02-02 01:26:371109
Sahel Sharify38cabdc2020-01-16 00:40:011110 if args.format == 'html':
1111 processor.PrepareHtmlReport()
Yuke Liao506e8822017-12-04 16:52:541112
Abhishek Arya1ec832c2017-12-05 18:06:591113
Yuke Liao506e8822017-12-04 16:52:541114if __name__ == '__main__':
1115 sys.exit(Main())