Skip to content

Commit a1ca705

Browse files
committed
Add support for extra rustc environment variables
This adds support for injecting environment variables into rustc invocations via build settings: - `//rust/settings:extra_rustc_env` for the target configuration - `//rust/settings:extra_exec_rustc_env` for the exec configuration These settings can be set from the command line with flags like: ``` --@rules_rust//rust/settings:extra_rustc_env=RUSTC_BOOTSTRAP=1 ``` This mirrors how extra rustc flags are handled and enables build-wide env-based feature control without editing individual rules. Useful for enabling unstable features across many crates, or toggling behavior in proc macros and build scripts.
1 parent f04b2a6 commit a1ca705

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

rust/private/rust.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,12 +543,18 @@ RUSTC_ATTRS = {
543543
"_extra_exec_rustc_flags": attr.label(
544544
default = Label("//rust/settings:extra_exec_rustc_flags"),
545545
),
546+
"_extra_exec_rustc_env": attr.label(
547+
default = Label("//rust/settings:extra_exec_rustc_env"),
548+
),
546549
"_extra_rustc_flag": attr.label(
547550
default = Label("//rust/settings:extra_rustc_flag"),
548551
),
549552
"_extra_rustc_flags": attr.label(
550553
default = Label("//rust/settings:extra_rustc_flags"),
551554
),
555+
"_extra_rustc_env": attr.label(
556+
default = Label("//rust/settings:extra_rustc_env"),
557+
),
552558
"_is_proc_macro_dep": attr.label(
553559
default = Label("//rust/private:is_proc_macro_dep"),
554560
),

rust/private/rustc.bzl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ ExtraExecRustcFlagsInfo = provider(
7777
fields = {"extra_exec_rustc_flags": "List[string] Extra flags to pass to rustc in exec configuration"},
7878
)
7979

80+
ExtraRustcEnvInfo = provider(
81+
doc = "Pass each value as an environment variable to non-exec rustc invocations",
82+
fields = {"extra_rustc_env": "List[string] Extra env to pass to rustc in non-exec configuration"},
83+
)
84+
85+
ExtraExecRustcEnvInfo = provider(
86+
doc = "Pass each value as an environment variable to exec rustc invocations",
87+
fields = {"extra_exec_rustc_env": "List[string] Extra env to pass to rustc in exec configuration"},
88+
)
89+
8090
PerCrateRustcFlagsInfo = provider(
8191
doc = "Pass each value as an additional flag to non-exec rustc invocations for crates matching the provided filter",
8292
fields = {"per_crate_rustc_flags": "List[string] Extra flags to pass to rustc in non-exec configuration"},
@@ -1111,6 +1121,9 @@ def construct_arguments(
11111121
if hasattr(ctx.attr, "_extra_rustc_flag") and not is_exec_configuration(ctx):
11121122
rustc_flags.add_all(ctx.attr._extra_rustc_flag[ExtraRustcFlagsInfo].extra_rustc_flags, map_each = map_flag)
11131123

1124+
if hasattr(ctx.attr, "_extra_rustc_env") and not is_exec_configuration(ctx):
1125+
env.update(ctx.attr._extra_rustc_env[ExtraRustcEnvInfo].extra_rustc_env)
1126+
11141127
if hasattr(ctx.attr, "_per_crate_rustc_flag") and not is_exec_configuration(ctx):
11151128
per_crate_rustc_flags = ctx.attr._per_crate_rustc_flag[PerCrateRustcFlagsInfo].per_crate_rustc_flags
11161129
_add_per_crate_rustc_flags(ctx, rustc_flags, map_flag, crate_info, per_crate_rustc_flags)
@@ -1121,6 +1134,9 @@ def construct_arguments(
11211134
if hasattr(ctx.attr, "_extra_exec_rustc_flag") and is_exec_configuration(ctx):
11221135
rustc_flags.add_all(ctx.attr._extra_exec_rustc_flag[ExtraExecRustcFlagsInfo].extra_exec_rustc_flags, map_each = map_flag)
11231136

1137+
if hasattr(ctx.attr, "_extra_exec_rustc_env") and is_exec_configuration(ctx):
1138+
env.update(ctx.attr._extra_exec_rustc_env[ExtraExecRustcEnvInfo].extra_exec_rustc_env)
1139+
11241140
if _is_no_std(ctx, toolchain, crate_info):
11251141
rustc_flags.add('--cfg=feature="no_std"')
11261142

@@ -2353,6 +2369,42 @@ extra_rustc_flag = rule(
23532369
build_setting = config.string_list(flag = True, repeatable = True),
23542370
)
23552371

2372+
def _extra_rustc_env_impl(ctx):
2373+
env_vars = {}
2374+
for entry in ctx.build_setting_value:
2375+
if "=" not in entry:
2376+
fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry))
2377+
key, val = entry.split("=", 1)
2378+
env_vars[key] = val
2379+
return ExtraRustcEnvInfo(extra_rustc_env = env_vars)
2380+
2381+
extra_rustc_env = rule(
2382+
doc = (
2383+
"Add additional environment variables to rustc in non-exec configuration using " +
2384+
"`--@rules_rust//rust/settings:extra_rustc_env=FOO=bar`. Multiple values may be specified."
2385+
),
2386+
implementation = _extra_rustc_env_impl,
2387+
build_setting = config.string_list(flag = True),
2388+
)
2389+
2390+
def _extra_exec_rustc_env_impl(ctx):
2391+
env_vars = {}
2392+
for entry in ctx.build_setting_value:
2393+
if "=" not in entry:
2394+
fail("Invalid format for rustc env var: '{}'. Expected 'KEY=value'".format(entry))
2395+
key, val = entry.split("=", 1)
2396+
env_vars[key] = val
2397+
return ExtraExecRustcEnvInfo(extra_exec_rustc_env = env_vars)
2398+
2399+
extra_exec_rustc_env = rule(
2400+
doc = (
2401+
"Add additional environment variables to rustc in non-exec configuration using " +
2402+
"`--@rules_rust//rust/settings:extra_exec_rustc_env=FOO=bar`. Multiple values may be specified."
2403+
),
2404+
implementation = _extra_exec_rustc_env_impl,
2405+
build_setting = config.string_list(flag = True),
2406+
)
2407+
23562408
def _extra_exec_rustc_flags_impl(ctx):
23572409
return ExtraExecRustcFlagsInfo(extra_exec_rustc_flags = ctx.build_setting_value)
23582410

rust/settings/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ load(
1717
"experimental_use_sh_toolchain_for_bootstrap_process_wrapper",
1818
"extra_exec_rustc_flag",
1919
"extra_exec_rustc_flags",
20+
"extra_exec_rustc_env",
2021
"extra_rustc_flag",
2122
"extra_rustc_flags",
23+
"extra_rustc_env",
2224
"incompatible_change_clippy_error_format",
2325
"incompatible_change_rust_test_compilation_output_directory",
2426
"incompatible_do_not_include_data_in_compile_data",
@@ -86,10 +88,14 @@ extra_exec_rustc_flag()
8688

8789
extra_exec_rustc_flags()
8890

91+
extra_exec_rustc_env()
92+
8993
extra_rustc_flag()
9094

9195
extra_rustc_flags()
9296

97+
extra_rustc_env()
98+
9399
incompatible_change_clippy_error_format()
94100

95101
incompatible_change_rust_test_compilation_output_directory()

rust/settings/settings.bzl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ load(
2121
_error_format = "error_format",
2222
_extra_exec_rustc_flag = "extra_exec_rustc_flag",
2323
_extra_exec_rustc_flags = "extra_exec_rustc_flags",
24+
_extra_exec_rustc_env = "extra_exec_rustc_env",
2425
_extra_rustc_flag = "extra_rustc_flag",
2526
_extra_rustc_flags = "extra_rustc_flags",
27+
_extra_rustc_env = "extra_rustc_env",
2628
_no_std = "no_std",
2729
_per_crate_rustc_flag = "per_crate_rustc_flag",
2830
_rustc_output_diagnostics = "rustc_output_diagnostics",
@@ -399,6 +401,30 @@ def extra_exec_rustc_flag():
399401
build_setting_default = [],
400402
)
401403

404+
# buildifier: disable=unnamed-macro
405+
def extra_rustc_env():
406+
"""This setting may be used to pass extra environment variables to rustc from the command line in non-exec configuration.
407+
408+
It applies across all targets whereas environment variables set in a specific rule apply only to that target.
409+
This can be useful for setting build-wide env flags such as `RUSTC_BOOTSTRAP=1`.
410+
"""
411+
_extra_rustc_env(
412+
name = "extra_rustc_env",
413+
build_setting_default = [],
414+
)
415+
416+
# buildifier: disable=unnamed-macro
417+
def extra_exec_rustc_env():
418+
"""This setting may be used to pass extra environment variables to rustc from the command line in exec configuration.
419+
420+
It applies to tools built and run during the build process, such as proc-macros and build scripts.
421+
This can be useful for enabling features that are needed during tool compilation.
422+
"""
423+
_extra_exec_rustc_env(
424+
name = "extra_exec_rustc_env",
425+
build_setting_default = [],
426+
)
427+
402428
# buildifier: disable=unnamed-macro
403429
def experimental_per_crate_rustc_flag():
404430
"""Add additional rustc_flag to matching crates from the command line with `--@rules_rust//rust/settings:experimental_per_crate_rustc_flag`.

0 commit comments

Comments
 (0)