Commit 809c1dc9 authored by Fu Chen's avatar Fu Chen Committed by pingcap-github-bot

bindgen: pre-generate bindings (#402)

parent 8d06b36b
...@@ -34,6 +34,14 @@ $ git submodule update --init --recursive # if you just cloned the repository ...@@ -34,6 +34,14 @@ $ git submodule update --init --recursive # if you just cloned the repository
$ cargo build $ cargo build
``` ```
Bindings are pre-generated for x86_64 Linux. For other platforms, bindings are generated at compile time.
If the content in librocksdb_sys/crocksdb/crocksdb/c.h is updated, you may need to regenerate bindings:
```
$ ./scripts/generate-bindings.sh
```
## Running ## Running
###### Cargo.toml ###### Cargo.toml
......
...@@ -23,6 +23,7 @@ sse = ["libtitan_sys/sse"] ...@@ -23,6 +23,7 @@ sse = ["libtitan_sys/sse"]
[build-dependencies] [build-dependencies]
cc = "1.0.3" cc = "1.0.3"
cmake = "0.1" cmake = "0.1"
bindgen = "0.52"
[dependencies.jemalloc-sys] [dependencies.jemalloc-sys]
version = "0.1.8" version = "0.1.8"
......
This source diff could not be displayed because it is too large. You can view the blob instead.
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
extern crate bindgen;
extern crate cc; extern crate cc;
extern crate cmake; extern crate cmake;
...@@ -24,7 +25,52 @@ use std::{env, str}; ...@@ -24,7 +25,52 @@ use std::{env, str};
// See https://github.com/gnzlbg/jemallocator/blob/bfc89192971e026e6423d9ee5aaa02bc56585c58/jemalloc-sys/build.rs#L45 // See https://github.com/gnzlbg/jemallocator/blob/bfc89192971e026e6423d9ee5aaa02bc56585c58/jemalloc-sys/build.rs#L45
const NO_JEMALLOC_TARGETS: &[&str] = &["android", "dragonfly", "musl", "darwin"]; const NO_JEMALLOC_TARGETS: &[&str] = &["android", "dragonfly", "musl", "darwin"];
// Generate the bindings to rocksdb C-API.
// Try to disable the generation of platform-related bindings.
fn bindgen_rocksdb(file_path: &PathBuf) {
let bindings = bindgen::Builder::default()
.header("crocksdb/crocksdb/c.h")
.ctypes_prefix("libc")
.generate()
.expect("unable to generate rocksdb bindings");
bindings
.write_to_file(file_path)
.expect("unable to write rocksdb bindings");
}
// Determine if need to update bindings. Supported platforms do not
// need to be updated by default unless the UPDATE_BIND is specified.
// Other platforms use bindgen to generate the bindings every time.
fn config_binding_path() {
let file_path: PathBuf;
match env::var("TARGET").unwrap_or("".to_owned()).as_str() {
"x86_64-unknown-linux-gnu" => {
file_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join("bindings")
.join("x86_64-unknown-linux-gnu-bindings.rs");
if env::var("UPDATE_BIND")
.map(|s| s == "1".to_owned())
.unwrap_or(false)
{
bindgen_rocksdb(&file_path);
}
}
_ => {
file_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("rocksdb-bindings.rs");
bindgen_rocksdb(&file_path);
}
};
println!(
"cargo:rustc-env=BINDING_PATH={}",
file_path.to_str().unwrap()
);
}
fn main() { fn main() {
println!("cargo:rerun-if-env-changed=UPDATE_BIND");
let mut build = build_rocksdb(); let mut build = build_rocksdb();
build.cpp(true).file("crocksdb/c.cc"); build.cpp(true).file("crocksdb/c.cc");
...@@ -134,6 +180,8 @@ fn build_rocksdb() -> Build { ...@@ -134,6 +180,8 @@ fn build_rocksdb() -> Build {
build.define("OS_FREEBSD", None); build.define("OS_FREEBSD", None);
} }
config_binding_path();
let cur_dir = env::current_dir().unwrap(); let cur_dir = env::current_dir().unwrap();
build.include(cur_dir.join("rocksdb").join("include")); build.include(cur_dir.join("rocksdb").join("include"));
build.include(cur_dir.join("rocksdb")); build.include(cur_dir.join("rocksdb"));
......
#!/bin/bash
# NOTE:
# This script is only used when you want to generate bindings yourself.
# The generated bindings will overwrite librocksdb_sys/bindings/*
export UPDATE_BIND=1
cargo build --target x86_64-unknown-linux-gnu
rustfmt librocksdb_sys/bindings/*
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment