1
0

Add day-based build/test/run system

This commit is contained in:
xeals 2023-11-30 14:52:04 +11:00
parent 9286a82d86
commit e70c44437e
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc
4 changed files with 56 additions and 71 deletions

View File

@ -15,56 +15,57 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize. // set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{ // Build harness based on https://github.com/SpexGuy/Zig-AoC-Template.
.name = "aoc23",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
// This declares intent for the executable to be installed into the // Build, run, and install targets for "all."
// standard location when the user invokes the "install" step (the default const build_all = b.step("all", "Build all days");
// step when running `zig build`). const run_all = b.step("run", "Run all days");
b.installArtifact(exe); const test_all = b.step("test", "Run all tests");
// This *creates* a Run step in the build graph, to be executed when another // Generate build, run, and install targets for all days that have been
// step is evaluated that depends on it. The next line below will establish // created.
// such a dependency. for (1..25) |day| {
const run_cmd = b.addRunArtifact(exe); const day_string = b.fmt("day{:0>2}", .{day});
const src = b.fmt("src/{s}.zig", .{day_string});
std.fs.cwd().access(src, .{}) catch continue;
// By making the run step depend on the install step, it will be run from the // Build and install
// installation directory rather than directly from within the cache directory. const exe = b.addExecutable(.{
// This is not necessary, however, if the application depends on other installed .name = b.fmt("aoc23-{s}", .{day_string}),
// files, this ensures they will be present and in the expected location. .root_source_file = .{ .path = src },
run_cmd.step.dependOn(b.getInstallStep()); .target = target,
.optimize = optimize,
});
b.installArtifact(exe);
build_all.dependOn(&exe.step);
// This allows the user to pass arguments to the application in the build // Run
// command itself, like this: `zig build run -- arg1 arg2 etc` const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| { run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addArgs(args); if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(
b.fmt("run-{s}", .{day_string}),
b.fmt("Run {s} executable", .{day_string}),
);
run_step.dependOn(&run_cmd.step);
run_all.dependOn(&run_cmd.step);
// Test
const unit_test = b.addTest(.{
.root_source_file = .{ .path = src },
.target = target,
.optimize = optimize,
});
const test_cmd = b.addRunArtifact(unit_test);
const test_step = b.step(
b.fmt("test-{s}", .{day_string}),
b.fmt("Run {s} tests", .{day_string}),
);
test_step.dependOn(&test_cmd.step);
test_all.dependOn(&test_cmd.step);
} }
// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build run`
// This will evaluate the `run` step rather than the default, which is "install".
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
const run_unit_tests = b.addRunArtifact(unit_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step);
} }

4
src/day01.zig Normal file
View File

@ -0,0 +1,4 @@
const std = @import("std");
const util = @import("util.zig");
pub fn main() !void {}

View File

@ -1,24 +0,0 @@
const std = @import("std");
pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();
try stdout.print("Run `zig build test` to run the tests.\n", .{});
try bw.flush(); // don't forget to flush!
}
test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}

4
src/util.zig Normal file
View File

@ -0,0 +1,4 @@
const std = @import("std");
const gpa_impl = std.heap.GeneralPurposeAllocator(.{});
pub const gpa = gpa_impl.allocator();