]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
rust: use cbindgen to generate header file
authorRamakrishnan Muthukrishnan <ram@leastauthority.com>
Fri, 17 Feb 2023 11:26:21 +0000 (16:56 +0530)
committerRamakrishnan Muthukrishnan <ram@leastauthority.com>
Fri, 17 Feb 2023 11:26:21 +0000 (16:56 +0530)
rust/Cargo.toml
rust/build.rs [new file with mode: 0644]
rust/cbindgen.toml [new file with mode: 0644]
rust/src/lib.rs

index 48363f2c0f2c73df87fbd3e3b26bc0374e811889..0060b33b5c0cf4b1aba0fd88bf8f685fd1339539 100644 (file)
@@ -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 (file)
index 0000000..fac6a07
--- /dev/null
@@ -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 (file)
index 0000000..b17abbd
--- /dev/null
@@ -0,0 +1,3 @@
+[parse.expand]
+crates = ["libhpsdr"]
+features = ["cbindgen"]
\ No newline at end of file
index 5d7f64a62026088cacfc3885887fe980705f569d..83d02690b5bc079cb73d1a3ceb539f8d244cb6e5 100644 (file)
@@ -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
 }