1#[macro_export]
4macro_rules! crate_version {
26 (VERSION) => {{
27 use std::{env, fs::File, io::Write, path::Path, process::Command};
28
29 let main_version = env!("CARGO_PKG_VERSION");
30
31 let branch = Command::new("git")
32 .args(["branch", "--show-current"])
33 .output()
34 .map(|o| String::from_utf8(o.stdout).unwrap())
35 .unwrap();
36
37 let commit = Command::new("git")
38 .args(["describe", "--always"])
39 .output()
40 .map(|o| String::from_utf8(o.stdout).unwrap())
41 .unwrap();
42
43 let release_mode = if cfg!(debug_assertions) || cfg!(test) {
44 "DEBUG"
45 } else {
46 "RELEASE"
47 };
48
49 let version =
50 format!("{}-{}-{}-{}", main_version, branch, commit, release_mode).replace('\n', "");
51 File::create(Path::new(&env::var("OUT_DIR")?).join("VERSION"))?
52 .write_all(version.trim().as_bytes())?;
53 }};
54 (BUILD_TIME) => {{
55 use std::{env, fs::File, io::Write, path::Path};
56
57 let now = chrono::Local::now().to_rfc3339();
58 File::create(Path::new(&env::var("OUT_DIR")?).join("BUILD_TIME"))?
59 .write_all(now.trim().as_bytes())?;
60 }};
61 (VERSION => $vis:vis $name:ident) => {
62 $vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/VERSION"));
64 };
65 (VERSION => $vis:vis $name:ident, $($c:tt)*) => {
66 $vis static $name: &'static str = concat!($($c)*, include_str!(concat!(env!("OUT_DIR"), "/VERSION")));
68 };
69 (BUILD_TIME => $vis:vis $name:ident) => {
70 $vis static $name: &'static str = include_str!(concat!(env!("OUT_DIR"), "/BUILD_TIME"));
72 };
73}