summaryrefslogtreecommitdiff
path: root/build.zig
blob: 8a080d16bcfabcb3508c82909815d9d4a665579d (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
81
82
83
84
85
86
87
88
const builtin = @import("builtin");
const std = @import("std");

const Build = std.Build;
const SemanticVersion = std.SemanticVersion;
const Version = std.builtin.Version;

pub fn build(b: *Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const semanticVersion = getSemanticVersion(b);
    const version = Version{
        .major = @intCast(u32, semanticVersion.major),
        .minor = @intCast(u32, semanticVersion.minor),
        .patch = @intCast(u32, semanticVersion.patch),
    };

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

    const exe = b.addExecutable(.{
        .name = "zup",
        .version = version,
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });
    exe.addOptions("zup-config", config);
    exe.addModule("clap", b.createModule(.{ .source_file = .{ .path = "libs/clap/clap.zig" }}));
    exe.addModule("curl", b.createModule(.{ .source_file = .{ .path = "libs/curl/curl.zig" }}));
    exe.addModule("libarchive", b.createModule(.{
        .source_file = .{ .path = "libs/libarchive/libarchive.zig" },
    }));
    exe.addModule("xdg", b.createModule(.{ .source_file = .{ .path = "libs/xdg/xdg.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(.{
        .root_source_file = .{ .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.2.1") catch unreachable;

fn getSemanticVersion(b: *Build) SemanticVersion {
    var out_code: u8 = undefined;
    const untrimmed = b.execAllowFail(
        &.{ "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.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;
}