about summary refs log tree commit diff
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2024-08-27 14:46:24 +1000
committerZalathar <Zalathar@users.noreply.github.com>2024-08-27 17:29:47 +1000
commitfcce75e9a666c60d106a04cbffc365c01364f1fd (patch)
treee925e07096d1b950b36ba07f4afc71a885541dda
parent2c141a454249ea0edbab2f4697eb634063e8b032 (diff)
downloadrust-fcce75e9a666c60d106a04cbffc365c01364f1fd.tar.gz
rust-fcce75e9a666c60d106a04cbffc365c01364f1fd.zip
Use helper functions to read environment variables
This also migrates from legacy `cargo:` directives to the newer `cargo::`
prefix.
-rw-r--r--library/profiler_builtins/build.rs26
1 files changed, 17 insertions, 9 deletions
diff --git a/library/profiler_builtins/build.rs b/library/profiler_builtins/build.rs
index b57f1187f83..b9aaf512d5a 100644
--- a/library/profiler_builtins/build.rs
+++ b/library/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<String, env::VarError> {
+    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()
+    })
+}