Skip to content

Commit

Permalink
Added out_name attribute to rust_wasm_bindgen. (#3086)
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre authored Dec 11, 2024
1 parent 7743935 commit 9cdb86c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 33 deletions.
5 changes: 3 additions & 2 deletions docs/src/rust_wasm_bindgen.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ for the `rules_js` submodule).
## rust_wasm_bindgen

<pre>
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
rust_wasm_bindgen(<a href="#rust_wasm_bindgen-name">name</a>, <a href="#rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#rust_wasm_bindgen-out_name">out_name</a>, <a href="#rust_wasm_bindgen-target">target</a>, <a href="#rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#rust_wasm_bindgen-wasm_file">wasm_file</a>)
</pre>

Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
Expand All @@ -62,7 +62,8 @@ An example of this rule in use can be seen at [@rules_rust//examples/wasm](../ex
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <a id="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="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="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="rust_wasm_bindgen-out_name"></a>out_name | Set a custom output filename (Without extension. Defaults to target name). | String | optional | `""` |
| <a id="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="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="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
3 changes: 2 additions & 1 deletion docs/src/rust_wasm_bindgen_rules_js.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Rust WASM-bindgen rules for interfacing with aspect-build/rules_js
## js_rust_wasm_bindgen

<pre>
js_rust_wasm_bindgen(<a href="#js_rust_wasm_bindgen-name">name</a>, <a href="#js_rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#js_rust_wasm_bindgen-target">target</a>, <a href="#js_rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#js_rust_wasm_bindgen-wasm_file">wasm_file</a>)
js_rust_wasm_bindgen(<a href="#js_rust_wasm_bindgen-name">name</a>, <a href="#js_rust_wasm_bindgen-bindgen_flags">bindgen_flags</a>, <a href="#js_rust_wasm_bindgen-out_name">out_name</a>, <a href="#js_rust_wasm_bindgen-target">target</a>, <a href="#js_rust_wasm_bindgen-target_arch">target_arch</a>, <a href="#js_rust_wasm_bindgen-wasm_file">wasm_file</a>)
</pre>

Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws] that interface with [aspect-build/rules_js][abjs].
Expand All @@ -22,6 +22,7 @@ Generates javascript and typescript bindings for a webassembly module using [was
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | `[]` |
| <a id="js_rust_wasm_bindgen-out_name"></a>out_name | Set a custom output filename (Without extension. Defaults to target name). | String | 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
44 changes: 14 additions & 30 deletions extensions/wasm_bindgen/private/wasm_bindgen.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,28 @@ def rust_wasm_bindgen_action(*, ctx, toolchain, wasm_file, target_output, flags
wasm_file,
))

bindgen_wasm_module = ctx.actions.declare_file(ctx.label.name + "_bg.wasm")
out_name = ctx.label.name
if ctx.attr.out_name:
out_name = ctx.attr.out_name

js_out = [ctx.actions.declare_file(ctx.label.name + ".js")]
bindgen_wasm_module = ctx.actions.declare_file(out_name + "_bg.wasm")

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

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

outputs = [bindgen_wasm_module] + js_out + ts_out

args = ctx.actions.args()
args.add("--target", target_output)
args.add("--out-dir", bindgen_wasm_module.dirname)
args.add("--out-name", ctx.label.name)
args.add("--out-name", out_name)
args.add_all(flags)
args.add(input_file)

Expand Down Expand Up @@ -120,6 +124,9 @@ WASM_BINDGEN_ATTR = {
"bindgen_flags": attr.string_list(
doc = "Flags to pass directly to the wasm-bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details.",
),
"out_name": attr.string(
doc = "Set a custom output filename (Without extension. Defaults to target name).",
),
"target": attr.string(
doc = "The type of output to generate. See https://rustwasm.github.io/wasm-bindgen/reference/deployment.html for details.",
default = "bundler",
Expand Down Expand Up @@ -150,30 +157,7 @@ Generates javascript and typescript bindings for a webassembly module using [was
An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
""",
attrs = {
"bindgen_flags": attr.string_list(
doc = "Flags to pass directly to the 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.",
default = "bundler",
values = ["web", "bundler", "nodejs", "no-modules", "deno"],
),
"target_arch": attr.string(
doc = "The target architecture to use for the wasm-bindgen command line option.",
default = "wasm32",
values = ["wasm32", "wasm64"],
),
"wasm_file": attr.label(
doc = "The `.wasm` file or crate to generate bindings for.",
allow_single_file = True,
cfg = wasm_bindgen_transition,
mandatory = True,
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
),
},
attrs = WASM_BINDGEN_ATTR,
toolchains = [
str(Label("//:toolchain_type")),
],
Expand Down

0 comments on commit 9cdb86c

Please sign in to comment.