Merge pull request #1273 from mitchellh/macos-sdk

build: use Xcode to find macOS SDK
This commit is contained in:
Mitchell Hashimoto
2024-01-10 13:53:16 -08:00
committed by GitHub
7 changed files with 70 additions and 28 deletions

View File

@ -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

View File

@ -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="

View File

@ -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;
},
};
}
};

View File

@ -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 = .{},
}

View File

@ -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"),

View File

@ -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", .{});
}

View File

@ -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);