Skip to content

Commit 8176ab9

Browse files
Implement --env compiler flag
1 parent cc11307 commit 8176ab9

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

compiler/rustc_builtin_macros/src/env.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ use thin_vec::thin_vec;
1313

1414
use crate::errors;
1515

16+
fn lookup_env<'cx>(cx: &'cx ExtCtxt<'_>, var: Symbol) -> Option<Symbol> {
17+
let var = var.as_str();
18+
if let Some(value) = cx.sess.opts.logical_env.get(var) {
19+
return Some(Symbol::intern(value));
20+
}
21+
// If the environment variable was not defined with the `--env` option, we try to retrieve it
22+
// from rustc's environment.
23+
env::var(var).ok().as_deref().map(Symbol::intern)
24+
}
25+
1626
pub fn expand_option_env<'cx>(
1727
cx: &'cx mut ExtCtxt<'_>,
1828
sp: Span,
@@ -23,7 +33,7 @@ pub fn expand_option_env<'cx>(
2333
};
2434

2535
let sp = cx.with_def_site_ctxt(sp);
26-
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
36+
let value = lookup_env(cx, var);
2737
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
2838
let e = match value {
2939
None => {
@@ -77,7 +87,7 @@ pub fn expand_env<'cx>(
7787
};
7888

7989
let span = cx.with_def_site_ctxt(sp);
80-
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
90+
let value = lookup_env(cx, var);
8191
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
8292
let e = match value {
8393
None => {

compiler/rustc_session/src/config.rs

+22
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ impl Default for Options {
11151115
pretty: None,
11161116
working_dir: RealFileName::LocalPath(std::env::current_dir().unwrap()),
11171117
color: ColorConfig::Auto,
1118+
logical_env: FxHashMap::default(),
11181119
}
11191120
}
11201121
}
@@ -1812,6 +1813,7 @@ pub fn rustc_optgroups() -> Vec<RustcOptGroup> {
18121813
"Remap source names in all output (compiler messages and output files)",
18131814
"FROM=TO",
18141815
),
1816+
opt::multi("", "env", "Inject an environment variable", "VAR=VALUE"),
18151817
]);
18161818
opts
18171819
}
@@ -2595,6 +2597,23 @@ fn parse_remap_path_prefix(
25952597
mapping
25962598
}
25972599

2600+
fn parse_logical_env(
2601+
handler: &mut EarlyErrorHandler,
2602+
matches: &getopts::Matches,
2603+
) -> FxHashMap<String, String> {
2604+
let mut vars = FxHashMap::default();
2605+
2606+
for arg in matches.opt_strs("env") {
2607+
if let Some((name, val)) = arg.split_once('=') {
2608+
vars.insert(name.to_string(), val.to_string());
2609+
} else {
2610+
handler.early_error(format!("`--env`: specify value for variable `{arg}`"));
2611+
}
2612+
}
2613+
2614+
vars
2615+
}
2616+
25982617
// JUSTIFICATION: before wrapper fn is available
25992618
#[allow(rustc::bad_opt_access)]
26002619
pub fn build_session_options(
@@ -2824,6 +2843,8 @@ pub fn build_session_options(
28242843
handler.early_error("can't dump dependency graph without `-Z query-dep-graph`");
28252844
}
28262845

2846+
let logical_env = parse_logical_env(handler, matches);
2847+
28272848
// Try to find a directory containing the Rust `src`, for more details see
28282849
// the doc comment on the `real_rust_source_base_dir` field.
28292850
let tmp_buf;
@@ -2904,6 +2925,7 @@ pub fn build_session_options(
29042925
pretty,
29052926
working_dir,
29062927
color,
2928+
logical_env,
29072929
}
29082930
}
29092931

compiler/rustc_session/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::config::*;
33
use crate::search_paths::SearchPath;
44
use crate::utils::NativeLib;
55
use crate::{lint, EarlyErrorHandler};
6+
use rustc_data_structures::fx::FxHashMap;
67
use rustc_data_structures::profiling::TimePassesFormat;
78
use rustc_data_structures::stable_hasher::Hash64;
89
use rustc_errors::ColorConfig;
@@ -150,6 +151,9 @@ top_level_options!(
150151

151152
target_triple: TargetTriple [TRACKED],
152153

154+
/// Effective logical environment used by `env!`/`option_env!` macros
155+
logical_env: FxHashMap<String, String> [UNTRACKED],
156+
153157
test: bool [TRACKED],
154158
error_format: ErrorOutputType [UNTRACKED],
155159
diagnostic_width: Option<usize> [UNTRACKED],

0 commit comments

Comments
 (0)