blob: 36a5e9aa7754597367d38a5b36666406f8bf5eb0 [file] [log] [blame]
Yuke Liaobb571bd62018-10-31 21:51:521#!/usr/bin/env python
2# Copyright 2018 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.
Sajjad Mirzabf9e66a2019-03-07 21:49:065"""Removes code coverage flags from invocations of the Clang C/C++ compiler.
Yuke Liaobb571bd62018-10-31 21:51:526
Sajjad Mirzabf9e66a2019-03-07 21:49:067If the GN arg `use_clang_coverage=true`, this script will be invoked by default.
8GN will add coverage instrumentation flags to almost all source files.
9
10This script is used to remove instrumentation flags from a subset of the source
11files. By default, it will not remove flags from any files. If the option
12--files-to-instrument is passed, this script will remove flags from all files
13except the ones listed in --files-to-instrument.
14
15This script also contains hard-coded exclusion lists of files to never
16instrument, indexed by target operating system. Files in these lists have their
17flags removed in both modes. The OS can be selected with --target-os.
Yuke Liaobb571bd62018-10-31 21:51:5218
19The path to the coverage instrumentation input file should be relative to the
20root build directory, and the file consists of multiple lines where each line
21represents a path to a source file, and the specified paths must be relative to
22the root build directory. e.g. ../../base/task/post_task.cc for build
23directory 'out/Release'.
24
25One caveat with this compiler wrapper is that it may introduce unexpected
26behaviors in incremental builds when the file path to the coverage
27instrumentation input file changes between consecutive runs, so callers of this
28script are strongly advised to always use the same path such as
29"${root_build_dir}/coverage_instrumentation_input.txt".
30
31It's worth noting on try job builders, if the contents of the instrumentation
32file changes so that a file doesn't need to be instrumented any longer, it will
33be recompiled automatically because if try job B runs after try job A, the files
34that were instrumented in A will be updated (i.e., reverted to the checked in
35version) in B, and so they'll be considered out of date by ninja and recompiled.
36
37Example usage:
38 clang_code_coverage_wrapper.py \\
39 --files-to-instrument=coverage_instrumentation_input.txt
40"""
41
Raul Tambre9e24293b2019-05-12 06:11:0742from __future__ import print_function
43
Yuke Liaobb571bd62018-10-31 21:51:5244import argparse
45import os
46import subprocess
47import sys
48
49# Flags used to enable coverage instrumentation.
Sajjad Mirzabf9e66a2019-03-07 21:49:0650# Flags should be listed in the same order that they are added in
51# build/config/coverage/BUILD.gn
Yuke Liao5eff7822019-02-28 03:56:2452_COVERAGE_FLAGS = [
Sajjad Mirza49c00e32019-03-01 22:46:5253 '-fprofile-instr-generate', '-fcoverage-mapping',
54 # Following experimental flags remove unused header functions from the
55 # coverage mapping data embedded in the test binaries, and the reduction
56 # of binary size enables building Chrome's large unit test targets on
57 # MacOS. Please refer to crbug.com/796290 for more details.
58 '-mllvm', '-limited-coverage-experimental=true'
Yuke Liao5eff7822019-02-28 03:56:2459]
Yuke Liaobb571bd62018-10-31 21:51:5260
Sajjad Mirza750bd0b2019-10-16 23:39:5161# Files that should not be built with coverage flags by default.
62_DEFAULT_COVERAGE_EXCLUSION_LIST = []
63
Sajjad Mirzabf9e66a2019-03-07 21:49:0664# Map of exclusion lists indexed by target OS.
65# If no target OS is defined, or one is defined that doesn't have a specific
Sajjad Mirza750bd0b2019-10-16 23:39:5166# entry, use _DEFAULT_COVERAGE_EXCLUSION_LIST.
Sajjad Mirzabf9e66a2019-03-07 21:49:0667_COVERAGE_EXCLUSION_LIST_MAP = {
Yuke Liao8098d702019-08-05 23:57:4168 'linux': [
69 # These files caused a static initializer to be generated, which
70 # shouldn't.
71 # TODO(crbug.com/990948): Remove when the bug is fixed.
Sajjad Mirza750bd0b2019-10-16 23:39:5172 '../../chrome/browser/media/router/providers/cast/cast_internal_message_util.cc', #pylint: disable=line-too-long
Yuke Liao8098d702019-08-05 23:57:4173 '../../chrome/common/media_router/providers/cast/cast_media_source.cc',
74 '../../components/cast_channel/cast_channel_enum.cc',
Sajjad Mirza750bd0b2019-10-16 23:39:5175 '../../components/cast_channel/cast_message_util.cc',
Yuke Liao8098d702019-08-05 23:57:4176 ],
Sajjad Mirzabf9e66a2019-03-07 21:49:0677 'chromeos': [
78 # These files caused clang to crash while compiling them. They are
79 # excluded pending an investigation into the underlying compiler bug.
80 '../../third_party/webrtc/p2p/base/p2p_transport_channel.cc',
81 '../../third_party/icu/source/common/uts46.cpp',
82 '../../third_party/icu/source/common/ucnvmbcs.cpp',
83 '../../base/android/android_image_reader_compat.cc',
Sajjad Mirza750bd0b2019-10-16 23:39:5184 ],
85 'win': [],
Sajjad Mirzabf9e66a2019-03-07 21:49:0686}
87
88
Sajjad Mirza750bd0b2019-10-16 23:39:5189
Sajjad Mirzabf9e66a2019-03-07 21:49:0690def _remove_flags_from_command(command):
91 # We need to remove the coverage flags for this file, but we only want to
92 # remove them if we see the exact sequence defined in _COVERAGE_FLAGS.
93 # That ensures that we only remove the flags added by GN when
94 # "use_clang_coverage" is true. Otherwise, we would remove flags set by
95 # other parts of the build system.
96 start_flag = _COVERAGE_FLAGS[0]
97 num_flags = len(_COVERAGE_FLAGS)
98 start_idx = 0
99 try:
100 while True:
101 idx = command.index(start_flag, start_idx)
102 start_idx = idx + 1
103 if command[idx:idx+num_flags] == _COVERAGE_FLAGS:
104 del command[idx:idx+num_flags]
105 break
106 except ValueError:
107 pass
Yuke Liaobb571bd62018-10-31 21:51:52108
109def main():
110 # TODO(crbug.com/898695): Make this wrapper work on Windows platform.
111 arg_parser = argparse.ArgumentParser()
112 arg_parser.usage = __doc__
113 arg_parser.add_argument(
114 '--files-to-instrument',
115 type=str,
Yuke Liaobb571bd62018-10-31 21:51:52116 help='Path to a file that contains a list of file names to instrument.')
Sajjad Mirzabf9e66a2019-03-07 21:49:06117 arg_parser.add_argument(
118 '--target-os',
119 required=False,
120 help='The OS to compile for.')
Yuke Liaobb571bd62018-10-31 21:51:52121 arg_parser.add_argument('args', nargs=argparse.REMAINDER)
122 parsed_args = arg_parser.parse_args()
123
Sajjad Mirzabf9e66a2019-03-07 21:49:06124 if (parsed_args.files_to_instrument and
125 not os.path.isfile(parsed_args.files_to_instrument)):
Yuke Liaobb571bd62018-10-31 21:51:52126 raise Exception('Path to the coverage instrumentation file: "%s" doesn\'t '
127 'exist.' % parsed_args.files_to_instrument)
128
129 compile_command = parsed_args.args
Sajjad Mirzabf9e66a2019-03-07 21:49:06130 if not any('clang' in s for s in compile_command):
131 return subprocess.call(compile_command)
132
Sajjad Mirza750bd0b2019-10-16 23:39:51133 target_os = parsed_args.target_os
134
Yuke Liaobb571bd62018-10-31 21:51:52135 try:
136 # The command is assumed to use Clang as the compiler, and the path to the
137 # source file is behind the -c argument, and the path to the source path is
138 # relative to the root build directory. For example:
139 # clang++ -fvisibility=hidden -c ../../base/files/file_path.cc -o \
140 # obj/base/base/file_path.o
Sajjad Mirza750bd0b2019-10-16 23:39:51141 # On Windows, clang-cl.exe uses /c instead of -c.
142 source_flag = '/c' if target_os == 'win' else '-c'
143 source_flag_index = compile_command.index(source_flag)
Yuke Liaobb571bd62018-10-31 21:51:52144 except ValueError:
Sajjad Mirza750bd0b2019-10-16 23:39:51145 print('%s argument is not found in the compile command.' % source_flag)
Yuke Liaobb571bd62018-10-31 21:51:52146 raise
147
Sajjad Mirza750bd0b2019-10-16 23:39:51148 if source_flag_index + 1 >= len(compile_command):
Yuke Liaobb571bd62018-10-31 21:51:52149 raise Exception('Source file to be compiled is missing from the command.')
150
Sajjad Mirza750bd0b2019-10-16 23:39:51151 compile_source_file = compile_command[source_flag_index + 1]
152 exclusion_list = _COVERAGE_EXCLUSION_LIST_MAP.get(
153 target_os, _DEFAULT_COVERAGE_EXCLUSION_LIST)
Sajjad Mirzabf9e66a2019-03-07 21:49:06154
155 if compile_source_file in exclusion_list:
156 _remove_flags_from_command(compile_command)
157 elif parsed_args.files_to_instrument:
158 with open(parsed_args.files_to_instrument) as f:
159 if compile_source_file not in f.read():
160 _remove_flags_from_command(compile_command)
Yuke Liaobb571bd62018-10-31 21:51:52161
162 return subprocess.call(compile_command)
163
Yuke Liaobb571bd62018-10-31 21:51:52164if __name__ == '__main__':
165 sys.exit(main())