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

pub fn build(b: *std.Build) void {
    const clap_mod = b.addModule("clap", .{ .source_file = .{ .path = "clap.zig" } });

    const optimize = b.standardOptimizeOption(.{});
    const target = b.standardTargetOptions(.{});

    const test_step = b.step("test", "Run all tests in all modes.");
    const tests = b.addTest(.{
        .root_source_file = .{ .path = "clap.zig" },
        .target = target,
        .optimize = optimize,
    });
    test_step.dependOn(&tests.run().step);

    const example_step = b.step("examples", "Build examples");
    inline for (.{
        "simple",
        "simple-ex",
        "streaming-clap",
        "help",
        "usage",
    }) |example_name| {
        const example = b.addExecutable(.{
            .name = example_name,
            .root_source_file = .{ .path = "example/" ++ example_name ++ ".zig" },
            .target = target,
            .optimize = optimize,
        });
        example.addModule("clap", clap_mod);
        example.install();
        example_step.dependOn(&example.step);
    }

    const readme_step = b.step("readme", "Remake README.");
    const readme = readMeStep(b);
    readme.dependOn(example_step);
    readme_step.dependOn(readme);

    const all_step = b.step("all", "Build everything and runs all tests");
    all_step.dependOn(test_step);
    all_step.dependOn(example_step);
    all_step.dependOn(readme_step);

    b.default_step.dependOn(all_step);
}

fn readMeStep(b: *std.Build) *std.Build.Step {
    const s = b.allocator.create(std.build.Step) catch unreachable;
    s.* = std.build.Step.init(.{
        .id = .custom,
        .name = "ReadMeStep",
        .owner = b,
        .makeFn = struct {
            fn make(step: *std.build.Step, _: *std.Progress.Node) anyerror!void {
                @setEvalBranchQuota(10000);
                _ = step;
                const file = try std.fs.cwd().createFile("README.md", .{});
                const stream = file.writer();
                try stream.print(@embedFile("example/README.md.template"), .{
                    @embedFile("example/simple.zig"),
                    @embedFile("example/simple-ex.zig"),
                    @embedFile("example/streaming-clap.zig"),
                    @embedFile("example/help.zig"),
                    @embedFile("example/usage.zig"),
                });
            }
        }.make,
    });
    return s;
}