-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
127 lines (116 loc) · 5.41 KB
/
Copy pathbuild.zig
File metadata and controls
127 lines (116 loc) · 5.41 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
const std = @import("std");
// The package manifest is the single source of truth for the version. Read it
// here and inject it into the module (below) as `build_options.version`, so
// `zigui.version` always matches `build.zig.zon` and the `zig fetch` tag.
const manifest = @import("build.zig.zon");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// The core library module: pure Zig, no C dependencies. This is what the
// test suite exercises, so the entire foundation runs headless and inside
// Docker on Linux without a GPU or window server.
const mod = b.addModule("zigui", .{
.root_source_file = b.path("src/zigui.zig"),
.target = target,
.optimize = optimize,
});
// Inject the manifest version so `zigui.version` derives from build.zig.zon.
const options = b.addOptions();
options.addOption([]const u8, "version", manifest.version);
mod.addImport("build_options", options.createModule());
// Bundle the default font (Inter, OFL) as an embeddable blob, importable as
// `@embedFile("inter_font")` from anywhere in the module.
mod.addAnonymousImport("inter_font", .{
.root_source_file = b.path("assets/fonts/Inter.ttf"),
});
// Bundle the icon font (a subset of Lucide, ISC) the same way, importable as
// `@embedFile("icon_font")`. See `src/icons.zig` and `assets/fonts/NOTICE.md`.
mod.addAnonymousImport("icon_font", .{
.root_source_file = b.path("assets/fonts/icons.ttf"),
});
// Bundle the monochrome emoji fallback font (Noto Emoji, OFL), importable as
// `@embedFile("emoji_font")`. Wired as a fallback face for glyphs Inter
// lacks; see `assets/fonts/NOTICE.md`.
mod.addAnonymousImport("emoji_font", .{
.root_source_file = b.path("assets/fonts/NotoEmoji.ttf"),
});
// ---- tests ------------------------------------------------------------
const lib_tests = b.addTest(.{ .root_module = mod });
const run_lib_tests = b.addRunArtifact(lib_tests);
const test_step = b.step("test", "Run the zigui test suite");
test_step.dependOn(&run_lib_tests.step);
// ---- docs (optional) --------------------------------------------------
const docs_step = b.step("docs", "Generate library documentation");
const docs = b.addObject(.{ .name = "zigui", .root_module = mod });
const install_docs = b.addInstallDirectory(.{
.source_dir = docs.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "docs",
});
docs_step.dependOn(&install_docs.step);
// ---- examples (link SDL3; not part of `zig build test`) ---------------
// `showcase` is the kitchen-sink gallery (every component + light/dark and
// accent switchers); `edit` is the multi-line text editor. Both support a
// headless `--screenshot <out.bmp>` flag for windowless rendering.
addExample(b, mod, target, optimize, "showcase", "examples/showcase/main.zig");
addExample(b, mod, target, optimize, "edit", "examples/edit/main.zig");
}
/// Build a runnable example that links the SDL3-backed runtime (`src/app.zig`).
fn addExample(
b: *std.Build,
zigui_mod: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
name: []const u8,
root_path: []const u8,
) void {
// The SDL-linking runtime module.
const app_mod = b.createModule(.{
.root_source_file = b.path("src/app.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
.imports = &.{.{ .name = "zigui", .module = zigui_mod }},
});
linkSdl3(app_mod);
const exe = b.addExecutable(.{
.name = name,
.root_module = b.createModule(.{
.root_source_file = b.path(root_path),
.target = target,
.optimize = optimize,
// libc so examples can `@cImport` POSIX headers directly (e.g. the
// llm-chat socket client); the SDL runtime needs it regardless.
.link_libc = true,
.imports = &.{
.{ .name = "zigui", .module = zigui_mod },
.{ .name = "zigui_app", .module = app_mod },
},
}),
});
const install = b.addInstallArtifact(exe, .{});
// `zig build <name>` builds (and installs) the example.
const build_step = b.step(name, b.fmt("Build the '{s}' example", .{name}));
build_step.dependOn(&install.step);
// `zig build run-<name>` builds then launches it (opens a window).
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(&install.step);
const run_step = b.step(b.fmt("run-{s}", .{name}), b.fmt("Run the '{s}' example", .{name}));
run_step.dependOn(&run_cmd.step);
}
/// Wire up SDL3 headers/libs. On a macOS build host, point at the Homebrew
/// prefix matching the host arch (Apple Silicon → /opt/homebrew, Intel →
/// /usr/local). On Linux the system linker / pkg-config finds SDL3.
fn linkSdl3(m: *std.Build.Module) void {
const host = @import("builtin");
if (host.os.tag == .macos) {
const prefix = if (host.cpu.arch == .aarch64)
"/opt/homebrew/opt/sdl3"
else
"/usr/local/opt/sdl3";
m.addIncludePath(.{ .cwd_relative = prefix ++ "/include" });
m.addLibraryPath(.{ .cwd_relative = prefix ++ "/lib" });
m.addRPath(.{ .cwd_relative = prefix ++ "/lib" });
}
m.linkSystemLibrary("SDL3", .{});
}