use std::env;
use std::fs;
use std::path::Path;
fn has_feature(list: &str, feature: &str) -> bool {
list.split(',').any(|f| f == feature)
}
fn main() {
#[cfg(feature = "c_ffi_tests")]
cc::Build::new()
.file("tests/c_inspect_arrow.c")
.compile("cinspect_arrow");
#[cfg(feature = "c_ffi_tests")]
println!("cargo:rerun-if-changed=tests/c_inspect_arrow.c");
let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let feats = env::var("CARGO_CFG_TARGET_FEATURE").unwrap_or_default();
let override_lanes = env::var("SIMD_LANES_OVERRIDE").ok();
let (w8, w16, w32, w64) = if let Some(val) = override_lanes {
let parts: Vec<_> = val.split(',').map(|s| s.trim().parse::<usize>()).collect();
if parts.len() == 4 && parts.iter().all(|r| r.is_ok()) {
let vals: Vec<usize> = parts.into_iter().map(|r| r.unwrap()).collect();
println!("cargo:warning=SIMD_LANES_OVERRIDE applied: {:?}", vals);
(vals[0], vals[1], vals[2], vals[3])
} else {
panic!(
"Invalid SIMD_LANES_OVERRIDE. Expected 4 comma-separated integers, e.g., \"64,32,16,8\""
);
}
} else {
match arch.as_str() {
"x86_64" | "x86" => {
if has_feature(&feats, "avx512f") {
(64, 32, 16, 8)
}
else if has_feature(&feats, "avx2") {
(32, 16, 8, 4)
}
else if has_feature(&feats, "sse2") {
(16, 8, 4, 2)
}
else {
(8, 4, 2, 1)
} }
"aarch64" => {
if has_feature(&feats, "neon") {
(16, 8, 4, 2)
} else {
(8, 4, 2, 1)
}
}
"wasm32" => {
if has_feature(&feats, "simd128") {
(16, 8, 4, 2)
} else {
(8, 4, 2, 1)
}
}
_ => (8, 4, 2, 1),
}
};
let out_path = Path::new(&env::var("OUT_DIR").unwrap()).join("simd_lanes.rs");
fs::write(
&out_path,
format!(
"
/// Auto-generated SIMD lane widths from build.rs
/// SIMD lane count for 8-bit elements (u8, i8).
/// Determined at build time based on target architecture capabilities,
/// or overridden via `SIMD_LANES_OVERRIDE`.
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub const W8: usize = {w8};
/// SIMD lane count for 16-bit elements (u16, i16).
/// Determined at build time based on target architecture capabilities,
/// or overridden via `SIMD_LANES_OVERRIDE`.
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub const W16: usize = {w16};
/// SIMD lane count for 32-bit elements (u32, i32, f32).
/// Determined at build time based on target architecture capabilities,
/// or overridden via `SIMD_LANES_OVERRIDE`.
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub const W32: usize = {w32};
/// SIMD lane count for 64-bit elements (u64, i64, f64).
/// Determined at build time based on target architecture capabilities,
/// or overridden via `SIMD_LANES_OVERRIDE`.
#[allow(non_upper_case_globals)]
#[allow(dead_code)]
pub const W64: usize = {w64};
"
),
)
.unwrap();
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_ARCH");
println!("cargo:rerun-if-env-changed=CARGO_CFG_TARGET_FEATURE");
println!("cargo:rerun-if-env-changed=SIMD_LANES_OVERRIDE");
}