2022-11-16 20:34:16 +00:00
|
|
|
// The compiler code necessary to support the env! extension. Eventually this
|
2016-06-06 14:52:48 +00:00
|
|
|
// should all get sucked into either the compiler syntax extension plugin
|
|
|
|
// interface.
|
|
|
|
//
|
2012-12-23 22:41:37 +00:00
|
|
|
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast::{self as ast, GenericArg};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::{self, *};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2015-01-27 20:20:58 +00:00
|
|
|
use std::env;
|
2022-11-23 00:55:16 +00:00
|
|
|
use thin_vec::thin_vec;
|
2013-05-25 02:35:29 +00:00
|
|
|
|
2019-02-04 12:49:54 +00:00
|
|
|
pub fn expand_option_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 14:52:48 +00:00
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream,
|
2018-07-12 09:58:16 +00:00
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some(var) = get_single_str_from_tts(cx, sp, tts, "option_env!") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-08-15 06:06:33 +00:00
|
|
|
|
2019-09-14 20:17:11 +00:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2022-04-05 12:52:53 +00:00
|
|
|
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
|
|
|
|
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
|
2020-05-03 17:47:51 +00:00
|
|
|
let e = match value {
|
|
|
|
None => {
|
2019-09-14 20:16:51 +00:00
|
|
|
let lt = cx.lifetime(sp, Ident::new(kw::StaticLifetime, sp));
|
2016-06-06 14:52:48 +00:00
|
|
|
cx.expr_path(cx.path_all(
|
|
|
|
sp,
|
|
|
|
true,
|
2019-05-22 04:41:15 +00:00
|
|
|
cx.std_path(&[sym::option, sym::Option, sym::None]),
|
2022-12-28 17:06:11 +00:00
|
|
|
vec![GenericArg::Type(cx.ty_ref(
|
2019-05-17 08:37:53 +00:00
|
|
|
sp,
|
2019-09-14 20:16:51 +00:00
|
|
|
cx.ty_ident(sp, Ident::new(sym::str, sp)),
|
2018-03-08 11:27:23 +00:00
|
|
|
Some(lt),
|
2019-12-16 16:28:40 +00:00
|
|
|
ast::Mutability::Not,
|
|
|
|
))],
|
2019-09-21 19:01:10 +00:00
|
|
|
))
|
2016-06-06 14:52:48 +00:00
|
|
|
}
|
2020-05-03 17:47:51 +00:00
|
|
|
Some(value) => cx.expr_call_global(
|
2019-12-22 22:42:04 +00:00
|
|
|
sp,
|
2019-05-22 04:41:15 +00:00
|
|
|
cx.std_path(&[sym::option, sym::Option, sym::Some]),
|
2022-11-23 00:55:16 +00:00
|
|
|
thin_vec![cx.expr_str(sp, value)],
|
2019-12-22 22:42:04 +00:00
|
|
|
),
|
2013-08-15 06:06:33 +00:00
|
|
|
};
|
2015-02-27 19:14:42 +00:00
|
|
|
MacEager::expr(e)
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2012-05-14 22:32:32 +00:00
|
|
|
|
2019-02-04 12:49:54 +00:00
|
|
|
pub fn expand_env<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 14:52:48 +00:00
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream,
|
2018-07-12 09:58:16 +00:00
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2022-11-15 13:24:33 +00:00
|
|
|
let mut exprs = match get_exprs_from_tts(cx, tts) {
|
2023-02-28 16:20:43 +00:00
|
|
|
Some(exprs) if exprs.is_empty() || exprs.len() > 2 => {
|
2014-01-17 14:53:10 +00:00
|
|
|
cx.span_err(sp, "env! takes 1 or 2 arguments");
|
2019-08-13 17:51:54 +00:00
|
|
|
return DummyResult::any(sp);
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
2019-08-13 17:51:54 +00:00
|
|
|
None => return DummyResult::any(sp),
|
2016-06-06 14:52:48 +00:00
|
|
|
Some(exprs) => exprs.into_iter(),
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2013-08-07 04:50:23 +00:00
|
|
|
|
2022-02-18 23:48:49 +00:00
|
|
|
let Some((var, _style)) = expr_to_string(cx, exprs.next().unwrap(), "expected string literal") else {
|
|
|
|
return DummyResult::any(sp);
|
2014-01-17 14:53:10 +00:00
|
|
|
};
|
2023-02-28 16:20:43 +00:00
|
|
|
|
|
|
|
let custom_msg = match exprs.next() {
|
|
|
|
None => None,
|
2014-09-13 16:06:01 +00:00
|
|
|
Some(second) => match expr_to_string(cx, second, "expected string literal") {
|
2019-08-13 17:51:54 +00:00
|
|
|
None => return DummyResult::any(sp),
|
2023-02-28 16:20:43 +00:00
|
|
|
Some((s, _style)) => Some(s),
|
2014-01-17 14:53:10 +00:00
|
|
|
},
|
2014-09-13 16:06:01 +00:00
|
|
|
};
|
|
|
|
|
2020-05-26 23:45:30 +00:00
|
|
|
let sp = cx.with_def_site_ctxt(sp);
|
2021-12-15 03:39:23 +00:00
|
|
|
let value = env::var(var.as_str()).ok().as_deref().map(Symbol::intern);
|
2020-07-30 01:27:50 +00:00
|
|
|
cx.sess.parse_sess.env_depinfo.borrow_mut().insert((var, value));
|
2020-05-03 17:47:51 +00:00
|
|
|
let e = match value {
|
|
|
|
None => {
|
2023-02-28 16:20:43 +00:00
|
|
|
let (msg, help) = match custom_msg {
|
|
|
|
None => (
|
|
|
|
format!("environment variable `{var}` not defined at compile time"),
|
|
|
|
Some(help_for_missing_env_var(var.as_str())),
|
|
|
|
),
|
|
|
|
Some(s) => (s.to_string(), None),
|
|
|
|
};
|
|
|
|
let mut diag = cx.struct_span_err(sp, &msg);
|
|
|
|
if let Some(help) = help {
|
|
|
|
diag.help(help);
|
|
|
|
}
|
|
|
|
diag.emit();
|
2019-08-13 17:51:54 +00:00
|
|
|
return DummyResult::any(sp);
|
2014-01-17 14:53:10 +00:00
|
|
|
}
|
2020-05-03 17:47:51 +00:00
|
|
|
Some(value) => cx.expr_str(sp, value),
|
2012-12-13 01:08:09 +00:00
|
|
|
};
|
2015-02-27 19:14:42 +00:00
|
|
|
MacEager::expr(e)
|
2011-05-05 02:05:16 +00:00
|
|
|
}
|
2023-02-28 16:20:43 +00:00
|
|
|
|
|
|
|
fn help_for_missing_env_var(var: &str) -> String {
|
|
|
|
if var.starts_with("CARGO_")
|
|
|
|
|| var.starts_with("DEP_")
|
|
|
|
|| matches!(var, "OUT_DIR" | "OPT_LEVEL" | "PROFILE" | "HOST" | "TARGET")
|
|
|
|
{
|
|
|
|
format!(
|
|
|
|
"Cargo sets build script variables at run time. Use `std::env::var(\"{var}\")` instead"
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
format!("Use `std::env::var(\"{var}\")` to read the variable at run time")
|
|
|
|
}
|
|
|
|
}
|