Simplify build.zig to Zig 0.15 best practices
All checks were successful
Create Release / release (push) Successful in 43s

This commit is contained in:
Jeremie Fraeys 2026-02-10 20:33:13 -05:00
parent 3e12aea424
commit c1fde4ff77
No known key found for this signature in database

158
build.zig
View file

@ -4,138 +4,43 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ============================================================================
// MODULES
// ============================================================================
// Main module (uses standard optimize option)
const root_module = b.createModule(.{
// Module
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Debug module (always Debug optimization)
const debug_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .Debug,
});
// Release module (always ReleaseFast optimization)
const release_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .ReleaseFast,
});
// Test module (uses standard optimize option)
const test_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// ============================================================================
// EXECUTABLES
// ============================================================================
// Main executable
// Executable
const exe = b.addExecutable(.{
.name = "manwhere",
.root_module = root_module,
.root_module = exe_mod,
});
b.installArtifact(exe);
// Debug executable
const debug_exe = b.addExecutable(.{
.name = "manwhere-debug",
.root_module = debug_module,
});
// Release executable
const release_exe = b.addExecutable(.{
.name = "manwhere",
.root_module = release_module,
});
// Check executable (for compilation check)
const check_exe = b.addExecutable(.{
.name = "check-compile",
.root_module = root_module,
});
// Test executable
const unit_tests = b.addTest(.{
.root_module = test_module,
});
// ============================================================================
// BUILD STEPS
// ============================================================================
// Default run step
// 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);
// Debug build step
const debug_step = b.step("debug", "Build with Debug optimization");
b.installArtifact(debug_exe);
debug_step.dependOn(b.getInstallStep());
// Debug run step
const debug_run_cmd = b.addRunArtifact(debug_exe);
if (b.args) |args| debug_run_cmd.addArgs(args);
debug_run_cmd.step.dependOn(&debug_exe.step);
const debug_run_step = b.step("debug-run", "Run the app with Debug optimization");
debug_run_step.dependOn(&debug_run_cmd.step);
// Release step - build optimized binary and install to ~/.local/bin
const release_step = b.step("release", "Build optimized binary and install to ~/.local/bin/");
// Get home directory from environment
const home = std.process.getEnvVarOwned(b.allocator, "HOME") catch {
std.debug.print("Error: HOME environment variable not set\n", .{});
return;
};
defer b.allocator.free(home);
const install_path = std.fs.path.join(b.allocator, &.{ home, ".local", "bin" }) catch {
std.debug.print("Error: Failed to construct install path\n", .{});
return;
};
// Note: NOT freed - used by build system
// Create install directory if it doesn't exist
const mkdir_cmd = b.addSystemCommand(&.{
"mkdir", "-p", install_path,
// Test step
const unit_tests = b.addTest(.{
.root_module = exe_mod,
});
release_step.dependOn(&mkdir_cmd.step);
// Build the release executable
const release_build = b.addInstallArtifact(release_exe, .{});
release_build.step.dependOn(&mkdir_cmd.step);
release_step.dependOn(&release_build.step);
// Copy to final destination
const copy_cmd = b.addSystemCommand(&.{
"cp", "zig-out/bin/manwhere", install_path,
});
copy_cmd.step.dependOn(&release_build.step);
release_step.dependOn(&copy_cmd.step);
// Test step - run all unit tests
const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
// Check step - compilation check without output
const check_step = b.step("check", "Check code formatting and compilation");
// Check step
const check_exe = b.addExecutable(.{
.name = "check-compile",
.root_module = exe_mod,
});
const check_step = b.step("check", "Check compilation");
check_step.dependOn(&check_exe.step);
// Format step - format source code
// Format step
const fmt_step = b.step("fmt", "Format source code");
const fmt_cmd = b.addFmt(.{
.paths = &.{ "src", "build.zig" },
@ -143,10 +48,41 @@ pub fn build(b: *std.Build) void {
});
fmt_step.dependOn(&fmt_cmd.step);
// Clean step - remove build artifacts
// Clean step
const clean_step = b.step("clean", "Remove build artifacts");
const clean_cmd = b.addSystemCommand(&.{
"rm", "-rf", ".zig-cache", "zig-out",
});
clean_step.dependOn(&clean_cmd.step);
// Release step
const release_step = b.step("release", "Build and install to ~/.local/bin");
const release_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = .ReleaseFast,
});
const release_exe = b.addExecutable(.{
.name = "manwhere",
.root_module = release_mod,
});
// Get install path
const home = std.process.getEnvVarOwned(b.allocator, "HOME") catch {
std.debug.print("Error: HOME not set\n", .{});
return;
};
defer b.allocator.free(home);
const bin_path = b.pathJoin(&.{ home, ".local", "bin" });
// Create directory
const mkdir_cmd = b.addSystemCommand(&.{ "mkdir", "-p", bin_path });
// Install the binary
const install_bin = b.addInstallBinFile(release_exe.getEmittedBin(), "manwhere");
install_bin.step.dependOn(&mkdir_cmd.step);
release_step.dependOn(&install_bin.step);
}