Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#![feature(path, io, env, core)]
extern crate "pkg-config" as pkg_config;
use std::env;
use std::old_io::{self, fs, Command};
use std::old_io::process::InheritFd;
fn main() {
let (build, mut cargo) = crater::current();
register_dep(&build, "SSH2");
register_dep(&build, "OPENSSL");
let mut opts = pkg_config::default_options("libgit2");
opts.atleast_version = Some("0.22.0".to_string());
match pkg_config::find_library_opts("libgit2", &opts) {
Ok(()) => return,
Err(..) => {}
}
let mut cflags = env::var_string("CFLAGS").unwrap_or(String::new());
cflags.push_str(" -ffunction-sections -fdata-sections");
if build.is_32bit() {
cflags.push_str(" -m32");
} else if build.is_64bit() {
cflags.push_str(" -m64");
}
if !build.is_32bit() {
cflags.push_str(" -fPIC");
}
let dst = build.out_dir();
let _ = fs::mkdir(&dst.join("build"), old_io::USER_DIR);
let mut cmd = Command::new("cmake");
cmd.arg(build.root_dir().join("libgit2"))
.cwd(&dst.join("build"));
if build.target.is_mingw() {
cmd.arg("-G").arg("Unix Makefiles");
}
let profile = match env::var_string("PROFILE").unwrap().as_slice() {
"bench" | "release" => "Release",
_ => "Debug",
};
run(cmd.arg("-DTHREADSAFE=ON")
.arg("-DBUILD_SHARED_LIBS=OFF")
.arg("-DBUILD_CLAR=OFF")
.arg(format!("-DCMAKE_BUILD_TYPE={}", profile))
.arg(format!("-DCMAKE_INSTALL_PREFIX={}", dst.display()))
.arg("-DBUILD_EXAMPLES=OFF")
.arg(format!("-DCMAKE_C_FLAGS={}", cflags)));
run(Command::new("cmake")
.arg("--build").arg(".")
.arg("--target").arg("install")
.cwd(&dst.join("build")));
cargo.set_root_dir(&dst);
if build.target.is_windows() {
cargo.link_library("winhttp");
cargo.link_library("rpcrt4");
cargo.link_library("ole32");
cargo.link_library("ws2_32");
cargo.link_library("bcrypt");
cargo.link_library("crypt32");
cargo.link_static_library("git2");
cargo.add_library_dir("git2", &dst.join("lib"));
} else if !build.is_cross_compilation() {
opts.statik = true;
opts.atleast_version = None;
append("PKG_CONFIG_PATH", dst.join("lib/pkgconfig"));
pkg_config::find_library_opts("libgit2", &opts).unwrap();
} else {
cargo.link_static_library("git2");
cargo.add_library_dir("git2", &dst.join("lib"));
if build.target.is_os_x() {
cargo.link_library("iconv");
}
}
}
fn run(cmd: &mut Command) {
println!("running: {:?}", cmd);
assert!(cmd.stdout(InheritFd(1))
.stderr(InheritFd(2))
.status()
.unwrap()
.success());
}
fn register_dep(build: &crater::BuildInput, dep: &str) {
match cargo.dependency(dep).unwrap().root_dir() {
Some(s) => {
append("CMAKE_PREFIX_PATH", s.as_slice());
append("PKG_CONFIG_PATH", s.join("lib/pkgconfig");
}
_ => {}
}
}
fn append(var: &str, val: Path) {
let prefix = env::var_string(var).unwrap_or(String::new());
let val = env::join_paths(env::split_paths(&prefix)
.chain(Some(val).into_iter())).unwrap();
env::set_var(var, &val);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.