advent-of-code-2023/build.zig

103 lines
3.3 KiB
Zig
Raw Normal View History

2020-11-26 22:46:03 +01:00
const std = @import("std");
2023-11-29 08:06:07 +01:00
const Build = std.Build;
const CompileStep = std.Build.Step.Compile;
/// set this to true to link libc
2020-11-26 22:46:03 +01:00
const should_link_libc = false;
2023-11-29 18:17:18 +01:00
const required_zig_version = std.SemanticVersion.parse("0.12.0-dev.1754+2a3226453") catch unreachable;
2023-11-29 08:06:07 +01:00
fn linkObject(b: *Build, obj: *CompileStep) void {
2020-11-26 22:46:03 +01:00
if (should_link_libc) obj.linkLibC();
2021-11-24 05:56:51 +01:00
_ = b;
2020-11-26 22:46:03 +01:00
// Add linking for packages or third party libraries here
}
2023-11-29 08:06:07 +01:00
pub fn build(b: *Build) void {
2023-11-29 18:17:18 +01:00
if (comptime @import("builtin").zig_version.order(required_zig_version) == .lt) {
std.debug.print("Warning: Your version of Zig too old. You will need to download a newer build\n", .{});
std.os.exit(1);
}
2020-11-26 22:46:03 +01:00
const target = b.standardTargetOptions(.{});
2023-11-29 08:06:07 +01:00
const mode = b.standardOptimizeOption(.{});
2020-11-26 22:46:03 +01:00
2021-11-24 05:56:51 +01:00
const install_all = b.step("install_all", "Install all days");
const run_all = b.step("run_all", "Run all days");
2022-11-26 21:35:05 +01:00
const generate = b.step("generate", "Generate stub files from template/template.zig");
2023-11-29 08:06:07 +01:00
const build_generate = b.addExecutable(.{
.name = "generate",
.root_source_file = .{ .path = "template/generate.zig" },
.optimize = .ReleaseSafe,
});
const run_generate = b.addRunArtifact(build_generate);
run_generate.setCwd(.{ .path = std.fs.path.dirname(@src().file).? });
2022-11-26 21:35:05 +01:00
generate.dependOn(&run_generate.step);
2020-11-26 22:46:03 +01:00
// Set up an exe for each day
2021-11-24 05:56:51 +01:00
var day: u32 = 1;
while (day <= 25) : (day += 1) {
const dayString = b.fmt("day{:0>2}", .{day});
const zigFile = b.fmt("src/{s}.zig", .{dayString});
2020-11-26 22:46:03 +01:00
2023-11-29 08:06:07 +01:00
const exe = b.addExecutable(.{
.name = dayString,
.root_source_file = .{ .path = zigFile },
.target = target,
.optimize = mode,
});
2020-11-26 22:46:03 +01:00
linkObject(b, exe);
2023-11-29 08:06:07 +01:00
const install_cmd = b.addInstallArtifact(exe, .{});
2020-11-26 22:46:03 +01:00
2023-11-29 08:06:07 +01:00
const build_test = b.addTest(.{
.root_source_file = .{ .path = zigFile },
.target = target,
.optimize = mode,
});
linkObject(b, build_test);
2023-11-29 08:06:07 +01:00
const run_test = b.addRunArtifact(build_test);
{
const step_key = b.fmt("install_{s}", .{dayString});
const step_desc = b.fmt("Install {s}.exe", .{dayString});
const install_step = b.step(step_key, step_desc);
install_step.dependOn(&install_cmd.step);
install_all.dependOn(&install_cmd.step);
}
{
const step_key = b.fmt("test_{s}", .{dayString});
const step_desc = b.fmt("Run tests in {s}", .{zigFile});
const step = b.step(step_key, step_desc);
step.dependOn(&run_test.step);
}
2023-11-29 08:06:07 +01:00
const run_cmd = b.addRunArtifact(exe);
2020-11-26 22:46:03 +01:00
if (b.args) |args| {
run_cmd.addArgs(args);
}
2021-11-24 05:56:51 +01:00
const run_desc = b.fmt("Run {s}", .{dayString});
const run_step = b.step(dayString, run_desc);
2020-11-26 22:46:03 +01:00
run_step.dependOn(&run_cmd.step);
2021-11-24 05:56:51 +01:00
run_all.dependOn(&run_cmd.step);
2020-11-26 22:46:03 +01:00
}
// Set up tests for util.zig
{
const test_util = b.step("test_util", "Run tests in util.zig");
2023-11-29 08:06:07 +01:00
const test_cmd = b.addTest(.{
.root_source_file = .{ .path = "src/util.zig" },
.target = target,
.optimize = mode,
});
2020-11-26 22:46:03 +01:00
linkObject(b, test_cmd);
test_util.dependOn(&test_cmd.step);
}
2020-11-26 22:46:03 +01:00
}