Got initial unsafe bindings working. See comments.

- This now builds the bindings (still with cbindgen in build.rs,
  will change that in a bit) and links to them successfully. Everything
  currently points to the master branch at Gekkio/imgui-rs, because
  there has not been a release yet with the PR merged that exposes
  the defines in the sys crate.

- I have now included a little demo crate that calls into the unsafe
  generated bindings as a proof of concept. Next there will need to
  be safe and more Rust-y bindings written around this, just like
  imgui-rs has them.
This commit is contained in:
4bb4 2020-08-01 18:28:17 +02:00
parent 5d293f8f79
commit c1ccb4eb6f
7 changed files with 269 additions and 20 deletions

View file

@ -7,6 +7,7 @@ description = "Raw FFI bindings to implot"
license = "MIT/Apache-2.0"
categories = ["gui", "external-ffi-bindings"]
build = "build.rs"
links = "implot"
[dependencies]
imgui-sys = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" }

View file

@ -5,7 +5,7 @@
// for now, but expected to diverge from that over time.
use std::{env, fs, io, path::Path};
//use bindgen; // Not used anymore, TODO(4bb4) remove
use bindgen;
const CPP_FILES: [&str; 2] = [
"third-party/cimplot/cimplot.cpp",
@ -45,8 +45,8 @@ fn main() -> io::Result<()> {
let cimgui_include_path =
env::var_os("DEP_IMGUI_THIRD_PARTY").expect("DEP_IMGUI_THIRD_PARTY not defined");
let imgui_include_path = Path::new(&cimgui_include_path).join("imgui");
build.include(cimgui_include_path);
build.include(imgui_include_path);
build.include(&cimgui_include_path);
build.include(&imgui_include_path);
// Taken from the imgui-sys build as well
build.flag_if_supported("-Wno-return-type-c-linkage");
@ -58,21 +58,20 @@ fn main() -> io::Result<()> {
build.compile("cimplot");
// --- Create bindgen bindings
// TODO(4bb4) move this out to separate shell script (see #1)
// The actual generate() errors out right now with parsing errors,
// will probably need to whiltelist things, fix preprocessor definitions,
// bindgen settings or some combination thereof.
//let _bindings = bindgen::Builder::default()
//.header(imgui_third_party.into_string().unwrap() + "/cimgui.h")
//.header("wrapper.h")
//.parse_callbacks(Box::new(bindgen::CargoCallbacks));
//.generate()
//.expect("Unable to generate bindings");
// TODO(4bb4) move this out to separate shell script (see #1) so users don't have
// to have clang installed to build this crate.
let bindings = bindgen::Builder::default()
.header(&(cimgui_include_path.into_string().unwrap() + "/cimgui.h"))
.header("third-party/cimplot/cimplot.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_arg("-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS=1")
.generate()
.expect("Unable to generate bindings");
//let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
//bindings
//.write_to_file(out_path.join("bindings.rs"))
//.expect("Couldn't write bindings!");
let out_path = std::path::PathBuf::from(env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
Ok(())
}

View file

@ -1,3 +1,7 @@
fn _does_nothing() {
println!("This does nothing yet");
}
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
// TODO(4bb4) change this to include the bindings we hand-generate
// once that is happening
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));