From b9758637c8a65b120d69a670443f1dda86cea5cb Mon Sep 17 00:00:00 2001 From: jyviko <2501648+jyviko@users.noreply.github.com> Date: Wed, 21 Oct 2026 00:35:15 -0400 Subject: [PATCH] Wave 12/509: solidcraft-smlib: read SMLib's log lines inside the FFI module (and 2 more) 3 commits, squashed; the originals stay reachable from tag archive/merge-main-develop. - 60aa5e697 jyviko: solidcraft-smlib: build every C API source file - 3c0b53935 jyviko: solidcraft-smlib: read SMLib's log lines inside the FFI module - 858f2c3bd jyviko: solidcraft-smlib: the C types are imported only when SMLib is built Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DtL5M41G8Nua7HvuFPZkmJ --- crates/smlib/build.rs | 3 ++- crates/smlib/src/ffi.rs | 31 ++++++++++++++++++++++++------- crates/smlib/src/lib.rs | 5 ++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/crates/smlib/build.rs b/crates/smlib/build.rs index 9614f0f6d..dca8a9bf3 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 86abf3f47..6cb72bfc0 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) { + /// Routes SMLib's log lines to `sink` (`None`: stops them). + pub fn set_log(sink: Option) { + let log: Option = 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) {} + pub fn set_log(_: Option) {} 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 = 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 4004e7253..d36236173 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::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 {