Skip to content

Commit 9202569

Browse files
authored
The default wasm-bindgen toolchain can now optionally be registered (#640)
I find it to be the common case that users will want to register their own `rust_wasm_bindgen_toolchain` since they'll be writing code with their own set of crate dependencies and would otherwise be limited to the version of `wasm-bindgen` used in the default toolchain. This PR should make it clearer and easier for users looking to setup their own toolchain. Notable changes in this PR: - Added `register_default_toolchain` to `rust_wasm_bindgen_repositories` - Improved docs - Raised the default version of `rules_nodejs` from `3.1.0` to `3.2.2` to pick up a few bug fixes. - Added a `providers.bzl` file for easy access to what `rust_wasm_bindgen` returns
1 parent a3d4b12 commit 9202569

File tree

6 files changed

+95
-48
lines changed

6 files changed

+95
-48
lines changed

docs/flatten.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,9 @@ Generates a toolchain-bearing repository that declares the toolchains from some
11491149
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-wasm_file">wasm_file</a>)
11501150
</pre>
11511151

1152-
Generates javascript and typescript bindings for a webassembly module.
1152+
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
1153+
1154+
[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
11531155

11541156
To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the
11551157
external repositories for the Rust bindgen toolchain (in addition to the Rust rules setup):
@@ -1160,7 +1162,9 @@ load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositori
11601162
rust_wasm_bindgen_repositories()
11611163
```
11621164

1163-
An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm)
1165+
For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories).
1166+
1167+
An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
11641168

11651169

11661170
**ATTRIBUTES**
@@ -1183,18 +1187,19 @@ rust_wasm_bindgen_toolchain(<a href="#rust_wasm_bindgen_toolchain-name">name</a>
11831187

11841188
The tools required for the `rust_wasm_bindgen` rule.
11851189

1186-
You can also use your own version of wasm-bindgen using the toolchain rules below:
1190+
In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen),
1191+
a unique toolchain can be created as in the example below:
11871192

11881193
```python
11891194
load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
11901195

11911196
rust_bindgen_toolchain(
1192-
bindgen = "//my/raze:cargo_bin_wasm_bindgen",
1197+
bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen",
11931198
)
11941199

11951200
toolchain(
1196-
name = "wasm-bindgen-toolchain",
1197-
toolchain = "wasm-bindgen-toolchain-impl",
1201+
name = "wasm_bindgen_toolchain",
1202+
toolchain = "wasm_bindgen_toolchain_impl",
11981203
toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain",
11991204
)
12001205
```
@@ -1203,10 +1208,12 @@ Now that you have your own toolchain, you need to register it by
12031208
inserting the following statement in your `WORKSPACE` file:
12041209

12051210
```python
1206-
register_toolchains("//my/toolchains:wasm-bindgen-toolchain")
1211+
register_toolchains("//my/toolchains:wasm_bindgen_toolchain")
12071212
```
12081213

1209-
For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html).
1214+
For additional information, see the [Bazel toolchains documentation][toolchains].
1215+
1216+
[toolchains]: https://docs.bazel.build/versions/master/toolchains.html
12101217

12111218

12121219
**ATTRIBUTES**
@@ -1215,7 +1222,7 @@ For additional information, see the [Bazel toolchains documentation](https://doc
12151222
| Name | Description | Type | Mandatory | Default |
12161223
| :------------- | :------------- | :------------- | :------------- | :------------- |
12171224
| <a id="rust_wasm_bindgen_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
1218-
| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen | The label of a <code>wasm-bindgen</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
1225+
| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen | The label of a <code>wasm-bindgen-cli</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
12191226

12201227

12211228
<a id="#cargo_build_script"></a>
@@ -1426,12 +1433,16 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
14261433
## rust_wasm_bindgen_repositories
14271434

14281435
<pre>
1429-
rust_wasm_bindgen_repositories()
1436+
rust_wasm_bindgen_repositories(<a href="#rust_wasm_bindgen_repositories-register_default_toolchain">register_default_toolchain</a>)
14301437
</pre>
14311438

1432-
Declare dependencies needed for wasm-bindgen.
1439+
Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen).
1440+
1441+
**PARAMETERS**
14331442

1434-
This macro will load crate dependencies of `wasm-bindgen` that are generated using [cargo raze][raze] inside the rules_rust repository. This makes the default toolchain `@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain` available. For more information on `wasm_bindgen` toolchains, see [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain).
14351443

1444+
| Name | Description | Default Value |
1445+
| :------------- | :------------- | :------------- |
1446+
| <a id="rust_wasm_bindgen_repositories-register_default_toolchain"></a>register_default_toolchain | If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) (<code>@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain</code>) is registered. This toolchain requires a set of dependencies that were generated using [cargo raze](https://github.com/google/cargo-raze). These will also be loaded. | <code>True</code> |
14361447

14371448

docs/rust_wasm_bindgen.md

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
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-wasm_file">wasm_file</a>)
1212
</pre>
1313

14-
Generates javascript and typescript bindings for a webassembly module.
14+
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
15+
16+
[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
1517

1618
To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the
1719
external repositories for the Rust bindgen toolchain (in addition to the Rust rules setup):
@@ -22,7 +24,9 @@ load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositori
2224
rust_wasm_bindgen_repositories()
2325
```
2426

25-
An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm)
27+
For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories).
28+
29+
An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
2630

