From 135b49b629ad10a13548cd4a97f620b220fd6cbd Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Wed, 9 Aug 2023 22:48:41 +0530
Subject: [PATCH] rust: use cbindgen to generate header file

---
 rust/Cargo.toml    |  4 ++++
 rust/build.rs      | 17 +++++++++++++++++
 rust/cbindgen.toml |  3 +++
 rust/src/lib.rs    |  2 +-
 4 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 rust/build.rs
 create mode 100644 rust/cbindgen.toml

diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 48363f2..0060b33 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -6,7 +6,11 @@ authors = ["Ramakrishnan Muthukrishnan <ram@rkrishnan.org>"]
 
 [dependencies]
 libc = "0.2"
+cbindgen = "0.24.3"
 
 [lib]
 name = "hpsdr"
 crate-type = ["staticlib"]
+
+[build-dependencies]
+cbindgen = "0.20.0"
diff --git a/rust/build.rs b/rust/build.rs
new file mode 100644
index 0000000..fac6a07
--- /dev/null
+++ b/rust/build.rs
@@ -0,0 +1,17 @@
+use std::env;
+use std::path::Path;
+use cbindgen::{Config, Builder};
+
+fn main() {
+let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
+    let mut config: cbindgen::Config = Default::default();
+    config.language = cbindgen::Language::C;
+
+    cbindgen::Builder::new()
+        .with_crate(crate_dir)
+        .with_config(config)
+        .generate()
+        .expect("Unable to generate bindings")
+        .write_to_file("target/debug/libhpsdr.h");
+    println!("cargo:rerun-if-changed=libhpsdr.h");
+}
diff --git a/rust/cbindgen.toml b/rust/cbindgen.toml
new file mode 100644
index 0000000..5c7702e
--- /dev/null
+++ b/rust/cbindgen.toml
@@ -0,0 +1,3 @@
+[parse.expand]
+crates = ["libhpsdr"]
+features = ["cbindgen"]
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 5d7f64a..83d0269 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -1,7 +1,7 @@
 use libc::{size_t};
 
 #[no_mangle]
-pub extern fn add(left: size_t, right: size_t) -> size_t {
+pub extern "C" fn add(left: usize, right: usize) -> usize {
     left + right
 }
 
-- 
2.45.2