Skip to content

Commit 262df4f

Browse files
author
Charlie Friend
committed
Generate bindings from C++
1 parent d1fb427 commit 262df4f

File tree

2 files changed

+24
-89
lines changed

2 files changed

+24
-89
lines changed
Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
use std::env;
2-
use std::fs;
32
use std::path::PathBuf;
4-
use subprocess::Exec;
53

64
static PROJECT_ROOT: &str = "../../../";
75

8-
fn write_bindings(header_path: &PathBuf) {
9-
let header_path_string = String::from(header_path.to_string_lossy());
10-
6+
fn write_bindings() {
117
// The bindgen::Builder is the main entry point
128
// to bindgen, and lets you build up options for
139
// the resulting bindings.
@@ -19,25 +15,14 @@ fn write_bindings(header_path: &PathBuf) {
1915
// .raw_line("#![allow(non_upper_case_globals)]")
2016
// .raw_line("#![allow(non_camel_case_types)]")
2117
// .raw_line("#![allow(non_snake_case)]")
22-
.header(&header_path_string)
23-
.allowlist_file(&header_path_string)
24-
.c_naming(false)
25-
.blocklist_type("struct_IMAGE_TLS_DIRECTORY")
26-
.blocklist_type("struct_PIMAGE_TLS_DIRECTORY")
27-
.blocklist_type("struct_IMAGE_TLS_DIRECTORY64")
28-
.blocklist_type("struct_PIMAGE_TLS_DIRECTORY64")
29-
.blocklist_type("struct__IMAGE_TLS_DIRECTORY64")
30-
.blocklist_type("IMAGE_TLS_DIRECTORY")
31-
.blocklist_type("PIMAGE_TLS_DIRECTORY")
32-
.blocklist_type("IMAGE_TLS_DIRECTORY64")
33-
.blocklist_type("PIMAGE_TLS_DIRECTORY64")
34-
.blocklist_type("_IMAGE_TLS_DIRECTORY64")
35-
.allowlist_type("evt_.*")
36-
.allowlist_function("evt_.*")
37-
.allowlist_var("evt_.*")
38-
.allowlist_recursively(false)
39-
.layout_tests(false)
40-
.merge_extern_blocks(true)
18+
.clang_arg(format!("-I{}", PathBuf::from(PROJECT_ROOT).join("lib/include").display()))
19+
.header("./include/wrapper.hpp")
20+
//.enable_cxx_namespaces()
21+
.allowlist_type("Microsoft::Applications::Events::LogManagerProvider")
22+
.allowlist_recursively(true)
23+
// STL types must be marked as 'opaque' as bindgen can't handle the internals of these types.
24+
.opaque_type("std::(.*)")
25+
//.blocklist_function("std::*")
4126
// Tell cargo to invalidate the built crate whenever any of the
4227
// included header files changed.
4328
// .wrap_static_fns(true)
@@ -65,20 +50,20 @@ fn main() {
6550
println!("cargo:rustc-link-search=native={}", out_dir);
6651
println!("cargo:rustc-link-lib=ClientTelemetry");
6752

68-
// TODO use clang crate instead of invoking CLI directly
69-
let header_out = Exec::cmd("clang")
70-
.arg("-E")
71-
.arg(mat_h_location)
72-
.arg("-D")
73-
.arg("HAVE_DYNAMIC_C_LIB")
74-
.capture()
75-
.expect("Failed to open clang process")
76-
.stdout_str();
53+
// // TODO use clang crate instead of invoking CLI directly
54+
// let header_out = Exec::cmd("clang")
55+
// .arg("-E")
56+
// .arg(mat_h_location)
57+
// .arg("-D")
58+
// .arg("HAVE_DYNAMIC_C_LIB")
59+
// .capture()
60+
// .expect("Failed to open clang process")
61+
// .stdout_str();
7762

78-
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
79-
let mat_out_path = out_dir.join("mat.out.h");
63+
// let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
64+
// let mat_out_path = out_dir.join("mat.out.h");
8065

81-
fs::write(&mat_out_path, header_out).unwrap();
66+
// fs::write(&mat_out_path, header_out).unwrap();
8267

83-
write_bindings(&mat_out_path);
68+
write_bindings();
8469
}

wrappers/rust/cpp-client-telemetry-sys/src/lib.rs

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,7 @@
33
#![allow(non_snake_case)]
44
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
55

6-
use std::{ffi::{c_void}, os::raw};
7-
8-
fn evt_api_call_wrapper(context_ptr: *mut evt_context_t) -> Option<evt_handle_t> {
9-
let mut result = -1;
10-
unsafe {
11-
if let Some(cb) = evt_api_call {
12-
result = cb(context_ptr);
13-
}
14-
}
15-
16-
if result == -1 {
17-
return Option::None;
18-
}
19-
20-
// Box is now responsible for destroying the context
21-
let context = unsafe { Box::from_raw(context_ptr) };
22-
23-
Some(context.handle.clone())
24-
}
25-
26-
pub fn evt_open(config: *mut c_void) -> Option<evt_handle_t> {
27-
let context: Box<evt_context_t> = Box::new(evt_context_t {
28-
call: evt_call_t_EVT_OP_OPEN,
29-
handle: 0,
30-
data: config,
31-
result: 0,
32-
size: 0,
33-
});
34-
35-
evt_api_call_wrapper(Box::into_raw(context))
36-
}
37-
38-
pub fn evt_close(handle: &evt_handle_t) -> evt_status_t {
39-
let context: Box<evt_context_t> = Box::new(evt_context_t {
40-
call: evt_call_t_EVT_OP_CLOSE,
41-
handle: *handle,
42-
data: std::ptr::null_mut(),
43-
result: 0,
44-
size: 0,
45-
});
46-
47-
let raw_pointer = Box::into_raw(context);
48-
49-
let mut result: evt_status_t = -1;
50-
unsafe {
51-
if let Some(cb) = evt_api_call {
52-
result = cb(raw_pointer);
53-
let _context = Box::from_raw(raw_pointer);
54-
}
55-
}
56-
57-
result
6+
fn test_fn() {
587
}
598

609
#[cfg(test)]
@@ -79,5 +28,6 @@ mod tests {
7928
}"#;
8029

8130
let result = evt_open(config.as_ptr() as *mut c_void);
31+
assert_eq!(result, Some(0));
8232
}
8333
}

0 commit comments

Comments
 (0)