/ build.zig
build.zig
 1  const std = @import("std");
 2  
 3  pub fn build(b: *std.Build) void {
 4      const target = b.standardTargetOptions(.{});
 5      const optimize = b.standardOptimizeOption(.{});
 6  
 7      const options = b.addOptions();
 8  
 9      const day = b.option(u8, "day", "Which day to run");
10      const day_path: ?[]const u8 = blk: {
11          if (day) |d| {
12              const dp = std.fmt.allocPrint(b.allocator, "days/day{}.zig", .{d}) catch |err| {
13                  std.log.err("Unable to create path to file day{}: {}", .{ d, err });
14                  return;
15              };
16              break :blk dp;
17          }
18          break :blk null;
19      };
20  
21      options.addOption(?[]const u8, "day_path", day_path);
22      options.addOption(?u8, "day", day);
23  
24      const lib = b.addStaticLibrary(.{
25          .name = "aoc-2022",
26          .root_source_file = b.path("src/root.zig"),
27          .target = target,
28          .optimize = optimize,
29      });
30  
31      b.installArtifact(lib);
32  
33      const exe = b.addExecutable(.{
34          .name = "aoc-2022",
35          .root_source_file = b.path("src/main.zig"),
36          .target = target,
37          .optimize = optimize,
38      });
39      exe.root_module.addOptions("build-options", options);
40      b.installArtifact(exe);
41  
42      const run_cmd = b.addRunArtifact(exe);
43  
44      run_cmd.step.dependOn(b.getInstallStep());
45  
46      if (b.args) |args| {
47          run_cmd.addArgs(args);
48      }
49  
50      const run_step = b.step("run", "Run the app");
51      run_step.dependOn(&run_cmd.step);
52  
53      const exe_unit_tests = b.addTest(.{
54          .root_source_file = b.path("src/main.zig"),
55          .target = target,
56          .optimize = optimize,
57      });
58  
59      const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
60      const test_step = b.step("test", "Run unit tests");
61      test_step.dependOn(&run_exe_unit_tests.step);
62  
63      if (day) |d| {
64          const day_path_test = std.fmt.allocPrint(b.allocator, "src/days/day{}.zig", .{d}) catch |err| {
65              std.log.err("Unable to create path to file day{}: {}", .{ d, err });
66              return;
67          };
68          const day_unit_tests = b.addTest(.{
69              .optimize = optimize,
70              .target = target,
71              .root_source_file = b.path(day_path_test),
72          });
73          const run_day_unit_tests = b.addRunArtifact(day_unit_tests);
74          test_step.dependOn(&run_day_unit_tests.step);
75      }
76  }