From a1604f49f04aeaa5eb526da65cfbd7e807f53ea2 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Thu, 19 Sep 2019 22:02:14 -0400 Subject: [PATCH] Initial version allows for tests that actually run! --- .gitignore | 2 ++ Cargo.toml | 13 +++++++++--- bindings.h | 2 ++ build.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 16 ++++++++++++-- 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 bindings.h create mode 100644 build.rs diff --git a/.gitignore b/.gitignore index 6936990..f8d06a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /target **/*.rs.bk Cargo.lock + +.idea/ diff --git a/Cargo.toml b/Cargo.toml index 090c85e..e2841e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,16 @@ [package] -name = "aeron-driver-sys" -version = "0.1.0" +name = "libaeron_driver-sys" +version = "0.1.0+1.21.2" authors = ["Bradlee Speice "] edition = "2018" -links = "aeron-driver" +links = "aeron_driver" build = "build.rs" [dependencies] + +[build-dependencies] +bindgen = "0.51" +cmake = "0.1" + +[features] +static = [] diff --git a/bindings.h b/bindings.h new file mode 100644 index 0000000..f9b4222 --- /dev/null +++ b/bindings.h @@ -0,0 +1,2 @@ +#include +#include diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..8f3a5ae --- /dev/null +++ b/build.rs @@ -0,0 +1,62 @@ +use cmake::Config; + +use std::env; +use std::path::{Path, PathBuf}; +use std::fs::canonicalize; + +pub enum LinkType { + Dynamic, + Static +} + +impl LinkType { + fn detect() -> LinkType { + if cfg!(feature = "static") { + LinkType::Static + } else { + LinkType::Dynamic + } + } + + fn link_lib(&self) -> &'static str { + match self { + LinkType::Dynamic => "dylib=", + LinkType::Static => "static=" + } + } + + fn target_name(&self) -> &'static str { + match self { + LinkType::Dynamic => "aeron_driver", + LinkType::Static => "aeron_driver_static" + } + } +} + +pub fn main() { + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=bindings.h"); + + let aeron_path = canonicalize(Path::new("./aeron")).unwrap(); + let header_path = aeron_path.join("aeron-driver/src/main/c"); + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + + let link_type = LinkType::detect(); + println!("cargo:rustc-link-lib={}{}", link_type.link_lib(), link_type.target_name()); + let lib_dir = Config::new(&aeron_path) + .build_target(link_type.target_name()) + .build(); + println!("cargo:rustc-link-search=native={}", lib_dir.join("build/lib").display()); + + println!("cargo:include={}", header_path.display()); + let bindings = bindgen::Builder::default() + .clang_arg(&format!("-I{}", header_path.display())) + .header("bindings.h") + .whitelist_function("aeron_version_.*") + .generate() + .expect("Unable to generate aeron_driver bindings"); + + bindings + .write_to_file(out_path.join("bindings.rs")) + .expect("Couldn't write bindings!"); +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 31e1bb2..ebb1484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,19 @@ +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +include!(concat!(env!("OUT_DIR"), "/bindings.rs")); + #[cfg(test)] mod tests { + #[test] - fn it_works() { - assert_eq!(2 + 2, 4); + fn version_check() { + let major = unsafe { crate::aeron_version_major() }; + let minor = unsafe { crate::aeron_version_minor() }; + let patch = unsafe { crate::aeron_version_patch() }; + assert_eq!(major, 1); + assert_eq!(minor, 21); + assert_eq!(patch, 2); } }