Add an example for using a build script

This commit is contained in:
Ryan Hunt 2017-04-17 22:22:34 -04:00
parent cedd1b3cc2
commit e76b25701d
4 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,8 @@
[package]
name = "build-script"
version = "0.1.0"
authors = ["Ryan Hunt <rhunt@eqrion.net>"]
build = "build.rs"
[build-dependencies]
cbindgen = { path = "../../" }

View File

@ -0,0 +1,11 @@
extern crate cbindgen;
use cbindgen::{Config, Library};
fn main() {
let config = Config::default();
Library::load("../build-script", &config)
.build(&config).unwrap()
.write_to_file(&config, "bindings.h");
}

View File

@ -0,0 +1,19 @@
#![allow(dead_code)]
#![allow(unused_variables)]
pub struct Opaque {
x: i32,
y: f32,
}
#[repr(C)]
pub struct Normal {
x: i32,
y: f32,
}
#[no_mangle]
pub extern "C" fn root(x: *mut Opaque, y: Normal)
{
}

View File

@ -1,6 +1,7 @@
use std::io::Write;
use std::collections::BTreeMap;
use std::cmp::Ordering;
use std::fs::File;
use syn::*;
@ -327,6 +328,11 @@ impl BuiltLibrary {
}
}
pub fn write_to_file(&self, config: &Config, path: &str) {
self.write(&config,
&mut File::create(path).unwrap());
}
pub fn write<F: Write>(&self, config: &Config, out: &mut F) {
if let Some(ref f) = config.file_header {
write!(out, "{}\n", f).unwrap();