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
|
const std = @import("std");
/// 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 devices (`dev`: the renderer and the
/// glyph atlas), FreeType and HarfBuzz. A piece whose lazy package is not
/// fetched yet is null.
pub const Window = struct {
sdl: ?Sdl = null,
/// sketchbook as the window takes it: the devices over gui.zig's `c`
/// (the GPU addons take its scene format from the same).
sketchbook: ?*std.Build.Dependency = null,
freetype: ?*std.Build.Step.Compile,
harfbuzz: ?HarfBuzz = null,
darwin: bool,
const Sdl = struct {
lib: *std.Build.Step.Compile,
include: std.Build.LazyPath,
/// 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);
if (w.sketchbook) |sb| {
gm.addImport("gui_c", sb.module("c"));
gm.addImport("sketchbook", sb.module("dev"));
}
if (sdl.trackpad) |object| {
gm.addObjectFile(object);
gm.linkFramework("Cocoa", .{});
}
}
const ft = w.freetype orelse return;
gm.linkLibrary(ft);
if (w.harfbuzz) |hb| {
gm.linkLibrary(hb.lib);
gm.addIncludePath(hb.include);
}
if (w.darwin)
gm.addRPath(.{ .cwd_relative = "/usr/local/lib" });
if (w.sketchbook) |sb| gm.addCSourceFile(.{ .file = sb.namedLazyPath("font.c") });
// the embedded UI font: provided as a build import since assets/ lives
// outside the dev/ module root (@embedFile can't escape it)
gm.addAnonymousImport("AdwaitaMono-Regular.ttf", .{
.root_source_file = b.path("assets/AdwaitaMono-Regular.ttf"),
});
}
};
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) Window {
var w: Window = .{ .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, translated by sketchbook with the devices it
// compiles against it (its `c` and `dev`); their SPIR-V is
// sketchbook's GLSL through glslc, or its prebuilt copies with
// -Dprebuilt-shaders.
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 include: []const std.Build.LazyPath = &.{ dep.path("include"), b.path("dev/gui") };
w.sketchbook = b.lazyDependency("sketchbook", .{ .target = target, .optimize = optimize, .@"c-header" = header, .@"c-include" = include, .@"prebuilt-shaders" = prebuilt_shaders });
var sdl: Window.Sdl = .{ .lib = sdl_lib, .include = dep.path("include") };
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("dev/gui/macos_trackpad.m"));
// Header changes invalidate the independently compiled adapter.
shim.addFileInput(b.path("dev/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") };
}
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(sdl.include);
adapter_test.addFileArg(b.path("test/macos_trackpad.m"));
adapter_test.addFileInput(b.path("dev/gui/macos_trackpad.m"));
adapter_test.addFileInput(b.path("dev/gui/macos_trackpad.h"));
adapter_test.addArg("-o");
const adapter_bin = adapter_test.addOutputFileArg("macos-trackpad-test");
const run_adapter = std.Build.Step.Run.create(b, "run macos-trackpad-test");
run_adapter.addFileArg(adapter_bin);
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 w;
}
|