From 096d585100636bc2e9f09d7eefec38c5b334d47b Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Fri, 30 Aug 2024 11:44:59 +0200 Subject: [PATCH] Use runtime env in build script While build.rs runtime and compile-time environment coincide when using cargo, they may not when using other build systems (notably bazel, which is therefore unable to build a project depending on this crate). As explained in [the docs][], build scripts should get their inputs from the environment at runtime, not during compilation. [the docs]:https://doc.rust-lang.org/cargo/reference/build-scripts.html#inputs-to-the-build-script --- build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.rs b/build.rs index 920b3b4..2fd7fa8 100644 --- a/build.rs +++ b/build.rs @@ -36,7 +36,7 @@ fn main() { }); let expected_version_metadata = format!("+llvm-{}", &llvm_commit_hash[..12]); - let actual_version = env!("CARGO_PKG_VERSION"); + let actual_version = std::env::var("CARGO_PKG_VERSION").expect("no CARGO_PKG_VERSION in env"); if !actual_version.ends_with(&expected_version_metadata) { eprintln!("\nexpected version ending in `{expected_version_metadata}`, found `{actual_version}`\n"); panic!("failed to validate Cargo package version (see above)");