Skip to content

Commit 0e0134f

Browse files
committed
Use helper functions to read environment variables
This also migrates from legacy `cargo:` directives to the newer `cargo::` prefix.
1 parent 6d3344f commit 0e0134f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

Diff for: profiler_builtins/build.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use std::env;
88
use std::path::PathBuf;
99

1010
fn main() {
11-
println!("cargo:rerun-if-env-changed=LLVM_PROFILER_RT_LIB");
12-
if let Ok(rt) = env::var("LLVM_PROFILER_RT_LIB") {
13-
println!("cargo:rustc-link-lib=static:+verbatim={rt}");
11+
if let Ok(rt) = tracked_env_var("LLVM_PROFILER_RT_LIB") {
12+
println!("cargo::rustc-link-lib=static:+verbatim={rt}");
1413
return;
1514
}
1615

@@ -82,12 +81,10 @@ fn main() {
8281
}
8382

8483
// Get the LLVM `compiler-rt` directory from bootstrap.
85-
println!("cargo:rerun-if-env-changed=RUST_COMPILER_RT_FOR_PROFILER");
86-
let root = PathBuf::from(env::var("RUST_COMPILER_RT_FOR_PROFILER").unwrap_or_else(|_| {
87-
let path = "../../src/llvm-project/compiler-rt";
88-
println!("RUST_COMPILER_RT_FOR_PROFILER was not set; falling back to {path:?}");
89-
path.to_owned()
90-
}));
84+
let root = PathBuf::from(tracked_env_var_or_fallback(
85+
"RUST_COMPILER_RT_FOR_PROFILER",
86+
"../../src/llvm-project/compiler-rt",
87+
));
9188

9289
let src_root = root.join("lib").join("profile");
9390
assert!(src_root.exists(), "profiler runtime source directory not found: {src_root:?}");
@@ -105,3 +102,14 @@ fn main() {
105102
cfg.warnings(false);
106103
cfg.compile("profiler-rt");
107104
}
105+
106+
fn tracked_env_var(key: &str) -> Result<String, env::VarError> {
107+
println!("cargo::rerun-if-env-changed={key}");
108+
env::var(key)
109+
}
110+
fn tracked_env_var_or_fallback(key: &str, fallback: &str) -> String {
111+
tracked_env_var(key).unwrap_or_else(|_| {
112+
println!("cargo::warning={key} was not set; falling back to {fallback:?}");
113+
fallback.to_owned()
114+
})
115+
}

0 commit comments

Comments
 (0)