-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
429 lines (398 loc) · 20.9 KB
/
build.zig
File metadata and controls
429 lines (398 loc) · 20.9 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
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 default_zero_native_path ="/Users/mindon/.nvm/versions/node/v22.15.0/lib/node_modules/zero-native";
const app_exe_name = "znbiu";
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
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;
const zero_native_path = b.option([]const u8, "zero-native-path", "Path to the zero-native framework checkout") orelse default_zero_native_path;
const optimize_name = @tagName(optimize);
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 app_options = b.addOptions();
app_options.addOption([]const u8, "id", app_web_engine.id);
app_options.addOption([]const u8, "name", app_web_engine.name);
app_options.addOption([]const u8, "display", app_web_engine.display);
app_options.addOption([]const u8, "out", app_web_engine.frontend_out);
const app_options_mod = app_options.createModule();
app_mod.addImport("app_options", app_options_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);
b.installArtifact(exe);
const frontend_build = b.addSystemCommand(&.{ "biu" });
const frontend_step = b.step("frontend-build", "Build the frontend");
frontend_step.dependOn(&frontend_build.step);
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);
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/dist",
"--optimize",
optimize_name,
"--output",
b.fmt("zig-out/package/{s}-0.1.0-{s}-{s}{s}", .{ app_exe_name, @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 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) void {
if (platform == .macos) {
switch (web_engine) {
.system => {
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/macos/appkit_host.m"), .flags = &.{ "-fobjc-arc", "-ObjC" } });
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});
app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/macos/cef_host.mm"), .flags = &.{ "-fobjc-arc", "-ObjC++", "-std=c++17", "-stdlib=libc++", include_arg, define_arg } });
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" });
},
}
app_mod.linkFramework("AppKit", .{});
app_mod.linkFramework("Foundation", .{});
app_mod.linkFramework("UniformTypeIdentifiers", .{});
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) {
switch (web_engine) {
.system => app_mod.addCSourceFile(.{ .file = zeroNativePath(b, zero_native_path, "src/platform/windows/webview2_host.cpp"), .flags = &.{ "-std=c++17" } }),
.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", .{});
app_mod.linkSystemLibrary("c++", .{});
app_mod.linkSystemLibrary("user32", .{});
app_mod.linkSystemLibrary("ole32", .{});
app_mod.linkSystemLibrary("shell32", .{});
if (web_engine == .chromium) app_mod.linkSystemLibrary("libcef", .{});
}
}
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 {
id: []const u8 = "",
name: []const u8 = "znbiu",
display: []const u8 = "ZeroN-biu",
frontend_out: []const u8 = "dist",
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, ".id")) |value| {
config.id = value;
}
if (stringField(source, ".name")) |value| {
config.name = value;
}
if (stringField(source, ".display_name")) |value| {
config.display = value;
}
if (stringField(source, ".display_name")) |value| {
config.display = value;
}
if (objectSection(source, ".frontend")) |frontend| {
if (stringField(frontend, ".dist")) |value| {
config.frontend_out = value;
}
}
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;
}