Check in platform-independent bindings

Automatically generating bindings as part of the build process caused
some user complaints, since installing clang/libclang can be a hassle.
The rdkafka bindings are not particularly platform-dependent, and with a
bit of work we can generate bindings that depend on platform-dependent
types, like libc::FILE, so that we only need to run bindgen once.

Fix #126.
This commit is contained in:
Nikhil Benesch 2019-10-23 16:38:59 -04:00
parent 0b4e653ea1
commit 1c7a58c54e
No known key found for this signature in database
GPG Key ID: FCF98542083C5A69
8 changed files with 2160 additions and 33 deletions

View File

@ -2,7 +2,6 @@ FROM ubuntu:16.04
RUN apt-get update && apt-get install -y build-essential \
curl \
llvm-3.9-dev libclang-3.9-dev clang-3.9 \
openssl libssl-dev \
pkg-config \
python \

View File

@ -11,13 +11,13 @@ keywords = ["kafka", "rdkafka"]
categories = ["external-ffi-bindings"]
[dependencies]
libc = "0.2.65"
libz-sys = "1.0"
zstd-sys = { version = "1.3", features = [], optional = true }
openssl-sys = { version = "~ 0.9.0", optional = true }
lz4-sys = { version = "1.8.3", optional = true }
[build-dependencies]
bindgen = "0.51.1"
num_cpus = "0.2.0"
pkg-config = "0.3.9"
cmake = { version = "^0.1", optional = true }

View File

@ -8,8 +8,8 @@ To regenerate the bindings:
``` bash
git submodule update --init
cargo install bindgen --vers 0.30.0
bindgen --builtins --no-doc-comments librdkafka/src/rdkafka.h -o src/bindings/{platform}.rs
cargo install bindgen
./update-bindings.sh
```
## Version

View File

@ -4,7 +4,6 @@ extern crate num_cpus;
extern crate pkg_config;
use std::env;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{self, Command};
@ -77,29 +76,6 @@ fn main() {
eprintln!("Building and linking librdkafka statically");
build_librdkafka();
}
let bindings = bindgen::Builder::default()
.header("librdkafka/src/rdkafka.h")
.generate_comments(false)
.emit_builtins()
// TODO: using rustified_enum is somewhat dangerous, especially when
// also using shared libraries.
// For details: https://github.com/rust-lang/rust-bindgen/issues/758
.rustified_enum("rd_kafka_vtype_t")
.rustified_enum("rd_kafka_type_t")
.rustified_enum("rd_kafka_conf_res_t")
.rustified_enum("rd_kafka_resp_err_t")
.rustified_enum("rd_kafka_timestamp_type_t")
.rustified_enum("rd_kafka_admin_op_t")
.rustified_enum("rd_kafka_ResourceType_t")
.rustified_enum("rd_kafka_ConfigSource_t")
.generate()
.expect("failed to generate bindings");
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("failed to write bindings");
}
#[cfg(not(feature = "cmake_build"))]

File diff suppressed because it is too large Load Diff

View File

@ -43,6 +43,7 @@ extern crate openssl_sys;
extern crate libz_sys;
#[allow(non_camel_case_types, non_upper_case_globals, non_snake_case, clippy::all)]
pub mod bindings;
pub mod helpers;
pub mod types;

19
rdkafka-sys/update-bindings.sh Executable file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
# rd_kafka_conf_set_open_cb is blacklisted because it is not compiled on
# Windows due to its usage of the Unix-only `mode_t` type. With a bit of
# elbow grease we could include it if not targeting Windows, but it doesn't
# seem worthwhile at the moment.
bindgen \
--no-doc-comments \
--no-layout-tests \
--rustified-enum ".*" \
--whitelist-function "rd_kafka.*" \
--whitelist-type "rd_kafka.*" \
--whitelist-var "rd_kafka.*|RD_KAFKA_.*" \
--no-recursive-whitelist \
--blacklist-function "rd_kafka_conf_set_open_cb" \
--raw-line "type FILE = libc::FILE;" \
--raw-line "type sockaddr = libc::sockaddr;" \
librdkafka/src/rdkafka.h -o src/bindings.rs

0
src/bindings.rs Normal file
View File