fetch_ml/cli/build.zig

89 lines
2.8 KiB
Zig

const std = @import("std");
// Clean build configuration for optimized CLI (Zig 0.15 std.Build API)
pub fn build(b: *std.Build) void {
// Standard target options
const target = b.standardTargetOptions(.{});
// Optimized release mode for size
const optimize = b.standardOptimizeOption(.{ .preferred_optimize_mode = .ReleaseSmall });
// CLI executable
const exe = b.addExecutable(.{
.name = "ml",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
// Install the executable to zig-out/bin
b.installArtifact(exe);
// Default build: install optimized CLI (used by `zig build`)
const prod_step = b.step("prod", "Build production CLI binary");
prod_step.dependOn(&exe.step);
// Convenience run step
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Standard Zig test discovery - find all test files automatically
const test_step = b.step("test", "Run unit tests");
// Test main executable
const main_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .Debug,
}),
});
const run_main_tests = b.addRunArtifact(main_tests);
test_step.dependOn(&run_main_tests.step);
// Find all test files in tests/ directory automatically
var test_dir = std.fs.cwd().openDir("tests", .{}) catch |err| {
std.log.warn("Failed to open tests directory: {}", .{err});
return;
};
defer test_dir.close();
// Create src module that tests can import from
const src_module = b.createModule(.{
.root_source_file = b.path("src.zig"),
.target = target,
.optimize = .Debug,
});
var iter = test_dir.iterate();
while (iter.next() catch |err| {
std.log.warn("Error iterating test files: {}", .{err});
return;
}) |entry| {
if (entry.kind == .file and std.mem.endsWith(u8, entry.name, "_test.zig")) {
const test_path = b.pathJoin(&.{ "tests", entry.name });
const test_module = b.createModule(.{
.root_source_file = b.path(test_path),
.target = target,
.optimize = .Debug,
});
// Make src module available to tests as "src"
test_module.addImport("src", src_module);
const test_exe = b.addTest(.{
.root_module = test_module,
});
const run_test = b.addRunArtifact(test_exe);
test_step.dependOn(&run_test.step);
}
}
}