rust_worker: a Rust Cloudflare Worker built by buck2, packed the way worker-build packs one (src/lib.rs says how, and from which release).
load("//third-party/rust/rustcrates/worker-pack:worker.bzl", "rust_worker")
rust_library(name = "lib", crate = "app", srcs = ..., deps = ...,
rustc_flags = ["-Clto=fat"], # the cdylib's link-time optimisation
env = cargo_env("app", "app")) # what wasm-bindgen's macros read
rust_worker(name = "worker", lib = ":lib",
default_target_platform = "//platforms:wasm32")
buck2 build //app:worker --out build then leaves build/index.js and
build/index_bg.wasm, which wrangler.toml's main = "build/index.js" loads
with no [build] command.
What the consuming repository provides:
- a wasm32 target platform, and toolchains//:rust and :cxx that select a wasm32-unknown-unknown rustc (panic = abort) and rust-lld for it;
- in .buckconfig.local, [worker] wasm_bindgen, wasm_opt and esbuild: each tool's absolute path. A path is part of the action's key, so a new version of a tool rebuilds the pack;
- rustcrates mounted at third-party/rust/rustcrates, or
packnaming this package's :worker-pack where it is mounted.
25def cargo_env(manifest_dir, name, version = "0.0.0"): 26 """The variables cargo sets for a crate it compiles, which wasm-bindgen's 27 macros read (and panic without): reindeer writes them for third-party 28 crates, so a first-party crate that uses `#[wasm_bindgen]` or 29 `#[event]` says them with this. `manifest_dir` is the crate's directory 30 from the repository root, where wasm-bindgen resolves `module = "/..."` 31 paths.""" 32 major, minor, patch = (version.split("-")[0].split(".") + ["0", "0"])[:3] 33 return { 34 "CARGO_CRATE_NAME": name.replace("-", "_"), 35 "CARGO_MANIFEST_DIR": manifest_dir, 36 "CARGO_PKG_NAME": name, 37 "CARGO_PKG_VERSION": version, 38 "CARGO_PKG_VERSION_MAJOR": major, 39 "CARGO_PKG_VERSION_MINOR": minor, 40 "CARGO_PKG_VERSION_PATCH": patch, 41 "CARGO_PKG_VERSION_PRE": "", 42 }
44def _rust_worker_impl(ctx: AnalysisContext) -> list[Provider]: 45 out = ctx.actions.declare_output(ctx.label.name, dir = True) 46 wasm = ctx.attrs.wasm[DefaultInfo].default_outputs[0] 47 cmd = cmd_args( 48 ctx.attrs.pack[RunInfo], 49 "--wasm", 50 wasm, 51 "--out", 52 out.as_output(), 53 "--wasm-bindgen", 54 ctx.attrs.wasm_bindgen, 55 "--wasm-opt", 56 ctx.attrs.wasm_opt, 57 "--esbuild", 58 ctx.attrs.esbuild, 59 ) 60 if not ctx.attrs.opt: 61 cmd.add("--no-opt") 62 if not ctx.attrs.minify: 63 cmd.add("--no-minify") 64 ctx.actions.run(cmd, category = "worker_pack") 65 return [DefaultInfo(default_output = out)] 66 67_rust_worker = rule( 68 impl = _rust_worker_impl, 69 attrs = { 70 "esbuild": attrs.string(), 71 "minify": attrs.bool(default = True), 72 "opt": attrs.bool(default = True), 73 "pack": attrs.exec_dep(providers = [RunInfo]), 74 "wasm": attrs.dep(providers = [DefaultInfo]), 75 "wasm_bindgen": attrs.string(), 76 "wasm_opt": attrs.string(), 77 }, 78) 79 80def _tool(name): 81 return read_root_config("worker", name) or fail( 82 "worker.{} is unset: .buckconfig.local must name the tool's path (the repository's configure step)".format(name), 83 ) 84 85def rust_worker(name, lib, pack = "//third-party/rust/rustcrates/worker-pack:worker-pack", **kwargs): 86 _rust_worker( 87 name = name, 88 wasm = lib + "[cdylib]", 89 pack = pack, 90 wasm_bindgen = _tool("wasm_bindgen"), 91 wasm_opt = _tool("wasm_opt"), 92 esbuild = _tool("esbuild"), 93 **kwargs 94 )