@@ -77,6 +77,16 @@ ExtraExecRustcFlagsInfo = provider(
77
77
fields = {"extra_exec_rustc_flags" : "List[string] Extra flags to pass to rustc in exec configuration" },
78
78
)
79
79
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
+
80
90
PerCrateRustcFlagsInfo = provider (
81
91
doc = "Pass each value as an additional flag to non-exec rustc invocations for crates matching the provided filter" ,
82
92
fields = {"per_crate_rustc_flags" : "List[string] Extra flags to pass to rustc in non-exec configuration" },
@@ -1111,6 +1121,9 @@ def construct_arguments(
1111
1121
if hasattr (ctx .attr , "_extra_rustc_flag" ) and not is_exec_configuration (ctx ):
1112
1122
rustc_flags .add_all (ctx .attr ._extra_rustc_flag [ExtraRustcFlagsInfo ].extra_rustc_flags , map_each = map_flag )
1113
1123
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
+
1114
1127
if hasattr (ctx .attr , "_per_crate_rustc_flag" ) and not is_exec_configuration (ctx ):
1115
1128
per_crate_rustc_flags = ctx .attr ._per_crate_rustc_flag [PerCrateRustcFlagsInfo ].per_crate_rustc_flags
1116
1129
_add_per_crate_rustc_flags (ctx , rustc_flags , map_flag , crate_info , per_crate_rustc_flags )
@@ -1121,6 +1134,9 @@ def construct_arguments(
1121
1134
if hasattr (ctx .attr , "_extra_exec_rustc_flag" ) and is_exec_configuration (ctx ):
1122
1135
rustc_flags .add_all (ctx .attr ._extra_exec_rustc_flag [ExtraExecRustcFlagsInfo ].extra_exec_rustc_flags , map_each = map_flag )
1123
1136
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
+
1124
1140
if _is_no_std (ctx , toolchain , crate_info ):
1125
1141
rustc_flags .add ('--cfg=feature="no_std"' )
1126
1142
@@ -2353,6 +2369,42 @@ extra_rustc_flag = rule(
2353
2369
build_setting = config .string_list (flag = True , repeatable = True ),
2354
2370
)
2355
2371
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
+
2356
2408
def _extra_exec_rustc_flags_impl (ctx ):
2357
2409
return ExtraExecRustcFlagsInfo (extra_exec_rustc_flags = ctx .build_setting_value )
2358
2410
0 commit comments