diff options
author | Mathias Magnusson <mathias@magnusson.space> | 2024-12-03 14:26:04 +0100 |
---|---|---|
committer | Mathias Magnusson <mathias@magnusson.space> | 2024-12-03 14:36:12 +0100 |
commit | bf494891a715080dde14753b51befb463a6c246f (patch) | |
tree | a1be0795f4d3b8db29f0f5569183989d3d23f6fc /aoc24/build.zig | |
parent | 0c01b3924d55e0923cf895254b9412c9cedc9047 (diff) | |
download | programming-problem-solving-bf494891a715080dde14753b51befb463a6c246f.tar.gz |
aoc2024: make build tool download input & add shared main function
Diffstat (limited to 'aoc24/build.zig')
-rw-r--r-- | aoc24/build.zig | 111 |
1 files changed, 72 insertions, 39 deletions
diff --git a/aoc24/build.zig b/aoc24/build.zig index 4648193..7648bf0 100644 --- a/aoc24/build.zig +++ b/aoc24/build.zig @@ -15,50 +15,83 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); - for (1..26) |day| for ([_]usize{ 1, 2 }) |part| { - const name = b.fmt("day{}p{}", .{ day, part }); - const path = b.path(b.fmt("src/{s}.zig", .{name})); - const exe = b.addExecutable(.{ - .name = name, - .root_source_file = path, - .target = target, - .optimize = optimize, - }); + const generate_main = b.addExecutable(.{ + .name = "generate_main", + .root_source_file = b.path("tools/generate_main.zig"), + .target = b.host, + }); - // 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); + const download_input = b.addExecutable(.{ + .name = "download_input", + .root_source_file = b.path("tools/download_input.zig"), + .target = b.host, + }); - // This *creates* a Run step in the build graph, to be executed when another - // step is evaluated that depends on it. The next line below will establish - // such a dependency. - const run_cmd = b.addRunArtifact(exe); + for (1..26) |day| { + const run_download_input = b.addRunArtifact(download_input); + run_download_input.addArg(b.fmt("{}", .{day})); + const input_path = run_download_input.addOutputFileArg(b.fmt("{}.txt", .{day})); + const path = b.path(b.fmt("src/day{}.zig", .{day})); - // 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| { - run_cmd.addArgs(args); - } + for ([_]usize{ 1, 2 }) |part| { + const name = b.fmt("day{}p{}", .{ day, part }); + + const run_generate_main = b.addRunArtifact(generate_main); + run_generate_main.addArg(b.fmt("{}", .{part})); + const main_path = run_generate_main.addOutputFileArg(b.fmt("{s}_main.zig", .{name})); + + const exe = b.addExecutable(.{ + .name = b.fmt("{s}", .{name}), + .root_source_file = main_path, + .target = target, + .optimize = optimize, + }); + exe.root_module.addAnonymousImport("solution", .{ + .root_source_file = path, + .target = target, + .optimize = optimize, + }); + exe.root_module.addAnonymousImport("input", .{ + .root_source_file = input_path, + .target = target, + .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); - // 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(name, b.fmt("Run {s}", .{name})); - run_step.dependOn(&run_cmd.step); + // This *creates* a Run step in the build graph, to be executed when another + // step is evaluated that depends on it. The next line below will establish + // such a dependency. + const run_cmd = b.addRunArtifact(exe); - const exe_unit_tests = b.addTest(.{ - .root_source_file = path, - .target = target, - .optimize = optimize, - }); + // 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| { + run_cmd.addArgs(args); + } - const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); + // 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(name, b.fmt("Run {s}", .{name})); + run_step.dependOn(&run_cmd.step); - // 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(b.fmt("{s}-test", .{name}), b.fmt("Test {s}", .{name})); - test_step.dependOn(&run_exe_unit_tests.step); - }; + const exe_unit_tests = b.addTest(.{ + .root_source_file = path, + .target = target, + .optimize = optimize, + }); + + const run_exe_unit_tests = b.addRunArtifact(exe_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(b.fmt("{s}-test", .{name}), b.fmt("Test {s}", .{name})); + test_step.dependOn(&run_exe_unit_tests.step); + } + } } |