@rules_rs//rs:cargo_build_script.bzl
Functions & Macros
cargo_build_scriptCompile and execute a rust build script to generate build attributes
This rules take the same arguments as rust_binary.
Example:
Suppose you have a crate with a cargo build script build.rs:
[workspace]/ hello_lib/ BUILD build.rs src/ lib.rs
Then you want to use the build script in the following:
hello_lib/BUILD:
package(default_visibility = ["//visibility:public"]) load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library") load("@rules_rust//cargo:defs.bzl", "cargo_build_script") # This will run the build script from the root of the workspace, and # collect the outputs. cargo_build_script( name = "build_script", srcs = ["build.rs"], # Optional environment variables passed during build.rs compilation rustc_env = { "CARGO_PKG_VERSION": "0.1.2", }, # Optional environment variables passed during build.rs execution. # Note that as the build script's working directory is not execroot, # execpath/location will return an absolute path, instead of a relative # one. build_script_env = { "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)" }, # Optional data/tool dependencies data = ["@tool//:binary"], ) rust_library( name = "hello_lib", srcs = [ "src/lib.rs", ], deps = [":build_script"], )
The hello_lib target will be build with the flags and the environment variables declared by the build script in addition to the file generated by it.
Parameters
*name | The name for the underlying rule. This should be the name of the package |
edition | The rust edition to use for the internal binary crate. Default: None |
crate_name | Crate name to use for build script. Default: None |
crate_root | The file that will be passed to rustc to be used for building this crate. Default: None |
srcs | Source files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made. Default: [] |
crate_features | A list of features to enable for the build script. Default: [] |
version | The semantic version (semver) of the crate. Default: None |
deps | The build-dependencies of the crate. Default: [] |
link_deps | The subset of the (normal) dependencies of the crate that have the Default: [] |
proc_macro_deps | List of rust_proc_macro targets used to build the script. Default: [] |
build_script_env | Environment variables for build scripts. Default: {} |
build_script_env_files | Files containing additional environment variables to set Default: [] |
use_default_shell_env | Whether or not to include the default shell environment for the build script action. If unset the global Default: None |
data | Files needed by the build script. Default: [] |
compile_data | Files needed for the compilation of the build script. Default: [] |
tools | Tools (executables) needed by the build script. Default: [] |
links | Name of the native library this crate links against. Default: None |
rundir | A directory to The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts. If set to Default: None |
rustc_env | Environment variables to set in rustc when compiling the build script. Default: {} |
rustc_env_files | Files containing additional environment variables to set for rustc Default: [] |
rustc_flags | List of compiler flags passed to Default: [] |
visibility | Visibility to apply to the generated build script output. Default: None |
tags | (list of str, optional): Tags to apply to the generated build script output. Default: None |
aliases | Remap crates to a new name or moniker for linkage to this target. These are other Default: None |
pkg_name | Override the package name used for the build script. This is useful if the build target name gets too long otherwise. Default: None |
kwargs | Forwards to the underlying |
@rules_rs//rs:extensions.bzl
Module Extensions
crateTag Classes
annotationA collection of extra attributes and settings for a particular crate.
| Attribute | Type | Description |
|---|---|---|
*crate | string | The name of the crate the annotation is applied to |
version | string | The version of the crate the annotation is applied to. Defaults to all versions. Default: "*" |
repositories | list of strings | A list of repository names specified from Default: [] |
additive_build_file | label | A file containing extra contents to write to the bottom of generated BUILD files. Default: None |
additive_build_file_content | string | Extra contents to write to the bottom of generated BUILD files. Default: "" |
build_script_data | list of strings | A list of labels to add to a crate's Default: [] |
build_script_data_select | dictionary: String → List of strings | A list of labels to add to a crate's Default: {} |
build_script_env | dictionary: String → String | Additional environment variables to set on a crate's Default: {} |
build_script_env_select | dictionary: String → String | Additional environment variables to set on a crate's Default: {} |
build_script_toolchains | list of labels | A list of labels to set on a crates's Default: [] |
build_script_tags | list of strings | A list of tags to add to a crate's Default: [] |
build_script_tools | list of strings | A list of labels to add to a crate's Default: [] |
build_script_tools_select | dictionary: String → List of strings | A list of labels to add to a crate's Default: {} |
crate_features | list of strings | A list of strings to add to a crate's Default: [] |
crate_features_select | dictionary: String → List of strings | A list of strings to add to a crate's Default: {} |
data | list of strings | A list of labels to add to a crate's Default: [] |
deps | list of strings | A list of labels to add to a crate's Default: [] |
tags | list of strings | A list of tags to add to a crate's generated targets. Default: [] |
extra_aliased_targets | dictionary: String → String | A list of targets to add to the generated aliases in the root crate repository. Default: {} |
gen_binaries | list of strings | As a list, the subset of the crate's bins that should get Default: [] |
gen_build_script | string | An authoritative flag to determine whether or not to produce Default: "auto" |
patch_args | list of strings | The Default: [] |
patch_tool | string | The Default: "" |
patches | list of labels | The Default: [] |
rustc_flags | list of strings | A list of strings to set on a crate's Default: [] |
rustc_flags_select | dictionary: String → List of strings | A list of strings to set on a crate's Default: {} |
strip_prefix | string | Default: "" |
workspace_cargo_toml | string | For crates from git, the ruleset assumes the (workspace) Cargo.toml is in the repo root. This attribute overrides the assumption. Default: "Cargo.toml" |
from_cargoGenerates a repo @crates from a Cargo.toml / Cargo.lock pair.
| Attribute | Type | Description |
|---|---|---|
name | name | The name of the repo to generate Default: "crates" |
cargo_toml | label | The workspace-level Cargo.toml. There can be multiple crates in the workspace. Default: None |
cargo_lock | label | Default: None |
cargo_config | label | Default: None |
use_home_cargo_credentials | boolean | If set, the ruleset will load Default: False |
*platform_triples | list of strings | The set of triples to resolve for. They must correspond to the union of any exec/target platforms that will participate in your build. |
use_experimental_platforms | boolean | If true, use experimental rules_rs platforms. If false, use the stable rules_rust platforms. Default: False |
validate_lockfile | boolean | If true, fail if Cargo.lock versions don't satisfy Cargo.toml requirements. Default: True |
debug | boolean | Default: False |
@rules_rs//rs:rust_binary.bzl
@rules_rs//rs:rust_library.bzl
@rules_rs//rs:rust_proc_macro.bzl
Functions & Macros
rust_proc_macroParameters
*name | |
deps | Default: [] |
proc_macro_deps | Default: [] |
kwargs |
Functions & Macros
@rules_rs//rs:rust_static_library.bzl
Functions & Macros
rust_static_libraryParameters
*name | |
deps | Default: [] |
proc_macro_deps | Default: [] |
kwargs |
@rules_rs//rs:rust_test.bzl
@rules_rs//rs/experimental:rules_rust_prost.bzl
Module extension that provisions the rules_rust_prost repository.
Module Extensions
rules_rust_prost@rules_rs//rs/experimental:rules_rust_reexported_extensions.bzl
Module Extensions
rustRust toolchain extension.
Tag Classes
repository_setTags for defining rust repository sets (where toolchains are defined).
| Attribute | Type | Description |
|---|---|---|
exec_triple | string | Exec triple for this repository_set. Default: "" |
name | name | Name of the repository_set - if you're looking to replace default toolchains you must use the exact name you're replacing. Default: "" |
opt_level | dictionary: String → String | Rustc optimization levels. For more details see the documentation for Default: {} |
target_compatible_with | list of labels | List of platform constraints this toolchain produces, for the particular target_triple this call is for. Default: [] |
target_settings | list of labels | A list of Default: [] |
target_triple | string | target_triple to configure. Default: "" |
versions | list of strings | A list of toolchain versions to download. This parameter only accepts one version per channel. E.g. Default: [] |
allocator_library | string | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. Default: "" |
dev_components | boolean | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". Default: False |
edition | string | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its Default: "" |
rustfmt_version | string | The version of the tool among "nightly", "beta", or an exact version. Default: "nightly/2026-03-05" |
sha256s | dictionary: String → String | A dict associating tool subdirectories to sha256 hashes. See rust_repositories for more details. Default: {} |
urls | list of strings | A 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). Default: ["https://static.rust-lang.org/dist/{}.tar.xz"] |
toolchainTags for defining rust toolchains (where toolchain tools are fetched).
| Attribute | Type | Description |
|---|---|---|
aliases | dictionary: String → String | Map of full toolchain repository name to an alias. If any repository is created by this extension matches a key in this dictionary, the name of the created repository will be remapped to the value instead. This may be required to work around path length limits on Windows. Default: {} |
extra_exec_rustc_flags | list of strings | Extra flags to pass to rustc in exec configuration Default: [] |
extra_rustc_flags | list of strings | Extra flags to pass to rustc in non-exec configuration Default: [] |
extra_rustc_flags_triples | dictionary: String → List of strings | Extra flags to pass to rustc in non-exec configuration. Key is the triple, value is the flag. Default: {} |
extra_target_triples | list of strings | Default: ["aarch64-apple-darwin", "aarch64-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "wasm32-unknown-unknown", "wasm32-wasip1", "wasm32-wasip2"] |
rust_analyzer_version | string | The version of Rustc to pair with rust-analyzer. Default: "" |
target_settings | list of labels | A list of Default: [] |
versions | list of strings | A list of toolchain versions to download. This parameter only accepts one version per channel. E.g. Default: ["1.94.0", "nightly/2026-03-05"] |
allocator_library | string | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. Default: "" |
dev_components | boolean | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". Default: False |
edition | string | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its Default: "" |
rustfmt_version | string | The version of the tool among "nightly", "beta", or an exact version. Default: "nightly/2026-03-05" |
sha256s | dictionary: String → String | A dict associating tool subdirectories to sha256 hashes. See rust_repositories for more details. Default: {} |
urls | list of strings | A 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). Default: ["https://static.rust-lang.org/dist/{}.tar.xz"] |
rust_host_toolsAn extension which exposes Rust tools compatible with the current host platform.
Tag Classes
host_tools| Attribute | Type | Description |
|---|---|---|
name | name | The name of the module to create Default: "rust_host_tools" |
version | string | The version of Rust to use for tools executed on the Bazel host. Default: "1.94.0" |
allocator_library | string | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. Default: "" |
dev_components | boolean | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". Default: False |
edition | string | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its Default: "" |
rustfmt_version | string | The version of the tool among "nightly", "beta", or an exact version. Default: "nightly/2026-03-05" |
sha256s | dictionary: String → String | A dict associating tool subdirectories to sha256 hashes. See rust_repositories for more details. Default: {} |
urls | list of strings | A 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). Default: ["https://static.rust-lang.org/dist/{}.tar.xz"] |
@rules_rs//rs/experimental:rules_rust.bzl
Module extension that provisions the rules_rust repository.
Module Extensions
rules_rustTag Classes
patchAdditional patches to apply to the pinned rules_rust archive.
| Attribute | Type | Description |
|---|---|---|
patches | list of labels | Additional patch files to apply to rules_rust. Default: [] |
strip | integer | Equivalent to adding Default: 0 |
@rules_rs//rs/experimental/platforms:declare_platforms.bzl
Platform definitions for Rust target triples.
Functions & Macros
declare_platformsParameters
targets | Default: ["aarch64-apple-darwin", "aarch64-pc-windows-msvc", "aarch64-unknown-linux-gnu", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-pc-windows-gnullvm", "aarch64-unknown-linux-musl", "arm-unknown-linux-gnueabi", "arm-unknown-linux-gnueabihf", "armv7-unknown-linux-gnueabihf", "i686-pc-windows-gnu", "powerpc-unknown-linux-gnu", "powerpc64-unknown-linux-gnu", "powerpc64le-unknown-linux-gnu", "powerpc64le-unknown-linux-musl", "riscv64gc-unknown-linux-gnu", "s390x-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-gnullvm", "x86_64-unknown-freebsd", "x86_64-unknown-linux-musl", "x86_64-unknown-netbsd", "aarch64-apple-ios", "aarch64-apple-ios-macabi", "aarch64-apple-ios-sim", "aarch64-linux-android", "aarch64-unknown-fuchsia", "aarch64-unknown-none", "aarch64-unknown-none-softfloat", "aarch64-unknown-uefi", "arm-linux-androideabi", "arm-unknown-linux-musleabi", "arm-unknown-linux-musleabihf", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "armv7-unknown-linux-musleabi", "armv7-unknown-linux-musleabihf", "i686-linux-android", "i686-pc-windows-gnullvm", "i686-unknown-freebsd", "i686-unknown-linux-musl", "i686-unknown-uefi", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-linux-musl", "riscv64gc-unknown-none-elf", "thumbv6m-none-eabi", "thumbv7em-none-eabi", "thumbv7em-none-eabihf", "thumbv7m-none-eabi", "thumbv8m.main-none-eabi", "thumbv8m.main-none-eabihf", "wasm32-unknown-emscripten", "wasm32-unknown-unknown", "wasm32-wasip1", "wasm32-wasip1-threads", "wasm32-wasip2", "x86_64-apple-ios", "x86_64-apple-ios-macabi", "x86_64-linux-android", "x86_64-unknown-fuchsia", "x86_64-unknown-none", "x86_64-unknown-uefi"] |
@rules_rs//rs/experimental/platforms:triples.bzl
Functions & Macros
triple_to_constraint_setParameters
*target_triple |
@rules_rs//rs/experimental/platforms/config:declare_config_settings.bzl
Config settings for Rust target triples.
Functions & Macros
declare_config_settingsParameters
targets | Default: ["aarch64-apple-darwin", "aarch64-pc-windows-msvc", "aarch64-unknown-linux-gnu", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-pc-windows-gnullvm", "aarch64-unknown-linux-musl", "arm-unknown-linux-gnueabi", "arm-unknown-linux-gnueabihf", "armv7-unknown-linux-gnueabihf", "i686-pc-windows-gnu", "powerpc-unknown-linux-gnu", "powerpc64-unknown-linux-gnu", "powerpc64le-unknown-linux-gnu", "powerpc64le-unknown-linux-musl", "riscv64gc-unknown-linux-gnu", "s390x-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-gnullvm", "x86_64-unknown-freebsd", "x86_64-unknown-linux-musl", "x86_64-unknown-netbsd", "aarch64-apple-ios", "aarch64-apple-ios-macabi", "aarch64-apple-ios-sim", "aarch64-linux-android", "aarch64-unknown-fuchsia", "aarch64-unknown-none", "aarch64-unknown-none-softfloat", "aarch64-unknown-uefi", "arm-linux-androideabi", "arm-unknown-linux-musleabi", "arm-unknown-linux-musleabihf", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "armv7-unknown-linux-musleabi", "armv7-unknown-linux-musleabihf", "i686-linux-android", "i686-pc-windows-gnullvm", "i686-unknown-freebsd", "i686-unknown-linux-musl", "i686-unknown-uefi", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-linux-musl", "riscv64gc-unknown-none-elf", "thumbv6m-none-eabi", "thumbv7em-none-eabi", "thumbv7em-none-eabihf", "thumbv7m-none-eabi", "thumbv8m.main-none-eabi", "thumbv8m.main-none-eabihf", "wasm32-unknown-emscripten", "wasm32-unknown-unknown", "wasm32-wasip1", "wasm32-wasip1-threads", "wasm32-wasip2", "x86_64-apple-ios", "x86_64-apple-ios-macabi", "x86_64-linux-android", "x86_64-unknown-fuchsia", "x86_64-unknown-none", "x86_64-unknown-uefi"] |
@rules_rs//rs/experimental/toolchains:declare_rust_analyzer_toolchains.bzl
Functions & Macros
declare_rust_analyzer_toolchainsParameters
*version | |
*rust_analyzer_version | |
execs | Default: ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin"] |
@rules_rs//rs/experimental/toolchains:declare_rustc_toolchains.bzl
Functions & Macros
declare_rustc_toolchainsDeclare toolchains for all supported target platforms.
Parameters
*version | |
*edition | |
extra_rustc_flags | Default: {} |
extra_exec_rustc_flags | Default: {} |
execs | Default: ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin"] |
targets | Default: ["aarch64-apple-darwin", "aarch64-pc-windows-msvc", "aarch64-unknown-linux-gnu", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-pc-windows-gnu", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-pc-windows-gnullvm", "aarch64-unknown-linux-musl", "arm-unknown-linux-gnueabi", "arm-unknown-linux-gnueabihf", "armv7-unknown-linux-gnueabihf", "i686-pc-windows-gnu", "powerpc-unknown-linux-gnu", "powerpc64-unknown-linux-gnu", "powerpc64le-unknown-linux-gnu", "powerpc64le-unknown-linux-musl", "riscv64gc-unknown-linux-gnu", "s390x-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-gnullvm", "x86_64-unknown-freebsd", "x86_64-unknown-linux-musl", "x86_64-unknown-netbsd", "aarch64-apple-ios", "aarch64-apple-ios-macabi", "aarch64-apple-ios-sim", "aarch64-linux-android", "aarch64-unknown-fuchsia", "aarch64-unknown-none", "aarch64-unknown-none-softfloat", "aarch64-unknown-uefi", "arm-linux-androideabi", "arm-unknown-linux-musleabi", "arm-unknown-linux-musleabihf", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "armv7-unknown-linux-musleabi", "armv7-unknown-linux-musleabihf", "i686-linux-android", "i686-pc-windows-gnullvm", "i686-unknown-freebsd", "i686-unknown-linux-musl", "i686-unknown-uefi", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-linux-musl", "riscv64gc-unknown-none-elf", "thumbv6m-none-eabi", "thumbv7em-none-eabi", "thumbv7em-none-eabihf", "thumbv7m-none-eabi", "thumbv8m.main-none-eabi", "thumbv8m.main-none-eabihf", "wasm32-unknown-emscripten", "wasm32-unknown-unknown", "wasm32-wasip1", "wasm32-wasip1-threads", "wasm32-wasip2", "x86_64-apple-ios", "x86_64-apple-ios-macabi", "x86_64-linux-android", "x86_64-unknown-fuchsia", "x86_64-unknown-none", "x86_64-unknown-uefi"] |
@rules_rs//rs/experimental/toolchains:declare_rustfmt_toolchains.bzl
Functions & Macros
declare_rustfmt_toolchainsParameters
*version | |
*rustfmt_version | |
*edition | |
execs | Default: ["x86_64-unknown-linux-gnu", "aarch64-unknown-linux-gnu", "x86_64-pc-windows-msvc", "aarch64-pc-windows-msvc", "x86_64-apple-darwin", "aarch64-apple-darwin"] |
@rules_rs//rs/experimental/toolchains:module_extension.bzl
Module extension for configuring experimental Rust toolchains.
Module Extensions
toolchainsTag Classes
toolchain| Attribute | Type | Description |
|---|---|---|
name | name | Name of the generated toolchain repo. Default: "default_rust_toolchains" |
version | string | Rust version (e.g. 1.86.0 or nightly/2025-04-03) Default: "1.92.0" |
rustfmt_version | string | Rustfmt version (e.g. 1.86.0 or nightly/2025-04-03) Default: "" |
rust_analyzer_version | string | Rust-analyzer version (e.g. 1.86.0 or nightly/2025-04-03) Default: "" |
edition | string | Default edition to apply to toolchains. Default: "2021" |
extra_rustc_flags | dictionary: String → List of strings | Additional rustc flags by target triple. Default: {} |
extra_exec_rustc_flags | dictionary: String → List of strings | Additional rustc flags by exec triple. Default: {} |
@rules_rs//rs/experimental/toolchains:toolchain_utils.bzl
Shared helpers for toolchain generation.
Functions & Macros
sanitize_tripleParameters
*triple_str |
sanitize_versionParameters
*version |