Rules rust
Overview
This repository provides rules for building Rust projects with Bazel.
Setup
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 = "18c0a02a007cd26c3f5b4d21dc26a80af776ef755460028a796bc61c649fdf3f",
strip_prefix = "rules_rust-467a301fd665db344803c1d8a2401ec2bf8c74ce",
urls = [
# Master branch as of 2021-04-23
"https://github.com/bazelbuild/rules_rust/archive/467a301fd665db344803c1d8a2401ec2bf8c74ce.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.
Rules
- defs: standard rust rules for building and testing libraries and binaries.
- rust_doc: rules for generating and testing rust documentation.
- rust_clippy: rules for running clippy.
- rust_fmt: rules for running rustfmt.
- rust_proto: rules for generating protobuf. and gRPC stubs.
- rust_bindgen: rules for generating C++ bindings.
- rust_wasm_bindgen: rules for generating WebAssembly bindings.
- cargo_build_script: a rule to run
build.rs
script from Bazel.
You can also browse the full API in one page.
Experimental rules
- crate_universe: A repository rule for fetching dependencies from a crate registry.
- rust_analyzer: rules for generating
rust-project.json
files for rust-analyzer
Specifying Rust version
To build with a particular version of the Rust compiler, pass that version to rust_repositories
:
rust_repositories(version = "1.52.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.52.0")
External Dependencies
Currently, the most common approach to managing external dependencies is using
cargo-raze to generate BUILD
files for Cargo crates.