Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250//! The repo build: the engine plus the engine's modes as build steps.//!//! The engine is the Zig seed under `src/seed/`: one binary, `interv-seed`,//! built here and installed at `zig-out/bin/interv-seed`. This file owns//! that build and exposes the command layer on top of it: one step per//! engine mode, plus the native helpers the engine's own backend produces.//!//! zig build -l list the steps//! zig build build and install the engine//! zig build eval -- docs/examples/hello_world.interv//! zig build build-exe -- docs/examples/native/hello.interv -o /tmp/hello//!//! The engine is a ReleaseFast build by default; `-Doptimize=Debug` selects//! the checked allocator for chasing reference-counting bugs. Arguments//! after `--` are passed through to the engine verbatim; every step runs//! with the repo root as its working directory.
const std = @import("std");
// The embedding API: a consumer's `build.zig` imports this file (a// package dependency's build.zig is importable by name) and calls// `addIntervFiles(exe, ...)`.pub const Options = @import("src/build_api.zig").Options;pub const addIntervFiles = @import("src/build_api.zig").addIntervFiles;
const SELF_STACK = [_][]const u8{ "src/ast.interv", "src/lexer.interv", "src/sugar.interv", "src/front.interv", "src/parser.interv", "src/eval.interv", "src/cli.interv",};
const CHECKER_STACK = [_][]const u8{ "src/ast.interv", "src/lexer.interv", "src/sugar.interv", "src/front.interv", "src/parser.interv", "src/eval.interv", "src/typecheck.interv", "src/check.interv",};
pub fn build(b: *std.Build) void { _ = b.addModule("interv", .{ .root_source_file = b.path("src/build_api.zig"), });
const optimize = b.option( std.builtin.OptimizeMode, "optimize", "Engine optimize mode (default ReleaseFast)", ) orelse .fast;
// The engine. It is a host program -- cross builds are the engine's own // `build-exe --target`/`--arch` modes -- so the seed itself is always // built for the host. const exe = b.addExecutable(.{ .name = "interv-seed", .root_module = b.createModule(.{ .root_source_file = b.path("src/seed/main.zig"), .target = b.graph.host, .optimize = optimize, }), });
// `zig build seed`: the engine on disk at zig-out/bin/interv-seed, the // path every step and the Python gate (verify/lib.py, verify/lib.ts) // invokes it by. const seed_install = b.addInstallArtifact(exe, .{}); const seed_step = b.step("seed", "Build the engine: zig-out/bin/interv-seed"); seed_step.dependOn(&seed_install.step);
// Unit tests: `zig build test` (formerly `cd src/seed && zig build test`). const unit = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/seed/tests.zig"), .target = b.graph.host, .optimize = optimize, }), }); const run_unit = b.addRunArtifact(unit); const test_step = b.step("test", "Run the engine's unit tests (src/seed/tests.zig)"); test_step.dependOn(&run_unit.step);
// `zig build install` (the default step): the engine as // <prefix>/bin/interv and the stdlib as <prefix>/lib/interv/std. The // driver appends `<exe dir>/../lib/interv` to its module search roots, // which is what makes an installed `interv` resolve `import std/...` // with no flags and no $INTERV_PATH. const install_interv = b.addInstallFileWithDir( exe.getEmittedBin(), .bin, "interv", ); // The install is self-contained: the stdlib plus everything the engine // reads at run time (the self-hosted stacks, the compiler's default // profile, the link runtime, the corpus selfverify/fixpoint walk), all // under <prefix>/lib/interv. const install_dirs = [_]struct { src: []const u8, sub: []const u8 }{ .{ .src = "src/std", .sub = "interv/std" }, .{ .src = "src", .sub = "interv/src" }, .{ .src = "docs/examples", .sub = "interv/examples" }, }; b.getInstallStep().dependOn(&seed_install.step); b.getInstallStep().dependOn(&install_interv.step); for (install_dirs) |d| { const inst = b.addInstallDirectory(.{ .source_dir = b.path(d.src), .install_dir = .lib, .install_subdir = d.sub, }); b.getInstallStep().dependOn(&inst.step); }
// One step per engine mode. `--` args go to the engine. addMode(b, exe, &seed_install.step, "eval", "Interpret a program: -- FILE [ARGS...]", &.{"eval"}); addMode(b, exe, &seed_install.step, "rewrite", "Render a construction-chain trace as .interv: -- TRACE.ivt", &.{"rewrite"}); addMode(b, exe, &seed_install.step, "check", "Type/usage check before running: -- FILES...", &.{"check"}); addMode(b, exe, &seed_install.step, "proof", "Cubical proof driver (check, norm, axioms): -- check FILE.intervp", &.{"proof"}); addMode(b, exe, &seed_install.step, "ssa", "The compiler's IL text on stdout: -- FILE", &.{"ssa"}); addMode(b, exe, &seed_install.step, "llvm", "LLVM IR text, the second sink: -- FILE", &.{"llvm"}); addMode(b, exe, &seed_install.step, "build-exe", "Native binary (our backend + zig cc): -- FILE [-o OUT]", &.{"build-exe"}); addMode(b, exe, &seed_install.step, "selftest", "Corpus parity: the seed against the self-hosted stack", &.{"selftest"}); addMode(b, exe, &seed_install.step, "fixpoint", "Level-2 self-interpretation parity: -- FILES...", &.{"fixpoint"});
addMode(b, exe, &seed_install.step, "asm", "GAS for a program, our backend's machine code: -- FILE", &.{"asm"});
// The native helpers the engine's own backend produces. const helper_self = b.addRunArtifact(exe); helper_self.addArgs(&.{"build-exe"}); helper_self.addArgs(&SELF_STACK); helper_self.addArgs(&.{ "--llvm", "-o", "build/bin/interv-self" }); finish(b, &seed_install.step, helper_self, "build-self", "The self-hosted interpreter compiled to build/bin/interv-self", false);
const helper_check = b.addRunArtifact(exe); helper_check.addArgs(&.{"build-exe"}); helper_check.addArgs(&CHECKER_STACK); helper_check.addArgs(&.{ "--llvm", "-o", "build/bin/interv-check" }); finish(b, &seed_install.step, helper_check, "build-check", "The type checker compiled to build/bin/interv-check", false);
const helpers_step = b.step("helpers", "Both native helpers under build/bin/ (interv-self and interv-check)"); helpers_step.dependOn(&helper_self.step); helpers_step.dependOn(&helper_check.step);
// The Python gate (verify/run.py): the engine is a build-graph // dependency, so the gate itself needs no prelude beyond checking that // the binary exists. const verify_run = b.addSystemCommand(&.{ "python3", "verify/run.py" }); verify_run.addPassthruArgs(); verify_run.stdio = .inherit; verify_run.setCwd(b.path(".")); // Only the engine is a build dependency: the helpers are stamp-cached by // the gate itself (ensure_native), so making them a build step would // rebuild them on every run -- build steps always execute. verify_run.step.dependOn(&seed_install.step); const verify_step = b.step("verify", "Run the Python gate: zig build verify -- [--only parity|readme] ..."); verify_step.dependOn(&verify_run.step);
// The compiled self-hosted interpreter on a program. { const run = b.addSystemCommand(&.{ "build/bin/interv-self", "eval" }); finish(b, &helper_self.step, run, "self", "Run the compiled self-hosted interpreter: -- FILE [ARGS...]", true); }
// The terminal demoscene: expand its sugar, then play it, both as build // steps (no shell script: the demo runs with the same Zig toolchain the // rest of the tree needs). const demo_exe = b.addExecutable(.{ .name = "interv-demo", .root_module = b.createModule(.{ .root_source_file = b.path("verify/demoscene/supervisor.zig"), .target = b.graph.host, .optimize = optimize, }), }); const demo_install = b.addInstallArtifact(demo_exe, .{}); b.getInstallStep().dependOn(&demo_install.step);
// The demoscene's syntax scanner: the engine's lexer and the parser's // keyword predicate over one file. The demo spawns it, so it installs // beside the supervisor. const syntax_mod = b.createModule(.{ .root_source_file = b.path("src/seed/syntax.zig"), .target = b.graph.host, .optimize = optimize, }); const scan_exe = b.addExecutable(.{ .name = "interv-scan", .root_module = b.createModule(.{ .root_source_file = b.path("verify/demoscene/scan.zig"), .target = b.graph.host, .optimize = optimize, .imports = &.{ .{ .name = "syntax", .module = syntax_mod }, }, }), }); const scan_install = b.addInstallArtifact(scan_exe, .{}); b.getInstallStep().dependOn(&scan_install.step);
const expand = b.addSystemCommand(&.{ "zig-out/bin/interv-seed", "expand" }); const expanded = expand.addOutputFileArg2("demo.interv", .{ .prefix = "-o" }); expand.addFileArg(b.path("verify/demoscene/demo.interv")); expand.step.dependOn(&seed_install.step); const demo = b.addSystemCommand(&.{ "zig-out/bin/interv-demo", "zig-out/bin/interv-seed" }); demo.addFileArg(expanded); finish(b, &demo_install.step, demo, "demo", "Play the terminal demoscene: -- [DEMO ARGS...]", true); demo.step.dependOn(&scan_install.step); // The RSS watchdog is a measurement, not the interactive step: its own // binary, its own step, the frame count after `--`. const rss_exe = b.addExecutable(.{ .name = "interv-demo-rss", .root_module = b.createModule(.{ .root_source_file = b.path("verify/demoscene/memcheck.zig"), .target = b.graph.host, .optimize = optimize, }), }); const rss = b.addRunArtifact(rss_exe); rss.addArg("zig-out/bin/interv-seed"); rss.addFileArg(expanded); finish(b, &seed_install.step, rss, "demo-rss", "Sample the demo's RSS: -- FRAMES", true); rss.step.dependOn(&scan_install.step);}
/// A step that runs the engine with a mode token and `--` args appended.fn addMode( b: *std.Build, exe: *std.Build.Step.Compile, dep: *std.Build.Step, name: []const u8, desc: []const u8, mode: []const []const u8,) void { const run = b.addRunArtifact(exe); run.addArgs(mode); finish(b, dep, run, name, desc, true);}
/// Wire a run step into the build graph: side-effect stdio, the repo root as/// cwd, `--` passthrough, a dependency on the engine, and its own named step.fn finish( b: *std.Build, dep: *std.Build.Step, run: *std.Build.Step.Run, name: []const u8, desc: []const u8, passthru: bool,) void { if (passthru) run.addPassthruArgs(); run.stdio = .inherit; run.setCwd(b.path(".")); run.step.dependOn(dep); const s = b.step(name, desc); s.dependOn(&run.step);}