From 0e0134f5d4b88473b0a87d4847f22a2b07263e3d Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 27 Aug 2024 14:46:24 +1000 Subject: [PATCH] Use helper functions to read environment variables This also migrates from legacy `cargo:` directives to the newer `cargo::` prefix. --- profiler_builtins/build.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/profiler_builtins/build.rs b/profiler_builtins/build.rs index b57f1187f8356..b9aaf512d5a17 100644 --- a/profiler_builtins/build.rs +++ b/profiler_builtins/build.rs @@ -8,9 +8,8 @@ use std::env; use std::path::PathBuf; fn main() { - println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB"); - if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") { - println!("cargo:rustc-link-lib=static:+verbatim={rt}"); + if let Ok(rt) = tracked_env_var("LLVM_PROFILER_RT_LIB") { + println!("cargo::rustc-link-lib=static:+verbatim={rt}"); return; } @@ -82,12 +81,10 @@ fn main() { } // Get the LLVM `compiler-rt` directory from bootstrap. - println!("cargo:rerun-if-env-changed=RUST_COMPILER_RT_FOR_PROFILER"); - let root = PathBuf::from(env::var("RUST_COMPILER_RT_FOR_PROFILER").unwrap_or_else(|_| { - let path = "../../src/llvm-project/compiler-rt"; - println!("RUST_COMPILER_RT_FOR_PROFILER was not set; falling back to {path:?}"); - path.to_owned() - })); + let root = PathBuf::from(tracked_env_var_or_fallback( + "RUST_COMPILER_RT_FOR_PROFILER", + "../../src/llvm-project/compiler-rt", + )); let src_root = root.join("lib").join("profile"); assert!(src_root.exists(), "profiler runtime source directory not found: {src_root:?}"); @@ -105,3 +102,14 @@ fn main() { cfg.warnings(false); cfg.compile("profiler-rt"); } + +fn tracked_env_var(key: &str) -> Result { + println!("cargo::rerun-if-env-changed={key}"); + env::var(key) +} +fn tracked_env_var_or_fallback(key: &str, fallback: &str) -> String { + tracked_env_var(key).unwrap_or_else(|_| { + println!("cargo::warning={key} was not set; falling back to {fallback:?}"); + fallback.to_owned() + }) +}