From f98b5b741771ba35c09ccfb1c2247da12b7d517d Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Fri, 26 Jan 2024 12:04:49 +0100 Subject: [PATCH 1/2] apple-sdk: use zig's libstd getSdk to get active Apple SDK --- pkg/apple-sdk/build.zig | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/pkg/apple-sdk/build.zig b/pkg/apple-sdk/build.zig index ffb1671da..1e21c5401 100644 --- a/pkg/apple-sdk/build.zig +++ b/pkg/apple-sdk/build.zig @@ -11,46 +11,19 @@ pub fn build(b: *std.Build) !void { /// 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 + // Get the path to our active SDK 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"); + const path = std.zig.system.darwin.getSdk(b.allocator, m.resolved_target.?.result) orelse ""; 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" }) }); + // The active SDK we want to use + m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/System/Library/Frameworks" }) }); + m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/include" }) }); + m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/lib" }) }); } - -const SDK = struct { - platform: []const u8, - version: []const u8, - - pub fn fromTarget(target: std.Target) !SDK { - return switch (target.os.tag) { - .ios => .{ .platform = "iPhoneOS", .version = "" }, - .macos => .{ .platform = "MacOSX", .version = "14" }, - else => { - std.log.err("unsupported os={}", .{target.os.tag}); - return error.UnsupportedOS; - }, - }; - } -}; From 2f5f14df9fb6f09d7661a728644d361950e1d056 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 26 Jan 2024 08:31:30 -0800 Subject: [PATCH 2/2] pkg/apple-sdk: use std.zig.system.darwin.getSdk --- pkg/apple-sdk/build.zig | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/pkg/apple-sdk/build.zig b/pkg/apple-sdk/build.zig index 1e21c5401..580c0de3c 100644 --- a/pkg/apple-sdk/build.zig +++ b/pkg/apple-sdk/build.zig @@ -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 /// a resolved target. pub fn addPaths(b: *std.Build, m: *std.Build.Module) !void { - // Get the path to our active SDK 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.zig.system.darwin.getSdk(b.allocator, m.resolved_target.?.result) orelse ""; - Path.value = path; - break :path path; + // The cache. This always uses b.allocator and never frees memory + // (which is idiomatic for a Zig build exe). + const Cache = struct { + const Key = struct { + arch: std.Target.Cpu.Arch, + os: std.Target.Os.Tag, + abi: std.Target.Abi, + }; + + 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 + const path = gop.value_ptr.* orelse return error.AppleSDKNotFound; m.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/System/Library/Frameworks" }) }); m.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/include" }) }); m.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ path, "/usr/lib" }) });