From b9b1d485457f3ed1b113537e79448df9c56d5d25 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Sat, 11 Jan 2020 10:40:44 -0500 Subject: avoid using MSVC-style flags with the GNU toolchain on Windows --- build.rs | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) (limited to 'build.rs') diff --git a/build.rs b/build.rs index 3611258..14c054c 100644 --- a/build.rs +++ b/build.rs @@ -4,33 +4,33 @@ fn defined(var: &str) -> bool { env::var_os(var).is_some() } -fn target_arch() -> String { +fn target_components() -> Vec { let target = env::var("TARGET").unwrap(); - let target_components: Vec<&str> = target.split("-").collect(); - target_components[0].to_string() -} - -fn target_os() -> String { - let target = env::var("TARGET").unwrap(); - let target_components: Vec<&str> = target.split("-").collect(); - target_components[2].to_string() -} - -fn is_windows() -> bool { - target_os() == "windows" + target.split("-").map(|s| s.to_string()).collect() } fn is_x86_64() -> bool { - target_arch() == "x86_64" + target_components()[0] == "x86_64" } fn is_armv7() -> bool { - target_arch() == "armv7" + target_components()[0] == "armv7" +} + +// Windows targets may be using the MSVC toolchain or the GNU toolchain. The +// right compiler flags to use depend on the toolchain. (And we don't want to +// use flag_if_supported, because we don't want features to be silently +// disabled by old compilers.) +fn is_windows_msvc() -> bool { + // Some targets are only two components long, so check in steps. + target_components()[1] == "pc" + && target_components()[2] == "windows" + && target_components()[3] == "msvc" } fn new_build() -> cc::Build { let mut build = cc::Build::new(); - if !is_windows() { + if !is_windows_msvc() { build.flag("-std=c11"); } build @@ -45,7 +45,7 @@ fn main() -> Result<(), Box> { if defined("CARGO_FEATURE_C_AVX512") && is_x86_64() { let mut build = new_build(); build.file("c/blake3_avx512.c"); - if is_windows() { + if is_windows_msvc() { // Note that a lot of versions of MSVC don't support /arch:AVX512, // and they'll discard it with a warning, hopefully leading to a // build error. -- cgit v1.2.3