// SPDX-License-Identifier: LCL-1.0
// Copyright (c) 2026 Markus Maiwald
// Stewardship: Self Sovereign Society Foundation
//
// This file is part of the Nexus Commonwealth.
// See legal/LICENSE_COMMONWEALTH.md for license terms.

//! Rumpk Build System
//!
//! Orchestrates L0 (Zig) and L1 (Nim) compilation.
//! Builds the Hardware Abstraction Layer as a static library.

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // =========================================================
    // L0: Hardware Abstraction Layer (Zig)
    // =========================================================

    // NOTE(Build): Zig 0.15.x API - using addLibrary with static linkage
    const hal_mod = b.createModule(.{
        .root_source_file = b.path("hal/abi.zig"),
        .target = target,
        .optimize = optimize,
    });
    // Freestanding kernel - no libc, no red zone, no stack checks
    hal_mod.red_zone = false;
    hal_mod.stack_check = false;

    const hal = b.addLibrary(.{
        .name = "rumpk_hal",
        .root_module = hal_mod,
        .linkage = .static,
    });

    b.installArtifact(hal);

    // TODO(Build): Microui needs stdio.h stubs for freestanding.
    // Re-enable after creating libs/microui/stdio_stub.h
    // Microui Integration (Phase 3.5b)
    // hal_mod.addIncludePath(b.path("libs/microui"));
    // hal_mod.addCSourceFile(.{
    //     .file = b.path("libs/microui/microui.c"),
    //     .flags = &.{"-std=c99"},
    // });

    // =========================================================
    // Boot: Entry Point (Assembly + Zig)
    // =========================================================

    const boot_mod = b.createModule(.{
        .root_source_file = b.path("boot/header.zig"),
        .target = target,
        .optimize = optimize,
    });
    boot_mod.red_zone = false;
    boot_mod.stack_check = false;

    const boot = b.addObject(.{
        .name = "boot",
        .root_module = boot_mod,
    });

    _ = boot; // Mark as used for now

    // =========================================================
    // Tests
    // =========================================================

    const test_mod = b.createModule(.{
        .root_source_file = b.path("hal/abi.zig"),
        .target = target,
        .optimize = optimize,
    });

    const hal_tests = b.addTest(.{
        .root_module = test_mod,
    });

    const run_tests = b.addRunArtifact(hal_tests);
    const test_step = b.step("test", "Run Rumpk HAL tests");
    test_step.dependOn(&run_tests.step);
}
