From 951ca804e234569619ac88f85e43359ed50c2f11 Mon Sep 17 00:00:00 2001 From: Dan Burkert Date: Thu, 8 Jan 2015 23:07:44 -0800 Subject: [PATCH] Execute bindgen in build.rs --- lmdb-sys/Cargo.toml | 2 +- lmdb-sys/build.rs | 19 ++++++++++++++++--- lmdb-sys/src/lib.rs | 13 ++++++++----- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/lmdb-sys/Cargo.toml b/lmdb-sys/Cargo.toml index 57c1a8c..fc35cf4 100644 --- a/lmdb-sys/Cargo.toml +++ b/lmdb-sys/Cargo.toml @@ -12,7 +12,7 @@ build = "build.rs" name = "lmdb-sys" -[dependencies.rust-bindgen] +[build-dependencies.rust-bindgen] git = "https://github.com/crabtw/rust-bindgen.git" [build-dependencies.pkg-config] diff --git a/lmdb-sys/build.rs b/lmdb-sys/build.rs index dee22ab..9818abb 100644 --- a/lmdb-sys/build.rs +++ b/lmdb-sys/build.rs @@ -1,18 +1,31 @@ extern crate "pkg-config" as pkg_config; +extern crate bindgen; extern crate gcc; +use bindgen::{Bindings, BindgenOptions, LinkType}; use std::default::Default; +use std::io::fs; use std::os; fn main() { - if !pkg_config::find_library("liblmdb").is_ok() { - let mdb = Path::new(os::getenv("CARGO_MANIFEST_DIR").unwrap()) - .join_many(&["mdb", "libraries", "liblmdb"]); + let mdb = Path::new(os::getenv("CARGO_MANIFEST_DIR").unwrap()) + .join_many(&["mdb", "libraries", "liblmdb"]); + if !pkg_config::find_library("liblmdb").is_ok() { gcc::compile_library("liblmdb.a", &Default::default(), &[mdb.join("mdb.c").as_str().unwrap(), mdb.join("midl.c").as_str().unwrap()]); } + + let mut bindgen_opts: BindgenOptions = Default::default(); + bindgen_opts.clang_args.push(mdb.join("lmdb.h").as_str().unwrap().to_string()); + bindgen_opts.links.push(("lmdb".to_string(), LinkType::Default)); + bindgen_opts.builtins = true; + + let bindings: Bindings = Bindings::generate(&bindgen_opts, None, None).unwrap(); + let mut dst = fs::File::create(&Path::new(os::getenv("OUT_DIR").unwrap()) + .join("lmdb.rs")).unwrap(); + bindings.write(&mut dst).unwrap() } diff --git a/lmdb-sys/src/lib.rs b/lmdb-sys/src/lib.rs index 77a6135..881be80 100644 --- a/lmdb-sys/src/lib.rs +++ b/lmdb-sys/src/lib.rs @@ -1,13 +1,16 @@ -#![allow(dead_code, uppercase_variables, non_camel_case_types)] +#![allow( + dead_code, + missing_copy_implementations, + non_camel_case_types, + non_snake_case, + raw_pointer_derive, + )] #![feature(plugin)] -#[plugin] -extern crate bindgen; extern crate libc; -use libc::{size_t, mode_t}; pub use constants::*; mod constants; -bindgen!("../mdb/libraries/liblmdb/lmdb.h", match="lmdb.h", link="lmdb"); +include!(concat!(env!("OUT_DIR"), "/lmdb.rs"));