Include git rev in the version number

This commit is contained in:
bjorn3
2021-02-16 19:01:02 +01:00
parent 720da20588
commit 0693b7dade
2 changed files with 38 additions and 2 deletions

View File

@@ -17,6 +17,7 @@
use cranelift_codegen_meta as meta; use cranelift_codegen_meta as meta;
use std::env; use std::env;
use std::io::Read;
use std::process; use std::process;
use std::time::Instant; use std::time::Instant;
@@ -97,4 +98,40 @@ fn main() {
) )
.unwrap() .unwrap()
} }
let pkg_version = env::var("CARGO_PKG_VERSION").unwrap();
let mut cmd = std::process::Command::new("git");
cmd.arg("rev-parse")
.arg("HEAD")
.stdout(std::process::Stdio::piped())
.current_dir(env::var("CARGO_MANIFEST_DIR").unwrap());
let version = if let Ok(mut child) = cmd.spawn() {
let mut git_rev = String::new();
child
.stdout
.as_mut()
.unwrap()
.read_to_string(&mut git_rev)
.unwrap();
let status = child.wait().unwrap();
if status.success() {
let git_rev = git_rev.trim().chars().take(9).collect::<String>();
format!("{}-{}", pkg_version, git_rev)
} else {
// not a git repo
pkg_version
}
} else {
// git not available
pkg_version
};
std::fs::write(
std::path::Path::new(&out_dir).join("version.rs"),
format!(
"/// Version number of this crate. \n\
pub const VERSION: &str = \"{}\";",
version
),
)
.unwrap();
} }

View File

@@ -121,5 +121,4 @@ mod souper_harvest;
pub use crate::result::{CodegenError, CodegenResult}; pub use crate::result::{CodegenError, CodegenResult};
/// Version number of this crate. include!(concat!(env!("OUT_DIR"), "/version.rs"));
pub const VERSION: &str = env!("CARGO_PKG_VERSION");