Rust Repositories

rust_toolchain

Declares a Rust toolchain for use.

This is for declaring a custom toolchain, eg. for configuring a particular version of rust or supporting a new platform.

Example:

Suppose the core rust team has ported the compiler to a new target CPU, called cpuX. This support can be used in Bazel by defining a new toolchain definition and declaration:

load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain')

rust_toolchain(
    name = "rust_cpuX_impl",
    rustc = "@rust_cpuX//:rustc",
    rustc_lib = "@rust_cpuX//:rustc_lib",
    rust_lib = "@rust_cpuX//:rust_lib",
    rust_doc = "@rust_cpuX//:rustdoc",
    binary_ext = "",
    staticlib_ext = ".a",
    dylib_ext = ".so",
    stdlib_linkflags = ["-lpthread", "-ldl"],
    os = "linux",
)

toolchain(
    name = "rust_cpuX",
    exec_compatible_with = [
        "@platforms//cpu:cpuX",
    ],
    target_compatible_with = [
        "@platforms//cpu:cpuX",
    ],
    toolchain = ":rust_cpuX_impl",
)

Then, either add the label of the toolchain rule to register_toolchains in the WORKSPACE, or pass it to the "--extra_toolchains" flag for Bazel, and it will be used.

See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository with the actual binaries and libraries.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
allocator_libraryTarget that provides allocator functions when rust_library targets are embedded in a cc_binary.LabeloptionalNone
binary_extThe extension for binaries created from rustc.Stringrequired
cargoThe location of the cargo binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
clippy_driverThe location of the clippy-driver binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
debug_infoRustc debug info levels per opt levelDictionary: String -> Stringoptional{“dbg”: “2”, “fastbuild”: “0”, “opt”: “0”}
default_editionThe edition to use for rust_* rules that don't specify an edition.Stringoptional“2015”
dylib_extThe extension for dynamic libraries created from rustc.Stringrequired
exec_tripleThe platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurationsStringoptional""
opt_levelRustc optimization levels.Dictionary: String -> Stringoptional{“dbg”: “0”, “fastbuild”: “0”, “opt”: “3”}
osThe operating system for the current toolchainStringrequired
rust_docThe location of the rustdoc binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
rust_libThe rust standard library.LabeloptionalNone
rustcThe location of the rustc binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
rustc_libThe libraries used by rustc during compilation.LabeloptionalNone
rustc_srcThe source code of rustc.LabeloptionalNone
rustfmtThe location of the rustfmt binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
staticlib_extThe extension for static libraries created from rustc.Stringrequired
stdlib_linkflagsAdditional linker libs used when std lib is linked, see https://github.com/rust-lang/rust/blob/master/src/libstd/build.rsList of stringsrequired
target_tripleThe platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurationsStringoptional""

rust_toolchain_repository

Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms.

A given instance of this rule should be accompanied by a rust_toolchain_repository_proxy invocation to declare its toolchains to Bazel; the indirection allows separating toolchain selection from toolchain fetching.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
dev_componentsWhether to download the rustc-dev components (defaults to False). Requires version to be “nightly”.BooleanoptionalFalse
editionThe rust edition to be used by default.Stringoptional“2015”
exec_tripleThe Rust-style target that this compiler runs onStringrequired
extra_target_triplesAdditional rust-style targets that this set of toolchains should support.List of stringsoptional[]
include_rustc_srcWhether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer.BooleanoptionalFalse
iso_dateThe date of the tool (or None, if the version is a specific version).Stringoptional""
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry “@foo”: “@bar” declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).Dictionary: String -> Stringrequired
rustfmt_versionThe version of the tool among “nightly”, “beta”, or an exact version.Stringoptional""
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_repositories for more details.Dictionary: String -> Stringoptional{}
toolchain_name_prefixThe per-target prefix expected for the rust_toolchain declarations in the parent workspace.Stringoptional""
urlsA list of mirror urls containing the tools from the Rust-lang static file server. These must contain the ‘{}’ used to substitute the tool being fetched (using .format).List of stringsoptional[“https://static.rust-lang.org/dist/{}.tar.gz”]
versionThe version of the tool among “nightly”, “beta”, or an exact version.Stringrequired

rust_toolchain_repository_proxy

Generates a toolchain-bearing repository that declares the toolchains from some other rust_toolchain_repository.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this repository.Namerequired
exec_tripleThe Rust-style target triple for the compilation platformStringrequired
extra_target_triplesThe Rust-style triples for extra compilation targetsList of stringsoptional[]
parent_workspace_nameThe name of the other rust_toolchain_repositoryStringrequired
repo_mappingA dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry “@foo”: “@bar” declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).Dictionary: String -> Stringrequired
toolchain_name_prefixThe per-target prefix expected for the rust_toolchain declarations in the parent workspace.Stringoptional""

