Skip to content

Commit 7cde1e4

Browse files
johnedmondsacmcarther
authored andcommitted
Add support for wasm-bindgen. (#240)
* Add support for WebAssembly. * Add a comment explaining that the npm deps are for the wasm example. * Use transitions for web assembly. * Upgrade WASM bindgen and make compatible with Typescript. * Remove unnecessary setting. * Re-run cargo-raze. * Add nodejs rules for the examples workspace. * Fix link_env missing. * Bazel 1.0 compatibility. * Exclude matrix_dylib_test on RBE due to bazelbuild/bazel#9987. * Ignore the wasm test because rust-lld isn't available on RBE. * Use extra target triples. * Remove extra newline.
1 parent 5c0e90a commit 7cde1e4

File tree

181 files changed

+10784
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

181 files changed

+10784
-13
lines changed

.bazelci/presubmit.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ tasks:
4040
- "-@examples//hello_lib:hello_lib_doc_test"
4141
- "-//tools/runfiles:runfiles_doc_test"
4242
- "-@examples//ffi/rust_calling_c/simple/..."
43+
# See https://github.com/bazelbuild/bazel/issues/9987
44+
- "-@examples//ffi/rust_calling_c:matrix_dylib_test"
45+
# rust-lld isn't available on RBE
46+
- "-@examples//hello_world_wasm:hello_world_wasm_test"
4347
examples:
4448
name: Examples
4549
platform: ubuntu1804

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# The email address is not required for organizations.
88

99
Google Inc.
10+
Spotify AB
1011
Damien Martin-Guillerez <[email protected]>
1112
David Chen <[email protected]>
1213
Florian Weikert <[email protected]>

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Kristina Chodorow <[email protected]>
1919
Philipp Wollermann <[email protected]>
2020
Ulf Adams <[email protected]>
2121
Justine Alexandra Roberts Tunney <[email protected]>
22+
John Edmonds <[email protected]>

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ This repository provides rules for building [Rust][rust] projects with [Bazel](h
2222
</ul>
2323
</div>
2424

25+
#### WebAssembly
26+
27+
To build a `rust_binary` for wasm32-unknown-unknown add the `--platforms=//rust/platform:wasm` flag.
28+
29+
bazel build @examples//hello_world_wasm --platforms=//rust/platform:wasm
30+
31+
`rust_wasm_bindgen` will automatically transition to the wasm platform and can be used when
32+
building wasm code for the host target.
33+
2534
### Protobuf
2635
<div class="toc">
2736
<ul>

WORKSPACE

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ rust_proto_repositories()
4141
load("@io_bazel_rules_rust//bindgen:repositories.bzl", "rust_bindgen_repositories")
4242
rust_bindgen_repositories()
4343

44+
load("@io_bazel_rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositories")
45+
rust_wasm_bindgen_repositories()
46+
4447
# Stardoc and its dependencies
4548
http_archive(
4649
name = "io_bazel_skydoc",
@@ -54,9 +57,16 @@ skydoc_repositories()
5457
load("@io_bazel_rules_sass//:package.bzl", "rules_sass_dependencies")
5558
rules_sass_dependencies()
5659

57-
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories")
60+
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install")
5861
node_repositories()
5962

63+
# Dependencies for the @examples//hello_world_wasm example.
64+
npm_install(
65+
name = "npm",
66+
package_json = "//:package.json",
67+
package_lock_json = "//:package-lock.json",
68+
)
69+
6070
load("@io_bazel_rules_sass//:defs.bzl", "sass_repositories")
6171
sass_repositories()
6272
# --- end stardoc

examples/WORKSPACE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,31 @@ http_archive(
3737
],
3838
)
3939

40+
http_archive(
41+
name = "build_bazel_rules_nodejs",
42+
url = "https://github.com/bazelbuild/rules_nodejs/archive/0.16.2.zip",
43+
strip_prefix = "rules_nodejs-0.16.2",
44+
sha256 = "9b72bb0aea72d7cbcfc82a01b1e25bf3d85f791e790ddec16c65e2d906382ee0"
45+
)
46+
47+
load("@build_bazel_rules_nodejs//:defs.bzl", "node_repositories", "npm_install")
48+
node_repositories()
49+
50+
# Dependencies for the @examples//hello_world_wasm example.
51+
npm_install(
52+
name = "npm",
53+
package_json = "@io_bazel_rules_rust//:package.json",
54+
package_lock_json = "@io_bazel_rules_rust//:package-lock.json",
55+
)
56+
4057
load("@io_bazel_rules_rust//bindgen:repositories.bzl", "rust_bindgen_repositories")
4158

4259
rust_bindgen_repositories()
4360

61+
load("@io_bazel_rules_rust//wasm_bindgen:repositories.bzl", "rust_wasm_bindgen_repositories")
62+
63+
rust_wasm_bindgen_repositories()
64+
4465
load("@io_bazel_rules_rust//:workspace.bzl", "bazel_version")
4566

4667
bazel_version(name = "bazel_version")

examples/hello_world_wasm/BUILD

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(
4+
"@io_bazel_rules_rust//rust:rust.bzl",
5+
"rust_binary",
6+
)
7+
8+
load(
9+
"@io_bazel_rules_rust//wasm_bindgen:wasm_bindgen.bzl",
10+
"rust_wasm_bindgen",
11+
)
12+
13+
load(
14+
"@build_bazel_rules_nodejs//:defs.bzl",
15+
"jasmine_node_test",
16+
)
17+
18+
rust_binary(
19+
name = "hello_world_wasm",
20+
srcs = ["main.rs"],
21+
edition = "2018",
22+
deps = ["@io_bazel_rules_rust//wasm_bindgen/raze:wasm_bindgen"],
23+
)
24+
25+
rust_wasm_bindgen(
26+
name = "hello_world_wasm_bindgen",
27+
wasm_file = ":hello_world_wasm"
28+
)
29+
30+
jasmine_node_test(
31+
name = "hello_world_wasm_test",
32+
srcs = [
33+
"hello_world_wasm_test.js",
34+
],
35+
data = [
36+
":hello_world_wasm_bindgen_bg.wasm",
37+
"@npm//jasmine",
38+
],
39+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
describe('Calling WebAssembly code', () => {
5+
it('Should run WebAssembly code', async () => {
6+
const buf = fs.readFileSync(path.join(__dirname, 'hello_world_wasm_bindgen_bg.wasm'));
7+
const res = await WebAssembly.instantiate(buf);
8+
expect(res.instance.exports.hello_world(2)).toEqual(4);
9+
})
10+
});

examples/hello_world_wasm/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use wasm_bindgen::prelude::*;
2+
3+
#[wasm_bindgen]
4+
pub fn hello_world(i: i32) -> i32 {
5+
i * 2
6+
}
7+
8+
fn main() {
9+
println!("Hello {}", hello_world(2));
10+
}

package-lock.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"jasmine": "3.4.0"
5+
},
6+
"scripts": {}
7+
}

rust/platform/BUILD

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ package(default_visibility = ["//visibility:public"])
33
load(":platform.bzl", "declare_config_settings")
44

55
declare_config_settings()
6+
7+
package_group(
8+
name = "function_transition_whitelist",
9+
packages = [
10+
"//...",
11+
],
12+
)
13+

rust/platform/platform.bzl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,15 @@ def declare_config_settings():
7979
name = triple,
8080
constraint_values = triple_to_constraint_set(triple),
8181
)
82+
83+
native.constraint_value(
84+
name = "wasm32",
85+
constraint_setting = "@platforms//cpu"
86+
)
87+
88+
native.platform(
89+
name = "wasm",
90+
constraint_values = [
91+
"@io_bazel_rules_rust//rust/platform:wasm32",
92+
]
93+
)

