Add macro to get rustc version. (#62)

Add macro to query git-describe. (#62)

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk
2025-06-12 21:32:35 +00:00
parent 5799059196
commit b0b441d534
4 changed files with 109 additions and 0 deletions

64
src/macros/git.rs Normal file
View File

@@ -0,0 +1,64 @@
use std::process::Command;
use proc_macro::TokenStream;
use quote::quote;
pub(super) fn semantic(_args: TokenStream) -> TokenStream {
static ARGS: &[&str] = &["describe", "--tags", "--abbrev=1"];
let output = git(ARGS);
let output = output
.strip_prefix('v')
.map(str::to_string)
.unwrap_or(output);
let output = output
.rsplit_once('-')
.map(|(s, _)| s)
.map(str::to_string)
.unwrap_or(output);
let ret = quote! {
static GIT_SEMANTIC: &'static str = #output;
};
ret.into()
}
pub(super) fn commit(_args: TokenStream) -> TokenStream {
static ARGS: &[&str] = &["describe", "--always", "--dirty", "--abbrev=10"];
let output = git(ARGS);
let ret = quote! {
static GIT_COMMIT: &'static str = #output;
};
ret.into()
}
pub(super) fn describe(_args: TokenStream) -> TokenStream {
static ARGS: &[&str] =
&["describe", "--dirty", "--tags", "--always", "--broken", "--abbrev=10"];
let output = git(ARGS);
let ret = quote! {
static GIT_DESCRIBE: &'static str = #output;
};
ret.into()
}
fn git(args: &[&str]) -> String {
Command::new("git")
.args(args)
.output()
.map(|output| {
str::from_utf8(&output.stdout)
.map(str::trim)
.map(String::from)
.ok()
})
.ok()
.flatten()
.unwrap_or_default()
}