cargo_toolchain.bzl

cargo_toolchain

load("@rules_rust//cargo:cargo_toolchain.bzl", "cargo_toolchain")

cargo_toolchain(name, cargo, channel, iso_date, version)

A toolchain providing a Cargo binary.

Rules which run Cargo on the execution platform, such as crates_vendor and cargo_build_script, resolve @rules_rust//cargo:toolchain_type and use the binary it provides. The toolchain carries Cargo alone, so compile actions, which never run Cargo, do not depend on it.

Cargo is released together with rustc and is only tested against the compiler it shipped with, so the rust module extension declares a cargo_toolchain in the same repository as every rust_toolchain it registers and registers the two with the same constraints and channel setting. The toolchain carries the same version, iso_date and channel identifiers as rust_toolchain; cargo_build_script fails when the Cargo and Rust toolchains it resolves state different ones.

$(CARGO) and $(CARGO_RLOCATIONPATH) are available as Make variables to targets which list @rules_rust//cargo:current_cargo_toolchain, a cargo_toolchain_alias, in toolchains.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
cargoThe cargo binary. Can be a direct source or a filegroup containing one item.Labelrequired
channelThe Rust release channel Cargo comes from (stable, nightly, or beta), as in rust_toolchain.channel.Stringoptional""
iso_dateThe ISO date of the nightly or beta release Cargo comes from, as in rust_toolchain.iso_date. Empty for stable releases.Stringoptional""
versionThe version of the Rust release Cargo comes from (e.g. 1.94.1), as in rust_toolchain.version. Rules which run Cargo next to rustc compare the two; leave empty to opt out.Stringoptional""

cargo_toolchain_alias

load("@rules_rust//cargo:cargo_toolchain.bzl", "cargo_toolchain_alias")

cargo_toolchain_alias(name, mandatory)

A target standing in for the cargo_toolchain resolved for the current platform.

The alias forwards the toolchain's providers, its Make variables ($(CARGO), $(CARGO_RLOCATIONPATH)) and the cargo binary. @rules_rust//cargo:current_cargo_toolchain is the alias rules_rust declares. While the deprecated cargo attribute of rust_toolchain exists, the alias stands in for it when no cargo_toolchain is registered.

This follows cc_toolchain_alias from rules_cc.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
mandatoryWhether to fail when no toolchain is registered. When False, the target provides nothing instead.BooleanoptionalTrue

find_cargo

load("@rules_rust//cargo:cargo_toolchain.bzl", "find_cargo")

find_cargo(ctx, rust_toolchain)

The Cargo binary a rule should run, and the files it needs at run time.

The registered cargo_toolchain is preferred. When none is registered the cargo of the rust_toolchain is used, which is how rules found Cargo before @rules_rust//cargo:toolchain_type existed.

PARAMETERS

NameDescriptionDefault Value
ctxThe rule context. The rule must list config_common.toolchain_type(rust_common.cargo_toolchain_type, mandatory = False) in toolchains.none
rust_toolchainThe resolved rust_toolchain, or None.none

RETURNS

struct: cargo, the binary (a File, or None when neither toolchain provides one), files, a depset to add to the action's tools, and toolchain, the cargo_toolchain used, or None when falling back.