From 100a6034c923f16ad083ce839ebcf6f971661bcf Mon Sep 17 00:00:00 2001 From: Carter Canedy Date: Sat, 23 Aug 2025 21:54:21 -0700 Subject: [PATCH] specify windows-msvc compiler flags --- Cargo.lock | 4 ++-- turbojpeg-sys/build.rs | 31 +++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1f6f660..eaf35ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "adler2" @@ -602,7 +602,7 @@ dependencies = [ [[package]] name = "turbojpeg" -version = "1.3.3" +version = "1.3.4-pre" dependencies = [ "anyhow", "clap", diff --git a/turbojpeg-sys/build.rs b/turbojpeg-sys/build.rs index 254d5c6..751ea47 100644 --- a/turbojpeg-sys/build.rs +++ b/turbojpeg-sys/build.rs @@ -153,6 +153,35 @@ fn build_vendor(link_kind: LinkKind) -> Result { cmake.configure_arg("-DREQUIRE_SIMD=ON"); } + let is_msvc = env("CARGO_CFG_TARGET_ENV").unwrap() == "msvc"; + + #[cfg(target_os = "windows")] { + // add missing msvc-specific flags + if is_msvc { + let opt_level = env("OPT_LEVEL") + .map(|s| s + .to_string_lossy() + .chars() + .nth(0) + .unwrap() + ) + .unwrap(); + + let compiler_flags: &[&str] = match opt_level { + 's' => &["/O1", "/Ob1"], // min size + no opt + min inlining + 'z' => &["/O1", "/Ob0"], // min size + no opt + no inlining + '1' | '2' => &["/O1"], // min opt + '3' => &["/O2", "/Ob3"], // max opt + max inlining + '0' => &["/Od"], // no opt + _ => panic!("unknown optimization level: '{opt_level}'") + }; + + for flag in compiler_flags { + cmake.cflag(flag); + } + } + } + let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap(); if target_os == "android" { let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); @@ -171,8 +200,6 @@ fn build_vendor(link_kind: LinkKind) -> Result { let lib_path = dst_path.join("lib"); let include_path = dst_path.join("include"); - let is_msvc = env("CARGO_CFG_TARGET_ENV").unwrap() == "msvc"; - println!("cargo:rustc-link-search=native={}", lib_path.display()); println!("cargo:rustc-link-lib={}=turbojpeg{}", match link_kind { LinkKind::Static | LinkKind::Default => "static",