From 92a231fc005dd1884e2d113d603057138f2cb136 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Gonz=C3=A1lez?= Date: Sat, 23 Nov 2024 18:10:14 +0100 Subject: [PATCH] fix: set Windows executable metadata when cross-compiling Our build script only worked when compiling from a Windows host to a Windows target. --- packages/packsquash_cli/Cargo.toml | 2 +- packages/packsquash_cli/build.rs | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/packsquash_cli/Cargo.toml b/packages/packsquash_cli/Cargo.toml index 0cadceedd..cbe40c551 100644 --- a/packages/packsquash_cli/Cargo.toml +++ b/packages/packsquash_cli/Cargo.toml @@ -40,7 +40,7 @@ mimalloc = { version = "0.1.43", default-features = false, features = [ "override", ] } -[target.'cfg(windows)'.build-dependencies] +[build-dependencies] winresource = "0.1.17" [[bin]] diff --git a/packages/packsquash_cli/build.rs b/packages/packsquash_cli/build.rs index 8793004e8..a8c547b66 100644 --- a/packages/packsquash_cli/build.rs +++ b/packages/packsquash_cli/build.rs @@ -1,6 +1,7 @@ //! Build helper for the `PackSquash` CLI. use std::env; +use winresource::WindowsResource; /// Initializes environment variables that will be accessible in the source /// code via the env! macro, and takes care of build-time metadata. @@ -47,9 +48,14 @@ fn main() { add_executable_metadata(); } -#[cfg(windows)] fn add_executable_metadata() { - let mut windows_resource = winresource::WindowsResource::new(); + // Build scripts are always compiled for the host target, so to make sure metadata is + // added when cross-compiling to Windows, we need to check the target OS during runtime + if env::var_os("CARGO_CFG_WINDOWS").is_none() { + return; + } + + let mut windows_resource = WindowsResource::new(); windows_resource.set("LegalCopyright", env!("CARGO_PKG_AUTHORS")); windows_resource.set( "FileDescription", @@ -63,6 +69,3 @@ fn add_executable_metadata() { .compile() .expect("Windows executable resource build failure"); } - -#[cfg(not(windows))] -fn add_executable_metadata() {}