-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbuild.zig
More file actions
381 lines (345 loc) · 20.3 KB
/
build.zig
File metadata and controls
381 lines (345 loc) · 20.3 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
const std = @import("std");
const helpers = @import("build/helpers.zig");
const examples = @import("build/examples.zig");
const tools = @import("build/tools.zig");
const packages = @import("build/packages.zig");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ── dhi dependency ──────────────────────────────────────────────────────
const dhi_dep = b.dependency("dhi", .{});
const dhi_model_mod = dhi_dep.module("model");
const dhi_validator_mod = dhi_dep.module("validator");
// ── kuri dependency (browser automation for debug mode) ─────────────────
// TODO: re-enable once kuri is updated for Zig 0.16
// const kuri_dep = b.dependency("kuri", .{
// .target = target,
// .optimize = if (optimize != .Debug) optimize else .ReleaseFast,
// });
// ── Runtime module (std.Io instance management) ───────────────────────────
const runtime_mod = b.addModule("runtime", .{
.root_source_file = b.path("src/runtime.zig"),
});
// ── "mer" module (framework public API) ──────────────────────────────────
const mer_mod = b.addModule("mer", .{
.root_source_file = b.path("src/mer.zig"),
.link_libc = true,
});
mer_mod.addImport("dhi_model", dhi_model_mod);
mer_mod.addImport("dhi_validator", dhi_validator_mod);
mer_mod.addImport("runtime", runtime_mod);
// ── turboapi-core (shared router + HTTP utilities) ──
const core_dep = b.dependency("turboapi_core", .{});
const core_mod = core_dep.module("turboapi-core");
mer_mod.addImport("turboapi-core", core_mod);
// Self-referential import: internal files (server.zig, router.zig, …)
// file-imported from mer.zig still resolve their `@import("mer")` calls.
mer_mod.addImport("mer", mer_mod);
// ── Expose framework internals as named modules for consumer projects ────
// Consumers do: `merjs_dep.module("server")` in their build.zig.
// Each module has "mer" wired so transitive file-imports just work.
const server_mod = b.addModule("server", .{ .root_source_file = b.path("src/server.zig") });
server_mod.addImport("mer", mer_mod);
const watcher_named = b.addModule("watcher", .{ .root_source_file = b.path("src/watcher.zig") });
_ = watcher_named;
const prerender_mod = b.addModule("prerender", .{ .root_source_file = b.path("src/prerender.zig") });
prerender_mod.addImport("mer", mer_mod);
// ── Demo site (examples/site) ───────────────────────────────────────────
const counter_config_mod = b.addModule("counter_config", .{
.root_source_file = b.path("examples/site/wasm/counter_config.zig"),
});
const site_extras: []const struct { []const u8, *std.Build.Module } = &.{.{ "counter_config", counter_config_mod }};
const main_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.strip = if (optimize != .Debug) true else null,
.link_libc = true, // 0.16: std.c.* (pthread, clock_gettime, etc.) needs explicit libc
});
main_mod.addImport("mer", mer_mod);
main_mod.addImport("runtime", runtime_mod);
main_mod.addImport("counter_config", counter_config_mod);
helpers.addDirModules(b, main_mod, mer_mod, "examples/site/app", "app", site_extras);
helpers.addDirModules(b, main_mod, mer_mod, "examples/site/api", "api", &.{});
helpers.addRoutesModule(b, main_mod, mer_mod, "src/generated/routes.zig", "examples/site/app", "examples/site/api", site_extras);
const exe = b.addExecutable(.{ .name = "merjs", .root_module = main_mod });
b.installArtifact(exe);
// Install kuri binary alongside merjs.
// TODO: re-enable once kuri is updated for Zig 0.16
// const install_kuri = b.addInstallArtifact(kuri_dep.artifact("kuri"), .{});
// b.getInstallStep().dependOn(&install_kuri.step);
// ── Codegen ──────────────────────────────────────────────────────────────
const codegen_mod = b.createModule(.{
.root_source_file = b.path("tools/codegen.zig"),
.target = b.graph.host,
.optimize = .Debug,
});
// Wire up runtime for tools/codegen.zig
codegen_mod.addImport("runtime", runtime_mod);
const codegen_exe = b.addExecutable(.{ .name = "codegen", .root_module = codegen_mod });
const run_codegen = b.addRunArtifact(codegen_exe);
run_codegen.setCwd(b.path("."));
b.step("codegen", "Regenerate src/generated/routes.zig").dependOn(&run_codegen.step);
// ── Auto-run codegen before compiling (fresh clones just work) ───────────
exe.step.dependOn(&run_codegen.step);
// ── `zig build serve` ────────────────────────────────────────────────────
const run_exe = b.addRunArtifact(exe);
run_exe.step.dependOn(b.getInstallStep());
if (b.args) |args| run_exe.addArgs(args);
b.step("serve", "Start the merjs dev server").dependOn(&run_exe.step);
// ── Prerender (SSG) ─────────────────────────────────────────────────────
const run_prerender = b.addRunArtifact(exe);
run_prerender.addArg("--prerender");
run_prerender.step.dependOn(b.getInstallStep());
b.step("prerender", "Pre-render pages to dist/").dependOn(&run_prerender.step);
// ── `zig build prod` ────────────────────────────────────────────────────
const prod_step = b.step("prod", "Full production build: codegen + compile + prerender to dist/");
prod_step.dependOn(&run_codegen.step);
prod_step.dependOn(b.getInstallStep());
prod_step.dependOn(&run_prerender.step);
// ── WASM targets ────────────────────────────────────────────────────────
const wasm_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const counter_wasm = helpers.addWasmExe(b, "counter", "examples/site/wasm/counter.zig", wasm_target);
const install_counter = b.addInstallFile(counter_wasm.getEmittedBin(), "../examples/site/public/counter.wasm");
const wasm_step = b.step("wasm", "Compile WASM modules → public/*.wasm");
wasm_step.dependOn(&install_counter.step);
const synth_wasm = helpers.addWasmExe(b, "synth", "examples/site/wasm/synth.zig", wasm_target);
const install_synth = b.addInstallFile(synth_wasm.getEmittedBin(), "../examples/site/public/synth.wasm");
wasm_step.dependOn(&install_synth.step);
const grep_wasm = helpers.addWasmExe(b, "grep", "examples/site/wasm/grep.zig", wasm_target);
const install_grep = b.addInstallFile(grep_wasm.getEmittedBin(), "../examples/site/worker/grep.wasm");
b.step("grep", "Compile grep WASM").dependOn(&install_grep.step);
// ── Worker WASM ─────────────────────────────────────────────────────────
const worker_named = b.addModule("worker", .{
.root_source_file = b.path("src/worker.zig"),
.target = wasm_target,
.optimize = .ReleaseSmall,
});
worker_named.addImport("mer", mer_mod);
const worker_mod = b.createModule(.{
.root_source_file = b.path("src/worker.zig"),
.target = wasm_target,
.optimize = .ReleaseSmall,
});
worker_mod.addImport("mer", mer_mod);
worker_mod.addImport("counter_config", counter_config_mod);
helpers.addDirModules(b, worker_mod, mer_mod, "examples/site/app", "app", site_extras);
helpers.addDirModules(b, worker_mod, mer_mod, "examples/site/api", "api", &.{});
helpers.addRoutesModule(b, worker_mod, mer_mod, "src/generated/routes.zig", "examples/site/app", "examples/site/api", site_extras);
const worker_wasm = b.addExecutable(.{ .name = "merjs", .root_module = worker_mod });
worker_wasm.rdynamic = true;
worker_wasm.entry = .disabled;
// Auto-run codegen before worker compilation too.
worker_wasm.step.dependOn(&run_codegen.step);
const install_worker = b.addInstallFile(worker_wasm.getEmittedBin(), "../examples/site/worker/merjs.wasm");
const worker_step = b.step("worker", "Compile worker WASM for Cloudflare Workers");
worker_step.dependOn(&install_worker.step);
worker_step.dependOn(&install_grep.step);
// ── Examples (sgdata, kanban) ────────────────────────────────────────────
examples.addExamples(b, mer_mod, wasm_target);
// ── Tools (CSS, setup) ──────────────────────────────────────────────────
tools.addTools(b);
// ── CLI ─────────────────────────────────────────────────────────────────
const cli_mod = b.createModule(.{
.root_source_file = b.path("cli.zig"),
.target = target,
.optimize = optimize,
.strip = if (optimize != .Debug) true else null,
.link_libc = true,
});
cli_mod.addImport("runtime", runtime_mod);
const cli_exe = b.addExecutable(.{ .name = "mer", .root_module = cli_mod });
b.step("cli", "Build the `mer` CLI binary").dependOn(&b.addInstallArtifact(cli_exe, .{}).step);
// ── Tests ───────────────────────────────────────────────────────────────
const test_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
test_mod.addImport("mer", mer_mod);
helpers.addDirModules(b, test_mod, mer_mod, "examples/site/app", "app", site_extras);
helpers.addDirModules(b, test_mod, mer_mod, "examples/site/api", "api", &.{});
helpers.addRoutesModule(b, test_mod, mer_mod, "src/generated/routes.zig", "examples/site/app", "examples/site/api", site_extras);
const run_tests = b.addRunArtifact(b.addTest(.{ .root_module = test_mod }));
// Auto-run codegen before tests too.
run_tests.step.dependOn(&run_codegen.step);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_tests.step);
// Run inline tests in individual framework source files.
for ([_][]const u8{ "src/css.zig", "src/session.zig", "src/telemetry.zig" }) |src_path| {
const file_test_mod = b.createModule(.{
.root_source_file = b.path(src_path),
.target = target,
.optimize = optimize,
.link_libc = true,
});
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = file_test_mod })).step);
}
{
const cli_test_mod = b.createModule(.{
.root_source_file = b.path("cli.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cli_test_mod })).step);
}
// Run router + runtime inline tests (through mer.zig as root to avoid
// file-ownership conflict: mer.zig file-imports router.zig/server.zig/etc.,
// so those files belong to the mer module and can't also be test roots).
{
const mer_test_mod = b.createModule(.{
.root_source_file = b.path("src/mer.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
mer_test_mod.addImport("dhi_model", dhi_model_mod);
mer_test_mod.addImport("dhi_validator", dhi_validator_mod);
mer_test_mod.addImport("turboapi-core", core_mod);
mer_test_mod.addImport("mer", mer_test_mod);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mer_test_mod })).step);
}
// ── Consumer integration test (issue #62, #69) ────────────────────────
// Simulates a consumer project with its own routes — proves that
// `mer.Router.fromGenerated` works and framework example routes don't leak in.
// With the self-referential mer import, no manual wiring of ssr.zig/router.zig
// transitive deps is needed — consumers just use `@import("mer")`.
{
const consumer_test_mod = b.createModule(.{
.root_source_file = b.path("tests/consumer/src/test_consumer_routes.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
consumer_test_mod.addImport("mer", mer_mod);
// The key: wire "routes" to the CONSUMER's routes, not the framework's.
const consumer_routes_mod = b.createModule(.{
.root_source_file = b.path("tests/consumer/src/routes.zig"),
});
consumer_routes_mod.addImport("mer", mer_mod);
// Add consumer page modules to both routes and test modules.
const consumer_pages = [_]struct { []const u8, []const u8 }{
.{ "app/index", "tests/consumer/app/index.zig" },
.{ "app/dashboard", "tests/consumer/app/dashboard.zig" },
};
for (consumer_pages) |page| {
const page_mod = b.createModule(.{ .root_source_file = b.path(page[1]) });
page_mod.addImport("mer", mer_mod);
consumer_routes_mod.addImport(page[0], page_mod);
consumer_test_mod.addImport(page[0], page_mod);
}
consumer_test_mod.addImport("routes", consumer_routes_mod);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = consumer_test_mod })).step);
}
// ── Starter scaffold smoke test ──────────────────────────────────────────
// Compiles the embedded starter templates that `mer init` writes so
// scaffold regressions fail in CI before they ship.
{
const starter_test_mod = b.createModule(.{
.root_source_file = b.path("tests/starter/src/test_starter_scaffold.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
starter_test_mod.addImport("mer", mer_mod);
const starter_layout_mod = b.createModule(.{
.root_source_file = b.path("examples/starter/app/layout.zig"),
});
starter_layout_mod.addImport("mer", mer_mod);
const starter_page_specs = [_]struct { []const u8, []const u8 }{
.{ "app/index", "examples/starter/app/index.zig" },
.{ "app/about", "examples/starter/app/about.zig" },
.{ "app/404", "examples/starter/app/404.zig" },
};
const starter_routes_mod = b.createModule(.{
.root_source_file = b.path("tests/starter/src/routes.zig"),
});
starter_routes_mod.addImport("mer", mer_mod);
starter_routes_mod.addImport("app/layout", starter_layout_mod);
for (starter_page_specs) |page| {
const page_mod = b.createModule(.{ .root_source_file = b.path(page[1]) });
page_mod.addImport("mer", mer_mod);
page_mod.addImport("app/layout", starter_layout_mod);
starter_routes_mod.addImport(page[0], page_mod);
starter_test_mod.addImport(page[0], page_mod);
}
const starter_api_mod = b.createModule(.{
.root_source_file = b.path("examples/starter/api/hello.zig"),
});
starter_api_mod.addImport("mer", mer_mod);
starter_routes_mod.addImport("api/hello", starter_api_mod);
starter_test_mod.addImport("api/hello", starter_api_mod);
starter_test_mod.addImport("app/layout", starter_layout_mod);
starter_test_mod.addImport("routes", starter_routes_mod);
test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = starter_test_mod })).step);
}
// ── Packages ────────────────────────────────────────────────────────────
packages.addPackages(b, target, optimize, mer_mod);
// ── `zig build desktop-spike` — macOS native app research (#50) ─────────
if (target.result.os.tag == .macos) {
const spike_mod = b.createModule(.{
.root_source_file = b.path("examples/desktop/spike.zig"),
.target = target,
.optimize = optimize,
});
const spike_exe = b.addExecutable(.{ .name = "desktop-spike", .root_module = spike_mod });
spike_mod.linkFramework("AppKit", .{});
spike_mod.linkFramework("WebKit", .{});
spike_mod.linkFramework("Foundation", .{});
spike_mod.link_libc = true;
const spike_step = b.step("desktop-spike", "Research spike: Zig ObjC bridge for AppKit/WebKit (#50)");
spike_step.dependOn(&b.addInstallArtifact(spike_exe, .{}).step);
}
// ── `zig build desktop` — native macOS desktop app ──────────────────────
if (target.result.os.tag == .macos) {
const desktop_mod = b.createModule(.{
.root_source_file = b.path("examples/desktop/main.zig"),
.target = target,
.optimize = optimize,
});
desktop_mod.addImport("mer", mer_mod);
helpers.addDirModules(b, desktop_mod, mer_mod, "examples/site/app", "app", site_extras);
helpers.addDirModules(b, desktop_mod, mer_mod, "examples/site/api", "api", &.{});
helpers.addRoutesModule(b, desktop_mod, mer_mod, "src/generated/routes.zig", "examples/site/app", "examples/site/api", site_extras);
const desktop_exe = b.addExecutable(.{ .name = "merapp", .root_module = desktop_mod });
desktop_mod.linkFramework("AppKit", .{});
desktop_mod.linkFramework("WebKit", .{});
desktop_mod.linkFramework("Foundation", .{});
desktop_mod.link_libc = true;
const desktop_install = b.addInstallArtifact(desktop_exe, .{});
const desktop_step = b.step("desktop", "Build native macOS desktop app (also produces MerApp.app bundle)");
desktop_step.dependOn(&desktop_install.step);
// ── .app bundle — MerApp.app/Contents/MacOS/merapp + Info.plist ──────
const plist = b.addWriteFile("MerApp.app/Contents/Info.plist",
\\<?xml version="1.0" encoding="UTF-8"?>
\\<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
\\<plist version="1.0">
\\<dict>
\\ <key>CFBundleExecutable</key> <string>merapp</string>
\\ <key>CFBundleIdentifier</key> <string>com.merjs.desktop</string>
\\ <key>CFBundleName</key> <string>MerApp</string>
\\ <key>CFBundleVersion</key> <string>0.2.5</string>
\\ <key>NSHighResolutionCapable</key><true/>
\\ <key>NSPrincipalClass</key> <string>NSApplication</string>
\\</dict>
\\</plist>
);
const bundle_bin = b.addInstallFile(
desktop_exe.getEmittedBin(),
"MerApp.app/Contents/MacOS/merapp",
);
bundle_bin.step.dependOn(&desktop_install.step);
const bundle_plist = b.addInstallDirectory(.{
.source_dir = plist.getDirectory(),
.install_dir = .prefix,
.install_subdir = "",
});
desktop_step.dependOn(&bundle_bin.step);
desktop_step.dependOn(&bundle_plist.step);
}
}