rust_toolchain.bzl

rust_toolchain

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

rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, channel, clippy_driver,
               debug_info, default_edition, dlltool, dylib_ext, env, exec_triple,
               experimental_use_allocator_libraries_with_mangled_symbols,
               experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags,
               extra_rustc_flags_for_crate_types, global_allocator_library, iso_date, link_std_dylib,
               linker, linker_preference, linker_type, llvm_cov, llvm_lib, llvm_profdata, llvm_tools,
               lto, opt_level, per_crate_rustc_flags, require_explicit_unstable_features, rust_doc,
               rust_objcopy, rust_std, rustc, rustc_lib, rustfmt, staticlib_ext, stdlib_linkflags,
               strip_level, target_json, target_triple, version)

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.

To use a platform-specific linker, you can use a select() in the linker attribute:

rust_toolchain(
    name = "rust_toolchain_impl",
    # ... other attributes ...
    linker = select({
        "@platforms//os:linux": "//tools:rust-lld-linux",
        "@platforms//os:windows": "//tools:rust-lld-windows",
        "//conditions:default": "//tools:rust-lld",
    }),
)

The select() is evaluated against the target platform before the exec transition is applied, allowing platform-specific linker selection while ensuring the selected linker is built for the exec platform.

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//rust/settings:default_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. Deprecated: register a cargo_toolchain for @rules_rust//cargo:toolchain_type instead. Rules which run Cargo use that toolchain when one is registered and fall back to this attribute when none is.LabeloptionalNone
cargo_clippyThe location of the cargo_clippy binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
channelThe Rust release channel (stable, nightly, or beta).Stringoptional""
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 compilation mode (keyed by dbg, fastbuild, opt).Dictionary: 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""
dlltoolThe dlltool binary rustc uses to generate import libraries. If unset, it is derived from the cc_toolchain's linker path, which assumes a MinGW binutils layout.LabeloptionalNone
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_use_allocator_libraries_with_mangled_symbolsWhether to use rust-based allocator libraries with mangled symbols. Possible values: [-1, 0, 1]. -1 means to use the value of the build setting //rust/settings:experimental_use_allocator_libraries_with_mangled_symbols. 0 means do not use. In that case, rules_rust will try to use the c-based allocator libraries that don't support symbol mangling. 1 means use the rust-based allocator libraries.Integeroptional-1
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 configuration. Subject to location expansion with respect to the srcs of the rust_std attribute. Subject to Make variable expansion with respect to RUST_SYSROOT, RUST_SYSROOT_SHORT, RUSTC, etc.List 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. Subject to Make variable expansion with respect to RUST_SYSROOT, RUST_SYSROOT_SHORT, RUSTC, etc.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//rust/private/cc:global_allocator_library"
iso_dateThe ISO date of the nightly or beta release (e.g. 2026-03-26). Empty for stable releases.Stringoptional""
link_std_dylibWhether to link libstd dynamically. Possible values: [-1, 0, 1]. -1 means to use the value of the build setting @rules_rust//rust/settings:experimental_link_std_dylib. 0 means do not link libstd dynamically. 1 means link libstd dynamically.Integeroptional-1
linkerThe label to an explicit linker to use (e.g. rust-lld, ld, link-ld.exe, etc.). Linker binaries must be runnable in the exec configuration, so cfg = "exec" is used. To choose a linker based on the target platform, use a select() when providing this attribute. The select() will be evaluated against the target platform before the exec transition is applied, allowing platform-specific linker selection while ensuring the selected linker is built for the exec platform.LabeloptionalNone
linker_preferenceThe preferred linker to use. If unspecified, cc is preferred and rust is used as a fallback whenever linker is provided.Stringoptional""
linker_typeThe type of linker invocation: 'direct' (ld, rust-lld) or 'indirect' (via compiler like clang/gcc). If unset, defaults based on linker_preference.Stringoptional""
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_libThe location of the libLLVM shared object files. If llvm_cov is None, this can be None as well and 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
ltoA rust_lto_flag target that sets the link time optimization mode for crates built with this toolchain.Labeloptional"@rules_rust//rust/settings:lto"
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[]
require_explicit_unstable_featuresLabel to a boolean build setting that controls whether all uses of unstable Rust features must be explicitly opted in to using -Zallow-features=....Labeloptional"@rules_rust//rust/settings:require_explicit_unstable_features"
rust_docThe location of the rustdoc binary. Can be a direct source or a filegroup containing one item.Labelrequired
rust_objcopyThe location of the rust-objcopy binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
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. Subject to Make variable expansion with respect to RUST_SYSROOT, RUST_SYSROOT_SHORT, RUSTC, etc.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""
versionThe version of the Rust compiler (e.g. 1.94.1).Stringoptional""