xref: /aosp_15_r20/external/bazelbuild-rules_rust/docs/cargo.md (revision d4726bddaa87cc4778e7472feed243fa4b6c267f)
1*d4726bddSHONG Yifan<!-- Generated with Stardoc: http://skydoc.bazel.build -->
2*d4726bddSHONG Yifan# Cargo
3*d4726bddSHONG Yifan
4*d4726bddSHONG Yifan* [cargo_bootstrap_repository](#cargo_bootstrap_repository)
5*d4726bddSHONG Yifan* [cargo_build_script](#cargo_build_script)
6*d4726bddSHONG Yifan* [cargo_dep_env](#cargo_dep_env)
7*d4726bddSHONG Yifan* [cargo_env](#cargo_env)
8*d4726bddSHONG Yifan
9*d4726bddSHONG Yifan<a id="cargo_dep_env"></a>
10*d4726bddSHONG Yifan
11*d4726bddSHONG Yifan## cargo_dep_env
12*d4726bddSHONG Yifan
13*d4726bddSHONG Yifan<pre>
14*d4726bddSHONG Yifancargo_dep_env(<a href="#cargo_dep_env-name">name</a>, <a href="#cargo_dep_env-src">src</a>, <a href="#cargo_dep_env-out_dir">out_dir</a>)
15*d4726bddSHONG Yifan</pre>
16*d4726bddSHONG Yifan
17*d4726bddSHONG YifanA rule for generating variables for dependent `cargo_build_script`s without a build script. This is useful for using Bazel rules instead of a build script, while also generating configuration information for build scripts which depend on this crate.
18*d4726bddSHONG Yifan
19*d4726bddSHONG Yifan**ATTRIBUTES**
20*d4726bddSHONG Yifan
21*d4726bddSHONG Yifan
22*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
23*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
24*d4726bddSHONG Yifan| <a id="cargo_dep_env-name"></a>name |  A unique name for this target.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
25*d4726bddSHONG Yifan| <a id="cargo_dep_env-src"></a>src |  File containing additional environment variables to set for build scripts of direct dependencies.<br><br>This has the same effect as a `cargo_build_script` which prints `cargo:VAR=VALUE` lines, but without requiring a build script.<br><br>This files should  contain a single variable per line, of format `NAME=value`, and newlines may be included in a value by ending a line with a trailing back-slash (`\\`).   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
26*d4726bddSHONG Yifan| <a id="cargo_dep_env-out_dir"></a>out_dir |  Folder containing additional inputs when building all direct dependencies.<br><br>This has the same effect as a `cargo_build_script` which prints puts files into `$OUT_DIR`, but without requiring a build script.   | <a href="https://bazel.build/concepts/labels">Label</a> | optional |  `None`  |
27*d4726bddSHONG Yifan
28*d4726bddSHONG Yifan
29*d4726bddSHONG Yifan<a id="cargo_build_script"></a>
30*d4726bddSHONG Yifan
31*d4726bddSHONG Yifan## cargo_build_script
32*d4726bddSHONG Yifan
33*d4726bddSHONG Yifan<pre>
34*d4726bddSHONG Yifancargo_build_script(<a href="#cargo_build_script-name">name</a>, <a href="#cargo_build_script-edition">edition</a>, <a href="#cargo_build_script-crate_name">crate_name</a>, <a href="#cargo_build_script-crate_root">crate_root</a>, <a href="#cargo_build_script-srcs">srcs</a>, <a href="#cargo_build_script-crate_features">crate_features</a>, <a href="#cargo_build_script-version">version</a>, <a href="#cargo_build_script-deps">deps</a>,
35*d4726bddSHONG Yifan                   <a href="#cargo_build_script-link_deps">link_deps</a>, <a href="#cargo_build_script-proc_macro_deps">proc_macro_deps</a>, <a href="#cargo_build_script-build_script_env">build_script_env</a>, <a href="#cargo_build_script-data">data</a>, <a href="#cargo_build_script-compile_data">compile_data</a>, <a href="#cargo_build_script-tools">tools</a>, <a href="#cargo_build_script-links">links</a>,
36*d4726bddSHONG Yifan                   <a href="#cargo_build_script-rundir">rundir</a>, <a href="#cargo_build_script-rustc_env">rustc_env</a>, <a href="#cargo_build_script-rustc_env_files">rustc_env_files</a>, <a href="#cargo_build_script-rustc_flags">rustc_flags</a>, <a href="#cargo_build_script-visibility">visibility</a>, <a href="#cargo_build_script-tags">tags</a>, <a href="#cargo_build_script-aliases">aliases</a>,
37*d4726bddSHONG Yifan                   <a href="#cargo_build_script-pkg_name">pkg_name</a>, <a href="#cargo_build_script-kwargs">kwargs</a>)
38*d4726bddSHONG Yifan</pre>
39*d4726bddSHONG Yifan
40*d4726bddSHONG YifanCompile and execute a rust build script to generate build attributes
41*d4726bddSHONG Yifan
42*d4726bddSHONG YifanThis rules take the same arguments as rust_binary.
43*d4726bddSHONG Yifan
44*d4726bddSHONG YifanExample:
45*d4726bddSHONG Yifan
46*d4726bddSHONG YifanSuppose you have a crate with a cargo build script `build.rs`:
47*d4726bddSHONG Yifan
48*d4726bddSHONG Yifan```output
49*d4726bddSHONG Yifan[workspace]/
50*d4726bddSHONG Yifan    hello_lib/
51*d4726bddSHONG Yifan        BUILD
52*d4726bddSHONG Yifan        build.rs
53*d4726bddSHONG Yifan        src/
54*d4726bddSHONG Yifan            lib.rs
55*d4726bddSHONG Yifan```
56*d4726bddSHONG Yifan
57*d4726bddSHONG YifanThen you want to use the build script in the following:
58*d4726bddSHONG Yifan
59*d4726bddSHONG Yifan`hello_lib/BUILD`:
60*d4726bddSHONG Yifan```python
61*d4726bddSHONG Yifanpackage(default_visibility = ["//visibility:public"])
62*d4726bddSHONG Yifan
63*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
64*d4726bddSHONG Yifanload("@rules_rust//cargo:defs.bzl", "cargo_build_script")
65*d4726bddSHONG Yifan
66*d4726bddSHONG Yifan# This will run the build script from the root of the workspace, and
67*d4726bddSHONG Yifan# collect the outputs.
68*d4726bddSHONG Yifancargo_build_script(
69*d4726bddSHONG Yifan    name = "build_script",
70*d4726bddSHONG Yifan    srcs = ["build.rs"],
71*d4726bddSHONG Yifan    # Optional environment variables passed during build.rs compilation
72*d4726bddSHONG Yifan    rustc_env = {
73*d4726bddSHONG Yifan       "CARGO_PKG_VERSION": "0.1.2",
74*d4726bddSHONG Yifan    },
75*d4726bddSHONG Yifan    # Optional environment variables passed during build.rs execution.
76*d4726bddSHONG Yifan    # Note that as the build script's working directory is not execroot,
77*d4726bddSHONG Yifan    # execpath/location will return an absolute path, instead of a relative
78*d4726bddSHONG Yifan    # one.
79*d4726bddSHONG Yifan    build_script_env = {
80*d4726bddSHONG Yifan        "SOME_TOOL_OR_FILE": "$(execpath @tool//:binary)"
81*d4726bddSHONG Yifan    },
82*d4726bddSHONG Yifan    # Optional data/tool dependencies
83*d4726bddSHONG Yifan    data = ["@tool//:binary"],
84*d4726bddSHONG Yifan)
85*d4726bddSHONG Yifan
86*d4726bddSHONG Yifanrust_library(
87*d4726bddSHONG Yifan    name = "hello_lib",
88*d4726bddSHONG Yifan    srcs = [
89*d4726bddSHONG Yifan        "src/lib.rs",
90*d4726bddSHONG Yifan    ],
91*d4726bddSHONG Yifan    deps = [":build_script"],
92*d4726bddSHONG Yifan)
93*d4726bddSHONG Yifan```
94*d4726bddSHONG Yifan
95*d4726bddSHONG YifanThe `hello_lib` target will be build with the flags and the environment variables declared by the     build script in addition to the file generated by it.
96*d4726bddSHONG Yifan
97*d4726bddSHONG Yifan
98*d4726bddSHONG Yifan**PARAMETERS**
99*d4726bddSHONG Yifan
100*d4726bddSHONG Yifan
101*d4726bddSHONG Yifan| Name  | Description | Default Value |
102*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
103*d4726bddSHONG Yifan| <a id="cargo_build_script-name"></a>name |  The name for the underlying rule. This should be the name of the package being compiled, optionally with a suffix of `_bs`. Otherwise, you can set the package name via `pkg_name`.   |  none |
104*d4726bddSHONG Yifan| <a id="cargo_build_script-edition"></a>edition |  The rust edition to use for the internal binary crate.   |  `None` |
105*d4726bddSHONG Yifan| <a id="cargo_build_script-crate_name"></a>crate_name |  Crate name to use for build script.   |  `None` |
106*d4726bddSHONG Yifan| <a id="cargo_build_script-crate_root"></a>crate_root |  The file that will be passed to rustc to be used for building this crate.   |  `None` |
107*d4726bddSHONG Yifan| <a id="cargo_build_script-srcs"></a>srcs |  Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made.   |  `[]` |
108*d4726bddSHONG Yifan| <a id="cargo_build_script-crate_features"></a>crate_features |  A list of features to enable for the build script.   |  `[]` |
109*d4726bddSHONG Yifan| <a id="cargo_build_script-version"></a>version |  The semantic version (semver) of the crate.   |  `None` |
110*d4726bddSHONG Yifan| <a id="cargo_build_script-deps"></a>deps |  The build-dependencies of the crate.   |  `[]` |
111*d4726bddSHONG Yifan| <a id="cargo_build_script-link_deps"></a>link_deps |  The subset of the (normal) dependencies of the crate that have the links attribute and therefore provide environment variables to this build script.   |  `[]` |
112*d4726bddSHONG Yifan| <a id="cargo_build_script-proc_macro_deps"></a>proc_macro_deps |  List of rust_proc_macro targets used to build the script.   |  `[]` |
113*d4726bddSHONG Yifan| <a id="cargo_build_script-build_script_env"></a>build_script_env |  Environment variables for build scripts.   |  `{}` |
114*d4726bddSHONG Yifan| <a id="cargo_build_script-data"></a>data |  Files needed by the build script.   |  `[]` |
115*d4726bddSHONG Yifan| <a id="cargo_build_script-compile_data"></a>compile_data |  Files needed for the compilation of the build script.   |  `[]` |
116*d4726bddSHONG Yifan| <a id="cargo_build_script-tools"></a>tools |  Tools (executables) needed by the build script.   |  `[]` |
117*d4726bddSHONG Yifan| <a id="cargo_build_script-links"></a>links |  Name of the native library this crate links against.   |  `None` |
118*d4726bddSHONG Yifan| <a id="cargo_build_script-rundir"></a>rundir |  A directory to `cd` to before the cargo_build_script is run. This should be a path relative to the exec root.<br><br>The default behaviour (and the behaviour if rundir is set to the empty string) is to change to the relative path corresponding to the cargo manifest directory, which replicates the normal behaviour of cargo so it is easy to write compatible build scripts.<br><br>If set to `.`, the cargo build script will run in the exec root.   |  `None` |
119*d4726bddSHONG Yifan| <a id="cargo_build_script-rustc_env"></a>rustc_env |  Environment variables to set in rustc when compiling the build script.   |  `{}` |
120*d4726bddSHONG Yifan| <a id="cargo_build_script-rustc_env_files"></a>rustc_env_files |  Files containing additional environment variables to set for rustc when building the build script.   |  `[]` |
121*d4726bddSHONG Yifan| <a id="cargo_build_script-rustc_flags"></a>rustc_flags |  List of compiler flags passed to `rustc`.   |  `[]` |
122*d4726bddSHONG Yifan| <a id="cargo_build_script-visibility"></a>visibility |  Visibility to apply to the generated build script output.   |  `None` |
123*d4726bddSHONG Yifan| <a id="cargo_build_script-tags"></a>tags |  (list of str, optional): Tags to apply to the generated build script output.   |  `None` |
124*d4726bddSHONG Yifan| <a id="cargo_build_script-aliases"></a>aliases |  Remap crates to a new name or moniker for linkage to this target.             These are other `rust_library` targets and will be presented as the new name given.   |  `None` |
125*d4726bddSHONG Yifan| <a id="cargo_build_script-pkg_name"></a>pkg_name |  Override the package name used for the build script. This is useful if the build target name gets too long otherwise.   |  `None` |
126*d4726bddSHONG Yifan| <a id="cargo_build_script-kwargs"></a>kwargs |  Forwards to the underlying `rust_binary` rule. An exception is the `compatible_with` attribute, which shouldn't be forwarded to the `rust_binary`, as the `rust_binary` is only built and used in `exec` mode. We propagate the `compatible_with` attribute to the `_build_scirpt_run` target.   |  none |
127*d4726bddSHONG Yifan
128*d4726bddSHONG Yifan
129*d4726bddSHONG Yifan<a id="cargo_env"></a>
130*d4726bddSHONG Yifan
131*d4726bddSHONG Yifan## cargo_env
132*d4726bddSHONG Yifan
133*d4726bddSHONG Yifan<pre>
134*d4726bddSHONG Yifancargo_env(<a href="#cargo_env-env">env</a>)
135*d4726bddSHONG Yifan</pre>
136*d4726bddSHONG Yifan
137*d4726bddSHONG YifanA helper for generating platform specific environment variables
138*d4726bddSHONG Yifan
139*d4726bddSHONG Yifan```python
140*d4726bddSHONG Yifanload("@rules_rust//rust:defs.bzl", "rust_common")
141*d4726bddSHONG Yifanload("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env")
142*d4726bddSHONG Yifan
143*d4726bddSHONG Yifancargo_bootstrap_repository(
144*d4726bddSHONG Yifan    name = "bootstrapped_bin",
145*d4726bddSHONG Yifan    cargo_lockfile = "//:Cargo.lock",
146*d4726bddSHONG Yifan    cargo_toml = "//:Cargo.toml",
147*d4726bddSHONG Yifan    srcs = ["//:resolver_srcs"],
148*d4726bddSHONG Yifan    version = rust_common.default_version,
149*d4726bddSHONG Yifan    binary = "my-crate-binary",
150*d4726bddSHONG Yifan    env = {
151*d4726bddSHONG Yifan        "x86_64-unknown-linux-gnu": cargo_env({
152*d4726bddSHONG Yifan            "FOO": "BAR",
153*d4726bddSHONG Yifan        }),
154*d4726bddSHONG Yifan    },
155*d4726bddSHONG Yifan    env_label = {
156*d4726bddSHONG Yifan        "aarch64-unknown-linux-musl": cargo_env({
157*d4726bddSHONG Yifan            "DOC": "//:README.md",
158*d4726bddSHONG Yifan        }),
159*d4726bddSHONG Yifan    }
160*d4726bddSHONG Yifan)
161*d4726bddSHONG Yifan```
162*d4726bddSHONG Yifan
163*d4726bddSHONG Yifan
164*d4726bddSHONG Yifan**PARAMETERS**
165*d4726bddSHONG Yifan
166*d4726bddSHONG Yifan
167*d4726bddSHONG Yifan| Name  | Description | Default Value |
168*d4726bddSHONG Yifan| :------------- | :------------- | :------------- |
169*d4726bddSHONG Yifan| <a id="cargo_env-env"></a>env |  A map of environment variables   |  none |
170*d4726bddSHONG Yifan
171*d4726bddSHONG Yifan**RETURNS**
172*d4726bddSHONG Yifan
173*d4726bddSHONG Yifanstr: A json encoded string of the environment variables
174*d4726bddSHONG Yifan
175*d4726bddSHONG Yifan
176*d4726bddSHONG Yifan<a id="cargo_bootstrap_repository"></a>
177*d4726bddSHONG Yifan
178*d4726bddSHONG Yifan## cargo_bootstrap_repository
179*d4726bddSHONG Yifan
180*d4726bddSHONG Yifan<pre>
181*d4726bddSHONG Yifancargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-binary">binary</a>, <a href="#cargo_bootstrap_repository-build_mode">build_mode</a>, <a href="#cargo_bootstrap_repository-cargo_lockfile">cargo_lockfile</a>, <a href="#cargo_bootstrap_repository-cargo_toml">cargo_toml</a>, <a href="#cargo_bootstrap_repository-env">env</a>,
182*d4726bddSHONG Yifan                           <a href="#cargo_bootstrap_repository-env_label">env_label</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_cargo_template">rust_toolchain_cargo_template</a>,
183*d4726bddSHONG Yifan                           <a href="#cargo_bootstrap_repository-rust_toolchain_rustc_template">rust_toolchain_rustc_template</a>, <a href="#cargo_bootstrap_repository-timeout">timeout</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
184*d4726bddSHONG Yifan</pre>
185*d4726bddSHONG Yifan
186*d4726bddSHONG YifanA rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/)
187*d4726bddSHONG Yifan
188*d4726bddSHONG Yifan**ATTRIBUTES**
189*d4726bddSHONG Yifan
190*d4726bddSHONG Yifan
191*d4726bddSHONG Yifan| Name  | Description | Type | Mandatory | Default |
192*d4726bddSHONG Yifan| :------------- | :------------- | :------------- | :------------- | :------------- |
193*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-name"></a>name |  A unique name for this repository.   | <a href="https://bazel.build/concepts/labels#target-names">Name</a> | required |  |
194*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-srcs"></a>srcs |  Souce files of the crate to build. Passing source files here can be used to trigger rebuilds when changes are made   | <a href="https://bazel.build/concepts/labels">List of labels</a> | optional |  `[]`  |
195*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-binary"></a>binary |  The binary to build (the `--bin` parameter for Cargo). If left empty, the repository name will be used.   | String | optional |  `""`  |
196*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-build_mode"></a>build_mode |  The build mode the binary should be built with   | String | optional |  `"release"`  |
197*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-cargo_lockfile"></a>cargo_lockfile |  The lockfile of the crate_universe resolver   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
198*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml |  The path of the crate_universe resolver manifest (`Cargo.toml` file)   | <a href="https://bazel.build/concepts/labels">Label</a> | required |  |
199*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-env"></a>env |  A mapping of platform triple to a set of environment variables. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
200*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-env_label"></a>env_label |  A mapping of platform triple to a set of environment variables. This attribute differs from `env` in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. Additionally, the platform triple `*` applies to all platforms.   | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional |  `{}`  |
201*d4726bddSHONG Yifan| <a id="cargo_bootstrap_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 |  |
202*d4726bddSHONG Yifan| <a id="cargo_bootstrap_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'), `{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}"`  |
203*d4726bddSHONG Yifan| <a id="cargo_bootstrap_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'), `{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}"`  |
204*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-timeout"></a>timeout |  Maximum duration of the Cargo build command in seconds   | Integer | optional |  `600`  |
205*d4726bddSHONG Yifan| <a id="cargo_bootstrap_repository-version"></a>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"`  |
206*d4726bddSHONG Yifan
207*d4726bddSHONG Yifan
208