summaryrefslogtreecommitdiff
path: root/build.zig
blob: 934e82600c9f7a25f35375d9425fb099434b1f41 (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
const builtin = @import("builtin");
const std = @import("std");

const Builder = std.build.Builder;
const SemanticVersion = std.SemanticVersion;

pub fn build(b: *Builder) void {
    const target = b.standardTargetOptions(.{});
    const mode = b.standardReleaseOptions();

    const config = b.addOptions();
    config.addOption(SemanticVersion, "version", getVersion(b));

    const exe = b.addExecutable("zup", "src/main.zig");
    exe.setTarget(target);
    exe.setBuildMode(mode);
    exe.addOptions("zup-config", config);
    exe.addPackagePath("clap", "libs/clap/clap.zig");
    exe.addPackagePath("curl", "libs/curl/curl.zig");
    exe.addPackagePath("libarchive", "libs/libarchive/libarchive.zig");
    exe.addPackagePath("xdg", "libs/xdg/xdg.zig");
    exe.addPackagePath("zup", "src/main.zig");
    exe.linkLibC();
    exe.linkSystemLibrary("libarchive");
    exe.linkSystemLibrary("libcurl");
    exe.install();

    const run_cmd = exe.run();
    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("src/main.zig");
    exe_tests.setTarget(target);
    exe_tests.setBuildMode(mode);

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

const default_version = SemanticVersion.parse("0.2.1") catch unreachable;

fn getVersion(b: *Builder) SemanticVersion {
    var out_code: u8 = undefined;
    const untrimmed = b.execAllowFail(
        &.{ "git", "-C", b.build_root, "describe", "--tags" },
        &out_code,
        .Ignore,
    ) catch return default_version;

    const git_desc = std.mem.trim(u8, untrimmed, &std.ascii.spaces);
    // 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.split(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;
}