xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/defs.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan# Defs
3*d4726bddSHONG Yifan
4*d4726bddSHONG Yifan* [rust_binary](#rust_binary)
5*d4726bddSHONG Yifan* [rust_library](#rust_library)
6*d4726bddSHONG Yifan* [rust_library_group](#rust_library_group)
7*d4726bddSHONG Yifan* [rust_static_library](#rust_static_library)
8*d4726bddSHONG Yifan* [rust_shared_library](#rust_shared_library)
9*d4726bddSHONG Yifan* [rust_proc_macro](#rust_proc_macro)
10*d4726bddSHONG Yifan* [rust_test](#rust_test)
11*d4726bddSHONG Yifan* [rust_test_suite](#rust_test_suite)
12*d4726bddSHONG Yifan* [error_format](#error_format)
13*d4726bddSHONG Yifan* [extra_rustc_flag](#extra_rustc_flag)
14*d4726bddSHONG Yifan* [extra_rustc_flags](#extra_rustc_flags)
15*d4726bddSHONG Yifan* [capture_clippy_output](#capture_clippy_output)
16*d4726bddSHONG Yifan
17*d4726bddSHONG Yifan<a id="capture_clippy_output"></a>
18*d4726bddSHONG Yifan
19*d4726bddSHONG Yifan## capture_clippy_output
20*d4726bddSHONG Yifan
21*d4726bddSHONG Yifan<pre>
22*d4726bddSHONG Yifancapture_clippy_output(<a href="#capture_clippy_output-name">name</a>)
23*d4726bddSHONG Yifan</pre>
24*d4726bddSHONG Yifan
25*d4726bddSHONG YifanControl whether to print clippy output or store it to a file, using the configured error_format.
26*d4726bddSHONG Yifan
27*d4726bddSHONG Yifan**ATTRIBUTES**
28*d4726bddSHONG Yifan
29*d4726bddSHONG Yifan
30*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
31*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
32*d4726bddSHONG Yifan| <a id="capture_clippy_output-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
33*d4726bddSHONG Yifan
34*d4726bddSHONG Yifan
35*d4726bddSHONG Yifan<a id="error_format"></a>
36*d4726bddSHONG Yifan
37*d4726bddSHONG Yifan## error_format
38*d4726bddSHONG Yifan
39*d4726bddSHONG Yifan<pre>
40*d4726bddSHONG Yifanerror_format(<a href="#error_format-name">name</a>)
41*d4726bddSHONG Yifan</pre>
42*d4726bddSHONG Yifan
43*d4726bddSHONG YifanChange the [--error-format](https://doc.rust-lang.org/rustc/command-line-arguments.html#option-error-format) flag from the command line with `--@rules_rust//:error_format`. See rustc documentation for valid values.
44*d4726bddSHONG Yifan
45*d4726bddSHONG Yifan**ATTRIBUTES**
46*d4726bddSHONG Yifan
47*d4726bddSHONG Yifan
48*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
49*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
50*d4726bddSHONG Yifan| <a id="error_format-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
51*d4726bddSHONG Yifan
52*d4726bddSHONG Yifan
53*d4726bddSHONG Yifan<a id="extra_rustc_flag"></a>
54*d4726bddSHONG Yifan
55*d4726bddSHONG Yifan## extra_rustc_flag
56*d4726bddSHONG Yifan
57*d4726bddSHONG Yifan<pre>
58*d4726bddSHONG Yifanextra_rustc_flag(<a href="#extra_rustc_flag-name">name</a>)
59*d4726bddSHONG Yifan</pre>
60*d4726bddSHONG Yifan
61*d4726bddSHONG YifanAdd additional rustc_flag from the command line with `--@rules_rust//:extra_rustc_flag`. Multiple uses are accumulated and appended after the extra_rustc_flags.
62*d4726bddSHONG Yifan
63*d4726bddSHONG Yifan**ATTRIBUTES**
64*d4726bddSHONG Yifan
65*d4726bddSHONG Yifan
66*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
67*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
68*d4726bddSHONG Yifan| <a id="extra_rustc_flag-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
69*d4726bddSHONG Yifan
70*d4726bddSHONG Yifan
71*d4726bddSHONG Yifan<a id="extra_rustc_flags"></a>
72*d4726bddSHONG Yifan
73*d4726bddSHONG Yifan## extra_rustc_flags
74*d4726bddSHONG Yifan
75*d4726bddSHONG Yifan<pre>
76*d4726bddSHONG Yifanextra_rustc_flags(<a href="#extra_rustc_flags-name">name</a>)
77*d4726bddSHONG Yifan</pre>
78*d4726bddSHONG Yifan
79*d4726bddSHONG YifanAdd additional rustc_flags from the command line with `--@rules_rust//:extra_rustc_flags`. This flag should only be used for flags that need to be applied across the entire build. For options that apply to individual crates, use the rustc_flags attribute on the individual crate's rule instead. NOTE: These flags not applied to the exec configuration (proc-macros, cargo_build_script, etc); use `--@rules_rust//:extra_exec_rustc_flags` to apply flags to the exec configuration.
80*d4726bddSHONG Yifan
81*d4726bddSHONG Yifan**ATTRIBUTES**
82*d4726bddSHONG Yifan
83*d4726bddSHONG Yifan
84*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
85*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
86*d4726bddSHONG Yifan| <a id="extra_rustc_flags-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
87*d4726bddSHONG Yifan
88*d4726bddSHONG Yifan
89*d4726bddSHONG Yifan<a id="rust_binary"></a>
90*d4726bddSHONG Yifan
91*d4726bddSHONG Yifan## rust_binary
92*d4726bddSHONG Yifan
93*d4726bddSHONG Yifan<pre>
94*d4726bddSHONG Yifanrust_binary(<a href="#rust_binary-name">name</a>, <a href="#rust_binary-deps">deps</a>, <a href="#rust_binary-srcs">srcs</a>, <a href="#rust_binary-data">data</a>, <a href="#rust_binary-aliases">aliases</a>, <a href="#rust_binary-alwayslink">alwayslink</a>, <a href="#rust_binary-compile_data">compile_data</a>, <a href="#rust_binary-crate_features">crate_features</a>, <a href="#rust_binary-crate_name">crate_name</a>,
95*d4726bddSHONG Yifan            <a href="#rust_binary-crate_root">crate_root</a>, <a href="#rust_binary-crate_type">crate_type</a>, <a href="#rust_binary-edition">edition</a>, <a href="#rust_binary-env">env</a>, <a href="#rust_binary-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_binary-linker_script">linker_script</a>,
96*d4726bddSHONG Yifan            <a href="#rust_binary-malloc">malloc</a>, <a href="#rust_binary-out_binary">out_binary</a>, <a href="#rust_binary-platform">platform</a>, <a href="#rust_binary-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_binary-rustc_env">rustc_env</a>, <a href="#rust_binary-rustc_env_files">rustc_env_files</a>, <a href="#rust_binary-rustc_flags">rustc_flags</a>,
97*d4726bddSHONG Yifan            <a href="#rust_binary-stamp">stamp</a>, <a href="#rust_binary-version">version</a>)
98*d4726bddSHONG Yifan</pre>
99*d4726bddSHONG Yifan
100*d4726bddSHONG YifanBuilds a Rust binary crate.
101*d4726bddSHONG Yifan
102*d4726bddSHONG YifanExample:
103*d4726bddSHONG Yifan
104*d4726bddSHONG YifanSuppose you have the following directory structure for a Rust project with a
105*d4726bddSHONG Yifanlibrary crate, `hello_lib`, and a binary crate, `hello_world` that uses the
106*d4726bddSHONG Yifan`hello_lib` library:
107*d4726bddSHONG Yifan
108*d4726bddSHONG Yifan```output
109*d4726bddSHONG Yifan[workspace]/
110*d4726bddSHONG Yifan    WORKSPACE
111*d4726bddSHONG Yifan    hello_lib/
112*d4726bddSHONG Yifan        BUILD
113*d4726bddSHONG Yifan        src/
114*d4726bddSHONG Yifan            lib.rs
115*d4726bddSHONG Yifan    hello_world/
116*d4726bddSHONG Yifan        BUILD
117*d4726bddSHONG Yifan        src/
118*d4726bddSHONG Yifan            main.rs
119*d4726bddSHONG Yifan```
120*d4726bddSHONG Yifan
121*d4726bddSHONG Yifan`hello_lib/src/lib.rs`:
122*d4726bddSHONG Yifan```rust
123*d4726bddSHONG Yifanpub struct Greeter {
124*d4726bddSHONG Yifan    greeting: String,
125*d4726bddSHONG Yifan}
126*d4726bddSHONG Yifan
127*d4726bddSHONG Yifanimpl Greeter {
128*d4726bddSHONG Yifan    pub fn new(greeting: &str) -> Greeter {
129*d4726bddSHONG Yifan        Greeter { greeting: greeting.to_string(), }
130*d4726bddSHONG Yifan    }
131*d4726bddSHONG Yifan
132*d4726bddSHONG Yifan    pub fn greet(&self, thing: &str) {
133*d4726bddSHONG Yifan        println!("{} {}", &self.greeting, thing);
134*d4726bddSHONG Yifan    }
135*d4726bddSHONG Yifan}
136*d4726bddSHONG Yifan```
137*d4726bddSHONG Yifan
138*d4726bddSHONG Yifan`hello_lib/BUILD`:
139*d4726bddSHONG Yifan```python
140*d4726bddSHONG Yifanpackage(default_visibility = ["//visibility:public"])
141*d4726bddSHONG Yifan
142*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library")
143*d4726bddSHONG Yifan
144*d4726bddSHONG Yifanrust_library(
145*d4726bddSHONG Yifan    name = "hello_lib",
146*d4726bddSHONG Yifan    srcs = ["src/lib.rs"],
147*d4726bddSHONG Yifan)
148*d4726bddSHONG Yifan```
149*d4726bddSHONG Yifan
150*d4726bddSHONG Yifan`hello_world/src/main.rs`:
151*d4726bddSHONG Yifan```rust
152*d4726bddSHONG Yifanextern crate hello_lib;
153*d4726bddSHONG Yifan
154*d4726bddSHONG Yifanfn main() {
155*d4726bddSHONG Yifan    let hello = hello_lib::Greeter::new("Hello");
156*d4726bddSHONG Yifan    hello.greet("world");
157*d4726bddSHONG Yifan}
158*d4726bddSHONG Yifan```
159*d4726bddSHONG Yifan
160*d4726bddSHONG Yifan`hello_world/BUILD`:
161*d4726bddSHONG Yifan```python
162*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_binary")
163*d4726bddSHONG Yifan
164*d4726bddSHONG Yifanrust_binary(
165*d4726bddSHONG Yifan    name = "hello_world",
166*d4726bddSHONG Yifan    srcs = ["src/main.rs"],
167*d4726bddSHONG Yifan    deps = ["//hello_lib"],
168*d4726bddSHONG Yifan)
169*d4726bddSHONG Yifan```
170*d4726bddSHONG Yifan
171*d4726bddSHONG YifanBuild and run `hello_world`:
172*d4726bddSHONG Yifan```
173*d4726bddSHONG Yifan$ bazel run //hello_world
174*d4726bddSHONG YifanINFO: Found 1 target...
175*d4726bddSHONG YifanTarget //examples/rust/hello_world:hello_world up-to-date:
176*d4726bddSHONG Yifanbazel-bin/examples/rust/hello_world/hello_world
177*d4726bddSHONG YifanINFO: Elapsed time: 1.308s, Critical Path: 1.22s
178*d4726bddSHONG Yifan
179*d4726bddSHONG YifanINFO: Running command line: bazel-bin/examples/rust/hello_world/hello_world
180*d4726bddSHONG YifanHello world
181*d4726bddSHONG Yifan```
182*d4726bddSHONG Yifan
183*d4726bddSHONG YifanOn Windows, a PDB file containing debugging information is available under
184*d4726bddSHONG Yifanthe key `pdb_file` in `OutputGroupInfo`. Similarly on macOS, a dSYM folder
185*d4726bddSHONG Yifanis available under the key `dsym_folder` in `OutputGroupInfo`.
186*d4726bddSHONG Yifan
187*d4726bddSHONG Yifan**ATTRIBUTES**
188*d4726bddSHONG Yifan
189*d4726bddSHONG Yifan
190*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
191*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
192*d4726bddSHONG Yifan| <a id="rust_binary-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
193*d4726bddSHONG Yifan| <a id="rust_binary-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
194*d4726bddSHONG Yifan| <a id="rust_binary-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
195*d4726bddSHONG Yifan| <a id="rust_binary-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
196*d4726bddSHONG Yifan| <a id="rust_binary-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
197*d4726bddSHONG Yifan| <a id="rust_binary-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
198*d4726bddSHONG Yifan| <a id="rust_binary-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
199*d4726bddSHONG Yifan| <a id="rust_binary-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
200*d4726bddSHONG Yifan| <a id="rust_binary-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
201*d4726bddSHONG Yifan| <a id="rust_binary-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
202*d4726bddSHONG Yifan| <a id="rust_binary-crate_type"></a>crate_type |  Crate type that will be passed to `rustc` to be used for building this crate.<br><br>This option is a temporary workaround and should be used only when building for WebAssembly targets (//rust/platform:wasi and //rust/platform:wasm).   | String | optional |  `"bin"`  |
203*d4726bddSHONG Yifan| <a id="rust_binary-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
204*d4726bddSHONG Yifan| <a id="rust_binary-env"></a>env |  Specifies additional environment variables to set when the target is executed by bazel run. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.<br><br>Execpath returns absolute path, and in order to be able to construct the absolute path we need to wrap the test binary in a launcher. Using a launcher comes with complications, such as more complicated debugger attachment.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
205*d4726bddSHONG Yifan| <a id="rust_binary-experimental_use_cc_common_link"></a>experimental_use_cc_common_link |  Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link.   | Integer | optional |  `-1`  |
206*d4726bddSHONG Yifan| <a id="rust_binary-linker_script"></a>linker_script |  Link script to forward into linker via rustc options.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
207*d4726bddSHONG Yifan| <a id="rust_binary-malloc"></a>malloc |  Override the default dependency on `malloc`.<br><br>By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `"@bazel_tools//tools/cpp:malloc"`  |
208*d4726bddSHONG Yifan| <a id="rust_binary-out_binary"></a>out_binary |  Force a target, regardless of it's `crate_type`, to always mark the file as executable. This attribute is only used to support wasm targets but is expected to be removed following a resolution to https://github.com/bazelbuild/rules_rust/issues/771.   | Boolean | optional |  `False`  |
209*d4726bddSHONG Yifan| <a id="rust_binary-platform"></a>platform |  Optional platform to transition the binary to.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
210*d4726bddSHONG Yifan| <a id="rust_binary-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
211*d4726bddSHONG Yifan| <a id="rust_binary-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
212*d4726bddSHONG Yifan| <a id="rust_binary-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
213*d4726bddSHONG Yifan| <a id="rust_binary-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
214*d4726bddSHONG Yifan| <a id="rust_binary-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `-1`  |
215*d4726bddSHONG Yifan| <a id="rust_binary-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
216*d4726bddSHONG Yifan
217*d4726bddSHONG Yifan
218*d4726bddSHONG Yifan<a id="rust_library"></a>
219*d4726bddSHONG Yifan
220*d4726bddSHONG Yifan## rust_library
221*d4726bddSHONG Yifan
222*d4726bddSHONG Yifan<pre>
223*d4726bddSHONG Yifanrust_library(<a href="#rust_library-name">name</a>, <a href="#rust_library-deps">deps</a>, <a href="#rust_library-srcs">srcs</a>, <a href="#rust_library-data">data</a>, <a href="#rust_library-aliases">aliases</a>, <a href="#rust_library-alwayslink">alwayslink</a>, <a href="#rust_library-compile_data">compile_data</a>, <a href="#rust_library-crate_features">crate_features</a>, <a href="#rust_library-crate_name">crate_name</a>,
224*d4726bddSHONG Yifan             <a href="#rust_library-crate_root">crate_root</a>, <a href="#rust_library-disable_pipelining">disable_pipelining</a>, <a href="#rust_library-edition">edition</a>, <a href="#rust_library-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_library-rustc_env">rustc_env</a>, <a href="#rust_library-rustc_env_files">rustc_env_files</a>,
225*d4726bddSHONG Yifan             <a href="#rust_library-rustc_flags">rustc_flags</a>, <a href="#rust_library-stamp">stamp</a>, <a href="#rust_library-version">version</a>)
226*d4726bddSHONG Yifan</pre>
227*d4726bddSHONG Yifan
228*d4726bddSHONG YifanBuilds a Rust library crate.
229*d4726bddSHONG Yifan
230*d4726bddSHONG YifanExample:
231*d4726bddSHONG Yifan
232*d4726bddSHONG YifanSuppose you have the following directory structure for a simple Rust library crate:
233*d4726bddSHONG Yifan
234*d4726bddSHONG Yifan```output
235*d4726bddSHONG Yifan[workspace]/
236*d4726bddSHONG Yifan    WORKSPACE
237*d4726bddSHONG Yifan    hello_lib/
238*d4726bddSHONG Yifan        BUILD
239*d4726bddSHONG Yifan        src/
240*d4726bddSHONG Yifan            greeter.rs
241*d4726bddSHONG Yifan            lib.rs
242*d4726bddSHONG Yifan```
243*d4726bddSHONG Yifan
244*d4726bddSHONG Yifan`hello_lib/src/greeter.rs`:
245*d4726bddSHONG Yifan```rust
246*d4726bddSHONG Yifanpub struct Greeter {
247*d4726bddSHONG Yifan    greeting: String,
248*d4726bddSHONG Yifan}
249*d4726bddSHONG Yifan
250*d4726bddSHONG Yifanimpl Greeter {
251*d4726bddSHONG Yifan    pub fn new(greeting: &str) -> Greeter {
252*d4726bddSHONG Yifan        Greeter { greeting: greeting.to_string(), }
253*d4726bddSHONG Yifan    }
254*d4726bddSHONG Yifan
255*d4726bddSHONG Yifan    pub fn greet(&self, thing: &str) {
256*d4726bddSHONG Yifan        println!("{} {}", &self.greeting, thing);
257*d4726bddSHONG Yifan    }
258*d4726bddSHONG Yifan}
259*d4726bddSHONG Yifan```
260*d4726bddSHONG Yifan
261*d4726bddSHONG Yifan`hello_lib/src/lib.rs`:
262*d4726bddSHONG Yifan
263*d4726bddSHONG Yifan```rust
264*d4726bddSHONG Yifanpub mod greeter;
265*d4726bddSHONG Yifan```
266*d4726bddSHONG Yifan
267*d4726bddSHONG Yifan`hello_lib/BUILD`:
268*d4726bddSHONG Yifan```python
269*d4726bddSHONG Yifanpackage(default_visibility = ["//visibility:public"])
270*d4726bddSHONG Yifan
271*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library")
272*d4726bddSHONG Yifan
273*d4726bddSHONG Yifanrust_library(
274*d4726bddSHONG Yifan    name = "hello_lib",
275*d4726bddSHONG Yifan    srcs = [
276*d4726bddSHONG Yifan        "src/greeter.rs",
277*d4726bddSHONG Yifan        "src/lib.rs",
278*d4726bddSHONG Yifan    ],
279*d4726bddSHONG Yifan)
280*d4726bddSHONG Yifan```
281*d4726bddSHONG Yifan
282*d4726bddSHONG YifanBuild the library:
283*d4726bddSHONG Yifan```output
284*d4726bddSHONG Yifan$ bazel build //hello_lib
285*d4726bddSHONG YifanINFO: Found 1 target...
286*d4726bddSHONG YifanTarget //examples/rust/hello_lib:hello_lib up-to-date:
287*d4726bddSHONG Yifanbazel-bin/examples/rust/hello_lib/libhello_lib.rlib
288*d4726bddSHONG YifanINFO: Elapsed time: 1.245s, Critical Path: 1.01s
289*d4726bddSHONG Yifan```
290*d4726bddSHONG Yifan
291*d4726bddSHONG Yifan**ATTRIBUTES**
292*d4726bddSHONG Yifan
293*d4726bddSHONG Yifan
294*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
295*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
296*d4726bddSHONG Yifan| <a id="rust_library-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
297*d4726bddSHONG Yifan| <a id="rust_library-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
298*d4726bddSHONG Yifan| <a id="rust_library-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
299*d4726bddSHONG Yifan| <a id="rust_library-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
300*d4726bddSHONG Yifan| <a id="rust_library-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
301*d4726bddSHONG Yifan| <a id="rust_library-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
302*d4726bddSHONG Yifan| <a id="rust_library-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
303*d4726bddSHONG Yifan| <a id="rust_library-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
304*d4726bddSHONG Yifan| <a id="rust_library-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
305*d4726bddSHONG Yifan| <a id="rust_library-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
306*d4726bddSHONG Yifan| <a id="rust_library-disable_pipelining"></a>disable_pipelining |  Disables pipelining for this rule if it is globally enabled. This will cause this rule to not produce a `.rmeta` file and all the dependent crates will instead use the `.rlib` file.   | Boolean | optional |  `False`  |
307*d4726bddSHONG Yifan| <a id="rust_library-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
308*d4726bddSHONG Yifan| <a id="rust_library-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
309*d4726bddSHONG Yifan| <a id="rust_library-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
310*d4726bddSHONG Yifan| <a id="rust_library-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
311*d4726bddSHONG Yifan| <a id="rust_library-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
312*d4726bddSHONG Yifan| <a id="rust_library-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `0`  |
313*d4726bddSHONG Yifan| <a id="rust_library-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
314*d4726bddSHONG Yifan
315*d4726bddSHONG Yifan
316*d4726bddSHONG Yifan<a id="rust_library_group"></a>
317*d4726bddSHONG Yifan
318*d4726bddSHONG Yifan## rust_library_group
319*d4726bddSHONG Yifan
320*d4726bddSHONG Yifan<pre>
321*d4726bddSHONG Yifanrust_library_group(<a href="#rust_library_group-name">name</a>, <a href="#rust_library_group-deps">deps</a>)
322*d4726bddSHONG Yifan</pre>
323*d4726bddSHONG Yifan
324*d4726bddSHONG YifanFunctions as an alias for a set of dependencies.
325*d4726bddSHONG Yifan
326*d4726bddSHONG YifanSpecifically, the following are equivalent:
327*d4726bddSHONG Yifan
328*d4726bddSHONG Yifan```starlark
329*d4726bddSHONG Yifanrust_library_group(
330*d4726bddSHONG Yifan    name = "crate_group",
331*d4726bddSHONG Yifan    deps = [
332*d4726bddSHONG Yifan        ":crate1",
333*d4726bddSHONG Yifan        ":crate2",
334*d4726bddSHONG Yifan    ],
335*d4726bddSHONG Yifan)
336*d4726bddSHONG Yifan
337*d4726bddSHONG Yifanrust_library(
338*d4726bddSHONG Yifan    name = "foobar",
339*d4726bddSHONG Yifan    deps = [":crate_group"],
340*d4726bddSHONG Yifan    ...
341*d4726bddSHONG Yifan)
342*d4726bddSHONG Yifan```
343*d4726bddSHONG Yifan
344*d4726bddSHONG Yifanand
345*d4726bddSHONG Yifan
346*d4726bddSHONG Yifan```starlark
347*d4726bddSHONG Yifanrust_library(
348*d4726bddSHONG Yifan    name = "foobar",
349*d4726bddSHONG Yifan    deps = [
350*d4726bddSHONG Yifan        ":crate1",
351*d4726bddSHONG Yifan        ":crate2",
352*d4726bddSHONG Yifan    ],
353*d4726bddSHONG Yifan    ...
354*d4726bddSHONG Yifan)
355*d4726bddSHONG Yifan```
356*d4726bddSHONG Yifan
357*d4726bddSHONG Yifan**ATTRIBUTES**
358*d4726bddSHONG Yifan
359*d4726bddSHONG Yifan
360*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
361*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
362*d4726bddSHONG Yifan| <a id="rust_library_group-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
363*d4726bddSHONG Yifan| <a id="rust_library_group-deps"></a>deps |  Other dependencies to forward through this crate group.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
364*d4726bddSHONG Yifan
365*d4726bddSHONG Yifan
366*d4726bddSHONG Yifan<a id="rust_proc_macro"></a>
367*d4726bddSHONG Yifan
368*d4726bddSHONG Yifan## rust_proc_macro
369*d4726bddSHONG Yifan
370*d4726bddSHONG Yifan<pre>
371*d4726bddSHONG Yifanrust_proc_macro(<a href="#rust_proc_macro-name">name</a>, <a href="#rust_proc_macro-deps">deps</a>, <a href="#rust_proc_macro-srcs">srcs</a>, <a href="#rust_proc_macro-data">data</a>, <a href="#rust_proc_macro-aliases">aliases</a>, <a href="#rust_proc_macro-alwayslink">alwayslink</a>, <a href="#rust_proc_macro-compile_data">compile_data</a>, <a href="#rust_proc_macro-crate_features">crate_features</a>,
372*d4726bddSHONG Yifan                <a href="#rust_proc_macro-crate_name">crate_name</a>, <a href="#rust_proc_macro-crate_root">crate_root</a>, <a href="#rust_proc_macro-edition">edition</a>, <a href="#rust_proc_macro-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_proc_macro-rustc_env">rustc_env</a>, <a href="#rust_proc_macro-rustc_env_files">rustc_env_files</a>,
373*d4726bddSHONG Yifan                <a href="#rust_proc_macro-rustc_flags">rustc_flags</a>, <a href="#rust_proc_macro-stamp">stamp</a>, <a href="#rust_proc_macro-version">version</a>)
374*d4726bddSHONG Yifan</pre>
375*d4726bddSHONG Yifan
376*d4726bddSHONG YifanBuilds a Rust proc-macro crate.
377*d4726bddSHONG Yifan
378*d4726bddSHONG Yifan**ATTRIBUTES**
379*d4726bddSHONG Yifan
380*d4726bddSHONG Yifan
381*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
382*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
383*d4726bddSHONG Yifan| <a id="rust_proc_macro-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
384*d4726bddSHONG Yifan| <a id="rust_proc_macro-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
385*d4726bddSHONG Yifan| <a id="rust_proc_macro-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
386*d4726bddSHONG Yifan| <a id="rust_proc_macro-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
387*d4726bddSHONG Yifan| <a id="rust_proc_macro-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
388*d4726bddSHONG Yifan| <a id="rust_proc_macro-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
389*d4726bddSHONG Yifan| <a id="rust_proc_macro-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
390*d4726bddSHONG Yifan| <a id="rust_proc_macro-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
391*d4726bddSHONG Yifan| <a id="rust_proc_macro-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
392*d4726bddSHONG Yifan| <a id="rust_proc_macro-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
393*d4726bddSHONG Yifan| <a id="rust_proc_macro-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
394*d4726bddSHONG Yifan| <a id="rust_proc_macro-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
395*d4726bddSHONG Yifan| <a id="rust_proc_macro-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
396*d4726bddSHONG Yifan| <a id="rust_proc_macro-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
397*d4726bddSHONG Yifan| <a id="rust_proc_macro-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
398*d4726bddSHONG Yifan| <a id="rust_proc_macro-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `0`  |
399*d4726bddSHONG Yifan| <a id="rust_proc_macro-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
400*d4726bddSHONG Yifan
401*d4726bddSHONG Yifan
402*d4726bddSHONG Yifan<a id="rust_shared_library"></a>
403*d4726bddSHONG Yifan
404*d4726bddSHONG Yifan## rust_shared_library
405*d4726bddSHONG Yifan
406*d4726bddSHONG Yifan<pre>
407*d4726bddSHONG Yifanrust_shared_library(<a href="#rust_shared_library-name">name</a>, <a href="#rust_shared_library-deps">deps</a>, <a href="#rust_shared_library-srcs">srcs</a>, <a href="#rust_shared_library-data">data</a>, <a href="#rust_shared_library-aliases">aliases</a>, <a href="#rust_shared_library-alwayslink">alwayslink</a>, <a href="#rust_shared_library-compile_data">compile_data</a>, <a href="#rust_shared_library-crate_features">crate_features</a>,
408*d4726bddSHONG Yifan                    <a href="#rust_shared_library-crate_name">crate_name</a>, <a href="#rust_shared_library-crate_root">crate_root</a>, <a href="#rust_shared_library-edition">edition</a>, <a href="#rust_shared_library-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_shared_library-malloc">malloc</a>,
409*d4726bddSHONG Yifan                    <a href="#rust_shared_library-platform">platform</a>, <a href="#rust_shared_library-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_shared_library-rustc_env">rustc_env</a>, <a href="#rust_shared_library-rustc_env_files">rustc_env_files</a>, <a href="#rust_shared_library-rustc_flags">rustc_flags</a>, <a href="#rust_shared_library-stamp">stamp</a>,
410*d4726bddSHONG Yifan                    <a href="#rust_shared_library-version">version</a>)
411*d4726bddSHONG Yifan</pre>
412*d4726bddSHONG Yifan
413*d4726bddSHONG YifanBuilds a Rust shared library.
414*d4726bddSHONG Yifan
415*d4726bddSHONG YifanThis shared library will contain all transitively reachable crates and native objects.
416*d4726bddSHONG YifanIt is meant to be used when producing an artifact that is then consumed by some other build system
417*d4726bddSHONG Yifan(for example to produce a shared library that Python program links against).
418*d4726bddSHONG Yifan
419*d4726bddSHONG YifanThis rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`.
420*d4726bddSHONG Yifan
421*d4726bddSHONG YifanWhen building the whole binary in Bazel, use `rust_library` instead.
422*d4726bddSHONG Yifan
423*d4726bddSHONG Yifan**ATTRIBUTES**
424*d4726bddSHONG Yifan
425*d4726bddSHONG Yifan
426*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
427*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
428*d4726bddSHONG Yifan| <a id="rust_shared_library-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
429*d4726bddSHONG Yifan| <a id="rust_shared_library-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
430*d4726bddSHONG Yifan| <a id="rust_shared_library-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
431*d4726bddSHONG Yifan| <a id="rust_shared_library-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
432*d4726bddSHONG Yifan| <a id="rust_shared_library-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
433*d4726bddSHONG Yifan| <a id="rust_shared_library-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
434*d4726bddSHONG Yifan| <a id="rust_shared_library-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
435*d4726bddSHONG Yifan| <a id="rust_shared_library-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
436*d4726bddSHONG Yifan| <a id="rust_shared_library-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
437*d4726bddSHONG Yifan| <a id="rust_shared_library-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
438*d4726bddSHONG Yifan| <a id="rust_shared_library-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
439*d4726bddSHONG Yifan| <a id="rust_shared_library-experimental_use_cc_common_link"></a>experimental_use_cc_common_link |  Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link.   | Integer | optional |  `-1`  |
440*d4726bddSHONG Yifan| <a id="rust_shared_library-malloc"></a>malloc |  Override the default dependency on `malloc`.<br><br>By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `"@bazel_tools//tools/cpp:malloc"`  |
441*d4726bddSHONG Yifan| <a id="rust_shared_library-platform"></a>platform |  Optional platform to transition the shared library to.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
442*d4726bddSHONG Yifan| <a id="rust_shared_library-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
443*d4726bddSHONG Yifan| <a id="rust_shared_library-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
444*d4726bddSHONG Yifan| <a id="rust_shared_library-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
445*d4726bddSHONG Yifan| <a id="rust_shared_library-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
446*d4726bddSHONG Yifan| <a id="rust_shared_library-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `0`  |
447*d4726bddSHONG Yifan| <a id="rust_shared_library-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
448*d4726bddSHONG Yifan
449*d4726bddSHONG Yifan
450*d4726bddSHONG Yifan<a id="rust_static_library"></a>
451*d4726bddSHONG Yifan
452*d4726bddSHONG Yifan## rust_static_library
453*d4726bddSHONG Yifan
454*d4726bddSHONG Yifan<pre>
455*d4726bddSHONG Yifanrust_static_library(<a href="#rust_static_library-name">name</a>, <a href="#rust_static_library-deps">deps</a>, <a href="#rust_static_library-srcs">srcs</a>, <a href="#rust_static_library-data">data</a>, <a href="#rust_static_library-aliases">aliases</a>, <a href="#rust_static_library-alwayslink">alwayslink</a>, <a href="#rust_static_library-compile_data">compile_data</a>, <a href="#rust_static_library-crate_features">crate_features</a>,
456*d4726bddSHONG Yifan                    <a href="#rust_static_library-crate_name">crate_name</a>, <a href="#rust_static_library-crate_root">crate_root</a>, <a href="#rust_static_library-edition">edition</a>, <a href="#rust_static_library-platform">platform</a>, <a href="#rust_static_library-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_static_library-rustc_env">rustc_env</a>,
457*d4726bddSHONG Yifan                    <a href="#rust_static_library-rustc_env_files">rustc_env_files</a>, <a href="#rust_static_library-rustc_flags">rustc_flags</a>, <a href="#rust_static_library-stamp">stamp</a>, <a href="#rust_static_library-version">version</a>)
458*d4726bddSHONG Yifan</pre>
459*d4726bddSHONG Yifan
460*d4726bddSHONG YifanBuilds a Rust static library.
461*d4726bddSHONG Yifan
462*d4726bddSHONG YifanThis static library will contain all transitively reachable crates and native objects.
463*d4726bddSHONG YifanIt is meant to be used when producing an artifact that is then consumed by some other build system
464*d4726bddSHONG Yifan(for example to produce an archive that Python program links against).
465*d4726bddSHONG Yifan
466*d4726bddSHONG YifanThis rule provides CcInfo, so it can be used everywhere Bazel expects `rules_cc`.
467*d4726bddSHONG Yifan
468*d4726bddSHONG YifanWhen building the whole binary in Bazel, use `rust_library` instead.
469*d4726bddSHONG Yifan
470*d4726bddSHONG Yifan**ATTRIBUTES**
471*d4726bddSHONG Yifan
472*d4726bddSHONG Yifan
473*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
474*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
475*d4726bddSHONG Yifan| <a id="rust_static_library-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
476*d4726bddSHONG Yifan| <a id="rust_static_library-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
477*d4726bddSHONG Yifan| <a id="rust_static_library-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
478*d4726bddSHONG Yifan| <a id="rust_static_library-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
479*d4726bddSHONG Yifan| <a id="rust_static_library-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
480*d4726bddSHONG Yifan| <a id="rust_static_library-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
481*d4726bddSHONG Yifan| <a id="rust_static_library-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
482*d4726bddSHONG Yifan| <a id="rust_static_library-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
483*d4726bddSHONG Yifan| <a id="rust_static_library-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
484*d4726bddSHONG Yifan| <a id="rust_static_library-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
485*d4726bddSHONG Yifan| <a id="rust_static_library-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
486*d4726bddSHONG Yifan| <a id="rust_static_library-platform"></a>platform |  Optional platform to transition the static library to.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
487*d4726bddSHONG Yifan| <a id="rust_static_library-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
488*d4726bddSHONG Yifan| <a id="rust_static_library-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
489*d4726bddSHONG Yifan| <a id="rust_static_library-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
490*d4726bddSHONG Yifan| <a id="rust_static_library-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
491*d4726bddSHONG Yifan| <a id="rust_static_library-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `0`  |
492*d4726bddSHONG Yifan| <a id="rust_static_library-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
493*d4726bddSHONG Yifan
494*d4726bddSHONG Yifan
495*d4726bddSHONG Yifan<a id="rust_test"></a>
496*d4726bddSHONG Yifan
497*d4726bddSHONG Yifan## rust_test
498*d4726bddSHONG Yifan
499*d4726bddSHONG Yifan<pre>
500*d4726bddSHONG Yifanrust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-deps">deps</a>, <a href="#rust_test-srcs">srcs</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-alwayslink">alwayslink</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>,
501*d4726bddSHONG Yifan          <a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_test-malloc">malloc</a>, <a href="#rust_test-platform">platform</a>,
502*d4726bddSHONG Yifan          <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-stamp">stamp</a>, <a href="#rust_test-use_libtest_harness">use_libtest_harness</a>,
503*d4726bddSHONG Yifan          <a href="#rust_test-version">version</a>)
504*d4726bddSHONG Yifan</pre>
505*d4726bddSHONG Yifan
506*d4726bddSHONG YifanBuilds a Rust test crate.
507*d4726bddSHONG Yifan
508*d4726bddSHONG YifanExamples:
509*d4726bddSHONG Yifan
510*d4726bddSHONG YifanSuppose you have the following directory structure for a Rust library crate         with unit test code in the library sources:
511*d4726bddSHONG Yifan
512*d4726bddSHONG Yifan```output
513*d4726bddSHONG Yifan[workspace]/
514*d4726bddSHONG Yifan    WORKSPACE
515*d4726bddSHONG Yifan    hello_lib/
516*d4726bddSHONG Yifan        BUILD
517*d4726bddSHONG Yifan        src/
518*d4726bddSHONG Yifan            lib.rs
519*d4726bddSHONG Yifan```
520*d4726bddSHONG Yifan
521*d4726bddSHONG Yifan`hello_lib/src/lib.rs`:
522*d4726bddSHONG Yifan```rust
523*d4726bddSHONG Yifanpub struct Greeter {
524*d4726bddSHONG Yifan    greeting: String,
525*d4726bddSHONG Yifan}
526*d4726bddSHONG Yifan
527*d4726bddSHONG Yifanimpl Greeter {
528*d4726bddSHONG Yifan    pub fn new(greeting: &str) -> Greeter {
529*d4726bddSHONG Yifan        Greeter { greeting: greeting.to_string(), }
530*d4726bddSHONG Yifan    }
531*d4726bddSHONG Yifan
532*d4726bddSHONG Yifan    pub fn greet(&self, thing: &str) -> String {
533*d4726bddSHONG Yifan        format!("{} {}", &self.greeting, thing)
534*d4726bddSHONG Yifan    }
535*d4726bddSHONG Yifan}
536*d4726bddSHONG Yifan
537*d4726bddSHONG Yifan#[cfg(test)]
538*d4726bddSHONG Yifanmod test {
539*d4726bddSHONG Yifan    use super::Greeter;
540*d4726bddSHONG Yifan
541*d4726bddSHONG Yifan    #[test]
542*d4726bddSHONG Yifan    fn test_greeting() {
543*d4726bddSHONG Yifan        let hello = Greeter::new("Hi");
544*d4726bddSHONG Yifan        assert_eq!("Hi Rust", hello.greet("Rust"));
545*d4726bddSHONG Yifan    }
546*d4726bddSHONG Yifan}
547*d4726bddSHONG Yifan```
548*d4726bddSHONG Yifan
549*d4726bddSHONG YifanTo build and run the tests, simply add a `rust_test` rule with no `srcs`
550*d4726bddSHONG Yifanand only depends on the `hello_lib` `rust_library` target via the
551*d4726bddSHONG Yifan`crate` attribute:
552*d4726bddSHONG Yifan
553*d4726bddSHONG Yifan`hello_lib/BUILD`:
554*d4726bddSHONG Yifan```python
555*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
556*d4726bddSHONG Yifan
557*d4726bddSHONG Yifanrust_library(
558*d4726bddSHONG Yifan    name = "hello_lib",
559*d4726bddSHONG Yifan    srcs = ["src/lib.rs"],
560*d4726bddSHONG Yifan)
561*d4726bddSHONG Yifan
562*d4726bddSHONG Yifanrust_test(
563*d4726bddSHONG Yifan    name = "hello_lib_test",
564*d4726bddSHONG Yifan    crate = ":hello_lib",
565*d4726bddSHONG Yifan    # You may add other deps that are specific to the test configuration
566*d4726bddSHONG Yifan    deps = ["//some/dev/dep"],
567*d4726bddSHONG Yifan)
568*d4726bddSHONG Yifan```
569*d4726bddSHONG Yifan
570*d4726bddSHONG YifanRun the test with `bazel test //hello_lib:hello_lib_test`. The crate
571*d4726bddSHONG Yifanwill be built using the same crate name as the underlying ":hello_lib"
572*d4726bddSHONG Yifancrate.
573*d4726bddSHONG Yifan
574*d4726bddSHONG Yifan### Example: `test` directory
575*d4726bddSHONG Yifan
576*d4726bddSHONG YifanIntegration tests that live in the [`tests` directory][int-tests], 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:
577*d4726bddSHONG Yifan
578*d4726bddSHONG Yifan[int-tests]: http://doc.rust-lang.org/book/testing.html#the-tests-directory
579*d4726bddSHONG Yifan
580*d4726bddSHONG Yifan```output
581*d4726bddSHONG Yifan[workspace]/
582*d4726bddSHONG Yifan    WORKSPACE
583*d4726bddSHONG Yifan    hello_lib/
584*d4726bddSHONG Yifan        BUILD
585*d4726bddSHONG Yifan        src/
586*d4726bddSHONG Yifan            lib.rs
587*d4726bddSHONG Yifan        tests/
588*d4726bddSHONG Yifan            greeting.rs
589*d4726bddSHONG Yifan```
590*d4726bddSHONG Yifan
591*d4726bddSHONG Yifan`hello_lib/tests/greeting.rs`:
592*d4726bddSHONG Yifan```rust
593*d4726bddSHONG Yifanextern crate hello_lib;
594*d4726bddSHONG Yifan
595*d4726bddSHONG Yifanuse hello_lib;
596*d4726bddSHONG Yifan
597*d4726bddSHONG Yifan#[test]
598*d4726bddSHONG Yifanfn test_greeting() {
599*d4726bddSHONG Yifan    let hello = greeter::Greeter::new("Hello");
600*d4726bddSHONG Yifan    assert_eq!("Hello world", hello.greeting("world"));
601*d4726bddSHONG Yifan}
602*d4726bddSHONG Yifan```
603*d4726bddSHONG Yifan
604*d4726bddSHONG YifanTo build the `greeting.rs` integration test, simply add a `rust_test` target
605*d4726bddSHONG Yifanwith `greeting.rs` in `srcs` and a dependency on the `hello_lib` target:
606*d4726bddSHONG Yifan
607*d4726bddSHONG Yifan`hello_lib/BUILD`:
608*d4726bddSHONG Yifan```python
609*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
610*d4726bddSHONG Yifan
611*d4726bddSHONG Yifanrust_library(
612*d4726bddSHONG Yifan    name = "hello_lib",
613*d4726bddSHONG Yifan    srcs = ["src/lib.rs"],
614*d4726bddSHONG Yifan)
615*d4726bddSHONG Yifan
616*d4726bddSHONG Yifanrust_test(
617*d4726bddSHONG Yifan    name = "greeting_test",
618*d4726bddSHONG Yifan    srcs = ["tests/greeting.rs"],
619*d4726bddSHONG Yifan    deps = [":hello_lib"],
620*d4726bddSHONG Yifan)
621*d4726bddSHONG Yifan```
622*d4726bddSHONG Yifan
623*d4726bddSHONG YifanRun the test with `bazel test //hello_lib:greeting_test`.
624*d4726bddSHONG Yifan
625*d4726bddSHONG Yifan**ATTRIBUTES**
626*d4726bddSHONG Yifan
627*d4726bddSHONG Yifan
628*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
629*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
630*d4726bddSHONG Yifan| <a id="rust_test-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
631*d4726bddSHONG Yifan| <a id="rust_test-deps"></a>deps |  List of other libraries to be linked to this library target.<br><br>These can be either other `rust_library` targets or `cc_library` targets if linking a native library.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
632*d4726bddSHONG Yifan| <a id="rust_test-srcs"></a>srcs |  List of Rust `.rs` source files used to build the library.<br><br>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.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
633*d4726bddSHONG Yifan| <a id="rust_test-data"></a>data |  List of files used by this rule at compile time and runtime.<br><br>If including data at compile time with include_str!() and similar, prefer `compile_data` over `data`, to prevent the data also being included in the runfiles.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
634*d4726bddSHONG Yifan| <a id="rust_test-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target<br><br>These are other `rust_library` targets and will be presented as the new name given.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: Label -> String</a> | optional |  `{}`  |
635*d4726bddSHONG Yifan| <a id="rust_test-alwayslink"></a>alwayslink |  If 1, any binary that depends (directly or indirectly) on this library will link in all the object files even if some contain no symbols referenced by the binary.<br><br>This attribute is used by the C++ Starlark API when passing CcInfo providers.   | Boolean | optional |  `False`  |
636*d4726bddSHONG Yifan| <a id="rust_test-compile_data"></a>compile_data |  List of files used by this rule at compile time.<br><br>This attribute can be used to specify any data files that are embedded into the library, such as via the [`include_str!`](https://doc.rust-lang.org/std/macro.include_str!.html) macro.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
637*d4726bddSHONG Yifan| <a id="rust_test-crate"></a>crate |  Target inline tests declared in the given crate<br><br>These tests are typically those that would be held out under `#[cfg(test)]` declarations.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
638*d4726bddSHONG Yifan| <a id="rust_test-crate_features"></a>crate_features |  List of features to enable for this crate.<br><br>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 |  `[]`  |
639*d4726bddSHONG Yifan| <a id="rust_test-crate_name"></a>crate_name |  Crate name to use for this target.<br><br>This must be a valid Rust identifier, i.e. it may contain only alphanumeric characters and underscores. Defaults to the target name, with any hyphens replaced by underscores.   | String | optional |  `""`  |
640*d4726bddSHONG Yifan| <a id="rust_test-crate_root"></a>crate_root |  The file that will be passed to `rustc` to be used for building this crate.<br><br>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.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
641*d4726bddSHONG Yifan| <a id="rust_test-edition"></a>edition |  The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain.   | String | optional |  `""`  |
642*d4726bddSHONG Yifan| <a id="rust_test-env"></a>env |  Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
643*d4726bddSHONG Yifan| <a id="rust_test-experimental_use_cc_common_link"></a>experimental_use_cc_common_link |  Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link.   | Integer | optional |  `-1`  |
644*d4726bddSHONG Yifan| <a id="rust_test-malloc"></a>malloc |  Override the default dependency on `malloc`.<br><br>By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `"@bazel_tools//tools/cpp:malloc"`  |
645*d4726bddSHONG Yifan| <a id="rust_test-platform"></a>platform |  Optional platform to transition the test to.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
646*d4726bddSHONG Yifan| <a id="rust_test-proc_macro_deps"></a>proc_macro_deps |  List of `rust_proc_macro` targets used to help build this library target.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
647*d4726bddSHONG Yifan| <a id="rust_test-rustc_env"></a>rustc_env |  Dictionary of additional `"key": "value"` environment variables to set for rustc.<br><br>rust_test()/rust_binary() rules can use $(rootpath //package:target) to pass in the location of a generated file or external tool. Cargo build scripts that wish to expand locations should use cargo_build_script()'s build_script_env argument instead, as build scripts are run in a different environment - see cargo_build_script()'s documentation for more.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
648*d4726bddSHONG Yifan| <a id="rust_test-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc.<br><br>These files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).<br><br>The order that these files will be processed is unspecified, so multiple definitions of a particular variable are discouraged.<br><br>Note that the variables here are subject to [workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status) stamping should the `stamp` attribute be enabled. Stamp variables should be wrapped in brackets in order to be resolved. E.g. `NAME={WORKSPACE_STATUS_VARIABLE}`.   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
649*d4726bddSHONG Yifan| <a id="rust_test-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.<br><br>These strings are subject to Make variable expansion for predefined source/output path variables like `$location`, `$execpath`, and `$rootpath`. This expansion is useful if you wish to pass a generated file of arguments to rustc: `@$(location //package:target)`.   | List of strings | optional |  `[]`  |
650*d4726bddSHONG Yifan| <a id="rust_test-stamp"></a>stamp |  Whether to encode build information into the `Rustc` action. Possible values:<br><br>- `stamp = 1`: Always stamp the build information into the `Rustc` action, even in             [--nostamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) builds.             This setting should be avoided, since it potentially kills remote caching for the target and             any downstream actions that depend on it.<br><br>- `stamp = 0`: Always replace build information by constant values. This gives good build result caching.<br><br>- `stamp = -1`: Embedding of build information is controlled by the             [--[no]stamp](https://docs.bazel.build/versions/main/user-manual.html#flag--stamp) flag.<br><br>Stamped targets are not rebuilt unless their dependencies change.<br><br>For example if a `rust_library` is stamped, and a `rust_binary` depends on that library, the stamped library won't be rebuilt when we change sources of the `rust_binary`. This is different from how [`cc_library.linkstamps`](https://docs.bazel.build/versions/main/be/c-cpp.html#cc_library.linkstamp) behaves.   | Integer | optional |  `0`  |
651*d4726bddSHONG Yifan| <a id="rust_test-use_libtest_harness"></a>use_libtest_harness |  Whether to use `libtest`. For targets using this flag, individual tests can be run by using the [--test_arg](https://docs.bazel.build/versions/4.0.0/command-line-reference.html#flag--test_arg) flag. E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`.   | Boolean | optional |  `True`  |
652*d4726bddSHONG Yifan| <a id="rust_test-version"></a>version |  A version to inject in the cargo environment variable.   | String | optional |  `"0.0.0"`  |
653*d4726bddSHONG Yifan
654*d4726bddSHONG Yifan
655*d4726bddSHONG Yifan<a id="rust_test_suite"></a>
656*d4726bddSHONG Yifan
657*d4726bddSHONG Yifan## rust_test_suite
658*d4726bddSHONG Yifan
659*d4726bddSHONG Yifan<pre>
660*d4726bddSHONG Yifanrust_test_suite(<a href="#rust_test_suite-name">name</a>, <a href="#rust_test_suite-srcs">srcs</a>, <a href="#rust_test_suite-shared_srcs">shared_srcs</a>, <a href="#rust_test_suite-kwargs">kwargs</a>)
661*d4726bddSHONG Yifan</pre>
662*d4726bddSHONG Yifan
663*d4726bddSHONG YifanA rule for creating a test suite for a set of `rust_test` targets.
664*d4726bddSHONG Yifan
665*d4726bddSHONG YifanThis rule can be used for setting up typical rust [integration tests][it]. Given the following
666*d4726bddSHONG Yifandirectory structure:
667*d4726bddSHONG Yifan
668*d4726bddSHONG Yifan```text
669*d4726bddSHONG Yifan[crate]/
670*d4726bddSHONG Yifan    BUILD.bazel
671*d4726bddSHONG Yifan    src/
672*d4726bddSHONG Yifan        lib.rs
673*d4726bddSHONG Yifan        main.rs
674*d4726bddSHONG Yifan    tests/
675*d4726bddSHONG Yifan        integrated_test_a.rs
676*d4726bddSHONG Yifan        integrated_test_b.rs
677*d4726bddSHONG Yifan        integrated_test_c.rs
678*d4726bddSHONG Yifan        patterns/
679*d4726bddSHONG Yifan            fibonacci_test.rs
680*d4726bddSHONG Yifan        helpers/
681*d4726bddSHONG Yifan            mod.rs
682*d4726bddSHONG Yifan```
683*d4726bddSHONG Yifan
684*d4726bddSHONG YifanThe rule can be used to generate [rust_test](#rust_test) targets for each source file under `tests`
685*d4726bddSHONG Yifanand a [test_suite][ts] which encapsulates all tests.
686*d4726bddSHONG Yifan
687*d4726bddSHONG Yifan```python
688*d4726bddSHONG Yifanload("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test_suite")
689*d4726bddSHONG Yifan
690*d4726bddSHONG Yifanrust_library(
691*d4726bddSHONG Yifan    name = "math_lib",
692*d4726bddSHONG Yifan    srcs = ["src/lib.rs"],
693*d4726bddSHONG Yifan)
694*d4726bddSHONG Yifan
695*d4726bddSHONG Yifanrust_binary(
696*d4726bddSHONG Yifan    name = "math_bin",
697*d4726bddSHONG Yifan    srcs = ["src/main.rs"],
698*d4726bddSHONG Yifan)
699*d4726bddSHONG Yifan
700*d4726bddSHONG Yifanrust_test_suite(
701*d4726bddSHONG Yifan    name = "integrated_tests_suite",
702*d4726bddSHONG Yifan    srcs = glob(["tests/**"]),
703*d4726bddSHONG Yifan    shared_srcs=glob(["tests/helpers/**"]),
704*d4726bddSHONG Yifan    deps = [":math_lib"],
705*d4726bddSHONG Yifan)
706*d4726bddSHONG Yifan```
707*d4726bddSHONG Yifan
708*d4726bddSHONG Yifan[it]: https://doc.rust-lang.org/rust-by-example/testing/integration_testing.html
709*d4726bddSHONG Yifan[ts]: https://docs.bazel.build/versions/master/be/general.html#test_suite
710*d4726bddSHONG Yifan
711*d4726bddSHONG Yifan
712*d4726bddSHONG Yifan**PARAMETERS**
713*d4726bddSHONG Yifan
714*d4726bddSHONG Yifan
715*d4726bddSHONG Yifan| Name  | Description | Default Value |
716*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
717*d4726bddSHONG Yifan| <a id="rust_test_suite-name"></a>name |  The name of the `test_suite`.   |  none |
718*d4726bddSHONG Yifan| <a id="rust_test_suite-srcs"></a>srcs |  All test sources, typically `glob(["tests/**/*.rs"])`.   |  none |
719*d4726bddSHONG Yifan| <a id="rust_test_suite-shared_srcs"></a>shared_srcs |  Optional argument for sources shared among tests, typically helper functions.   |  `[]` |
720*d4726bddSHONG Yifan| <a id="rust_test_suite-kwargs"></a>kwargs |  Additional keyword arguments for the underyling [rust_test](#rust_test) targets. The `tags` argument is also passed to the generated `test_suite` target.   |  none |
721*d4726bddSHONG Yifan
722*d4726bddSHONG Yifan
723