From 741d8a2b30d1d8bedde33c66f79d3e08284b40a5 Mon Sep 17 00:00:00 2001 From: Andre Brisco Date: Sun, 21 Mar 2021 15:53:39 -0700 Subject: [PATCH] The default wasm-bindgen toolchain can now optionally be registered --- docs/defs.md | 7 ++--- docs/flatten.md | 42 ++++++++++++++++++----------- docs/rust_wasm_bindgen.md | 35 +++++++++++++++--------- wasm_bindgen/{BUILD => BUILD.bazel} | 0 wasm_bindgen/providers.bzl | 17 ++++++++++++ wasm_bindgen/repositories.bzl | 22 ++++++++------- wasm_bindgen/wasm_bindgen.bzl | 34 +++++++++++++---------- 7 files changed, 103 insertions(+), 54 deletions(-) rename wasm_bindgen/{BUILD => BUILD.bazel} (100%) create mode 100644 wasm_bindgen/providers.bzl diff --git a/docs/defs.md b/docs/defs.md index a0e69b32a5..33986110f9 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -119,9 +119,9 @@ Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`. ## rust_binary
-rust_binary(name, aliases, compile_data, crate_features, crate_root, data, deps, edition,
-            linker_script, out_binary, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
-            rustc_flags, srcs, version)
+rust_binary(name, aliases, compile_data, crate_features, crate_root, crate_type, data, deps,
+            edition, linker_script, out_binary, out_dir_tar, proc_macro_deps, rustc_env,
+            rustc_env_files, rustc_flags, srcs, version)
 
Builds a Rust binary crate. @@ -217,6 +217,7 @@ Hello world | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [include_str!](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | [] | | crate_features | List of features to enable for this crate.

Features are defined in the code using the #[cfg(feature = "foo")] configuration option. The features listed here will be passed to rustc with --cfg feature="${feature_name}" flags. | List of strings | optional | [] | | crate_root | The file that will be passed to rustc to be used for building this crate.

If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None | +| crate_type | Crate type that will be passed to rustc to be used for building this crate.

This option is a temporary workaround and should be used only when building for WebAssembly targets (//rust/platform:wasi and //rust/platform:wasm). | String | optional | "bin" | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer compile_data over data, to prevent the data also being included in the runfiles. | List of labels | optional | [] | | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | diff --git a/docs/flatten.md b/docs/flatten.md index b36309a952..603ee6b91e 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -163,9 +163,9 @@ Run the benchmark test using: `bazel run //fibonacci:fibonacci_bench`. ## rust_binary
-rust_binary(name, aliases, compile_data, crate_features, crate_root, data, deps, edition,
-            linker_script, out_binary, out_dir_tar, proc_macro_deps, rustc_env, rustc_env_files,
-            rustc_flags, srcs, version)
+rust_binary(name, aliases, compile_data, crate_features, crate_root, crate_type, data, deps,
+            edition, linker_script, out_binary, out_dir_tar, proc_macro_deps, rustc_env,
+            rustc_env_files, rustc_flags, srcs, version)
 
Builds a Rust binary crate. @@ -261,6 +261,7 @@ Hello world | compile_data | List of files used by this rule at compile time.

