Skip to content

Commit a764b0e

Browse files
authored
Merge pull request #26 from mjakeman/issue/change-linking-approach
Linking: Remove `link_SDL2*` methods, update README, and fix race condition
2 parents 6a711ea + e84b2cc commit a764b0e

File tree

2 files changed

+99
-54
lines changed

2 files changed

+99
-54
lines changed

README.md

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,45 @@ pub fn build(b: *std.Build) !void {
1313
exe.linkLibC();
1414
1515
const zsdl = b.dependency("zsdl", .{});
16-
17-
exe.root_module.addImport("zsdl2", zsdl.module("zsdl2"));
18-
@import("zsdl").link_SDL2(exe);
1916
17+
exe.root_module.addImport("zsdl2", zsdl.module("zsdl2"));
2018
exe.root_module.addImport("zsdl2_ttf", zsdl.module("zsdl2_ttf"));
21-
@import("zsdl").link_SDL2_ttf(exe);
22-
2319
exe.root_module.addImport("zsdl2_image", zsdl.module("zsdl2_image"));
24-
@import("zsdl").link_SDL2_image(exe);
2520
26-
// Optionally use prebuilt libs instead of relying on system installed SDL...
27-
@import("zsdl").prebuilt_sdl2.addLibraryPathsTo(exe);
28-
if (@import("zsdl").prebuilt_sdl2.install(b, target.result, .bin), .{
29-
.ttf = true,
30-
.image = true,
31-
}) |install_sdl2_step| {
32-
b.getInstallStep().dependOn(install_sdl2_step);
21+
// Link against SDL libs
22+
linkSdlLibs(exe);
23+
}
24+
```
25+
26+
Link against SDL:
27+
28+
```zig
29+
pub fn linkSdlLibs(compile_step: *std.Build.Step.Compile) void {
30+
// Adjust as needed for the libraries you are using.
31+
switch (compile_step.rootModuleTarget().os.tag) {
32+
.windows => {
33+
compile_step.linkSystemLibrary("SDL2");
34+
compile_step.linkSystemLibrary("SDL2main"); // Only needed for SDL2, not ttf or image
35+
36+
compile_step.linkSystemLibrary("SDL2_ttf");
37+
compile_step.linkSystemLibrary("SDL2_image");
38+
},
39+
.linux => {
40+
compile_step.linkSystemLibrary("SDL2");
41+
compile_step.linkSystemLibrary("SDL2_ttf");
42+
compile_step.linkSystemLibrary("SDL2_image");
43+
},
44+
.macos => {
45+
compile_step.linkFramework("SDL2");
46+
compile_step.linkFramework("SDL2_ttf");
47+
compile_step.linkFramework("SDL2_image");
48+
},
49+
else => {},
3350
}
3451
}
3552
```
3653

54+
### Using prebuilt libraries
3755
NOTE: If you want to use our prebuilt libraries also add the following to your `build.zig.zon`:
3856
```zig
3957
.@"sdl2-prebuilt-macos" = .{
@@ -53,6 +71,34 @@ NOTE: If you want to use our prebuilt libraries also add the following to your `
5371
},
5472
```
5573

74+
And add the following to your `build.zig`:
75+
76+
```zig
77+
fn build(b: *std.Build) !void {
78+
79+
// ... other build steps ...
80+
81+
// Optionally use prebuilt libs instead of relying on system installed SDL...
82+
@import("zsdl").prebuilt_sdl2.addLibraryPathsTo(exe);
83+
if (@import("zsdl").prebuilt_sdl2.install(b, target.result, .bin), .{
84+
.ttf = true,
85+
.image = true,
86+
}) |install_sdl2_step| {
87+
b.getInstallStep().dependOn(install_sdl2_step);
88+
}
89+
90+
// Prebuilt libraries are installed to the executable directory. Set the RPath so the
91+
// executable knows where to look at runtime.
92+
switch (exe.rootModuleTarget().os.tag) {
93+
.windows => {}, // rpath is not used on Windows
94+
.linux => exe.root_module.addRPathSpecial("$ORIGIN"),
95+
.macos => exe.root_module.addRPathSpecial("@executable_path"),
96+
else => {},
97+
}
98+
}
99+
```
100+
101+
### Using zsdl2 in your code
56102
Now in your code you may import and use `zsdl2`:
57103

58104
```zig

build.zig

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void {
3333
const test_step = b.step("test", "Run bindings tests");
3434
{ // Test SDL2 bindings
3535
const zsdl2_tests = addTests(test_step, target, optimize, "zsdl2-tests", "src/sdl2.zig");
36-
link_SDL2(zsdl2_tests);
36+
link_SDL2_libs_testing(zsdl2_tests);
3737
prebuilt_sdl2.addLibraryPathsTo(zsdl2_tests);
3838
}
3939
{ // Test SDL2_ttf bindings
@@ -46,8 +46,7 @@ pub fn build(b: *std.Build) void {
4646
"zsdl2",
4747
zsdl2_module,
4848
);
49-
link_SDL2(zsdl2_ttf_tests);
50-
link_SDL2_ttf(zsdl2_ttf_tests);
49+
link_SDL2_libs_testing(zsdl2_ttf_tests);
5150
prebuilt_sdl2.addLibraryPathsTo(zsdl2_ttf_tests);
5251
}
5352
{ // Test SDL2_image bindings
@@ -60,13 +59,12 @@ pub fn build(b: *std.Build) void {
6059
"zsdl2",
6160
zsdl2_module,
6261
);
63-
link_SDL2(zsdl2_image_tests);
64-
link_SDL2_image(zsdl2_image_tests);
62+
link_SDL2_libs_testing(zsdl2_image_tests);
6563
prebuilt_sdl2.addLibraryPathsTo(zsdl2_image_tests);
6664
}
6765
{ // Test SDL3 bindings
6866
const zsdl3_tests = addTests(test_step, target, optimize, "zsdl3-tests", "src/sdl3.zig");
69-
link_SDL3(zsdl3_tests);
67+
link_SDL3_libs_testing(zsdl3_tests);
7068
prebuilt_sdl3.addLibraryPathsTo(zsdl3_tests);
7169
}
7270

@@ -80,73 +78,73 @@ pub fn build(b: *std.Build) void {
8078
}
8179
}
8280

83-
pub fn link_SDL2(compile_step: *std.Build.Step.Compile) void {
81+
fn link_SDL2_libs_testing(compile_step: *std.Build.Step.Compile) void {
8482
switch (compile_step.rootModuleTarget().os.tag) {
8583
.windows => {
8684
compile_step.linkSystemLibrary("SDL2");
8785
compile_step.linkSystemLibrary("SDL2main");
86+
compile_step.linkSystemLibrary("SDL2_ttf");
87+
compile_step.linkSystemLibrary("SDL2_image");
8888
},
8989
.linux => {
9090
compile_step.linkSystemLibrary("SDL2");
91+
compile_step.linkSystemLibrary("SDL2_ttf");
92+
compile_step.linkSystemLibrary("SDL2_image");
9193
compile_step.root_module.addRPathSpecial("$ORIGIN");
9294
},
9395
.macos => {
9496
compile_step.linkFramework("SDL2");
97+
compile_step.linkFramework("SDL2_ttf");
98+
compile_step.linkFramework("SDL2_image");
9599
compile_step.root_module.addRPathSpecial("@executable_path");
96100
},
97101
else => {},
98102
}
99103
}
100104

101-
pub fn link_SDL2_ttf(compile_step: *std.Build.Step.Compile) void {
105+
fn link_SDL3_libs_testing(compile_step: *std.Build.Step.Compile) void {
102106
switch (compile_step.rootModuleTarget().os.tag) {
103107
.windows => {
104-
compile_step.linkSystemLibrary("SDL2_ttf");
108+
compile_step.linkSystemLibrary("SDL3");
105109
},
106110
.linux => {
107-
compile_step.linkSystemLibrary("SDL2_ttf");
111+
compile_step.linkSystemLibrary("SDL3");
108112
compile_step.root_module.addRPathSpecial("$ORIGIN");
109113
},
110114
.macos => {
111-
compile_step.linkFramework("SDL2_ttf");
115+
compile_step.linkFramework("SDL3");
112116
compile_step.root_module.addRPathSpecial("@executable_path");
113117
},
114118
else => {},
115119
}
116120
}
117121

122+
pub fn link_SDL2(compile_step: *std.Build.Step.Compile) void {
123+
_ = compile_step;
124+
@compileError("link_SDL2 no longer supported. Refer to README for linking instructions.");
125+
126+
// link_SDL2 is no longer supported for linking, as it assumes too much
127+
// about the build environment. You should instead copy the relevant link
128+
// calls from the README into your build.zig file and adjust as necessary.
129+
}
130+
131+
pub fn link_SDL2_ttf(compile_step: *std.Build.Step.Compile) void {
132+
_ = compile_step;
133+
@compileError("link_SDL2_ttf no longer supported. Refer to README for linking instructions.");
134+
}
135+
118136
pub fn link_SDL2_image(compile_step: *std.Build.Step.Compile) void {
119-
switch (compile_step.rootModuleTarget().os.tag) {
120-
.windows => {
121-
compile_step.linkSystemLibrary("SDL2_image");
122-
},
123-
.linux => {
124-
compile_step.linkSystemLibrary("SDL2_image");
125-
compile_step.root_module.addRPathSpecial("$ORIGIN");
126-
},
127-
.macos => {
128-
compile_step.linkFramework("SDL2_image");
129-
compile_step.root_module.addRPathSpecial("@executable_path");
130-
},
131-
else => {},
132-
}
137+
_ = compile_step;
138+
@compileError("link_SDL2_image no longer supported. Refer to README for linking instructions.");
133139
}
134140

135141
pub fn link_SDL3(compile_step: *std.Build.Step.Compile) void {
136-
switch (compile_step.rootModuleTarget().os.tag) {
137-
.windows => {
138-
compile_step.linkSystemLibrary("SDL3");
139-
},
140-
.linux => {
141-
compile_step.linkSystemLibrary("SDL3");
142-
compile_step.root_module.addRPathSpecial("$ORIGIN");
143-
},
144-
.macos => {
145-
compile_step.linkFramework("SDL3");
146-
compile_step.root_module.addRPathSpecial("@executable_path");
147-
},
148-
else => {},
149-
}
142+
_ = compile_step;
143+
@compileError("link_SDL3 no longer supported. Refer to README for linking instructions.");
144+
145+
// link_SDL3 is no longer supported for linking, as it assumes too much
146+
// about the build environment. You should instead copy the relevant link
147+
// calls from the README into your build.zig file and adjust as necessary.
150148
}
151149

152150
pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Step {
@@ -159,7 +157,7 @@ pub fn testVersionCheckSDL2(b: *std.Build, target: std.Build.ResolvedTarget) *st
159157
}),
160158
});
161159

162-
link_SDL2(test_sdl2_version_check);
160+
link_SDL2_libs_testing(test_sdl2_version_check);
163161

164162
prebuilt_sdl2.addLibraryPathsTo(test_sdl2_version_check);
165163

@@ -391,7 +389,7 @@ fn addTests(
391389
.optimize = optimize,
392390
}),
393391
});
394-
b.installArtifact(tests);
392+
const install = b.addInstallArtifact(tests, .{});
395393

396394
const run = b.addRunArtifact(tests);
397395
if (target.result.os.tag == .windows) {
@@ -400,6 +398,7 @@ fn addTests(
400398
});
401399
}
402400

401+
run.step.dependOn(&install.step);
403402
test_step.dependOn(&run.step);
404403
return tests;
405404
}

0 commit comments

Comments
 (0)