Rust Toolchains

Toolchain rules for Rust.

Rules

rust_analyzer_toolchain

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

rust_analyzer_toolchain(name, proc_macro_srv, rustc, rustc_srcs)

A toolchain for rust-analyzer.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
proc_macro_srvThe path to a rust_analyzer_proc_macro_srv binary.LabeloptionalNone
rustcThe path to a rustc binary.Labelrequired
rustc_srcsThe source code of rustc.Labelrequired

rust_toolchain

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

rust_toolchain(name, allocator_library, binary_ext, cargo, cargo_clippy, clippy_driver, debug_info,
               default_edition, dylib_ext, env, exec_triple, experimental_link_std_dylib,
               experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags,
               extra_rustc_flags_for_crate_types, global_allocator_library, llvm_cov, llvm_profdata,
               llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, rustc_lib,
               rustfmt, staticlib_ext, stdlib_linkflags, strip_level, target_json, target_triple)

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""

rustfmt_toolchain

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

rustfmt_toolchain(name, rustc, rustc_lib, rustfmt)

A toolchain for rustfmt

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
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
rustfmtThe location of the rustfmt binary. Can be a direct source or a filegroup containing one item.Labelrequired