Files
tuwunel/src/macros/rustc.rs
Jason Volk b0b441d534 Add macro to get rustc version. (#62)
Add macro to query git-describe. (#62)

Signed-off-by: Jason Volk <jason@zemos.net>
2025-06-13 01:27:28 +00:00

61 lines
1.3 KiB
Rust

use std::process::Command;
use proc_macro::TokenStream;
use quote::quote;
use crate::utils::get_crate_name;
pub(super) fn flags_capture(args: TokenStream) -> TokenStream {
let Some(crate_name) = get_crate_name() else {
return args;
};
let flag = std::env::args().collect::<Vec<_>>();
let flag_len = flag.len();
let ret = quote! {
pub static RUSTC_FLAGS: [&str; #flag_len] = [#( #flag ),*];
#[tuwunel_core::ctor]
fn _set_rustc_flags() {
tuwunel_core::info::rustc::FLAGS.lock().expect("locked").insert(#crate_name, &RUSTC_FLAGS);
}
// static strings have to be yanked on module unload
#[tuwunel_core::dtor]
fn _unset_rustc_flags() {
tuwunel_core::info::rustc::FLAGS.lock().expect("locked").remove(#crate_name);
}
};
ret.into()
}
pub(super) fn version(args: TokenStream) -> TokenStream {
let Some(_) = get_crate_name() else {
return args;
};
let rustc_path = std::env::args()
.next()
.expect("Path to rustc executable");
let version = Command::new(rustc_path)
.args(["-V"])
.output()
.map(|output| {
str::from_utf8(&output.stdout)
.map(str::trim)
.map(String::from)
.ok()
})
.ok()
.flatten()
.unwrap_or_default();
let ret = quote! {
static RUSTC_VERSION: &'static str = #version;
};
ret.into()
}