Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Wave 12/509: solidcraft-smlib: read SMLib's log lines inside the FFI module (and 2 more)

jyvikob9758637c8a6
diff --git a/crates/smlib/build.rs b/crates/smlib/build.rs
index 9614f0f6dd..dca8a9bf32 100644
--- a/crates/smlib/build.rs
+++ b/crates/smlib/build.rs
@@ -60,8 +60,9 @@ fn build() -> Result<(), String> {
};
// Link order: the C API needs SM_API, which needs SMLib.
+ let capi = sources(&root.join("capi/src"), &[])?;
let mut c = base();
- c.file(root.join("capi/src/smlib_c.cpp")).include(root.join("capi/include")).include(src.join("SM_API/inc")).include(src.join("SMLib/inc"));
+ c.files(&capi).include(root.join("capi/include")).include(src.join("SM_API/inc")).include(src.join("SMLib/inc"));
quiet(&mut c);
c.try_compile("smlib_capi").map_err(|e| e.to_string())?;
diff --git a/crates/smlib/src/ffi.rs b/crates/smlib/src/ffi.rs
index 86abf3f477..6cb72bfc02 100644
--- a/crates/smlib/src/ffi.rs
+++ b/crates/smlib/src/ffi.rs
@@ -3,6 +3,7 @@
//! root, which check their arguments and own the handles.
#![allow(unsafe_code)]
+#[cfg(feature = "native")]
use std::ffi::{c_char, c_void};
/// `SMLIB_ABI_VERSION` this crate was written against.
@@ -20,6 +21,7 @@ pub struct RawBrep {
_private: [u8; 0],
}
+#[cfg(feature = "native")]
pub type LogFn = extern "C" fn(user: *mut c_void, level: i32, line: *const c_char);
#[cfg(feature = "native")]
@@ -88,7 +90,16 @@ mod calls {
pub fn init() -> i32 {
unsafe { smlib_init() }
}
- pub fn set_log(log: Option<LogFn>) {
+ /// Routes SMLib's log lines to `sink` (`None`: stops them).
+ pub fn set_log(sink: Option<fn(i32, String)>) {
+ let log: Option<LogFn> = match sink {
+ Some(sink) => {
+ // The first sink stays: init sets it once, and lines keep a single destination.
+ let _ = SINK.set(sink);
+ Some(trampoline)
+ }
+ None => None,
+ };
unsafe { smlib_set_log(log, std::ptr::null_mut()) }
}
pub fn set_dump_dir(dir: Option<&CStr>) -> i32 {
@@ -139,7 +150,7 @@ mod calls {
pub fn init() -> i32 {
NOT_BUILT
}
- pub fn set_log(_: Option<LogFn>) {}
+ pub fn set_log(_: Option<fn(i32, String)>) {}
pub fn set_dump_dir(_: Option<&CStr>) -> i32 {
NOT_BUILT
}
@@ -168,10 +179,16 @@ mod calls {
pub use calls::*;
-/// A log line from the C API: NUL-terminated and valid for the callback's duration.
-pub fn line(p: *const c_char) -> String {
- if p.is_null() {
- return String::new();
+/// Where [`set_log`] sends SMLib's log lines.
+#[cfg(feature = "native")]
+static SINK: std::sync::OnceLock<fn(i32, String)> = std::sync::OnceLock::new();
+
+/// The log callback the C API calls: `line` is NUL-terminated and valid for the call's duration
+/// (smlib.h), so it is read here, inside the one module that touches raw pointers.
+#[cfg(feature = "native")]
+extern "C" fn trampoline(_: *mut c_void, level: i32, line: *const c_char) {
+ let text = if line.is_null() { String::new() } else { unsafe { std::ffi::CStr::from_ptr(line) }.to_string_lossy().into_owned() };
+ if let Some(sink) = SINK.get() {
+ sink(level, text);
}
- unsafe { std::ffi::CStr::from_ptr(p) }.to_string_lossy().into_owned()
}
diff --git a/crates/smlib/src/lib.rs b/crates/smlib/src/lib.rs
index 4004e72532..d36236173f 100644
--- a/crates/smlib/src/lib.rs
+++ b/crates/smlib/src/lib.rs
@@ -10,7 +10,7 @@
//! [`Error::NotBuilt`].
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
-use std::ffi::{CString, c_char, c_void};
+use std::ffi::CString;
use std::path::Path;
use std::sync::Once;
use std::sync::atomic::{AtomicI32, Ordering};
@@ -70,8 +70,7 @@ fn c_path(p: &Path) -> Result<CString> {
CString::new(p.to_string_lossy().as_bytes()).map_err(|_| Error::Argument)
}
-extern "C" fn log_line(_: *mut c_void, level: i32, line: *const c_char) {
- let text = ffi::line(line);
+fn log_line(level: i32, text: String) {
if level == 0 {
log::debug!(target: "smlib", "{text}");
} else {