Files
leleka/control-plane/build.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 a83913f69e refactor: put the tokenizer and generation decoding behind a model-family codec (STRA-218)
The Responses code used the Qwen tokenizer directly. A TextCodec trait now carries what differs between
model families: encode, a stateful per-generation TokenDecoder, the terminal token ids and the family.
QwenCodec wraps the existing tokenizer with identical behaviour; the buffered and streaming paths decode
through a decoder created per generation, and reasoning_enabled takes the family so a family that always
reasons can say so. All 46 responses tests pass unchanged apart from the test helper.

Also fixes the Windows (MSVC) link of the control plane's test binary: ggml-quants.c references four
helpers from ggml.c that are not vendored; GNU linkers drop the unused code, MSVC does not. Abort-on-use
stubs are compiled for MSVC targets only. 13 other control-plane unit tests fail on Windows for
Linux-path reasons (absolute paths such as /private/api.key, disk-space and service-unit assumptions) and
are unrelated to this change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 22:07:09 +02:00

124 lines
4.8 KiB
Rust

#[path = "src/embedded_spec.rs"]
mod embedded_spec;
fn main() {
embed_engine().expect("invalid embedded engine build configuration");
println!("cargo:rerun-if-changed=src/ggml_bridge.c");
println!("cargo:rerun-if-changed=third_party/ggml");
println!("cargo:rerun-if-changed=src/ggml_msvc_stubs.c");
let mut build = cc::Build::new();
build
.file("third_party/ggml/ggml-quants.c")
.file("src/ggml_bridge.c")
.include("third_party/ggml")
.flag_if_supported("-std=c11")
.flag_if_supported("-ffunction-sections")
.flag_if_supported("-fdata-sections")
.warnings(false);
// The MSVC linker keeps the quantizer object and needs ggml.c's four helpers; see the file.
if std::env::var("CARGO_CFG_TARGET_ENV").as_deref() == Ok("msvc") {
build.file("src/ggml_msvc_stubs.c");
}
build.compile("strata_ggml_quants");
}
fn embed_engine() -> Result<(), Box<dyn std::error::Error>> {
use std::{env, fs, path::PathBuf};
let names = [
"STRATA_EMBED_ENGINE_PATH",
"STRATA_EMBED_ENGINE_SHA256",
"STRATA_EMBED_ENGINE_REVISION",
];
for name in names {
println!("cargo:rerun-if-env-changed={name}");
}
let values: Vec<_> = names.iter().map(env::var_os).collect();
let output = PathBuf::from(env::var_os("OUT_DIR").ok_or("missing OUT_DIR")?);
let payload_path = output.join("embedded-engine.bin");
let (digest, revision) = if values.iter().all(Option::is_none) {
fs::write(&payload_path, [])?;
(String::new(), String::new())
} else {
if values.iter().any(Option::is_none) {
return Err("set all three STRATA_EMBED_ENGINE_PATH/SHA256/REVISION variables".into());
}
if env::var("CARGO_CFG_TARGET_OS")? != "linux" {
return Err("embedded engines require a Linux target".into());
}
let path = PathBuf::from(values[0].as_ref().unwrap()).canonicalize()?;
let printable = path.to_str().ok_or("engine path must be UTF-8")?;
if printable.contains(['\n', '\r']) {
return Err("engine path cannot contain newlines".into());
}
println!("cargo:rerun-if-changed={printable}");
let file = fs::File::open(path)?;
if !file.metadata()?.is_file() {
return Err("embedded engine must be a regular file".into());
}
if file.metadata()?.len() > embedded_spec::MAX_BYTES {
return Err("embedded engine exceeds 1 GiB".into());
}
let digest = values[1]
.as_ref()
.unwrap()
.to_str()
.ok_or("digest must be ASCII")?;
let revision = values[2]
.as_ref()
.unwrap()
.to_str()
.ok_or("revision must be ASCII")?;
embedded_spec::copy_verified(
file,
fs::File::create(&payload_path)?,
&env::var("CARGO_CFG_TARGET_ARCH")?,
digest,
revision,
)?;
(digest.to_ascii_lowercase(), revision.to_ascii_lowercase())
};
let size = fs::metadata(&payload_path)?.len();
let declaration = if size == 0 {
"pub const BYTES: &[u8] = &[];\n".to_owned()
} else {
// Keep large verified payloads out of Rust constant evaluation/LLVM IR.
// The target assembler copies the bytes into an immutable ELF section.
let path = payload_path.to_str().ok_or("payload path must be UTF-8")?;
if path.contains(['\n', '\r']) {
return Err("payload path cannot contain newlines".into());
}
let path = path.replace('\\', "\\\\").replace('"', "\\\"");
let assembly = output.join("embedded-engine.S");
fs::write(
&assembly,
format!(
".section .rodata.strata_engine,\"a\"\n\
.balign 16\n\
.global strata_embedded_engine_bytes\n\
.hidden strata_embedded_engine_bytes\n\
strata_embedded_engine_bytes:\n\
.incbin \"{path}\"\n\
.size strata_embedded_engine_bytes, .-strata_embedded_engine_bytes\n\
.section .note.GNU-stack,\"\"\n"
),
)?;
cc::Build::new()
.file(assembly)
.compile("strata_embedded_payload");
format!(
"unsafe extern \"C\" {{ static strata_embedded_engine_bytes: [u8; {size}]; }}\n\
// SAFETY: build.rs emits exactly {size} verified bytes in read-only storage.\n\
pub static BYTES: &[u8] = unsafe {{ &strata_embedded_engine_bytes }};\n"
)
};
fs::write(
output.join("embedded-engine.rs"),
format!(
"{declaration}\
pub const SHA256: &str = {digest:?};\n\
pub const SOURCE_REVISION: &str = {revision:?};\n"
),
)?;
Ok(())
}