Skip to content
File

Blob: build/deps/dep_pyodide.bzl

4.7 KB
1load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
2load("//:build/python_metadata.bzl", "BUNDLE_VERSION_INFO", "PYODIDE_VERSIONS", "PYTHON_LOCKFILES")
3 
4def _pyodide_core(*, version, sha256, **_kwds):
5 # Use @workerd prefix on build_file so we can use this from edgeworker too
6 name = "pyodide-%s" % version
7 http_archive(
8 name = name,
9 build_file = "@workerd//:build/BUILD.pyodide",
10 sha256 = sha256,
11 urls = ["https://github.com/pyodide/pyodide/releases/download/%s/pyodide-core-%s.tar.bz2" % (version, version)],
12 )
13 return [name]
14 
15def _pyodide_packages(*, tag, lockfile_hash, all_wheels_hash, **_kwds):
16 lock_name = "pyodide-lock_%s.json" % tag
17 http_file(
18 name = lock_name,
19 sha256 = lockfile_hash,
20 url = "https://github.com/cloudflare/pyodide-build-scripts/releases/download/%s/pyodide-lock.json" % tag,
21 )
22 
23 # Use @workerd prefix on build_file so we can use this from edgeworker too
24 archive_name = "all_pyodide_wheels_%s" % tag
25 http_archive(
26 name = archive_name,
27 build_file = "@workerd//:build/BUILD.all_pyodide_wheels",
28 sha256 = all_wheels_hash,
29 urls = ["https://github.com/cloudflare/pyodide-build-scripts/releases/download/%s/all_wheels.zip" % tag],
30 )
31 return [lock_name, archive_name]
32 
33VENDOR_R2 = "https://pub-25a5b2f2f1b84655b185a505c7a3ad23.r2.dev/"
34 
35def _py_vendor_test_deps(version, name, sha256, abi, **_kwds):
36 pyver = "-" + abi.replace(".", "") if abi else ""
37 archive_name = name + "_src_" + version
38 http_archive(
39 name = archive_name,
40 build_file_content = """
41filegroup(
42 name = "all_srcs",
43 srcs = glob(["**"]),
44 visibility = ["//visibility:public"],
45)
46""",
47 sha256 = sha256,
48 url = VENDOR_R2 + name + pyver + "-vendored-for-ew-testing.zip",
49 )
50 return [archive_name]
51 
52PYODIDE_CAPN_BIN = "https://pyodide-capnp-bin.edgeworker.net/"
53 
54def _capnp_bundle(id = None, integrity = None, **_kwds):
55 if not id or not integrity:
56 return []
57 name = "pyodide_%s.capnp.bin" % id
58 http_file(
59 name = name,
60 integrity = integrity,
61 url = PYODIDE_CAPN_BIN + name,
62 )
63 return [name]
64 
65def _snapshot_http_file(bundle_name, folder, snapshot, integrity, hash, r2_base = PYODIDE_CAPN_BIN):
66 if not snapshot:
67 return []
68 if not integrity:
69 fail("Snapshot %s from bundle %s has missing integrity" % (snapshot, bundle_name))
70 if folder == "baseline-snapshot/":
71 key = hash
72 else:
73 key = snapshot
74 return [struct(
75 name = "pyodide-snapshot-" + snapshot,
76 snapshot = snapshot,
77 integrity = integrity,
78 url = r2_base + folder + key,
79 )]
80 
81def _snapshot_http_files_version(
82 name,
83 baseline_snapshot = None,
84 baseline_snapshot_hash = None,
85 baseline_snapshot_integrity = None,
86 numpy_snapshot = None,
87 numpy_snapshot_integrity = None,
88 fastapi_snapshot = None,
89 fastapi_snapshot_integrity = None,
90 dedicated_fastapi_snapshot = None,
91 dedicated_fastapi_snapshot_integrity = None,
92 **_kwds):
93 return (_snapshot_http_file(name, "baseline-snapshot/", baseline_snapshot, baseline_snapshot_integrity, baseline_snapshot_hash) +
94 _snapshot_http_file(name, "test-snapshot/", numpy_snapshot, numpy_snapshot_integrity, None) +
95 _snapshot_http_file(name, "test-snapshot/", fastapi_snapshot, fastapi_snapshot_integrity, None) +
96 _snapshot_http_file(name, "", dedicated_fastapi_snapshot, dedicated_fastapi_snapshot_integrity, None, VENDOR_R2))
97 
98def _snapshot_http_files():
99 files = []
100 for ver in BUNDLE_VERSION_INFO.values():
101 files += _snapshot_http_files_version(**ver)
102 
103 # Deduplicate generated http_file rules.
104 http_files = {info.snapshot: info for info in files}
105 deps = []
106 for info in http_files.values():
107 http_file(
108 name = info.name,
109 integrity = info.integrity,
110 url = info.url,
111 )
112 deps.append(info.name)
113 return deps
114 
115def dep_pyodide():
116 deps = []
117 for info in PYODIDE_VERSIONS:
118 deps += _pyodide_core(**info)
119 
120 for info in BUNDLE_VERSION_INFO.values():
121 deps += _capnp_bundle(**info)
122 for pkg in info["vendored_packages_for_tests"].values():
123 deps += _py_vendor_test_deps(version = info["name"], **pkg)
124 
125 for info in PYTHON_LOCKFILES:
126 deps += _pyodide_packages(**info)
127 
128 deps += _snapshot_http_files()
129 return deps
130 
131def _impl(module_ctx):
132 deps = dep_pyodide()
133 return module_ctx.extension_metadata(
134 root_module_direct_deps = deps,
135 root_module_direct_dev_deps = [],
136 )
137 
138pyodide = module_extension(implementation = _impl)