//! Build script for the alkane-toolkit binary. On Windows targets it compiles //! windows/alkane-toolkit.rc (application manifest, icon, version info, and //! wxMSW's stock resources) and links the result into the GUI executable only. //! Other targets need nothing here. use std::env; use std::path::PathBuf; use std::process::Command; fn main() { println!("cargo:rerun-if-changed=build.rs"); if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") { return; } for f in [ "alkane-toolkit.rc", "alkane-toolkit.exe.manifest", "alkane-toolkit.ico", ] { println!("cargo:rerun-if-changed=windows/{f}"); } println!("cargo:rerun-if-env-changed=WX_RS_WXCONFIG"); if let Err(e) = compile_resources() { panic!("alkane-gui: Windows resources: {e}"); } } fn compile_resources() -> Result<(), String> { // The resource compiler and wx's resource include path come from the same // wx-config the wx-rs bridge builds against, so they always match the // linked wxMSW (and the mingw cross windres when cross-compiling). let wxconfig = env::var("WX_RS_WXCONFIG").unwrap_or_else(|_| "wx-config".into()); let run = |arg: &str| -> Result { let out = Command::new(&wxconfig) .arg(arg) .output() .map_err(|e| format!("cannot run {wxconfig} {arg}: {e}"))?; if !out.status.success() { return Err(format!("{wxconfig} {arg} failed")); } String::from_utf8(out.stdout).map_err(|e| e.to_string()) }; let rescomp = run("--rescomp")?; let mut words = rescomp.split_whitespace(); let windres = words.next().ok_or("wx-config --rescomp printed nothing")?; let mut args: Vec = words.map(str::to_owned).collect(); // rcdefs.h lives in the build-specific setup directory, which --rescomp // omits; it is the -I entry of --cxxflags. for flag in run("--cxxflags")?.split_whitespace() { if let Some(dir) = flag.strip_prefix("-I") { args.push("--include-dir".into()); args.push(dir.into()); } } let version = env::var("CARGO_PKG_VERSION").unwrap_or_default(); let mut parts = version .split(['.', '-', '+']) .map(|p| p.parse::().unwrap_or(0)); let (major, minor, patch) = ( parts.next().unwrap_or(0), parts.next().unwrap_or(0), parts.next().unwrap_or(0), ); let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").map_err(|e| e.to_string())?); let out = PathBuf::from(env::var("OUT_DIR").map_err(|e| e.to_string())?).join("alkane-toolkit-res.o"); let status = Command::new(windres) .args(&args) .arg("--include-dir") .arg(manifest_dir.join("windows")) .arg(format!("--define=ALKANE_VER_MAJOR={major}")) .arg(format!("--define=ALKANE_VER_MINOR={minor}")) .arg(format!("--define=ALKANE_VER_PATCH={patch}")) .arg(format!("--define=ALKANE_VER_STR=\\\"{version}\\\"")) .arg("--input") .arg(manifest_dir.join("windows/alkane-toolkit.rc")) .arg("--output-format=coff") .arg("--output") .arg(&out) .status() .map_err(|e| format!("cannot run {windres}: {e}"))?; if !status.success() { return Err(format!("{windres} failed ({status})")); } // Only the GUI binary carries the manifest/icon; miner_probe stays a plain // console tool. A linked object (not an archive member) is never dropped by // the linker, which is what keeps the resource section in the .exe. println!("cargo:rustc-link-arg-bin=alkane-toolkit={}", out.display()); Ok(()) }