xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/index.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan# [Rules rust](https://github.com/bazelbuild/rules_rust)
2*d4726bddSHONG Yifan
3*d4726bddSHONG Yifan## Overview
4*d4726bddSHONG Yifan
5*d4726bddSHONG YifanThis repository provides rules for building [Rust][rust] projects with [Bazel][bazel].
6*d4726bddSHONG Yifan
7*d4726bddSHONG Yifan[bazel]: https://bazel.build/
8*d4726bddSHONG Yifan[rust]: http://www.rust-lang.org/
9*d4726bddSHONG Yifan
10*d4726bddSHONG Yifan<!-- TODO: Render generated docs on the github pages site again, https://bazelbuild.github.io/rules_rust/ -->
11*d4726bddSHONG Yifan
12*d4726bddSHONG Yifan<a name="setup"></a>
13*d4726bddSHONG Yifan
14*d4726bddSHONG Yifan## Setup
15*d4726bddSHONG Yifan
16*d4726bddSHONG YifanThe rules are released, and releases can be found on [the GitHub Releases page](https://github.com/bazelbuild/rules_rust/releases). We recommend using the latest release from that page.
17*d4726bddSHONG Yifan
18*d4726bddSHONG Yifan### Bzlmod
19*d4726bddSHONG Yifan
20*d4726bddSHONG YifanNote that rules_rust bzlmod support is still a work in progress. Most features should work, but bugs are more likely. This is not a desired end-state - please report (or better yet, help fix!) bugs you run into.
21*d4726bddSHONG Yifan
22*d4726bddSHONG YifanTo use `rules_rust` in a project using bzlmod, add the following to your `MODULE.bazel` file:
23*d4726bddSHONG Yifan
24*d4726bddSHONG Yifan```python
25*d4726bddSHONG Yifanbazel_dep(name = "rules_rust", version = "0.48.0")
26*d4726bddSHONG Yifan```
27*d4726bddSHONG Yifan
28*d4726bddSHONG YifanDon't forget to substitute in your desired release's version number.
29*d4726bddSHONG Yifan
30*d4726bddSHONG Yifan### WORKSPACE
31*d4726bddSHONG Yifan
32*d4726bddSHONG YifanTo use `rules_rust` in a project using a WORKSPACE file, add the following to your `WORKSPACE` file to add the external repositories for the Rust toolchain:
33*d4726bddSHONG Yifan
34*d4726bddSHONG Yifan```python
35*d4726bddSHONG Yifanload("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
36*d4726bddSHONG Yifan
37*d4726bddSHONG Yifan# To find additional information on this release or newer ones visit:
38*d4726bddSHONG Yifan# https://github.com/bazelbuild/rules_rust/releases
39*d4726bddSHONG Yifanhttp_archive(
40*d4726bddSHONG Yifan    name = "rules_rust",
41*d4726bddSHONG Yifan    integrity = "sha256-Weev1uz2QztBlDA88JX6A1N72SucD1V8lBsaliM0TTg=",
42*d4726bddSHONG Yifan    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.48.0/rules_rust-v0.48.0.tar.gz"],
43*d4726bddSHONG Yifan)
44*d4726bddSHONG Yifan
45*d4726bddSHONG Yifanload("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
46*d4726bddSHONG Yifan
47*d4726bddSHONG Yifanrules_rust_dependencies()
48*d4726bddSHONG Yifan
49*d4726bddSHONG Yifanrust_register_toolchains()
50*d4726bddSHONG Yifan```
51*d4726bddSHONG Yifan
52*d4726bddSHONG YifanDon't forget to substitute in your desired release's version number and integrity hash.
53*d4726bddSHONG Yifan
54*d4726bddSHONG Yifan## Rules
55*d4726bddSHONG Yifan
56*d4726bddSHONG Yifan- [defs](defs.md): standard rust rules for building and testing libraries and binaries.
57*d4726bddSHONG Yifan- [rust_doc](rust_doc.md): rules for generating and testing rust documentation.
58*d4726bddSHONG Yifan- [rust_clippy](rust_clippy.md): rules for running [clippy](https://github.com/rust-lang/rust-clippy#readme).
59*d4726bddSHONG Yifan- [rust_fmt](rust_fmt.md): rules for running [rustfmt](https://github.com/rust-lang/rustfmt#readme).
60*d4726bddSHONG Yifan- [rust_proto](rust_proto.md): rules for generating [protobuf](https://developers.google.com/protocol-buffers) and [gRPC](https://grpc.io) stubs.
61*d4726bddSHONG Yifan- [rust_bindgen](rust_bindgen.md): rules for generating C++ bindings.
62*d4726bddSHONG Yifan- [rust_wasm_bindgen](rust_wasm_bindgen.md): rules for generating [WebAssembly](https://www.rust-lang.org/what/wasm) bindings.
63*d4726bddSHONG Yifan- [cargo](cargo.md): Rules dedicated to Cargo compatibility. ie: [`build.rs` scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
64*d4726bddSHONG Yifan- [crate_universe (bzlmod)](crate_universe_bzlmod.md): Rules for generating Bazel targets for external crate dependencies when using bzlmod.
65*d4726bddSHONG Yifan- [crate_universe (WORKSPACE)](crate_universe.md): Rules for generating Bazel targets for external crate dependencies when using WORKSPACE files.
66*d4726bddSHONG Yifan
67*d4726bddSHONG YifanYou can also browse the [full API in one page](flatten.md).
68*d4726bddSHONG Yifan
69*d4726bddSHONG Yifan### Experimental rules
70*d4726bddSHONG Yifan
71*d4726bddSHONG Yifan- [rust_analyzer](rust_analyzer.md): rules for generating `rust-project.json` files for [rust-analyzer](https://rust-analyzer.github.io/)
72*d4726bddSHONG Yifan
73*d4726bddSHONG Yifan## Specifying Rust version
74*d4726bddSHONG Yifan
75*d4726bddSHONG YifanTo build with a particular version of the Rust compiler, pass that version to [`rust_register_toolchains`](flatten.md#rust_register_toolchains):
76*d4726bddSHONG Yifan
77*d4726bddSHONG Yifan```python
78*d4726bddSHONG Yifanrust_register_toolchains(
79*d4726bddSHONG Yifan    edition = "2021",
80*d4726bddSHONG Yifan    versions = [
81*d4726bddSHONG Yifan        "1.79.0"
82*d4726bddSHONG Yifan    ],
83*d4726bddSHONG Yifan)
84*d4726bddSHONG Yifan```
85*d4726bddSHONG Yifan
86*d4726bddSHONG YifanAs well as an exact version, `versions` can accept `nightly/{iso_date}` and `beta/{iso_date}` strings for toolchains from different release channels.
87*d4726bddSHONG Yifan
88*d4726bddSHONG Yifan```python
89*d4726bddSHONG Yifanrust_register_toolchains(
90*d4726bddSHONG Yifan    edition = "2021",
91*d4726bddSHONG Yifan    versions = [
92*d4726bddSHONG Yifan        "nightly/2024-06-13",
93*d4726bddSHONG Yifan    ],
94*d4726bddSHONG Yifan)
95*d4726bddSHONG Yifan```
96*d4726bddSHONG Yifan
97*d4726bddSHONG YifanBy default, a `stable` and `nightly` toolchain will be registered if no versions are passed to `rust_register_toolchains`. However,
98*d4726bddSHONG Yifanif only 1 version is passed and it is from the `nightly` or `beta` release channels (i.e. __not__ `stable`), then a build setting must
99*d4726bddSHONG Yifanalso be set in the project's `.bazelrc` file.
100*d4726bddSHONG Yifan
101*d4726bddSHONG Yifan```text
102*d4726bddSHONG Yifanbuild --@rules_rust//rust/toolchain/channel=nightly
103*d4726bddSHONG Yifan```
104*d4726bddSHONG Yifan
105*d4726bddSHONG YifanFailure to do so will result in rules attempting to match a `stable` toolchain when one was not registered.
106*d4726bddSHONG Yifan
107*d4726bddSHONG Yifan## External Dependencies
108*d4726bddSHONG Yifan
109*d4726bddSHONG Yifan[crate_universe](crate_universe.md) ([crate_universe bzlmod](crate_universe_bzlmod.md)) is a tool built into `rules_rust` that can be used to fetch dependencies.
110*d4726bddSHONG Yifan
111*d4726bddSHONG Yifan## Supported bazel versions
112*d4726bddSHONG Yifan
113*d4726bddSHONG YifanThe oldest version of Bazel the `main` branch is tested against is `6.3.0`. Previous versions may still be functional in certain environments, but this is the minimum version we strive to fully support.
114*d4726bddSHONG Yifan
115*d4726bddSHONG YifanWe test these rules against the latest rolling releases of Bazel, and aim for compatibility with them, but prioritise stable releases over rolling releases where necessary.
116*d4726bddSHONG Yifan
117*d4726bddSHONG Yifan## Supported platforms
118*d4726bddSHONG Yifan
119*d4726bddSHONG YifanWe aim to support Linux and macOS.
120*d4726bddSHONG Yifan
121*d4726bddSHONG YifanWe do not have sufficient maintainer expertise to support Windows. Most things probably work, but we have had to disable many tests in CI because we lack the expertise to fix them. We welcome contributions to help improve its support.
122*d4726bddSHONG Yifan
123*d4726bddSHONG YifanWindows support for some features requires `--enable_runfiles` to be passed to Bazel, we recommend putting it in your bazelrc. See [Using Bazel on Windows](https://bazel.build/configure/windows) for more Windows-specific recommendations.
124