From 2fde3d8150795792c8e04ec67b5962c2a50aaeb6 Mon Sep 17 00:00:00 2001 From: Torrekie Gen <31795847+Torrekie@users.noreply.github.com> Date: Sun, 14 Jul 2024 15:23:49 +0800 Subject: [PATCH] allow per-target env detection (#91) Signed-off-by: Torrekie <31795847+Torrekie@users.noreply.github.com> --- jemalloc-sys/build.rs | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/jemalloc-sys/build.rs b/jemalloc-sys/build.rs index b106589..b91d3e3 100644 --- a/jemalloc-sys/build.rs +++ b/jemalloc-sys/build.rs @@ -28,14 +28,28 @@ macro_rules! warning { } } +fn read_and_watch_env_impl(name: &str, env_getter: F) -> Option +where + F: Fn(&str) -> Option, +{ + let prefix = env::var("TARGET").unwrap().to_uppercase().replace('-', "_"); + let prefixed_name = format!("{}_{}", prefix, name); + + println!("cargo:rerun-if-env-changed={}", prefixed_name); + if let Some(value) = env_getter(&prefixed_name) { + return Some(value); + } + + println!("cargo:rerun-if-env-changed={}", name); + env_getter(name) +} + fn read_and_watch_env(name: &str) -> Result { - println!("cargo:rerun-if-env-changed={name}"); - env::var(name) + read_and_watch_env_impl(name, |n| env::var(n).ok()).ok_or(env::VarError::NotPresent) } fn read_and_watch_env_os(name: &str) -> Option { - println!("cargo:rerun-if-env-changed={name}"); - env::var_os(name) + read_and_watch_env_impl(name, |n| env::var_os(n)) } fn copy_recursively(src: &Path, dst: &Path) -> io::Result<()> {