@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: [] |
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 |
Rules
@rules_rs//rs:rust_static_library.bzl
Rules
rust_static_libraryBuilds a Rust static library.
This static library will contain all transitively reachable crates and native objects.
It is meant to be used when producing an artifact that is then consumed by some other build system
(for example to produce an archive that Python program links against).
This rule provides CcInfo, so it can be used everywhere Bazel expects rules_cc.
When building the whole binary in Bazel, use rust_library instead.
| Attribute | Type | Description |
|---|---|---|
*name | name | A unique name for this target. |
aliases | dictionary: Label → String | Remap crates to a new name or moniker for linkage to this target These are other Default: {} |
alwayslink | boolean | If 1, any binary that depends (directly or indirectly) on this library This attribute is used by the C++ Starlark API when passing CcInfo providers. Default: False |
compile_data | list of labels | List of files used by this rule at compile time. This attribute can be used to specify any data files that are embedded into Default: [] |
crate_features | list of strings | List of features to enable for this crate. Features are defined in the code using the Default: [] |
crate_name | string | Crate name to use for this target. This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Default: "" |
crate_root | label | The file that will be passed to If Default: None |
data | list of labels | List of files used by this rule at compile time and runtime. If including data at compile time with include_str!() and similar, Default: [] |
deps | list of labels | List of other libraries to be linked to this library target. These can be either other Default: [] |
edition | string | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. Default: "" |
lint_config | label | Set of lints to apply when building this crate. Default: None |
proc_macro_deps | list of labels | Copy of deps in exec configuration. This should really be called Rule implementations use this to select exec-configured Default: [] |
require_explicit_unstable_features | integer | Whether to require all unstable features to be explicitly opted in to using Default: -1 |
rustc_env | dictionary: String → String | Dictionary of additional rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the Default: {} |
rustc_env_files | list of labels | Files containing additional environment variables to set for rustc. These files should contain a single variable per line, of format The order that these files will be processed is unspecified, so Note that the variables here are subject to Default: [] |
rustc_flags | list of strings | List of compiler flags passed to These strings are subject to Make variable expansion for predefined Default: [] |
srcs | list of labels | List of Rust If Default: [] |
stamp | integer | Whether to encode build information into the
Stamped targets are not rebuilt unless their dependencies change. For example if a Default: 0 |
version | string | A version to inject in the cargo environment variable. Default: "0.0.0" |
allocator_libraries | label | Default: "@rules_rust//ffi/rs:default_allocator_libraries" |
platform | label | Optional platform to transition the static library to. Default: None |
@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.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_rustc_toolchains.bzl
Functions & Macros
declare_rustc_toolchainsDeclare toolchains for all supported target platforms.
Parameters
*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"] |
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: "" |
edition | string | Default edition to apply to toolchains. Default: "2021" |
@rules_rs//rs/experimental/toolchains:toolchain_utils.bzl
Shared helpers for toolchain generation.
Functions & Macros
sanitize_tripleParameters
*triple_str |
sanitize_versionParameters
*version |