-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
352 lines (298 loc) · 13.6 KB
/
build.zig
File metadata and controls
352 lines (298 loc) · 13.6 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
const std = @import("std");
const Build = std.Build;
const DEFAULT_RENDER_BACKEND: RenderBackend = .vulkan;
pub const RenderBackend = enum {
d3d11,
opengl,
vulkan,
};
pub const WindowSystem = enum {
win32,
xlib,
xcb,
glfw,
};
pub fn build(b: *Build) !void {
// -------------------------------------------------------------------------
// Target & Optimization
// -------------------------------------------------------------------------
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// const debug_mode = optimize == .Debug;
const target_tag = target.result.os.tag;
const is_native = target.query.isNativeOs();
const is_gnu = target.result.isGnuLibC();
const linkage: std.builtin.LinkMode = if (is_gnu and is_native) .dynamic else .static;
// -------------------------------------------------------------------------
// Build Options
// -------------------------------------------------------------------------
const use_llvm = b.option(bool, "use-llvm", "") orelse false;
const comptime_check = b.option(bool, "comptime-check", "") orelse false;
const render_backend = b.option(RenderBackend, "render-backend", "") orelse DEFAULT_RENDER_BACKEND;
const default_window_system: WindowSystem = .glfw;
const window_system = b.option(WindowSystem, "window-system", "") orelse default_window_system;
const disable_renderer_debug = b.option(
bool,
"disable-renderer-debug",
"Disable debugging for renderer backends (Vulkan validation layers, OpenGL debug callbacks)",
) orelse !comptime_check;
const options = b.addOptions();
options.addOption(RenderBackend, "render-backend", render_backend);
options.addOption(WindowSystem, "window-system", window_system);
options.addOption(bool, "renderer-debug", !disable_renderer_debug);
options.addOption(bool, "comptime_check", comptime_check);
const options_mod = options.createModule();
// -------------------------------------------------------------------------
// External Dependencies
// -------------------------------------------------------------------------
const vtparse_dep = b.dependency("vtparse", .{ .target = target, .optimize = optimize });
const vtparse_mod = vtparse_dep.module("vtparse");
const truetype_dep = b.dependency("TrueType", .{ .target = target, .optimize = optimize });
const truetype_mod = truetype_dep.module("TrueType");
const machfreetype_dep = b.dependency("mach_freetype", .{ .target = target, .optimize = optimize });
const machfreetype_mod = machfreetype_dep.module("mach-freetype");
const machharfbuzz_mod = machfreetype_dep.module("mach-harfbuzz");
const zigimg_dep = b.dependency("zigimg", .{ .target = target, .optimize = optimize });
const zigimg_mod = zigimg_dep.module("zigimg");
// -------------------------------------------------------------------------
// Internal Modules Definition
// -------------------------------------------------------------------------
const input_mod = b.createModule(.{ .root_source_file = b.path("src/input/root.zig") });
const window_mod = b.createModule(.{ .root_source_file = b.path("src/window/root.zig") });
const pty_mod = b.createModule(.{ .root_source_file = b.path("src/pty/root.zig") });
const childprocess_mod = b.createModule(.{ .root_source_file = b.path("src/ChildProcess.zig") });
const color_mod = b.createModule(.{ .root_source_file = b.path("src/color.zig") });
const grid_mod = b.createModule(.{ .root_source_file = b.path("src/Grid.zig") });
const cursor_mod = b.createModule(.{ .root_source_file = b.path("src/Cursor.zig") });
const dynamiclibrary_mod = b.createModule(.{ .root_source_file = b.path("src/DynamicLibrary.zig") });
const io_mod = b.createModule(.{ .root_source_file = b.path("src/io/root.zig") });
const math_mod = b.createModule(.{ .root_source_file = b.path("src/renderer/common/math.zig") });
const font_mod = b.createModule(.{ .root_source_file = b.path("src/font/root.zig") });
const assets_mod = b.createModule(.{ .root_source_file = b.path("assets/assets.zig") });
const renderer_mod = b.createModule(.{ .root_source_file = b.path("src/renderer/root.zig") });
const circulararray_mod = b.createModule(.{ .root_source_file = b.path("src/circular_array/root.zig") });
// -------------------------------------------------------------------------
// Internal Module Wiring (Imports)
// -------------------------------------------------------------------------
// Window imports
window_mod.addImport("build_options", options_mod);
window_mod.addImport("input", input_mod);
window_mod.addImport("zigimg", zigimg_mod);
window_mod.addImport("assets", assets_mod);
window_mod.addImport("renderer", renderer_mod);
// PTY imports
pty_mod.addImport("build_options", options_mod);
// ChildProcess imports
childprocess_mod.addImport("pty", pty_mod);
// Font imports
font_mod.addImport("build_options", options_mod);
font_mod.addImport("math", math_mod);
font_mod.addImport("TrueType", truetype_mod);
font_mod.addImport("mach-freetype", machfreetype_mod);
font_mod.addImport("mach-harfbuzz", machharfbuzz_mod);
font_mod.addImport("zigimg", zigimg_mod);
font_mod.addImport("assets", assets_mod);
// Renderer imports
renderer_mod.addImport("build_options", options_mod);
renderer_mod.addImport("font", font_mod);
renderer_mod.addImport("grid", grid_mod);
renderer_mod.addImport("cursor", cursor_mod);
renderer_mod.addImport("color", color_mod);
renderer_mod.addImport("window", window_mod);
renderer_mod.addImport("math", math_mod);
renderer_mod.addImport("assets", assets_mod);
// -------------------------------------------------------------------------
// Conditional System Dependencies
// -------------------------------------------------------------------------
// Windows Dependencies
if (comptime_check or target_tag == .windows) {
if (b.lazyDependency("zigwin32", .{})) |dep| {
const win32_mod = dep.module("win32");
window_mod.addImport("win32", win32_mod);
pty_mod.addImport("win32", win32_mod);
childprocess_mod.addImport("win32", win32_mod);
}
}
// Linux Dependencies
if (comptime_check or target_tag == .linux) {
if (b.lazyDependency("zig_openpty", .{})) |dep| {
const openpty_mod = dep.module("openpty");
pty_mod.addImport("openpty", openpty_mod);
}
}
// -------------------------------------------------------------------------
// Backend Specific Setup
// -------------------------------------------------------------------------
// Shaders
const compiled_shaders = @import("build/shaders.zig").compiledShadersPathes(
b,
b.path("src/renderer/shaders"),
&.{ "cell.frag", "cell.vert" },
render_backend,
) catch unreachable;
@import("build/shaders.zig").addCompiledShadersToModule(compiled_shaders, assets_mod);
// Backend Bindings
switch (render_backend) {
.d3d11 => {},
.opengl => {
const gl_mod = createOpenGLBindings(b, target);
renderer_mod.addImport("gl", gl_mod);
},
.vulkan => {
const vulkan_headers = b.lazyDependency("vulkan_headers", .{});
// Resolve Vulkan dependency based on headers presence
const vulkan_dep = if (vulkan_headers) |vk_headers|
b.lazyDependency("vulkan", .{ .registry = vk_headers.path("registry/vk.xml") })
else
b.lazyDependency("vulkan", .{});
if (vulkan_headers != null) {
if (vulkan_dep) |dep| {
renderer_mod.addImport("vulkan", dep.module("vulkan-zig"));
}
}
const core_mod = b.createModule(.{ .root_source_file = b.path("src/renderer/vulkan/core/root.zig") });
const memory_mod = b.createModule(.{ .root_source_file = b.path("src/renderer/vulkan/core/memory/root.zig") });
renderer_mod.addImport("core", core_mod);
renderer_mod.addImport("memory", memory_mod);
},
}
// -------------------------------------------------------------------------
// Application Assembly
// -------------------------------------------------------------------------
// Create Executable
const exe_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{
.{ .name = "vtparse", .module = vtparse_mod },
.{ .name = "assets", .module = assets_mod },
.{ .name = "io", .module = io_mod },
.{ .name = "pty", .module = pty_mod },
.{ .name = "grid", .module = grid_mod },
.{ .name = "math", .module = math_mod },
.{ .name = "font", .module = font_mod },
.{ .name = "color", .module = color_mod },
.{ .name = "input", .module = input_mod },
.{ .name = "window", .module = window_mod },
.{ .name = "cursor", .module = cursor_mod },
.{ .name = "renderer", .module = renderer_mod },
.{ .name = "ChildProcess", .module = childprocess_mod },
.{ .name = "DynamicLibrary", .module = dynamiclibrary_mod },
.{ .name = "circular_array", .module = circulararray_mod },
},
});
exe_mod.addImport("build_options", options_mod);
if (window_system == .xlib) {
window_mod.linkSystemLibrary("X11", .{ .needed = true });
if (render_backend == .opengl) {
renderer_mod.linkSystemLibrary("GL", .{});
}
}
if (window_system == .xcb) {
if (target.query.isNativeOs()) {
window_mod.resolved_target = target;
window_mod.linkSystemLibrary("xcb", .{});
window_mod.linkSystemLibrary("xkbcommon", .{});
} else {
if (b.lazyDependency("xcb", .{
.target = target,
.optimize = optimize,
.linkage = linkage,
})) |dep| {
window_mod.linkLibrary(dep.artifact("xcb"));
}
}
}
if (window_system == .glfw) {
if (b.lazyDependency("glfw_zig", .{
.target = target,
.optimize = optimize,
})) |dep| {
const glfw_lib = dep.artifact("glfw");
window_mod.linkLibrary(glfw_lib);
renderer_mod.linkLibrary(glfw_lib);
}
}
if (target_tag == .linux) {
if (b.lazyDependency("xkbcommon", .{
.target = target,
.optimize = optimize,
.@"xkb-config-root" = "/usr/share/X11/xkb",
})) |dep| exe_mod.linkLibrary(dep.artifact("xkbcommon"));
}
const exe = b.addExecutable(.{
.name = "zerotty",
.root_module = exe_mod,
.use_llvm = use_llvm,
});
// Windows Specific EXE settings
if (window_system == .win32 and optimize != .Debug) {
exe.subsystem = .Windows;
exe.mingw_unicode_entry_point = true;
exe.bundle_compiler_rt = true;
}
exe.addWin32ResourceFile(.{ .file = b.path("assets/zerotty.rc") });
b.installArtifact(exe);
// -------------------------------------------------------------------------
// Run Step
// -------------------------------------------------------------------------
const run_cmd = b.addRunArtifact(exe);
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);
// -------------------------------------------------------------------------
// Test Step
// -------------------------------------------------------------------------
const test_mod = b.createModule(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
// .imports = exe_imports.items,
});
test_mod.addImport("build_options", options_mod);
const unit_test = b.addTest(.{
.name = "zerotty",
.root_module = test_mod,
});
const run_unit_test = b.addRunArtifact(unit_test);
const test_step = b.step("test", "run test.zig tests");
test_step.dependOn(&run_unit_test.step);
}
// -------------------------------------------------------------------------
// Helper Functions
// -------------------------------------------------------------------------
fn createOpenGLBindings(b: *Build, target: Build.ResolvedTarget) *Build.Module {
const extensions: []const []const u8 = &.{
"KHR_debug",
"ARB_shader_storage_buffer_object",
"ARB_gl_spirv",
};
const is_gles = switch (target.result.os.tag) {
.emscripten, .wasi, .ios => true,
.linux, .windows => switch (target.result.cpu.arch) {
.arm, .armeb, .aarch64 => true,
else => false,
},
else => false,
};
const gl_target = if (is_gles) "gles-3.2" else "gl-4.1-core";
const gl = b.createModule(.{});
if (b.lazyDependency("zigglgen", .{})) |dep| {
const zigglgen_exe = dep.artifact("zigglgen");
const zigglgen_run = b.addRunArtifact(zigglgen_exe);
zigglgen_run.addArg(gl_target);
for (extensions) |extension| {
zigglgen_run.addArg(extension);
}
const output = zigglgen_run.captureStdOut();
zigglgen_run.captured_stdout.?.basename = "gl.zig";
gl.root_source_file = output;
}
return gl;
}