zip.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. from __future__ import print_function
  3. import os
  4. import subprocess
  5. import sys
  6. import zipfile
  7. EXTENSIONS_TO_SKIP = [
  8. '.pdb',
  9. '.mojom.js',
  10. '.mojom-lite.js',
  11. '.info',
  12. '.m.js'
  13. ]
  14. PATHS_TO_SKIP = [
  15. # Skip because it is an output of //ui/gl that we don't need.
  16. 'angledata',
  17. # Skip because these are outputs that we don't need.
  18. './libVkICD_mock_',
  19. # Skip because these are outputs that we don't need.
  20. './VkICD_mock_',
  21. # Skip because its an output of create_bundle from
  22. # //build/config/mac/rules.gni that we don't need
  23. 'Electron.dSYM',
  24. # Refs https://chromium-review.googlesource.com/c/angle/angle/+/2425197.
  25. # Remove this when Angle themselves remove the file:
  26. # https://issuetracker.google.com/issues/168736059
  27. 'gen/angle/angle_commit.h',
  28. # //chrome/browser:resources depends on this via
  29. # //chrome/browser/resources/ssl/ssl_error_assistant, but we don't need to
  30. # ship it.
  31. 'pyproto',
  32. # On Windows, this binary doesn't exist (the crashpad handler is built-in).
  33. # On MacOS, the binary is called 'chrome_crashpad_handler' and is inside the
  34. # app bundle.
  35. # On Linux, we don't use crashpad, but this binary is still built for some
  36. # reason. Exclude it from the zip.
  37. './crashpad_handler',
  38. # Skip because these are outputs that we don't need.
  39. 'resources/inspector',
  40. 'gen/third_party/devtools-frontend/src'
  41. ]
  42. def skip_path(dep, dist_zip, target_cpu):
  43. # Skip specific paths and extensions as well as the following special case:
  44. # snapshot_blob.bin is a dependency of mksnapshot.zip because
  45. # v8_context_generator needs it, but this file does not get generated for arm
  46. # and arm 64 binaries of mksnapshot since they are built on x64 hardware.
  47. # Consumers of arm and arm64 mksnapshot can generate snapshot_blob.bin
  48. # themselves by running mksnapshot.
  49. should_skip = (
  50. any(dep.startswith(path) for path in PATHS_TO_SKIP) or
  51. any(dep.endswith(ext) for ext in EXTENSIONS_TO_SKIP) or
  52. (
  53. "arm" in target_cpu
  54. and dist_zip == "mksnapshot.zip"
  55. and dep == "snapshot_blob.bin"
  56. )
  57. )
  58. if should_skip:
  59. print("Skipping {}".format(dep))
  60. return should_skip
  61. def execute(argv):
  62. try:
  63. output = subprocess.check_output(argv, stderr=subprocess.STDOUT)
  64. return output
  65. except subprocess.CalledProcessError as e:
  66. print(e.output)
  67. raise e
  68. def main(argv):
  69. dist_zip, runtime_deps, target_cpu, _, flatten_val, flatten_relative_to = argv
  70. should_flatten = flatten_val == "true"
  71. dist_files = set()
  72. with open(runtime_deps) as f:
  73. for dep in f.readlines():
  74. dep = dep.strip()
  75. if not skip_path(dep, dist_zip, target_cpu):
  76. dist_files.add(dep)
  77. if sys.platform == 'darwin' and not should_flatten:
  78. execute(['zip', '-r', '-y', dist_zip] + list(dist_files))
  79. else:
  80. with zipfile.ZipFile(
  81. dist_zip, 'w', zipfile.ZIP_DEFLATED, allowZip64=True
  82. ) as z:
  83. for dep in dist_files:
  84. if os.path.isdir(dep):
  85. for root, _, files in os.walk(dep):
  86. for filename in files:
  87. z.write(os.path.join(root, filename))
  88. else:
  89. basename = os.path.basename(dep)
  90. dirname = os.path.dirname(dep)
  91. arcname = (
  92. os.path.join(dirname, 'chrome-sandbox')
  93. if basename == 'chrome_sandbox'
  94. else dep
  95. )
  96. name_to_write = arcname
  97. if should_flatten:
  98. if flatten_relative_to:
  99. if name_to_write.startswith(flatten_relative_to):
  100. name_to_write = name_to_write[len(flatten_relative_to):]
  101. else:
  102. name_to_write = os.path.basename(arcname)
  103. else:
  104. name_to_write = os.path.basename(arcname)
  105. z.write(
  106. dep,
  107. name_to_write,
  108. )
  109. if __name__ == '__main__':
  110. sys.exit(main(sys.argv[1:]))