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-omega-compose / 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 | //! Links omega `librx_compose.a` (COMPOSITION-2 + the rxc_host ABI facade). |
| 17 | //! |
| 18 | //! Offline: nothing is downloaded. Order: |
| 19 | //! 1. `AIEN_FORCE_CPU_STUB=1` -> stub (no library; every call returns |
| 20 | //! `ComposeError::Unavailable`). Refused when `AIEN_REQUIRE_NATIVE_LIBS=1`. |
| 21 | //! 2. `AIEN_OMEGA_COMPOSE_LIB=<file>` -> link that prebuilt archive; `AIEN_OMEGA_COMPOSE_LIB_SHA256` |
| 22 | //! must name its sha256 (checked) and it must define every function in the ABI manifest. |
| 23 | //! A development override: its identity says `prebuilt` and it never qualifies. |
| 24 | //! 3. `AIEN_OMEGA_COMPOSE_DIR=<checkout>`, else `AIEN_OMEGA_DIR` (the checkout |
| 25 | //! aien-omega-gpu builds from) -> its HEAD must equal omega.lock with no modified tracked files, then |
| 26 | //! `make <OUT_DIR>/librx_compose.a` runs in it. `AIEN_OMEGA_COMPOSE_SHA=<40-hex>` |
| 27 | //! replaces the expected sha: a documented developer override for building |
| 28 | //! against an unmerged omega branch, never used for a release or a receipt. `AIEN_PHYSICS_DIR` (default `<omega>/../physics`) is passed |
| 29 | //! as PHYSICS_DIR and `AIEN_AIENOS_LOCK_REPO` (if set) as AIENOS_LOCK_REPO; |
| 30 | //! omega's own lock checks still run. |
| 31 | //! 4. otherwise -> stub, with a build warning. |
| 32 | //! |
| 33 | //! `AIEN_OMEGA_COMPOSE_DIR` exists so a developer override never touches the |
| 34 | //! checkout aien-omega-gpu checks against omega.lock. |
| 35 | //! `cfg(has_omega_compose)` is set only when the real library is linked. |
| 36 | //! |
| 37 | //! Before linking a checkout build, `aien_abi_conformance::build::qualify_built` checks the |
| 38 | //! pinned `rxc_host_abi.h`, a compiled layout probe and the archive symbols against |
| 39 | //! `abi/rxc_host_abi.manifest`, and the archive sha256 against `native-artifacts.lock`; any |
| 40 | //! mismatch fails the build. The result is recorded for `native_identity()`. |
| 41 | use aien_abi_conformance::build; |
| 42 | use std::path::{Path, PathBuf}; |
| 43 | use std::process::Command; |
| 44 | |
| 45 | const PREFIX: &str = "AIEN_OMEGA_COMPOSE"; |
| 46 | |
| 47 | fn main() { |
| 48 | println!("cargo:rustc-check-cfg=cfg(has_omega_compose)"); |
| 49 | println!("cargo:rustc-check-cfg=cfg(has_omega_wait_ms)"); |
| 50 | for v in [ |
| 51 | "AIEN_FORCE_CPU_STUB", |
| 52 | "AIEN_OMEGA_COMPOSE_LIB", |
| 53 | "AIEN_OMEGA_COMPOSE_DIR", |
| 54 | "AIEN_OMEGA_DIR", |
| 55 | "AIEN_OMEGA_COMPOSE_SHA", |
| 56 | "AIEN_PHYSICS_DIR", |
| 57 | "AIEN_AIENOS_LOCK_REPO", |
| 58 | "AIEN_REQUIRE_NATIVE_LIBS", |
| 59 | ] { |
| 60 | println!("cargo:rerun-if-env-changed={v}"); |
| 61 | } |
| 62 | let lock_path = Path::new(&env("CARGO_MANIFEST_DIR")).join("../../omega.lock"); |
| 63 | println!("cargo:rerun-if-changed={}", lock_path.display()); |
| 64 | let pinned = std::fs::read_to_string(&lock_path) |
| 65 | .unwrap_or_else(|e| panic!("cannot read {}: {e}", lock_path.display())) |
| 66 | .trim() |
| 67 | .to_string(); |
| 68 | assert!(is_sha(&pinned), "omega.lock must hold one full 40-hex sha"); |
| 69 | let expected = match std::env::var("AIEN_OMEGA_COMPOSE_SHA") { |
| 70 | Ok(s) => { |
| 71 | let s = s.trim().to_string(); |
| 72 | assert!( |
| 73 | is_sha(&s), |
| 74 | "AIEN_OMEGA_COMPOSE_SHA must be a full 40-hex sha" |
| 75 | ); |
| 76 | println!("cargo:warning=aien-omega-compose: AIEN_OMEGA_COMPOSE_SHA={s} overrides omega.lock {pinned} (developer override, not omega.lock)"); |
| 77 | s |
| 78 | } |
| 79 | Err(_) => pinned.clone(), |
| 80 | }; |
| 81 | println!("cargo:rustc-env=AIEN_OMEGA_COMPOSE_EXPECTED_SHA={expected}"); |
| 82 | |
| 83 | let root = PathBuf::from(env("CARGO_MANIFEST_DIR")); |
| 84 | let manifest = root.join("abi/rxc_host_abi.manifest"); |
| 85 | let artifacts_lock = root.join("../../native-artifacts.lock"); |
| 86 | println!("cargo:rerun-if-env-changed=AIEN_OMEGA_COMPOSE_LIB_SHA256"); |
| 87 | |
| 88 | if std::env::var("AIEN_FORCE_CPU_STUB").as_deref() == Ok("1") { |
| 89 | build::refuse_stub_if_required("aien-omega-compose"); |
| 90 | build::Identity::stub(&expected).emit(PREFIX); |
| 91 | return; |
| 92 | } |
| 93 | if let Ok(lib) = std::env::var("AIEN_OMEGA_COMPOSE_LIB") { |
| 94 | let lib = PathBuf::from(lib); |
| 95 | assert!( |
| 96 | lib.is_file(), |
| 97 | "AIEN_OMEGA_COMPOSE_LIB {} is not a file", |
| 98 | lib.display() |
| 99 | ); |
| 100 | build::qualify_prebuilt( |
| 101 | "aien-omega-compose", |
| 102 | &manifest, |
| 103 | &lib, |
| 104 | "AIEN_OMEGA_COMPOSE_LIB_SHA256", |
| 105 | "unverified", |
| 106 | ) |
| 107 | .emit(PREFIX); |
| 108 | require_wait_ms_symbol(&lib); |
| 109 | link(&lib); |
| 110 | return; |
| 111 | } |
| 112 | let Ok(dir) = |
| 113 | std::env::var("AIEN_OMEGA_COMPOSE_DIR").or_else(|_| std::env::var("AIEN_OMEGA_DIR")) |
| 114 | else { |
| 115 | // See aien-omega-gpu/build.rs: AIEN_REQUIRE_NATIVE_LIBS=1 refuses the stub. |
| 116 | assert!( |
| 117 | !build::require_native(), |
| 118 | "aien-omega-compose: AIEN_REQUIRE_NATIVE_LIBS=1 but no AIEN_OMEGA_COMPOSE_DIR, AIEN_OMEGA_DIR or AIEN_OMEGA_COMPOSE_LIB is set (run scripts/pins.sh and source target/pins/env.sh)" |
| 119 | ); |
| 120 | println!("cargo:warning=aien-omega-compose: no AIEN_OMEGA_COMPOSE_DIR, AIEN_OMEGA_DIR or AIEN_OMEGA_COMPOSE_LIB, building the stub"); |
| 121 | build::Identity::stub(&expected).emit(PREFIX); |
| 122 | return; |
| 123 | }; |
| 124 | let dir = PathBuf::from(dir); |
| 125 | build::require_pinned_checkout("aien-omega-compose", &dir, &expected); |
| 126 | let physics = std::env::var("AIEN_PHYSICS_DIR") |
| 127 | .map(PathBuf::from) |
| 128 | .unwrap_or_else(|_| dir.join("../physics")); |
| 129 | let out = PathBuf::from(env("OUT_DIR")).join("omega-compose-build"); |
| 130 | if build::fresh_out_dir(&out, &expected) { |
| 131 | println!("cargo:warning=aien-omega-compose: build dir was for another omega commit, rebuilt from clean"); |
| 132 | } |
| 133 | let mut make = Command::new("make"); |
| 134 | make.current_dir(&dir) |
| 135 | .env_remove("MAKEFLAGS") |
| 136 | .env_remove("MFLAGS") |
| 137 | .arg(format!("OUT_DIR={}", out.display())) |
| 138 | .arg(format!("PHYSICS_DIR={}", physics.display())); |
| 139 | if let Ok(repo) = std::env::var("AIEN_AIENOS_LOCK_REPO") { |
| 140 | make.arg(format!("AIENOS_LOCK_REPO={repo}")); |
| 141 | } |
| 142 | let status = make |
| 143 | .arg(out.join("librx_compose.a")) |
| 144 | .status() |
| 145 | .expect("run make"); |
| 146 | assert!( |
| 147 | status.success(), |
| 148 | "make librx_compose.a failed in {}", |
| 149 | dir.display() |
| 150 | ); |
| 151 | detect_wait_ms(&dir); |
| 152 | let archive = out.join("librx_compose.a"); |
| 153 | build::qualify_built( |
| 154 | "aien-omega-compose", |
| 155 | &manifest, |
| 156 | &artifacts_lock, |
| 157 | &dir, |
| 158 | &physics, |
| 159 | &archive, |
| 160 | &expected, |
| 161 | expected == pinned, |
| 162 | ) |
| 163 | .emit(PREFIX); |
| 164 | require_wait_ms_symbol(&archive); |
| 165 | link(&archive); |
| 166 | } |
| 167 | |
| 168 | /// Check the omega checkout's host header. omega.lock pins a commit that |
| 169 | /// declares `rxc_host_set_wait_ms`, so a real link without it is a build error, never a silent |
| 170 | /// downgrade (the 120 s document budget would be refused at runtime). |
| 171 | fn detect_wait_ms(dir: &Path) { |
| 172 | let h = dir.join("src/runtime/rxc_host_abi.h"); |
| 173 | let text = |
| 174 | std::fs::read_to_string(&h).unwrap_or_else(|e| panic!("cannot read {}: {e}", h.display())); |
| 175 | assert!( |
| 176 | header_declares_wait_ms(&text), |
| 177 | "omega header {} does not declare rxc_host_set_wait_ms; omega.lock pins a commit that has it, so this checkout is not usable. Use AIEN_FORCE_CPU_STUB=1 for a stub build.", |
| 178 | h.display() |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /// A real declaration, not a mention in a comment: a line that starts with |
| 183 | /// `int rxc_host_set_wait_ms(`. |
| 184 | fn header_declares_wait_ms(header: &str) -> bool { |
| 185 | header |
| 186 | .lines() |
| 187 | .any(|l| l.trim_start().starts_with("int rxc_host_set_wait_ms(")) |
| 188 | } |
| 189 | |
| 190 | /// Fail the build unless the linked archive defines `rxc_host_set_wait_ms` (checked with `nm` |
| 191 | /// on both the make path and a prebuilt `AIEN_OMEGA_COMPOSE_LIB`), then set `has_omega_wait_ms`. |
| 192 | fn require_wait_ms_symbol(lib: &Path) { |
| 193 | let out = Command::new("nm") |
| 194 | .arg("-g") |
| 195 | .arg("--defined-only") |
| 196 | .arg(lib) |
| 197 | .output() |
| 198 | .unwrap_or_else(|e| { |
| 199 | panic!( |
| 200 | "cannot run `nm` to check {} for rxc_host_set_wait_ms ({e}); install binutils or use AIEN_FORCE_CPU_STUB=1", |
| 201 | lib.display() |
| 202 | ) |
| 203 | }); |
| 204 | assert!( |
| 205 | out.status.success(), |
| 206 | "`nm` failed on {}: {}", |
| 207 | lib.display(), |
| 208 | String::from_utf8_lossy(&out.stderr) |
| 209 | ); |
| 210 | let text = String::from_utf8_lossy(&out.stdout); |
| 211 | assert!( |
| 212 | archive_defines_wait_ms(&text), |
| 213 | "{} does not define rxc_host_set_wait_ms; omega.lock pins a commit that has it. Rebuild the archive from that omega, or use AIEN_FORCE_CPU_STUB=1 for a stub build.", |
| 214 | lib.display() |
| 215 | ); |
| 216 | println!("cargo:rustc-cfg=has_omega_wait_ms"); |
| 217 | } |
| 218 | |
| 219 | fn archive_defines_wait_ms(nm_out: &str) -> bool { |
| 220 | nm_out |
| 221 | .lines() |
| 222 | .any(|l| l.split_whitespace().last() == Some("rxc_host_set_wait_ms")) |
| 223 | } |
| 224 | |
| 225 | fn link(lib: &Path) { |
| 226 | let dir = lib.parent().expect("lib has a parent dir"); |
| 227 | println!("cargo:rustc-link-search=native={}", dir.display()); |
| 228 | println!("cargo:rustc-link-lib=static=rx_compose"); |
| 229 | println!("cargo:rustc-link-lib=dylib=pthread"); |
| 230 | println!("cargo:rustc-link-lib=dylib=m"); |
| 231 | println!("cargo:rerun-if-changed={}", lib.display()); |
| 232 | println!("cargo:rustc-cfg=has_omega_compose"); |
| 233 | println!("cargo:linked=1"); |
| 234 | } |
| 235 | |
| 236 | fn is_sha(s: &str) -> bool { |
| 237 | s.len() == 40 && s.bytes().all(|b| b.is_ascii_hexdigit()) |
| 238 | } |
| 239 | |
| 240 | fn env(k: &str) -> String { |
| 241 | std::env::var(k).unwrap_or_else(|_| panic!("{k} not set")) |
| 242 | } |