2731

2832
**ATTRIBUTES**
@@ -45,18 +49,19 @@ rust_wasm_bindgen_toolchain(<a href="#rust_wasm_bindgen_toolchain-name">name</a>
4549

4650
The tools required for the `rust_wasm_bindgen` rule.
4751

48-
You can also use your own version of wasm-bindgen using the toolchain rules below:
52+
In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen),
53+
a unique toolchain can be created as in the example below:
4954

5055
```python
5156
load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
5257

5358
rust_bindgen_toolchain(
54-
bindgen = "//my/raze:cargo_bin_wasm_bindgen",
59+
bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen",
5560
)
5661

5762
toolchain(
58-
name = "wasm-bindgen-toolchain",
59-
toolchain = "wasm-bindgen-toolchain-impl",
63+
name = "wasm_bindgen_toolchain",
64+
toolchain = "wasm_bindgen_toolchain_impl",
6065
toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain",
6166
)
6267
```
@@ -65,10 +70,12 @@ Now that you have your own toolchain, you need to register it by
6570
inserting the following statement in your `WORKSPACE` file:
6671

6772
```python
68-
register_toolchains("//my/toolchains:wasm-bindgen-toolchain")
73+
register_toolchains("//my/toolchains:wasm_bindgen_toolchain")
6974
```
7075

71-
For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html).
76+
For additional information, see the [Bazel toolchains documentation][toolchains].
77+
78+
[toolchains]: https://docs.bazel.build/versions/master/toolchains.html
7279

7380

7481
**ATTRIBUTES**
@@ -77,20 +84,24 @@ For additional information, see the [Bazel toolchains documentation](https://doc
7784
| Name | Description | Type | Mandatory | Default |
7885
| :------------- | :------------- | :------------- | :------------- | :------------- |
7986
| <a id="rust_wasm_bindgen_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
80-
| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen | The label of a <code>wasm-bindgen</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
87+
| <a id="rust_wasm_bindgen_toolchain-bindgen"></a>bindgen | The label of a <code>wasm-bindgen-cli</code> executable. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
8188

8289

8390
<a id="#rust_wasm_bindgen_repositories"></a>
8491

8592
## rust_wasm_bindgen_repositories
8693

8794
<pre>
88-
rust_wasm_bindgen_repositories()
95+
rust_wasm_bindgen_repositories(<a href="#rust_wasm_bindgen_repositories-register_default_toolchain">register_default_toolchain</a>)
8996
</pre>
9097

91-
Declare dependencies needed for wasm-bindgen.
98+
Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen).
99+
100+
**PARAMETERS**
92101

93-
This macro will load crate dependencies of `wasm-bindgen` that are generated using [cargo raze][raze] inside the rules_rust repository. This makes the default toolchain `@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain` available. For more information on `wasm_bindgen` toolchains, see [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain).
94102

