const ICON_ICO: &str = "assets/mb-icon.ico";
fn main() {
println!("cargo::rerun-if-changed={ICON_ICO}");
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
return;
}
if let Err(reason) = embed_icon() {
println!("cargo::warning=MahBot's icon is missing from this Windows build: {reason}");
}
}
fn embed_icon() -> Result<(), String> {
if std::env::var_os("RC_PATH").is_none()
&& std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc")
&& let Some(rc) = rc_on_path()
{
unsafe { std::env::set_var("RC_PATH", rc) };
}
let mut resource = winresource::WindowsResource::new();
resource.set("ProductName", "MahBot");
resource.set("FileDescription", "MahBot");
let manifest = std::env::var("CARGO_MANIFEST_DIR")
.map_err(|_| "CARGO_MANIFEST_DIR is not set".to_string())?;
resource.set_icon(
&std::path::Path::new(&manifest)
.join(ICON_ICO)
.to_string_lossy(),
);
resource.compile().map_err(|e| e.to_string())
}
#[cfg(windows)]
fn rc_on_path() -> Option<std::path::PathBuf> {
std::env::split_paths(&std::env::var_os("PATH")?).find_map(|dir| {
let rc = dir.join("rc.exe");
rc.is_file().then_some(rc)
})
}
#[cfg(not(windows))]
fn rc_on_path() -> Option<std::path::PathBuf> {
None
}