aboutsummaryrefslogtreecommitdiff
path: root/build.rs
diff options
context:
space:
mode:
authorSporif <[email protected]>2022-03-06 01:17:13 +0000
committerJack O'Connor <[email protected]>2025-03-14 22:12:12 -0700
commit6da62fb314f48e81dea36649b1703e86c8a59b15 (patch)
tree0b5713c33d02a9f393381a5662e79cb3d1e0602b /build.rs
parent8eb35fde4187c6ce087824c5c26046574273d8c0 (diff)
Fix cross compilation with clang-cl
Diffstat (limited to 'build.rs')
-rw-r--r--build.rs68
1 files changed, 49 insertions, 19 deletions
diff --git a/build.rs b/build.rs
index 2557120..8e06376 100644
--- a/build.rs
+++ b/build.rs
@@ -43,6 +43,23 @@ fn is_x86_64() -> bool {
target_components()[0] == "x86_64"
}
+fn is_windows_target() -> bool {
+ env::var("CARGO_CFG_TARGET_OS").unwrap() == "windows"
+}
+
+fn use_msvc_asm() -> bool {
+ // cc assumes if it is passed .asm and targetting MSVC that it can use the
+ // Microsoft assemblers, which isn't true when we're not on a Windows
+ // host, but are cross-compiling with clang-cl, so we explicitly check if
+ // we're on a Windows host (with the assumption they'll have the MSVC
+ // toolchain installed if they are, though that's not necessarily true)
+ if env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default() == "gnu" {
+ false
+ } else {
+ env::var("HOST").unwrap_or_default().contains("-windows-")
+ }
+}
+
fn is_x86_32() -> bool {
let arch = &target_components()[0];
arch == "i386" || arch == "i586" || arch == "i686"
@@ -172,14 +189,16 @@ fn build_sse2_sse41_avx2_assembly() {
println!("cargo:rustc-cfg=blake3_sse41_ffi");
println!("cargo:rustc-cfg=blake3_avx2_ffi");
let mut build = new_build();
- if is_windows_msvc() {
- build.file("c/blake3_sse2_x86-64_windows_msvc.asm");
- build.file("c/blake3_sse41_x86-64_windows_msvc.asm");
- build.file("c/blake3_avx2_x86-64_windows_msvc.asm");
- } else if is_windows_gnu() {
- build.file("c/blake3_sse2_x86-64_windows_gnu.S");
- build.file("c/blake3_sse41_x86-64_windows_gnu.S");
- build.file("c/blake3_avx2_x86-64_windows_gnu.S");
+ if is_windows_target() {
+ if use_msvc_asm() {
+ build.file("c/blake3_sse2_x86-64_windows_msvc.asm");
+ build.file("c/blake3_sse41_x86-64_windows_msvc.asm");
+ build.file("c/blake3_avx2_x86-64_windows_msvc.asm");
+ } else {
+ build.file("c/blake3_sse2_x86-64_windows_gnu.S");
+ build.file("c/blake3_sse41_x86-64_windows_gnu.S");
+ build.file("c/blake3_avx2_x86-64_windows_gnu.S");
+ }
} else {
// All non-Windows implementations are assumed to support
// Linux-style assembly. These files do contain a small
@@ -216,19 +235,21 @@ fn build_avx512_assembly() {
assert!(is_x86_64());
println!("cargo:rustc-cfg=blake3_avx512_ffi");
let mut build = new_build();
- if is_windows_msvc() {
- build.file("c/blake3_avx512_x86-64_windows_msvc.asm");
- } else {
- if is_windows_gnu() {
- build.file("c/blake3_avx512_x86-64_windows_gnu.S");
+ let mut is_msvc = false;
+ if is_windows_target() {
+ if use_msvc_asm() {
+ build.file("c/blake3_avx512_x86-64_windows_msvc.asm");
+ is_msvc = true;
} else {
- // All non-Windows implementations are assumed to support Linux-style
- // assembly. These files do contain a small explicit workaround for
- // macOS also.
- build.file("c/blake3_avx512_x86-64_unix.S");
+ build.file("c/blake3_avx512_x86-64_windows_gnu.S");
}
- // Older versions of Clang require these flags, even for assembly. See
- // https://github.com/BLAKE3-team/BLAKE3/issues/79.
+ } else {
+ build.file("c/blake3_avx512_x86-64_unix.S");
+ }
+
+ // Older versions of Clang require these flags, even for assembly. See
+ // https://github.com/BLAKE3-team/BLAKE3/issues/79.
+ if !is_msvc {
build.flag("-mavx512f");
build.flag("-mavx512vl");
}
@@ -321,5 +342,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
);
}
+ // When compiling with clang-cl for windows, it adds .asm files to the root
+ // which we need to delete so cargo doesn't get angry
+ if is_windows_target() && !use_msvc_asm() {
+ let _ = std::fs::remove_file("blake3_avx2_x86-64_windows_gnu.asm");
+ let _ = std::fs::remove_file("blake3_avx512_x86-64_windows_gnu.asm");
+ let _ = std::fs::remove_file("blake3_sse2_x86-64_windows_gnu.asm");
+ let _ = std::fs::remove_file("blake3_sse41_x86-64_windows_gnu.asm");
+ }
+
Ok(())
}