-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpure-bindgen.rs
More file actions
68 lines (61 loc) · 2.43 KB
/
Copy pathpure-bindgen.rs
File metadata and controls
68 lines (61 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//! Regenerate the committed language bindings from the `pure-ffi` C ABI.
//!
//! ```sh
//! cargo run -p pure-ffi --features bindgen --bin pure-bindgen
//! ```
//!
//! Emits, relative to the crate root:
//! * `include/pure_study.h` — C header (cbindgen), for
//! C consumers and csbindgen/P-Invoke.
//! * `bindings/csharp/PureStudyNative.g.cs` — C# P/Invoke shim (csbindgen).
//!
//! Kotlin/Android binds the same header via JNA (see `bindings/kotlin/`); its
//! wrapper is hand-written rather than generated, so it is not produced here.
//!
//! This is a developer tool, kept out of the cdylib so a plain build /
//! cross-compile never pulls host-only generators.
use std::path::Path;
fn main() {
let crate_dir = env!("CARGO_MANIFEST_DIR");
generate_c_header(crate_dir);
generate_csharp(crate_dir);
println!("bindings regenerated.");
}
fn generate_c_header(crate_dir: &str) {
let out = Path::new(crate_dir).join("include/pure_study.h");
std::fs::create_dir_all(out.parent().unwrap()).expect("create include/");
let config = cbindgen::Config {
language: cbindgen::Language::C,
pragma_once: true,
cpp_compat: true, // wrap in extern "C" when compiled as C++
documentation: true,
documentation_style: cbindgen::DocumentationStyle::C99,
header: Some(
"/* pure-study C ABI — GENERATED by `cargo run -p pure-ffi \
--features bindgen`. Do not edit by hand. */"
.to_string(),
),
usize_is_size_t: true,
..Default::default()
};
cbindgen::Builder::new()
.with_crate(crate_dir)
.with_config(config)
.generate()
.expect("cbindgen: generate C header")
.write_to_file(&out);
println!("wrote {}", out.display());
}
fn generate_csharp(crate_dir: &str) {
let out = Path::new(crate_dir).join("bindings/csharp/PureStudyNative.g.cs");
std::fs::create_dir_all(out.parent().unwrap()).expect("create bindings/csharp/");
csbindgen::Builder::default()
.input_extern_file(Path::new(crate_dir).join("src/lib.rs"))
.csharp_class_name("PureStudyNative")
.csharp_dll_name("pure_ffi")
.csharp_namespace("PureStudy.Native")
.csharp_class_accessibility("public")
.generate_csharp_file(&out)
.expect("csbindgen: generate C# bindings");
println!("wrote {}", out.display());
}