-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
415 lines (361 loc) · 14.7 KB
/
build.zig
File metadata and controls
415 lines (361 loc) · 14.7 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
const std = @import("std");
const GlfwCBuild = struct {
lib: *std.Build.Step.Compile,
include_dir: std.Build.LazyPath,
};
fn buildGlfwCLib(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) GlfwCBuild {
const os_tag = target.result.os.tag;
// Fetch GLFW C source via Zon dependency "glfw-c".
const glfw_dep = b.dependency("glfw-c", .{
.target = target,
.optimize = optimize,
});
const include_dir = glfw_dep.path("include");
// C-only module + library for GLFW.
const glfw_c_mod = b.createModule(.{
.target = target,
.optimize = optimize,
});
const glfw_c = b.addLibrary(.{
.name = "glfw_c",
.root_module = glfw_c_mod,
});
glfw_c.addIncludePath(include_dir);
glfw_c.addIncludePath(glfw_dep.path("src"));
// Common GLFW C sources (shared across all backends).
const common_rel = [_][]const u8{
"src/context.c",
"src/init.c",
"src/input.c",
"src/monitor.c",
"src/platform.c",
"src/vulkan.c",
"src/window.c",
"src/egl_context.c",
"src/osmesa_context.c",
"src/null_init.c",
"src/null_monitor.c",
"src/null_window.c",
"src/null_joystick.c",
};
switch (os_tag) {
// ─────────────────────────────────────────────────────────────────────
// Windows (Win32 backend)
// ─────────────────────────────────────────────────────────────────────
.windows => {
const win_flags = &[_][]const u8{
"-D_GLFW_WIN32",
"-DUNICODE",
"-D_UNICODE",
};
// Common sources compiled as Win32 backend.
for (common_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = win_flags,
});
}
// Windows-specific sources.
const win_rel = [_][]const u8{
"src/win32_init.c",
"src/win32_joystick.c",
"src/win32_module.c",
"src/win32_monitor.c",
"src/win32_time.c",
"src/win32_thread.c",
"src/win32_window.c",
"src/wgl_context.c",
};
for (win_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = win_flags,
});
}
glfw_c.linkLibC();
glfw_c.linkSystemLibrary("user32");
glfw_c.linkSystemLibrary("gdi32");
glfw_c.linkSystemLibrary("shell32");
glfw_c.linkSystemLibrary("advapi32");
glfw_c.linkSystemLibrary("winmm");
},
// ─────────────────────────────────────────────────────────────────────
// macOS (Cocoa backend)
// ─────────────────────────────────────────────────────────────────────
.macos => {
const cocoa_flags = &[_][]const u8{
"-D_GLFW_COCOA",
};
// Common sources compiled as Cocoa backend.
for (common_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = cocoa_flags,
});
}
// Cocoa-specific sources. Note: .m files are Obj-C.
const cocoa_rel = [_][]const u8{
"src/cocoa_init.m",
"src/cocoa_joystick.m",
"src/cocoa_monitor.m",
"src/cocoa_window.m",
"src/cocoa_time.c",
"src/nsgl_context.m",
"src/posix_thread.c",
"src/posix_module.c",
};
for (cocoa_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = cocoa_flags,
});
}
glfw_c.linkLibC();
glfw_c.linkFramework("Cocoa");
glfw_c.linkFramework("IOKit");
glfw_c.linkFramework("CoreFoundation");
glfw_c.linkFramework("CoreVideo");
},
// ─────────────────────────────────────────────────────────────────────
// Linux (X11 backend)
// ─────────────────────────────────────────────────────────────────────
.linux => {
const x11_flags = &[_][]const u8{
"-D_GLFW_X11",
};
// Common sources compiled as X11 backend.
for (common_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = x11_flags,
});
}
// X11 / POSIX platform sources.
const x11_rel = [_][]const u8{
"src/x11_init.c",
"src/x11_monitor.c",
"src/x11_window.c",
"src/xkb_unicode.c",
"src/linux_joystick.c",
"src/posix_time.c",
"src/posix_thread.c",
"src/posix_module.c",
"src/posix_tls.c",
"src/glx_context.c",
};
for (x11_rel) |rel| {
glfw_c.addCSourceFile(.{
.file = glfw_dep.path(rel),
.flags = x11_flags,
});
}
glfw_c.linkLibC();
// Core X11 + extensions used by GLFW.
glfw_c.linkSystemLibrary("X11");
glfw_c.linkSystemLibrary("Xi");
glfw_c.linkSystemLibrary("Xrandr");
glfw_c.linkSystemLibrary("Xinerama");
glfw_c.linkSystemLibrary("Xcursor");
glfw_c.linkSystemLibrary("Xxf86vm");
// POSIX deps.
glfw_c.linkSystemLibrary("pthread");
glfw_c.linkSystemLibrary("dl");
glfw_c.linkSystemLibrary("m");
},
// ─────────────────────────────────────────────────────────────────────
// Others: not yet wired
// ─────────────────────────────────────────────────────────────────────
else => {
@panic("glfw-zig: buildGlfwCLib is wired for Windows, macOS, and Linux only for now.");
},
}
return .{
.lib = glfw_c,
.include_dir = include_dir,
};
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// 1) Build vendored GLFW C static lib + get its include dir.
const glfw_c_build = buildGlfwCLib(b, target, optimize);
const glfw_c = glfw_c_build.lib;
const glfw_include = glfw_c_build.include_dir;
// 2) Internal Zig modules (wired via named imports, Zigadel-style).
const c_bindings_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/c_bindings.zig"),
.target = target,
.optimize = optimize,
});
// c_import lives here, so it needs the C include path.
c_bindings_mod.addIncludePath(glfw_include);
const core_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/core.zig"),
.target = target,
.optimize = optimize,
});
core_mod.addImport("c_bindings", c_bindings_mod);
const window_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/window.zig"),
.target = target,
.optimize = optimize,
});
window_mod.addImport("c_bindings", c_bindings_mod);
window_mod.addImport("core", core_mod);
const context_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/context.zig"),
.target = target,
.optimize = optimize,
});
context_mod.addImport("c_bindings", c_bindings_mod);
context_mod.addImport("core", core_mod);
context_mod.addImport("window", window_mod);
const monitor_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/monitor.zig"),
.target = target,
.optimize = optimize,
});
monitor_mod.addImport("c_bindings", c_bindings_mod);
monitor_mod.addImport("core", core_mod);
const vulkan_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/vulkan.zig"),
.target = target,
.optimize = optimize,
});
vulkan_mod.addImport("c_bindings", c_bindings_mod);
vulkan_mod.addImport("core", core_mod);
vulkan_mod.addImport("window", window_mod);
const joystick_mod = b.createModule(.{
.root_source_file = b.path("src/glfw/joystick.zig"),
.target = target,
.optimize = optimize,
});
joystick_mod.addImport("c_bindings", c_bindings_mod);
joystick_mod.addImport("core", core_mod);
// 3) Public façade module (what users import as @import("glfw")).
const glfw_mod = b.addModule("glfw", .{
.root_source_file = b.path("src/glfw.zig"),
.target = target,
.optimize = optimize,
});
glfw_mod.addIncludePath(glfw_include);
glfw_mod.addImport("c_bindings", c_bindings_mod);
glfw_mod.addImport("core", core_mod);
glfw_mod.addImport("window", window_mod);
glfw_mod.addImport("monitor", monitor_mod);
glfw_mod.addImport("vulkan", vulkan_mod);
glfw_mod.addImport("context", context_mod);
glfw_mod.addImport("joystick", joystick_mod);
// 4) Library artifact for the Zig wrapper: libglfw-zig.
const lib = b.addLibrary(.{
.name = "glfw-zig",
.root_module = glfw_mod,
});
lib.linkLibrary(glfw_c);
b.installArtifact(lib);
// 5) Sample executable (src/sample.zig).
const sample_mod = b.createModule(.{
.root_source_file = b.path("src/sample.zig"),
.target = target,
.optimize = optimize,
});
sample_mod.addImport("glfw", glfw_mod);
const exe = b.addExecutable(.{
.name = "sample",
.root_module = sample_mod,
});
exe.linkLibrary(glfw_c);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run", "Run GLFW sample");
run_step.dependOn(&run_cmd.step);
// ─────────────────────────────────────────────────────────────────────
// 6) Tests: unit (inline), conformance, integration, e2e
// ─────────────────────────────────────────────────────────────────────
// Unit tests: inline `test {}` blocks in src/glfw.zig + submodules.
const unit_step = b.step("test-unit", "Run unit tests (glfw façade + submodules)");
{
const unit_tests = b.addTest(.{
.root_module = glfw_mod,
});
unit_tests.linkLibrary(glfw_c);
const run_unit = b.addRunArtifact(unit_tests);
unit_step.dependOn(&run_unit.step);
}
// Conformance tests: tests/test_all_conformance.zig (optional)
const conformance_step = b.step("test-conformance", "Run conformance tests");
const have_conformance = blk: {
_ = std.fs.cwd().statFile("tests/test_all_conformance.zig") catch break :blk false;
break :blk true;
};
if (have_conformance) {
const conf_mod = b.createModule(.{
.root_source_file = b.path("tests/test_all_conformance.zig"),
.target = target,
.optimize = optimize,
});
conf_mod.addImport("glfw", glfw_mod);
const conf_tests = b.addTest(.{
.root_module = conf_mod,
});
conf_tests.linkLibrary(glfw_c);
const conf_run = b.addRunArtifact(conf_tests);
conformance_step.dependOn(&conf_run.step);
}
// Integration tests: tests/test_all_integration.zig (optional)
const integration_step = b.step("test-integration", "Run integration tests");
const have_integration = blk: {
_ = std.fs.cwd().statFile("tests/test_all_integration.zig") catch break :blk false;
break :blk true;
};
if (have_integration) {
const integ_mod = b.createModule(.{
.root_source_file = b.path("tests/test_all_integration.zig"),
.target = target,
.optimize = optimize,
});
integ_mod.addImport("glfw", glfw_mod);
const integ_tests = b.addTest(.{
.root_module = integ_mod,
});
integ_tests.linkLibrary(glfw_c);
const integ_run = b.addRunArtifact(integ_tests);
integration_step.dependOn(&integ_run.step);
}
// E2E tests: tests/test_all_e2e.zig (optional)
const e2e_step = b.step("test-e2e", "Run end-to-end tests");
const have_e2e = blk: {
_ = std.fs.cwd().statFile("tests/test_all_e2e.zig") catch break :blk false;
break :blk true;
};
if (have_e2e) {
const e2e_mod = b.createModule(.{
.root_source_file = b.path("tests/test_all_e2e.zig"),
.target = target,
.optimize = optimize,
});
e2e_mod.addImport("glfw", glfw_mod);
const e2e_tests = b.addTest(.{
.root_module = e2e_mod,
});
e2e_tests.linkLibrary(glfw_c);
const e2e_run = b.addRunArtifact(e2e_tests);
e2e_step.dependOn(&e2e_run.step);
}
// Aggregate step: run everything.
const test_step = b.step(
"test",
"Run all GLFW test suites (unit + conformance + integration + e2e)",
);
test_step.dependOn(unit_step);
test_step.dependOn(conformance_step);
test_step.dependOn(integration_step);
test_step.dependOn(e2e_step);
// Make plain `zig build` run the full test suite by default.
b.default_step = test_step;
}