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.
This commit is contained in:
Gabe Venberg 2025-04-18 22:43:34 +02:00
parent 0a9f08cbf0
commit 560db87929
10 changed files with 139 additions and 29 deletions

2
.gitignore vendored
View file

@ -1,5 +1,5 @@
# ---> Zig
zig-cache/
.zig-cache/
zig-out/
build/
build-*/

75
build.zig Normal file
View file

@ -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);
}
}

46
build.zig.zon Normal file
View file

@ -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 <url>`, 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",
},
}

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

View file

@ -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)});
}

View file

@ -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);
}

View file

@ -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);
}

1
src/utils/mod.zig Normal file
View file

@ -0,0 +1 @@
const std = @import("std");