1<!-- Generated with Stardoc: http://skydoc.bazel.build --> 2 3# Crate Universe 4 5Crate Universe is a set of Bazel rule for generating Rust targets using Cargo. 6 7This doc describes using crate_universe from a WORKSPACE file. 8 9If you're using bzlmod, please see [the bzlmod equivalent of this doc](crate_universe_bzlmod.html). 10 11## Setup 12 13After loading `rules_rust` in your workspace, set the following to begin using `crate_universe`: 14 15```python 16load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencies") 17 18crate_universe_dependencies() 19``` 20 21Note that if the current version of `rules_rust` is not a release artifact, you may need to set additional 22flags such as [`bootstrap = True`](#crate_universe_dependencies-bootstrap) on the `crate_universe_dependencies` 23call above or [crates_repository::generator_urls](#crates_repository-generator_urls) in uses of `crates_repository`. 24 25## Rules 26 27- [crates_repository](#crates_repository) 28- [crates_vendor](#crates_vendor) 29 30## Utility Macros 31 32- [crate_universe_dependencies](#crate_universe_dependencies) 33- [crate.annotation](#crateannotation) 34- [crate.select](#crateselect) 35- [crate.spec](#cratespec) 36- [crate.workspace_member](#crateworkspace_member) 37- [render_config](#render_config) 38- [splicing_config](#splicing_config) 39 40## Workflows 41 42The [`crates_repository`](#crates_repository) rule (the primary repository rule of `rules_rust`'s cargo support) supports a number of different 43ways users can express and organize their dependencies. The most common are listed below though there are more to be found in 44the [./examples/crate_universe](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe) directory. 45 46### Cargo Workspaces 47 48One of the simpler ways to wire up dependencies would be to first structure your project into a [Cargo workspace][cw]. 49The `crates_repository` rule can ingest a root `Cargo.toml` file and generate dependencies from there. 50 51```python 52load("@rules_rust//crate_universe:defs.bzl", "crates_repository") 53 54crates_repository( 55 name = "crate_index", 56 cargo_lockfile = "//:Cargo.lock", 57 lockfile = "//:Cargo.Bazel.lock", 58 manifests = ["//:Cargo.toml"], 59) 60 61load("@crate_index//:defs.bzl", "crate_repositories") 62 63crate_repositories() 64``` 65 66The generated `crates_repository` contains helper macros which make collecting dependencies for Bazel targets simpler. 67Notably, the `all_crate_deps` and `aliases` macros (see [Dependencies API](#dependencies-api)) commonly allow the 68`Cargo.toml` files to be the single source of truth for dependencies. Since these macros come from the generated 69repository, the dependencies and alias definitions they return will automatically update BUILD targets. 70 71```python 72load("@crate_index//:defs.bzl", "aliases", "all_crate_deps") 73load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 74 75rust_library( 76 name = "lib", 77 aliases = aliases(), 78 deps = all_crate_deps( 79 normal = True, 80 ), 81 proc_macro_deps = all_crate_deps( 82 proc_macro = True, 83 ), 84) 85 86rust_test( 87 name = "unit_test", 88 crate = ":lib", 89 aliases = aliases( 90 normal_dev = True, 91 proc_macro_dev = True, 92 ), 93 deps = all_crate_deps( 94 normal_dev = True, 95 ), 96 proc_macro_deps = all_crate_deps( 97 proc_macro_dev = True, 98 ), 99) 100``` 101 102### Direct Packages 103 104In cases where Rust targets have heavy interractions with other Bazel targests ([Cc][cc], [Proto][proto], etc.), 105maintaining `Cargo.toml` files may have deminishing returns as things like [rust-analyzer][ra] begin to be confused 106about missing targets or environment variables defined only in Bazel. In workspaces like this, it may be desirable 107to have a "Cargo free" setup. `crates_repository` supports this through the `packages` attribute. 108 109```python 110load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository", "render_config") 111 112crates_repository( 113 name = "crate_index", 114 cargo_lockfile = "//:Cargo.lock", 115 lockfile = "//:Cargo.Bazel.lock", 116 packages = { 117 "async-trait": crate.spec( 118 version = "0.1.51", 119 ), 120 "mockall": crate.spec( 121 version = "0.10.2", 122 ), 123 "tokio": crate.spec( 124 version = "1.12.0", 125 ), 126 }, 127 # Setting the default package name to `""` forces the use of the macros defined in this repository 128 # to always use the root package when looking for dependencies or aliases. This should be considered 129 # optional as the repository also exposes alises for easy access to all dependencies. 130 render_config = render_config( 131 default_package_name = "" 132 ), 133) 134 135load("@crate_index//:defs.bzl", "crate_repositories") 136 137crate_repositories() 138``` 139 140Consuming dependencies may be more ergonomic in this case through the aliases defined in the new repository. 141 142```python 143load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") 144 145rust_library( 146 name = "lib", 147 deps = [ 148 "@crate_index//:tokio", 149 ], 150 proc_macro_deps = [ 151 "@crate_index//:async-trait", 152 ], 153) 154 155rust_test( 156 name = "unit_test", 157 crate = ":lib", 158 deps = [ 159 "@crate_index//:mockall", 160 ], 161) 162``` 163 164### Binary dependencies 165 166Neither of the above approaches supports depending on binary-only packages. 167 168In order to depend on a Cargo package that contains binaries and no library, you 169will need to do one of the following: 170 171- Fork the package to add an empty lib.rs, which makes the package visible to 172 Cargo metadata and compatible with the above approaches; 173 174- Or handwrite your own build target for the binary, use `http_archive` to 175 import its source code, and use `crates_repository` to make build targets for 176 its dependencies. This is demonstrated below using the `rustfilt` crate as an 177 example. 178 179```python 180# in WORKSPACE.bazel 181 182load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 183 184http_archive( 185 name = "rustfilt", 186 build_file = "//rustfilt:BUILD.rustfilt.bazel", 187 sha256 = "c8d748b182c8f95224336d20dcc5609598af612581ce60cfb29da4dc8d0091f2", 188 strip_prefix = "rustfilt-0.2.1", 189 type = "tar.gz", 190 urls = ["https://static.crates.io/crates/rustfilt/rustfilt-0.2.1.crate"], 191) 192 193load("@rules_rust//crate_universe:defs.bzl", "crates_repository") 194 195crates_repository( 196 name = "rustfilt_deps", 197 cargo_lockfile = "//rustfilt:Cargo.lock", 198 manifests = ["@rustfilt//:Cargo.toml"], 199) 200 201load("@rustfilt_deps//:defs.bzl", rustfilt_deps = "crate_repositories") 202 203rustfilt_deps() 204``` 205 206```python 207# in rustfilt/BUILD.rustfilt.bazel 208 209load("@rules_rust//rust:defs.bzl", "rust_binary") 210 211rust_binary( 212 name = "rustfilt", 213 srcs = glob(["src/**/*.rs"]), 214 edition = "2018", 215 deps = [ 216 "@rustfilt_deps//:clap", 217 "@rustfilt_deps//:lazy_static", 218 "@rustfilt_deps//:regex", 219 "@rustfilt_deps//:rustc-demangle", 220 ], 221) 222``` 223 224If you use either `crates_repository` or `crates_vendor` to depend on a Cargo 225package that contains _both_ a library crate _and_ binaries, by default only the 226library gets made available to Bazel. To generate Bazel targets for the binary 227crates as well, you must opt in to it with an annotation on the package: 228 229```python 230load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate") 231 232crates_repository( 233 name = "crate_index", 234 annotations = { 235 "thepackage": [crate.annotation( 236 gen_binaries = True, 237 # Or, to expose just a subset of the package's binaries by name: 238 gen_binaries = ["rustfilt"], 239 )], 240 }, 241 # Or, to expose every binary of every package: 242 generate_binaries = True, 243 ... 244) 245``` 246 247## Dependencies API 248 249After rendering dependencies, convenience macros may also be generated to provide 250convenient accessors to larger sections of the dependency graph. 251 252- [aliases](#aliases) 253- [crate_deps](#crate_deps) 254- [all_crate_deps](#all_crate_deps) 255- [crate_repositories](#crate_repositories) 256 257## Building crates with complicated dependencies 258 259Some 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 261There are a few approaches to making sure these run: 262 263### Some things work without intervention 264 265Some build scripts will happily run without any support needed. 266 267rules_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 269rules_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 271### Supplying extra tools to build 272 273Some build scripts can be made to work by pulling in some extra files and making them available to the build script. 274 275Commonly 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 277There 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 279### Building with Bazel and supplying via an override 280 281Some build scripts have hooks to allow replacing parts that are complicated to build with output prepared by Bazel. 282 283We 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 285There 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 287--- 288 289--- 290 291[cw]: https://doc.rust-lang.org/book/ch14-03-cargo-workspaces.html 292[cc]: https://docs.bazel.build/versions/main/be/c-cpp.html 293[proto]: https://rules-proto-grpc.com/en/latest/lang/rust.html 294[ra]: https://rust-analyzer.github.io/ 295[cc-rs]: https://github.com/rust-lang/cc-rs 296 297<a id="crates_vendor"></a> 298 299## crates_vendor 300 301<pre> 302crates_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 <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 <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 <a href="#crates_vendor-supported_platform_triples">supported_platform_triples</a>, <a href="#crates_vendor-vendor_path">vendor_path</a>) 306</pre> 307 308A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. 309This rule is useful for users whose workspaces are expected to be consumed in other workspaces as the 310rendered `BUILD` files reduce the number of workspace dependencies, allowing for easier loads. This rule 311handles all the same [workflows](#workflows) `crate_universe` rules do. 312 313Example: 314 315Given the following workspace structure: 316 317```text 318[workspace]/ 319 WORKSPACE 320 BUILD 321 Cargo.toml 322 3rdparty/ 323 BUILD 324 src/ 325 main.rs 326``` 327 328The following is something that'd be found in `3rdparty/BUILD`: 329 330```python 331load("@rules_rust//crate_universe:defs.bzl", "crates_vendor", "crate") 332 333crates_vendor( 334 name = "crates_vendor", 335 annotations = { 336 "rand": [crate.annotation( 337 default_features = False, 338 features = ["small_rng"], 339 )], 340 }, 341 cargo_lockfile = "//:Cargo.Bazel.lock", 342 manifests = ["//:Cargo.toml"], 343 mode = "remote", 344 vendor_path = "crates", 345 tags = ["manual"], 346) 347``` 348 349The above creates a target that can be run to write `BUILD` files into the `3rdparty` 350directory next to where the target is defined. To run it, simply call: 351 352```shell 353bazel run //3rdparty:crates_vendor 354``` 355 356<a id="#crates_vendor_repinning_updating_dependencies"></a> 357 358### Repinning / Updating Dependencies 359 360Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin` 361flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation. 362 363```shell 364bazel run //3rdparty:crates_vendor -- --repin 365``` 366 367Under the hood, `--repin` will trigger a [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) 368call against the generated workspace. The following table describes how to control particular values passed to the 369`cargo update` command. 370 371| Value | Cargo command | 372| --- | --- | 373| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` | 374| Any of [`full`, `eager`, `all`] | `cargo update` | 375| `package_name` | `cargo upgrade --package package_name` | 376| `[email protected]` | `cargo upgrade --package package_name --precise 1.2.3` | 377 378**ATTRIBUTES** 379 380 381| Name | Description | Type | Mandatory | Default | 382| :------------- | :------------- | :------------- | :------------- | :------------- | 383| <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| <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| <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| <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| <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| <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| <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| <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| <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| <a id="crates_vendor-generate_target_compatible_with"></a>generate_target_compatible_with | DEPRECATED: Moved to `render_config`. | Boolean | optional | `True` | 393| <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| <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| <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| <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| <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| <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| <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| <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 402 403<a id="aliases"></a> 404 405## aliases 406 407<pre> 408aliases(<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</pre> 410 411Produces a map of Crate alias names to their original label 412 413If no dependency kinds are specified, `normal` and `proc_macro` are used by default. 414Setting any one flag will otherwise determine the contents of the returned dict. 415 416 417**PARAMETERS** 418 419 420| Name | Description | Default Value | 421| :------------- | :------------- | :------------- | 422| <a id="aliases-normal"></a>normal | If True, normal dependencies are included in the output list. | `False` | 423| <a id="aliases-normal_dev"></a>normal_dev | If True, normal dev dependencies will be included in the output list.. | `False` | 424| <a id="aliases-proc_macro"></a>proc_macro | If True, proc_macro dependencies are included in the output list. | `False` | 425| <a id="aliases-proc_macro_dev"></a>proc_macro_dev | If True, dev proc_macro dependencies are included in the output list. | `False` | 426| <a id="aliases-build"></a>build | If True, build dependencies are included in the output list. | `False` | 427| <a id="aliases-build_proc_macro"></a>build_proc_macro | If True, build proc_macro dependencies are included in the output list. | `False` | 428| <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 430**RETURNS** 431 432dict: The aliases of all associated packages 433 434 435<a id="all_crate_deps"></a> 436 437## all_crate_deps 438 439<pre> 440all_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 <a href="#all_crate_deps-package_name">package_name</a>) 442</pre> 443 444Finds the fully qualified label of all requested direct crate dependencies for the package where this macro is called. 445 446If no parameters are set, all normal dependencies are returned. Setting any one flag will 447otherwise impact the contents of the returned list. 448 449 450**PARAMETERS** 451 452 453| Name | Description | Default Value | 454| :------------- | :------------- | :------------- | 455| <a id="all_crate_deps-normal"></a>normal | If True, normal dependencies are included in the output list. | `False` | 456| <a id="all_crate_deps-normal_dev"></a>normal_dev | If True, normal dev dependencies will be included in the output list.. | `False` | 457| <a id="all_crate_deps-proc_macro"></a>proc_macro | If True, proc_macro dependencies are included in the output list. | `False` | 458| <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| <a id="all_crate_deps-build"></a>build | If True, build dependencies are included in the output list. | `False` | 460| <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| <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 463**RETURNS** 464 465list: A list of labels to generated rust targets (str) 466 467 468<a id="crate.annotation"></a> 469 470## crate.annotation 471 472<pre> 473crate.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 <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 <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 <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 <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 <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 <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 <a href="#crate.annotation-override_targets">override_targets</a>) 481</pre> 482 483A collection of extra attributes and settings for a particular crate 484 485**PARAMETERS** 486 487 488| Name | Description | Default Value | 489| :------------- | :------------- | :------------- | 490| <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| <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| <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| <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| <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| <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| <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| <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| <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| <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| <a id="crate.annotation-build_script_rundir"></a>build_script_rundir | An override for the build script's rundir attribute. | `None` | 501| <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| <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| <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| <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| <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| <a id="crate.annotation-data"></a>data | A list of labels to add to a crate's `rust_library::data` attribute. | `None` | 507| <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| <a id="crate.annotation-deps"></a>deps | A list of labels to add to a crate's `rust_library::deps` attribute. | `None` | 509| <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| <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| <a id="crate.annotation-disable_pipelining"></a>disable_pipelining | If True, disables pipelining for library targets for this crate. | `False` | 512| <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| <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| <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| <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| <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| <a id="crate.annotation-rustc_env"></a>rustc_env | Additional variables to set on a crate's `rust_library::rustc_env` attribute. | `None` | 518| <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| <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| <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| <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 523**RETURNS** 524 525string: A json encoded string containing the specified version and separately all other inputs. 526 527 528<a id="crate.select"></a> 529 530## crate.select 531 532<pre> 533crate.select(<a href="#crate.select-common">common</a>, <a href="#crate.select-selects">selects</a>) 534</pre> 535 536A Starlark Select for `crate.annotation()`. 537 538**PARAMETERS** 539 540 541| Name | Description | Default Value | 542| :------------- | :------------- | :------------- | 543| <a id="crate.select-common"></a>common | A value that applies to all configurations. | none | 544| <a id="crate.select-selects"></a>selects | A dict of `target_triple` to values. | none | 545 546**RETURNS** 547 548struct: A struct representing the Starlark Select. 549 550 551<a id="crate.spec"></a> 552 553## crate.spec 554 555<pre> 556crate.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</pre> 558 559A constructor for a crate dependency. 560 561See [specifying dependencies][sd] in the Cargo book for more details. 562 563[sd]: https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html 564 565 566**PARAMETERS** 567 568 569| Name | Description | Default Value | 570| :------------- | :------------- | :------------- | 571| <a id="crate.spec-package"></a>package | The explicit name of the package (used when attempting to alias a crate). | `None` | 572| <a id="crate.spec-version"></a>version | The exact version of the crate. Cannot be used with `git`. | `None` | 573| <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| <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| <a id="crate.spec-default_features"></a>default_features | Maps to the `default-features` flag. | `True` | 576| <a id="crate.spec-features"></a>features | A list of features to use for the crate | `[]` | 577| <a id="crate.spec-git"></a>git | The Git url to use for the crate. Cannot be used with `version`. | `None` | 578| <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| <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| <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 582**RETURNS** 583 584string: A json encoded string of all inputs 585 586 587<a id="crate.workspace_member"></a> 588 589## crate.workspace_member 590 591<pre> 592crate.workspace_member(<a href="#crate.workspace_member-version">version</a>, <a href="#crate.workspace_member-sha256">sha256</a>) 593</pre> 594 595Define information for extra workspace members 596 597**PARAMETERS** 598 599 600| Name | Description | Default Value | 601| :------------- | :------------- | :------------- | 602| <a id="crate.workspace_member-version"></a>version | The semver of the crate to download. Must be an exact version. | none | 603| <a id="crate.workspace_member-sha256"></a>sha256 | The sha256 checksum of the `.crate` file. | `None` | 604 605**RETURNS** 606 607string: A json encoded string of all inputs 608 609 610<a id="crate_deps"></a> 611 612## crate_deps 613 614<pre> 615crate_deps(<a href="#crate_deps-deps">deps</a>, <a href="#crate_deps-package_name">package_name</a>) 616</pre> 617 618Finds the fully qualified label of the requested crates for the package where this macro is called. 619 620**PARAMETERS** 621 622 623| Name | Description | Default Value | 624| :------------- | :------------- | :------------- | 625| <a id="crate_deps-deps"></a>deps | The desired list of crate targets. | none | 626| <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 628**RETURNS** 629 630list: A list of labels to generated rust targets (str) 631 632 633<a id="crate_repositories"></a> 634 635## crate_repositories 636 637<pre> 638crate_repositories() 639</pre> 640 641A macro for defining repositories for all generated crates. 642 643 644**RETURNS** 645 646A list of repos visible to the module through the module extension. 647 648 649<a id="crate_universe_dependencies"></a> 650 651## crate_universe_dependencies 652 653<pre> 654crate_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</pre> 656 657Define dependencies of the `cargo-bazel` Rust target 658 659**PARAMETERS** 660 661 662| Name | Description | Default Value | 663| :------------- | :------------- | :------------- | 664| <a id="crate_universe_dependencies-rust_version"></a>rust_version | The version of rust to use when generating dependencies. | `"1.80.0"` | 665| <a id="crate_universe_dependencies-bootstrap"></a>bootstrap | If true, a `cargo_bootstrap_repository` target will be generated. | `False` | 666| <a id="crate_universe_dependencies-kwargs"></a>kwargs | Arguments to pass through to cargo_bazel_bootstrap. | none | 667 668**RETURNS** 669 670list[struct(repo=str, is_dev_dep=bool)]: A list of the repositories 671 defined by this macro. 672 673 674<a id="render_config"></a> 675 676## render_config 677 678<pre> 679render_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 <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 <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 <a href="#render_config-generate_rules_license_metadata">generate_rules_license_metadata</a>) 683</pre> 684 685Various settings used to configure rendered outputs 686 687The template parameters each support a select number of format keys. A description of each key 688can be found below where the supported keys for each template can be found in the parameter docs 689 690| key | definition | 691| --- | --- | 692| `name` | The name of the crate. Eg `tokio` | 693| `repository` | The rendered repository name for the crate. Directly relates to `crate_repository_template`. | 694| `triple` | A platform triple. Eg `x86_64-unknown-linux-gnu` | 695| `version` | The crate version. Eg `1.2.3` | 696| `target` | The library or binary target of the crate | 697| `file` | The basename of a file | 698 699 700**PARAMETERS** 701 702 703| Name | Description | Default Value | 704| :------------- | :------------- | :------------- | 705| <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| <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| <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| <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| <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| <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| <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| <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| <a id="render_config-regen_command"></a>regen_command | An optional command to demonstrate how generated files should be regenerated. | `None` | 714| <a id="render_config-vendor_mode"></a>vendor_mode | An optional configuration for rendirng content to be rendered into repositories. | `None` | 715| <a id="render_config-generate_rules_license_metadata"></a>generate_rules_license_metadata | Whether to generate rules license metedata | `False` | 716 717**RETURNS** 718 719string: A json encoded struct to match the Rust `config::RenderConfig` struct 720 721 722<a id="splicing_config"></a> 723 724## splicing_config 725 726<pre> 727splicing_config(<a href="#splicing_config-resolver_version">resolver_version</a>) 728</pre> 729 730Various settings used to configure Cargo manifest splicing behavior. 731 732[rv]: https://doc.rust-lang.org/cargo/reference/resolver.html#resolver-versions 733 734 735**PARAMETERS** 736 737 738| Name | Description | Default Value | 739| :------------- | :------------- | :------------- | 740| <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 742**RETURNS** 743 744str: A json encoded string of the parameters provided 745 746 747<a id="crates_repository"></a> 748 749## crates_repository 750 751<pre> 752crates_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 <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 <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 <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 <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 <a href="#crates_repository-supported_platform_triples">supported_platform_triples</a>) 758</pre> 759 760A rule for defining and downloading Rust dependencies (crates). This rule 761handles all the same [workflows](#workflows) `crate_universe` rules do. 762 763Environment Variables: 764 765| variable | usage | 766| --- | --- | 767| `CARGO_BAZEL_GENERATOR_SHA256` | The sha256 checksum of the file located at `CARGO_BAZEL_GENERATOR_URL` | 768| `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| `CARGO_BAZEL_ISOLATED` | An authorative flag as to whether or not the `CARGO_HOME` environment variable should be isolated from the host configuration | 770| `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| `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 773Example: 774 775Given the following workspace structure: 776 777```text 778[workspace]/ 779 WORKSPACE.bazel 780 BUILD.bazel 781 Cargo.toml 782 Cargo.Bazel.lock 783 src/ 784 main.rs 785``` 786 787The following is something that'd be found in the `WORKSPACE` file: 788 789```python 790load("@rules_rust//crate_universe:defs.bzl", "crates_repository", "crate") 791 792crates_repository( 793 name = "crate_index", 794 annotations = { 795 "rand": [crate.annotation( 796 default_features = False, 797 features = ["small_rng"], 798 )], 799 }, 800 cargo_lockfile = "//:Cargo.Bazel.lock", 801 lockfile = "//:cargo-bazel-lock.json", 802 manifests = ["//:Cargo.toml"], 803 # Should match the version represented by the currently registered `rust_toolchain`. 804 rust_version = "1.60.0", 805) 806``` 807 808The above will create an external repository which contains aliases and macros for accessing 809Rust targets found in the dependency graph defined by the given manifests. 810 811**NOTE**: The `cargo_lockfile` and `lockfile` must be manually created. The rule unfortunately does not yet create 812it on its own. When initially setting up this rule, an empty file should be created and then 813populated by repinning dependencies. 814 815### Repinning / Updating Dependencies 816 817Dependency syncing and updating is done in the repository rule which means it's done during the 818analysis phase of builds. As mentioned in the environments variable table above, the `CARGO_BAZEL_REPIN` 819(or `REPIN`) environment variables can be used to force the rule to update dependencies and potentially 820render a new lockfile. Given an instance of this repository rule named `crate_index`, the easiest way to 821repin dependencies is to run: 822 823```shell 824CARGO_BAZEL_REPIN=1 bazel sync --only=crate_index 825``` 826 827This will result in all dependencies being updated for a project. The `CARGO_BAZEL_REPIN` environment variable 828can also be used to customize how dependencies are updated. The following table shows translations from environment 829variable values to the equivilant [cargo update](https://doc.rust-lang.org/cargo/commands/cargo-update.html) command 830that is called behind the scenes to update dependencies. 831 832| Value | Cargo command | 833| --- | --- | 834| Any of [`true`, `1`, `yes`, `on`, `workspace`] | `cargo update --workspace` | 835| Any of [`full`, `eager`, `all`] | `cargo update` | 836| `package_name` | `cargo upgrade --package package_name` | 837| `[email protected]` | `cargo upgrade --package [email protected]` | 838| `[email protected]=4.5.6` | `cargo upgrade --package [email protected] --precise=4.5.6` | 839 840If the `crates_repository` is used multiple times in the same Bazel workspace (e.g. for multiple independent 841Rust workspaces), it may additionally be useful to use the `CARGO_BAZEL_REPIN_ONLY` environment variable, which 842limits execution of the repinning to one or multiple instances of the `crates_repository` rule via a comma-delimited 843allowlist: 844 845```shell 846CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_index 847``` 848 849**ATTRIBUTES** 850 851 852| Name | Description | Type | Mandatory | Default | 853| :------------- | :------------- | :------------- | :------------- | :------------- | 854| <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| <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| <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| <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| <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| <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| <a id="crates_repository-generate_target_compatible_with"></a>generate_target_compatible_with | DEPRECATED: Moved to `render_config`. | Boolean | optional | `True` | 861| <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| <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| <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| <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| <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| <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| <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| <a id="crates_repository-quiet"></a>quiet | If stdout and stderr should not be printed to the terminal. | Boolean | optional | `True` | 869| <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| <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| <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| <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| <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| <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| <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| <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 878**ENVIRONMENT VARIABLES** 879 880This repository rule depends on the following environment variables: 881* `CARGO_BAZEL_GENERATOR_URL` 882* `CARGO_BAZEL_GENERATOR_SHA256` 883* `CARGO_BAZEL_REPIN` 884* `REPIN` 885* `CARGO_BAZEL_REPIN_ONLY` 886* `CARGO_BAZEL_ISOLATED` 887* `CARGO_BAZEL_DEBUG` 888 889 890