Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
118 lines
4.5 KiB
Rust
118 lines
4.5 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");
|
|
cc::Build::new()
|
|
.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)
|
|
.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(())
|
|
}
|