diff options
Diffstat (limited to 'bindgen-integration')
-rw-r--r-- | bindgen-integration/Cargo.toml | 12 | ||||
-rw-r--r-- | bindgen-integration/build.rs | 28 | ||||
-rw-r--r-- | bindgen-integration/cpp/Test.cc | 15 | ||||
-rw-r--r-- | bindgen-integration/cpp/Test.h | 10 | ||||
-rw-r--r-- | bindgen-integration/src/lib.rs | 28 |
5 files changed, 93 insertions, 0 deletions
diff --git a/bindgen-integration/Cargo.toml b/bindgen-integration/Cargo.toml new file mode 100644 index 00000000..04f0f84c --- /dev/null +++ b/bindgen-integration/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "bindgen-integration" +description = "A package to test various bindgen features" +version = "0.1.0" +authors = ["Emilio Cobos Álvarez <emilio@crisal.io>"] +workspace = ".." +publish = false +build = "build.rs" + +[build-dependencies] +libbindgen = { path = "../libbindgen" } +gcc = "0.3" diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs new file mode 100644 index 00000000..ff8ba172 --- /dev/null +++ b/bindgen-integration/build.rs @@ -0,0 +1,28 @@ +extern crate libbindgen; +extern crate gcc; + +use std::env; +use std::path::PathBuf; +use libbindgen::Builder; + +fn main() { + gcc::Config::new() + .cpp(true) + .file("cpp/Test.cc") + .compile("libtest.a"); + + let bindings = Builder::default() + .no_unstable_rust() + .header("cpp/Test.h") + .clang_arg("-x") + .clang_arg("c++") + .clang_arg("-std=c++11") + .generate() + .expect("Unable to generate bindings"); + + + let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); + bindings + .write_to_file(out_path.join("test.rs")) + .expect("Couldn't write bindings!"); +} diff --git a/bindgen-integration/cpp/Test.cc b/bindgen-integration/cpp/Test.cc new file mode 100644 index 00000000..d9c13a76 --- /dev/null +++ b/bindgen-integration/cpp/Test.cc @@ -0,0 +1,15 @@ +#include "Test.h" + +const char* Test::name() { + return "Test"; +} + +Test::Test(int foo) + : m_int(foo) + , m_double(0.0) +{} + +Test::Test(double foo) + : m_int(0) + , m_double(foo) +{} diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h new file mode 100644 index 00000000..2e15bb49 --- /dev/null +++ b/bindgen-integration/cpp/Test.h @@ -0,0 +1,10 @@ +#pragma once + +class Test final { + int m_int; + double m_double; +public: + static const char* name(); + Test(int foo); + Test(double foo); +}; diff --git a/bindgen-integration/src/lib.rs b/bindgen-integration/src/lib.rs new file mode 100644 index 00000000..4f239510 --- /dev/null +++ b/bindgen-integration/src/lib.rs @@ -0,0 +1,28 @@ +mod bindings { + include!(concat!(env!("OUT_DIR"), "/test.rs")); +} + +use std::ffi::CStr; + +#[test] +fn test_static_method() { + let c_str = unsafe { bindings::Test::name() }; + let name = unsafe { + CStr::from_ptr(c_str).to_string_lossy().into_owned() + }; + assert_eq!(name, "Test", "Calling a static C++ method works!"); +} + +#[test] +fn test_constructor() { + let test = unsafe { bindings::Test::new(5) }; + assert_eq!(test.m_int, 5); + assert_eq!(test.m_double, 0.0); +} + +#[test] +fn test_overload() { + let test = unsafe { bindings::Test::new1(5.0) }; + assert_eq!(test.m_int, 0); + assert_eq!(test.m_double, 5.0); +} |