1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
cf4dea84d9
Add AOC CLI to flake
All checks were successful
Build and test / test (push) Successful in 2m57s
2023-11-30 14:52:15 +11:00
e70c44437e
Add day-based build/test/run system 2023-11-30 14:52:04 +11:00
5 changed files with 61 additions and 72 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(.{});
// Build harness based on https://github.com/SpexGuy/Zig-AoC-Template.
// Build, run, and install targets for "all."
const build_all = b.step("all", "Build all days");
const run_all = b.step("run", "Run all days");
const test_all = b.step("test", "Run all tests");
// Generate build, run, and install targets for all days that have been
// created.
for (1..25) |day| {
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;
// Build and install
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = "aoc23", .name = b.fmt("aoc23-{s}", .{day_string}),
// In this case the main source file is merely a path, however, in more .root_source_file = .{ .path = src },
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
// step when running `zig build`).
b.installArtifact(exe); b.installArtifact(exe);
build_all.dependOn(&exe.step);
// This *creates* a Run step in the build graph, to be executed when another // Run
// step is evaluated that depends on it. The next line below will establish
// such a dependency.
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
// By making the run step depend on the install step, it will be run from the
// installation directory rather than directly from within the cache directory.
// This is not necessary, however, if the application depends on other installed
// files, this ensures they will be present and in the expected location.
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
// This allows the user to pass arguments to the application in the build
// command itself, like this: `zig build run -- arg1 arg2 etc`
if (b.args) |args| { if (b.args) |args| {
run_cmd.addArgs(args); run_cmd.addArgs(args);
} }
// This creates a build step. It will be visible in the `zig build --help` menu, const run_step = b.step(
// and can be selected like this: `zig build run` b.fmt("run-{s}", .{day_string}),
// This will evaluate the `run` step rather than the default, which is "install". b.fmt("Run {s} executable", .{day_string}),
const run_step = b.step("run", "Run the app"); );
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
run_all.dependOn(&run_cmd.step);
// Creates a step for unit testing. This only builds the test executable // Test
// but does not run it. const unit_test = b.addTest(.{
const unit_tests = b.addTest(.{ .root_source_file = .{ .path = src },
.root_source_file = .{ .path = "src/main.zig" },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
const run_unit_tests = b.addRunArtifact(unit_tests); const test_cmd = b.addRunArtifact(unit_test);
const test_step = b.step(
// Similar to creating the run step earlier, this exposes a `test` step to b.fmt("test-{s}", .{day_string}),
// the `zig build --help` menu, providing a way for the user to request b.fmt("Run {s} tests", .{day_string}),
// running the unit tests. );
const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&test_cmd.step);
test_step.dependOn(&run_unit_tests.step); test_all.dependOn(&test_cmd.step);
}
} }

View File

@ -14,7 +14,11 @@
in in
{ {
devShells.default = pkgs.mkShellNoCC { devShells.default = pkgs.mkShellNoCC {
buildInputs = [ zig pkgs.zls ]; buildInputs = [
pkgs.aoc-cli
zig
pkgs.zls
];
}; };
packages.default = pkgs.stdenv.mkDerivation { packages.default = pkgs.stdenv.mkDerivation {

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();