rust/platform/triple_mappings.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ _SYSTEM_TO_BINARY_EXT = {
4343
"darwin": "",
4444
"windows": ".exe",
4545
"emscripten": ".js",
46+
"unknown": "",
4647
}
4748

4849
_SYSTEM_TO_STATICLIB_EXT = {
@@ -52,6 +53,7 @@ _SYSTEM_TO_STATICLIB_EXT = {
5253
# TODO(acmcarther): To be verified
5354
"windows": ".lib",
5455
"emscripten": ".js",
56+
"unknown": "",
5557
}
5658

5759
_SYSTEM_TO_DYLIB_EXT = {
@@ -61,6 +63,7 @@ _SYSTEM_TO_DYLIB_EXT = {
6163
# TODO(acmcarther): To be verified
6264
"windows": ".dll",
6365
"emscripten": ".js",
66+
"unknown": ".wasm",
6467
}
6568

6669
def cpu_arch_to_constraints(cpu_arch):
@@ -120,6 +123,9 @@ def triple_to_constraint_set(triple):
120123
if len(component_parts) == 4:
121124
abi = component_parts[3]
122125

126+
if cpu_arch == "wasm32":
127+
return ["@io_bazel_rules_rust//rust/platform:wasm32"]
128+
123129
constraint_set = []
124130
constraint_set += cpu_arch_to_constraints(cpu_arch)
125131
constraint_set += vendor_to_constraints(vendor)

rust/private/dummy_cc_toolchain/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
load(":dummy_cc_toolchain.bzl", "dummy_cc_toolchain")
2+
dummy_cc_toolchain(name = "dummy_cc_wasm32")
3+
4+
# When compiling Rust code for wasm32, we avoid linking to cpp code so we introduce a dummy cc
5+
# toolchain since we know we'll never look it up.
6+
# TODO([email protected]): Need to support linking C code to rust code when compiling for wasm32.
7+
toolchain(
8+
name = "dummy_cc_wasm32_toolchain",
9+
toolchain = ":dummy_cc_wasm32",
10+
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
11+
target_compatible_with = ["//rust/platform:wasm32"]
12+
)
13+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def _dummy_cc_toolchain_impl(ctx):
2+
return [platform_common.ToolchainInfo()]
3+
4+
dummy_cc_toolchain = rule(
5+
implementation = _dummy_cc_toolchain_impl,
6+
attrs = {},
7+
)
8+

0 commit comments

Comments
 (0)