This repository provides rules for building Rust projects with Bazel.
To use the Rust rules, add the following to your WORKSPACE file to add the external repositories for the Rust toolchain:
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "rules_rust", sha256 = "e6d835ee673f388aa5b62dc23d82db8fc76497e93fa47d8a4afe97abaf09b10d", strip_prefix = "rules_rust-f37b9d6a552e9412285e627f30cb124e709f4f7a", urls = [ # Master branch as of 2021-01-27 "https://github.com/bazelbuild/rules_rust/archive/f37b9d6a552e9412285e627f30cb124e709f4f7a.tar.gz", ], ) load("@rules_rust//rust:repositories.bzl", "rust_repositories") rust_repositories()
The rules are under active development, as such the lastest commit on the main branch should be used. main is only tested against 3.5.0 as the minimum supported version of Bazel. Though previous versions may still be supported in certain environments.
build.rs script from Bazel.You can also browse the full API in one page.
To build with a particular version of the Rust compiler, pass that version to rust_repositories:
rust_repositories(version = "1.51.0", edition="2018")
As well as an exact version, version can be set to "nightly" or "beta". If set to these values, iso_date must also be set:
rust_repositories(version = "nightly", iso_date = "2020-12-30", edition="2018")
Similarly, rustfmt_version may also be configured:
rust_repositories(rustfmt_version = "1.51.0")
Currently the most common approach to managing external dependencies is using cargo-raze to generate BUILD files for Cargo crates.
To build a rust_binary for wasm32-unknown-unknown target add the --platforms=@rules_rust//rust/platform:wasm flag.
bazel build @examples//hello_world_wasm --platforms=@rules_rust//rust/platform:wasm
To build a rust_binary for wasm32-wasi target add the --platforms=@rules_rust//rust/platform:wasi flag.
bazel build @examples//hello_world_wasm --platforms=@rules_rust//rust/platform:wasi
rust_wasm_bindgen will automatically transition to the wasm platform and can be used when building WebAssembly code for the host target.