pkg/apple-sdk: use std.zig.system.darwin.getSdk

This commit is contained in:
Mitchell Hashimoto
2024-01-26 08:31:30 -08:00
parent f98b5b7417
commit 2f5f14df9f

View File

@ -11,18 +11,36 @@ pub fn build(b: *std.Build) !void {
/// The module target is used to determine the SDK to use so it must have /// The module target is used to determine the SDK to use so it must have
/// a resolved target. /// a resolved target.
pub fn addPaths(b: *std.Build, m: *std.Build.Module) !void { pub fn addPaths(b: *std.Build, m: *std.Build.Module) !void {
// Get the path to our active SDK installation. If this fails then // The cache. This always uses b.allocator and never frees memory
// the zig build will fail. We store this in a struct variable so its // (which is idiomatic for a Zig build exe).
// static and only calculated once per build. const Cache = struct {
const Path = struct { const Key = struct {
var value: ?[]const u8 = null; arch: std.Target.Cpu.Arch,
}; os: std.Target.Os.Tag,
const path = Path.value orelse path: { abi: std.Target.Abi,
const path = std.zig.system.darwin.getSdk(b.allocator, m.resolved_target.?.result) orelse ""; };
Path.value = path;
break :path path; var map: std.AutoHashMapUnmanaged(Key, ?[]const u8) = .{};
}; };
const target = m.resolved_target.?.result;
const gop = try Cache.map.getOrPut(b.allocator, .{
.arch = target.cpu.arch,
.os = target.os.tag,
.abi = target.abi,
});
// This executes `xcrun` to get the SDK path. We don't want to execute
// this multiple times so we cache the value.
if (!gop.found_existing) {
gop.value_ptr.* = std.zig.system.darwin.getSdk(
b.allocator,
m.resolved_target.?.result,
);
}
// The active SDK we want to use // The active SDK we want to use
const path = gop.value_ptr.* orelse return error.AppleSDKNotFound;
m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/System/Library/Frameworks" }) }); m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/System/Library/Frameworks" }) });
m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/include" }) }); m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/include" }) });
m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/lib" }) }); m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/lib" }) });