summaryrefslogtreecommitdiff
path: root/src/build/HelpStrings.zig
blob: 96505cabacb00f1fc5d4fd153b26a455c482e376 (plain)
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
const HelpStrings = @This();

const std = @import("std");
const Config = @import("Config.zig");

/// The "helpgen" exe.
exe: *std.Build.Step.Compile,

/// The output path for the help strings.
output: std.Build.LazyPath,

pub fn init(b: *std.Build, cfg: *const Config) !HelpStrings {
    const exe = b.addExecutable(.{
        .name = "helpgen",
        .root_module = b.createModule(.{
            .root_source_file = b.path("src/helpgen.zig"),
            .target = b.graph.host,
            .strip = false,
            .omit_frame_pointer = false,
            .unwind_tables = .sync,
        }),
    });

    const help_config = config: {
        var copy = cfg.*;
        copy.exe_entrypoint = .helpgen;
        break :config copy;
    };
    const options = b.addOptions();
    try help_config.addOptions(options);
    exe.root_module.addOptions("build_options", options);

    const help_run = b.addRunArtifact(exe);

    // Generated Zig files have to end with .zig
    const wf = b.addWriteFiles();
    const output = wf.addCopyFile(help_run.captureStdOut(), "helpgen.zig");

    return .{
        .exe = exe,
        .output = output,
    };
}

/// Add the "help_strings" import.
pub fn addImport(self: *const HelpStrings, step: *std.Build.Step.Compile) void {
    self.output.addStepDependencies(&step.step);
    step.root_module.addAnonymousImport("help_strings", .{
        .root_source_file = self.output,
    });
}

/// Install the help exe
pub fn install(self: *const HelpStrings) void {
    self.exe.step.owner.installArtifact(self.exe);
}