Skip to content

Commit 251c5da

Browse files
Added support for plat specific env vars to cargo_bootstrap_repository (#932)
* Added support for plat specific env vars to cargo_bootstrap_repository * Regenerate documentation * Update cargo/cargo_bootstrap.bzl Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com> Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
1 parent 531053e commit 251c5da

File tree

6 files changed

+184
-10
lines changed

6 files changed

+184
-10
lines changed

cargo/cargo_bootstrap.bzl

+80-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def cargo_bootstrap(
1414
rustc_bin,
1515
binary,
1616
cargo_manifest,
17+
environment = {},
1718
quiet = False,
1819
build_mode = "release",
1920
target_dir = None):
@@ -25,6 +26,7 @@ def cargo_bootstrap(
2526
rustc_bin (path): The path to a Rustc binary.
2627
binary (str): The binary to build (the `--bin` parameter for Cargo).
2728
cargo_manifest (path): The path to a Cargo manifest (Cargo.toml file).
29+
environment (dict): Environment variables to use during execution.
2830
quiet (bool, optional): Whether or not to print output from the Cargo command.
2931
build_mode (str, optional): The build mode to use
3032
target_dir (path, optional): The directory in which to produce build outputs
@@ -55,12 +57,14 @@ def cargo_bootstrap(
5557
if build_mode == "release":
5658
args.append("--release")
5759

60+
env = dict({
61+
"RUSTC": str(rustc_bin),
62+
}.items() + environment.items())
63+
5864
repository_ctx.report_progress("Cargo Bootstrapping {}".format(binary))
5965
result = repository_ctx.execute(
6066
args,
61-
environment = {
62-
"RUSTC": str(rustc_bin),
63-
},
67+
environment = env,
6468
quiet = quiet,
6569
)
6670

@@ -108,6 +112,28 @@ rust_binary(
108112
)
109113
"""
110114

115+
def _collect_environ(repository_ctx, host_triple):
116+
"""Gather environment varialbes to use from the current rule context
117+
118+
Args:
119+
repository_ctx (repository_ctx): The rule's context object.
120+
host_triple (str): A string of the current host triple
121+
122+
Returns:
123+
dict: A map of environment variables
124+
"""
125+
env_vars = dict(json.decode(repository_ctx.attr.env.get(host_triple, "{}")))
126+
127+
# Gather the path for each label and ensure it exists
128+
env_labels = dict(json.decode(repository_ctx.attr.env_label.get(host_triple, "{}")))
129+
env_labels = {key: repository_ctx.path(Label(value)) for (key, value) in env_labels.items()}
130+
for key in env_labels:
131+
if not env_labels[key].exists:
132+
fail("File for key '{}' does not exist: {}", key, env_labels[key])
133+
env_labels = {key: str(value) for (key, value) in env_labels.items()}
134+
135+
return dict(env_vars.items() + env_labels.items())
136+
111137
def _cargo_bootstrap_repository_impl(repository_ctx):
112138
if repository_ctx.attr.version in ("beta", "nightly"):
113139
version_str = "{}-{}".format(repository_ctx.attr.version, repository_ctx.attr.iso_date)
@@ -124,13 +150,16 @@ def _cargo_bootstrap_repository_impl(repository_ctx):
124150

125151
binary_name = repository_ctx.attr.binary or repository_ctx.name
126152

153+
environment = _collect_environ(repository_ctx, host_triple.triple)
154+
127155
built_binary = cargo_bootstrap(
128156
repository_ctx,
129157
cargo_bin = tools.cargo,
130158
rustc_bin = tools.rustc,
131159
binary = binary_name,
132160
cargo_manifest = repository_ctx.path(repository_ctx.attr.cargo_toml),
133161
build_mode = repository_ctx.attr.build_mode,
162+
environment = environment,
134163
)
135164

136165
# Create a symlink so that the binary can be accesed via it's target name
@@ -166,6 +195,19 @@ cargo_bootstrap_repository = repository_rule(
166195
allow_single_file = ["Cargo.toml"],
167196
mandatory = True,
168197
),
198+
"env": attr.string_dict(
199+
doc = (
200+
"A mapping of platform triple to a set of environment variables. See " +
201+
"[cargo_env](#cargo_env) for usage details."
202+
),
203+
),
204+
"env_label": attr.string_dict(
205+
doc = (
206+
"A mapping of platform triple to a set of environment variables. This " +
207+
"attribute differs from `env` in that all variables passed here must be " +
208+
"fully qualified labels of files. See [cargo_env](#cargo_env) for usage details."
209+
),
210+
),
169211
"iso_date": attr.string(
170212
doc = "The iso_date of cargo binary the resolver should use. Note: This can only be set if `version` is `beta` or `nightly`",
171213
),
@@ -191,3 +233,38 @@ cargo_bootstrap_repository = repository_rule(
191233
),
192234
},
193235
)
236+
237+
def cargo_env(env):
238+
"""A helper for generating platform specific environment variables
239+
240+
```python
241+
load("@rules_rust//rust:defs.bzl", "rust_common")
242+
load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env")
243+
244+
cargo_bootstrap_repository(
245+
name = "bootstrapped_bin",
246+
cargo_lockfile = "//:Cargo.lock",
247+
cargo_toml = "//:Cargo.toml",
248+
srcs = ["//:resolver_srcs"],
249+
version = rust_common.default_version,
250+
binary = "my-crate-binary",
251+
env = {
252+
"x86_64-unknown-linux-gnu": cargo_env({
253+
"FOO": "BAR",
254+
}),
255+
},
256+
env_label = {
257+
"aarch64-unknown-linux-musl": cargo_env({
258+
"DOC": "//:README.md",
259+
}),
260+
}
261+
)
262+
```
263+
264+
Args:
265+
env (dict): A map of environment variables
266+
267+
Returns:
268+
str: A json encoded string of the environment variables
269+
"""
270+
return json.encode(dict(env))

cargo/defs.bzl

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Common definitions for the `@rules_rust//cargo` package"""
22

3-
load(":cargo_bootstrap.bzl", _cargo_bootstrap_repository = "cargo_bootstrap_repository")
3+
load(":cargo_bootstrap.bzl", _cargo_bootstrap_repository = "cargo_bootstrap_repository", _cargo_env = "cargo_env")
44
load(":cargo_build_script.bzl", _cargo_build_script = "cargo_build_script")
55

66
cargo_bootstrap_repository = _cargo_bootstrap_repository
7+
cargo_env = _cargo_env
8+
79
cargo_build_script = _cargo_build_script

docs/BUILD.bazel

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ PAGES = dict([
3636
page(
3737
name = "cargo",
3838
symbols = [
39-
"cargo_build_script",
4039
"cargo_bootstrap_repository",
40+
"cargo_build_script",
41+
"cargo_env",
4142
],
4243
),
4344
page(

docs/cargo.md

+49-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<!-- Generated with Stardoc: http://skydoc.bazel.build -->
22
# Cargo
33

4-
* [cargo_build_script](#cargo_build_script)
54
* [cargo_bootstrap_repository](#cargo_bootstrap_repository)
5+
* [cargo_build_script](#cargo_build_script)
6+
* [cargo_env](#cargo_env)
67

78
<a id="#cargo_bootstrap_repository"></a>
89

910
## cargo_bootstrap_repository
1011

1112
<pre>
12-
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</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-iso_date">iso_date</a>,
13-
<a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_repository_template">rust_toolchain_repository_template</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
13+
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</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>, <a href="#cargo_bootstrap_repository-env_label">env_label</a>,
14+
<a href="#cargo_bootstrap_repository-iso_date">iso_date</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_repository_template">rust_toolchain_repository_template</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
1415
</pre>
1516

1617
A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/)
@@ -25,6 +26,8 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
2526
| <a id="cargo_bootstrap_repository-build_mode"></a>build_mode | The build mode the binary should be built with | String | optional | "release" |
2627
| <a id="cargo_bootstrap_repository-cargo_lockfile"></a>cargo_lockfile | The lockfile of the crate_universe resolver | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
2728
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the crate_universe resolver manifest (<code>Cargo.toml</code> file) | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
29+
| <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. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
30+
| <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 <code>env</code> in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
2831
| <a id="cargo_bootstrap_repository-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note: This can only be set if <code>version</code> is <code>beta</code> or <code>nightly</code> | String | optional | "" |
2932
| <a id="cargo_bootstrap_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/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
3033
| <a id="cargo_bootstrap_repository-rust_toolchain_repository_template"></a>rust_toolchain_repository_template | The template to use for finding the host <code>rust_toolchain</code> repository. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{system}</code> (eg. 'darwin'), and <code>{arch}</code> (eg. 'aarch64') will be replaced in the string if present. | String | optional | "rust_{system}_{arch}" |
@@ -118,3 +121,46 @@ The `hello_lib` target will be build with the flags and the environment variable
118121
| <a id="cargo_build_script-kwargs"></a>kwargs | Forwards to the underlying <code>rust_binary</code> rule. | none |
119122

120123

124+
<a id="#cargo_env"></a>
125+
126+
## cargo_env
127+
128+
<pre>
129+
cargo_env(<a href="#cargo_env-env">env</a>)
130+
</pre>
131+
132+
A helper for generating platform specific environment variables
133+
134+
```python
135+
load("@rules_rust//rust:defs.bzl", "rust_common")
136+
load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env")
137+
138+
cargo_bootstrap_repository(
139+
name = "bootstrapped_bin",
140+
cargo_lockfile = "//:Cargo.lock",
141+
cargo_toml = "//:Cargo.toml",
142+
srcs = ["//:resolver_srcs"],
143+
version = rust_common.default_version,
144+
binary = "my-crate-binary",
145+
env = {
146+
"x86_64-unknown-linux-gnu": cargo_env({
147+
"FOO": "BAR",
148+
}),
149+
},
150+
env_label = {
151+
"aarch64-unknown-linux-musl": cargo_env({
152+
"DOC": "//:README.md",
153+
}),
154+
}
155+
)
156+
```
157+
158+
159+
**PARAMETERS**
160+
161+
162+
| Name | Description | Default Value |
163+
| :------------- | :------------- | :------------- |
164+
| <a id="cargo_env-env"></a>env | A map of environment variables | none |
165+
166+

docs/flatten.md

+48-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [StdLibInfo](#StdLibInfo)
66
* [cargo_bootstrap_repository](#cargo_bootstrap_repository)
77
* [cargo_build_script](#cargo_build_script)
8+
* [cargo_env](#cargo_env)
89
* [crate](#crate)
910
* [crate_universe](#crate_universe)
1011
* [error_format](#error_format)
@@ -52,8 +53,8 @@
5253
## cargo_bootstrap_repository
5354

5455
<pre>
55-
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</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-iso_date">iso_date</a>,
56-
<a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_repository_template">rust_toolchain_repository_template</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
56+
cargo_bootstrap_repository(<a href="#cargo_bootstrap_repository-name">name</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>, <a href="#cargo_bootstrap_repository-env_label">env_label</a>,
57+
<a href="#cargo_bootstrap_repository-iso_date">iso_date</a>, <a href="#cargo_bootstrap_repository-repo_mapping">repo_mapping</a>, <a href="#cargo_bootstrap_repository-rust_toolchain_repository_template">rust_toolchain_repository_template</a>, <a href="#cargo_bootstrap_repository-srcs">srcs</a>, <a href="#cargo_bootstrap_repository-version">version</a>)
5758
</pre>
5859

5960
A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/cargo/)
@@ -68,6 +69,8 @@ A rule for bootstrapping a Rust binary using [Cargo](https://doc.rust-lang.org/c
6869
| <a id="cargo_bootstrap_repository-build_mode"></a>build_mode | The build mode the binary should be built with | String | optional | "release" |
6970
| <a id="cargo_bootstrap_repository-cargo_lockfile"></a>cargo_lockfile | The lockfile of the crate_universe resolver | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
7071
| <a id="cargo_bootstrap_repository-cargo_toml"></a>cargo_toml | The path of the crate_universe resolver manifest (<code>Cargo.toml</code> file) | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | required | |
72+
| <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. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
73+
| <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 <code>env</code> in that all variables passed here must be fully qualified labels of files. See [cargo_env](#cargo_env) for usage details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
7174
| <a id="cargo_bootstrap_repository-iso_date"></a>iso_date | The iso_date of cargo binary the resolver should use. Note: This can only be set if <code>version</code> is <code>beta</code> or <code>nightly</code> | String | optional | "" |
7275
| <a id="cargo_bootstrap_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/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | required | |
7376
| <a id="cargo_bootstrap_repository-rust_toolchain_repository_template"></a>rust_toolchain_repository_template | The template to use for finding the host <code>rust_toolchain</code> repository. <code>{version}</code> (eg. '1.53.0'), <code>{triple}</code> (eg. 'x86_64-unknown-linux-gnu'), <code>{system}</code> (eg. 'darwin'), and <code>{arch}</code> (eg. 'aarch64') will be replaced in the string if present. | String | optional | "rust_{system}_{arch}" |
@@ -1603,6 +1606,49 @@ The `hello_lib` target will be build with the flags and the environment variable
16031606
| <a id="cargo_build_script-kwargs"></a>kwargs | Forwards to the underlying <code>rust_binary</code> rule. | none |
16041607

16051608

1609+
<a id="#cargo_env"></a>
1610+
1611+
## cargo_env
1612+
1613+
<pre>
1614+
cargo_env(<a href="#cargo_env-env">env</a>)
1615+
</pre>
1616+
1617+
A helper for generating platform specific environment variables
1618+
1619+
```python
1620+
load("@rules_rust//rust:defs.bzl", "rust_common")
1621+
load("@rules_rust//cargo:defs.bzl", "cargo_bootstrap_repository", "cargo_env")
1622+
1623+
cargo_bootstrap_repository(
1624+
name = "bootstrapped_bin",
1625+
cargo_lockfile = "//:Cargo.lock",
1626+
cargo_toml = "//:Cargo.toml",
1627+
srcs = ["//:resolver_srcs"],
1628+
version = rust_common.default_version,
1629+
binary = "my-crate-binary",
1630+
env = {
1631+
"x86_64-unknown-linux-gnu": cargo_env({
1632+
"FOO": "BAR",
1633+
}),
1634+
},
1635+
env_label = {
1636+
"aarch64-unknown-linux-musl": cargo_env({
1637+
"DOC": "//:README.md",
1638+
}),
1639+
}
1640+
)
1641+
```
1642+
1643+
1644+
**PARAMETERS**
1645+
1646+
1647+
| Name | Description | Default Value |
1648+
| :------------- | :------------- | :------------- |
1649+
| <a id="cargo_env-env"></a>env | A map of environment variables | none |
1650+
1651+
16061652
<a id="#crate.spec"></a>
16071653

16081654
## crate.spec

docs/symbols.bzl

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ load(
1616
"@rules_rust//cargo:defs.bzl",
1717
_cargo_bootstrap_repository = "cargo_bootstrap_repository",
1818
_cargo_build_script = "cargo_build_script",
19+
_cargo_env = "cargo_env",
1920
)
2021
load(
2122
"@rules_rust//crate_universe:defs.bzl",
@@ -122,6 +123,7 @@ rust_proto_transitive_repositories = _rust_proto_transitive_repositories
122123

123124
cargo_build_script = _cargo_build_script
124125
cargo_bootstrap_repository = _cargo_bootstrap_repository
126+
cargo_env = _cargo_env
125127

126128
rust_wasm_bindgen = _rust_wasm_bindgen
127129
rust_wasm_bindgen_toolchain = _rust_wasm_bindgen_toolchain

0 commit comments

Comments
 (0)