Skip to content

Commit 6690e15

Browse files
Charlie Friendmsft-tsharp
authored andcommitted
Copy correct bindings for static linkage
1 parent c6c6039 commit 6690e15

File tree

2 files changed

+91
-38
lines changed

2 files changed

+91
-38
lines changed
Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,51 @@
11
use std::env;
2+
use std::fs;
23
use std::path::PathBuf;
4+
use subprocess::Exec;
35

46
static PROJECT_ROOT: &str = "../../../";
57

8+
fn copy_static_libs() {
9+
// TODO add compatibility for x86 and ARM
10+
let cpp_project_out = PathBuf::from(PROJECT_ROOT).join("Solutions/out/Release/x64");
11+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
12+
13+
std::fs::copy(cpp_project_out.join("win32-lib/ClientTelemetry.lib"), PathBuf::from(&out_dir).join("ClientTelemetry.lib"))
14+
.expect("Failed to copy ClientTelemetry lib");
15+
std::fs::copy(cpp_project_out.join("sqlite/sqlite.lib"), out_dir.join("sqlite.lib"))
16+
.expect("Failed to copy sqlite native library");
17+
std::fs::copy(cpp_project_out.join("zlib/zlib.lib"), out_dir.join("zlib.lib"))
18+
.expect("Failed to copy zlib native library");
19+
20+
// Tell cargo to look for shared libraries in the specified directory
21+
println!("cargo:rustc-link-search={}", out_dir.display());
22+
println!("cargo:rustc-link-lib=ClientTelemetry");
23+
println!("cargo:rustc-link-lib=wininet");
24+
println!("cargo:rustc-link-lib=crypt32");
25+
println!("cargo:rustc-link-lib=sqlite");
26+
println!("cargo:rustc-link-lib=zlib");
27+
}
28+
629
fn write_bindings() {
30+
// Precompile header with the appropriate preprocessor options
31+
let mat_h_location = PathBuf::from(PROJECT_ROOT).join("lib/include/public/mat.h");
32+
println!("cargo:rerun-if-changed={}", mat_h_location.to_string_lossy());
33+
34+
// TODO use clang crate instead of invoking CLI directly
35+
let header_out = Exec::cmd("clang")
36+
.arg("-E")
37+
.arg(mat_h_location)
38+
.arg("-D")
39+
.arg("MATSDK_STATIC_LIB=1")
40+
.capture()
41+
.expect("Failed to open clang process")
42+
.stdout_str();
43+
44+
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
45+
let mat_out_path = out_dir.join("mat.out.h");
46+
47+
fs::write(&mat_out_path, header_out).unwrap();
48+
749
// The bindgen::Builder is the main entry point
850
// to bindgen, and lets you build up options for
951
// the resulting bindings.
@@ -15,17 +57,11 @@ fn write_bindings() {
1557
// .raw_line("#![allow(non_upper_case_globals)]")
1658
// .raw_line("#![allow(non_camel_case_types)]")
1759
// .raw_line("#![allow(non_snake_case)]")
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::*")
26-
// Tell cargo to invalidate the built crate whenever any of the
27-
// included header files changed.
28-
// .wrap_static_fns(true)
60+
//.clang_arg(format!("-I{}", PathBuf::from(PROJECT_ROOT).join("lib/include").display()))
61+
.header(PathBuf::from(out_dir).join("mat.out.h").to_string_lossy())
62+
.allowlist_type("evt_.*")
63+
.allowlist_function("evt_.*")
64+
.allowlist_var("evt_.*")
2965
// Finish the builder and generate the bindings.
3066
.generate()
3167
// Unwrap the Result and panic on failure.
@@ -39,31 +75,6 @@ fn write_bindings() {
3975
}
4076

4177
fn main() {
42-
let mat_h_location = PathBuf::from(PROJECT_ROOT).join("lib/include/public/mat.h");
43-
println!("cargo:rerun-if-changed={}", mat_h_location.to_string_lossy());
44-
45-
let out_dir = env::var("OUT_DIR").unwrap();
46-
std::fs::copy("../lib/ClientTelemetry.lib", PathBuf::from(&out_dir).join("ClientTelemetry.lib"))
47-
.expect("Failed to copy native ClientTelemetry lib");
48-
49-
// Tell cargo to look for shared libraries in the specified directory
50-
println!("cargo:rustc-link-search=native={}", out_dir);
51-
println!("cargo:rustc-link-lib=ClientTelemetry");
52-
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();
62-
63-
// let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
64-
// let mat_out_path = out_dir.join("mat.out.h");
65-
66-
// fs::write(&mat_out_path, header_out).unwrap();
67-
6878
write_bindings();
79+
copy_static_libs();
6980
}

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

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

6-
fn test_fn() {
6+
use std::ffi::c_void;
7+
8+
fn evt_api_call_wrapper(evt_context: Box<evt_context_t>) -> (evt_status_t, Box<evt_context_t>) {
9+
let raw_pointer = Box::into_raw(evt_context);
10+
11+
let mut result: evt_status_t = -1;
12+
unsafe {
13+
result = evt_api_call_default(raw_pointer);
14+
}
15+
16+
let out_context = unsafe { Box::from_raw(raw_pointer) };
17+
(result, out_context)
18+
}
19+
20+
fn evt_open(config: *mut c_void) -> Option<evt_handle_t> {
21+
let context: Box<evt_context_t> = Box::new(evt_context_t {
22+
call: evt_call_t_EVT_OP_OPEN,
23+
handle: 0,
24+
data: config,
25+
result: 0,
26+
size: 0,
27+
});
28+
29+
let (result, context) = evt_api_call_wrapper(context);
30+
31+
if result == -1 {
32+
return Option::None;
33+
}
34+
35+
return Some(context.handle.clone());
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 (result, _) = evt_api_call_wrapper(context);
48+
result
749
}
850

951
#[cfg(test)]

0 commit comments

Comments
 (0)