diff --git a/build.zig b/build.zig index 9d3fc1641..5a998bdbd 100644 --- a/build.zig +++ b/build.zig @@ -444,8 +444,8 @@ pub fn build(b: *std.Build) !void { b.installFile("images/icons/icon_256x256@2x@2x.png", "share/icons/hicolor/256x256@2/apps/com.mitchellh.ghostty.png"); } - // On Mac we can build the embedding library. - if (builtin.target.isDarwin() and target.result.isDarwin()) { + // On Mac we can build the embedding library. This only handles the macOS lib. + if (builtin.target.isDarwin() and target.result.os.tag == .macos) { const static_lib_aarch64 = lib: { const lib = b.addStaticLibrary(.{ .name = "ghostty", @@ -792,7 +792,7 @@ fn addDeps( // We always require the system SDK so that our system headers are available. // This makes things like `os/log.h` available for cross-compiling. if (step.rootModuleTarget().isDarwin()) { - try @import("apple_sdk").addPaths(b, step); + try @import("apple_sdk").addPaths(b, &step.root_module); } // We always need the Zig packages diff --git a/nix/zigCacheHash.nix b/nix/zigCacheHash.nix index 0e4626c77..7b0bd5cb5 100644 --- a/nix/zigCacheHash.nix +++ b/nix/zigCacheHash.nix @@ -1,3 +1,3 @@ # This file is auto-generated! check build-support/check-zig-cache-hash.sh for # more details. -"sha256-iRXzPgzOkt+TTcqPCRQubP3dN6lS+Wvn17l+0I/pDGg=" +"sha256-1qlnSOyr2C9DtUdf+4QRNrBr4vUM1nVnv/mVUXxE5fA=" diff --git a/pkg/apple-sdk/build.zig b/pkg/apple-sdk/build.zig index b8863cff2..62f1372c6 100644 --- a/pkg/apple-sdk/build.zig +++ b/pkg/apple-sdk/build.zig @@ -7,12 +7,49 @@ pub fn build(b: *std.Build) !void { _ = optimize; } -pub fn addPaths(b: *std.Build, step: *std.Build.Step.Compile) !void { - _ = b; - @import("macos_sdk").addPaths(step); +/// Add the SDK framework, include, and library paths to the given module. +/// The module target is used to determine the SDK to use so it must have +/// a resolved target. +pub fn addPaths(b: *std.Build, m: *std.Build.Module) !void { + // The active SDK we want to use + const sdk = try SDK.fromTarget(m.resolved_target.?.result); + + // Get the path to our active Xcode installation. If this fails then + // the zig build will fail. We store this in a struct variable so its + // static and only calculated once per build. + const Path = struct { + var value: ?[]const u8 = null; + }; + const path = Path.value orelse path: { + const path = std.mem.trim(u8, b.run(&.{ "xcode-select", "--print-path" }), " \r\n"); + Path.value = path; + break :path path; + }; + + // Base path + const base = b.fmt("{s}/Platforms/{s}.platform/Developer/SDKs/{s}{s}.sdk", .{ + path, + sdk.platform, + sdk.platform, + sdk.version, + }); + + m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ base, "/System/Library/Frameworks" }) }); + m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ base, "/usr/include" }) }); + m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ base, "/usr/lib" }) }); } -pub fn addPathsModule(b: *std.Build, m: *std.Build.Module) !void { - _ = b; - @import("macos_sdk").addPathsModule(m); -} +const SDK = struct { + platform: []const u8, + version: []const u8, + + pub fn fromTarget(target: std.Target) !SDK { + return switch (target.os.tag) { + .macos => .{ .platform = "MacOSX", .version = "14" }, + else => { + std.log.err("unsupported os={}", .{target.os.tag}); + return error.UnsupportedOS; + }, + }; + } +}; diff --git a/pkg/apple-sdk/build.zig.zon b/pkg/apple-sdk/build.zig.zon index b41c9ac0d..226c3b088 100644 --- a/pkg/apple-sdk/build.zig.zon +++ b/pkg/apple-sdk/build.zig.zon @@ -1,10 +1,5 @@ .{ .name = "apple-sdk", .version = "0.1.0", - .dependencies = .{ - .macos_sdk = .{ - .url = "https://github.com/mitchellh/zig-build-macos-sdk/archive/ee70f27c08680307fa35ada92e6b2c36e0ff84c6.tar.gz", - .hash = "1220b415f529f1c04ed876c2b481e9f8119d353d4e3d4d7c8607ee302d2142e13eca", - }, - }, + .dependencies = .{}, } diff --git a/pkg/cimgui/build.zig b/pkg/cimgui/build.zig index d37cfc538..34585bac9 100644 --- a/pkg/cimgui/build.zig +++ b/pkg/cimgui/build.zig @@ -5,7 +5,11 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const module = b.addModule("cimgui", .{ .root_source_file = .{ .path = "main.zig" } }); + const module = b.addModule("cimgui", .{ + .root_source_file = .{ .path = "main.zig" }, + .target = target, + .optimize = optimize, + }); const imgui = b.dependency("imgui", .{}); const freetype = b.dependency("freetype", .{ @@ -60,8 +64,8 @@ pub fn build(b: *std.Build) !void { if (target.result.isDarwin()) { if (!target.query.isNative()) { - try @import("apple_sdk").addPaths(b, lib); - try @import("apple_sdk").addPathsModule(b, module); + try @import("apple_sdk").addPaths(b, &lib.root_module); + try @import("apple_sdk").addPaths(b, module); } lib.addCSourceFile(.{ .file = imgui.path("backends/imgui_impl_metal.mm"), diff --git a/pkg/harfbuzz/build.zig b/pkg/harfbuzz/build.zig index 25d1ff00b..280b66a57 100644 --- a/pkg/harfbuzz/build.zig +++ b/pkg/harfbuzz/build.zig @@ -18,6 +18,8 @@ pub fn build(b: *std.Build) !void { const module = b.addModule("harfbuzz", .{ .root_source_file = .{ .path = "main.zig" }, + .target = target, + .optimize = optimize, .imports = &.{ .{ .name = "freetype", .module = freetype.module("freetype") }, .{ .name = "macos", .module = macos.module("macos") }, @@ -59,10 +61,10 @@ pub fn build(b: *std.Build) !void { "-DHAVE_FT_DONE_MM_VAR=1", "-DHAVE_FT_GET_TRANSFORM=1", }); - if (coretext_enabled) { + if (coretext_enabled and target.result.isDarwin()) { try flags.appendSlice(&.{"-DHAVE_CORETEXT=1"}); - try apple_sdk.addPaths(b, lib); - try apple_sdk.addPathsModule(b, module); + try apple_sdk.addPaths(b, &lib.root_module); + try apple_sdk.addPaths(b, module); lib.linkFramework("ApplicationServices"); module.linkFramework("ApplicationServices", .{}); } diff --git a/pkg/macos/build.zig b/pkg/macos/build.zig index 05035aa6a..3891553f2 100644 --- a/pkg/macos/build.zig +++ b/pkg/macos/build.zig @@ -6,7 +6,11 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); - const module = b.addModule("macos", .{ .root_source_file = .{ .path = "main.zig" } }); + const module = b.addModule("macos", .{ + .root_source_file = .{ .path = "main.zig" }, + .target = target, + .optimize = optimize, + }); const lib = b.addStaticLibrary(.{ .name = "macos", @@ -36,11 +40,11 @@ pub fn build(b: *std.Build) !void { module.linkFramework("CoreGraphics", .{}); module.linkFramework("CoreText", .{}); module.linkFramework("CoreVideo", .{}); - } - if (!target.query.isNative()) { - try apple_sdk.addPaths(b, lib); - try apple_sdk.addPathsModule(b, module); + if (!target.query.isNative()) { + try apple_sdk.addPaths(b, &lib.root_module); + try apple_sdk.addPaths(b, module); + } } b.installArtifact(lib);