diff --git a/.gitignore b/.gitignore index 4fffb2f..2c96eb1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target -/Cargo.lock +target/ +Cargo.lock diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0dc08b1 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lmdb-sys/mdb"] + path = lmdb-sys/mdb + url = https://gitorious.org/mdb/mdb.git diff --git a/Cargo.toml b/Cargo.toml index fdfc0cf..ccfa844 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,19 @@ [package] name = "lmdb" -version = "0.1.0" +version = "0.2.0" authors = ["Dan Burkert "] license = "Apache-2.0" description = "Safe Rust bindings for LMDB" repository = "https://github.com/danburkert/lmdb-rs.git" readme = "README.md" -keywords = ["LMDB", "database", "storage-engine", "key-value-store", "bindings"] +keywords = ["LMDB", "database", "storage-engine", "key-value-store", "bindings", "library"] [lib] name = "lmdb" + +[dependencies.lmdb-sys] +path = "lmdb-sys" + diff --git a/README.md b/README.md index c563546..78efa90 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,7 @@ general, the API is very similar to the LMDB [C-API](http://symas.com/mdb/doc/). ## TODO +* [x] lmdb-sys. * [ ] Cursors. * [ ] Zero-copy put API. * [ ] Nested transactions. diff --git a/lmdb-sys/Cargo.toml b/lmdb-sys/Cargo.toml new file mode 100644 index 0000000..52b6cb6 --- /dev/null +++ b/lmdb-sys/Cargo.toml @@ -0,0 +1,16 @@ +[package] + +name = "lmdb-sys" +version = "0.2.0" +authors = ["Dan Burkert "] +license = "Apache-2.0" + +links = "lmdb" +build = "build.rs" + +[lib] + +name = "lmdb-sys" + +[build-dependencies.pkg-config] +pkg-config = "*" diff --git a/lmdb-sys/build.rs b/lmdb-sys/build.rs new file mode 100644 index 0000000..4742810 --- /dev/null +++ b/lmdb-sys/build.rs @@ -0,0 +1,49 @@ +extern crate "pkg-config" as pkg_config; + +use std::io::process::Command; +use std::os; + +/// Run a command and ensure a successful result. +fn run_cmd(cmd: &Command) { + assert!(cmd.status().unwrap().success(), + format!("Failed to execute command \"{}\"", cmd)) +} + +fn main() { + if pkg_config::find_library("liblmdb").is_ok() { return } + + let base_dir = Path::new(os::getenv("CARGO_MANIFEST_DIR").unwrap()); + let lmdb_dir = base_dir.join_many(&["mdb", "libraries", "liblmdb"]); + let dest_dir = Path::new(os::getenv("OUT_DIR").unwrap()); + + let mut cflags = os::getenv("CFLAGS").unwrap_or(String::new()); + let target = os::getenv("TARGET").unwrap(); + + if target.contains("i686") { + cflags.push_str(" -m32"); + } else if target.contains("x86_64") { + cflags.push_str(" -m64"); + } + if !target.contains("i686") { + cflags.push_str(" -fPIC"); + } + + let mut make = Command::new("make"); + make.arg("-C").arg(lmdb_dir.clone()); + + let mut make_build = make.clone(); + make_build.arg("liblmdb.a") + .arg(format!("XCFLAGS={}", cflags)); + + let mut make_clean = make.clone(); + make_clean.arg("clean"); + + run_cmd(&make_clean); + run_cmd(&make_build); + run_cmd(Command::new("cp") + .arg(lmdb_dir.join("liblmdb.a")) + .arg(dest_dir.clone())); + run_cmd(&make_clean); + + println!("cargo:rustc-flags=-L {} -l lmdb:static", dest_dir.display()); +} diff --git a/lmdb-sys/mdb b/lmdb-sys/mdb new file mode 160000 index 0000000..9a8eb95 --- /dev/null +++ b/lmdb-sys/mdb @@ -0,0 +1 @@ +Subproject commit 9a8eb95674c7b500cfe5f44d03493ff76c9fc0c1 diff --git a/src/ffi.rs b/lmdb-sys/src/lib.rs similarity index 97% rename from src/ffi.rs rename to lmdb-sys/src/lib.rs index f01f635..c80403a 100644 --- a/src/ffi.rs +++ b/lmdb-sys/src/lib.rs @@ -1,4 +1,7 @@ -#![allow(non_camel_case_types, dead_code)] +//! Provides extern declarations for `liblmdb`. Additionally, this crate provides `liblmdb` as a +//! native Cargo dependency. + +#![allow(non_camel_case_types)] extern crate libc; use libc::{c_int, c_uint, c_void, c_char, size_t}; @@ -123,7 +126,6 @@ pub const MDB_NOLOCK: c_uint = 0x400000; pub const MDB_NORDAHEAD: c_uint = 0x800000; pub const MDB_NOMEMINIT: c_uint = 0x1000000; -#[link(name = "lmdb")] extern { pub fn mdb_version(major: *mut c_int, minor: *mut c_int, patch: *mut c_int) -> *mut c_char; pub fn mdb_strerror(err: c_int) -> *mut c_char; diff --git a/src/lib.rs b/src/lib.rs index 1834f1e..bc59db2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ #[phase(plugin, link)] extern crate log; extern crate libc; extern crate sync; +extern crate "lmdb-sys" as ffi; pub use environment::{Environment, EnvironmentBuilder}; pub use error::{LmdbResult, LmdbError}; @@ -37,5 +38,4 @@ macro_rules! lmdb_try_with_cleanup { mod environment; mod error; mod transaction; -pub mod ffi; pub mod flags;