Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
327 lines (312 loc) · 21.6 KB
/
Copy pathbuild.zig
File metadata and controls
327 lines (312 loc) · 21.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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const lib = b.addModule("opengorky", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
addCDeps(b, lib);
addFoundation(b, lib);
// Plain `zig build` builds everything with -Doptimize (default Debug): one engine
// copy, ~20 s after a one-line change. `-Drelease` is the optimized setup for checks
// and play: the viewer and the editor ReleaseFast, gorky-tool ReleaseSafe (three
// engine copies through LLVM, 2–10 min). -Dviewer-optimize / -Dtool-optimize override
// either. The packages (scripts/dist-*.sh) pass -Doptimize=ReleaseSafe and
// -Dviewer-optimize themselves.
const release = b.option(bool, "release", "Optimized build: viewer and editor ReleaseFast, gorky-tool ReleaseSafe") orelse false;
// gorky-tool runs the whole game in checks (scripts/fullgame.sh, linux/check.sh):
// a debug build takes ~1 min, mostly in the debug allocator; with -Drelease it (and
// its engine copy) builds ReleaseSafe — bounds and overflow checks stay. The game run
// does not depend on the mode: logic has no @setFloatMode (no FMA or reordering),
// Debug/ReleaseSafe/ReleaseSmall runs give the same log.
const tool_optimize = b.option(std.builtin.OptimizeMode, "tool-optimize", "gorky-tool optimization (default: -Doptimize; safe with -Drelease)") orelse
if (release and optimize == .debug) .safe else optimize;
const tool_lib = if (tool_optimize == optimize) lib else engineModule(b, target, tool_optimize);
const tool = b.addExecutable(.{
.name = "gorky-tool",
.root_module = b.createModule(.{
.root_source_file = b.path("src/tool/main.zig"),
.target = target,
.optimize = tool_optimize,
.imports = &.{.{ .name = "opengorky", .module = tool_lib }},
}),
});
// `-Dtool=false` — packages (scripts/dist-*.sh) ship only the viewer and the editor:
// gorky-tool (a developer's console tool) is not built for them.
if (b.option(bool, "tool", "Install gorky-tool (default true)") orelse true) b.installArtifact(tool);
const run_tool = b.addRunArtifact(tool);
run_tool.step.dependOn(b.getInstallStep());
run_tool.addPassthruArgs();
b.step("tool", "Run gorky-tool").dependOn(&run_tool.step);
// SDL3 is linked as a system library (brew install sdl3 / package libsdl3-dev).
// `-Dsdl-prefix=DIR` — our own build (scripts/build-sdl3.sh: universal arm64 +
// x86_64 for macOS distribution) instead of pkg-config.
const sdl_prefix = b.option([]const u8, "sdl-prefix", "Own SDL3 build dir (include/, lib/) instead of pkg-config");
const sdl = b.addTranslateC(.{
.root_source_file = b.path("src/viewer/sdl.h"),
.target = target,
.optimize = optimize,
});
var pkg_config_exit: u8 = 0;
if (sdl_prefix) |pre| {
sdl.addIncludePath(.{ .cwd_relative = b.fmt("{s}/include", .{pre}) });
} else if (b.runAllowFail(&.{ "pkg-config", "--cflags-only-I", "sdl3" }, &pkg_config_exit, .ignore)) |out| {
var it = std.mem.tokenizeAny(u8, out, " \n");
while (it.next()) |flag| {
if (std.mem.startsWith(u8, flag, "-I")) sdl.addSystemIncludePath(.{ .cwd_relative = flag[2..] });
}
} else |_| {}
const sdl_mod = sdl.createModule();
if (sdl_prefix) |pre| {
const ext = switch (target.result.os.tag) {
.macos => "libSDL3.0.dylib",
.windows => "libSDL3.dll.a", // import library; SDL3.dll goes next to the exe
else => "libSDL3.so.0",
};
sdl_mod.addObjectFile(.{ .cwd_relative = b.fmt("{s}/lib/{s}", .{ pre, ext }) });
} else sdl_mod.linkSystemLibrary("SDL3", .{});
// FFmpeg for videos and the editor's imports (libavformat etc. via pkg-config,
// shared); without it videos play through an external ffmpeg program if present and
// the editor cannot import.
// `-Dffmpeg-prefix=DIR` — our own FFmpeg build (scripts/build-ffmpeg.sh: all
// decoders, the game's encoders, LAME), linked statically, instead of pkg-config.
const ff_libs = [_][]const u8{ "libavformat", "libavcodec", "libswscale", "libswresample", "libavutil" };
const ff_prefix = b.option([]const u8, "ffmpeg-prefix", "Own FFmpeg build dir (include/, lib/) instead of pkg-config");
var ff_found = true;
var ff_cflags: ?[]const u8 = null;
if (ff_prefix) |pre| {
ff_cflags = b.fmt("-I{s}/include", .{pre});
} else if (b.runAllowFail(&(.{ "pkg-config", "--cflags-only-I" } ++ ff_libs), &pkg_config_exit, .ignore)) |out| {
ff_cflags = out;
} else |_| ff_found = false;
const use_ffmpeg = b.option(bool, "ffmpeg", "Decode videos with linked FFmpeg (default: if found)") orelse ff_found;
const options = b.addOptions();
options.addOption(bool, "ffmpeg", use_ffmpeg);
const options_mod = options.createModule();
const ffmpeg = b.addTranslateC(.{
.root_source_file = b.path("src/viewer/ffmpeg.h"),
.target = target,
.optimize = optimize,
});
// Own-build headers via -I: they must come before the system ones (otherwise on
// Linux with libav*-dev packages headers of another FFmpeg version get picked up
// and struct layouts diverge from the libraries).
if (ff_prefix) |pre| {
ffmpeg.addIncludePath(.{ .cwd_relative = b.fmt("{s}/include", .{pre}) });
} else if (ff_cflags) |out| {
var it = std.mem.tokenizeAny(u8, out, " \n");
while (it.next()) |flag| {
if (std.mem.startsWith(u8, flag, "-I")) ffmpeg.addSystemIncludePath(.{ .cwd_relative = flag[2..] });
}
}
const ffmpeg_mod = ffmpeg.createModule();
// Own build by full archive path: otherwise the linker finds the system FFmpeg in
// the SDL3 directory (Homebrew) first.
if (use_ffmpeg) for (ff_libs ++ .{"libmp3lame"}) |l| {
if (ff_prefix) |pre| {
ffmpeg_mod.addObjectFile(.{ .cwd_relative = b.fmt("{s}/lib/{s}.a", .{ pre, l }) });
} else if (!std.mem.eql(u8, l, "libmp3lame")) ffmpeg_mod.linkSystemLibrary(l[3..], .{});
};
// Static libraries' system needs: Windows' bcrypt (libavutil's random seed); the
// macOS frameworks libavutil.pc lists go unused without VideoToolbox.
if (use_ffmpeg and ff_prefix != null and target.result.os.tag == .windows) ffmpeg_mod.linkSystemLibrary("bcrypt", .{});
// The viewer renders models in software: a debug build is ~10× slower, so with
// -Drelease it (and its engine copy) builds ReleaseFast. Not
// ReleaseSmall: there LLVM (Zig 0.17, arm64) outlines repeated code
// (MachineOutliner) and clobbers registers — in combat.devices.destroyBarrel the
// return address saved in x2 was taken by an outlined chunk as argument `v`, and
// shooting a barrel crashed the game (EXC_BAD_ACCESS). The outliner cannot be
// disabled in Zig 0.17. `-Dviewer-optimize=safe` keeps bounds and overflow checks
// (~14% slower), `=debug` builds a debug viewer.
// Release logic is the same: Zig does not reorder floating-point operations without
// @setFloatMode. Tests use -Doptimize (default Debug), gorky-tool -Dtool-optimize.
// `-Dstrip` drops debug info (smaller binary; for distribution in
// scripts/dist-*.sh; during development it gives crash stacks).
const strip = b.option(bool, "strip", "Strip debug info from the viewer") orelse false;
const viewer_optimize = b.option(std.builtin.OptimizeMode, "viewer-optimize", "Viewer and editor optimization (default: -Doptimize; fast with -Drelease)") orelse
if (release and optimize == .debug) .fast else optimize;
const viewer_lib = if (viewer_optimize == optimize) lib else if (viewer_optimize == tool_optimize) tool_lib else engineModule(b, target, viewer_optimize);
const viewer = b.addExecutable(.{
.name = "opengorky-viewer",
.root_module = b.createModule(.{
.root_source_file = b.path("src/viewer/main.zig"),
.target = target,
.optimize = viewer_optimize,
.strip = strip,
.imports = if (use_ffmpeg) &.{
.{ .name = "opengorky", .module = viewer_lib },
.{ .name = "sdl", .module = sdl_mod },
.{ .name = "ffmpeg", .module = ffmpeg_mod },
.{ .name = "build_options", .module = options_mod },
} else &.{
.{ .name = "opengorky", .module = viewer_lib },
.{ .name = "sdl", .module = sdl_mod },
.{ .name = "build_options", .module = options_mod },
},
}),
});
// Own SDL3 build: a shared library with @rpath; when run from zig-out it is looked
// up in its directory (the scripts/dist-macos.sh and dist-linux.sh packages point next to the binary).
// `-Dbundle-rpath='$ORIGIN/lib'` — library lookup path in the package (Linux) instead
// of the SDL build directory.
const bundle_rpath = b.option([]const u8, "bundle-rpath", "Viewer and editor rpath in the package instead of the SDL build dir");
if (bundle_rpath) |rp| {
viewer.root_module.addRPath(.{ .cwd_relative = rp });
} else if (sdl_prefix) |pre| viewer.root_module.addRPath(.{ .cwd_relative = b.fmt("{s}/lib", .{pre}) });
// Windows: a GUI program, no console window next to the game (logs go nowhere).
if (target.result.os.tag == .windows) viewer.subsystem = .windows;
// Window icon: the project's own picture, embedded in the binary (@embedFile("icon_png")).
viewer.root_module.addAnonymousImport("icon_png", .{ .root_source_file = b.path("assets/icon-256.png") });
// `scaling = adaptive`: the riveted plate beside the 640-wide panel.
viewer.root_module.addAnonymousImport("panel_plate_png", .{ .root_source_file = b.path("assets/panel-plate.png") });
// The launcher window (src/viewer/app/launcher.zig): the editor's Clay and text kit,
// PT Sans and Oswald (OFL), icons (scripts/launcher-icons.swift), the background picture
// (its reference design's, the buttons painted into it removed).
const ui = b.addTranslateC(.{
.root_source_file = b.path("src/editor/ui.h"),
.target = target,
.optimize = optimize,
});
ui.addIncludePath(b.path("third_party/clay"));
ui.addIncludePath(b.path("third_party/stb"));
const ui_mod = ui.createModule();
const kit = b.createModule(.{ .root_source_file = b.path("src/editor/kit.zig"), .target = target, .optimize = viewer_optimize });
kit.addImport("sdl", sdl_mod);
kit.addImport("ui", ui_mod);
kit.addImport("opengorky", viewer_lib);
viewer.root_module.addImport("kit", kit);
viewer.root_module.addImport("ui", ui_mod);
viewer.root_module.addCSourceFile(.{ .file = b.path("third_party/clay/clay.c"), .flags = &.{"-O2"} });
viewer.root_module.addCSourceFile(.{ .file = b.path("third_party/stb/stb_truetype.c"), .flags = &.{"-O2"} });
viewer.root_module.addAnonymousImport("font_regular", .{ .root_source_file = b.path("assets/fonts/PTSans-Regular.ttf") });
viewer.root_module.addAnonymousImport("font_bold", .{ .root_source_file = b.path("assets/fonts/PTSans-Bold.ttf") });
viewer.root_module.addAnonymousImport("font_heading", .{ .root_source_file = b.path("assets/fonts/Oswald-Bold.ttf") });
for ([_][]const u8{ "settings", "info", "back", "check", "warning", "play" }) |n| {
viewer.root_module.addAnonymousImport(b.fmt("launcher_{s}", .{n}), .{ .root_source_file = b.path(b.fmt("assets/launcher/icons/{s}.png", .{n})) });
}
viewer.root_module.addAnonymousImport("launcher_background", .{ .root_source_file = b.path("assets/launcher/textures/background.jpg") });
// Version shown by the launcher: `-Dversion` (the dist scripts pass theirs), else
// `git describe` as the packages' Info.plist.
var git_exit: u8 = 0;
const version = b.option([]const u8, "version", "Version shown in the launcher (default: git describe)") orelse
if (b.runAllowFail(&.{ "git", "-C", b.root.toString(b.allocator) catch ".", "describe", "--always", "--dirty" }, &git_exit, .ignore)) |out| std.mem.trim(u8, out, " \r\n") else |_| "dev";
const version_opts = b.addOptions();
version_opts.addOption([]const u8, "version", version);
viewer.root_module.addImport("version", version_opts.createModule());
b.installArtifact(viewer);
const run_viewer = b.addRunArtifact(viewer);
run_viewer.step.dependOn(b.getInstallStep());
run_viewer.addPassthruArgs();
b.step("viewer", "Run the resource viewer").dependOn(&run_viewer.step);
// Level editor: the engine and SDL as in the viewer, UI layout by Clay, text by
// stb_truetype with the embedded PT Sans.
const editor = b.addExecutable(.{
.name = "opengorky-editor",
.root_module = b.createModule(.{
.root_source_file = b.path("src/editor/main.zig"),
.target = target,
.optimize = viewer_optimize,
.strip = strip,
.imports = &.{
.{ .name = "opengorky", .module = viewer_lib },
.{ .name = "sdl", .module = sdl_mod },
.{ .name = "ui", .module = ui_mod },
},
}),
});
// The viewer's video player (the Videos page plays them in the centre) and level
// composition (the level view's "as in the game" picture): one module, they share
// the viewer's media files.
const viewer_part = b.createModule(.{ .root_source_file = b.path("src/viewer/level_render.zig"), .target = target, .optimize = viewer_optimize });
viewer_part.addImport("opengorky", viewer_lib);
viewer_part.addImport("sdl", sdl_mod);
viewer_part.addImport("build_options", options_mod);
if (use_ffmpeg) viewer_part.addImport("ffmpeg", ffmpeg_mod);
editor.root_module.addImport("viewer", viewer_part);
// Imports transcode in-process (src/editor/transcode.zig).
editor.root_module.addImport("build_options", options_mod);
if (use_ffmpeg) editor.root_module.addImport("ffmpeg", ffmpeg_mod);
editor.root_module.addIncludePath(b.path("third_party/clay"));
editor.root_module.addIncludePath(b.path("third_party/stb"));
editor.root_module.addCSourceFile(.{ .file = b.path("third_party/clay/clay.c"), .flags = &.{"-O2"} });
editor.root_module.addCSourceFile(.{ .file = b.path("third_party/stb/stb_truetype.c"), .flags = &.{"-O2"} });
editor.root_module.addAnonymousImport("font_regular", .{ .root_source_file = b.path("assets/fonts/PTSans-Regular.ttf") });
editor.root_module.addAnonymousImport("font_bold", .{ .root_source_file = b.path("assets/fonts/PTSans-Bold.ttf") });
editor.root_module.addAnonymousImport("font_italic", .{ .root_source_file = b.path("assets/fonts/PTSans-Italic.ttf") });
// Map icons (Lucide, ISC; scripts/editor-icons.swift renders them).
for ([_][]const u8{ "facing", "turn_ccw", "turn_cw" }) |n| {
editor.root_module.addAnonymousImport(b.fmt("icon_{s}", .{n}), .{ .root_source_file = b.path(b.fmt("assets/icons/{s}.png", .{n})) });
}
// UI icons (the same script, white, tinted when drawn): names as src/editor/icons.zig's Icon.
for ([_][]const u8{ "save", "check", "play", "settings", "history", "search", "folder", "chevron_right", "chevron_down", "map_pin", "swords", "user", "package", "clapperboard", "volume_2", "plus", "file_text", "mouse_pointer_2", "hand", "paintbrush", "pipette", "lightbulb", "square_dashed", "log_out", "crosshair", "camera", "pencil", "code", "message_square", "maximize", "x", "undo_2", "redo_2", "eye", "eye_off", "lock", "lock_open", "layers", "trash_2", "copy", "image", "box", "pause", "skip_back", "skip_forward" }) |n| {
editor.root_module.addAnonymousImport(b.fmt("ui_{s}", .{n}), .{ .root_source_file = b.path(b.fmt("assets/icons/ui_{s}.png", .{n})) });
}
// Window icon (assets/icon-editor.png at 256 px), as the viewer's.
editor.root_module.addAnonymousImport("icon_png", .{ .root_source_file = b.path("assets/icon-editor-256.png") });
// Settings folder and atomic writes, shared with the viewer (std only).
editor.root_module.addAnonymousImport("config", .{ .root_source_file = b.path("src/viewer/app/config.zig") });
if (bundle_rpath) |rp| {
editor.root_module.addRPath(.{ .cwd_relative = rp });
} else if (sdl_prefix) |pre| editor.root_module.addRPath(.{ .cwd_relative = b.fmt("{s}/lib", .{pre}) });
if (target.result.os.tag == .windows) editor.subsystem = .windows;
b.installArtifact(editor);
const run_editor = b.addRunArtifact(editor);
run_editor.step.dependOn(b.getInstallStep());
run_editor.addPassthruArgs();
b.step("editor", "Run the level editor").dependOn(&run_editor.step);
const lib_tests = b.addRunArtifact(b.addTest(.{ .root_module = lib }));
const test_step = b.step("test", "Run tests");
test_step.dependOn(&lib_tests.step);
// Viewer and editor files without SDL: settings, icon shadow, background pause, level script edits.
for ([_][]const u8{ "src/viewer/app/config.zig", "src/viewer/app/packages.zig", "src/viewer/media/shadow.zig", "src/viewer/gpu/models.zig", "src/viewer/app/pause.zig", "src/editor/placement.zig", "src/editor/model.zig", "src/editor/model_write.zig", "src/editor/settings.zig", "src/editor/box.zig", "src/editor/search.zig", "src/editor/scenes.zig", "src/editor/code.zig", "src/editor/callscan.zig", "src/editor/api.zig", "src/editor/rulesapi.zig", "src/editor/callbacks.zig", "src/editor/rulescheck.zig", "src/editor/trigger.zig", "src/editor/newlevel.zig", "src/editor/texts.zig", "src/editor/modpack.zig", "src/editor/project.zig", "src/editor/adjusttext.zig", "src/editor/adjustdoc.zig", "src/editor/refdata.zig", "src/editor/savelog.zig", "src/editor/zone.zig", "src/editor/catalog.zig", "src/editor/store.zig", "src/editor/entry_anims.zig", "src/editor/layout.zig", "src/editor/doctab.zig", "src/editor/rules_rows.zig", "src/editor/entry_tables.zig", "src/editor/fxmaterial.zig", "src/editor/postpage.zig", "src/editor/freename.zig", "src/editor/uses.zig", "src/editor/preview.zig", "src/editor/entry_attacks.zig", "src/editor/testkit.zig", "src/editor/traitinfo.zig", "src/editor/model_og.zig", "src/editor/projconvert.zig", "src/editor/completion.zig", "src/editor/scenedoc.zig" }) |p| {
const m = b.createModule(.{ .root_source_file = b.path(p), .target = target, .optimize = optimize });
// The weapon patterns (zone.zig) and the class files (catalog.zig) are the engine's.
m.addImport("opengorky", lib);
// api.zig's test checks its entries against the builtins the port registers.
m.addAnonymousImport("builtins_src", .{ .root_source_file = b.path("src/builtins.zig") });
// settings.zig keeps its paths in the viewer config's buffers.
if (std.mem.endsWith(u8, p, "/settings.zig")) m.addAnonymousImport("config", .{ .root_source_file = b.path("src/viewer/app/config.zig") });
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
}
// The editor's glyph atlas (stb_truetype and the embedded PT Sans; no renderer).
const font_test = b.createModule(.{ .root_source_file = b.path("src/editor/font.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sdl", .module = sdl_mod }, .{ .name = "ui", .module = ui_mod } } });
font_test.addCSourceFile(.{ .file = b.path("third_party/stb/stb_truetype.c"), .flags = &.{"-O2"} });
inline for (.{ "regular", "bold", "italic" }) |n| font_test.addAnonymousImport("font_" ++ n, .{ .root_source_file = b.path("assets/fonts/PTSans-" ++ [_]u8{std.ascii.toUpper(n[0])} ++ n[1..] ++ ".ttf") });
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = font_test })).step);
// The editor's import transcoding through the linked FFmpeg.
const transcode = b.createModule(.{ .root_source_file = b.path("src/editor/transcode.zig"), .target = target, .optimize = optimize, .link_libc = true });
transcode.addImport("build_options", options_mod);
if (use_ffmpeg) transcode.addImport("ffmpeg", ffmpeg_mod);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = transcode })).step);
// Unformatted code fails the test (`zig fmt src build.zig` fixes it).
test_step.dependOn(&b.addFmt(.{ .paths = &.{ b.path("src"), b.path("build.zig") }, .check = true }).step);
}
/// The language's own declarations (foundation/*.ogs: the built-in traits, the types, the
/// block kinds' fields), as the engine reads them (`@embedFile("foundation_traits")`).
fn addFoundation(b: *std.Build, m: *std.Build.Module) void {
inline for (.{ "traits", "types", "hosts" }) |n| m.addAnonymousImport("foundation_" ++ n, .{ .root_source_file = b.path("foundation/" ++ n ++ ".ogs") });
}
/// Third-party engine decoders (stb_image, dr_libs).
fn addCDeps(b: *std.Build, m: *std.Build.Module) void {
m.addIncludePath(b.path("third_party/stb"));
// stb_image_write's JPEG bit buffer shifts signed ints on purpose (well defined on
// every compiler it targets); Debug's UB check would trap on it.
m.addCSourceFile(.{ .file = b.path("third_party/stb/stb_image.c"), .flags = &.{ "-O2", "-fno-sanitize=shift" } });
m.addIncludePath(b.path("third_party/dr_libs"));
m.addCSourceFile(.{ .file = b.path("third_party/dr_libs/dr_libs.c"), .flags = &.{"-O2"} });
}
/// Engine copy with its own optimization (for gorky-tool and the viewer).
fn engineModule(b: *std.Build, target: std.Build.ResolvedTarget, opt: std.builtin.OptimizeMode) *std.Build.Module {
const m = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = opt,
.link_libc = true,
});
addCDeps(b, m);
addFoundation(b, m);
return m;
}