-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.zig
More file actions
228 lines (203 loc) · 7.61 KB
/
build.zig
File metadata and controls
228 lines (203 loc) · 7.61 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
const std = @import("std");
const Build = std.Build;
const Dependency = Build.Dependency;
const LazyPath = Build.LazyPath;
const Module = Build.Module;
const OptimizeMode = std.builtin.OptimizeMode;
const ResolvedTarget = Build.ResolvedTarget;
const Step = Build.Step;
const Config = struct {
optimize: OptimizeMode,
target: ResolvedTarget,
enable_harfbuzz: bool,
enable_opengl: bool,
};
const deps = struct {
fn addHarfbuzz(b: *Build, config: Config, module: *Module) void {
if (!config.enable_harfbuzz) return;
if (b.lazyDependency("harfbuzz", .{
.optimize = config.optimize,
.target = config.target,
})) |harfbuzz| {
module.addImport("harfbuzz", harfbuzz.module("harfbuzz"));
module.linkLibrary(harfbuzz.artifact("harfbuzz"));
}
}
fn addMachGlfw(b: *Build, config: Config, module: *Module) void {
if (b.lazyDependency("mach-glfw", .{
.optimize = config.optimize,
.target = config.target,
})) |mach_glfw| {
module.addImport("mach-glfw", mach_glfw.module("mach-glfw"));
}
}
fn addZgl(b: *Build, config: Config, module: *Module) void {
if (!config.enable_opengl) return;
if (b.lazyDependency("zgl", .{
.optimize = config.optimize,
.target = config.target,
})) |zgl| {
module.addImport("zgl", zgl.module("zgl"));
}
}
fn addZigimg(b: *Build, config: Config, module: *Module) void {
if (b.lazyDependency("zigimg", .{
.optimize = config.optimize,
.target = config.target,
})) |zigimg| {
module.addImport("zigimg", zigimg.module("zigimg"));
}
}
};
pub fn build(b: *Build) void {
const config = Config{
.optimize = b.standardOptimizeOption(.{}),
.target = b.standardTargetOptions(.{}),
.enable_harfbuzz = b.option(bool, "enable-harfbuzz", "Enable Harfbuzz support") orelse true,
.enable_opengl = b.option(bool, "enable-opengl", "Enable OpenGL support") orelse true,
};
const zss = b.addModule("zss", .{
.root_source_file = b.path("source/zss.zig"),
.target = config.target,
.optimize = config.optimize,
});
deps.addHarfbuzz(b, config, zss);
deps.addZgl(b, config, zss);
const unit_tests = addUnitTests(b, config, zss);
const test_suite = addTestSuite(b, config, zss);
addDemo(b, config, zss);
addExamples(b, config, zss);
const all_tests = b.step("test", "Run all tests");
all_tests.dependOn(&unit_tests.step);
all_tests.dependOn(&test_suite.step);
}
fn addUnitTests(b: *Build, config: Config, zss: *Module) *Step.Run {
const test_filters = b.option([]const []const u8, "test-filter", "A unit test filter (can be used multiple times)") orelse &.{};
const unit_tests = b.addTest(.{
.name = "zss-unit-tests",
.root_module = zss,
.filters = test_filters,
});
deps.addHarfbuzz(b, config, unit_tests.root_module);
const run = b.addRunArtifact(unit_tests);
const install = b.addInstallArtifact(unit_tests, .{});
const step = b.step("test-units", "Run unit tests");
step.dependOn(&run.step);
step.dependOn(&install.step);
b.getInstallStep().dependOn(&install.step);
return run;
}
fn addTestSuite(b: *Build, config: Config, zss: *Module) *Step.Run {
const test_suite = b.addExecutable(.{
.name = "zss-test-suite",
.root_module = b.createModule(.{
.root_source_file = b.path("test/suite.zig"),
.target = config.target,
.optimize = config.optimize,
.imports = &.{
.{ .name = "zss", .module = zss },
},
}),
});
deps.addHarfbuzz(b, config, test_suite.root_module);
deps.addZgl(b, config, test_suite.root_module);
deps.addZigimg(b, config, test_suite.root_module);
{
const TestSuiteCategory = enum {
check,
memory,
opengl,
print,
};
const category_strings = b.option([]const []const u8, "test", "A test suite category to run (can be used multiple times)") orelse &.{};
var category_set = std.enums.EnumSet(TestSuiteCategory).initEmpty();
var categories: [std.meta.fields(TestSuiteCategory).len][]const u8 = undefined;
var categories_len: usize = 0;
for (category_strings) |in| {
const val = std.meta.stringToEnum(TestSuiteCategory, in) orelse std.debug.panic("Invalid test suite category: {s}", .{in});
if (!category_set.contains(val)) {
category_set.insert(val);
categories[categories_len] = in;
categories_len += 1;
switch (val) {
.check, .memory, .print => {},
.opengl => {
deps.addMachGlfw(b, config, test_suite.root_module);
},
}
}
}
const options_module = b.addOptions();
options_module.addOption([]const []const u8, "test_categories", categories[0..categories_len]);
test_suite.root_module.addOptions("build-options", options_module);
}
const run = b.addRunArtifact(test_suite);
const test_cases_path = b.path("test/cases");
const resources_path = b.path("test/res");
const output_path = b.path("test/output");
run.addDirectoryArg(test_cases_path);
run.addDirectoryArg(resources_path);
run.addDirectoryArg(output_path);
if (b.args) |args| run.addArgs(args);
const install = b.addInstallArtifact(test_suite, .{});
const step = b.step("test-suite", "Run the test suite");
step.dependOn(&run.step);
step.dependOn(&install.step);
b.getInstallStep().dependOn(&install.step);
return run;
}
fn addDemo(b: *Build, config: Config, zss: *Module) void {
const demo = b.addExecutable(.{
.name = "demo",
.root_module = b.createModule(.{
.root_source_file = b.path("demo/demo.zig"),
.target = config.target,
.optimize = config.optimize,
.imports = &.{
.{ .name = "zss", .module = zss },
},
}),
});
deps.addHarfbuzz(b, config, demo.root_module);
deps.addZgl(b, config, demo.root_module);
deps.addZigimg(b, config, demo.root_module);
deps.addMachGlfw(b, config, demo.root_module);
const run = b.addRunArtifact(demo);
if (b.args) |args| run.addArgs(args);
const install = b.addInstallArtifact(demo, .{});
const step = b.step("demo", "Run a graphical demo program");
step.dependOn(&run.step);
step.dependOn(&install.step);
b.getInstallStep().dependOn(&install.step);
}
fn addExamples(b: *Build, config: Config, zss: *Module) void {
addExample(b, config, zss, "parse", "examples/parse.zig", "Run an example parser program");
}
fn addExample(
b: *Build,
config: Config,
zss: *Module,
name: []const u8,
path: []const u8,
description: []const u8,
) void {
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(path),
.target = config.target,
.optimize = config.optimize,
.imports = &.{
.{ .name = "zss", .module = zss },
},
}),
});
b.installArtifact(exe);
const run = b.addRunArtifact(exe);
if (b.args) |args| run.addArgs(args);
const install = b.addInstallArtifact(exe, .{});
const step = b.step(name, description);
step.dependOn(&run.step);
step.dependOn(&install.step);
b.getInstallStep().dependOn(&install.step);
}