| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Tool for finding the cause of binary size bloat. | 6 """Tool for finding the cause of binary size bloat. |
| 7 | 7 |
| 8 See //tools/binary_size/README.md for example usage. | 8 See //tools/binary_size/README.md for example usage. |
| 9 | 9 |
| 10 Note: this tool will perform gclient sync/git checkout on your local repo if | 10 Note: this tool will perform gclient sync/git checkout on your local repo if |
| (...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 rebased_tool_prefix = os.path.join(_SRC_ROOT, tool_prefix) | 512 rebased_tool_prefix = os.path.join(_SRC_ROOT, tool_prefix) |
| 513 if os.path.exists(rebased_tool_prefix + 'readelf'): | 513 if os.path.exists(rebased_tool_prefix + 'readelf'): |
| 514 return rebased_tool_prefix | 514 return rebased_tool_prefix |
| 515 tool_prefix = tool_prefix[tool_prefix.find(os.path.sep) + 1:] | 515 tool_prefix = tool_prefix[tool_prefix.find(os.path.sep) + 1:] |
| 516 return '' | 516 return '' |
| 517 | 517 |
| 518 | 518 |
| 519 def _SyncAndBuild(archive, build, subrepo): | 519 def _SyncAndBuild(archive, build, subrepo): |
| 520 """Sync, build and return non 0 if any commands failed.""" | 520 """Sync, build and return non 0 if any commands failed.""" |
| 521 # Simply do a checkout if subrepo is used. | 521 # Simply do a checkout if subrepo is used. |
| 522 retcode = 0 |
| 522 if subrepo != _SRC_ROOT: | 523 if subrepo != _SRC_ROOT: |
| 523 _GitCmd(['checkout', archive.rev], subrepo) | 524 _GitCmd(['checkout', archive.rev], subrepo) |
| 524 return 0 | |
| 525 else: | 525 else: |
| 526 # Move to a detached state since gclient sync doesn't work with local | 526 # Move to a detached state since gclient sync doesn't work with local |
| 527 # commits on a branch. | 527 # commits on a branch. |
| 528 _GitCmd(['checkout', '--detach'], subrepo) | 528 _GitCmd(['checkout', '--detach'], subrepo) |
| 529 logging.info('Syncing to %s', archive.rev) | 529 logging.info('Syncing to %s', archive.rev) |
| 530 return _GclientSyncCmd(archive.rev, subrepo) or build.Run() | 530 retcode = _GclientSyncCmd(archive.rev, subrepo) |
| 531 return retcode or build.Run() |
| 531 | 532 |
| 532 | 533 |
| 533 def _GenerateRevList(rev, reference_rev, all_in_range, subrepo): | 534 def _GenerateRevList(rev, reference_rev, all_in_range, subrepo): |
| 534 """Normalize and optionally generate a list of commits in the given range. | 535 """Normalize and optionally generate a list of commits in the given range. |
| 535 | 536 |
| 536 Returns: | 537 Returns: |
| 537 A list of revisions ordered from oldest to newest. | 538 A list of revisions ordered from oldest to newest. |
| 538 """ | 539 """ |
| 539 rev_seq = '%s^..%s' % (reference_rev, rev) | 540 rev_seq = '%s^..%s' % (reference_rev, rev) |
| 540 stdout = _GitCmd(['rev-list', rev_seq], subrepo) | 541 stdout = _GitCmd(['rev-list', rev_seq], subrepo) |
| 541 all_revs = stdout.splitlines()[::-1] | 542 all_revs = stdout.splitlines()[::-1] |
| 542 if all_in_range: | 543 if all_in_range: |
| 543 revs = all_revs | 544 revs = all_revs |
| 544 else: | 545 else: |
| 545 revs = [all_revs[0], all_revs[-1]] | 546 revs = [all_revs[0], all_revs[-1]] |
| 546 if len(revs) >= _COMMIT_COUNT_WARN_THRESHOLD: | 547 if len(revs) >= _COMMIT_COUNT_WARN_THRESHOLD: |
| 547 _VerifyUserAccepts( | 548 _VerifyUserAccepts( |
| 548 'You\'ve provided a commit range that contains %d commits' % len(revs)) | 549 'You\'ve provided a commit range that contains %d commits.' % len(revs)) |
| 549 return revs | 550 return revs |
| 550 | 551 |
| 551 | 552 |
| 552 def _ValidateRevs(rev, reference_rev, subrepo): | 553 def _ValidateRevs(rev, reference_rev, subrepo): |
| 553 def git_fatal(args, message): | 554 def git_fatal(args, message): |
| 554 devnull = open(os.devnull, 'wb') | 555 devnull = open(os.devnull, 'wb') |
| 555 retcode = subprocess.call( | 556 retcode = subprocess.call( |
| 556 ['git', '-C', subrepo] + args, stdout=devnull, stderr=subprocess.STDOUT) | 557 ['git', '-C', subrepo] + args, stdout=devnull, stderr=subprocess.STDOUT) |
| 557 if retcode: | 558 if retcode: |
| 558 _Die(message) | 559 _Die(message) |
| 559 | 560 |
| 560 if rev == reference_rev: | 561 if rev == reference_rev: |
| 561 _Die('rev and reference-rev cannot be equal') | 562 _Die('rev and reference-rev cannot be equal') |
| 562 no_obj_message = ('%s either doesn\'t exist or your local repo is out of ' | 563 no_obj_message = ('%s either doesn\'t exist or your local repo is out of ' |
| 563 'date, try "git fetch origin master"') | 564 'date, try "git fetch origin master"') |
| 564 git_fatal(['cat-file', '-e', rev], no_obj_message % rev) | 565 git_fatal(['cat-file', '-e', rev], no_obj_message % rev) |
| 565 git_fatal(['cat-file', '-e', reference_rev], no_obj_message % reference_rev) | 566 git_fatal(['cat-file', '-e', reference_rev], no_obj_message % reference_rev) |
| 566 git_fatal(['merge-base', '--is-ancestor', reference_rev, rev], | 567 git_fatal(['merge-base', '--is-ancestor', reference_rev, rev], |
| 567 'reference-rev is newer than rev') | 568 'reference-rev is newer than rev') |
| 568 return rev, reference_rev | 569 return rev, reference_rev |
| 569 | 570 |
| 570 | 571 |
| 571 def _VerifyUserAccepts(message): | 572 def _VerifyUserAccepts(message): |
| 572 print message + 'Do you want to proceed? [y/n]' | 573 print message + ' Do you want to proceed? [y/n]' |
| 573 if raw_input('> ').lower() != 'y': | 574 if raw_input('> ').lower() != 'y': |
| 574 sys.exit() | 575 sys.exit() |
| 575 | 576 |
| 576 | 577 |
| 577 def _EnsureDirectoryClean(subrepo): | 578 def _EnsureDirectoryClean(subrepo): |
| 578 logging.info('Checking source directory') | 579 logging.info('Checking source directory') |
| 579 stdout = _GitCmd(['status', '--porcelain'], subrepo) | 580 stdout = _GitCmd(['status', '--porcelain'], subrepo) |
| 580 # Ignore untracked files. | 581 # Ignore untracked files. |
| 581 if stdout and stdout[:2] != '??': | 582 if stdout and stdout[:2] != '??': |
| 582 logging.error('Failure: please ensure working directory is clean.') | 583 logging.error('Failure: please ensure working directory is clean.') |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 754 logging.basicConfig(level=log_level, | 755 logging.basicConfig(level=log_level, |
| 755 format='%(levelname).1s %(relativeCreated)6d %(message)s') | 756 format='%(levelname).1s %(relativeCreated)6d %(message)s') |
| 756 build = _BuildHelper(args) | 757 build = _BuildHelper(args) |
| 757 if build.IsCloud() and args.subrepo: | 758 if build.IsCloud() and args.subrepo: |
| 758 parser.error('--subrepo doesn\'t work with --cloud') | 759 parser.error('--subrepo doesn\'t work with --cloud') |
| 759 | 760 |
| 760 subrepo = args.subrepo or _SRC_ROOT | 761 subrepo = args.subrepo or _SRC_ROOT |
| 761 _EnsureDirectoryClean(subrepo) | 762 _EnsureDirectoryClean(subrepo) |
| 762 _SetRestoreFunc(subrepo) | 763 _SetRestoreFunc(subrepo) |
| 763 if build.IsLinux(): | 764 if build.IsLinux(): |
| 764 _VerifyUserAccepts('Linux diffs have known deficiencies (crbug/717550). ') | 765 _VerifyUserAccepts('Linux diffs have known deficiencies (crbug/717550).') |
| 765 | 766 |
| 766 rev, reference_rev = _ValidateRevs( | 767 rev, reference_rev = _ValidateRevs( |
| 767 args.rev, args.reference_rev or args.rev + '^', subrepo) | 768 args.rev, args.reference_rev or args.rev + '^', subrepo) |
| 768 revs = _GenerateRevList(rev, reference_rev, args.all, subrepo) | 769 revs = _GenerateRevList(rev, reference_rev, args.all, subrepo) |
| 769 with _TmpCopyBinarySizeDir() as supersize_path: | 770 with _TmpCopyBinarySizeDir() as supersize_path: |
| 770 diffs = [NativeDiff(build.size_name, supersize_path)] | 771 diffs = [NativeDiff(build.size_name, supersize_path)] |
| 771 if build.IsAndroid(): | 772 if build.IsAndroid(): |
| 772 diffs += [ | 773 diffs += [ |
| 773 ResourceSizesDiff( | 774 ResourceSizesDiff( |
| 774 build.apk_name, slow_options=args.include_slow_options) | 775 build.apk_name, slow_options=args.include_slow_options) |
| (...skipping 26 matching lines...) Expand all Loading... |
| 801 | 802 |
| 802 if i != 0: | 803 if i != 0: |
| 803 diff_mngr.MaybeDiff(i - 1, i) | 804 diff_mngr.MaybeDiff(i - 1, i) |
| 804 | 805 |
| 805 diff_mngr.Summarize() | 806 diff_mngr.Summarize() |
| 806 | 807 |
| 807 | 808 |
| 808 if __name__ == '__main__': | 809 if __name__ == '__main__': |
| 809 sys.exit(main()) | 810 sys.exit(main()) |
| 810 | 811 |
| OLD | NEW |