interplane.Get Omega
Read in your language

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

branch main Branches Tags History

aien-sovereign-core-mv / crates / aien-omega-gpu / build.rs

153 lines · 6540 bytes Raw Blame History
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 `libomega_gpu.a` from the omega commit pinned in `omega.lock`.
17//!
18//! Offline by design: nothing is downloaded and no CUDA/nvcc is used. Order:
19//! 1. `AIEN_FORCE_CPU_STUB=1` -> stub (CI without the chip). BUILD-TIME ONLY: a binary already
20//! built with the GPU library ignores it at run time. For a CPU-only RUN set `AIEN_BACKEND=cpu`:
21//! the daemon refuses to start (`CPU_ONLY_BUT_GPU_BUILD`) if the binary would select the GB10 backend.
22//! Refused when `AIEN_REQUIRE_NATIVE_LIBS=1` (a required-native build is never a stub).
23//! 2. `AIEN_OMEGA_GPU_LIB=<file>` -> link that prebuilt archive; `AIEN_OMEGA_GPU_LIB_SHA256` must
24//! name its sha256 (checked) and it must define every function in the ABI manifest. A
25//! development override: its identity says `prebuilt` and it never qualifies.
26//! 3. `AIEN_OMEGA_DIR=<checkout>` -> its HEAD must equal omega.lock, with no modified tracked files; built with
27//! `make libomega_gpu` into OUT_DIR. `AIEN_PHYSICS_DIR` (default `<omega>/../physics`)
28//! is passed as PHYSICS_DIR; omega's own physics.lock check still runs.
29//! 4. otherwise -> stub, with a build warning.
30//!
31//! `cfg(has_omega_gpu)` is set only when the real library is linked.
32//!
33//! Before linking a checkout build, `aien_abi_conformance::build::qualify_built` checks the
34//! pinned headers, a compiled layout probe and the archive symbols against
35//! `abi/omega_gpu.manifest`, and the archive sha256 against `native-artifacts.lock`; any mismatch
36//! fails the build. The result is recorded for `native_identity()`.
37use aien_abi_conformance::build;
38use std::path::{Path, PathBuf};
39use std::process::Command;
40
41const PREFIX: &str = "AIEN_OMEGA_GPU";
42
43fn main() {
44 println!("cargo:rustc-check-cfg=cfg(has_omega_gpu)");
45 for v in [
46 "AIEN_FORCE_CPU_STUB",
47 "AIEN_OMEGA_GPU_LIB",
48 "AIEN_OMEGA_DIR",
49 "AIEN_PHYSICS_DIR",
50 "AIEN_REQUIRE_NATIVE_LIBS",
51 ] {
52 println!("cargo:rerun-if-env-changed={v}");
53 }
54 let lock_path = Path::new(&env("CARGO_MANIFEST_DIR")).join("../../omega.lock");
55 println!("cargo:rerun-if-changed={}", lock_path.display());
56 let pinned = std::fs::read_to_string(&lock_path)
57 .unwrap_or_else(|e| panic!("cannot read {}: {e}", lock_path.display()))
58 .trim()
59 .to_string();
60 assert!(
61 pinned.len() == 40 && pinned.bytes().all(|b| b.is_ascii_hexdigit()),
62 "omega.lock must hold one full 40-hex sha"
63 );
64 println!("cargo:rustc-env=AIEN_OMEGA_PINNED_SHA={pinned}");
65
66 let root = PathBuf::from(env("CARGO_MANIFEST_DIR"));
67 let manifest = root.join("abi/omega_gpu.manifest");
68 let artifacts_lock = root.join("../../native-artifacts.lock");
69 println!("cargo:rerun-if-env-changed=AIEN_OMEGA_GPU_LIB_SHA256");
70 if std::env::var("AIEN_FORCE_CPU_STUB").as_deref() == Ok("1") {
71 build::refuse_stub_if_required("aien-omega-gpu");
72 build::Identity::stub(&pinned).emit(PREFIX);
73 return;
74 }
75 if let Ok(lib) = std::env::var("AIEN_OMEGA_GPU_LIB") {
76 let lib = PathBuf::from(lib);
77 assert!(
78 lib.is_file(),
79 "AIEN_OMEGA_GPU_LIB {} is not a file",
80 lib.display()
81 );
82 build::qualify_prebuilt(
83 "aien-omega-gpu",
84 &manifest,
85 &lib,
86 "AIEN_OMEGA_GPU_LIB_SHA256",
87 "unverified",
88 )
89 .emit(PREFIX);
90 link(&lib);
91 return;
92 }
93 let Ok(dir) = std::env::var("AIEN_OMEGA_DIR") else {
94 // AIEN_REQUIRE_NATIVE_LIBS=1 (set by scripts/pins.sh env.sh) turns the silent
95 // stub fallback into a build error: a release or receipt build must never
96 // discover at run time that it carries the CPU stub (sovereign-core #92).
97 assert!(
98 std::env::var("AIEN_REQUIRE_NATIVE_LIBS").as_deref() != Ok("1"),
99 "aien-omega-gpu: AIEN_REQUIRE_NATIVE_LIBS=1 but neither AIEN_OMEGA_DIR nor AIEN_OMEGA_GPU_LIB is set (run scripts/pins.sh --gpu and source target/pins/env.sh)"
100 );
101 println!("cargo:warning=aien-omega-gpu: no AIEN_OMEGA_DIR or AIEN_OMEGA_GPU_LIB, building the CPU stub");
102 build::Identity::stub(&pinned).emit(PREFIX);
103 return;
104 };
105 let dir = PathBuf::from(dir);
106 build::require_pinned_checkout("aien-omega-gpu", &dir, &pinned);
107 let physics = std::env::var("AIEN_PHYSICS_DIR")
108 .map(PathBuf::from)
109 .unwrap_or_else(|_| dir.join("../physics"));
110 let out = PathBuf::from(env("OUT_DIR")).join("omega-build");
111 if build::fresh_out_dir(&out, &pinned) {
112 println!("cargo:warning=aien-omega-gpu: build dir was for another omega commit, rebuilt from clean");
113 }
114 let status = Command::new("make")
115 .current_dir(&dir)
116 .arg(format!("OUT_DIR={}", out.display()))
117 .arg(format!("PHYSICS_DIR={}", physics.display()))
118 .arg(out.join("libomega_gpu.a"))
119 .status()
120 .expect("run make");
121 assert!(
122 status.success(),
123 "make libomega_gpu failed in {}",
124 dir.display()
125 );
126 let archive = out.join("libomega_gpu.a");
127 build::qualify_built(
128 "aien-omega-gpu",
129 &manifest,
130 &artifacts_lock,
131 &dir,
132 &physics,
133 &archive,
134 &pinned,
135 true,
136 )
137 .emit(PREFIX);
138 link(&archive);
139}
140
141fn link(lib: &Path) {
142 let dir = lib.parent().expect("lib has a parent dir");
143 println!("cargo:rustc-link-search=native={}", dir.display());
144 println!("cargo:rustc-link-lib=static=omega_gpu");
145 println!("cargo:rustc-link-lib=dylib=pthread");
146 println!("cargo:rustc-link-lib=dylib=m");
147 println!("cargo:rerun-if-changed={}", lib.display());
148 println!("cargo:rustc-cfg=has_omega_gpu");
149}
150
151fn env(k: &str) -> String {
152 std::env::var(k).unwrap_or_else(|_| panic!("{k} not set"))
153}