diff --git a/pkg/apple-sdk/build.zig b/pkg/apple-sdk/build.zig new file mode 100644 index 000000000..85a3326ac --- /dev/null +++ b/pkg/apple-sdk/build.zig @@ -0,0 +1,141 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const lib = b.addStaticLibrary(.{ + .name = "stub", + .target = target, + .optimize = optimize, + }); + lib.linkLibC(); + lib.addCSourceFiles(&.{"stub.c"}, &.{}); + try addPaths(b, lib); + + b.installArtifact(lib); +} + +/// Add the necessary paths to the given compilation step for the Xcode SDK. +/// +/// This is required to workaround a Zig issue where we can't depend directly +/// on the hexops/xcode-frameworks repository: https://github.com/ziglang/zig/pull/15382 +/// +/// This is copied and adapted from hexops/mach. I modified it slightly +/// for my own personal taste (nothing wrong with theirs!), but the logic +/// is effectively identical. +pub fn addPaths(b: *std.Build, step: *std.build.CompileStep) !void { + // branch: mach + try ensureGitRepoCloned( + b.allocator, + // WARNING: forked temporarily for https://github.com/hexops/xcode-frameworks/issues/4 + "https://github.com/mitchellh/xcode-frameworks", + "983deb1ab8d03861db30ea297cc78e6d121da4db", + xSdkPath("/zig-cache/xcode_frameworks"), + ); + + // This may be a bug in Zig. + b.sysroot = xSdkPath("/zig-cache/xcode_frameworks"); + + step.addSystemFrameworkPath(.{ .path = xSdkPath("/zig-cache/xcode_frameworks/Frameworks") }); + step.addSystemIncludePath(.{ .path = xSdkPath("/zig-cache/xcode_frameworks/include") }); + step.addLibraryPath(.{ .path = xSdkPath("/zig-cache/xcode_frameworks/lib") }); +} + +fn ensureGitRepoCloned( + allocator: std.mem.Allocator, + clone_url: []const u8, + revision: []const u8, + dir: []const u8, +) !void { + if (envVarIsTruthy(allocator, "NO_ENSURE_SUBMODULES") or + envVarIsTruthy(allocator, "NO_ENSURE_GIT")) return; + + ensureGit(allocator); + + if (std.fs.openDirAbsolute(dir, .{})) |_| { + const current_revision = try currentGitRevision(allocator, dir); + if (!std.mem.eql(u8, current_revision, revision)) { + // Reset to the desired revision + exec( + allocator, + &[_][]const u8{ "git", "fetch" }, + dir, + ) catch |err| std.debug.print( + "warning: failed to 'git fetch' in {s}: {s}\n", + .{ dir, @errorName(err) }, + ); + try exec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, dir); + try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir); + } + return; + } else |err| return switch (err) { + error.FileNotFound => { + std.log.info("cloning required dependency..\ngit clone {s} {s}..\n", .{ clone_url, dir }); + try exec(allocator, &[_][]const u8{ "git", "clone", "-c", "core.longpaths=true", clone_url, dir }, "."); + try exec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, dir); + try exec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir); + return; + }, + else => err, + }; +} + +fn exec( + allocator: std.mem.Allocator, + argv: []const []const u8, + cwd: []const u8, +) !void { + var child = std.ChildProcess.init(argv, allocator); + child.cwd = cwd; + _ = try child.spawnAndWait(); +} + +fn currentGitRevision(allocator: std.mem.Allocator, cwd: []const u8) ![]const u8 { + const result = try std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = &.{ "git", "rev-parse", "HEAD" }, + .cwd = cwd, + }); + allocator.free(result.stderr); + if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline + return result.stdout; +} + +fn ensureGit(allocator: std.mem.Allocator) void { + const argv = &[_][]const u8{ "git", "--version" }; + const result = std.ChildProcess.exec(.{ + .allocator = allocator, + .argv = argv, + .cwd = ".", + }) catch { // e.g. FileNotFound + std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{}); + std.process.exit(1); + }; + defer { + allocator.free(result.stderr); + allocator.free(result.stdout); + } + if (result.term.Exited != 0) { + std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{}); + std.process.exit(1); + } +} + +fn envVarIsTruthy(allocator: std.mem.Allocator, name: []const u8) bool { + if (std.process.getEnvVarOwned(allocator, name)) |truthy| { + defer allocator.free(truthy); + if (std.mem.eql(u8, truthy, "true")) return true; + return false; + } else |_| { + return false; + } +} + +fn xSdkPath(comptime suffix: []const u8) []const u8 { + if (suffix[0] != '/') @compileError("suffix must be an absolute path"); + return comptime blk: { + const root_dir = std.fs.path.dirname(@src().file) orelse "."; + break :blk root_dir ++ suffix; + }; +} diff --git a/pkg/apple-sdk/stub.c b/pkg/apple-sdk/stub.c new file mode 100644 index 000000000..ce34f4ab0 --- /dev/null +++ b/pkg/apple-sdk/stub.c @@ -0,0 +1 @@ +// Blank on purpose diff --git a/pkg/harfbuzz/build.zig b/pkg/harfbuzz/build.zig index 161489b7a..b9eeab26d 100644 --- a/pkg/harfbuzz/build.zig +++ b/pkg/harfbuzz/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const apple_sdk = @import("apple_sdk"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); @@ -42,7 +43,11 @@ pub fn build(b: *std.Build) !void { "-DHAVE_FT_DONE_MM_VAR=1", "-DHAVE_FT_GET_TRANSFORM=1", }); - if (coretext_enabled) try flags.appendSlice(&.{"-DHAVE_CORETEXT=1"}); + if (coretext_enabled) { + try flags.appendSlice(&.{"-DHAVE_CORETEXT=1"}); + try apple_sdk.addPaths(b, lib); + lib.linkFramework("ApplicationServices"); + } lib.addCSourceFile(.{ .file = .{ .path = upstream_root ++ "/src/harfbuzz.cc" }, diff --git a/pkg/harfbuzz/build.zig.zon b/pkg/harfbuzz/build.zig.zon index 96112ae42..2f5c08dba 100644 --- a/pkg/harfbuzz/build.zig.zon +++ b/pkg/harfbuzz/build.zig.zon @@ -3,5 +3,6 @@ .version = "2.13.2", .dependencies = .{ .freetype = .{ .path = "../freetype" }, + .apple_sdk = .{ .path = "../apple-sdk" }, }, } diff --git a/pkg/harfbuzz/libharfbuzz.a b/pkg/harfbuzz/libharfbuzz.a new file mode 100644 index 000000000..6226b2c02 Binary files /dev/null and b/pkg/harfbuzz/libharfbuzz.a differ