Builds a Rust benchmark test.
Warning: This rule is currently experimental. Rust Benchmark tests require the Bencher
interface in the unstable libtest
crate, which is behind the test
unstable feature gate. As a result, using this rule would require using a nightly binary release of Rust.
Example:
Suppose you have the following directory structure for a Rust project with a library crate, fibonacci
with benchmarks under the benches/
directory:
[workspace]/ WORKSPACE fibonacci/ BUILD src/ lib.rs benches/ fibonacci_bench.rs
fibonacci/src/lib.rs
:
pub fn fibonacci(n: u64) -> u64 { if n < 2 { return n; } let mut n1: u64 = 0; let mut n2: u64 = 1; for _ in 1..n { let sum = n1 + n2; n1 = n2; n2 = sum; } n2 }
fibonacci/benches/fibonacci_bench.rs
:
#![feature(test)] extern crate test; extern crate fibonacci; use test::Bencher; #[bench] fn bench_fibonacci(b: &mut Bencher) { b.iter(|| fibonacci::fibonacci(40)); }
To build the benchmark test, add a rust_benchmark
target:
fibonacci/BUILD
:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_benchmark") rust_library( name = "fibonacci", srcs = ["src/lib.rs"], ) rust_benchmark( name = "fibonacci_bench", srcs = ["benches/fibonacci_bench.rs"], deps = [":fibonacci"], )
Run the benchmark test using: bazel run //fibonacci:fibonacci_bench
.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
aliases | Remap crates to a new name or moniker for linkage to this target These are other rust_library targets and will be presented as the new name given. | Dictionary: Label -> String | optional | {} |
crate_features | List of features to enable for this crate. Features are defined in the code using the #[cfg(feature = “foo”)] configuration option. The features listed here will be passed to rustc with --cfg feature=“${feature_name}” flags. | List of strings | optional | [] |
crate_root | The file that will be passed to rustc to be used for building this crate. If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None |
data | List of files used by this rule at runtime. This attribute can be used to specify any data files that are embedded into the library, such as via the include_str! macro. | List of labels | optional | [] |
deps | List of other libraries to be linked to this library target. These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] |
edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
out_dir_tar | Deprecated, do not use, see [#cargo_build_script] instead. | Label | optional | None |
proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] |
rustc_env | Dictionary of additional “key”: “value” environment variables to set for rustc. | Dictionary: String -> String | optional | {} |
rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] |
srcs | List of Rust .rs source files used to build the library. If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] |
version | A version to inject in the cargo environment variable. | String | optional | “0.0.0” |
Builds a Rust binary crate.
Example:
Suppose you have the following directory structure for a Rust project with a library crate, hello_lib
, and a binary crate, hello_world
that uses the hello_lib
library:
[workspace]/ WORKSPACE hello_lib/ BUILD src/ lib.rs hello_world/ BUILD src/ main.rs
hello_lib/src/lib.rs
:
pub struct Greeter { greeting: String, } impl Greeter { pub fn new(greeting: &str) -> Greeter { Greeter { greeting: greeting.to_string(), } } pub fn greet(&self, thing: &str) { println!("{} {}", &self.greeting, thing); } }
hello_lib/BUILD
:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library") rust_library( name = "hello_lib", srcs = ["src/lib.rs"], )
hello_world/src/main.rs
:
extern crate hello_lib; fn main() { let hello = hello_lib::Greeter::new("Hello"); hello.greet("world"); }
hello_world/BUILD
:
load("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary") rust_binary( name = "hello_world", srcs = ["src/main.rs"], deps = ["//hello_lib"], )
Build and run hello_world
:
$ bazel run //hello_world INFO: Found 1 target... Target //examples/rust/hello_world:hello_world up-to-date: bazel-bin/examples/rust/hello_world/hello_world INFO: Elapsed time: 1.308s, Critical Path: 1.22s INFO: Running command line: bazel-bin/examples/rust/hello_world/hello_world Hello world
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
aliases | Remap crates to a new name or moniker for linkage to this target These are other rust_library targets and will be presented as the new name given. | Dictionary: Label -> String | optional | {} |
crate_features | List of features to enable for this crate. Features are defined in the code using the #[cfg(feature = “foo”)] configuration option. The features listed here will be passed to rustc with --cfg feature=“${feature_name}” flags. | List of strings | optional | [] |
crate_root | The file that will be passed to rustc to be used for building this crate. If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None |
crate_type | - | String | optional | “bin” |
data | List of files used by this rule at runtime. This attribute can be used to specify any data files that are embedded into the library, such as via the include_str! macro. | List of labels | optional | [] |
deps | List of other libraries to be linked to this library target. These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] |
edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
linker_script | Link script to forward into linker via rustc options. | Label | optional | None |
out_binary | - | Boolean | optional | False |
out_dir_tar | Deprecated, do not use, see [#cargo_build_script] instead. | Label | optional | None |
proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] |
rustc_env | Dictionary of additional “key”: “value” environment variables to set for rustc. | Dictionary: String -> String | optional | {} |
rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] |
srcs | List of Rust .rs source files used to build the library. If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] |
version | A version to inject in the cargo environment variable. | String | optional | “0.0.0” |
Generates a rust source file from a cc_library and a header.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
bindgen_flags | Flags to pass directly to the bindgen executable. See https://rust-lang.github.io/rust-bindgen/ for details. | List of strings | optional | [] |
cc_lib | The cc_library that contains the .h file. This is used to find the transitive includes. | Label | optional | None |
clang_flags | Flags to pass directly to the clang executable. | List of strings | optional | [] |
header | The .h file to generate bindings for. | Label | optional | None |
The tools required for the rust_bindgen
rule.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
bindgen | The label of a bindgen executable. | Label | optional | None |
clang | The label of a clang executable. | Label | optional | None |
libclang | A cc_library that provides bindgen's runtime dependency on libclang. | Label | optional | None |
libstdcxx | A cc_library that satisfies libclang's libstdc++ dependency. | Label | optional | None |
rustfmt | The label of a rustfmt executable. If this is provided, generated sources will be formatted. | Label | optional | None |
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
dep | The crate to generate documentation for. | Label | required | |
html_after_content | - | Label | optional | None |
html_before_content | - | Label | optional | None |
html_in_header | - | Label | optional | None |
markdown_css | - | List of labels | optional | [] |
Runs Rust documentation tests.
Example:
Suppose you have the following directory structure for a Rust library crate:
[workspace]/ WORKSPACE hello_lib/ BUILD src/ lib.rs
To run documentation tests for the hello_lib
crate, define a rust_doc_test
target that depends on the hello_lib
rust_library
target:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_doc_test") rust_library( name = "hello_lib", srcs = ["src/lib.rs"], ) rust_doc_test( name = "hello_lib_doc_test", dep = ":hello_lib", )
Running bazel test //hello_lib:hello_lib_doc_test
will run all documentation tests for the hello_lib
library crate.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
dep | The label of the target to run documentation tests for. rust_doc_test can run documentation tests for the source files of rust_library or rust_binary targets. | Label | required |
Builds a Rust library crate from a set of proto_library
s suitable for gRPC.
Example:
load("@io_bazel_rules_rust//proto:proto.bzl", "rust_grpc_library") load("@io_bazel_rules_rust//proto:toolchain.bzl", "GRPC_COMPILE_DEPS") proto_library( name = "my_proto", srcs = ["my.proto"] ) rust_grpc_library( name = "rust", deps = [":my_proto"], ) rust_binary( name = "my_service", srcs = ["my_service.rs"], deps = [":rust"] + GRPC_COMPILE_DEPS, )
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding gRPC stubs. | List of labels | required | |
rust_deps | The crates the generated library depends on. | List of labels | optional | [“@io_bazel_rules_rust//proto/raze:protobuf”, “@io_bazel_rules_rust//proto/raze:grpc”, “@io_bazel_rules_rust//proto/raze:tls_api”, “@io_bazel_rules_rust//proto/raze:tls_api_stub”] |
Builds a Rust library crate.
Example:
Suppose you have the following directory structure for a simple Rust library crate:
[workspace]/ WORKSPACE hello_lib/ BUILD src/ greeter.rs lib.rs
hello_lib/src/greeter.rs
:
pub struct Greeter { greeting: String, } impl Greeter { pub fn new(greeting: &str) -> Greeter { Greeter { greeting: greeting.to_string(), } } pub fn greet(&self, thing: &str) { println!("{} {}", &self.greeting, thing); } }
hello_lib/src/lib.rs
:
pub mod greeter;
hello_lib/BUILD
:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library") rust_library( name = "hello_lib", srcs = [ "src/greeter.rs", "src/lib.rs", ], )
Build the library:
$ bazel build //hello_lib INFO: Found 1 target... Target //examples/rust/hello_lib:hello_lib up-to-date: bazel-bin/examples/rust/hello_lib/libhello_lib.rlib INFO: Elapsed time: 1.245s, Critical Path: 1.01s
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
aliases | Remap crates to a new name or moniker for linkage to this target These are other rust_library targets and will be presented as the new name given. | Dictionary: Label -> String | optional | {} |
crate_features | List of features to enable for this crate. Features are defined in the code using the #[cfg(feature = “foo”)] configuration option. The features listed here will be passed to rustc with --cfg feature=“${feature_name}” flags. | List of strings | optional | [] |
crate_root | The file that will be passed to rustc to be used for building this crate. If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None |
crate_type | The type of linkage to use for building this library. Options include “lib”, “rlib”, “dylib”, “cdylib”, “staticlib”, and “proc-macro”. The exact output file will depend on the toolchain used. | String | optional | “rlib” |
data | List of files used by this rule at runtime. This attribute can be used to specify any data files that are embedded into the library, such as via the include_str! macro. | List of labels | optional | [] |
deps | List of other libraries to be linked to this library target. These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] |
edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
out_dir_tar | Deprecated, do not use, see [#cargo_build_script] instead. | Label | optional | None |
proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] |
rustc_env | Dictionary of additional “key”: “value” environment variables to set for rustc. | Dictionary: String -> String | optional | {} |
rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] |
srcs | List of Rust .rs source files used to build the library. If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] |
version | A version to inject in the cargo environment variable. | String | optional | “0.0.0” |
Builds a Rust library crate from a set of proto_library
s.
Example:
load("@io_bazel_rules_rust//proto:proto.bzl", "rust_proto_library") load("@io_bazel_rules_rust//proto:toolchain.bzl", "PROTO_COMPILE_DEPS") proto_library( name = "my_proto", srcs = ["my.proto"] ) proto_rust_library( name = "rust", deps = [":my_proto"], ) rust_binary( name = "my_proto_binary", srcs = ["my_proto_binary.rs"], deps = [":rust"] + PROTO_COMPILE_DEPS, )
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
deps | List of proto_library dependencies that will be built. One crate for each proto_library will be created with the corresponding stubs. | List of labels | required | |
rust_deps | The crates the generated library depends on. | List of labels | optional | [“@io_bazel_rules_rust//proto/raze:protobuf”] |
Declares a Rust Proto toolchain for use.
This is used to configure proto compilation and can be used to set different protobuf compiler plugin.
Example:
Suppose a new nicer gRPC plugin has came out. The new plugin can be used in Bazel by defining a new toolchain definition and declaration:
load('@io_bazel_rules_rust//proto:toolchain.bzl', 'rust_proto_toolchain') rust_proto_toolchain( name="rust_proto_impl", grpc_plugin="@rust_grpc//:grpc_plugin", grpc_compile_deps=["@rust_grpc//:grpc_deps"], ) toolchain( name="rust_proto", exec_compatible_with = [ "@platforms//cpu:cpuX", ], target_compatible_with = [ "@platforms//cpu:cpuX", ], toolchain = ":rust_proto_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 @io_bazel_rules_rust//proto:BUILD for examples of defining the toolchain.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
edition | The edition used by the generated rust source. | String | optional | “2015” |
grpc_plugin | The location of the Rust protobuf compiler plugin to generate rust gRPC stubs. | Label | optional | @io_bazel_rules_rust//proto:protoc_gen_rust_grpc |
proto_plugin | The location of the Rust protobuf compiler plugin used to generate rust sources. | Label | optional | @io_bazel_rules_rust//proto:protoc_gen_rust |
protoc | The location of the protoc binary. It should be an executable target. | Label | optional | @com_google_protobuf//:protoc |
Builds a Rust test crate.
Examples:
Suppose you have the following directory structure for a Rust library crate with unit test code in the library sources:
[workspace]/ WORKSPACE hello_lib/ BUILD src/ lib.rs
hello_lib/src/lib.rs
:
pub struct Greeter { greeting: String, } impl Greeter { pub fn new(greeting: &str) -> Greeter { Greeter { greeting: greeting.to_string(), } } pub fn greet(&self, thing: &str) { println!("{} {}", &self.greeting, thing); } } #[cfg(test)] mod test { use super::Greeter; #[test] fn test_greeting() { let hello = Greeter::new("Hi"); assert_eq!("Hi Rust", hello.greeting("Rust")); } }
To build and run the tests, simply add a rust_test
rule with no srcs
and only depends on the hello_lib
rust_library
target:
hello_lib/BUILD
:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_test") rust_library( name = "hello_lib", srcs = ["src/lib.rs"], ) rust_test( name = "hello_lib_test", deps = [":hello_lib"], )
Run the test with bazel build //hello_lib:hello_lib_test
.
To run a crate or lib with the #[cfg(test)]
configuration, handling inline tests, you should specify the crate directly like so.
rust_test( name = "hello_lib_test", crate = ":hello_lib", # You may add other deps that are specific to the test configuration deps = ["//some/dev/dep"], )
test
directoryIntegration tests that live in the tests
directory, they are essentially built as separate crates. Suppose you have the following directory structure where greeting.rs
is an integration test for the hello_lib
library crate:
[workspace]/ WORKSPACE hello_lib/ BUILD src/ lib.rs tests/ greeting.rs
hello_lib/tests/greeting.rs
:
extern crate hello_lib; use hello_lib; #[test] fn test_greeting() { let hello = greeter::Greeter::new("Hello"); assert_eq!("Hello world", hello.greeting("world")); }
To build the greeting.rs
integration test, simply add a rust_test
target with greeting.rs
in srcs
and a dependency on the hello_lib
target:
hello_lib/BUILD
:
package(default_visibility = ["//visibility:public"]) load("@io_bazel_rules_rust//rust:rust.bzl", "rust_library", "rust_test") rust_library( name = "hello_lib", srcs = ["src/lib.rs"], ) rust_test( name = "greeting_test", srcs = ["tests/greeting.rs"], deps = [":hello_lib"], )
Run the test with bazel build //hello_lib:hello_lib_test
.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
aliases | Remap crates to a new name or moniker for linkage to this target These are other rust_library targets and will be presented as the new name given. | Dictionary: Label -> String | optional | {} |
crate | Target inline tests declared in the given crate These tests are typically those that would be held out under #[cfg(test)] declarations. | Label | optional | None |
crate_features | List of features to enable for this crate. Features are defined in the code using the #[cfg(feature = “foo”)] configuration option. The features listed here will be passed to rustc with --cfg feature=“${feature_name}” flags. | List of strings | optional | [] |
crate_root | The file that will be passed to rustc to be used for building this crate. If crate_root is not set, then this rule will look for a lib.rs file (or main.rs for rust_binary) or the single file in srcs if srcs contains only one file. | Label | optional | None |
data | List of files used by this rule at runtime. This attribute can be used to specify any data files that are embedded into the library, such as via the include_str! macro. | List of labels | optional | [] |
deps | List of other libraries to be linked to this library target. These can be either other rust_library targets or cc_library targets if linking a native library. | List of labels | optional | [] |
edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | "" |
out_dir_tar | Deprecated, do not use, see [#cargo_build_script] instead. | Label | optional | None |
proc_macro_deps | List of rust_library targets with kind proc-macro used to help build this library target. | List of labels | optional | [] |
rustc_env | Dictionary of additional “key”: “value” environment variables to set for rustc. | Dictionary: String -> String | optional | {} |
rustc_flags | List of compiler flags passed to rustc. | List of strings | optional | [] |
srcs | List of Rust .rs source files used to build the library. If srcs contains more than one file, then there must be a file either named lib.rs. Otherwise, crate_root must be set to the source file that is the root of the crate to be passed to rustc to build this crate. | List of labels | optional | [] |
version | A version to inject in the cargo environment variable. | String | optional | “0.0.0” |
Generates javascript and typescript bindings for a webassembly module.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
bindgen_flags | Flags to pass directly to the bindgen executable. See https://github.com/rustwasm/wasm-bindgen/ for details. | List of strings | optional | [] |
wasm_file | The .wasm file to generate bindings for. | Label | optional | None |
The tools required for the rust_wasm_bindgen
rule.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
bindgen | The label of a bindgen executable. | Label | optional | None |
Compile 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("@io_bazel_rules_rust//rust:rust.bzl", "rust_binary", "rust_library") load("@io_bazel_rules_rust//cargo:cargo_build_script.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"], # Data are shipped during execution. data = ["src/lib.rs"], # Environment variables passed during build.rs execution build_script_env = {"CARGO_PKG_VERSION": "0.1.2"}, ) 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 | Description | Default Value |
---|---|---|
name | - | none |
crate_name | - | "" |
crate_features | - | [] |
deps | - | [] |
build_script_env | - | {} |
kwargs | - | none |
Generates a rust source file for header
, and builds a rust_library.
Arguments are the same as rust_bindgen
, and kwargs
are passed directly to rust_library.
PARAMETERS
Name | Description | Default Value |
---|---|---|
name | - | none |
header | - | none |
cc_lib | - | none |
bindgen_flags | - | None |
clang_flags | - | None |
kwargs | - | none |