rlx-runtime 0.2.18

RLX runtime — feature-gated backends, session API, compile+execute pipeline
// RLX — versatile ML compiler + runtime.
// Copyright (C) 2026 Eugene Hauptmann, Nataliya Kosmyna.
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Emits `backends_manifest.json` describing which backend Cargo features
//! were enabled for this build (plan: deploy-time manifest).

use std::env;
use std::fs;
use std::path::Path;

fn main() {
    let out_dir = env::var("OUT_DIR").expect("OUT_DIR");
    let out = Path::new(&out_dir);
    let manifest_path = out.join("backends_manifest.json");

    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
    println!("cargo::rustc-check-cfg=cfg(rlx_mlx_host)");
    // MLX has a real backend on macOS / Linux / Windows and on the Apple
    // platforms that ship Metal — iOS, tvOS and visionOS (device + simulator).
    // **watchOS keeps the stub**: no public Metal API, so MLX has no backend to
    // build there.
    //
    // Three build scripts gate on this same fact and must move together —
    // rlx-mlx/build.rs, rlx-runtime/build.rs (the module + registry entry) and
    // rlx-mlx-sys/build.rs (whether libmlx is cross-compiled at all). Out of
    // sync gives "no backend registered for MLX" at runtime, or a link against
    // symbols nothing built.
    if matches!(
        target_os.as_str(),
        "macos" | "linux" | "windows" | "ios" | "tvos" | "visionos"
    ) {
        println!("cargo:rustc-cfg=rlx_mlx_host");
    }

    let backends: Vec<&str> = [
        ("cpu", "cpu"),
        ("metal", "metal"),
        ("mlx", "mlx"),
        ("gpu", "gpu"),
        ("cuda", "cuda"),
        ("rocm", "rocm"),
        ("tpu", "tpu"),
        ("vulkan", "vulkan"),
        ("oneapi", "oneapi"),
        ("opengl", "opengl"),
        ("directx", "directx"),
        ("webgpu", "webgpu"),
        ("ane", "ane"),
        ("blas-accelerate", "blas-accelerate"),
        ("blas-mkl", "blas-mkl"),
        ("blas-openblas", "blas-openblas"),
    ]
    .into_iter()
    .filter_map(|(feature, label)| {
        let key = format!("CARGO_FEATURE_{}", feature.replace('-', "_").to_uppercase());
        env::var(key).ok().map(|_| label)
    })
    .collect();

    let json = serde_json::json!({
        "crate": "rlx-runtime",
        "version": env!("CARGO_PKG_VERSION"),
        "backends": backends,
    });
    fs::write(
        &manifest_path,
        serde_json::to_string_pretty(&json).expect("json"),
    )
    .expect("write backends_manifest.json");
    println!(
        "cargo:rustc-env=RLX_BACKENDS_MANIFEST_PATH={}",
        manifest_path.display()
    );
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=Cargo.toml");
}