clang_code_coverage_wrapper.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #!/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.
  5. """Removes code coverage flags from invocations of the Clang C/C++ compiler.
  6. If the GN arg `use_clang_coverage=true`, this script will be invoked by default.
  7. GN will add coverage instrumentation flags to almost all source files.
  8. This script is used to remove instrumentation flags from a subset of the source
  9. files. By default, it will not remove flags from any files. If the option
  10. --files-to-instrument is passed, this script will remove flags from all files
  11. except the ones listed in --files-to-instrument.
  12. This script also contains hard-coded exclusion lists of files to never
  13. instrument, indexed by target operating system. Files in these lists have their
  14. flags removed in both modes. The OS can be selected with --target-os.
  15. This script also contains hard-coded force lists of files to always instrument,
  16. indexed by target operating system. Files in these lists never have their flags
  17. removed in either mode. The OS can be selected with --target-os.
  18. The order of precedence is: force list, exclusion list, --files-to-instrument.
  19. The path to the coverage instrumentation input file should be relative to the
  20. root build directory, and the file consists of multiple lines where each line
  21. represents a path to a source file, and the specified paths must be relative to
  22. the root build directory. e.g. ../../base/task/post_task.cc for build
  23. directory 'out/Release'. The paths should be written using OS-native path
  24. separators for the current platform.
  25. One caveat with this compiler wrapper is that it may introduce unexpected
  26. behaviors in incremental builds when the file path to the coverage
  27. instrumentation input file changes between consecutive runs, so callers of this
  28. script are strongly advised to always use the same path such as
  29. "${root_build_dir}/coverage_instrumentation_input.txt".
  30. It's worth noting on try job builders, if the contents of the instrumentation
  31. file changes so that a file doesn't need to be instrumented any longer, it will
  32. be recompiled automatically because if try job B runs after try job A, the files
  33. that were instrumented in A will be updated (i.e., reverted to the checked in
  34. version) in B, and so they'll be considered out of date by ninja and recompiled.
  35. Example usage:
  36. clang_code_coverage_wrapper.py \\
  37. --files-to-instrument=coverage_instrumentation_input.txt
  38. """
  39. from __future__ import print_function
  40. import argparse
  41. import os
  42. import subprocess
  43. import sys
  44. # Flags used to enable coverage instrumentation.
  45. # Flags should be listed in the same order that they are added in
  46. # build/config/coverage/BUILD.gn
  47. _COVERAGE_FLAGS = [
  48. '-fprofile-instr-generate',
  49. '-fcoverage-mapping',
  50. # Following experimental flags remove unused header functions from the
  51. # coverage mapping data embedded in the test binaries, and the reduction
  52. # of binary size enables building Chrome's large unit test targets on
  53. # MacOS. Please refer to crbug.com/796290 for more details.
  54. '-mllvm',
  55. '-limited-coverage-experimental=true',
  56. ]
  57. # Files that should not be built with coverage flags by default.
  58. _DEFAULT_COVERAGE_EXCLUSION_LIST = [
  59. # TODO(crbug.com/1051561): angle_unittests affected by coverage.
  60. '../../base/message_loop/message_pump_default.cc',
  61. '../../base/message_loop/message_pump_libevent.cc',
  62. '../../base/message_loop/message_pump_win.cc',
  63. '../../base/task/sequence_manager/thread_controller_with_message_pump_impl.cc', #pylint: disable=line-too-long
  64. ]
  65. # Map of exclusion lists indexed by target OS.
  66. # If no target OS is defined, or one is defined that doesn't have a specific
  67. # entry, use _DEFAULT_COVERAGE_EXCLUSION_LIST.
  68. _COVERAGE_EXCLUSION_LIST_MAP = {
  69. 'android': [
  70. # This file caused webview native library failed on arm64.
  71. '../../device/gamepad/dualshock4_controller.cc',
  72. ],
  73. 'fuchsia': [
  74. # TODO(crbug.com/1174725): These files caused clang to crash while
  75. # compiling them.
  76. '../../base/allocator/partition_allocator/pcscan.cc',
  77. '../../third_party/skia/src/core/SkOpts.cpp',
  78. '../../third_party/skia/src/opts/SkOpts_hsw.cpp',
  79. '../../third_party/skia/third_party/skcms/skcms.cc',
  80. ],
  81. 'linux': [
  82. # These files caused a static initializer to be generated, which
  83. # shouldn't.
  84. # TODO(crbug.com/990948): Remove when the bug is fixed.
  85. '../../chrome/browser/media/router/providers/cast/cast_internal_message_util.cc', #pylint: disable=line-too-long
  86. '../../components/cast_channel/cast_channel_enum.cc',
  87. '../../components/cast_channel/cast_message_util.cc',
  88. '../../components/media_router/common/providers/cast/cast_media_source.cc', #pylint: disable=line-too-long
  89. '../../ui/events/keycodes/dom/keycode_converter.cc',
  90. # TODO(crbug.com/1051561): angle_unittests affected by coverage.
  91. '../../base/message_loop/message_pump_default.cc',
  92. '../../base/message_loop/message_pump_libevent.cc',
  93. '../../base/message_loop/message_pump_win.cc',
  94. '../../base/task/sequence_manager/thread_controller_with_message_pump_impl.cc', #pylint: disable=line-too-long
  95. ],
  96. 'chromeos': [
  97. # These files caused clang to crash while compiling them. They are
  98. # excluded pending an investigation into the underlying compiler bug.
  99. '../../third_party/webrtc/p2p/base/p2p_transport_channel.cc',
  100. '../../third_party/icu/source/common/uts46.cpp',
  101. '../../third_party/icu/source/common/ucnvmbcs.cpp',
  102. '../../base/android/android_image_reader_compat.cc',
  103. # TODO(crbug.com/1051561): angle_unittests affected by coverage.
  104. '../../base/message_loop/message_pump_default.cc',
  105. '../../base/message_loop/message_pump_libevent.cc',
  106. '../../base/message_loop/message_pump_win.cc',
  107. '../../base/task/sequence_manager/thread_controller_with_message_pump_impl.cc', #pylint: disable=line-too-long
  108. ],
  109. 'win': [
  110. # TODO(crbug.com/1051561): angle_unittests affected by coverage.
  111. '../../base/message_loop/message_pump_default.cc',
  112. '../../base/message_loop/message_pump_libevent.cc',
  113. '../../base/message_loop/message_pump_win.cc',
  114. '../../base/task/sequence_manager/thread_controller_with_message_pump_impl.cc', #pylint: disable=line-too-long
  115. ],
  116. }
  117. # Map of force lists indexed by target OS.
  118. _COVERAGE_FORCE_LIST_MAP = {
  119. # clang_profiling.cc refers to the symbol `__llvm_profile_dump` from the
  120. # profiling runtime. In a partial coverage build, it is possible for a
  121. # binary to include clang_profiling.cc but have no instrumented files, thus
  122. # causing an unresolved symbol error because the profiling runtime will not
  123. # be linked in. Therefore we force coverage for this file to ensure that
  124. # any target that includes it will also get the profiling runtime.
  125. 'win': [r'..\..\base\test\clang_profiling.cc'],
  126. # TODO(crbug.com/1141727) We're seeing runtime LLVM errors in mac-rel when
  127. # no files are changed, so we suspect that this is similar to the other
  128. # problem with clang_profiling.cc on Windows. The TODO here is to force
  129. # coverage for this specific file on ALL platforms, if it turns out to fix
  130. # this issue on Mac as well. It's the only file that directly calls
  131. # `__llvm_profile_dump` so it warrants some special treatment.
  132. 'mac': ['../../base/test/clang_profiling.cc'],
  133. }
  134. def _remove_flags_from_command(command):
  135. # We need to remove the coverage flags for this file, but we only want to
  136. # remove them if we see the exact sequence defined in _COVERAGE_FLAGS.
  137. # That ensures that we only remove the flags added by GN when
  138. # "use_clang_coverage" is true. Otherwise, we would remove flags set by
  139. # other parts of the build system.
  140. start_flag = _COVERAGE_FLAGS[0]
  141. num_flags = len(_COVERAGE_FLAGS)
  142. start_idx = 0
  143. try:
  144. while True:
  145. idx = command.index(start_flag, start_idx)
  146. if command[idx:idx + num_flags] == _COVERAGE_FLAGS:
  147. del command[idx:idx + num_flags]
  148. # There can be multiple sets of _COVERAGE_FLAGS. All of these need to be
  149. # removed.
  150. start_idx = idx
  151. else:
  152. start_idx = idx + 1
  153. except ValueError:
  154. pass
  155. def main():
  156. arg_parser = argparse.ArgumentParser()
  157. arg_parser.usage = __doc__
  158. arg_parser.add_argument(
  159. '--files-to-instrument',
  160. type=str,
  161. help='Path to a file that contains a list of file names to instrument.')
  162. arg_parser.add_argument(
  163. '--target-os', required=False, help='The OS to compile for.')
  164. arg_parser.add_argument('args', nargs=argparse.REMAINDER)
  165. parsed_args = arg_parser.parse_args()
  166. if (parsed_args.files_to_instrument and
  167. not os.path.isfile(parsed_args.files_to_instrument)):
  168. raise Exception('Path to the coverage instrumentation file: "%s" doesn\'t '
  169. 'exist.' % parsed_args.files_to_instrument)
  170. compile_command = parsed_args.args
  171. if not any('clang' in s for s in compile_command):
  172. return subprocess.call(compile_command)
  173. target_os = parsed_args.target_os
  174. try:
  175. # The command is assumed to use Clang as the compiler, and the path to the
  176. # source file is behind the -c argument, and the path to the source path is
  177. # relative to the root build directory. For example:
  178. # clang++ -fvisibility=hidden -c ../../base/files/file_path.cc -o \
  179. # obj/base/base/file_path.o
  180. # On Windows, clang-cl.exe uses /c instead of -c.
  181. source_flag = '/c' if target_os == 'win' else '-c'
  182. source_flag_index = compile_command.index(source_flag)
  183. except ValueError:
  184. print('%s argument is not found in the compile command.' % source_flag)
  185. raise
  186. if source_flag_index + 1 >= len(compile_command):
  187. raise Exception('Source file to be compiled is missing from the command.')
  188. # On Windows, filesystem paths should use '\', but GN creates build commands
  189. # that use '/'. We invoke os.path.normpath to ensure that the path uses the
  190. # correct separator for the current platform (i.e. '\' on Windows and '/'
  191. # otherwise).
  192. compile_source_file = os.path.normpath(compile_command[source_flag_index + 1])
  193. extension = os.path.splitext(compile_source_file)[1]
  194. if not extension in ['.c', '.cc', '.cpp', '.cxx', '.m', '.mm', '.S']:
  195. raise Exception('Invalid source file %s found' % compile_source_file)
  196. exclusion_list = _COVERAGE_EXCLUSION_LIST_MAP.get(
  197. target_os, _DEFAULT_COVERAGE_EXCLUSION_LIST)
  198. force_list = _COVERAGE_FORCE_LIST_MAP.get(target_os, [])
  199. should_remove_flags = False
  200. if compile_source_file not in force_list:
  201. if compile_source_file in exclusion_list:
  202. should_remove_flags = True
  203. elif parsed_args.files_to_instrument:
  204. with open(parsed_args.files_to_instrument) as f:
  205. if compile_source_file not in f.read():
  206. should_remove_flags = True
  207. if should_remove_flags:
  208. _remove_flags_from_command(compile_command)
  209. return subprocess.call(compile_command)
  210. if __name__ == '__main__':
  211. sys.exit(main())