generate_commit_size_analysis.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python3
  2. # Copyright 2020 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. # Lint as: python3
  6. """Creates files required to feed into trybot_commit_size_checker"""
  7. import argparse
  8. import json
  9. import logging
  10. import os
  11. import shutil
  12. import subprocess
  13. _SRC_ROOT = os.path.normpath(
  14. os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
  15. _RESOURCE_SIZES_PATH = os.path.join(_SRC_ROOT, 'build', 'android',
  16. 'resource_sizes.py')
  17. _BINARY_SIZE_DIR = os.path.join(_SRC_ROOT, 'tools', 'binary_size')
  18. _CLANG_UPDATE_PATH = os.path.join(_SRC_ROOT, 'tools', 'clang', 'scripts',
  19. 'update.py')
  20. def _copy_files_to_staging_dir(files_to_copy, make_staging_path):
  21. """Copies files from output directory to staging_dir"""
  22. for filename in files_to_copy:
  23. shutil.copy(filename, make_staging_path(filename))
  24. def _generate_resource_sizes(to_resource_sizes_py, make_chromium_output_path,
  25. make_staging_path):
  26. """Creates results-chart.json file in staging_dir"""
  27. cmd = [
  28. _RESOURCE_SIZES_PATH,
  29. make_chromium_output_path(to_resource_sizes_py['apk_name']),
  30. '--output-format=chartjson',
  31. '--chromium-output-directory',
  32. make_chromium_output_path(),
  33. '--output-dir',
  34. make_staging_path(),
  35. ]
  36. FORWARDED_PARAMS = [
  37. ('--trichrome-library', make_chromium_output_path, 'trichrome_library'),
  38. ('--trichrome-chrome', make_chromium_output_path, 'trichrome_chrome'),
  39. ('--trichrome-webview', make_chromium_output_path, 'trichrome_webview'),
  40. ]
  41. for switch, fun, key in FORWARDED_PARAMS:
  42. if key in to_resource_sizes_py:
  43. cmd += [switch, fun(to_resource_sizes_py[key])]
  44. subprocess.run(cmd, check=True)
  45. def _generate_supersize_archive(supersize_input_file, make_chromium_output_path,
  46. make_staging_path):
  47. """Creates a .size file for the given .apk or .minimal.apks"""
  48. subprocess.run([_CLANG_UPDATE_PATH, '--package=objdump'], check=True)
  49. supersize_input_path = make_chromium_output_path(supersize_input_file)
  50. size_path = make_staging_path(supersize_input_file) + '.size'
  51. supersize_script_path = os.path.join(_BINARY_SIZE_DIR, 'supersize')
  52. subprocess.run(
  53. [
  54. supersize_script_path,
  55. 'archive',
  56. size_path,
  57. '-f',
  58. supersize_input_path,
  59. '-v',
  60. ],
  61. check=True,
  62. )
  63. def main():
  64. parser = argparse.ArgumentParser()
  65. # Schema for android_size_bot_config:
  66. # name: The name of the path to the generated size config JSON file.
  67. # archive_files: List of files to archive after building, and make available
  68. # to trybot_commit_size_checker.py.
  69. # mapping_files: A list of .mapping files.
  70. # Used by trybot_commit_size_checker.py to look for ForTesting symbols.
  71. # supersize_input_file: Main input for SuperSize, and can be {.apk,
  72. # .minimal.apks, .ssargs}.
  73. # to_resource_sizes_py: Scope containing data to pass to resource_sizes.py.
  74. # Its fields are:
  75. # * resource_size_args: A dict of arguments for resource_sizes.py. Its
  76. # sub-fields are:
  77. # * apk_name: Required main input, although for Trichrome this can be
  78. # a placeholder name.
  79. # * trichrome_library: --trichrome-library param (Trichrome only).
  80. # * trichrome_chrome: --trichrome-chrome param (Trichrome only).
  81. # * trichrome_webview: --trichrome-webview param (Trichrome only).
  82. # * supersize_input_file: Main input for SuperSize.
  83. parser.add_argument('--size-config-json',
  84. required=True,
  85. help='Path to android_size_bot_config JSON')
  86. parser.add_argument('--chromium-output-directory',
  87. required=True,
  88. help='Location of the build artifacts.')
  89. parser.add_argument('--staging-dir',
  90. required=True,
  91. help='Directory to write generated files to.')
  92. args = parser.parse_args()
  93. with open(args.size_config_json, 'rt') as fh:
  94. config = json.load(fh)
  95. to_resource_sizes_py = config['to_resource_sizes_py']
  96. mapping_files = config['mapping_files']
  97. supersize_input_file = config['supersize_input_file']
  98. # TODO(agrieve): Remove fallback to mapping_files once archive_files is added
  99. # to all files.
  100. archive_files = config.get('archive_files', mapping_files)
  101. def make_chromium_output_path(path_rel_to_output=None):
  102. if path_rel_to_output is None:
  103. return args.chromium_output_directory
  104. return os.path.join(args.chromium_output_directory, path_rel_to_output)
  105. # N.B. os.path.basename() usage.
  106. def make_staging_path(path_rel_to_output=None):
  107. if path_rel_to_output is None:
  108. return args.staging_dir
  109. return os.path.join(args.staging_dir, os.path.basename(path_rel_to_output))
  110. files_to_copy = [make_chromium_output_path(f) for f in archive_files]
  111. # Copy size config JSON to staging dir to save settings used.
  112. if args.size_config_json:
  113. files_to_copy.append(args.size_config_json)
  114. _copy_files_to_staging_dir(files_to_copy, make_staging_path)
  115. _generate_resource_sizes(to_resource_sizes_py, make_chromium_output_path,
  116. make_staging_path)
  117. _generate_supersize_archive(supersize_input_file, make_chromium_output_path,
  118. make_staging_path)
  119. if __name__ == '__main__':
  120. main()