This attribute can be used to specify any data files that are embedded into the library, such as via the [include_str!](https://doc.rust-lang.org/std/macro.include_str!.html) macro. | List of labels | optional | [] | | crate_features | List of features to enable for this crate.

Features are defined in the code using the #[cfg(feature = "foo")] configuration option. The features listed here will be passed to rustc with --cfg feature="${feature_name}" flags. | List of strings | optional | [] | | crate_root | The file that will be passed to rustc to be used for building this crate.

If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None | +| crate_type | Crate type that will be passed to rustc to be used for building this crate.

This option is a temporary workaround and should be used only when building for WebAssembly targets (//rust/platform:wasi and //rust/platform:wasm). | String | optional | "bin" | | data | List of files used by this rule at compile time and runtime.

If including data at compile time with include_str!() and similar, prefer compile_data over data, to prevent the data also being included in the runfiles. | List of labels | optional | [] | | deps | List of other libraries to be linked to this library target.

These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" | @@ -1148,7 +1149,9 @@ Generates a toolchain-bearing repository that declares the toolchains from some rust_wasm_bindgen(name, bindgen_flags, wasm_file) -Generates javascript and typescript bindings for a webassembly module. +Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws]. + +[ws]: https://rustwasm.github.io/docs/wasm-bindgen/ To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the external repositories for the Rust bindgen toolchain (in addition to the Rust rules setup): @@ -1159,7 +1162,9 @@ load("@rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositori rust_wasm_bindgen_repositories() ``` -An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm) +For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories). + +An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm) **ATTRIBUTES** @@ -1182,18 +1187,19 @@ rust_wasm_bindgen_toolchain(name The tools required for the `rust_wasm_bindgen` rule. -You can also use your own version of wasm-bindgen using the toolchain rules below: +In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen), +a unique toolchain can be created as in the example below: ```python load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain") rust_bindgen_toolchain( - bindgen = "//my/raze:cargo_bin_wasm_bindgen", + bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen", ) toolchain( - name = "wasm-bindgen-toolchain", - toolchain = "wasm-bindgen-toolchain-impl", + name = "wasm_bindgen_toolchain", + toolchain = "wasm_bindgen_toolchain_impl", toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain", ) ``` @@ -1202,10 +1208,12 @@ Now that you have your own toolchain, you need to register it by inserting the following statement in your `WORKSPACE` file: ```python -register_toolchains("//my/toolchains:wasm-bindgen-toolchain") +register_toolchains("//my/toolchains:wasm_bindgen_toolchain") ``` -For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html). +For additional information, see the [Bazel toolchains documentation][toolchains]. + +[toolchains]: https://docs.bazel.build/versions/master/toolchains.html **ATTRIBUTES** @@ -1214,7 +1222,7 @@ For additional information, see the [Bazel toolchains documentation](https://doc | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| bindgen | The label of a wasm-bindgen executable. | Label | optional | None | +| bindgen | The label of a wasm-bindgen-cli executable. | Label | optional | None | @@ -1425,12 +1433,16 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with ## rust_wasm_bindgen_repositories
-rust_wasm_bindgen_repositories()
+rust_wasm_bindgen_repositories(register_default_toolchain)
 
-Declare dependencies needed for wasm-bindgen. +Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen). + +**PARAMETERS** -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). +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| register_default_toolchain | If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) (@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain) 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. | True | diff --git a/docs/rust_wasm_bindgen.md b/docs/rust_wasm_bindgen.md index 8d1b5f57ab..96017702f3 100644 --- a/docs/rust_wasm_bindgen.md +++ b/docs/rust_wasm_bindgen.md @@ -11,7 +11,9 @@ rust_wasm_bindgen(name, bindgen_flags, wasm_file) -Generates javascript and typescript bindings for a webassembly module. +Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws]. + +[ws]: https://rustwasm.github.io/docs/wasm-bindgen/ To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the 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 rust_wasm_bindgen_repositories() ``` -An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm) +For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories). + +An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm) **ATTRIBUTES** @@ -45,18 +49,19 @@ rust_wasm_bindgen_toolchain(name The tools required for the `rust_wasm_bindgen` rule. -You can also use your own version of wasm-bindgen using the toolchain rules below: +In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen), +a unique toolchain can be created as in the example below: ```python load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain") rust_bindgen_toolchain( - bindgen = "//my/raze:cargo_bin_wasm_bindgen", + bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen", ) toolchain( - name = "wasm-bindgen-toolchain", - toolchain = "wasm-bindgen-toolchain-impl", + name = "wasm_bindgen_toolchain", + toolchain = "wasm_bindgen_toolchain_impl", toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain", ) ``` @@ -65,10 +70,12 @@ Now that you have your own toolchain, you need to register it by inserting the following statement in your `WORKSPACE` file: ```python -register_toolchains("//my/toolchains:wasm-bindgen-toolchain") +register_toolchains("//my/toolchains:wasm_bindgen_toolchain") ``` -For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html). +For additional information, see the [Bazel toolchains documentation][toolchains]. + +[toolchains]: https://docs.bazel.build/versions/master/toolchains.html **ATTRIBUTES** @@ -77,7 +84,7 @@ For additional information, see the [Bazel toolchains documentation](https://doc | Name | Description | Type | Mandatory | Default | | :------------- | :------------- | :------------- | :------------- | :------------- | | name | A unique name for this target. | Name | required | | -| bindgen | The label of a wasm-bindgen executable. | Label | optional | None | +| bindgen | The label of a wasm-bindgen-cli executable. | Label | optional | None | @@ -85,12 +92,16 @@ For additional information, see the [Bazel toolchains documentation](https://doc ## rust_wasm_bindgen_repositories
-rust_wasm_bindgen_repositories()
+rust_wasm_bindgen_repositories(register_default_toolchain)
 
