use std::env; use std::fs::File; use std::io::Write; // {arch}-{vendor}-{sys}-{abi}. // https://doc.rust-lang.org/reference/conditional-compilation.html fn main() { // BUILDING_INFO='-X main.Name=$(APP_NAME) -X main.Author=$(COMMITER) -X main.Commit=$(GIT_HASH) -X main.Time=$(TIME)' let mut app_name = String::new(); let mut author = String::new(); let mut git_version = String::new(); let mut build_time = "1694885701".to_string(); if let Ok(input) = std::env::var("BUILDING_INFO") { let parts: Vec<&str> = input.split("-X ").collect(); for part in &parts { if let Some(value) = part.trim().strip_prefix("main.Name=") { app_name = value .trim() .trim_end_matches('\'') .trim_start_matches('\'') .to_string(); } else if let Some(value) = part.trim().strip_prefix("main.Author=") { author = value .trim() .trim_end_matches('\'') .trim_start_matches('\'') .to_string(); } else if let Some(value) = part.trim().strip_prefix("main.Commit=") { git_version = value .trim() .trim_end_matches('\'') .trim_start_matches('\'') .to_string(); } else if let Some(value) = part.trim().strip_prefix("main.Time=") { build_time = value .trim() .trim_end_matches('\'') .trim_start_matches('\'') .to_string(); } } } let target_family = if cfg!(target_family = "unix") { "Linux" } else if cfg!(target_os = "windows") { "macOS" } else { "unknowos" }; let target_os = if cfg!(target_os = "linux") { "Linux" } else if cfg!(target_os = "macos") { "macOS" } else if cfg!(target_os = "windows") { "windows" } else { "unknowos" }; // Get the target architecture let target_arch = if cfg!(target_arch = "x86") { "32-Bit x86" } else if cfg!(target_arch = "x86_64") { "64-Bit x86_64" } else if cfg!(target_arch = "aarch64") { "64-Bit ARM64" } else { "unknow" }; let dest_path = std::path::Path::new(&env::var("OUT_DIR").unwrap()).join("build_info.rs"); let mut f = File::create(dest_path).unwrap(); let build_info = format!( "\ pub const GIT_VERSION: &str = \"{}\";\n\ pub const MAIN_AUTHOR: &str = \"{}\";\n\ pub const APP_NAME: &str = \"{}\";\n\ pub const BUILDING_TIME: i64 = {};\n\ pub const TARGET_OS: &str = \"{} {}\";\n\ pub const TARGET_ARCH: &str = \"{}\";\n\ ", git_version.trim(), author.trim(), app_name.trim(), build_time, target_os, target_family, target_arch ); f.write_all(build_info.as_bytes()).unwrap(); }