Skip to content

Commit e4ad650

Browse files
committed
Add a LIBGIT2_NO_VENDOR environment variable to build.rs
1 parent d6d3c0b commit e4ad650

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

libgit2-sys/build.rs

+40-13
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,59 @@
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+
if let Ok(lib) = cfg.range_version("1.6.4".."1.7.0").probe("libgit2") {
17+
for include in &lib.include_paths {
18+
println!("cargo:root={}", include.display());
19+
}
20+
Ok(())
21+
} else {
22+
Err(())
23+
}
24+
}
25+
726
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();
27+
let https = env_var("CARGO_FEATURE_HTTPS").is_some();
28+
let ssh = env_var("CARGO_FEATURE_SSH").is_some();
29+
let vendored = env_var("CARGO_FEATURE_VENDORED").is_some();
30+
let zlib_ng_compat = env_var("CARGO_FEATURE_ZLIB_NG_COMPAT").is_some();
31+
let no_vendor = env_var("LIBGIT2_NO_VENDOR").map_or(false, |s| s != "0");
32+
33+
if no_vendor {
34+
if use_system_libgit2().is_err() {
35+
panic!("
36+
37+
The environment variable LIBGIT2_NO_VENDOR has been set but no compatible system libgit2 could be found.
38+
The build is now aborting. To disable, unset the variable or use `LIBGIT2_NO_VENDOR=0`.
39+
40+
");
41+
}
42+
return;
43+
}
1244

1345
// To use zlib-ng in zlib-compat mode, we have to build libgit2 ourselves.
1446
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-
if let Ok(lib) = cfg.range_version("1.6.4".."1.7.0").probe("libgit2") {
18-
for include in &lib.include_paths {
19-
println!("cargo:root={}", include.display());
20-
}
21-
return;
22-
}
47+
if try_to_use_system_libgit2 && use_system_libgit2().is_ok() {
48+
// using system libgit2 has worked
49+
return;
2350
}
2451

2552
println!("cargo:rustc-cfg=libgit2_vendored");
2653

2754
if !Path::new("libgit2/src").exists() {
2855
let _ = Command::new("git")
29-
.args(&["submodule", "update", "--init", "libgit2"])
56+
.args(["submodule", "update", "--init", "libgit2"])
3057
.status();
3158
}
3259

0 commit comments

Comments
 (0)