Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Don't generate bindings as part of the build phase
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Porof <victor.porof@gmail.com>
  • Loading branch information
victorporof committed Sep 4, 2019
1 parent 63f7cd0 commit 317168b
Show file tree
Hide file tree
Showing 5 changed files with 1,466 additions and 57 deletions.
2 changes: 2 additions & 0 deletions lmdb-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ documentation = "https://docs.rs/lmdb-rkv-sys"
keywords = ["LMDB", "database", "storage-engine", "bindings", "library"]
categories = ["database", "external-ffi-bindings"]

# NB: When generating bindings, change to "bindgen.rs" or use something
# like `cargo-script` instead.
build = "build.rs"

[dependencies]
Expand Down
68 changes: 68 additions & 0 deletions lmdb-sys/bindgen.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
extern crate bindgen;

use bindgen::callbacks::IntKind;
use bindgen::callbacks::ParseCallbacks;
use std::env;
use std::path::PathBuf;

#[derive(Debug)]
struct Callbacks;

impl ParseCallbacks for Callbacks {
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
match name {
"MDB_SUCCESS"
| "MDB_KEYEXIST"
| "MDB_NOTFOUND"
| "MDB_PAGE_NOTFOUND"
| "MDB_CORRUPTED"
| "MDB_PANIC"
| "MDB_VERSION_MISMATCH"
| "MDB_INVALID"
| "MDB_MAP_FULL"
| "MDB_DBS_FULL"
| "MDB_READERS_FULL"
| "MDB_TLS_FULL"
| "MDB_TXN_FULL"
| "MDB_CURSOR_FULL"
| "MDB_PAGE_FULL"
| "MDB_MAP_RESIZED"
| "MDB_INCOMPATIBLE"
| "MDB_BAD_RSLOT"
| "MDB_BAD_TXN"
| "MDB_BAD_VALSIZE"
| "MDB_BAD_DBI"
| "MDB_LAST_ERRCODE" => Some(IntKind::Int),
_ => Some(IntKind::UInt),
}
}
}

fn main() {
let mut lmdb = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap());
lmdb.push("lmdb");
lmdb.push("libraries");
lmdb.push("liblmdb");

let mut out_path = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap());
out_path.push("src");

let bindings = bindgen::Builder::default()
.header(lmdb.join("lmdb.h").to_string_lossy())
.whitelist_var("^(MDB|mdb)_.*")
.whitelist_type("^(MDB|mdb)_.*")
.whitelist_function("^(MDB|mdb)_.*")
.ctypes_prefix("::libc")
.blacklist_item("mode_t")
.blacklist_item("^__.*")
.parse_callbacks(Box::new(Callbacks {}))
.layout_tests(false)
.prepend_enum_name(false)
.rustfmt_bindings(true)
.generate()
.expect("Unable to generate bindings");

bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
56 changes: 0 additions & 56 deletions lmdb-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
extern crate bindgen;
extern crate cc;
extern crate pkg_config;

use bindgen::callbacks::IntKind;
use bindgen::callbacks::ParseCallbacks;
use std::env;
use std::path::PathBuf;

Expand Down Expand Up @@ -35,39 +32,6 @@ const MDB_IDL_LOGN: u8 = 15;
)))]
const MDB_IDL_LOGN: u8 = 16;

#[derive(Debug)]
struct Callbacks;

impl ParseCallbacks for Callbacks {
fn int_macro(&self, name: &str, _value: i64) -> Option<IntKind> {
match name {
"MDB_SUCCESS"
| "MDB_KEYEXIST"
| "MDB_NOTFOUND"
| "MDB_PAGE_NOTFOUND"
| "MDB_CORRUPTED"
| "MDB_PANIC"
| "MDB_VERSION_MISMATCH"
| "MDB_INVALID"
| "MDB_MAP_FULL"
| "MDB_DBS_FULL"
| "MDB_READERS_FULL"
| "MDB_TLS_FULL"
| "MDB_TXN_FULL"
| "MDB_CURSOR_FULL"
| "MDB_PAGE_FULL"
| "MDB_MAP_RESIZED"
| "MDB_INCOMPATIBLE"
| "MDB_BAD_RSLOT"
| "MDB_BAD_TXN"
| "MDB_BAD_VALSIZE"
| "MDB_BAD_DBI"
| "MDB_LAST_ERRCODE" => Some(IntKind::Int),
_ => Some(IntKind::UInt),
}
}
}

fn main() {
let mut lmdb = PathBuf::from(&env::var("CARGO_MANIFEST_DIR").unwrap());
lmdb.push("lmdb");
Expand All @@ -82,24 +46,4 @@ fn main() {
.flag_if_supported("-Wno-unused-parameter")
.compile("liblmdb.a")
}

let bindings = bindgen::Builder::default()
.header(lmdb.join("lmdb.h").to_string_lossy())
.whitelist_var("^(MDB|mdb)_.*")
.whitelist_type("^(MDB|mdb)_.*")
.whitelist_function("^(MDB|mdb)_.*")
.ctypes_prefix("::libc")
.blacklist_item("mode_t")
.blacklist_item("^__.*")
.parse_callbacks(Box::new(Callbacks {}))
.layout_tests(false)
.prepend_enum_name(false)
.rustfmt_bindings(true)
.generate()
.expect("Unable to generate bindings");

let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
Loading

0 comments on commit 317168b

Please sign in to comment.