diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 13:35:31 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 13:45:32 -0300 |
| commit | 7a1c66f1a15911763db42d890e9e8172fe404170 (patch) | |
| tree | 2604fc13399b96b091af1350bc6b78d6e0a9f89a /build/gui.zig | |
| parent | e5c57a863004914136c4e6b0fa13ed805b3b383d (diff) | |
| download | pardes-7a1c66f1a15911763db42d890e9e8172fe404170.tar.gz pardes-7a1c66f1a15911763db42d890e9e8172fe404170.zip | |
build: the core named once (core.module), the window's pieces made once
build/core.zig's module() makes every core: the shell, the core, the core
without the terminal (core-test), pardes-isolate, the static, the bare
build's window, the syntax module's (syntax-test) and the 9P test roots;
each import and library a core takes is written there once, in the order
the fold over core_mods and wireCore gave them, so every compile keeps its
inputs. Parts holds what a core is made of, Core the few ways the products
differ. Tracy's client and BSD libutil stay where they were (Tracy: the
shell and the core; libutil: the shell), now said by a field. Gone: the
core_mods fold and its buffers, wireCore and its eleven optional fields,
the 9P roots' copies of core_module's import table and link objects.
The editor library's and pardes-syntax's import lists are written out in
full (editorModule, library.zig); TreeSitter.wire adds the grammars;
ShellConfig has defaults, so the library's two literals are one.
build/gui.zig's make() builds SDL, gui_c, sketchbook's renderer, HarfBuzz
and the renderer's and atlas's tests once, and Window.wire links them into
each window shell. One pardes-lspmock (tests and process-test shared it
twice from one root); tools.program() makes the eleven uniform runner
programs in a line each. The pdf benches (tools.pdfBenches) are made once
the core is.
Gates: zig build -l and -h byte-identical to the base over the matrix
(bare, tui, gui, macos, ReleaseFast, -Dpdf=false, -Dtree-sitter=minimal,
musl, -Dtracy), test/lib's configuration byte-identical. Warm rebuild of
install addons and core-test shell-test unit-test process-test on the
base's cache: tui every compile cached (0 rebuilt), 2339/2486 tests passed
with the base's 4 known failures, the same counts as the base; gui one
change: SDL3 had -DSDL_DISABLE_STB=1 added once per window shell (twice
with -Dplatform=gui), now once, so libSDL3.a's cache path moves and
pardes-gui and the shell-test root relink (their argv differ in that one
path, nothing else); tests 2398/2545, as the base. Installed bin/pardes,
image.so and pdf.so cmp-identical on tui; image.so and pdf.so identical on
gui, pardes-gui differs by the relinked SDL3 (size 398,737,210 ->
398,733,114). zig build syntax-test, which did not compile on main, now
compiles and passes (40/40).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'build/gui.zig')
| -rw-r--r-- | build/gui.zig | 305 |
1 files changed, 159 insertions, 146 deletions
diff --git a/build/gui.zig b/build/gui.zig index aaf64707..9b46f6c5 100644 --- a/build/gui.zig +++ b/build/gui.zig @@ -1,5 +1,4 @@ const std = @import("std"); -const Platform = @import("../build.zig").Platform; const gui_shaders = [_][]const u8{ "ui.vert", @@ -11,163 +10,177 @@ const gui_shaders = [_][]const u8{ "image.frag", }; -/// The window's shells (the bare build's pardes-gui, -Dplatform=gui's -/// pardes-gui and its static): SDL, gui.zig's `c`, sketchbook's renderer and -/// glyph atlas, FreeType and HarfBuzz; and the renderer's and atlas's own -/// tests, for unit-test. -pub fn add(b: *std.Build, c: struct { +/// The window's pieces, made once and wired into each of its shells (the +/// bare build's pardes-gui; -Dplatform=gui's pardes-gui and its static): +/// SDL, gui.zig's `c`, sketchbook's renderer and glyph atlas, FreeType and +/// HarfBuzz; and the renderer's and atlas's own tests, for unit-test. A +/// piece whose lazy package is not fetched yet is null. +pub const Window = struct { + sdl: ?Sdl = null, + sketchbook: ?*std.Build.Dependency, freetype: ?*std.Build.Step.Compile, - gui_mod: ?*std.Build.Module, - macos_sdk: ?[]const u8, - optimize: std.lang.Optimize, - platform: Platform, - prebuilt_shaders: bool, - root_mod: *std.Build.Module, - static_mod: ?*std.Build.Module, - target: std.Build.ResolvedTarget, - test_filters: []const []const u8, -}) struct { cells_test: ?*std.Build.Step.Compile, atlas_test: ?*std.Build.Step.Compile } { - const gui_shell_mod: ?*std.Build.Module = c.gui_mod orelse - if (c.platform == .gui) c.root_mod else null; - // The static step's gui module is a gui shell too. - const gui_shell_mods: [2]?*std.Build.Module = .{ gui_shell_mod, if (c.platform == .gui) c.static_mod else null }; - // SDL's headers, for the macOS trackpad adapter's test after the loop. - var sdl_include: ?std.Build.LazyPath = null; - // sketchbook's Cells.zig's own tests (the instance ABI), for unit-test. - var cells_test: ?*std.Build.Step.Compile = null; - // sketchbook's Atlas.zig's own tests, for unit-test. - var atlas_test: ?*std.Build.Step.Compile = null; - for (gui_shell_mods) |gui_shell_entry| if (gui_shell_entry) |gm| { - // This shell's `c`, for the Atlas's tests below. - var font_c: ?*std.Build.Module = null; - // SDL's build asks a cross build for macOS for the SDK's paths. - const sdk: ?std.Build.LazyPath = if (c.macos_sdk) |p| .{ .cwd_relative = p } else null; - const sdl_dep = b.lazyDependency("sdl", .{ .target = c.target, .optimize = c.optimize, .sanitize_c = .off, .preferred_linkage = .static, .system_include_path = if (sdk) |p| p.path(b, "usr/include") else null, .system_framework_path = if (sdk) |p| p.path(b, "System/Library/Frameworks") else null, .library_path = if (sdk) |p| p.path(b, "usr/lib") else null }); - if (sdl_dep) |dep| { - const sdl_lib = dep.artifact("SDL3"); - // SDL's own stb_image (SDL_stb.c, for SDL_LoadPNG and a camera's - // MJPEG, neither of which pardes calls) is a second PNG and JPEG - // decoder beside the build's: off (docs/shared-libs.md). - sdl_lib.root_module.addCMacro("SDL_DISABLE_STB", "1"); - gm.linkLibrary(sdl_lib); - gm.addIncludePath(dep.path("include")); - // gui.zig's `c`: SDL and the font shim, which @cImport gave it before Zig 0.17. - const header = b.addWriteFiles().add("gui_c.h", - \\#define SDL_DISABLE_OLD_NAMES 1 - \\#include <SDL3/SDL.h> - \\#include "font.h" - \\#include "macos_trackpad.h" - \\ - ); - const gui_c = b.addTranslateC(.{ .root_source_file = header, .target = c.target, .optimize = c.optimize }); - gui_c.addIncludePath(dep.path("include")); - gui_c.addIncludePath(b.path("src/gui")); - const gui_c_mod = gui_c.createModule(); - font_c = gui_c_mod; - gm.addImport("gui_c", gui_c_mod); - if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| { - const gpu = b.createModule(.{ .root_source_file = sb.path("src/gpu/Executor.zig"), .imports = &.{ .{ .name = "abi", .module = sb.module("abi") }, .{ .name = "c", .module = gui_c_mod } } }); - gm.addImport("sketch_gpu", gpu); - // The glyph atlas (src/font/Atlas.zig) over the font shim - // (vendor/font.{c,h}), compiled below against pardes's - // FreeType and HarfBuzz. - const atlas_mod = b.createModule(.{ .root_source_file = sb.path("src/font/Atlas.zig"), .imports = &.{.{ .name = "c", .module = gui_c_mod }} }); - gm.addImport("font_atlas", atlas_mod); - // pardes's renderer, moved to sketchbook (src/gpu/), compiled - // against pardes's SDL; its tests run with unit-test. Its - // SPIR-V is sketchbook's GLSL through glslc, or the prebuilt - // copies with -Dprebuilt-shaders. - const cells_mod = b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "font_atlas", .module = atlas_mod } } }); - for (gui_shaders) |name| cells_mod.addAnonymousImport(b.fmt("{s}.spv", .{name}), .{ - .root_source_file = if (c.prebuilt_shaders) - sb.path(b.fmt("src/gpu/shaders/prebuilt/{s}.spv", .{name})) - else - compileGlsl(b, sb.path(b.fmt("src/gpu/shaders/{s}.glsl", .{name})), name), - }); - gm.addImport("gpu_cells", cells_mod); - gm.addImport("gpu_present", b.createModule(.{ .root_source_file = sb.path("src/gpu/Present.zig"), .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "gpu_cells", .module = cells_mod } } })); - if (cells_test == null) cells_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .target = c.target, .optimize = c.optimize, .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "font_atlas", .module = atlas_mod } } }), .filters = c.test_filters }); - gui_c.addIncludePath(sb.path("vendor")); - gm.addImport("gpu_scene", sb.module("gpu_scene")); - } - sdl_include = dep.path("include"); - if (c.target.result.os.tag == .macos) { - const arch = if (c.target.result.cpu.arch == .aarch64) "arm64" else @tagName(c.target.result.cpu.arch); - const shim = b.addSystemCommand(&.{ "xcrun", "clang", "-fobjc-arc", "-O2", "-target", b.fmt("{s}-apple-macos13.0", .{arch}), "-I" }); - shim.addDirectoryArg(dep.path("include")); - shim.addArg("-c"); - shim.addFileArg(b.path("src/gui/macos_trackpad.m")); - // Header changes invalidate the independently compiled adapter. - shim.addFileInput(b.path("src/gui/macos_trackpad.h")); - shim.addArg("-o"); - gm.addObjectFile(shim.addOutputFileArg("macos_trackpad.o")); + harfbuzz: ?HarfBuzz = null, + darwin: bool, + /// sketchbook's Cells.zig's own tests (the instance ABI). + cells_test: ?*std.Build.Step.Compile = null, + /// sketchbook's Atlas.zig's own tests. + atlas_test: ?*std.Build.Step.Compile = null, + + const Sdl = struct { + lib: *std.Build.Step.Compile, + include: std.Build.LazyPath, + gui_c: *std.Build.Module, + /// sketchbook's renderer by file, over gui_c: sketch_gpu, + /// font_atlas, gpu_cells, gpu_present, gpu_scene. + renderer: ?[5]std.Build.Module.Import = null, + /// The macOS trackpad adapter, compiled apart. + trackpad: ?std.Build.LazyPath = null, + }; + const HarfBuzz = struct { lib: *std.Build.Step.Compile, include: std.Build.LazyPath }; + + /// `gm`, a window shell, given the pieces, in the order it links them. + pub fn wire(w: Window, gm: *std.Build.Module) void { + const b = gm.owner; + if (w.sdl) |sdl| { + gm.linkLibrary(sdl.lib); + gm.addIncludePath(sdl.include); + gm.addImport("gui_c", sdl.gui_c); + if (sdl.renderer) |imports| for (imports) |i| gm.addImport(i.name, i.module); + if (sdl.trackpad) |object| { + gm.addObjectFile(object); gm.linkFramework("Cocoa", .{}); } } - const ft = c.freetype orelse continue; + const ft = w.freetype orelse return; gm.linkLibrary(ft); - // HarfBuzz from source, like FreeType: its FreeType integration - // (HAVE_FREETYPE, hb-ft) links the freetype artifact above, so the two - // share one FreeType. The flags are the ones Ghostty builds it with, - // plus its allocator: gui.zig's ui_malloc and friends, over a Zig - // allocator, as FreeType's is. - if (b.lazyDependency("harfbuzz", .{})) |upstream| { - const harfbuzz = b.addLibrary(.{ - .name = "harfbuzz", - .linkage = .static, - .root_module = b.createModule(.{ - .target = c.target, - .optimize = c.optimize, - .link_libc = true, - .link_libcpp = true, - }), - }); - harfbuzz.root_module.linkLibrary(ft); - harfbuzz.root_module.addIncludePath(upstream.path("src")); - harfbuzz.root_module.addCSourceFile(.{ - .file = upstream.path("src/harfbuzz.cc"), - .flags = &.{ - "-DHAVE_STDBOOL_H", - "-DHAVE_UNISTD_H", - "-DHAVE_SYS_MMAN_H", - "-DHAVE_PTHREAD=1", - "-DHAVE_FREETYPE=1", - "-DHAVE_FT_GET_VAR_BLEND_COORDINATES=1", - "-DHAVE_FT_SET_VAR_BLEND_COORDINATES=1", - "-DHAVE_FT_DONE_MM_VAR=1", - "-DHAVE_FT_GET_TRANSFORM=1", - "-Dhb_malloc_impl=ui_malloc", - "-Dhb_calloc_impl=ui_calloc", - "-Dhb_realloc_impl=ui_realloc", - "-Dhb_free_impl=ui_free", - }, - }); - gm.linkLibrary(harfbuzz); - gm.addIncludePath(upstream.path("src")); - // The Atlas's own tests, over the same FreeType, HarfBuzz and - // shim, the embedded face theirs to draw. - if (atlas_test == null) if (font_c) |fc| if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| { - const atlas_mod = b.createModule(.{ .root_source_file = sb.path("src/font/Atlas.zig"), .target = c.target, .optimize = c.optimize, .link_libc = true, .imports = &.{.{ .name = "c", .module = fc }} }); - atlas_mod.linkLibrary(ft); - atlas_mod.linkLibrary(harfbuzz); - atlas_mod.addIncludePath(upstream.path("src")); - atlas_mod.addCSourceFile(.{ .file = sb.path("vendor/font.c") }); - atlas_mod.addAnonymousImport("AdwaitaMono-Regular.ttf", .{ .root_source_file = b.path("assets/AdwaitaMono-Regular.ttf") }); - atlas_test = b.addTest(.{ .root_module = atlas_mod, .filters = c.test_filters }); - }; + if (w.harfbuzz) |hb| { + gm.linkLibrary(hb.lib); + gm.addIncludePath(hb.include); } - if (c.target.result.os.tag.isDarwin()) + if (w.darwin) gm.addRPath(.{ .cwd_relative = "/usr/local/lib" }); - if (b.lazyDependency("sketchbook", .{ .@"gpu-only" = true })) |sb| gm.addCSourceFile(.{ .file = sb.path("vendor/font.c") }); + if (w.sketchbook) |sb| gm.addCSourceFile(.{ .file = sb.path("vendor/font.c") }); // the embedded UI font: provided as a build import since assets/ lives // outside the src/ module root (@embedFile can't escape it) gm.addAnonymousImport("AdwaitaMono-Regular.ttf", .{ .root_source_file = b.path("assets/AdwaitaMono-Regular.ttf"), }); - }; - if (c.target.result.os.tag == .macos) if (sdl_include) |include| { + } +}; + +pub fn make(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, macos_sdk: ?[]const u8, prebuilt_shaders: bool, freetype: ?*std.Build.Step.Compile, test_filters: []const []const u8) Window { + const sketchbook = b.lazyDependency("sketchbook", .{ .@"gpu-only" = true }); + var w: Window = .{ .sketchbook = sketchbook, .freetype = freetype, .darwin = target.result.os.tag.isDarwin() }; + // SDL's build asks a cross build for macOS for the SDK's paths. + const sdk: ?std.Build.LazyPath = if (macos_sdk) |p| .{ .cwd_relative = p } else null; + const sdl_dep = b.lazyDependency("sdl", .{ .target = target, .optimize = optimize, .sanitize_c = .off, .preferred_linkage = .static, .system_include_path = if (sdk) |p| p.path(b, "usr/include") else null, .system_framework_path = if (sdk) |p| p.path(b, "System/Library/Frameworks") else null, .library_path = if (sdk) |p| p.path(b, "usr/lib") else null }); + if (sdl_dep) |dep| { + const sdl_lib = dep.artifact("SDL3"); + // SDL's own stb_image (SDL_stb.c, for SDL_LoadPNG and a camera's + // MJPEG, neither of which pardes calls) is a second PNG and JPEG + // decoder beside the build's: off (docs/shared-libs.md). + sdl_lib.root_module.addCMacro("SDL_DISABLE_STB", "1"); + // gui.zig's `c`: SDL and the font shim, which @cImport gave it before Zig 0.17. + const header = b.addWriteFiles().add("gui_c.h", + \\#define SDL_DISABLE_OLD_NAMES 1 + \\#include <SDL3/SDL.h> + \\#include "font.h" + \\#include "macos_trackpad.h" + \\ + ); + const gui_c = b.addTranslateC(.{ .root_source_file = header, .target = target, .optimize = optimize }); + gui_c.addIncludePath(dep.path("include")); + gui_c.addIncludePath(b.path("src/gui")); + const gui_c_mod = gui_c.createModule(); + var sdl: Window.Sdl = .{ .lib = sdl_lib, .include = dep.path("include"), .gui_c = gui_c_mod }; + if (sketchbook) |sb| { + const gpu = b.createModule(.{ .root_source_file = sb.path("src/gpu/Executor.zig"), .imports = &.{ .{ .name = "abi", .module = sb.module("abi") }, .{ .name = "c", .module = gui_c_mod } } }); + // The glyph atlas (src/font/Atlas.zig) over the font shim + // (vendor/font.{c,h}), compiled below against pardes's + // FreeType and HarfBuzz. + const atlas_mod = b.createModule(.{ .root_source_file = sb.path("src/font/Atlas.zig"), .imports = &.{.{ .name = "c", .module = gui_c_mod }} }); + // pardes's renderer, moved to sketchbook (src/gpu/), compiled + // against pardes's SDL; its tests run with unit-test. Its + // SPIR-V is sketchbook's GLSL through glslc, or the prebuilt + // copies with -Dprebuilt-shaders. + const cells_mod = b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "font_atlas", .module = atlas_mod } } }); + for (gui_shaders) |name| cells_mod.addAnonymousImport(b.fmt("{s}.spv", .{name}), .{ + .root_source_file = if (prebuilt_shaders) + sb.path(b.fmt("src/gpu/shaders/prebuilt/{s}.spv", .{name})) + else + compileGlsl(b, sb.path(b.fmt("src/gpu/shaders/{s}.glsl", .{name})), name), + }); + const present = b.createModule(.{ .root_source_file = sb.path("src/gpu/Present.zig"), .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "gpu_cells", .module = cells_mod } } }); + sdl.renderer = .{ .{ .name = "sketch_gpu", .module = gpu }, .{ .name = "font_atlas", .module = atlas_mod }, .{ .name = "gpu_cells", .module = cells_mod }, .{ .name = "gpu_present", .module = present }, .{ .name = "gpu_scene", .module = sb.module("gpu_scene") } }; + w.cells_test = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = sb.path("src/gpu/Cells.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "c", .module = gui_c_mod }, .{ .name = "font_atlas", .module = atlas_mod } } }), .filters = test_filters }); + gui_c.addIncludePath(sb.path("vendor")); + } + if (target.result.os.tag == .macos) { + const arch = if (target.result.cpu.arch == .aarch64) "arm64" else @tagName(target.result.cpu.arch); + const shim = b.addSystemCommand(&.{ "xcrun", "clang", "-fobjc-arc", "-O2", "-target", b.fmt("{s}-apple-macos13.0", .{arch}), "-I" }); + shim.addDirectoryArg(dep.path("include")); + shim.addArg("-c"); + shim.addFileArg(b.path("src/gui/macos_trackpad.m")); + // Header changes invalidate the independently compiled adapter. + shim.addFileInput(b.path("src/gui/macos_trackpad.h")); + shim.addArg("-o"); + sdl.trackpad = shim.addOutputFileArg("macos_trackpad.o"); + } + w.sdl = sdl; + } + const ft = freetype orelse return w; + // HarfBuzz from source, like FreeType: its FreeType integration + // (HAVE_FREETYPE, hb-ft) links the freetype artifact above, so the two + // share one FreeType. The flags are the ones Ghostty builds it with, + // plus its allocator: gui.zig's ui_malloc and friends, over a Zig + // allocator, as FreeType's is. + if (b.lazyDependency("harfbuzz", .{})) |upstream| { + const harfbuzz = b.addLibrary(.{ + .name = "harfbuzz", + .linkage = .static, + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + .link_libcpp = true, + }), + }); + harfbuzz.root_module.linkLibrary(ft); + harfbuzz.root_module.addIncludePath(upstream.path("src")); + harfbuzz.root_module.addCSourceFile(.{ + .file = upstream.path("src/harfbuzz.cc"), + .flags = &.{ + "-DHAVE_STDBOOL_H", + "-DHAVE_UNISTD_H", + "-DHAVE_SYS_MMAN_H", + "-DHAVE_PTHREAD=1", + "-DHAVE_FREETYPE=1", + "-DHAVE_FT_GET_VAR_BLEND_COORDINATES=1", + "-DHAVE_FT_SET_VAR_BLEND_COORDINATES=1", + "-DHAVE_FT_DONE_MM_VAR=1", + "-DHAVE_FT_GET_TRANSFORM=1", + "-Dhb_malloc_impl=ui_malloc", + "-Dhb_calloc_impl=ui_calloc", + "-Dhb_realloc_impl=ui_realloc", + "-Dhb_free_impl=ui_free", + }, + }); + w.harfbuzz = .{ .lib = harfbuzz, .include = upstream.path("src") }; + // The Atlas's own tests, over the same FreeType, HarfBuzz and + // shim, the embedded face theirs to draw. + if (w.sdl) |sdl| if (sketchbook) |sb| { + const atlas_mod = b.createModule(.{ .root_source_file = sb.path("src/font/Atlas.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "c", .module = sdl.gui_c }} }); + atlas_mod.linkLibrary(ft); + atlas_mod.linkLibrary(harfbuzz); + atlas_mod.addIncludePath(upstream.path("src")); + atlas_mod.addCSourceFile(.{ .file = sb.path("vendor/font.c") }); + atlas_mod.addAnonymousImport("AdwaitaMono-Regular.ttf", .{ .root_source_file = b.path("assets/AdwaitaMono-Regular.ttf") }); + w.atlas_test = b.addTest(.{ .root_module = atlas_mod, .filters = test_filters }); + }; + } + if (target.result.os.tag == .macos) if (w.sdl) |sdl| { const adapter_test = b.addSystemCommand(&.{ "xcrun", "clang", "-fobjc-arc", "-O1", "-framework", "Cocoa", "-I" }); - adapter_test.addDirectoryArg(include); + adapter_test.addDirectoryArg(sdl.include); adapter_test.addFileArg(b.path("test/macos_trackpad.m")); adapter_test.addFileInput(b.path("src/gui/macos_trackpad.m")); adapter_test.addFileInput(b.path("src/gui/macos_trackpad.h")); @@ -178,7 +191,7 @@ pub fn add(b: *std.Build, c: struct { run_adapter.has_side_effects = true; b.step("macos-trackpad-test", "test native SDL trackpad decoding and haptics without hardware").dependOn(&run_adapter.step); }; - return .{ .cells_test = cells_test, .atlas_test = atlas_test }; + return w; } /// glslc <glsl> -fshader-stage=<vertex|fragment> -o <name>.spv, returned as |
