-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
68 lines (61 loc) · 2.85 KB
/
build.rs
File metadata and controls
68 lines (61 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2026 Raimo Geisel
// SPDX-License-Identifier: GPL-3.0-only
//! Build script for narsil.
//!
//! When cross-compiling for Windows, embeds `icon.ico` as the application icon
//! so Explorer and the taskbar display it correctly.
//!
//! The `.ico` file is generated from `icon.png` by `scripts/release-github.sh`
//! before the Windows cross-compile step. If the file is absent the build
//! proceeds without an icon and emits a `cargo:warning` instead of failing.
fn main() {
// Only do anything when the *target* platform is Windows.
// This env var is set by Cargo and reflects the target, not the host.
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("windows") {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = std::env::var("OUT_DIR").unwrap();
let ico_src = std::path::PathBuf::from(&manifest_dir).join("icon.ico");
if !ico_src.exists() {
println!(
"cargo:warning=icon.ico not found; Windows exe will have no icon. \
Run `convert icon.png -define icon:auto-resize=256,128,64,48,32,16 icon.ico` \
to generate it, or use scripts/release-github.sh which does this automatically."
);
return;
}
// Copy icon next to the .rc file so windres can resolve the relative path.
let ico_dest = std::path::PathBuf::from(&out_dir).join("narsil.ico");
std::fs::copy(&ico_src, &ico_dest).expect("failed to copy icon to OUT_DIR");
// Write a minimal Windows resource script.
let rc_path = std::path::PathBuf::from(&out_dir).join("narsil.rc");
std::fs::write(&rc_path, "1 ICON \"narsil.ico\"\n")
.expect("failed to write resource script");
// Compile the .rc → COFF object with the mingw windres tool.
let obj_path = std::path::PathBuf::from(&out_dir).join("narsil_icon.o");
let windres = "x86_64-w64-mingw32-windres";
let status = std::process::Command::new(windres)
.args([
rc_path.to_str().unwrap(),
"-o",
obj_path.to_str().unwrap(),
"--target=pe-x86-64",
])
// Run inside OUT_DIR so relative paths in the .rc file resolve.
.current_dir(&out_dir)
.status();
match status {
Ok(s) if s.success() => {
// Tell the linker to include the resource object.
println!("cargo:rustc-link-arg={}", obj_path.display());
}
Ok(s) => {
println!("cargo:warning=windres exited with {s}; Windows exe will have no icon");
}
Err(e) => {
println!(
"cargo:warning=Could not run {windres}: {e}; Windows exe will have no icon"
);
}
}
}
}