Skip to content

Commit e2f2bff

Browse files
authored
cargo_build_script can use rustc_env_files (#2276)
Fixes #2248
1 parent 364f130 commit e2f2bff

File tree

7 files changed

+46
-2
lines changed

7 files changed

+46
-2
lines changed

cargo/private/cargo_build_script_wrapper.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def cargo_build_script(
2626
links = None,
2727
rundir = None,
2828
rustc_env = {},
29+
rustc_env_files = [],
2930
rustc_flags = [],
3031
visibility = None,
3132
tags = None,
@@ -113,6 +114,8 @@ def cargo_build_script(
113114
114115
If set to `.`, the cargo build script will run in the exec root.
115116
rustc_env (dict, optional): Environment variables to set in rustc when compiling the build script.
117+
rustc_env_files (list of label, optional): Files containing additional environment variables to set for rustc
118+
when building the build script.
116119
rustc_flags (list, optional): List of compiler flags passed to `rustc`.
117120
visibility (list of label, optional): Visibility to apply to the generated build script output.
118121
tags: (list of str, optional): Tags to apply to the generated build script output.
@@ -149,6 +152,7 @@ def cargo_build_script(
149152
data = data,
150153
compile_data = compile_data,
151154
rustc_env = rustc_env,
155+
rustc_env_files = rustc_env_files,
152156
rustc_flags = rustc_flags,
153157
edition = edition,
154158
tags = binary_tags,

docs/cargo.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ A rule for generating variables for dependent `cargo_build_script`s without a bu
6666
<pre>
6767
cargo_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>,
6868
<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>,
69-
<a href="#cargo_build_script-rundir">rundir</a>, <a href="#cargo_build_script-rustc_env">rustc_env</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>, <a href="#cargo_build_script-kwargs">kwargs</a>)
69+
<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>, <a href="#cargo_build_script-kwargs">kwargs</a>)
7070
</pre>
7171

7272
Compile and execute a rust build script to generate build attributes
@@ -149,6 +149,7 @@ The `hello_lib` target will be build with the flags and the environment variable
149149
| <a id="cargo_build_script-links"></a>links | Name of the native library this crate links against. | `None` |
150150
| <a id="cargo_build_script-rundir"></a>rundir | A directory to <code>cd</code> 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 <code>.</code>, the cargo build script will run in the exec root. | `None` |
151151
| <a id="cargo_build_script-rustc_env"></a>rustc_env | Environment variables to set in rustc when compiling the build script. | `{}` |
152+
| <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. | `[]` |
152153
| <a id="cargo_build_script-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>. | `[]` |
153154
| <a id="cargo_build_script-visibility"></a>visibility | Visibility to apply to the generated build script output. | `None` |
154155
| <a id="cargo_build_script-tags"></a>tags | (list of str, optional): Tags to apply to the generated build script output. | `None` |

docs/flatten.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ A collection of files either found within the `rust-stdlib` artifact or generate
15471547
<pre>
15481548
cargo_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>,
15491549
<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>,
1550-
<a href="#cargo_build_script-rundir">rundir</a>, <a href="#cargo_build_script-rustc_env">rustc_env</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>, <a href="#cargo_build_script-kwargs">kwargs</a>)
1550+
<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>, <a href="#cargo_build_script-kwargs">kwargs</a>)
15511551
</pre>
15521552

15531553
Compile and execute a rust build script to generate build attributes
@@ -1630,6 +1630,7 @@ The `hello_lib` target will be build with the flags and the environment variable
16301630
| <a id="cargo_build_script-links"></a>links | Name of the native library this crate links against. | `None` |
16311631
| <a id="cargo_build_script-rundir"></a>rundir | A directory to <code>cd</code> 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 <code>.</code>, the cargo build script will run in the exec root. | `None` |
16321632
| <a id="cargo_build_script-rustc_env"></a>rustc_env | Environment variables to set in rustc when compiling the build script. | `{}` |
1633+
| <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. | `[]` |
16331634
| <a id="cargo_build_script-rustc_flags"></a>rustc_flags | List of compiler flags passed to <code>rustc</code>. | `[]` |
16341635
| <a id="cargo_build_script-visibility"></a>visibility | Visibility to apply to the generated build script output. | `None` |
16351636
| <a id="cargo_build_script-tags"></a>tags | (list of str, optional): Tags to apply to the generated build script output. | `None` |

examples/hello_world/example_test.sh

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
2+
load("//cargo:defs.bzl", "cargo_build_script")
3+
load("//rust:defs.bzl", "rust_library", "rust_test")
4+
5+
write_file(
6+
name = "generate_rustc_env_file",
7+
out = "rustc_env_file",
8+
content = [
9+
"GREETING=Howdy",
10+
"",
11+
],
12+
)
13+
14+
cargo_build_script(
15+
name = "cargo_build_script",
16+
srcs = ["build.rs"],
17+
edition = "2018",
18+
rustc_env_files = [":generate_rustc_env_file"],
19+
)
20+
21+
rust_library(
22+
name = "test_lib",
23+
srcs = ["test_lib.rs"],
24+
edition = "2018",
25+
deps = [":cargo_build_script"],
26+
)
27+
28+
rust_test(
29+
name = "consume_build_script",
30+
crate = ":test_lib",
31+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("cargo:rustc-env=FROM_BUILD_SCRIPT={}", env!("GREETING"));
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn check_env_set() {
3+
assert_eq!("Howdy", env!("FROM_BUILD_SCRIPT"));
4+
}

0 commit comments

Comments
 (0)