xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/rust_wasm_bindgen.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan# Rust Wasm Bindgen
3*d4726bddSHONG Yifan
4*d4726bddSHONG Yifan* [rust_wasm_bindgen_dependencies](#rust_wasm_bindgen_dependencies)
5*d4726bddSHONG Yifan* [rust_wasm_bindgen_register_toolchains](#rust_wasm_bindgen_register_toolchains)
6*d4726bddSHONG Yifan* [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain)
7*d4726bddSHONG Yifan* [rust_wasm_bindgen](#rust_wasm_bindgen)
8*d4726bddSHONG Yifan* [RustWasmBindgenInfo](#RustWasmBindgenInfo)
9*d4726bddSHONG Yifan
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan## Overview
12*d4726bddSHONG Yifan
13*d4726bddSHONG YifanBazel rules for generating wasm modules for Javascript using [wasm-bindgen][wb].
14*d4726bddSHONG Yifan
15*d4726bddSHONG Yifan## Setup
16*d4726bddSHONG Yifan
17*d4726bddSHONG YifanTo begin using the `wasm-bindgen` rules, users can load the necessary dependencies
18*d4726bddSHONG Yifanin their workspace by adding the following to their `WORKSPACE.bazel` file.
19*d4726bddSHONG Yifan
20*d4726bddSHONG Yifan```starlark
21*d4726bddSHONG Yifanload("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_dependencies", "rust_wasm_bindgen_register_toolchains")
22*d4726bddSHONG Yifan
23*d4726bddSHONG Yifanrust_wasm_bindgen_dependencies()
24*d4726bddSHONG Yifan
25*d4726bddSHONG Yifanrust_wasm_bindgen_register_toolchains()
26*d4726bddSHONG Yifan```
27*d4726bddSHONG Yifan
28*d4726bddSHONG YifanThis should enable users to start using the [rust_wasm_bindgen](#rust_wasm_bindgen)
29*d4726bddSHONG Yifanrule. However, it's common to want to control the version of `wasm-bindgen` in the
30*d4726bddSHONG Yifanworkspace instead of relying on the one provided by `rules_rust`. In this case, users
31*d4726bddSHONG Yifanshould avoid calling `rust_wasm_bindgen_register_toolchains` and instead use the
32*d4726bddSHONG Yifan[rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) rule to define their own
33*d4726bddSHONG Yifantoolchains to register in the workspace.
34*d4726bddSHONG Yifan
35*d4726bddSHONG Yifan### Interfacing with Javascript rules
36*d4726bddSHONG Yifan
37*d4726bddSHONG YifanWhile it's recommended for users to mantain their own , in the
38*d4726bddSHONG Yifan`@rules_rust//wasm_bindgen` package there exists interface sub-packages for various
39*d4726bddSHONG YifanJavascript Bazel rules. E.g. `build_bazel_rules_nodejs` or `aspect_rules_js`. The
40*d4726bddSHONG Yifanrules defined there are a more convenient way to use `rust_wasm_bindgen` with the
41*d4726bddSHONG Yifanassociated javascript rules due to the inclusion of additional providers. Each
42*d4726bddSHONG Yifandirectory contains a `defs.bzl` file that defines the different variants of
43*d4726bddSHONG Yifan`rust_wasm_bindgen`. (e.g. `nodejs_rust_wasm_bindgen` for the `rules_nodejs` submodule).
44*d4726bddSHONG Yifan
45*d4726bddSHONG Yifan
46*d4726bddSHONG Yifan[wb]: https://github.com/rustwasm/wasm-bindgen
47*d4726bddSHONG Yifan
48*d4726bddSHONG Yifan
49*d4726bddSHONG Yifan<a id="rust_wasm_bindgen"></a>
50*d4726bddSHONG Yifan
51*d4726bddSHONG Yifan## rust_wasm_bindgen
52*d4726bddSHONG Yifan
53*d4726bddSHONG Yifan<pre>
54*d4726bddSHONG Yifanrust_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-wasm_file">wasm_file</a>)
55*d4726bddSHONG Yifan</pre>
56*d4726bddSHONG Yifan
57*d4726bddSHONG YifanGenerates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
58*d4726bddSHONG Yifan
59*d4726bddSHONG Yifan[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
60*d4726bddSHONG Yifan
61*d4726bddSHONG YifanAn example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
62*d4726bddSHONG Yifan
63*d4726bddSHONG Yifan**ATTRIBUTES**
64*d4726bddSHONG Yifan
65*d4726bddSHONG Yifan
66*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
67*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
68*d4726bddSHONG Yifan| <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 |  |
69*d4726bddSHONG Yifan| <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 |  `[]`  |
70*d4726bddSHONG Yifan| <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"`  |
71*d4726bddSHONG Yifan| <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 |  |
72*d4726bddSHONG Yifan
73*d4726bddSHONG Yifan
74*d4726bddSHONG Yifan<a id="rust_wasm_bindgen_toolchain"></a>
75*d4726bddSHONG Yifan
76*d4726bddSHONG Yifan## rust_wasm_bindgen_toolchain
77*d4726bddSHONG Yifan
78*d4726bddSHONG Yifan<pre>
79*d4726bddSHONG Yifanrust_wasm_bindgen_toolchain(<a href="#rust_wasm_bindgen_toolchain-name">name</a>, <a href="#rust_wasm_bindgen_toolchain-bindgen">bindgen</a>)
80*d4726bddSHONG Yifan</pre>
81*d4726bddSHONG Yifan
82*d4726bddSHONG YifanThe tools required for the `rust_wasm_bindgen` rule.
83*d4726bddSHONG Yifan
84*d4726bddSHONG YifanIn cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen),
85*d4726bddSHONG Yifana unique toolchain can be created as in the example below:
86*d4726bddSHONG Yifan
87*d4726bddSHONG Yifan```python
88*d4726bddSHONG Yifanload("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
89*d4726bddSHONG Yifan
90*d4726bddSHONG Yifanrust_bindgen_toolchain(
91*d4726bddSHONG Yifan    bindgen = "//3rdparty/crates:wasm_bindgen_cli__bin",
92*d4726bddSHONG Yifan)
93*d4726bddSHONG Yifan
94*d4726bddSHONG Yifantoolchain(
95*d4726bddSHONG Yifan    name = "wasm_bindgen_toolchain",
96*d4726bddSHONG Yifan    toolchain = "wasm_bindgen_toolchain_impl",
97*d4726bddSHONG Yifan    toolchain_type = "@rules_rust//wasm_bindgen:toolchain_type",
98*d4726bddSHONG Yifan)
99*d4726bddSHONG Yifan```
100*d4726bddSHONG Yifan
101*d4726bddSHONG YifanNow that you have your own toolchain, you need to register it by
102*d4726bddSHONG Yifaninserting the following statement in your `WORKSPACE` file:
103*d4726bddSHONG Yifan
104*d4726bddSHONG Yifan```python
105*d4726bddSHONG Yifanregister_toolchains("//my/toolchains:wasm_bindgen_toolchain")
106*d4726bddSHONG Yifan```
107*d4726bddSHONG Yifan
108*d4726bddSHONG YifanFor additional information, see the [Bazel toolchains documentation][toolchains].
109*d4726bddSHONG Yifan
110*d4726bddSHONG Yifan[toolchains]: https://docs.bazel.build/versions/master/toolchains.html
111*d4726bddSHONG Yifan
112*d4726bddSHONG Yifan**ATTRIBUTES**
113*d4726bddSHONG Yifan
114*d4726bddSHONG Yifan
115*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
116*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
117*d4726bddSHONG Yifan| <a id="rust_wasm_bindgen_toolchain-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
118*d4726bddSHONG Yifan| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen |  The label of a `wasm-bindgen-cli` executable.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
119*d4726bddSHONG Yifan
120*d4726bddSHONG Yifan
121*d4726bddSHONG Yifan<a id="RustWasmBindgenInfo"></a>
122*d4726bddSHONG Yifan
123*d4726bddSHONG Yifan## RustWasmBindgenInfo
124*d4726bddSHONG Yifan
125*d4726bddSHONG Yifan<pre>
126*d4726bddSHONG YifanRustWasmBindgenInfo(<a href="#RustWasmBindgenInfo-js">js</a>, <a href="#RustWasmBindgenInfo-ts">ts</a>, <a href="#RustWasmBindgenInfo-wasm">wasm</a>)
127*d4726bddSHONG Yifan</pre>
128*d4726bddSHONG Yifan
129*d4726bddSHONG YifanInfo about wasm-bindgen outputs.
130*d4726bddSHONG Yifan
131*d4726bddSHONG Yifan**FIELDS**
132*d4726bddSHONG Yifan
133*d4726bddSHONG Yifan
134*d4726bddSHONG Yifan| Name  | Description |
135*d4726bddSHONG Yifan| :------------- | :------------- |
136*d4726bddSHONG Yifan| <a id="RustWasmBindgenInfo-js"></a>js |  Depset[File]: The Javascript files produced by `wasm-bindgen`.    |
137*d4726bddSHONG Yifan| <a id="RustWasmBindgenInfo-ts"></a>ts |  Depset[File]: The Typescript files produced by `wasm-bindgen`.    |
138*d4726bddSHONG Yifan| <a id="RustWasmBindgenInfo-wasm"></a>wasm |  File: The `.wasm` file generated by `wasm-bindgen`.    |
139*d4726bddSHONG Yifan
140*d4726bddSHONG Yifan
141*d4726bddSHONG Yifan<a id="rust_wasm_bindgen_dependencies"></a>
142*d4726bddSHONG Yifan
143*d4726bddSHONG Yifan## rust_wasm_bindgen_dependencies
144*d4726bddSHONG Yifan
145*d4726bddSHONG Yifan<pre>
146*d4726bddSHONG Yifanrust_wasm_bindgen_dependencies()
147*d4726bddSHONG Yifan</pre>
148*d4726bddSHONG Yifan
149*d4726bddSHONG YifanDeclare dependencies needed for the `rules_rust` [wasm-bindgen][wb] rules.
150*d4726bddSHONG Yifan
151*d4726bddSHONG Yifan[wb]: https://github.com/rustwasm/wasm-bindgen
152*d4726bddSHONG Yifan
153*d4726bddSHONG Yifan
154*d4726bddSHONG Yifan
155*d4726bddSHONG Yifan**RETURNS**
156*d4726bddSHONG Yifan
157*d4726bddSHONG Yifanlist[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
158*d4726bddSHONG Yifan  defined by this macro.
159*d4726bddSHONG Yifan
160*d4726bddSHONG Yifan
161*d4726bddSHONG Yifan<a id="rust_wasm_bindgen_register_toolchains"></a>
162*d4726bddSHONG Yifan
163*d4726bddSHONG Yifan## rust_wasm_bindgen_register_toolchains
164*d4726bddSHONG Yifan
165*d4726bddSHONG Yifan<pre>
166*d4726bddSHONG Yifanrust_wasm_bindgen_register_toolchains(<a href="#rust_wasm_bindgen_register_toolchains-register_toolchains">register_toolchains</a>)
167*d4726bddSHONG Yifan</pre>
168*d4726bddSHONG Yifan
169*d4726bddSHONG YifanRegisters the default toolchains for the `rules_rust` [wasm-bindgen][wb] rules.
170*d4726bddSHONG Yifan
171*d4726bddSHONG Yifan[wb]: https://github.com/rustwasm/wasm-bindgen
172*d4726bddSHONG Yifan
173*d4726bddSHONG Yifan
174*d4726bddSHONG Yifan**PARAMETERS**
175*d4726bddSHONG Yifan
176*d4726bddSHONG Yifan
177*d4726bddSHONG Yifan| Name  | Description | Default Value |
178*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
179*d4726bddSHONG Yifan| <a id="rust_wasm_bindgen_register_toolchains-register_toolchains"></a>register_toolchains |  Whether or not to register toolchains.   |  `True` |
180*d4726bddSHONG Yifan
181*d4726bddSHONG Yifan
182