diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..cbcd493 --- /dev/null +++ b/build.zig @@ -0,0 +1,84 @@ +const std = @import("std"); + +/// What each area's build file (`build/*.zig`) is handed. +pub const Ctx = struct { + b: *std.Build, + target: std.Build.ResolvedTarget, + optimize: std.builtin.OptimizeMode, + /// The tracing library: events, ring, recorder, backends. + ztrace: *std.Build.Module, + /// The `ztrace` command. + exe: *std.Build.Step.Compile, + /// `zig build test`: everything. + test_step: *std.Build.Step, + + /// A C program to trace, from `targets/`: -O1 with debug info, frame + /// pointers kept, so its code is short and still maps to its source. + pub fn cTarget(ctx: Ctx, name: []const u8, files: []const []const u8) *std.Build.Step.Compile { + const mod = ctx.b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false }); + mod.addCSourceFiles(.{ .root = ctx.b.path("targets"), .files = files, .flags = &.{ "-O1", "-g", "-fno-omit-frame-pointer" } }); + return ctx.b.addExecutable(.{ .name = name, .root_module = mod }); + } + + /// A Zig program to trace, from `targets/`, with the library importable + /// as `ztrace`. + pub fn zigTarget(ctx: Ctx, name: []const u8, file: []const u8) *std.Build.Step.Compile { + return ctx.b.addExecutable(.{ .name = name, .root_module = ctx.b.createModule(.{ + .root_source_file = ctx.b.path(file), + .target = ctx.target, + .optimize = ctx.optimize, + .imports = &.{.{ .name = "ztrace", .module = ctx.ztrace }}, + }) }); + } + + /// A step of its own for one area's tests, also part of `test`. + pub fn areaTest(ctx: Ctx, name: []const u8, description: []const u8) *std.Build.Step { + const step = ctx.b.step(name, description); + ctx.test_step.dependOn(step); + return step; + } +}; + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const xitdb = b.dependency("xitdb", .{}).module("xitdb"); + const ztrace = b.addModule("ztrace", .{ + .root_source_file = b.path("src/trace/root.zig"), + .target = target, + .optimize = optimize, + .imports = &.{.{ .name = "xitdb", .module = xitdb }}, + }); + + const exe = b.addExecutable(.{ .name = "ztrace", .root_module = b.createModule(.{ + .root_source_file = b.path("src/cli/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{.{ .name = "ztrace", .module = ztrace }}, + }) }); + b.installArtifact(exe); + + const ctx: Ctx = .{ .b = b, .target = target, .optimize = optimize, .ztrace = ztrace, .exe = exe, .test_step = b.step("test", "Run every test") }; + + // The library's own tests, and a target with its own producer recorded + // through the ring. + const core = ctx.areaTest("test-core", "Events, ring, recorder, ptrace basics"); + core.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ztrace })).step); + const emit = b.addRunArtifact(exe); + emit.addArgs(&.{ "run", "-o" }); + _ = emit.addOutputFileArg("emit.ztrace"); + emit.addArg("--"); + emit.addArtifactArg(ctx.zigTarget("emit", "targets/emit.zig")); + emit.addArg("5000"); + emit.expectStdErrMatch("5001 events (0 dropped)"); + emit.expectStdErrMatch("exit 7"); + core.dependOn(&emit.step); + + // Each area adds its own artifacts, targets and test step. + @import("build/debugger.zig").add(ctx); + @import("build/agent.zig").add(ctx); + @import("build/llvm.zig").add(ctx); + @import("build/kernel.zig").add(ctx); + @import("build/anim.zig").add(ctx); +} |
