summaryrefslogtreecommitdiff
path: root/build.zig
blob: 20ac769fa44e1dd305152f7068b204f24894f1d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const builtin = @import("builtin");
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 clap = b.dependency("clap", .{});
    const libarchive = b.dependency("libarchive", .{});
    const xdg = b.dependency("xdg", .{});

    const exe = b.addExecutable(.{
        .name = "zup",
        .version = version,
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });
    exe.root_module.addOptions("zup-config", config);
    exe.root_module.addImport("clap", clap.module("clap"));
    exe.root_module.addImport("libarchive", libarchive.module("libarchive"));
    exe.root_module.addImport("xdg", xdg.module("xdg"));
    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 exe_tests = b.addTest(.{
        .root_source_file = b.path("src/main.zig"),
        .target = target,
        .optimize = optimize,
    });

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&exe_tests.step);
}

const default_version = SemanticVersion.parse("0.5.0") 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;
}