Skip to content
Myr

build.rs

This file

Path

build.rs

61 lines · 2.2 KiB · Rust

Last changed by

build: emit warning if git is missing during buildkevichi7before adoption00e501b

History of this file

Take it

1fn main() {2    set_git_revision_hash();3    set_windows_exe_options();4}56/// Embed a Windows manifest and set some linker options.7///8/// The main reason for this is to enable long path support on Windows. This9/// still, I believe, requires enabling long path support in the registry. But10/// if that's enabled, then this will let ripgrep use C:\... style paths that11/// are longer than 260 characters.12fn set_windows_exe_options() {13    static MANIFEST: &str = "pkg/windows/Manifest.xml";1415    let Ok(target_os) = std::env::var("CARGO_CFG_TARGET_OS") else { return };16    let Ok(target_env) = std::env::var("CARGO_CFG_TARGET_ENV") else { return };17    if !(target_os == "windows" && target_env == "msvc") {18        return;19    }2021    let Ok(mut manifest) = std::env::current_dir() else { return };22    manifest.push(MANIFEST);23    let Some(manifest) = manifest.to_str() else { return };2425    println!("cargo:rerun-if-changed={MANIFEST}");26    // Embed the Windows application manifest file.27    println!("cargo:rustc-link-arg-bin=rg=/MANIFEST:EMBED");28    println!("cargo:rustc-link-arg-bin=rg=/MANIFESTINPUT:{manifest}");29    // Turn linker warnings into errors. Helps debugging, otherwise the30    // warnings get squashed (I believe).31    println!("cargo:rustc-link-arg-bin=rg=/WX");32}3334/// Make the current git hash available to the build as the environment35/// variable `RIPGREP_BUILD_GIT_HASH`.36fn set_git_revision_hash() {37    use std::process::Command;3839    let args = &["rev-parse", "--short=10", "HEAD"];40    let output = Command::new("git").args(args).output();41    match output {42        Ok(output) => {43            let rev =44                String::from_utf8_lossy(&output.stdout).trim().to_string();45            if rev.is_empty() {46                println!(47                    "cargo:warning=output from `git rev-parse` is empty, \48                     so skipping embedding of commit hash"49                );50                return;51            }52            println!("cargo:rustc-env=RIPGREP_BUILD_GIT_HASH={rev}");53        }54        Err(e) => {55            println!(56                "cargo:warning=failed to run `git rev-parse`, \57                 so skipping embedding of commit hash: {e}"58            );59        }60    }61}