-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
583 lines (544 loc) · 31.5 KB
/
Copy pathbuild.zig
File metadata and controls
583 lines (544 loc) · 31.5 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
const std = @import("std");
const PlatformOption = enum {
auto,
@"null",
macos,
linux,
windows,
};
const TraceOption = enum {
off,
events,
runtime,
all,
};
const WebEngineOption = enum {
system,
chromium,
};
const PackageTarget = enum {
macos,
windows,
linux,
};
const app_exe_name = "keyparty";
pub fn build(b: *std.Build) void {
const target = zeroNativeTarget(b);
const optimize = b.standardOptimizeOption(.{});
const platform_option = b.option(PlatformOption, "platform", "Desktop backend: auto, null, macos, linux, windows") orelse .auto;
const trace_option = b.option(TraceOption, "trace", "Trace output: off, events, runtime, all") orelse .events;
const debug_overlay = b.option(bool, "debug-overlay", "Enable debug overlay output") orelse false;
const automation_enabled = b.option(bool, "automation", "Enable zero-native automation artifacts") orelse false;
const js_bridge_enabled = b.option(bool, "js-bridge", "Enable optional JavaScript bridge stubs") orelse false;
const web_engine_override = b.option(WebEngineOption, "web-engine", "Override app.zon web engine: system, chromium");
const cef_dir_override = b.option([]const u8, "cef-dir", "Override CEF root directory for Chromium builds");
const cef_auto_install_override = b.option(bool, "cef-auto-install", "Override app.zon CEF auto-install setting");
const package_target = b.option(PackageTarget, "package-target", "Package target: macos, windows, linux") orelse .macos;
// zero-native is a normal dependency (see package.json), so the framework
// sources live in the project's node_modules. Anchor to the build root so it
// resolves no matter what directory `zig build` is invoked from. Override with
// -Dzero-native-path=/path/to/zero-native to point at a different checkout.
const zero_native_path = b.option([]const u8, "zero-native-path", "Path to the zero-native framework checkout") orelse b.pathFromRoot("node_modules/zero-native");
// Folder that holds WebView2.h (from the Microsoft.Web.WebView2 NuGet package).
// Needed to compile the vendored Windows kiosk host; the CI release job sets it.
const webview2_include = b.option([]const u8, "webview2-include", "Path to the WebView2 SDK headers (folder containing WebView2.h)");
// Folder that holds wrl.h — the Windows SDK "winrt" include dir. Zig's msvc
// target adds um/shared/ucrt but not winrt, so the WebView2 host needs it
// passed in explicitly (the CI job derives it from the MSVC dev environment).
const winrt_include = b.option([]const u8, "winrt-include", "Path to the Windows SDK winrt headers (folder containing wrl.h)");
// Folder holding WebView2LoaderStatic.lib (the WebView2 NuGet's build/native/x64).
// Static-linking the loader means no WebView2Loader.dll to ship — the app is a
// single self-contained exe. Required for Windows -Dweb-engine=system builds.
const webview2_lib_dir = b.option([]const u8, "webview2-lib-dir", "Path to the WebView2 static loader lib dir (contains WebView2LoaderStatic.lib)");
const optimize_name = @tagName(optimize);
// Single source of truth for the version (kept in sync by scripts/sync-version.mjs).
const app_version = stringField(@embedFile("build.zig.zon"), ".version") orelse "0.0.0";
const selected_platform: PlatformOption = switch (platform_option) {
.auto => if (target.result.os.tag == .macos) .macos else if (target.result.os.tag == .linux) .linux else if (target.result.os.tag == .windows) .windows else .@"null",
else => platform_option,
};
if (selected_platform == .macos and target.result.os.tag != .macos) {
@panic("-Dplatform=macos requires a macOS target");
}
if (selected_platform == .linux and target.result.os.tag != .linux) {
@panic("-Dplatform=linux requires a Linux target");
}
if (selected_platform == .windows and target.result.os.tag != .windows) {
@panic("-Dplatform=windows requires a Windows target");
}
const app_web_engine = appWebEngineConfig();
const web_engine = web_engine_override orelse app_web_engine.web_engine;
const cef_dir = cef_dir_override orelse defaultCefDir(selected_platform, app_web_engine.cef_dir);
const cef_auto_install = cef_auto_install_override orelse app_web_engine.cef_auto_install;
if (web_engine == .chromium and selected_platform == .@"null") {
@panic("-Dweb-engine=chromium requires -Dplatform=macos, linux, or windows");
}
const zero_native_mod = zeroNativeModule(b, target, optimize, zero_native_path);
const options = b.addOptions();
options.addOption([]const u8, "platform", switch (selected_platform) {
.auto => unreachable,
.@"null" => "null",
.macos => "macos",
.linux => "linux",
.windows => "windows",
});
options.addOption([]const u8, "trace", @tagName(trace_option));
options.addOption([]const u8, "web_engine", @tagName(web_engine));
options.addOption(bool, "debug_overlay", debug_overlay);
options.addOption(bool, "automation", automation_enabled);
options.addOption(bool, "js_bridge", js_bridge_enabled);
const options_mod = options.createModule();
const runner_mod = localModule(b, target, optimize, "src/runner.zig");
runner_mod.addImport("zero-native", zero_native_mod);
runner_mod.addImport("build_options", options_mod);
const app_mod = localModule(b, target, optimize, "src/main.zig");
app_mod.addImport("zero-native", zero_native_mod);
app_mod.addImport("runner", runner_mod);
const exe = b.addExecutable(.{
.name = app_exe_name,
.root_module = app_mod,
});
linkPlatform(b, target, app_mod, exe, selected_platform, web_engine, zero_native_path, cef_dir, cef_auto_install, webview2_include, winrt_include, webview2_lib_dir);
// Embed the app icon into the Windows exe (assets/icon.rc -> assets/icon.ico).
// The lowest-ID icon group is what Explorer/taskbar show for the binary, and
// native/webview2_host.cpp loads it by resource id for the window. Zig's
// built-in resource compiler (resinator) needs assets/ on its include path so
// the .rc's relative reference to icon.ico resolves.
if (selected_platform == .windows) {
app_mod.addWin32ResourceFile(.{
.file = b.path("assets/icon.rc"),
.include_paths = &.{b.path("assets")},
});
}
b.installArtifact(exe);
const frontend_install = b.addSystemCommand(&.{ "npm", "install", "--prefix", "frontend" });
const frontend_install_step = b.step("frontend-install", "Install frontend dependencies");
frontend_install_step.dependOn(&frontend_install.step);
const frontend_build = b.addSystemCommand(&.{ "npm", "--prefix", "frontend", "run", "build" });
frontend_build.step.dependOn(&frontend_install.step);
const frontend_step = b.step("frontend-build", "Build the frontend");
frontend_step.dependOn(&frontend_build.step);
// Single-file Windows: embed the built frontend into the exe (the host serves
// it from memory) so there is no resources/ folder to ship alongside it.
if (selected_platform == .windows and web_engine == .system) {
addWindowsAssetEmbed(b, app_mod, frontend_build);
}
const run = b.addRunArtifact(exe);
run.step.dependOn(&frontend_build.step);
addCefRuntimeRunFiles(b, target, run, exe, web_engine, cef_dir);
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run.step);
const dev = b.addSystemCommand(&.{ "zero-native", "dev", "--manifest", "app.zon", "--binary" });
dev.addFileArg(exe.getEmittedBin());
dev.step.dependOn(&exe.step);
dev.step.dependOn(&frontend_install.step);
const dev_step = b.step("dev", "Run the frontend dev server and native shell");
dev_step.dependOn(&dev.step);
const package = b.addSystemCommand(&.{
"zero-native",
"package",
"--target",
@tagName(package_target),
"--manifest",
"app.zon",
"--assets","frontend/out",
"--optimize",
optimize_name,
"--output",
b.fmt("zig-out/package/{s}-{s}-{s}-{s}{s}", .{ app_exe_name, app_version, @tagName(package_target), optimize_name, packageSuffix(package_target) }),
"--binary",
});
package.addFileArg(exe.getEmittedBin());
package.addArgs(&.{ "--web-engine", @tagName(web_engine), "--cef-dir", cef_dir });
if (cef_auto_install) package.addArg("--cef-auto-install");
package.step.dependOn(&exe.step);
package.step.dependOn(&frontend_build.step);
const package_step = b.step("package", "Create a local package artifact");
package_step.dependOn(&package.step);
const tests = b.addTest(.{ .root_module = app_mod });
const test_step = b.step("test", "Run tests");
test_step.dependOn(&b.addRunArtifact(tests).step);
}
fn zeroNativeTarget(b: *std.Build) std.Build.ResolvedTarget {
const target = b.standardTargetOptions(.{});
if (target.result.os.tag != .macos) return target;
if (b.sysroot == null) {
b.sysroot = macosSdkPath(b) orelse b.sysroot;
}
var query = target.query;
query.os_tag = .macos;
query.os_version_min = .{ .semver = .{ .major = 11, .minor = 0, .patch = 0 } };
return b.resolveTargetQuery(query);
}
fn macosSdkPath(b: *std.Build) ?[]const u8 {
if (b.graph.environ_map.get("SDKROOT")) |sdkroot| {
if (sdkroot.len > 0) return sdkroot;
}
const result = std.process.run(b.allocator, b.graph.io, .{
.argv = &.{ "xcrun", "--sdk", "macosx", "--show-sdk-path" },
.stdout_limit = .limited(4096),
.stderr_limit = .limited(4096),
}) catch return null;
defer b.allocator.free(result.stderr);
if (result.term != .exited or result.term.exited != 0) {
b.allocator.free(result.stdout);
return null;
}
return std.mem.trimEnd(u8, result.stdout, "\r\n");
}
fn localModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, path: []const u8) *std.Build.Module {
return b.createModule(.{
.root_source_file = b.path(path),
.target = target,
.optimize = optimize,
});
}
fn zeroNativePath(b: *std.Build, zero_native_path: []const u8, sub_path: []const u8) std.Build.LazyPath {
return .{ .cwd_relative = b.pathJoin(&.{ zero_native_path, sub_path }) };
}
fn zeroNativeModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, zero_native_path: []const u8) *std.Build.Module {
const geometry_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/geometry/root.zig");
const assets_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/assets/root.zig");
const app_dirs_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/app_dirs/root.zig");
const trace_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/trace/root.zig");
const app_manifest_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/app_manifest/root.zig");
const diagnostics_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/diagnostics/root.zig");
const platform_info_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/platform_info/root.zig");
const json_mod = externalModule(b, target, optimize, zero_native_path, "src/primitives/json/root.zig");
const debug_mod = externalModule(b, target, optimize, zero_native_path, "src/debug/root.zig");
debug_mod.addImport("app_dirs", app_dirs_mod);
debug_mod.addImport("trace", trace_mod);
const zero_native_mod = externalModule(b, target, optimize, zero_native_path, "src/root.zig");
zero_native_mod.addImport("geometry", geometry_mod);
zero_native_mod.addImport("assets", assets_mod);
zero_native_mod.addImport("app_dirs", app_dirs_mod);
zero_native_mod.addImport("trace", trace_mod);
zero_native_mod.addImport("app_manifest", app_manifest_mod);
zero_native_mod.addImport("diagnostics", diagnostics_mod);
zero_native_mod.addImport("platform_info", platform_info_mod);
zero_native_mod.addImport("json", json_mod);
return zero_native_mod;
}
fn externalModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, zero_native_path: []const u8, path: []const u8) *std.Build.Module {
return b.createModule(.{
.root_source_file = zeroNativePath(b, zero_native_path, path),
.target = target,
.optimize = optimize,
});
}
fn linkPlatform(b: *std.Build, target: std.Build.ResolvedTarget, app_mod: *std.Build.Module, exe: *std.Build.Step.Compile, platform: PlatformOption, web_engine: WebEngineOption, zero_native_path: []const u8, cef_dir: []const u8, cef_auto_install: bool, webview2_include: ?[]const u8, winrt_include: ?[]const u8, webview2_lib_dir: ?[]const u8) void {
if (platform == .macos) {
switch (web_engine) {
.system => {
const sdk_include = if (b.sysroot) |sysroot| b.fmt("-I{s}/usr/include", .{sysroot}) else "";
const flags: []const []const u8 = if (b.sysroot) |sysroot| &.{ "-fobjc-arc", "-ObjC", "-mmacosx-version-min=11.0", "-isysroot", sysroot, sdk_include } else &.{ "-fobjc-arc", "-ObjC", "-mmacosx-version-min=11.0" };
// KeyParty vendors a patched copy of the AppKit host (native/appkit_host.m)
// that adds full-screen kiosk mode, key-blocking, and the quit chord.
// It keeps the framework's C ABI, so the rest of zero-native is unchanged.
app_mod.addCSourceFile(.{ .file = b.path("native/appkit_host.m"), .flags = flags });
app_mod.linkFramework("WebKit", .{});
},
.chromium => {
const cef_check = addCefCheck(b, target, cef_dir);
if (cef_auto_install) {
const cef_auto = b.addSystemCommand(&.{ "zero-native", "cef", "install", "--dir", cef_dir });
cef_check.step.dependOn(&cef_auto.step);
}
exe.step.dependOn(&cef_check.step);
const include_arg = b.fmt("-I{s}", .{cef_dir});
const define_arg = b.fmt("-DZERO_NATIVE_CEF_DIR=\"{s}\"", .{cef_dir});
const sdk_include = if (b.sysroot) |sysroot| b.fmt("-I{s}/usr/include", .{sysroot}) else "";
const flags: []const []const u8 = if (b.sysroot) |sysroot| &.{ "-fobjc-arc", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", "-isysroot", sysroot, sdk_include, include_arg, define_arg } else &.{ "-fobjc-arc", "-ObjC++", "-std=c++17", "-stdlib=libc++", "-mmacosx-version-min=11.0", include_arg, define_arg };
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/macos/cef_host.mm"), .flags = flags });
app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
app_mod.addFrameworkPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
app_mod.linkFramework("Chromium Embedded Framework", .{});
app_mod.addRPath(.{ .cwd_relative = "@executable_path/Frameworks" });
},
}
if (b.sysroot) |sysroot| {
app_mod.addFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "System/Library/Frameworks" }) });
}
app_mod.linkFramework("AppKit", .{});
app_mod.linkFramework("Foundation", .{});
app_mod.linkFramework("UniformTypeIdentifiers", .{});
// KeyParty: CGEventTap (CoreGraphics) + Accessibility checks (ApplicationServices).
app_mod.linkFramework("CoreGraphics", .{});
app_mod.linkFramework("ApplicationServices", .{});
app_mod.linkSystemLibrary("c", .{});
if (web_engine == .chromium) app_mod.linkSystemLibrary("c++", .{});
} else if (platform == .linux) {
switch (web_engine) {
.system => {
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/linux/gtk_host.c"), .flags = &.{} });
app_mod.linkSystemLibrary("gtk4", .{});
app_mod.linkSystemLibrary("webkitgtk-6.0", .{});
},
.chromium => {
const cef_check = addCefCheck(b, target, cef_dir);
if (cef_auto_install) {
const cef_auto = b.addSystemCommand(&.{ "zero-native", "cef", "install", "--dir", cef_dir });
cef_check.step.dependOn(&cef_auto.step);
}
exe.step.dependOn(&cef_check.step);
const include_arg = b.fmt("-I{s}", .{cef_dir});
const define_arg = b.fmt("-DZERO_NATIVE_CEF_DIR=\"{s}\"", .{cef_dir});
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/linux/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.a", .{cef_dir})));
app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
app_mod.linkSystemLibrary("cef", .{});
app_mod.addRPath(.{ .cwd_relative = "$ORIGIN" });
},
}
app_mod.linkSystemLibrary("c", .{});
if (web_engine == .chromium) app_mod.linkSystemLibrary("stdc++", .{});
} else if (platform == .windows) {
// GUI app, not a console app — otherwise Windows opens a terminal window
// alongside the game. The MSVC CRT's GUI startup (WinMainCRTStartup) wants
// a WinMain, but our entry is the Zig/C `main`, so keep the console CRT
// startup (mainCRTStartup, which calls main) while still marking the binary
// as a GUI subsystem so no console is allocated. (Dev logs go to files via
// ZERO_NATIVE_LOG_DIR.)
exe.subsystem = .Windows;
exe.entry = .{ .symbol_name = "mainCRTStartup" };
switch (web_engine) {
// KeyParty vendors a patched copy of the WebView2 host (native/webview2_host.cpp)
// that adds full-screen kiosk mode, the global key-blocking hook, the quit
// chord, and the JS bridge the menu UI needs. It keeps the framework's C ABI,
// so the rest of zero-native is unchanged.
.system => {
// WebView2 + WRL (wrl.h) is MSVC-flavored C++: Zig's bundled clang
// can't build the Windows SDK winrt headers, and its libc++abi
// collides with MSVC's vcruntime on the msvc ABI (the "sub-
// compilation of libcxxabi failed" / conflicting type_info error).
// So compile the host with the real MSVC compiler — cl.exe, on PATH
// in an x64 Native Tools prompt or the CI msvc-dev-cmd environment —
// and link the resulting object. cl.exe resolves the Windows SDK,
// winrt (wrl.h), and STL headers from %INCLUDE%; only the WebView2
// SDK headers (from NuGet) need to be passed in. /MD matches the
// dynamic UCRT Zig links for the msvc target.
// cl.exe writes its diagnostics (errors AND warnings) to STDOUT, not
// stderr. Zig's Run step captures a command's stdout when it has an
// output-file argument (the /Fo object below) and does not echo it on
// failure — only stderr — so a cl compile error would otherwise show
// up in CI as a bare "process exited with error code 2" with no
// message. Run cl through cmd.exe and redirect its stdout to stderr
// (the trailing "1>&2") so failures are diagnosable. Zig passes "1>&2"
// verbatim (it only quotes args with spaces/tabs/quotes), so cmd sees
// it as a redirection rather than an argument to cl.
const cl = b.addSystemCommand(&.{ "cmd", "/C", "cl", "/nologo", "/c", "/std:c++17", "/EHsc", "/MD", "/O2" });
if (webview2_include) |inc| cl.addArg(b.fmt("/I{s}", .{inc}));
// winrt is already on cl's %INCLUDE%; pass it too if provided, as a
// belt-and-suspenders for setups where it isn't.
if (winrt_include) |inc| cl.addArg(b.fmt("/I{s}", .{inc}));
const host_obj = cl.addPrefixedOutputFileArg("/Fo", "webview2_host.obj");
cl.addFileArg(b.path("native/webview2_host.cpp"));
cl.addArg("1>&2");
app_mod.addObjectFile(host_obj);
},
.chromium => {
const cef_check = addCefCheck(b, target, cef_dir);
if (cef_auto_install) {
const cef_auto = b.addSystemCommand(&.{ "zero-native", "cef", "install", "--dir", cef_dir });
cef_check.step.dependOn(&cef_auto.step);
}
exe.step.dependOn(&cef_check.step);
const include_arg = b.fmt("-I{s}", .{cef_dir});
const define_arg = b.fmt("-DZERO_NATIVE_CEF_DIR=\"{s}\"", .{cef_dir});
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/windows/cef_host.cpp"), .flags = &.{ "-std=c++17", include_arg, define_arg } });
app_mod.addObjectFile(b.path(b.fmt("{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib", .{cef_dir})));
app_mod.addLibraryPath(b.path(b.fmt("{s}/Release", .{cef_dir})));
},
}
app_mod.linkSystemLibrary("c", .{});
if (web_engine == .chromium) {
// The CEF host is compiled by Zig, so it uses Zig's libc++.
app_mod.linkSystemLibrary("c++", .{});
app_mod.linkSystemLibrary("libcef", .{});
} else {
// The WebView2 host is compiled by cl.exe (/MD), so its C++ standard
// library comes from MSVC's dynamic runtime (msvcprt.lib), not Zig's
// libc++ — whose libc++abi can't build against the MSVC vcruntime ABI.
app_mod.linkSystemLibrary("msvcprt", .{});
// Static WebView2 loader -> no WebView2Loader.dll to ship (single exe).
if (webview2_lib_dir) |dir| {
app_mod.addLibraryPath(.{ .cwd_relative = dir });
app_mod.linkSystemLibrary("WebView2LoaderStatic", .{});
}
// SHCreateMemStream (embedded-asset responses) + deps of the static loader.
app_mod.linkSystemLibrary("shlwapi", .{});
app_mod.linkSystemLibrary("version", .{});
app_mod.linkSystemLibrary("advapi32", .{});
}
app_mod.linkSystemLibrary("user32", .{});
app_mod.linkSystemLibrary("ole32", .{});
app_mod.linkSystemLibrary("shell32", .{});
// DwmSetWindowAttribute — dark/colored title bar (native/webview2_host.cpp).
app_mod.linkSystemLibrary("dwmapi", .{});
// CreateSolidBrush — dark window-class background (native/webview2_host.cpp).
app_mod.linkSystemLibrary("gdi32", .{});
// DCompositionCreateDevice — composition (visual) hosting that lets the
// WebView2 render transparently for the see-through backdrop modes
// (native/webview2_host.cpp).
app_mod.linkSystemLibrary("dcomp", .{});
}
}
// Embed the built frontend (frontend/out) into the Windows exe: a Node script
// generates a C translation unit holding the assets as one byte blob + a lookup
// table, which cl.exe compiles and we link in. The host (native/webview2_host.cpp)
// serves it from memory via WebResourceRequested, so the package needs no
// resources/ folder — the exe is fully self-contained.
fn addWindowsAssetEmbed(b: *std.Build, app_mod: *std.Build.Module, frontend_build: *std.Build.Step.Run) void {
const gen = b.addSystemCommand(&.{ "node", "scripts/embed-assets.mjs" });
gen.addArg(b.pathFromRoot("frontend/out"));
const gen_c = gen.addOutputFileArg("keyparty_assets.c");
gen.step.dependOn(&frontend_build.step); // needs the built frontend on disk
// Always regenerate the embed; never serve it from Zig's build cache. This Run
// step produces an output file, so Zig would normally cache it keyed on its argv
// — which is CONSTANT (the frontend/out path string), regardless of what npm
// actually rebuilt into that directory. So a fresh frontend reused a previously
// generated keyparty_assets.c and the exe shipped a STALE embedded UI (e.g. the
// backdrop toggle was missing because page.tsx changed but the cached embed did
// not). addDirectoryArg did NOT fix this: Zig does not content-hash a directory
// arg, so the key stayed constant and the cache still hit. Worse, mlugg/setup-zig
// restores the Zig cache across CI runs, so the stale embed persisted build after
// build while native (recompiled-every-time) changes landed — the exact "native
// updates, web doesn't" symptom. has_side_effects skips the cache lookup so the
// embed is rebuilt from the current frontend/out every time. The downstream cl
// compile is still cached on the generated C's contents, so unchanged frontends
// cost only one fast node run.
gen.has_side_effects = true;
// cl.exe so the CRT matches the host's (/MD); the file is plain C.
const cl = b.addSystemCommand(&.{ "cl", "/nologo", "/c", "/O2", "/MD" });
const obj = cl.addPrefixedOutputFileArg("/Fo", "keyparty_assets.obj");
cl.addFileArg(gen_c);
app_mod.addObjectFile(obj);
}
fn addCefRuntimeRunFiles(b: *std.Build, target: std.Build.ResolvedTarget, run: *std.Build.Step.Run, exe: *std.Build.Step.Compile, web_engine: WebEngineOption, cef_dir: []const u8) void {
if (web_engine != .chromium) return;
if (target.result.os.tag != .macos) return;
const copy = b.addSystemCommand(&.{ "sh", "-c", b.fmt(
\\set -e
\\exe="$0"
\\exe_dir="$(dirname "$exe")"
\\rm -rf "zig-out/Frameworks/Chromium Embedded Framework.framework" "zig-out/bin/Frameworks/Chromium Embedded Framework.framework" ".zig-cache/o/Frameworks/Chromium Embedded Framework.framework" &&
\\mkdir -p "zig-out/Frameworks" "zig-out/bin/Frameworks" ".zig-cache/o/Frameworks" "$exe_dir" &&
\\cp -R "{s}/Release/Chromium Embedded Framework.framework" "zig-out/Frameworks/" &&
\\cp -R "{s}/Release/Chromium Embedded Framework.framework" "zig-out/bin/Frameworks/" &&
\\cp -R "{s}/Release/Chromium Embedded Framework.framework" ".zig-cache/o/Frameworks/" &&
\\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libEGL.dylib" "$exe_dir/" &&
\\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libGLESv2.dylib" "$exe_dir/" &&
\\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/libvk_swiftshader.dylib" "$exe_dir/" &&
\\cp "{s}/Release/Chromium Embedded Framework.framework/Libraries/vk_swiftshader_icd.json" "$exe_dir/"
, .{ cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir }) });
copy.addFileArg(exe.getEmittedBin());
run.step.dependOn(©.step);
}
fn addCefCheck(b: *std.Build, target: std.Build.ResolvedTarget, cef_dir: []const u8) *std.Build.Step.Run {
const script = switch (target.result.os.tag) {
.macos => b.fmt(
\\test -f "{s}/include/cef_app.h" &&
\\test -d "{s}/Release/Chromium Embedded Framework.framework" &&
\\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.a" || {{
\\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
\\ echo "Expected:" >&2
\\ echo " {s}/include/cef_app.h" >&2
\\ echo " {s}/Release/Chromium Embedded Framework.framework" >&2
\\ echo " {s}/libcef_dll_wrapper/libcef_dll_wrapper.a" >&2
\\ echo "Fix with: zero-native cef install --dir {s}" >&2
\\ echo "Or rerun with: -Dcef-auto-install=true" >&2
\\ echo "Pass -Dcef-dir=/path/to/cef if your bundle lives elsewhere." >&2
\\ exit 1
\\}}
, .{ cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir, cef_dir }),
.linux => b.fmt(
\\test -f "{s}/include/cef_app.h" &&
\\test -f "{s}/Release/libcef.so" &&
\\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.a" || {{
\\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
\\ echo "Fix with: zero-native cef install --dir {s}" >&2
\\ exit 1
\\}}
, .{ cef_dir, cef_dir, cef_dir, cef_dir }),
.windows => b.fmt(
\\test -f "{s}/include/cef_app.h" &&
\\test -f "{s}/Release/libcef.dll" &&
\\test -f "{s}/libcef_dll_wrapper/libcef_dll_wrapper.lib" || {{
\\ echo "missing CEF dependency for -Dweb-engine=chromium" >&2
\\ echo "Fix with: zero-native cef install --dir {s}" >&2
\\ exit 1
\\}}
, .{ cef_dir, cef_dir, cef_dir, cef_dir }),
else => "echo unsupported CEF target >&2; exit 1",
};
return b.addSystemCommand(&.{ "sh", "-c", script });
}
fn packageSuffix(target: PackageTarget) []const u8 {
return switch (target) {
.macos => ".app",
.windows, .linux => "",
};
}
const AppWebEngineConfig = struct {
web_engine: WebEngineOption = .system,
cef_dir: []const u8 = "third_party/cef/macos",
cef_auto_install: bool = false,
};
fn defaultCefDir(platform: PlatformOption, configured: []const u8) []const u8 {
if (!std.mem.eql(u8, configured, "third_party/cef/macos")) return configured;
return switch (platform) {
.linux => "third_party/cef/linux",
.windows => "third_party/cef/windows",
else => configured,
};
}
fn appWebEngineConfig() AppWebEngineConfig {
const source = @embedFile("app.zon");
var config: AppWebEngineConfig = .{};
if (stringField(source, ".web_engine")) |value| {
config.web_engine = parseWebEngine(value) orelse .system;
}
if (objectSection(source, ".cef")) |cef| {
if (stringField(cef, ".dir")) |value| config.cef_dir = value;
if (boolField(cef, ".auto_install")) |value| config.cef_auto_install = value;
}
return config;
}
fn parseWebEngine(value: []const u8) ?WebEngineOption {
if (std.mem.eql(u8, value, "system")) return .system;
if (std.mem.eql(u8, value, "chromium")) return .chromium;
return null;
}
fn stringField(source: []const u8, field: []const u8) ?[]const u8 {
const field_index = std.mem.indexOf(u8, source, field) orelse return null;
const equals = std.mem.indexOfScalarPos(u8, source, field_index, '=') orelse return null;
const start_quote = std.mem.indexOfScalarPos(u8, source, equals, '"') orelse return null;
const end_quote = std.mem.indexOfScalarPos(u8, source, start_quote + 1, '"') orelse return null;
return source[start_quote + 1 .. end_quote];
}
fn objectSection(source: []const u8, field: []const u8) ?[]const u8 {
const field_index = std.mem.indexOf(u8, source, field) orelse return null;
const open = std.mem.indexOfScalarPos(u8, source, field_index, '{') orelse return null;
var depth: usize = 0;
var index = open;
while (index < source.len) : (index += 1) {
switch (source[index]) {
'{' => depth += 1,
'}' => {
depth -= 1;
if (depth == 0) return source[open + 1 .. index];
},
else => {},
}
}
return null;
}
fn boolField(source: []const u8, field: []const u8) ?bool {
const field_index = std.mem.indexOf(u8, source, field) orelse return null;
const equals = std.mem.indexOfScalarPos(u8, source, field_index, '=') orelse return null;
var index = equals + 1;
while (index < source.len and std.ascii.isWhitespace(source[index])) : (index += 1) {}
if (std.mem.startsWith(u8, source[index..], "true")) return true;
if (std.mem.startsWith(u8, source[index..], "false")) return false;
return null;
}