Skip to content

Commit

Permalink
use lazyDependency to optimize dependency fetching
Browse files Browse the repository at this point in the history
Helps hexops/mach#1197

Signed-off-by: Stephen Gutekanst <[email protected]>
  • Loading branch information
slimsag committed Apr 20, 2024
1 parent 100c7f6 commit 082cb06
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
52 changes: 28 additions & 24 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,19 @@ pub fn build(b: *std.Build) !void {
const use_system_zlib = b.option(bool, "use_system_zlib", "Use system zlib") orelse false;
const enable_brotli = b.option(bool, "enable_brotli", "Build brotli") orelse true;

const font_assets_dep = b.dependency("font_assets", .{});
const freetype_dep = b.dependency("freetype", .{
.target = target,
.optimize = optimize,
.use_system_zlib = use_system_zlib,
.enable_brotli = enable_brotli,
});
const harfbuzz_dep = b.dependency("harfbuzz", .{
.target = target,
.optimize = optimize,
.enable_freetype = true,
.freetype_use_system_zlib = use_system_zlib,
.freetype_enable_brotli = enable_brotli,
});

const freetype_module = b.addModule("mach-freetype", .{ .root_source_file = .{ .path = "src/freetype.zig" } });
freetype_module.linkLibrary(freetype_dep.artifact("freetype"));

const harfbuzz_module = b.addModule("mach-harfbuzz", .{
.root_source_file = .{ .path = "src/harfbuzz.zig" },
.imports = &.{.{ .name = "freetype", .module = freetype_module }},
});
harfbuzz_module.linkLibrary(freetype_dep.artifact("freetype"));
harfbuzz_module.linkLibrary(harfbuzz_dep.artifact("harfbuzz"));

const freetype_tests = b.addTest(.{
.name = "freetype-tests",
.root_source_file = .{ .path = "src/freetype.zig" },
.target = target,
.optimize = optimize,
});
freetype_tests.root_module.addImport("font-assets", font_assets_dep.module("font-assets"));
freetype_tests.root_module.addImport("freetype", freetype_module);
freetype_tests.root_module.linkLibrary(freetype_dep.artifact("freetype"));

const harfbuzz_tests = b.addTest(.{
.name = "harfbuzz-tests",
Expand All @@ -49,8 +28,31 @@ pub fn build(b: *std.Build) !void {
});
harfbuzz_tests.root_module.addImport("freetype", freetype_module);
harfbuzz_tests.root_module.addImport("harfbuzz", harfbuzz_module);
harfbuzz_tests.root_module.linkLibrary(harfbuzz_dep.artifact("harfbuzz"));
harfbuzz_tests.root_module.linkLibrary(freetype_dep.artifact("freetype"));

if (b.lazyDependency("font_assets", .{})) |dep| {
freetype_tests.root_module.addImport("font-assets", dep.module("font-assets"));
}
if (b.lazyDependency("freetype", .{
.target = target,
.optimize = optimize,
.use_system_zlib = use_system_zlib,
.enable_brotli = enable_brotli,
})) |dep| {
freetype_tests.root_module.linkLibrary(dep.artifact("freetype"));
freetype_module.linkLibrary(dep.artifact("freetype"));
harfbuzz_module.linkLibrary(dep.artifact("freetype"));
harfbuzz_tests.root_module.linkLibrary(dep.artifact("freetype"));
}
if (b.lazyDependency("harfbuzz", .{
.target = target,
.optimize = optimize,
.enable_freetype = true,
.freetype_use_system_zlib = use_system_zlib,
.freetype_enable_brotli = enable_brotli,
})) |dep| {
harfbuzz_module.linkLibrary(dep.artifact("harfbuzz"));
harfbuzz_tests.root_module.linkLibrary(dep.artifact("harfbuzz"));
}

const test_step = b.step("test", "Run library tests");
test_step.dependOn(&b.addRunArtifact(freetype_tests).step);
Expand All @@ -66,8 +68,10 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
example_exe.root_module.addImport("font-assets", font_assets_dep.module("font-assets"));
example_exe.root_module.addImport("freetype", freetype_module);
if (b.lazyDependency("font_assets", .{})) |dep| {
example_exe.root_module.addImport("font-assets", dep.module("font-assets"));
}

const example_run_cmd = b.addRunArtifact(example_exe);
if (b.args) |args| example_run_cmd.addArgs(args);
Expand Down
11 changes: 7 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
},
.dependencies = .{
.freetype = .{
.url = "https://pkg.machengine.org/freetype/a62aa6402fa0857723ac5556ddee98f038aec4e5.tar.gz",
.hash = "12209614e8bc8a6bde561b9c24b27a37f4ede244dba598b345cc56199fa7c6201f01",
.url = "https://pkg.machengine.org/freetype/ad7313db50a6bd29121d587effb3b308e21a20fd.tar.gz",
.hash = "122016f743edf65f5c0569f7ad06509218688e101c66f2c094d5d9400326945bcb90",
.lazy = true,
},
.harfbuzz = .{
.url = "https://pkg.machengine.org/harfbuzz/6560e4534509fc40dd1b28dc74d26d562fcb27bf.tar.gz",
.hash = "12203bd6464c3fe9a48fbc2d1444f932aacbc5bb19576860e170fcfe89d5884e7f80",
.url = "https://pkg.machengine.org/harfbuzz/ab0ce2cee88a03fb8a00aca9945ce7623f0544ed.tar.gz",
.hash = "1220d001c200dc5839f1b4302d12a699c7398498980f019671432ef9b0a1c22d85d7",
.lazy = true,
},
.font_assets = .{
.url = "https://pkg.machengine.org/font-assets/7977df057e855140a207de49039114f1ab8e6c2d.tar.gz",
.hash = "12208106eef051bc730bac17c2d10f7e42ea63b579b919480fec86b7c75a620c75d4",
.lazy = true,
},
},
}

0 comments on commit 082cb06

Please sign in to comment.