bock 1.0.0

The Bock language compiler and CLI — feature-declarative, target-agnostic programming language with AI-native transpilation
//! Cargo build script: embeds the Bock core standard library sources into the
//! `bock` binary.
//!
//! Walks `stdlib/core/**/*.bock` (relative to the repo root) and generates
//! `$OUT_DIR/stdlib_embedded.rs`, which `src/stdlib.rs` `include!`s. Each entry
//! is `(logical_path, include_str!(absolute_path))`, so the source text is
//! baked into the binary at compile time — `bock` resolves `core.*` with no
//! filesystem access at run time. A `rerun-if-changed` directive on the stdlib
//! tree forces a rebuild whenever any core source changes.

use std::fs;
use std::path::{Path, PathBuf};

fn main() {
    // The crate lives at `<repo>/compiler/crates/bock-cli`; the stdlib tree is
    // at `<repo>/stdlib`. Resolve it from CARGO_MANIFEST_DIR so the build is
    // independent of the invocation cwd.
    let manifest_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
    let repo_root = manifest_dir
        .join("../../..")
        .canonicalize()
        .expect("resolve repo root from bock-cli manifest dir");
    let core_dir = repo_root.join("stdlib/core");

    // Rebuild if any core stdlib source changes.
    println!("cargo:rerun-if-changed={}", core_dir.display());
    println!("cargo:rerun-if-env-changed=BOCK_STDLIB");

    let mut sources: Vec<(String, PathBuf)> = Vec::new();
    collect_bock_sources(&core_dir, &core_dir, &mut sources);
    // Deterministic order so the generated file is stable across builds.
    sources.sort_by(|a, b| a.0.cmp(&b.0));

    let mut generated = String::from(
        "// @generated by build.rs — embedded core stdlib sources. Do not edit.\n\
         &[\n",
    );
    for (rel, abs) in &sources {
        // `logical_path` is repo-relative (`stdlib/core/...`) for diagnostics;
        // the include path is absolute so it resolves regardless of cwd.
        let logical = format!("stdlib/core/{rel}");
        generated.push_str(&format!(
            "    ({logical:?}, include_str!({abs:?})),\n",
            abs = abs.display().to_string(),
        ));
        // Each embedded file must also trigger a rebuild when it changes.
        println!("cargo:rerun-if-changed={}", abs.display());
    }
    generated.push_str("]\n");

    let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
    fs::write(out_dir.join("stdlib_embedded.rs"), generated)
        .expect("write generated stdlib_embedded.rs");
}

/// Recursively collect `*.bock` files under `dir`, recording each as
/// `(path_relative_to_root, absolute_path)`.
fn collect_bock_sources(root: &Path, dir: &Path, out: &mut Vec<(String, PathBuf)>) {
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return,
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            collect_bock_sources(root, &path, out);
        } else if path.extension().and_then(|e| e.to_str()) == Some("bock") {
            let rel = path
                .strip_prefix(root)
                .expect("path under core root")
                .to_string_lossy()
                .replace('\\', "/");
            out.push((rel, path));
        }
    }
}