forked from unum-cloud/usearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
118 lines (107 loc) · 3.92 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
fn main() {
let mut build = cxx_build::bridge("rust/lib.rs");
build
.file("rust/lib.cpp")
.flag_if_supported("-Wno-unknown-pragmas")
.warnings(false)
.include("include")
.include("rust")
.include("fp16/include")
.include("simsimd/include");
// Check for optional features
if cfg!(feature = "openmp") {
build.define("USEARCH_USE_OPENMP", "1");
} else {
build.define("USEARCH_USE_OPENMP", "0");
}
if cfg!(feature = "fp16lib") {
build.define("USEARCH_USE_FP16LIB", "1");
} else {
build.define("USEARCH_USE_FP16LIB", "0");
}
// Define all possible SIMD targets as 1
let target_arch = std::env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default();
let flags_to_try = match target_arch.as_str() {
"arm" | "aarch64" => vec![
"SIMSIMD_TARGET_SVE_BF16",
"SIMSIMD_TARGET_SVE_F16",
"SIMSIMD_TARGET_SVE_I8",
"SIMSIMD_TARGET_SVE",
"SIMSIMD_TARGET_NEON_BF16",
"SIMSIMD_TARGET_NEON_F16",
"SIMSIMD_TARGET_NEON_I8",
"SIMSIMD_TARGET_NEON",
],
_ => vec![
"SIMSIMD_TARGET_SAPPHIRE",
"SIMSIMD_TARGET_GENOA",
"SIMSIMD_TARGET_ICE",
"SIMSIMD_TARGET_SKYLAKE",
"SIMSIMD_TARGET_HASWELL",
],
};
if cfg!(feature = "simsimd") {
build.define("USEARCH_USE_SIMSIMD", "1")
.define("SIMSIMD_DYNAMIC_DISPATCH", "1")
.define("SIMSIMD_NATIVE_BF16", "0")
.define("SIMSIMD_NATIVE_F16", "0");
for flag in &flags_to_try {
build.define(flag, "1");
}
} else {
build.define("USEARCH_USE_SIMSIMD", "0");
}
// Conditional compilation depending on the target operating system.
if cfg!(target_os = "linux") {
build
.flag_if_supported("-std=c++17")
.flag_if_supported("-O3")
.flag_if_supported("-ffast-math")
.flag_if_supported("-fdiagnostics-color=always")
.flag_if_supported("-g1"); // Simplify debugging
} else if cfg!(target_os = "macos") {
build
.flag_if_supported("-mmacosx-version-min=10.15")
.flag_if_supported("-std=c++17")
.flag_if_supported("-O3")
.flag_if_supported("-ffast-math")
.flag_if_supported("-fcolor-diagnostics")
.flag_if_supported("-g1"); // Simplify debugging
} else if cfg!(target_os = "windows") {
build
.flag_if_supported("/std:c++17")
.flag_if_supported("/O2")
.flag_if_supported("/fp:fast")
.flag_if_supported("/W1")
.flag_if_supported("/EHsc")
.flag_if_supported("/MD")
.flag_if_supported("/permissive-")
.flag_if_supported("/sdl-")
.define("_ALLOW_RUNTIME_LIBRARY_MISMATCH", None)
.define("_ALLOW_POINTER_TO_CONST_MISMATCH", None);
}
let mut result = build.try_compile("usearch");
if result.is_err() {
print!("cargo:warning=Failed to compile with all SIMD backends...");
for flag in flags_to_try {
build.define(flag, "0");
result = build.try_compile("usearch");
if result.is_err() {
println!(
"cargo:warning=Failed to compile after disabling {}, trying next configuration...",
flag
);
} else {
break;
}
}
}
// Ensure one build has been successful
result.unwrap();
println!("cargo:rerun-if-changed=rust/lib.rs");
println!("cargo:rerun-if-changed=rust/lib.cpp");
println!("cargo:rerun-if-changed=rust/lib.hpp");
println!("cargo:rerun-if-changed=include/index_plugins.hpp");
println!("cargo:rerun-if-changed=include/index_dense.hpp");
println!("cargo:rerun-if-changed=include/usearch/index.hpp");
}