#[cfg(feature = "cpp")]
fn build_fastpfor() {
use std::env;
use std::path::Path;
assert!(
Path::new("cpp/CMakeLists.txt").exists(),
"FastPFOR submodule not initialized. Run `git submodule update --init`."
);
println!("cargo:rerun-if-changed=cpp");
let simd_features = [
(cfg!(feature = "cpp_portable"), "'cpp_portable'"),
(cfg!(feature = "cpp_native"), "'cpp_native'"),
];
let enabled_simd_features: Vec<_> = simd_features
.into_iter()
.filter_map(|(enabled, name)| enabled.then_some(name))
.collect();
let simd_mode = env::var("FASTPFOR_SIMD_MODE");
if enabled_simd_features.len() > 1 {
let feats = enabled_simd_features.join(", ");
if let Ok(simd_mode) = &simd_mode {
println!(
"cargo::warning=Multiple SIMD mode features are enabled: {feats}, but FASTPFOR_SIMD_MODE overrides it with {simd_mode}."
);
} else {
println!(
"cargo::warning=Multiple SIMD mode features enabled: {feats}. Defaulting to {}.",
enabled_simd_features[0]
);
}
}
let simd_mode = simd_mode.as_deref().unwrap_or({
{
if cfg!(feature = "cpp_portable") {
"portable"
} else if cfg!(feature = "cpp_native") {
"native"
} else {
"portable" }
}
});
println!("cargo:rerun-if-env-changed=FASTPFOR_SIMD_MODE");
let cmake_out = cmake::Config::new("cpp")
.define("FASTPFOR_WITH_TEST", "OFF")
.define("FASTPFOR_SIMD_MODE", simd_mode)
.build();
let lib_path = cmake_out.join("lib");
let lib_path = lib_path.to_str().unwrap();
println!("cargo:rerun-if-changed=src/cpp/fastpfor_bridge.h");
println!("cargo:rerun-if-changed=src/cpp/mod.rs");
let mut bridge = cxx_build::bridge("src/cpp/mod.rs");
bridge
.include("cpp/headers")
.include("src/cpp")
.std("c++14");
if env::var("CARGO_CFG_TARGET_ARCH").is_ok_and(|arch| arch == "aarch64") {
bridge.define("SIMDE_ENABLE_NATIVE_ALIASES", None);
let simde_include = cmake_out.join("build").join("_deps").join("simde-src");
if simde_include.exists() {
bridge.include(simde_include);
} else {
println!(
"cargo:warning=SIMDe headers were not found in CMake build output; \
ensure SIMDe is available on the include path if bridge compilation fails."
);
}
}
bridge.compile("fastpfor_bridge");
println!("cargo:rustc-link-search=native={lib_path}");
println!("cargo:rustc-link-lib=static=FastPFOR");
}
fn main() {
#[cfg(feature = "cpp")]
build_fastpfor();
}