-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.zig
158 lines (124 loc) · 4.87 KB
/
build.zig
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const std = @import("std");
const Build = std.Build;
const Target = std.Target;
const SemanticVersion = std.SemanticVersion;
pub fn build(b: *Build) void {
const t = b.standardTargetOptions(.{});
const o = b.standardOptimizeOption(.{});
runStep(b, t, o);
checkStep(b, t, o);
testStep(b, t, o);
releaseStep(b);
}
const version: SemanticVersion = .{ .major = 0, .minor = 12, .patch = 0 };
fn versionString(b: *Build) []const u8 {
return b.fmt("{d}.{d}.{d}", .{ version.major, version.minor, version.patch });
}
fn addCliExecutable(b: *Build, name: []const u8, target: anytype, optimize: anytype) *Build.Step.Compile {
const clap_module = b.dependency("clap", .{
.target = target,
.optimize = optimize,
}).module("clap");
const cli = b.addExecutable(.{
.name = name,
.root_source_file = b.path("src/cli.zig"),
.target = target,
.optimize = optimize,
});
cli.root_module.addImport("clap", clap_module);
addDocImports(b, cli);
addStdlibImports(b, cli);
addBuildOptions(b, cli);
return cli;
}
fn addWasmExecutable(b: *Build, name: []const u8) *Build.Step.Compile {
const wasm = b.addExecutable(.{
.name = name,
.root_source_file = b.path("src/wasm-lib.zig"),
.target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
}),
.optimize = .ReleaseSmall,
});
wasm.entry = .disabled;
wasm.rdynamic = true;
addStdlibImports(b, wasm);
return wasm;
}
fn addDocImports(b: *Build, exe: *Build.Step.Compile) void {
exe.root_module.addAnonymousImport("docs/cli", .{
.root_source_file = b.path("docs/cli.txt"),
});
const markdown_docs = [_][]const u8{ "advanced", "language", "overview", "stdlib", "stdlib-ast" };
for (markdown_docs) |filename| {
const input_file = b.fmt("docs/{s}.md", .{filename});
const output_name = b.fmt("docs/{s}", .{filename});
// Convert from github flavored markdown to plain text
const pandoc = b.addSystemCommand(&.{"pandoc"});
pandoc.addArgs(&.{ "-f", "gfm", "-t", "plain" });
pandoc.addFileArg(b.path(input_file));
const output = pandoc.captureStdOut();
exe.root_module.addAnonymousImport(output_name, .{
.root_source_file = output,
});
}
}
fn addStdlibImports(b: *Build, exe: *Build.Step.Compile) void {
exe.root_module.addAnonymousImport("stdlib/core.possum", .{
.root_source_file = b.path("stdlib/core.possum"),
});
}
fn addBuildOptions(b: *Build, exe: *Build.Step.Compile) void {
const options = b.addOptions();
exe.root_module.addOptions("build_options", options);
options.addOption([]const u8, "version", versionString(b));
}
fn runStep(b: *Build, target: anytype, optimize: anytype) void {
const run_step = b.step("run", "Run CLI app");
const cli = addCliExecutable(b, "possum", target, optimize);
b.installArtifact(cli);
const run_cli = b.addRunArtifact(cli);
run_cli.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cli.addArgs(args);
run_step.dependOn(&run_cli.step);
}
fn checkStep(b: *Build, target: anytype, optimize: anytype) void {
const check_step = b.step("check", "Check for compilation errors");
const check_cli = addCliExecutable(b, "possum", target, optimize);
const check_wasm = addWasmExecutable(b, "possum");
check_step.dependOn(&check_cli.step);
check_step.dependOn(&check_wasm.step);
}
fn testStep(b: *Build, target: anytype, optimize: anytype) void {
const test_step = b.step("test", "Run unit tests");
const unit_tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
addStdlibImports(b, unit_tests);
const run_unit_tests = b.addRunArtifact(unit_tests);
test_step.dependOn(&run_unit_tests.step);
}
fn releaseStep(b: *Build) void {
const release_step = b.step("release", "Build release binaries");
const wasm = addWasmExecutable(b, "possum");
const wasm_output = b.addInstallArtifact(wasm, .{});
release_step.dependOn(&wasm_output.step);
const targets: []const Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl },
};
for (targets) |query| {
const target = b.resolveTargetQuery(query);
const target_string = query.zigTriple(b.allocator) catch @panic("OOM");
const name = b.fmt("possum_{s}", .{target_string});
const cli = addCliExecutable(b, name, target, .ReleaseSafe);
cli.root_module.strip = false;
const target_output = b.addInstallArtifact(cli, .{});
release_step.dependOn(&target_output.step);
}
}