Rust Repositories

rust_stdlib_filegroup

A dedicated filegroup-like rule for Rust stdlib artifacts.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
srcsThe list of targets/files that are components of the rust-stdlib file groupList of labelsrequired

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",
    binary_ext = "",
    dylib_ext = ".so",
    exec_triple = "cpuX-unknown-linux-gnu",
    rust_doc = "@rust_cpuX//:rustdoc",
    rust_std = "@rust_cpuX//:rust_std",
    rustc = "@rust_cpuX//:rustc",
    rustc_lib = "@rust_cpuX//:rustc_lib",
    staticlib_ext = ".a",
    stdlib_linkflags = ["-lpthread", "-ldl"],
    target_triple = "cpuX-unknown-linux-gnu",
)

toolchain(
    name = "rust_cpuX",
    exec_compatible_with = [
        "@platforms//cpu:cpuX",
        "@platforms//os:linux",
    ],
    target_compatible_with = [
        "@platforms//cpu:cpuX",
        "@platforms//os:linux",
    ],
    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.Labeloptional"@rules_rust//ffi/cc/allocator_library"
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
cargo_clippyThe location of the cargo_clippy 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. If absent, every rule is required to specify its edition attribute.Stringoptional""
dylib_extThe extension for dynamic libraries created from rustc.Stringrequired
envEnvironment variables to set in actions.Dictionary: String -> Stringoptional{}
exec_tripleThe platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurationsStringrequired
experimental_link_std_dylibLabel to a boolean build setting that controls whether whether to link libstd dynamically.Labeloptional"@rules_rust//rust/settings:experimental_link_std_dylib"
experimental_use_cc_common_linkLabel to a boolean build setting that controls whether cc_common.link is used to link rust binaries.Labeloptional"@rules_rust//rust/settings:experimental_use_cc_common_link"
extra_exec_rustc_flagsExtra flags to pass to rustc in exec configurationList of stringsoptional[]
extra_rustc_flagsExtra flags to pass to rustc in non-exec configuration. Subject to location expansion with respect to the srcs of the rust_std attribute.List of stringsoptional[]
extra_rustc_flags_for_crate_typesExtra flags to pass to rustc based on crate typeDictionary: String -> List of stringsoptional{}
global_allocator_libraryTarget that provides allocator functions for when a global allocator is present.Labeloptional"@rules_rust//ffi/cc/global_allocator_library"
llvm_covThe location of the llvm-cov binary. Can be a direct source or a filegroup containing one item. If None, rust code is not instrumented for coverage.LabeloptionalNone
llvm_profdataThe location of the llvm-profdata binary. Can be a direct source or a filegroup containing one item. If llvm_cov is None, this can be None as well and rust code is not instrumented for coverage.LabeloptionalNone
llvm_toolsLLVM tools that are shipped with the Rust toolchain.LabeloptionalNone
opt_levelRustc optimization levels.Dictionary: String -> Stringoptional{"dbg": "0", "fastbuild": "0", "opt": "3"}
per_crate_rustc_flagsExtra flags to pass to rustc in non-exec configurationList of stringsoptional[]
rust_docThe location of the rustdoc binary. Can be a direct source or a filegroup containing one item.Labelrequired
rust_stdThe Rust standard library.Labelrequired
rustcThe location of the rustc binary. Can be a direct source or a filegroup containing one item.Labelrequired
rustc_libThe libraries used by rustc during compilation.LabeloptionalNone
rustfmtDeprecated: Instead see rustfmt_toolchainLabeloptionalNone
staticlib_extThe extension for static libraries created from rustc.Stringrequired
stdlib_linkflagsAdditional linker flags to use when Rust standard library is linked by a C++ linker (rustc will deal with these automatically). Subject to location expansion with respect to the srcs of the rust_std attribute.List of stringsrequired
strip_levelRustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#stripDictionary: String -> Stringoptional{"dbg": "none", "fastbuild": "none", "opt": "debuginfo"}
target_jsonOverride the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.htmlStringoptional""
target_tripleThe platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurationsStringoptional""

rules_rust_dependencies

Dependencies used in the implementation of rules_rust.

rust_analyzer_toolchain_repository

Assemble a remote rust_analyzer_toolchain target based on the given params.

PARAMETERS

NameDescriptionDefault Value
nameThe name of the toolchain proxy repository contianing the registerable toolchain.none
versionThe version of the tool among “nightly”, "beta', or an exact version.none
exec_compatible_withA list of constraints for the execution platform for this toolchain.[]
target_compatible_withA list of constraints for the target platform for this toolchain.[]
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_register_toolchains 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.xz’]None
authAuth object compatible with repository_ctx.download to use when downloading files. See repository_ctx.download for more details.None
netrc.netrc file to use for authentication; mirrors the eponymous attribute from http_archiveNone
auth_patternsOverride mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archiveNone

RETURNS

str: The name of a registerable rust_analyzer_toolchain.

rust_register_toolchains

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 sha256s 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
dev_componentsWhether to download the rustc-dev components (defaults to False). Requires version to be “nightly”.False
editionThe rust edition to be used by default (2015, 2018, or 2021). If absent, every target is required to specify its edition attribute.None
allocator_libraryTarget that provides allocator functions when rust_library targets are embedded in a cc_binary.None
global_allocator_libraryTarget that provides allocator functions when global allocator is used with cc_common.link.None
register_toolchainsIf true, repositories will be generated to produce and register rust_toolchain targets.True
rustfmt_versionThe version of rustfmt. If none is supplied and only a single version in versions is given, then this defaults to that version, otherwise will default to the default nightly version.None
rust_analyzer_versionThe version of Rustc to pair with rust-analyzer.None
sha256sA dict associating tool subdirectories to sha256 hashes.None
extra_target_triplesAdditional rust-style targets that rust toolchains should support.["wasm32-unknown-unknown", "wasm32-wasi"]
extra_rustc_flagsDictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.None
extra_exec_rustc_flagsExtra flags to pass to rustc in exec configuration.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).["https://static.rust-lang.org/dist/{}.tar.xz"]
versionsA list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"].["1.80.0", "nightly/2024-07-25"]

