From c7441af7878fe5323e88cd4ec091c009710dac42 Mon Sep 17 00:00:00 2001 From: Reilly Brogan Date: Thu, 4 Jul 2024 13:35:30 -0500 Subject: [PATCH] Allow overriding of the libgit2 pkgconfig at build time It is often desirable to Linux distributions to de-bundle packages as much as possible so that system versions of libraries are used instead of statically compiled versions. This is problematic with libgit2-sys as the supported version range is so narrow (also a problem for most libgit2 bindings). A common solution to this is to have co-installable versions of libgit2 where the pkgconfig file is renamed (perhaps to something like `libgit-1.7` or similar). Support this in libgit2-sys so that distributions can override this at build time for arbitrary applications that may use the libgit2 bindings. Signed-off-by: Reilly Brogan --- libgit2-sys/build.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/libgit2-sys/build.rs b/libgit2-sys/build.rs index 1abf628c6e..0010f18037 100644 --- a/libgit2-sys/build.rs +++ b/libgit2-sys/build.rs @@ -6,8 +6,19 @@ use std::process::Command; /// Tries to use system libgit2 and emits necessary build script instructions. fn try_system_libgit2() -> Result { + // Specify `LIBGIT2_OVERRIDE_PKGCONFIG_NAME` to change the name of the pkgconfig configuration + // that will be looked for when checking to see if the system libgit2 is usable. This is + // most useful for distributions that may ship co-installable versions of libgit2 where + // the pkgconfig may be renamed. + println!("cargo:rerun-if-env-changed=LIBGIT2_OVERRIDE_PKGCONFIG_NAME"); + let libgit2_pkgconfig = + env::var("LIBGIT2_OVERRIDE_PKGCONFIG_NAME").unwrap_or("libgit2".to_string()); + let mut cfg = pkg_config::Config::new(); - match cfg.range_version("1.8.1".."1.9.0").probe("libgit2") { + match cfg + .range_version("1.8.1".."1.9.0") + .probe(&libgit2_pkgconfig) + { Ok(lib) => { for include in &lib.include_paths { println!("cargo:root={}", include.display());