diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 13:26:02 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 13:26:17 -0300 |
| commit | e5c57a863004914136c4e6b0fa13ed805b3b383d (patch) | |
| tree | 4c3895acfcf310a82432546f9ed02c666a7daefa /build/macos.zig | |
| parent | 9aa2ac1a21bd96260538f2657ff37b63562380ff (diff) | |
| download | pardes-e5c57a863004914136c4e6b0fa13ed805b3b383d.tar.gz pardes-e5c57a863004914136c4e6b0fa13ed805b3b383d.zip | |
build: build() cut into build/*.zig, as pure moves
build.zig 2,406 -> 658 lines; the pieces moved verbatim into a file per
thing, each a function called where its code stood, so every step, module
and option is made in the same order as before: build/docs.zig (the typst
renders, builtins-json, command-line.txt), build/addons.zig (the GPU
addons, the pdf addon and its benches, the image addon), build/patches.zig
(vaxis, zstbi, ghostty's kitty loader, mvzr), build/gui.zig (the window's
shells and its renderer and atlas tests), build/macos.zig (the app, dmg,
e2e, its unit-test), build/tools.zig (harnesses, hx*, benches, perf, the
syntax snapshots, lib-test, history), build/tests.zig (unit-test and its
roots), build/library.zig (what a dependent package gets), build/core.zig
(the options module, themes, grammars, uucode, the editor module,
wireCore); mupdf.zig is build/mupdf.zig. A fragment takes what it uses as
explicit parameters, or as one struct of exactly those (cloud9's and
build/process.zig's pattern). Platform, PerfBuild and TreeSitterGrammars
stay in build.zig: Options names an enum or struct by @typeName, so where
they are declared is in every options file.
Gate: zig build --print-configuration, -l and -h byte-identical to the
base over bare (-Dhome-install=false), tui, gui, macos, tui and gui
ReleaseFast, -Dpdf=false (tui, gui), -Dtree-sitter=minimal, musl,
-Dtracy, and test/lib's -l and --print-configuration.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'build/macos.zig')
| -rw-r--r-- | build/macos.zig | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/build/macos.zig b/build/macos.zig new file mode 100644 index 00000000..bcaf4d4c --- /dev/null +++ b/build/macos.zig @@ -0,0 +1,200 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const macos_min_version = @import("../build.zig").macos_min_version; +const display_version = @import("core.zig").display_version; +const testMatch = @import("tests.zig").testMatch; + +/// `-Dplatform=macos`: libpardes.a under a Swift host, pardes.app, the dmg, +/// the AppKit snapshot suite and the C-ABI unit tests. +pub fn add(b: *std.Build, c: struct { + macos_identity: []const u8, + optimize: std.lang.Optimize, + root_mod: *std.Build.Module, + target: std.Build.ResolvedTarget, + test_build: *std.Build.Step, + test_filters: []const []const u8, + test_nonce: ?*std.Build.Module, + unit_profile: *std.Build.Step.Compile, +}) void { + const header = b.addTranslateC(.{ + .root_source_file = b.path("src/macos/pardes.h"), + .target = c.target, + .optimize = c.optimize, + }); + c.root_mod.addImport("pardes.h", header.createModule()); + + const lib = b.addLibrary(.{ + .name = "pardes", + .linkage = .static, + .root_module = c.root_mod, + }); + lib.bundle_compiler_rt = true; + lib.bundle_ubsan_rt = true; + const archive: ?std.Build.LazyPath = + if (builtin.os.tag.isDarwin() and c.target.result.os.tag.isDarwin()) fatArchive(b, lib) else null; + const install_lib: *std.Build.Step = if (archive) |a| + &b.addInstallLibFile(a, "libpardes.a").step + else + &b.addInstallArtifact(lib, .{}).step; + b.getInstallStep().dependOn(install_lib); + b.installFile("src/macos/pardes.h", "include/pardes.h"); + + const app_step = b.step("macos-app", "assemble zig-out/pardes.app (needs macOS + swiftc)"); + const dmg_step = b.step("macos-dmg", "package zig-out/pardes.dmg for distribution (needs macOS + swiftc)"); + if (archive) |lib_archive| { + const apple_arch: []const u8 = + if (c.target.result.cpu.arch == .aarch64) "arm64" else @tagName(c.target.result.cpu.arch); + const triple = b.fmt("{s}-apple-macos{d}.{d}", .{ + apple_arch, macos_min_version.major, macos_min_version.minor, + }); + const min_version = b.fmt("{d}.{d}", .{ macos_min_version.major, macos_min_version.minor }); + + const swift_mode: []const u8 = switch (c.optimize) { + .debug => "-Onone", + .small => "-Osize", + .fast, .safe => "-O", + }; + + const link = b.addSystemCommand(&.{ "swiftc", swift_mode, "-target", triple, "-import-objc-header" }); + link.addFileArg(b.path("src/macos/pardes.h")); + link.addArg("-o"); + const app_bin = link.addOutputFileArg("pardes"); + for ([_][]const u8{ + "main.swift", + "AppDelegate.swift", + "PardesView.swift", + "ScenePostprocessor.swift", + "FileWatcher.swift", + }) |src| + link.addFileArg(b.path(b.fmt("src/macos/Sources/{s}", .{src}))); + link.addFileArg(lib_archive); + link.addArgs(&.{ + "-lc++", + "-framework", + "AppKit", + "-framework", + "CoreText", + "-framework", + "CoreGraphics", + "-framework", + "CoreImage", + "-framework", + "Metal", + }); + + const icon_build = b.addSystemCommand(&.{ "swiftc", "-O", "-target", triple, "-o" }); + const icon_bin = icon_build.addOutputFileArg("pardes-icon"); + icon_build.addFileArg(b.path("src/macos/icon.swift")); + const icon_run = std.Build.Step.Run.create(b, "pardes-icon"); + icon_run.addFileArg(icon_bin); + icon_run.addFileArg(b.path("src/macos/icon.png")); + const icon_dir = icon_run.addOutputDirectoryArg("Resources"); + + const plist = b.addSystemCommand(&.{ + "plutil", "-replace", "LSMinimumSystemVersion", "-string", min_version, "-o", + }); + const plist_out = plist.addOutputFileArg("Info.plist"); + plist.addFileArg(b.path("src/macos/Info.plist")); + + const version_plist = b.addSystemCommand(&.{ + "plutil", "-replace", "CFBundleShortVersionString", "-string", display_version, "-o", + }); + const version_plist_out = version_plist.addOutputFileArg("Info.plist"); + version_plist.addFileArg(plist_out); + + const install_app_bin = b.addInstallFileWithDir(app_bin, .{ .custom = "pardes.app/Contents/MacOS" }, "pardes"); + const install_plist = b.addInstallFileWithDir(version_plist_out, .{ .custom = "pardes.app/Contents" }, "Info.plist"); + const install_scene_kernel = b.addInstallFileWithDir( + b.path("shaders/crt.ci.metal"), + .{ .custom = "pardes.app/Contents/Resources" }, + "crt.ci.metal", + ); + // CFBundleIconFile names this without its extension; without both + // halves the Dock shows the generic blank page. + const install_icon = b.addInstallFileWithDir(icon_dir.path(b, "pardes.icns"), .{ .custom = "pardes.app/Contents/Resources" }, "pardes.icns"); + + const sign = b.addSystemCommand(&.{ "codesign", "--force", "--sign", c.macos_identity }); + if (!std.mem.eql(u8, c.macos_identity, "-")) sign.addArgs(&.{ "--options", "runtime", "--timestamp" }); + sign.setCwd(installed("")); + sign.addArg("pardes.app"); + sign.has_side_effects = true; + sign.step.dependOn(&install_app_bin.step); + sign.step.dependOn(&install_plist.step); + sign.step.dependOn(&install_icon.step); + sign.step.dependOn(&install_scene_kernel.step); + app_step.dependOn(&sign.step); + + const dmg = b.addSystemCommand(&.{ "hdiutil", "create", "-volname", "pardes", "-ov", "-format", "UDZO", "-srcfolder" }); + dmg.setCwd(installed("")); + dmg.addArgs(&.{ "pardes.app", "pardes.dmg" }); + dmg.has_side_effects = true; + dmg.step.dependOn(&sign.step); + dmg_step.dependOn(&dmg.step); + + b.getInstallStep().dependOn(&sign.step); + } else { + const why = b.addFail("pardes.app needs a Darwin host and target; drop -Dtarget= or pass -Dtarget=native"); + app_step.dependOn(&why.step); + dmg_step.dependOn(&why.step); + } + + const e2e_step = b.step("macos-e2e", "run the offscreen AppKit snapshot suite (needs macOS + swiftc)"); + if (archive) |lib_archive| { + const arch = if (c.target.result.cpu.arch == .aarch64) "arm64" else @tagName(c.target.result.cpu.arch); + const triple = b.fmt("{s}-apple-macos{d}.{d}", .{ arch, macos_min_version.major, macos_min_version.minor }); + const e2e = b.addSystemCommand(&.{ "swiftc", "-O", "-target", triple, "-import-objc-header" }); + e2e.addFileArg(b.path("src/macos/pardes.h")); + e2e.addArg("-o"); + const e2e_bin = e2e.addOutputFileArg("pardes-macos-e2e"); + for ([_][]const u8{ + "src/macos/Sources/PardesView.swift", + "src/macos/Sources/ScenePostprocessor.swift", + "src/macos/Sources/FileWatcher.swift", + "src/macos/Sources/AppDelegate.swift", + "test/macos_e2e.swift", + }) |source| e2e.addFileArg(b.path(source)); + e2e.addFileArg(lib_archive); + e2e.addArgs(&.{ "-lc++", "-framework", "AppKit", "-framework", "CoreText", "-framework", "CoreGraphics", "-framework", "CoreImage", "-framework", "Metal" }); + const files = b.addWriteFiles(); + const runnable = files.addCopyFile(e2e_bin, "pardes-macos-e2e"); + _ = files.addCopyFile(b.path("shaders/crt.ci.metal"), "crt.ci.metal"); + const run_e2e = std.Build.Step.Run.create(b, "run macos-e2e"); + run_e2e.addFileArg(runnable); + run_e2e.setCwd(b.path(".")); + run_e2e.addPassthruArgs(); + run_e2e.has_side_effects = true; + e2e_step.dependOn(&run_e2e.step); + } else e2e_step.dependOn(&b.addFail("macos-e2e needs a Darwin host and target").step); + + const unit_step = b.step("unit-test", "run the C-ABI guard and core unit tests"); + const unit_tests = b.addTest(.{ .root_module = c.root_mod, .filters = c.test_filters }); + if (c.test_nonce) |nonce| unit_tests.root_module.addImport("test_rebuild_nonce", nonce); + c.test_build.dependOn(&unit_tests.step); + const run_unit = b.addRunArtifact(unit_tests); + run_unit.has_side_effects = true; + unit_step.dependOn(&run_unit.step); + if (testMatch(b, c.unit_profile, c.test_filters, unit_step)) |check| check.addArtifactArg(unit_tests); +} + +/// `sub` under the install prefix, known only when the build runs. +fn installed(sub: []const u8) std.Build.LazyPath { + return .{ .relative = .{ .base = .install_prefix, .sub_path = sub } }; +} + +fn fatArchive(b: *std.Build, lib: *std.Build.Step.Compile) std.Build.LazyPath { + const run = std.Build.Step.Run.create(b, "libtool libpardes.a"); + run.addArgs(&.{ "libtool", "-static", "-o" }); + const output = run.addOutputFileArg("libpardes.a"); + for (lib.getCompileDependencies(false), 0..) |dep, i| { + if (dep.kind != .lib) continue; + run.addFileArg(reindexed(b, dep.getEmittedBin(), i)); + } + return output; +} + +fn reindexed(b: *std.Build, archive: std.Build.LazyPath, index: usize) std.Build.LazyPath { + const run = std.Build.Step.Run.create(b, b.fmt("ranlib #{d}", .{index})); + run.addArgs(&.{ "/bin/sh", "-c", "/bin/cp \"$1\" \"$2\" && /usr/bin/ranlib \"$2\"", "_" }); + run.addFileArg(archive); + return run.addOutputFileArg(b.fmt("{d}.a", .{index})); +} |
