Skip to content

Commit 38d3db6

Browse files
committed
Avoid enabling AVX when rustc isn't using it
This is needed for ABI to match
1 parent e8626ac commit 38d3db6

File tree

2 files changed

+61
-23
lines changed

2 files changed

+61
-23
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cloudflare-zlib-sys"
3-
version = "0.3.4"
3+
version = "0.3.5"
44
edition = "2021"
55
authors = ["Vlad Krasnov <[email protected]>", "Kornel Lesiński <[email protected]>", "Mark Adler <[email protected]>"]
66
categories = ["external-ffi-bindings", "compression"]

rust/build.rs

+60-22
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ use std::path::PathBuf;
44
fn main() {
55
let mut cc = cc::Build::new();
66
let comp = cc.get_compiler();
7-
let msvc_bugs = comp.is_like_msvc();
7+
let is_msvc = comp.is_like_msvc();
88
cc.warnings(false);
99
cc.pic(true);
1010

11-
let target_pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").expect("CARGO_CFG_TARGET_POINTER_WIDTH var");
12-
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH var");
11+
let target_pointer_width = env::var("CARGO_CFG_TARGET_POINTER_WIDTH").expect("CARGO_CFG_TARGET_POINTER_WIDTH");
12+
let target_endian = env::var("CARGO_CFG_TARGET_ENDIAN").expect("CARGO_CFG_TARGET_ENDIAN");
13+
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH");
14+
let target_feature = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
15+
let has_feature = |f| target_feature.split(',').any(|t| t == f);
16+
17+
if target_endian != "little" {
18+
println!("cargo::error=cloudflare-zlib does not support big endian");
19+
// don't exit(1): https://github.com/rust-lang/cargo/issues/15038
20+
}
1321

1422
if target_pointer_width != "64" {
1523
println!("cargo:warning=cloudflare-zlib does not support 32-bit architectures");
24+
println!("cargo::error=cloudflare-zlib does not support 32-bit architectures");
25+
// don't exit(1): https://github.com/rust-lang/cargo/issues/15038
1626
}
1727

1828
if let Ok(target_cpu) = env::var("TARGET_CPU") {
@@ -26,19 +36,61 @@ fn main() {
2636
if target_arch == "aarch64" {
2737
cc.define("INFLATE_CHUNK_SIMD_NEON", Some("1"));
2838
cc.define("ADLER32_SIMD_NEON", Some("1"));
39+
cc.flag_if_supported(if is_msvc { "/arch:armv8.3" } else { "-march=armv8-a+crc" });
2940
} else if target_arch == "x86_64" {
30-
cc.define("INFLATE_CHUNK_SIMD_SSE2", Some("1"));
31-
if msvc_bugs {
32-
cc.define("__x86_64__", Some("1"));
41+
let sse42_flag = if is_msvc { "/arch:SSE4.2" } else { "-msse4.2" };
42+
43+
let has_avx = has_feature("avx"); // AVX changes ABI, so it can't be enabled just in C
44+
let has_sse42 = has_avx || has_feature("sse4.2") ||
45+
cc.is_flag_supported(sse42_flag).unwrap_or(false) ||
46+
// MSDN says /arch:SSE4.2 exists, but MSVC disagrees.
47+
(is_msvc && cc.is_flag_supported("/arch:AVX").unwrap_or(false));
48+
let has_ssse3 = has_avx || has_feature("ssse3") || cc.is_flag_supported("-mssse3").unwrap_or(false);
49+
let has_pcmul = has_feature("pclmulqdq") || cc.is_flag_supported("-mpclmul").unwrap_or(false);
50+
51+
if has_avx {
52+
cc.define("HAS_AVX", Some("1"));
53+
cc.flag(if is_msvc { "/arch:AVX" } else { "-mavx" });
54+
} else if has_sse42 {
55+
cc.flag(sse42_flag);
56+
}
57+
58+
if has_sse42 {
59+
cc.define("INFLATE_CHUNK_SIMD_SSE2", Some("1"));
60+
if is_msvc {
61+
cc.define("__x86_64__", Some("1"));
62+
}
63+
cc.define("HAS_SSE2", Some("1"));
64+
cc.define("HAS_SSE42", Some("1"));
65+
66+
if has_ssse3 {
67+
cc.flag_if_supported("-mssse3");
68+
cc.define("HAS_SSSE3", Some("1"));
69+
cc.define("ADLER32_SIMD_SSSE3", Some("1"));
70+
}
71+
72+
if has_pcmul {
73+
cc.flag_if_supported("-mpclmul");
74+
cc.define("HAS_PCLMUL", Some("1"));
75+
cc.file(vendor.join("crc32_simd.c"));
76+
77+
if has_feature("pclmulqdq") {
78+
cc.define("SKIP_CPUID_CHECK", Some("1"));
79+
}
80+
}
81+
82+
} else {
83+
println!("cargo::error=This build has disabled SSE4.2 support, but cloudflare-zlib-sys requires it");
84+
println!("cargo:warning=Build with RUSTFLAGS='-Ctarget-feature=+sse4.2. Currently enabled: {target_feature}");
85+
cc.define("INFLATE_CHUNK_GENERIC", Some("1"));
3386
}
3487
}
3588

3689
cc.define("INFLATE_CHUNK_READ_64LE", Some("1"));
3790

38-
if msvc_bugs {
91+
if is_msvc {
3992
cc.define("_CRT_SECURE_NO_DEPRECATE", Some("1"));
4093
cc.define("_CRT_NONSTDC_NO_DEPRECATE", Some("1"));
41-
cc.flag_if_supported("/arch:AVX");
4294
} else {
4395
cc.define("HAVE_UNISTD_H", Some("1"));
4496
cc.define("HAVE_HIDDEN", Some("1"));
@@ -47,20 +99,6 @@ fn main() {
4799
cc.define("HAVE_OFF64_T", Some("1"));
48100
cc.define("_LARGEFILE64_SOURCE", Some("1"));
49101
}
50-
if target_arch == "aarch64" {
51-
cc.flag_if_supported("-march=armv8-a+crc");
52-
} else {
53-
if cc.is_flag_supported("-mssse3").is_ok() {
54-
cc.flag("-mssse3");
55-
cc.define("ADLER32_SIMD_SSSE3", Some("1"));
56-
}
57-
if cc.is_flag_supported("-mpclmul").is_ok() {
58-
cc.flag("-mpclmul");
59-
cc.define("HAS_PCLMUL", None);
60-
cc.file(vendor.join("crc32_simd.c"));
61-
cc.flag_if_supported("-msse4.2");
62-
}
63-
}
64102
}
65103

66104
cc.file(vendor.join("adler32.c"));

0 commit comments

Comments
 (0)