bazelbuild / bazelbuild/rules_rust

Equivalent Method to Specify `--target=wasm32-unknown-emscripten`

Open
#2,796 0 comments 0 reactions 0 assignees View on GitHub
needs-triage
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

I'd like to use Bazel to build a Wasm target from a Rust library. With cargo, I'd use the following:

```
cargo build --release --target=wasm32-unknown-emscripten
```

**What's the equivalent method with `rules_rust` to specify a particular target architecture?**

# Trial & Error

I tried mucking around with `rustc_flags`, per below:
```
rust_library(
name = "my_library",
srcs = ["lib.rs"],
rustc_flags = [
"--target=wasm32-unknown-unknown"
]
)
```
But this resulted in, "`error: Option 'target' given more than once`."

# More details

I want to use Bazel to create a Wasm binary from a C++ binary that relies on a Rust library. I'm making use of `wasm_cc_binary` as follows.
```
load("@emsdk//emscripten_toolchain:wasm_rules.bzl", "wasm_cc_binary")

wasm_cc_binary(
name = "my_wasm_binary",
cc_target = ":main",
)
```

The `cc_binary` associated looks as follows:

```
cc_binary(
name = "main",
srcs = ["main.cpp"],
deps = [
"//my/rust:mylibrary",
]
)
```

And lastly, the `rust_library` looks as follows:
```
rust_library(
name = "mylibrary",
srcs = ["lib.rs"],
deps = all_crate_deps(normal = True),
)
```

If I run `bazel build` on the above, I get the following error:
```
No matching toolchains found for types @@rules_rust~//rust:toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@@rules_rust~//rust:toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
```

## A hacky fix

I'm able to successfully build a Wasm target if I do the following.
1. Run `cargo build --release --target=wasm32-unknown-emscripten` to generate a `target/wasm32-unknown-emscripten/release/mylibrary.a` output
2. Update my `cc_binary` above so that it uses this `mylibrary.a` file as a `src`:
```
cc_binary(
name = "main",
srcs = [
"main.cpp"
"path/to/mylibrary.a"
]
)
```

I hope I didn't miss a simple answer in the documentation!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.