open-standards/wai/wai-rs/build.rs
Transaction Science 46c5688b86 wai: the WAI open standard — full cargo-class surface + provenance
Commit the WAI standard source: a content + capability-dispatch standard
whose thesis is "carries whatever it can reconstruct, verifies whatever
it carries." Source only — large vendored/model assets and build output
are gitignored.

Reconstruct classes (each: spec + conformance corpus + in-tab browser
sink that re-derives the corpus hash, one wai.det.fixed64 fixed-point
floor, one *-equivalence apiece):
  - interactive worlds  (replay-equivalence)   + 3 sim registrants,
    multiplayer over a relay, session receipts, glTF + real SPZ assets
  - spatial audio        (mixdown-equivalence)
  - haptics              (sample-equivalence)
  - state-feeds          (replay-equivalence, non-physics sim)
  - avatars / digital humans (pose-equivalence) + telepresence over the relay
  - volumetric / 4D splats   (frame-state-equivalence) + WebGPU EWA render,
    real-SPZ-derived clip

Verify half (lever 4):
  - asset provenance: a signed, energy-accounted lineage for opaque
    captured content WAI does not reconstruct; tamper-evident, in-tab.

Cross-cutting: JWP receipt profiles per class; a native energy meter
(IOReport via macmon, no sudo) emitting receipts with REAL measured
joules; the learned-image/neural codec family and JPEG AI entropy port
from earlier work.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-06-13 22:28:51 -04:00

83 lines
3.4 KiB
Rust

//! Cross-platform linker paths for the C libraries we wrap.
//!
//! Most of our codec deps (`opus`, `jpegxl-rs`, `dav1d`, `zstd`, `xz2`,
//! `libavif-image`) already have build.rs files that probe pkg-config
//! themselves. `flac-bound` does NOT — it just links `-lflac` and
//! expects the linker to find it. On Homebrew macOS that lives under
//! `/opt/homebrew/opt/flac/lib`, which Cargo doesn't probe. We use the
//! `pkg-config` crate to discover the right path on whatever system
//! the user is on (Linux distros, macOS Homebrew, BSD ports, etc.).
//!
//! This replaces the macOS-hardcoded `.cargo/config.toml` rustflags.
fn main() {
generate_c_header();
// Only the `codecs` feature compiles flac-bound — and pkg-config's
// probe() emits a `cargo:rustc-link-lib=flac` of its own, so running
// it unconditionally makes every consumer (e.g. the wasm32 world
// build, the wai-world-relay binary) link libFLAC it never calls.
if std::env::var_os("CARGO_FEATURE_CODECS").is_some() {
link_libflac();
}
}
/// Regenerate `include/wai.h` from the FFI surface so SDK consumers can
/// always grab a typed header that matches the shipped binary. Only
/// regenerates when source files actually change (cargo handles this
/// via `rerun-if-changed` declarations).
fn generate_c_header() {
println!("cargo:rerun-if-changed=src/ffi.rs");
println!("cargo:rerun-if-changed=src/container.rs");
println!("cargo:rerun-if-changed=cbindgen.toml");
let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let out_path = std::path::Path::new(&crate_dir).join("include/wai.h");
if let Err(e) = std::fs::create_dir_all(out_path.parent().unwrap()) {
println!("cargo:warning=cbindgen: could not create include/ dir: {e}");
return;
}
match cbindgen::Builder::new()
.with_crate(&crate_dir)
.with_config(cbindgen::Config::from_file(
std::path::Path::new(&crate_dir).join("cbindgen.toml")).unwrap_or_default())
.generate()
{
Ok(bindings) => {
bindings.write_to_file(&out_path);
}
Err(e) => {
println!("cargo:warning=cbindgen failed: {e}");
}
}
}
fn link_libflac() {
if let Ok(lib) = pkg_config::Config::new()
.atleast_version("1.3")
.probe("flac")
{
for path in &lib.link_paths {
println!("cargo:rustc-link-search=native={}", path.display());
}
// pkg-config also emits the library names; flac-bound links its
// own `-lflac` directive so we only need the search path.
} else {
// Fallback: try the canonical Homebrew location on macOS so the
// common case still works without pkg-config installed.
#[cfg(target_os = "macos")]
{
let candidates = [
"/opt/homebrew/opt/flac/lib", // Apple Silicon
"/usr/local/opt/flac/lib", // Intel macOS
];
for p in candidates {
if std::path::Path::new(p).exists() {
println!("cargo:rustc-link-search=native={p}");
return;
}
}
}
// Linux/other: hope that the system linker finds -lflac on its own.
// If not, the link error will be obvious ("cannot find -lflac")
// and the user needs to install libflac-dev / equivalent.
}
}