diff --git a/build.rs b/build.rs index 9440ab9..f45e3c7 100644 --- a/build.rs +++ b/build.rs @@ -3,36 +3,29 @@ use std::ffi::OsString; use std::process::Command; fn main() { - let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); - let output = Command::new(&rustc) - .arg("--version") - .output() - .unwrap() - .stdout; - let version = String::from_utf8(output).unwrap(); - - enable_simd_optimizations(&version); + let version = match Version::read() { + Ok(version) => version, + Err(err) => { + eprintln!("failed to parse `rustc --version`: {}", err); + return; + } + }; + enable_simd_optimizations(version); } -fn enable_simd_optimizations(version: &str) { +fn enable_simd_optimizations(version: Version) { // We don't activate SIMD optimizations these if we've explicitly disabled // them. Disabling auto optimizations is intended for use in tests, so that // we can reliably test fallback implementations. if env::var_os("CARGO_CFG_REGEX_DISABLE_AUTO_OPTIMIZATIONS").is_some() { return; } - let parsed = match Version::parse(&version) { - Ok(parsed) => parsed, - Err(err) => { - eprintln!("failed to parse `rustc --version`: {}", err); - return; - } - }; - let minimum = Version { major: 1, minor: 27, patch: 0 }; - if version.contains("nightly") || parsed >= minimum { - println!("cargo:rustc-cfg=regex_runtime_teddy_ssse3"); - println!("cargo:rustc-cfg=regex_runtime_teddy_avx2"); + if version < (Version { major: 1, minor: 27, patch: 0 }) { + return; } + + println!("cargo:rustc-cfg=regex_runtime_teddy_ssse3"); + println!("cargo:rustc-cfg=regex_runtime_teddy_avx2"); } #[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)] @@ -43,6 +36,16 @@ struct Version { } impl Version { + fn read() -> Result { + let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc")); + let output = Command::new(&rustc) + .arg("--version") + .output() + .unwrap() + .stdout; + Version::parse(&String::from_utf8(output).unwrap()) + } + fn parse(mut s: &str) -> Result { if !s.starts_with("rustc ") { return Err(format!("unrecognized version string: {}", s));