xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/crate_universe.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan
3*d4726bddSHONG Yifan# Crate Universe
4*d4726bddSHONG Yifan
5*d4726bddSHONG YifanCrate Universe is a set of Bazel rule for generating Rust targets using Cargo.
6*d4726bddSHONG Yifan
7*d4726bddSHONG YifanThis doc describes using crate_universe from a WORKSPACE file.
8*d4726bddSHONG Yifan
9*d4726bddSHONG YifanIf you're using bzlmod, please see [the bzlmod equivalent of this doc](crate_universe_bzlmod.html).
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan## Setup
12*d4726bddSHONG Yifan
13*d4726bddSHONG YifanAfter loading `rules_rust` in your workspace, set the following to begin using `crate_universe`:
14*d4726bddSHONG Yifan
15*d4726bddSHONG Yifan```python
16*d4726bddSHONG Yifanload("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
17*d4726bddSHONG Yifan
18*d4726bddSHONG Yifancrate_universe_dependencies()
19*d4726bddSHONG Yifan```
20*d4726bddSHONG Yifan
21*d4726bddSHONG YifanNote that if the current version of `rules_rust` is not a release artifact, you may need to set additional
22*d4726bddSHONG Yifanflags such as [`bootstrap = True`](#crate_universe_dependencies-bootstrap) on the `crate_universe_dependencies`
23*d4726bddSHONG Yifancall above or [crates_repository::generator_urls](#crates_repository-generator_urls) in uses of `crates_repository`.
24*d4726bddSHONG Yifan
25*d4726bddSHONG Yifan## Rules
26*d4726bddSHONG Yifan
27*d4726bddSHONG Yifan- [crates_repository](#crates_repository)
28*d4726bddSHONG Yifan- [crates_vendor](#crates_vendor)
29*d4726bddSHONG Yifan
30*d4726bddSHONG Yifan## Utility Macros
31*d4726bddSHONG Yifan
32*d4726bddSHONG Yifan- [crate_universe_dependencies](#crate_universe_dependencies)
33*d4726bddSHONG Yifan- [crate.annotation](#crateannotation)
34*d4726bddSHONG Yifan- [crate.select](#crateselect)
35*d4726bddSHONG Yifan- [crate.spec](#cratespec)
36*d4726bddSHONG Yifan- [crate.workspace_member](#crateworkspace_member)
37*d4726bddSHONG Yifan- [render_config](#render_config)
38*d4726bddSHONG Yifan- [splicing_config](#splicing_config)
39*d4726bddSHONG Yifan
40*d4726bddSHONG Yifan## Workflows
41*d4726bddSHONG Yifan
42*d4726bddSHONG YifanThe [`crates_repository`](#crates_repository) rule (the primary repository rule of `rules_rust`'s cargo support) supports a number of different
43*d4726bddSHONG Yifanways users can express and organize their dependencies. The most common are listed below though there are more to be found in
44*d4726bddSHONG Yifanthe [./examples/crate_universe](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe) directory.
45*d4726bddSHONG Yifan
46*d4726bddSHONG Yifan### Cargo Workspaces
47*d4726bddSHONG Yifan
48*d4726bddSHONG YifanOne of the simpler ways to wire up dependencies would be to first structure your project into a [Cargo workspace][cw].
49*d4726bddSHONG YifanThe `crates_repository` rule can ingest a root `Cargo.toml` file and generate dependencies from there.
50*d4726bddSHONG Yifan
51*d4726bddSHONG Yifan```python
52*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crates_repository")
53*d4726bddSHONG Yifan
54*d4726bddSHONG Yifancrates_repository(
55*d4726bddSHONG Yifan    name = "crate_index",
56*d4726bddSHONG Yifan    cargo_lockfile = "//:Cargo.lock",
57*d4726bddSHONG Yifan    lockfile = "//:Cargo.Bazel.lock",
58*d4726bddSHONG Yifan    manifests = ["//:Cargo.toml"],
59*d4726bddSHONG Yifan)
60*d4726bddSHONG Yifan
61*d4726bddSHONG Yifanload("@crate_index//:defs.bzl", "crate_repositories")
62*d4726bddSHONG Yifan
63*d4726bddSHONG Yifancrate_repositories()
64*d4726bddSHONG Yifan```
65*d4726bddSHONG Yifan
66*d4726bddSHONG YifanThe generated `crates_repository` contains helper macros which make collecting dependencies for Bazel targets simpler.
67*d4726bddSHONG YifanNotably, the `all_crate_deps` and `aliases` macros (see [Dependencies API](#dependencies-api)) commonly allow the
68*d4726bddSHONG Yifan`Cargo.toml` files to be the single source of truth for dependencies. Since these macros come from the generated
69*d4726bddSHONG Yifanrepository, the dependencies and alias definitions they return will automatically update BUILD targets.
70*d4726bddSHONG Yifan
71*d4726bddSHONG Yifan```python
72*d4726bddSHONG Yifanload("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
73*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
74*d4726bddSHONG Yifan
75*d4726bddSHONG Yifanrust_library(
76*d4726bddSHONG Yifan    name = "lib",
77*d4726bddSHONG Yifan    aliases = aliases(),
78*d4726bddSHONG Yifan    deps = all_crate_deps(
79*d4726bddSHONG Yifan        normal = True,
80*d4726bddSHONG Yifan    ),
81*d4726bddSHONG Yifan    proc_macro_deps = all_crate_deps(
82*d4726bddSHONG Yifan        proc_macro = True,
83*d4726bddSHONG Yifan    ),
84*d4726bddSHONG Yifan)
85*d4726bddSHONG Yifan
86*d4726bddSHONG Yifanrust_test(
87*d4726bddSHONG Yifan    name = "unit_test",
88*d4726bddSHONG Yifan    crate = ":lib",
89*d4726bddSHONG Yifan    aliases = aliases(
90*d4726bddSHONG Yifan        normal_dev = True,
91*d4726bddSHONG Yifan        proc_macro_dev = True,
92*d4726bddSHONG Yifan    ),
93*d4726bddSHONG Yifan    deps = all_crate_deps(
94*d4726bddSHONG Yifan        normal_dev = True,
95*d4726bddSHONG Yifan    ),
96*d4726bddSHONG Yifan    proc_macro_deps = all_crate_deps(
97*d4726bddSHONG Yifan        proc_macro_dev = True,
98*d4726bddSHONG Yifan    ),
99*d4726bddSHONG Yifan)
100*d4726bddSHONG Yifan```
101*d4726bddSHONG Yifan
102*d4726bddSHONG Yifan### Direct Packages
103*d4726bddSHONG Yifan
104*d4726bddSHONG YifanIn cases where Rust targets have heavy interractions with other Bazel targests ([Cc][cc], [Proto][proto], etc.),
105*d4726bddSHONG Yifanmaintaining `Cargo.toml` files may have deminishing returns as things like [rust-analyzer][ra] begin to be confused
106*d4726bddSHONG Yifanabout missing targets or environment variables defined only in Bazel. In workspaces like this, it may be desirable
107*d4726bddSHONG Yifanto have a "Cargo free" setup. `crates_repository` supports this through the `packages` attribute.
108*d4726bddSHONG Yifan
109*d4726bddSHONG Yifan```python
110*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "render_config")
111*d4726bddSHONG Yifan
112*d4726bddSHONG Yifancrates_repository(
113*d4726bddSHONG Yifan    name = "crate_index",
114*d4726bddSHONG Yifan    cargo_lockfile = "//:Cargo.lock",
115*d4726bddSHONG Yifan    lockfile = "//:Cargo.Bazel.lock",
116*d4726bddSHONG Yifan    packages = {
117*d4726bddSHONG Yifan        "async-trait": crate.spec(
118*d4726bddSHONG Yifan            version = "0.1.51",
119*d4726bddSHONG Yifan        ),
120*d4726bddSHONG Yifan        "mockall": crate.spec(
121*d4726bddSHONG Yifan            version = "0.10.2",
122*d4726bddSHONG Yifan        ),
123*d4726bddSHONG Yifan        "tokio": crate.spec(
124*d4726bddSHONG Yifan            version = "1.12.0",
125*d4726bddSHONG Yifan        ),
126*d4726bddSHONG Yifan    },
127*d4726bddSHONG Yifan    # Setting the default package name to `""` forces the use of the macros defined in this repository
128*d4726bddSHONG Yifan    # to always use the root package when looking for dependencies or aliases. This should be considered
129*d4726bddSHONG Yifan    # optional as the repository also exposes alises for easy access to all dependencies.
130*d4726bddSHONG Yifan    render_config = render_config(
131*d4726bddSHONG Yifan        default_package_name = ""
132*d4726bddSHONG Yifan    ),
133*d4726bddSHONG Yifan)
134*d4726bddSHONG Yifan
135*d4726bddSHONG Yifanload("@crate_index//:defs.bzl", "crate_repositories")
136*d4726bddSHONG Yifan
137*d4726bddSHONG Yifancrate_repositories()
138*d4726bddSHONG Yifan```
139*d4726bddSHONG Yifan
140*d4726bddSHONG YifanConsuming dependencies may be more ergonomic in this case through the aliases defined in the new repository.
141*d4726bddSHONG Yifan
142*d4726bddSHONG Yifan```python
143*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
144*d4726bddSHONG Yifan
145*d4726bddSHONG Yifanrust_library(
146*d4726bddSHONG Yifan    name = "lib",
147*d4726bddSHONG Yifan    deps = [
148*d4726bddSHONG Yifan        "@crate_index//:tokio",
149*d4726bddSHONG Yifan    ],
150*d4726bddSHONG Yifan    proc_macro_deps = [
151*d4726bddSHONG Yifan        "@crate_index//:async-trait",
152*d4726bddSHONG Yifan    ],
153*d4726bddSHONG Yifan)
154*d4726bddSHONG Yifan
155*d4726bddSHONG Yifanrust_test(
156*d4726bddSHONG Yifan    name = "unit_test",
157*d4726bddSHONG Yifan    crate = ":lib",
158*d4726bddSHONG Yifan    deps = [
159*d4726bddSHONG Yifan        "@crate_index//:mockall",
160*d4726bddSHONG Yifan    ],
161*d4726bddSHONG Yifan)
162*d4726bddSHONG Yifan```
163*d4726bddSHONG Yifan
164*d4726bddSHONG Yifan### Binary dependencies
165*d4726bddSHONG Yifan
166*d4726bddSHONG YifanNeither of the above approaches supports depending on binary-only packages.
167*d4726bddSHONG Yifan
168*d4726bddSHONG YifanIn order to depend on a Cargo package that contains binaries and no library, you
169*d4726bddSHONG Yifanwill need to do one of the following:
170*d4726bddSHONG Yifan
171*d4726bddSHONG Yifan- Fork the package to add an empty lib.rs, which makes the package visible to
172*d4726bddSHONG Yifan  Cargo metadata and compatible with the above approaches;
173*d4726bddSHONG Yifan
174*d4726bddSHONG Yifan- Or handwrite your own build target for the binary, use `http_archive` to
175*d4726bddSHONG Yifan  import its source code, and use `crates_repository` to make build targets for
176*d4726bddSHONG Yifan  its dependencies. This is demonstrated below using the `rustfilt` crate as an
177*d4726bddSHONG Yifan  example.
178*d4726bddSHONG Yifan
179*d4726bddSHONG Yifan```python
180*d4726bddSHONG Yifan# in WORKSPACE.bazel
181*d4726bddSHONG Yifan
182*d4726bddSHONG Yifanload("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
183*d4726bddSHONG Yifan
184*d4726bddSHONG Yifanhttp_archive(
185*d4726bddSHONG Yifan    name = "rustfilt",
186*d4726bddSHONG Yifan    build_file = "//rustfilt:BUILD.rustfilt.bazel",
187*d4726bddSHONG Yifan    sha256 = "c8d748b182c8f95224336d20dcc5609598af612581ce60cfb29da4dc8d0091f2",
188*d4726bddSHONG Yifan    strip_prefix = "rustfilt-0.2.1",
189*d4726bddSHONG Yifan    type = "tar.gz",
190*d4726bddSHONG Yifan    urls = ["https://static.crates.io/crates/rustfilt/rustfilt-0.2.1.crate"],
191*d4726bddSHONG Yifan)
192*d4726bddSHONG Yifan
193*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crates_repository")
194*d4726bddSHONG Yifan
195*d4726bddSHONG Yifancrates_repository(
196*d4726bddSHONG Yifan    name = "rustfilt_deps",
197*d4726bddSHONG Yifan    cargo_lockfile = "//rustfilt:Cargo.lock",
198*d4726bddSHONG Yifan    manifests = ["@rustfilt//:Cargo.toml"],
199*d4726bddSHONG Yifan)
200*d4726bddSHONG Yifan
201*d4726bddSHONG Yifanload("@rustfilt_deps//:defs.bzl", rustfilt_deps = "crate_repositories")
202*d4726bddSHONG Yifan
203*d4726bddSHONG Yifanrustfilt_deps()
204*d4726bddSHONG Yifan```
205*d4726bddSHONG Yifan
206*d4726bddSHONG Yifan```python
207*d4726bddSHONG Yifan# in rustfilt/BUILD.rustfilt.bazel
208*d4726bddSHONG Yifan
209*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_binary")
210*d4726bddSHONG Yifan
211*d4726bddSHONG Yifanrust_binary(
212*d4726bddSHONG Yifan    name = "rustfilt",
213*d4726bddSHONG Yifan    srcs = glob(["src/**/*.rs"]),
214*d4726bddSHONG Yifan    edition = "2018",
215*d4726bddSHONG Yifan    deps = [
216*d4726bddSHONG Yifan        "@rustfilt_deps//:clap",
217*d4726bddSHONG Yifan        "@rustfilt_deps//:lazy_static",
218*d4726bddSHONG Yifan        "@rustfilt_deps//:regex",
219*d4726bddSHONG Yifan        "@rustfilt_deps//:rustc-demangle",
220*d4726bddSHONG Yifan    ],
221*d4726bddSHONG Yifan)
222*d4726bddSHONG Yifan```
223*d4726bddSHONG Yifan
224*d4726bddSHONG YifanIf you use either `crates_repository` or `crates_vendor` to depend on a Cargo
225*d4726bddSHONG Yifanpackage that contains _both_ a library crate _and_ binaries, by default only the
226*d4726bddSHONG Yifanlibrary gets made available to Bazel. To generate Bazel targets for the binary
227*d4726bddSHONG Yifancrates as well, you must opt in to it with an annotation on the package:
228*d4726bddSHONG Yifan
229*d4726bddSHONG Yifan```python
230*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")
231*d4726bddSHONG Yifan
232*d4726bddSHONG Yifancrates_repository(
233*d4726bddSHONG Yifan    name = "crate_index",
234*d4726bddSHONG Yifan    annotations = {
235*d4726bddSHONG Yifan        "thepackage": [crate.annotation(
236*d4726bddSHONG Yifan            gen_binaries = True,
237*d4726bddSHONG Yifan            # Or, to expose just a subset of the package's binaries by name:
238*d4726bddSHONG Yifan            gen_binaries = ["rustfilt"],
239*d4726bddSHONG Yifan        )],
240*d4726bddSHONG Yifan    },
241*d4726bddSHONG Yifan    # Or, to expose every binary of every package:
242*d4726bddSHONG Yifan    generate_binaries = True,
243*d4726bddSHONG Yifan    ...
244*d4726bddSHONG Yifan)
245*d4726bddSHONG Yifan```
246*d4726bddSHONG Yifan
247*d4726bddSHONG Yifan## Dependencies API
248*d4726bddSHONG Yifan
249*d4726bddSHONG YifanAfter rendering dependencies, convenience macros may also be generated to provide
250*d4726bddSHONG Yifanconvenient accessors to larger sections of the dependency graph.
251*d4726bddSHONG Yifan
252*d4726bddSHONG Yifan- [aliases](#aliases)
253*d4726bddSHONG Yifan- [crate_deps](#crate_deps)
254*d4726bddSHONG Yifan- [all_crate_deps](#all_crate_deps)
255*d4726bddSHONG Yifan- [crate_repositories](#crate_repositories)
256*d4726bddSHONG Yifan
257*d4726bddSHONG Yifan## Building crates with complicated dependencies
258*d4726bddSHONG Yifan
259*d4726bddSHONG YifanSome crates have build.rs scripts which are complicated to run. Typically these build C++ (or other languages), or attempt to find pre-installed libraries on the build machine.
260*d4726bddSHONG Yifan
261*d4726bddSHONG YifanThere are a few approaches to making sure these run:
262*d4726bddSHONG Yifan
263*d4726bddSHONG Yifan### Some things work without intervention
264*d4726bddSHONG Yifan
265*d4726bddSHONG YifanSome build scripts will happily run without any support needed.
266*d4726bddSHONG Yifan
267*d4726bddSHONG Yifanrules_rust already supplies a configured C++ toolchain as input to build script execution, and sets variables like `CC`, `CXX`, `LD`, `LDFLAGS`, etc as needed. Many crates which invoke a compiler with the default environment, or forward these env vars, will Just Work (e.g. if using [`cc-rs`][cc-rs]).
268*d4726bddSHONG Yifan
269*d4726bddSHONG Yifanrules_rust is open to PRs which make build scripts more likely to work by default with intervention assuming they're broadly applicable (e.g. setting extra widely-known env vars is probably fine, wiring up additional toolchains like `cmake` that aren't present by default for most Bazel users probably isn't).
270*d4726bddSHONG Yifan
271*d4726bddSHONG Yifan### Supplying extra tools to build
272*d4726bddSHONG Yifan
273*d4726bddSHONG YifanSome build scripts can be made to work by pulling in some extra files and making them available to the build script.
274*d4726bddSHONG Yifan
275*d4726bddSHONG YifanCommonly this is done by passing the file to the `build_script_data` annotation for the crate, and using `build_script_env` to tell the build script where the file is. That env var may often use `$(execroot)` to get the path to the label, or `$${pwd}/` as a prefix if the path given is relative to the execroot (as will frequently happen when using a toolchain).A
276*d4726bddSHONG Yifan
277*d4726bddSHONG YifanThere is an example of this in the "complicated dependencies" section of https://github.com/bazelbuild/rules_rust/blob/main/examples/crate_universe/WORKSPACE.bazel which builds libz-ng-sys.
278*d4726bddSHONG Yifan
279*d4726bddSHONG Yifan### Building with Bazel and supplying via an override
280*d4726bddSHONG Yifan
281*d4726bddSHONG YifanSome build scripts have hooks to allow replacing parts that are complicated to build with output prepared by Bazel.
282*d4726bddSHONG Yifan
283*d4726bddSHONG YifanWe can use those hooks by specifying paths (generally using the `build_script_data` and `build_script_env` annotations) and pointing them at labels which Bazel will then build. These env vars may often use `$(execroot)` to get the path to the label, or `$${pwd}/` as a prefix if the path given is relative to the execroot (as will frequently happen when using a toolchain).
284*d4726bddSHONG Yifan
285*d4726bddSHONG YifanThere is an example of this in the "complicated dependencies" section of https://github.com/bazelbuild/rules_rust/blob/main/examples/crate_universe/WORKSPACE.bazel which builds boring-sys.
286*d4726bddSHONG Yifan
287*d4726bddSHONG Yifan---
288*d4726bddSHONG Yifan
289*d4726bddSHONG Yifan---
290*d4726bddSHONG Yifan
291*d4726bddSHONG Yifan[cw]: https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html
292*d4726bddSHONG Yifan[cc]: https://docs.bazel.build/versions/main/be/c-cpp.html
293*d4726bddSHONG Yifan[proto]: https://rules-proto-grpc.com/en/latest/lang/rust.html
294*d4726bddSHONG Yifan[ra]: https://rust-analyzer.github.io/
295*d4726bddSHONG Yifan[cc-rs]: https://github.com/rust-lang/cc-rs
296*d4726bddSHONG Yifan
297*d4726bddSHONG Yifan<a id="crates_vendor"></a>
298*d4726bddSHONG Yifan
299*d4726bddSHONG Yifan## crates_vendor
300*d4726bddSHONG Yifan
301*d4726bddSHONG Yifan<pre>
302*d4726bddSHONG Yifancrates_vendor(<a href="#crates_vendor-name">name</a>, <a href="#crates_vendor-annotations">annotations</a>, <a href="#crates_vendor-bazel">bazel</a>, <a href="#crates_vendor-buildifier">buildifier</a>, <a href="#crates_vendor-cargo_bazel">cargo_bazel</a>, <a href="#crates_vendor-cargo_config">cargo_config</a>, <a href="#crates_vendor-cargo_lockfile">cargo_lockfile</a>,
303*d4726bddSHONG Yifan              <a href="#crates_vendor-generate_binaries">generate_binaries</a>, <a href="#crates_vendor-generate_build_scripts">generate_build_scripts</a>, <a href="#crates_vendor-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#crates_vendor-manifests">manifests</a>,
304*d4726bddSHONG Yifan              <a href="#crates_vendor-mode">mode</a>, <a href="#crates_vendor-packages">packages</a>, <a href="#crates_vendor-render_config">render_config</a>, <a href="#crates_vendor-repository_name">repository_name</a>, <a href="#crates_vendor-splicing_config">splicing_config</a>,
305*d4726bddSHONG Yifan              <a href="#crates_vendor-supported_platform_triples">supported_platform_triples</a>, <a href="#crates_vendor-vendor_path">vendor_path</a>)
306*d4726bddSHONG Yifan</pre>
307*d4726bddSHONG Yifan
308*d4726bddSHONG YifanA rule for defining Rust dependencies (crates) and writing targets for them to the current workspace.
309*d4726bddSHONG YifanThis rule is useful for users whose workspaces are expected to be consumed in other workspaces as the
310*d4726bddSHONG Yifanrendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule
311*d4726bddSHONG Yifanhandles all the same [workflows](#workflows) `crate_universe` rules do.
312*d4726bddSHONG Yifan
313*d4726bddSHONG YifanExample:
314*d4726bddSHONG Yifan
315*d4726bddSHONG YifanGiven the following workspace structure:
316*d4726bddSHONG Yifan
317*d4726bddSHONG Yifan```text
318*d4726bddSHONG Yifan[workspace]/
319*d4726bddSHONG Yifan    WORKSPACE
320*d4726bddSHONG Yifan    BUILD
321*d4726bddSHONG Yifan    Cargo.toml
322*d4726bddSHONG Yifan    3rdparty/
323*d4726bddSHONG Yifan        BUILD
324*d4726bddSHONG Yifan    src/
325*d4726bddSHONG Yifan        main.rs
326*d4726bddSHONG Yifan```
327*d4726bddSHONG Yifan
328*d4726bddSHONG YifanThe following is something that'd be found in `3rdparty/BUILD`:
329*d4726bddSHONG Yifan
330*d4726bddSHONG Yifan```python
331*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate")
332*d4726bddSHONG Yifan
333*d4726bddSHONG Yifancrates_vendor(
334*d4726bddSHONG Yifan    name = "crates_vendor",
335*d4726bddSHONG Yifan    annotations = {
336*d4726bddSHONG Yifan        "rand": [crate.annotation(
337*d4726bddSHONG Yifan            default_features = False,
338*d4726bddSHONG Yifan            features = ["small_rng"],
339*d4726bddSHONG Yifan        )],
340*d4726bddSHONG Yifan    },
341*d4726bddSHONG Yifan    cargo_lockfile = "//:Cargo.Bazel.lock",
342*d4726bddSHONG Yifan    manifests = ["//:Cargo.toml"],
343*d4726bddSHONG Yifan    mode = "remote",
344*d4726bddSHONG Yifan    vendor_path = "crates",
345*d4726bddSHONG Yifan    tags = ["manual"],
346*d4726bddSHONG Yifan)
347*d4726bddSHONG Yifan```
348*d4726bddSHONG Yifan
349*d4726bddSHONG YifanThe above creates a target that can be run to write `BUILD` files into the `3rdparty`
350*d4726bddSHONG Yifandirectory next to where the target is defined. To run it, simply call:
351*d4726bddSHONG Yifan
352*d4726bddSHONG Yifan```shell
353*d4726bddSHONG Yifanbazel run //3rdparty:crates_vendor
354*d4726bddSHONG Yifan```
355*d4726bddSHONG Yifan
356*d4726bddSHONG Yifan<a id="#crates_vendor_repinning_updating_dependencies"></a>
357*d4726bddSHONG Yifan
358*d4726bddSHONG Yifan### Repinning / Updating Dependencies
359*d4726bddSHONG Yifan
360*d4726bddSHONG YifanRepinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin`
361*d4726bddSHONG Yifanflag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation.
362*d4726bddSHONG Yifan
363*d4726bddSHONG Yifan```shell
364*d4726bddSHONG Yifanbazel run //3rdparty:crates_vendor -- --repin
365*d4726bddSHONG Yifan```
366*d4726bddSHONG Yifan
367*d4726bddSHONG YifanUnder the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
368*d4726bddSHONG Yifancall against the generated workspace. The following table describes how to control particular values passed to the
369*d4726bddSHONG Yifan`cargo update` command.
370*d4726bddSHONG Yifan
371*d4726bddSHONG Yifan| Value | Cargo command |
372*d4726bddSHONG Yifan| --- | --- |
373*d4726bddSHONG Yifan| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` |
374*d4726bddSHONG Yifan| Any of [`full`, `eager`, `all`] | `cargo update` |
375*d4726bddSHONG Yifan| `package_name` | `cargo upgrade --package package_name` |
376*d4726bddSHONG Yifan| `[email protected]` | `cargo upgrade --package package_name --precise 1.2.3` |
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="crates_vendor-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="crates_vendor-annotations"></a>annotations |  Extra settings to apply to crates. See [crate.annotation](#crateannotation).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional |  `{}`  |
385*d4726bddSHONG Yifan| <a id="crates_vendor-bazel"></a>bazel |  The path to a bazel binary used to locate the output_base for the current workspace.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
386*d4726bddSHONG Yifan| <a id="crates_vendor-buildifier"></a>buildifier |  The path to a [buildifier](https://github.com/bazelbuild/buildtools/blob/5.0.1/buildifier/README.md) binary used to format generated BUILD files.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `"@rules_rust//crate_universe/private/vendor:buildifier"`  |
387*d4726bddSHONG Yifan| <a id="crates_vendor-cargo_bazel"></a>cargo_bazel |  The cargo-bazel binary to use for vendoring. If this attribute is not set, then a `CARGO_BAZEL_GENERATOR_PATH` action env will be used.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `"@@cargo_bazel_bootstrap//:binary"`  |
388*d4726bddSHONG Yifan| <a id="crates_vendor-cargo_config"></a>cargo_config |  A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
389*d4726bddSHONG Yifan| <a id="crates_vendor-cargo_lockfile"></a>cargo_lockfile |  The path to an existing `Cargo.lock` file   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
390*d4726bddSHONG Yifan| <a id="crates_vendor-generate_binaries"></a>generate_binaries |  Whether to generate `rust_binary` targets for all the binary crates in every package. By default only the `rust_library` targets are generated.   | Boolean | optional |  `False`  |
391*d4726bddSHONG Yifan| <a id="crates_vendor-generate_build_scripts"></a>generate_build_scripts |  Whether or not to generate [cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default.   | Boolean | optional |  `True`  |
392*d4726bddSHONG Yifan| <a id="crates_vendor-generate_target_compatible_with"></a>generate_target_compatible_with |  DEPRECATED: Moved to `render_config`.   | Boolean | optional |  `True`  |
393*d4726bddSHONG Yifan| <a id="crates_vendor-manifests"></a>manifests |  A list of Cargo manifests (`Cargo.toml` files).   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
394*d4726bddSHONG Yifan| <a id="crates_vendor-mode"></a>mode |  Flags determining how crates should be vendored. `local` is where crate source and BUILD files are written to the repository. `remote` is where only BUILD files are written and repository rules used to fetch source code.   | String | optional |  `"remote"`  |
395*d4726bddSHONG Yifan| <a id="crates_vendor-packages"></a>packages |  A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
396*d4726bddSHONG Yifan| <a id="crates_vendor-render_config"></a>render_config |  The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\%render_config` to generate the value for this field. If unset, the defaults defined there will be used.   | String | optional |  `""`  |
397*d4726bddSHONG Yifan| <a id="crates_vendor-repository_name"></a>repository_name |  The name of the repository to generate for `remote` vendor modes. If unset, the label name will be used   | String | optional |  `""`  |
398*d4726bddSHONG Yifan| <a id="crates_vendor-splicing_config"></a>splicing_config |  The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\%rsplicing_config` to generate the value for this field. If unset, the defaults defined there will be used.   | String | optional |  `""`  |
399*d4726bddSHONG Yifan| <a id="crates_vendor-supported_platform_triples"></a>supported_platform_triples |  A set of all platform triples to consider when generating dependencies.   | List of strings | optional |  `["aarch64-unknown-linux-gnu", "aarch64-unknown-nixos-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "x86_64-unknown-nixos-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "thumbv8m.main-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", "x86_64-unknown-none", "aarch64-unknown-nto-qnx710"]`  |
400*d4726bddSHONG Yifan| <a id="crates_vendor-vendor_path"></a>vendor_path |  The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root   | String | optional |  `"crates"`  |
401*d4726bddSHONG Yifan
402*d4726bddSHONG Yifan
403*d4726bddSHONG Yifan<a id="aliases"></a>
404*d4726bddSHONG Yifan
405*d4726bddSHONG Yifan## aliases
406*d4726bddSHONG Yifan
407*d4726bddSHONG Yifan<pre>
408*d4726bddSHONG Yifanaliases(<a href="#aliases-normal">normal</a>, <a href="#aliases-normal_dev">normal_dev</a>, <a href="#aliases-proc_macro">proc_macro</a>, <a href="#aliases-proc_macro_dev">proc_macro_dev</a>, <a href="#aliases-build">build</a>, <a href="#aliases-build_proc_macro">build_proc_macro</a>, <a href="#aliases-package_name">package_name</a>)
409*d4726bddSHONG Yifan</pre>
410*d4726bddSHONG Yifan
411*d4726bddSHONG YifanProduces a map of Crate alias names to their original label
412*d4726bddSHONG Yifan
413*d4726bddSHONG YifanIf no dependency kinds are specified, `normal` and `proc_macro` are used by default.
414*d4726bddSHONG YifanSetting any one flag will otherwise determine the contents of the returned dict.
415*d4726bddSHONG Yifan
416*d4726bddSHONG Yifan
417*d4726bddSHONG Yifan**PARAMETERS**
418*d4726bddSHONG Yifan
419*d4726bddSHONG Yifan
420*d4726bddSHONG Yifan| Name  | Description | Default Value |
421*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
422*d4726bddSHONG Yifan| <a id="aliases-normal"></a>normal |  If True, normal dependencies are included in the output list.   |  `False` |
423*d4726bddSHONG Yifan| <a id="aliases-normal_dev"></a>normal_dev |  If True, normal dev dependencies will be included in the output list..   |  `False` |
424*d4726bddSHONG Yifan| <a id="aliases-proc_macro"></a>proc_macro |  If True, proc_macro dependencies are included in the output list.   |  `False` |
425*d4726bddSHONG Yifan| <a id="aliases-proc_macro_dev"></a>proc_macro_dev |  If True, dev proc_macro dependencies are included in the output list.   |  `False` |
426*d4726bddSHONG Yifan| <a id="aliases-build"></a>build |  If True, build dependencies are included in the output list.   |  `False` |
427*d4726bddSHONG Yifan| <a id="aliases-build_proc_macro"></a>build_proc_macro |  If True, build proc_macro dependencies are included in the output list.   |  `False` |
428*d4726bddSHONG Yifan| <a id="aliases-package_name"></a>package_name |  The package name of the set of dependencies to look up. Defaults to `native.package_name()` when unset.   |  `None` |
429*d4726bddSHONG Yifan
430*d4726bddSHONG Yifan**RETURNS**
431*d4726bddSHONG Yifan
432*d4726bddSHONG Yifandict: The aliases of all associated packages
433*d4726bddSHONG Yifan
434*d4726bddSHONG Yifan
435*d4726bddSHONG Yifan<a id="all_crate_deps"></a>
436*d4726bddSHONG Yifan
437*d4726bddSHONG Yifan## all_crate_deps
438*d4726bddSHONG Yifan
439*d4726bddSHONG Yifan<pre>
440*d4726bddSHONG Yifanall_crate_deps(<a href="#all_crate_deps-normal">normal</a>, <a href="#all_crate_deps-normal_dev">normal_dev</a>, <a href="#all_crate_deps-proc_macro">proc_macro</a>, <a href="#all_crate_deps-proc_macro_dev">proc_macro_dev</a>, <a href="#all_crate_deps-build">build</a>, <a href="#all_crate_deps-build_proc_macro">build_proc_macro</a>,
441*d4726bddSHONG Yifan               <a href="#all_crate_deps-package_name">package_name</a>)
442*d4726bddSHONG Yifan</pre>
443*d4726bddSHONG Yifan
444*d4726bddSHONG YifanFinds the fully qualified label of all requested direct crate dependencies     for the package where this macro is called.
445*d4726bddSHONG Yifan
446*d4726bddSHONG YifanIf no parameters are set, all normal dependencies are returned. Setting any one flag will
447*d4726bddSHONG Yifanotherwise impact the contents of the returned list.
448*d4726bddSHONG Yifan
449*d4726bddSHONG Yifan
450*d4726bddSHONG Yifan**PARAMETERS**
451*d4726bddSHONG Yifan
452*d4726bddSHONG Yifan
453*d4726bddSHONG Yifan| Name  | Description | Default Value |
454*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
455*d4726bddSHONG Yifan| <a id="all_crate_deps-normal"></a>normal |  If True, normal dependencies are included in the output list.   |  `False` |
456*d4726bddSHONG Yifan| <a id="all_crate_deps-normal_dev"></a>normal_dev |  If True, normal dev dependencies will be included in the output list..   |  `False` |
457*d4726bddSHONG Yifan| <a id="all_crate_deps-proc_macro"></a>proc_macro |  If True, proc_macro dependencies are included in the output list.   |  `False` |
458*d4726bddSHONG Yifan| <a id="all_crate_deps-proc_macro_dev"></a>proc_macro_dev |  If True, dev proc_macro dependencies are included in the output list.   |  `False` |
459*d4726bddSHONG Yifan| <a id="all_crate_deps-build"></a>build |  If True, build dependencies are included in the output list.   |  `False` |
460*d4726bddSHONG Yifan| <a id="all_crate_deps-build_proc_macro"></a>build_proc_macro |  If True, build proc_macro dependencies are included in the output list.   |  `False` |
461*d4726bddSHONG Yifan| <a id="all_crate_deps-package_name"></a>package_name |  The package name of the set of dependencies to look up. Defaults to `native.package_name()` when unset.   |  `None` |
462*d4726bddSHONG Yifan
463*d4726bddSHONG Yifan**RETURNS**
464*d4726bddSHONG Yifan
465*d4726bddSHONG Yifanlist: A list of labels to generated rust targets (str)
466*d4726bddSHONG Yifan
467*d4726bddSHONG Yifan
468*d4726bddSHONG Yifan<a id="crate.annotation"></a>
469*d4726bddSHONG Yifan
470*d4726bddSHONG Yifan## crate.annotation
471*d4726bddSHONG Yifan
472*d4726bddSHONG Yifan<pre>
473*d4726bddSHONG Yifancrate.annotation(<a href="#crate.annotation-version">version</a>, <a href="#crate.annotation-additive_build_file">additive_build_file</a>, <a href="#crate.annotation-additive_build_file_content">additive_build_file_content</a>, <a href="#crate.annotation-alias_rule">alias_rule</a>,
474*d4726bddSHONG Yifan                 <a href="#crate.annotation-build_script_data">build_script_data</a>, <a href="#crate.annotation-build_script_tools">build_script_tools</a>, <a href="#crate.annotation-build_script_data_glob">build_script_data_glob</a>, <a href="#crate.annotation-build_script_deps">build_script_deps</a>,
475*d4726bddSHONG Yifan                 <a href="#crate.annotation-build_script_env">build_script_env</a>, <a href="#crate.annotation-build_script_proc_macro_deps">build_script_proc_macro_deps</a>, <a href="#crate.annotation-build_script_rundir">build_script_rundir</a>,
476*d4726bddSHONG Yifan                 <a href="#crate.annotation-build_script_rustc_env">build_script_rustc_env</a>, <a href="#crate.annotation-build_script_toolchains">build_script_toolchains</a>, <a href="#crate.annotation-compile_data">compile_data</a>, <a href="#crate.annotation-compile_data_glob">compile_data_glob</a>,
477*d4726bddSHONG Yifan                 <a href="#crate.annotation-crate_features">crate_features</a>, <a href="#crate.annotation-data">data</a>, <a href="#crate.annotation-data_glob">data_glob</a>, <a href="#crate.annotation-deps">deps</a>, <a href="#crate.annotation-extra_aliased_targets">extra_aliased_targets</a>, <a href="#crate.annotation-gen_binaries">gen_binaries</a>,
478*d4726bddSHONG Yifan                 <a href="#crate.annotation-disable_pipelining">disable_pipelining</a>, <a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>,
479*d4726bddSHONG Yifan                 <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>, <a href="#crate.annotation-rustc_env">rustc_env</a>, <a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>,
480*d4726bddSHONG Yifan                 <a href="#crate.annotation-override_targets">override_targets</a>)
481*d4726bddSHONG Yifan</pre>
482*d4726bddSHONG Yifan
483*d4726bddSHONG YifanA collection of extra attributes and settings for a particular crate
484*d4726bddSHONG Yifan
485*d4726bddSHONG Yifan**PARAMETERS**
486*d4726bddSHONG Yifan
487*d4726bddSHONG Yifan
488*d4726bddSHONG Yifan| Name  | Description | Default Value |
489*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
490*d4726bddSHONG Yifan| <a id="crate.annotation-version"></a>version |  The version or semver-conditions to match with a crate. The wildcard `*` matches any version, including prerelease versions.   |  `"*"` |
491*d4726bddSHONG Yifan| <a id="crate.annotation-additive_build_file"></a>additive_build_file |  A file containing extra contents to write to the bottom of generated BUILD files.   |  `None` |
492*d4726bddSHONG Yifan| <a id="crate.annotation-additive_build_file_content"></a>additive_build_file_content |  Extra contents to write to the bottom of generated BUILD files.   |  `None` |
493*d4726bddSHONG Yifan| <a id="crate.annotation-alias_rule"></a>alias_rule |  Alias rule to use instead of `native.alias()`.  Overrides [render_config](#render_config)'s 'default_alias_rule'.   |  `None` |
494*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_data"></a>build_script_data |  A list of labels to add to a crate's `cargo_build_script::data` attribute.   |  `None` |
495*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_tools"></a>build_script_tools |  A list of labels to add to a crate's `cargo_build_script::tools` attribute.   |  `None` |
496*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_data_glob"></a>build_script_data_glob |  A list of glob patterns to add to a crate's `cargo_build_script::data` attribute.   |  `None` |
497*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_deps"></a>build_script_deps |  A list of labels to add to a crate's `cargo_build_script::deps` attribute.   |  `None` |
498*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_env"></a>build_script_env |  Additional environment variables to set on a crate's `cargo_build_script::env` attribute.   |  `None` |
499*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_proc_macro_deps"></a>build_script_proc_macro_deps |  A list of labels to add to a crate's `cargo_build_script::proc_macro_deps` attribute.   |  `None` |
500*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_rundir"></a>build_script_rundir |  An override for the build script's rundir attribute.   |  `None` |
501*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_rustc_env"></a>build_script_rustc_env |  Additional environment variables to set on a crate's `cargo_build_script::env` attribute.   |  `None` |
502*d4726bddSHONG Yifan| <a id="crate.annotation-build_script_toolchains"></a>build_script_toolchains |  A list of labels to set on a crates's `cargo_build_script::toolchains` attribute.   |  `None` |
503*d4726bddSHONG Yifan| <a id="crate.annotation-compile_data"></a>compile_data |  A list of labels to add to a crate's `rust_library::compile_data` attribute.   |  `None` |
504*d4726bddSHONG Yifan| <a id="crate.annotation-compile_data_glob"></a>compile_data_glob |  A list of glob patterns to add to a crate's `rust_library::compile_data` attribute.   |  `None` |
505*d4726bddSHONG Yifan| <a id="crate.annotation-crate_features"></a>crate_features |  A list of strings to add to a crate's `rust_library::crate_features` attribute.   |  `None` |
506*d4726bddSHONG Yifan| <a id="crate.annotation-data"></a>data |  A list of labels to add to a crate's `rust_library::data` attribute.   |  `None` |
507*d4726bddSHONG Yifan| <a id="crate.annotation-data_glob"></a>data_glob |  A list of glob patterns to add to a crate's `rust_library::data` attribute.   |  `None` |
508*d4726bddSHONG Yifan| <a id="crate.annotation-deps"></a>deps |  A list of labels to add to a crate's `rust_library::deps` attribute.   |  `None` |
509*d4726bddSHONG Yifan| <a id="crate.annotation-extra_aliased_targets"></a>extra_aliased_targets |  A list of targets to add to the generated aliases in the root crate_universe repository.   |  `None` |
510*d4726bddSHONG Yifan| <a id="crate.annotation-gen_binaries"></a>gen_binaries |  As a list, the subset of the crate's bins that should get `rust_binary` targets produced. Or `True` to generate all, `False` to generate none.   |  `None` |
511*d4726bddSHONG Yifan| <a id="crate.annotation-disable_pipelining"></a>disable_pipelining |  If True, disables pipelining for library targets for this crate.   |  `False` |
512*d4726bddSHONG Yifan| <a id="crate.annotation-gen_build_script"></a>gen_build_script |  An authorative flag to determine whether or not to produce `cargo_build_script` targets for the current crate.   |  `None` |
513*d4726bddSHONG Yifan| <a id="crate.annotation-patch_args"></a>patch_args |  The `patch_args` attribute of a Bazel repository rule. See [http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args)   |  `None` |
514*d4726bddSHONG Yifan| <a id="crate.annotation-patch_tool"></a>patch_tool |  The `patch_tool` attribute of a Bazel repository rule. See [http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool)   |  `None` |
515*d4726bddSHONG Yifan| <a id="crate.annotation-patches"></a>patches |  The `patches` attribute of a Bazel repository rule. See [http_archive.patches](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patches)   |  `None` |
516*d4726bddSHONG Yifan| <a id="crate.annotation-proc_macro_deps"></a>proc_macro_deps |  A list of labels to add to a crate's `rust_library::proc_macro_deps` attribute.   |  `None` |
517*d4726bddSHONG Yifan| <a id="crate.annotation-rustc_env"></a>rustc_env |  Additional variables to set on a crate's `rust_library::rustc_env` attribute.   |  `None` |
518*d4726bddSHONG Yifan| <a id="crate.annotation-rustc_env_files"></a>rustc_env_files |  A list of labels to set on a crate's `rust_library::rustc_env_files` attribute.   |  `None` |
519*d4726bddSHONG Yifan| <a id="crate.annotation-rustc_flags"></a>rustc_flags |  A list of strings to set on a crate's `rust_library::rustc_flags` attribute.   |  `None` |
520*d4726bddSHONG Yifan| <a id="crate.annotation-shallow_since"></a>shallow_since |  An optional timestamp used for crates originating from a git repository instead of a crate registry. This flag optimizes fetching the source code.   |  `None` |
521*d4726bddSHONG Yifan| <a id="crate.annotation-override_targets"></a>override_targets |  A dictionary of alternate tagets to use when something depends on this crate to allow the parent repo to provide its own version of this dependency. Keys can be `proc_marco`, `build_script`, `lib`, `bin`.   |  `None` |
522*d4726bddSHONG Yifan
523*d4726bddSHONG Yifan**RETURNS**
524*d4726bddSHONG Yifan
525*d4726bddSHONG Yifanstring: A json encoded string containing the specified version and separately all other inputs.
526*d4726bddSHONG Yifan
527*d4726bddSHONG Yifan
528*d4726bddSHONG Yifan<a id="crate.select"></a>
529*d4726bddSHONG Yifan
530*d4726bddSHONG Yifan## crate.select
531*d4726bddSHONG Yifan
532*d4726bddSHONG Yifan<pre>
533*d4726bddSHONG Yifancrate.select(<a href="#crate.select-common">common</a>, <a href="#crate.select-selects">selects</a>)
534*d4726bddSHONG Yifan</pre>
535*d4726bddSHONG Yifan
536*d4726bddSHONG YifanA Starlark Select for `crate.annotation()`.
537*d4726bddSHONG Yifan
538*d4726bddSHONG Yifan**PARAMETERS**
539*d4726bddSHONG Yifan
540*d4726bddSHONG Yifan
541*d4726bddSHONG Yifan| Name  | Description | Default Value |
542*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
543*d4726bddSHONG Yifan| <a id="crate.select-common"></a>common |  A value that applies to all configurations.   |  none |
544*d4726bddSHONG Yifan| <a id="crate.select-selects"></a>selects |  A dict of `target_triple` to values.   |  none |
545*d4726bddSHONG Yifan
546*d4726bddSHONG Yifan**RETURNS**
547*d4726bddSHONG Yifan
548*d4726bddSHONG Yifanstruct: A struct representing the Starlark Select.
549*d4726bddSHONG Yifan
550*d4726bddSHONG Yifan
551*d4726bddSHONG Yifan<a id="crate.spec"></a>
552*d4726bddSHONG Yifan
553*d4726bddSHONG Yifan## crate.spec
554*d4726bddSHONG Yifan
555*d4726bddSHONG Yifan<pre>
556*d4726bddSHONG Yifancrate.spec(<a href="#crate.spec-package">package</a>, <a href="#crate.spec-version">version</a>, <a href="#crate.spec-artifact">artifact</a>, <a href="#crate.spec-lib">lib</a>, <a href="#crate.spec-default_features">default_features</a>, <a href="#crate.spec-features">features</a>, <a href="#crate.spec-git">git</a>, <a href="#crate.spec-branch">branch</a>, <a href="#crate.spec-tag">tag</a>, <a href="#crate.spec-rev">rev</a>)
557*d4726bddSHONG Yifan</pre>
558*d4726bddSHONG Yifan
559*d4726bddSHONG YifanA constructor for a crate dependency.
560*d4726bddSHONG Yifan
561*d4726bddSHONG YifanSee [specifying dependencies][sd] in the Cargo book for more details.
562*d4726bddSHONG Yifan
563*d4726bddSHONG Yifan[sd]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
564*d4726bddSHONG Yifan
565*d4726bddSHONG Yifan
566*d4726bddSHONG Yifan**PARAMETERS**
567*d4726bddSHONG Yifan
568*d4726bddSHONG Yifan
569*d4726bddSHONG Yifan| Name  | Description | Default Value |
570*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
571*d4726bddSHONG Yifan| <a id="crate.spec-package"></a>package |  The explicit name of the package (used when attempting to alias a crate).   |  `None` |
572*d4726bddSHONG Yifan| <a id="crate.spec-version"></a>version |  The exact version of the crate. Cannot be used with `git`.   |  `None` |
573*d4726bddSHONG Yifan| <a id="crate.spec-artifact"></a>artifact |  Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo.   |  `None` |
574*d4726bddSHONG Yifan| <a id="crate.spec-lib"></a>lib |  If using `artifact = "bin"`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary.   |  `None` |
575*d4726bddSHONG Yifan| <a id="crate.spec-default_features"></a>default_features |  Maps to the `default-features` flag.   |  `True` |
576*d4726bddSHONG Yifan| <a id="crate.spec-features"></a>features |  A list of features to use for the crate   |  `[]` |
577*d4726bddSHONG Yifan| <a id="crate.spec-git"></a>git |  The Git url to use for the crate. Cannot be used with `version`.   |  `None` |
578*d4726bddSHONG Yifan| <a id="crate.spec-branch"></a>branch |  The git branch of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds.   |  `None` |
579*d4726bddSHONG Yifan| <a id="crate.spec-tag"></a>tag |  The git tag of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds.   |  `None` |
580*d4726bddSHONG Yifan| <a id="crate.spec-rev"></a>rev |  The git revision of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified.   |  `None` |
581*d4726bddSHONG Yifan
582*d4726bddSHONG Yifan**RETURNS**
583*d4726bddSHONG Yifan
584*d4726bddSHONG Yifanstring: A json encoded string of all inputs
585*d4726bddSHONG Yifan
586*d4726bddSHONG Yifan
587*d4726bddSHONG Yifan<a id="crate.workspace_member"></a>
588*d4726bddSHONG Yifan
589*d4726bddSHONG Yifan## crate.workspace_member
590*d4726bddSHONG Yifan
591*d4726bddSHONG Yifan<pre>
592*d4726bddSHONG Yifancrate.workspace_member(<a href="#crate.workspace_member-version">version</a>, <a href="#crate.workspace_member-sha256">sha256</a>)
593*d4726bddSHONG Yifan</pre>
594*d4726bddSHONG Yifan
595*d4726bddSHONG YifanDefine information for extra workspace members
596*d4726bddSHONG Yifan
597*d4726bddSHONG Yifan**PARAMETERS**
598*d4726bddSHONG Yifan
599*d4726bddSHONG Yifan
600*d4726bddSHONG Yifan| Name  | Description | Default Value |
601*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
602*d4726bddSHONG Yifan| <a id="crate.workspace_member-version"></a>version |  The semver of the crate to download. Must be an exact version.   |  none |
603*d4726bddSHONG Yifan| <a id="crate.workspace_member-sha256"></a>sha256 |  The sha256 checksum of the `.crate` file.   |  `None` |
604*d4726bddSHONG Yifan
605*d4726bddSHONG Yifan**RETURNS**
606*d4726bddSHONG Yifan
607*d4726bddSHONG Yifanstring: A json encoded string of all inputs
608*d4726bddSHONG Yifan
609*d4726bddSHONG Yifan
610*d4726bddSHONG Yifan<a id="crate_deps"></a>
611*d4726bddSHONG Yifan
612*d4726bddSHONG Yifan## crate_deps
613*d4726bddSHONG Yifan
614*d4726bddSHONG Yifan<pre>
615*d4726bddSHONG Yifancrate_deps(<a href="#crate_deps-deps">deps</a>, <a href="#crate_deps-package_name">package_name</a>)
616*d4726bddSHONG Yifan</pre>
617*d4726bddSHONG Yifan
618*d4726bddSHONG YifanFinds the fully qualified label of the requested crates for the package where this macro is called.
619*d4726bddSHONG Yifan
620*d4726bddSHONG Yifan**PARAMETERS**
621*d4726bddSHONG Yifan
622*d4726bddSHONG Yifan
623*d4726bddSHONG Yifan| Name  | Description | Default Value |
624*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
625*d4726bddSHONG Yifan| <a id="crate_deps-deps"></a>deps |  The desired list of crate targets.   |  none |
626*d4726bddSHONG Yifan| <a id="crate_deps-package_name"></a>package_name |  The package name of the set of dependencies to look up. Defaults to `native.package_name()`.   |  `None` |
627*d4726bddSHONG Yifan
628*d4726bddSHONG Yifan**RETURNS**
629*d4726bddSHONG Yifan
630*d4726bddSHONG Yifanlist: A list of labels to generated rust targets (str)
631*d4726bddSHONG Yifan
632*d4726bddSHONG Yifan
633*d4726bddSHONG Yifan<a id="crate_repositories"></a>
634*d4726bddSHONG Yifan
635*d4726bddSHONG Yifan## crate_repositories
636*d4726bddSHONG Yifan
637*d4726bddSHONG Yifan<pre>
638*d4726bddSHONG Yifancrate_repositories()
639*d4726bddSHONG Yifan</pre>
640*d4726bddSHONG Yifan
641*d4726bddSHONG YifanA macro for defining repositories for all generated crates.
642*d4726bddSHONG Yifan
643*d4726bddSHONG Yifan
644*d4726bddSHONG Yifan**RETURNS**
645*d4726bddSHONG Yifan
646*d4726bddSHONG YifanA list of repos visible to the module through the module extension.
647*d4726bddSHONG Yifan
648*d4726bddSHONG Yifan
649*d4726bddSHONG Yifan<a id="crate_universe_dependencies"></a>
650*d4726bddSHONG Yifan
651*d4726bddSHONG Yifan## crate_universe_dependencies
652*d4726bddSHONG Yifan
653*d4726bddSHONG Yifan<pre>
654*d4726bddSHONG Yifancrate_universe_dependencies(<a href="#crate_universe_dependencies-rust_version">rust_version</a>, <a href="#crate_universe_dependencies-bootstrap">bootstrap</a>, <a href="#crate_universe_dependencies-kwargs">kwargs</a>)
655*d4726bddSHONG Yifan</pre>
656*d4726bddSHONG Yifan
657*d4726bddSHONG YifanDefine dependencies of the `cargo-bazel` Rust target
658*d4726bddSHONG Yifan
659*d4726bddSHONG Yifan**PARAMETERS**
660*d4726bddSHONG Yifan
661*d4726bddSHONG Yifan
662*d4726bddSHONG Yifan| Name  | Description | Default Value |
663*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
664*d4726bddSHONG Yifan| <a id="crate_universe_dependencies-rust_version"></a>rust_version |  The version of rust to use when generating dependencies.   |  `"1.80.0"` |
665*d4726bddSHONG Yifan| <a id="crate_universe_dependencies-bootstrap"></a>bootstrap |  If true, a `cargo_bootstrap_repository` target will be generated.   |  `False` |
666*d4726bddSHONG Yifan| <a id="crate_universe_dependencies-kwargs"></a>kwargs |  Arguments to pass through to cargo_bazel_bootstrap.   |  none |
667*d4726bddSHONG Yifan
668*d4726bddSHONG Yifan**RETURNS**
669*d4726bddSHONG Yifan
670*d4726bddSHONG Yifanlist[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
671*d4726bddSHONG Yifan  defined by this macro.
672*d4726bddSHONG Yifan
673*d4726bddSHONG Yifan
674*d4726bddSHONG Yifan<a id="render_config"></a>
675*d4726bddSHONG Yifan
676*d4726bddSHONG Yifan## render_config
677*d4726bddSHONG Yifan
678*d4726bddSHONG Yifan<pre>
679*d4726bddSHONG Yifanrender_config(<a href="#render_config-build_file_template">build_file_template</a>, <a href="#render_config-crate_label_template">crate_label_template</a>, <a href="#render_config-crate_repository_template">crate_repository_template</a>,
680*d4726bddSHONG Yifan              <a href="#render_config-crates_module_template">crates_module_template</a>, <a href="#render_config-default_alias_rule">default_alias_rule</a>, <a href="#render_config-default_package_name">default_package_name</a>,
681*d4726bddSHONG Yifan              <a href="#render_config-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#render_config-platforms_template">platforms_template</a>, <a href="#render_config-regen_command">regen_command</a>, <a href="#render_config-vendor_mode">vendor_mode</a>,
682*d4726bddSHONG Yifan              <a href="#render_config-generate_rules_license_metadata">generate_rules_license_metadata</a>)
683*d4726bddSHONG Yifan</pre>
684*d4726bddSHONG Yifan
685*d4726bddSHONG YifanVarious settings used to configure rendered outputs
686*d4726bddSHONG Yifan
687*d4726bddSHONG YifanThe template parameters each support a select number of format keys. A description of each key
688*d4726bddSHONG Yifancan be found below where the supported keys for each template can be found in the parameter docs
689*d4726bddSHONG Yifan
690*d4726bddSHONG Yifan| key | definition |
691*d4726bddSHONG Yifan| --- | --- |
692*d4726bddSHONG Yifan| `name` | The name of the crate. Eg `tokio` |
693*d4726bddSHONG Yifan| `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. |
694*d4726bddSHONG Yifan| `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` |
695*d4726bddSHONG Yifan| `version` | The crate version. Eg `1.2.3` |
696*d4726bddSHONG Yifan| `target` | The library or binary target of the crate |
697*d4726bddSHONG Yifan| `file` | The basename of a file |
698*d4726bddSHONG Yifan
699*d4726bddSHONG Yifan
700*d4726bddSHONG Yifan**PARAMETERS**
701*d4726bddSHONG Yifan
702*d4726bddSHONG Yifan
703*d4726bddSHONG Yifan| Name  | Description | Default Value |
704*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
705*d4726bddSHONG Yifan| <a id="render_config-build_file_template"></a>build_file_template |  The base template to use for BUILD file names. The available format keys are [`{name}`, {version}`].   |  `"//:BUILD.{name}-{version}.bazel"` |
706*d4726bddSHONG Yifan| <a id="render_config-crate_label_template"></a>crate_label_template |  The base template to use for crate labels. The available format keys are [`{repository}`, `{name}`, `{version}`, `{target}`].   |  `"@{repository}__{name}-{version}//:{target}"` |
707*d4726bddSHONG Yifan| <a id="render_config-crate_repository_template"></a>crate_repository_template |  The base template to use for Crate label repository names. The available format keys are [`{repository}`, `{name}`, `{version}`].   |  `"{repository}__{name}-{version}"` |
708*d4726bddSHONG Yifan| <a id="render_config-crates_module_template"></a>crates_module_template |  The pattern to use for the `defs.bzl` and `BUILD.bazel` file names used for the crates module. The available format keys are [`{file}`].   |  `"//:{file}"` |
709*d4726bddSHONG Yifan| <a id="render_config-default_alias_rule"></a>default_alias_rule |  Alias rule to use when generating aliases for all crates.  Acceptable values are 'alias', 'dbg'/'fastbuild'/'opt' (transitions each crate's `compilation_mode`)  or a string representing a rule in the form '<label to .bzl>:<rule>' that takes a single label parameter 'actual'. See '@crate_index//:alias_rules.bzl' for an example.   |  `"alias"` |
710*d4726bddSHONG Yifan| <a id="render_config-default_package_name"></a>default_package_name |  The default package name to use in the rendered macros. This affects the auto package detection of things like `all_crate_deps`.   |  `None` |
711*d4726bddSHONG Yifan| <a id="render_config-generate_target_compatible_with"></a>generate_target_compatible_with |  Whether to generate `target_compatible_with` annotations on the generated BUILD files.  This catches a `target_triple`being targeted that isn't declared in `supported_platform_triples`.   |  `True` |
712*d4726bddSHONG Yifan| <a id="render_config-platforms_template"></a>platforms_template |  The base template to use for platform names. See [platforms documentation](https://docs.bazel.build/versions/main/platforms.html). The available format keys are [`{triple}`].   |  `"@rules_rust//rust/platform:{triple}"` |
713*d4726bddSHONG Yifan| <a id="render_config-regen_command"></a>regen_command |  An optional command to demonstrate how generated files should be regenerated.   |  `None` |
714*d4726bddSHONG Yifan| <a id="render_config-vendor_mode"></a>vendor_mode |  An optional configuration for rendirng content to be rendered into repositories.   |  `None` |
715*d4726bddSHONG Yifan| <a id="render_config-generate_rules_license_metadata"></a>generate_rules_license_metadata |  Whether to generate rules license metedata   |  `False` |
716*d4726bddSHONG Yifan
717*d4726bddSHONG Yifan**RETURNS**
718*d4726bddSHONG Yifan
719*d4726bddSHONG Yifanstring: A json encoded struct to match the Rust `config::RenderConfig` struct
720*d4726bddSHONG Yifan
721*d4726bddSHONG Yifan
722*d4726bddSHONG Yifan<a id="splicing_config"></a>
723*d4726bddSHONG Yifan
724*d4726bddSHONG Yifan## splicing_config
725*d4726bddSHONG Yifan
726*d4726bddSHONG Yifan<pre>
727*d4726bddSHONG Yifansplicing_config(<a href="#splicing_config-resolver_version">resolver_version</a>)
728*d4726bddSHONG Yifan</pre>
729*d4726bddSHONG Yifan
730*d4726bddSHONG YifanVarious settings used to configure Cargo manifest splicing behavior.
731*d4726bddSHONG Yifan
732*d4726bddSHONG Yifan[rv]: https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
733*d4726bddSHONG Yifan
734*d4726bddSHONG Yifan
735*d4726bddSHONG Yifan**PARAMETERS**
736*d4726bddSHONG Yifan
737*d4726bddSHONG Yifan
738*d4726bddSHONG Yifan| Name  | Description | Default Value |
739*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
740*d4726bddSHONG Yifan| <a id="splicing_config-resolver_version"></a>resolver_version |  The [resolver version][rv] to use in generated Cargo manifests. This flag is **only** used when splicing a manifest from direct package definitions. See `crates_repository::packages`.   |  `"2"` |
741*d4726bddSHONG Yifan
742*d4726bddSHONG Yifan**RETURNS**
743*d4726bddSHONG Yifan
744*d4726bddSHONG Yifanstr: A json encoded string of the parameters provided
745*d4726bddSHONG Yifan
746*d4726bddSHONG Yifan
747*d4726bddSHONG Yifan<a id="crates_repository"></a>
748*d4726bddSHONG Yifan
749*d4726bddSHONG Yifan## crates_repository
750*d4726bddSHONG Yifan
751*d4726bddSHONG Yifan<pre>
752*d4726bddSHONG Yifancrates_repository(<a href="#crates_repository-name">name</a>, <a href="#crates_repository-annotations">annotations</a>, <a href="#crates_repository-cargo_config">cargo_config</a>, <a href="#crates_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#crates_repository-generate_binaries">generate_binaries</a>,
753*d4726bddSHONG Yifan                  <a href="#crates_repository-generate_build_scripts">generate_build_scripts</a>, <a href="#crates_repository-generate_target_compatible_with">generate_target_compatible_with</a>, <a href="#crates_repository-generator">generator</a>,
754*d4726bddSHONG Yifan                  <a href="#crates_repository-generator_sha256s">generator_sha256s</a>, <a href="#crates_repository-generator_urls">generator_urls</a>, <a href="#crates_repository-isolated">isolated</a>, <a href="#crates_repository-lockfile">lockfile</a>, <a href="#crates_repository-manifests">manifests</a>, <a href="#crates_repository-packages">packages</a>, <a href="#crates_repository-quiet">quiet</a>,
755*d4726bddSHONG Yifan                  <a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repin_instructions">repin_instructions</a>, <a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
756*d4726bddSHONG Yifan                  <a href="#crates_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#crates_repository-rust_version">rust_version</a>, <a href="#crates_repository-splicing_config">splicing_config</a>,
757*d4726bddSHONG Yifan                  <a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>)
758*d4726bddSHONG Yifan</pre>
759*d4726bddSHONG Yifan
760*d4726bddSHONG YifanA rule for defining and downloading Rust dependencies (crates). This rule
761*d4726bddSHONG Yifanhandles all the same [workflows](#workflows) `crate_universe` rules do.
762*d4726bddSHONG Yifan
763*d4726bddSHONG YifanEnvironment Variables:
764*d4726bddSHONG Yifan
765*d4726bddSHONG Yifan| variable | usage |
766*d4726bddSHONG Yifan| --- | --- |
767*d4726bddSHONG Yifan| `CARGO_BAZEL_GENERATOR_SHA256` | The sha256 checksum of the file located at `CARGO_BAZEL_GENERATOR_URL` |
768*d4726bddSHONG Yifan| `CARGO_BAZEL_GENERATOR_URL` | The URL of a cargo-bazel binary. This variable takes precedence over attributes and can use `file://` for local paths |
769*d4726bddSHONG Yifan| `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration |
770*d4726bddSHONG Yifan| `CARGO_BAZEL_REPIN` | An indicator that the dependencies represented by the rule should be regenerated. `REPIN` may also be used. See [Repinning / Updating Dependencies](#repinning--updating-dependencies) for more details. |
771*d4726bddSHONG Yifan| `CARGO_BAZEL_REPIN_ONLY` | A comma-delimited allowlist for rules to execute repinning. Can be useful if multiple instances of the repository rule are used in a Bazel workspace, but repinning should be limited to one of them. |
772*d4726bddSHONG Yifan
773*d4726bddSHONG YifanExample:
774*d4726bddSHONG Yifan
775*d4726bddSHONG YifanGiven the following workspace structure:
776*d4726bddSHONG Yifan
777*d4726bddSHONG Yifan```text
778*d4726bddSHONG Yifan[workspace]/
779*d4726bddSHONG Yifan    WORKSPACE.bazel
780*d4726bddSHONG Yifan    BUILD.bazel
781*d4726bddSHONG Yifan    Cargo.toml
782*d4726bddSHONG Yifan    Cargo.Bazel.lock
783*d4726bddSHONG Yifan    src/
784*d4726bddSHONG Yifan        main.rs
785*d4726bddSHONG Yifan```
786*d4726bddSHONG Yifan
787*d4726bddSHONG YifanThe following is something that'd be found in the `WORKSPACE` file:
788*d4726bddSHONG Yifan
789*d4726bddSHONG Yifan```python
790*d4726bddSHONG Yifanload("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")
791*d4726bddSHONG Yifan
792*d4726bddSHONG Yifancrates_repository(
793*d4726bddSHONG Yifan    name = "crate_index",
794*d4726bddSHONG Yifan    annotations = {
795*d4726bddSHONG Yifan        "rand": [crate.annotation(
796*d4726bddSHONG Yifan            default_features = False,
797*d4726bddSHONG Yifan            features = ["small_rng"],
798*d4726bddSHONG Yifan        )],
799*d4726bddSHONG Yifan    },
800*d4726bddSHONG Yifan    cargo_lockfile = "//:Cargo.Bazel.lock",
801*d4726bddSHONG Yifan    lockfile = "//:cargo-bazel-lock.json",
802*d4726bddSHONG Yifan    manifests = ["//:Cargo.toml"],
803*d4726bddSHONG Yifan    # Should match the version represented by the currently registered `rust_toolchain`.
804*d4726bddSHONG Yifan    rust_version = "1.60.0",
805*d4726bddSHONG Yifan)
806*d4726bddSHONG Yifan```
807*d4726bddSHONG Yifan
808*d4726bddSHONG YifanThe above will create an external repository which contains aliases and macros for accessing
809*d4726bddSHONG YifanRust targets found in the dependency graph defined by the given manifests.
810*d4726bddSHONG Yifan
811*d4726bddSHONG Yifan**NOTE**: The `cargo_lockfile` and `lockfile` must be manually created. The rule unfortunately does not yet create
812*d4726bddSHONG Yifanit on its own. When initially setting up this rule, an empty file should be created and then
813*d4726bddSHONG Yifanpopulated by repinning dependencies.
814*d4726bddSHONG Yifan
815*d4726bddSHONG Yifan### Repinning / Updating Dependencies
816*d4726bddSHONG Yifan
817*d4726bddSHONG YifanDependency syncing and updating is done in the repository rule which means it's done during the
818*d4726bddSHONG Yifananalysis phase of builds. As mentioned in the environments variable table above, the `CARGO_BAZEL_REPIN`
819*d4726bddSHONG Yifan(or `REPIN`) environment variables can be used to force the rule to update dependencies and potentially
820*d4726bddSHONG Yifanrender a new lockfile. Given an instance of this repository rule named `crate_index`, the easiest way to
821*d4726bddSHONG Yifanrepin dependencies is to run:
822*d4726bddSHONG Yifan
823*d4726bddSHONG Yifan```shell
824*d4726bddSHONG YifanCARGO_BAZEL_REPIN=1 bazel sync --only=crate_index
825*d4726bddSHONG Yifan```
826*d4726bddSHONG Yifan
827*d4726bddSHONG YifanThis will result in all dependencies being updated for a project. The `CARGO_BAZEL_REPIN` environment variable
828*d4726bddSHONG Yifancan also be used to customize how dependencies are updated. The following table shows translations from environment
829*d4726bddSHONG Yifanvariable values to the equivilant [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) command
830*d4726bddSHONG Yifanthat is called behind the scenes to update dependencies.
831*d4726bddSHONG Yifan
832*d4726bddSHONG Yifan| Value | Cargo command |
833*d4726bddSHONG Yifan| --- | --- |
834*d4726bddSHONG Yifan| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` |
835*d4726bddSHONG Yifan| Any of [`full`, `eager`, `all`] | `cargo update` |
836*d4726bddSHONG Yifan| `package_name` | `cargo upgrade --package package_name` |
837*d4726bddSHONG Yifan| `[email protected]` | `cargo upgrade --package [email protected]` |
838*d4726bddSHONG Yifan| `[email protected]=4.5.6` | `cargo upgrade --package [email protected] --precise=4.5.6` |
839*d4726bddSHONG Yifan
840*d4726bddSHONG YifanIf the `crates_repository` is used multiple times in the same Bazel workspace (e.g. for multiple independent
841*d4726bddSHONG YifanRust workspaces), it may additionally be useful to use the `CARGO_BAZEL_REPIN_ONLY` environment variable, which
842*d4726bddSHONG Yifanlimits execution of the repinning to one or multiple instances of the `crates_repository` rule via a comma-delimited
843*d4726bddSHONG Yifanallowlist:
844*d4726bddSHONG Yifan
845*d4726bddSHONG Yifan```shell
846*d4726bddSHONG YifanCARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_index
847*d4726bddSHONG Yifan```
848*d4726bddSHONG Yifan
849*d4726bddSHONG Yifan**ATTRIBUTES**
850*d4726bddSHONG Yifan
851*d4726bddSHONG Yifan
852*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
853*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
854*d4726bddSHONG Yifan| <a id="crates_repository-name"></a>name |  A unique name for this repository.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
855*d4726bddSHONG Yifan| <a id="crates_repository-annotations"></a>annotations |  Extra settings to apply to crates. See [crate.annotation](#crateannotation).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> List of strings</a> | optional |  `{}`  |
856*d4726bddSHONG Yifan| <a id="crates_repository-cargo_config"></a>cargo_config |  A [Cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html) file   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
857*d4726bddSHONG Yifan| <a id="crates_repository-cargo_lockfile"></a>cargo_lockfile |  The path used to store the `crates_repository` specific [Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. In the case that your `crates_repository` corresponds directly with an existing `Cargo.toml` file which has a paired `Cargo.lock` file, that `Cargo.lock` file should be used here, which will keep the versions used by cargo and bazel in sync.   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
858*d4726bddSHONG Yifan| <a id="crates_repository-generate_binaries"></a>generate_binaries |  Whether to generate `rust_binary` targets for all the binary crates in every package. By default only the `rust_library` targets are generated.   | Boolean | optional |  `False`  |
859*d4726bddSHONG Yifan| <a id="crates_repository-generate_build_scripts"></a>generate_build_scripts |  Whether or not to generate [cargo build scripts](https://doc.rust-lang.org/cargo/reference/build-scripts.html) by default.   | Boolean | optional |  `True`  |
860*d4726bddSHONG Yifan| <a id="crates_repository-generate_target_compatible_with"></a>generate_target_compatible_with |  DEPRECATED: Moved to `render_config`.   | Boolean | optional |  `True`  |
861*d4726bddSHONG Yifan| <a id="crates_repository-generator"></a>generator |  The absolute label of a generator. Eg. `@cargo_bazel_bootstrap//:cargo-bazel`. This is typically used when bootstrapping   | String | optional |  `""`  |
862*d4726bddSHONG Yifan| <a id="crates_repository-generator_sha256s"></a>generator_sha256s |  Dictionary of `host_triple` -> `sha256` for a `cargo-bazel` binary.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
863*d4726bddSHONG Yifan| <a id="crates_repository-generator_urls"></a>generator_urls |  URL template from which to download the `cargo-bazel` binary. `{host_triple}` and will be filled in according to the host platform.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
864*d4726bddSHONG Yifan| <a id="crates_repository-isolated"></a>isolated |  If true, `CARGO_HOME` will be overwritten to a directory within the generated repository in order to prevent other uses of Cargo from impacting having any effect on the generated targets produced by this rule. For users who either have multiple `crate_repository` definitions in a WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This variable is also controled by `CARGO_BAZEL_ISOLATED` environment variable.   | Boolean | optional |  `True`  |
865*d4726bddSHONG Yifan| <a id="crates_repository-lockfile"></a>lockfile |  The path to a file to use for reproducible renderings. If set, this file must exist within the workspace (but can be empty) before this rule will work.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
866*d4726bddSHONG Yifan| <a id="crates_repository-manifests"></a>manifests |  A list of Cargo manifests (`Cargo.toml` files).   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
867*d4726bddSHONG Yifan| <a id="crates_repository-packages"></a>packages |  A set of crates (packages) specifications to depend on. See [crate.spec](#crate.spec).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
868*d4726bddSHONG Yifan| <a id="crates_repository-quiet"></a>quiet |  If stdout and stderr should not be printed to the terminal.   | Boolean | optional |  `True`  |
869*d4726bddSHONG Yifan| <a id="crates_repository-render_config"></a>render_config |  The configuration flags to use for rendering. Use `//crate_universe:defs.bzl\%render_config` to generate the value for this field. If unset, the defaults defined there will be used.   | String | optional |  `""`  |
870*d4726bddSHONG Yifan| <a id="crates_repository-repin_instructions"></a>repin_instructions |  Instructions to re-pin the repository if required. Many people have wrapper scripts for keeping dependencies up to date, and would like to point users to that instead of the default.   | String | optional |  `""`  |
871*d4726bddSHONG Yifan| <a id="crates_repository-repo_mapping"></a>repo_mapping |  In `WORKSPACE` context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<br><br>For example, an entry `"@foo": "@bar"` declares that, for any time this repository depends on `@foo` (such as a dependency on `@foo//some:target`, it should actually resolve that dependency within globally-declared `@bar` (`@bar//some:target`).<br><br>This attribute is _not_ supported in `MODULE.bazel` context (when invoking a repository rule inside a module extension's implementation function).   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  |
872*d4726bddSHONG Yifan| <a id="crates_repository-rust_toolchain_cargo_template"></a>rust_toolchain_cargo_template |  The template to use for finding the host `cargo` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'rustc.exe') will be replaced in the string if present.   | String | optional |  `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"`  |
873*d4726bddSHONG Yifan| <a id="crates_repository-rust_toolchain_rustc_template"></a>rust_toolchain_rustc_template |  The template to use for finding the host `rustc` binary. `{version}` (eg. '1.53.0'), `{triple}` (eg. 'x86_64-unknown-linux-gnu'), `{arch}` (eg. 'aarch64'), `{vendor}` (eg. 'unknown'), `{system}` (eg. 'darwin'), `{cfg}` (eg. 'exec'), `{channel}` (eg. 'stable'), and `{tool}` (eg. 'cargo.exe') will be replaced in the string if present.   | String | optional |  `"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"`  |
874*d4726bddSHONG Yifan| <a id="crates_repository-rust_version"></a>rust_version |  The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08`   | String | optional |  `"1.80.0"`  |
875*d4726bddSHONG Yifan| <a id="crates_repository-splicing_config"></a>splicing_config |  The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\%rsplicing_config` to generate the value for this field. If unset, the defaults defined there will be used.   | String | optional |  `""`  |
876*d4726bddSHONG Yifan| <a id="crates_repository-supported_platform_triples"></a>supported_platform_triples |  A set of all platform triples to consider when generating dependencies.   | List of strings | optional |  `["aarch64-unknown-linux-gnu", "aarch64-unknown-nixos-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "x86_64-unknown-nixos-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "thumbv8m.main-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", "x86_64-unknown-none", "aarch64-unknown-nto-qnx710"]`  |
877*d4726bddSHONG Yifan
878*d4726bddSHONG Yifan**ENVIRONMENT VARIABLES**
879*d4726bddSHONG Yifan
880*d4726bddSHONG YifanThis repository rule depends on the following environment variables:
881*d4726bddSHONG Yifan* `CARGO_BAZEL_GENERATOR_URL`
882*d4726bddSHONG Yifan* `CARGO_BAZEL_GENERATOR_SHA256`
883*d4726bddSHONG Yifan* `CARGO_BAZEL_REPIN`
884*d4726bddSHONG Yifan* `REPIN`
885*d4726bddSHONG Yifan* `CARGO_BAZEL_REPIN_ONLY`
886*d4726bddSHONG Yifan* `CARGO_BAZEL_ISOLATED`
887*d4726bddSHONG Yifan* `CARGO_BAZEL_DEBUG`
888*d4726bddSHONG Yifan
889*d4726bddSHONG Yifan
890