Skip to content

Commit

Permalink
[feat] Add bindgen and auto generate __cplusplus guards
Browse files Browse the repository at this point in the history
  • Loading branch information
uttarayan21 committed Jan 11, 2024
1 parent 2bc976e commit f70a628
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 53 deletions.
30 changes: 17 additions & 13 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,29 @@ pub fn riio(out_dir: impl AsRef<Path>) -> anyhow::Result<()> {

cbindgen::Builder::new()
.with_crate(env!("CARGO_MANIFEST_DIR"))
.with_language(cbindgen::Language::Cxx)
.with_language(cbindgen::Language::C)
.with_no_includes()
.with_cpp_compat(true)
.with_header("#include<stdint.h>")
.with_include_guard("RUST_IO_H")
.generate()
.expect("Unable to generate bindings")
.write_to_file("io/riio.h");

// dbg!(&includes);
// bindgen::Builder::default()
// // .header("io/io.cpp")
// .clang_args(
// includes
// .iter()
// .map(|p| format!("-I{}", p.to_str().unwrap())),
// )
// .generate()
// .unwrap()
// .write_to_file("src/wrapper.rs")
// .unwrap();
let clang_args = includes
.iter()
.map(|p| format!("-I{}", p.to_str().unwrap()))
.inspect(|p| println!("cargo:warning=clang_args: {}", p));

bindgen::Builder::default()
.header("io/io.h")
.allowlist_file("io/io.h")
.blocklist_type(".*")
.clang_args(clang_args)
.generate()
.expect("failed to generate bindings")
.write_to_file(out_dir.as_ref().join("io.rs"))
.expect("failed to write bindings");

let mut riio = cc::Build::new();
riio.includes(includes)
Expand All @@ -79,6 +82,7 @@ pub fn riio(out_dir: impl AsRef<Path>) -> anyhow::Result<()> {
riio.compile("riio");

println!("cargo:rerun-if-changed=io/io.cpp");
println!("cargo:rerun-if-changed=io/io.h");
println!("cargo:rustc-link-lib=static=riio");
println!(
"cargo:rustc-link-search=native={}",
Expand Down
2 changes: 1 addition & 1 deletion io/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class LibrawIO : public LibRaw_abstract_datastream {
LibrawOpaqueDatastream *inner;
};

extern "C" int libraw_open_io(libraw_data_t *libraw,
int libraw_open_io(libraw_data_t *libraw,
LibrawOpaqueDatastream *io) {
LibrawIO* libraw_io = new LibrawIO(io);
LibRaw *lr = (LibRaw *)libraw->parent_class;
Expand Down
16 changes: 13 additions & 3 deletions io/io.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/* #include "riio.h" */
/* #include <libraw.h> */
/* extern "C" int libraw_open_io(LibRaw *libraw, LibrawOpaqueDatastream *io); */
#include "riio.h"
#include <libraw.h>

#ifdef __cplusplus
extern "C" {
#endif

int libraw_open_io(libraw_data_t *libraw,
LibrawOpaqueDatastream *io);

#ifdef __cplusplus
}
#endif
29 changes: 18 additions & 11 deletions io/riio.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,35 @@



struct LibrawOpaqueDatastream;
typedef struct LibrawOpaqueDatastream LibrawOpaqueDatastream;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

int32_t lod_valid(LibrawOpaqueDatastream *this_);
int32_t lod_valid(struct LibrawOpaqueDatastream *this_);

int32_t lod_read(LibrawOpaqueDatastream *this_, const void *buffer, uintptr_t sz, uintptr_t nmemb);
int32_t lod_read(struct LibrawOpaqueDatastream *this_,
const void *buffer,
uintptr_t sz,
uintptr_t nmemb);

int32_t lod_seek(LibrawOpaqueDatastream *this_, int64_t offset, uint32_t whence);
int32_t lod_seek(struct LibrawOpaqueDatastream *this_, int64_t offset, uint32_t whence);

int64_t lod_tell(LibrawOpaqueDatastream *this_);
int64_t lod_tell(struct LibrawOpaqueDatastream *this_);

int32_t lod_eof(LibrawOpaqueDatastream *this_);
int32_t lod_eof(struct LibrawOpaqueDatastream *this_);

int64_t lod_size(LibrawOpaqueDatastream *this_);
int64_t lod_size(struct LibrawOpaqueDatastream *this_);

int lod_get_char(LibrawOpaqueDatastream *this_);
int lod_get_char(struct LibrawOpaqueDatastream *this_);

char *lod_gets(LibrawOpaqueDatastream *this_, char *buffer, int size);
char *lod_gets(struct LibrawOpaqueDatastream *this_, char *buffer, int size);

int lod_scanf_one(LibrawOpaqueDatastream *this_, const char *fmt, void *val);
int lod_scanf_one(struct LibrawOpaqueDatastream *this_, const char *fmt, void *val);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // RUST_IO_H
#endif /* RUST_IO_H */
2 changes: 1 addition & 1 deletion libraw-sys/vendor
1 change: 1 addition & 0 deletions src/datastream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ impl From<i32> for InternalLibrawError {
e if e == TooBig as i32 => TooBig,
e if e == MempoolOverflow as i32 => MempoolOverflow,
e if e == Self::SUCCESS => panic!("This call was a success"),
_ => unreachable!("If the error is reached then libraw has added new error types"),
_ => unreachable!("If the error is reached then libraw has added new error types, {e}"),
}
}
}
Expand Down
17 changes: 5 additions & 12 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod bindings;
/// Input layer abstraction
///
/// class LibRaw_abstract_datastream - abstract RAW read interface
Expand Down Expand Up @@ -135,24 +136,16 @@ pub trait LibrawDatastream: Read + Seek + Eof {
///
/// Sus
unsafe fn seek(&mut self, offset: i64, whence: u32) -> i32 {
// assert!(!this.is_null());
// let this = unsafe { &mut *this };
if offset == 1856 {
println!("offset: {}", offset);
let x = std::io::Seek::seek(self, std::io::SeekFrom::Start(0));
println!("x: {:?}", x);
}
match match whence {
match whence {
sys::SEEK_SET => {
std::io::Seek::seek(self, std::io::SeekFrom::Start(offset as u64)).ok()
}
sys::SEEK_CUR => std::io::Seek::seek(self, std::io::SeekFrom::Current(offset)).ok(),
sys::SEEK_END => std::io::Seek::seek(self, std::io::SeekFrom::End(offset)).ok(),
_ => return 0,
} {
None => -1,
Some(_) => 0,
}
.expect("Failed to seek");
return 0;
}
/// # Safety
///
Expand Down Expand Up @@ -441,5 +434,5 @@ pub unsafe extern "C" fn lod_scanf_one(
fmt: *const libc::c_char,
val: *mut libc::c_void,
) -> libc::c_int {
panic!();
todo!();
}
4 changes: 4 additions & 0 deletions src/io/bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use super::LibrawOpaqueDatastream;
use libraw_sys::*;

include!(concat!(env!("OUT_DIR"), "/io.rs"));
12 changes: 1 addition & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,9 @@ impl EmptyProcessor {
let file = std::fs::File::open(path)?;
let buffered = std::io::BufReader::new(file);
let mut io = io::LibrawOpaqueDatastream::new(buffered);
let ret = unsafe { libraw_open_io(self.inner.as_mut(), &mut io) };
let ret = unsafe { io::bindings::libraw_open_io(self.inner.as_mut(), &mut io) };
LibrawError::check(ret)?;
core::mem::forget(io);
Ok(Processor { inner: self.inner })
}
}


extern {
/// cbindgen:no-export
#[link(name = "libraw_open_io")]
fn libraw_open_io(
data: *mut sys::libraw_data_t,
io: *mut io::LibrawOpaqueDatastream,
) -> libc::c_int;
}

0 comments on commit f70a628

Please sign in to comment.