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