From 8c67d8c592dde1a4af425305c8dc780efba6ae7e Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Wed, 15 Jul 2026 04:03:01 +0200 Subject: [PATCH] - cross-compilation --- .gitignore | 2 ++ build.rs | 31 +++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index fbf1ddc..be13f29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ /target /Cargo.lock +/build-test +*.o # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/build.rs b/build.rs index 525f195..84abc99 100644 --- a/build.rs +++ b/build.rs @@ -34,15 +34,24 @@ fn find_static_lib(dir: &Path, base_name: &str, target_is_windows: bool) -> Opti None } -fn lib_name_from_path(lib_path: &Path) -> Result> { +fn lib_name_from_path( + lib_path: &Path, + target_is_windows: bool, +) -> Result> { let file_name = lib_path .file_stem() .ok_or(Error::msg("cannot get file stem"))? .to_str() .ok_or(Error::msg("cannot into string"))?; - // cargo::rustc-link-lib=static=NAME tells the linker to find libNAME.a / NAME.lib, - // so strip any leading "lib" prefix to avoid liblibNAME. - Ok(file_name.strip_prefix("lib").unwrap_or(file_name).to_string()) + // On Unix, `cargo::rustc-link-lib=static=NAME` makes the linker search for libNAME.a, + // so strip any leading "lib" prefix to avoid liblibNAME.a. + // On MSVC, the linker searches for NAME.lib directly, so keep the full name + // (e.g. libCZIAPIStatic.lib needs link-lib=static=libCZIAPIStatic). + if target_is_windows { + Ok(file_name.to_string()) + } else { + Ok(file_name.strip_prefix("lib").unwrap_or(file_name).to_string()) + } } struct Error(String); @@ -124,6 +133,10 @@ fn main() -> Result<(), Box> { let mut cmake_config = cmake::Config::new(&libczi_dir); cmake_config .cxxflag("-fms-extensions") + .define( + "ADDITIONAL_LIBS_REQUIRED_FOR_ATOMIC", + "", + ) .define("LIBCZI_BUILD_UNITTESTS", "OFF") .define("LIBCZI_BUILD_CZICMD", "OFF") .define("LIBCZI_BUILD_DYNLIB", "OFF") @@ -139,8 +152,7 @@ fn main() -> Result<(), Box> { if is_windows { cmake_config .define("CMAKE_SYSTEM_NAME", "Windows") - .define("CRASH_ON_UNALIGNED_ACCESS", "FALSE") - .define("ADDITIONAL_LIBS_REQUIRED_FOR_ATOMIC", ""); + .define("CRASH_ON_UNALIGNED_ACCESS", "FALSE"); let has_toolchain_file = env::var("TARGET_CMAKE_TOOLCHAIN_FILE").is_ok() || env::var(format!( @@ -161,7 +173,6 @@ fn main() -> Result<(), Box> { } else if target.contains("apple") { cmake_config .define("CMAKE_SYSTEM_NAME", "Darwin") - .define("ADDITIONAL_LIBS_REQUIRED_FOR_ATOMIC", "") .define("CRASH_ON_UNALIGNED_ACCESS", "OFF"); if target.contains("aarch64") { cmake_config.define("NEON_INTRINSICS_CAN_BE_USED", "ON"); @@ -235,7 +246,7 @@ fn main() -> Result<(), Box> { let libcziapi_lib = find_static_lib(&libcziapi_dir, "libCZIAPIStatic", is_windows) .or_else(|| find_static_lib(&libcziapi_dir2, "libCZIAPIStatic", is_windows)) .ok_or(Error::msg("cannot find libCZIAPIStatic for linking"))?; - let libcziapi_name = lib_name_from_path(&libcziapi_lib)?; + let libcziapi_name = lib_name_from_path(&libcziapi_lib, is_windows)?; let profile = env::var("PROFILE")?; let libczi_base = match profile.as_str() { @@ -249,7 +260,7 @@ fn main() -> Result<(), Box> { "cannot find {} for linking", libczi_base )))?; - let libczi_name = lib_name_from_path(&libczi_lib)?; + let libczi_name = lib_name_from_path(&libczi_lib, is_windows)?; if libcziapi_dir.exists() { println!( @@ -304,7 +315,7 @@ fn main() -> Result<(), Box> { } let zstd_name = find_static_lib(&zstd_dir, "zstd_static", is_windows) .or_else(|| find_static_lib(&zstd_dir2, "zstd_static", is_windows)) - .map(|p| lib_name_from_path(&p).unwrap_or_else(|_| "zstd_static".to_string())) + .map(|p| lib_name_from_path(&p, is_windows).unwrap_or_else(|_| "zstd_static".to_string())) .unwrap_or_else(|| "zstd_static".to_string()); println!("cargo::rustc-link-lib=static={}", zstd_name); } else {