Skip to content
Myr

version.rs

This file

Path

crates/core/flags/doc/version.rs

173 lines · 5.4 KiB · Rust

Last changed by

style: apply rustfmtAndrew Gallantbefore adoptionbb8172f

History of this file

Take it

1/*!2Provides routines for generating version strings.34Version strings can be just the digits, an overall short one-line description5or something more verbose that includes things like CPU target feature support.6*/78use std::fmt::Write;910/// Generates just the numerical part of the version of ripgrep.11///12/// This includes the git revision hash.13pub(crate) fn generate_digits() -> String {14    let semver = option_env!("CARGO_PKG_VERSION").unwrap_or("N/A");15    match option_env!("RIPGREP_BUILD_GIT_HASH") {16        None => semver.to_string(),17        Some(hash) => format!("{semver} (rev {hash})"),18    }19}2021/// Generates a short version string of the form `ripgrep x.y.z`.22pub(crate) fn generate_short() -> String {23    let digits = generate_digits();24    format!("ripgrep {digits}")25}2627/// Generates a longer multi-line version string.28///29/// This includes not only the version of ripgrep but some other information30/// about its build. For example, SIMD support and PCRE2 support.31pub(crate) fn generate_long() -> String {32    let (compile, runtime) = (compile_cpu_features(), runtime_cpu_features());3334    let mut out = String::new();35    writeln!(out, "{}", generate_short()).unwrap();36    writeln!(out).unwrap();37    writeln!(out, "features:{}", features().join(",")).unwrap();38    if !compile.is_empty() {39        writeln!(out, "simd(compile):{}", compile.join(",")).unwrap();40    }41    if !runtime.is_empty() {42        writeln!(out, "simd(runtime):{}", runtime.join(",")).unwrap();43    }44    let (pcre2_version, _) = generate_pcre2();45    writeln!(out, "\n{pcre2_version}").unwrap();46    out47}4849/// Generates multi-line version string with PCRE2 information.50///51/// This also returns whether PCRE2 is actually available in this build of52/// ripgrep.53pub(crate) fn generate_pcre2() -> (String, bool) {54    let mut out = String::new();5556    #[cfg(feature = "pcre2")]57    {58        use grep::pcre2;5960        let (major, minor) = pcre2::version();61        write!(out, "PCRE2 {}.{} is available", major, minor).unwrap();62        if cfg!(target_pointer_width = "64") && pcre2::is_jit_available() {63            writeln!(out, " (JIT is available)").unwrap();64        } else {65            writeln!(out, " (JIT is unavailable)").unwrap();66        }67        (out, true)68    }6970    #[cfg(not(feature = "pcre2"))]71    {72        writeln!(out, "PCRE2 is not available in this build of ripgrep.")73            .unwrap();74        (out, false)75    }76}7778/// Returns the relevant SIMD features supported by the CPU at runtime.79///80/// This is kind of a dirty violation of abstraction, since it assumes81/// knowledge about what specific SIMD features are being used by various82/// components.83fn runtime_cpu_features() -> Vec<String> {84    #[cfg(target_arch = "x86_64")]85    {86        let mut features = vec![];8788        let sse2 = is_x86_feature_detected!("sse2");89        features.push(format!("{sign}SSE2", sign = sign(sse2)));9091        let ssse3 = is_x86_feature_detected!("ssse3");92        features.push(format!("{sign}SSSE3", sign = sign(ssse3)));9394        let avx2 = is_x86_feature_detected!("avx2");95        features.push(format!("{sign}AVX2", sign = sign(avx2)));9697        features98    }99    #[cfg(target_arch = "aarch64")]100    {101        let mut features = vec![];102103        // memchr and aho-corasick only use NEON when it is available at104        // compile time. This isn't strictly necessary, but NEON is supposed105        // to be available for all aarch64 targets. If this isn't true, please106        // file an issue at https://github.com/BurntSushi/memchr.107        let neon = cfg!(target_feature = "neon");108        features.push(format!("{sign}NEON", sign = sign(neon)));109110        features111    }112    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]113    {114        vec![]115    }116}117118/// Returns the SIMD features supported while compiling ripgrep.119///120/// In essence, any features listed here are required to run ripgrep correctly.121///122/// This is kind of a dirty violation of abstraction, since it assumes123/// knowledge about what specific SIMD features are being used by various124/// components.125///126/// An easy way to enable everything available on your current CPU is to127/// compile ripgrep with `RUSTFLAGS="-C target-cpu=native"`. But note that128/// the binary produced by this will not be portable.129fn compile_cpu_features() -> Vec<String> {130    #[cfg(target_arch = "x86_64")]131    {132        let mut features = vec![];133134        let sse2 = cfg!(target_feature = "sse2");135        features.push(format!("{sign}SSE2", sign = sign(sse2)));136137        let ssse3 = cfg!(target_feature = "ssse3");138        features.push(format!("{sign}SSSE3", sign = sign(ssse3)));139140        let avx2 = cfg!(target_feature = "avx2");141        features.push(format!("{sign}AVX2", sign = sign(avx2)));142143        features144    }145    #[cfg(target_arch = "aarch64")]146    {147        let mut features = vec![];148149        let neon = cfg!(target_feature = "neon");150        features.push(format!("{sign}NEON", sign = sign(neon)));151152        features153    }154    #[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]155    {156        vec![]157    }158}159160/// Returns a list of "features" supported (or not) by this build of ripgrpe.161fn features() -> Vec<String> {162    let mut features = vec![];163164    let pcre2 = cfg!(feature = "pcre2");165    features.push(format!("{sign}pcre2", sign = sign(pcre2)));166167    features168}169170/// Returns `+` when `enabled` is `true` and `-` otherwise.171fn sign(enabled: bool) -> &'static str {172    if enabled { "+" } else { "-" }173}