use embed_resource::{CompilationResult, compile};
use git2::Repository;
use std::path::PathBuf;
fn main() {
println!("cargo:rustc-env=MACOSX_DEPLOYMENT_TARGET=10.13");
println!("cargo:rerun-if-changed=build.rs");
built::write_built_file().expect("Failed to acquire build-time information");
if std::env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" {
compile_windows_resources();
}
#[cfg(all(windows, target_env = "msvc"))]
static_vcruntime::metabuild();
}
fn compile_windows_resources() {
let commit_id = if let Ok(repo) = Repository::open("../..") {
let head = repo.head().unwrap();
let commit = head.peel_to_commit().unwrap();
let commit_id = commit.id().to_string()[..7].to_string();
let statuses = repo.statuses(None).unwrap();
let is_dirty = !statuses.is_empty();
format!(" ({}{})", if is_dirty { "!" } else { "" }, &commit_id,)
} else {
println!("cargo:warning=Could not open git repository to get commit ID");
"".to_string()
};
let version = format!("v{}{}", env!("CARGO_PKG_VERSION"), commit_id,);
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let icon_path = PathBuf::from(manifest_dir)
.join("..")
.join("studio")
.join("assets")
.join("onerom.ico")
.display()
.to_string()
.replace("\\", "/");
let cargo_version = env!("CARGO_PKG_VERSION");
let parts: Vec<&str> = cargo_version.split('.').collect();
let file_version = format!(
"{},{},{},0",
parts.first().unwrap_or(&"0"),
parts.get(1).unwrap_or(&"0"),
parts.get(2).unwrap_or(&"0")
);
let rc_content = format!(
r#"1 ICON "{icon_path}"
1 VERSIONINFO
FILEVERSION {file_version}
PRODUCTVERSION {file_version}
{{
BLOCK "StringFileInfo"
{{
BLOCK "080904E4"
{{
VALUE "ProductName", "One ROM"
VALUE "FileDescription", "One ROM CLI - Manage One ROMs"
VALUE "ProductVersion", "{version}"
VALUE "OriginalFilename", "onerom.exe"
VALUE "LegalCopyright", "2026 Piers Finlayson"
VALUE "CompanyName", "piers.rocks"
}}
}}
BLOCK "VarFileInfo"
{{
VALUE "Translation", 0x809, 1252
}}
}}"#
);
let out_dir = std::env::var("OUT_DIR").unwrap();
let rc_path = format!("{}/resources.rc", out_dir);
std::fs::write(&rc_path, rc_content).unwrap();
let result = compile(&rc_path, embed_resource::NONE);
if result != CompilationResult::Ok {
panic!("Failed to compile Windows resources {result}");
}
}