comparison third_party/emsdk/bazel/emscripten_toolchain/wasm_binary.py @ 186:8cf4ec5e2191 hg-web

Fixed merge conflict.
author MrJuneJune <me@mrjunejune.com>
date Fri, 23 Jan 2026 22:38:59 -0800
parents 8d17f6e6e290
children
comparison
equal deleted inserted replaced
176:fed99fc04e12 186:8cf4ec5e2191
1 """Unpackages a bazel emscripten archive for use in a bazel BUILD rule.
2
3 This script will take a tar archive containing the output of the emscripten
4 toolchain. This file contains any output files produced by a wasm_cc_binary or a
5 cc_binary built with --config=wasm. The files are extracted into the given
6 output paths.
7
8 The contents of the archive are expected to match the given outputs extnames.
9
10 This script and its accompanying Bazel rule should allow you to extract a
11 WebAssembly binary into a larger web application.
12 """
13
14 import argparse
15 import os
16 import tarfile
17
18
19 def ensure(f):
20 if not os.path.exists(f):
21 with open(f, 'w'):
22 pass
23
24
25 def main():
26 parser = argparse.ArgumentParser()
27 parser.add_argument('--archive', help='The archive to extract from.')
28 parser.add_argument('--outputs', help='Comma separated list of files that should be extracted from the archive. Only the extname has to match a file in the archive.')
29 parser.add_argument('--allow_empty_outputs', help='If an output listed in --outputs does not exist, create it anyways.', action='store_true')
30 args = parser.parse_args()
31
32 args.archive = os.path.normpath(args.archive)
33 args.outputs = args.outputs.split(",")
34
35 tar = tarfile.open(args.archive)
36
37 for member in tar.getmembers():
38 extname = '.' + member.name.split('.', 1)[1]
39 for idx, output in enumerate(args.outputs):
40 if output.endswith(extname):
41 member_file = tar.extractfile(member)
42 with open(output, "wb") as output_file:
43 output_file.write(member_file.read())
44 args.outputs.pop(idx)
45 break
46
47 for output in args.outputs:
48 extname = '.' + output.split('.', 1)[1]
49 if args.allow_empty_outputs:
50 ensure(output)
51 else:
52 print("[ERROR] Archive does not contain file with extname: %s" % extname)
53
54
55 if __name__ == '__main__':
56 main()