Skip to content

Commit

Permalink
Allow rust_wasm_bindgen rules to re-expose rust-analyzer providers
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Dec 3, 2024
1 parent c3d71ed commit d439c41
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
37 changes: 24 additions & 13 deletions extensions/wasm_bindgen/private/wasm_bindgen.bzl
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
"""Bazel rules for [wasm-bindgen](https://crates.io/crates/wasm-bindgen)"""

load("@rules_rust//rust:defs.bzl", "rust_common")
load("//:providers.bzl", "RustWasmBindgenInfo")

# buildifier: disable=bzl-visibility
load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo")
load(":providers.bzl", "RustWasmBindgenInfo")
load(":transitions.bzl", "wasm_bindgen_transition")

def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_flags = []):
def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags = []):
"""Spawn a `RustWasmBindgen` action.
Args:
ctx (ctx): _description_
toolchain (ToolchainInfo): _description_
wasm_file (Target): _description_
ctx (ctx): The rule's context object.
toolchain (ToolchainInfo): The current `rust_wasm_bindgen_toolchain`.
wasm_file (Target): The target representing the `.wasm` file.
target_output (str): _description_
bindgen_flags (list, optional): _description_. Defaults to [].
flags (list, optional): Flags to pass to `wasm-bindgen`.
Returns:
RustWasmBindgenInfo: _description_
RustWasmBindgenInfo: A provider containing action outputs.
"""
bindgen_bin = toolchain.bindgen

Expand Down Expand Up @@ -54,12 +57,12 @@ def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_f

js_out = [ctx.actions.declare_file(ctx.label.name + ".js")]
ts_out = []
if not "--no-typescript" in bindgen_flags:
if not "--no-typescript" in flags:
ts_out.append(ctx.actions.declare_file(ctx.label.name + ".d.ts"))

if target_output == "bundler":
js_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.js"))
if not "--no-typescript" in bindgen_flags:
if not "--no-typescript" in flags:
ts_out.append(ctx.actions.declare_file(ctx.label.name + "_bg.wasm.d.ts"))

outputs = [bindgen_wasm_module] + js_out + ts_out
Expand All @@ -68,15 +71,15 @@ def rust_wasm_bindgen_action(ctx, toolchain, wasm_file, target_output, bindgen_f
args.add("--target", target_output)
args.add("--out-dir", bindgen_wasm_module.dirname)
args.add("--out-name", ctx.label.name)
args.add_all(bindgen_flags)
args.add_all(flags)
args.add(input_file)

ctx.actions.run(
executable = bindgen_bin,
inputs = [input_file],
outputs = outputs,
mnemonic = "RustWasmBindgen",
progress_message = "Generating WebAssembly bindings for {}...".format(progress_message_label),
progress_message = "Generating WebAssembly bindings for {}".format(progress_message_label),
arguments = [args],
toolchain = str(Label("//:toolchain_type")),
)
Expand All @@ -98,16 +101,24 @@ def _rust_wasm_bindgen_impl(ctx):
bindgen_flags = ctx.attr.bindgen_flags,
)

return [
providers = [
DefaultInfo(
files = depset([info.wasm], transitive = [info.js, info.ts]),
),
info,
]

if RustAnalyzerGroupInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo])

if RustAnalyzerInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerInfo])

return providers

WASM_BINDGEN_ATTR = {
"bindgen_flags": attr.string_list(
doc = "Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.",
doc = "Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.",
),
"target": attr.string(
doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.",
Expand Down
13 changes: 12 additions & 1 deletion extensions/wasm_bindgen/rules_js/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Rust WASM-bindgen rules for interfacing with aspect-build/rules_js"""

load("@aspect_rules_js//js:providers.bzl", "js_info")

# buildifier: disable=bzl-visibility
load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo")
load("//private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action")

def _js_rust_wasm_bindgen_impl(ctx):
Expand All @@ -14,7 +17,7 @@ def _js_rust_wasm_bindgen_impl(ctx):
bindgen_flags = ctx.attr.bindgen_flags,
)

return [
providers = [
DefaultInfo(
files = depset([info.wasm], transitive = [info.js, info.ts]),
),
Expand All @@ -29,6 +32,14 @@ def _js_rust_wasm_bindgen_impl(ctx):
),
]

if RustAnalyzerGroupInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo])

if RustAnalyzerInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerInfo])

return providers

js_rust_wasm_bindgen = rule(
doc = """\
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs].
Expand Down
13 changes: 12 additions & 1 deletion extensions/wasm_bindgen/rules_nodejs/defs.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Rust WASM-bindgen rules for interfacing with bazelbuild/rules_nodejs"""

load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo")

# buildifier: disable=bzl-visibility
load("@rules_rust//rust/private:providers.bzl", "RustAnalyzerGroupInfo", "RustAnalyzerInfo")
load("//private:wasm_bindgen.bzl", "WASM_BINDGEN_ATTR", "rust_wasm_bindgen_action")

def _nodejs_rust_wasm_bindgen_impl(ctx):
Expand All @@ -18,7 +21,7 @@ def _nodejs_rust_wasm_bindgen_impl(ctx):
declarations = info.ts
es5_sources = info.js

return [
providers = [
DefaultInfo(
files = depset([info.wasm], transitive = [info.js, info.ts]),
),
Expand All @@ -34,6 +37,14 @@ def _nodejs_rust_wasm_bindgen_impl(ctx):
info,
]

if RustAnalyzerGroupInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerGroupInfo])

if RustAnalyzerInfo in ctx.attr.wasm_file:
providers.append(ctx.attr.wasm_file[RustAnalyzerInfo])

return providers

nodejs_rust_wasm_bindgen = rule(
doc = """\
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [bazelbuild/rules_nodejs][bbnjs].
Expand Down

0 comments on commit d439c41

Please sign in to comment.