Skip to content

Commit 356cea5

Browse files
committed
Extract parse_env_strings
1 parent 87ec024 commit 356cea5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

rust/private/rustc.bzl

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ load(
4141
"is_exec_configuration",
4242
"is_std_dylib",
4343
"make_static_lib_symlink",
44+
"parse_env_strings",
4445
"relativize",
4546
)
4647

@@ -2344,12 +2345,7 @@ rustc_output_diagnostics = rule(
23442345
)
23452346

23462347
def _extra_rustc_env_impl(ctx):
2347-
env_vars = {}
2348-
for entry in ctx.build_setting_value:
2349-
if "=" not in entry:
2350-
fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry))
2351-
key, val = entry.split("=", 1)
2352-
env_vars[key] = val
2348+
env_vars = parse_env_strings(ctx.build_setting_value)
23532349
return ExtraRustcEnvInfo(extra_rustc_env = env_vars)
23542350

23552351
extra_rustc_env = rule(
@@ -2402,12 +2398,7 @@ extra_exec_rustc_flags = rule(
24022398
)
24032399

24042400
def _extra_exec_rustc_env_impl(ctx):
2405-
env_vars = {}
2406-
for entry in ctx.build_setting_value:
2407-
if "=" not in entry:
2408-
fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry))
2409-
key, val = entry.split("=", 1)
2410-
env_vars[key] = val
2401+
env_vars = parse_env_strings(ctx.build_setting_value)
24112402
return ExtraExecRustcEnvInfo(extra_exec_rustc_env = env_vars)
24122403

24132404
extra_exec_rustc_env = rule(

rust/private/utils.bzl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@ UNSUPPORTED_FEATURES = [
3333
"rules_rust_unsupported_feature",
3434
]
3535

36+
def parse_env_strings(entries):
37+
"""Parses a list of environment variable entries in the form 'KEY=value'.
38+
39+
Args:
40+
entries(list): A list of strings, each of the form 'KEY=value'.
41+
42+
Returns:
43+
A dict mapping environment variable names to their values.
44+
"""
45+
env_vars = {}
46+
for entry in entries:
47+
if "=" not in entry:
48+
fail("Invalid format for env var: '{}'. Expected 'KEY=value'".format(entry))
49+
key, val = entry.split("=", 1)
50+
env_vars[key] = val
51+
return env_vars
52+
3653
def find_toolchain(ctx):
3754
"""Finds the first rust toolchain that is configured.
3855

0 commit comments

Comments
 (0)