| 1 |
use std::env; |
| 2 |
|
| 3 |
fn defined(var: &str) -> bool { |
| 4 |
println!("cargo::rerun-if-env-changed={}", var); |
| 5 |
env::var_os(var).is_some() |
| 6 |
} |
| 7 |
|
| 8 |
fn is_pure() -> bool { |
| 9 |
defined("CARGO_FEATURE_PURE") |
| 10 |
} |
| 11 |
|
| 12 |
fn should_prefer_intrinsics() -> bool { |
| 13 |
defined("CARGO_FEATURE_PREFER_INTRINSICS") |
| 14 |
} |
| 15 |
|
| 16 |
fn is_neon() -> bool { |
| 17 |
defined("CARGO_FEATURE_NEON") |
| 18 |
} |
| 19 |
|
| 20 |
fn is_no_neon() -> bool { |
| 21 |
defined("CARGO_FEATURE_NO_NEON") |
| 22 |
} |
| 23 |
|
| 24 |
fn is_wasm32_simd() -> bool { |
| 25 |
defined("CARGO_FEATURE_WASM32_SIMD") |
| 26 |
} |
| 27 |
|
| 28 |
fn is_ci() -> bool { |
| 29 |
defined("BLAKE3_CI") |
| 30 |
} |
| 31 |
|
| 32 |
fn warn(warning: &str) { |
| 33 |
assert!(!warning.contains("\n")); |
| 34 |
println!("cargo::warning={}", warning); |
| 35 |
if is_ci() { |
| 36 |
println!("cargo::warning=Warnings in CI are treated as errors. Build failed."); |
| 37 |
std::process::exit(1); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
fn target_components() -> Vec<String> { |
| 42 |
let target = env::var("TARGET").unwrap(); |
| 43 |
target.split("-").map(|s| s.to_string()).collect() |
| 44 |
} |
| 45 |
|
| 46 |
fn is_x86_64() -> bool { |
| 47 |
target_components()[0] == "x86_64" |
| 48 |
} |
| 49 |
|
| 50 |
fn is_windows_target() -> bool { |
| 51 |
env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows" |
| 52 |
} |
| 53 |
|
| 54 |
fn use_msvc_asm() -> bool { |
| 55 |
const MSVC_NAMES: &[&str] = &["", "cl", "cl.exe"]; |
| 56 |
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default(); |
| 57 |
let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default(); |
| 58 |
let target_windows_msvc = target_os == "windows" && target_env == "msvc"; |
| 59 |
let host_triple = env::var("HOST").unwrap_or_default(); |
| 60 |
let target_triple = env::var("TARGET").unwrap_or_default(); |
| 61 |
let cross_compiling = host_triple != target_triple; |
| 62 |
let cc = env::var("CC").unwrap_or_default().to_ascii_lowercase(); |
| 63 |
if !target_windows_msvc { |
| 64 |
// We are not building for Windows with the MSVC toolchain. |
| 65 |
false |
| 66 |
} else if !cross_compiling && MSVC_NAMES.contains(&&*cc) { |
| 67 |
// We are building on Windows with the MSVC toolchain (and not cross-compiling for another architecture or target). |
| 68 |
true |
| 69 |
} else { |
| 70 |
// We are cross-compiling to Windows with the MSVC toolchain. |
| 71 |
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap_or_default(); |
| 72 |
let target_vendor = env::var("CARGO_CFG_TARGET_VENDOR").unwrap_or_default(); |
| 73 |
let cc = env::var(format!("CC_{target_arch}_{target_vendor}_windows_msvc")) |
| 74 |
.unwrap_or_default() |
| 75 |
.to_ascii_lowercase(); |
| 76 |
// Check if we are using the MSVC compiler. |
| 77 |
MSVC_NAMES.contains(&&*cc) |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
fn is_x86_32() -> bool { |
| 82 |
let arch = &target_components()[0]; |
| 83 |
arch == "i386" || arch == "i586" || arch == "i686" |
| 84 |
} |
| 85 |
|
| 86 |
fn is_arm() -> bool { |
| 87 |
is_armv7() || is_aarch64() || target_components()[0] == "arm" |
| 88 |
} |
| 89 |
|
| 90 |
fn is_aarch64() -> bool { |
| 91 |
target_components()[0] == "aarch64" |
| 92 |
} |
| 93 |
|
| 94 |
fn is_armv7() -> bool { |
| 95 |
target_components()[0] == "armv7" |
| 96 |
} |
| 97 |
|
| 98 |
fn is_wasm32() -> bool { |
| 99 |
target_components()[0] == "wasm32" |
| 100 |
} |
| 101 |
|
| 102 |
fn endianness() -> String { |
| 103 |
let endianness = env::var("CARGO_CFG_TARGET_ENDIAN").unwrap(); |
| 104 |
assert!(endianness == "little" || endianness == "big"); |
| 105 |
endianness |
| 106 |
} |
| 107 |
|
| 108 |
fn is_little_endian() -> bool { |
| 109 |
endianness() == "little" |
| 110 |
} |
| 111 |
|
| 112 |
fn is_big_endian() -> bool { |
| 113 |
endianness() == "big" |
| 114 |
} |
| 115 |
|
| 116 |
// Windows targets may be using the MSVC toolchain or the MinGW toolchain. The |
| 117 |
// right compiler flags to use depend on the toolchain. (And we don't want to |
| 118 |
// use flag_if_supported, because we don't want features to be silently |
| 119 |
// disabled by old compilers.) |
| 120 |
fn is_windows_msvc() -> bool { |
| 121 |
// Some targets are only two components long, so check in steps. |
| 122 |
let second_component = &target_components()[1]; |
| 123 |
(second_component == "pc" || second_component == "win7") |
| 124 |
&& target_components()[2] == "windows" |
| 125 |
&& target_components()[3] == "msvc" |
| 126 |
} |
| 127 |
|
| 128 |
// MinGW toolchain uses 2 different targets depending on the main compiler. |
| 129 |
// Target for a general MinGW toolchain ends with `-gnu` (GCC is used as C |
| 130 |
// compiler). Target for a LLVM-MinGW toolchain (Clang is used as C compiler) |
| 131 |
// ends with `-gnullvm`. |
| 132 |
fn is_windows_gnu() -> bool { |
| 133 |
// Some targets are only two components long, so check in steps. |
| 134 |
let second_component = &target_components()[1]; |
| 135 |
(second_component == "pc" || second_component == "win7") |
| 136 |
&& target_components()[2] == "windows" |
| 137 |
&& target_components()[3] != "msvc" |
| 138 |
} |
| 139 |
|
| 140 |
fn new_build() -> cc::Build { |
| 141 |
let mut build = cc::Build::new(); |
| 142 |
if !is_windows_msvc() { |
| 143 |
build.flag("-std=c11"); |
| 144 |
} |
| 145 |
// Do NOT trigger a rebuild any time the env changes (e.g. $PATH). |
| 146 |
// This prevents all downstream crates from being rebuilt when `cargo check` |
| 147 |
// or `cargo build` are run in different environments, like Rust Analyzer |
| 148 |
// vs. in the terminal vs. in a Git pre-commit hook. |
| 149 |
build.emit_rerun_if_env_changed(false); |
| 150 |
build |
| 151 |
} |
| 152 |
|
| 153 |
#[derive(PartialEq)] |
| 154 |
enum CCompilerSupport { |
| 155 |
NoCompiler, |
| 156 |
NoAVX512, |
| 157 |
YesAVX512, |
| 158 |
} |
| 159 |
use CCompilerSupport::*; |
| 160 |
|
| 161 |
fn c_compiler_support() -> CCompilerSupport { |
| 162 |
let build = new_build(); |
| 163 |
let flags_checked; |
| 164 |
let support_result: Result<bool, _> = if is_windows_msvc() { |
| 165 |
flags_checked = "/arch:AVX512"; |
| 166 |
build.is_flag_supported("/arch:AVX512") |
| 167 |
} else { |
| 168 |
// Check for both of the flags we use. If -mavx512f works, then -mavx512vl |
| 169 |
// will probably always work too, but we might as well be thorough. |
| 170 |
flags_checked = "-mavx512f and -mavx512vl"; |
| 171 |
match build.is_flag_supported("-mavx512f") { |
| 172 |
Ok(true) => build.is_flag_supported("-mavx512vl"), |
| 173 |
false_or_error => false_or_error, |
| 174 |
} |
| 175 |
}; |
| 176 |
match support_result { |
| 177 |
Ok(true) => YesAVX512, |
| 178 |
Ok(false) => { |
| 179 |
warn(&format!( |
| 180 |
"The C compiler {:?} does not support {}.", |
| 181 |
build.get_compiler().path(), |
| 182 |
flags_checked, |
| 183 |
)); |
| 184 |
NoAVX512 |
| 185 |
} |
| 186 |
Err(e) => { |
| 187 |
println!("{:?}", e); |
| 188 |
warn(&format!( |
| 189 |
"No C compiler {:?} detected.", |
| 190 |
build.get_compiler().path() |
| 191 |
)); |
| 192 |
NoCompiler |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
fn build_sse2_sse41_avx2_rust_intrinsics() { |
| 198 |
// No C code to compile here. Set the cfg flags that enable the Rust SSE2, |
| 199 |
// SSE4.1, and AVX2 intrinsics modules. The regular Cargo build will compile |
| 200 |
// them. |
| 201 |
println!("cargo::rustc-cfg=blake3_sse2_rust"); |
| 202 |
println!("cargo::rustc-cfg=blake3_sse41_rust"); |
| 203 |
println!("cargo::rustc-cfg=blake3_avx2_rust"); |
| 204 |
} |
| 205 |
|
| 206 |
fn build_sse2_sse41_avx2_assembly() { |
| 207 |
// Build the assembly implementations for SSE4.1 and AVX2. This is |
| 208 |
// preferred, but it only supports x86_64. |
| 209 |
assert!(is_x86_64()); |
| 210 |
println!("cargo::rustc-cfg=blake3_sse2_ffi"); |
| 211 |
println!("cargo::rustc-cfg=blake3_sse41_ffi"); |
| 212 |
println!("cargo::rustc-cfg=blake3_avx2_ffi"); |
| 213 |
let mut build = new_build(); |
| 214 |
if is_windows_target() { |
| 215 |
if use_msvc_asm() { |
| 216 |
build.file("c/blake3_sse2_x86-64_windows_msvc.asm"); |
| 217 |
build.file("c/blake3_sse41_x86-64_windows_msvc.asm"); |
| 218 |
build.file("c/blake3_avx2_x86-64_windows_msvc.asm"); |
| 219 |
} else { |
| 220 |
build.file("c/blake3_sse2_x86-64_windows_gnu.S"); |
| 221 |
build.file("c/blake3_sse41_x86-64_windows_gnu.S"); |
| 222 |
build.file("c/blake3_avx2_x86-64_windows_gnu.S"); |
| 223 |
} |
| 224 |
} else { |
| 225 |
// All non-Windows implementations are assumed to support |
| 226 |
// Linux-style assembly. These files do contain a small |
| 227 |
// explicit workaround for macOS also. |
| 228 |
build.file("c/blake3_sse2_x86-64_unix.S"); |
| 229 |
build.file("c/blake3_sse41_x86-64_unix.S"); |
| 230 |
build.file("c/blake3_avx2_x86-64_unix.S"); |
| 231 |
} |
| 232 |
build.compile("blake3_sse2_sse41_avx2_assembly"); |
| 233 |
} |
| 234 |
|
| 235 |
fn build_avx512_c_intrinsics() { |
| 236 |
// This is required on 32-bit x86 targets, since the assembly |
| 237 |
// implementation doesn't support those. |
| 238 |
println!("cargo::rustc-cfg=blake3_avx512_ffi"); |
| 239 |
let mut build = new_build(); |
| 240 |
build.file("c/blake3_avx512.c"); |
| 241 |
if is_windows_msvc() { |
| 242 |
build.flag("/arch:AVX512"); |
| 243 |
} else { |
| 244 |
build.flag("-mavx512f"); |
| 245 |
build.flag("-mavx512vl"); |
| 246 |
} |
| 247 |
if is_windows_gnu() { |
| 248 |
// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65782. |
| 249 |
build.flag("-fno-asynchronous-unwind-tables"); |
| 250 |
} |
| 251 |
build.compile("blake3_avx512_intrinsics"); |
| 252 |
} |
| 253 |
|
| 254 |
fn build_avx512_assembly() { |
| 255 |
// Build the assembly implementation for AVX-512. This is preferred, but it |
| 256 |
// only supports x86_64. |
| 257 |
assert!(is_x86_64()); |
| 258 |
println!("cargo::rustc-cfg=blake3_avx512_ffi"); |
| 259 |
let mut build = new_build(); |
| 260 |
let mut is_msvc = false; |
| 261 |
if is_windows_target() { |
| 262 |
if use_msvc_asm() { |
| 263 |
build.file("c/blake3_avx512_x86-64_windows_msvc.asm"); |
| 264 |
is_msvc = true; |
| 265 |
} else { |
| 266 |
build.file("c/blake3_avx512_x86-64_windows_gnu.S"); |
| 267 |
} |
| 268 |
} else { |
| 269 |
build.file("c/blake3_avx512_x86-64_unix.S"); |
| 270 |
} |
| 271 |
|
| 272 |
// Older versions of Clang require these flags, even for assembly. See |
| 273 |
// https://github.com/BLAKE3-team/BLAKE3/issues/79. |
| 274 |
if !is_msvc { |
| 275 |
build.flag("-mavx512f"); |
| 276 |
build.flag("-mavx512vl"); |
| 277 |
} |
| 278 |
build.compile("blake3_avx512_assembly"); |
| 279 |
} |
| 280 |
|
| 281 |
fn build_neon_c_intrinsics() { |
| 282 |
let mut build = new_build(); |
| 283 |
// Note that blake3_neon.c normally depends on the blake3_portable.c |
| 284 |
// for the single-instance compression function, but we expose |
| 285 |
// portable.rs over FFI instead. See ffi_neon.rs. |
| 286 |
build.file("c/blake3_neon.c"); |
| 287 |
// ARMv7 platforms that support NEON generally need the following |
| 288 |
// flags. AArch64 supports NEON by default and does not support -mpfu. |
| 289 |
if is_armv7() { |
| 290 |
build.flag("-mfpu=neon-vfpv4"); |
| 291 |
build.flag("-mfloat-abi=hard"); |
| 292 |
} |
| 293 |
build.compile("blake3_neon"); |
| 294 |
} |
| 295 |
|
| 296 |
fn build_wasm32_simd() { |
| 297 |
assert!(is_wasm32()); |
| 298 |
// No C code to compile here. Set the cfg flags that enable the Wasm SIMD. |
| 299 |
// The regular Cargo build will compile it. |
| 300 |
println!("cargo::rustc-cfg=blake3_wasm32_simd"); |
| 301 |
} |
| 302 |
|
| 303 |
fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 304 |
// If CFLAGS is set, make sure LTO is disabled by appending -fno-lto. (We can't do this with |
| 305 |
// .flag() later on, because CFLAGS takes precedence.) Any mismatch between the C compiler and |
| 306 |
// the Rust linker can cause linker errors in our C intrinsics implementations if LTO is |
| 307 |
// enabled. See https://github.com/BLAKE3-team/BLAKE3/issues/550. |
| 308 |
if !is_windows_msvc() { |
| 309 |
if let Some(mut flags) = env::var_os("CFLAGS") { |
| 310 |
flags.push(" -fno-lto"); |
| 311 |
// SAFETY: This build script is single-threaded. |
| 312 |
unsafe { |
| 313 |
env::set_var("CFLAGS", flags); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
// As of Rust 1.80, unrecognized config names are warnings. Give Cargo all of our config names. |
| 319 |
let all_cfgs = [ |
| 320 |
"blake3_sse2_ffi", |
| 321 |
"blake3_sse2_rust", |
| 322 |
"blake3_sse41_ffi", |
| 323 |
"blake3_sse41_rust", |
| 324 |
"blake3_avx2_ffi", |
| 325 |
"blake3_avx2_rust", |
| 326 |
"blake3_avx512_ffi", |
| 327 |
"blake3_neon", |
| 328 |
"blake3_wasm32_simd", |
| 329 |
]; |
| 330 |
for cfg_name in all_cfgs { |
| 331 |
// https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script |
| 332 |
println!("cargo::rustc-check-cfg=cfg({cfg_name}, values(none()))"); |
| 333 |
} |
| 334 |
|
| 335 |
if is_pure() && is_neon() { |
| 336 |
panic!("It doesn't make sense to enable both \"pure\" and \"neon\"."); |
| 337 |
} |
| 338 |
|
| 339 |
if is_no_neon() && is_neon() { |
| 340 |
panic!("It doesn't make sense to enable both \"no_neon\" and \"neon\"."); |
| 341 |
} |
| 342 |
|
| 343 |
if is_x86_64() || is_x86_32() { |
| 344 |
let support = c_compiler_support(); |
| 345 |
if is_x86_32() || should_prefer_intrinsics() || is_pure() || support == NoCompiler { |
| 346 |
build_sse2_sse41_avx2_rust_intrinsics(); |
| 347 |
} else { |
| 348 |
// We assume that all C compilers can assemble SSE4.1 and AVX2. We |
| 349 |
// don't explicitly check for support. |
| 350 |
build_sse2_sse41_avx2_assembly(); |
| 351 |
} |
| 352 |
|
| 353 |
if is_pure() || support == NoCompiler || support == NoAVX512 { |
| 354 |
// The binary will not include any AVX-512 code. |
| 355 |
} else if is_x86_32() || should_prefer_intrinsics() { |
| 356 |
build_avx512_c_intrinsics(); |
| 357 |
} else { |
| 358 |
build_avx512_assembly(); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if is_neon() && is_big_endian() { |
| 363 |
panic!("The NEON implementation doesn't support big-endian ARM.") |
| 364 |
} |
| 365 |
|
| 366 |
if (is_arm() && is_neon()) |
| 367 |
|| (!is_no_neon() && !is_pure() && is_aarch64() && is_little_endian()) |
| 368 |
{ |
| 369 |
println!("cargo::rustc-cfg=blake3_neon"); |
| 370 |
build_neon_c_intrinsics(); |
| 371 |
} |
| 372 |
|
| 373 |
if is_wasm32() && is_wasm32_simd() { |
| 374 |
build_wasm32_simd(); |
| 375 |
} |
| 376 |
|
| 377 |
// The `cc` crate doesn't automatically emit rerun-if directives for the |
| 378 |
// environment variables it supports, in particular for $CC. We expect to |
| 379 |
// do a lot of benchmarking across different compilers, so we explicitly |
| 380 |
// add the variables that we're likely to need. |
| 381 |
println!("cargo::rerun-if-env-changed=CC"); |
| 382 |
println!("cargo::rerun-if-env-changed=CFLAGS"); |
| 383 |
|
| 384 |
// Ditto for source files, though these shouldn't change as often. |
| 385 |
for file in std::fs::read_dir("c")? { |
| 386 |
println!( |
| 387 |
"cargo::rerun-if-changed={}", |
| 388 |
file?.path().to_str().expect("utf-8") |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
// When compiling with clang-cl for windows, it adds .asm files to the root |
| 393 |
// which we need to delete so cargo doesn't get angry |
| 394 |
if is_windows_target() && !use_msvc_asm() { |
| 395 |
let _ = std::fs::remove_file("blake3_avx2_x86-64_windows_gnu.asm"); |
| 396 |
let _ = std::fs::remove_file("blake3_avx512_x86-64_windows_gnu.asm"); |
| 397 |
let _ = std::fs::remove_file("blake3_sse2_x86-64_windows_gnu.asm"); |
| 398 |
let _ = std::fs::remove_file("blake3_sse41_x86-64_windows_gnu.asm"); |
| 399 |
} |
| 400 |
|
| 401 |
Ok(()) |
| 402 |
} |