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
…3035)

This is useful for targets which are gated behind
`target_compatible_with = ["@platforms//os:wasm32"]` but developers
still want them to analyzed by rust-analyzer.
  • Loading branch information
UebelAndre authored Dec 5, 2024
1 parent a7aba2d commit 1374678
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/src/rust_wasm_bindgen_rules_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Generates javascript and typescript bindings for a webassembly module using [was
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="js_rust_wasm_bindgen-name"></a>name | A unique name for this target. | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required | |
| <a id="js_rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
| <a id="js_rust_wasm_bindgen-bindgen_flags"></a>bindgen_flags | Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
| <a id="js_rust_wasm_bindgen-target"></a>target | The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details. | String | optional | `"bundler"` |
| <a id="js_rust_wasm_bindgen-target_arch"></a>target_arch | The target architecture to use for the wasm-bindgen command line option. | String | optional | `"wasm32"` |
| <a id="js_rust_wasm_bindgen-wasm_file"></a>wasm_file | The `.wasm` file or crate to generate bindings for. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
Expand Down
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")

# 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 @@ -95,19 +98,27 @@ def _rust_wasm_bindgen_impl(ctx):
toolchain = toolchain,
wasm_file = ctx.attr.wasm_file,
target_output = ctx.attr.target,
bindgen_flags = ctx.attr.bindgen_flags,
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
15 changes: 13 additions & 2 deletions 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 @@ -11,10 +14,10 @@ def _js_rust_wasm_bindgen_impl(ctx):
toolchain = toolchain,
wasm_file = ctx.attr.wasm_file,
target_output = ctx.attr.target,
bindgen_flags = ctx.attr.bindgen_flags,
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
15 changes: 13 additions & 2 deletions 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 @@ -11,14 +14,14 @@ def _nodejs_rust_wasm_bindgen_impl(ctx):
toolchain = toolchain,
wasm_file = ctx.attr.wasm_file,
target_output = ctx.attr.target,
bindgen_flags = ctx.attr.bindgen_flags,
flags = ctx.attr.bindgen_flags,
)

# Return a structure that is compatible with the deps[] of a ts_library.
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 1374678

Please sign in to comment.