aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-abi-conformance / src / build.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Shared `build.rs` steps for the crates that link an omega archive: the C-side ABI check, |
| 17 | //! the archive identity check, and the `cargo:rustc-env` identity record the crate exposes at |
| 18 | //! run time (`native_identity()`), so a binary can say what it linked. |
| 19 | |
| 20 | use crate::identity::{self, Digest}; |
| 21 | use crate::probe::{check_c, defined_symbols, CCheck}; |
| 22 | use crate::Manifest; |
| 23 | use std::path::{Path, PathBuf}; |
| 24 | |
| 25 | /// How the archive was obtained. |
| 26 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 27 | pub enum Link { |
| 28 | /// no native library (every native call returns Unavailable) |
| 29 | Stub, |
| 30 | /// built from an omega checkout at the commit `omega.lock` pins |
| 31 | PinnedSource, |
| 32 | /// built from an omega checkout at a developer-override commit (not qualifiable) |
| 33 | DevOverride, |
| 34 | /// a prebuilt archive given by path and expected digest (not qualifiable) |
| 35 | Prebuilt, |
| 36 | } |
| 37 | |
| 38 | impl Link { |
| 39 | pub fn as_str(self) -> &'static str { |
| 40 | match self { |
| 41 | Link::Stub => "stub", |
| 42 | Link::PinnedSource => "pinned-source", |
| 43 | Link::DevOverride => "dev-override", |
| 44 | Link::Prebuilt => "prebuilt", |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /// What `build.rs` established; [`Identity::emit`] records it for the crate. |
| 50 | #[derive(Debug, Clone)] |
| 51 | pub struct Identity { |
| 52 | pub link: Link, |
| 53 | /// "pass" (headers + probe + symbols), "symbols-only" (prebuilt: no headers), "none" (stub) |
| 54 | pub abi: &'static str, |
| 55 | /// "locked" | "unrecorded" | "prebuilt-match" | "none" |
| 56 | pub digest: &'static str, |
| 57 | pub archive_sha256: String, |
| 58 | pub omega_sha: String, |
| 59 | pub aienos_lock: String, |
| 60 | pub physics_lock: String, |
| 61 | pub target: String, |
| 62 | pub cc: String, |
| 63 | } |
| 64 | |
| 65 | impl Identity { |
| 66 | pub fn stub(omega_sha: &str) -> Identity { |
| 67 | Identity { |
| 68 | link: Link::Stub, |
| 69 | abi: "none", |
| 70 | digest: "none", |
| 71 | archive_sha256: String::new(), |
| 72 | omega_sha: omega_sha.into(), |
| 73 | aienos_lock: String::new(), |
| 74 | physics_lock: String::new(), |
| 75 | target: env_or("TARGET", ""), |
| 76 | cc: String::new(), |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// `cargo:rustc-env={prefix}_{KEY}=value` for every field. |
| 81 | pub fn emit(&self, prefix: &str) { |
| 82 | let kv = [ |
| 83 | ("LINK", self.link.as_str().to_string()), |
| 84 | ("ABI", self.abi.to_string()), |
| 85 | ("DIGEST", self.digest.to_string()), |
| 86 | ("ARCHIVE_SHA256", self.archive_sha256.clone()), |
| 87 | ("SOURCE_SHA", self.omega_sha.clone()), |
| 88 | ("AIENOS_LOCK", self.aienos_lock.clone()), |
| 89 | ("PHYSICS_LOCK", self.physics_lock.clone()), |
| 90 | ("TARGET", self.target.clone()), |
| 91 | ("CC", self.cc.replace(['\n', '\r'], " ")), |
| 92 | ]; |
| 93 | for (k, v) in kv { |
| 94 | println!("cargo:rustc-env={prefix}_{k}={v}"); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | fn env_or(k: &str, d: &str) -> String { |
| 100 | std::env::var(k).unwrap_or_else(|_| d.to_string()) |
| 101 | } |
| 102 | |
| 103 | /// Makes `out` a build directory for the omega commit `pinned`: when the directory was last |
| 104 | /// used for another commit (or carries no stamp) it is removed first, so `make` never reuses |
| 105 | /// objects compiled from the other commit's sources. The omega Makefile tracks header |
| 106 | /// dependencies per object, but a reused directory also survives Makefile fixes and rule gaps |
| 107 | /// (2026-10-12: `librx_compose.a` mixed objects of two layouts of a Cortex struct after a |
| 108 | /// repin, and the host seal refused with RX_ERR_AUTHORITY). Returns `true` when it wiped. |
| 109 | pub fn fresh_out_dir(out: &Path, pinned: &str) -> bool { |
| 110 | let stamp = out.join(".omega-commit"); |
| 111 | let same = std::fs::read_to_string(&stamp) |
| 112 | .map(|s| s.trim() == pinned) |
| 113 | .unwrap_or(false); |
| 114 | let wiped = !same && out.exists(); |
| 115 | if !same { |
| 116 | if wiped { |
| 117 | std::fs::remove_dir_all(out) |
| 118 | .unwrap_or_else(|e| panic!("remove stale build dir {}: {e}", out.display())); |
| 119 | } |
| 120 | std::fs::create_dir_all(out) |
| 121 | .unwrap_or_else(|e| panic!("create build dir {}: {e}", out.display())); |
| 122 | std::fs::write(&stamp, format!("{pinned}\n")) |
| 123 | .unwrap_or_else(|e| panic!("write {}: {e}", stamp.display())); |
| 124 | } |
| 125 | wiped |
| 126 | } |
| 127 | |
| 128 | /// Refuse an omega checkout that is not exactly the pinned source: `HEAD` must be `pinned` |
| 129 | /// and no tracked file may differ from that commit (`git status --porcelain |
| 130 | /// --untracked-files=no` empty). A modified tracked file, such as a swapped `seed/out/omc`, |
| 131 | /// would otherwise build an archive reported as `pinned-source`. Index entries flagged |
| 132 | /// assume-unchanged or skip-worktree are refused too, since they hide edits from `git status`. |
| 133 | pub fn require_pinned_checkout(krate: &str, dir: &Path, pinned: &str) { |
| 134 | let git = |args: &[&str]| { |
| 135 | std::process::Command::new("git") |
| 136 | .arg("-C") |
| 137 | .arg(dir) |
| 138 | .args(args) |
| 139 | .output() |
| 140 | .ok() |
| 141 | .filter(|o| o.status.success()) |
| 142 | .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string()) |
| 143 | }; |
| 144 | let head = git(&["rev-parse", "HEAD"]).unwrap_or_default(); |
| 145 | assert!( |
| 146 | head == pinned, |
| 147 | "{krate}: omega checkout {} is at '{head}', omega.lock pins {pinned}", |
| 148 | dir.display() |
| 149 | ); |
| 150 | // `git status` trusts the index flags: an assume-unchanged (lowercase tag) or |
| 151 | // skip-worktree (`S`) entry hides any edit to that file, so such entries are refused. |
| 152 | let flags = git(&["ls-files", "-v"]).unwrap_or_else(|| { |
| 153 | panic!( |
| 154 | "{krate}: cannot list the index of omega checkout {}", |
| 155 | dir.display() |
| 156 | ) |
| 157 | }); |
| 158 | let hidden: Vec<&str> = flags |
| 159 | .lines() |
| 160 | .filter(|l| l.starts_with(|c: char| c.is_ascii_lowercase() || c == 'S')) |
| 161 | .collect(); |
| 162 | assert!( |
| 163 | hidden.is_empty(), |
| 164 | "{krate}: omega checkout {} marks tracked files assume-unchanged or skip-worktree, so \ |
| 165 | edits to them are hidden and it cannot be proven the pinned source (git update-index \ |
| 166 | --no-assume-unchanged --no-skip-worktree):\n{}", |
| 167 | dir.display(), |
| 168 | hidden.join("\n") |
| 169 | ); |
| 170 | match git(&["status", "--porcelain", "--untracked-files=no"]) { |
| 171 | Some(s) if s.is_empty() => {} |
| 172 | Some(s) => panic!( |
| 173 | "{krate}: omega checkout {} is at {pinned} but has uncommitted changes to tracked \ |
| 174 | files, so it is not the pinned source (commit or discard them):\n{s}", |
| 175 | dir.display() |
| 176 | ), |
| 177 | None => panic!( |
| 178 | "{krate}: cannot read git status of omega checkout {}", |
| 179 | dir.display() |
| 180 | ), |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /// `true` when `AIEN_REQUIRE_NATIVE_LIBS=1` (release and qualification builds). |
| 185 | pub fn require_native() -> bool { |
| 186 | std::env::var("AIEN_REQUIRE_NATIVE_LIBS").as_deref() == Ok("1") |
| 187 | } |
| 188 | |
| 189 | /// Refuse `AIEN_FORCE_CPU_STUB=1` together with `AIEN_REQUIRE_NATIVE_LIBS=1`: a build that |
| 190 | /// requires the native library must never quietly become a stub. |
| 191 | pub fn refuse_stub_if_required(krate: &str) { |
| 192 | if require_native() { |
| 193 | panic!( |
| 194 | "{krate}: AIEN_FORCE_CPU_STUB=1 and AIEN_REQUIRE_NATIVE_LIBS=1 contradict each other: \ |
| 195 | a required-native build cannot be a stub (unset one of them)" |
| 196 | ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | fn read_trim(p: &Path) -> String { |
| 201 | std::fs::read_to_string(p) |
| 202 | .map(|s| s.trim().to_string()) |
| 203 | .unwrap_or_default() |
| 204 | } |
| 205 | |
| 206 | fn load_manifest(path: &Path) -> Manifest { |
| 207 | println!("cargo:rerun-if-changed={}", path.display()); |
| 208 | let text = std::fs::read_to_string(path) |
| 209 | .unwrap_or_else(|e| panic!("cannot read ABI manifest {}: {e}", path.display())); |
| 210 | Manifest::parse(&text).unwrap_or_else(|e| panic!("{}: {e}", path.display())) |
| 211 | } |
| 212 | |
| 213 | fn load_lock(path: &Path) -> Vec<identity::LockEntry> { |
| 214 | println!("cargo:rerun-if-changed={}", path.display()); |
| 215 | match std::fs::read_to_string(path) { |
| 216 | Ok(t) => identity::parse_lock(&t).unwrap_or_else(|e| panic!("{e}")), |
| 217 | Err(_) => Vec::new(), |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /// Checks for an archive built from an omega checkout. Panics (fails the build) on any ABI |
| 222 | /// mismatch, missing symbol or digest that differs from `native-artifacts.lock`. |
| 223 | #[allow(clippy::too_many_arguments)] |
| 224 | pub fn qualify_built( |
| 225 | krate: &str, |
| 226 | manifest: &Path, |
| 227 | lock: &Path, |
| 228 | omega: &Path, |
| 229 | physics: &Path, |
| 230 | archive: &Path, |
| 231 | omega_sha: &str, |
| 232 | pinned: bool, |
| 233 | ) -> Identity { |
| 234 | let m = load_manifest(manifest); |
| 235 | for h in &m.headers { |
| 236 | println!("cargo:rerun-if-changed={}", omega.join(h).display()); |
| 237 | } |
| 238 | let cc = env_or("CC", "cc"); |
| 239 | let target = env_or("TARGET", ""); |
| 240 | let host = env_or("HOST", ""); |
| 241 | let abi = if target == host { |
| 242 | let work = PathBuf::from(env_or("OUT_DIR", ".")).join("abi-probe"); |
| 243 | let includes = vec![ |
| 244 | omega.join("src"), |
| 245 | omega.join("src/runtime"), |
| 246 | physics.join("m16"), |
| 247 | physics.join("nvrm"), |
| 248 | ]; |
| 249 | match check_c(&CCheck { |
| 250 | manifest: &m, |
| 251 | root: omega, |
| 252 | includes, |
| 253 | archive: Some(archive), |
| 254 | work: &work, |
| 255 | cc: cc.clone(), |
| 256 | }) { |
| 257 | Ok(report) => { |
| 258 | let _ = std::fs::write(work.join("report.txt"), &report); |
| 259 | "pass" |
| 260 | } |
| 261 | Err(errs) => panic!( |
| 262 | "{krate}: omega {omega_sha} does not match {} ({} problem(s)); nothing was linked:\n - {}", |
| 263 | manifest.display(), |
| 264 | errs.len(), |
| 265 | errs.join("\n - ") |
| 266 | ), |
| 267 | } |
| 268 | } else if require_native() { |
| 269 | panic!("{krate}: cross build ({host} -> {target}): the ABI layout probe cannot run, and AIEN_REQUIRE_NATIVE_LIBS=1 needs it"); |
| 270 | } else { |
| 271 | println!("cargo:warning={krate}: cross build ({host} -> {target}): C ABI layout probe skipped (not qualifiable)"); |
| 272 | "unchecked" |
| 273 | }; |
| 274 | let sha = identity::sha256_file(archive).unwrap_or_else(|e| panic!("{krate}: {e}")); |
| 275 | let ccv = identity::cc_version(&cc); |
| 276 | let name = archive |
| 277 | .file_name() |
| 278 | .map(|n| n.to_string_lossy().to_string()) |
| 279 | .unwrap_or_default(); |
| 280 | let digest = match identity::check_built( |
| 281 | &load_lock(lock), |
| 282 | &name, |
| 283 | omega_sha, |
| 284 | &target, |
| 285 | &ccv, |
| 286 | &sha, |
| 287 | ) { |
| 288 | Ok(Digest::Locked) => "locked", |
| 289 | Ok(Digest::Unrecorded) => { |
| 290 | println!( |
| 291 | "cargo:warning={krate}: {name} sha256 {sha} (omega {omega_sha}, {target}, `{ccv}`) is not recorded in native-artifacts.lock: builds, but does not qualify" |
| 292 | ); |
| 293 | "unrecorded" |
| 294 | } |
| 295 | Err(e) => panic!("{krate}: {e}"), |
| 296 | }; |
| 297 | Identity { |
| 298 | link: if pinned { |
| 299 | Link::PinnedSource |
| 300 | } else { |
| 301 | Link::DevOverride |
| 302 | }, |
| 303 | abi, |
| 304 | digest, |
| 305 | archive_sha256: sha, |
| 306 | omega_sha: omega_sha.into(), |
| 307 | aienos_lock: read_trim(&omega.join("aienos.lock")), |
| 308 | physics_lock: read_trim(&omega.join("physics.lock")), |
| 309 | target, |
| 310 | cc: ccv, |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /// Checks for a prebuilt archive: its digest must equal `$sha_var`, and it must define every |
| 315 | /// function the manifest names (no headers are available, so layouts are not re-probed; the |
| 316 | /// identity says so and qualification does not accept it). |
| 317 | pub fn qualify_prebuilt( |
| 318 | krate: &str, |
| 319 | manifest: &Path, |
| 320 | archive: &Path, |
| 321 | sha_var: &str, |
| 322 | omega_sha: &str, |
| 323 | ) -> Identity { |
| 324 | println!("cargo:rerun-if-env-changed={sha_var}"); |
| 325 | let want = std::env::var(sha_var).ok(); |
| 326 | let sha = identity::check_prebuilt(archive, want.as_deref(), sha_var) |
| 327 | .unwrap_or_else(|e| panic!("{krate}: {e}")); |
| 328 | let m = load_manifest(manifest); |
| 329 | let syms = defined_symbols(archive).unwrap_or_else(|e| panic!("{krate}: {e}")); |
| 330 | let missing: Vec<&str> = m |
| 331 | .fns |
| 332 | .iter() |
| 333 | .map(|(n, _)| n.as_str()) |
| 334 | .filter(|n| !syms.iter().any(|s| s == n)) |
| 335 | .collect(); |
| 336 | assert!( |
| 337 | missing.is_empty(), |
| 338 | "{krate}: prebuilt {} does not define {missing:?}", |
| 339 | archive.display() |
| 340 | ); |
| 341 | println!( |
| 342 | "cargo:warning={krate}: linking prebuilt {} (sha256 {sha}): a development override, not a qualified build", |
| 343 | archive.display() |
| 344 | ); |
| 345 | Identity { |
| 346 | link: Link::Prebuilt, |
| 347 | abi: "symbols-only", |
| 348 | digest: "prebuilt-match", |
| 349 | archive_sha256: sha, |
| 350 | omega_sha: omega_sha.into(), |
| 351 | aienos_lock: String::new(), |
| 352 | physics_lock: String::new(), |
| 353 | target: env_or("TARGET", ""), |
| 354 | cc: String::new(), |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | #[cfg(test)] |
| 359 | mod tests { |
| 360 | use super::fresh_out_dir; |
| 361 | |
| 362 | #[test] |
| 363 | fn fresh_out_dir_wipes_only_on_another_commit() { |
| 364 | let out = std::env::temp_dir().join(format!("aien-fresh-out-{}", std::process::id())); |
| 365 | let _ = std::fs::remove_dir_all(&out); |
| 366 | assert!(!fresh_out_dir(&out, "aaaa"), "nothing to wipe on first use"); |
| 367 | std::fs::write(out.join("obj.o"), b"x").unwrap(); |
| 368 | assert!( |
| 369 | !fresh_out_dir(&out, "aaaa"), |
| 370 | "same commit keeps the directory" |
| 371 | ); |
| 372 | assert!(out.join("obj.o").exists()); |
| 373 | assert!(fresh_out_dir(&out, "bbbb"), "another commit wipes it"); |
| 374 | assert!(!out.join("obj.o").exists()); |
| 375 | assert_eq!( |
| 376 | std::fs::read_to_string(out.join(".omega-commit")) |
| 377 | .unwrap() |
| 378 | .trim(), |
| 379 | "bbbb" |
| 380 | ); |
| 381 | std::fs::remove_file(out.join(".omega-commit")).unwrap(); |
| 382 | std::fs::write(out.join("obj.o"), b"x").unwrap(); |
| 383 | assert!( |
| 384 | fresh_out_dir(&out, "bbbb"), |
| 385 | "no stamp: the directory is of unknown origin, wiped" |
| 386 | ); |
| 387 | assert!(!out.join("obj.o").exists()); |
| 388 | let _ = std::fs::remove_dir_all(&out); |
| 389 | } |
| 390 | } |