Skip to content

Commit 3737cf1

Browse files
committed
Add a LIBGIT2_NO_VENDOR environment variable to build.rs
1 parent 3df7e81 commit 3737cf1

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

libgit2-sys/build.rs

+46-19
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,65 @@
11
use std::env;
2+
use std::ffi::OsString;
23
use std::fs;
34
use std::io;
45
use std::path::{Path, PathBuf};
56
use std::process::Command;
67

8+
fn env_var(name: &str) -> Option<OsString> {
9+
let var = env::var_os(name);
10+
println!("cargo:rerun-if-env-changed={}", name);
11+
var
12+
}
13+
14+
fn use_system_libgit2() -> Result<(), ()> {
15+
let mut cfg = pkg_config::Config::new();
16+
// These version ranges specifically request a version that includes
17+
// the SSH fixes for CVE-2023-22742 (1.5.1+ or 1.4.5+).
18+
if let Ok(lib) = cfg
19+
.range_version("1.5.1".."1.6.0")
20+
.probe("libgit2")
21+
.or_else(|_| cfg.range_version("1.4.5".."1.5.0").probe("libgit2"))
22+
{
23+
for include in &lib.include_paths {
24+
println!("cargo:root={}", include.display());
25+
}
26+
Ok(())
27+
} else {
28+
Err(())
29+
}
30+
}
31+
732
fn main() {
8-
let https = env::var("CARGO_FEATURE_HTTPS").is_ok();
9-
let ssh = env::var("CARGO_FEATURE_SSH").is_ok();
10-
let vendored = env::var("CARGO_FEATURE_VENDORED").is_ok();
11-
let zlib_ng_compat = env::var("CARGO_FEATURE_ZLIB_NG_COMPAT").is_ok();
33+
let https = env_var("CARGO_FEATURE_HTTPS").is_some();
34+
let ssh = env_var("CARGO_FEATURE_SSH").is_some();
35+
let vendored = env_var("CARGO_FEATURE_VENDORED").is_some();
36+
let zlib_ng_compat = env_var("CARGO_FEATURE_ZLIB_NG_COMPAT").is_some();
37+
let no_vendor = env_var("LIBGIT2_NO_VENDOR").map_or(false, |s| s != "0");
38+
39+
if no_vendor {
40+
if use_system_libgit2().is_err() {
41+
panic!("
42+
43+
The environment variable LIBGIT2_NO_VENDOR has been set but no compatible system libgit2 could be found.
44+
The build is now aborting. To disable, unset the variable or use `LIBGIT2_NO_VENDOR=0`.
45+
46+
");
47+
}
48+
return;
49+
}
1250

1351
// To use zlib-ng in zlib-compat mode, we have to build libgit2 ourselves.
1452
let try_to_use_system_libgit2 = !vendored && !zlib_ng_compat;
15-
if try_to_use_system_libgit2 {
16-
let mut cfg = pkg_config::Config::new();
17-
// These version ranges specifically request a version that includes
18-
// the SSH fixes for CVE-2023-22742 (1.5.1+ or 1.4.5+).
19-
if let Ok(lib) = cfg
20-
.range_version("1.5.1".."1.6.0")
21-
.probe("libgit2")
22-
.or_else(|_| cfg.range_version("1.4.5".."1.5.0").probe("libgit2"))
23-
{
24-
for include in &lib.include_paths {
25-
println!("cargo:root={}", include.display());
26-
}
27-
return;
28-
}
53+
if try_to_use_system_libgit2 && use_system_libgit2().is_ok() {
54+
// using system libgit2 has worked
55+
return;
2956
}
3057

3158
println!("cargo:rustc-cfg=libgit2_vendored");
3259

3360
if !Path::new("libgit2/src").exists() {
3461
let _ = Command::new("git")
35-
.args(&["submodule", "update", "--init", "libgit2"])
62+
.args(["submodule", "update", "--init", "libgit2"])
3663
.status();
3764
}
3865

0 commit comments

Comments
 (0)