From 560db879293d2b544e04b26e03cdf2faac33cad2 Mon Sep 17 00:00:00 2001 From: Gabe Venberg Date: Fri, 18 Apr 2025 22:43:34 +0200 Subject: [PATCH] made build.zig. Has runner and tester for each day, as well as a runner and tester for all the days. Also has check step for zls. --- .gitignore | 2 +- build.zig | 75 +++++++++++++++++++++++++++ build.zig.zon | 46 ++++++++++++++++ days/template/main.zig | 24 --------- {days => src/days}/template/input.txt | 0 {days => src/days}/template/lib.zig | 0 src/days/template/main.zig | 10 ++++ {days => src/days}/template/part1.zig | 5 +- {days => src/days}/template/part2.zig | 5 +- src/utils/mod.zig | 1 + 10 files changed, 139 insertions(+), 29 deletions(-) create mode 100644 build.zig create mode 100644 build.zig.zon delete mode 100644 days/template/main.zig rename {days => src/days}/template/input.txt (100%) rename {days => src/days}/template/lib.zig (100%) create mode 100644 src/days/template/main.zig rename {days => src/days}/template/part1.zig (53%) rename {days => src/days}/template/part2.zig (53%) create mode 100644 src/utils/mod.zig diff --git a/.gitignore b/.gitignore index c313b0d..3639c96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # ---> Zig -zig-cache/ +.zig-cache/ zig-out/ build/ build-*/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..8bf641a --- /dev/null +++ b/build.zig @@ -0,0 +1,75 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const allocator = b.allocator; + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const run_all = b.step("run", "Run all days"); + const test_all = b.step("test", "Test all days"); + const check = b.step("check", "run checks"); + + const utils_mod = b.createModule(.{ .root_source_file = b.path("src/utils/mod.zig") }); + + var dirIt = (try std.fs.cwd().openDir( + "./src/days", + .{ .iterate = true }, + )).iterate(); + + while (try dirIt.next()) |entry| { + if (entry.kind == .directory) {} + // each day entry will have a main.zig + const source_file = try std.fs.path.join(allocator, &.{ "src", "days", entry.name, "main.zig" }); + defer allocator.free(source_file); + //make sure the main.zig exists + _ = std.fs.cwd().openFile(source_file, .{}) catch continue; + + const exe = b.addExecutable(.{ + .name = entry.name, + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + exe.root_module.addImport("utils", utils_mod); + + b.installArtifact(exe); + const install_cmd = b.addInstallArtifact(exe, .{}); + const install_step = b.step( + b.fmt("install_{s}", .{entry.name}), + b.fmt("install {s}", .{entry.name}), + ); + install_step.dependOn(&install_cmd.step); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step( + b.fmt("run_{s}", .{entry.name}), + b.fmt("run {s}", .{entry.name}), + ); + run_step.dependOn(&run_cmd.step); + run_all.dependOn(&run_cmd.step); + + const exe_test = b.addTest(.{ + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + const test_cmd = b.addRunArtifact(exe_test); + const test_step = b.step( + b.fmt("test_{s}", .{entry.name}), + b.fmt("test {s}", .{entry.name}), + ); + test_step.dependOn(&test_cmd.step); + test_all.dependOn(&test_cmd.step); + + const exe_check = b.addExecutable(.{ + .name = entry.name, + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + exe_check.root_module.addImport("utils", utils_mod); + check.dependOn(&exe_check.step); + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..7f01cca --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,46 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save `, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = .aoc2024, + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.1", + + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0x25105d4386327470, // Changing this has security and trust implications. + + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.14.0", + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, +} diff --git a/days/template/main.zig b/days/template/main.zig deleted file mode 100644 index c8a3f67..0000000 --- a/days/template/main.zig +++ /dev/null @@ -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()); -} diff --git a/days/template/input.txt b/src/days/template/input.txt similarity index 100% rename from days/template/input.txt rename to src/days/template/input.txt diff --git a/days/template/lib.zig b/src/days/template/lib.zig similarity index 100% rename from days/template/lib.zig rename to src/days/template/lib.zig diff --git a/src/days/template/main.zig b/src/days/template/main.zig new file mode 100644 index 0000000..8c0f96f --- /dev/null +++ b/src/days/template/main.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const part1 = @import("part1.zig"); +const part2 = @import("part2.zig"); +const input = @embedFile("input.txt"); + +pub fn main() !void { + const stdout = std.io.getStdErr().writer(); + try stdout.print("Part1: {d}\n", .{part1.solve(input)}); + try stdout.print("Part2: {d}\n", .{part2.solve(input)}); +} diff --git a/days/template/part1.zig b/src/days/template/part1.zig similarity index 53% rename from days/template/part1.zig rename to src/days/template/part1.zig index 2d9e59d..2ded691 100644 --- a/days/template/part1.zig +++ b/src/days/template/part1.zig @@ -1,7 +1,8 @@ const lib = @import("lib.zig"); const std = @import("std"); +const utils = @import("utils"); -pub fn part1(comptime input: []const u8) i32 { +pub fn solve(comptime input: []const u8) i32 { return input.len; } @@ -9,5 +10,5 @@ test "part1 sample" { const input = \\0 ; - try std.testing.expectEqual(part1(input), 11); + try std.testing.expectEqual(solve(input), 1); } diff --git a/days/template/part2.zig b/src/days/template/part2.zig similarity index 53% rename from days/template/part2.zig rename to src/days/template/part2.zig index 9b0420f..30680d9 100644 --- a/days/template/part2.zig +++ b/src/days/template/part2.zig @@ -1,7 +1,8 @@ const lib = @import("lib.zig"); const std = @import("std"); +const utils = @import("utils"); -pub fn part2(comptime input: []const u8) i32 { +pub fn solve(comptime input: []const u8) i32 { return input.len; } @@ -9,5 +10,5 @@ test "part2 sample" { const input = \\0 ; - try std.testing.expectEqual(part2(input), 1); + try std.testing.expectEqual(solve(input), 1); } diff --git a/src/utils/mod.zig b/src/utils/mod.zig new file mode 100644 index 0000000..95a0b68 --- /dev/null +++ b/src/utils/mod.zig @@ -0,0 +1 @@ +const std = @import("std");