Yuke Liao | 506e882 | 2017-12-04 16:52:54 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | # 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. |
| 5 | |
| 6 | """Script to generate Clang source based code coverage report. |
| 7 | |
| 8 | NOTE: This script must be called from the root of checkout, and because it |
| 9 | requires building with gn arg "is_component_build=false", the build is not |
| 10 | compatible with sanitizer flags (such as "is_asan" and "is_msan") and flag |
| 11 | "optimize_for_fuzzing". |
| 12 | |
| 13 | Example usages: |
| 14 | python tools/code_coverage/coverage.py crypto_unittests url_unittests |
| 15 | -b out/Coverage -o out/report -c 'out/Coverage/crypto_unittests' |
| 16 | -c 'out/Coverage/url_unittests --gtest_filter=URLParser.PathURL' |
| 17 | # Generate code coverage report for crypto_unittests and url_unittests and |
| 18 | # all generated artifacts are stored in out/report. For url_unittests, only |
| 19 | # run test URLParser.PathURL. |
| 20 | |
| 21 | For more options, please refer to tools/coverage/coverage.py -h for help. |
| 22 | """ |
| 23 | |
| 24 | from __future__ import print_function |
| 25 | |
| 26 | import sys |
| 27 | |
| 28 | import argparse |
| 29 | import os |
| 30 | import subprocess |
| 31 | import threading |
| 32 | import urllib2 |
| 33 | |
| 34 | sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir, |
| 35 | os.path.pardir, 'tools', 'clang', 'scripts')) |
| 36 | |
| 37 | import update as clang_update |
| 38 | |
| 39 | # Absolute path to the root of the checkout. |
| 40 | SRC_ROOT_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), |
| 41 | os.path.pardir, os.path.pardir)) |
| 42 | |
| 43 | # Absolute path to the code coverage tools binary. |
| 44 | LLVM_BUILD_DIR = clang_update.LLVM_BUILD_DIR |
| 45 | LLVM_COV_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-cov') |
| 46 | LLVM_PROFDATA_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-profdata') |
| 47 | |
| 48 | # Build directory, the value is parsed from command line arguments. |
| 49 | BUILD_DIR = None |
| 50 | |
| 51 | # Output directory for generated artifacts, the value is parsed from command |
| 52 | # line arguemnts. |
| 53 | OUTPUT_DIR = None |
| 54 | |
| 55 | # Default number of jobs used to build when goma is configured and enabled. |
| 56 | DEFAULT_GOMA_JOBS = 100 |
| 57 | |
| 58 | # Name of the file extension for profraw data files. |
| 59 | PROFRAW_FILE_EXTENSION = 'profraw' |
| 60 | |
| 61 | # Name of the final profdata file, and this file needs to be passed to |
| 62 | # "llvm-cov" command in order to call "llvm-cov show" to inspect the |
| 63 | # line-by-line coverage of specific files. |
| 64 | PROFDATA_FILE_NAME = 'coverage.profdata' |
| 65 | |
| 66 | # Build arg required for generating code coverage data. |
| 67 | CLANG_COVERAGE_BUILD_ARG = 'use_clang_coverage' |
| 68 | |
| 69 | # A set of targets that depend on target "testing/gtest", this set is generated |
| 70 | # by 'gn refs "testing/gtest"', and it is lazily initialized when needed. |
| 71 | GTEST_TARGET_NAMES = None |
| 72 | |
| 73 | |
| 74 | # TODO(crbug.com/759794): remove this function once tools get included to |
| 75 | # Clang bundle: |
| 76 | # https://chromium-review.googlesource.com/c/chromium/src/+/688221 |
| 77 | def DownloadCoverageToolsIfNeeded(): |
| 78 | """Temporary solution to download llvm-profdata and llvm-cov tools.""" |
| 79 | def _GetRevisionFromStampFile(stamp_file_path): |
| 80 | """Returns a pair of revision number by reading the build stamp file. |
| 81 | |
| 82 | Args: |
| 83 | stamp_file_path: A path the build stamp file created by |
| 84 | tools/clang/scripts/update.py. |
| 85 | Returns: |
| 86 | A pair of integers represeting the main and sub revision respectively. |
| 87 | """ |
| 88 | if not os.path.exists(stamp_file_path): |
| 89 | return 0, 0 |
| 90 | |
| 91 | with open(stamp_file_path) as stamp_file: |
| 92 | revision_stamp_data = stamp_file.readline().strip().split('-') |
| 93 | return int(revision_stamp_data[0]), int(revision_stamp_data[1]) |
| 94 | |
| 95 | clang_revision, clang_sub_revision = _GetRevisionFromStampFile( |
| 96 | clang_update.STAMP_FILE) |
| 97 | |
| 98 | coverage_revision_stamp_file = os.path.join( |
| 99 | os.path.dirname(clang_update.STAMP_FILE), 'cr_coverage_revision') |
| 100 | coverage_revision, coverage_sub_revision = _GetRevisionFromStampFile( |
| 101 | coverage_revision_stamp_file) |
| 102 | |
| 103 | if (coverage_revision == clang_revision and |
| 104 | coverage_sub_revision == clang_sub_revision): |
| 105 | # LLVM coverage tools are up to date, bail out. |
| 106 | return clang_revision |
| 107 | |
| 108 | package_version = '%d-%d' % (clang_revision, clang_sub_revision) |
| 109 | coverage_tools_file = 'llvm-code-coverage-%s.tgz' % package_version |
| 110 | |
| 111 | # The code bellow follows the code from tools/clang/scripts/update.py. |
| 112 | if sys.platform == 'win32' or sys.platform == 'cygwin': |
| 113 | coverage_tools_url = clang_update.CDS_URL + '/Win/' + coverage_tools_file |
| 114 | elif sys.platform == 'darwin': |
| 115 | coverage_tools_url = clang_update.CDS_URL + '/Mac/' + coverage_tools_file |
| 116 | else: |
| 117 | assert sys.platform.startswith('linux') |
| 118 | coverage_tools_url = ( |
| 119 | clang_update.CDS_URL + '/Linux_x64/' + coverage_tools_file) |
| 120 | |
| 121 | try: |
| 122 | clang_update.DownloadAndUnpack(coverage_tools_url, |
| 123 | clang_update.LLVM_BUILD_DIR) |
| 124 | print('Coverage tools %s unpacked' % package_version) |
| 125 | with open(coverage_revision_stamp_file, 'w') as file_handle: |
| 126 | file_handle.write(package_version) |
| 127 | file_handle.write('\n') |
| 128 | except urllib2.URLError: |
| 129 | raise Exception( |
| 130 | 'Failed to download coverage tools: %s.' % coverage_tools_url) |
| 131 | |
| 132 | |
| 133 | def _GenerateLineByLineFileCoverageInHtml(binary_paths, profdata_file_path): |
| 134 | """Generates per file line-by-line coverage in html using 'llvm-cov show'. |
| 135 | |
| 136 | For a file with absolute path /a/b/x.cc, a html report is generated as: |
| 137 | OUTPUT_DIR/coverage/a/b/x.cc.html. An index html file is also generated as: |
| 138 | OUTPUT_DIR/index.html. |
| 139 | |
| 140 | Args: |
| 141 | binary_paths: A list of paths to the instrumented binaries. |
| 142 | profdata_file_path: A path to the profdata file. |
| 143 | """ |
| 144 | print('Generating per file line by line code coverage in html') |
| 145 | |
| 146 | # llvm-cov show [options] -instr-profile PROFILE BIN [-object BIN,...] |
| 147 | # [[-object BIN]] [SOURCES] |
| 148 | # NOTE: For object files, the first one is specified as a positional argument, |
| 149 | # and the rest are specified as keyword argument. |
| 150 | subprocess_cmd = [LLVM_COV_PATH, 'show', '-format=html', |
| 151 | '-output-dir={}'.format(OUTPUT_DIR), |
| 152 | '-instr-profile={}'.format(profdata_file_path), |
| 153 | binary_paths[0]] |
| 154 | subprocess_cmd.extend(['-object=' + binary_path |
| 155 | for binary_path in binary_paths[1:]]) |
| 156 | |
| 157 | subprocess.check_call(subprocess_cmd) |
| 158 | |
| 159 | |
| 160 | def _CreateCoverageProfileDataForTargets(targets, commands, jobs_count=None): |
| 161 | """Builds and runs target to generate the coverage profile data. |
| 162 | |
| 163 | Args: |
| 164 | targets: A list of targets to build with coverage instrumentation. |
| 165 | commands: A list of commands used to run the targets. |
| 166 | jobs_count: Number of jobs to run in parallel for building. If None, a |
| 167 | default value is derived based on CPUs availability. |
| 168 | |
| 169 | Returns: |
| 170 | A relative path to the generated profdata file. |
| 171 | """ |
| 172 | _BuildTargets(targets, jobs_count) |
| 173 | profraw_file_paths = _GetProfileRawDataPathsByExecutingCommands(targets, |
| 174 | commands) |
| 175 | profdata_file_path = _CreateCoverageProfileDataFromProfRawData( |
| 176 | profraw_file_paths) |
| 177 | |
| 178 | return profdata_file_path |
| 179 | |
| 180 | |
| 181 | def _BuildTargets(targets, jobs_count): |
| 182 | """Builds target with Clang coverage instrumentation. |
| 183 | |
| 184 | This function requires current working directory to be the root of checkout. |
| 185 | |
| 186 | Args: |
| 187 | targets: A list of targets to build with coverage instrumentation. |
| 188 | jobs_count: Number of jobs to run in parallel for compilation. If None, a |
| 189 | default value is derived based on CPUs availability. |
| 190 | |
| 191 | |
| 192 | """ |
| 193 | def _IsGomaConfigured(): |
| 194 | """Returns True if goma is enabled in the gn build args. |
| 195 | |
| 196 | Returns: |
| 197 | A boolean indicates whether goma is configured for building or not. |
| 198 | """ |
| 199 | build_args = _ParseArgsGnFile() |
| 200 | return 'use_goma' in build_args and build_args['use_goma'] == 'true' |
| 201 | |
| 202 | print('Building %s' % str(targets)) |
| 203 | |
| 204 | if jobs_count is None and _IsGomaConfigured(): |
| 205 | jobs_count = DEFAULT_GOMA_JOBS |
| 206 | |
| 207 | subprocess_cmd = ['ninja', '-C', BUILD_DIR] |
| 208 | if jobs_count is not None: |
| 209 | subprocess_cmd.append('-j' + str(jobs_count)) |
| 210 | |
| 211 | subprocess_cmd.extend(targets) |
| 212 | subprocess.check_call(subprocess_cmd) |
| 213 | |
| 214 | |
| 215 | def _GetProfileRawDataPathsByExecutingCommands(targets, commands): |
| 216 | """Runs commands and returns the relative paths to the profraw data files. |
| 217 | |
| 218 | Args: |
| 219 | targets: A list of targets built with coverage instrumentation. |
| 220 | commands: A list of commands used to run the targets. |
| 221 | |
| 222 | Returns: |
| 223 | A list of relative paths to the generated profraw data files. |
| 224 | """ |
| 225 | # Remove existing profraw data files. |
| 226 | for file_or_dir in os.listdir(OUTPUT_DIR): |
| 227 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
| 228 | os.remove(os.path.join(OUTPUT_DIR, file_or_dir)) |
| 229 | |
| 230 | # Run different test targets in parallel to generate profraw data files. |
| 231 | threads = [] |
| 232 | for target, command in zip(targets, commands): |
| 233 | thread = threading.Thread(target=_ExecuteCommand, args=(target, command)) |
| 234 | thread.start() |
| 235 | threads.append(thread) |
| 236 | for thread in threads: |
| 237 | thread.join() |
| 238 | |
| 239 | profraw_file_paths = [] |
| 240 | for file_or_dir in os.listdir(OUTPUT_DIR): |
| 241 | if file_or_dir.endswith(PROFRAW_FILE_EXTENSION): |
| 242 | profraw_file_paths.append(os.path.join(OUTPUT_DIR, file_or_dir)) |
| 243 | |
| 244 | # Assert one target/command generates at least one profraw data file. |
| 245 | for target in targets: |
| 246 | assert any(os.path.basename(profraw_file).startswith(target) for |
| 247 | profraw_file in profraw_file_paths), ('Running target: %s ' |
| 248 | 'failed to generate any ' |
| 249 | 'profraw data file, ' |
| 250 | 'please make sure the ' |
| 251 | 'binary exists and is ' |
| 252 | 'properly instrumented.' |
| 253 | % target) |
| 254 | |
| 255 | return profraw_file_paths |
| 256 | |
| 257 | |
| 258 | def _ExecuteCommand(target, command): |
| 259 | """Runs a single command and generates a profraw data file. |
| 260 | |
| 261 | Args: |
| 262 | target: A target built with coverage instrumentation. |
| 263 | command: A command used to run the target. |
| 264 | """ |
| 265 | if _IsTargetGTestTarget(target): |
| 266 | # This test argument is required and only required for gtest unit test |
| 267 | # targets because by default, they run tests in parallel, and that won't |
| 268 | # generated code coverage data correctly. |
| 269 | command += ' --test-launcher-jobs=1' |
| 270 | |
| 271 | expected_profraw_file_name = os.extsep.join([target, '%p', |
| 272 | PROFRAW_FILE_EXTENSION]) |
| 273 | expected_profraw_file_path = os.path.join(OUTPUT_DIR, |
| 274 | expected_profraw_file_name) |
| 275 | output_file_name = os.extsep.join([target + '_output', 'txt']) |
| 276 | output_file_path = os.path.join(OUTPUT_DIR, output_file_name) |
| 277 | |
| 278 | print('Running command: "%s", the output is redirected to "%s"' % |
| 279 | (command, output_file_path)) |
| 280 | output = subprocess.check_output(command.split(), |
| 281 | env={'LLVM_PROFILE_FILE': |
| 282 | expected_profraw_file_path}) |
| 283 | with open(output_file_path, 'w') as output_file: |
| 284 | output_file.write(output) |
| 285 | |
| 286 | |
| 287 | def _CreateCoverageProfileDataFromProfRawData(profraw_file_paths): |
| 288 | """Returns a relative path to the profdata file by merging profraw data files. |
| 289 | |
| 290 | Args: |
| 291 | profraw_file_paths: A list of relative paths to the profraw data files that |
| 292 | are to be merged. |
| 293 | |
| 294 | Returns: |
| 295 | A relative path to the generated profdata file. |
| 296 | |
| 297 | Raises: |
| 298 | CalledProcessError: An error occurred merging profraw data files. |
| 299 | """ |
| 300 | print('Creating the profile data file') |
| 301 | |
| 302 | profdata_file_path = os.path.join(OUTPUT_DIR, PROFDATA_FILE_NAME) |
| 303 | try: |
| 304 | subprocess_cmd = [LLVM_PROFDATA_PATH, 'merge', '-o', profdata_file_path, |
| 305 | '-sparse=true'] |
| 306 | subprocess_cmd.extend(profraw_file_paths) |
| 307 | subprocess.check_call(subprocess_cmd) |
| 308 | except subprocess.CalledProcessError as error: |
| 309 | print('Failed to merge profraw files to create profdata file') |
| 310 | raise error |
| 311 | |
| 312 | return profdata_file_path |
| 313 | |
| 314 | |
| 315 | def _GetBinaryPath(command): |
| 316 | """Returns a relative path to the binary to be run by the command. |
| 317 | |
| 318 | Args: |
| 319 | command: A command used to run a target. |
| 320 | |
| 321 | Returns: |
| 322 | A relative path to the binary. |
| 323 | """ |
| 324 | return command.split()[0] |
| 325 | |
| 326 | |
| 327 | def _IsTargetGTestTarget(target): |
| 328 | """Returns True if the target is a gtest target. |
| 329 | |
| 330 | Args: |
| 331 | target: A target built with coverage instrumentation. |
| 332 | |
| 333 | Returns: |
| 334 | A boolean value indicates whether the target is a gtest target. |
| 335 | """ |
| 336 | global GTEST_TARGET_NAMES |
| 337 | if GTEST_TARGET_NAMES is None: |
| 338 | output = subprocess.check_output(['gn', 'refs', BUILD_DIR, 'testing/gtest']) |
| 339 | list_of_gtest_targets = [gtest_target |
| 340 | for gtest_target in output.splitlines() |
| 341 | if gtest_target] |
| 342 | GTEST_TARGET_NAMES = set([gtest_target.split(':')[1] |
| 343 | for gtest_target in list_of_gtest_targets]) |
| 344 | |
| 345 | return target in GTEST_TARGET_NAMES |
| 346 | |
| 347 | |
| 348 | def _ValidateCommandsAreRelativeToSrcRoot(commands): |
| 349 | for command in commands: |
| 350 | binary_path = _GetBinaryPath(command) |
| 351 | assert binary_path.startswith(BUILD_DIR), ('Target executable "%s" is ' |
| 352 | 'outside of the given build ' |
| 353 | 'directory: "%s". Please make ' |
| 354 | 'sure the command: "%s" is ' |
| 355 | 'relative to the root of the ' |
| 356 | 'checkout.' |
| 357 | %(binary_path, BUILD_DIR, |
| 358 | command)) |
| 359 | |
| 360 | |
| 361 | def _ValidateBuildingWithClangCoverage(): |
| 362 | """Asserts that targets are built with Clang coverage enabled.""" |
| 363 | build_args = _ParseArgsGnFile() |
| 364 | |
| 365 | if (CLANG_COVERAGE_BUILD_ARG not in build_args or |
| 366 | build_args[CLANG_COVERAGE_BUILD_ARG] != 'true'): |
| 367 | assert False, ('\'{} = true\' is required in args.gn.').format( |
| 368 | CLANG_COVERAGE_BUILD_ARG) |
| 369 | |
| 370 | |
| 371 | def _ParseArgsGnFile(): |
| 372 | """Parses args.gn file and returns results as a dictionary. |
| 373 | |
| 374 | Returns: |
| 375 | A dictionary representing the build args. |
| 376 | """ |
| 377 | build_args_path = os.path.join(BUILD_DIR, 'args.gn') |
| 378 | assert os.path.exists(build_args_path), ('"%s" is not a build directory, ' |
| 379 | 'missing args.gn file.' % BUILD_DIR) |
| 380 | with open(build_args_path) as build_args_file: |
| 381 | build_args_lines = build_args_file.readlines() |
| 382 | |
| 383 | build_args = {} |
| 384 | for build_arg_line in build_args_lines: |
| 385 | build_arg_without_comments = build_arg_line.split('#')[0] |
| 386 | key_value_pair = build_arg_without_comments.split('=') |
| 387 | if len(key_value_pair) != 2: |
| 388 | continue |
| 389 | |
| 390 | key = key_value_pair[0].strip() |
| 391 | value = key_value_pair[1].strip() |
| 392 | build_args[key] = value |
| 393 | |
| 394 | return build_args |
| 395 | |
| 396 | |
| 397 | def _ParseCommandArguments(): |
| 398 | """Adds and parses relevant arguments for tool comands. |
| 399 | |
| 400 | Returns: |
| 401 | A dictionary representing the arguments. |
| 402 | """ |
| 403 | arg_parser = argparse.ArgumentParser() |
| 404 | arg_parser.usage = __doc__ |
| 405 | |
| 406 | arg_parser.add_argument('-b', '--build-dir', type=str, required=True, |
| 407 | help='The build directory, the path needs to be ' |
| 408 | 'relative to the root of the checkout.') |
| 409 | |
| 410 | arg_parser.add_argument('-o', '--output-dir', type=str, required=True, |
| 411 | help='Output directory for generated artifacts.') |
| 412 | |
| 413 | arg_parser.add_argument('-c', '--command', action='append', |
| 414 | required=True, |
| 415 | help='Commands used to run test targets, one test ' |
| 416 | 'target needs one and only one command, when ' |
| 417 | 'specifying commands, one should assume the ' |
| 418 | 'current working directory is the root of the ' |
| 419 | 'checkout.') |
| 420 | |
| 421 | arg_parser.add_argument('-j', '--jobs', type=int, default=None, |
| 422 | help='Run N jobs to build in parallel. If not ' |
| 423 | 'specified, a default value will be derived ' |
| 424 | 'based on CPUs availability. Please refer to ' |
| 425 | '\'ninja -h\' for more details.') |
| 426 | |
| 427 | arg_parser.add_argument('targets', nargs='+', |
| 428 | help='The names of the test targets to run.') |
| 429 | |
| 430 | args = arg_parser.parse_args() |
| 431 | return args |
| 432 | |
| 433 | |
| 434 | def Main(): |
| 435 | """Execute tool commands.""" |
| 436 | assert os.path.abspath(os.getcwd()) == SRC_ROOT_PATH, ('This script must be ' |
| 437 | 'called from the root ' |
| 438 | 'of checkout') |
| 439 | DownloadCoverageToolsIfNeeded() |
| 440 | |
| 441 | args = _ParseCommandArguments() |
| 442 | global BUILD_DIR |
| 443 | BUILD_DIR = args.build_dir |
| 444 | global OUTPUT_DIR |
| 445 | OUTPUT_DIR = args.output_dir |
| 446 | |
| 447 | assert len(args.targets) == len(args.command), ('Number of targets must be ' |
| 448 | 'equal to the number of test ' |
| 449 | 'commands.') |
| 450 | assert os.path.exists(BUILD_DIR), ('Build directory: {} doesn\'t exist. ' |
| 451 | 'Please run "gn gen" to generate.').format( |
| 452 | BUILD_DIR) |
| 453 | _ValidateBuildingWithClangCoverage() |
| 454 | _ValidateCommandsAreRelativeToSrcRoot(args.command) |
| 455 | if not os.path.exists(OUTPUT_DIR): |
| 456 | os.makedirs(OUTPUT_DIR) |
| 457 | |
| 458 | profdata_file_path = _CreateCoverageProfileDataForTargets(args.targets, |
| 459 | args.command, |
| 460 | args.jobs) |
| 461 | |
| 462 | binary_paths = [_GetBinaryPath(command) for command in args.command] |
| 463 | _GenerateLineByLineFileCoverageInHtml(binary_paths, profdata_file_path) |
| 464 | html_index_file_path = 'file://' + os.path.abspath( |
| 465 | os.path.join(OUTPUT_DIR, 'index.html')) |
| 466 | print('\nCode coverage profile data is created as: %s' % profdata_file_path) |
| 467 | print('index file for html report is generated as: %s' % html_index_file_path) |
| 468 | |
| 469 | if __name__ == '__main__': |
| 470 | sys.exit(Main()) |