-Declare dependencies needed for wasm-bindgen. +Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen). + +**PARAMETERS** -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). +| Name | Description | Default Value | +| :------------- | :------------- | :------------- | +| register_default_toolchain | If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) (@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain) 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. | True | diff --git a/wasm_bindgen/BUILD b/wasm_bindgen/BUILD.bazel similarity index 100% rename from wasm_bindgen/BUILD rename to wasm_bindgen/BUILD.bazel diff --git a/wasm_bindgen/providers.bzl b/wasm_bindgen/providers.bzl new file mode 100644 index 0000000000..22a7c922cd --- /dev/null +++ b/wasm_bindgen/providers.bzl @@ -0,0 +1,17 @@ +"""A module for re-exporting the providers used by the rust_wasm_bindgen rule""" + +load( + "@build_bazel_rules_nodejs//internal/providers:declaration_info.bzl", + _DeclarationInfo = "DeclarationInfo", +) +load( + "@build_bazel_rules_nodejs//internal/providers:js_providers.bzl", + _JSEcmaScriptModuleInfo = "JSEcmaScriptModuleInfo", + _JSModuleInfo = "JSModuleInfo", + _JSNamedModuleInfo = "JSNamedModuleInfo", +) + +DeclarationInfo = _DeclarationInfo +JSEcmaScriptModuleInfo = _JSEcmaScriptModuleInfo +JSModuleInfo = _JSModuleInfo +JSNamedModuleInfo = _JSNamedModuleInfo diff --git a/wasm_bindgen/repositories.bzl b/wasm_bindgen/repositories.bzl index a888e37180..3aec9fec6d 100644 --- a/wasm_bindgen/repositories.bzl +++ b/wasm_bindgen/repositories.bzl @@ -18,21 +18,23 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//wasm_bindgen/raze:crates.bzl", "rules_rust_wasm_bindgen_fetch_remote_crates") # buildifier: disable=unnamed-macro -def rust_wasm_bindgen_repositories(): - """Declare dependencies needed for wasm-bindgen. +def rust_wasm_bindgen_repositories(register_default_toolchain = True): + """Declare dependencies needed for [rust_wasm_bindgen](#rust_wasm_bindgen). - 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). + Args: + register_default_toolchain (Label, optional): If True, the default [rust_wasm_bindgen_toolchain](#rust_wasm_bindgen_toolchain) + (`@rules_rust//wasm_bindgen:default_wasm_bindgen_toolchain`) 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. """ maybe( http_archive, name = "build_bazel_rules_nodejs", - sha256 = "dd4dc46066e2ce034cba0c81aa3e862b27e8e8d95871f567359f7a534cccb666", - urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.1.0/rules_nodejs-3.1.0.tar.gz"], + sha256 = "55a25a762fcf9c9b88ab54436581e671bc9f4f523cb5a1bd32459ebec7be68a8", + urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/3.2.2/rules_nodejs-3.2.2.tar.gz"], ) - rules_rust_wasm_bindgen_fetch_remote_crates() - - native.register_toolchains(str(Label("//wasm_bindgen:default_wasm_bindgen_toolchain"))) + # Load dependencies of the default toolchain and register it. + if register_default_toolchain: + rules_rust_wasm_bindgen_fetch_remote_crates() + native.register_toolchains(str(Label("//wasm_bindgen:default_wasm_bindgen_toolchain"))) diff --git a/wasm_bindgen/wasm_bindgen.bzl b/wasm_bindgen/wasm_bindgen.bzl index 82ff5d442f..4d23d90851 100644 --- a/wasm_bindgen/wasm_bindgen.bzl +++ b/wasm_bindgen/wasm_bindgen.bzl @@ -13,19 +13,20 @@ # limitations under the License. # buildifier: disable=module-docstring -load("@build_bazel_rules_nodejs//internal/providers:declaration_info.bzl", "DeclarationInfo") +# buildifier: disable=bzl-visibility +load("//rust/private:transitions.bzl", "wasm_bindgen_transition") load( - "@build_bazel_rules_nodejs//internal/providers:js_providers.bzl", + ":providers.bzl", + "DeclarationInfo", "JSEcmaScriptModuleInfo", "JSModuleInfo", "JSNamedModuleInfo", ) -# buildifier: disable=bzl-visibility -load("//rust/private:transitions.bzl", "wasm_bindgen_transition") - _WASM_BINDGEN_DOC = """\ -Generates javascript and typescript bindings for a webassembly module. +Generates javascript and typescript bindings for a webassembly module using [wasm-bindgen][ws]. + +[ws]: https://rustwasm.github.io/docs/wasm-bindgen/ To use the Rust WebAssembly bindgen rules, add the following to your `WORKSPACE` file to add the 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 rust_wasm_bindgen_repositories() ``` -An example of this rule in use can be seen at [@rules_rust//examples/wasm/...](../examples/wasm) +For more details on `rust_wasm_bindgen_repositories`, see [here](#rust_wasm_bindgen_repositories). + +An example of this rule in use can be seen at [@rules_rust//examples/wasm](../examples/wasm) """ _WASM_BINDGEN_TOOLCHAIN_DOC = """\ The tools required for the `rust_wasm_bindgen` rule. -You can also use your own version of wasm-bindgen using the toolchain rules below: +In cases where users want to control or change the version of `wasm-bindgen` used by [rust_wasm_bindgen](#rust_wasm_bindgen), +a unique toolchain can be created as in the example below: ```python load("@rules_rust//bindgen:bindgen.bzl", "rust_bindgen_toolchain") rust_bindgen_toolchain( - bindgen = "//my/raze:cargo_bin_wasm_bindgen", + bindgen = "//my/cargo_raze:cargo_bin_wasm_bindgen", ) toolchain( - name = "wasm-bindgen-toolchain", - toolchain = "wasm-bindgen-toolchain-impl", + name = "wasm_bindgen_toolchain", + toolchain = "wasm_bindgen_toolchain_impl", toolchain_type = "@rules_rust//wasm_bindgen:wasm_bindgen_toolchain", ) ``` @@ -62,10 +66,12 @@ Now that you have your own toolchain, you need to register it by inserting the following statement in your `WORKSPACE` file: ```python -register_toolchains("//my/toolchains:wasm-bindgen-toolchain") +register_toolchains("//my/toolchains:wasm_bindgen_toolchain") ``` -For additional information, see the [Bazel toolchains documentation](https://docs.bazel.build/versions/master/toolchains.html). +For additional information, see the [Bazel toolchains documentation][toolchains]. + +[toolchains]: https://docs.bazel.build/versions/master/toolchains.html """ def _rust_wasm_bindgen_impl(ctx): @@ -175,7 +181,7 @@ rust_wasm_bindgen_toolchain = rule( doc = _WASM_BINDGEN_TOOLCHAIN_DOC, attrs = { "bindgen": attr.label( - doc = "The label of a `wasm-bindgen` executable.", + doc = "The label of a `wasm-bindgen-cli` executable.", executable = True, cfg = "exec", ),