xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/index.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1# [Rules rust](https://github.com/bazelbuild/rules_rust)
2
3## Overview
4
5This repository provides rules for building [Rust][rust] projects with [Bazel][bazel].
6
7[bazel]: https://bazel.build/
8[rust]: http://www.rust-lang.org/
9
10<!-- TODO: Render generated docs on the github pages site again, https://bazelbuild.github.io/rules_rust/ -->
11
12<a name="setup"></a>
13
14## Setup
15
16The 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
18### Bzlmod
19
20Note 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
22To use `rules_rust` in a project using bzlmod, add the following to your `MODULE.bazel` file:
23
24```python
25bazel_dep(name = "rules_rust", version = "0.48.0")
26```
27
28Don't forget to substitute in your desired release's version number.
29
30### WORKSPACE
31
32To 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
34```python
35load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
36
37# To find additional information on this release or newer ones visit:
38# https://github.com/bazelbuild/rules_rust/releases
39http_archive(
40    name = "rules_rust",
41    integrity = "sha256-Weev1uz2QztBlDA88JX6A1N72SucD1V8lBsaliM0TTg=",
42    urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.48.0/rules_rust-v0.48.0.tar.gz"],
43)
44
45load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
46
47rules_rust_dependencies()
48
49rust_register_toolchains()
50```
51
52Don't forget to substitute in your desired release's version number and integrity hash.
53
54## Rules
55
56- [defs](defs.md): standard rust rules for building and testing libraries and binaries.
57- [rust_doc](rust_doc.md): rules for generating and testing rust documentation.
58- [rust_clippy](rust_clippy.md): rules for running [clippy](https://github.com/rust-lang/rust-clippy#readme).
59- [rust_fmt](rust_fmt.md): rules for running [rustfmt](https://github.com/rust-lang/rustfmt#readme).
60- [rust_proto](rust_proto.md): rules for generating [protobuf](https://developers.google.com/protocol-buffers) and [gRPC](https://grpc.io) stubs.
61- [rust_bindgen](rust_bindgen.md): rules for generating C++ bindings.
62- [rust_wasm_bindgen](rust_wasm_bindgen.md): rules for generating [WebAssembly](https://www.rust-lang.org/what/wasm) bindings.
63- [cargo](cargo.md): Rules dedicated to Cargo compatibility. ie: [`build.rs` scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html).
64- [crate_universe (bzlmod)](crate_universe_bzlmod.md): Rules for generating Bazel targets for external crate dependencies when using bzlmod.
65- [crate_universe (WORKSPACE)](crate_universe.md): Rules for generating Bazel targets for external crate dependencies when using WORKSPACE files.
66
67You can also browse the [full API in one page](flatten.md).
68
69### Experimental rules
70
71- [rust_analyzer](rust_analyzer.md): rules for generating `rust-project.json` files for [rust-analyzer](https://rust-analyzer.github.io/)
72
73## Specifying Rust version
74
75To build with a particular version of the Rust compiler, pass that version to [`rust_register_toolchains`](flatten.md#rust_register_toolchains):
76
77```python
78rust_register_toolchains(
79    edition = "2021",
80    versions = [
81        "1.79.0"
82    ],
83)
84```
85
86As well as an exact version, `versions` can accept `nightly/{iso_date}` and `beta/{iso_date}` strings for toolchains from different release channels.
87
88```python
89rust_register_toolchains(
90    edition = "2021",
91    versions = [
92        "nightly/2024-06-13",
93    ],
94)
95```
96
97By default, a `stable` and `nightly` toolchain will be registered if no versions are passed to `rust_register_toolchains`. However,
98if only 1 version is passed and it is from the `nightly` or `beta` release channels (i.e. __not__ `stable`), then a build setting must
99also be set in the project's `.bazelrc` file.
100
101```text
102build --@rules_rust//rust/toolchain/channel=nightly
103```
104
105Failure to do so will result in rules attempting to match a `stable` toolchain when one was not registered.
106
107## External Dependencies
108
109[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
111## Supported bazel versions
112
113The 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
115We 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
117## Supported platforms
118
119We aim to support Linux and macOS.
120
121We 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
123Windows 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