Skip to content

Commit 5b3f01d

Browse files
authored
Merge pull request #45 from allyourcodebase/mason/simplify-lazy-deps
Simplify lazy dependency handling
2 parents 910884e + 00d355a commit 5b3f01d

3 files changed

Lines changed: 43 additions & 74 deletions

File tree

‎build.zig‎

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -174,39 +174,35 @@ pub fn build(b: *std.Build) !void {
174174
run_step.dependOn(&run_example.step);
175175
}
176176

177-
/// Prefetches a list of dependencies, where each dependency has a name field and an args field. If
178-
/// any dependencies are not yet fetched, returns `error.LazyDependencyNeeded`. Saves us from
179-
/// repeatedly earlying out every time a new lazy dependency is needed.
180-
pub fn prefetch(b: *std.Build, comptime deps: anytype) error{LazyDependencyNeeded}!void {
181-
var lazy_deps_needed = false;
182-
inline for (deps) |dep| {
183-
if (!b.systemIntegrationOption(dep.name, .{})) {
184-
if (b.lazyDependency(dep.name, dep.args) == null) {
185-
lazy_deps_needed = true;
186-
}
187-
}
188-
}
189-
if (lazy_deps_needed) {
190-
return error.LazyDependencyNeeded;
177+
/// Fetches a lazy dependency, unless provided via `-fsys`.
178+
///
179+
/// A more typical setup would still need to link to the library in the event that it's provided by
180+
/// `-fsys`, but we can just treat this as a noop as SDL doesn't link to its dependencies, it
181+
/// `dlopen`s them when available. We only pull them in for the headers.
182+
pub fn lazyDependency(
183+
step: *std.Build.Step,
184+
name: []const u8,
185+
args: anytype,
186+
) ?*std.Build.Dependency {
187+
// If we're using a system integration, return null as no work needs to be done. SDL will
188+
// `dlopen` the library, if available, at runtime so we don't need to link it here.
189+
if (step.owner.systemIntegrationOption(name, .{})) return null;
190+
if (@hasDecl(std.Build, "dependencyStep")) {
191+
return step.owner.dependencyStep(step, name, args);
192+
} else {
193+
// https://github.com/allyourcodebase/SDL/issues/44
194+
return step.owner.lazyDependency(name, .{});
191195
}
192196
}
193197

194-
/// Similar to `std.Build.lazyDependency`, but returns `null` if provided by the system with
195-
/// `-fsys`, and otherwise assserts that the dependency was already prefetched (e.g. with
196-
/// `prefetch`.)
197-
pub fn lazyDependency(b: *std.Build, name: []const u8) ?*std.Build.Dependency {
198-
if (b.systemIntegrationOption(name, .{})) return null;
199-
return b.lazyDependency(name, .{}) orelse std.debug.panic("{s} not prefetched", .{name});
200-
}
201-
202198
/// Adds an include path via `lazyDependency`, unless provided via `-fsys`.
203199
pub fn addDepIncludePath(
204-
b: *std.Build,
205200
lib: *std.Build.Step.Compile,
206201
name: []const u8,
207202
sub_path: []const u8,
203+
args: anytype,
208204
) void {
209-
if (lazyDependency(b, name)) |dep| {
205+
if (lazyDependency(&lib.step, name, args)) |dep| {
210206
lib.root_module.addIncludePath(dep.path(sub_path));
211207
}
212208
}

‎build.zig.zon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = .sdl,
33
.fingerprint = 0xec638ccbd103848b,
4-
.version = "1.0.7+3.4.18",
4+
.version = "1.0.8+3.4.18",
55
.minimum_zig_version = "0.16.0",
66
.dependencies = .{
77
.sdl = .{

‎src/linux.zig‎

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,6 @@ pub fn build(
1717
else => "pulseaudio",
1818
};
1919

20-
root.prefetch(b, .{
21-
.{ .name = "dbus", .args = .{} },
22-
.{ .name = "x11", .args = .{} },
23-
.{ .name = "xcursor", .args = .{} },
24-
.{ .name = "pipewire", .args = .{} },
25-
.{ .name = pulseaudio_name, .args = .{} },
26-
.{ .name = "wayland", .args = .{} },
27-
.{ .name = "alsa", .args = .{} },
28-
.{ .name = "fribidi", .args = .{} },
29-
.{ .name = "ibus", .args = .{} },
30-
.{ .name = "xkbcommon", .args = .{} },
31-
.{ .name = "xorgproto", .args = .{} },
32-
.{ .name = "xext", .args = .{} },
33-
.{ .name = "usb", .args = .{} },
34-
.{ .name = "xi", .args = .{} },
35-
.{ .name = "xfixes", .args = .{} },
36-
.{ .name = "xrandr", .args = .{} },
37-
.{ .name = "xrender", .args = .{} },
38-
.{ .name = "xscrnsaver", .args = .{} },
39-
.{ .name = "jack", .args = .{} },
40-
.{ .name = "sndio", .args = .{} },
41-
.{ .name = "decor", .args = .{} },
42-
.{ .name = "thai", .args = .{} },
43-
}) catch |err| switch (err) {
44-
error.LazyDependencyNeeded => return,
45-
};
46-
4720
const upstream = b.dependency("sdl", .{});
4821

4922
// Provide the platform specific dependency include paths
@@ -53,7 +26,7 @@ pub fn build(
5326
lib.root_module.addIncludePath(generated.getDirectory());
5427

5528
// Provide the D-Bus headers
56-
if (root.lazyDependency(b, "dbus")) |dbus| {
29+
if (root.lazyDependency(&lib.step, "dbus", .{})) |dbus| {
5730
lib.root_module.addIncludePath(dbus.path("."));
5831

5932
const version_string = build_zon.dependencies.dbus.version;
@@ -138,7 +111,7 @@ pub fn build(
138111
// Provide the IBus headers
139112
{
140113
// The headers are here
141-
root.addDepIncludePath(b, lib, "ibus", "src");
114+
root.addDepIncludePath(lib, "ibus", "src", .{});
142115

143116
// They depend on the GLib headers, which require some configuration
144117
lib.root_module.addIncludePath(b.path("deps/glib/upstream/include"));
@@ -321,7 +294,7 @@ pub fn build(
321294

322295
// Provide the X11 headers
323296
{
324-
if (root.lazyDependency(b, "x11")) |x11| {
297+
if (root.lazyDependency(&lib.step, "x11", .{})) |x11| {
325298
lib.root_module.addIncludePath(x11.path("include"));
326299

327300
const config = b.addConfigHeader(.{
@@ -335,7 +308,7 @@ pub fn build(
335308
}
336309

337310
// Provide the Xcursor headers
338-
if (root.lazyDependency(b, "xcursor")) |xcursor| {
311+
if (root.lazyDependency(&lib.step, "xcursor", .{})) |xcursor| {
339312
const version_string = build_zon.dependencies.xcursor.version;
340313
const version = comptime std.SemanticVersion.parse(version_string) catch unreachable;
341314
const config = b.addConfigHeader(.{
@@ -381,7 +354,7 @@ pub fn build(
381354
}
382355

383356
// Provide the pipewire headers
384-
if (root.lazyDependency(b, "pipewire")) |pipewire| {
357+
if (root.lazyDependency(&lib.step, "pipewire", .{})) |pipewire| {
385358
lib.root_module.addIncludePath(pipewire.path("spa/include"));
386359
lib.root_module.addIncludePath(pipewire.path("src"));
387360
const version_string = build_zon.dependencies.pipewire.version;
@@ -402,7 +375,7 @@ pub fn build(
402375
// Provide the pulseaudio headers
403376
{
404377
@setEvalBranchQuota(2000);
405-
if (root.lazyDependency(b, pulseaudio_name)) |pulseaudio| {
378+
if (root.lazyDependency(&lib.step, pulseaudio_name, .{})) |pulseaudio| {
406379
lib.root_module.addIncludePath(pulseaudio.path("src"));
407380
const version_string = build_zon.dependencies.pulseaudio.version;
408381
const version = comptime std.SemanticVersion.parse(version_string) catch unreachable;
@@ -424,7 +397,7 @@ pub fn build(
424397
}
425398

426399
// Provide the Wayland headers
427-
if (root.lazyDependency(b, "wayland")) |wayland| {
400+
if (root.lazyDependency(&lib.step, "wayland", .{})) |wayland| {
428401
lib.root_module.addIncludePath(wayland.path("src"));
429402
lib.root_module.addIncludePath(wayland.path("cursor"));
430403
lib.root_module.addIncludePath(wayland.path("egl"));
@@ -452,7 +425,7 @@ pub fn build(
452425
}
453426

454427
// Provide the Alsa headers
455-
if (root.lazyDependency(b, "alsa")) |alsa| {
428+
if (root.lazyDependency(&lib.step, "alsa", .{})) |alsa| {
456429
_ = generated.addCopyDirectory(alsa.path("include"), "alsa", .{
457430
.include_extensions = &.{".h"},
458431
});
@@ -461,7 +434,7 @@ pub fn build(
461434
}
462435

463436
// Provide the Fribidi headers
464-
if (root.lazyDependency(b, "fribidi")) |fribidi| {
437+
if (root.lazyDependency(&lib.step, "fribidi", .{})) |fribidi| {
465438
const interface_version = build_zon.dependencies.fribidi.interface_version;
466439
const version_string = build_zon.dependencies.fribidi.version;
467440
const version = comptime std.SemanticVersion.parse(version_string) catch unreachable;
@@ -507,21 +480,21 @@ pub fn build(
507480
}
508481

509482
// Provide upstream headers that don't require any special handling
510-
root.addDepIncludePath(b, lib, "xkbcommon", "include");
511-
root.addDepIncludePath(b, lib, "xorgproto", "include");
512-
root.addDepIncludePath(b, lib, "xext", "include");
513-
root.addDepIncludePath(b, lib, "usb", "libusb");
514-
root.addDepIncludePath(b, lib, "xi", "include");
515-
root.addDepIncludePath(b, lib, "xfixes", "include");
516-
root.addDepIncludePath(b, lib, "xrandr", "include");
517-
root.addDepIncludePath(b, lib, "xrender", "include");
518-
root.addDepIncludePath(b, lib, "xscrnsaver", "include");
519-
root.addDepIncludePath(b, lib, "jack", "common");
520-
root.addDepIncludePath(b, lib, "sndio", "libsndio");
483+
root.addDepIncludePath(lib, "xkbcommon", "include", .{});
484+
root.addDepIncludePath(lib, "xorgproto", "include", .{});
485+
root.addDepIncludePath(lib, "xext", "include", .{});
486+
root.addDepIncludePath(lib, "usb", "libusb", .{});
487+
root.addDepIncludePath(lib, "xi", "include", .{});
488+
root.addDepIncludePath(lib, "xfixes", "include", .{});
489+
root.addDepIncludePath(lib, "xrandr", "include", .{});
490+
root.addDepIncludePath(lib, "xrender", "include", .{});
491+
root.addDepIncludePath(lib, "xscrnsaver", "include", .{});
492+
root.addDepIncludePath(lib, "jack", "common", .{});
493+
root.addDepIncludePath(lib, "sndio", "libsndio", .{});
521494
lib.root_module.addIncludePath(b.path("deps/wayland/protocols"));
522-
root.addDepIncludePath(b, lib, "decor", "src");
495+
root.addDepIncludePath(lib, "decor", "src", .{});
523496
lib.root_module.addIncludePath(b.path("deps/mesa/include"));
524-
root.addDepIncludePath(b, lib, "thai", "include");
497+
root.addDepIncludePath(lib, "thai", "include", .{});
525498

526499
// Provide vendored headers that don't require any special handling
527500
lib.root_module.addIncludePath(b.path("deps/xcb/include"));

0 commit comments

Comments
 (0)