Skip to content

Commit 85f4eb0

Browse files
committed
Use bindgen to expose tskit's guts.
1 parent e724b0b commit 85f4eb0

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ edition = "2018"
1010
[dependencies]
1111

1212
[build-dependencies]
13+
bindgen = "0.56.0"
1314
cc = { version = "1.0", features = ["parallel"] }
1415
pkg-config = "0.3"

build.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
extern crate bindgen;
12
use std::path::Path;
23

34
fn main() {
4-
pkg_config::Config::new()
5-
.atleast_version("1.2");
5+
pkg_config::Config::new().atleast_version("1.2");
66

77
let src = [
88
"subprojects/tskit/c/tskit/convert.c",
@@ -24,4 +24,29 @@ fn main() {
2424
.include(kastore_path)
2525
.flag("-Wno-unused-parameter");
2626
build.compile("tskit");
27+
28+
// The bindgen::Builder is the main entry point
29+
// to bindgen, and lets you build up options for
30+
// the resulting bindings.
31+
let bindings = bindgen::Builder::default()
32+
// The input header we would like to generate
33+
// bindings for.
34+
.header("wrapper.h")
35+
.clang_arg("-Isubprojects/tskit/c")
36+
.clang_arg("-Isubprojects/tskit/c/subprojects/kastore")
37+
.whitelist_type("tsk.*")
38+
.whitelist_function("tsk.*")
39+
// Tell cargo to invalidate the built crate whenever any of the
40+
// included header files changed.
41+
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
42+
// Finish the builder and generate the bindings.
43+
.generate()
44+
// Unwrap the Result and panic on failure.
45+
.expect("Unable to generate bindings");
46+
47+
// Write the bindings to the $OUT_DIR/bindings.rs file.
48+
//let out_path = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
49+
bindings
50+
.write_to_file("src/bindings.rs")
51+
.expect("Couldn't write bindings!");
2752
}

src/lib.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
7-
}
1+
//mod bindings;
2+
3+
#![allow(non_upper_case_globals)]
4+
#![allow(non_camel_case_types)]
5+
#![allow(non_snake_case)]
6+
7+
include!("bindings.rs");
8+
9+
// Testing modules
10+
mod test_table_collection;

src/test_table_collection.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use crate::*;
4+
use std::mem::MaybeUninit;
5+
6+
#[test]
7+
fn test_add_edge_table_rows() -> () {
8+
let mut edges: MaybeUninit<tsk_edge_table_t> = MaybeUninit::uninit();
9+
unsafe {
10+
let mut rv = tsk_edge_table_init(edges.as_mut_ptr(), 0);
11+
rv = tsk_edge_table_add_row(edges.as_mut_ptr(), 0., 10., 0, 1, std::ptr::null(), 0);
12+
assert_eq!((*edges.as_ptr()).num_rows, 1);
13+
tsk_edge_table_free(edges.as_mut_ptr());
14+
}
15+
}
16+
}

wrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include <tskit.h>

0 commit comments

Comments
 (0)