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 #

signature
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:

cargo_package #

signature
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 = ".").

ArgumentMeaning
paththe member directory, relative to the BUCK file (. in the member's own package)
tablethe generated table as a dict (bound by //:buildrules.bzl)
third_partythe package that holds the registry crates (bound to //third_party/buildrules)
checkthe label of the workspace :check; the default is //:check
extra_srcsfiles 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
refusewhat 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_enva 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_enva 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_branchesbuild :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 #

TargetWhat it is
:libthe library
:binthe binary, only when the member has one
:debug, :releasethe member built with the dev or release profile
:testruns the member's Cargo test targets; one passing marker per target. Doctests are not run.
:lintclippy over the member, with its [lints] as flags and clippy.toml as an input; -D warnings when the policy says so
:typecheckthe 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
:fmtthe 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
:flycheckthe 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.
:coveragellvm-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_flooronly 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 actioneach 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.

KeyTypeMeaning
default-actionstringan action or a step; recorded in the table, and no target is derived from it yet
actionstable of name to non-empty list of stepsfor example ci = ["fmt", "lint", "test"]; a name may not be a step name
lint.deny-warningsboolclippy -D warnings for :lint
postgrestable { 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 #

Hand-written rules #

shell
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:

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.

Limit. crates.io, and git repositories on github.com pinned by commit; alternate registries, branches and tags are refused. Doctests are out (of :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> (.

VersionRole
1.95.0the default: toolchains//:rust is this version, and a hand-written rust_library uses it
1.85.0the oldest supported (edition 2024 needs 1.85)

Choosing a version #

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:

buildrules-pins.toml
[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 .clippy

All 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
# 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.