use std::env;
fn main() {
const ENV_KEY: &str = "LAMBERT_W_ENSURE_NO_PANICS";
const ENV_VAL: &str = "1";
const NEEDED_PROFILE: &str = "release-lto";
println!("cargo:rerun-if-env-changed={ENV_KEY}");
println!("cargo:rustc-check-cfg=cfg(assert_no_panic)");
let env_val = env::var(ENV_KEY);
if env_val.as_ref().map(String::as_str) == Ok(ENV_VAL) {
println!("cargo:rustc-cfg=assert_no_panic");
let suggestion = format!(
"The `{NEEDED_PROFILE}` build profile must be enabled to ensure no false positives."
);
match parse_build_profile_name_from_environment() {
Ok(Some(profile_name)) => {
if profile_name != NEEDED_PROFILE {
println!("cargo:warning=the `{ENV_KEY}` environment variable is set to \"{ENV_VAL}\", but the `{profile_name}` build profile seems to be enabled. {suggestion}");
}
}
Ok(None) => {
println!("cargo:warning=the `{ENV_KEY}` environment variable is set to \"{ENV_VAL}\", but the build profile could not be determined. {suggestion}");
}
Err(e) => {
println!("cargo:warning=the `{ENV_KEY}` environment variable is set to \"{ENV_VAL}\", but the `OUT_DIR` environment variable could not be read due to: {e}. The build profile could therefore not be determined. {suggestion}");
}
}
} else if let Ok(unexpected_env_val) = env_val {
println!(
"cargo:warning=the `{ENV_KEY}` environment variable is set to \"{unexpected_env_val}\", but it must be set to \"{ENV_VAL}\" to enable the check for panics."
);
}
}
fn parse_build_profile_name_from_environment() -> Result<Option<String>, env::VarError> {
env::var("OUT_DIR").map(|env_var_val| {
env_var_val
.split(std::path::MAIN_SEPARATOR)
.nth_back(3)
.map(std::borrow::ToOwned::to_owned)
})
}