const std = @import("std"); const Build = std.Build; const SemanticVersion = std.SemanticVersion; pub fn build(b: *Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); const version = getVersion(b); const config = b.addOptions(); config.addOption(SemanticVersion, "version", version); const sqlite = b.dependency("sqlite", .{ .target = target, .optimize = optimize, }); const zg = b.dependency("zg", .{}); const exe = b.addExecutable(.{ .name = "ukkobot", .version = version, .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); exe.root_module.addOptions("ukkobot-config", config); exe.root_module.addImport("GeneralCategories", zg.module("GeneralCategories")); exe.root_module.addImport("LetterCasing", zg.module("LetterCasing")); exe.root_module.addImport("sqlite", sqlite.module("sqlite")); exe.linkLibrary(sqlite.artifact("sqlite")); b.installArtifact(exe); 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("run", "Run the app"); run_step.dependOn(&run_cmd.step); } const default_version = SemanticVersion.parse("0.0.1") catch unreachable; fn getVersion(b: *Build) SemanticVersion { var out_code: u8 = undefined; const untrimmed = b.runAllowFail( &.{ "git", "-C", b.build_root.path.?, "describe", "--tags" }, &out_code, .Ignore, ) catch return default_version; const git_desc = std.mem.trim(u8, untrimmed, &std.ascii.whitespace); // Turn something like 0.0.1-1-g85f815d into 0.0.1-1+g85f815d const ver_str = switch (std.mem.count(u8, git_desc, "-")) { 0 => git_desc, 2 => blk: { var it = std.mem.splitScalar(u8, git_desc, '-'); const tag = it.next() orelse unreachable; const height = it.next() orelse unreachable; const hash = it.next() orelse unreachable; break :blk b.fmt("{s}-{s}+{s}", .{ tag, height, hash }); }, else => { std.log.err("Unexpected `git describe` output: {s}", .{git_desc}); return default_version; }, }; return SemanticVersion.parse(ver_str) catch default_version; }