freetype: update to latest Zig build API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-02-08 01:10:51 -07:00 committed by Stephen Gutekanst
parent 8c72c124e2
commit a2eb384eaf
2 changed files with 72 additions and 52 deletions

View file

@ -26,9 +26,9 @@ Then in your `build.zig` add:
... ...
const freetype = @import("libs/mach-freetype/build.zig"); const freetype = @import("libs/mach-freetype/build.zig");
pub fn build(b: *Builder) void { pub fn build(b: *Build) void {
... ...
exe.addPackage(freetype.pkg); exe.addModule("freetype", freetype.module(b));
freetype.link(b, exe, .{}); freetype.link(b, exe, .{});
// use this option if you are including zlib separately // use this option if you are including zlib separately
@ -39,14 +39,14 @@ pub fn build(b: *Builder) void {
and optionaly add harfbuzz: and optionaly add harfbuzz:
```zig ```zig
exe.addPackage(freetype.harfbuzz_pkg); exe.addModule("harfbuzz", freetype.harfbuzzModule(b));
freetype.link(b, exe, .{ .harfbuzz = .{} }); freetype.link(b, exe, .{ .harfbuzz = .{} });
``` ```
You can also optionally build brotli compression (for WOFF2 font support): You can also optionally build brotli compression (for WOFF2 font support):
```zig ```zig
exe.addPackage(freetype.pkg); exe.addModule("freetype", freetype.module(b));
freetype.link(b, exe, .{ .freetype = .{ .brotli = true } }); freetype.link(b, exe, .{ .freetype = .{ .brotli = true } });
``` ```
@ -65,10 +65,10 @@ Then in your `build.zig` add:
const pkgs = @import("deps.zig").pkgs; const pkgs = @import("deps.zig").pkgs;
const freetype = @import("build-freetype"); const freetype = @import("build-freetype");
pub fn build(b: *Builder) void { pub fn build(b: *Build) void {
... ...
exe.addPackage(pkgs.freetype); exe.addModule("freetype", pkgs.freetype);
freetype.link(b, exe, .{}); freetype.link(b, exe, .{});
} }
``` ```

View file

@ -1,5 +1,5 @@
const std = @import("std"); const std = @import("std");
const Builder = std.build.Builder; const Build = std.Build;
const ft_root = sdkPath("/upstream/freetype"); const ft_root = sdkPath("/upstream/freetype");
const ft_include_path = ft_root ++ "/include"; const ft_include_path = ft_root ++ "/include";
@ -7,17 +7,20 @@ const hb_root = sdkPath("/upstream/harfbuzz");
const hb_include_path = hb_root ++ "/src"; const hb_include_path = hb_root ++ "/src";
const brotli_root = sdkPath("/upstream/brotli"); const brotli_root = sdkPath("/upstream/brotli");
pub const pkg = std.build.Pkg{ pub fn module(b: *std.Build) *std.build.Module {
.name = "freetype", return b.createModule(.{
.source = .{ .path = sdkPath("/src/main.zig") }, .source_file = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{}, });
}; }
pub const harfbuzz_pkg = std.build.Pkg{ pub fn harfbuzzModule(b: *std.Build) *std.build.Module {
.name = "harfbuzz", return b.createModule(.{
.source = .{ .path = sdkPath("/src/harfbuzz/main.zig") }, .source_file = .{ .path = sdkPath("/src/harfbuzz/main.zig") },
.dependencies = &.{pkg}, .dependencies = &.{
}; .{ .name = "freetype", .module = module(b) },
},
});
}
pub const Options = struct { pub const Options = struct {
freetype: FreetypeOptions = .{}, freetype: FreetypeOptions = .{},
@ -37,21 +40,24 @@ pub const HarfbuzzOptions = struct {
install_libs: bool = false, install_libs: bool = false,
}; };
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.Build) !void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const test_step = b.step("test", "Run library tests"); const test_step = b.step("test", "Run library tests");
test_step.dependOn(&testStep(b, mode, target).step); test_step.dependOn(&testStep(b, optimize, target).step);
inline for ([_][]const u8{ inline for ([_][]const u8{
"single-glyph", "single-glyph",
"glyph-to-svg", "glyph-to-svg",
}) |example| { }) |example| {
const example_exe = b.addExecutable("example-" ++ example, "examples/" ++ example ++ ".zig"); const example_exe = b.addExecutable(.{
example_exe.setBuildMode(mode); .name = "example-" ++ example,
example_exe.setTarget(target); .root_source_file = .{ .path = "examples/" ++ example ++ ".zig" },
example_exe.addPackage(pkg); .target = target,
.optimize = optimize,
});
example_exe.addModule("freetype", module(b));
link(b, example_exe, .{}); link(b, example_exe, .{});
@ -70,11 +76,15 @@ pub fn build(b: *std.build.Builder) !void {
} }
} }
pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { pub fn testStep(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("freetype-tests", sdkPath("/src/main.zig")); const main_tests = b.addTest(.{
main_tests.setBuildMode(mode); .name = "freetype-tests",
main_tests.setTarget(target); .kind = .test_exe,
main_tests.addPackage(pkg); .root_source_file = .{ .path = sdkPath("/src/main.zig") },
.target = target,
.optimize = optimize,
});
main_tests.addModule("freetype", module(b));
link(b, main_tests, .{ link(b, main_tests, .{
.freetype = .{ .freetype = .{
.brotli = true, .brotli = true,
@ -84,10 +94,14 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget
main_tests.main_pkg_path = sdkPath("/"); main_tests.main_pkg_path = sdkPath("/");
main_tests.install(); main_tests.install();
const harfbuzz_tests = b.addTestExe("harfbuzz-tests", sdkPath("/src/harfbuzz/main.zig")); const harfbuzz_tests = b.addTest(.{
harfbuzz_tests.setBuildMode(mode); .name = "harfbuzz-tests",
harfbuzz_tests.setTarget(target); .kind = .test_exe,
harfbuzz_tests.addPackage(pkg); .root_source_file = .{ .path = sdkPath("/src/harfbuzz/main.zig") },
.target = target,
.optimize = optimize,
});
harfbuzz_tests.addModule("freetype", module(b));
link(b, harfbuzz_tests, .{ link(b, harfbuzz_tests, .{
.freetype = .{ .freetype = .{
.brotli = true, .brotli = true,
@ -102,41 +116,43 @@ pub fn testStep(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget
return main_tests_run; return main_tests_run;
} }
pub fn link(b: *Builder, step: *std.build.LibExeObjStep, options: Options) void { pub fn link(b: *Build, step: *std.build.CompileStep, options: Options) void {
linkFreetype(b, step, options.freetype); linkFreetype(b, step, options.freetype);
if (options.harfbuzz) |harfbuzz_options| if (options.harfbuzz) |harfbuzz_options|
linkHarfbuzz(b, step, harfbuzz_options); linkHarfbuzz(b, step, harfbuzz_options);
} }
pub fn linkFreetype(b: *Builder, step: *std.build.LibExeObjStep, options: FreetypeOptions) void { pub fn linkFreetype(b: *Build, step: *std.build.CompileStep, options: FreetypeOptions) void {
const ft_lib = buildFreetype(b, step.build_mode, step.target, options); const ft_lib = buildFreetype(b, step.optimize, step.target, options);
step.linkLibrary(ft_lib); step.linkLibrary(ft_lib);
step.addIncludePath(ft_include_path); step.addIncludePath(ft_include_path);
if (options.brotli) { if (options.brotli) {
const brotli_lib = buildBrotli(b, step.build_mode, step.target); const brotli_lib = buildBrotli(b, step.optimize, step.target);
if (options.install_libs) if (options.install_libs)
brotli_lib.install(); brotli_lib.install();
step.linkLibrary(brotli_lib); step.linkLibrary(brotli_lib);
} }
} }
pub fn linkHarfbuzz(b: *Builder, step: *std.build.LibExeObjStep, options: HarfbuzzOptions) void { pub fn linkHarfbuzz(b: *Build, step: *std.build.CompileStep, options: HarfbuzzOptions) void {
const hb_lib = buildHarfbuzz(b, step.build_mode, step.target, options); const hb_lib = buildHarfbuzz(b, step.optimize, step.target, options);
step.linkLibrary(hb_lib); step.linkLibrary(hb_lib);
step.addIncludePath(hb_include_path); step.addIncludePath(hb_include_path);
} }
pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: FreetypeOptions) *std.build.LibExeObjStep { pub fn buildFreetype(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, options: FreetypeOptions) *std.build.CompileStep {
// TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939 // TODO(build-system): https://github.com/hexops/mach/issues/229#issuecomment-1100958939
ensureDependencySubmodule(b.allocator, "upstream") catch unreachable; ensureDependencySubmodule(b.allocator, "upstream") catch unreachable;
const lib = b.addStaticLibrary("freetype", null); const lib = b.addStaticLibrary(.{
.name = "freetype",
.target = target,
.optimize = optimize,
});
lib.defineCMacro("FT2_BUILD_LIBRARY", "1"); lib.defineCMacro("FT2_BUILD_LIBRARY", "1");
if (options.use_system_zlib) { if (options.use_system_zlib) {
lib.defineCMacro("FT_CONFIG_OPTION_SYSTEM_ZLIB", "1"); lib.defineCMacro("FT_CONFIG_OPTION_SYSTEM_ZLIB", "1");
} }
lib.setBuildMode(mode);
lib.setTarget(target);
lib.linkLibC(); lib.linkLibC();
lib.addIncludePath(ft_include_path); lib.addIncludePath(ft_include_path);
@ -169,11 +185,13 @@ pub fn buildFreetype(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossT
return lib; return lib;
} }
pub fn buildHarfbuzz(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: HarfbuzzOptions) *std.build.LibExeObjStep { pub fn buildHarfbuzz(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, options: HarfbuzzOptions) *std.build.CompileStep {
const main_abs = hb_root ++ "/src/harfbuzz.cc"; const lib = b.addStaticLibrary(.{
const lib = b.addStaticLibrary("harfbuzz", main_abs); .name = "harfbuzz",
lib.setBuildMode(mode); .root_source_file = .{ .path = hb_root ++ "/src/harfbuzz.cc" },
lib.setTarget(target); .target = target,
.optimize = optimize,
});
lib.linkLibCpp(); lib.linkLibCpp();
lib.addIncludePath(hb_include_path); lib.addIncludePath(hb_include_path);
lib.addIncludePath(ft_include_path); lib.addIncludePath(ft_include_path);
@ -184,10 +202,12 @@ pub fn buildHarfbuzz(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossT
return lib; return lib;
} }
fn buildBrotli(b: *Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.LibExeObjStep { fn buildBrotli(b: *Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.CompileStep {
const lib = b.addStaticLibrary("brotli", null); const lib = b.addStaticLibrary(.{
lib.setBuildMode(mode); .name = "brotli",
lib.setTarget(target); .target = target,
.optimize = optimize,
});
lib.linkLibC(); lib.linkLibC();
lib.addIncludePath(brotli_root ++ "/include"); lib.addIncludePath(brotli_root ++ "/include");
lib.addCSourceFiles(brotli_base_sources, &.{}); lib.addCSourceFiles(brotli_base_sources, &.{});