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, rust_analyzer, rustc, rustc_srcs, rustc_srcs_path)
A toolchain for rust-analyzer.
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| proc_macro_srv | The path to a rust_analyzer_proc_macro_srv binary. | Label | optional | None |
| rust_analyzer | The path to a rust-analyzer binary. | Label | optional | None |
| rustc | The path to a rustc binary. | Label | required | |
| rustc_srcs | The source code of rustc. | Label | required | |
| rustc_srcs_path | The direct path to rustc srcs relative to rustc_srcs package root. | String | optional | "library" |
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_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, llvm_cov, llvm_lib,
llvm_profdata, llvm_tools, lto, 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
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | Label | optional | "@rules_rust//ffi/cc/allocator_library" |
| binary_ext | The extension for binaries created from rustc. | String | required | |
| cargo | The location of the cargo binary. Can be a direct source or a filegroup containing one item. | Label | optional | None |
| cargo_clippy | The location of the cargo_clippy binary. Can be a direct source or a filegroup containing one item. | Label | optional | None |
| clippy_driver | The location of the clippy-driver binary. Can be a direct source or a filegroup containing one item. | Label | optional | None |
| debug_info | Rustc debug info levels per opt level | Dictionary: String -> String | optional | {"dbg": "2", "fastbuild": "0", "opt": "0"} |
| default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its edition attribute. | String | optional | "" |
| dylib_ext | The extension for dynamic libraries created from rustc. | String | required | |
| env | Environment variables to set in actions. | Dictionary: String -> String | optional | {} |
| exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | |
| experimental_link_std_dylib | Label to a boolean build setting that controls whether whether to link libstd dynamically. | Label | optional | "@rules_rust//rust/settings:experimental_link_std_dylib" |
| experimental_use_allocator_libraries_with_mangled_symbols | Whether 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. | Integer | optional | -1 |
| experimental_use_cc_common_link | Label to a boolean build setting that controls whether cc_common.link is used to link rust binaries. | Label | optional | "@rules_rust//rust/settings:experimental_use_cc_common_link" |
| extra_exec_rustc_flags | Extra 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 strings | optional | [] |
| extra_rustc_flags | Extra 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 strings | optional | [] |
| extra_rustc_flags_for_crate_types | Extra flags to pass to rustc based on crate type | Dictionary: String -> List of strings | optional | {} |
| global_allocator_library | Target that provides allocator functions for when a global allocator is present. | Label | optional | "@rules_rust//ffi/cc/global_allocator_library" |
| llvm_cov | The 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. | Label | optional | None |
| llvm_lib | The 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. | Label | optional | None |
| llvm_profdata | The 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. | Label | optional | None |
| llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | None |
| lto | Label to an LTO setting whether which can enable custom LTO settings | Label | optional | "@rules_rust//rust/settings:lto" |
| opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | {"dbg": "0", "fastbuild": "0", "opt": "3"} |
| per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] |
| rust_doc | The location of the rustdoc binary. Can be a direct source or a filegroup containing one item. | Label | required | |
| rust_std | The Rust standard library. | Label | required | |
| rustc | The location of the rustc binary. Can be a direct source or a filegroup containing one item. | Label | required | |
| rustc_lib | The libraries used by rustc during compilation. | Label | optional | None |
| rustfmt | Deprecated: Instead see rustfmt_toolchain | Label | optional | None |
| staticlib_ext | The extension for static libraries created from rustc. | String | required | |
| stdlib_linkflags | Additional 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 strings | required | |
| strip_level | Rustc strip levels. For all potential options, see https://doc.rust-lang.org/rustc/codegen-options/index.html#strip | Dictionary: String -> String | optional | {"dbg": "none", "fastbuild": "none", "opt": "debuginfo"} |
| target_json | Override the target_triple with a custom target specification. For more details see: https://doc.rust-lang.org/rustc/targets/custom.html | String | optional | "" |
| target_triple | The platform triple for the toolchains target environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | optional | "" |
rustfmt_toolchain
load("@rules_rust//rust:toolchain.bzl", "rustfmt_toolchain")
rustfmt_toolchain(name, rustc, rustc_lib, rustfmt)
A toolchain for rustfmt
ATTRIBUTES
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | A unique name for this target. | Name | required | |
| rustc | The location of the rustc binary. Can be a direct source or a filegroup containing one item. | Label | optional | None |
| rustc_lib | The libraries used by rustc during compilation. | Label | optional | None |
| rustfmt | The location of the rustfmt binary. Can be a direct source or a filegroup containing one item. | Label | required |