use lazyDependency to optimize dependency fetching

Helps hexops/mach#1197

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-21 12:02:57 -07:00
parent 1a9a033990
commit ba7fe27109
2 changed files with 11 additions and 10 deletions

View file

@ -5,18 +5,11 @@ pub fn build(b: *std.Build) !void {
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const glfw_dep = b.dependency("glfw", .{
.target = target,
.optimize = optimize,
});
var module = b.addModule("mach-glfw", .{ var module = b.addModule("mach-glfw", .{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.root_source_file = .{ .path = "src/main.zig" }, .root_source_file = .{ .path = "src/main.zig" },
}); });
module.linkLibrary(glfw_dep.artifact("glfw"));
@import("glfw").addPaths(module);
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
const main_tests = b.addTest(.{ const main_tests = b.addTest(.{
@ -25,11 +18,18 @@ pub fn build(b: *std.Build) !void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
main_tests.linkLibrary(glfw_dep.artifact("glfw"));
@import("glfw").addPaths(&main_tests.root_module);
b.installArtifact(main_tests); b.installArtifact(main_tests);
test_step.dependOn(&b.addRunArtifact(main_tests).step); test_step.dependOn(&b.addRunArtifact(main_tests).step);
if (b.lazyDependency("glfw", .{
.target = target,
.optimize = optimize,
})) |dep| {
module.linkLibrary(dep.artifact("glfw"));
@import("glfw").addPaths(module);
main_tests.linkLibrary(dep.artifact("glfw"));
@import("glfw").addPaths(&main_tests.root_module);
}
} }
comptime { comptime {

View file

@ -14,6 +14,7 @@
.glfw = .{ .glfw = .{
.url = "https://pkg.machengine.org/glfw/8f7ca982913c0ab64df44d79d6ad2fb592e5ec39.tar.gz", .url = "https://pkg.machengine.org/glfw/8f7ca982913c0ab64df44d79d6ad2fb592e5ec39.tar.gz",
.hash = "122085745c053e68fea3772c7239c7c87bcc7f042fa3189592b30e0e39cbb9603d79", .hash = "122085745c053e68fea3772c7239c7c87bcc7f042fa3189592b30e0e39cbb9603d79",
.lazy = true,
}, },
}, },
} }