rust_repositories

Emits a default set of toolchains for Linux, MacOS, and Freebsd

Skip this macro and call the rust_repository_set macros directly if you need a compiler for other hosts or for additional target triples.

The sha256 attribute represents a dict associating tool subdirectories to sha256 hashes. As an example:

{
    "rust-1.46.0-x86_64-unknown-linux-gnu": "e3b98bc3440fe92817881933f9564389eccb396f5f431f33d48b979fa2fbdcf5",
    "rustfmt-1.4.12-x86_64-unknown-linux-gnu": "1894e76913303d66bf40885a601462844eec15fca9e76a6d13c390d7000d64b0",
    "rust-std-1.46.0-x86_64-unknown-linux-gnu": "ac04aef80423f612c0079829b504902de27a6997214eb58ab0765d02f7ec1dbc",
}

This would match for exec_triple = "x86_64-unknown-linux-gnu". If not specified, rules_rust pulls from a non-exhaustive list of known checksums..

See load_arbitrary_tool in @rules_rust//rust:repositories.bzl for more details.

PARAMETERS

NameDescriptionDefault Value
versionThe version of Rust. Either “nightly”, “beta”, or an exact version. Defaults to a modern version.“1.52.1”
iso_dateThe date of the nightly or beta release (or None, if the version is a specific version).None
rustfmt_versionThe version of rustfmt. Either “nightly”, “beta”, or an exact version. Defaults to version if not specified.None
editionThe rust edition to be used by default (2015 (default) or 2018)None
dev_componentsWhether to download the rustc-dev components (defaults to False). Requires version to be “nightly”.False
sha256sA dict associating tool subdirectories to sha256 hashes. Defaults to None.None
include_rustc_srcWhether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.False
urlsA list of mirror urls containing the tools from the Rust-lang static file server. These must contain the ‘{}’ used to substitute the tool being fetched (using .format). Defaults to [‘https://static.rust-lang.org/dist/{}.tar.gz’][“https://static.rust-lang.org/dist/{}.tar.gz”]

rust_repository_set

Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains.

N.B. A “proxy repository” is needed to allow for registering the toolchain (with constraints) without actually downloading the toolchain.

PARAMETERS

NameDescriptionDefault Value
nameThe name of the generated repositorynone
versionThe version of the tool among “nightly”, "beta', or an exact version.none
exec_tripleThe Rust-style target that this compiler runs onnone
include_rustc_srcWhether to download rustc's src code. This is required in order to use rust-analyzer support. Defaults to False.False
extra_target_triplesAdditional rust-style targets that this set of toolchains should support. Defaults to [].[]
iso_dateThe date of the tool. Defaults to None.None
rustfmt_versionThe version of rustfmt to be associated with the toolchain. Defaults to None.None
editionThe rust edition to be used by default (2015 (if None) or 2018).None
dev_componentsWhether to download the rustc-dev components. Requires version to be “nightly”. Defaults to False.False
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_repositories for more details.None
urlsA list of mirror urls containing the tools from the Rust-lang static file server. These must contain the ‘{}’ used to substitute the tool being fetched (using .format). Defaults to [‘https://static.rust-lang.org/dist/{}.tar.gz’][“https://static.rust-lang.org/dist/{}.tar.gz”]