Skip to content

Commit d1fb427

Browse files
author
Charlie Friend
committed
Static link telemetry lib
1 parent 8e17918 commit d1fb427

File tree

2 files changed

+84
-7
lines changed

2 files changed

+84
-7
lines changed

wrappers/rust/cpp-client-telemetry-sys/build.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,21 @@ fn write_bindings(header_path: &PathBuf) {
5454
}
5555

5656
fn main() {
57+
let mat_h_location = PathBuf::from(PROJECT_ROOT).join("lib/include/public/mat.h");
58+
println!("cargo:rerun-if-changed={}", mat_h_location.to_string_lossy());
59+
60+
let out_dir = env::var("OUT_DIR").unwrap();
61+
std::fs::copy("../lib/ClientTelemetry.lib", PathBuf::from(&out_dir).join("ClientTelemetry.lib"))
62+
.expect("Failed to copy native ClientTelemetry lib");
63+
5764
// Tell cargo to look for shared libraries in the specified directory
58-
println!("cargo:rustc-link-search=../lib");
65+
println!("cargo:rustc-link-search=native={}", out_dir);
66+
println!("cargo:rustc-link-lib=ClientTelemetry");
5967

6068
// TODO use clang crate instead of invoking CLI directly
6169
let header_out = Exec::cmd("clang")
6270
.arg("-E")
63-
.arg(PathBuf::from(PROJECT_ROOT).join("lib/include/public/mat.h"))
71+
.arg(mat_h_location)
6472
.arg("-D")
6573
.arg("HAVE_DYNAMIC_C_LIB")
6674
.capture()
Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,83 @@
1-
pub fn add(left: usize, right: usize) -> usize {
2-
left + right
1+
#![allow(non_upper_case_globals)]
2+
#![allow(non_camel_case_types)]
3+
#![allow(non_snake_case)]
4+
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
5+
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
358
}
459

560
#[cfg(test)]
661
mod tests {
762
use super::*;
863

964
#[test]
10-
fn it_works() {
11-
let result = add(2, 2);
12-
assert_eq!(result, 4);
65+
fn test_open_close() {
66+
let config = r#"{
67+
"eventCollectorUri": "http://localhost:64099/OneCollector/track",
68+
"cacheFilePath":"hackathon_storage.db",
69+
"config":{"host": "*"},
70+
"name":"Rust-API-Client-0",
71+
"version":"1.0.0",
72+
"primaryToken":"99999999999999999999999999999999-99999999-9999-9999-9999-999999999999-9999",
73+
"maxTeardownUploadTimeInSec":5,
74+
"hostMode":false,
75+
"traceLevelMask": 4294967295,
76+
"minimumTraceLevel":0,
77+
"sdkmode":0,
78+
"compat": {"customTypePrefix": "compat_event"}
79+
}"#;
80+
81+
let result = evt_open(config.as_ptr() as *mut c_void);
1382
}
1483
}

0 commit comments

Comments
 (0)