rust_repositories

Deprecated: Use rules_rust_dependencies and rust_register_toolchains directly.

PARAMETERS

NameDescriptionDefault Value
kwargsKeyword arguments for the rust_register_toolchains macro.none

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.

PARAMETERS

NameDescriptionDefault Value
nameThe name of the generated repositorynone
versionsA list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"].none
exec_tripleThe Rust-style target that this compiler runs onnone
target_settingsA list of config_settings that must be satisfied by the target configuration in order for this set of toolchains to be selected during toolchain resolution.[]
allocator_libraryTarget that provides allocator functions when rust_library targets are embedded in a cc_binary.None
global_allocator_libraryTarget that provides allocator functions a global allocator is used with cc_common.link.None
extra_target_triplesAdditional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple.{}
rustfmt_versionThe version of rustfmt to be associated with the toolchain.None
editionThe rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute.None
dev_componentsWhether to download the rustc-dev components. Requires version to be “nightly”.False
extra_rustc_flagsDictionary of target triples to list of extra flags to pass to rustc in non-exec configuration.None
extra_exec_rustc_flagsExtra flags to pass to rustc in exec configuration.None
opt_levelDictionary of target triples to optimiztion config.None
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_register_toolchains 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).["https://static.rust-lang.org/dist/{}.tar.xz"]
authAuth object compatible with repository_ctx.download to use when downloading files. See repository_ctx.download for more details.None
netrc.netrc file to use for authentication; mirrors the eponymous attribute from http_archiveNone
auth_patternsOverride mapping of hostnames to authorization patterns; mirrors the eponymous attribute from http_archiveNone
register_toolchainIf True, the generated rust_toolchain target will become a registered toolchain.True
exec_compatible_withA list of constraints for the execution platform for this toolchain.None
default_target_compatible_withA list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform.None

rust_toolchain_repository

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

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 on.none
target_tripleThe Rust-style target to build for.none
exec_compatible_withA list of constraints for the execution platform for this toolchain.None
target_compatible_withA list of constraints for the target platform for this toolchain.None
target_settingsA list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.[]
channelThe channel of the Rust toolchain.None
allocator_libraryTarget that provides allocator functions when rust_library targets are embedded in a cc_binary.None
global_allocator_libraryTarget that provides allocator functions when a global allocator is used with cc_common.link.None
rustfmt_versionThe version of rustfmt to be associated with the toolchain.None
editionThe rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute.None
dev_componentsWhether to download the rustc-dev components. Requires version to be “nightly”. Defaults to False.False
extra_rustc_flagsExtra flags to pass to rustc in non-exec configuration.None
extra_exec_rustc_flagsExtra flags to pass to rustc in exec configuration.None
opt_levelOptimization level config for this toolchain.None
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_register_toolchains 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.xz’]["https://static.rust-lang.org/dist/{}.tar.xz"]
authAuth object compatible with repository_ctx.download to use when downloading files. See repository_ctx.download for more details.None
netrc.netrc file to use for authentication; mirrors the eponymous attribute from http_archiveNone
auth_patternsA list of patterns to match against urls for which the auth object should be used.None

RETURNS

str: The name of the registerable toolchain created by this rule.

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_compatible_withA list of constraints for the execution platform for this toolchain.List of stringsoptional[]
repo_mappingIn WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

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).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> Stringoptional
target_compatible_withA list of constraints for the target platform for this toolchain.List of stringsoptional[]
target_settingsA list of config_settings that must be satisfied by the target configuration in order for this toolchain to be selected during toolchain resolution.List of stringsoptional[]
toolchainThe name of the toolchain implementation target.Stringrequired
toolchain_typeThe toolchain type of the toolchain to declareStringrequired

rust_toolchain_tools_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 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
allocator_libraryTarget that provides allocator functions when rust_library targets are embedded in a cc_binary.Stringoptional"@rules_rust//ffi/cc/allocator_library"
authAuth object compatible with repository_ctx.download to use when downloading files. See repository_ctx.download for more details.Dictionary: String -> Stringoptional{}
auth_patternsA list of patterns to match against urls for which the auth object should be used.List of stringsoptional[]
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 (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute.Stringoptional""
exec_tripleThe Rust-style target that this compiler runs onStringrequired
extra_exec_rustc_flagsExtra flags to pass to rustc in exec configurationList of stringsoptional[]
extra_rustc_flagsExtra flags to pass to rustc in non-exec configurationList of stringsoptional[]
global_allocator_libraryTarget that provides allocator functions when a global allocator is used with cc_common.link.Stringoptional"@rules_rust//ffi/cc/global_allocator_library"
netrc.netrc file to use for authentication; mirrors the eponymous attribute from http_archiveStringoptional""
opt_levelRustc optimization levels. For more details see the documentation for rust_toolchain.opt_level.Dictionary: String -> Stringoptional{}
repo_mappingIn WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

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).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> Stringoptional
rustfmt_versionThe version of the tool among “nightly”, “beta”, or an exact version.Stringoptional""
sha256sA dict associating tool subdirectories to sha256 hashes. See rust_register_toolchains for more details.Dictionary: String -> Stringoptional{}
target_tripleThe Rust-style target that this compiler builds for.Stringrequired
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.xz"]
versionThe version of the tool among “nightly”, “beta”, or an exact version.Stringrequired