Added a separate bindgen building crate and pre-generated bindings

This commit is contained in:
4bb4 2020-08-03 21:50:03 +02:00
parent c1ccb4eb6f
commit 0f11df5989
7 changed files with 10072 additions and 24 deletions

View file

@ -0,0 +1,12 @@
[package]
name = "implot-sys-bindgen"
version = "0.1.0"
authors = ["Sandro Merkli"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bindgen = "0.53.1"
imgui-sys = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" }
imgui = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" }

View file

@ -0,0 +1,16 @@
use std::env;
use std::io::Write;
use std::path::PathBuf;
fn main() {
// We just forward the DEP_IMGUI_THIRD_PARTY variable here because the
// main function outside the build script does not actually see it
let cimgui_include_path =
env::var_os("DEP_IMGUI_THIRD_PARTY").expect("DEP_IMGUI_THIRD_PARTY not defined");
println!(
"cargo:rustc-env=DEP_IMGUI_THIRD_PARTY={}",
cimgui_include_path
.to_str()
.expect("Could not turn cimgui include path to string")
);
}

View file

@ -0,0 +1,44 @@
use bindgen::{Builder, CargoCallbacks};
use std::{env, path::PathBuf};
// All this crate does is run bindgen on cimplot and store the result
// in the src folder of the implot-sys crate. We add those bindings
// to git so people don't have to install clang just to use implot-rs.
fn main() {
let cwd = env::current_dir().expect("Could not read current directory");
let sys_crate_path = cwd
.join("..")
.join("implot-sys")
.canonicalize()
.expect("Could not find sys crate directory");
let cimgui_include_path = PathBuf::from(
env::var_os("DEP_IMGUI_THIRD_PARTY").expect("DEP_IMGUI_THIRD_PARTY not defined"),
);
let bindings = Builder::default()
.header(
cimgui_include_path
.join("cimgui.h")
.to_str()
.expect("Could not convert cimgui.h path to string"),
)
.header(
sys_crate_path
.join("third-party")
.join("cimplot")
.join("cimplot.h")
.to_str()
.expect("Could not turn cimplot.h path into string"),
)
.parse_callbacks(Box::new(CargoCallbacks))
.clang_arg("-DCIMGUI_DEFINE_ENUMS_AND_STRUCTS=1")
.generate()
.expect("Unable to generate bindings");
let out_path = sys_crate_path.join("src");
bindings
.write_to_file(&out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}

View file

@ -15,4 +15,3 @@ imgui = { git = "https://github.com/Gekkio/imgui-rs/", branch = "master" }
[build-dependencies] [build-dependencies]
cc = "1.0" cc = "1.0"
bindgen = "0.53.1"

View file

@ -5,8 +5,6 @@
// for now, but expected to diverge from that over time. // for now, but expected to diverge from that over time.
use std::{env, fs, io, path::Path}; use std::{env, fs, io, path::Path};
use bindgen;
const CPP_FILES: [&str; 2] = [ const CPP_FILES: [&str; 2] = [
"third-party/cimplot/cimplot.cpp", "third-party/cimplot/cimplot.cpp",
"third-party/cimplot/implot/implot.cpp", "third-party/cimplot/implot/implot.cpp",
@ -54,24 +52,6 @@ fn main() -> io::Result<()> {
assert_file_exists(path)?; assert_file_exists(path)?;
build.file(path); build.file(path);
} }
build.compile("cimplot"); build.compile("cimplot");
// --- Create bindgen 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!");
Ok(()) Ok(())
} }

9999
implot-sys/src/bindings.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,4 @@
#![allow(non_camel_case_types)] #![allow(non_camel_case_types)]
#![allow(non_snake_case)] #![allow(non_snake_case)]
// TODO(4bb4) change this to include the bindings we hand-generate include!("bindings.rs");
// once that is happening
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));