blob: 850e080f20f30174df79ad085cdb20e933dc2198 [file] [log] [blame] [view]
Alex Eagle971ff662021-05-17 09:13:00 -07001<!-- Generated with Stardoc: http://skydoc.bazel.build -->
UebelAndre59fab4e2022-03-03 06:49:27 -08002
UebelAndre2ccd2bc2021-04-27 07:39:30 -07003# Crate Universe
4
UebelAndre59fab4e2022-03-03 06:49:27 -08005Crate Universe is a set of Bazel rule for generating Rust targets using Cargo.
UebelAndre467a3012021-04-23 08:18:07 -07006
UebelAndre3205c9d2022-03-21 07:31:35 -07007## Setup
8
9After loading `rules_rust` in your workspace, set the following to begin using `crate_universe`:
10
11```python
UebelAndre628e85e2022-03-21 16:48:20 -070012load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies")
UebelAndre3205c9d2022-03-21 07:31:35 -070013
UebelAndre628e85e2022-03-21 16:48:20 -070014crate_universe_dependencies()
UebelAndre3205c9d2022-03-21 07:31:35 -070015```
16
17Note that if the current version of `rules_rust` is not a release artifact, you may need to set additional
UebelAndre628e85e2022-03-21 16:48:20 -070018flags such as [`bootstrap = True`](#crate_universe_dependencies-bootstrap) on the `crate_universe_dependencies`
UebelAndre3205c9d2022-03-21 07:31:35 -070019call above or [crates_repository::generator_urls](#crates_repository-generator_urls) in uses of `crates_repository`.
20
UebelAndre59fab4e2022-03-03 06:49:27 -080021## Rules
UebelAndre0c74f0e2021-07-10 05:17:19 -070022
UebelAndre59fab4e2022-03-03 06:49:27 -080023- [crates_repository](#crates_repository)
24- [crates_vendor](#crates_vendor)
UebelAndree07881f2022-04-27 09:58:02 -070025
26## Utility Macros
27
28- [crate_universe_dependencies](#crate_universe_dependencies)
UebelAndre3205c9d2022-03-21 07:31:35 -070029- [crate.annotation](#crateannotation)
James Leitch40232a02024-01-03 12:16:57 -070030- [crate.select](#crateselect)
UebelAndre59fab4e2022-03-03 06:49:27 -080031- [crate.spec](#cratespec)
32- [crate.workspace_member](#crateworkspace_member)
UebelAndre59fab4e2022-03-03 06:49:27 -080033- [render_config](#render_config)
34- [splicing_config](#splicing_config)
UebelAndre0c74f0e2021-07-10 05:17:19 -070035
UebelAndree07881f2022-04-27 09:58:02 -070036## Workflows
UebelAndre0c74f0e2021-07-10 05:17:19 -070037
UebelAndre42f78f22022-05-03 06:52:55 -070038The [`crates_repository`](#crates_repository) rule (the primary repository rule of `rules_rust`'s cargo support) supports a number of different
39ways users can express and organize their dependencies. The most common are listed below though there are more to be found in
UebelAndre59fab4e2022-03-03 06:49:27 -080040the [./examples/crate_universe](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe) directory.
UebelAndre0c74f0e2021-07-10 05:17:19 -070041
UebelAndre59fab4e2022-03-03 06:49:27 -080042### Cargo Workspaces
UebelAndre0c74f0e2021-07-10 05:17:19 -070043
UebelAndre59fab4e2022-03-03 06:49:27 -080044One of the simpler ways to wire up dependencies would be to first structure your project into a [Cargo workspace][cw].
UebelAndrefd912e62022-03-21 11:24:54 -070045The `crates_repository` rule can ingest a root `Cargo.toml` file and generate dependencies from there.
UebelAndre0c74f0e2021-07-10 05:17:19 -070046
47```python
UebelAndrefd912e62022-03-21 11:24:54 -070048load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
UebelAndre0c74f0e2021-07-10 05:17:19 -070049
UebelAndre59fab4e2022-03-03 06:49:27 -080050crates_repository(
51 name = "crate_index",
Daniel Wagner-Hallb7c36c02022-11-29 14:16:48 +000052 cargo_lockfile = "//:Cargo.lock",
UebelAndre59fab4e2022-03-03 06:49:27 -080053 lockfile = "//:Cargo.Bazel.lock",
54 manifests = ["//:Cargo.toml"],
55)
UebelAndre0c74f0e2021-07-10 05:17:19 -070056
UebelAndre59fab4e2022-03-03 06:49:27 -080057load("@crate_index//:defs.bzl", "crate_repositories")
58
59crate_repositories()
60```
61
62The generated `crates_repository` contains helper macros which make collecting dependencies for Bazel targets simpler.
UebelAndre42f78f22022-05-03 06:52:55 -070063Notably, the `all_crate_deps` and `aliases` macros (see [Dependencies API](#dependencies-api)) commonly allow the
64`Cargo.toml` files to be the single source of truth for dependencies. Since these macros come from the generated
65repository, the dependencies and alias definitions they return will automatically update BUILD targets.
UebelAndre59fab4e2022-03-03 06:49:27 -080066
67```python
68load("@crate_index//:defs.bzl", "aliases", "all_crate_deps")
69load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
70
71rust_library(
72 name = "lib",
73 aliases = aliases(),
74 deps = all_crate_deps(
75 normal = True,
76 ),
77 proc_macro_deps = all_crate_deps(
78 proc_macro = True,
79 ),
80)
81
82rust_test(
83 name = "unit_test",
84 crate = ":lib",
85 aliases = aliases(
86 normal_dev = True,
87 proc_macro_dev = True,
88 ),
89 deps = all_crate_deps(
90 normal_dev = True,
91 ),
92 proc_macro_deps = all_crate_deps(
93 proc_macro_dev = True,
94 ),
95)
96```
97
98### Direct Packages
99
100In cases where Rust targets have heavy interractions with other Bazel targests ([Cc][cc], [Proto][proto], etc.),
101maintaining `Cargo.toml` files may have deminishing returns as things like [rust-analyzer][ra] begin to be confused
102about missing targets or environment variables defined only in Bazel. In workspaces like this, it may be desirable
103to have a "Cargo free" setup. `crates_repository` supports this through the `packages` attribute.
104
105```python
Daniel Wagner-Hallb7c36c02022-11-29 14:16:48 +0000106load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "render_config")
UebelAndre59fab4e2022-03-03 06:49:27 -0800107
108crates_repository(
109 name = "crate_index",
Daniel Wagner-Hallb7c36c02022-11-29 14:16:48 +0000110 cargo_lockfile = "//:Cargo.lock",
UebelAndre59fab4e2022-03-03 06:49:27 -0800111 lockfile = "//:Cargo.Bazel.lock",
112 packages = {
113 "async-trait": crate.spec(
114 version = "0.1.51",
115 ),
116 "mockall": crate.spec(
117 version = "0.10.2",
118 ),
119 "tokio": crate.spec(
120 version = "1.12.0",
UebelAndre0c74f0e2021-07-10 05:17:19 -0700121 ),
122 },
UebelAndre59fab4e2022-03-03 06:49:27 -0800123 # Setting the default package name to `""` forces the use of the macros defined in this repository
124 # to always use the root package when looking for dependencies or aliases. This should be considered
125 # optional as the repository also exposes alises for easy access to all dependencies.
126 render_config = render_config(
127 default_package_name = ""
128 ),
UebelAndre0c74f0e2021-07-10 05:17:19 -0700129)
130
UebelAndre59fab4e2022-03-03 06:49:27 -0800131load("@crate_index//:defs.bzl", "crate_repositories")
UebelAndre0c74f0e2021-07-10 05:17:19 -0700132
UebelAndre59fab4e2022-03-03 06:49:27 -0800133crate_repositories()
UebelAndre0c74f0e2021-07-10 05:17:19 -0700134```
135
UebelAndre59fab4e2022-03-03 06:49:27 -0800136Consuming dependencies may be more ergonomic in this case through the aliases defined in the new repository.
UebelAndre0c74f0e2021-07-10 05:17:19 -0700137
138```python
UebelAndre59fab4e2022-03-03 06:49:27 -0800139load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
UebelAndre0c74f0e2021-07-10 05:17:19 -0700140
141rust_library(
UebelAndre59fab4e2022-03-03 06:49:27 -0800142 name = "lib",
143 deps = [
144 "@crate_index//:tokio",
UebelAndre0c74f0e2021-07-10 05:17:19 -0700145 ],
UebelAndre59fab4e2022-03-03 06:49:27 -0800146 proc_macro_deps = [
147 "@crate_index//:async-trait",
148 ],
149)
150
151rust_test(
152 name = "unit_test",
153 crate = ":lib",
154 deps = [
155 "@crate_index//:mockall",
156 ],
UebelAndre0c74f0e2021-07-10 05:17:19 -0700157)
158```
159
David Tolnay36b57af2023-01-03 13:12:33 -0800160### Binary dependencies
161
162Neither of the above approaches supports depending on binary-only packages.
163
164In order to depend on a Cargo package that contains binaries and no library, you
165will need to do one of the following:
166
167- Fork the package to add an empty lib.rs, which makes the package visible to
168 Cargo metadata and compatible with the above approaches;
169
170- Or handwrite your own build target for the binary, use `http_archive` to
171 import its source code, and use `crates_repository` to make build targets for
172 its dependencies. This is demonstrated below using the `rustfilt` crate as an
173 example.
174
175```python
176# in WORKSPACE.bazel
177
178load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
179
180http_archive(
181 name = "rustfilt",
182 build_file = "//rustfilt:BUILD.rustfilt.bazel",
183 sha256 = "c8d748b182c8f95224336d20dcc5609598af612581ce60cfb29da4dc8d0091f2",
184 strip_prefix = "rustfilt-0.2.1",
185 type = "tar.gz",
186 urls = ["https://crates.io/api/v1/crates/rustfilt/0.2.1/download"],
187)
188
189load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
190
191crates_repository(
192 name = "rustfilt_deps",
193 cargo_lockfile = "//rustfilt:Cargo.lock",
194 manifests = ["@rustfilt//:Cargo.toml"],
195)
196
197load("@rustfilt_deps//:defs.bzl", rustfilt_deps = "crate_repositories")
198
199rustfilt_deps()
200```
201
202```python
203# in rustfilt/BUILD.rustfilt.bazel
204
205load("@rules_rust//rust:defs.bzl", "rust_binary")
206
207rust_binary(
208 name = "rustfilt",
209 srcs = glob(["src/**/*.rs"]),
210 edition = "2018",
211 deps = [
212 "@rustfilt_deps//:clap",
213 "@rustfilt_deps//:lazy_static",
214 "@rustfilt_deps//:regex",
215 "@rustfilt_deps//:rustc-demangle",
216 ],
217)
218```
219
220If you use either `crates_repository` or `crates_vendor` to depend on a Cargo
221package that contains _both_ a library crate _and_ binaries, by default only the
222library gets made available to Bazel. To generate Bazel targets for the binary
223crates as well, you must opt in to it with an annotation on the package:
224
225```python
226load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")
227
228crates_repository(
229 name = "crate_index",
230 annotations = {
231 "thepackage": [crate.annotation(
232 gen_binaries = True,
233 # Or, to expose just a subset of the package's binaries by name:
234 gen_binaries = ["rustfilt"],
235 )],
236 },
237 # Or, to expose every binary of every package:
238 generate_binaries = True,
239 ...
240)
241```
242
UebelAndre42f78f22022-05-03 06:52:55 -0700243## Dependencies API
244
245After rendering dependencies, convenience macros may also be generated to provide
246convenient accessors to larger sections of the dependency graph.
247
248- [aliases](#aliases)
249- [crate_deps](#crate_deps)
250- [all_crate_deps](#all_crate_deps)
251- [crate_repositories](#crate_repositories)
252
253---
254
255---
256
UebelAndre59fab4e2022-03-03 06:49:27 -0800257[cw]: https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html
258[cc]: https://docs.bazel.build/versions/main/be/c-cpp.html
259[proto]: https://rules-proto-grpc.com/en/latest/lang/rust.html
260[ra]: https://rust-analyzer.github.io/
UebelAndre0c74f0e2021-07-10 05:17:19 -0700261
262
Brian Silverman38e841a2022-07-11 08:15:49 -0700263<a id="crates_repository"></a>
UebelAndre467a3012021-04-23 08:18:07 -0700264
UebelAndre59fab4e2022-03-03 06:49:27 -0800265## crates_repository
UebelAndre467a3012021-04-23 08:18:07 -0700266
267<pre>
David Tolnay36b57af2023-01-03 13:12:33 -0800268crates_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>,
James Leitch23f99bb2023-06-16 05:58:57 -0700269 <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>,
270 <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>,
271 <a href="#crates_repository-render_config">render_config</a>, <a href="#crates_repository-repo_mapping">repo_mapping</a>, <a href="#crates_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
272 <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>,
273 <a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>)
UebelAndre467a3012021-04-23 08:18:07 -0700274</pre>
275
UebelAndree07881f2022-04-27 09:58:02 -0700276A rule for defining and downloading Rust dependencies (crates). This rule
277handles all the same [workflows](#workflows) `crate_universe` rules do.
UebelAndre467a3012021-04-23 08:18:07 -0700278
279Environment Variables:
UebelAndre59fab4e2022-03-03 06:49:27 -0800280
281| variable | usage |
282| --- | --- |
283| `CARGO_BAZEL_GENERATOR_SHA256` | The sha256 checksum of the file located at `CARGO_BAZEL_GENERATOR_URL` |
284| `CARGO_BAZEL_GENERATOR_URL` | The URL of a cargo-bazel binary. This variable takes precedence over attributes and can use `file://` for local paths |
285| `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration |
Daniel Wagner-Hallc1632b52023-06-02 10:16:17 +0100286| `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. |
Maximilian Goisserf651cd12023-01-25 20:39:00 +0100287| `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. |
UebelAndre59fab4e2022-03-03 06:49:27 -0800288
UebelAndree07881f2022-04-27 09:58:02 -0700289Example:
290
291Given the following workspace structure:
UebelAndre3d652142022-07-04 08:12:42 -0700292
293```text
UebelAndree07881f2022-04-27 09:58:02 -0700294[workspace]/
UebelAndreacd9e612024-02-19 03:15:56 -0800295 WORKSPACE.bazel
296 BUILD.bazel
UebelAndree07881f2022-04-27 09:58:02 -0700297 Cargo.toml
298 Cargo.Bazel.lock
299 src/
300 main.rs
301```
302
303The following is something that'd be found in the `WORKSPACE` file:
304
305```python
306load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate")
307
308crates_repository(
309 name = "crate_index",
Tetsuo Kiso055abd42022-08-18 20:40:43 +0900310 annotations = {
UebelAndree07881f2022-04-27 09:58:02 -0700311 "rand": [crate.annotation(
312 default_features = False,
313 features = ["small_rng"],
314 )],
315 },
UebelAndre3d652142022-07-04 08:12:42 -0700316 cargo_lockfile = "//:Cargo.Bazel.lock",
317 lockfile = "//:cargo-bazel-lock.json",
UebelAndree07881f2022-04-27 09:58:02 -0700318 manifests = ["//:Cargo.toml"],
319 # Should match the version represented by the currently registered `rust_toolchain`.
320 rust_version = "1.60.0",
321)
322```
323
324The above will create an external repository which contains aliases and macros for accessing
325Rust targets found in the dependency graph defined by the given manifests.
326
Daniel Wagner-Hallb7c36c02022-11-29 14:16:48 +0000327**NOTE**: The `cargo_lockfile` and `lockfile` must be manually created. The rule unfortunately does not yet create
UebelAndre42f78f22022-05-03 06:52:55 -0700328it on its own. When initially setting up this rule, an empty file should be created and then
329populated by repinning dependencies.
UebelAndree07881f2022-04-27 09:58:02 -0700330
331### Repinning / Updating Dependencies
332
333Dependency syncing and updating is done in the repository rule which means it's done during the
334analysis phase of builds. As mentioned in the environments variable table above, the `CARGO_BAZEL_REPIN`
335(or `REPIN`) environment variables can be used to force the rule to update dependencies and potentially
336render a new lockfile. Given an instance of this repository rule named `crate_index`, the easiest way to
337repin dependencies is to run:
338
339```shell
340CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index
341```
342
UebelAndre3d652142022-07-04 08:12:42 -0700343This will result in all dependencies being updated for a project. The `CARGO_BAZEL_REPIN` environment variable
344can also be used to customize how dependencies are updated. The following table shows translations from environment
345variable values to the equivilant [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) command
346that is called behind the scenes to update dependencies.
347
348| Value | Cargo command |
349| --- | --- |
UebelAndred916a6f2023-01-03 12:34:11 -0800350| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` |
351| Any of [`full`, `eager`, `all`] | `cargo update` |
UebelAndre3d652142022-07-04 08:12:42 -0700352| `package_name` | `cargo upgrade --package package_name` |
UebelAndreacd9e612024-02-19 03:15:56 -0800353| `package_name@1.2.3` | `cargo upgrade --package package_name@1.2.3` |
354| `package_name@1.2.3=4.5.6` | `cargo upgrade --package package_name@1.2.3 --precise=4.5.6` |
UebelAndre3d652142022-07-04 08:12:42 -0700355
Maximilian Goisserf651cd12023-01-25 20:39:00 +0100356If the `crates_repository` is used multiple times in the same Bazel workspace (e.g. for multiple independent
357Rust workspaces), it may additionally be useful to use the `CARGO_BAZEL_REPIN_ONLY` environment variable, which
358limits execution of the repinning to one or multiple instances of the `crates_repository` rule via a comma-delimited
359allowlist:
360
361```shell
362CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_index
363```
364
UebelAndre467a3012021-04-23 08:18:07 -0700365
366
367**ATTRIBUTES**
368
369
370| Name | Description | Type | Mandatory | Default |
371| :------------- | :------------- | :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000372| <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 | |
373| <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 | <code>{}</code> |
374| <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 | <code>None</code> |
375| <a id="crates_repository-cargo_lockfile"></a>cargo_lockfile | The path used to store the <code>crates_repository</code> specific [Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) file. In the case that your <code>crates_repository</code> corresponds directly with an existing <code>Cargo.toml</code> file which has a paired <code>Cargo.lock</code> file, that <code>Cargo.lock</code> 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 | |
David Tolnay36b57af2023-01-03 13:12:33 -0800376| <a id="crates_repository-generate_binaries"></a>generate_binaries | Whether to generate <code>rust_binary</code> targets for all the binary crates in every package. By default only the <code>rust_library</code> targets are generated. | Boolean | optional | <code>False</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000377| <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 | <code>True</code> |
James Leitch2a5ca6a2023-12-14 08:47:10 -0800378| <a id="crates_repository-generate_target_compatible_with"></a>generate_target_compatible_with | DEPRECATED: Moved to <code>render_config</code>. | Boolean | optional | <code>True</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000379| <a id="crates_repository-generator"></a>generator | The absolute label of a generator. Eg. <code>@cargo_bazel_bootstrap//:cargo-bazel</code>. This is typically used when bootstrapping | String | optional | <code>""</code> |
380| <a id="crates_repository-generator_sha256s"></a>generator_sha256s | Dictionary of <code>host_triple</code> -&gt; <code>sha256</code> for a <code>cargo-bazel</code> binary. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
381| <a id="crates_repository-generator_urls"></a>generator_urls | URL template from which to download the <code>cargo-bazel</code> binary. <code>{host_triple}</code> and will be filled in according to the host platform. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{}</code> |
382| <a id="crates_repository-isolated"></a>isolated | If true, <code>CARGO_HOME</code> 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 <code>crate_repository</code> definitions in a WORKSPACE or rapidly re-pin dependencies, setting this to false may improve build times. This variable is also controled by <code>CARGO_BAZEL_ISOLATED</code> environment variable. | Boolean | optional | <code>True</code> |
383| <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 | <code>None</code> |
384| <a id="crates_repository-manifests"></a>manifests | A list of Cargo manifests (<code>Cargo.toml</code> files). | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
385| <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 | <code>{}</code> |
386| <a id="crates_repository-quiet"></a>quiet | If stdout and stderr should not be printed to the terminal. | Boolean | optional | <code>True</code> |
387| <a id="crates_repository-render_config"></a>render_config | The configuration flags to use for rendering. Use <code>//crate_universe:defs.bzl\%render_config</code> to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | <code>""</code> |
388| <a id="crates_repository-repo_mapping"></a>repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.&lt;p&gt;For example, an entry <code>"@foo": "@bar"</code> declares that, for any time this repository depends on <code>@foo</code> (such as a dependency on <code>@foo//some:target</code>, it should actually resolve that dependency within globally-declared <code>@bar</code> (<code>@bar//some:target</code>). | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | required | |
UebelAndre46b7ea52022-12-06 08:26:19 -0800389| <a id="crates_repository-rust_toolchain_cargo_template"></a>rust_toolchain_cargo_template | The template to use for finding the host <code>cargo</code> binary. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{arch}</code> (eg. 'aarch64'), <code>{vendor}</code> (eg. 'unknown'), <code>{system}</code> (eg. 'darwin'), <code>{cfg}</code> (eg. 'exec'), <code>{channel}</code> (eg. 'stable'), and <code>{tool}</code> (eg. 'rustc.exe') will be replaced in the string if present. | String | optional | <code>"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"</code> |
390| <a id="crates_repository-rust_toolchain_rustc_template"></a>rust_toolchain_rustc_template | The template to use for finding the host <code>rustc</code> binary. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{arch}</code> (eg. 'aarch64'), <code>{vendor}</code> (eg. 'unknown'), <code>{system}</code> (eg. 'darwin'), <code>{cfg}</code> (eg. 'exec'), <code>{channel}</code> (eg. 'stable'), and <code>{tool}</code> (eg. 'cargo.exe') will be replaced in the string if present. | String | optional | <code>"@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}"</code> |
UebelAndre768eaf42024-04-10 01:58:26 -0700391| <a id="crates_repository-rust_version"></a>rust_version | The version of Rust the currently registered toolchain is using. Eg. <code>1.56.0</code>, or <code>nightly/2021-09-08</code> | String | optional | <code>"1.77.2"</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000392| <a id="crates_repository-splicing_config"></a>splicing_config | The configuration flags to use for splicing Cargo maniests. Use <code>//crate_universe:defs.bzl\%rsplicing_config</code> to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | <code>""</code> |
John Hughesc3581a52023-12-13 17:27:47 +0100393| <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 | <code>["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"]</code> |
UebelAndre59fab4e2022-03-03 06:49:27 -0800394
395
Brian Silverman38e841a2022-07-11 08:15:49 -0700396<a id="crates_vendor"></a>
UebelAndre59fab4e2022-03-03 06:49:27 -0800397
398## crates_vendor
399
400<pre>
David Koloskid4b31a42022-11-30 11:18:01 -0500401crates_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>,
James Leitch23f99bb2023-06-16 05:58:57 -0700402 <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>,
403 <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>,
404 <a href="#crates_vendor-supported_platform_triples">supported_platform_triples</a>, <a href="#crates_vendor-vendor_path">vendor_path</a>)
UebelAndre59fab4e2022-03-03 06:49:27 -0800405</pre>
406
UebelAndree07881f2022-04-27 09:58:02 -0700407A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace.
408This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the
409rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule
410handles all the same [workflows](#workflows) `crate_universe` rules do.
411
James Leitch23f99bb2023-06-16 05:58:57 -0700412Example:
UebelAndree07881f2022-04-27 09:58:02 -0700413
414Given the following workspace structure:
UebelAndre3d652142022-07-04 08:12:42 -0700415
416```text
UebelAndree07881f2022-04-27 09:58:02 -0700417[workspace]/
418 WORKSPACE
419 BUILD
420 Cargo.toml
421 3rdparty/
422 BUILD
423 src/
424 main.rs
425```
426
427The following is something that'd be found in `3rdparty/BUILD`:
428
429```python
430load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate")
431
432crates_vendor(
433 name = "crates_vendor",
434 annotations = {
435 "rand": [crate.annotation(
436 default_features = False,
437 features = ["small_rng"],
438 )],
439 },
UebelAndre3d652142022-07-04 08:12:42 -0700440 cargo_lockfile = "//:Cargo.Bazel.lock",
UebelAndree07881f2022-04-27 09:58:02 -0700441 manifests = ["//:Cargo.toml"],
442 mode = "remote",
443 vendor_path = "crates",
444 tags = ["manual"],
445)
446```
447
448The above creates a target that can be run to write `BUILD` files into the `3rdparty`
449directory next to where the target is defined. To run it, simply call:
450
451```shell
452bazel run //3rdparty:crates_vendor
453```
454
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000455&lt;a id="#crates_vendor_repinning_updating_dependencies"&gt;&lt;/a&gt;
UebelAndreadf92b12022-07-05 11:02:07 -0700456
457### Repinning / Updating Dependencies
458
459Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin`
460flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation.
461
462```shell
463bazel run //3rdparty:crates_vendor -- --repin
464```
465
466Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html)
freeformstua6f29fd2023-06-30 12:16:49 -0700467call against the generated workspace. The following table describes how to control particular values passed to the
UebelAndreadf92b12022-07-05 11:02:07 -0700468`cargo update` command.
469
470| Value | Cargo command |
471| --- | --- |
UebelAndred916a6f2023-01-03 12:34:11 -0800472| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` |
473| Any of [`full`, `eager`, `all`] | `cargo update` |
UebelAndreadf92b12022-07-05 11:02:07 -0700474| `package_name` | `cargo upgrade --package package_name` |
475| `package_name@1.2.3` | `cargo upgrade --package package_name --precise 1.2.3` |
476
477
UebelAndre59fab4e2022-03-03 06:49:27 -0800478
479**ATTRIBUTES**
480
481
482| Name | Description | Type | Mandatory | Default |
483| :------------- | :------------- | :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000484| <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 | |
485| <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 | <code>{}</code> |
486| <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 | <code>None</code> |
487| <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 | <code>//crate_universe/private/vendor:buildifier</code> |
488| <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 <code>CARGO_BAZEL_GENERATOR_PATH</code> action env will be used. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>@cargo_bazel_bootstrap//:binary</code> |
489| <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 | <code>None</code> |
490| <a id="crates_vendor-cargo_lockfile"></a>cargo_lockfile | The path to an existing <code>Cargo.lock</code> file | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
David Tolnay36b57af2023-01-03 13:12:33 -0800491| <a id="crates_vendor-generate_binaries"></a>generate_binaries | Whether to generate <code>rust_binary</code> targets for all the binary crates in every package. By default only the <code>rust_library</code> targets are generated. | Boolean | optional | <code>False</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000492| <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 | <code>True</code> |
James Leitch2a5ca6a2023-12-14 08:47:10 -0800493| <a id="crates_vendor-generate_target_compatible_with"></a>generate_target_compatible_with | DEPRECATED: Moved to <code>render_config</code>. | Boolean | optional | <code>True</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000494| <a id="crates_vendor-manifests"></a>manifests | A list of Cargo manifests (<code>Cargo.toml</code> files). | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional | <code>[]</code> |
495| <a id="crates_vendor-mode"></a>mode | Flags determining how crates should be vendored. <code>local</code> is where crate source and BUILD files are written to the repository. <code>remote</code> is where only BUILD files are written and repository rules used to fetch source code. | String | optional | <code>"remote"</code> |
496| <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 | <code>{}</code> |
UebelAndre02557a42023-02-10 06:46:23 -0800497| <a id="crates_vendor-render_config"></a>render_config | The configuration flags to use for rendering. Use <code>//crate_universe:defs.bzl\%render_config</code> to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | <code>""</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000498| <a id="crates_vendor-repository_name"></a>repository_name | The name of the repository to generate for <code>remote</code> vendor modes. If unset, the label name will be used | String | optional | <code>""</code> |
499| <a id="crates_vendor-splicing_config"></a>splicing_config | The configuration flags to use for splicing Cargo maniests. Use <code>//crate_universe:defs.bzl\%rsplicing_config</code> to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | <code>""</code> |
John Hughesc3581a52023-12-13 17:27:47 +0100500| <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 | <code>["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"]</code> |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000501| <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 | <code>"crates"</code> |
UebelAndre467a3012021-04-23 08:18:07 -0700502
503
Brian Silverman38e841a2022-07-11 08:15:49 -0700504<a id="aliases"></a>
UebelAndre42f78f22022-05-03 06:52:55 -0700505
506## aliases
507
508<pre>
509aliases(<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>)
510</pre>
511
512Produces a map of Crate alias names to their original label
513
514If no dependency kinds are specified, `normal` and `proc_macro` are used by default.
515Setting any one flag will otherwise determine the contents of the returned dict.
516
517
518**PARAMETERS**
519
520
521| Name | Description | Default Value |
522| :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000523| <a id="aliases-normal"></a>normal | If True, normal dependencies are included in the output list. | `False` |
Diana Sulmone40dee952022-12-14 09:17:46 -0600524| <a id="aliases-normal_dev"></a>normal_dev | If True, normal dev dependencies will be included in the output list.. | `False` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000525| <a id="aliases-proc_macro"></a>proc_macro | If True, proc_macro dependencies are included in the output list. | `False` |
526| <a id="aliases-proc_macro_dev"></a>proc_macro_dev | If True, dev proc_macro dependencies are included in the output list. | `False` |
527| <a id="aliases-build"></a>build | If True, build dependencies are included in the output list. | `False` |
528| <a id="aliases-build_proc_macro"></a>build_proc_macro | If True, build proc_macro dependencies are included in the output list. | `False` |
529| <a id="aliases-package_name"></a>package_name | The package name of the set of dependencies to look up. Defaults to <code>native.package_name()</code> when unset. | `None` |
UebelAndre42f78f22022-05-03 06:52:55 -0700530
531**RETURNS**
532
533dict: The aliases of all associated packages
534
535
Brian Silverman38e841a2022-07-11 08:15:49 -0700536<a id="all_crate_deps"></a>
UebelAndre42f78f22022-05-03 06:52:55 -0700537
538## all_crate_deps
539
540<pre>
541all_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>,
542 <a href="#all_crate_deps-package_name">package_name</a>)
543</pre>
544
545Finds the fully qualified label of all requested direct crate dependencies for the package where this macro is called.
546
547If no parameters are set, all normal dependencies are returned. Setting any one flag will
548otherwise impact the contents of the returned list.
549
550
551**PARAMETERS**
552
553
554| Name | Description | Default Value |
555| :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000556| <a id="all_crate_deps-normal"></a>normal | If True, normal dependencies are included in the output list. | `False` |
Diana Sulmone40dee952022-12-14 09:17:46 -0600557| <a id="all_crate_deps-normal_dev"></a>normal_dev | If True, normal dev dependencies will be included in the output list.. | `False` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000558| <a id="all_crate_deps-proc_macro"></a>proc_macro | If True, proc_macro dependencies are included in the output list. | `False` |
559| <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` |
560| <a id="all_crate_deps-build"></a>build | If True, build dependencies are included in the output list. | `False` |
561| <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` |
562| <a id="all_crate_deps-package_name"></a>package_name | The package name of the set of dependencies to look up. Defaults to <code>native.package_name()</code> when unset. | `None` |
UebelAndre42f78f22022-05-03 06:52:55 -0700563
564**RETURNS**
565
566list: A list of labels to generated rust targets (str)
567
568
Brian Silverman38e841a2022-07-11 08:15:49 -0700569<a id="crate.spec"></a>
UebelAndre467a3012021-04-23 08:18:07 -0700570
571## crate.spec
572
573<pre>
David Tolnay841a7332023-10-31 12:28:10 -0400574crate.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>)
UebelAndre467a3012021-04-23 08:18:07 -0700575</pre>
576
UebelAndre59fab4e2022-03-03 06:49:27 -0800577A constructor for a crate dependency.
UebelAndre467a3012021-04-23 08:18:07 -0700578
UebelAndre59fab4e2022-03-03 06:49:27 -0800579See [specifying dependencies][sd] in the Cargo book for more details.
UebelAndre467a3012021-04-23 08:18:07 -0700580
UebelAndre59fab4e2022-03-03 06:49:27 -0800581[sd]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html
UebelAndre467a3012021-04-23 08:18:07 -0700582
583
584**PARAMETERS**
585
586
587| Name | Description | Default Value |
588| :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000589| <a id="crate.spec-package"></a>package | The explicit name of the package (used when attempting to alias a crate). | `None` |
590| <a id="crate.spec-version"></a>version | The exact version of the crate. Cannot be used with <code>git</code>. | `None` |
David Tolnay841a7332023-10-31 12:28:10 -0400591| <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` |
592| <a id="crate.spec-lib"></a>lib | If using <code>artifact = "bin"</code>, additionally setting <code>lib = True</code> declares a dependency on both the package's library and binary, as opposed to just the binary. | `None` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000593| <a id="crate.spec-default_features"></a>default_features | Maps to the <code>default-features</code> flag. | `True` |
594| <a id="crate.spec-features"></a>features | A list of features to use for the crate | `[]` |
595| <a id="crate.spec-git"></a>git | The Git url to use for the crate. Cannot be used with <code>version</code>. | `None` |
ted-logand9ecc8d2023-03-06 10:24:21 -0800596| <a id="crate.spec-branch"></a>branch | The git branch of the remote crate. Tied with the <code>git</code> param. Only one of branch, tag or rev may be specified. Specifying <code>rev</code> is recommended for fully-reproducible builds. | `None` |
597| <a id="crate.spec-tag"></a>tag | The git tag of the remote crate. Tied with the <code>git</code> param. Only one of branch, tag or rev may be specified. Specifying <code>rev</code> is recommended for fully-reproducible builds. | `None` |
598| <a id="crate.spec-rev"></a>rev | The git revision of the remote crate. Tied with the <code>git</code> param. Only one of branch, tag or rev may be specified. | `None` |
UebelAndre467a3012021-04-23 08:18:07 -0700599
UebelAndre3438d062021-10-12 06:15:52 -0700600**RETURNS**
601
UebelAndre59fab4e2022-03-03 06:49:27 -0800602string: A json encoded string of all inputs
UebelAndre3438d062021-10-12 06:15:52 -0700603
UebelAndre467a3012021-04-23 08:18:07 -0700604
Brian Silverman38e841a2022-07-11 08:15:49 -0700605<a id="crate.annotation"></a>
UebelAndre467a3012021-04-23 08:18:07 -0700606
UebelAndre59fab4e2022-03-03 06:49:27 -0800607## crate.annotation
UebelAndre467a3012021-04-23 08:18:07 -0700608
609<pre>
James Leitch38034012023-12-15 09:13:47 -0800610crate.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>,
611 <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>,
612 <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>,
613 <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>,
614 <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>,
615 <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>,
616 <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>)
UebelAndre467a3012021-04-23 08:18:07 -0700617</pre>
618
UebelAndre59fab4e2022-03-03 06:49:27 -0800619A collection of extra attributes and settings for a particular crate
UebelAndre467a3012021-04-23 08:18:07 -0700620
UebelAndre59fab4e2022-03-03 06:49:27 -0800621**PARAMETERS**
UebelAndre467a3012021-04-23 08:18:07 -0700622
UebelAndre467a3012021-04-23 08:18:07 -0700623
UebelAndre59fab4e2022-03-03 06:49:27 -0800624| Name | Description | Default Value |
625| :------------- | :------------- | :------------- |
John Firebaughbe82ff82022-12-22 03:42:19 -0800626| <a id="crate.annotation-version"></a>version | The version or semver-conditions to match with a crate. The wildcard <code>*</code> matches any version, including prerelease versions. | `"*"` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000627| <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` |
628| <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` |
James Leitch38034012023-12-15 09:13:47 -0800629| <a id="crate.annotation-alias_rule"></a>alias_rule | Alias rule to use instead of <code>native.alias()</code>. Overrides [render_config](#render_config)'s 'default_alias_rule'. | `None` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000630| <a id="crate.annotation-build_script_data"></a>build_script_data | A list of labels to add to a crate's <code>cargo_build_script::data</code> attribute. | `None` |
631| <a id="crate.annotation-build_script_tools"></a>build_script_tools | A list of labels to add to a crate's <code>cargo_build_script::tools</code> attribute. | `None` |
632| <a id="crate.annotation-build_script_data_glob"></a>build_script_data_glob | A list of glob patterns to add to a crate's <code>cargo_build_script::data</code> attribute. | `None` |
633| <a id="crate.annotation-build_script_deps"></a>build_script_deps | A list of labels to add to a crate's <code>cargo_build_script::deps</code> attribute. | `None` |
634| <a id="crate.annotation-build_script_env"></a>build_script_env | Additional environment variables to set on a crate's <code>cargo_build_script::env</code> attribute. | `None` |
635| <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 <code>cargo_build_script::proc_macro_deps</code> attribute. | `None` |
Daniel Wagner-Hall1f9b63a2023-09-06 17:38:35 +0100636| <a id="crate.annotation-build_script_rundir"></a>build_script_rundir | An override for the build script's rundir attribute. | `None` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000637| <a id="crate.annotation-build_script_rustc_env"></a>build_script_rustc_env | Additional environment variables to set on a crate's <code>cargo_build_script::env</code> attribute. | `None` |
638| <a id="crate.annotation-build_script_toolchains"></a>build_script_toolchains | A list of labels to set on a crates's <code>cargo_build_script::toolchains</code> attribute. | `None` |
639| <a id="crate.annotation-compile_data"></a>compile_data | A list of labels to add to a crate's <code>rust_library::compile_data</code> attribute. | `None` |
640| <a id="crate.annotation-compile_data_glob"></a>compile_data_glob | A list of glob patterns to add to a crate's <code>rust_library::compile_data</code> attribute. | `None` |
641| <a id="crate.annotation-crate_features"></a>crate_features | A list of strings to add to a crate's <code>rust_library::crate_features</code> attribute. | `None` |
642| <a id="crate.annotation-data"></a>data | A list of labels to add to a crate's <code>rust_library::data</code> attribute. | `None` |
643| <a id="crate.annotation-data_glob"></a>data_glob | A list of glob patterns to add to a crate's <code>rust_library::data</code> attribute. | `None` |
644| <a id="crate.annotation-deps"></a>deps | A list of labels to add to a crate's <code>rust_library::deps</code> attribute. | `None` |
Daniel Wagner-Hall0f582d52023-10-12 17:01:25 +0100645| <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` |
646| <a id="crate.annotation-gen_binaries"></a>gen_binaries | As a list, the subset of the crate's bins that should get <code>rust_binary</code> targets produced. Or <code>True</code> to generate all, <code>False</code> to generate none. | `None` |
William Smithad01d1b2023-01-26 05:29:16 -0500647| <a id="crate.annotation-disable_pipelining"></a>disable_pipelining | If True, disables pipelining for library targets for this crate. | `False` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000648| <a id="crate.annotation-gen_build_script"></a>gen_build_script | An authorative flag to determine whether or not to produce <code>cargo_build_script</code> targets for the current crate. | `None` |
649| <a id="crate.annotation-patch_args"></a>patch_args | The <code>patch_args</code> 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` |
650| <a id="crate.annotation-patch_tool"></a>patch_tool | The <code>patch_tool</code> 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` |
651| <a id="crate.annotation-patches"></a>patches | The <code>patches</code> attribute of a Bazel repository rule. See [http_archive.patches](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patches) | `None` |
652| <a id="crate.annotation-proc_macro_deps"></a>proc_macro_deps | A list of labels to add to a crate's <code>rust_library::proc_macro_deps</code> attribute. | `None` |
653| <a id="crate.annotation-rustc_env"></a>rustc_env | Additional variables to set on a crate's <code>rust_library::rustc_env</code> attribute. | `None` |
654| <a id="crate.annotation-rustc_env_files"></a>rustc_env_files | A list of labels to set on a crate's <code>rust_library::rustc_env_files</code> attribute. | `None` |
655| <a id="crate.annotation-rustc_flags"></a>rustc_flags | A list of strings to set on a crate's <code>rust_library::rustc_flags</code> attribute. | `None` |
656| <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` |
UebelAndre467a3012021-04-23 08:18:07 -0700657
UebelAndre59fab4e2022-03-03 06:49:27 -0800658**RETURNS**
659
660string: A json encoded string containing the specified version and separately all other inputs.
661
662
Brian Silverman38e841a2022-07-11 08:15:49 -0700663<a id="crate.workspace_member"></a>
UebelAndre59fab4e2022-03-03 06:49:27 -0800664
665## crate.workspace_member
666
667<pre>
668crate.workspace_member(<a href="#crate.workspace_member-version">version</a>, <a href="#crate.workspace_member-sha256">sha256</a>)
669</pre>
670
671Define information for extra workspace members
672
673**PARAMETERS**
674
675
676| Name | Description | Default Value |
677| :------------- | :------------- | :------------- |
678| <a id="crate.workspace_member-version"></a>version | The semver of the crate to download. Must be an exact version. | none |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000679| <a id="crate.workspace_member-sha256"></a>sha256 | The sha256 checksum of the <code>.crate</code> file. | `None` |
UebelAndre59fab4e2022-03-03 06:49:27 -0800680
681**RETURNS**
682
683string: A json encoded string of all inputs
684
685
James Leitch978d7a02023-12-27 10:28:11 -0700686<a id="crate.select"></a>
687
688## crate.select
689
690<pre>
691crate.select(<a href="#crate.select-common">common</a>, <a href="#crate.select-selects">selects</a>)
692</pre>
693
694A Starlark Select for `crate.annotation()`.
695
696**PARAMETERS**
697
698
699| Name | Description | Default Value |
700| :------------- | :------------- | :------------- |
701| <a id="crate.select-common"></a>common | A value that applies to all configurations. | none |
702| <a id="crate.select-selects"></a>selects | A dict of <code>target_triple</code> to values. | none |
703
704**RETURNS**
705
706struct: A struct representing the Starlark Select.
707
708
Brian Silverman38e841a2022-07-11 08:15:49 -0700709<a id="crate_deps"></a>
UebelAndre42f78f22022-05-03 06:52:55 -0700710
711## crate_deps
712
713<pre>
714crate_deps(<a href="#crate_deps-deps">deps</a>, <a href="#crate_deps-package_name">package_name</a>)
715</pre>
716
717Finds the fully qualified label of the requested crates for the package where this macro is called.
718
719**PARAMETERS**
720
721
722| Name | Description | Default Value |
723| :------------- | :------------- | :------------- |
724| <a id="crate_deps-deps"></a>deps | The desired list of crate targets. | none |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000725| <a id="crate_deps-package_name"></a>package_name | The package name of the set of dependencies to look up. Defaults to <code>native.package_name()</code>. | `None` |
UebelAndre42f78f22022-05-03 06:52:55 -0700726
727**RETURNS**
728
729list: A list of labels to generated rust targets (str)
730
731
Brian Silverman38e841a2022-07-11 08:15:49 -0700732<a id="crate_repositories"></a>
UebelAndre42f78f22022-05-03 06:52:55 -0700733
734## crate_repositories
735
736<pre>
737crate_repositories()
738</pre>
739
Matt3fd49872023-12-05 02:50:52 +1100740A macro for defining repositories for all generated crates.
UebelAndre42f78f22022-05-03 06:52:55 -0700741
742
Matt3fd49872023-12-05 02:50:52 +1100743**RETURNS**
744
745A list of repos visible to the module through the module extension.
746
UebelAndre42f78f22022-05-03 06:52:55 -0700747
Brian Silverman38e841a2022-07-11 08:15:49 -0700748<a id="crate_universe_dependencies"></a>
UebelAndre3205c9d2022-03-21 07:31:35 -0700749
UebelAndre628e85e2022-03-21 16:48:20 -0700750## crate_universe_dependencies
UebelAndre3205c9d2022-03-21 07:31:35 -0700751
752<pre>
DolceTriadeb96e37e2023-11-26 02:43:48 +0530753crate_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>)
UebelAndre3205c9d2022-03-21 07:31:35 -0700754</pre>
755
756Define dependencies of the `cargo-bazel` Rust target
757
758**PARAMETERS**
759
760
761| Name | Description | Default Value |
762| :------------- | :------------- | :------------- |
UebelAndre768eaf42024-04-10 01:58:26 -0700763| <a id="crate_universe_dependencies-rust_version"></a>rust_version | The version of rust to use when generating dependencies. | `"1.77.2"` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000764| <a id="crate_universe_dependencies-bootstrap"></a>bootstrap | If true, a <code>cargo_bootstrap_repository</code> target will be generated. | `False` |
DolceTriadeb96e37e2023-11-26 02:43:48 +0530765| <a id="crate_universe_dependencies-kwargs"></a>kwargs | Arguments to pass through to cargo_bazel_bootstrap. | none |
UebelAndre3205c9d2022-03-21 07:31:35 -0700766
Matt44ffccb2023-12-13 00:52:58 +1100767**RETURNS**
768
769list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories
770 defined by this macro.
771
UebelAndre3205c9d2022-03-21 07:31:35 -0700772
Brian Silverman38e841a2022-07-11 08:15:49 -0700773<a id="render_config"></a>
UebelAndre59fab4e2022-03-03 06:49:27 -0800774
775## render_config
776
777<pre>
778render_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>,
James Leitch38034012023-12-15 09:13:47 -0800779 <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>,
Nick Collier172b34a2024-02-14 00:37:38 +0000780 <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>,
781 <a href="#render_config-generate_rules_license_metadata">generate_rules_license_metadata</a>)
UebelAndre59fab4e2022-03-03 06:49:27 -0800782</pre>
783
784Various settings used to configure rendered outputs
785
786The template parameters each support a select number of format keys. A description of each key
787can be found below where the supported keys for each template can be found in the parameter docs
788
789| key | definition |
790| --- | --- |
791| `name` | The name of the crate. Eg `tokio` |
792| `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. |
793| `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` |
794| `version` | The crate version. Eg `1.2.3` |
795| `target` | The library or binary target of the crate |
796| `file` | The basename of a file |
UebelAndre467a3012021-04-23 08:18:07 -0700797
798
799**PARAMETERS**
800
801
802| Name | Description | Default Value |
803| :------------- | :------------- | :------------- |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000804| <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 [<code>{name}</code>, {version}<code>]. | `"//:BUILD.{name}-{version}.bazel"` |
805| <a id="render_config-crate_label_template"></a>crate_label_template | The base template to use for crate labels. The available format keys are [<code>{repository}</code>, <code>{name}</code>, <code>{version}</code>, <code>{target}</code>]. | `"@{repository}__{name}-{version}//:{target}"` |
806| <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 [<code>{repository}</code>, <code>{name}</code>, <code>{version}</code>]. | `"{repository}__{name}-{version}"` |
807| <a id="render_config-crates_module_template"></a>crates_module_template | The pattern to use for the <code>defs.bzl</code> and <code>BUILD.bazel</code> file names used for the crates module. The available format keys are [<code>{file}</code>]. | `"//:{file}"` |
James Leitch38034012023-12-15 09:13:47 -0800808| <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 <code>compilation_mode</code>) or a string representing a rule in the form '&lt;label to .bzl&gt;:&lt;rule&gt;' that takes a single label parameter 'actual'. See '@crate_index//:alias_rules.bzl' for an example. | `"alias"` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000809| <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 <code>all_crate_deps</code>. | `None` |
James Leitch2a5ca6a2023-12-14 08:47:10 -0800810| <a id="render_config-generate_target_compatible_with"></a>generate_target_compatible_with | Whether to generate <code>target_compatible_with</code> annotations on the generated BUILD files. This catches a <code>target_triple</code>being targeted that isn't declared in <code>supported_platform_triples</code>. | `True` |
Daniel Wagner-Hall56237412022-11-30 17:28:41 +0000811| <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 [<code>{triple}</code>]. | `"@rules_rust//rust/platform:{triple}"` |
812| <a id="render_config-regen_command"></a>regen_command | An optional command to demonstrate how generated files should be regenerated. | `None` |
813| <a id="render_config-vendor_mode"></a>vendor_mode | An optional configuration for rendirng content to be rendered into repositories. | `None` |
Nick Collier172b34a2024-02-14 00:37:38 +0000814| <a id="render_config-generate_rules_license_metadata"></a>generate_rules_license_metadata | Whether to generate rules license metedata | `False` |
UebelAndre467a3012021-04-23 08:18:07 -0700815
UebelAndre3438d062021-10-12 06:15:52 -0700816**RETURNS**
817
UebelAndre59fab4e2022-03-03 06:49:27 -0800818string: A json encoded struct to match the Rust `config::RenderConfig` struct
819
820
Brian Silverman38e841a2022-07-11 08:15:49 -0700821<a id="splicing_config"></a>
UebelAndre59fab4e2022-03-03 06:49:27 -0800822
823## splicing_config
824
825<pre>
826splicing_config(<a href="#splicing_config-resolver_version">resolver_version</a>)
827</pre>
828
Leopold Schabelaca4ec12022-05-24 15:09:08 +0200829Various settings used to configure Cargo manifest splicing behavior.
UebelAndre59fab4e2022-03-03 06:49:27 -0800830
831[rv]: https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions
832
833
834**PARAMETERS**
835
836
837| Name | Description | Default Value |
838| :------------- | :------------- | :------------- |
UebelAndre03a90882023-10-30 05:46:01 -0700839| <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 <code>crates_repository::packages</code>. | `"2"` |
UebelAndre59fab4e2022-03-03 06:49:27 -0800840
841**RETURNS**
842
843str: A json encoded string of the parameters provided
UebelAndre3438d062021-10-12 06:15:52 -0700844
UebelAndre467a3012021-04-23 08:18:07 -0700845