Skip to content

Commit c552243

Browse files
committed
Add an env_flags attribute to rust_library/binary rules
* Accept passing environment variables through "--env=NAME=VAL" flags * Add two rust internal tools to assist environment variable flags processing Signed-off-by: Yuxuan Dai <[email protected]>
1 parent 3c0a2ac commit c552243

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

prelude/decls/rust_common.bzl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,15 @@ def _env_arg():
112112
"""),
113113
}
114114

115+
def _env_flags_arg():
116+
return {
117+
"env_flags": attrs.list(attrs.arg(), default = [], doc = """
118+
A sequence of "--env=NAME=VAL" flags for additional environment variables for this rule's
119+
invocations of rustc. For example `env_flags = ["--env=NAME1=val1", "--env=NAME2=val2"]`.
120+
The environment variable values may include macros which are expanded.
121+
"""),
122+
}
123+
115124
def _run_env_arg():
116125
return {
117126
"run_env": attrs.dict(key = attrs.string(), value = attrs.arg(), sorted = False, default = {}, doc = """
@@ -186,6 +195,7 @@ rust_common = struct(
186195
crate_root = _crate_root,
187196
default_roots_arg = _default_roots_arg,
188197
env_arg = _env_arg,
198+
env_flags_arg = _env_flags_arg,
189199
run_env_arg = _run_env_arg,
190200
build_and_run_env_arg = _build_and_run_env_arg,
191201
mapped_srcs_arg = _mapped_srcs_arg,

prelude/decls/rust_rules.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ rust_binary = prelude_rule(
121121
rust_common.crate(crate_type = attrs.option(attrs.string(), default = None)) |
122122
rust_common.crate_root() |
123123
rust_common.env_arg() |
124+
rust_common.env_flags_arg() |
124125
_rust_binary_attrs_group(prefix = "") |
125126
_rust_common_attributes(is_binary = True) |
126127
_RUST_EXECUTABLE_ATTRIBUTES |
@@ -186,6 +187,7 @@ rust_library = prelude_rule(
186187
rust_common.linker_flags_arg() |
187188
rust_common.exported_linker_flags_arg() |
188189
rust_common.env_arg() |
190+
rust_common.env_flags_arg() |
189191
rust_common.crate(crate_type = attrs.option(attrs.string(), default = None)) |
190192
rust_common.crate_root() |
191193
native_common.preferred_linkage(preferred_linkage_type = attrs.enum(Linkage.values(), default = "any")) |

prelude/rust/build.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,8 @@ def _rustc_invoke(
15401540
compile_cmd.add(cmd_args("--env=", k, "=", v, delimiter = ""))
15411541
for k, v in path_env.items():
15421542
compile_cmd.add(cmd_args("--path-env=", k, "=", v, delimiter = ""))
1543+
for flag in ctx.attrs.env_flags:
1544+
compile_cmd.add(cmd_args(flag))
15431545

15441546
build_status = None
15451547
if infallible_diagnostics:

0 commit comments

Comments
 (0)