build: mark most dependencies as lazy

Lazy dependencies are only fetched if the build script would actually
reach a usage of that dependency at runtime (when the `lazyDependency`
function is called). This can save a lot of network traffic, disk uage,
and time because we don't have to fetch and build dependencies that we
don't actually need.

Prior to this commit, Ghostty fetched almost everything for all
platforms and configurations all the time. This commit reverses that to
fetching almost nothing until it's actually needed.

There are very little downsides to doing this[1]. One downside is `zig
build --fetch` doesn't fetch lazy dependencies, but we don't rely on
this command for packaging and suggest using our custom shell script
that downloads a cached list of URLs (`build.zig.zon.txt`).

This commit doesn't cover 100% of dependencies, since some provide no
benefit to make lazy while the complexity to make them lazy is higher
(in code style typically).

Conversely, some simple dependencies are marked lazy even if they're
almost always needed if they don't introduce any real complexity to the
code, because there is very little downside to do so.

[1]: https://ziggit.dev/t/lazy-dependencies-best-dependencies/5509/5
This commit is contained in:
Mitchell Hashimoto
2025-03-13 21:30:24 -07:00
parent 234b804872
commit cfea2ea12c
29 changed files with 977 additions and 781 deletions

View File

@@ -35,14 +35,20 @@ fn buildLib(b: *std.Build, module: *std.Build.Module, options: anytype) !*std.Bu
const target = options.target;
const optimize = options.optimize;
const upstream = b.dependency("gtk4_layer_shell", .{});
const wayland_protocols = b.dependency("wayland_protocols", .{});
// Shared library
const lib = b.addSharedLibrary(.{
.name = "gtk4-layer-shell",
.target = target,
.optimize = optimize,
});
b.installArtifact(lib);
// We need to call both lazy dependencies to tell Zig we need both
const upstream_ = b.lazyDependency("gtk4_layer_shell", .{});
const wayland_protocols_ = b.lazyDependency("wayland_protocols", .{});
const upstream = upstream_ orelse return lib;
const wayland_protocols = wayland_protocols_ orelse return lib;
lib.linkLibC();
lib.addIncludePath(upstream.path("include"));
lib.addIncludePath(upstream.path("src"));
@@ -124,6 +130,5 @@ fn buildLib(b: *std.Build, module: *std.Build.Module, options: anytype) !*std.Bu
},
});
b.installArtifact(lib);
return lib;
}

View File

@@ -7,10 +7,12 @@
.gtk4_layer_shell = .{
.url = "https://deps.files.ghostty.org/gtk4-layer-shell-1.1.0.tar.gz",
.hash = "N-V-__8AALiNBAA-_0gprYr92CjrMj1I5bqNu0TSJOnjFNSr",
.lazy = true,
},
.wayland_protocols = .{
.url = "https://deps.files.ghostty.org/wayland-protocols-258d8f88f2c8c25a830c6316f87d23ce1a0f12d9.tar.gz",
.hash = "N-V-__8AAKw-DAAaV8bOAAGqA0-oD7o-HNIlPFYKRXSPT03S",
.lazy = true,
},
},
}