trying to build for windows

pull/47/head
inv2004 6 years ago
parent 1539d8f3b8
commit c23b3798e8

@ -16,3 +16,4 @@ openssl-sys = "0.9"
[build-dependencies] [build-dependencies]
pkg-config = "0.3" pkg-config = "0.3"
vcpkg = "0.2.6"

@ -1,100 +1,130 @@
extern crate pkg_config; extern crate pkg_config;
#[cfg(target_env = "msvc")]
extern crate vcpkg;
use std::env; use std::env;
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
fn main() { #[cfg(not(target_env = "msvc"))]
let mongoc_version = env!("CARGO_PKG_VERSION") fn main_linux(mongoc_version: &str) {
.split('-')
.next()
.expect("Crate version is not valid");
if pkg_config::Config::new() if pkg_config::Config::new()
.atleast_version(mongoc_version) .atleast_version(mongoc_version)
.statik(true) .statik(true)
.probe("libmongoc-1.0") .probe("libmongoc-1.0")
.is_err() .is_err()
{ {
let out_dir_var = env::var("OUT_DIR").expect("No out dir"); let out_dir_var = env::var("OUT_DIR").expect("No out dir");
let out_dir = format!("{}/{}", out_dir_var, mongoc_version); let out_dir = format!("{}/{}", out_dir_var, mongoc_version);
let driver_src_path = format!("mongo-c-driver-{}", mongoc_version); let driver_src_path = format!("mongo-c-driver-{}", mongoc_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",
mongoc_version, mongoc_version,
mongoc_version mongoc_version
); );
assert!( assert!(
Command::new("curl").arg("-O") // Save to disk 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!("mongo-c-driver-{}.tar.gz", mongoc_version); let archive_name = format!("mongo-c-driver-{}.tar.gz", mongoc_version);
assert!( assert!(
Command::new("tar") Command::new("tar")
.arg("xzf") .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 // Enable debug symbols if configured for this profile
if env::var("DEBUG") == Ok("true".to_string()) { if env::var("DEBUG") == Ok("true".to_string()) {
command.arg("--enable-debug-symbols=yes"); command.arg("--enable-debug-symbols=yes");
} }
// Use target that Cargo sets // Use target that Cargo sets
if let Ok(target) = env::var("TARGET") { if let Ok(target) = env::var("TARGET") {
command.arg(format!("--build={}", 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()
);
} }
assert!(command.status().expect("Could not run configure").success()); // Output to Cargo
assert!( println!("cargo:rustc-link-search=native={}/lib", &out_dir);
Command::new("make") println!("cargo:rustc-link-lib=static=bson-1.0");
.current_dir(&driver_src_path) println!("cargo:rustc-link-lib=static=mongoc-1.0");
.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()
);
} }
}
#[cfg(target_env = "msvc")]
fn main_win(mongoc_version: &str) {
use vcpkg;
if vcpkg::Config::new()
.emit_includes(true)
.probe("mongo-c-driver")
.is_ok()
{
let out_dir_var = env::var("OUT_DIR").expect("No out dir");
let out_dir = format!("{}/{}", out_dir_var, mongoc_version);
// println!("found");
// Output to Cargo // Output to Cargo
println!("cargo:rustc-link-search=native={}/lib", &out_dir); println!("cargo:rustc-link-search=native={}/lib", &out_dir);
println!("cargo:rustc-link-lib=static=bson-1.0"); println!("cargo:rustc-link-lib=bson");
println!("cargo:rustc-link-lib=static=mongoc-1.0"); println!("cargo:rustc-link-lib=mongo-c-driver");
} }
} }
fn main() {
let mongoc_version = env!("CARGO_PKG_VERSION")
.split('-')
.next()
.expect("Crate version is not valid");
#[cfg(target_env = "msvc")]
main_win(mongoc_version);
#[cfg(not(target_env = "msvc"))]
main_linux(mongoc_version);
}

@ -30,7 +30,7 @@ impl Bsonc {
let inner = unsafe { let inner = unsafe {
bindings::bson_new_from_data( bindings::bson_new_from_data(
buffer[..].as_ptr(), buffer[..].as_ptr(),
buffer.len() as u64 buffer.len() as u32
) )
}; };

Loading…
Cancel
Save