From 8e9751271cd3923e231dc8ebe42ba8a8f7c1cdad Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Mon, 25 Jan 2021 19:15:21 +0100 Subject: [PATCH 1/2] Upgrade mongoc to 1.17 --- .gitignore | 1 - .semaphore/docker-compose.yml | 2 - .semaphore/semaphore.yml | 20 +---- Cargo.toml | 2 +- README.md | 3 +- mongoc-sys/Cargo.toml | 10 +-- mongoc-sys/build.rs | 142 +++++++++++----------------------- mongoc-sys/src/lib.rs | 8 ++ src/bsonc.rs | 21 +++-- tests/collection.rs | 1 - tests/cursor.rs | 1 - tests/database.rs | 1 - 12 files changed, 78 insertions(+), 134 deletions(-) diff --git a/.gitignore b/.gitignore index eac3509..88e89d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ target Cargo.lock -mongoc-sys/mongo-c-driver* ssl_env_vars .idea/ .DS_Store diff --git a/.semaphore/docker-compose.yml b/.semaphore/docker-compose.yml index 2b7e102..c0be2f7 100644 --- a/.semaphore/docker-compose.yml +++ b/.semaphore/docker-compose.yml @@ -14,10 +14,8 @@ services: depends_on: - mongodb environment: - CARGO_HOME: "/tmp/_cargo" RUST_TEST_THREADS: "1" RUST_BACKTRACE: "full" - LIBCLANG_PATH: "/usr/lib/llvm-7/lib" SKIP_SSL_CONNECTION_TESTS: "true" MONGODB_CONNECTION_STRING: "mongodb://mongodb:27017" working_dir: /project diff --git a/.semaphore/semaphore.yml b/.semaphore/semaphore.yml index 8bd2376..def8321 100644 --- a/.semaphore/semaphore.yml +++ b/.semaphore/semaphore.yml @@ -31,28 +31,14 @@ blocks: commands: # Create containers but don't start them yet. We first want to copy the caches onto it - docker-compose $COMPOSE_OPTIONS up --no-start - # Copy cargo cache to main test container - - cache restore v3-cargo-$(checksum Cargo.lock),v3-cargo - - "[ -d \"/tmp/_cargo\" ] && docker cp /tmp/_cargo $CONTAINER_NAME:/tmp/_cargo || echo 'No cache to copy'" # Copy project to main test container - - cache restore v3-project-$(cat PACKAGES_CHECKSUM),v3-project - docker cp . $CONTAINER_NAME:/project - # Then start the containers so we can run the test on it - docker-compose $COMPOSE_OPTIONS up -d --no-recreate + # Install dependencies + - docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME apt-get update + - docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME apt-get install -y cmake libssl-dev jobs: - name: Test commands: - docker-compose $COMPOSE_OPTIONS exec $CONTAINER_NAME cargo test - epilogue: - always: - commands: - # Pull artifacts from containers and cache them - - rm -rf target - - docker cp $CONTAINER_NAME:/project/target target - - cache store v3-project-$(cat PACKAGES_CHECKSUM) target - - # Pull cargo dir and cache it - - rm -rf /tmp/_cargo - - docker cp $CONTAINER_NAME:/tmp/_cargo /tmp/_cargo - - cache store v3-cargo-$(checksum Cargo.lock) /tmp/_cargo diff --git a/Cargo.toml b/Cargo.toml index f2031cf..53462f2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ serde_derive = "1.0" [dependencies.mongoc-sys] path = "mongoc-sys" -version = "1.8.2-1" +version = "1.17.4" [dev-dependencies] chrono = "^0.4" diff --git a/README.md b/README.md index c8da680..35811b3 100644 --- a/README.md +++ b/README.md @@ -21,10 +21,11 @@ The driver currently only builds on Unix, tested on Mac Os X and Linux so far. I If you have any trouble installing the crate (linking openssl can be tricky) please check out the [installation instructions for the C driver](http://mongoc.org/libmongoc/current/installing.html). -To build on Mac install OpenSSL 1.1: +To build on Mac install OpenSSL 1.1 and cmake: ``` brew install openssl@1.1 +brew install cmake ``` Export these env vars the before you make a clean build: diff --git a/mongoc-sys/Cargo.toml b/mongoc-sys/Cargo.toml index 4772675..7a312ac 100644 --- a/mongoc-sys/Cargo.toml +++ b/mongoc-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mongoc-sys" -version = "1.8.2-1" +version = "1.17.4" description = "Sys package with installer and bindings for mongoc" authors = ["Thijs Cadier "] build = "build.rs" @@ -8,12 +8,10 @@ repository = "https://github.com/thijsc/mongo-rust-driver" links = "mongoc" license = "MIT/Apache-2.0" categories = ["external-ffi-bindings"] -exclude = ["mongo-c-driver-*"] [dependencies] -libc = "0.2" -openssl-sys = "0.9" +libc = "^0.2" +openssl-sys = "^0.9" [build-dependencies] -pkg-config = "0.3" -vcpkg = "0.2.6" \ No newline at end of file +pkg-config = "^0.3" diff --git a/mongoc-sys/build.rs b/mongoc-sys/build.rs index c336a26..daa2356 100644 --- a/mongoc-sys/build.rs +++ b/mongoc-sys/build.rs @@ -1,13 +1,16 @@ extern crate pkg_config; -#[cfg(target_env = "msvc")] -extern crate vcpkg; use std::env; +use std::path::Path; +use std::process::Command; -#[cfg(not(target_env = "msvc"))] -fn lin(mongoc_version: &str) { - use std::path::Path; - use std::process::Command; +fn main() { + let mongoc_version = env!("CARGO_PKG_VERSION") + .split('-') + .next() + .expect("Crate version is not valid"); + + pkg_config::Config::new().probe("zlib").expect("Cannot find zlib"); if pkg_config::Config::new() .atleast_version(mongoc_version) @@ -16,10 +19,10 @@ fn lin(mongoc_version: &str) { .is_err() { let out_dir_var = env::var("OUT_DIR").expect("No out dir"); - let out_dir = format!("{}/{}", out_dir_var, mongoc_version); - let driver_src_path = format!("mongo-c-driver-{}", mongoc_version); + let out_dir = Path::new(&out_dir_var); + let driver_src_path = out_dir.join(format!("mongo-c-driver-{}", mongoc_version)); - let libmongoc_path = Path::new(&out_dir).join("lib/libmongoc-1.0.a"); + let libmongoc_path = out_dir.join("usr/local/lib/libmongoc-static-1.0.a"); if !libmongoc_path.exists() { // Download and extract driver archive let url = format!( @@ -29,6 +32,7 @@ fn lin(mongoc_version: &str) { ); assert!( Command::new("curl").arg("-O") // Save to disk + .current_dir(out_dir) .arg("-L") // Follow redirects .arg(url) .status() @@ -39,6 +43,7 @@ fn lin(mongoc_version: &str) { let archive_name = format!("mongo-c-driver-{}.tar.gz", mongoc_version); assert!( Command::new("tar") + .current_dir(out_dir) .arg("xzf") .arg(&archive_name) .status() @@ -46,97 +51,38 @@ fn lin(mongoc_version: &str) { .success() ); - // Configure and install - let mut command = Command::new("sh"); - command.arg("configure"); - command.arg("--enable-ssl=openssl"); - command.arg("--enable-sasl=no"); - command.arg("--enable-static=yes"); - command.arg("--enable-shared=no"); - command.arg("--enable-shm-counters=no"); - command.arg("--with-libbson=bundled"); - command.arg("--with-pic=yes"); - command.arg("--with-snappy=no"); - command.arg("--with-zlib=no"); - command.arg(format!("--prefix={}", &out_dir)); - 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() - ); + // Set up cmake command + let mut cmake = Command::new("cmake"); + cmake.current_dir(&driver_src_path); + + cmake.arg("-DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF"); + cmake.arg("-DENABLE_SSL=OPENSSL"); + cmake.arg("-DENABLE_SASL=OFF"); + cmake.arg("-DENABLE_STATIC=OFF"); + cmake.arg("-DENABLE_TESTS=OFF"); + cmake.arg("-DWITH_PIC=ON"); + cmake.arg("-DWITH_SNAPPY=OFF"); + cmake.arg("-DWITH_ZLIB=ON"); + + // Run in current dir + cmake.arg("."); + + // Run cmake command + assert!(cmake.status().expect("Could not run cmake").success()); + + // Set up make install command + let mut make = Command::new("make"); + make.current_dir(&driver_src_path); + make.arg(format!("DESTDIR={}", out_dir.to_string_lossy())); + make.arg("install"); + + // Run make command + assert!(make.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"); - } -} - -#[cfg(target_env = "msvc")] -fn win(_mongoc_version: &str) { - use vcpkg; - - let mongo_lib = "mongoc-1.0"; - let bson_lib = "bson-1.0"; - - if vcpkg::Config::new() - .emit_includes(true) - .probe("mongo-c-driver") - .is_ok() - { - // Output to Cargo - println!("cargo:rustc-link-lib={}", bson_lib); - println!("cargo:rustc-link-lib={}", mongo_lib); - } else { - if let Ok(bson_dir_lib) = env::var("MONGO_LIB") { - if let Ok(mongo_dir_lib) = env::var("BSON_LIB") { - println!("cargo:rustc-link-search=native={}", bson_dir_lib); - println!("cargo:rustc-link-lib=dylib={}", bson_lib); - println!("cargo:rustc-link-search=native={}", mongo_dir_lib); - println!("cargo:rustc-link-lib=dylib={}", mongo_lib); - - } else { - panic!("please define BSON_LIB to {}.lib, \n for example set BSON_LIB=C:\\vcpkg\\packages\\libbson_x64-windows\\lib", bson_lib); - } - } else { - panic!("please define MONGO_LIB to {}.lib, \n for example set MONGO_LIB=C:\\vcpkg\\packages\\mongo-c-driver_x64-windows\\lib", mongo_lib); + println!("cargo:rustc-link-search=native={}/usr/local/lib", &out_dir.to_string_lossy()); + println!("cargo:rustc-link-lib=bson-1.0"); + println!("cargo:rustc-link-lib=mongoc-1.0"); } - } -} - -fn main() { - let mongoc_version = env!("CARGO_PKG_VERSION") - .split('-') - .next() - .expect("Crate version is not valid"); - - #[cfg(target_env = "msvc")] - win(mongoc_version); - #[cfg(not(target_env = "msvc"))] - lin(mongoc_version); } diff --git a/mongoc-sys/src/lib.rs b/mongoc-sys/src/lib.rs index 7ee2495..f13fac0 100644 --- a/mongoc-sys/src/lib.rs +++ b/mongoc-sys/src/lib.rs @@ -254,3 +254,11 @@ pub mod bindings { pub const MONGOC_ERROR_WRITE_CONCERN_ERROR: ::libc::c_uint = 64; pub const MONGOC_ERROR_DUPLICATE_KEY: ::libc::c_uint = 11000; } + +#[cfg(test)] +mod tests { + #[test] + fn run() { + // Just a quick test to make seeing if everything links right easy + } +} diff --git a/src/bsonc.rs b/src/bsonc.rs index db99ae9..3dc00b7 100644 --- a/src/bsonc.rs +++ b/src/bsonc.rs @@ -10,7 +10,8 @@ use bson; use super::Result; pub struct Bsonc { - inner: *mut bindings::bson_t + inner: *mut bindings::bson_t, + destroy_inner_on_drop: bool } impl Bsonc { @@ -18,9 +19,14 @@ impl Bsonc { Bsonc::from_ptr(unsafe { bindings::bson_new() }) } + /// Create a bsonc from a raw pointer. Does not run cleanup + /// logic on drop in this case. pub fn from_ptr(inner: *const bindings::bson_t) -> Bsonc { assert!(!inner.is_null()); - Bsonc { inner: inner as *mut bindings::bson_t } + Bsonc { + inner: inner as *mut bindings::bson_t, + destroy_inner_on_drop: false + } } pub fn from_document(document: &bson::Document) -> Result { @@ -39,7 +45,10 @@ impl Bsonc { // See: http://mongoc.org/libbson/current/bson_new_from_data.html assert!(!inner.is_null()); - Ok(Bsonc{ inner: inner }) + Ok(Bsonc{ + inner: inner, + destroy_inner_on_drop: true + }) } /// Decode a bson from the C side to a document @@ -92,8 +101,10 @@ impl fmt::Debug for Bsonc { impl Drop for Bsonc { fn drop(&mut self) { - unsafe { - bindings::bson_destroy(self.inner); + if self.destroy_inner_on_drop { + unsafe { + bindings::bson_destroy(self.inner); + } } } } diff --git a/tests/collection.rs b/tests/collection.rs index 55f34c0..96a8eb5 100644 --- a/tests/collection.rs +++ b/tests/collection.rs @@ -38,7 +38,6 @@ fn test_aggregate() { assert_eq!(Ok(5), total.get_i32("total")); } -#[cfg_attr(target_os = "windows", ignore)] #[test] fn test_command() { let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); diff --git a/tests/cursor.rs b/tests/cursor.rs index 5ba6139..426b31f 100644 --- a/tests/cursor.rs +++ b/tests/cursor.rs @@ -93,7 +93,6 @@ fn test_tailing_cursor() { assert_eq!(25, guard.join().expect("Thread failed")); } -#[cfg_attr(target_os = "windows", ignore)] #[test] fn test_batch_cursor() { let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); diff --git a/tests/database.rs b/tests/database.rs index 76757bc..970cadb 100644 --- a/tests/database.rs +++ b/tests/database.rs @@ -7,7 +7,6 @@ use bson::doc; use mongo_driver::client::{ClientPool,Uri}; -#[cfg_attr(target_os = "windows", ignore)] #[test] fn test_command() { let uri = Uri::new(helpers::mongodb_test_connection_string()).unwrap(); From 7a2baf438b59c27365f6da074858493a78a4c01f Mon Sep 17 00:00:00 2001 From: Thijs Cadier Date: Fri, 19 Feb 2021 11:25:24 +0100 Subject: [PATCH 2/2] Use latest mongodb in tests --- .semaphore/docker-compose.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.semaphore/docker-compose.yml b/.semaphore/docker-compose.yml index c0be2f7..4e7e587 100644 --- a/.semaphore/docker-compose.yml +++ b/.semaphore/docker-compose.yml @@ -2,12 +2,11 @@ version: '3.7' services: mongodb: - image: mongo:3.6 + image: mongo:latest environment: - MONGO_DATA_DIR=/data/db ports: - "27017:27017" - command: mongod --smallfiles rust: image: rust:latest container_name: "rust"