Skip to main content

cactus_sys/
lib.rs

1//! # cactus-sys
2//!
3//! Raw FFI bindings to [Cactus Compute](https://cactuscompute.com) inference engines.
4//!
5//! ## What is cactus-sys?
6//!
7//! Cactus Compute ships its engines as prebuilt static libraries with a small C API. This crate
8//! links one of them and declares that API, nothing more: every item is `unsafe`, and no Rust
9//! type stands between the caller and the engine. For a safe, typed API use
10//! [`cactus-rs`](https://docs.rs/cactus-rs) instead.
11//!
12//! ## Key Features
13//!
14//! - **Needle 3 and Whistle**: the nine functions of `needle.h`, behind the `needle` feature
15//!   (default)
16//! - **Pinned Binaries**: the archive is fetched from one Hugging Face commit and checked
17//!   against a SHA-256 recorded in this crate
18//! - **Offline Builds**: point `CACTUS_NEEDLE_LIB_DIR` at a local archive and nothing is
19//!   downloaded
20//! - **No Dependencies**: `#![no_std]`, and no runtime crates
21//!
22//! ## Engines
23//!
24//! | Engine | Header | Feature | Status |
25//! | --- | --- | --- | --- |
26//! | Needle 3 | `include/needle.h` | `needle` | supported |
27//! | Whistle | `include/needle.h` | `needle` | supported |
28//! | Cactus | `cactus_engine.h` | none yet | planned |
29//!
30//! ## Linking
31//!
32//! The build script picks the first source that applies:
33//!
34//! | Source | Behaviour |
35//! | --- | --- |
36//! | `DOCS_RS` | Links nothing, so documentation builds need no archive. |
37//! | `CACTUS_NEEDLE_LIB_DIR` | Links `libneedle.a` from that directory. |
38//! | feature `download-binaries` (default) | Downloads the archive for the target from the pinned commit and verifies its SHA-256. |
39//!
40//! With `download-binaries` off and `CACTUS_NEEDLE_LIB_DIR` unset the build fails and says so.
41//!
42//! Every upstream archive is built against LLVM libc++, never libstdc++. The build script links
43//! `c++` on Apple and Windows (gnullvm) targets, `c++` and `c++abi` on Linux, and `c++_shared`
44//! on Android. On Debian
45//! and Ubuntu that means installing `libc++-dev libc++abi-dev`, and a native Linux build without
46//! libc++ stops early and says so. The archive also needs one function that libc++ 18 and 20 do
47//! not export, so on Linux this crate links a weak definition of it (`shim/libcxx_compat.c`);
48//! a libc++ that has the real one takes precedence.
49//!
50//! Set `CACTUS_CXXSTDLIB` to a comma-separated list to replace the default: a bare name links
51//! dynamically, `static=name` links the static archive, and an empty string links nothing.
52//! `CACTUS_CXXSTDLIB="static=c++,static=c++abi"` was checked on Ubuntu 24.04 and gives a binary
53//! with no libc++ dependency (add the directory holding `libc++.a` to the linker search path).
54//!
55//! Upstream publishes no archive for Intel macOS, and its Windows archives are llvm-mingw builds
56//! that link only with the `*-pc-windows-gnullvm` targets, not MSVC.
57//!
58//! ## What the archive contains
59//!
60//! The engine is CPU only. Its symbol table names kernels for AVX2, AVX-512, AVX-512 VNNI and AMX
61//! on x86 and for NEON on ARM, chosen at startup from what the CPU offers, and it has no CUDA,
62//! Vulkan, OpenCL or Metal symbols. `nm` also shows no socket, resolver or HTTP symbols, so the
63//! telemetry note in upstream's documentation covers their command-line runner and Python
64//! package rather than this library.
65//!
66//! ## Safety Guarantees
67//!
68//! None. The engine holds one process-global model per kind, text (Needle 3) and speech (Whistle),
69//! behind one non-thread-safe runtime with no handles and one global error string. Every call, of
70//! either kind, must be serialised by the caller, and nothing here enforces that. Each function
71//! documents the contract that was measured against the shipped archive under `# Safety`.
72//!
73//! ## Affiliation
74//!
75//! This crate is unofficial and is not affiliated with Cactus Compute. The engine it links is
76//! published by them under Apache-2.0.
77//!
78//! ## Examples
79//!
80//! ```
81//! // The pin the `download-binaries` feature fetches from.
82//! assert_eq!(cactus_sys::NEEDLE_ENGINE_COMMIT.len(), 40);
83//! ```
84
85#![no_std]
86
87#[cfg(feature = "needle")]
88pub mod needle;
89
90#[cfg(feature = "needle")]
91pub use needle::{
92    NEEDLE_ENGINE_COMMIT, NEEDLE_SPEECH, NEEDLE_TEXT, needle_complete, needle_embed, needle_init,
93    needle_last_error, needle_load, needle_models, needle_reset, needle_set_audio,
94    needle_transcribe,
95};