~ knotless Search/
↑↓ move↵ open⇧↵ all results

zat.dev/zds

atproto pds · mirror of https://tangled.org/zat.dev/zds

0
0
242 lines · 10.1 KB Raw History
const std = @import("std");
const manifest = @import("build.zig.zon");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});
    const version = b.option([]const u8, "version", "Build version reported by /xrpc/_health") orelse manifest.version;
    const web_garden = b.option(bool, "web-garden", "Embed the garden skin (WebGL scene, its script and ground textures, about 250 KB); without it ZDS_WEB_SKIN=garden is refused") orelse false;

    const zat = b.dependency("zat", .{
        .target = target,
        .optimize = optimize,
    });
    const zqlite = b.dependency("zqlite", .{
        .target = target,
        .optimize = optimize,
    });
    const webauthn = b.dependency("webauthn", .{
        .target = target,
        .optimize = optimize,
    });
    const metrics = b.dependency("metrics", .{
        .target = target,
        .optimize = optimize,
    });
    const websocket = b.dependency("websocket", .{
        .target = target,
        .optimize = optimize,
    });
    // zat and httpz must share one websocket module.
    const zat_mod = zat.module("zat");
    zat_mod.addImport("websocket", websocket.module("websocket"));

    const httpz = b.dependency("httpz", .{
        .target = target,
        .optimize = optimize,
    });
    const httpz_mod = b.addModule("httpz", .{
        .link_libc = true,
        .root_source_file = httpz.path("src/httpz.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "metrics", .module = metrics.module("metrics") },
            .{ .name = "websocket", .module = websocket.module("websocket") },
        },
    });
    {
        const options = b.addOptions();
        options.addOption(bool, "httpz_blocking", false);
        httpz_mod.addOptions("build", options);
    }

    const drawing_manifest = b.option([]const u8, "web-drawings", "Path to an explicitly selected cached drawing manifest");
    const web_drawings = b.createModule(.{
        .root_source_file = b.path(drawing_manifest orelse "src/internal/web/icons/defaults.zig"),
        .target = target,
        .optimize = optimize,
    });

    const mod = b.addModule("zds", .{
        .link_libc = true,
        .root_source_file = b.path("src/root.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{
            .{ .name = "build_options", .module = buildOptions(b, version, web_garden) },
            .{ .name = "web_drawings", .module = web_drawings },
            .{ .name = "zat", .module = zat_mod },
            .{ .name = "zqlite", .module = zqlite.module("zqlite") },
            .{ .name = "webauthn", .module = webauthn.module("webauthn") },
            .{ .name = "httpz", .module = httpz_mod },
        },
    });
    mod.addCSourceFile(.{ .file = b.path("src/http/landing/preview/render.c"), .flags = &.{"-std=c99"} });

    const exe = b.addExecutable(.{
        .name = "zds",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "zds", .module = mod }},
        }),
    });
    // DWARF is about three quarters of a ReleaseSafe binary. Compressed, the
    // image stays small and a panic still prints file and line.
    exe.compress_debug_sections = .zlib;
    b.installArtifact(exe);

    const run_cmd = b.addRunArtifact(exe);
    run_cmd.addPassthruArgs();

    const run_step = b.step("run", "Run zds");
    run_step.dependOn(&run_cmd.step);

    const bench = b.addExecutable(.{
        .name = "zds-bench",
        .root_module = b.createModule(.{
            .root_source_file = b.path("bench/main.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "zds", .module = mod }},
        }),
    });
    b.installArtifact(bench);

    const run_bench = b.addRunArtifact(bench);
    run_bench.addPassthruArgs();

    const bench_step = b.step("bench", "Run local ZDS benchmarks");
    bench_step.dependOn(&run_bench.step);

    const scope_bench = b.addTest(.{ .filters = &.{"cached dynamic collection authorization"}, .root_module = b.createModule(.{
        .root_source_file = b.path("bench/space_authorization.zig"),
        .target = target,
        .optimize = optimize,
        .imports = &.{.{ .name = "zds", .module = mod }},
    }) });
    const run_scope_bench = b.addRunArtifact(scope_bench);
    run_scope_bench.has_side_effects = true;
    b.step("bench-space-auth", "Measure cached dynamic collection authorization").dependOn(&run_scope_bench.step);

    const rpc_bench = b.addExecutable(.{
        .name = "rpc-scopes-bench",
        .root_module = b.createModule(.{
            .root_source_file = b.path("bench/rpc_scopes.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "scopes", .module = b.createModule(.{
                .root_source_file = b.path("src/internal/scopes.zig"),
                .target = target,
                .optimize = optimize,
                .imports = &.{.{ .name = "zat", .module = zat.module("zat") }},
            }) }},
        }),
    });
    const run_rpc_bench = b.addRunArtifact(rpc_bench);
    run_rpc_bench.has_side_effects = true;
    b.step("bench-rpc-scopes", "Measure encoded RPC audience authorization").dependOn(&run_rpc_bench.step);

    if (b.option(bool, "hegel", "Build optional property campaigns") orelse false) {
        if (b.lazyDependency("hegel", .{ .target = target, .optimize = optimize })) |hegel| {
            const fuzz_filter = b.option([]const u8, "fuzz-filter", "Select one campaign for replay") orelse "campaign ";
            const properties = b.addTest(.{ .name = "zds-properties", .filters = &.{fuzz_filter}, .root_module = b.createModule(.{
                .root_source_file = b.path("tools/fuzz/root.zig"),
                .target = target,
                .optimize = optimize,
                .imports = &.{
                    .{ .name = "zds", .module = mod },
                    .{ .name = "hegel", .module = hegel.module("hegel") },
                },
            }) });
            const run_properties = b.addRunArtifact(properties);
            run_properties.has_side_effects = true;
            b.step("fuzz", "Run the property campaigns").dependOn(&run_properties.step);
            b.step("install-properties", "Build the property campaigns for hegel-campaign").dependOn(&b.addInstallArtifact(properties, .{}).step);
            b.step("install-campaign", "Install the pinned Hegel campaign runner").dependOn(&b.addInstallArtifact(hegel.artifact("hegel-campaign"), .{}).step);
        }
    }

    const plc_repair = b.addExecutable(.{
        .name = "zds-plc-repair",
        .root_module = b.createModule(.{
            .root_source_file = b.path("tools/plc_repair.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "zds", .module = mod }},
        }),
    });
    plc_repair.compress_debug_sections = .zlib;
    b.installArtifact(plc_repair);

    const run_plc_repair = b.addRunArtifact(plc_repair);
    run_plc_repair.addPassthruArgs();

    const plc_repair_step = b.step("plc-repair", "Repair an existing account PLC rotation key set");
    plc_repair_step.dependOn(&run_plc_repair.step);

    const drawings = b.addExecutable(.{
        .name = "zds-drawings",
        .root_module = b.createModule(.{
            .root_source_file = b.path("tools/drawings.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "zds", .module = mod }},
        }),
    });
    const run_drawings = b.addRunArtifact(drawings);
    run_drawings.addPassthruArgs();
    b.step("drawings", "Cache operator-selected Doodl drawings").dependOn(&run_drawings.step);

    const space_signature = b.addExecutable(.{
        .name = "zds-space-signature",
        .root_module = b.createModule(.{
            .root_source_file = b.path("tools/space_signature.zig"),
            .target = target,
            .optimize = optimize,
            .imports = &.{.{ .name = "zds", .module = mod }},
        }),
    });
    b.installArtifact(space_signature);

    // The deployed image needs the server and its repair tool. Each binary is a
    // whole-program ReleaseSafe compile, so a deploy builds only these.
    const deploy_build = b.step("deploy-build", "Install the binaries the deployed image ships");
    deploy_build.dependOn(&b.addInstallArtifact(exe, .{}).step);
    deploy_build.dependOn(&b.addInstallArtifact(plc_repair, .{}).step);

    // HTTP smoke tests need these two binaries, not the bench/admin tools.
    const smoke_build = b.step("smoke-build", "Install binaries for HTTP smoke tests");
    smoke_build.dependOn(&b.addInstallArtifact(exe, .{}).step);
    smoke_build.dependOn(&b.addInstallArtifact(space_signature, .{}).step);

    const run_space_signature = b.addRunArtifact(space_signature);
    run_space_signature.addPassthruArgs();
    const space_signature_step = b.step("space-signature", "Create permissioned-data HTTP signature headers for local testing");
    space_signature_step.dependOn(&run_space_signature.step);

    const tests = b.addTest(.{ .root_module = mod });
    const run_tests = b.addRunArtifact(tests);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe.step);
    test_step.dependOn(&bench.step);
    test_step.dependOn(&plc_repair.step);
    test_step.dependOn(&space_signature.step);
    test_step.dependOn(&run_tests.step);
    const drawing_tests = b.addTest(.{ .root_module = drawings.root_module });
    test_step.dependOn(&b.addRunArtifact(drawing_tests).step);
}

/// Options files are content-addressed, and zig rejects one file in two
/// modules, so the name keeps this distinct from a dependency that also
/// exports only its version, as zat at 0.6.0 does.
fn buildOptions(b: *std.Build, version: []const u8, web_garden: bool) *std.Build.Module {
    const options = b.addOptions();
    options.addOption([]const u8, "name", "zds");
    options.addOption([]const u8, "version", version);
    options.addOption(bool, "web_garden", web_garden);
    return options.createModule();
}