Skip to content

Commit

Permalink
add tests, docs, demo, and serve steps to build.zig
Browse files Browse the repository at this point in the history
  • Loading branch information
pyotrk committed May 24, 2026
1 parent 8af5d83 commit 79e4416
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

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

Expand All @@ -9,11 +9,98 @@ pub fn build(b: *std.Build) void {
.target = target,
.optimize = optimize,
});
const lib_test = b.addTest(.{ .root_module = lib_module, .name = "lib tests" });
const lib_test_run = b.addRunArtifact(lib_test);

const server_module = b.addModule("crdt_server", .{
.root_source_file = b.path("crdt-server/src/root.zig"),
.target = target,
.optimize = optimize,
});
server_module.addImport("crdt", lib_module);
const server_test = b.addTest(.{ .root_module = server_module, .name = "server tests" });
const server_test_run = b.addRunArtifact(server_test);

const server_main = b.createModule(.{
.root_source_file = b.path("crdt-server/src/main.zig"),
.target = target,
.optimize = optimize,
});
server_main.addImport("crdt-server", server_module);

const server_exe = b.addExecutable(.{
.name = "crdt-server",
.root_module = server_main,
});

const run_server = b.addRunArtifact(server_exe);
if (b.args) |args| {
run_server.addArgs(args);
}

const demo_module = b.createModule(.{
.root_source_file = b.path("crdt-demo/src/demo.zig"),
.target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding }),
.optimize = optimize,
});
demo_module.addImport("crdt", lib_module);

const demo_wasm = b.addExecutable(.{
.name = "crdt-demo",
.root_module = demo_module,
});
demo_wasm.entry = .disabled;
demo_wasm.rdynamic = true;

const install_demo_wasm = b.addInstallArtifact(demo_wasm, .{ .dest_dir = .{ .override = .{ .custom = "www" } } });
const install_demo_static = b.addInstallDirectory(.{
.source_dir = b.path("crdt-demo/www"),
.install_dir = .prefix,
.install_subdir = "www",
});

const python = try b.findProgram(&.{ "python3", "python", "py" }, &.{});
const run_demo = b.addSystemCommand(&.{
"sh", "-c",
b.fmt(
"trap 'kill 0' INT; {s} -m http.server -d {s} & {s} >/dev/null 2>&1 & wait",
.{
python,
b.getInstallPath(.prefix, "www"),
b.getInstallPath(.{ .custom = "bin" }, "crdt-server"),
},
),
});
run_demo.step.dependOn(&install_demo_wasm.step);
run_demo.step.dependOn(&install_demo_static.step);
run_demo.step.dependOn(&b.addInstallArtifact(server_exe, .{}).step);

const demo_step = b.step("demo", "builds and hosts the demo and starts the CRDT server");
demo_step.dependOn(&run_demo.step);

const serve_step = b.step("serve", "runs the CRDT server");
serve_step.dependOn(&run_server.step);

const test_step = b.step("test", "runs crdt-lib and crdt-server tests");
test_step.dependOn(&lib_test_run.step);
test_step.dependOn(&server_test_run.step);

const docs_step = b.step("docs", "generate documentation for crdt-server and crdt-lib");
const lib_docs_install = b.addInstallDirectory(.{
.install_dir = .prefix,
.install_subdir = "docs/lib",
.source_dir = lib_test.getEmittedDocs(),
});
const server_docs_install = b.addInstallDirectory(.{
.install_dir = .prefix,
.install_subdir = "docs/server",
.source_dir = server_test.getEmittedDocs(),
});
docs_step.dependOn(&lib_docs_install.step);
docs_step.dependOn(&server_docs_install.step);

const install_step = b.getInstallStep();
install_step.dependOn(&install_demo_wasm.step);
install_step.dependOn(&install_demo_static.step);
b.installArtifact(server_exe);
}

0 comments on commit 79e4416

Please sign in to comment.