103+
| Name | Description | Default Value |
104+
| :------------- | :------------- | :------------- |
105+
| <a id="rust_wasm_bindgen_repositories-register_default_toolchain"></a>register_default_toolchain | If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) (<code>@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain</code>) is registered. This toolchain requires a set of dependencies that were generated using [cargo raze](https://github.com/google/cargo-raze). These will also be loaded. | <code>True</code> |
95106

96107

File renamed without changes.

wasm_bindgen/providers.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""A module for re-exporting the providers used by the rust_wasm_bindgen rule"""
2+
3+
load(
4+
"@build_bazel_rules_nodejs//internal/providers:declaration_info.bzl",
5+
_DeclarationInfo = "DeclarationInfo",
6+
)
7+
load(
8+
"@build_bazel_rules_nodejs//internal/providers:js_providers.bzl",
9+
_JSEcmaScriptModuleInfo = "JSEcmaScriptModuleInfo",
10+
_JSModuleInfo = "JSModuleInfo",
11+
_JSNamedModuleInfo = "JSNamedModuleInfo",
12+
)
13+
14+
DeclarationInfo = _DeclarationInfo
15+
JSEcmaScriptModuleInfo = _JSEcmaScriptModuleInfo
16+
JSModuleInfo = _JSModuleInfo
17+
JSNamedModuleInfo = _JSNamedModuleInfo

wasm_bindgen/repositories.bzl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
1818
load("//wasm_bindgen/raze:crates.bzl", "rules_rust_wasm_bindgen_fetch_remote_crates")
1919

2020
# buildifier: disable=unnamed-macro
21-
def rust_wasm_bindgen_repositories():
22-
"""Declare dependencies needed for wasm-bindgen.
21+
def rust_wasm_bindgen_repositories(register_default_toolchain = True):
22+
"""Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen).
2323
24-
This macro will load crate dependencies of `wasm-bindgen` that are generated using [cargo raze][raze] inside the rules_rust \
25-
repository. This makes the default toolchain `@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain` available. For \
26-
more information on `wasm_bindgen` toolchains, see [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain).
24+
Args:
25+
register_default_toolchain (Label, optional): If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain)
26+
(`@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain`) is registered. This toolchain requires a set of dependencies
27+
that were generated using [cargo raze](https://github.com/google/cargo-raze). These will also be loaded.
2728
"""
2829

2930
maybe(
3031
http_archive,
3132
name = "build_bazel_rules_nodejs",
32-
sha256 = "dd4dc46066e2ce034cba0c81aa3e862b27e8e8d95871f567359f7a534cccb666",
33-
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.1.0/rules_nodejs-3.1.0.tar.gz"],
33+
sha256 = "55a25a762fcf9c9b88ab54436581e671bc9f4f523cb5a1bd32459ebec7be68a8",
34+
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.2.2/rules_nodejs-3.2.2.tar.gz"],
3435
)
3536

36-
rules_rust_wasm_bindgen_fetch_remote_crates()
37-
38-
native.register_toolchains(str(Label("//wasm_bindgen:default_wasm_bindgen_toolchain")))
37+
# Load dependencies of the default toolchain and register it.
38+
if register_default_toolchain:
39+
rules_rust_wasm_bindgen_fetch_remote_crates()
40+
native.register_toolchains(str(Label("//wasm_bindgen:default_wasm_bindgen_toolchain")))

wasm_bindgen/wasm_bindgen.bzl

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
# limitations under the License.
1414

1515
# buildifier: disable=module-docstring
16-
load("@build_bazel_rules_nodejs//internal/providers:declaration_info.bzl", "DeclarationInfo")
16+
# buildifier: disable=bzl-visibility
17+
load("//rust/private:transitions.bzl", "wasm_bindgen_transition")
1718
load(
18-
"@build_bazel_rules_nodejs//internal/providers:js_providers.bzl",
19+
":providers.bzl",
20+
"DeclarationInfo",
1921
"JSEcmaScriptModuleInfo",
2022
"JSModuleInfo",
2123
"JSNamedModuleInfo",
2224
)
2325

24-
# buildifier: disable=bzl-visibility
25-
load("//rust/private:transitions.bzl", "wasm_bindgen_transition")
26-
2726
_WASM_BINDGEN_DOC = """\
28-
Generates javascript and typescript bindings for a webassembly module.
27+
Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws].
28+
29+
[ws]: https://rustwasm.github.io/docs/wasm-bindgen/
2930
3031
To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the
3132
external repositories for the Rust bindgen toolchain (in addition to the Rust rules setup):
@@ -36,24 +37,27 @@ load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositori
3637
rust_wasm_bindgen_repositories()
3738
```
3839
39-
An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm)
40+
For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories).
41+
42+
An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm)
4043
"""
4144

4245
_WASM_BINDGEN_TOOLCHAIN_DOC = """\
4346
The tools required for the `rust_wasm_bindgen` rule.
4447
45-
You can also use your own version of wasm-bindgen using the toolchain rules below:
48+
In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen),
49+
a unique toolchain can be created as in the example below:
4650
4751
```python
4852
load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain")
4953
5054
rust_bindgen_toolchain(
51-
bindgen = "//my/raze:cargo_bin_wasm_bindgen",
55+
bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen",
5256
)
5357
5458
toolchain(
55-
name = "wasm-bindgen-toolchain",
56-
toolchain = "wasm-bindgen-toolchain-impl",
59+
name = "wasm_bindgen_toolchain",
60+
toolchain = "wasm_bindgen_toolchain_impl",
5761
toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain",
5862
)
5963
```
@@ -62,10 +66,12 @@ Now that you have your own toolchain, you need to register it by
6266
inserting the following statement in your `WORKSPACE` file:
6367
6468
```python
65-
register_toolchains("//my/toolchains:wasm-bindgen-toolchain")
69+
register_toolchains("//my/toolchains:wasm_bindgen_toolchain")
6670
```
6771
68-
For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html).
72+
For additional information, see the [Bazel toolchains documentation][toolchains].
73+
74+
[toolchains]: https://docs.bazel.build/versions/master/toolchains.html
6975
"""
7076

7177
def _rust_wasm_bindgen_impl(ctx):
@@ -175,7 +181,7 @@ rust_wasm_bindgen_toolchain = rule(
175181
doc = _WASM_BINDGEN_TOOLCHAIN_DOC,
176182
attrs = {
177183
"bindgen": attr.label(
178-
doc = "The label of a `wasm-bindgen` executable.",
184+
doc = "The label of a `wasm-bindgen-cli` executable.",
179185
executable = True,
180186
cfg = "exec",
181187
),

0 commit comments

Comments
 (0)