streamer/ljm_mode.rs
1//! LabJack LJM library initialization for the selected linking mode.
2//!
3//! The crate is built with exactly one of the `dynlink` (default) or `staticlib`
4//! features; enabling both or neither is a compile error. Dynamic mode loads the
5//! LJM shared library at run time, so builds do not depend on platform linker
6//! paths. Static mode initializes the library linked at build time.
7//!
8//! # Configuration
9//!
10//! * `LJM_PATH` - Path to the LJM shared library, read only in `dynlink` builds.
11//! When unset, no path is passed to `ljmrs::LJMLibrary::init`.
12
13use ljmrs::{LJMError, LJMLibrary};
14
15#[cfg(all(feature = "dynlink", feature = "staticlib"))]
16compile_error!(
17 "Choose only one LJM mode. Use the default `dynlink`, or `--no-default-features --features staticlib`."
18);
19
20#[cfg(all(not(feature = "dynlink"), not(feature = "staticlib")))]
21compile_error!("Enable one LJM mode: `dynlink` or `staticlib`.");
22
23/// Initializes the LabJack LJM library for the configured feature mode.
24///
25/// # Errors
26///
27/// Returns the error from `LJMLibrary::init` unchanged.
28///
29/// # Safety
30///
31/// This delegates to `ljmrs::LJMLibrary::init`, which is unsafe because it
32/// initializes process-wide FFI library state. Call it once during service
33/// startup before using other LJM operations.
34pub unsafe fn init_ljm() -> Result<(), LJMError> {
35 #[cfg(all(feature = "dynlink", not(feature = "staticlib")))]
36 {
37 let path = std::env::var("LJM_PATH").ok();
38 unsafe { LJMLibrary::init(path) }
39 }
40
41 #[cfg(all(feature = "staticlib", not(feature = "dynlink")))]
42 {
43 unsafe { LJMLibrary::init() }
44 }
45}