Compare commits
	
		
			2 Commits
		
	
	
		
			9286a82d86
			...
			cf4dea84d9
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						
						
							
						
						cf4dea84d9
	
				 | 
					
					
						|||
| 
						
						
							
						
						e70c44437e
	
				 | 
					
					
						
							
								
								
									
										95
									
								
								build.zig
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								build.zig
									
									
									
									
									
								
							@@ -15,56 +15,57 @@ pub fn build(b: *std.Build) void {
 | 
			
		||||
    // set a preferred release mode, allowing the user to decide how to optimize.
 | 
			
		||||
    const optimize = b.standardOptimizeOption(.{});
 | 
			
		||||
 | 
			
		||||
    const exe = b.addExecutable(.{
 | 
			
		||||
        .name = "aoc23",
 | 
			
		||||
        // In this case the main source file is merely a path, however, in more
 | 
			
		||||
        // complicated build scripts, this could be a generated file.
 | 
			
		||||
        .root_source_file = .{ .path = "src/main.zig" },
 | 
			
		||||
        .target = target,
 | 
			
		||||
        .optimize = optimize,
 | 
			
		||||
    });
 | 
			
		||||
    // Build harness based on https://github.com/SpexGuy/Zig-AoC-Template.
 | 
			
		||||
 | 
			
		||||
    // 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);
 | 
			
		||||
    // 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");
 | 
			
		||||
 | 
			
		||||
    // 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);
 | 
			
		||||
    // 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;
 | 
			
		||||
 | 
			
		||||
    // 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());
 | 
			
		||||
        // Build and install
 | 
			
		||||
        const exe = b.addExecutable(.{
 | 
			
		||||
            .name = b.fmt("aoc23-{s}", .{day_string}),
 | 
			
		||||
            .root_source_file = .{ .path = src },
 | 
			
		||||
            .target = target,
 | 
			
		||||
            .optimize = optimize,
 | 
			
		||||
        });
 | 
			
		||||
        b.installArtifact(exe);
 | 
			
		||||
        build_all.dependOn(&exe.step);
 | 
			
		||||
 | 
			
		||||
    // 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);
 | 
			
		||||
        // Run
 | 
			
		||||
        const run_cmd = b.addRunArtifact(exe);
 | 
			
		||||
        run_cmd.step.dependOn(b.getInstallStep());
 | 
			
		||||
        if (b.args) |args| {
 | 
			
		||||
            run_cmd.addArgs(args);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        const run_step = b.step(
 | 
			
		||||
            b.fmt("run-{s}", .{day_string}),
 | 
			
		||||
            b.fmt("Run {s} executable", .{day_string}),
 | 
			
		||||
        );
 | 
			
		||||
        run_step.dependOn(&run_cmd.step);
 | 
			
		||||
        run_all.dependOn(&run_cmd.step);
 | 
			
		||||
 | 
			
		||||
        // Test
 | 
			
		||||
        const unit_test = b.addTest(.{
 | 
			
		||||
            .root_source_file = .{ .path = src },
 | 
			
		||||
            .target = target,
 | 
			
		||||
            .optimize = optimize,
 | 
			
		||||
        });
 | 
			
		||||
 | 
			
		||||
        const test_cmd = b.addRunArtifact(unit_test);
 | 
			
		||||
        const test_step = b.step(
 | 
			
		||||
            b.fmt("test-{s}", .{day_string}),
 | 
			
		||||
            b.fmt("Run {s} tests", .{day_string}),
 | 
			
		||||
        );
 | 
			
		||||
        test_step.dependOn(&test_cmd.step);
 | 
			
		||||
        test_all.dependOn(&test_cmd.step);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 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("run", "Run the app");
 | 
			
		||||
    run_step.dependOn(&run_cmd.step);
 | 
			
		||||
 | 
			
		||||
    // Creates a step for unit testing. This only builds the test executable
 | 
			
		||||
    // but does not run it.
 | 
			
		||||
    const unit_tests = b.addTest(.{
 | 
			
		||||
        .root_source_file = .{ .path = "src/main.zig" },
 | 
			
		||||
        .target = target,
 | 
			
		||||
        .optimize = optimize,
 | 
			
		||||
    });
 | 
			
		||||
 | 
			
		||||
    const run_unit_tests = b.addRunArtifact(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("test", "Run unit tests");
 | 
			
		||||
    test_step.dependOn(&run_unit_tests.step);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -14,7 +14,11 @@
 | 
			
		||||
      in
 | 
			
		||||
      {
 | 
			
		||||
        devShells.default = pkgs.mkShellNoCC {
 | 
			
		||||
          buildInputs = [ zig pkgs.zls ];
 | 
			
		||||
          buildInputs = [
 | 
			
		||||
            pkgs.aoc-cli
 | 
			
		||||
            zig
 | 
			
		||||
            pkgs.zls
 | 
			
		||||
          ];
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        packages.default = pkgs.stdenv.mkDerivation {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								src/day01.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								src/day01.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
const std = @import("std");
 | 
			
		||||
const util = @import("util.zig");
 | 
			
		||||
 | 
			
		||||
pub fn main() !void {}
 | 
			
		||||
							
								
								
									
										24
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								src/main.zig
									
									
									
									
									
								
							@@ -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
									
								
							
							
						
						
									
										4
									
								
								src/util.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,4 @@
 | 
			
		||||
const std = @import("std");
 | 
			
		||||
 | 
			
		||||
const gpa_impl = std.heap.GeneralPurposeAllocator(.{});
 | 
			
		||||
pub const gpa = gpa_impl.allocator();
 | 
			
		||||
		Reference in New Issue
	
	Block a user