comparison third_party/emsdk/scripts/update_bazel_workspace.py @ 179:8d17f6e6e290

[ThirdParty] Added emsdk bazel rules that can be supported by bazel 9.0.0
author MrJuneJune <me@mrjunejune.com>
date Thu, 22 Jan 2026 21:23:17 -0800
parents
children
comparison
equal deleted inserted replaced
178:94705b5986b3 179:8d17f6e6e290
1 #!/usr/bin/env python3
2 # This script will update emsdk/bazel/revisions.bzl to the latest version of
3 # emscripten. It reads emsdk/emscripten-releases-tags.json to get the latest
4 # version number. Then, it downloads the prebuilts for that version and computes
5 # the sha256sum for the archive. It then puts all this information into the
6 # emsdk/bazel/revisions.bzl file.
7
8 import hashlib
9 import json
10 import os
11 import re
12 import sys
13
14 import requests
15
16 STORAGE_URL = 'https://storage.googleapis.com/webassembly/emscripten-releases-builds'
17
18 EMSDK_ROOT = os.path.dirname(os.path.dirname(__file__))
19 RELEASES_TAGS_FILE = EMSDK_ROOT + '/emscripten-releases-tags.json'
20 BAZEL_REVISIONS_FILE = EMSDK_ROOT + '/bazel/revisions.bzl'
21 BAZEL_MODULE_FILE = EMSDK_ROOT + '/bazel/MODULE.bazel'
22
23
24 def get_latest_info():
25 with open(RELEASES_TAGS_FILE) as f:
26 info = json.load(f)
27 latest = info['aliases']['latest']
28 return latest, info['releases'][latest]
29
30
31 def get_sha(platform, archive_fmt, latest_hash, arch_suffix=''):
32 r = requests.get(f'{STORAGE_URL}/{platform}/{latest_hash}/wasm-binaries{arch_suffix}.{archive_fmt}')
33 r.raise_for_status()
34 print(f'Fetching {r.url}')
35 h = hashlib.new('sha256')
36 for chunk in r.iter_content(chunk_size=1024):
37 h.update(chunk)
38 return h.hexdigest()
39
40
41 def revisions_item(version, latest_hash):
42 return f'''\
43 "{version}": struct(
44 hash = "{latest_hash}",
45 sha_linux = "{get_sha('linux', 'tar.xz', latest_hash)}",
46 sha_linux_arm64 = "{get_sha('linux', 'tar.xz', latest_hash, '-arm64')}",
47 sha_mac = "{get_sha('mac', 'tar.xz', latest_hash)}",
48 sha_mac_arm64 = "{get_sha('mac', 'tar.xz', latest_hash, '-arm64')}",
49 sha_win = "{get_sha('win', 'zip', latest_hash)}",
50 ),
51 '''
52
53
54 def insert_revision(item):
55 with open(BAZEL_REVISIONS_FILE) as f:
56 lines = f.readlines()
57
58 lines.insert(lines.index('EMSCRIPTEN_TAGS = {\n') + 1, item)
59
60 with open(BAZEL_REVISIONS_FILE, 'w') as f:
61 f.write(''.join(lines))
62
63
64 def update_module_version(version):
65 with open(BAZEL_MODULE_FILE) as f:
66 content = f.read()
67
68 pattern = r'(module\(\s*name = "emsdk",\s*version = )"\d+.\d+.\d+",\n\)'
69 # Verify that the pattern exists in the input since re.sub will
70 # will succeed either way.
71 assert re.search(pattern, content)
72 content = re.sub(pattern, fr'\1"{version}",\n)', content)
73
74 with open(BAZEL_MODULE_FILE, 'w') as f:
75 f.write(content)
76
77
78 def main():
79 version, latest_hash = get_latest_info()
80 update_module_version(version)
81 item = revisions_item(version, latest_hash)
82 print('inserting item:')
83 print(item)
84 insert_revision(item)
85
86
87 if __name__ == '__main__':
88 sys.exit(main())