Skip to content

Commit 56176ea

Browse files
authored
Allow passing env files to build script runtime env (#3632)
Fixes #3625, see that issue for explanation
1 parent 069565d commit 56176ea

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

cargo/private/cargo_build_script.bzl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,9 @@ def _cargo_build_script_impl(ctx):
536536

537537
build_script_inputs = []
538538

539+
build_script_inputs.extend(ctx.files.build_script_env_files)
540+
args.add_all(ctx.files.build_script_env_files, format_each = "--input_dep_env_path=%s")
541+
539542
for dep in ctx.attr.link_deps:
540543
if rust_common.dep_info in dep and dep[rust_common.dep_info].dep_env:
541544
dep_env_file = dep[rust_common.dep_info].dep_env
@@ -614,6 +617,25 @@ cargo_build_script = rule(
614617
"build_script_env": attr.string_dict(
615618
doc = "Environment variables for build scripts.",
616619
),
620+
"build_script_env_files": attr.label_list(
621+
doc = dedent("""\
622+
Files containing additional environment variables to set for rustc.
623+
624+
These files should contain a single variable per line, of format
625+
`NAME=value`, and newlines may be included in a value by ending a
626+
line with a trailing back-slash (`\\\\`).
627+
628+
The order that these files will be processed is unspecified, so
629+
multiple definitions of a particular variable are discouraged.
630+
631+
Note that the variables here are subject to
632+
[workspace status](https://docs.bazel.build/versions/main/user-manual.html#workspace_status)
633+
stamping should the `stamp` attribute be enabled. Stamp variables
634+
should be wrapped in brackets in order to be resolved. E.g.
635+
`NAME={WORKSPACE_STATUS_VARIABLE}`.
636+
"""),
637+
allow_files = True,
638+
),
617639
"crate_features": attr.string_list(
618640
doc = "The list of rust features that the build script should consider activated.",
619641
),

cargo/private/cargo_build_script_wrapper.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def cargo_build_script(
2222
link_deps = [],
2323
proc_macro_deps = [],
2424
build_script_env = {},
25+
build_script_env_files = [],
2526
use_default_shell_env = None,
2627
data = [],
2728
compile_data = [],
@@ -109,6 +110,8 @@ def cargo_build_script(
109110
links attribute and therefore provide environment variables to this build script.
110111
proc_macro_deps (list of label, optional): List of rust_proc_macro targets used to build the script.
111112
build_script_env (dict, optional): Environment variables for build scripts.
113+
build_script_env_files (list of label, optional): Files containing additional environment variables to set
114+
when running the build script.
112115
use_default_shell_env (bool, optional): Whether or not to include the default shell environment for the build script action. If unset the global
113116
setting `@rules_rust//cargo/settings:use_default_shell_env` will be used to determine this value.
114117
data (list, optional): Files needed by the build script.
@@ -209,6 +212,7 @@ def cargo_build_script(
209212
crate_features = crate_features,
210213
version = version,
211214
build_script_env = build_script_env,
215+
build_script_env_files = build_script_env_files,
212216
use_default_shell_env = sanitized_use_default_shell_env,
213217
links = links,
214218
deps = deps,

crate_universe/src/context/crate_context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ pub(crate) struct BuildScriptAttributes {
229229
#[serde(skip_serializing_if = "Select::is_empty")]
230230
pub(crate) build_script_env: Select<BTreeMap<String, String>>,
231231

232+
#[serde(skip_serializing_if = "Select::is_empty")]
233+
pub(crate) build_script_env_files: Select<BTreeSet<String>>,
234+
232235
#[serde(skip_serializing_if = "Select::is_empty")]
233236
pub(crate) rundir: Select<String>,
234237

@@ -276,6 +279,7 @@ impl Default for BuildScriptAttributes {
276279
link_deps: Default::default(),
277280
extra_link_deps: Default::default(),
278281
build_script_env: Default::default(),
282+
build_script_env_files: Default::default(),
279283
rundir: Default::default(),
280284
extra_proc_macro_deps: Default::default(),
281285
proc_macro_deps: Default::default(),

crate_universe/src/rendering.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ impl Renderer {
386386
build_script_attrs
387387
.rustc_env_files
388388
.insert(":cargo_toml_env_vars".to_owned(), None);
389+
build_script_attrs
390+
.build_script_env_files
391+
.insert(":cargo_toml_env_vars".to_owned(), None);
389392
}
390393
}
391394

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_build_script_env_file",
7+
out = "build_script_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+
build_script_env_files = [":generate_build_script_env_file"],
18+
edition = "2018",
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={}", std::env::var("GREETING").unwrap());
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)