rust/crates/rust-analyzer/build.rs

46 lines
1.5 KiB
Rust
Raw Normal View History

2019-12-09 14:59:04 +00:00
//! Just embed git-hash to `--version`
2020-02-21 02:04:03 +00:00
use std::{env, path::PathBuf, process::Command};
2019-12-09 14:59:04 +00:00
fn main() {
2020-02-20 08:47:12 +00:00
set_rerun();
2020-02-21 02:04:03 +00:00
let rev = env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string());
2019-12-09 14:59:04 +00:00
println!("cargo:rustc-env=REV={}", rev)
}
2020-02-21 02:04:03 +00:00
fn set_rerun() {
println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
2020-02-21 02:04:03 +00:00
let mut manifest_dir = PathBuf::from(
env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
);
while manifest_dir.parent().is_some() {
if manifest_dir.join(".git/HEAD").exists() {
let git_dir = manifest_dir.join(".git");
println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display());
// current branch ref
if let Ok(output) =
Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output()
{
if let Ok(ref_link) = String::from_utf8(output.stdout) {
println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display());
}
}
return;
}
manifest_dir.pop();
}
println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
}
2019-12-09 14:59:04 +00:00
fn rev() -> Option<String> {
let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().ok()?;
let stdout = String::from_utf8(output.stdout).ok()?;
let short_hash = stdout.get(0..7)?;
Some(short_hash.to_owned())
}