rust/crates/rust-analyzer/build.rs

46 lines
1.4 KiB
Rust
Raw Normal View History

2019-12-09 08:59:04 -06:00
//! Just embed git-hash to `--version`
2020-02-20 20:04:03 -06:00
use std::{env, path::PathBuf, process::Command};
2019-12-09 08:59:04 -06:00
fn main() {
2020-02-20 02:47:12 -06:00
set_rerun();
2020-02-20 20:04:03 -06:00
2020-06-18 13:25:07 -05:00
let rev =
env::var("RUST_ANALYZER_REV").ok().or_else(rev).unwrap_or_else(|| "???????".to_string());
2019-12-09 08:59:04 -06:00
println!("cargo:rustc-env=REV={}", rev)
}
2020-02-20 20:04:03 -06:00
fn set_rerun() {
println!("cargo:rerun-if-env-changed=RUST_ANALYZER_REV");
2020-02-20 20:04:03 -06: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 08:59:04 -06:00
fn rev() -> Option<String> {
2021-03-12 11:49:00 -06:00
let output = Command::new("git").args(&["describe", "--tags"]).output().ok()?;
2019-12-09 08:59:04 -06:00
let stdout = String::from_utf8(output.stdout).ok()?;
2021-03-12 11:49:00 -06:00
Some(stdout)
2019-12-09 08:59:04 -06:00
}