zip.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. ]
  41. def skip_path(dep, dist_zip, target_cpu):
  42. # Skip specific paths and extensions as well as the following special case:
  43. # snapshot_blob.bin is a dependency of mksnapshot.zip because
  44. # v8_context_generator needs it, but this file does not get generated for arm
  45. # and arm 64 binaries of mksnapshot since they are built on x64 hardware.
  46. # Consumers of arm and arm64 mksnapshot can generate snapshot_blob.bin
  47. # themselves by running mksnapshot.
  48. should_skip = (
  49. any(dep.startswith(path) for path in PATHS_TO_SKIP) or
  50. any(dep.endswith(ext) for ext in EXTENSIONS_TO_SKIP) or
  51. (
  52. "arm" in target_cpu
  53. and dist_zip == "mksnapshot.zip"
  54. and dep == "snapshot_blob.bin"
  55. )
  56. )
  57. if should_skip:
  58. print("Skipping {}".format(dep))
  59. return should_skip
  60. def execute(argv):
  61. try:
  62. output = subprocess.check_output(argv, stderr=subprocess.STDOUT)
  63. return output
  64. except subprocess.CalledProcessError as e:
  65. print(e.output)
  66. raise e
  67. def main(argv):
  68. dist_zip, runtime_deps, target_cpu, _, flatten_val = argv
  69. should_flatten = flatten_val == "true"
  70. dist_files = set()
  71. with open(runtime_deps) as f:
  72. for dep in f.readlines():
  73. dep = dep.strip()
  74. if not skip_path(dep, dist_zip, target_cpu):
  75. dist_files.add(dep)
  76. if sys.platform == 'darwin' and not should_flatten:
  77. execute(['zip', '-r', '-y', dist_zip] + list(dist_files))
  78. else:
  79. with zipfile.ZipFile(
  80. dist_zip, 'w', zipfile.ZIP_DEFLATED, allowZip64=True
  81. ) as z:
  82. for dep in dist_files:
  83. if os.path.isdir(dep):
  84. for root, _, files in os.walk(dep):
  85. for filename in files:
  86. z.write(os.path.join(root, filename))
  87. else:
  88. basename = os.path.basename(dep)
  89. dirname = os.path.dirname(dep)
  90. arcname = (
  91. os.path.join(dirname, 'chrome-sandbox')
  92. if basename == 'chrome_sandbox'
  93. else dep
  94. )
  95. z.write(
  96. dep,
  97. os.path.basename(arcname)
  98. if should_flatten
  99. else arcname,
  100. )
  101. if __name__ == '__main__':
  102. sys.exit(main(sys.argv[1:]))