From 816effae97cb806e838e2aa993459b4b82988edd Mon Sep 17 00:00:00 2001 From: rteoo Date: Sat, 26 Sep 2026 10:32:00 -0300 Subject: [PATCH] fix(windows): embed icon in executable --- .github/workflows/ci.yml | 18 +++++++++++ assets/disktree.ico | Bin 0 -> 3267 bytes crates/disktree-app/build.rs | 57 +++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 assets/disktree.ico create mode 100644 crates/disktree-app/build.rs diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 72b7442..4807901 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -99,6 +99,24 @@ jobs: - name: Test run: cargo xtask test + - name: Check embedded executable icon + shell: pwsh + run: | + cargo build --release --locked -p disktree-app + Add-Type @' + using System; + using System.Runtime.InteropServices; + public static class ExecutableIcons { + [DllImport("shell32.dll", CharSet = CharSet.Unicode)] + public static extern uint ExtractIconEx( + string file, int index, IntPtr[] large, IntPtr[] small, uint count); + } + '@ + $exe = (Resolve-Path target/release/disktree.exe).Path + if ([ExecutableIcons]::ExtractIconEx($exe, -1, $null, $null, 0) -lt 1) { + throw "disktree.exe has no embedded icon" + } + # The other half of each release: the same tests on the architectures # the release builds for but the jobs above do not run on. other-architectures: diff --git a/assets/disktree.ico b/assets/disktree.ico new file mode 100644 index 0000000000000000000000000000000000000000..77ca68b311da7e89478f28dc53ea893c886755d5 GIT binary patch literal 3267 zcmcImc|6qZ7XJ-1WjDsymxhK^%8;?9K@&qr%91xJOO}wVQ)p}%YcwfKDTIXb+CxOP zEQ2hKrLjlWQG+4dRsC~6_r2Hcy`Rs0{&~)MmUGT?zUO;B4*-AwFaU)DI|Bi%g8)G1 zhlKd0?`H-8EhYd!p?>LAJGvd@hx}t8fb<<*f)xNFjE!_T*@W447CH5BSd*RkheasM z&Rrnxo}&@~fZpk0wa()odz%M_0g`XlCi|a>u;e!_2-lf zrIXf{wlc5U`zQ^IZEh{JklMEq8H&eEPvBwldTMjDLWSg1ZX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@B$h z-XgV?LytKft<8C#>P(XvI`Ht=_yp33-K$i&Qc9yLAtFy=*4${l8S8ff>c@4md_fYE z-KRo+@FcoejXO=4#USs~`mCg@464NaEzRw=YPK^(*dCgc>qCF^A6CP*2*yB_jo!%Rj zf~_M_n`NlhnQeG+_BKdfilot*eC6ZrC%bbz9AcJl#v}VLaLmhu`=9Kb>rygRBt5 v+b;$0RjaD-G-K?V^3t6uFI*A7jF$edRsAo)L>FSFDS~XMjG5JcX6QcvD|=I! literal 0 HcmV?d00001 diff --git a/crates/disktree-app/build.rs b/crates/disktree-app/build.rs new file mode 100644 index 0000000..cb46147 --- /dev/null +++ b/crates/disktree-app/build.rs @@ -0,0 +1,57 @@ +#[cfg(windows)] +fn main() { + use std::{env, fs, path::PathBuf, process::Command}; + + let icon = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()) + .join("../../assets/disktree.ico") + .canonicalize() + .expect("Windows icon asset is missing"); + let out = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + let rc = out.join("disktree.rc"); + let res = out.join("disktree.res"); + + // rc.exe treats backslashes inside quoted paths as separators, not escapes. + let icon_path = icon.to_string_lossy().replace('\\', "/"); + fs::write(&rc, format!("1 ICON \"{icon_path}\"\n")) + .expect("could not write Windows icon resource script"); + + // Cargo can run outside a Visual Studio prompt, where the SDK bin is not + // on PATH even though the MSVC linker and Windows SDK are installed. + let rc_exe = env::var_os("DISKTREE_RC").map(PathBuf::from).unwrap_or_else(|| { + let sdk_bin = env::var_os("WindowsSdkDir") + .map(PathBuf::from) + .or_else(|| { + env::var_os("ProgramFiles(x86)") + .map(|root| PathBuf::from(root).join("Windows Kits/10")) + }) + .expect("Windows SDK is required to embed the icon") + .join("bin"); + let host = match env::consts::ARCH { + "x86_64" => "x64", + "aarch64" => "arm64", + "x86" => "x86", + other => panic!("unsupported Windows build host: {other}"), + }; + let mut versions: Vec<_> = fs::read_dir(&sdk_bin) + .expect("Windows SDK bin directory is missing") + .filter_map(Result::ok) + .map(|entry| entry.path().join(host).join("rc.exe")) + .filter(|path| path.is_file()) + .collect(); + versions.sort(); + versions.pop().expect("rc.exe is missing from the Windows SDK") + }); + let status = Command::new(rc_exe) + .arg("/nologo") + .arg(format!("/fo{}", res.display())) + .arg(&rc) + .status() + .expect("could not start rc.exe to embed the Windows icon"); + assert!(status.success(), "rc.exe failed to compile the Windows icon"); + + println!("cargo:rerun-if-changed={}", icon.display()); + println!("cargo:rustc-link-arg-bin=disktree={}", res.display()); +} + +#[cfg(not(windows))] +fn main() {}