Rust rules
The Rust family builds a Cargo workspace from Cargo.lock. You normally use it through the command line, which writes the BUCK files; the macros are documented here for when you read them.
cargo_workspace #
cargo_workspace(path = ".", table)Called once, in the package of the workspace root (the repository root, in v0). Through //:buildrules.bzl, table is already bound, and the root BUCK that sync writes calls it for you. It declares:
:check, the validation that comparesCargo.lock,rust-toolchain.tomland eachCargo.tomlwith the table;- exports of
Cargo.toml,Cargo.lock,rust-toolchain.tomland, if present,clippy.toml; :rustfmt_config, the workspace root's.rustfmt.tomlorrustfmt.toml(an empty file, rustfmt's defaults, when there is none);- one aggregate target per step (
:fmt,:lint,:typecheck,:debug,:test,:release), each depending on the same-named target of every member; - one aggregate target per action named in the policy;
:rust_analyzer_inputs, what rust-analyzer needs that only a build makes (registry sources, build-script output, proc-macros as shared objects, the pinned sysroot, standard library sources and rust-analyzer), as one directory with anindex.json;buildrules rust-project developbuilds it and writesrust-project.jsonfrom it (CLI). It is refused by name for a Rust version whose row inrules/rust/pins.bzldoes not pinrust_srcandrust_analyzer.
cargo_package #
cargo_package(path = ".", table, third_party, check = None, extra_srcs = [], refuse = fail, stamp_env = {}, stamp_rustc_env = {}, coverage_branches = True)Called once per workspace member, in the member's own package. Through //:buildrules.bzl the call is cargo_package(path = ".").
| Argument | Meaning |
|---|---|
path | the member directory, relative to the BUCK file (. in the member's own package) |
table | the generated table as a dict (bound by //:buildrules.bzl) |
third_party | the package that holds the registry crates (bound to //third_party/buildrules) |
check | the label of the workspace :check; the default is //:check |
extra_srcs | files or directories that the sources or the build script read, such as with #[path] or include_str!, and that a unit does not see by itself: outside the package, exported with export_file(mode = "reference"), or in the package outside the unit's own files (a library sees src/ and build.rs, an integration test tests/), by its path ("tests/fixtures"). They become inputs of every unit of the package and of its test runs. Each must be the file of the tree: a copy made by a rule is refused |
refuse | what a refused table calls with the reason; the default, fail, stops the load. Only tests of the refusal pass anything else (rules/rust/refusal.bzl) |
stamp_env | a variable name and the label of a text file (stamp_file): the runs of the package's build script get the variable set to the file's text. A commit given this way replaces asking git, which a build script cannot run here |
stamp_rustc_env | a rustc-env name and the label of a text file: the value the build script prints for that name is replaced by the file's text |
coverage_branches | build :coverage with branch counters (the default); False is lines only, for a member whose branch records the pinned llvm-cov cannot read, and its floor is then lines alone |
Targets of a member #
| Target | What it is |
|---|---|
:lib | the library |
:bin | the binary, only when the member has one |
:debug, :release | the member built with the dev or release profile |
:test | runs the member's Cargo test targets; one passing marker per target. Doctests are not run. |
:lint | clippy over the member, with its [lints] as flags and clippy.toml as an input; -D warnings when the policy says so |
:typecheck | the pinned rustc over the units :lint checks, to metadata only, as cargo check --all-targets: no clippy and no -D warnings, so a type or borrow error is red and a clippy warning is not; the member's [lints.rust] levels apply, as under cargo check |
:fmt | the pinned rustfmt --check over the files cargo fmt --check -p reads (every target root, and the modules they reach), with the member's own rustfmt.toml or the workspace's; red on any difference, showing it |
:flycheck | the same clippy runs as diagnostics: one directory of <unit>.json files, rustc's diagnostics as JSON lines, built even when the code has errors. What buildrules rust-project check builds for an editor's check on save. |
:coverage | llvm-cov's line and branch figures of the member's own sources under its own tests, each test built again with coverage counters: coverage.txt, a row per source file (those under tests/ not counted), a total of lines and one of branch arms; [lcov] is llvm-cov's report |
:coverage_floor | only with a coverage_floor.txt beside the member's BUCK file: the figures of :coverage held to it, red when one fell below its floor |
| one per action | each action in [workspace.metadata.buildrules.actions], such as ci = ["lint", "test"] |
:debug, :release, :test, :lint, :typecheck, :fmt, :flycheck and :coverage carry the workspace :check as a validation. The unit targets themselves are named by content id (<crate>-<version>_<hash>) and live in third_party/buildrules and the member packages.
The policy #
Policy goes in [workspace.metadata.buildrules] and, overriding it per key, in [package.metadata.buildrules]. Any other key is refused by sync, naming the key and the file. Profiles, lints, features and dependencies stay in Cargo's own tables.
| Key | Type | Meaning |
|---|---|---|
default-action | string | an action or a step; recorded in the table, and no target is derived from it yet |
actions | table of name to non-empty list of steps | for example ci = ["fmt", "lint", "test"]; a name may not be a step name |
lint.deny-warnings | bool | clippy -D warnings for :lint |
postgres | table { url-env = "NAME" } | every test of the package runs with a private Postgres, started for that run and stopped after it, its URL in NAME; a package's replaces the workspace's |
The steps are fmt, lint, typecheck, debug, test, release and coverage; coverage is the measurement, and the floor is the member's own :coverage_floor.
Refused when the package loads #
- a table whose compiler is not in the version table, or whose hashes differ from the table's;
- a path that is not a member in the table;
- link-time optimization, a codegen backend, rpath, incremental compilation or split debuginfo in a profile.
Hand-written rules #
load("@buildrules//rules/rust:defs.bzl", "rust_library", "rust_binary", "rust_test")For code that has no Cargo manifest, rust_library, rust_binary and rust_test take srcs, crate_root, crate_name, edition, deps and rustc_flags. A library or binary can list gate_tests, the tests it is welded to.
Build scripts and native libraries #
A registry crate's build script runs as an action. For the few that compile C code of their own, a repository file third_party/buildrules/overrides.json, read by sync, replaces the script with a buildrules target. Each entry is keyed by crate name and version and must say why:
drop_build_scriptandcfgs: skip the script and set the cfgs it would have set;native_deps: static archives a binary or test links;patch: copy, substitute or add files in the unpacked crate.
The example examples/crates has a crate whose cc build script compiles zstd; the script runs as an action, with no override. The override keys are exercised by the unit tests of the generator, and no shipped example uses drop_build_script or native_deps. The C libraries that the C/C++ rules can build, such as AWS-LC, are not yet wired to any Rust crate.
:coverage too). Linux x86_64 only.Toolchains #
The version table, RUST_VERSIONS in rules/rust/pins.bzl, maps an exact release to the sha256 and size of the .tar.gz of rustc, rust_std, cargo and clippy, fetched from their versioned URLs (static.rust-lang.org/dist/rustc-1.95.0-x86_64-unknown-linux-gnu.tar.gz, no date in them). Nothing is fetched without a hash, and the toolchain refuses to exist if rustc --version and cargo --version do not start with rustc <version> ( and cargo <version> (.
| Version | Role |
|---|---|
1.95.0 | the default: toolchains//:rust is this version, and a hand-written rust_library uses it |
1.85.0 | the oldest supported (edition 2024 needs 1.85) |
Choosing a version #
rust-toolchain.tomlselects it.channelmust be an exact release in the table.buildrules syncbuilds that version and records it inbuildrules.lock.bzlasTOOLCHAIN.rust_toolchains()declares one toolchain per version:toolchains//:rust_1_85_0,:sysroot_1_85_0,:cargo_1_85_0,:clippy_1_85_0,:rustfmt_1_85_0. The unsuffixed names are aliases of the default. A toolchain is an input of every compile, test and lint action of its units, so two versions never share an action key.- A target platform can override the choice for a whole build, with the
rust_versionconstraint:buildrules//platforms:linux_x86_64_rust_1_85_0. That is how the CI matrix builds the same sources with the oldest compiler.
A version of your own #
A version that is in neither the table nor your own pins is refused, by buildrules sync and by the rules at load. buildrules-pins.toml at the repository root adds a version with the hashes and sizes of the versioned tarballs, which you supply:
[rust."1.86.0"]
[rust."1.86.0".rustc]
sha256 = "<64 lower-case hex>"
size = 122722040
# likewise [rust."1.86.0".rust-std], .cargo and .clippyAll four components are needed, each with a sha256 and a size. A version that the table already pins is refused: the reviewed hashes win. sync copies the file into toolchains/rust_pins.bzl, so the pin is committed twice, and check fails when the two differ. A wrong hash fails the download.
# toolchains/BUCK
load("@buildrules//rules/rust:toolchain.bzl", "rust_toolchains")
rust_toolchains(extra = EXTRA_RUST_VERSIONS)buildrules init writes this file and an empty toolchains/rust_pins.bzl.