Skip to content

Commit 7533d8f

Browse files
authoredFeb 23, 2025··
build.zig Refactor prebuilt API (#13)
* build.zig Refector prebuilt API * update README
1 parent 788a90f commit 7533d8f

File tree

2 files changed

+127
-142
lines changed

2 files changed

+127
-142
lines changed
 

Diff for: ‎README.md

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# [zsdl](https://github.com/zig-gamedev/zsdl)
22

3-
Zig bindings for SDL libs. Work in progress.
3+
Zigified bindings for SDL libs. Work in progress.
44

55
## Getting started (SDL2)
66

@@ -24,16 +24,13 @@ pub fn build(b: *std.Build) !void {
2424
@import("zsdl").link_SDL2_image(exe);
2525
2626
// Optionally use prebuilt libs instead of relying on system installed SDL...
27-
@import("zsdl").prebuilt.addLibraryPathsTo(exe);
28-
if (@import("zsdl").prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| {
27+
@import("zsdl").prebuilt_sdl2.addLibraryPathsTo(exe);
28+
if (@import("zsdl").prebuilt_sdl2.install_SDL2(b, target.result, .bin), .{
29+
.ttf = true,
30+
.image = true,
31+
}) |install_sdl2_step| {
2932
b.getInstallStep().dependOn(install_sdl2_step);
3033
}
31-
if (@import("zsdl").prebuilt.install_SDL2_ttf(b, target.result, .bin)) |install_sdl2_ttf_step| {
32-
b.getInstallStep().dependOn(install_sdl2_ttf_step);
33-
}
34-
if (@import("zsdl").prebuilt.install_SDL2_image(b, target.result, .bin)) |install_sdl2_image_step| {
35-
b.getInstallStep().dependOn(install_sdl2_image_step);
36-
}
3734
}
3835
```
3936

Diff for: ‎build.zig

+121-133
Original file line numberDiff line numberDiff line change
@@ -29,53 +29,54 @@ pub fn build(b: *std.Build) void {
2929
.root_source_file = b.path("src/sdl3.zig"),
3030
});
3131

32-
const test_step = b.step("test", "Run bindings tests");
33-
{ // Test SDL2 bindings
34-
const zsdl2_tests = addTests(test_step, target, optimize, "zsdl2-tests", "src/sdl2.zig");
35-
link_SDL2(zsdl2_tests);
36-
}
37-
{ // Test SDL2_ttf bindings
38-
const zsdl2_ttf_tests = addTestsAux(
39-
test_step,
40-
target,
41-
optimize,
42-
"zsdl2_ttf-tests",
43-
"src/sdl2_ttf.zig",
44-
"zsdl2",
45-
zsdl2_module,
46-
);
47-
link_SDL2(zsdl2_ttf_tests);
48-
link_SDL2_ttf(zsdl2_ttf_tests);
49-
}
50-
{ // Test SDL2_image bindings
51-
const zsdl2_image_tests = addTestsAux(
52-
test_step,
53-
target,
54-
optimize,
55-
"zsdl2_ttf-image",
56-
"src/sdl2_image.zig",
57-
"zsdl2",
58-
zsdl2_module,
59-
);
60-
link_SDL2(zsdl2_image_tests);
61-
link_SDL2_image(zsdl2_image_tests);
62-
}
63-
{ // Test SDL3 bindings
64-
const zsdl3_tests = addTests(test_step, target, optimize, "zsdl3-tests", "src/sdl3.zig");
65-
link_SDL3(zsdl3_tests);
66-
}
32+
{
33+
const test_step = b.step("test", "Run bindings tests");
34+
{ // Test SDL2 bindings
35+
const zsdl2_tests = addTests(test_step, target, optimize, "zsdl2-tests", "src/sdl2.zig");
36+
link_SDL2(zsdl2_tests);
37+
prebuilt_sdl2.addLibraryPathsTo(zsdl2_tests);
38+
}
39+
{ // Test SDL2_ttf bindings
40+
const zsdl2_ttf_tests = addTestsAux(
41+
test_step,
42+
target,
43+
optimize,
44+
"zsdl2_ttf-tests",
45+
"src/sdl2_ttf.zig",
46+
"zsdl2",
47+
zsdl2_module,
48+
);
49+
link_SDL2(zsdl2_ttf_tests);
50+
link_SDL2_ttf(zsdl2_ttf_tests);
51+
prebuilt_sdl2.addLibraryPathsTo(zsdl2_ttf_tests);
52+
}
53+
{ // Test SDL2_image bindings
54+
const zsdl2_image_tests = addTestsAux(
55+
test_step,
56+
target,
57+
optimize,
58+
"zsdl2_ttf-image",
59+
"src/sdl2_image.zig",
60+
"zsdl2",
61+
zsdl2_module,
62+
);
63+
link_SDL2(zsdl2_image_tests);
64+
link_SDL2_image(zsdl2_image_tests);
65+
prebuilt_sdl2.addLibraryPathsTo(zsdl2_image_tests);
66+
}
67+
{ // Test SDL3 bindings
68+
const zsdl3_tests = addTests(test_step, target, optimize, "zsdl3-tests", "src/sdl3.zig");
69+
link_SDL3(zsdl3_tests);
70+
prebuilt_sdl3.addLibraryPathsTo(zsdl3_tests);
71+
}
6772

68-
if (prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| {
69-
b.getInstallStep().dependOn(install_sdl2_step);
70-
}
71-
if (prebuilt.install_SDL2_ttf(b, target.result, .bin)) |install_sdl2_ttf_step| {
72-
b.getInstallStep().dependOn(install_sdl2_ttf_step);
73-
}
74-
if (prebuilt.install_SDL2_image(b, target.result, .bin)) |install_sdl2_image_step| {
75-
b.getInstallStep().dependOn(install_sdl2_image_step);
76-
}
77-
if (prebuilt.install_SDL3(b, target.result, .bin)) |install_sdl3_step| {
78-
b.getInstallStep().dependOn(install_sdl3_step);
73+
if (prebuilt_sdl2.install(b, target.result, .bin, .{ .ttf = true, .image = true })) |install_sdl2_step| {
74+
b.getInstallStep().dependOn(install_sdl2_step);
75+
}
76+
77+
if (prebuilt_sdl3.install(b, target.result, .bin, .{})) |install_sdl3_step| {
78+
b.getInstallStep().dependOn(install_sdl3_step);
79+
}
7980
}
8081
}
8182

@@ -158,7 +159,7 @@ pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *st
158159

159160
link_SDL2(test_sdl2_version_check);
160161

161-
prebuilt.addLibraryPathsTo(test_sdl2_version_check);
162+
prebuilt_sdl2.addLibraryPathsTo(test_sdl2_version_check);
162163

163164
const version_check_run = b.addRunArtifact(test_sdl2_version_check);
164165

@@ -170,14 +171,14 @@ pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *st
170171

171172
version_check_run.step.dependOn(&test_sdl2_version_check.step);
172173

173-
if (prebuilt.install_SDL2(b, target.result, .bin)) |install_sdl2_step| {
174+
if (prebuilt_sdl2.install(b, target.result, .bin, .{})) |install_sdl2_step| {
174175
version_check_run.step.dependOn(install_sdl2_step);
175176
}
176177

177178
return &version_check_run.step;
178179
}
179180

180-
pub const prebuilt = struct {
181+
pub const prebuilt_sdl2 = struct {
181182
pub fn addLibraryPathsTo(compile_step: *std.Build.Step.Compile) void {
182183
const b = compile_step.step.owner;
183184
const target = compile_step.rootModuleTarget();
@@ -187,164 +188,153 @@ pub const prebuilt = struct {
187188
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
188189
compile_step.addLibraryPath(sdl2_prebuilt.path("lib"));
189190
}
190-
if (b.lazyDependency("sdl3-prebuilt-x86_64-windows-gnu", .{})) |sdl3_prebuilt| {
191-
compile_step.addLibraryPath(sdl3_prebuilt.path("bin"));
192-
}
193191
}
194192
},
195193
.linux => {
196194
if (target.cpu.arch.isX86()) {
197195
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
198196
compile_step.addLibraryPath(sdl2_prebuilt.path("lib"));
199197
}
200-
if (b.lazyDependency("sdl3-prebuilt-x86_64-linux-gnu", .{})) |sdl3_prebuilt| {
201-
compile_step.addLibraryPath(sdl3_prebuilt.path("lib"));
202-
}
203198
}
204199
},
205200
.macos => {
206201
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
207202
compile_step.addFrameworkPath(sdl2_prebuilt.path("Frameworks"));
208203
}
209-
if (b.lazyDependency("sdl3-prebuilt-macos", .{})) |sdl3_prebuilt| {
210-
compile_step.addFrameworkPath(sdl3_prebuilt.path("Frameworks"));
211-
}
212204
},
213205
else => {},
214206
}
215207
}
216208

217-
pub fn install_SDL2(
209+
pub fn install(
218210
b: *std.Build,
219211
target: std.Target,
220212
install_dir: std.Build.InstallDir,
213+
aux_libs: packed struct {
214+
ttf: bool = false,
215+
image: bool = false,
216+
},
221217
) ?*std.Build.Step {
218+
var install_step = b.step("Install SDL2", "Installs SDL2 and auxillary runtime libraries.");
219+
222220
switch (target.os.tag) {
223221
.windows => {
224222
if (target.cpu.arch.isX86()) {
225223
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
226-
return &b.addInstallFileWithDir(
224+
install_step.dependOn(&b.addInstallFileWithDir(
227225
sdl2_prebuilt.path("bin/SDL2.dll"),
228226
install_dir,
229227
"SDL2.dll",
230-
).step;
228+
).step);
229+
if (aux_libs.ttf) {
230+
install_step.dependOn(&b.addInstallFileWithDir(
231+
sdl2_prebuilt.path("bin/SDL2_ttf.dll"),
232+
install_dir,
233+
"SDL2_ttf.dll",
234+
).step);
235+
}
236+
if (aux_libs.image) {
237+
install_step.dependOn(&b.addInstallFileWithDir(
238+
sdl2_prebuilt.path("bin/SDL2_image.dll"),
239+
install_dir,
240+
"SDL2_image.dll",
241+
).step);
242+
}
231243
}
232244
}
233245
},
234246
.linux => {
235247
if (target.cpu.arch.isX86()) {
236248
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
237-
return &b.addInstallFileWithDir(
249+
install_step.dependOn(&b.addInstallFileWithDir(
238250
sdl2_prebuilt.path("lib/libSDL2.so"),
239251
install_dir,
240252
"libSDL2.so",
241-
).step;
253+
).step);
254+
if (aux_libs.ttf) {
255+
install_step.dependOn(&b.addInstallFileWithDir(
256+
sdl2_prebuilt.path("lib/libSDL2_ttf.so"),
257+
install_dir,
258+
"libSDL2_ttf.so",
259+
).step);
260+
}
261+
if (aux_libs.image) {
262+
install_step.dependOn(&b.addInstallFileWithDir(
263+
sdl2_prebuilt.path("lib/libSDL2_image.so"),
264+
install_dir,
265+
"libSDL2_image.so",
266+
).step);
267+
}
242268
}
243269
}
244270
},
245271
.macos => {
246272
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
247-
return &b.addInstallDirectory(.{
273+
install_step.dependOn(&b.addInstallDirectory(.{
248274
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2.framework"),
249275
.install_dir = install_dir,
250276
.install_subdir = "SDL2.framework",
251-
}).step;
252-
}
253-
},
254-
else => {},
255-
}
256-
return null;
257-
}
258-
259-
pub fn install_SDL2_ttf(
260-
b: *std.Build,
261-
target: std.Target,
262-
install_dir: std.Build.InstallDir,
263-
) ?*std.Build.Step {
264-
switch (target.os.tag) {
265-
.windows => {
266-
if (target.cpu.arch.isX86()) {
267-
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
268-
return &b.addInstallFileWithDir(
269-
sdl2_prebuilt.path("bin/SDL2_ttf.dll"),
270-
install_dir,
271-
"SDL2_ttf.dll",
272-
).step;
277+
}).step);
278+
if (aux_libs.ttf) {
279+
install_step.dependOn(&b.addInstallDirectory(.{
280+
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_ttf.framework"),
281+
.install_dir = install_dir,
282+
.install_subdir = "SDL2_ttf.framework",
283+
}).step);
273284
}
274-
}
275-
},
276-
.linux => {
277-
if (target.cpu.arch.isX86()) {
278-
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
279-
return &b.addInstallFileWithDir(
280-
sdl2_prebuilt.path("lib/libSDL2_ttf.so"),
281-
install_dir,
282-
"libSDL2_ttf.so",
283-
).step;
285+
if (aux_libs.image) {
286+
install_step.dependOn(&b.addInstallDirectory(.{
287+
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_image.framework"),
288+
.install_dir = install_dir,
289+
.install_subdir = "SDL2_image.framework",
290+
}).step);
284291
}
285292
}
286293
},
287-
.macos => {
288-
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
289-
return &b.addInstallDirectory(.{
290-
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_ttf.framework"),
291-
.install_dir = install_dir,
292-
.install_subdir = "SDL2_ttf.framework",
293-
}).step;
294-
}
295-
},
296294
else => {},
297295
}
298-
return null;
296+
297+
return install_step;
299298
}
299+
};
300300

301-
pub fn install_SDL2_image(
302-
b: *std.Build,
303-
target: std.Target,
304-
install_dir: std.Build.InstallDir,
305-
) ?*std.Build.Step {
301+
pub const prebuilt_sdl3 = struct {
302+
pub fn addLibraryPathsTo(compile_step: *std.Build.Step.Compile) void {
303+
const b = compile_step.step.owner;
304+
const target = compile_step.rootModuleTarget();
306305
switch (target.os.tag) {
307306
.windows => {
308307
if (target.cpu.arch.isX86()) {
309-
if (b.lazyDependency("sdl2-prebuilt-x86_64-windows-gnu", .{})) |sdl2_prebuilt| {
310-
return &b.addInstallFileWithDir(
311-
sdl2_prebuilt.path("bin/SDL2_image.dll"),
312-
install_dir,
313-
"SDL2_image.dll",
314-
).step;
308+
if (b.lazyDependency("sdl3-prebuilt-x86_64-windows-gnu", .{})) |sdl3_prebuilt| {
309+
compile_step.addLibraryPath(sdl3_prebuilt.path("bin"));
315310
}
316311
}
317312
},
318313
.linux => {
319314
if (target.cpu.arch.isX86()) {
320-
if (b.lazyDependency("sdl2-prebuilt-x86_64-linux-gnu", .{})) |sdl2_prebuilt| {
321-
return &b.addInstallFileWithDir(
322-
sdl2_prebuilt.path("lib/libSDL2_image.so"),
323-
install_dir,
324-
"libSDL2_image.so",
325-
).step;
315+
if (b.lazyDependency("sdl3-prebuilt-x86_64-linux-gnu", .{})) |sdl3_prebuilt| {
316+
compile_step.addLibraryPath(sdl3_prebuilt.path("lib"));
326317
}
327318
}
328319
},
329320
.macos => {
330-
if (b.lazyDependency("sdl2-prebuilt-macos", .{})) |sdl2_prebuilt| {
331-
return &b.addInstallDirectory(.{
332-
.source_dir = sdl2_prebuilt.path("Frameworks/SDL2_image.framework"),
333-
.install_dir = install_dir,
334-
.install_subdir = "SDL2_image.framework",
335-
}).step;
321+
if (b.lazyDependency("sdl3-prebuilt-macos", .{})) |sdl3_prebuilt| {
322+
compile_step.addFrameworkPath(sdl3_prebuilt.path("Frameworks"));
336323
}
337324
},
338325
else => {},
339326
}
340-
return null;
341327
}
342328

343-
pub fn install_SDL3(
329+
pub fn install(
344330
b: *std.Build,
345331
target: std.Target,
346332
install_dir: std.Build.InstallDir,
333+
aux_libs: packed struct {
334+
// TODO
335+
},
347336
) ?*std.Build.Step {
337+
_ = aux_libs;
348338
switch (target.os.tag) {
349339
.windows => {
350340
if (target.cpu.arch.isX86()) {
@@ -397,8 +387,6 @@ fn addTests(
397387
.target = target,
398388
.optimize = optimize,
399389
});
400-
// TODO(hazeycode): Using prebuilt libs for now. Compile from source in future.
401-
prebuilt.addLibraryPathsTo(tests);
402390
b.installArtifact(tests);
403391
test_step.dependOn(&b.addRunArtifact(tests).step);
404392
return tests;

0 commit comments

Comments
 (0)
Please sign in to comment.