find local installed libmongoc first

pull/34/head
Matrix 7 years ago
parent 4c9cdbf8e1
commit a1fd155da3

@ -13,3 +13,6 @@ exclude = ["mongo-c-driver-*"]
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
openssl-sys = "0.9" openssl-sys = "0.9"
[build-dependencies]
semver = "0.8"

@ -1,81 +1,116 @@
extern crate semver;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::str::FromStr;
use semver::Version;
static VERSION: &'static str = "1.8.0"; // Should be the same major version as in the manifest static VERSION: &'static str = "1.8.0"; // Should be the same major version as in the manifest
fn main() { fn main() {
let out_dir_var = env::var("OUT_DIR").expect("No out dir"); let mut local_exist = false;
let out_dir = format!("{}/{}", out_dir_var, VERSION); if let Ok(output) = Command::new("pkg-config")
let driver_src_path = format!("mongo-c-driver-{}", VERSION); .arg("--modversion")
.arg("libmongoc-1.0")
.output()
{
if output.status.success() {
if let Ok(ref stdout) = String::from_utf8(output.stdout) {
if let Ok(local_version) = Version::from_str(stdout) {
let except_version = Version::from_str(VERSION).unwrap();
if local_version.major == except_version.major &&
local_version.minor == except_version.minor
{
local_exist = true;
}
}
}
}
}
if !local_exist {
let out_dir_var = env::var("OUT_DIR").expect("No out dir");
let out_dir = format!("{}/{}", out_dir_var, VERSION);
let driver_src_path = format!("mongo-c-driver-{}", VERSION);
let libmongoc_path = Path::new(&out_dir).join("lib/libmongoc-1.0.a"); let libmongoc_path = Path::new(&out_dir).join("lib/libmongoc-1.0.a");
if !libmongoc_path.exists() { if !libmongoc_path.exists() {
// Download and extract driver archive // Download and extract driver archive
let url = format!( let url = format!(
"https://github.com/mongodb/mongo-c-driver/releases/download/{}/mongo-c-driver-{}.tar.gz", "https://github.com/mongodb/mongo-c-driver/releases/download/{}/mongo-c-driver-{}.tar.gz",
VERSION, VERSION,
VERSION VERSION
); );
assert!(Command::new("curl").arg("-O") // Save to disk assert!(
Command::new("curl").arg("-O") // Save to disk
.arg("-L") // Follow redirects .arg("-L") // Follow redirects
.arg(url) .arg(url)
.status() .status()
.expect("Could not run curl") .expect("Could not run curl")
.success()); .success()
);
let archive_name = format!( let archive_name = format!("mongo-c-driver-{}.tar.gz", VERSION);
"mongo-c-driver-{}.tar.gz", assert!(
VERSION Command::new("tar")
); .arg("xzf")
assert!(Command::new("tar").arg("xzf") .arg(&archive_name)
.arg(&archive_name) .status()
.status() .expect("Could not run tar")
.expect("Could not run tar") .success()
.success()); );
// Configure and install // Configure and install
let mut command = Command::new("sh"); let mut command = Command::new("sh");
command.arg("configure"); command.arg("configure");
command.arg("--enable-ssl=openssl"); command.arg("--enable-ssl=openssl");
command.arg("--enable-sasl=no"); command.arg("--enable-sasl=no");
command.arg("--enable-static=yes"); command.arg("--enable-static=yes");
command.arg("--enable-shared=no"); command.arg("--enable-shared=no");
command.arg("--enable-shm-counters=no"); command.arg("--enable-shm-counters=no");
command.arg("--with-libbson=bundled"); command.arg("--with-libbson=bundled");
command.arg("--with-pic=yes"); command.arg("--with-pic=yes");
command.arg("--with-snappy=no"); command.arg("--with-snappy=no");
command.arg("--with-zlib=no"); command.arg("--with-zlib=no");
command.arg(format!("--prefix={}", &out_dir)); command.arg(format!("--prefix={}", &out_dir));
command.current_dir(&driver_src_path); command.current_dir(&driver_src_path);
// Enable debug symbols if configured for this profile
if env::var("DEBUG") == Ok("true".to_string()) {
command.arg("--enable-debug-symbols=yes");
}
// Use target that Cargo sets
if let Ok(target) = env::var("TARGET") {
command.arg(format!("--build={}", target));
}
assert!(command.status().expect("Could not run configure").success());
assert!(
Command::new("make")
.current_dir(&driver_src_path)
.env("CFLAGS", "-DMONGOC_TRACE")
.status()
.expect("Could not run make")
.success()
);
assert!(
Command::new("make")
.arg("install")
.current_dir(&driver_src_path)
.status()
.expect("Could not run make install")
.success()
);
// Enable debug symbols if configured for this profile
if env::var("DEBUG") == Ok("true".to_string()) {
command.arg("--enable-debug-symbols=yes");
}
// Use target that Cargo sets
if let Ok(target) = env::var("TARGET") {
command.arg(format!("--build={}", target));
} }
assert!(command.status().expect("Could not run configure").success()); // Output to Cargo
assert!(Command::new("make"). println!("cargo:rustc-link-search=native={}/lib", &out_dir);
current_dir(&driver_src_path). println!("cargo:rustc-link-lib=static=bson-1.0");
env("CFLAGS", "-DMONGOC_TRACE"). println!("cargo:rustc-link-lib=static=mongoc-1.0");
status().
expect("Could not run make").
success());
assert!(Command::new("make").
arg("install").
current_dir(&driver_src_path).
status().
expect("Could not run make install").
success());
} }
// Output to Cargo
println!("cargo:rustc-link-search=native={}/lib", &out_dir);
println!("cargo:rustc-link-lib=static=bson-1.0");
println!("cargo:rustc-link-lib=static=mongoc-1.0");
} }

Loading…
Cancel
Save